1
0
mirror of https://github.com/wolfpld/tracy synced 2025-04-29 04:23:51 +00:00

Add software S3TC decoder.

This commit is contained in:
Bartosz Taudul 2022-10-15 13:06:08 +02:00
parent fdb130651d
commit dc6c3962d3
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3

View File

@ -1,4 +1,5 @@
#include <inttypes.h>
#include <string.h>
#ifdef __EMSCRIPTEN__
# include <emscripten/html5.h>
@ -7,6 +8,7 @@
# include "../profiler/src/imgui/imgui_impl_opengl3_loader.h"
#endif
#include "TracyTexture.hpp"
#include "../public/common/TracyForceInline.hpp"
#ifndef COMPRESSED_RGB_S3TC_DXT1_EXT
# define COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
@ -55,6 +57,88 @@ void FreeTexture( void* _tex, void(*runOnMainThread)(std::function<void()>, bool
runOnMainThread( [tex] { glDeleteTextures( 1, &tex ); }, false );
}
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 );
}
void UpdateTexture( void* _tex, const char* data, int w, int h )
{
auto tex = (GLuint)(intptr_t)_tex;
@ -63,6 +147,24 @@ void UpdateTexture( void* _tex, const char* data, int w, int h )
{
glCompressedTexImage2D( GL_TEXTURE_2D, 0, COMPRESSED_RGB_S3TC_DXT1_EXT, w, h, 0, w * h / 2, data );
}
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;
}
}
void UpdateTextureRGBA( void* _tex, void* data, int w, int h )