Advanced search tips: use spaces to require multiple terms (AND), | for alternatives (OR), wrap phrases in quotes (="a b"), =term for exact match, 'term to include, !term to exclude, ^term to match prefix, !^term to exclude prefix, .ext$ to match suffix, and !.ext$ to exclude suffix.

shadow_sampling.wgsl

Import path

Constants

SPOT_SHADOW_TEXEL_SIZE

#
const SPOT_SHADOW_TEXEL_SIZE: f32 = 0.0134277345

POINT_SHADOW_SCALE

#
const POINT_SHADOW_SCALE: f32 = 0.003

POINT_SHADOW_TEMPORAL_OFFSET_SCALE

#
const POINT_SHADOW_TEMPORAL_OFFSET_SCALE: f32 = 0.5

Functions

sample_shadow_map_hardware

#

Do the lookup, using HW 2x2 PCF and comparison

fn sample_shadow_map_hardware (
light_local: vec2<f32>
depth: f32
array_index: i32
) -> f32

search_for_blockers_in_shadow_map_hardware

#

Does a single sample of the blocker search, a part of the PCSS algorithm. This is the variant used for directional lights.

fn search_for_blockers_in_shadow_map_hardware (
light_local: vec2<f32>
depth: f32
array_index: i32
) -> vec2<f32>

sample_shadow_map_castano_thirteen

#
fn sample_shadow_map_castano_thirteen (
light_local: vec2<f32>
depth: f32
array_index: i32
) -> f32

map

#
fn map (
min1: f32
max1: f32
min2: f32
max2: f32
value: f32
) -> f32

random_rotation_matrix

#
fn random_rotation_matrix (
temporal: bool
) -> mat2x2<f32>

calculate_uv_offset_scale_jimenez_fourteen

#

Calculates the distance between spiral samples for the given texel size and penumbra size. This is used for the Jimenez ‘14 (i.e. temporal) variant of shadow sampling.

fn calculate_uv_offset_scale_jimenez_fourteen (
texel_size: f32
blur_size: f32
) -> vec2<f32>

sample_shadow_map_jimenez_fourteen

#
fn sample_shadow_map_jimenez_fourteen (
light_local: vec2<f32>
depth: f32
array_index: i32
texel_size: f32
blur_size: f32
temporal: bool
) -> f32

search_for_blockers_in_shadow_map

#

search_size is the size of the search region in texels.

fn search_for_blockers_in_shadow_map (
light_local: vec2<f32>
depth: f32
array_index: i32
texel_size: f32
search_size: f32
) -> f32

sample_shadow_map

#
fn sample_shadow_map (
light_local: vec2<f32>
depth: f32
array_index: i32
texel_size: f32
) -> f32

sample_shadow_map_pcss

#
fn sample_shadow_map_pcss (
light_local: vec2<f32>
depth: f32
array_index: i32
texel_size: f32
light_size: f32
) -> f32

sample_shadow_cubemap_hardware

#

NOTE: Due to the non-uniform control flow in shadows::fetch_point_shadow, we must use the Level variant of textureSampleCompare to avoid undefined behavior due to some of the fragments in a quad (2x2 fragments) being processed not being sampled, and this messing with mip-mapping functionality. The shadow maps have no mipmaps so Level just samples from LOD 0.

fn sample_shadow_cubemap_hardware (
light_local: vec3<f32>
depth: f32
light_id: u32
) -> f32

search_for_blockers_in_shadow_cubemap_hardware

#

Performs one sample of the blocker search. This variation of the blocker search function is for point and spot lights.

fn search_for_blockers_in_shadow_cubemap_hardware (
light_local: vec3<f32>
depth: f32
light_id: u32
) -> vec2<f32>

sample_shadow_cubemap_at_offset

#
fn sample_shadow_cubemap_at_offset (
position: vec2<f32>
coeff: f32
x_basis: vec3<f32>
y_basis: vec3<f32>
light_local: vec3<f32>
depth: f32
light_id: u32
) -> f32

search_for_blockers_in_shadow_cubemap_at_offset

#

x_basis, y_basis, and light_local form an orthonormal basis over which the blocker search happens.

fn search_for_blockers_in_shadow_cubemap_at_offset (
position: vec2<f32>
x_basis: vec3<f32>
y_basis: vec3<f32>
light_local: vec3<f32>
depth: f32
light_id: u32
) -> vec2<f32>

sample_shadow_cubemap_gaussian

#

This more or less does what Castano13 does, but in 3D space. Castano13 is essentially an optimized 2D Gaussian filter that takes advantage of the bilinear filtering hardware to reduce the number of samples needed. This trick doesn’t apply to cubemaps, so we manually apply a Gaussian filter over the standard 8xMSAA pattern instead.

fn sample_shadow_cubemap_gaussian (
light_local: vec3<f32>
depth: f32
scale: f32
distance_to_light: f32
light_id: u32
) -> f32

sample_shadow_cubemap_jittered

#

This is a port of the Jimenez14 filter above to the 3D space. It jitters the points in the spiral pattern after first creating a 2D orthonormal basis along the principal light direction.

fn sample_shadow_cubemap_jittered (
light_local: vec3<f32>
depth: f32
scale: f32
distance_to_light: f32
light_id: u32
temporal: bool
) -> f32

sample_shadow_cubemap

#
fn sample_shadow_cubemap (
light_local: vec3<f32>
distance_to_light: f32
depth: f32
light_id: u32
) -> f32

search_for_blockers_in_shadow_cubemap

#

The scale parameter represents the size of the light.

fn search_for_blockers_in_shadow_cubemap (
light_local: vec3<f32>
depth: f32
scale: f32
distance_to_light: f32
light_id: u32
) -> f32

sample_shadow_cubemap_pcss

#
fn sample_shadow_cubemap_pcss (
light_local: vec3<f32>
distance_to_light: f32
depth: f32
light_id: u32
light_size: f32
) -> f32