From a53d076f94c4b38ac511d6d3a5d4fc21e026b183 Mon Sep 17 00:00:00 2001 From: Connor Thomson Date: Fri, 25 Sep 2026 19:47:30 -0700 Subject: Add PCSS shadows --- src/model.c | 27 +++++++++++++++++---------- src/shadow.c | 17 +++++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/model.c b/src/model.c index 2c78850..396e70a 100644 --- a/src/model.c +++ b/src/model.c @@ -145,7 +145,7 @@ typedef struct ta_model_light_uniforms { float shadow_minimum_bias; float shadow_maximum_bias; float shadow_strength; - float padding; + float shadow_penumbra_scale; } ta_model_light_uniforms; typedef struct ta_model_shadow_uniforms { @@ -182,7 +182,7 @@ bool ta_model_init(ta_model *model, SDL_GPUDevice *device, const float positions } model->vertex_count = (Uint32)model->mesh.vertex_count; SDL_GPUShader *vertex_shader = ta_model_shader(device, file_model_vert_slang_spv_start, file_model_vert_slang_spv_size, SDL_GPU_SHADERSTAGE_VERTEX, 0, 1); - SDL_GPUShader *fragment_shader = ta_model_shader(device, file_model_frag_slang_spv_start, file_model_frag_slang_spv_size, SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 1); + SDL_GPUShader *fragment_shader = ta_model_shader(device, file_model_frag_slang_spv_start, file_model_frag_slang_spv_size, SDL_GPU_SHADERSTAGE_FRAGMENT, 2, 1); SDL_GPUVertexBufferDescription vertex_buffer = {.slot = 0, .pitch = sizeof(ta_model_vertex), .input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX}; SDL_GPUVertexAttribute attributes[2] = { { @@ -374,17 +374,24 @@ void ta_model_render(const ta_model *model, SDL_GPUCommandBuffer *command_buffer ta_shadow_light_direction[1], ta_shadow_light_direction[2] }, - .shadow_texel_size = 1.0f / (float)TA_SHADOW_MAP_SIZE, - .shadow_minimum_bias = ta_shadow_minimum_bias, - .shadow_maximum_bias = ta_shadow_maximum_bias, - .shadow_strength = ta_shadow_strength, + .shadow_texel_size = 1.0f / (float)TA_SHADOW_MAP_SIZE, + .shadow_minimum_bias = ta_shadow_minimum_bias, + .shadow_maximum_bias = ta_shadow_maximum_bias, + .shadow_strength = ta_shadow_strength, + .shadow_penumbra_scale = ta_shadow_penumbra_scale, }; - SDL_GPUTextureSamplerBinding shadow_binding = { - .texture = ta_shadow_texture, - .sampler = ta_shadow_sampler + SDL_GPUTextureSamplerBinding shadow_bindings[2] = { + { + .texture = ta_shadow_texture, + .sampler = ta_shadow_sampler + }, + { + .texture = ta_shadow_texture, + .sampler = ta_shadow_depth_sampler + }, }; SDL_PushGPUFragmentUniformData(command_buffer, 0, &light, sizeof(light)); - SDL_BindGPUFragmentSamplers(render_pass, 0, &shadow_binding, 1); + SDL_BindGPUFragmentSamplers(render_pass, 0, shadow_bindings, 2); } SDL_GPUBufferBinding binding = { diff --git a/src/shadow.c b/src/shadow.c index 2967674..469a3bf 100644 --- a/src/shadow.c +++ b/src/shadow.c @@ -28,6 +28,7 @@ SDL_GPUTexture *ta_shadow_texture; SDL_GPUSampler *ta_shadow_sampler; +SDL_GPUSampler *ta_shadow_depth_sampler; float ta_shadow_light_direction[TA_SHADOW_DIMENSION] = { -0.45f, 0.75f, 0.55f }; @@ -35,6 +36,8 @@ float ta_shadow_normal_offset = 0.03f; float ta_shadow_minimum_bias = 0.0005f; float ta_shadow_maximum_bias = 0.003f; float ta_shadow_strength = 1.0f; +float ta_shadow_light_size = 0.04f; +float ta_shadow_penumbra_scale; static float ta_shadow_dot(const float *left, const float *right) { return left[0] * right[0] + left[1] * right[1] + left[2] * right[2]; @@ -56,6 +59,8 @@ static void ta_shadow_normalize(float *vector) { } void ta_shadow_init(void) { + ta_shadow_penumbra_scale = ta_shadow_light_size * (TA_SHADOW_FAR - TA_SHADOW_NEAR) / (2.0f * TA_SHADOW_EXTENT); + SDL_GPUTextureCreateInfo texture_info = { .type = SDL_GPU_TEXTURETYPE_2D, .format = TA_SHADOW_FORMAT, @@ -79,6 +84,16 @@ void ta_shadow_init(void) { .enable_compare = true, }; ta_shadow_sampler = ta_sdl_create_gpu_sampler(ta_gpu_device, &sampler_info); + + SDL_GPUSamplerCreateInfo depth_sampler_info = { + .min_filter = SDL_GPU_FILTER_NEAREST, + .mag_filter = SDL_GPU_FILTER_NEAREST, + .mipmap_mode = SDL_GPU_SAMPLERMIPMAPMODE_NEAREST, + .address_mode_u = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE, + .address_mode_v = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE, + .address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE, + }; + ta_shadow_depth_sampler = ta_sdl_create_gpu_sampler(ta_gpu_device, &depth_sampler_info); } void ta_shadow_get_light_view_projection(float *matrix) { @@ -145,8 +160,10 @@ SDL_GPURenderPass *ta_shadow_begin_render_pass(SDL_GPUCommandBuffer *command_buf } void ta_shadow_shutdown(void) { + SDL_ReleaseGPUSampler(ta_gpu_device, ta_shadow_depth_sampler); SDL_ReleaseGPUSampler(ta_gpu_device, ta_shadow_sampler); SDL_ReleaseGPUTexture(ta_gpu_device, ta_shadow_texture); + ta_shadow_depth_sampler = NULL; ta_shadow_sampler = NULL; ta_shadow_texture = NULL; } \ No newline at end of file -- cgit v1.2.3