I've been trying to implement a skybox for some time now and there's probably only a tiny problem left to solve. When I load a texture using DirectXTK's CreateDDSTextureFromFileEx with a TEXTURECUBE flag, the resulting shader resource has its view dimension set to Texture2D and not TextureCube, which means I can't treat it as a TextureCube in HLSL. Also the file I'm loading contains all six faces. Here's the code where the texture is loaded:
// Loading the texture
HRESULT hr = DirectX::CreateDDSTextureFromFileEx(device, filename, 0, D3D11_USAGE_IMMUTABLE, D3D11_BIND_SHADER_RESOURCE,
0, D3D11_RESOURCE_MISC_TEXTURECUBE, false, &texture, &mSkyboxSRV);
This is the error message being output by DirectX:
D3D11 ERROR: ID3D11DeviceContext::Draw: The Shader Resource View dimension declared in the shader code (TEXTURECUBE) does not match the view type bound to slot 0 of the Pixel Shader unit (TEXTURE2D). This mismatch is invalid if the shader actually uses the view (e.g. it is not skipped due to shader code branching). [ EXECUTION ERROR #354: DEVICE_DRAW_VIEW_DIMENSION_MISMATCH]
I've also tried this:
ID3D11Texture2D* texture = nullptr;
HRESULT hr = DirectX::CreateDDSTextureFromFileEx(device, filename, 0, D3D11_USAGE_IMMUTABLE, D3D11_BIND_SHADER_RESOURCE,
0, D3D11_RESOURCE_MISC_TEXTURECUBE, false, (ID3D11Resource**)&texture, nullptr);
D3D11_TEXTURE2D_DESC texDesc;
texture->GetDesc(&texDesc);
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
srvDesc.TextureCube.MipLevels = texDesc.MipLevels;
srvDesc.TextureCube.MostDetailedMip = 0;
srvDesc.Format = texDesc.Format;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
hr = device->CreateShaderResourceView(texture, &srvDesc, &mSkyboxSRV);
But that gives me an error message saying the texture's view dimension and the srv's view dimension are not the same:
D3D11 ERROR: ID3D11Device::CreateShaderResourceView: Resources created without D3D11_RESOURCE_MISC_TEXTURECUBE may not be treated as cubemap ShaderResourceViews. [ STATE_CREATION ERROR #126: CREATESHADERRESOURCEVIEW_INVALIDDESC]
Does anyone have any ideas on what to do? Any help is much appreciated!
DDS
you are using actually contain a cubemap or does it contain a texture 2D array? If you can share a link to theDDS
, I can take a look. \$\endgroup\$ – Chuck Walbourn Aug 5 '18 at 19:04ID3D11Resource*
and then useQueryInterface
to convert to aID3D11Texture2D*
. ComPtr makes this a lot easier. See the DirectXTK wiki for a more complete code sample that can also help in diagnosing your problem. \$\endgroup\$ – Chuck Walbourn Aug 5 '18 at 19:06D3D11_RESOURCE_MISC_TEXTURECUBE
flag as a parameter. I should probably not clear it out in the case of a 2D texture arrayDDS
if it has a multiple of 6 array entries... Otherwise it's not valid or it's set anyhow if theDDS
is actually a cube map. \$\endgroup\$ – Chuck Walbourn Aug 5 '18 at 19:12