assume constant dimensions for created textures, shaves a lot of time in uploading

This commit is contained in:
veclavtalica
2025-02-09 08:07:58 +03:00
parent 322fbf6bbd
commit d6aaef3f68
4 changed files with 57 additions and 21 deletions

View File

@ -119,11 +119,14 @@ GLuint get_scratch_vertex_array(void) {
}
GPUTexture create_gpu_texture(TextureFilter filter, bool generate_mipmaps) {
GPUTexture create_gpu_texture(TextureFilter filter, bool generate_mipmaps, int channels, int width, int height) {
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
SDL_assert(width > 0 && height > 0);
SDL_assert(channels > 0 && channels <= 4);
#if !defined(EMSCRIPTEN)
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, (GLboolean)generate_mipmaps);
#else
@ -146,19 +149,6 @@ GPUTexture create_gpu_texture(TextureFilter filter, bool generate_mipmaps) {
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
#endif
glBindTexture(GL_TEXTURE_2D, 0);
return texture;
}
void delete_gpu_texture(GPUTexture texture) {
glDeleteTextures(1, &texture);
}
void upload_gpu_texture(GPUTexture texture, void *pixels, int channels, int width, int height) {
glBindTexture(GL_TEXTURE_2D, texture);
int format_internal, format;
if (channels == 4) {
#ifdef EMSCRIPTEN
@ -179,9 +169,11 @@ void upload_gpu_texture(GPUTexture texture, void *pixels, int channels, int widt
format = GL_ALPHA;
} else {
CRY("upload_gpu_texture", "Unsupported channel count");
return;
format_internal = GL_ALPHA;
format = GL_ALPHA;
}
/* preallocate texture storage in advance */
glTexImage2D(GL_TEXTURE_2D,
0,
format_internal,
@ -190,7 +182,46 @@ void upload_gpu_texture(GPUTexture texture, void *pixels, int channels, int widt
0,
format,
GL_UNSIGNED_BYTE,
pixels);
NULL);
glBindTexture(GL_TEXTURE_2D, 0);
return texture;
}
void delete_gpu_texture(GPUTexture texture) {
glDeleteTextures(1, &texture);
}
void upload_gpu_texture(GPUTexture texture, void *pixels, int channels, int width, int height) {
glBindTexture(GL_TEXTURE_2D, texture);
int format;
if (channels == 4) {
format = GL_RGBA;
} else if (channels == 3) {
format = GL_RGB;
} else if (channels == 1) {
format = GL_ALPHA;
} else {
CRY("upload_gpu_texture", "Unsupported channel count");
return;
}
profile_start("texture upload");
glTexSubImage2D(GL_TEXTURE_2D,
0,
0,
0,
width,
height,
format,
GL_UNSIGNED_BYTE,
pixels);
profile_end("texture upload");
glBindTexture(GL_TEXTURE_2D, 0);
}

View File

@ -11,7 +11,8 @@ typedef enum TextureFilter {
TEXTURE_FILTER_LINEAR,
} TextureFilter;
GPUTexture create_gpu_texture(TextureFilter filter, bool generate_mipmaps);
/* allocate a texture storage with constant parameters */
GPUTexture create_gpu_texture(TextureFilter filter, bool generate_mipmaps, int channels, int width, int height);
void delete_gpu_texture(GPUTexture texture);

View File

@ -150,7 +150,7 @@ static FontData *text_load_font_data(const char *path, int height_px) {
stbtt_PackEnd(&pctx);
}
font_data->texture = create_gpu_texture(ctx.font_filtering, true);
font_data->texture = create_gpu_texture(ctx.font_filtering, true, 1, (int)ctx.font_texture_size, (int)ctx.font_texture_size);
upload_gpu_texture(
font_data->texture,
bitmap,