R2 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
sThe shadow
momentsThe sampled shadow map depth moments
depthThe 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 }
float variance_minimum
The minimum variance level. Used to eliminate shadow bias. Typically 0.00002f will suffice for all sc...

§ 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
sThe shadow
pThe 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 }
float R2_varianceLinearStep(const float min, const float max, const float x)
float bleed_reduction
The amount of bleed reduction (typically [0.2, 1.0]). Setting this value too high results in loss of ...

§ 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
minThe minimum value
maxThe maximum value
xThe 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
sThe shadow
vThe 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 =
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 =
115 
116  return max (p_reduced, s.factor_minimum);
117 }
vec4 surface_light_eye
The surface position in light-eye-space.
float R2_logDepthEncodePartial(const float z, const float depth_coefficient)
Definition: R2LogDepth.h:33
sampler2D map
The variance shadow map.
vec2 surface_light_uv
The surface position as UV coordinates from the perspective of the light.
float R2_varianceLightBleedReduction(const R2_shadow_variance_t s, const float p_max)
float R2_logDepthPrepareEyeZ(const float z)
Definition: R2LogDepth.h:15
float depth_coefficient
The logarithmic depth coefficient that was used to encode depth values.
float factor_minimum
The minimum level of attenuation by the shadow (0.0 means "completely attenuated", 1.0 means "no attenuation")
float R2_varianceChebyshevUpperBound(const R2_shadow_variance_t s, const vec2 moments, const float depth)