1 / 49

DirectX High-Level Shading Language

Outline. What drove the language design?BackgroundWhat does it look like?Syntax definitionHow does it work?API integrationHow to use it efficiently?Tips and Tricks. DirectX

janeeva
Download Presentation

DirectX High-Level Shading Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. DirectX® High-Level Shading Language Chas. Boyd DirectX Graphics Architect Microsoft

    2. Outline What drove the language design? Background What does it look like? Syntax definition How does it work? API integration How to use it efficiently? Tips and Tricks

    3. DirectX® 8 Assembly tex t0 ; base texture tex t1 ; environment map add r0, t0, t1 ; apply reflection

    4. DirectX® 9 HLL Syntax outColor = tex2d( baseTextureCoord, baseTexture ) + texCube( EnvironmentMapCoord, Environment );

    5. Why an HLL? Scalability vs hw Programming complexity Higher Level Language solves these

    6. 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

    7. Design Baseline ‘C’ -like syntax A standard language like c++ or C# or HTML in the VS.net IDE

    8. Graphics Architecture

    9. Preprocessor #define #elif #else #endif #error #if #include #line #undef

    10. Types Basic types float int bool double half Structs and arrays supported

    11. Vectors and Matrices Typedef to shorthand user defined types float1, float2, float3, float4 Float1x1, float1x2 … float4x4 Defined for all basic types Int1-4, half1-4, etc. Component access and swizzles supported on vector/matrix types FloatVector.xyz FloatMatrix._11_12 or FloatMatrix[1][1]

    12. 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

    13. Operators Pretty much all of C operators Including ?:, ++, --, +=, -=, etc 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

    14. Statement Syntax { [statements] } [expression] ; return [expression] ; if ( expression ) statement [else statement] for ( [expression | variable_declaration] ; [expression] ; [expression] ) statement

    15. Some Intrinsic functions

    16. 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

    17. 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

    18. Differences from C No pointers No recursion

    19. 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

    20. 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

    21. 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

    22. DX8 Vertex Declaration

    23. New Vertex Declaration

    24. 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[#] }

    25. VS Input Semantics position[n] blendweight[n] blendindices[n] normal[n] psize[n] diffuse[n] specular[n] texcoord[n] tangent[n] binormal[n]

    26. VS output / PS input semantics position psize fog color[n] texcoord[n]

    27.  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 Highlight Semantics deprecate connectorsHighlight Semantics deprecate connectors

    28. 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

    29. 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

    30. ID3DXConstantTable Exposes constant parameter metadata For convenient specification of shader input data SetMatrix( “curv”, matrix ); String or handle D3DXHandle *hHandle SetVector() SetValue() Use effect parameters Per primitive expressions of parameters computed outside the vertex shader

    31. Performance  Compiler updates will be frequent Microsoft has good compiler people

    32. Current Back Ends Vertex Shader 1.1, 2.0 Pixel Shader 1.1, 1.4, 2.0

    33. Tips And Tricks Using the int datatype Using matrix datatypes How do if statements work Using constant specialization Pixel shader 1.x optimizations

    34. Int Datatype Declare indexing variables as ints avoids unnecessary frc's used to truncate allows for other int optimizations What are the frc’s required for? The float to int truncation must happen before multiplying by the size of the datatype for correct results

    35. Int Index example OutPos = mul(WorldArray[Index], Pos);

    36. Matrix Datatypes Advantages over array of vectors Will be stored in optimal format Column major or row major depending on usage Easy to cast down to 4x3, 3x3, etc Allows for better performance and correct behavior Matrix is supported by set of intrinsics Column major is preferred storage Recommend mul(matrix, vector) order Allows the compiler to use dp4/3s

    37. If statements All back ends support if statements If branching is not supported (i.e. vs.1.1) Both sides of the if are executed and final result chosen Depending on the conditions this can be expensive If constant branching is supported (i.e. 2.0) If the condition is constant, constant branch instructions are used Else will fall back to the vs 1.1 solution

    38. If statement example

    39. Constant Specialization Specify constants that are to be literals via ID3DXEffectCompiler Interface Call CompileShader() method returns pre-optimized shader or effect Easily generate multiple shaders optimized for specific cases Can help shader management by generating them on the fly

    40. PS 1.x optimizations Modifiers automatically used Complement, negate, x2, sat, etc Optimizes for Co-issue Instruction reordering done to utilize Still keep 1.x shaders simple Doesn’t have arbitrary swizzles If bad swizzle requested compile will fail Limited instruction count Complex shaders are possible Modifiers allow for a lot of computation in a small number of instructions Effective co-issue use helps as well

    41. PS 1.x sample shader

    42. Input Datatype Declarations Important to provide good type information for program inputs All int input should be declared as int Matrix indices, lookup values, etc. If the data is not integer odd results can happen! Take advantage of expansion to float4 i.e. declare Position as float4 If the vertex data has x,y, and z then w will be filled in with 1.0

    43. HLSL shader sample Wood Sample shader Thanks to Jason Mitchell (ATI) Procedural wood Complex - rings, wobble, noise

    44. hlsl_wood()

    45. Hlsl_wood() asm

    46. Summary HLSL abstraction solve Continuing hardware evolution Shader programming complexity API semantics solve Shader interoperability

    47. 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

    48. Check it out! Use it in your research &development Let us know what you think directx@microsoft.com http://msdn.microsoft.com/directx Action Items

    49. Questions

    50. Backup

More Related