From 46ba94bc2b3b1a3aa61255189b1402bd97cdaa79 Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Fri, 7 Jul 2023 17:49:15 -0700 Subject: [PATCH] Move shadow mapper shaders into threedee.glsl --- main.c | 8 +++---- run_codegen.bat | 1 - shadow_mapper.glsl | 60 ---------------------------------------------- threedee.glsl | 36 ++++++++++++++++++++++++++-- 4 files changed, 37 insertions(+), 68 deletions(-) delete mode 100644 shadow_mapper.glsl diff --git a/main.c b/main.c index f14dac0..998ef30 100644 --- a/main.c +++ b/main.c @@ -1391,7 +1391,6 @@ ThreeDeeLevel load_level(MD_Arena *arena, MD_String8 binary_file) #include "assets.gen.c" #include "quad-sapp.glsl.h" #include "threedee.glsl.h" -#include "shadow_mapper.glsl.h" AABB level_aabb = { .upper_left = { 0.0f, 0.0f }, .lower_right = { TILE_SIZE * LEVEL_TILES, -(TILE_SIZE * LEVEL_TILES) } }; GameState gs = { 0 }; @@ -3133,7 +3132,6 @@ void init(void) binary_file = MD_LoadEntireFile(frame_arena, MD_S8Lit("assets/exported_3d/DecimatedPlayer.bin")); mesh_player = load_mesh(persistent_arena, binary_file, MD_S8Lit("DecimatedPlayer.bin")); - binary_file = MD_LoadEntireFile(frame_arena, MD_S8Lit("assets/exported_3d/ArmatureExportedWithAnims.bin")); armature = load_armature(persistent_arena, binary_file, MD_S8Lit("ArmatureExportedWithAnims.bin")); @@ -5020,7 +5018,7 @@ Shadow_State init_shadow_state() { [ATTR_threedee_vs_uv_in].format = SG_VERTEXFORMAT_FLOAT2, } }, - .shader = sg_make_shader(shadow_mapper_program_shader_desc(sg_query_backend())), + .shader = sg_make_shader(threedee_mesh_shadow_mapping_shader_desc(sg_query_backend())), // Cull front faces in the shadow map pass // .cull_mode = SG_CULLMODE_BACK, .sample_count = 1, @@ -5361,12 +5359,12 @@ void frame(void) sg_apply_bindings(&bindings); Mat4 model = transform_to_matrix(it->t); - shadow_mapper_vs_params_t vs_params = { + threedee_vs_params_t vs_params = { .model = model, .view = shadow_view, .projection = shadow_projection, }; - sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_shadow_mapper_vs_params, &SG_RANGE(vs_params)); + sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_threedee_vs_params, &SG_RANGE(vs_params)); num_draw_calls += 1; num_vertices += (int)it->mesh->num_vertices; sg_draw(0, (int)it->mesh->num_vertices, 1); diff --git a/run_codegen.bat b/run_codegen.bat index 7a63ee7..6e9c9a3 100644 --- a/run_codegen.bat +++ b/run_codegen.bat @@ -15,7 +15,6 @@ mkdir gen @REM shaders thirdparty\sokol-shdc.exe --input quad.glsl --output gen\quad-sapp.glsl.h --slang glsl100:hlsl5:metal_macos:glsl330 || goto :error thirdparty\sokol-shdc.exe --input threedee.glsl --output gen\threedee.glsl.h --slang glsl100:hlsl5:metal_macos:glsl330 || goto :error -thirdparty\sokol-shdc.exe --input shadow_mapper.glsl --output gen\shadow_mapper.glsl.h --slang glsl100:hlsl5:metal_macos:glsl330 || goto :error @REM metadesk codegen cl /Ithirdparty /W3 /Zi /WX codegen.c || goto :error diff --git a/shadow_mapper.glsl b/shadow_mapper.glsl deleted file mode 100644 index 72449b9..0000000 --- a/shadow_mapper.glsl +++ /dev/null @@ -1,60 +0,0 @@ -@module shadow_mapper - -@ctype mat4 Mat4 -@ctype vec4 Vec4 -@ctype vec3 Vec3 -@ctype vec2 Vec2 - - - -@vs vs -in vec3 pos_in; -in vec2 uv_in; - -out vec3 pos; -out vec2 uv; - - -uniform vs_params { - mat4 model; - mat4 view; - mat4 projection; -}; - -void main() { - pos = pos_in; - uv = uv_in; - gl_Position = projection * view * model * vec4(pos_in, 1.0); -} -@end - -@fs fs - -vec4 encodeDepth(float v) { - vec4 enc = vec4(1.0, 255.0, 65025.0, 16581375.0) * v; - enc = fract(enc); - enc -= enc.yzww * vec4(1.0/255.0,1.0/255.0,1.0/255.0,0.0); - return enc; -} - - -uniform sampler2D tex; - -in vec3 pos; -in vec2 uv; - -out vec4 frag_color; - -void main() { - vec4 col = texture(tex, uv); - if(col.a < 0.5) - { - discard; - } - - float depth = gl_FragCoord.z; - frag_color = encodeDepth(depth); -} -@end - -@program program vs fs diff --git a/threedee.glsl b/threedee.glsl index 53352a5..1ab92bb 100644 --- a/threedee.glsl +++ b/threedee.glsl @@ -78,7 +78,7 @@ void main() { pos = gl_Position.xyz; uv = uv_in; - vec3 model_space_pos = (model * total_position).xyz; + vec3 model_space_pos = (total_position).xyz; @include_block vs_compute_light_output } @end @@ -106,7 +106,7 @@ void main() { gl_Position = projection * view * model * vec4(pos_in, 1.0); - vec3 model_space_pos = (model * vec4(pos_in, 1.0f)).xyz; + vec3 model_space_pos = (vec4(pos_in, 1.0f)).xyz; @include_block vs_compute_light_output } @end @@ -238,5 +238,37 @@ void main() { } @end +@fs fs_shadow_mapping + +uniform sampler2D tex; + +in vec3 pos; +in vec2 uv; +in vec4 light_space_fragment_position; +in vec3 light_dir; +in vec4 world_space_frag_pos; + +out vec4 frag_color; + +vec4 encodeDepth(float v) { + vec4 enc = vec4(1.0, 255.0, 65025.0, 16581375.0) * v; + enc = fract(enc); + enc -= enc.yzww * vec4(1.0/255.0,1.0/255.0,1.0/255.0,0.0); + return enc; +} + +void main() { + vec4 col = texture(tex, uv); + if(col.a < 0.5) + { + discard; + } + + float depth = gl_FragCoord.z; + frag_color = encodeDepth(depth); +} +@end + @program mesh vs fs @program armature vs_skeleton fs +@program mesh_shadow_mapping vs fs_shadow_mapping