Shaders for Shadows
Shader code for rendering shadows of translucent occluders, soft shadows and single scattering with moment shadow maps.
 All Classes Files Functions Variables Pages
Macros | Functions
ParticipatingMediaUtility.fx File Reference

Go to the source code of this file.

Macros

#define TEXEL_TYPE   float2
 
#define TEXEL_TYPE   float3
 
#define TEXEL_TYPE   float4
 

Functions

float3 RectifiedToCartesian (float Distance, float Inclination, float Azimuth)
 
void CartesianToRectified (out float OutDistance, out float OutInclination, out float OutAzimuth, float3 CartesianCoordinates)
 
void GetSparseRepresentation4Moments (out float2 OutDepth, out float OutWeight1, float4 OptimizedMoments)
 
float EstimateIntegralFrom4Moments (float IntervalEnd, float4 OptimizedMoments, float MomentBias, float OverestimationWeight)
 
float3 GetRoots (float4 Coefficient)
 
float EstimateIntegralFrom6Moments (float IntervalEnd, float2 OptimizedMoments0, float4 OptimizedMoments1, float MomentBias, float OverestimationWeight)
 
float ComputeTexelTransmittance (uint ShadowMapWidth, float ExtinctionCoefficient, float2 DistanceMinMax, float2 InclinationMinMax)
 
void PrepareShadowMapRowResampling (out float3 OutTextureSpaceRayStart, out float3 OutTextureSpaceRayOffset, out float2 OutInclinationToDepthTransform, out float OutOffsetLength, out float OutInitialOffsetLength, out float OutTexelTransmittance, uint2 OutputShadowMapResolution, uint iShadowMapRow, float ExtinctionCoefficient, float4x4 RectificationToShadowMapProjectionSpace, float2 DistanceMinMax, float2 InclinationMinMax, float2 AzimuthMinMax)
 
float ConvertShadowMapDepthNonLinearRectification (float ShadowMapDepth, float2 ShadowMapToRectificationDepthTransform, float2 InclinationToDepthTransform, float TotalOffsetLength)
 
void Convert4MomentCanonicalToOptimized (out float4 OutQuantizedMoments0, float4 CanonicalMoments0)
 
void ComputeMomentVector4MomentsOptimized (out float4 OutQuantizedMoments0, float Depth)
 
void Convert6MomentCanonicalToOptimized (out float2 OutQuantizedMoments0, out float4 OutQuantizedMoments1, float2 CanonicalMoments0, float4 CanonicalMoments1)
 
void ComputeMomentVector6MomentsOptimized (out float2 OutQuantizedMoments0, out float4 OutQuantizedMoments1, float Depth)
 
void ApplyTransmittanceWeightedPrefixSum (inout RWTexture2D< TEXEL_TYPE > OutFilteredShadowMap, Texture2D< TEXEL_TYPE > FilterableShadowMap, uint iThreadX, uint iThreadY, float TexelTransmittance)
 

Detailed Description

This header defines utility functions that help to avoid code duplication among different techniques for integrating single scattering in participating media.

Definition in file ParticipatingMediaUtility.fx.

Function Documentation

void ApplyTransmittanceWeightedPrefixSum ( inout RWTexture2D< TEXEL_TYPE >  OutFilteredShadowMap,
Texture2D< TEXEL_TYPE >  FilterableShadowMap,
uint  iThreadX,
uint  iThreadY,
float  TexelTransmittance 
)

This function generates a transmittance-weighted prefix sum for an already rectified filterable shadow map. It only writes to every eights texel in its row and thus implements a single thread of a thread group of width 8.

Parameters
OutFilteredShadowMapThe texture to which the output is written.
FilterableShadowMapThe rectified, filterable shadow map from which the input is read. It must have resolution identical to OutFilteredShadowMap.
iThreadXThe thread will write to columns of the form 8*x+iThreadX where x iterates over all integers yielding a coordinate within the texture.
iThreadYThe index of the row from which the function will read and to which it will write.
TexelTransmittanceThe source texel in column x is weighted by TexelTransmittance^x. Output values are divided by the total weight of the prefix sum. See ComputeTexelTransmittance().

