I have a DirectX10 texture (ID3D10Texture2D) that I load from disk with the following code:
CComPtr<ID3D10Device> spD3D; // Initialized correctly elsewhere hr = D3DX10CreateTextureFromFile( spD3D.p, wsFilename.c_str(), NULL, NULL, &spTextureResource, NULL ); if( FAILED(hr) || ! spTextureResource.p ) return false; spTextureResource->QueryInterface <ID3D10Texture2D> ( &m_spTexture ); if( ! m_spTexture.p ) return false;
The texture loads fine, I can blit this onscreen.
Later, I want to use that texture as an atlas. To do so, the first thing I want to do is to call ID3D10Texture2D::Map() in order to get at the texels and parse them (to determine where my tiles are).
The following call fails with E_INVALIDARG:
D3D10_MAPPED_TEXTURE2D mapped; HRESULT hr = spTexture->Map( 0, D3D10_MAP_READ, 0, &mapped );
So I’m thinking that this fails because the texture can’t be read from by the CPU. So in order to get a texture that is CPU readable, I tried setting the D3DX10_IMAGE_LOAD_INFO structure to include D3D10_CPU_ACCESS_READ. This fails on D3DX10CreateShaderResourceViewFromFile() with E_INVALIDARG.
So I guess I’ve given up trying to load in a texture and be able to read its data from on the CPU.
So I thought, what about making a texture with the following D3D10_TEXTURE2D_DESC flags:
D3D10_TEXTURE2D_DESC desc; ZeroMemory(&desc, sizeof(D3D10_TEXTURE2D_DESC)); desc.CPUAccessFlags = D3D10_CPU_ACCESS_READ | D3D10_CPU_ACCESS_WRITE; desc.Usage = D3D10_USAGE_DYNAMIC; desc.BindFlags = D3D10_BIND_RENDER_TARGET | D3D10_BIND_SHADER_RESOURCE;
Step 2 of my plan is to use CopyResource or CopyResourceSubregion to copy data from the disk file into my dynamic texture, so I can use the dynamic texture as a source to read texel data from.
Will this approach work? Am I barking up the wrong tree and missing an obvious simpler route?
I’m worried that CopyResource won’t work either, since the original texture was loaded from disk without the D3D10_CPU_ACCESS_READ flag.
God, I’ve got to write this for OpenGL too, I hope it’s easier with GL!
My plan is to parse this texture and determine where my tiles are, so I can use the texture as an atlas for generating sprites.