Graphics Reference
In-Depth Information
program. Since the writing of the data uses the append method, the count is automatically
incremented for each particle added to the output buffer. Likewise, the buffer that provides
the initial state of the simulation will have its count automatically decremented for each
consume operation performed on it, and should end up with a count of zero after the first
update phase is performed. Thus, the buffer that contains the initial particle data should get
an initial count that indicates the initial number of particles in the system, and the second
structured buffer will get an initial count of zero only for the first binding sequence. For
each update phase after the first one, both buffers should be bound with an initial count of
-1 to indicate that the internal count value should be used.
Dynamic injection of particles. Since we want to have a dynamic particle system, it is
necessary to be able to add new particles into the simulation during runtime. However, af-
ter the initial resource creation, the CPU cannot directly modify the contents of the buffer
(since it uses a default usage). Even so, the CPU is still able to perform particle injection
indirectly, by running a separate compute shader program in which each thread that is
invoked will create a new particle and add it to the output buffer resource. In this way, we
can allow the CPU to control the emission of new particles, and can still use the parallel
nature of the GPU to efficiently implement the desired number of new particles in batches.
For our sample program, we want to produce particles that emanate from an emitter
location, and to have them initialized with a randomized velocity direction. To do this, we
will create eight particles at a time for each thread group that is dispatched. We will use a
static array of direction vectors which point to the eight corners of a unit sized cube, and
then reflect the vectors about a random vector provided by the application. Then, each of
the eight threads in the thread group will read one of the static vectors, reflect it, and use
the resulting vector to specify the initial velocity vector for our system. The intended use
of this compute shader program is to dispatch a single thread group at a time, with a single
randomized vector supplied in a constant buffer to aid in the initialization of the particles.
Tf more thread groups are needed at the same time, a randomized vector could be read out
of a resource for each thread group by using the SVJSroupID system value semantic. This
compute shader to insert particles is shown in Listing 12.9.
struct Particle
{
float3 position;
float3 direction;
float time;
};
AppendStructuredBuffer<Particle> NewSimulationState : register( u0 );
cbuffer ParticleParameters
{
float4 EmitterLocation;
Search WWH ::




Custom Search