Graphics Reference
In-Depth Information
groupId : SV_GroupID, uint3 groupThreadId: SV_GroupThreadID,
uint3 dispatchThreadId :
SV_DispatchThreadID)
{ float4 sample = input[dispatchThreadId.xy];
// Calculate the Relative luminance (and map to 0-255)
float luminance = LUMINANCE(sample.xyz) * 255.0;
// Addressable as bytes, x4 to store 32-bit integers
// Atomic increment of value at address.
outputByteBuffer.InterlockedAdd ((uint)luminance * 4,
1);
}
3.
In order to interact with this compute shader, we need to prepare a buffer to store the
results. Note that we also create a buffer that is accessible from the CPU. The two
properties that make the buffer accessible to the CPU are highlighted.
var histogramResult = new SharpDX.Direct3D11.Buffer(device,
new BufferDescription
{
BindFlags = BindFlags.UnorderedAccess,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.BufferAllowRawViews,
Usage = ResourceUsage.Default,
SizeInBytes = 256 * 4,
StructureByteStride = 4
});
histogramResult.DebugName = "Histogram Result";
var histogramUAV = CreateBufferUAV (device, histogramResult);
// Create resource that can be read from the CPU for
// retrieving the histogram results
var cpuReadDesc = histogramResult.Description;
cpuReadDesc.OptionFlags = ResourceOptionFlags.None;
cpuReadDesc.BindFlags = BindFlags.None;
cpuReadDesc.CpuAccessFlags = CpuAccessFlags.Read ;
cpuReadDesc.Usage = ResourceUsage.Staging ;
var histogramCPU = new Buffer(device, cpuReadDesc);
histogramCPU.DebugName = "Histogram Result (CPU)";
4.
We will wrap the logic to create the buffer's UAV into a reusable function called
CreateBufferUAV .
public static UnorderedAccessView
CreateBufferUAV(SharpDX.Direct3D11.Device device,
SharpDX.Direct3D11.Buffer buffer)
{
 
Search WWH ::




Custom Search