Definition at line 417 of file ParticipatingMediaUtility.fx.

void CartesianToRectified ( out float  OutDistance,
out float  OutInclination,
out float  OutAzimuth,
float3  CartesianCoordinates 
)

This function converts the given position in Cartesian coordinates of rectification space into rectified coordinates. It is a right inverse of RectifiedToCartesian().

Parameters
OutDistanceThe non-negative distance from the origin after projecting to the z=0 plane.
OutInclinationThe angle to the z-axis in radians from zero to pi.
OutAzimuthThe planar angle to the x-axis in radians from zero to two pi.
CartesianCoordinatesThe Cartesian coordinates that are to be converted.

Definition at line 25 of file ParticipatingMediaUtility.fx.

void ComputeMomentVector4MomentsOptimized ( out float4  OutQuantizedMoments0,
float  Depth 
)

Converts a given depth value to a vector encoding four moments of a Dirac-delta distribution at this depth. An affine transform is applied to the moments to prepare them for storage in a texture with minimal loss of information due to quantization.

Definition at line 362 of file ParticipatingMediaUtility.fx.

void ComputeMomentVector6MomentsOptimized ( out float2  OutQuantizedMoments0,
out float4  OutQuantizedMoments1,
float  Depth 
)

Converts a given depth value to a vector encoding six moments of a Dirac-delta distribution at this depth. The moments are output as two vectors and an affine transform is applied to them to prepare them for storage in a texture with minimal loss of information due to quantization.

Definition at line 393 of file ParticipatingMediaUtility.fx.

float ComputeTexelTransmittance ( uint  ShadowMapWidth,
float  ExtinctionCoefficient,
float2  DistanceMinMax,
float2  InclinationMinMax 
)

This function computes the amount of light that is transmitted through one texel of a rectified shadow map horizontally at depth zero.

Parameters
ShadowMapWidthThe width of the shadow map in texels.
ExtinctionCoefficientThe extinction coefficient of the participating media.
DistanceMinMax,InclinationMinMaxSee GetRectifiedSpaceFrustumBounds().

Definition at line 277 of file ParticipatingMediaUtility.fx.

void Convert4MomentCanonicalToOptimized ( out float4  OutQuantizedMoments0,
float4  CanonicalMoments0 
)

Applies the optimized quantization transform to the given four power moments.

Definition at line 352 of file ParticipatingMediaUtility.fx.

void Convert6MomentCanonicalToOptimized ( out float2  OutQuantizedMoments0,
out float4  OutQuantizedMoments1,
float2  CanonicalMoments0,
float4  CanonicalMoments1 
)

Applies the optimized quantization transform to the given six power moments.

Definition at line 369 of file ParticipatingMediaUtility.fx.

float ConvertShadowMapDepthNonLinearRectification ( float  ShadowMapDepth,
float2  ShadowMapToRectificationDepthTransform,
float2  InclinationToDepthTransform,
float  TotalOffsetLength 
)

This function converts a depth taken from a shadow map and maps it to a depth value as used for non-linear rectification.

Parameters
ShadowMapDepthThe depth value obtained from the shadow map mapped into the interval [-1,1].
ShadowMapToRectificationDepthTransform,InclinationToDepthTransformSee PrepareShadowMapRowResampling().
TotalOffsetLengthThe number of times the offset has been applied to the ray start times the length of the offset as generated by PrepareShadowMapRowResampling().
Returns
The resampled depth value. It is obtained from a shadow map sample at the given texture coordinate using a non-linear mapping.

Definition at line 344 of file ParticipatingMediaUtility.fx.

float EstimateIntegralFrom4Moments ( float  IntervalEnd,
float4  OptimizedMoments,
float  MomentBias,
float  OverestimationWeight 
)

Estimates the integral of a probability distribution from minus infinity to a fixed point and returns it.

