summaryrefslogtreecommitdiff
path: root/tests/hlsl/dxsdk/VarianceShadows11/RenderVarianceScene.hlsl
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2018-12-07 13:31:06 -0800
committerGitHub <noreply@github.com>2018-12-07 13:31:06 -0800
commit135eaff6b892fc91a398714ddcf7ef377cd4cccb (patch)
treee69f30a4fadfdb834ea141c1ec9efc862ccc70d3 /tests/hlsl/dxsdk/VarianceShadows11/RenderVarianceScene.hlsl
parentb0c2423f00b910f2f4d5010e6a04114112e294fd (diff)
Change how buffers are emitted (#741)
* Change how buffers are emitted This is a change with a lot of pieces, which can't always be separated out cleanly. I'm going to walk through them in what I hope is a logical order. The main goal of this change was to allow arrays of structured buffers to translate to Vulkan. Consider two declarations of structured buffers in HLSL/Slang: ```hlsl StructuredBuffer<X> single; StructuredBuffer<Y> multiple[10]; ``` The current translation logic was handling `single` by translating it into an *unnamed* GLSL `buffer` block like: ```glsl layout(std430) buffer _S1 { X single[]; }; ``` That syntax allows an expression like `single[i]` in Slang to be translated simply as `single[i]` in GLSL. But that naive translating doesn't work for `multiple`, since we need to declare a array of blocks in GLSL, which requires giving the whole thing a name: ```glsl layout(std430) buffer _S2 { Y _data[]; } multiple[10]; ``` Now a reference to `multiple[i][j]` in Slang needs to become `multiple[i]._data[j]` in GLSL. To avoid having way too many special cases around single structured buffers vs. arrays, it makes sense to allows emit things in the latter form, so that we instead lower `single` as: ```glsl layout(std430) buffer _S1 { X _data[]; } single; ``` So that now a reference to `single[i]` becomes `single._data[i]` in GLSL. Most of that can be handled in the standard library translation of the structured buffer indexing operations. The only wrinkle there is that there were some *old* special-case instructions in the IR intended to handle buffer load/store operations (these were added back when I was trying to keep the "VM" path working). These aren't really needed to have structured-buffer operations work; they can be handled as ordinary functions as far as the stdlib is concerned. I removed the old instructions. Along the way, it became clear that a few other cases follow the same pattern. Byte-addressed buffers are an obvious case. We were lowering HLSL/Slang: ```hlsl ByteAddressBuffer b; ... uint x = b.Load(0); ``` to GLSL like: ```glsl layout(std430) buffer _S1 { uint b[]; }; ... uint x = b[0]; ``` That logic would fail for arrays the same way that the structured buffer case was failing. The fix is the same: use named `buffer` blocks and then introduce an explicit `_data` field: ```glsl layout(std430) buffer _S1 { uint _data[]; } b; ... uint x = b._data[0]; ``` Just like with structured buffers, all of the VK translation for operations on byte-addressed buffers can be implemented directly in teh stdlib, so once the emit logic was changed it was just a matter of adding `._data` to a bunch of VK tranlsations. It turns out that arrays of constant buffers have more or less the same problem, and furthermore we have some problems with any code that directly uses the modern HLSL `ConstantBuffer<T>` type. Note: the emit logic around constant buffers sometimes refers to "parameter groups" because that is being used in the compiler as a catch-all term for constant buffers, texture buffers, and parameter blocks. The existing code was going out of its way to reproduce the way that constant buffer declarations are implicitly referenced in HLSL: ```hlsl cbuffer C { float f; } ... float tmp = f; // No reference to `C` here ``` This can be seen in the emit logic with the `isDerefBaseImplicit` function, which is used to take the internal IR representation for a reference to `f` (which is closer to the expression `(*C).f` or `C->f`) and leave off any reference to `C` so that we emit just `f`. That kind of logic just flat out doesn't work in some important cases. Arrays of constant buffers are a clear one: ```hlsl ConstantBuffer<X> cbArray[3]; ... X x = cbArray[0]; ``` There is no way to translate that to an ordinary `cbuffer` declaration at all. The same problem can be created without arrays, though: ```hlsl ConstantBuffer<X> singleCB; ... X x = singleCB; ``` The current strategy for translating constant buffers was translating `singleCB` into a `cbuffer` declaration that reproduced the fields of `X` as its members, which just wouldn't work: ```hlsl cbuffer singleCB { float f; // field of `X` } ... X x = singleCB; // ERROR: there is nothing named `singleCB` in this HLSL ``` The new strategy is more consistent. We still generate a `cbuffer` declaration for a single constant buffer, but we always give it a single field of the chosen element type: ```hlsl cbuffer singleCB { X singleCB; } ... X x = singleCB; // this works fine! ``` And in the array case we generate code that uses the explicit `ConstantBuffer<T>` type: ```hlsl ConstantBuffer<X> cbArray[3]; ... X x = cbArray[0]; ``` The GLSL output is more complicated because unlike with HLSL there is no implicit conversion from a uniform block to its element type (there is no notion of an element type). The array case thus needs a `_data` field similar to what we do for structured buffers: ```glsl layout(std140) uniform _S3 { X _data; } cbArray[3]; ... X x = cbArray[0]._data; ``` And then the non-array case needs to have a similar `_data` field for consistency: ```glsl layout(std140) uniform _S1 { X _data; } singleCB; ... X x = singleCB._data; ``` This is handled by inserting the necessary reference to `_data` whenever we dereference a constant buffer, either as part of a load instruction (loading from the whole CB as a pointer), or an `IRFieldAddress` instruction which forms a pointer into the CB (e.g., `&(singleCB->f)` becomes `singleCB._data.f`). The current emit logic handles `ParameterBlock<X>` differently from `ConstantBuffer<X>`, but really only to allow parameter blocks to be explicitly named in the output, while constant buffers were left implicit by default. Thus the only difference was a legacy one (from back when trying to exactly reproduce the HLSL text we got as input was considered an important goal), and the new approach to emitting constant buffers would get rid of it. I removed the separate logic for emitting `ParameterBlock<X>` and just let the handling for constant buffers deal with it. Note that any resource types inside of a `ParameterBlock<X>` would have been moved out as part of legalization, so that a parameter block is 100% equivalent to a constant buffer when it comes time to emit code. Unsurprisingly, changing the way we generate HLSL and GLSL output for all these buffer types meant that any tests that were directly comparing the output of `slangc` against `fxc`, `dxc`, or `glslang` broke. The basic approach to fixing the breakage in GLSL tests was to update the GLSL baseline to reflect the new output startegy. In some cases I used macros to name the various `_S<digits>` temporaries so that future renaming will hopefully be easier (it would be great if we auto-generated temporary names with a bit more context). There was one GLSL test (`tests/bugs/vk-structured-buffer-binding`) that was using raw GLSL expected output, and this was changed to use a GLSL baseline to generate SPIR-V for comparison. For HLSL tests we were sometimes running the same input file through `slangc` and `fxc`/`dxc`, and in these cases I macro-ized the various `cbuffer` declarations to generate different declarations depending on the compiler. I completely dropped the tests coming from the D3D SDK because they aren't providing much coverage, and updating them would change them so far from the original code that the purported benefit (using a body of existing shaders) would be lost. I also dropped the explicit matrix layout qualifiers in the `matrix-layout` test because the new output strategy breaks those for GLSL (you can't put matrix layout qualifiers on `struct` fields, and now the body of every constant buffer is inside a `struct`). This isn't as big of a loss as it seems, because our handling of those qualifiers wasn't really right to begin with. Slang users should only be setting the matrix layout mode globally (and we should probably switch to error out on the explicit qualifiers for now). The other thing that got dropped is tests involving `packoffset` modifiers. Slang already warns that it doesn't support these, and the way they were used in the test cases is actually misleading. For the binding/layout-related tests, the goal was to show that Slang reproduces the same layout as fxc, in which case explicitly enforcing a layout via `packoffset` seems like cheating (are we sure we enforced the layout fxc would have produced?). The real reason was that Slang used to emit explicit `packoffset` on *every* field of a `cbuffer` it would output, because of an `fxc` bug where you couldn't use `register` on textures/samplers declared inside a `cbuffer` unless *every* field in the `cbuffer` used a `register` or `packoffset` modifier. Slang hasn't required that behavior in a while because it now splits textures and samplers, and the one test case where we needed `packoffset` to work around the `fxc` bug in the baseline HLSL has been macro-ified even more to work around the bug. The amount of churn in the test cases is unfortunate, but it continues to point at the weakness of any testing strategy that checks for exact equivalent between Slang's output and that of other compilers. We need to keep working to replace these tests with better alternatives. In `check.cpp` there is logic to perform implicit dereferencing, so that if you write `obj.f` where `obj` is a `ConstantBuffer<X>` (or some other "pointer-like" type) and `f` is a field in `X`, then this effectively translates as `(*obj).f`. That is, we dereference the value of type `ConstantBuffer<X>` to get a value of type `X`, and then refer to the field of the `X` value. There was a problem where the logic to insert that kind of implicit dereference operation was using a reference (`auto& type = ...`) for the type of the expression being dereferenced, and then clobbering it. This would mean that an expression of type `ConstantBuffer<X>` would have its type overwritten to be just `X` and then codegen would break later on. I'm not sure how we haven't run into that before. The `array-of-buffers` test case was added to confirm that we now support arrays of constant, structured, and byte-address buffers for both DXIL and SPIR-V output. Okay, so that was a lot of stuff, but hopefully it is clear how this all works to make the output of the compiler more consistent and explicit, while also supporting the required new functionality. * fixup: review feedback
Diffstat (limited to 'tests/hlsl/dxsdk/VarianceShadows11/RenderVarianceScene.hlsl')
-rw-r--r--tests/hlsl/dxsdk/VarianceShadows11/RenderVarianceScene.hlsl412
1 files changed, 0 insertions, 412 deletions
diff --git a/tests/hlsl/dxsdk/VarianceShadows11/RenderVarianceScene.hlsl b/tests/hlsl/dxsdk/VarianceShadows11/RenderVarianceScene.hlsl
deleted file mode 100644
index 29c9851d8..000000000
--- a/tests/hlsl/dxsdk/VarianceShadows11/RenderVarianceScene.hlsl
+++ /dev/null
@@ -1,412 +0,0 @@
-//TEST_IGNORE_FILE: Currently failing due to Slang compiler issues.
-//TEST:COMPARE_HLSL: -profile vs_4_0 -entry VSMain -profile ps_4_0 -entry PSBlurX -entry PSBlurY
-//--------------------------------------------------------------------------------------
-// File: RenderCascadeScene.hlsl
-//
-// This is the main shader file. This shader is compiled with several different flags
-// to provide different customizations based on user controls.
-//
-//
-// Copyright (c) Microsoft Corporation. All rights reserved.
-//--------------------------------------------------------------------------------------
-
-//--------------------------------------------------------------------------------------
-// Globals
-//--------------------------------------------------------------------------------------
-
-// This flag enables the shadow to blend between cascades. This is most useful when the
-// the shadow maps are small and artifact can be seen between the various cascade layers.
-#ifndef BLEND_BETWEEN_CASCADE_LAYERS_FLAG
-#define BLEND_BETWEEN_CASCADE_LAYERS_FLAG 0
-#endif
-
-// There are two methods for selecting the proper cascade a fragment lies in. Interval selection
-// compares the depth of the fragment against the frustum's depth partition.
-// Map based selection compares the texture coordinates against the acutal cascade maps.
-// Map based selection gives better coverage.
-// Interval based selection is easier to extend and understand.
-#ifndef SELECT_CASCADE_BY_INTERVAL_FLAG
-#define SELECT_CASCADE_BY_INTERVAL_FLAG 0
-#endif
-
-// The number of cascades
-#ifndef CASCADE_COUNT_FLAG
-#define CASCADE_COUNT_FLAG 3
-#endif
-
-
-// Most titles will find that 3-4 cascades with
-// BLEND_BETWEEN_CASCADE_LAYERS_FLAG, is good for lower end PCs.
-
-cbuffer cbAllShadowData : register( b0 )
-{
- matrix m_mWorldViewProjection;
- matrix m_mWorld;
- matrix m_mWorldView;
- matrix m_mShadow;
- float4 m_vCascadeOffset[8];
- float4 m_vCascadeScale[8];
- int m_nCascadeLevels; // Number of Cascades
- int m_iVisualizeCascades; // 1 is to visualize the cascades in different colors. 0 is to just draw the scene
-
- // For Map based selection scheme, this keeps the pixels inside of the the valid range.
- // When there is no boarder, these values are 0 and 1 respectivley.
- float m_fMinBorderPadding;
- float m_fMaxBorderPadding;
-
- float m_fCascadeBlendArea; // Amount to overlap when blending between cascades.
- float m_fTexelSize; // Padding variables exist because CBs must be a multiple of 16 bytes.
- float m_fNativeTexelSizeInX;
- float4 m_fCascadeFrustumsEyeSpaceDepthsData[2]; // The values along Z that seperate the cascades.
- // This code creates an array based pointer that points towards the vectorized input data.
- // This is the only way to index arbitrary arrays of data.
- // If the array is used at run time, the compiler will generate code that uses logic to index the correct component.
-
- static float m_fCascadeFrustumsEyeSpaceDepths[8] = (float[8])m_fCascadeFrustumsEyeSpaceDepthsData;
-
- float3 m_vLightDir;
- float m_fPaddingCB4;
-
-};
-
-
-
-//--------------------------------------------------------------------------------------
-// Textures and Samplers
-//--------------------------------------------------------------------------------------
-Texture2D g_txDiffuse : register( t0 );
-Texture2DArray g_txShadow : register( t5 );
-
-SamplerState g_samLinear : register( s0 );
-SamplerState g_samShadow : register( s5 );
-
-//--------------------------------------------------------------------------------------
-// Input / Output structures
-//--------------------------------------------------------------------------------------
-struct VS_INPUT
-{
- float4 vPosition : POSITION;
- float3 vNormal : NORMAL;
- float2 vTexcoord : TEXCOORD0;
-};
-
-struct VS_OUTPUT
-{
- float3 vNormal : NORMAL;
- float2 vTexcoord : COLOR0;
- float4 vTexShadow : TEXCOORD1;
- float4 vPosition : SV_POSITION;
- float4 vInterpPos : TEXCOORD2;
- float vDepth : TEXCOORD3;
-};
-
-//--------------------------------------------------------------------------------------
-// Vertex Shader
-//--------------------------------------------------------------------------------------
-VS_OUTPUT VSMain( VS_INPUT Input )
-{
- VS_OUTPUT Output;
-
- Output.vPosition = mul( Input.vPosition, m_mWorldViewProjection );
- Output.vNormal = mul( Input.vNormal, (float3x3)m_mWorld );
- Output.vTexcoord = Input.vTexcoord;
- Output.vInterpPos = Input.vPosition;
- Output.vDepth = mul( Input.vPosition, m_mWorldView ).z ;
-
- // Transform the shadow texture coordinates for all the cascades.
- Output.vTexShadow = mul( Input.vPosition, m_mShadow );
-
- return Output;
-}
-
-
-
-static const float4 vCascadeColorsMultiplier[8] =
-{
- float4 ( 1.5f, 0.0f, 0.0f, 1.0f ),
- float4 ( 0.0f, 1.5f, 0.0f, 1.0f ),
- float4 ( 0.0f, 0.0f, 5.5f, 1.0f ),
- float4 ( 1.5f, 0.0f, 5.5f, 1.0f ),
- float4 ( 1.5f, 1.5f, 0.0f, 1.0f ),
- float4 ( 1.0f, 1.0f, 1.0f, 1.0f ),
- float4 ( 0.0f, 1.0f, 5.5f, 1.0f ),
- float4 ( 0.5f, 3.5f, 0.75f, 1.0f )
-};
-
-
-void ComputeCoordinatesTransform( in int iCascadeIndex,
- in float4 InterpolatedPosition,
- in out float4 vShadowTexCoord,
- in out float4 vShadowTexCoordViewSpace )
-{
- // Now that we know the correct map, we can transform the world space position of the current fragment
- if( SELECT_CASCADE_BY_INTERVAL_FLAG )
- {
- vShadowTexCoord = vShadowTexCoordViewSpace * m_vCascadeScale[iCascadeIndex];
- vShadowTexCoord += m_vCascadeOffset[iCascadeIndex];
- }
- vShadowTexCoord.w = vShadowTexCoord.z; // We put the z value in w so that we can index the texture array with Z.
- vShadowTexCoord.z = iCascadeIndex;
-
-}
-
-//--------------------------------------------------------------------------------------
-// Use PCF to sample the depth map and return a percent lit value.
-//--------------------------------------------------------------------------------------
-void CalculateVarianceShadow ( in float4 vShadowTexCoord, in float4 vShadowMapTextureCoordViewSpace, int iCascade, out float fPercentLit )
-{
- fPercentLit = 0.0f;
- // This loop could be unrolled, and texture immediate offsets could be used if the kernel size were fixed.
- // This would be a performance improvment.
-
- float2 mapDepth = 0;
-
-
- // In orderto pull the derivative out of divergent flow control we calculate the
- // derivative off of the view space coordinates an then scale the deriviative.
-
- float3 vShadowTexCoordDDX =
- ddx(vShadowMapTextureCoordViewSpace );
- vShadowTexCoordDDX *= m_vCascadeScale[iCascade].xyz;
- float3 vShadowTexCoordDDY =
- ddy(vShadowMapTextureCoordViewSpace );
- vShadowTexCoordDDY *= m_vCascadeScale[iCascade].xyz;
-
- mapDepth += g_txShadow.SampleGrad( g_samShadow, vShadowTexCoord.xyz,
- vShadowTexCoordDDX,
- vShadowTexCoordDDY);
- // The sample instruction uses gradients for some filters.
-
- float fAvgZ = mapDepth.x; // Filtered z
- float fAvgZ2 = mapDepth.y; // Filtered z-squared
-
- if ( vShadowTexCoord.w <= fAvgZ ) // We put the z value in w so that we can index the texture array with Z.
- {
- fPercentLit = 1;
- }
- else
- {
- float variance = ( fAvgZ2 ) - ( fAvgZ * fAvgZ );
- variance = min( 1.0f, max( 0.0f, variance + 0.00001f ) );
-
- float mean = fAvgZ;
- float d = vShadowTexCoord.w - mean; // We put the z value in w so that we can index the texture array with Z.
- float p_max = variance / ( variance + d*d );
-
- // To combat light-bleeding, experiment with raising p_max to some power
- // (Try values from 0.1 to 100.0, if you like.)
- fPercentLit = pow( p_max, 4 );
-
- }
-
-}
-
-//--------------------------------------------------------------------------------------
-// Calculate amount to blend between two cascades and the band where blending will occure.
-//--------------------------------------------------------------------------------------
-void CalculateBlendAmountForInterval ( in int iNextCascadeIndex,
- in out float fPixelDepth,
- in out float fCurrentPixelsBlendBandLocation,
- out float fBlendBetweenCascadesAmount
- )
-{
-
- // We need to calculate the band of the current shadow map where it will fade into the next cascade.
- // We can then early out of the expensive PCF for loop.
- //
- float fBlendInterval = m_fCascadeFrustumsEyeSpaceDepths[ iNextCascadeIndex - 1 ];
- if( iNextCascadeIndex > 1 )
- {
- fPixelDepth -= m_fCascadeFrustumsEyeSpaceDepths[ iNextCascadeIndex-2 ];
- fBlendInterval -= m_fCascadeFrustumsEyeSpaceDepths[ iNextCascadeIndex-2 ];
- }
- // The current pixel's blend band location will be used to determine when we need to blend and by how much.
- fCurrentPixelsBlendBandLocation = fPixelDepth / fBlendInterval;
- fCurrentPixelsBlendBandLocation = 1.0f - fCurrentPixelsBlendBandLocation;
- // The fBlendBetweenCascadesAmount is our location in the blend band.
- fBlendBetweenCascadesAmount = fCurrentPixelsBlendBandLocation / m_fCascadeBlendArea;
-}
-
-
-//--------------------------------------------------------------------------------------
-// Calculate amount to blend between two cascades and the band where blending will occure.
-//--------------------------------------------------------------------------------------
-void CalculateBlendAmountForMap ( in float4 vShadowMapTextureCoord,
- in out float fCurrentPixelsBlendBandLocation,
- out float fBlendBetweenCascadesAmount )
-{
- // Calcaulte the blend band for the map based selection.
- float2 distanceToOne = float2 ( 1.0f - vShadowMapTextureCoord.x, 1.0f - vShadowMapTextureCoord.y );
- fCurrentPixelsBlendBandLocation = min( vShadowMapTextureCoord.x, vShadowMapTextureCoord.y );
- float fCurrentPixelsBlendBandLocation2 = min( distanceToOne.x, distanceToOne.y );
- fCurrentPixelsBlendBandLocation =
- min( fCurrentPixelsBlendBandLocation, fCurrentPixelsBlendBandLocation2 );
- fBlendBetweenCascadesAmount = fCurrentPixelsBlendBandLocation / m_fCascadeBlendArea;
-}
-
-//--------------------------------------------------------------------------------------
-// Calculate the shadow based on several options and rende the scene.
-//--------------------------------------------------------------------------------------
-
-float4 PSMain( VS_OUTPUT Input ) : SV_TARGET
-{
- float4 vDiffuse = g_txDiffuse.Sample( g_samLinear, Input.vTexcoord );
-
-
- float4 vShadowMapTextureCoordViewSpace = 0.0f;
- float4 vShadowMapTextureCoord = 0.0f;
- float4 vShadowMapTextureCoord_blend = 0.0f;
-
- float4 vVisualizeCascadeColor = float4(0.0f,0.0f,0.0f,1.0f);
-
- float fPercentLit = 0.0f;
- float fPercentLit_blend = 0.0f;
-
- int iCascadeFound = 0;
- int iCurrentCascadeIndex=1;
- int iNextCascadeIndex = 0;
-
- float fCurrentPixelDepth;
-
- // The interval based selection technique compares the pixel's depth against the frustum's cascade divisions.
- fCurrentPixelDepth = Input.vDepth;
-
- // This for loop is not necessary when the frustum is uniformaly divided and interval based selection is used.
- // In this case fCurrentPixelDepth could be used as an array lookup into the correct frustum.
- vShadowMapTextureCoordViewSpace = Input.vTexShadow;
-
-
- if( SELECT_CASCADE_BY_INTERVAL_FLAG )
- {
- iCurrentCascadeIndex = 0;
- if (CASCADE_COUNT_FLAG > 1 )
- {
- float4 vCurrentPixelDepth = Input.vDepth;
- float4 fComparison = ( vCurrentPixelDepth > m_fCascadeFrustumsEyeSpaceDepthsData[0]);
- float4 fComparison2 = ( vCurrentPixelDepth > m_fCascadeFrustumsEyeSpaceDepthsData[1]);
- float fIndex = dot(
- float4( CASCADE_COUNT_FLAG > 0,
- CASCADE_COUNT_FLAG > 1,
- CASCADE_COUNT_FLAG > 2,
- CASCADE_COUNT_FLAG > 3)
- , fComparison )
- + dot(
- float4(
- CASCADE_COUNT_FLAG > 4,
- CASCADE_COUNT_FLAG > 5,
- CASCADE_COUNT_FLAG > 6,
- CASCADE_COUNT_FLAG > 7)
- , fComparison2 ) ;
-
- fIndex = min( fIndex, CASCADE_COUNT_FLAG - 1 );
- iCurrentCascadeIndex = (int)fIndex;
- }
- }
-
- if ( !SELECT_CASCADE_BY_INTERVAL_FLAG )
- {
- iCurrentCascadeIndex = 0;
- if ( CASCADE_COUNT_FLAG == 1 )
- {
- vShadowMapTextureCoord = vShadowMapTextureCoordViewSpace * m_vCascadeScale[0];
- vShadowMapTextureCoord += m_vCascadeOffset[0];
- }
- if ( CASCADE_COUNT_FLAG > 1 ) {
- for( int iCascadeIndex = 0; iCascadeIndex < CASCADE_COUNT_FLAG && iCascadeFound == 0; ++iCascadeIndex )
- {
- vShadowMapTextureCoord = vShadowMapTextureCoordViewSpace * m_vCascadeScale[iCascadeIndex];
- vShadowMapTextureCoord += m_vCascadeOffset[iCascadeIndex];
-
- if ( min( vShadowMapTextureCoord.x, vShadowMapTextureCoord.y ) > m_fMinBorderPadding
- && max( vShadowMapTextureCoord.x, vShadowMapTextureCoord.y ) < m_fMaxBorderPadding )
- {
- iCurrentCascadeIndex = iCascadeIndex;
- iCascadeFound = 1;
- }
- }
- }
- }
- // Found the correct map.
- vVisualizeCascadeColor = vCascadeColorsMultiplier[iCurrentCascadeIndex];
-
- ComputeCoordinatesTransform( iCurrentCascadeIndex, Input.vInterpPos, vShadowMapTextureCoord, vShadowMapTextureCoordViewSpace );
-
- if( BLEND_BETWEEN_CASCADE_LAYERS_FLAG && CASCADE_COUNT_FLAG > 1 )
- {
- // Repeat text coord calculations for the next cascade.
- // The next cascade index is used for blurring between maps.
- iNextCascadeIndex = min ( CASCADE_COUNT_FLAG - 1, iCurrentCascadeIndex + 1 );
- if( !SELECT_CASCADE_BY_INTERVAL_FLAG )
- {
- vShadowMapTextureCoord_blend = vShadowMapTextureCoordViewSpace * m_vCascadeScale[iNextCascadeIndex];
- vShadowMapTextureCoord_blend += m_vCascadeOffset[iNextCascadeIndex];
- }
- ComputeCoordinatesTransform( iNextCascadeIndex, Input.vInterpPos, vShadowMapTextureCoord_blend, vShadowMapTextureCoordViewSpace );
- }
- float fBlendBetweenCascadesAmount = 1.0f;
- float fCurrentPixelsBlendBandLocation = 1.0f;
-
- if( SELECT_CASCADE_BY_INTERVAL_FLAG )
- {
- if( CASCADE_COUNT_FLAG > 1 && BLEND_BETWEEN_CASCADE_LAYERS_FLAG )
- {
- CalculateBlendAmountForInterval ( iNextCascadeIndex, fCurrentPixelDepth,
- fCurrentPixelsBlendBandLocation, fBlendBetweenCascadesAmount );
-
- }
- }
- else
- {
- if( CASCADE_COUNT_FLAG > 1 && BLEND_BETWEEN_CASCADE_LAYERS_FLAG )
- {
- CalculateBlendAmountForMap ( vShadowMapTextureCoord,
- fCurrentPixelsBlendBandLocation, fBlendBetweenCascadesAmount );
- }
- }
-
- // Because the Z coordinate specifies the texture array,
- // the derivative will be 0 when there is no divergence
- //float fDivergence = abs( ddy( vShadowMapTextureCoord.z ) ) + abs( ddx( vShadowMapTextureCoord.z ) );
- CalculateVarianceShadow ( vShadowMapTextureCoord, vShadowMapTextureCoordViewSpace,
- iCurrentCascadeIndex, fPercentLit);
-
- // We repeat the calcuation for the next cascade layer, when blending between maps.
- if( BLEND_BETWEEN_CASCADE_LAYERS_FLAG && CASCADE_COUNT_FLAG > 1 )
- {
- if( fCurrentPixelsBlendBandLocation < m_fCascadeBlendArea )
- { // the current pixel is within the blend band.
-
- // Because the Z coordinate species the texture array,
- // the derivative will be 0 when there is no divergence
- float fDivergence = abs( ddy( vShadowMapTextureCoord_blend.z ) ) +
- abs( ddx( vShadowMapTextureCoord_blend.z) );
- CalculateVarianceShadow ( vShadowMapTextureCoord_blend, vShadowMapTextureCoordViewSpace,
- iNextCascadeIndex, fPercentLit_blend );
-
- // Blend the two calculated shadows by the blend amount.
- fPercentLit = lerp( fPercentLit_blend, fPercentLit, fBlendBetweenCascadesAmount );
-
- }
- }
-
- if( !m_iVisualizeCascades ) vVisualizeCascadeColor = float4( 1.0f, 1.0f, 1.0f, 1.0f );
-
- float3 vLightDir1 = float3( -1.0f, 1.0f, -1.0f );
- float3 vLightDir2 = float3( 1.0f, 1.0f, -1.0f );
- float3 vLightDir3 = float3( 0.0f, -1.0f, 0.0f );
- float3 vLightDir4 = float3( 1.0f, 1.0f, 1.0f );
- // Some ambient-like lighting.
- float fLighting =
- saturate( dot( vLightDir1 , Input.vNormal ) )*0.05f +
- saturate( dot( vLightDir2 , Input.vNormal ) )*0.05f +
- saturate( dot( vLightDir3 , Input.vNormal ) )*0.05f +
- saturate( dot( vLightDir4 , Input.vNormal ) )*0.05f ;
-
- float4 vShadowLighting = fLighting * 0.5f;
- fLighting += saturate( dot( m_vLightDir , Input.vNormal ) );
- fLighting = lerp( vShadowLighting, fLighting, fPercentLit );
-
- return fLighting * vVisualizeCascadeColor * vDiffuse;
-
-}
-