From 4fdc043677152d370e5796d1607e09b6f1976800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Thu, 21 Mar 2024 08:13:47 +0100 Subject: [PATCH] shader uniform caching --- engine/joint/v4k.h | 20 ++++++++++++++++++-- engine/split/v4k_render.c | 20 ++++++++++++++++++-- engine/v4k.c | 20 ++++++++++++++++++-- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/engine/joint/v4k.h b/engine/joint/v4k.h index 8caf0d7..fe45eba 100644 --- a/engine/joint/v4k.h +++ b/engine/joint/v4k.h @@ -370547,12 +370547,28 @@ void ssbo_unbind(){ glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); } +typedef map(unsigned,int) uniform_binding; static __thread unsigned last_shader = -1; +static __thread quarks_db uniform_names; +static __thread map(handle, uniform_binding) uniforms; static int shader_uniform(const char *name) { - int ret = glGetUniformLocation(last_shader, name); + do_once map_init(uniforms, less_int, hash_int); + if (!map_find(uniforms, last_shader)) { + uniform_binding names_map = 0; + map_init(names_map, less_int, hash_int); + map_insert(uniforms, last_shader, names_map); + } + + uniform_binding names = *map_find(uniforms, last_shader); + unsigned name_hash = quark_intern(&uniform_names, name); + if (!map_find(names, name_hash)) { + map_insert(names, name_hash, glGetUniformLocation(last_shader, name)); + } + + // int ret = glGetUniformLocation(last_shader, name); // if( ret < 0 ) PRINTF("!cannot find uniform '%s' in shader program %d\n", name, (int)last_shader ); - return ret; + return *map_find(names, name_hash); } unsigned shader_get_active() { return last_shader; } unsigned shader_bind(unsigned program) { unsigned ret = last_shader; return glUseProgram(last_shader = program), ret; } diff --git a/engine/split/v4k_render.c b/engine/split/v4k_render.c index e0f5f9a..3b7f84c 100644 --- a/engine/split/v4k_render.c +++ b/engine/split/v4k_render.c @@ -520,12 +520,28 @@ void ssbo_unbind(){ glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); } +typedef map(unsigned,int) uniform_binding; static __thread unsigned last_shader = -1; +static __thread quarks_db uniform_names; +static __thread map(handle, uniform_binding) uniforms; static int shader_uniform(const char *name) { - int ret = glGetUniformLocation(last_shader, name); + do_once map_init(uniforms, less_int, hash_int); + if (!map_find(uniforms, last_shader)) { + uniform_binding names_map = 0; + map_init(names_map, less_int, hash_int); + map_insert(uniforms, last_shader, names_map); + } + + uniform_binding names = *map_find(uniforms, last_shader); + unsigned name_hash = quark_intern(&uniform_names, name); + if (!map_find(names, name_hash)) { + map_insert(names, name_hash, glGetUniformLocation(last_shader, name)); + } + + // int ret = glGetUniformLocation(last_shader, name); // if( ret < 0 ) PRINTF("!cannot find uniform '%s' in shader program %d\n", name, (int)last_shader ); - return ret; + return *map_find(names, name_hash); } unsigned shader_get_active() { return last_shader; } unsigned shader_bind(unsigned program) { unsigned ret = last_shader; return glUseProgram(last_shader = program), ret; } diff --git a/engine/v4k.c b/engine/v4k.c index 0157d1d..1fd4ea6 100644 --- a/engine/v4k.c +++ b/engine/v4k.c @@ -17694,12 +17694,28 @@ void ssbo_unbind(){ glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); } +typedef map(unsigned,int) uniform_binding; static __thread unsigned last_shader = -1; +static __thread quarks_db uniform_names; +static __thread map(handle, uniform_binding) uniforms; static int shader_uniform(const char *name) { - int ret = glGetUniformLocation(last_shader, name); + do_once map_init(uniforms, less_int, hash_int); + if (!map_find(uniforms, last_shader)) { + uniform_binding names_map = 0; + map_init(names_map, less_int, hash_int); + map_insert(uniforms, last_shader, names_map); + } + + uniform_binding names = *map_find(uniforms, last_shader); + unsigned name_hash = quark_intern(&uniform_names, name); + if (!map_find(names, name_hash)) { + map_insert(names, name_hash, glGetUniformLocation(last_shader, name)); + } + + // int ret = glGetUniformLocation(last_shader, name); // if( ret < 0 ) PRINTF("!cannot find uniform '%s' in shader program %d\n", name, (int)last_shader ); - return ret; + return *map_find(names, name_hash); } unsigned shader_get_active() { return last_shader; } unsigned shader_bind(unsigned program) { unsigned ret = last_shader; return glUseProgram(last_shader = program), ret; }