Graphics Reference
In-Depth Information
This example demonstrates perhaps the simplest possible resource structure with only
a single linear buffer of float values. However, this addressing structure is quite capable
of handling more complex resources as well. If we wanted to perform the same doubling
operation on all elements of a Texture2D resource, then we could modify our HLSL source
file as shown in Listing 5.3.
Texture2D<float>
InputTex
:
register(
t0 );
RWTexture2D<float>
OutputTex
:
register(
u0 );
//
Group
size
#define
size_x
20
#define
size_y
20
//
Declare
one
thread
for each
texel
of the
input
texture.
[numthreads(size_x,
size_y,
1)]
void
CSMAIN(
uint3
DispatchThreadID
:
SV_DispatchThreadID
)
{
int3
texturelocation
=
int3( 0,0
,
0 );
texturelocation.x
=
DispatchThreadID.
x;
texturelocation.y
=
DispatchThreadID.y;
float
Value
=
InputTex.
Load(
texturelocation
);
OutputTex[DispatchThreadID.xy]
= 2.0f * Value;
}
Listing S.3.
A sample compute shader for doubling the contents of a 2D texture resource.
Here we can see that we have changed the number of threads per thread group, as
well as the type of resource that will serve as our input and output storage. There is no
change in the system value semantics used for addressing remains, since the dispatch
thread ID can also handle two-dimensional identifiers. If our input texture resource was
640x480, then to process it, we would bind it to the compute shader with a UAV and call
Dispatch with a size of [32,24,1]. After the dispatch call was completed, the resource's
data elements would all have doubled values. This same concept would also be simple to
extend to a three-dimensional case, as well.
Up to this point, we have only considered direct resource mappings where the thread
addressing structure maps directly to the shape of the resource we are working with.
However, one of the benefits of allowing the developer to directly perform the resource
accesses in the shader program is that it is also possible to create customized resource
schemes. For example, if we wanted to store a four-dimensional data set in a resource,
there are no native four dimensional resource types. However, we could easily use a lin-
ear buffer resource and then manually implement the data access patterns. If we wanted
Search WWH ::
Custom Search