Graphics Reference
In-Depth Information
types of structured buffers. With variable record lengths, an HLSL program can be written
to implement almost any data structure that will fit within the bounds of the resource. It
would be equally simple to create a variable-element-sized linked list as it would be to cre-
ate an array-based binary tree. As long as the program implements the accessing semantics
for the data structure, there is complete freedom to use memory as desired. This is a very
powerful feature indeed, and it opens the doorway for a very large class of algorithms that
either were impossible to implement on the GPU before, or were unwieldy to implement.
Using byte address buffers. As mentioned above, byte address buffers are intended to al-
low the developer to implement custom data structures in buffer resources. The use of the
memory block is then, by definition, open to interpretation by the algorithm that it is being
used in conjunction with. For example, if a linked-list data structure will be used to store a
list of 32-bit color values, each link node will consist of a color value followed by a link to
the next element. When the first element is added, the color value is written into the memory
location at offset 0, and the link to the next element is initialized to -1, since the next ele-
ment has not been added yet. When another element is to be added to the list, the HLSL
program would start indexing the memory at location 0 and read two 32-bit elements worth
of data—one for the color value and one for the link to the next element. If the link is set
to -1, the program has found the tail of the list and can add another element at the end. This
simple scenario is shown in Figure 2.19.
With these basics of a linked list available, the program can insert or remove nodes in
the same way as in traditional CPU-based programs. This provides a completely generic way
to use the resources from within the GPU.
However, the parallel nature of the
GPU can also introduce some complex-
ity into the use of these resources. Since
the memory access patterns are defined
by the HLSL program, it is necessary to
ensure that memory is accessed in a co-
herent and safe manner between multiple
threads of execution. We will discuss the
details of this type of synchronization in
Chapter 5, "The Computation Pipeline,"
but there is a range of techniques avail-
able to the developer to ensure thread-
safe access to the resources.
Figure 2.19. A linked-list implementation using byte
address buffers.
Creating byte address buffers. Creating a byte address buffer is very similar to the pro-
cess we have seen for the previous buffer types. It must also indicate the usage scenario
that it will be used for, and at what locations it will be bound to the pipeline with its
Search WWH ::




Custom Search