Direct3D

From Wikipedia, the free encyclopedia

Direct3D is part of Microsoft's DirectX API. Direct3D is only available for Microsoft's various Windows operating systems (Windows 95 and above) and is the base for the graphics API on the Xbox and Xbox 360 console systems. Direct3D is used to render three dimensional graphics in applications where performance is important, such as games. Direct3D also allows applications to run fullscreen instead of embedded in a window, though they can still run in a window if programmed for that feature. Direct3D uses hardware acceleration if it is available on the graphic board.

Direct3D is a 3D API. That is, it contains many commands for 3D rendering, but contains few commands for rendering 2D graphics.[1] Microsoft strives to continually update Direct3D to support the latest technology available on 3D graphics cards. Direct3D offers full vertex software emulation but no pixel software emulation for features not available in hardware. For example, if a program programmed using Direct3D requires pixel shaders and the video card on the user's computer does not support that feature, Direct3D will not emulate it. The API does define a Reference Rasterizer (or REF device), which emulates a generic graphics card, although it's too slow to be used in any application to emulate pixel shaders and is usually ignored.

Direct3D's main competitor is OpenGL. There are numerous features and issues that proponents for either API disagree over, see comparison of Direct3D and OpenGL for a summary.

Contents

[edit] Architecture

Abstract Layer
Abstract Layer

Direct3D is a Window’s DirectX API subsystem component. The aim of Direct3D is to abstract the communication between a graphics application and the graphics hardware drivers. It is presented like a thin abstract layer at a level comparable to GDI (see attached diagram). With a COM based architecture, the main difference between GDI and Direct3D is that Direct3D is directly connected to display drivers and gets better results at rendering than GDI.

Direct3D is formed by two big APIs. Retained Mode and Immediate Mode. Immediate mode provides an interface to every video card 3D functions (lighting, materials, clipping, transformations, textures, depth buffering...). Retained mode is built over the previous one and provides higher level graphics techniques such as hierarchy and animation. Immediate mode is preferred by video game developers because it gives them more freedom to use graphics techniques.

Direct3D immediate mode presents three main abstractions: devices, resources and swap chains (see attached diagram). Devices are responsible for rendering the 3D scene. They provide an interface with different options of renderization. For example, mono device provides white and black renderization and the RGB device uses colours to render. There are three types of devices:

  • HAL (hardware abstract layer) device: if it supports hardware acceleration, the Direct3D code can run at hardware speeds.
Device
Device
  • Reference device: it is necessary to install previously the Direct3D SDK to use this device type, as this allows it to simulate those new features that aren’t yet supported by the graphics hardware.
  • Null reference device: it does nothing but returning a black screen. This device is used when the SDK is not installed and a reference device is requested.
  • Pluggable software device: it is used to perform software rasterization. Previously, it is needed to provide the device through the RegisterSoftwareDevice method. This device type wasn’t used until DirectX 9.0[2].

Every device contains at least one swap chain. A swap chain is made up of one or more back buffer surfaces (rectangular collection of pixel data and its attribures such as colour, depth/stencil, alpha or texture). In the back buffer is where the render will occur.

Moreover, devices contain a collection of resources too. Resources are specific data used during rendering. Each resource has four attributes:

  • Type: it describes what kind of resource is: surface, volume, texture, cube texture, volume texture, surface texture, index buffer or vertex buffer.
  • Pool[3]: it describes how the resource is managed by runtime and where is stored. Default pool means that the resource will exist only in device memory; managed pool means that the resource will be stored in system memory and will be send to the device when required; system memory pool means that the resource will only exist in system memory, and scratch pool means the same as system memory pool, but, in this case, resources are not bound by hardware restrictions.
  • Format: it describes the layout of the resource’s data in memory, mainly pixel data. For example, D3DFMT_R8G8B8 format value means a 24 bits colour depth (8 bits for red, 8 bits for green and 8 bits for blue).
  • Usage: it describes, with a collection of flag bits, how the resource will be used by the application. These flags are used to know which resources are used in dynamic or in static access pattern. Static resource values don’t change after being loaded, whereas dynamic resource values are repeatedly modified.

[edit] Pipeline

Graphics pipeline proccess
Graphics pipeline proccess

The different stages of the Direct3D 10 pipeline[4] are[5]:

  1. Input Assembler: Supplies the data to the pipeline.
  2. Vertex Shader: Performs single vertex operations such as transformations, skinning or lighting.
  3. Geometry Shader: Processes entire primitives (triangles, lines or vertex) and, sometimes, their edge-adjacent primitives. Given a primitive, this stage discards it, or generates one or more new primitives.
  4. Stream Output: Stores on memory previous stage results. It is useful to recirculate data back into the pipeline.
  5. Rasterizer: Rasterizes primitives into pixels, clipping what is not visible.
  6. Pixel Shader: Pixel operations such as colour.
  7. Output Merger: Merges various types of output data (pixel shader values, depth/stencil...) to build the final result.


