Title: Direct3D Tools
1Direct3D Tools
- Chas. Boyd
- WGGT
- Microsoft Corporation
2Outline
- D3DX Utilities
- Mesh, Math, Image routines
- Shader routines
- Effect Framework
- Fallback/LOD system
- Cross tool material
- Fragment linking
- What is it?
- How to use it?
3Math
- Set of commonly used math functions
- Vector math
- Matrix transforms
- Performance optimized for Intel and AMD special
instruction sets
4Image
- Core operations
- Format conversion and i/o
- For jpg, png, tga, bmp, ppm, dds
- Compression dxtc
- Scaling, Mip-Map generation
- CubeMap, VolumeMap i/o
- Procedural texture loaders
- Now supports gamma correction!
5Mesh
- Current Routines
- Progressive mesh LOD tuner
- Mesh cleaner
- Ray intersection
- Skinning support
- Mesh optimizer
- Normal, tangent basis generator
- New Routines
- Patches
- Frame Hierarchy
- Animation System
6D3DX Skinning
- Setup data for hardware skinning
- Non-indexed skinning (DirectX 7 style)
- Indexed palette skinning (DirectX 8, VS)
- Full software skinning
- If doing on skinning on CPU
- More efficient than emulating HW
- Needs only a VB
- Works with Patches, PMesh and Meshes
- Convert data between various skinning algorithms
7D3DX Patch Tessellation API
- Software tessellation for High Order Primitives
- Full match of DirectX 9 hardware
- N-Patches, Bezier, B-Splines, Catmull-Rom
- Fractional tessellation
- Adaptive tessellation
- Displacement mapping
8D3DX Displacement Mapping
- Use tessellated vertex texture coordinate to
sample a 2D texture - Sampled value is a per vertex displacement value
- Sampled data can be 1-4D
- Actual displacement done in vertex shader
- Adaptive tessellation supports MIP-mapped
sampling - MIP level based on tessellation factor
9Shader Management
- Modern apps must manage a combinatoric number of
shaders - Due to varying effects desired
- Diffuse, ambient, or specular terms
- Skinning on/off, number of lights
- Due to different hardware generations
- DirectX 8, 9, 9.1
- FF pipeline does not have these issues
10Solution Effects
- D3DX Effect Framework
- Routines to load and select shaders
- Originally developed to manage multitexture
shaders in DirectX 6 - Now fully integrated with the HLSL
11Effect Framework
- Encapsulation of device state
- Enables scalable rendering techniques
- Allows controlled fallback
- Cant just switch to multi-pass
- Older hardware cant do more passes since fill
rate is less - Helps rapid prototyping
- Runtime interpretation of text-based effect
definition
12Effect Framework Goals
- Standard way of representing shading algorithms
- Ease of authoring
- Enable some abstraction between author of effect
and user of the effect - Facilitates sharing of effects
- Enables effect geometry optimizations to be
done at author-time, install-time, load-time or
runtime - Cross Platform
- Efficient for runtime use
13Effect Model
Effect Water
Technique DX9
pass0
pass1
Technique DX8
pass0
Technique DX7
pass0
14Effect FrameworkFallback Techniques
- Techniques are grouped by their quality or LOD
- Techniques can be chosen based on what hardware
creates successfully - Can test performance in back buffer
- User responsible for drawing geometry
15Sharing values across effects
- Shared parameters (aka Globals)
- Shared across multiple effects
- Tagged in effect file as shared
- User selects the list of effects whose shared
parameters are linked at runtime - Setting once updates all linked effects
16Annotations
- Name/Value pairs attached to variables
- App specific information about the variable
- Can be from a set of defined annotations
- Used to allow effect files to be shared between
tools - i.e. the Name value is interpreted as the
resource name for textures, vertex shaders, etc. - Work with Techniques and Passes as well
- Can be used to specify additional information on
how the pass/technique works. - i.e. the pass needs to be rendered to a texture
17Annotation syntax
Texture DiffuseTex lt string Name
tiger.bmp int Width 256 int Height
512 gt
- Name/Value pairs
- Information for application only
- Provided to application through interface
- Not used by effect framework
18Design for Runtime Efficiency
- Compile effects into hardware friendly format
- State blocks/constant arrays
- Efficient setting of effect parameters
- Can set parameters within effect application
- Integration with an optimizing language back end
- Caching common parameter combinations
19Unify Fixed and Programmable Pipelines
- A consistent framework for programming shaders
- Fixed-function programming using render states,
texture stage state, fixed-function vertex
processing - Assembly level vertex and pixel shader
programming - Shading language based programming
20Unify Cross-Platform Programming
- Techniques in an effect can span shader models
(like VS 1.1, VS 2.0, etc.) - Unified parser allows handling the same effect
file cross platform - Only techniques that validate on runtime platform
available for actual use
21Effect-Language Integration
- Techniques can call language functions
- This is where the back-end is specified
- Same function can be used in multiple techniques
compiled to multiple back-ends - Expressions of parameters can be used in state
assignment - Per-primitive expressions detected and optimized
out by compiler when within an effect
22Effect and HLSL Usage
- Language functions can be compiled as either
pixel or vertex programs - Do not have to specify vertex constants
- Constant table from shader will be matched up
with the corresponding effect variables - Pixel shader samplers
- Matched up just like any other variable
- The difference is that a block of state is
applied to the stage that the sampler is bound to.
23Specialization
- Specify constants that are to be literals
- via ID3DXEffectCompiler Interface
- Call CompileEffect() method
- returns pre-optimized shader
- Easily generate multiple shaders optimized for
specific cases - Can helps shader management by generating them on
the fly
24Materials
- Effects are a good way to define materials
- Contain state required for drawing
- Contain Annotations with app info
- Texture filenames
- What type of geometry is required
- What type of skinning
- Tangents/Binormal requirements
- App specific information on how to draw
- i.e. lighting format info, render to texture
info, etc. - Convenient parameterization
- Allows for materials with user customization
25Parameterized Effects
- Effects can have parameters of various types
- Parameters augment static state description in
the .fx files - How (and which) parameters get used defined by
the effect - Allows for a convenient material system
26Example Material
texture tJerseyTexture lt string Name
jersey.bmp gt float4 JerseyColor 1.0, 0.0,
0.0, 1.0 technique UniformUnskinned lt string
TransformType Unskinned gt pass
VertexShader compile vs_1_1
DrawPlayer(JerseyColor) Texture0
(tJerseyTexture) technique
UniformSkinned lt string TransformType
NonIndexedSkinned gt pass
VertexShader compile vs_1_1 DrawPlayerSK(JerseyC
olor) Texture0 (tJerseyTexture)
27Effect Instance
- An effect instance is the information required to
instantiate a specific material - Contains
- Name of the effect
- A list of Name/Value pairs
- Each name represents an effect param
- The value is the initial value for that effect
parameter
28Why use Effect Instances?
- Allows one effect file to be customized for use
as a number of materials - i.e. draw multiple different types of leather
with one vertex/pixel shader combination - Each material instance could specify
- Color of leather
- Texture for the grain of the leather
- Other leather parameters
- Higher level abstraction of materials
- Convenient for models to reference
- Allows for customization specific to model
- While still allowing for sharing of code and
initial values
29Effect Instance Example
- Football Jersey example
- EffectInstance A
- Jersey.fx
- tJerseyTexture_at_Name hawks.bmp
- JerseyColor 1.0, 1.0, 1.0, 1.0
- EffectInstance B
- Jersey.fx
- tJerseyTexture_at_Name generic.bmp
- JerseyColor 0.0, 1.0, .0., 1.0
30Linking Effects with Geometry
- .X file extensions to link effect with geometry
- Uses effect instances
- Effect filename
- Initial values for effect parameters
- D3DX API support for associating effects with
meshes - D3DXEFFECTINSTANCE
- Structure used by D3DX to represent effect
instances - Simple struct containing
- Effect filename
- Array of Name/Value pairs
31Effect Performance Tip
- Handles vs Parameter Names
- Handle usage avoids string lookups
- All methods that take names accept both handles
and strings - Strings are great for prototypes and tools, but
not fast enough for heavy duty use - Easy to get a handle for any name
- IsParameterUsed
- Easy way to tell if a value is used by the
current technique - Perf advantage if only used values are updated
32Shader Management
- Dealing with the permutation problem
33How to generate groups of similar shaders?
- Use HLSL with compile time constants
- Compile the shader N times with different
constants - Great if permutation set is know ahead of time
- Run-time compilation is possible, although
expensive - Works with standalone HLSL code and Effect files
- Link shader fragments
- Cheap run-time linking
- Generate shaders needed only if necessary
- Downside is the lack of fragment to fragment
compile time checks
34Fragment Linker
- Efficiently links together
- HLL fragments
- And asm fragments
- Current Features
- Symbol table resolution
- Register use optimization
- Dead code removal
- via ID3DXFragmentLinker interface
35ASM Fragment Syntax
- Similar syntax to asm blocks in effect files
- Use logical names for registers
- Prefix of name specifies register type
- r_ for temp registers
- v_ for input registers
- c_ for constant registers
- Declared like a variable to link fragment name
and asm block
36ASM Fragment Syntax (2)
- Constant registers (c_)
- Constants must be defined at global scope
- Provides type information for building constant
table - Input registers (v_)
- Name portion is the semantic
- The semantic is used to build dcl statements
- Temp registers (r_)
- User defined register names
- Data links between fragments
- Reads of r_ will get the last value written to
that name - Writes of r_ will specify the value for the next
reader
37ASM Fragment Sample
vertexfragment ComputeGlowAsm asm_fragment
vs_1_1 // normalize normal dp3
r_Length.w, r_TransformedNormal.xyz,
r_TransformedNormal.xyz rsq r_Length.w,
r_Length.w mul r_TransformedNormal.xyz,
r_TransformedNormal.xyz, r_Length.www mov
r_Temp.x, c_One.x mad r_Power.z,
r_TransformedNormal.z, -r_TransformedNormal.z,
c_One.x mul r_Power.z, r_Power.z,
r_Power.z mul oD0, c_vGlowColor, r_Power.z
mad r_TransformedPosition.xyz,
r_TransformedNormal.xyz,
c_vDisplace.xxx, r_TransformedPosition.xyz
mov r_TransformedPosition.w, c_One.x m4x4
oPos, r_TransformedPosition, c_mProjection
38HLSL Fragment Syntax
- Follows all standard rules for defining a HLSL
function - The only addition is the user defined r_
semantics - In the input/output list, it is possible to
define a user semantic beginning with r_ anywhere
a profile semantic is valid. - The r_ semantics are the links that are setup
between the shader fragments - HLSL fragments have to be declared
- VertexFragment F compile_fragment vs_1_1
Func()
39HLSL Fragment Sample
void TransformUnskinned( float4 vPos
POSITION,
float3 vNormal NORMAL, out float3
vTransformedPosition r_TransformedPosition,
out float3 vTransformedNormal
r_TransformedNormal ) // Transform the
position into view space vTransformedPosition
mul(vPos, mWorldView) // Tranform the
normal into view space // (just use the upper
3x3 of WorldView) vTransformedNormal
mul(vNormal, (float3x3)mWorldView)
vertexfragment Transform compile_fragment
vs_1_1 TransformUnskinned()
40HLSL Fragment Sample
void ComputeGlow( float3 vTransformedPositio
n r_TransformedPosition, float3
vTransformedNormal r_TransformedNormal,
out float4 Position POSITION, out float4
Diffuse COLOR) vTransformedPosition
vTransformedNormal vDisplace.x //
determine the strength of the glow by the normal
direction float fPower vTransformedNormal.z
-vTransformedNormal.z1.0f fPower
fPower // tranform position into screen
space from view space Position
mul(float4(vTransformedPosition, 1.0),
mProjection) // output color is the
compute power times the glow color Diffuse
vGlowColor fPower vertexfragment
ComputeGlowHLSL compile_fragment vs_1_1
ComputeGlow()
41Assembling Fragments
- D3DXAssembleFragments
- Loads all fragments declared in source file
- If a fragment is ASM it is assembled
- If a fragment is HLSL it is compiled
- Most compile errors occur here
- Missing linked registers are found later
- Standard list of sources
- Memory, Resource, File, etc.
- Same include system as Assembler and Compiler
42ID3DXFragmentLinker
- AddFragments
- Adds fragments built from file by
D3DXAssembleFragments - LinkShader
- Generates binary of shader from list of fragments
- LinkVertexShader
- Generates shader object from a list of fragments
- Has a cache of recently generated fragments
43How to use a linked shader
- Has a Constant Table
- Can be treated like any normal HLSL generated
shader - Has dcl statements
- Links up to vertex data with the correct dcl
statements - Use a linked shader just as you would any other
HLSL shader - ASM fragments vs HLSL fragments make no
difference - Constant table and input dcls are always present
44Summary
- D3DX Effects
- Simplifies usage of shaders
- Convenient as materials
- Fragment Linking
- Allows for building up of shaders
- Provides run-time compilation without the perf
hit of a full compile - Both effects and fragments work great with HLSL
45Shaders
- Shaders are the next step in graphics
API/hardware evolution - Need to use abstractions to help solve the
problems with the number of shaders required
46Four LibrariesWhich do I use?
- D3dx.lib
- This is the version to use with your shipping
product - D3dxdt.lib
- Static debug version for use in debugging only
- D3dxd.lib
- Imports for the dynamic debug version of the
library - D3dxd.dll
47Action Items
- Sign up for the DirectX 9 Beta
- URL http//www.betaplace.com
- User ID DirectX9
- Password DXBeta
- Check it out!
- Use it in your research development
- Let us know what you think
- directx_at_microsoft.com
48Questions
49Exporters
- What is available?
- Discreet 3ds Max
- 3.x, 4.x
- AliasWavefront Maya
- 2.5, 3.0, 4.0
- NewTek Lightwave 3D
50Exporters
- What can be exported
- Objects
- Polygonal
- Materials
- Basic now
- Animation
- Matrix
- Skinning
51Exporters
- What will be exported
- Objects
- Patch support
- Materials
- Effect file linking
- Shader matching
- Animation
- Advanced skinning
52D3DX Frame Hierarchy
- Struct D3DXMESHCONTAINER
- Struct D3DXFRAME
- All members public
- Can be extended with app specific members
- Utility functions that work on hierarchies of
these structs - Designed to work with derived structs
53Struct D3DXMESHCONTAINER
typedef struct _D3DXMESHCONTAINER LPTSTR
Name union LPD3DXMESH
pMesh LPD3DXPATCHMESH pPatchMesh LPD3DXPME
SH pPMesh LPD3DXMATERIAL
pMaterials LPD3DXEFFECTINSTANCE
pEffects DWORD
NumMaterials DWORD
pAdjacency LPD3DXSKININFO
pSkinInfo _D3DXMESHCONTAINER
pNextMeshContainer D3DXMESHCONTAINER,
LPD3DXMESHCONTAINER
54Struct D3DXFRAME
typedef struct _D3DXFRAME LPTSTR Name
D3DXMATRIX TransformationMatrix
LPD3DXMESHCONTAINER pMeshContainer struct
_D3DXFRAME pFrameSibling struct _D3DXFRAME
pFrameFirstChild D3DXFRAME, LPD3DXFRAME
55Frame Hierarchy Functions
- Load / Save to .X files
- Append/delete nodes
- Compute bounding sphere
- Integrate with animation interface
- And more
56D3DX Animation API
- Key frame interpolation
- Linear, spline, quaternion, etc
- Animation blending (n-way)
- For mixing animations
- Smooth transitions
- Independent of frame hierarchy
- Just point to data being animated
57Animation Terminology
- Interpolator
- Animation sets
- Set of interpolators
- Tracks
- Subset of animation sets that are currently being
blended - Target Matrices
- Specified by app
- Results of blended interpolation go here
58Animation Tracks
- Has an associated animation set
- Blending weight
- Time transform
- Scale and offset
- Convert from animation set units to global units
- Used to control animation speed
- Enable / Disable
59Animation Transitions
- Track properties can themselves be changed over
time - Linearly so far
- Change track weight over time
- Change track speed over time
- Modify enable / disable status over time