chapter 5: directx ® chas. boyd directx ® microsoft 3 outlineoutline what drove the language...

78

Upload: amanda-watson

Post on 18-Jan-2018

229 views

Category:

Documents


0 download

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 Framework

TRANSCRIPT

Page 1: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What
Page 2: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

Chapter 5: DirectXChapter 5: DirectX®®

Chas. BoydChas. Boyd

Page 3: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 4: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

4DIRECTX®

Microsoft

BackgroundBackground

• The DirectXThe DirectX®® Process Process• Evolution of DirectXEvolution of DirectX®®

Page 5: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 6: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 7: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

7DIRECTX®

Microsoft

Industry ProcessIndustry Process

HardwareVendors

SoftwareDevelopers

End Users

DirectXTeam

DirectX Components New Hardware

Page 8: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 9: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 10: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 11: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 12: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 13: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 14: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 15: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 16: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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 );

Page 17: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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 );

Page 18: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 19: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

19DIRECTX®

Microsoft

DirectXDirectX®® 9 HLL Syntax 9 HLL Syntax

outColor = tex2d( baseTextureCoord, outColor = tex2d( baseTextureCoord, baseTexture )baseTexture )+ texCube( EnvironmentMapCoord, + texCube( EnvironmentMapCoord, Environment );Environment );

Page 20: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

20DIRECTX®

Microsoft

Compulsory FiguresCompulsory Figures

• Specular Bump MappingSpecular Bump Mapping• BRDFsBRDFs• Procedural WoodProcedural Wood

Page 21: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

21DIRECTX®

Microsoft

Per-Pixel SpecularPer-Pixel Specular

DIRECTX®Microsoft

Page 22: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

22DIRECTX®

Microsoft

BRDFsBRDFs

Page 23: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

23DIRECTX®

Microsoft

Basic Procedural WoodBasic Procedural Wood

Page 24: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

24DIRECTX®

Microsoft

Wood with NoiseWood with Noise

Page 25: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

25DIRECTX®

Microsoft

2-Hemisphere Model2-Hemisphere Model

Sky ColorSky Color

Ground Color

Page 26: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

26DIRECTX®

Microsoft

Hemisphere LightingHemisphere Lighting

Page 27: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

28DIRECTX®

Microsoft

Hemisphere ResultHemisphere Result

Page 28: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 29: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 30: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 31: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

32DIRECTX®

Microsoft

Graphics ArchitectureGraphics Architecture

Hardware

Driver

Direct3D

D3DX

Application

Code translation

Semantic mapping

Assembler, compiler,effects, utilities

Page 32: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 33: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 34: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 35: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 36: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 37: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 38: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 39: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 40: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

41DIRECTX®

Microsoft

Differences from CDifferences from C

• No pointersNo pointers• No recursionNo recursion

Page 41: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 42: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

43DIRECTX®

Microsoft

Application IntegrationApplication Integration

Page 43: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 44: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 45: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 46: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

……

Page 47: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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[#]

} }

Page 48: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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]

Page 49: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

50DIRECTX®

Microsoft

VS output / PS input VS output / PS input semanticssemantics• positionposition• psizepsize• fogfog• color[n]color[n]• texcoord[n]texcoord[n]

Page 50: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 51: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 52: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 53: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 54: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 55: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 56: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 57: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 58: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

59DIRECTX®

Microsoft

Effect ModelEffect ModelEffect Water

Technique DX8

Technique DX9

Technique DX7

pass0

pass1

pass0

pass0

Page 59: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 60: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 61: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 62: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 63: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 64: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 65: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 66: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 67: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 68: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 69: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

70DIRECTX®

Microsoft

Performance Performance • Compiler updates will be frequentCompiler updates will be frequent• Microsoft has good compiler peopleMicrosoft has good compiler people

Page 70: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 71: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 72: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 73: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

[email protected]

Action ItemsAction Items

Page 74: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

75DIRECTX®

Microsoft

QuestionsQuestions

Page 75: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

76DIRECTX®

Microsoft

BackupBackup

Page 76: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 77: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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

Page 78: Chapter 5: DirectX ® Chas. Boyd DIRECTX ® Microsoft 3 OutlineOutline What drove the language design? What drove the language design? Background What

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