You are on page 1of 47

Direct3D Shader

Programming
HLSL (High Level Shader Language)
Introduction to DirectX / HLSL
- Tutorial01
-

Fixed pipe-line version


Programmable pipe-line (shader) version

Today
Introduction to DirectX / HLSL
Tutorial01
- Fixed pipeline version
- Programmable pipeline (shader) version

DirectX / HLSL

DirectX (1/2)

1994 WinG

1995 DirectX 1.0

1995 WinG GDI


1996 DirectX 2.0

2D sprite
bitmap API API
5 (KOEI)


DOS Windows

1994 DirectX 3.0, 1997 DirectX 5.0,


1998 DirectX 6.0, 1999 DirectX 7.0

DirectX (2/2)

2000 DirectX 8.0

Assembler Shader

2002 12 DirectX 9.0 ,

HLSL

9.0a (2003.3), 9.0b (2003.8), 9.0c (2003.8)

200611 Directx 10.0

Geometry shader

10.1 (2008.2, 2009.4)

2009 10 DirectX 11.0

Hull shader, Domain shader

11.1 (2011.2, windows 8.0), 11.2 (, windows 8.1)

DirectX

object
object
E.g. Direct3D object Direct3DDevice object
2D graphics
3D graphics

Audio

Game input

Not Recommended

Direct3D

Microsoft DirectX graphics


O/S graphics hardware

Direct3D
Immediate Mode

Direct3D Device

Rendering state (rendering context)


Transformation, lighting, rasterization
2
HAL device

Reference (emulator)


Direct3D

HLSL(High Level Shader Language)

HLSL (High Level Shader Language)

High level shader language by Microsoft

Included in DX 9.0, 10.0, 10.1, 11.0 spec. and


Visual Studio .NET

Assembler type shader language in DX8.0

Similar syntax to C with many restrictions and


exceptions

Preprocessor

#define
#elif
#else
#endif
#error

#if
#include
#line
#undef

Types (1/2)

Basic types

float
Int
bool
double
Half

Structure and Array

Texture .

Texture array Direct3D11

Structure array element .

Types (2/2)

Vectors and Matrices

Type definition to shorthand user defined types

Defined for all basic types


Int1~4, half1~4, etc.
Component access and swizzles supported on vector/
matrix types
FloatVector.xyz
FloatVector.yyxz
FloatMatrix._11_12 or FloatMatrix[1][1]

float1, float2, float3, float4


Float1x1, float1x2 float4x4

Variables

Local/ global

Global

Uniform

Const

Cannot be modified by the shader program

Static

Can be set from the outside (CPU

Not externally visible

Can have initializers


Can have semantics for function parameters

Texture & Sampler

Texture

Direct3DTexture9

Sampler : filtering


sampler texture mapping
H/W shader version
HLSL 3.0 16
Filtering, address mode sampler
SetSamplerState

Operators

Almost 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

Statement Syntax

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

Some Intrinsic functions


Function

Explanation

Syntax

sqrt

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

User Functions

Standard C-like functions


Output type and input parameters
Parameters can be passed by copy in/copy out
mechanism

in/ out declaration

In-lined internally - no recursion

Have no statcks

Functions (cont.)

In the same effect only- Static (not externally


accessible)
Parameters can be marked const
Parameters can have default initializers

Differences from C

No pointers
No recursion
Limitation of loop count and nesting

HLSL Summary

Ease of Use

Consistency of Implementation

Enable software developers


Enable multiple vendors

Management of Evolution

Enable multiple generations

Shader Model

HLSL version

Under 1_1

2_a


( )
DirectX SDK
( unrolling)

3_0, 4_0, 5_0

Dynamic branching
(jump)

dynamic 1
256 (loop counter register , 8bit)

Tutorial 01. Fixed pipeline version

Tutorial 01: CreateDevice

windows framework direct3d


Tutorial 01

Direct3D

Scene rendering
Direct3D

Direct3D
1) application window
2) Direct3D object
3) Direct3DDevice object
4) objects
5) Rendering

Windows
RegisterClassEx()
CreateWindow()
ShowWindow(), UpdateWindow()
Message Loop

GetMessage()
TranslateMessage()
DispatchMessage()

Message loop

Windows Message Loop


D3D
RegisterClassEx()
CreateWindow()
InitD3D() Direct3D .
ShowWindow(), UpdateWindow()
Message Loop

GetMessage()
TranslateMessage()
DispatchMessage()

Render() Direct3D
Message loop Direct3D

Step 1 - Creating a Window

WinMain windows application framework

Message

Step 2 - Initializing Direct3D (1/3)

Window Direct3D Direct3DDevice

D3D_SDK_VERSION : SDK version


,

