0
\$\begingroup\$

I'm trying to render to a texture and then pull the image data out. I've created one texture as a render target and another as a staging texture. After rendering to the render target, I use CopyResource to copy from the render target texture to the staging texture. So far, so good. However, when I use DeviceContext.MapSubresource to get the data from the staging texture, I get E_INVALIDARGS exception, and I can't figure out why.

Here is how I create the staging texture:

        textureDesc = new Texture2DDescription()
        {
            ArraySize = 1,
            BindFlags = BindFlags.None,
            CpuAccessFlags = CpuAccessFlags.Read,
            Format = Format.B8G8R8A8_UNorm,
            Height = 256,
            MipLevels = 1,
            SampleDescription = new SampleDescription(1, 0),
            Usage = ResourceUsage.Staging,
            Width = 256
        };
        _renderStaging = new Texture2D(_dev, textureDesc);

Here is how I populate the staging texture and then try to map it:

            DataStream stream;
            _con.CopyResource(_renderTarget, _renderStaging);
            // The following is the line that fails
            box = _con.MapSubresource(_renderStaging, 0, MapMode.Read, MapFlags.None, out stream);

I have the same code working in C++, so I know I have the general idea right. This is the working C++ code:

textureDesc.Width = 256;
textureDesc.Height = 256;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
textureDesc.SampleDesc.Count = 1;
textureDesc.MiscFlags = 0;
textureDesc.Usage = D3D11_USAGE_STAGING;
textureDesc.BindFlags = 0; 
textureDesc.CPUAccessFlags = D3D10_CPU_ACCESS_READ;
hr = dev->CreateTexture2D(&textureDesc, NULL, &renderStagingTexture);

devcon->CopyResource(renderStagingTexture, renderTargetTextureMap);
D3D11_MAPPED_SUBRESOURCE mappedResource;
devcon->Map(renderStagingTexture, 0, D3D11_MAP_READ, 0, &mappedResource);
\$\endgroup\$
0
\$\begingroup\$

The error was due to _con being the deferred context while in the equivalent C++ code I was using the immediate context. When I switched the SharpDX code to use immediate context, it worked just as expected.

Unfortunately, the problem was not illustrated by the code I posted.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.