Graphics Reference
In-Depth Information
4.5 Implementation
We have now looked at all the algorithms; let's go through them in the same
order again, looking at sample code for implementation details.
4.5.1 Hi-Z Pass
The code snippet in Listing 4.3 shows how to implement a Hi-Z construction pass
in DirectX using Microsoft HLSL (High Level Shading Language). This shader is
executed successively, and the results are stored in the mip-channels of the Hi-Z
buffer. We read from level N
1 and write to N untilwereachasizeof1
×
1as
mentioned in Section 4.4.1.
To render into the same texture that we read from in DirectX 11 terms, we will
have to make sure that our ID3D11ShaderResourceView objects point to a single
mip-channel and not the entire range of mip-channels. The same rule applies to
our ID3D11RenderTargetViews objects.
4.5.2 Pre-integration Pass
The code snippet in Listing 4.4 shows how to implement a pre-integration pass
in DirectX using Microsoft HLSL. It basically calculates the percentage of empty
float4 main ( PS_INPUT input ): SV_Target
{ // Texture/image coordinates to sample/load/read the depth
// values with .
float2 texcoords = input . tex ;
// Sample the depth values with different offsets each time.
// We use point sampling to ignore the hardware bilinear
// filter . The constant prevLevel is a global that the
// application feeds with an integer to specify which level
// to sample the depth values from at each successive
// execution . It corresponds to the previous level .
float4 minDepth ;
minDepth . x = depthBuffer . SampleLevel ( pointSampler ,
texcoords , prevLevel , int2 (0, ));
minDepth . y = depthBuffer . SampleLevel ( pointSampler ,
texcoords , prevLevel , int2 (0,
1) ) ;
minDepth . z = depthBuffer . SampleLevel ( pointSampler ,
texcoords , prevLevel , int2 (
1, 0) ) ;
minDepth . w = depthBuffer . SampleLevel ( pointSampler ,
texcoords , prevLevel , int2 (
1,
1) ) ;
// Take the minimum of the four depth values and return i t .
float d = min ( min ( minDepth . x , minDepth . y ), min ( minDepth . z ,
minDepth . w ));
return d ;
}
Listing 4.3. How to implement the Hierarchical-Z buffer taking the minimum of 2 × 2
depth values from the depth buffer.
Search WWH ::




Custom Search