# Read part of texture using SharpDX

I have a Texture3D. I want to read a part of it from CPU side. (I'm using the SharpDX toolkit (v2.5.1).)

My data is always 0, but the output shows no errors.

This is the source texture:

testTex = Texture3D.New<float>(GraphicsDevice, wx, wy, wz, PixelFormat.R32.Float, texDat, TextureFlags.ShaderResource, sdx11.ResourceUsage.Default);


I'm trying to read part of it here:

tempFloat = new float[1];
TextureDescription stagedTexDesc = new TextureDescription()
{
Height = 1,
Width = 1,
Depth = 1,
MipLevels = 1,
ArraySize = 1,
Format = testTex.Format,
Usage = sdx11.ResourceUsage.Staging,
};
texNew = Texture3D.New(GraphicsDevice, stagedTexDesc);

deviceContext.CopySubresourceRegion(testTex, 0, new sdx11.ResourceRegion(iHPos.X, iHPos.Y, iHPos.Z, iHPos.X + 1, iHPos.Y + 1, iHPos.Z + 1), texNew, 0);
texNew.GetData(tempFloat);


tempFloat[0] value is always 0.0. The GetData method performs Map and Unmap of the resource automatically

If I try to call GetData on the original texture, it seems to get the data correctly.

Also, is there a better way to read the data?

• This could be a bug. Could you provide a reproducible simple VS project on github issues github.com/sharpdx/SharpDX/issues so that we can have a look at this? – xoofx Feb 17 '14 at 1:13
• Thank you for the quick response. I will try to create reproducible project this evening. – Renesis Feb 17 '14 at 9:24
• I have reported an issue and uploaded a sample project. github.com/sharpdx/SharpDX/issues/281 – Renesis Feb 17 '14 at 19:11
• This question appears to be off-topic because it is about a SharpDX bug best discussed with the developers. – Anko Feb 24 '14 at 13:11
• Actually it wasn't a bug, I posted a solution. – Renesis Feb 25 '14 at 10:30

It seems I used deviceContext in wrong way. It should have been either initialized like this:

var deviceContext = (SharpDX.Direct3D11.DeviceContext)GraphicsDevice;


Here is the explanation what I did wrong here:

var deviceContext = new SharpDX.Direct3D11.DeviceContext(GraphicsDevice);


When creating a new DeviceContext, it is actually creating a deferred context, more relevant in multithreaded scenarios. In your case, all commands issued on this newly created deferred context were in fact no-op (because a deferred context needs a special pattern to replay it on the immediate context).

Another solution would be to use directly:

GraphicsDevice.Copy(testTex, 0, new sdx11.ResourceRegion(iHPos.X, iHPos.Y, iHPos.Z, iHPos.X + 1, iHPos.Y + 1, iHPos.Z + 1), texNew, 0);