diff options
| -rw-r--r-- | include/shadow.h | 3 | ||||
| -rw-r--r-- | shaders/model.frag.slang | 163 | ||||
| -rw-r--r-- | src/model.c | 27 | ||||
| -rw-r--r-- | src/shadow.c | 17 |
4 files changed, 185 insertions, 25 deletions
diff --git a/include/shadow.h b/include/shadow.h index 0087ef4..b054418 100644 --- a/include/shadow.h +++ b/include/shadow.h @@ -27,11 +27,14 @@ extern SDL_GPUTexture *ta_shadow_texture; extern SDL_GPUSampler *ta_shadow_sampler; +extern SDL_GPUSampler *ta_shadow_depth_sampler; extern float ta_shadow_light_direction[TA_SHADOW_DIMENSION]; extern float ta_shadow_normal_offset; extern float ta_shadow_minimum_bias; extern float ta_shadow_maximum_bias; extern float ta_shadow_strength; +extern float ta_shadow_light_size; +extern float ta_shadow_penumbra_scale; void ta_shadow_init(void); void ta_shadow_get_light_view_projection(float *matrix); diff --git a/shaders/model.frag.slang b/shaders/model.frag.slang index ac12d0e..c8e9f90 100644 --- a/shaders/model.frag.slang +++ b/shaders/model.frag.slang @@ -22,17 +22,17 @@ struct light_uniforms { float shadow_minimum_bias; float shadow_maximum_bias; float shadow_strength; - float padding; + float shadow_penumbra_scale; }; [[vk::binding(0, 3)]] ConstantBuffer<light_uniforms> light : register(b0, space3); [[vk::binding(0, 2)]] -Texture2D<float> shadow_map : register(t0, space2); +Sampler2DShadow shadow_map : register(t0, space2); [[vk::binding(1, 2)]] -SamplerComparisonState shadow_sampler : register(s0, space2); +Sampler2D<float> shadow_depth_map : register(t1, space2); struct FSInput { float3 normal : TEXCOORD0; @@ -49,7 +49,142 @@ static const float ambient_strength = 0.22; static const float diffuse_strength = 0.78; static const float specular_strength = 0.6; static const float shininess = 32.0; -static const int shadow_pcf_radius = 1; + +static const int shadow_blocker_sample_count = 32; +static const int shadow_pcf_sample_count = 64; +static const float shadow_maximum_search_uv = 0.02; +static const float shadow_minimum_filter_texels = 1.5; +static const float shadow_maximum_filter_uv = 0.02; + +static const float2 blocker_search_disk[shadow_blocker_sample_count] = { + float2( 0.44011213, -0.38478364), + float2(-0.93881893, 0.28433844), + float2( 0.29851448, 0.91473036), + float2(-0.56853028, -0.74873636), + float2( 0.92702072, 0.34599034), + float2(-0.17203432, 0.12048409), + float2(-0.40881620, 0.87902032), + float2( 0.11214125, -0.97011162), + float2( 0.35807629, 0.34593269), + float2(-0.93515467, -0.30090412), + float2( 0.96965589, -0.21669445), + float2(-0.12747712, -0.42799143), + float2(-0.04160416, 0.58750344), + float2(-0.50661284, -0.18463137), + float2(-0.49591186, 0.43573011), + float2( 0.69214410, 0.71546306), + float2( 0.58762817, -0.78906213), + float2( 0.60529434, -0.00157846), + float2( 0.18530521, -0.07537744), + float2(-0.06393139, 0.98782194), + float2(-0.24668936, -0.92075575), + float2( 0.81235073, -0.52916214), + float2( 0.18896697, -0.62996998), + float2(-0.71415530, 0.69260619), + float2(-0.65167829, 0.11466757), + float2(-0.65839165, -0.45386823), + float2( 0.40773627, 0.63990787), + float2(-0.99945967, -0.01288785), + float2( 0.06793334, 0.29985044), + float2( 0.88601041, 0.06665496), + float2( 0.63692110, 0.42919088), + float2(-0.38553272, -0.52927284), +}; + +static const float2 pcf_filter_disk[shadow_pcf_sample_count] = { + float2( 0.32558563, -0.24988839), + float2(-0.98541266, 0.16930865), + float2( 0.09145512, 0.89445213), + float2(-0.54480597, -0.82331975), + float2( 0.86243144, 0.42877010), + float2( 0.19152898, -0.98054135), + float2(-0.24839078, 0.19012049), + float2(-0.58789047, 0.78475191), + float2( 0.97505840, -0.19006157), + float2( 0.30273086, 0.33427269), + float2(-0.60614320, -0.24764520), + float2( 0.64000315, -0.68394018), + float2(-0.12301224, -0.53311522), + float2( 0.51234899, 0.75192892), + float2( 0.64313826, 0.06972717), + float2(-0.19284146, 0.60355651), + float2(-0.62280056, 0.36318874), + float2(-0.18834340, -0.95691492), + float2(-0.04911758, -0.13578936), + float2(-0.82927681, -0.54621998), + float2( 0.27020206, -0.61902601), + float2(-0.98076941, -0.18488425), + float2(-0.26883836, 0.96108127), + float2( 0.66283184, -0.32922370), + float2( 0.98983969, 0.13429859), + float2(-0.44329948, -0.53094113), + float2(-0.54192541, 0.06588807), + float2( 0.14739192, 0.59331660), + float2( 0.04982322, 0.14943985), + float2(-0.31222249, -0.26794820), + float2( 0.56008364, 0.46637058), + float2(-0.81663912, 0.57351727), + float2( 0.32731617, 0.05812120), + float2( 0.85294944, -0.52194453), + float2( 0.44306862, -0.87673132), + float2( 0.03380403, -0.76031090), + float2( 0.08711519, -0.36824602), + float2(-0.04387798, 0.39043461), + float2(-0.44689900, 0.54474622), + float2(-0.79730201, -0.01361312), + float2( 0.34065561, 0.93312502), + float2(-0.28571328, -0.73045171), + float2( 0.74645565, 0.64790973), + float2( 0.47432929, -0.49756161), + float2(-0.32838125, -0.02568294), + float2( 0.49288123, -0.09977098), + float2( 0.51299973, 0.24791173), + float2( 0.75389572, -0.12623179), + float2(-0.84467634, 0.34163260), + float2(-0.36987966, 0.76150517), + float2(-0.27850542, 0.40472485), + float2(-0.10969806, 0.80499616), + float2(-0.81179020, -0.32586517), + float2( 0.17068056, -0.09060725), + float2( 0.36889045, 0.58095936), + float2( 0.79213670, 0.22321366), + float2(-0.62388309, -0.63161735), + float2(-0.45122724, 0.24914915), + float2(-0.11527424, -0.33262249), + float2(-0.71985451, 0.17533603), + float2( 0.07976847, -0.56354086), + float2(-0.64407590, -0.43844161), + float2( 0.25012793, 0.75807218), + float2(-0.13113449, 0.03615541), +}; + +float find_blocker(float2 uv, float reference, float search_radius, out float blocker_depth) { + float blockers = 0.0; + blocker_depth = 0.0; + for (int index = 0; index < shadow_blocker_sample_count; index++) { + float depth = shadow_depth_map.SampleLevel(uv + blocker_search_disk[index] * search_radius, 0.0); + if (depth < reference) { + blocker_depth += depth; + blockers += 1.0; + } + } + if (blockers > 0.0) { + blocker_depth /= blockers; + } + return blockers; +} + +float penumbra_size(float reference, float blocker_depth) { + return light.shadow_penumbra_scale * (reference - blocker_depth); +} + +float pcf_filter(float2 uv, float reference, float filter_radius) { + float visibility = 0.0; + for (int index = 0; index < shadow_pcf_sample_count; index++) { + visibility += shadow_map.SampleCmpLevelZero(uv + pcf_filter_disk[index] * filter_radius, reference); + } + return visibility / float(shadow_pcf_sample_count); +} float shadow_visibility(float4 light_space_position, float normal_dot_light) { float3 projected = light_space_position.xyz / light_space_position.w; @@ -62,19 +197,17 @@ float shadow_visibility(float4 light_space_position, float normal_dot_light) { return 1.0; } - float bias = max(light.shadow_maximum_bias * (1.0 - normal_dot_light), light.shadow_minimum_bias); - float reference = projected.z - bias; - float visibility = 0.0; - float taps = 0.0; - for (int y = -shadow_pcf_radius; y <= shadow_pcf_radius; y++) { - for (int x = -shadow_pcf_radius; x <= shadow_pcf_radius; x++) { - float2 offset = float2(x, y) * light.shadow_texel_size; - visibility += shadow_map.SampleCmpLevelZero(shadow_sampler, uv + offset, reference); - taps += 1.0; - } + float bias = max(light.shadow_maximum_bias * (1.0 - normal_dot_light), light.shadow_minimum_bias); + float reference = projected.z - bias; + + float search_radius = clamp(light.shadow_penumbra_scale * reference, light.shadow_texel_size, shadow_maximum_search_uv); + float blocker_depth; + if (find_blocker(uv, reference, search_radius, blocker_depth) == 0.0) { + return 1.0; } - return visibility / taps; + float filter_radius = clamp(penumbra_size(reference, blocker_depth), shadow_minimum_filter_texels * light.shadow_texel_size, shadow_maximum_filter_uv); + return pcf_filter(uv, reference, filter_radius); } FSOutput main(FSInput input) { 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 |