It is possible to configure all the pipeline stages, making it very flexible and adaptable.

[edit] Example

How to draw a triangle in Direct3D:

/* A 3-vertex polygon definition */
D3DLVERTEX v[3];
/* Vertex established */
v[0]=D3DLVERTEX( D3DVECTOR(0.f, 5.f, 10.f), 0x00FF0000, 0, 0, 0 );
/* Vertex established */
v[1]=D3DLVERTEX( D3DVECTOR(0.f, 5.f, 10.f), 0x0000FF00, 0, 0, 0 );
/* Vertex established */
v[2]=D3DLVERTEX( D3DVECTOR(0.f, 5.f, 10.f), 0x000000FF, 0, 0, 0 );
/* Function call to draw the triangle */
pDevice->DrawPrimitive( D3DPT_TRIANGLELIST, D3DFVF_LVERTEX, v, 3, 0 );

[edit] Display modes

Direct3D allows two distinct display modes:

  • Exclusive mode: Due to the direct connection between Direct3D and the display driver, Direct3D device is able to hoard the screen. Any request to use the display device will fail while an application is in exclusive mode.
  • Windowed mode: The result is shown inside the area of a window. Direct3D needs to communicate with GDI to complete the representation.


Windowed mode is slower than exclusive one, but is more useful in debug mode because it doesn’t hoard the screen.

[edit] Version history

In 1992, Servan Keondjian started a company named RenderMorphics, which developed a 3D graphics API named Reality Lab, which was used in medical imaging and CAD software. Two versions of this API were released. Microsoft bought RenderMorphics in February 1995, bringing Keondjian on board to implement a 3D graphics engine for Windows 95. This resulted in the first version of Direct3D that shipped in DirectX 2.0 and DirectX 3.0.

Direct3D initially implemented "retained mode" and "immediate mode" 3D APIs. The retained mode was a COM-based scene graph API that attained little adoption. Game developers clamored for more direct control of the hardware's activities than the Direct3D retained mode could provide. Only one game that sold a significant volume, Lego Island, was based on the Direct3D retained mode, so Microsoft did not update the retained mode after DirectX 3.0.

The first version of Direct3D immediate mode was based on an "execute buffer" programming model that Microsoft hoped hardware vendors would support directly. Execute buffers were intended to be allocated in hardware memory and parsed by the hardware in order to perform the 3D rendering. They were extremely awkward to program, however, hindering adoption of the new API and stimulating calls for Microsoft to adopt OpenGL as the official 3D rendering API for games as well as workstation applications. (see OpenGL vs. Direct3D)

Rather than adopt OpenGL as a gaming API, Microsoft chose to continue improving Direct3D, not only to be competitive with OpenGL, but to compete more effectively with proprietary APIs such as 3dfx's Glide. A team in Redmond took over development of the Direct3D immediate mode, while Servan's RenderMorphics team continued work on the retained mode.

Direct3D 5.0 introduced the DrawPrimitive API that eliminated the need for applications to construct execute buffers.

Direct3D 6.0 introduced numerous features to cover contemporary hardware (such as multitexture[6] and stencil buffers) as well as optimized geometry pipelines for x87, SSE and 3DNow! and optional texture management to simplify programming. Direct3D 6.0 also included support for features that had been licensed by Microsoft from specific hardware vendors for inclusion in the API, in exchange for the time-to-market advantage to the licensing vendor. S3 texture compression support was one such feature, renamed as DXTC for purposes of inclusion in the API. Another was TriTech's proprietary bump mapping technique. By including these features in DirectX, Microsoft virtually guaranteed that all PC graphics hardware vendors would support the feature at their earliest opportunity, driving industry standardization in a way that was inconceivable under the auspices of the OpenGL Architectural Review Board.