// full screen
// double buffering
// windows

Step 2 - Initializing Direct3D (2/3)

Window Direct3D Direct3DDevice

D3D_SDK_VERSION : SDK version


,

// full screen
// double buffering
// windows

Buffers for Rendering

Double buffering (D3DSWAPEFFECT_DISCARD )

Back frame buffer

Depth/ stencil
buffer
Graphic H/w

Screen

Color buffer, depth buffer, stencil buffer

Front frame buffer

Color buffer :
Depth/ stencil buffer : color buffer masking
, color buffer
1:1 .

Buffers for Rendering

Double buffering (D3DSWAPEFFECT_DISCARD )

Front frame buffer

Depth/ stencil
buffer
Graphic H/w

Screen

Color buffer, depth buffer, stencil buffer

Back frame buffer

Color buffer :
Depth/ stencil buffer : color buffer masking
, color buffer
1:1 .

Step 2 - Initializing Direct3D (3/3)

Direct3DDevice

CreateDevice()

[in] UINT Adapter:

[in] D3DDEVTYPE DeviceType:

,
D3DDEVTYPE_HAL : H/W rendering

[in] HWND hFocusWindow (hWnd)


D3DADAPTER_DEFAULT : adapter

[in] DWORD BehaviorFlags

Device flag
D3DCREATE_SOFTWARE_VERTEXPROCESSING
D3DCREATE_HARDWARE_VERTEXPROCESSING

&d3dpp

[in, out] D3DPRESENT_PARAMETERS pPresentationParameters:

[out, retval] IDirect3DDevice9 ppReturnedDeviceInterface : &g_pd3dDevice

Device

Step 3 - Handling System


Messages

Windows message queue

rendering

Message infinite loop


- message (get),
- (translate),
- message
(dispatch).

Step 3 - Handling System


Messages

render() rendering

Step 4 - Rendering and Displaying a


Scene

Render target, depth stencil buffer


- Opengl glClear()

Back buffer
rendering

OpenGL glBegin(),
glEnd()

Back buffer, front buffer


- (present)
- OpenGL glFlush(), gluSwapChainBuffers()

Clear() & Present()

Clear() : render target, depth/ stencil buffer

, 0, NULL

render target, depth buffer, stencil buffer | )

D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0

render target, depth buffer, stensil buffer

Render target rectangles


Rectangle D3DRECT (long x1, y1, x2, y2 )

render target render target .

Present() : back buffer, front buffer

NULL ( )

Step 5 - Shutting Down

WM_DESTROY Direct3D objects

Tutorial 01. Programable pipeline


(shader ) Version

Shader version of Tutorial01

Fixed pipeline version

Effect file (shader file, .fx file) loading

Technique setting

Pass setting

Load a Effect file (.fx)


( CreateEffectFromFile() )

Load a Effect file (.fx)

Shader (.fx file) loading (LPD3DXEFFECT interface)

D3DXCreateEffectFromFile() : 4 parameters
- LPDIRECT3DDEVICE9 pDevice
- LPCTSTR pSrcFile
- DWORD Flags
- LPD3DXEFFECT *ppEffect)

Setting A Technique and A Pass :


render()

Shader Codes (basic_shader.fx)

Effect file
struct VS_Input
{
float3 Pos
float4 Color
};
struct PS_Input
{
float4 Pos
float4 Color
};

No Input
D3DXCreateEffectFromFile()

0 1

15

Effect

Stream

Technique_Basic

VS_Basic()

: POSITION;
: COLOR0;

PS_Input VS_Basic(VS_Input In)


{
PS_Input Out = (PS_Input);

return Out;
}
float4 PS_Basic(PS_Input In) : COLOR0
{
return In.Color;
}

SetTechnique("Technique_Basic)

Vertex
Processing

: POSITION;
: COLOR0;

Begin(&Passes, 0)
BeginPass(0)

Rasterizer
pass0
Fragment
Processing

pass pass1
{
VertexShader = compile vs_3_0 VS_01();
PixelShader = compile ps_3_0 VS_01();
}

PS_Basic()
}

No Drawing
Output Merging
Pixels

Technique Technique_Basic
{
pass pass0
{
VertexShader = compile vs_3_0 VS_Basic();
PixelShader = compile ps_3_0 PS_Basic();
}

Effect file load, technique technique pass


(set) rendering pass

Technique Technique02
{
pass pass0
{
VertexShader = compile vs_3_0 VS_02();
PixelShader = compile ps_3_0 PS_02();
}
pass pass1 { }
}
Technique Technique03
{
pass pass0 { }
pass pass1 { }

}
Technique Technique03 { }

Tomorrow
Tutorial02
- Fixed pipeline version
- Programmable pipeline (shader) version

You might also like