Computer Graphics Using Direct 3D Getting D3D to Work
Outline • • • • •
Review on D3D Pipeline D3D H/W Interface (D3D Devices) COM Objects Direct3D Initialization Some Issues – Swap Chain – Depth Buffer – Multisampling
2
• • • • •
Outline
Review on D3D Pipeline D3D H/W Interface (D3D Devices) COM Objects Direct3D Initialization Some Issues – Swap Chain – Depth Buffer – Multisampling
3
Review on D3D Pipeline
4
D3D Devices
Image from [MSDN]
5
D3D Devices • D3D executes graphics operations via a device. • A HAL (Hardware Abstraction Layer) device is a set of device-specific code that instructs the graphics hardware device to perform graphics operations needed by Direct3D. It is implemented by the manufacturer of the graphics hardware, making D3D device independent.
6
D3D Devices (Cont.) • A device may not implement all features exposed by Direct3D in the HAL. • You must check the device capabilities for graphics features before using them.
7
D3D Devices (Cont.) • The REF device is a special device provided by Direct3D that emulates graphics operations in software. It can be used for testing and debugging purposes but is not appropriate for ordinary usage.
8
Component Object Model • Direct3D follows Component Object Model (COM) • All classes (called interfaces) are derived from IUknown • 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
9
• • • • •
Outline
Review on D3D Pipeline D3D H/W Interface (D3D Devices) COM Objects Direct3D Initialization Some Issues – Swap Chain – Depth Buffer – Multisampling
10
D3D Initialization • D3D Initialization Steps – – – –
Obtaining an IDirect3D9 interface Checking the hardware capabilities Filling out the presentation parameters Creating the device
11
D3D Initialization (Cont.) • Step 1 : Obtaining Idirect3D9 IDirect3D9* _d3d9; _d3d9 = Direct3DCreate9( D3D_SDK_VERSION);
12
D3D Initialization (Cont.) • Step 2 : Checking the HW Capabilities D3DCAPS9 caps; d3d9>GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps); //Example: Transform & Lighting if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT ) { //HW Vertex Processing is Supported }
13
D3D Initialization (Cont.) • Step 3 : Initializing Presentation Parameters 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 14
D3D Initialization (Cont.) • Step 3 : Initializing Presentation Parameters d3dpp.EnableAutoDepthStencil = true; d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; d3dpp.Flags = 0; d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
15
D3D Initialization (Cont.) • Step 4 : Creating Device IDirect3DDevice9* device = 0; hr = d3d9>CreateDevice( D3DADAPTER_DEFAULT, // primary adapter D3DDEVTYPE_HAL, // device type hwnd, // window associated with device D3DCREATE_HARDWARE_VERTEXPROCESSING, // vertex processing type &d3dpp, // present parameters &device); // returned created device if( FAILED(hr) ) { //Handle hailure here return 0; }
16
• • • • •
Outline
Review on D3D Pipeline D3D H/W Interface (D3D Devices) COM Objects Direct3D Initialization Some Issues – Swap Chain – Depth Buffer – Multisampling
17
Swap Chain • To provide smooth (flicker-free) animation between frames, Direct3D maintains multiple rendering surfaces (2 or 3)
18
Swap Chain (Cont.) • A back buffer is presented by being promoted as a front buffer.
19
Depth Buffer • It is a buffer of the same size of the image (one entry per pixel). • Contains the depth value corresponding each rendered pixel. • Used to test whether the pixel is hidden by another object (depthtest). 20
Multisampling • One of the problems in graphics is the aliasing artifact, which results in the appearance of jagged edges that should be straight.
21
Multisampling (Cont.) • Multisampling is a technique for full-screen antialiasing (FSAA). • Conceptually, it is achieved by rendering the scene to a higher resolution image and then sampling each pixel in the final image from the corresponding pixels in the higher resolution image.
22
Multisampling (Cont.) • Direct3D9 supports: – D3DMULTISAMPLE_NONE – D3DMULTISAMPLE_1_SAMPLE … D3DMULTISAMPLE_16_SAMPLE
• Notice that multisampling provides better images but slows down performance. Don't forget to check device • caps
23
Summary • Direct3D uses a device to access HW. • Direct3D provides HAL device and REF device (for SW emulation). • Direct3D follows COM model.
24
Summary (Cont.) • To initialize D3D we – – – –
Obtain IDirect3D9 interface Check HW capabilities Fill presentation parameters Create device
25
Summary (Cont.) • A swap chain of multiple buffers is used for smooth animation. • A depth buffer is used to test for occlusion. • Multisampling is used for full-screen anti-aliasing to provide smooth edges.
26
Any Questions ??
27
Surfaces (Optional Reading) • A surface is a matrix of pixels used to store 2D image data.
28
Surfaces (Cont.) • A surface is described by the IDirect3DSurface9 interface. Important methods are: – LockRect : returns a D3DLOCKED_RECT pointer to the surface memory. – UnlockRect : Unlocks the surface (Must be called when done with it). – GetDesc : Retrieves a description of the surface by filling out a D3DSURFACE_DESC structure. 29
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 (in pixels). – Use pointer arithmetic to access pixels. – Call UnlockRect to unlock surface memory.
30