DarkBASIC Professional Help File

DX11 CREATE STRUCTURED BUFFER

Creates a structured buffer.
Structured buffers are used to store array-like data where each element maps to a struct on the HLSL side. As such this type of buffer can be used to hold most any type of fixed-size data.
Structured buffers are available in both DX10- and DX11 mode.
A structured buffer is declared like so in HLSL:
`
StructuredBuffer MyBuffer : register(t16); // This defines a read-only structured buffer containing MyStruct elements that is bound to register t16 and is accessible to all shader stages.
RWStructuredBuffer MyWritableBuffer : register(u1); // This defines a Read/Write structured buffer containing MyStruct elements that is bound to register u1 and is only accessible to pixel and compute shaders.
`
Buffers can be used to share data between the CPU and GPU and can be bound to the shader pipeline using functions such as DX11 SET OBJECT BUFFER, DX11 SET SPRITE BUFFER, and so forth.
Buffers can be bound to everything that can also have textures bound. When accessed from your shader programs read-only buffers will be bound to registers t16 through t23 (registers t0 through t15 are
reserved for texture resources). Buffers can also be written to by pixel and compute shaders (the other shader stages can not have output resources). The nice thing about this is that you can
write anything to a buffer, at any position, which is different from for example writing to a render target where you will only be able to write to a single pre-defined position. You can also read from
any position within a writable buffer from your shader.
There are however some limitations; a buffer cannot be bound as a read-only resource at the same time as it is bound as a read-only resource. This means that if your pixel shader writes to a certain buffer
that buffer can not be accessed by any other shader stages within the same shader technique. Compute shaders are run in isolation so this limitation does not apply for them.
When using an output buffer with a pixel shader it should also be noted that it shares registers with render targets. In order to find out what register your writable resource will be bound to, all render
targets will be bound first, starting at register u0. After that any writable texture resources are set. Buffers will be bound to the slots after that.
There is also a hardware limitation on how many output resources that can be bound simultaneously; for DX11 this number is 8, meaning that your combined number of render targets, RWTextures and RWBuffers
must not exceed 8. On DX10 hardware you can only have a single output resource however. Since there is always a render target associated with a camera, this means that you cannot use output resources
with your pixel shaders in DX10 compatibility mode. You can still use them in compute shaders however.

Syntax

    Return Dword = DX11 CREATE STRUCTURED BUFFER(elementCount, elementSize, accessMode)

Parameters

    elementCount

> Dword
> The number of elements in the created structured buffer.

    elementSize

> Dword
> The size in bytes of each element in the buffer; this should correspond to the byte size of your HLSL element struct.

    accessMode

> Dword
> Corresponds to the ACCESSMODE_XXX constants. You must set this to ACCESSMODE_GPU_WRITABLE to create a buffer that can be written to by the GPU.

Returns

The created structured buffer.

See also

DIRECTCOMPUTE Functions Menu
DX11 Function Categories