Graphics Reference
In-Depth Information
To rectify this situation, Direct3D 11 has introduced a feature known as dynamic
shader linkage. It essentially allows applications to dynamically choose from multiple
implementations of an HLSL code path when binding a shader program to the pipeline,
effectively allowing dynamic dispatch at the level of a Draw or Dispatch call. We will
explore this capability further in the following sections to understand how it can be used in
a real-time rendering application.
6.6.1 Authoring Shaders for Dynamic Linkage
Shader programs that will make use of dynamic linkage must be authored using HLSL
interfaces and classes. Essentially, the procedure is similar to virtual dispatch in C++: inter-
faces are declared with a set of methods, and the shader code calls methods on those inter-
faces. Then at runtime, the host application assigns an instance of a class that implements
the interface to be used for the duration of a Draw or Dispatch call. Shader programs using
interfaces can declare a global instance of an interface, just like any other variable type.
That instance then functions like a polymorphic pointer in C++, and forwards any methods
called on it to the class instance specified by the host application. Listing 6.18 demonstrates
the syntax for declaring an interface instance and calling one of its methods.
interface Light
{
float3 GetLighting( fioat3 Position, float3 Normal );
};
Light LightInstance;
float4 PSMain( in float3 Position : POSITION,
in float3 Normal : NORMAL ) : SV_Target
{
float3 lightColor = LightInstance.GetLighting( Position, Normal );
return float4( lightColor, 1.0f );
}
Listing 6.18. Calling a method on an interface.
Any classes that can possibly be used to implement an interface must also be de-
clared in the HLSL shader program, or included through the use of a #include pragma.
If the class has member variables, an instance of the class must be declared in a constant
buffer. This allows the host application to specify values for those members at runtime.
Listing 6.19 demonstrates the syntax for declaring a class that implements an interface, and
declaring an instance in a constant buffer.
Search WWH ::




Custom Search