Graphics Reference
In-Depth Information
var srcTextureSRV = ShaderResourceView.FromFile(device,
"Village.png");
var srcTexture = srcTextureSRV.ResourceAs<Texture2D>();
var desc = srcTexture.Description;
// Compile the shaders
using (var bytecode = ShaderBytecode.Compile(hlslCode,
"HistogramCS", "cs_5_0"))
using (var cs = new ComputeShader(device, bytecode))
{
// The source resource is the original image
context.ComputeShader.SetShaderResource(0,
srcTextureSRV);
// The destination resource is the histogramResult
context.ComputeShader.SetUnorderedAccessView(0,
histogramUAV);
// Run the histogram shader
context.ComputeShader.Set(cs);
context.Dispatch((int)Math.Ceiling(desc.Width / 1024.0 ),
(int)Math.Ceiling(desc.Height / 1.0 ), 1);
// Set the compute shader stage SRV and UAV to null
context.ComputeShader.SetShaderResource(0, null);
context.ComputeShader.SetUnorderedAccessView(0, null);
...SNIP
}
6.
Lastly, we copy the result into our CPU accessible resource and then load this into
an array.
// Copy the result into our CPU accessible resource
context.CopyResource(histogramResult, histogramCPU);
// Retrieve histogram from GPU into int array
try
{ var databox = context.MapSubresource(histogramCPU, 0,
MapMode.Read, SharpDX.Direct3D11.MapFlags.None);
int[] intArray = new int[databox.RowPitch / sizeof(int)];
System.Runtime.InteropServices.Marshal.Copy(
databox.DataPointer, intArray, 0, intArray.Length);
// intArray now contains the histogram data,
// alternatively access databox.DataPointer directly
// MapSubresource has a number of overrides that, one
// provides a DataStream.
}
finally
{
 
Search WWH ::




Custom Search