User Tools

Site Tools


notes:misc:direct3d_q_and_a

Direct3D Q & A

Reference book: “Practical Rendering and Computation with Direct3D 11” by Jason Zink, Matt Pettineo, Jack Hoxley

What is Direct3D?

Direct3D is a native API used to control the video hardware to create a rendered image.

Device and Device Context

What is the device?

The device represents the physical graphics card and a link to a single adapter.

What is the device used for?

The device is primarily used to create resources such as textures and shaders. It is also used to create shader program objects, state objects, and query objects as well as to check availability of hardware features, for diagnostics, and for debugging. The device encapsulates feature levels.

What is the device context used for?

The device context is used to manipulate the pipeline and resources and to bind the resources, shader objects, and state objects to the pipeline. Using the device context you can issue draw calls and state changes to the graphics hardware.

What are the two types of device context?

Immediate context and deferred context.

How do the device and the device context work together?

The device is about creating resources throughout the lifetime of the application, while the device context applies those resources and creates the images that are displayed to the user.

  • Device creates resources
  • Device context consumes resources

How is the immediate context used?

The immediate context's methods are immediately submitted by Direct3D for execution in the driver. Only a single immediate context is allowed.

What is the deferred context?

The deferred context provides a thread-safe mechanism for recording a series of commands from secondary threads other than the main rendering thread. It is used to produce a command list object which can be “played back” on the immediate context.

DXGI and Swap Chain

What is the swap chain?

A swap chain is a series of textures used to manage the contents of a window's client area. It is created by DXGI.

Does the swap chain have to be recreated when the display area is resized?

Yes

What is the DXGI framework used for?

The DirectX Graphics Infrastructure (DXGI) framework:

  • manipulates the hardware resources
  • provides the “adapter” interface for each video card
  • provides the swap chain
  • manages the Desktop Window Manager (DWM) which handles compositing multiple Direct3D applications together to allow multiple windows to share the same screen; Windows Display Driver Model (WDDM) provides the functionality required to render the desktop and applications using DWM. It virtualizes video resources and access to GPU.

Pipeline

What are the two pipelines provided by Direct3D?

The rendering pipeline and the compute pipeline.

List the stages of the rendering pipeline.

  • IA - Input Assembler
  • VS - Vertex Shader
  • HS - Hull Shader
  • Tessellator
  • DS - Domain Shader
  • GS - Geometry Shader
  • Rasterizer
  • PS - Pixel Shader
  • OM - Output Merger

What is a fragment?

A fragment is a group of data that corresponds to a pixel in a rendered target.

What stages operate on the geometric level?

IA, VS, Tessellation Stages (HS, Tessellator, DS), and GS.

What stages operate on the fragment level?

Rasterizer, PS, and OM.

What is the compute pipeline and what is it used for?

The compute pipeline is a single-stage (a compute shader stage) pipeline devoted to general purpose computations. It provides the following functionality:

  • structured threading model - allows for implementation of highly parallel algorithms
  • group shared memory block - a memory block accessible to all threads within a thread group; this allows for communication between threads
  • random read/write access to resources

Resources

What are Direct3D resources?

Resources are blocks of memory that are made available for the GPU to use and manipulate. Resources must be bound to the pipeline in order to be used. Both video memory and system memory can hold resources, and resources can be copied to and from each type of memory by the Direct3D runtime.

What are the two categories of resources?

Resources may be divided into two groups:

  • Textures: 1D, 2D, 2D
  • Buffers: vertex, index, constant, structured, append and consume, and byte access buffers. A buffer is a one-dimensional linear block of memory.

What operations on resources are performed in C/C++ and HLSL?

  • Resources are created, bound/unbound, and released in C/C++.
  • Resources are used and manipulated in HLSL.

What stage of the application lifetime is the most suitable for creating resources?

All resources should be created at startup time (during initialization). Specifically, create all pipeline configurations (state objects used by fixed function stages and shader program objects used by the programmable stages) during startup.

What are the common elements of resource description structures?

Usage flags, bind flags, CPU access flags, and miscellaneous flags.

What usage flags may be specified in a resource description structure?

  • Default - GPU-Read/Write; examples of usage are render textures and stream output vertex buffers.
  • Dynamic - GPU-Read, CPU-Write; an example is a constant buffer.
  • Immutable - GPU-Read; the resource has to be initialized with its data at creation time through the D3D11_SUBREASOURCE_DATA parameter.
  • Staging - GPU-Read/Write, CPU-Read/Write; a resource can be manipulated by the GPU, then copied onto a staging resource, and then read back by the CPU; this arrangment lets the resources used by the GPU remain as close as possible to the GPU and not be concerned with CPU access.

How is a buffer's size measured?

A buffer's size is measured in bytes: buffer size = the number of elements * the size of an element

How can resources be bound to the pipeline?

Resources can be bound to the pipeline directly or through a resource view.

Resource Flags Location How
Vertex Buffer D3D11_BIND_VERTEX_BUFFER
D3D11_BIND_STREAM_OUTPUT
IA, Stream Output Stage directly
Index Buffer D3D11_BIND_INDEX_BUFFER IA directly
Constant Buffer D3D11_BIND_CONSTANT_BUFFER VS, HS, DS, GS, PS directly
Standard Buffer and
Structured Buffer
D3D11_BIND_SHADER_RESOURCE
D3D11_BIND_UNORDERED_ACCESS
read/write access: PS, CS
read access only: VS, HS, DS, GS, OM
SRV or UAV or both
Render Target OM RTV
Depth Stencil OM DSV
Stream Output Buffer output directly
  • Flags - allowed binding flags for the resource
  • Location - location(s) on the pipeline where a resource is allowed to be bound at
  • How - how is a resource bound to the pipeline location: directly or through a resource view

