1#define_import_path bevy_pbr::shadow_sampling 4 mesh_view_bindings as view_bindings, 5 utils::interleaved_gradient_noise, 8#import bevy_render::maths::{orthonormalize, PI} 11fn sample_shadow_map_hardware(light_local: vec2<f32>, depth: f32, array_index: i32) -> f32 { 12#ifdef NO_ARRAY_TEXTURES_SUPPORT 13 return textureSampleCompare( 14 view_bindings::directional_shadow_textures, 15 view_bindings::directional_shadow_textures_comparison_sampler, 20 return textureSampleCompareLevel( 21 view_bindings::directional_shadow_textures, 22 view_bindings::directional_shadow_textures_comparison_sampler, 32fn search_for_blockers_in_shadow_map_hardware( 33 light_local: vec2<f32>, 43#ifdef PCSS_SAMPLERS_AVAILABLE 45#ifdef NO_ARRAY_TEXTURES_SUPPORT 46 let sampled_depth = textureSampleLevel( 47 view_bindings::directional_shadow_textures, 48 view_bindings::directional_shadow_textures_linear_sampler, 52#else // NO_ARRAY_TEXTURES_SUPPORT 53 let sampled_depth = textureSampleLevel( 54 view_bindings::directional_shadow_textures, 55 view_bindings::directional_shadow_textures_linear_sampler, 60#endif // NO_ARRAY_TEXTURES_SUPPORT 61 return select(vec2(0.0), vec2(sampled_depth, 1.0), sampled_depth >= depth); 63#else // PCSS_SAMPLERS_AVAILABLE 65#endif // PCSS_SAMPLERS_AVAILABLE 71const SPOT_SHADOW_TEXEL_SIZE: f32 = 0.0134277345; 72const POINT_SHADOW_SCALE: f32 = 0.003; 73const POINT_SHADOW_TEMPORAL_OFFSET_SCALE: f32 = 0.5; 79const D3D_SAMPLE_POINT_POSITIONS: array<vec2<f32>, 8> = array( 93const D3D_SAMPLE_POINT_COEFFS: array<f32, 8> = array( 105fn sample_shadow_map_castano_thirteen(light_local: vec2<f32>, depth: f32, array_index: i32) -> f32 { 106 let shadow_map_size = vec2<f32>(textureDimensions(view_bindings::directional_shadow_textures)); 107 let inv_shadow_map_size = 1.0 / shadow_map_size; 109 let uv = light_local * shadow_map_size; 110 var base_uv = floor(uv + 0.5); 111 let s = (uv.x + 0.5 - base_uv.x); 112 let t = (uv.y + 0.5 - base_uv.y); 114 base_uv *= inv_shadow_map_size; 116 let uw0 = (4.0 - 3.0 * s); 118 let uw2 = (1.0 + 3.0 * s); 120 let u0 = (3.0 - 2.0 * s) / uw0 - 2.0; 121 let u1 = (3.0 + s) / uw1; 122 let u2 = s / uw2 + 2.0; 124 let vw0 = (4.0 - 3.0 * t); 126 let vw2 = (1.0 + 3.0 * t); 128 let v0 = (3.0 - 2.0 * t) / vw0 - 2.0; 129 let v1 = (3.0 + t) / vw1; 130 let v2 = t / vw2 + 2.0; 134 sum += uw0 * vw0 * sample_shadow_map_hardware(base_uv + (vec2(u0, v0) * inv_shadow_map_size), depth, array_index); 135 sum += uw1 * vw0 * sample_shadow_map_hardware(base_uv + (vec2(u1, v0) * inv_shadow_map_size), depth, array_index); 136 sum += uw2 * vw0 * sample_shadow_map_hardware(base_uv + (vec2(u2, v0) * inv_shadow_map_size), depth, array_index); 138 sum += uw0 * vw1 * sample_shadow_map_hardware(base_uv + (vec2(u0, v1) * inv_shadow_map_size), depth, array_index); 139 sum += uw1 * vw1 * sample_shadow_map_hardware(base_uv + (vec2(u1, v1) * inv_shadow_map_size), depth, array_index); 140 sum += uw2 * vw1 * sample_shadow_map_hardware(base_uv + (vec2(u2, v1) * inv_shadow_map_size), depth, array_index); 142 sum += uw0 * vw2 * sample_shadow_map_hardware(base_uv + (vec2(u0, v2) * inv_shadow_map_size), depth, array_index); 143 sum += uw1 * vw2 * sample_shadow_map_hardware(base_uv + (vec2(u1, v2) * inv_shadow_map_size), depth, array_index); 144 sum += uw2 * vw2 * sample_shadow_map_hardware(base_uv + (vec2(u2, v2) * inv_shadow_map_size), depth, array_index); 146 return sum * (1.0 / 144.0); 149fn map(min1: f32, max1: f32, min2: f32, max2: f32, value: f32) -> f32 { 150 return min2 + (value - min1) * (max2 - min2) / (max1 - min1); 156fn random_rotation_matrix(scale: vec2<f32>, temporal: bool) -> mat2x2<f32> { 157 let random_angle = 2.0 * PI * interleaved_gradient_noise( 158 scale, select(1u, view_bindings::globals.frame_count, temporal)); 159 let m = vec2(sin(random_angle), cos(random_angle)); 169fn calculate_uv_offset_scale_jimenez_fourteen(texel_size: f32, blur_size: f32) -> vec2<f32> { 170 let shadow_map_size = vec2<f32>(textureDimensions(view_bindings::directional_shadow_textures)); 173 let f = map(0.00390625, 0.022949219, 0.015, 0.035, texel_size); 174 return f * blur_size / (texel_size * shadow_map_size); 177fn sample_shadow_map_jimenez_fourteen( 178 light_local: vec2<f32>, 181 frag_coord_xy: vec2<f32>, 186 let rotation_matrix = random_rotation_matrix(frag_coord_xy, temporal); 187 let uv_offset_scale = calculate_uv_offset_scale_jimenez_fourteen(texel_size, blur_size); 190 let sample_offset0 = (rotation_matrix * utils::SPIRAL_OFFSET_0_) * uv_offset_scale; 191 let sample_offset1 = (rotation_matrix * utils::SPIRAL_OFFSET_1_) * uv_offset_scale; 192 let sample_offset2 = (rotation_matrix * utils::SPIRAL_OFFSET_2_) * uv_offset_scale; 193 let sample_offset3 = (rotation_matrix * utils::SPIRAL_OFFSET_3_) * uv_offset_scale; 194 let sample_offset4 = (rotation_matrix * utils::SPIRAL_OFFSET_4_) * uv_offset_scale; 195 let sample_offset5 = (rotation_matrix * utils::SPIRAL_OFFSET_5_) * uv_offset_scale; 196 let sample_offset6 = (rotation_matrix * utils::SPIRAL_OFFSET_6_) * uv_offset_scale; 197 let sample_offset7 = (rotation_matrix * utils::SPIRAL_OFFSET_7_) * uv_offset_scale; 200 sum += sample_shadow_map_hardware(light_local + sample_offset0, depth, array_index); 201 sum += sample_shadow_map_hardware(light_local + sample_offset1, depth, array_index); 202 sum += sample_shadow_map_hardware(light_local + sample_offset2, depth, array_index); 203 sum += sample_shadow_map_hardware(light_local + sample_offset3, depth, array_index); 204 sum += sample_shadow_map_hardware(light_local + sample_offset4, depth, array_index); 205 sum += sample_shadow_map_hardware(light_local + sample_offset5, depth, array_index); 206 sum += sample_shadow_map_hardware(light_local + sample_offset6, depth, array_index); 207 sum += sample_shadow_map_hardware(light_local + sample_offset7, depth, array_index); 221fn search_for_blockers_in_shadow_map( 222 light_local: vec2<f32>, 228 let shadow_map_size = vec2<f32>(textureDimensions(view_bindings::directional_shadow_textures)); 229 let uv_offset_scale = search_size / (texel_size * shadow_map_size); 231 let offset0 = D3D_SAMPLE_POINT_POSITIONS[0] * uv_offset_scale; 232 let offset1 = D3D_SAMPLE_POINT_POSITIONS[1] * uv_offset_scale; 233 let offset2 = D3D_SAMPLE_POINT_POSITIONS[2] * uv_offset_scale; 234 let offset3 = D3D_SAMPLE_POINT_POSITIONS[3] * uv_offset_scale; 235 let offset4 = D3D_SAMPLE_POINT_POSITIONS[4] * uv_offset_scale; 236 let offset5 = D3D_SAMPLE_POINT_POSITIONS[5] * uv_offset_scale; 237 let offset6 = D3D_SAMPLE_POINT_POSITIONS[6] * uv_offset_scale; 238 let offset7 = D3D_SAMPLE_POINT_POSITIONS[7] * uv_offset_scale; 241 sum += search_for_blockers_in_shadow_map_hardware(light_local + offset0, depth, array_index); 242 sum += search_for_blockers_in_shadow_map_hardware(light_local + offset1, depth, array_index); 243 sum += search_for_blockers_in_shadow_map_hardware(light_local + offset2, depth, array_index); 244 sum += search_for_blockers_in_shadow_map_hardware(light_local + offset3, depth, array_index); 245 sum += search_for_blockers_in_shadow_map_hardware(light_local + offset4, depth, array_index); 246 sum += search_for_blockers_in_shadow_map_hardware(light_local + offset5, depth, array_index); 247 sum += search_for_blockers_in_shadow_map_hardware(light_local + offset6, depth, array_index); 248 sum += search_for_blockers_in_shadow_map_hardware(light_local + offset7, depth, array_index); 253 return sum.x / sum.y; 257 light_local: vec2<f32>, 260 frag_coord_xy: vec2<f32>, 263#ifdef SHADOW_FILTER_METHOD_GAUSSIAN 264 return sample_shadow_map_castano_thirteen(light_local, depth, array_index); 265#else ifdef SHADOW_FILTER_METHOD_TEMPORAL 266 return sample_shadow_map_jimenez_fourteen( 267 light_local, depth, array_index, frag_coord_xy, texel_size, 1.0, true); 268#else ifdef SHADOW_FILTER_METHOD_HARDWARE_2X2 269 return sample_shadow_map_hardware(light_local, depth, array_index); 290fn sample_shadow_map_pcss( 291 light_local: vec2<f32>, 294 frag_coord_xy: vec2<f32>, 299 let z_blocker = search_for_blockers_in_shadow_map( 300 light_local, depth, array_index, texel_size, light_size); 303 let blur_size = max((z_blocker - depth) * light_size / depth, 0.5); 310#ifdef SHADOW_FILTER_METHOD_TEMPORAL 311 return sample_shadow_map_jimenez_fourteen( 312 light_local, depth, array_index, frag_coord_xy, texel_size, blur_size, true); 313#else // SHADOW_FILTER_METHOD_TEMPORAL 314 return sample_shadow_map_jimenez_fourteen( 315 light_local, depth, array_index, frag_coord_xy, texel_size, blur_size, false); 316#endif // SHADOW_FILTER_METHOD_TEMPORAL 324fn sample_shadow_cubemap_hardware(light_local: vec3<f32>, depth: f32, light_id: u32) -> f32 { 325#ifdef NO_CUBE_ARRAY_TEXTURES_SUPPORT 326 return textureSampleCompare( 327 view_bindings::point_shadow_textures, 328 view_bindings::point_shadow_textures_comparison_sampler, 333 return textureSampleCompareLevel( 334 view_bindings::point_shadow_textures, 335 view_bindings::point_shadow_textures_comparison_sampler, 345fn search_for_blockers_in_shadow_cubemap_hardware( 346 light_local: vec3<f32>, 356#ifdef PCSS_SAMPLERS_AVAILABLE 358#ifdef NO_CUBE_ARRAY_TEXTURES_SUPPORT 359 let sampled_depth = textureSample( 360 view_bindings::point_shadow_textures, 361 view_bindings::point_shadow_textures_linear_sampler, 365 let sampled_depth = textureSample( 366 view_bindings::point_shadow_textures, 367 view_bindings::point_shadow_textures_linear_sampler, 373 return select(vec2(0.0), vec2(sampled_depth, 1.0), sampled_depth >= depth); 375#else // PCSS_SAMPLERS_AVAILABLE 377#endif // PCSS_SAMPLERS_AVAILABLE 382fn sample_shadow_cubemap_at_offset( 387 light_local: vec3<f32>, 391 return sample_shadow_cubemap_hardware( 392 light_local + position.x * x_basis + position.y * y_basis, 403fn search_for_blockers_in_shadow_cubemap_at_offset( 407 light_local: vec3<f32>, 411 return search_for_blockers_in_shadow_cubemap_hardware( 412 light_local + position.x * x_basis + position.y * y_basis, 423fn sample_shadow_cubemap_gaussian( 424 light_local: vec3<f32>, 427 distance_to_light: f32, 432 let basis = orthonormalize(normalize(light_local)) * scale * distance_to_light; 435 sum += sample_shadow_cubemap_at_offset( 436 D3D_SAMPLE_POINT_POSITIONS[0], D3D_SAMPLE_POINT_COEFFS[0], 437 basis[0], basis[1], light_local, depth, light_id); 438 sum += sample_shadow_cubemap_at_offset( 439 D3D_SAMPLE_POINT_POSITIONS[1], D3D_SAMPLE_POINT_COEFFS[1], 440 basis[0], basis[1], light_local, depth, light_id); 441 sum += sample_shadow_cubemap_at_offset( 442 D3D_SAMPLE_POINT_POSITIONS[2], D3D_SAMPLE_POINT_COEFFS[2], 443 basis[0], basis[1], light_local, depth, light_id); 444 sum += sample_shadow_cubemap_at_offset( 445 D3D_SAMPLE_POINT_POSITIONS[3], D3D_SAMPLE_POINT_COEFFS[3], 446 basis[0], basis[1], light_local, depth, light_id); 447 sum += sample_shadow_cubemap_at_offset( 448 D3D_SAMPLE_POINT_POSITIONS[4], D3D_SAMPLE_POINT_COEFFS[4], 449 basis[0], basis[1], light_local, depth, light_id); 450 sum += sample_shadow_cubemap_at_offset( 451 D3D_SAMPLE_POINT_POSITIONS[5], D3D_SAMPLE_POINT_COEFFS[5], 452 basis[0], basis[1], light_local, depth, light_id); 453 sum += sample_shadow_cubemap_at_offset( 454 D3D_SAMPLE_POINT_POSITIONS[6], D3D_SAMPLE_POINT_COEFFS[6], 455 basis[0], basis[1], light_local, depth, light_id); 456 sum += sample_shadow_cubemap_at_offset( 457 D3D_SAMPLE_POINT_POSITIONS[7], D3D_SAMPLE_POINT_COEFFS[7], 458 basis[0], basis[1], light_local, depth, light_id); 465fn sample_shadow_cubemap_jittered( 466 light_local: vec3<f32>, 468 frag_coord_xy: vec2<f32>, 470 distance_to_light: f32, 474 let rotation_matrix = random_rotation_matrix(frag_coord_xy, temporal); 476 let sample_offset0 = rotation_matrix * utils::SPIRAL_OFFSET_0_ * 477 POINT_SHADOW_TEMPORAL_OFFSET_SCALE; 478 let sample_offset1 = rotation_matrix * utils::SPIRAL_OFFSET_1_ * 479 POINT_SHADOW_TEMPORAL_OFFSET_SCALE; 480 let sample_offset2 = rotation_matrix * utils::SPIRAL_OFFSET_2_ * 481 POINT_SHADOW_TEMPORAL_OFFSET_SCALE; 482 let sample_offset3 = rotation_matrix * utils::SPIRAL_OFFSET_3_ * 483 POINT_SHADOW_TEMPORAL_OFFSET_SCALE; 484 let sample_offset4 = rotation_matrix * utils::SPIRAL_OFFSET_4_ * 485 POINT_SHADOW_TEMPORAL_OFFSET_SCALE; 486 let sample_offset5 = rotation_matrix * utils::SPIRAL_OFFSET_5_ * 487 POINT_SHADOW_TEMPORAL_OFFSET_SCALE; 488 let sample_offset6 = rotation_matrix * utils::SPIRAL_OFFSET_6_ * 489 POINT_SHADOW_TEMPORAL_OFFSET_SCALE; 490 let sample_offset7 = rotation_matrix * utils::SPIRAL_OFFSET_7_ * 491 POINT_SHADOW_TEMPORAL_OFFSET_SCALE; 495 let basis = orthonormalize(normalize(light_local)) * scale * distance_to_light; 498 sum += sample_shadow_cubemap_at_offset( 499 sample_offset0, 0.125, basis[0], basis[1], light_local, depth, light_id); 500 sum += sample_shadow_cubemap_at_offset( 501 sample_offset1, 0.125, basis[0], basis[1], light_local, depth, light_id); 502 sum += sample_shadow_cubemap_at_offset( 503 sample_offset2, 0.125, basis[0], basis[1], light_local, depth, light_id); 504 sum += sample_shadow_cubemap_at_offset( 505 sample_offset3, 0.125, basis[0], basis[1], light_local, depth, light_id); 506 sum += sample_shadow_cubemap_at_offset( 507 sample_offset4, 0.125, basis[0], basis[1], light_local, depth, light_id); 508 sum += sample_shadow_cubemap_at_offset( 509 sample_offset5, 0.125, basis[0], basis[1], light_local, depth, light_id); 510 sum += sample_shadow_cubemap_at_offset( 511 sample_offset6, 0.125, basis[0], basis[1], light_local, depth, light_id); 512 sum += sample_shadow_cubemap_at_offset( 513 sample_offset7, 0.125, basis[0], basis[1], light_local, depth, light_id); 517fn sample_shadow_cubemap( 518 light_local: vec3<f32>, 519 distance_to_light: f32, 522 frag_coord_xy: vec2<f32>, 524#ifdef SHADOW_FILTER_METHOD_GAUSSIAN 525 return sample_shadow_cubemap_gaussian( 526 light_local, depth, POINT_SHADOW_SCALE, distance_to_light, light_id); 527#else ifdef SHADOW_FILTER_METHOD_TEMPORAL 528 return sample_shadow_cubemap_jittered( 529 light_local, depth, frag_coord_xy, POINT_SHADOW_SCALE, distance_to_light, light_id, true); 530#else ifdef SHADOW_FILTER_METHOD_HARDWARE_2X2 531 return sample_shadow_cubemap_hardware(light_local, depth, light_id); 548fn search_for_blockers_in_shadow_cubemap( 549 light_local: vec3<f32>, 552 distance_to_light: f32, 557 let basis = orthonormalize(normalize(light_local)) * scale * distance_to_light; 559 var sum: vec2<f32> = vec2(0.0); 560 sum += search_for_blockers_in_shadow_cubemap_at_offset( 561 D3D_SAMPLE_POINT_POSITIONS[0], basis[0], basis[1], light_local, depth, light_id); 562 sum += search_for_blockers_in_shadow_cubemap_at_offset( 563 D3D_SAMPLE_POINT_POSITIONS[1], basis[0], basis[1], light_local, depth, light_id); 564 sum += search_for_blockers_in_shadow_cubemap_at_offset( 565 D3D_SAMPLE_POINT_POSITIONS[2], basis[0], basis[1], light_local, depth, light_id); 566 sum += search_for_blockers_in_shadow_cubemap_at_offset( 567 D3D_SAMPLE_POINT_POSITIONS[3], basis[0], basis[1], light_local, depth, light_id); 568 sum += search_for_blockers_in_shadow_cubemap_at_offset( 569 D3D_SAMPLE_POINT_POSITIONS[4], basis[0], basis[1], light_local, depth, light_id); 570 sum += search_for_blockers_in_shadow_cubemap_at_offset( 571 D3D_SAMPLE_POINT_POSITIONS[5], basis[0], basis[1], light_local, depth, light_id); 572 sum += search_for_blockers_in_shadow_cubemap_at_offset( 573 D3D_SAMPLE_POINT_POSITIONS[6], basis[0], basis[1], light_local, depth, light_id); 574 sum += search_for_blockers_in_shadow_cubemap_at_offset( 575 D3D_SAMPLE_POINT_POSITIONS[7], basis[0], basis[1], light_local, depth, light_id); 580 return sum.x / sum.y; 588fn sample_shadow_cubemap_pcss( 589 light_local: vec3<f32>, 590 distance_to_light: f32, 594 frag_coord_xy: vec2<f32>, 596 let z_blocker = search_for_blockers_in_shadow_cubemap( 597 light_local, depth, light_size, distance_to_light, light_id); 600 let blur_size = max((z_blocker - depth) * light_size / depth, 0.5); 602#ifdef SHADOW_FILTER_METHOD_TEMPORAL 603 return sample_shadow_cubemap_jittered( 604 light_local, depth, frag_coord_xy, POINT_SHADOW_SCALE * blur_size, distance_to_light, light_id, true); 606 return sample_shadow_cubemap_jittered( 607 light_local, depth, frag_coord_xy, POINT_SHADOW_SCALE * blur_size, distance_to_light, light_id, false);