DirectX Tutorial 01

download DirectX Tutorial 01

of 47

Transcript of DirectX Tutorial 01

  • 8/10/2019 DirectX Tutorial 01

    1/47

    Direct3D Shader

    Programming

    HLSL (High Level Shader Language)

    -Introduction to DirectX / HLSL

    -Tutorial01

    - Fixed pipe-line version- Programmable pipe-line (shader) version

  • 8/10/2019 DirectX Tutorial 01

    2/47

    Today

    Introduction to DirectX / HLSL

    Tutorial01

    - Fixed pipeline version

    - Programmable pipeline (shader) version

  • 8/10/2019 DirectX Tutorial 01

    3/47

    DirectX / HLSL

  • 8/10/2019 DirectX Tutorial 01

    4/47

    DirectX(1/2)

    1994WinG

    2Dsprite

    bitmapAPIAPI

    5 (KOEI)

    1995DirectX 1.0

    1995WinGGDI

    1996DirectX 2.0

    DOSWindows

    1994DirectX 3.0, 1997DirectX 5.0,

    1998DirectX 6.0, 1999DirectX 7.0

  • 8/10/2019 DirectX Tutorial 01

    5/47

    DirectX(2/2)

    2000DirectX 8.0

    AssemblerShader

    200212DirectX 9.0,

    HLSL

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

    200611Directx 10.0 Geometry shader

    10.1 (2008.2, 2009.4)

    200910DirectX 11.0 Hull shader, Domain shader

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

  • 8/10/2019 DirectX Tutorial 01

    6/47

    DirectX

    object

    object

    E.g. Direct3D objectDirect3DDevice object

    3D graphics

    2D graphics

    Audio

    Game input

    Not Recommended

  • 8/10/2019 DirectX Tutorial 01

    7/47

    Direct3D

    Microsoft DirectXgraphics

    O/Sgraphics hardware

    Direct3D

    Immediate Mode

  • 8/10/2019 DirectX Tutorial 01

    8/47

    Direct3D Device

    Rendering state(rendering context)

    Transformation, lighting, rasterization

    2

    HAL device

    Reference (emulator)

    Direct3D

  • 8/10/2019 DirectX Tutorial 01

    9/47

    HLSL(High Level Shader Language)

  • 8/10/2019 DirectX Tutorial 01

    10/47

    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

  • 8/10/2019 DirectX Tutorial 01

    11/47

    Preprocessor

    #define

    #elif

    #else

    #endif

    #error

    #if

    #include

    #line

    #undef

  • 8/10/2019 DirectX Tutorial 01

    12/47

    Types (1/2)

    Basic types float

    Int

    bool

    double

    Half

    Structure and Array

    Texture.

    Texture arrayDirect3D11

    Structurearray element

    .

  • 8/10/2019 DirectX Tutorial 01

    13/47

    Types (2/2)

    Vectors and Matrices

    Type definition 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

    FloatVector.yyxz

    FloatMatrix._11_12 or FloatMatrix[1][1]

  • 8/10/2019 DirectX Tutorial 01

    14/47

    Variables

    Local/ global

    Global

    Uniform

    Can be set from the outside (CPU

    Const

    Cannot be modified by the shader program

    Static

    Not externally visible

    Can have initializers Can have semantics for function parameters

  • 8/10/2019 DirectX Tutorial 01

    15/47

    Texture & Sampler

    Texture

    Direct3DTexture9

    Sampler : filtering

    samplertexturemapping

    H/W shader version

    HLSL 3.016 Filtering, address modesampler

    SetSamplerState

  • 8/10/2019 DirectX Tutorial 01

    16/47

    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

  • 8/10/2019 DirectX Tutorial 01

    17/47

    Statement Syntax

    { [statements] }

    [expression] ;

    return [expression] ;

    if ( expression ) statement [else statement]

    for ( [expression | variable_declaration] ;

    [expression] ; [expression] ) statement

  • 8/10/2019 DirectX Tutorial 01

    18/47

    Some Intrinsic functions

    Function Syntax Explanation

    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

  • 8/10/2019 DirectX Tutorial 01

    19/47

    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

  • 8/10/2019 DirectX Tutorial 01

    20/47

    Functions (cont.)

    In the same effect only- Static (not externally

    accessible)

    Parameters can be marked const

    Parameters can have default initializers

  • 8/10/2019 DirectX Tutorial 01

    21/47

    Differences from C

    No pointers

    No recursion

    Limitation of loop count and nesting

  • 8/10/2019 DirectX Tutorial 01

    22/47

    HLSL Summary

    Ease of Use

    Enable software developers

    Consistency of Implementation

    Enable multiple vendors

    Management of Evolution

    Enable multiple generations

  • 8/10/2019 DirectX Tutorial 01

    23/47

    Shader Model

    HLSLversion

    Under 1_1

    ()

    DirectX SDK

    2_a

    (unrolling)

    3_0, 4_0, 5_0

    Dynamic branching (jump)

    dynamic1

    256(loop counter register, 8bit)

  • 8/10/2019 DirectX Tutorial 01

    24/47

    Tutorial 01. Fixed pipeline version

  • 8/10/2019 DirectX Tutorial 01

    25/47

    Tutorial 01: CreateDevice

    windows frameworkdirect3d

    Tutorial 01

    Direct3D

    Scene rendering

    Direct3D

    Direct3D

    1) application window

    2) Direct3D object

    3) Direct3DDevice object

    4)objects

    5) Rendering

  • 8/10/2019 DirectX Tutorial 01

    26/47

    Windows

    RegisterClassEx()

    CreateWindow()

    ShowWindow(), UpdateWindow()

    Message Loop

    GetMessage()

    TranslateMessage()

    DispatchMessage()

    Message loop

  • 8/10/2019 DirectX Tutorial 01

    27/47

    Windows Message Loop

    D3D

    RegisterClassEx()

    CreateWindow()

    InitD3D()Direct3D.

    ShowWindow(), UpdateWindow()

    Message Loop

    GetMessage()

    TranslateMessage()

    DispatchMessage()

    Render()Direct3D Message loopDirect3D

  • 8/10/2019 DirectX Tutorial 01

    28/47

    Step 1 - Creating a Window

    WinMainwindows application framework

    Message

  • 8/10/2019 DirectX Tutorial 01

    29/47

    Step 2 - Initializing Direct3D (1/3)

    WindowDirect3DDirect3DDevice

    D3D_SDK_VERSION :SDKversion

    ,

    // full screen

    // double buffering

    // windows

  • 8/10/2019 DirectX Tutorial 01

    30/47

    Step 2 - Initializing Direct3D (2/3)

    WindowDirect3DDirect3DDevice

    D3D_SDK_VERSION :SDKversion

    ,

    WindowDirect3DDirect3DDevice

    D3D_SDK_VERSION :SDKversion

    ,

    // full screen

    // double buffering

    // windows

  • 8/10/2019 DirectX Tutorial 01

    31/47

    Back frame buffer

    Double buffering (D3DSWAPEFFECT_DISCARD )

    Depth/ stencil

    buffer

    Screen

    Graphic H/w

    Buffers for Rendering

    Color buffer, depth buffer, stencil buffer

    Color buffer :

    Depth/ stencil buffer : color buffermasking

    , color buffer

    1:1.

    Front frame buffer

  • 8/10/2019 DirectX Tutorial 01

    32/47

    Front frame buffer

    Double buffering (D3DSWAPEFFECT_DISCARD )

    Depth/ stencil

    buffer

    Screen

    Graphic H/w

    Buffers for Rendering

    Color buffer, depth buffer, stencil buffer

    Color buffer :

    Depth/ stencil buffer : color buffermasking

    , color buffer

    1:1.

    Back frame buffer

  • 8/10/2019 DirectX Tutorial 01

    33/47

    Step 2 - Initializing Direct3D (3/3)

    Direct3DDevice

  • 8/10/2019 DirectX Tutorial 01

    34/47

    CreateDevice()

    [in] UINTAdapter:

    D3DADAPTER_DEFAULT :adapter

    [in] D3DDEVTYPEDeviceType:

    ,

    D3DDEVTYPE_HAL: H/W

    rendering [in] HWNDhFocusWindow (hWnd)

    [in] DWORDBehaviorFlags

    Deviceflag

    D3DCREATE_SOFTWARE_VERTEXPROCESSING

    D3DCREATE_HARDWARE_VERTEXPROCESSING

    [in, out] D3DPRESENT_PARAMETERS pPresentationParameters: &d3dpp [out, retval] IDirect3DDevice9ppReturnedDeviceInterface : &g_pd3dDevice

    Device

  • 8/10/2019 DirectX Tutorial 01

    35/47

    Step 3 - Handling System

    Messages

    Windows message queue

    rendering

    Message infinite loop

    - message(get),

    -(translate),

    - message(dispatch).

  • 8/10/2019 DirectX Tutorial 01

    36/47

    Step 3 - Handling System

    Messages

    render()rendering

  • 8/10/2019 DirectX Tutorial 01

    37/47

    Step 4 - Rendering and Displaying aScene

    Back buffer, front buffer-(present)

    - OpenGLglFlush(), gluSwapChainBuffers()

    Render target, depth stencil buffer- OpenglglClear()

    Back bufferrendering

    OpenGLglBegin(),

    glEnd()

  • 8/10/2019 DirectX Tutorial 01

    38/47

    Clear() & Present()

    Clear() : render target, depth/ stencil buffer

    ,0, NULL

    Render targetrectangles

    RectangleD3DRECT(long x1, y1, x2, y2 )

    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 targetrender target.

    Present() : back buffer, front buffer NULL()

  • 8/10/2019 DirectX Tutorial 01

    39/47

    Step 5 - Shutting Down

    WM_DESTROYDirect3Dobjects

  • 8/10/2019 DirectX Tutorial 01

    40/47

    Tutorial 01. Programable pipeline

    (shader ) Version

  • 8/10/2019 DirectX Tutorial 01

    41/47

    Shader version of Tutorial01

    Fixed pipeline version

    Effect file (shader file, .fx file) loading

    Technique setting

    Pass setting

  • 8/10/2019 DirectX Tutorial 01

    42/47

    Load a Effect file (.fx)

    (CreateEffectFromFile())

  • 8/10/2019 DirectX Tutorial 01

    43/47

    Load a Effect file (.fx)

    Shader (.fx file) loading (LPD3DXEFFECT interface)

    D3DXCreateEffectFromFile() : 4parameters- LPDIRECT3DDEVICE9 pDevice

    - LPCTSTR pSrcFile

    - DWORD Flags

    - LPD3DXEFFECT *ppEffect)

  • 8/10/2019 DirectX Tutorial 01

    44/47

    Setting A Technique and A Pass :

    render()

  • 8/10/2019 DirectX Tutorial 01

    45/47

    Shader Codes (basic_shader.fx)

  • 8/10/2019 DirectX Tutorial 01

    46/47

    Vertex

    Processing

    Rasterizer

    FragmentProcessing

    Output Merging

    Stream

    Pixels

    TechniqueTechnique_Basic{

    passpass0{

    VertexShader= compilevs_3_0VS_Basic();PixelShader = compileps_3_0PS_Basic();

    }

    passpass1{

    VertexShader= compilevs_3_0VS_01();PixelShader = compileps_3_0VS_01();}

    }

    TechniqueTechnique02{

    passpass0{

    VertexShader= compilevs_3_0VS_02();PixelShader = compileps_3_0PS_02();

    }passpass1{ }

    }

    TechniqueTechnique03{

    passpass0{ }passpass1{ }

    }TechniqueTechnique03 { }

    struct VS_Input

    {float3 Pos : POSITION;

    float4 Color : COLOR0;};

    struct PS_Input

    {float4 Pos : POSITION;

    float4 Color : COLOR0;

    };

    PS_Input VS_Basic(VS_Input In){

    PS_Input Out = (PS_Input);

    returnOut;

    }

    float4 PS_Basic(PS_Input In) : COLOR0

    {returnIn.Color;

    }

    No Input

    Effect file

    Effect fileload,techniquetechniquepass

    (set)rendering pass

    Effect

    D3DXCreateEffectFromFile()

    Technique_Basic

    SetTechnique("Technique_Basic)

    pass0

    Begin(&Passes, 0)

    BeginPass(0)

    VS_Basic()

    PS_Basic()

    0 1 15

    No Drawing

  • 8/10/2019 DirectX Tutorial 01

    47/47

    Tomorrow

    Tutorial02

    - Fixed pipeline version

    - Programmable pipeline (shader) version