So I'm trying to create an empty texture with a few mip levels, write to that texture using a compute shader, then read those values back to the application. At the moment I have created the texture and my compute shader is running and filling the mips with solid colours. When I try to map the resource though I am not able to read anything.
Here is the basic setup.
Texture2DDescription textureDesc;
textureDesc.Width = 64;
textureDesc.Height = 64;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = SharpDX.DXGI.Format.R16G16B16A16_Float;
textureDesc.SampleDescription.Count = 1;
textureDesc.SampleDescription.Quality = 0;
textureDesc.Usage = ResourceUsage.Default;
textureDesc.BindFlags = BindFlags.UnorderedAccess | BindFlags.ShaderResource;
textureDesc.CpuAccessFlags = CpuAccessFlags.None;
textureDesc.OptionFlags = ResourceOptionFlags.None;
SharpDX.Direct3D11.Texture2D emptyTexture = new SharpDX.Direct3D11.Texture2D(device, textureDesc);
UnorderedAccessViewDescription uavDesc = new UnorderedAccessViewDescription();
uavDesc.Format = SharpDX.DXGI.Format.R16G16B16A16_Float;
uavDesc.Dimension = UnorderedAccessViewDimension.Texture2D;
uavDesc.Texture2D.MipSlice = 0;
UnorderedAccessView uavMip0 = new UnorderedAccessView(device, emptyTexture, uavDesc);
computeShader.SetParameterResource("gOutput", uavMip0);
computeShader.SetParameterValue("fillColour", new Vector4(0.1f, 0.2f, 0.3f, 1.0f));
computeShader.Apply();
device.Dispatch(1, 64, 1);
BufferDescription bufferDesc = new BufferDescription();
bufferDesc.Usage = ResourceUsage.Staging;
bufferDesc.BindFlags = BindFlags.None;
bufferDesc.SizeInBytes = 8 * 64 * 64;
bufferDesc.CpuAccessFlags = CpuAccessFlags.Read;
bufferDesc.StructureByteStride = 8;
bufferDesc.OptionFlags = ResourceOptionFlags.None;
SharpDX.Direct3D11.Buffer localbuffer = new SharpDX.Direct3D11.Buffer(device, bufferDesc);
device.Copy(emptyTexture, 0, localbuffer, 0, SharpDX.DXGI.Format.R16G16B16A16_Float);
DataStream data = new DataStream(8 * 64 * 64, true, true);
DataBox box = ((DeviceContext)device).MapSubresource(localbuffer, MapMode.Read, MapFlags.None, out data);
Half4 value = data.ReadHalf4();
So no matter how many values I read at the end, they're always (0, 0, 0, 0). I'm not really sure where the problem lies at the moment.