Direct3D 7.0 introduced the .dds texture format[7] and support for transform and lighting hardware acceleration (first available on PC hardware with NVIDIA's GeForce), as well as the ability to allocate vertex buffers in hardware memory. Hardware vertex buffers represent the first substantive improvement over OpenGL in DirectX history. Direct3D 7.0 also augmented DirectX support for multitexturing hardware, and represents the pinnacle of fixed-function multitexture pipeline features: although powerful, it was so complicated to program that a new programming model was needed to expose the shading capabilities of graphics hardware.

Direct3D 8.0 introduced programmability in the form of vertex and pixel shaders, enabling developers to write code without worrying about superfluous hardware state. They wrote simple shaders to do simple tasks or more complicated shaders to do more complex tasks, and the display driver compiled those shaders to instructions that could be understood by the hardware. Direct3D 8.0 and its programmable shading capabilities were the first major departure from an OpenGL-style fixed-function architecture, where drawing is controlled by a complicated state machine. Direct3D 8.0 also eliminated DirectDraw as a separate API[8]. Direct3D subsumed all remaining DirectDraw API calls still needed for application development, such as Present(), the function used to display rendering results.

Direct3D was not considered to be user friendly, but as of DirectX version 8.1, many usability problems were resolved. Direct3D 8 contained many powerful 3D graphics features, such as vertex shaders, pixel shaders, fog, bump mapping and texture mapping.

Direct3D 9.0 added a new version of the High Level Shader Language[9], support for high dynamic range lighting, multiple render targets, and vertex buffer indexing. An extension only available in Windows Vista, called Direct3D 9Ex (previously version 9.0L), allows the use of the advantages offered by Vista's Windows Display Driver Model and is used for Windows Aero.[10]

Direct3D 10.0, included with Windows Vista, is described in the next section. The DirextX 10 SDK is available since February 2007[11].

[edit] Direct3D 10

Windows Vista includes a major update to the Direct3D API. Originally called WGF 2.0 (Windows Graphics Foundation 2.0), DirectX 10 and DirectX Next, it features an updated shader model (version 4) — the shaders still consist of fixed stages like on previous APIs, but all stages sport a nearly unified interface, as well as a unified access to resources. The language itself has been extended to be more expressive (integer operations, nearly unlimited instructions count). In addition to the previously available vertex and pixel shader stages, the API includes a geometry shader stage that breaks the old model of one vertex in/one vertex out, to allow for more complex effects in real time. Direct3D 10 no longer uses "capability bits" to indicate which features are active on the current hardware. Instead, it defines a minimum standard of hardware capabilities which must be supported for a display system to be "Direct3D 10 compatible".

Therefore, contrary to the previous revisions of Direct3D, it requires new graphics hardware backed by WDDM-compliant drivers to run at all, whereas prior versions allow the old hardware capabilities to be addressed within the new interface. This is one of the major departure of this new API, and it is justified by Microsoft as the only way to achieve the CPU efficiency gains needed for the newest pieces of hardware without the clutter of legacy code.

Because Direct3D 10 hardware will be comparatively rare for a period of time after the release of Windows Vista, and because the Vista Premium logo program does not require Direct3D 10 to be supported, the first D3D10-compatible games will most likely still provide a Direct3D 9 render path.

[edit] New features

  • Fixed pipelines[12] are being done away with in favor of fully programmable pipelines (often referred to as unified pipeline architecture), which can be programmed to emulate the same.
  • Paging of graphics memory, to allow data to be loaded to Video RAM when needed and move it out when not needed. This enables usage of the system memory to hold graphics data, such as textures, thereby allowing use of more and higher resolution textures in games (this was possible with older DirectX APIs by using the GART).
  • There is no limit on the number of objects which can be rendered, provided enough resources are available.[13]
  • Virtualization of the graphics hardware, to allow multiple threads/processes to use it, in turns.
  • New state object to enable the GPU to change states efficiently.
  • Shader model 4.0, enhances the programmability of the graphics pipeline. It adds instructions for integer and bitwise calculations.
  • Geometry shaders, which work on individual triangles which form a mesh.
  • Texture arrays enable swapping of textures in GPU without CPU intervention.
  • Resource View enables pre-caching of resources, thereby reducing latency.
  • Predicated Rendering allows drawing calls to be ignored based on some other conditions. This enables rapid occlusion culling, which prevents objects from being rendered if it is not visible or too far to be visible.
  • Instancing 2.0 support, allowing multiple instances of similar meshes, such as armies, or grass or trees, to be rendered in a single draw cell, reducing the processing time needed for multiple similar objects to that of a single one.[14]

[edit] Related tools

DirectX comes with D3DX, a library of tools designed to perform common mathematical calculations and several more complicated tasks, such as compiling or assembling shaders used for 3D graphic programming. It also includes several classes that simplify the use of 3D-models and, for example, particle systems. D3DX is provided as a dynamic link library (DLL).

DXUT (also called the sample framework) is a layer built on top of the Direct3D API. The framework is designed to help the programmer spend less time with mundane tasks, such as creating a window, creating a device, processing Windows messages and handling device events.

[edit] See also

[edit] References

  1. ^ Microsoft DirectX SDK Readme (October 2006)
  2. ^ Software Rasterizer for DirectX 9.0 SDK.
  3. ^ Direct3D Resources - Memory pool.
  4. ^ Direct3D 9.0 pipeline diagram.
  5. ^ Direct3D 10 pipeline stages.
  6. ^ Direct3D 6.0 introduces multitextures.
  7. ^ Direct3D 7 introduces DirectDraw Surface (.dds) format.
  8. ^ Direct3D takes over DirectDraw.
  9. ^ HLSL in Direct3D 9.0.
  10. ^ Chuck Walbourn (August 2006). Graphics APIs in Windows Vista. MSDN. Retrieved on 2007-02-26.
  11. ^ DirectX 10 SDK available since February 2007.
  12. ^ CNet News
  13. ^ SDK March 2006
  14. ^ Direct3D 10 Additional Improvements.

[edit] External links