R2 Core Shaders

Data Structures | Functions
R2ShadowVariance.h File Reference

Functions for variance shadows. More...

`#include "R2LogDepth.h"`
Include dependency graph for R2ShadowVariance.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Data Structures
struct | R2_shadow_variance_t
| A variance shadow. More...

## Functions
float | R2_varianceChebyshevUpperBound (const R2_shadow_variance_t s, const vec2 moments, const float depth)
float | R2_varianceLinearStep (const float min, const float max, const float x)
float | R2_varianceLightBleedReduction (const R2_shadow_variance_t s, const float p_max)
float | R2_varianceShadowFactor (const R2_shadow_variance_t s, const R2_light_projective_vectors_t v)

## Detailed Description

Functions for variance shadows.

Definition in file R2ShadowVariance.h.

## Function Documentation

## § R2_varianceChebyshevUpperBound()

float R2_varianceChebyshevUpperBound | ( | const R2_shadow_variance_t | s,
|  | const vec2 | moments,
|  | const float | depth
| ) |  |

Compute an upper bound on the probability that the position at `depth` is in shadow.

Parameters

  s | The shadow
  moments | The sampled shadow map depth moments
  depth | The current depth

Returns
  A value between `0.0` and `1.0`, where 1.0 indicates that the area is certainly in shadow.

Definition at line 38 of file R2ShadowVariance.h.

42 {
43 float p = float (depth <= moments.x);
44 float variance = max (s.variance_minimum, moments.y - (moments.x * moments.x));
45 float delta = depth - moments.x;
46 float p_max = variance / (variance + (delta * delta));
47 return max (p, p_max);
48 }
R2_shadow_variance_t::variance_minimum
float variance_minimum
The minimum variance level. Used to eliminate shadow bias. Typically 0.00002f will suffice for all sc...
Definition: R2ShadowVariance.h:15

## § R2_varianceLightBleedReduction()

float R2_varianceLightBleedReduction | ( | const R2_shadow_variance_t | s,
|  | const float | p_max
| ) |  |

Apply light bleed reduction to the given shadow probability.

Parameters

  s | The shadow
  p | The shadow probability

Returns
  `p` with light bleed reduction applied

Definition at line 79 of file R2ShadowVariance.h.

82 {
83 return R2_varianceLinearStep (s.bleed_reduction, 1.0, p_max);
84 }
R2_varianceLinearStep
float R2_varianceLinearStep(const float min, const float max, const float x)
Definition: R2ShadowVariance.h:61
R2_shadow_variance_t::bleed_reduction
float bleed_reduction
The amount of bleed reduction (typically [0.2, 1.0]). Setting this value too high results in loss of ...
Definition: R2ShadowVariance.h:18

## § R2_varianceLinearStep()

float R2_varianceLinearStep | ( | const float | min,
|  | const float | max,
|  | const float | x
| ) |  |

A linear step function that maps all values less than `min` to `min`, all values greater than `max` to `max`, and linearly scales all values in `[min, max]` to `[min, max]`.

Parameters

  min | The minimum value
  max | The maximum value
  x | The value to scale

Returns
  A linearly scaled value

Definition at line 61 of file R2ShadowVariance.h.

65 {
66 float vsm = x - min;
67 float msm = max - min;
68 return clamp (vsm / msm, 0.0, 1.0);
69 }

## § R2_varianceShadowFactor()

float R2_varianceShadowFactor | ( | const R2_shadow_variance_t | s,
|  | const R2_light_projective_vectors_t | v
| ) |  |

Calculate a shadow factor for the given position.

Parameters

  s | The shadow
  v | The projective light vectors, including the position in various light coordinate spaces

Returns
  `s.factor_minimum` if the point is fully in shadow, or `1.0` if the point is

Definition at line 95 of file R2ShadowVariance.h.

98 {
99 float pos_light_depth =
100 R2_logDepthEncodePartial(
101 R2_logDepthPrepareEyeZ (v.surface_light_eye.z),
102 s.depth_coefficient);
103
104 // Sample the variance map for the depth distribution
105 vec2 moments =
106 texture (s.map, v.surface_light_uv.xy).xy;
107
108 // Calculate the probability that the point is in shadow.
109 float p_max =
110 R2_varianceChebyshevUpperBound (s, moments, pos_light_depth);
111
112 // Apply light bleeding reduction
113 float p_reduced =
114 R2_varianceLightBleedReduction (s, p_max);
115
116 return max (p_reduced, s.factor_minimum);
117 }
R2_light_projective_vectors_t::surface_light_eye
vec4 surface_light_eye
The surface position in light-eye-space.
Definition: R2LightProjective.h:16
R2_logDepthEncodePartial
float R2_logDepthEncodePartial(const float z, const float depth_coefficient)
Definition: R2LogDepth.h:33
R2_shadow_variance_t::map
sampler2D map
The variance shadow map.
Definition: R2ShadowVariance.h:24
R2_light_projective_vectors_t::surface_light_uv
vec2 surface_light_uv
The surface position as UV coordinates from the perspective of the light.
Definition: R2LightProjective.h:25
R2_varianceLightBleedReduction
float R2_varianceLightBleedReduction(const R2_shadow_variance_t s, const float p_max)
Definition: R2ShadowVariance.h:79
R2_logDepthPrepareEyeZ
float R2_logDepthPrepareEyeZ(const float z)
Definition: R2LogDepth.h:15
R2_shadow_variance_t::depth_coefficient
float depth_coefficient
The logarithmic depth coefficient that was used to encode depth values.
Definition: R2ShadowVariance.h:21
R2_shadow_variance_t::factor_minimum
float factor_minimum
The minimum level of attenuation by the shadow (0.0 means "completely attenuated", 1.0 means "no attenuation")
Definition: R2ShadowVariance.h:12
R2_varianceChebyshevUpperBound
float R2_varianceChebyshevUpperBound(const R2_shadow_variance_t s, const vec2 moments, const float depth)
Definition: R2ShadowVariance.h:38