2

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;
assert(d3dtex);

ID3D11DeviceContext* ctx = NULL;
m_Device->GetImmediateContext(&ctx);
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);
Seacomit
  • 277
  • 2
  • 12

1 Answers1

2

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 (when you are using D3D11_MAP_WRITE in the call to Map). 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).

MuertoExcobito
  • 9,741
  • 2
  • 37
  • 78