Graphics Reference
In-Depth Information
19. We will now create the ParticleRenderer.UpdateCS function for compiling
and running the particle simulation's update compute shader:
private void UpdateCS(string csName, UnorderedAccessView
append, UnorderedAccessView consume)
{
var context = this.DeviceManager.Direct3DContext;
// Compile the shader if it isn't already
if (!computeShaders.ContainsKey(csName))
CompileComputeShader(csName);
// Set the shader to run
context.ComputeShader.Set(computeShaders[csName]);
// Dispatch the compute shader thread groups
context.Dispatch((int)Math.Ceiling(Constants
.MaxParticles / (double)ThreadsX), 1, 1);
}
20. Next, we will create the ParticleRenderer.GenerateCS function for running
the particle generator's compute shader.
public const int GeneratorThreadsX = 16;
private void GenerateCS(string name,
UnorderedAccessView append)
{
// Compile the shader if it isn't already
if (!computeShaders.ContainsKey(name))
{
int oldThreadsX = ThreadsX;
ThreadsX = GeneratorThreadsX;
CompileComputeShader(name);
ThreadsX = oldThreadsX;
}
// Set the shader to run
context.ComputeShader.Set(computeShaders[name]);
// Dispatch the compute shader thread groups
context.Dispatch((int)Math.Ceiling(ParticlesPerBatch /
16.0), 1, 1);
}
21. We will use the following CompileComputeShader function to compile our
compute shaders on demand. This allows easy addition of more shaders.
public Dictionary<String, ComputeShader> computeShaders =
new Dictionary<string, ComputeShader>();
public int ThreadsX = 128; // default thread group width
 
Search WWH ::




Custom Search