Jump to content
  • Advertisement
Sign in to follow this  
drowsyn

DX11 Accessing the data inside a texture from C++

This topic is 2485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a way to read the raw data inside a ID3D11Texture2D? I want to build a set of vertices and normals from a heightmap texture, but can't figure out how to actually get access to the color data. My assumption was that Map would be the solution, but for whatever reason I can't get it to work (there's no errors, which makes it all the more annoying). Am I approaching this wrong, or am I just screwing up somewhere?


// Load the texture
D3DX11_IMAGE_LOAD_INFO loadInfo;
loadInfo.MipLevels = 1;
loadInfo.Usage = D3D11_USAGE_STAGING;
loadInfo.CpuAccessFlags = D3D11_CPU_ACCESS_READ;

hr = D3DX11CreateTextureFromFile(g_D3DDevice, L"heightmap8x8.bmp", &loadInfo, NULL, (ID3D11Resource**)(&g_HeightmapTex), NULL);
if (FAILED(hr))
return hr;

// Try to access it's data
D3D11_MAPPED_SUBRESOURCE subres;
hr = g_D3DDeviceContext->Map(g_HeightmapTex, 0, D3D11_MAP_READ, NULL, &subres);
if (FAILED(hr))
return hr;
// do something with the data
g_D3DDeviceContext->Unmap(g_HeightmapTex, 0);


I can use the texture in a shader so it shouldn't be an issue of it not loading properly. I played around with the Usage and CpuAccessFlags, but I'm fairly certain that this is the way they should be set up if I want to just read the texture. As I said, I'm not getting any access errors (or any errors period), the application just exits quietly shortly after starting. PIX isn't helping me either.

Hopefully someone can smash me in the head with the Bat of Knowledge. Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
I guess writing about the problem can be helpful. I figured out that I have to assign 0 to loadInfo.BindFlags for a read-only Mapping (that's not the correct term, surely?)

I do wish Visual Studio gave me a hint though!

Share this post


Link to post
Share on other sites
0 is the only valid value for the bind flags of a staging texture. If you enable the DEBUG flag on the device and link to the debug version of D3DX, the you should get an error message.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!