Moved MAX_LIGHTS definition from shader to c code and implemented functionality to allow #defining from c code before shaders are compiled

dev
Shariq Shah 6 years ago
parent 3b5a8aecd3
commit 051c6a97e7
  1. 2
      assets/shaders/blinn_phong.frag
  2. 2
      src/game/gui.c
  3. 2
      src/game/im_render.c
  4. 7
      src/game/material.c
  5. 4
      src/game/renderer.c
  6. 30
      src/game/shader.c
  7. 2
      src/game/shader.h
  8. 4
      src/game/sprite.c
  9. 4
      todo.txt

@ -19,8 +19,6 @@ const int LT_SPOT = 0;
const int LT_DIR = 1; const int LT_DIR = 1;
const int LT_POINT = 2; const int LT_POINT = 2;
const int MAX_LIGHTS = 12;
uniform sampler2D diffuse_texture; uniform sampler2D diffuse_texture;
uniform Light lights[MAX_LIGHTS]; uniform Light lights[MAX_LIGHTS];
uniform int total_active_lights; uniform int total_active_lights;

@ -46,7 +46,7 @@ bool gui_init(struct Gui* gui)
gui->context.clip.paste = gui_on_clipbard_paste; gui->context.clip.paste = gui_on_clipbard_paste;
gui->context.clip.userdata = nk_handle_ptr(0); gui->context.clip.userdata = nk_handle_ptr(0);
gui->current_font = NULL; 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) if(gui->shader < 0)
{ {
log_error("gui:init", "Failed to create shader for gui"); log_error("gui:init", "Failed to create shader for gui");

@ -59,7 +59,7 @@ void im_init(void)
IM_State.curr_geom = -1; IM_State.curr_geom = -1;
IM_State.curr_vertex = 0; 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) void im_cleanup(void)

@ -6,6 +6,7 @@
#include "../common/log.h" #include "../common/log.h"
#include "texture.h" #include "texture.h"
#include "light.h" #include "light.h"
#include "scene.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -25,7 +26,9 @@ bool material_init(struct Material* material, int material_type)
case MAT_BLINN: case MAT_BLINN:
{ {
material->lit = true; 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) if(material->shader == -1)
{ {
@ -65,7 +68,7 @@ bool material_init(struct Material* material, int material_type)
case MAT_UNSHADED: case MAT_UNSHADED:
{ {
material->lit = false; material->lit = false;
material->shader = shader_create("unshaded.vert", "unshaded.frag"); material->shader = shader_create("unshaded.vert", "unshaded.frag", NULL);
if(material->shader == -1) if(material->shader == -1)
{ {

@ -119,8 +119,8 @@ void renderer_init(struct Renderer* renderer)
renderer->def_fbo = framebuffer_create(width, height, true, false, true); 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_albedo_tex, FA_COLOR_ATTACHMENT0);
framebuffer_texture_set(renderer->def_fbo, renderer->def_depth_tex, FA_DEPTH_ATTACHMENT); framebuffer_texture_set(renderer->def_fbo, renderer->def_depth_tex, FA_DEPTH_ATTACHMENT);
renderer->composition_shader = shader_create("fbo.vert", "fbo.frag"); renderer->composition_shader = shader_create("fbo.vert", "fbo.frag", NULL);
renderer->debug_shader = shader_create("debug.vert", "debug.frag"); renderer->debug_shader = shader_create("debug.vert", "debug.frag", NULL);
renderer->num_culled_slot = editor_debugvar_slot_create("Culled Geom", VT_INT); renderer->num_culled_slot = editor_debugvar_slot_create("Culled Geom", VT_INT);
renderer->num_rendered_slot = editor_debugvar_slot_create("Rendered Geom", VT_INT); renderer->num_rendered_slot = editor_debugvar_slot_create("Rendered Geom", VT_INT);

@ -34,7 +34,7 @@ void debug_print_shader(const char* shaderText)
log_raw("\n END_DEBUG_PRINT\n\n"); 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"); char* include_loc = strstr(shader_text, "//include");
if(include_loc) if(include_loc)
@ -66,6 +66,20 @@ char* run_preprocessor(char* shader_text)
filename = strtok(NULL, " "); 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; return shader_text;
} }
@ -75,7 +89,7 @@ void shader_init(void)
empty_indices = array_new(int); 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/"); char* vs_path = str_new("shaders/");
vs_path = str_concat(vs_path, vert_shader_name); 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(vert_source != NULL);
assert(frag_source != NULL); assert(frag_source != NULL);
vert_source = run_preprocessor(vert_source); vert_source = run_preprocessor(vert_source, custom_defines);
frag_source = run_preprocessor(frag_source); frag_source = run_preprocessor(frag_source, custom_defines);
const char* vert_sourcePtr[2] = { GLSL_VERSION_STR, vert_source }; const char* vert_sourcePtr = vert_source;
const char* frag_sourcePtr[2] = { GLSL_VERSION_STR, frag_source }; const char* frag_sourcePtr = frag_source;
GL_CHECK(glShaderSource(vert_shader, 2, &vert_sourcePtr, NULL)); GL_CHECK(glShaderSource(vert_shader, 1, &vert_sourcePtr, NULL));
GL_CHECK(glShaderSource(frag_shader, 2, &frag_sourcePtr, NULL)); GL_CHECK(glShaderSource(frag_shader, 1, &frag_sourcePtr, NULL));
GL_CHECK(glCompileShader(vert_shader)); GL_CHECK(glCompileShader(vert_shader));
GL_CHECK(glCompileShader(frag_shader)); GL_CHECK(glCompileShader(frag_shader));

@ -23,7 +23,7 @@ enum Uniform_Type
UT_TEX 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_init(void);
void shader_bind(const int shader_index); void shader_bind(const int shader_index);
void shader_remove(const int shader_index); void shader_remove(const int shader_index);

@ -47,11 +47,11 @@ void sprite_batch_create(struct Sprite_Batch* batch, const char* texture_name, c
} }
batch->texture = texture; batch->texture = texture;
int shader = shader_create(vert_shader, frag_shader); int shader = shader_create(vert_shader, frag_shader, NULL);
if(shader < -1) if(shader < -1)
{ {
log_error("sprite_batch:create", "Failed to create shader from '%s'/'%s' sprite batch", vert_shader, frag_shader); 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->shader = shader;
batch->draw_mode = draw_mode; batch->draw_mode = draw_mode;

@ -1,6 +1,4 @@
Todo: 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 bug with blinn shader reaching maximum uniform number on mac
- Fix Deployment target issue in xcode build on macos - Fix Deployment target issue in xcode build on macos
- Implement/Fix copying libraries to executable folder after build completes on mac os - 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 * Console command to read/write entity to file and add it to scene
* Fix editor camera right-click behaviour * Fix editor camera right-click behaviour
* Moved #version definition from shaders to c code for easier control * 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

Loading…
Cancel
Save