From 051c6a97e7086eb97eb5cf1c68727f11a94e7e4b Mon Sep 17 00:00:00 2001 From: Shariq Shah Date: Tue, 1 Oct 2019 13:02:04 +1000 Subject: [PATCH] Moved MAX_LIGHTS definition from shader to c code and implemented functionality to allow #defining from c code before shaders are compiled --- assets/shaders/blinn_phong.frag | 2 -- src/game/gui.c | 2 +- src/game/im_render.c | 2 +- src/game/material.c | 7 +++++-- src/game/renderer.c | 4 ++-- src/game/shader.c | 30 ++++++++++++++++++++++-------- src/game/shader.h | 2 +- src/game/sprite.c | 4 ++-- todo.txt | 4 ++-- 9 files changed, 36 insertions(+), 21 deletions(-) diff --git a/assets/shaders/blinn_phong.frag b/assets/shaders/blinn_phong.frag index 25a8a51..9c52702 100755 --- a/assets/shaders/blinn_phong.frag +++ b/assets/shaders/blinn_phong.frag @@ -19,8 +19,6 @@ const int LT_SPOT = 0; const int LT_DIR = 1; const int LT_POINT = 2; -const int MAX_LIGHTS = 12; - uniform sampler2D diffuse_texture; uniform Light lights[MAX_LIGHTS]; uniform int total_active_lights; diff --git a/src/game/gui.c b/src/game/gui.c index 28c2e96..f7105e3 100755 --- a/src/game/gui.c +++ b/src/game/gui.c @@ -46,7 +46,7 @@ bool gui_init(struct Gui* gui) gui->context.clip.paste = gui_on_clipbard_paste; gui->context.clip.userdata = nk_handle_ptr(0); gui->current_font = NULL; - gui->shader = shader_create("gui.vert", "gui.frag"); + gui->shader = shader_create("gui.vert", "gui.frag", NULL); if(gui->shader < 0) { log_error("gui:init", "Failed to create shader for gui"); diff --git a/src/game/im_render.c b/src/game/im_render.c index 56a3104..f64d3fd 100755 --- a/src/game/im_render.c +++ b/src/game/im_render.c @@ -59,7 +59,7 @@ void im_init(void) IM_State.curr_geom = -1; IM_State.curr_vertex = 0; - IM_State.im_shader = shader_create("im_geom.vert", "im_geom.frag"); + IM_State.im_shader = shader_create("im_geom.vert", "im_geom.frag", NULL); } void im_cleanup(void) diff --git a/src/game/material.c b/src/game/material.c index 990b8a4..bd8b1eb 100755 --- a/src/game/material.c +++ b/src/game/material.c @@ -6,6 +6,7 @@ #include "../common/log.h" #include "texture.h" #include "light.h" +#include "scene.h" #include #include @@ -25,7 +26,9 @@ bool material_init(struct Material* material, int material_type) case MAT_BLINN: { material->lit = true; - material->shader = shader_create("blinn_phong.vert", "blinn_phong.frag"); + char custom_defines[64]; + snprintf(custom_defines, 64, "#define MAX_LIGHTS %d", MAX_LIGHTS); + material->shader = shader_create("blinn_phong.vert", "blinn_phong.frag", custom_defines); if(material->shader == -1) { @@ -65,7 +68,7 @@ bool material_init(struct Material* material, int material_type) case MAT_UNSHADED: { material->lit = false; - material->shader = shader_create("unshaded.vert", "unshaded.frag"); + material->shader = shader_create("unshaded.vert", "unshaded.frag", NULL); if(material->shader == -1) { diff --git a/src/game/renderer.c b/src/game/renderer.c index 15f29b6..a003c6d 100755 --- a/src/game/renderer.c +++ b/src/game/renderer.c @@ -119,8 +119,8 @@ void renderer_init(struct Renderer* renderer) renderer->def_fbo = framebuffer_create(width, height, true, false, true); framebuffer_texture_set(renderer->def_fbo, renderer->def_albedo_tex, FA_COLOR_ATTACHMENT0); framebuffer_texture_set(renderer->def_fbo, renderer->def_depth_tex, FA_DEPTH_ATTACHMENT); - renderer->composition_shader = shader_create("fbo.vert", "fbo.frag"); - renderer->debug_shader = shader_create("debug.vert", "debug.frag"); + renderer->composition_shader = shader_create("fbo.vert", "fbo.frag", NULL); + renderer->debug_shader = shader_create("debug.vert", "debug.frag", NULL); renderer->num_culled_slot = editor_debugvar_slot_create("Culled Geom", VT_INT); renderer->num_rendered_slot = editor_debugvar_slot_create("Rendered Geom", VT_INT); diff --git a/src/game/shader.c b/src/game/shader.c index 1de0da1..fd00b61 100755 --- a/src/game/shader.c +++ b/src/game/shader.c @@ -34,7 +34,7 @@ void debug_print_shader(const char* shaderText) log_raw("\n END_DEBUG_PRINT\n\n"); } -char* run_preprocessor(char* shader_text) +char* run_preprocessor(char* shader_text, const char* custom_defines) { char* include_loc = strstr(shader_text, "//include"); if(include_loc) @@ -66,6 +66,20 @@ char* run_preprocessor(char* shader_text) filename = strtok(NULL, " "); } } + + //If there are other #defines, add them too + if(custom_defines) + { + char* shader_text_with_custom_defines = str_new("%s\n%s", custom_defines, shader_text); + free(shader_text); + shader_text = shader_text_with_custom_defines; + } + + // Insert #version line at the top + char* shader_text_with_version = str_new("%s\n%s", GLSL_VERSION_STR, shader_text); + free(shader_text); + shader_text = shader_text_with_version; + return shader_text; } @@ -75,7 +89,7 @@ void shader_init(void) empty_indices = array_new(int); } -int shader_create(const char* vert_shader_name, const char* frag_shader_name) +int shader_create(const char* vert_shader_name, const char* frag_shader_name, const char* custom_defines) { char* vs_path = str_new("shaders/"); vs_path = str_concat(vs_path, vert_shader_name); @@ -91,14 +105,14 @@ int shader_create(const char* vert_shader_name, const char* frag_shader_name) assert(vert_source != NULL); assert(frag_source != NULL); - vert_source = run_preprocessor(vert_source); - frag_source = run_preprocessor(frag_source); + vert_source = run_preprocessor(vert_source, custom_defines); + frag_source = run_preprocessor(frag_source, custom_defines); - const char* vert_sourcePtr[2] = { GLSL_VERSION_STR, vert_source }; - const char* frag_sourcePtr[2] = { GLSL_VERSION_STR, frag_source }; + const char* vert_sourcePtr = vert_source; + const char* frag_sourcePtr = frag_source; - GL_CHECK(glShaderSource(vert_shader, 2, &vert_sourcePtr, NULL)); - GL_CHECK(glShaderSource(frag_shader, 2, &frag_sourcePtr, NULL)); + GL_CHECK(glShaderSource(vert_shader, 1, &vert_sourcePtr, NULL)); + GL_CHECK(glShaderSource(frag_shader, 1, &frag_sourcePtr, NULL)); GL_CHECK(glCompileShader(vert_shader)); GL_CHECK(glCompileShader(frag_shader)); diff --git a/src/game/shader.h b/src/game/shader.h index 7f6a299..826a332 100755 --- a/src/game/shader.h +++ b/src/game/shader.h @@ -23,7 +23,7 @@ enum Uniform_Type UT_TEX }; -int shader_create(const char* vert_shader_name, const char* frag_shader_name); +int shader_create(const char* vert_shader_name, const char* frag_shader_name, const char* custom_defines); void shader_init(void); void shader_bind(const int shader_index); void shader_remove(const int shader_index); diff --git a/src/game/sprite.c b/src/game/sprite.c index a6c2fe8..8afd192 100755 --- a/src/game/sprite.c +++ b/src/game/sprite.c @@ -47,11 +47,11 @@ void sprite_batch_create(struct Sprite_Batch* batch, const char* texture_name, c } batch->texture = texture; - int shader = shader_create(vert_shader, frag_shader); + int shader = shader_create(vert_shader, frag_shader, NULL); if(shader < -1) { log_error("sprite_batch:create", "Failed to create shader from '%s'/'%s' sprite batch", vert_shader, frag_shader); - shader = shader_create("default.vert", "default.frag"); + shader = shader_create("default.vert", "default.frag", NULL); } batch->shader = shader; batch->draw_mode = draw_mode; diff --git a/todo.txt b/todo.txt index 88b9971..91e3bf4 100644 --- a/todo.txt +++ b/todo.txt @@ -1,6 +1,4 @@ Todo: - - Move MAX_LIGHTS shader definition from inside the shader to be dynamically defined and added to the shader code when it is compiled - to reduce inconsistencies - Fix bug with blinn shader reaching maximum uniform number on mac - Fix Deployment target issue in xcode build on macos - Implement/Fix copying libraries to executable folder after build completes on mac os @@ -315,3 +313,5 @@ Done: * Console command to read/write entity to file and add it to scene * Fix editor camera right-click behaviour * Moved #version definition from shaders to c code for easier control + * Moved MAX_LIGHTS shader definition from inside the shader to be dynamically defined and added to the shader code when it is compiled + to reduce inconsistencies