chapter 5: directx ® chas. boyd directx ® microsoft 3 outlineoutline what drove the language...
DESCRIPTION
DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What does it look like? What does it look like? Syntax definition How does it work? How does it work? API integration How is it used? How is it used? Effects Framework What drove the language design? What drove the language design? Background What does it look like? What does it look like? Syntax definition How does it work? How does it work? API integration How is it used? How is it used? Effects FrameworkTRANSCRIPT
Chapter 5: DirectXChapter 5: DirectX®®
Chas. BoydChas. Boyd
3DIRECTX®
Microsoft
OutlineOutline• What drove the language design?What drove the language design?
• Background• What does it look like?What does it look like?
• Syntax definition• How does it work?How does it work?
• API integration• How is it used?How is it used?
• Effects Framework
4DIRECTX®
Microsoft
BackgroundBackground
• The DirectXThe DirectX®® Process Process• Evolution of DirectXEvolution of DirectX®®
5DIRECTX®
Microsoft
Industry ProcessIndustry Process• Based on iteration with partnersBased on iteration with partners
• Requirements from software developers• Feasibilities from hardware developers
• Combine to deliver product for Combine to deliver product for programmersprogrammers
• Pipelined to work in parallelPipelined to work in parallel• Relies on Reference ImplementationRelies on Reference Implementation
6DIRECTX®
Microsoft
Direct3D ReferenceDirect3D Reference• Software model of hardwareSoftware model of hardware
•Analogous to/input to “C” model stage of hardware design
• Acts as “compilable specification:”Acts as “compilable specification:”•Used to communicate new features and
requirements to IHVs• Provided as comparison to ISVsProvided as comparison to ISVs
• to check behavior of drivers while coding• Used to test drivers for conformanceUsed to test drivers for conformance• Color rules by Jim BlinnColor rules by Jim Blinn
7DIRECTX®
Microsoft
Industry ProcessIndustry Process
HardwareVendors
SoftwareDevelopers
End Users
DirectXTeam
DirectX Components New Hardware
8DIRECTX®
Microsoft
DirectX EvolutionDirectX Evolution
96 97 98 99 00 01 02 03 04
DX9 Float Pixels DX8.1 More Shaders DX8 Programmability DX7 Hardware T&L DX6 Multi-Texture DX5 DrawPrimDX3 1st Direct3D
9DIRECTX®
Microsoft
Primitive OpsPrimitive Ops
DirectX Graphics ArchitectureDirectX Graphics Architecture
VertexComponents
PixelPixelShaderShader
Samplers
ImageImageSurfaceSurface
VBVB
PosPos ColorColor TC1TC1 TC2TC2
Output pixels
Tex1Tex1
Tex0Tex0
Tex2Tex2
VertexVertexShaderShader
Vec0Vec0 Vec1Vec1 Vec2Vec2 Vec3Vec3 VectorData
GeometryOps
PixelOps
10DIRECTX®
Microsoft
……
Vertex ShaderVertex ShaderVec0Vec0 Vec1Vec1 Vec2Vec2 Vec3Vec3 Vec4Vec4 Vec15Vec15
Const0Const0
R3R3
R0R0
R11R11
Const1Const1
Const2Const2
Const3Const3
Const95Const95
HposHpos Color0Color0 Color1Color1TC0TC0 TC1TC1 TC2TC2 TC3TC3
…… …
…
Vertex ALUVertex ALU
R1R1
A0A0
R2R2
11DIRECTX®
Microsoft
Pixel Shader ArchitecturePixel Shader Architecture
Pixel
c0
t3t2
t0
r1
c1c2c3
c7
c4
... Hpos
d0 d1TC0 TC1 TC2 TC3
r0
Pixel ALUt1
Stage3
Stage0
Stage2
Stage1
TextureStages
temp
color
texture constant
12DIRECTX®
Microsoft
DirectXDirectX®® 6 Multitexture 6 Multitexture
• Texture registersTexture registers 2-82-8• Temp registersTemp registers 11• Color registersColor registers 22• Constant registersConstant registers 11• Output registersOutput registers 00• Instruction countInstruction count 2-82-8• Supports dependent readsSupports dependent reads
13DIRECTX®
Microsoft
DirectXDirectX®® 8 Pixel 8 Pixel ShadersShaders• Texture registersTexture registers 4 4• Temp registersTemp registers 2 2• Color registersColor registers 2 2• Constant registersConstant registers 8 8• Output registersOutput registers 0 0• Instruction countInstruction count 8 8
•+texture address ops 4
14DIRECTX®
Microsoft
DirectXDirectX®® 9 Pixel 9 Pixel ShadersShaders• Texture registersTexture registers 1616• Temp registersTemp registers 1212• Color registersColor registers 2 2• Texture CoordinatesTexture Coordinates 8 8• Output registersOutput registers 4 4• Instruction countInstruction count 6464
•+texture address ops 32•Supports flow controlSupports flow control
15DIRECTX®
Microsoft
DirectXDirectX®® 9.1 Pixel Shaders 9.1 Pixel Shaders
• Texture registersTexture registers 1616• Temporary registersTemporary registers 1616• Color registersColor registers 0 0• Texture CoordinatesTexture Coordinates 8 8• Constant registersConstant registers 6464• Output RegistersOutput Registers 4 4• Instruction countInstruction count 10241024• Dynamic flow controlDynamic flow control
16DIRECTX®
Microsoft
DirectXDirectX®® 6 API 6 API• Multitexture:Multitexture:
SetTSS( 0, COLOROP, SELECT );SetTSS( 0, COLOROP, SELECT );SetTSS( 0, COLORARG1, TEXTURE );SetTSS( 0, COLORARG1, TEXTURE );
SetTSS( 1, COLOROP, ADD );SetTSS( 1, COLOROP, ADD );SetTSS( 1, COLORARG1, CURRENT );SetTSS( 1, COLORARG1, CURRENT );SetTSS( 1, COLORARG2, TEXTURE );SetTSS( 1, COLORARG2, TEXTURE );
17DIRECTX®
Microsoft
Multi-Texture MacrosMulti-Texture Macros
SETINSTR( 0, SELECT, TEXTURE, * );SETINSTR( 0, SELECT, TEXTURE, * );SETINSTR( 1, ADD, CURRENT, TEXTURE );SETINSTR( 1, ADD, CURRENT, TEXTURE );
18DIRECTX®
Microsoft
DirectXDirectX®® 8 Assembly 8 Assembly
tex t0tex t0 ; base texture; base texturetex t1tex t1 ; environment map; environment map
add r0, t0, t1add r0, t0, t1 ; apply reflection; apply reflection
19DIRECTX®
Microsoft
DirectXDirectX®® 9 HLL Syntax 9 HLL Syntax
outColor = tex2d( baseTextureCoord, outColor = tex2d( baseTextureCoord, baseTexture )baseTexture )+ texCube( EnvironmentMapCoord, + texCube( EnvironmentMapCoord, Environment );Environment );
20DIRECTX®
Microsoft
Compulsory FiguresCompulsory Figures
• Specular Bump MappingSpecular Bump Mapping• BRDFsBRDFs• Procedural WoodProcedural Wood
21DIRECTX®
Microsoft
Per-Pixel SpecularPer-Pixel Specular
DIRECTX®Microsoft
22DIRECTX®
Microsoft
BRDFsBRDFs
23DIRECTX®
Microsoft
Basic Procedural WoodBasic Procedural Wood
24DIRECTX®
Microsoft
Wood with NoiseWood with Noise
25DIRECTX®
Microsoft
2-Hemisphere Model2-Hemisphere Model
Sky ColorSky Color
Ground Color
26DIRECTX®
Microsoft
Hemisphere LightingHemisphere Lighting
28DIRECTX®
Microsoft
Hemisphere ResultHemisphere Result
29DIRECTX®
Microsoft
Why an HLL?Why an HLL?
• Scalability vs hwScalability vs hw• Programming complexityProgramming complexity• Higher Level Language solves Higher Level Language solves
thesethese
30DIRECTX®
Microsoft
Design GoalsDesign Goals
• High level enough to hide High level enough to hide hardware specific detailshardware specific details
• Simple enough for efficient code Simple enough for efficient code generationgeneration
• Familiar enough to reduce Familiar enough to reduce learning curvelearning curve
• With enough optimizing back-ends With enough optimizing back-ends for portabilityfor portability
31DIRECTX®
Microsoft
Design BaselineDesign Baseline• ‘‘C’ -like syntaxC’ -like syntax• A standard languageA standard language
• like c++ or C# or HTML• in the VS.net IDE
32DIRECTX®
Microsoft
Graphics ArchitectureGraphics Architecture
Hardware
Driver
Direct3D
D3DX
Application
Code translation
Semantic mapping
Assembler, compiler,effects, utilities
33DIRECTX®
Microsoft
PreprocessorPreprocessor
• #define#define• #elif#elif• #else#else• #endif#endif• #error#error
• Function-style #defines not Function-style #defines not supportedsupported
• #if#if• #include#include• #line#line• #undef#undef
34DIRECTX®
Microsoft
TypesTypes
• Basic type is 32-bit floatBasic type is 32-bit float• Structs and arrays supportedStructs and arrays supported• Typedef to shorthand user defined Typedef to shorthand user defined
typestypes• Component access and swizzlesComponent access and swizzles
float float2 float3 float4 float2x1 float2x2 float2x3 float2x4 float3x1 float3x2 float3x3 float3x4 float4x1 float4x2 float4x3 float4x4
35DIRECTX®
Microsoft
VariablesVariables
• Local / globalLocal / global• StaticStatic
• Global variables that are not externally visible
• ConstConst• Cannot be modified by the shader program• Can be set external to the program
• Can have initializersCan have initializers• Can have semanticsCan have semantics
• For function parameters
36DIRECTX®
Microsoft
OperatorsOperators
• Pretty much all of C operatorsPretty much all of C operators• Including ?: and sizeof()
• No new language semanticsNo new language semantics• Despite temptation
• Arithmetic operators are per componentArithmetic operators are per component• Matrix multiply is an intrinsic functionMatrix multiply is an intrinsic function• Logical operators are per componentLogical operators are per component• No bitwise operatorsNo bitwise operators
37DIRECTX®
Microsoft
Statement SyntaxStatement Syntax
• { { [[statementsstatements] ] }}• [[expressionexpression] ] ;;• return return [[expressionexpression] ] ;;• if ( if ( expression expression ) ) statement statement [[else else
statementstatement]]• for ( for ( [[expression expression | | variable_declarationvariable_declaration] ] ; ;
[[expressionexpression] ] ; ; [[expressionexpression] ] ) ) statementstatement
38DIRECTX®
Microsoft
Intrinsic functionsIntrinsic functionssqrt value sqrt(value a) Square root exp2 value exp2(value a) Base 2 Exp log2 value log2(value a) Base 2 Log min value min(value a, value b) Maximum max value max(value a, value b) Minimum abs value abs(value a, value b) Absolute value len float len(value a) Vector length det float det(value a) Matrix determinant dot float dot(value a) Dot product mul value mul(value a, value b) Matrix multiplication any float any(value a) Logical OR of all input
components all float all(value a) Logical AND of all input
components
39DIRECTX®
Microsoft
User FunctionsUser Functions
• Standard C-like functionsStandard C-like functions• Output type and input parametersOutput type and input parameters• Parameters can be passed by copy Parameters can be passed by copy
in/copy out mechanismin/copy out mechanism• in/out declaration
• Inlined internally -no recursionInlined internally -no recursion
40DIRECTX®
Microsoft
Functions (cont.)Functions (cont.)
• Can be static (not externally Can be static (not externally accessible)accessible)
• Non-static functions parameters Non-static functions parameters must have Direct3D declarator must have Direct3D declarator semanticssemantics
• Parameters can be marked constParameters can be marked const• Parameters can have default Parameters can have default
initializersinitializers
41DIRECTX®
Microsoft
Differences from CDifferences from C
• No pointersNo pointers• No recursionNo recursion
42DIRECTX®
Microsoft
HLSL SummaryHLSL Summary• Ease of UseEase of Use
• Enable software developers• Consistency of ImplementationConsistency of Implementation
• Enable multiple vendors• Management of EvolutionManagement of Evolution
• Enable multiple generations• Result:Result:
• Fundamental architecture of DXG software stack and higher level language
43DIRECTX®
Microsoft
Application IntegrationApplication Integration
44DIRECTX®
Microsoft
Geometry MappingGeometry Mapping
• DirectXDirectX®® 8 Vertex Shaders 8 Vertex Shaders assume a data layoutassume a data layout• Decl & shader code are tied together• Forces shader author to communicate
with geometry provider• Standard register conventions can
help some• Complicates combining shadersComplicates combining shaders
45DIRECTX®
Microsoft
SemanticsSemantics
• DirectXDirectX®® 9 decouples decl from VS 9 decouples decl from VS• Both decl and VS refer to Both decl and VS refer to semanticssemantics
rather than register namesrather than register names•Direct3D runtime connects appropriate
vertex stream data to Vertex Shader registers
• Key feature of DirectX9 low-level APIKey feature of DirectX9 low-level API•driven by HLSL and shader requirements
46DIRECTX®
Microsoft
DX8 Vertex DX8 Vertex DeclarationDeclaration
v0v0 skipskip v1v1
Strm0Strm0 Strm1Strm1
Declaration
Vertex layout
vs 1.1vs 1.1mov r0, v0mov r0, v0……
Shader program
Shader handleShader handle
47DIRECTX®
Microsoft
pospos normnorm diffdiff
Strm0Strm0 Strm1Strm1
pospos normnorm diffdiff
Strm0Strm0
Declaration
Vertex layout
vs 1.1vs 1.1
dcl_position v0dcl_position v0
dcl_diffuse v1dcl_diffuse v1
mov r0, v0mov r0, v0
……
Shader program(Shader handle)
New Vertex New Vertex DeclarationDeclaration
vs 1.1vs 1.1
dcl_position v0dcl_position v0
dcl_diffuse v1dcl_diffuse v1
mov r0, v0mov r0, v0
……
48DIRECTX®
Microsoft
Vertex DeclarationVertex Declarationstruct D3DVERTEXELEMENT9struct D3DVERTEXELEMENT9{ {
Stream; Stream; // id from setstream()// id from setstream()Offset; Offset; // offset# verts into str// offset# verts into str Type; Type; // float vs byte, etc.// float vs byte, etc. Method; Method; // tessellator op// tessellator opUsage; Usage; // default semantic(pos, etc)// default semantic(pos, etc) UsageIndex UsageIndex // e.g. texcoord[#]// e.g. texcoord[#]
} }
49DIRECTX®
Microsoft
VS Input SemanticsVS Input Semantics
• position[n]position[n]• blendweight[n]blendweight[n]• blendindices[n]blendindices[n]• normal[n]normal[n]• psize[n]psize[n]• diffuse[n]diffuse[n]• specular[n]specular[n]• texcoord[n]texcoord[n]• tangent[n]tangent[n]• binormal[n]binormal[n]
50DIRECTX®
Microsoft
VS output / PS input VS output / PS input semanticssemantics• positionposition• psizepsize• fogfog• color[n]color[n]• texcoord[n]texcoord[n]
51DIRECTX®
Microsoft
Uses for SemanticsUses for Semantics
• A data binding protocol:A data binding protocol:• Between vertex data and shaders• Between pixel and vertex shaders• Between pixel shaders and hardware • Between shader fragments
52DIRECTX®
Microsoft
Integrating with Integrating with ApplicationsApplications• Extract dissassembly and use Extract dissassembly and use
as .asm shader code ala DX8as .asm shader code ala DX8• Use compiled shader objectUse compiled shader object
• Enables constant table access• Via ID3DXConstantTable Interface
• Use in an effect objectUse in an effect object• Manage constants, fallbacks, etc.• Via ID3DXEffect Interface
53DIRECTX®
Microsoft
Language API: Language API: StandaloneStandalone• Compiler returns a VS or PS and a symbol tableCompiler returns a VS or PS and a symbol table
• Maps extern constants to registers• Any expression of constants (i.e. per primitive Any expression of constants (i.e. per primitive
expressions) still performed per vertexexpressions) still performed per vertex• Symbol table is a set of constantsSymbol table is a set of constants
• ID3DXConstantTable interface
54DIRECTX®
Microsoft
ID3DXConstantTableID3DXConstantTable
• Exposes constant parameter metadataExposes constant parameter metadata• For convenient specification of shader input data
• SetMatrix( “curv”, matrix );SetMatrix( “curv”, matrix );• String or handle• D3DXHandle *hHandle
• SetVector()SetVector()• SetValue()SetValue()
• Use effect parametersUse effect parameters• Per primitive expressions of parameters Per primitive expressions of parameters
computed outside the vertex shadercomputed outside the vertex shader
55DIRECTX®
Microsoft
Shader ManagementShader Management
• Due to varying effects desired:Due to varying effects desired:•Diffuse, ambient, or specular terms•Skinning on or off, number of lights
• Due to different hardware generationsDue to different hardware generations•DirectX® 8, 9, 9.1
• Apps must manage a combinatoric Apps must manage a combinatoric number of shadersnumber of shaders
• FF pipeline does not have these issuesFF pipeline does not have these issues
56DIRECTX®
Microsoft
Solution: “Effects”Solution: “Effects”
• D3DX Effect FrameworkD3DX Effect Framework• Routines to load and select shadersRoutines to load and select shaders• Originally developed to manage Originally developed to manage
multitexture shaders in DirectX 6multitexture shaders in DirectX 6• Now fully integrated with the HLSLNow fully integrated with the HLSL
57DIRECTX®
Microsoft
Effect FrameworkEffect Framework• Encapsulation of device stateEncapsulation of device state• Enables scalable rendering techniquesEnables scalable rendering techniques• Allows controlled fallbackAllows controlled fallback• Can’t just switch to multi-passCan’t just switch to multi-pass
• Older hardware can’t do more passes since fill rate is less
• Helps rapid prototypingHelps rapid prototyping• Runtime interpretation of text-based
effect definition
58DIRECTX®
Microsoft
Effect Framework Effect Framework GoalsGoals• Standard way of representing shading Standard way of representing shading
algorithmsalgorithms• Ease of authoringEase of authoring• Enable some abstraction between author Enable some abstraction between author
of effect and user of the effectof 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 PlatformCross Platform• Efficient for runtime useEfficient for runtime use
59DIRECTX®
Microsoft
Effect ModelEffect ModelEffect Water
Technique DX8
Technique DX9
Technique DX7
pass0
pass1
pass0
pass0
60DIRECTX®
Microsoft
Effect FrameworkEffect FrameworkFallback TechniquesFallback Techniques
• Techniques are grouped by their Techniques are grouped by their quality or “LOD”quality or “LOD”
• Techniques can be chosen based on Techniques can be chosen based on what hardware creates successfullywhat hardware creates successfully
• Can test performance in back Can test performance in back bufferbuffer
• User responsible for drawing User responsible for drawing geometrygeometry
61DIRECTX®
Microsoft
Parameterized EffectsParameterized Effects• Effects can have parameters Effects can have parameters
of various typesof various types• Parameters augment static Parameters augment static
state description in the .fx filesstate description in the .fx files• How (and which) parameters get How (and which) parameters get
used defined by the effectused defined by the effect
62DIRECTX®
Microsoft
Parameterized EffectsParameterized Effects
• Shared parameters (aka Globals)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
• Naming convention for artist tweakableNaming convention for artist tweakable• Enables hooking UI to specific parameters• .X file reference for the same
63DIRECTX®
Microsoft
Design for Runtime Design for Runtime EfficiencyEfficiency• Compile effects into hardware Compile effects into hardware
friendly formatfriendly format• State blocks and push buffers
• Efficient setting of effect Efficient setting of effect parametersparameters• Can set parameters within effect
application• Integration with an optimizing Integration with an optimizing
language back endlanguage back end• Caching common parameter
combinations
64DIRECTX®
Microsoft
Unify Fixed and Unify Fixed and Programmable Programmable PipelinesPipelines• A consistent framework for A consistent framework for
programming shadersprogramming 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
65DIRECTX®
Microsoft
Unify Unify Cross-PlatformCross-Platform ProgrammingProgramming
• Techniques in an effect can span Techniques in an effect can span shader models (like VS 1.1, VS 2.0, shader models (like VS 1.1, VS 2.0, etc.)etc.)
• Unified parser allows handling the Unified parser allows handling the same effect file cross platformsame effect file cross platform• Only techniques that validate on runtime
platform available for actual use
66DIRECTX®
Microsoft
Effect-Language Effect-Language IntegrationIntegration• Techniques can call language functionsTechniques 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 Expressions of parameters can be used in state assignmentstate assignment
• Per-primitive expressions detected and Per-primitive expressions detected and optimized out by compiler when within an optimized out by compiler when within an effecteffect
67DIRECTX®
Microsoft
SpecializationSpecialization
• Specify constants that are to be Specify constants that are to be literalsliterals
• via ID3DXEffectCompiler Interfacevia ID3DXEffectCompiler Interface• Call CompileEffect() methodCall CompileEffect() method
• returns pre-optimized shader• Easily generate multiple shaders Easily generate multiple shaders
optimized for specific casesoptimized for specific cases• Can helps shader management by Can helps shader management by
generating them on the flygenerating them on the fly
68DIRECTX®
Microsoft
Linking Effects with Linking Effects with GeometryGeometry• .X file extensions to link effect with .X file extensions to link effect with
geometrygeometry• Enhance the notion of materials to include
effect files• Inline or external references
• Besides referencing effects, certain parameter values can be specialized in EffectInstance
• This is usually the artist tweakable ones
• D3DX API support for associating effects D3DX API support for associating effects with mesheswith meshes
69DIRECTX®
Microsoft
Fragment LinkerFragment Linker
• Efficiently links togetherEfficiently links together• HLL fragments• And asm fragments
• Current Features:Current Features:• Symbol table resolution• Register use optimization• Dead code removal
• via ID3DXFragmentLinker interfacevia ID3DXFragmentLinker interface
70DIRECTX®
Microsoft
Performance Performance • Compiler updates will be frequentCompiler updates will be frequent• Microsoft has good compiler peopleMicrosoft has good compiler people
71DIRECTX®
Microsoft
Current Back EndsCurrent Back Ends
• Vertex Shader 1.1, 2.0Vertex Shader 1.1, 2.0• Pixel Shader 1.1, 1.4, 2.0Pixel Shader 1.1, 1.4, 2.0
72DIRECTX®
Microsoft
SummarySummary
• HLSL abstraction solveHLSL abstraction solve•Continuing hardware evolution•Shader programming complexity
• API semantics solveAPI semantics solve•Shader interoperability
• D3DX Effects, specializers solveD3DX Effects, specializers solve•Combinatoric numbers of shaders
73DIRECTX®
Microsoft
SummarySummary
• HLSL is the next step in graphics HLSL is the next step in graphics API/hardware evolutionAPI/hardware evolution
• DirectX implementation providesDirectX implementation provides•Close API integration
•Semantic binding to low-level API•Shader management via D3DX effects•Full IDE support including debugging
•Performant cross vendor support
74DIRECTX®
Microsoft
• Check it out!Check it out!• Use it in your research Use it in your research
&development&development• Let us know what you thinkLet us know what you think
Action ItemsAction Items
75DIRECTX®
Microsoft
QuestionsQuestions
76DIRECTX®
Microsoft
BackupBackup
78DIRECTX®
Microsoft
Unique FeaturesUnique Features
• Cleanly integrated with Direct3D Cleanly integrated with Direct3D APIAPI
• Run-time compilableRun-time compilable• Integrated with Effect FrameworkIntegrated with Effect Framework
79DIRECTX®
Microsoft
API IntegrationAPI Integration
• Co-designed with DirectX 9 APICo-designed with DirectX 9 API• Enables ease-of-use in apps and in Enables ease-of-use in apps and in
toolstools• Enables improved performanceEnables improved performance
80DIRECTX®
Microsoft
Run-Time CompilableRun-Time Compilable
• Enables CPU to construct and Enables CPU to construct and optimize shaders for GPU optimize shaders for GPU dynamicallydynamically
• Facilitates authoring tool/art Facilitates authoring tool/art pipelinepipeline•Enables dynamic content/titles