Parameters
IntervalEndThe end of the integration interval.
OptimizedMomentsPower moments one to four of the distribution stored in a a manner that is optimized for little quantization errors.
MomentBiasThe strength of the bias that serves to compensate quantization errors (e.g. 6*10^-5).
OverestimationWeightIf this value is zero, the function underestimates the integral. If it is one, it overestimates. Inbetween linear interpolation is employed.

Definition at line 77 of file ParticipatingMediaUtility.fx.

float EstimateIntegralFrom6Moments ( float  IntervalEnd,
float2  OptimizedMoments0,
float4  OptimizedMoments1,
float  MomentBias,
float  OverestimationWeight 
)

Like EstimateIntegralFrom4Moments() but uses six moments which have been stored in a manner that is optimized for small quantization error.

Definition at line 175 of file ParticipatingMediaUtility.fx.

float3 GetRoots ( float4  Coefficient)

Given the coefficients of a real, cubic polynomial Coefficient[0] +x*Coefficient[1]+x*x*Coefficient[2]+x*x*x*Coefficient[3] this function computes all of its roots and returns them.

Warning
Behavior is undefined if the polynomial does not have three real roots.
Note
The implementation is a cheap and specialized version of the algorithm proposed by James F. Blinn in his column "How to Solve a Cubic Equation". The full algorithm is summarized in part 5, published in IEEE Computer Graphics and Applications, vol. 27, no. 3.

Definition at line 138 of file ParticipatingMediaUtility.fx.

void GetSparseRepresentation4Moments ( out float2  OutDepth,
out float  OutWeight1,
float4  OptimizedMoments 
)

This function constructs a linear combination of two Dirac-delta distributions which matches three given moments.

Parameters
OutDepthThe locations of the Dirac-delta distributions. They are sorted.
OutWeight1The weight of the second Dirac-delta. The weights add up to one.
OptimizedMomentsFour moments stored in a manner that is optimized for quantization. The fourth moment is not used.

Definition at line 40 of file ParticipatingMediaUtility.fx.

void PrepareShadowMapRowResampling ( out float3  OutTextureSpaceRayStart,
out float3  OutTextureSpaceRayOffset,
out float2  OutInclinationToDepthTransform,
out float  OutOffsetLength,
out float  OutInitialOffsetLength,
out float  OutTexelTransmittance,
uint2  OutputShadowMapResolution,
uint  iShadowMapRow,
float  ExtinctionCoefficient,
float4x4  RectificationToShadowMapProjectionSpace,
float2  DistanceMinMax,
float2  InclinationMinMax,
float2  AzimuthMinMax 
)

This is a helper function for functions generating transmittance weighted shadow maps with resampling of a shadow map for non-linear rectification. It precomputes various useful quantities that can be prepared before entering a loop over a row of a shadow map.

Parameters
OutTextureSpaceRayStart,OutTextureSpaceRayOffsetCoordinates defining the ray which is to be traced in the source shadow map. OutTextureSpaceRayStart correspons to the left-most texel. They use projective coordinates, you should divide by z to get the texture coordinate in xy.
OutInclinationToDepthTransformA linear transform mapping inclination in radians to a value that can be used as depth value for the rectified shadow map.
OutTexelTransmittanceSee ComputeTexelTransmittance().
OutputShadowMapResolutionThe resolution of the shadow map in which prefix sums will be stored.
iShadowMapRowThe index of the row in the output shadow map (i.e. the non-negative integer y-coordinate).
ExtinctionCoefficientThe extinction coefficient of the participating media.
RectificationToShadowMapProjectionSpaceSee ApplyTransmittanceWeightedPrefixSumNonLinearRectificationMSM4().
DistanceMinMax,InclinationMinMax,AzimuthMinMaxSee GetRectifiedSpaceFrustumBounds().
See Also
ApplyTransmittanceWeightedPrefixSumNonLinearRectificationMSM4()

Definition at line 306 of file ParticipatingMediaUtility.fx.

float3 RectifiedToCartesian ( float  Distance,
float  Inclination,
float  Azimuth 
)

Converts the given rectified coordinates to Cartesian coordinates in rectification space and returns the result.

See Also
CartesianToRectified()

Definition at line 8 of file ParticipatingMediaUtility.fx.