Resource Views

What is a resource view?

A resource view is an adapter used to connect a resource to various locations in the pipeline. It conceptually provides a “view” of a resource. It is possible to use multiple resource views on a single resource for use at different locations on the pipeline. Each type of a resource view correponds to a particular location that a resource can be bound to on pipeline.

What are the four types of resource views?

Resource view Usage
RTV Render Target View allows a resource to receive an output from the pipeline; represents a color render target
DSV Depth Stencil View allows a resource to receive an output from the pipeline; represents a depth stencil buffer
SRV Shader Resource View provides read access to a resource to all programmable shader stages
UAV Unordered Access View provides random read/write access to a resource to a compute shader or pixel shader programs

Resource reference: p.36-37. Resource view creation: p.37-42

Vertex Buffer

What is the purpose of a vertex buffer?

A vertex buffer holds data assembled in the IA (Input Assembler) stage. It provides per-vertex information to the pipelin, either directly, in multiple buffer configurations, or through instancing. All data elements in a vertex buffer are of the same size.

What is the simplest vertex buffer configuration?

The simplest vertex buffer configuration is an array of vertex structures, where each vertex contains elements such as position, normal vector, and texture coordinates.

Is it possible to use more than one vertex buffer at once?

Yes, it is. This allows vertex data to be split among multiple buffers.

What is instanced rendering?

In instanced rendering one or more vertex buffers provide a model's per-vertex data, and an additional vertex buffer provides per-instance data. The model defined in the first buffer(s) is then rendered as a series of models, with the per-instance data from the second buffer applied to each instance. This setup allows for many objects to be rendered with a single draw call.

Reference: p.43, figure 2.8

Constant Buffer

The constant buffer is accessible from the programmable shader stages and is used in HLSL code. It is used to provide information to the programmable shader programs executed in the pipeline.

What is a constant buffer used for?

A constant buffer is a primary mechanism of data transfer from the application to the shader programs.

Does the constant buffer implement an array or a structure?

A structure.

What pipeline stages can accept a constant buffer?

Each of the programmable pipeline stages (VS, HS, DS, GS, PS) can accept one or more constant buffers.

Can a single constant buffer be simultaneously bound to multiple locations in the pipeline?

Yes, it can because constant buffers only support read accesses from the shader programs. Since the buffer is read-only, a single constant buffer can be simultaneously bound to multiple locations in the pipeline, without the possibility of causing memory access conflicts.

What a requirement for the constant buffer's ByteWidth?

A constant buffer must be created with a ByteWidth that is a multiple of 16 bytes. This requirement is only for constant buffers.

How are constant buffers declared in HLSL?

In HLSL, constant buffers are declared with the cbuffer keyword.

How is the contant buffer's name used?

The constant buffer's name is used by the application to identify the buffer, and to load the contents into it. However, the buffer's name is not used within HLSL.

Standard Buffer and Structured Buffer

What is a standard buffer?

The standard buffer is an array containing elements of a built-in type. Each value can be accessed by its index.

What is a structured buffer?

The structured buffer is an array containing elements of a user-defined structure.

Can the standard and structured buffers be used for reading and writing by the pipeline stages?

Yes, they can. The buffers can be bound to multiple locations (through a shader resource view) when used for read-only access. However, when an unordered access view is used, it may only be bound to a single location.

What may standard and structured buffers be used for?

  • to store large data structures accessed by programmable pipeline stages
  • to provide communication between the pipeline stages
  • to provide communication between the individual processing elements within the same pipeline stage

What additional parameters, besides ByteWidth, are required to create a structured buffer?

  • StructureByteStride - the size of the structure element in bytes
  • MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED - indicates that the buffer will be used as a structured buffer
  • The ByteWidth parameter is simply the number of elements in the buffer multiplied by the structure's size.

What are the two types of resource views suitable for binding a standard and a structured buffer to the pipeline?

SRV (Shader Resource View) and UAV (Unordered Access View)

How should the format be provided for standard buffers?

For standard buffers, the format has to be provided in the SRV description structure.

How should the format be provided for structured buffers?

For structured buffers, the format should be set to DXGI_FORMAT_UNKNOWN. A shader program determines the actual format from the HLSL structure declaration.

Creating a SRV and an UAV for a standard/structured buffer: p.61

How can a standard and a structured buffer be used in HLSL?

A structured buffer requires a corresponding structure declaration in the shader program. The structured buffer is declared with a template-like syntax:

// Declare the structure.
struct Point 
{ 
    float Width; 
    float4 Vec; 
}; 
 
// Declare the input and output resources.
RWStructuredBuffer<Point> NewVal : register(u0); 
StructuredBuffer<Point> CurrentVal : register(t0);

Append/Consume Buffers and Byte Address Buffers

Append/Consume Buffers and Byte Address Buffers are specialized ways to use buffer resources through Unordered Access Views.

notes/misc/direct3d_q_and_a.txt · Last modified: 2020/08/26 (external edit)