Advertisement

Loading texturecube using DirectXTK DDSTextureLoader

Started by August 05, 2018 09:31 AM
2 comments, last by Theo Berlin 6 years, 6 months ago

Hi, 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 snippets of my code where I load the texture, set the shader resource view to the pixel shader and then sample it in HLSL:


// 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);

// Setting the texture
DeviceContext->PSSetShaderResources(0, 1, mSkybox->GetSkyboxSRV());

// HLSL: Sampling the texture
TextureCube skyboxTexture : register(t0);

SamplerState sampWrap : register(s0);

struct PS_IN
{
	float4 Pos : SV_POSITION;
	float3 lookVector : TEXCOORD0;
};

float4 PS_main(PS_IN input) : SV_TARGET
{
    return skyboxTexture.Sample(sampWrap, input.lookVector);
}

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]

Does anyone have any ideas on what to do? Any help is much appreciated!

I never used their library so I took a quick look at https://github.com/Microsoft/DirectXTK/blob/master/Src/DDSTextureLoader.cpp

The loader do consider cube maps if the flags are properly set in the DDS file's header. You don't seem to be the one who have the responsibility to specify whether it's a cube map or not :

 


case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
                    if (d3d10ext->miscFlag & D3D11_RESOURCE_MISC_TEXTURECUBE)
                    {
                        arraySize *= 6;
                        isCubeMap = true;
                    }
                    depth = 1;
                    break;

 

The shader resource view is later created with consideration for D3D11_SRV_DIMENSION_TEXTURECUBE by using that same boolean :


if (isCubeMap)
                        {
                            if (arraySize > 6)
                            {
                                SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBEARRAY;
                                SRVDesc.TextureCubeArray.MipLevels = (!mipCount) ? -1 : desc.MipLevels;

                                // Earlier we set arraySize to (NumCubes * 6)
                                SRVDesc.TextureCubeArray.NumCubes = static_cast<UINT>(arraySize / 6);
                            }
                            else
                            {
                                SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
                                SRVDesc.TextureCube.MipLevels = (!mipCount) ? -1 : desc.MipLevels;
                            }
                        }

 

So far the problem seems to be your DDS file. Perhaps it is saved as a plain array of 2d texture and is missing few flags to be treated as a cube map? Only if you share the DDS file we could check.

Advertisement

You were right, it was the file! I got it sorted, now I just have some other tiny things left to solve before I can get this skybox working properly. I'll post another thread if I can't figure it out.

Thanks!

This topic is closed to new replies.

Advertisement