tracy/server/TracyTexture.cpp

188 lines
5.2 KiB
C++
Raw Permalink Normal View History

2021-08-22 11:38:28 +00:00
#include <inttypes.h>
2022-10-15 11:06:08 +00:00
#include <string.h>
2021-08-22 11:38:28 +00:00
2022-09-29 22:39:42 +00:00
#ifdef __EMSCRIPTEN__
2022-10-15 10:56:19 +00:00
# include <emscripten/html5.h>
2022-09-29 22:39:42 +00:00
# include <GLES2/gl2.h>
#else
# include "../profiler/src/imgui/imgui_impl_opengl3_loader.h"
#endif
2019-06-06 20:07:56 +00:00
#include "TracyTexture.hpp"
2022-10-15 11:06:08 +00:00
#include "../public/common/TracyForceInline.hpp"
2019-06-06 20:07:56 +00:00
#ifndef COMPRESSED_RGB_S3TC_DXT1_EXT
# define COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
#endif
2019-06-06 20:07:56 +00:00
namespace tracy
{
2022-10-15 10:56:19 +00:00
static bool s_hardwareS3tc;
void InitTexture()
{
#ifdef __EMSCRIPTEN__
s_hardwareS3tc = emscripten_webgl_enable_extension( emscripten_webgl_get_current_context(), "WEBGL_compressed_texture_s3tc" );
#else
s_hardwareS3tc = false;
GLint num;
glGetIntegerv( GL_NUM_EXTENSIONS, &num );
for( GLint i=0; i<num; i++ )
{
auto ext = (const char*)glGetStringi( GL_EXTENSIONS, GLuint( i ) );
if( strcmp( ext, "GL_EXT_texture_compression_s3tc" ) == 0 )
{
s_hardwareS3tc = true;
break;
}
}
#endif
}
2023-04-27 19:39:49 +00:00
void* MakeTexture( bool zigzag )
2019-06-06 20:07:56 +00:00
{
GLuint tex;
glGenTextures( 1, &tex );
glBindTexture( GL_TEXTURE_2D, tex );
2023-04-27 19:39:49 +00:00
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, zigzag ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
2023-04-27 19:39:49 +00:00
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, zigzag ? GL_REPEAT : GL_CLAMP_TO_EDGE );
2020-08-13 16:16:10 +00:00
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
2019-06-06 23:03:28 +00:00
return (void*)(intptr_t)tex;
2019-06-06 20:07:56 +00:00
}
void FreeTexture( void* _tex, void(*runOnMainThread)(const std::function<void()>&, bool) )
2019-06-06 20:07:56 +00:00
{
2019-06-06 23:03:28 +00:00
auto tex = (GLuint)(intptr_t)_tex;
runOnMainThread( [tex] { glDeleteTextures( 1, &tex ); }, false );
2019-06-06 20:07:56 +00:00
}
2022-10-15 11:06:08 +00:00
static tracy_force_inline void DecodeDxt1Part( uint64_t d, uint32_t* dst, uint32_t w )
{
uint8_t* in = (uint8_t*)&d;
uint16_t c0, c1;
uint32_t idx;
memcpy( &c0, in, 2 );
memcpy( &c1, in+2, 2 );
memcpy( &idx, in+4, 4 );
uint8_t r0 = ( ( c0 & 0xF800 ) >> 8 ) | ( ( c0 & 0xF800 ) >> 13 );
uint8_t g0 = ( ( c0 & 0x07E0 ) >> 3 ) | ( ( c0 & 0x07E0 ) >> 9 );
uint8_t b0 = ( ( c0 & 0x001F ) << 3 ) | ( ( c0 & 0x001F ) >> 2 );
uint8_t r1 = ( ( c1 & 0xF800 ) >> 8 ) | ( ( c1 & 0xF800 ) >> 13 );
uint8_t g1 = ( ( c1 & 0x07E0 ) >> 3 ) | ( ( c1 & 0x07E0 ) >> 9 );
uint8_t b1 = ( ( c1 & 0x001F ) << 3 ) | ( ( c1 & 0x001F ) >> 2 );
uint32_t dict[4];
dict[0] = 0xFF000000 | ( b0 << 16 ) | ( g0 << 8 ) | r0;
dict[1] = 0xFF000000 | ( b1 << 16 ) | ( g1 << 8 ) | r1;
uint32_t r, g, b;
if( c0 > c1 )
{
r = (2*r0+r1)/3;
g = (2*g0+g1)/3;
b = (2*b0+b1)/3;
dict[2] = 0xFF000000 | ( b << 16 ) | ( g << 8 ) | r;
r = (2*r1+r0)/3;
g = (2*g1+g0)/3;
b = (2*b1+b0)/3;
dict[3] = 0xFF000000 | ( b << 16 ) | ( g << 8 ) | r;
}
else
{
r = (int(r0)+r1)/2;
g = (int(g0)+g1)/2;
b = (int(b0)+b1)/2;
dict[2] = 0xFF000000 | ( b << 16 ) | ( g << 8 ) | r;
dict[3] = 0xFF000000;
}
memcpy( dst+0, dict + (idx & 0x3), 4 );
idx >>= 2;
memcpy( dst+1, dict + (idx & 0x3), 4 );
idx >>= 2;
memcpy( dst+2, dict + (idx & 0x3), 4 );
idx >>= 2;
memcpy( dst+3, dict + (idx & 0x3), 4 );
idx >>= 2;
dst += w;
memcpy( dst+0, dict + (idx & 0x3), 4 );
idx >>= 2;
memcpy( dst+1, dict + (idx & 0x3), 4 );
idx >>= 2;
memcpy( dst+2, dict + (idx & 0x3), 4 );
idx >>= 2;
memcpy( dst+3, dict + (idx & 0x3), 4 );
idx >>= 2;
dst += w;
memcpy( dst+0, dict + (idx & 0x3), 4 );
idx >>= 2;
memcpy( dst+1, dict + (idx & 0x3), 4 );
idx >>= 2;
memcpy( dst+2, dict + (idx & 0x3), 4 );
idx >>= 2;
memcpy( dst+3, dict + (idx & 0x3), 4 );
idx >>= 2;
dst += w;
memcpy( dst+0, dict + (idx & 0x3), 4 );
idx >>= 2;
memcpy( dst+1, dict + (idx & 0x3), 4 );
idx >>= 2;
memcpy( dst+2, dict + (idx & 0x3), 4 );
idx >>= 2;
memcpy( dst+3, dict + (idx & 0x3), 4 );
}
2019-08-15 14:29:50 +00:00
void UpdateTexture( void* _tex, const char* data, int w, int h )
2019-06-06 20:07:56 +00:00
{
2019-06-06 23:03:28 +00:00
auto tex = (GLuint)(intptr_t)_tex;
2019-06-06 20:07:56 +00:00
glBindTexture( GL_TEXTURE_2D, tex );
2022-10-15 10:56:19 +00:00
if( s_hardwareS3tc )
{
glCompressedTexImage2D( GL_TEXTURE_2D, 0, COMPRESSED_RGB_S3TC_DXT1_EXT, w, h, 0, w * h / 2, data );
}
2022-10-15 11:06:08 +00:00
else
{
auto tmp = new uint32_t[w*h];
auto src = (const uint64_t*)data;
auto dst = tmp;
for( int y=0; y<h/4; y++ )
{
for( int x=0; x<w/4; x++ )
{
uint64_t d = *src++;
DecodeDxt1Part( d, dst, w );
dst += 4;
}
dst += w*3;
}
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmp );
delete[] tmp;
}
2019-06-06 20:07:56 +00:00
}
2022-07-25 23:49:19 +00:00
void UpdateTextureRGBA( void* _tex, void* data, int w, int h )
{
auto tex = (GLuint)(intptr_t)_tex;
glBindTexture( GL_TEXTURE_2D, tex );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data );
}
2023-04-29 11:10:58 +00:00
void UpdateTextureRGBAMips( void* _tex, void** data, int* w, int* h, size_t mips )
2023-04-27 19:58:48 +00:00
{
auto tex = (GLuint)(intptr_t)_tex;
glBindTexture( GL_TEXTURE_2D, tex );
2023-04-29 11:10:58 +00:00
for( size_t i=0; i<mips; i++ )
{
glTexImage2D( GL_TEXTURE_2D, i, GL_RGBA, w[i], h[i], 0, GL_RGBA, GL_UNSIGNED_BYTE, data[i] );
}
2023-04-27 19:58:48 +00:00
}
2019-06-06 20:07:56 +00:00
}