180 likes | 211 Views
Computer Graphics Introducing DirectX. CO2409 Computer Graphics Week 5-2. Today’s Lecture. Terminology A History DirectX Components DirectX 10,11 and 12 Overview of the Direct3D Pipeline Programming Direct3D – First Steps. Terminology. DirectX is an SDK Software Development Kit
E N D
Computer GraphicsIntroducing DirectX CO2409 Computer Graphics Week 5-2
Today’s Lecture • Terminology • A History • DirectX Components • DirectX 10,11 and 12 • Overview of the Direct3D Pipeline • Programming Direct3D – First Steps
Terminology • DirectX is an SDK • Software Development Kit • Set of software tools, APIs and documentation to enable development of specialist software • DirectX contains a set of APIs • Application Program Interface • An API is a definition of how to interface with (i.e. use) some other software or device • DirectX interfaces with the hardware drivers • These APIs take the form of libraries of functions and types that can be used in C++, C# etc. • [Gamers: The TL-Engine is also an API]
DirectX – A History • Conceived in 1994 • Intended to provide low-level access to system resources within a high-level OS • First few iterations were fairly unusable • However, by DirectX 5 provided a reasonable development platform • Later versions have been developed in close collaboration with hardware vendors • Windows 7 supports up to DirectX 11 • Windows 10 only can use DirectX 12 • DirectX is now part of the core Windows SDK rather than a stand-alone download • Although we still use the stand-alone download to help in file loading later in the module
DirectX Components • DirectX contains many components • We are mainly interested in Direct3D: • 2D and 3D graphics • Low level control for performance • Higher level “helper” classes and functions for ease of use • DirectX also contains: • DirectInput • For keyboard, mouse, joystick & other input devices. • Replaced in part by the XInput API (Xbox related) • GDI+, for drawing high quality text • A large number of deprecated components…
Older Components • DirectX is generally backwards compatible with previous versions • So a DX9 program will work in DX11 for example • Many out-of-date components are still usable: • DirectSound – Replaced by XAudio2 or XACT • DirectDraw – 2D drawing, superseded • DirectPlay – Network gaming, deprecated • DirectShow – Video playback, superseded • DirectMusic – Music playback, deprecated • These components should be avoided • Ensure you read the latest documentation • Avoid tutorials for older versions of DirectX
DirectX Graphics • The DirectX SDK provides: • An API: a set of functions and classes • We will use this extensively (in C++) • Full documentation, tutorials and samples • Very useful reference • Development Tools, mainly regarding textures, and pixel, vertex & geometry shaders • We will see a little of these • To use the SDK you should • Get the last standalone version (June 2010) for legacy support • Ensure your windows is up to date for the current SDK • Optionally get the Windows SDK for the other tools / support • CM142 is ready for DirectX development
DirectX 10 / 11 • DirectX 10 provides a solid and robust graphics framework even for the most modern applications • Key points: • Windows itself is driven by DirectX • DirectX10 and above requires a minimum specification for hardware so programs are portable across PCs • DirectX 11 is strict superset of DirectX 10 • DX10 code works with few changes in DX11 • Mainly improved multi-core / multi-threading support • Only a few changes regarding graphics (primarily tessellation) • We will use Direct3D 11 in this module
DirectX 12 • DirectX 12 was introduced to address the needs of the most demanding games and graphics applications • In DirectX 11 and earlier the graphics driver made decisions on behalf of the programmer • For example whether to put a resource in GPU memory or in CPU memory • But this reduces the opportunities for optimisations • DirectX 12 requires the programmer to make all low level decisions regarding memory and multi-threading • Highly complex API for experts only • Note that Vulkan, the successor to OpenGL is similar to DX12 in this regard • So we won’t look at DirectX 12 in this module
DirectX Graphics Pipeline • The operation of D3D can be illustrated as a ‘pipeline’ of operations • From input 3D geometry to final pixel rendering • This diagram is for DX10 • We will look at most stages over the next few weeks • Roughly in order Reproduced from the DirectX SDK documentation
Pipeline: Input Stage • We first send 3D geometry to the input-assembler • The 3D elements in our scene • Artistic, technical or abstract, whatever suits our application • In any case the input is 3D meshes: vertices / polygons • 2D elements can be rendered by using flat 3D geometry • Usual to create/load geometry in CPU memory, then send to DX This 3D geometry is in the form of “triangle strips”, a method of storing geometry using less memory
Pipeline: Shaders Intro • Most stages of the pipeline operate automatically with only some limited setup: • We set states that determine how that stage will work • E.g. We can set states to perform additive or multiplicative blending in the final output stage • However, 3 pipeline stages are programmable • The vertex, geometry and pixel stages • We write programs to make these parts operate • These programs are called shaders • Written in a new language – we will use HLSL • Shaders gives immense flexibility in exactly how the pipeline will operate
Pipeline: Vertex Shader • The vertex shader stage is primarily responsible for: • Positioning the geometry in the overall 3D scene • Transforming 3D geometry into 2D geometry ready to render • Other geometry processing • E.g. animation, deformation Transformation from 3D to 2D geometry • Will need to introduce some maths concepts • Matrices and transformations • Plus the idea of a camera and 2D projection • Will revisit the idea of different coordinate systems • E.g. World space, camera space
Geometry Shader & Rasterisation • The vertex shader just described works on vertices one-by-one • Next the geometry shader works on entire triangles in a mesh • Used for special purposes • Will not see this stage in this module • The rasterization stage processes each finalized triangle • Determine all the pixels that are inside • Calls the pixel shader for each • Automated stage (no shader) Rasterization stage scans the 2D triangle geometry to determine all the pixels within The Pixel Shader stage is called for each one
Pixel Shader Stage • The pixel shader stage works on every pixel in our scene • This shader program needs to be efficient – millions of pixels • The pixel shader simply determines the colour required for each pixel • This is a simple concept but involves many techniques: • Textures, lighting, normal mapping, environment mapping, special effects (like cell shading), etc. Pixel shader effect to give the impression of depth to a surface that is actually flat
Pipeline: Lighting • We can calculate lighting in one of the shader stages • Usually the pixel shader • Three main kinds of lights • Directional lights • Point lights • Spot lights • The effects are calculated with some simple vector mathematics • Note that shadows are dealt with separately from lights • Shadows are much more complex and require processing at several stages Light Types
Output Stage • The final stage outputs pixels onto the viewport • However, we may wish to blend the new pixel colours with those already on the viewport • To create transparency effects • Same effects we saw when working with sprites • E.g. Additive, multiplicative, alpha blending • So this stage is actually called the output-merger stage • This summary has covered all the pipeline stages • Except the Stream-Output stage, which we won’t use (it allows the graphics pipeline to update the geometry it is working on) • The overall process is similar for other graphics APIs
Direct3D Programming – First Steps • First create a bare minimum Windows application • Need a window to render into (even for a full screen app) • Initialise DirectX and point it at our window • This stage is fairly standard for all DX applications • Prepare some 3D geometry to render • Type in something simple (e.g. a single triangle or a cube) • Or load geometry from a file (involves parsing) • Send the geometry to DirectX • Set the minimum states to initialise the pipeline stages • Then render: a single call will now trigger the pipeline and process / draw our geometry