aboutsummaryrefslogtreecommitdiff
path: root/src/core/core.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/core.c')
-rw-r--r--src/core/core.c36
1 files changed, 20 insertions, 16 deletions
diff --git a/src/core/core.c b/src/core/core.c
index e1671ea..9c04cc7 100644
--- a/src/core/core.c
+++ b/src/core/core.c
@@ -3,6 +3,7 @@
3#include "gl_util.h" 3#include "gl_util.h"
4 4
5// #include <log/log.h> 5// #include <log/log.h>
6#include <fnv1a.h>
6 7
7#include <assert.h> 8#include <assert.h>
8 9
@@ -282,26 +283,29 @@ void gfx_destroy_framebuffer(GfxCore* gfxcore, FrameBuffer** framebuffer) {
282// Shaders. 283// Shaders.
283// ----------------------------------------------------------------------------- 284// -----------------------------------------------------------------------------
284 285
285static uint64_t hash_shader_desc(const ShaderDesc* desc) { 286static hash_t hash_shader_desc(const ShaderDesc* desc) {
286 assert(desc); 287 assert(desc);
287 // Note that defines may affect shader permutations, so we need to hash those 288 // Defines may affect shader permutations, so we need to hash those as well.
288 // as well. 289 hash_t hash = fnv1a32_begin();
289 uint64_t hash = 0; 290 hash = fnv1a32_update(hash, desc->code, strlen(desc->code));
290 for (size_t i = 0; i < desc->num_defines; ++i) { 291 for (size_t i = 0; i < desc->num_defines; ++i) {
291 const ShaderCompilerDefine* define = &desc->defines[i]; 292 const ShaderCompilerDefine* define = &desc->defines[i];
292 hash = (((hash << 13) + sstring_hash(define->name)) << 7) + 293
293 sstring_hash(define->value); 294 hash = fnv1a32_update(
295 hash, sstring_cstr(&define->name), sstring_length(&define->name));
296 hash = fnv1a32_update(
297 hash, sstring_cstr(&define->value), sstring_length(&define->value));
294 } 298 }
295 return (hash << 17) + cstring_hash(desc->code); 299 return hash;
296} 300}
297 301
298static uint64_t hash_program_desc(const ShaderProgramDesc* desc) { 302static hash_t hash_program_desc(const ShaderProgramDesc* desc) {
299 assert(desc); 303 assert(desc);
300 return ((uint64_t)desc->vertex_shader->id << 32) | 304 return ((hash_t)desc->vertex_shader->id << 16) |
301 (uint64_t)desc->fragment_shader->id; 305 (hash_t)desc->fragment_shader->id;
302} 306}
303 307
304static Shader* find_cached_shader(ShaderCache* cache, uint64_t hash) { 308static Shader* find_cached_shader(ShaderCache* cache, hash_t hash) {
305 assert(cache); 309 assert(cache);
306 mempool_foreach(cache, entry, { 310 mempool_foreach(cache, entry, {
307 if (entry->hash == hash) { 311 if (entry->hash == hash) {
@@ -311,7 +315,7 @@ static Shader* find_cached_shader(ShaderCache* cache, uint64_t hash) {
311 return 0; 315 return 0;
312} 316}
313 317
314static ShaderProgram* find_cached_program(ProgramCache* cache, uint64_t hash) { 318static ShaderProgram* find_cached_program(ProgramCache* cache, hash_t hash) {
315 assert(cache); 319 assert(cache);
316 mempool_foreach(cache, entry, { 320 mempool_foreach(cache, entry, {
317 if (entry->hash == hash) { 321 if (entry->hash == hash) {
@@ -350,9 +354,9 @@ Shader* gfx_make_shader(GfxCore* gfxcore, const ShaderDesc* desc) {
350 assert(desc); 354 assert(desc);
351 355
352 // Check the shader cache first. 356 // Check the shader cache first.
353 ShaderCache* cache = &gfxcore->shader_cache; 357 ShaderCache* cache = &gfxcore->shader_cache;
354 const uint64_t hash = hash_shader_desc(desc); 358 const hash_t hash = hash_shader_desc(desc);
355 Shader* shader = find_cached_shader(cache, hash); 359 Shader* shader = find_cached_shader(cache, hash);
356 if (shader) { 360 if (shader) {
357 // LOGD("Found cached shader with hash [%lx]", hash); 361 // LOGD("Found cached shader with hash [%lx]", hash);
358 return shader; 362 return shader;
@@ -395,7 +399,7 @@ ShaderProgram* gfx_make_shader_program(
395 399
396 // Check the shader program cache first. 400 // Check the shader program cache first.
397 ProgramCache* cache = &gfxcore->program_cache; 401 ProgramCache* cache = &gfxcore->program_cache;
398 const uint64_t hash = hash_program_desc(desc); 402 const hash_t hash = hash_program_desc(desc);
399 ShaderProgram* prog = find_cached_program(cache, hash); 403 ShaderProgram* prog = find_cached_program(cache, hash);
400 if (prog) { 404 if (prog) {
401 // LOGD("Found cached shader program with hash [%lx]", hash); 405 // LOGD("Found cached shader program with hash [%lx]", hash);