D3DBook:D3D10 1 Considerations for Procedural Shaders

From GDWiki
Jump to: navigation, search

Contents

Direct3D 10.1 Considerations For Procedural Shaders

Direct3D 10.1, the new feature baseline for graphics cards announced by Microsoft in late 2007 and published in March 2008, gives programmers even more power for creating shaders. Here, we take a look at some of the new features that can benefit procedural shader programming.

Available Register Count Doubled to 32 per Shader Stage

More data can be transferred across the pipeline in a single pass, so more complex shader logic is possible without having to divide processing into smaller blocks at host program level. This ultimately results in higher performance for complex shaders, regardless of the pipeline stage these extra registers are used in.

You don’t need any special instructions to use the new register limits; it is just possible to use more variables than before, if the HLSL code is compiled with the new 4.1 profiles.


MSAA Enhancements

In D3D10.1, if pixels have attributes that vary across the pixel area, said attributes can be evaluated per sample rather than per pixel like in previous versions. This will greatly improve quality of procedural shaders when the device is set to perform multi-sample antialiasing.

To enable the per-sample evaluation of the pixel shader, include the new system-generated value SV_SampleIndex in the pixel shader parameters and use it in the shader code to determine the current sample index. This parameter is useful if a custom antialiasing pattern is to be implemented. The shader will then run multiple times per pixel, based on the number of samples per pixel in the destination render target.

If you need the destination sample position from the pixel shader, you can use the intrinsic functions GetRenderTargetSamplePosition() and GetRenderTargetSampleCount() to get information about where the current pixel is to be written. This info can be used to optimize the quality of ray-tracing type operations as well as discontinuous functions such as the checker pattern earlier in this section of the book, by adjusting the sampling positions given to the algorithms and taking multiple passes without having to guess where the corresponding samples and pixels actually are.


Custom Sample Resolving

It is possible to fetch individual samples of texels from a multi-sampled render target texture. This is very useful if you want to perform tone mapping of HDR data prior to combining the samples, as opposed to applying tone mapping after the samples are combined (legacy way). This will help avoid jagged edges that are an artifact of variance among the texel samples’ intensities, and therefore improves visual quality considerably in HDR scenarios. Strictly speaking, combining non-linear (after tonemapping) samples is not physically correct way to weight sample intensities, but in general usage the effect fools the eye quite efficiently.

To load individual samples from a MSAA texture, first declare your input MSAA texture in HLSL as Texture2DMS<float4, 4> where MS signifies “multisampled”, float4 is the texture data type and the last parameter, 4, is the number of samples on a single texel of that texture. Then, use the Load() method of the texture object; first parameter is the unnormalized (that is, 0…textureWidth or 0…textureHeight), and the second parameter is the index of the sample you want to fetch.

In conjunction with the Load() method, it is convenient to use the data available from Texture.GetDimensions() in HLSL to determine the width and height as well as the number of samples available in the source texture. This method is not very expensive, contrary to what developers may generally believe, since the graphics device has to carry this data through the entire pixel pipeline anyway.

There are several other enhancements in D3D10.1 as compared to 10.0, but the rest of them are not so specifically involved with procedural shaders and more with the logistics of resources, performance improvements and quality improvements. Nevertheless, it is likely worth taking a look at all the new features of the 10.1 interfaces as they have been developed by listening to – and catering to the needs of - actual graphics developers.

Personal tools