Chapter 5: DirectX® Chas. Boyd Outline • What drove the language design? • Background • What does it look like? • Syntax definition • How does it work? • API integration • How is it used? • Effects Framework Microsoft DIRECTX® 3 Background • The DirectX® Process • Evolution of DirectX® Microsoft DIRECTX® 4 Industry Process • Based on iteration with partners • Requirements from software developers • Feasibilities from hardware developers • Combine to deliver product for • • programmers Pipelined to work in parallel Relies on Reference Implementation Microsoft DIRECTX® 5 Direct3D Reference • Software model of hardware • Analogous to/input to “C” model stage of hardware design • Acts as “compilable specification:” • Used to communicate new features and requirements to IHVs • Provided as comparison to ISVs • to check behavior of drivers while coding • Used to test drivers for conformance • Color rules by Jim Blinn Microsoft DIRECTX® 6 Industry Process DirectX Team Hardware Vendors DirectX Components New Hardware Software Developers End Users Microsoft DIRECTX® 7 DirectX Evolution DX9 Float Pixels DX8.1 More Shaders DX8 Programmability DX7 Hardware T&L DX6 Multi-Texture DX5 DrawPrim DX3 1st Direct3D 96 97 98 99 00 01 02 03 Microsoft DIRECTX® 04 8 DirectX Graphics Architecture Vec0 Vec2 Vec3 Vertex Shader VB Pos Image Surface Vec1 Color TC1 Tex0 Primitive Ops Tex1 Pixel Shader Tex2 Samplers Output pixels Vector Data Geometry Ops TC2 Vertex Components Pixel Ops Microsoft DIRECTX® 9 Vertex Shader Vec0 Vec1 Vec2 Vec3 Vec4 … Vec15 A0 R0 Const0 R1 Const1 R2 Const2 Vertex ALU R3 Const3 … … R11 Const95 Hpos TC0 TC1 TC2 TC3 Color0 Color1 Microsoft DIRECTX® 10 Pixel Shader Architecture Hpos TC0 color TC1 TC2 TC3 d0 d1 constant texture Stage0 t0 c0 Stage1 t1 c1 Stage2 t2 Stage3 t3 c3 temp c4 c2 r0 r1 ... Texture Stages Pixel ALU Pixel c7 Microsoft DIRECTX® 11 DirectX® 6 Multitexture • Texture registers 2-8 • Temp registers 1 • Color registers 2 • Constant registers 1 • Output registers 0 • Instruction count 2-8 • Supports dependent reads Microsoft DIRECTX® 12 DirectX® 8 Pixel Shaders • Texture registers • Temp registers • Color registers • Constant registers • Output registers • Instruction count • +texture address ops 4 2 2 8 0 8 4 Microsoft DIRECTX® 13 DirectX® 9 Pixel Shaders • Texture registers • Temp registers • Color registers • Texture Coordinates • Output registers • Instruction count • +texture address ops 16 12 2 8 4 64 32 • Supports flow control Microsoft DIRECTX® 14 DirectX® 9.1 Pixel Shaders • Texture registers 16 • Temporary registers 16 • Color registers 0 • Texture Coordinates 8 • Constant registers 64 • Output Registers 4 • Instruction count 1024 • Dynamic flow control Microsoft DIRECTX® 15 DirectX® 6 API • Multitexture: SetTSS( 0, COLOROP, SELECT ); SetTSS( 0, COLORARG1, TEXTURE ); SetTSS( 1, COLOROP, ADD ); SetTSS( 1, COLORARG1, CURRENT ); SetTSS( 1, COLORARG2, TEXTURE ); Microsoft DIRECTX® 16 Multi-Texture Macros SETINSTR( 0, SELECT, TEXTURE, * ); SETINSTR( 1, ADD, CURRENT, TEXTURE ); Microsoft DIRECTX® 17 DirectX® 8 Assembly tex t0 tex t1 ; base texture ; environment map add r0, t0, t1 ; apply reflection Microsoft DIRECTX® 18 DirectX® 9 HLL Syntax outColor = tex2d( baseTextureCoord, baseTexture ) + texCube( EnvironmentMapCoord, Environment ); Microsoft DIRECTX® 19 Compulsory Figures • Specular Bump Mapping • BRDFs • Procedural Wood Microsoft DIRECTX® 20 Per-Pixel Specular Microsoft DIRECTX® 21 BRDFs Microsoft DIRECTX® 22 Basic Procedural Wood Microsoft DIRECTX® 23 Wood with Noise Microsoft DIRECTX® 24 2-Hemisphere Model Sky Color q Ground Color Microsoft DIRECTX® 25 Hemisphere Lighting Microsoft DIRECTX® 26 Hemisphere Result Microsoft DIRECTX® 28 Why an HLL? • Scalability vs hw • Programming complexity • Higher Level Language solves these Microsoft DIRECTX® 29 Design Goals • High level enough to hide hardware • • • specific details Simple enough for efficient code generation Familiar enough to reduce learning curve With enough optimizing back-ends for portability Microsoft DIRECTX® 30 Design Baseline • ‘C’ -like syntax • A standard language • like c++ or C# or HTML • in the VS.net IDE Microsoft DIRECTX® 31 Graphics Architecture Application D3DX Direct3D Driver Assembler, compiler, effects, utilities Semantic mapping Code translation Hardware Microsoft DIRECTX® 32 Preprocessor • • • • • #define #elif #else #endif #error • • • • #if #include #line #undef • Function-style #defines not supported Microsoft DIRECTX® 33 Types • Basic type is 32-bit float • Structs and arrays supported • Typedef to shorthand user defined • types Component access and swizzles float float2x1 float3x1 float4x1 float2 float2x2 float3x2 float4x2 float3 float2x3 float3x3 float4x3 float4 float2x4 float3x4 float4x4 Microsoft DIRECTX® 34 Variables • Local / global • Static • Global variables that are not externally visible • Const • Cannot be modified by the shader program • Can be set external to the program • Can have initializers • Can have semantics • For function parameters Microsoft DIRECTX® 35 Operators • Pretty much all of C operators • Including ?: and sizeof() • No new language semantics • Despite temptation • • • • Arithmetic operators are per component Matrix multiply is an intrinsic function Logical operators are per component No bitwise operators Microsoft DIRECTX® 36 Statement Syntax • • • • • { [statements] } [expression] ; return [expression] ; if ( expression ) statement [else statement] for ( [expression | variable_declaration] ; [expression] ; [expression] ) statement Microsoft DIRECTX® 37 Intrinsic functions sqrt exp2 log2 min max abs len det dot mul any value value value value value value float float float value float sqrt(value a) exp2(value a) log2(value a) min(value a, value max(value a, value abs(value a, value len(value a) det(value a) dot(value a) mul(value a, value any(value a) all float all(value a) b) b) b) b) Square root Base 2 Exp Base 2 Log Maximum Minimum Absolute value Vector length Matrix determinant Dot product Matrix multiplication Logical OR of all input components Logical AND of all input components Microsoft DIRECTX® 38 User Functions • Standard C-like functions • Output type and input parameters • Parameters can be passed by copy in/copy out mechanism • in/out declaration • Inlined internally -no recursion Microsoft DIRECTX® 39 Functions (cont.) • Can be static (not externally • • • accessible) Non-static functions parameters must have Direct3D declarator semantics Parameters can be marked const Parameters can have default initializers Microsoft DIRECTX® 40 Differences from C • No pointers • No recursion Microsoft DIRECTX® 41 HLSL Summary • Ease of Use • Enable software developers • Consistency of Implementation • Enable multiple vendors • Management of Evolution • Enable multiple generations • Result: • Fundamental architecture of DXG software stack and higher level language Microsoft DIRECTX® 42 Application Integration Microsoft DIRECTX® 43 Geometry Mapping • DirectX® 8 Vertex Shaders assume 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 shaders Microsoft DIRECTX® 44 Semantics • DirectX® 9 decouples decl from VS • Both decl and VS refer to semantics rather than register names • Direct3D runtime connects appropriate vertex stream data to Vertex Shader registers • Key feature of DirectX9 low-level API • driven by HLSL and shader requirements Microsoft DIRECTX® 45 DX8 Vertex Declaration Strm0 Strm1 Vertex layout v0 skip vs 1.1 mov r0, v0 … v1 Declaration Shader handle Shader program Microsoft DIRECTX® 46 New Vertex Declaration Strm0 Strm1 Strm0 Vertex layout pos norm diff Declaration pos norm diff vs 1.1 vs 1.1 dcl_position v0 dcl_position v0 dcl_diffuse v1 dcl_diffuse v1 mov r0, v0 … Shader program (Shader handle) mov r0, v0 Microsoft … DIRECTX® 47 Vertex Declaration struct D3DVERTEXELEMENT9 { Stream; // id from setstream() Offset; // offset# verts into str Type; // float vs byte, etc. Method; // tessellator op Usage; // default semantic(pos, etc) UsageIndex // e.g. texcoord[#] } Microsoft DIRECTX® 48 VS Input Semantics • • • • • • • • • • position[n] blendweight[n] blendindices[n] normal[n] psize[n] diffuse[n] specular[n] texcoord[n] tangent[n] binormal[n] Microsoft DIRECTX® 49 VS output / PS input semantics • position • psize • fog • color[n] • texcoord[n] Microsoft DIRECTX® 50 Uses for Semantics • A data binding protocol: • • • • Between vertex data and shaders Between pixel and vertex shaders Between pixel shaders and hardware Between shader fragments Microsoft DIRECTX® 51 Integrating with Applications • Extract dissassembly and use as .asm • shader code ala DX8 Use compiled shader object • Enables constant table access • Via ID3DXConstantTable Interface • Use in an effect object • Manage constants, fallbacks, etc. • Via ID3DXEffect Interface Microsoft DIRECTX® 52 Language API: Standalone • Compiler returns a VS or PS and a symbol table • Maps extern constants to registers • Any expression of constants (i.e. per • primitive expressions) still performed per vertex Symbol table is a set of constants • ID3DXConstantTable interface Microsoft DIRECTX® 53 ID3DXConstantTable • Exposes constant parameter metadata • For convenient specification of shader input data • SetMatrix( “curv”, matrix ); • String or handle • D3DXHandle *hHandle • SetVector() • SetValue() Microsoft DIRECTX® 54 Shader Management • Due to varying effects desired: • Diffuse, ambient, or specular terms • Skinning on or off, number of lights • Due to different hardware generations • DirectX® 8, 9, 9.1 • Apps must manage a combinatoric • number of shaders FF pipeline does not have these issues Microsoft DIRECTX® 55 Solution: “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 Microsoft DIRECTX® 56 Effect Framework • Encapsulation of device state • Enables scalable rendering • • techniques Allows controlled fallback Can’t just switch to multi-pass • Older hardware can’t do more passes since fill rate is less • Helps rapid prototyping • Runtime interpretation of text-based effect definition Microsoft DIRECTX® 57 Effect 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 Microsoft DIRECTX® 58 Effect Model Effect Water Technique DX9 pass0 pass1 Technique DX8 pass0 Technique DX7 pass0 Microsoft DIRECTX® 59 Effect Framework Fallback 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 Microsoft DIRECTX® 60 Parameterized 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 Microsoft DIRECTX® 61 Parameterized 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 • Naming convention for artist tweakable • Enables hooking UI to specific parameters • .X file reference for the same Microsoft DIRECTX® 62 Design for Runtime Efficiency • Compile effects into hardware • friendly format • State blocks and push buffers Efficient setting of effect parameters • Can set parameters within effect application • Integration with an optimizing language back end • Caching common parameter combinations Microsoft DIRECTX® 63 Unify 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 Microsoft DIRECTX® 64 Unify 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 Microsoft DIRECTX® 65 Effect-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 Microsoft DIRECTX® 66 Specialization • 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 Microsoft DIRECTX® 67 Linking Effects with Geometry • .X file extensions to link effect with geometry • 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 with meshes Microsoft DIRECTX® 68 Fragment Linker • Efficiently links together • HLL fragments • And asm fragments • Current Features: • Symbol table resolution • Register use optimization • Dead code removal • via ID3DXFragmentLinker interface Microsoft DIRECTX® 69 Performance • Compiler updates will be frequent • Microsoft has good compiler people Microsoft DIRECTX® 70 Current Back Ends • Vertex Shader 1.1, 2.0 • Pixel Shader 1.1, 1.4, 2.0 Microsoft DIRECTX® 71 Summary • HLSL abstraction solve • Continuing hardware evolution • Shader programming complexity • API semantics solve • Shader interoperability • D3DX Effects, specializers solve • Combinatoric numbers of shaders Microsoft DIRECTX® 72 Summary • HLSL is the next step in graphics • API/hardware evolution DirectX 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 Microsoft DIRECTX® 73 Action Items • Check it out! • Use it in your research &development • Let us know what you think • directx@microsoft.com Microsoft DIRECTX® 74 Questions Microsoft DIRECTX® 75 Backup Microsoft DIRECTX® 76 Unique Features • Cleanly integrated with Direct3D API • Run-time compilable • Integrated with Effect Framework Microsoft DIRECTX® 78 API Integration • Co-designed with DirectX 9 API • Enables ease-of-use in apps and in • tools Enables improved performance Microsoft DIRECTX® 79 Run-Time Compilable • Enables CPU to construct and • optimize shaders for GPU dynamically Facilitates authoring tool/art pipeline • Enables dynamic content/titles Microsoft DIRECTX® 80