Why does D3DX11CreateShaderResourceViewFromMemory only upload a partial copy of my texture?

I have a problem with the D3DX11CreateShaderResourceViewFromMemory helper function.

I read some texture from a file or ZIP and pass the raw bytes and length to the helper function, however only part of the texture is uploaded (as confirmed by PIX). I tried fiddling with the length manually but to no avail. Here is the code that loads the texture from file:

struct FileDataLoader { void Load() { std::ifstream file(mFileName); if (file) { file.seekg(0,std::ios::end); std::streampos length = file.tellg(); file.seekg(0,std::ios::beg); mBuffer.resize(length); file.read(&mBuffer[0],length); file.close(); } } void Decompress(void*& data, std::size_t& numBytes) { data = &mBuffer[0]; numBytes = mBuffer.size(); } std::wstring mFileName; std::vector<char> mBuffer; }; FileDataLoader fdl; fdl.mFileName = L"Content\\Textures\\Smoke.dds"; fdl.Load(); void* bytes; std::size_t size; fdl.Decompress(bytes, size); DXCall(D3DX11CreateShaderResourceViewFromMemory(device, bytes, size, NULL, NULL, &particleTexture, NULL)); 

That is only a sample code that I am using to debug this problem, and I narrowed it down to the file loading and the D3DX helper function. Now if I do this instead:

DXCall(D3DX11CreateShaderResourceViewFromFileW(device, L"Content\\Textures\\Smoke.dds", NULL, NULL, &particleTexture, NULL)); 

it works perfectly fine.

Any idea on why it would not upload the texture entirely ?

1 Answer

When opening the file, you need to specify that the file is binary:

std::ifstream file( fileName, std::ios::in | std::ios::binary ); 

Without the std::ios::binary flag you're reading in plain text by default, which is not what D3DX11CreateShaderResourceViewFromMemory expects.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like