tracy/server/TracyTexture.cpp

76 lines
2.0 KiB
C++
Raw Normal View History

2021-08-22 11:38:28 +00:00
#include <inttypes.h>
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"
#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
}
2019-06-06 20:07:56 +00:00
void* MakeTexture()
{
GLuint tex;
glGenTextures( 1, &tex );
glBindTexture( GL_TEXTURE_2D, tex );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
2020-08-13 16:16:10 +00:00
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
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)(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
}
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 );
}
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 );
}
2019-06-06 20:07:56 +00:00
}