user1926760 user1926760 - 1 year ago 81
C++ Question

Direct3D Map Pixels Black

Im trying to map pixel data as a byte array to a dynamic texture using direct3d, for some reason the resulting pixel data is black and its not getting transfered. I have converted this code directly from using updatesubresource before, but now im using map/unmap.

ID3D11Texture2D* d3dtex = (ID3D11Texture2D*)textureHandle;

ID3D11DeviceContext* ctx = NULL;
D3D11_MAPPED_SUBRESOURCE mappedResource;
ZeroMemory(&mappedResource, sizeof(D3D11_MAPPED_SUBRESOURCE));

// Disable GPU access to the vertex buffer data.
ctx->Map(d3dtex, 0, D3D11_MAP_WRITE, 0, &mappedResource);
// Update the vertex buffer here.
memcpy(mappedResource.pData, dataPtr, textureWidth * textureHeight * 4);
// Reenable GPU access to the vertex buffer data.
ctx->Unmap(d3dtex, 0);

Answer Source

You are not checking the HRESULT return from the ID3D11DeviceContext::Map function. Likely, the texture cannot be mapped. There are several reasons why a texture in D3D11 might not be mappable, the most obvious is that it was not created with the D3D11_CPU_ACCESS_WRITE access flag. If you force on the D3D11 debug layer, the debug output will print out a detailed message about the failure.

If you are relying on mapping a Unity internally created texture, you should not assume it is mappable. Using UpdateSubresource (as you were using) is more widely compatible, regardless of what flags were used to create the texture. You could query the CPUAccessFlags on the texture, using ID3D11Texture2D::GetDesc, and decide which update method to use (map/update).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download