:::::Pick from dx9 sdk
The behavior of shader constants has changed between Direct3D 8 and Direct3D 9.
- For Direct3D 9, constants set with defx assign values to the shader constant space. The lifetime of a constant declared with defx is confined to the execution of that shader only. Conversely, constants set using the APIs SetXXXShaderConstantX initialize constants in global space. Constants in global space are not copied to local space (visible to the shader) until SetxxxShaderConstants is called.
- For Direct3D 8, constants set with defx or the APIs both assign values to the shader constant space. Each time the shader is executed, the constants are used by the current shader regardless of the technique used to set them.
Behavior of Constant Registers in Assembly Shaders
There are two ways to set constant registers in an assembly shader:
- Declare a shader constant in assembly code using one of the def* instructions.
- Use one of the Set***ShaderConstant* API methods.
Direct3D 9 Shader Constants
In Direct3D 9 the lifetime of defined constants in a given shader is confined to the execution of that shader only (and is non-overridable). Defined constants in Direct3D 9 have no side effects outside of the shader.
Here's an example using Direct3D 9:
Given: Create shader1 which references c4 and defines it with the def instruction Scenario 1: Call Set***Shader shader1 Call Set***ShaderConstant* to set c4 Call Draw Result: The shader will see the def'd value in c4 Given: Scenario 1 has just completed Create shader2 (which references c4 but does not use the def instruction to define it) Scenario 2: Call Set***Shader shader2 Call Draw Result: The shader will see the value last set in c4 by Set***ShaderConstant* in scenario 1. This is because shader 2 didn't def c4.
In Direct3D 9, calling Get***ShaderConstant* will only retrieve constant values set via Set***ShaderConstant*.
Direct3D 8 Shader Constants
This behavior is different in Direct3D 8.x.
Given: Create shader1 which references c4 and defines it with the def instruction Scenario 1 (repeated with Direct3D 8): Call Set***Shader with shader1 Call Set***ShaderConstant to set c4 Call Draw Result: The shader will see the value in c4 from Set***ShaderConstant
In Direct3D 8.x Set***ShaderConstant takes effect immediately. Consider this scenario:
Given: Create shader1 which references c4 and defines it with the def instruction Scenario 3: Call Set***Shader with shader1 Call Draw Result: The shader will see the def'd value in c4 Given: Scenario 3 has just completed Create shader2 (which references c4 but does not use the def instruction to define it) Scenario 4 : Call Set***Shader with shader2 Call Draw Result: The shader will see the def'd value in c4 (set by def in shader 1)
The undesirable result is that the order in which the shaders are set could affect the observed behavior of individual shaders.
Shader Driver Model Requirements
Direct3D 9 interfaces are restricted to device driver interface (DDI) drivers that are DirectX 7-level and above. To check the DDI level, run the DirectX Diagnostic Tool and examine the saved text file.
For reference, Direct3D 8 interfaces work only on DDI drivers that are DirectX 6-level and above.
Shader Binary Format
The bitwise layout of the shader instruction stream is defined in D3d9types.h. If you want to design your own shader compiler or construction tools and you want more information about the shader token stream, refer to the Direct3D 9 Driver Development Kit (DDK).