Section 3 - Getting D3D to Work

download Section 3 - Getting D3D to Work

of 30

Transcript of Section 3 - Getting D3D to Work

  • 8/14/2019 Section 3 - Getting D3D to Work

    1/30

    Computer GraphicsComputer Graphics

    Using Direct 3DUsing Direct 3D

    Getting D3D to WorkGetting D3D to Work

  • 8/14/2019 Section 3 - Getting D3D to Work

    2/30

    2

    Outline Review on D3D Pipeline

    D3D H/W Interface (D3D Devices)

    COM Objects Direct3D Initialization

    Some Issues Swap Chain

    Depth Buffer

    Multisampling

  • 8/14/2019 Section 3 - Getting D3D to Work

    3/30

    3

    Outline Review on D3D Pipeline D3D H/W Interface (D3D Devices)

    COM Objects Direct3D Initialization

    Some Issues

    Swap Chain Depth Buffer

    Multisampling

  • 8/14/2019 Section 3 - Getting D3D to Work

    4/30

    4

    Review on D3D Pipeline

  • 8/14/2019 Section 3 - Getting D3D to Work

    5/30

    5

    D3D Devices

    Image from [MSDN]

  • 8/14/2019 Section 3 - Getting D3D to Work

    6/30

    6

    D3D Devices

    D3D executes graphics operations via a device.

    A HAL(Hardware Abstraction Layer) deviceisa set of device-specific code that instructs thegraphics hardware device to perform graphicsoperations needed by Direct3D. It isimplemented by the manufacturer of thegraphics hardware, making D3D device

    independent.

  • 8/14/2019 Section 3 - Getting D3D to Work

    7/30

    7

    D3D Devices (Cont.)

    A device may not implement allfeatures exposed by Direct3D in the

    HAL. You must check the device capabilities

    for graphics features before using

    them.

  • 8/14/2019 Section 3 - Getting D3D to Work

    8/30

    8

    D3D Devices (Cont.)

    The REFdevice is a special device provided byDirect3D that emulates graphics operations insoftware. It can be used for testing and

    debugging purposes but is not appropriate forordinary usage.

  • 8/14/2019 Section 3 - Getting D3D to Work

    9/30

    9

    Component Object Model

    Direct3D follows Component Object Model (COM)

    All classes (called interfaces) are derived fromIUknown

    COM objects perform their own memory management To create an object, use a create function (not new).

    When done, use IUknown::Release (not delete)

    All interfaces have their names prefixed with I

  • 8/14/2019 Section 3 - Getting D3D to Work

    10/30

    10

    Outline Review on D3D Pipeline D3D H/W Interface (D3D Devices)

    COM Objects Direct3D Initialization

    Some Issues

    Swap Chain Depth Buffer

    Multisampling

  • 8/14/2019 Section 3 - Getting D3D to Work

    11/30

    11

    D3D Initialization

    D3D Initialization Steps Obtaining an IDirect3D9 interface

    Checking the hardware capabilities Filling out the presentation parameters Creating the device

  • 8/14/2019 Section 3 - Getting D3D to Work

    12/30

    12

    D3D Initialization (Cont.)

    Step 1 : Obtaining Idirect3D9

    IDirect3D9* _d3d9; _d3d9 = Direct3DCreate9(

    D3D_SDK_VERSION);

  • 8/14/2019 Section 3 - Getting D3D to Work

    13/30

    13

    D3D Initialization (Cont.)

    Step 2 : Checking the HW CapabilitiesD3DCAPS9 caps;

    d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT,

    D3DDEVTYPE_HAL, &caps);

    //Example: Transform & Lighting

    if(caps.DevCaps &D3DDEVCAPS_HWTRANSFORMANDLIGHT )

    { //HW Vertex Processing is Supported }

  • 8/14/2019 Section 3 - Getting D3D to Work

    14/30

    14

    D3D Initialization (Cont.)

    Step 3 : Initializing PresentationParameters

    D3DPRESENT_PARAMETERS d3dpp;

    d3dpp.BackBufferWidth=800; d3dpp.BackBufferHeight=600;

    d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; //pixel format

    d3dpp.BackBufferCount = 1;

    d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;

    d3dpp.MultiSampleQuality = 0;

    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

    d3dpp.hDeviceWindow = hwnd;

    d3dpp.Windowed = false; // fullscreen

  • 8/14/2019 Section 3 - Getting D3D to Work

    15/30

    15

    D3D Initialization (Cont.)

    Step 3 : Initializing PresentationParameters

    d3dpp.EnableAutoDepthStencil = true;

    d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;

    d3dpp.Flags = 0;

    d3dpp.FullScreen_RefreshRateInHz =D3DPRESENT_RATE_DEFAULT;

    d3dpp.PresentationInterval =D3DPRESENT_INTERVAL_IMMEDIATE;

  • 8/14/2019 Section 3 - Getting D3D to Work

    16/30

    16

    D3D Initialization (Cont.)

    Step 4 : Creating DeviceIDirect3DDevice9* device = 0;

    hr = d3d9->CreateDevice(

    D3DADAPTER_DEFAULT, // primary adapter

    D3DDEVTYPE_HAL, // device type

    hwnd, // window associated with device

    D3DCREATE_HARDWARE_VERTEXPROCESSING, // vertexprocessing type

    &d3dpp, // present parameters

    &device); // returned created device

    if( FAILED(hr) )

    { //Handle hailure here

    return 0;

    }

  • 8/14/2019 Section 3 - Getting D3D to Work

    17/30

  • 8/14/2019 Section 3 - Getting D3D to Work

    18/30

    18

    Swap Chain

    To provide smooth (flicker-free)animation between frames, Direct3D

    maintains multiple rendering surfaces(2 or 3)

  • 8/14/2019 Section 3 - Getting D3D to Work

    19/30

    19

    Swap Chain (Cont.)

    A back buffer ispresentedby

    being promotedas a frontbuffer.

  • 8/14/2019 Section 3 - Getting D3D to Work

    20/30

    20

    Depth Buffer

    It is a buffer of the same size of theimage (one entry per pixel).

    Contains the depth valuecorresponding each rendered pixel.

    Used to test whether the pixel is

    hidden by another object (depth-test).

  • 8/14/2019 Section 3 - Getting D3D to Work

    21/30

    21

    Multisampling

    One of the problems in graphics is thealiasing artifact, which results in the

    appearance of jagged edges thatshould be straight.

  • 8/14/2019 Section 3 - Getting D3D to Work

    22/30

    22

    Multisampling (Cont.)

    Multisampling is a technique for full-screen anti-aliasing (FSAA).

    Conceptually, it is achieved by rendering the scene

    to a higher resolution image and then sampling eachpixel in the final image from the correspondingpixels in the higher resolution image.

  • 8/14/2019 Section 3 - Getting D3D to Work

    23/30

  • 8/14/2019 Section 3 - Getting D3D to Work

    24/30

    24

    Summary

    Direct3D uses a device to access HW.

    Direct3D provides HAL device and

    REF device (for SW emulation). Direct3D follows COM model.

  • 8/14/2019 Section 3 - Getting D3D to Work

    25/30

    25

    Summary (Cont.)

    To initialize D3D we Obtain IDirect3D9 interface

    Check HW capabilities Fill presentation parameters

    Create device

  • 8/14/2019 Section 3 - Getting D3D to Work

    26/30

    26

    Summary (Cont.)

    A swap chain of multiple buffers isused for smooth animation.

    A depth buffer is used to test forocclusion.

    Multisampling is used for full-screen

    anti-aliasing to provide smooth edges.

  • 8/14/2019 Section 3 - Getting D3D to Work

    27/30

    27

    Any Questions ??

  • 8/14/2019 Section 3 - Getting D3D to Work

    28/30

    28

    Surfaces (Optional

    Reading) A surface is a matrix of pixels used tostore 2D image data.

  • 8/14/2019 Section 3 - Getting D3D to Work

    29/30

  • 8/14/2019 Section 3 - Getting D3D to Work

    30/30

    30

    Surfaces (Cont.)

    To process a surface Use LockRect to obtain a D3DLOCKED_RECT pointer.

    It allows you to access the pixel array and get the

    pitch (in bytes). Use GetDesc to get surface description. It allows

    you to get information such as width and height (inpixels).

    Use pointer arithmetic to access pixels. Call UnlockRect to unlock surface memory.