Search code examples
c++directxdirectx-11shadow

CreateDepthStencilView() is failing


I'm trying to create a shadow map in my DirectX application, but currently having trouble when creating the Depth Stencil view. CreateDepthStencilView() is leaving my ID3D11DepthStencilView* variable as NULL.

The only error I've managed to get from the HRESULT is "The parameter is incorrect". It seems the problem is with both the D3D11_TEXTURE2D_DESC and the ID3D11_DEPTH_STENCIL_VIEW_DESC as when I've replaced them with the depth texture and nullptr respectively, it works. I then replaced the working function parameters with the shadow map values one at a time and it did not work with either.

I have also tried setting shadowDsv.Flags = 0 , as suggested in this post, but had the same result.

I'm following this tutorial, the code is the same as his (except mine is DX11 not DX10), I'm unsure what the problem could be. This is my code:

Application.h

    ID3D11Texture2D*        _shadowMapTexture;
    ID3D11DepthStencilView* _shadowMapStencil;
    ID3D11ShaderResourceView* _shadowMapRV;

Application.cpp

    //Set up shadow map
    D3D11_TEXTURE2D_DESC shadowTexDesc;
    shadowTexDesc.Width = _WindowWidth;
    shadowTexDesc.Height = _WindowHeight;
    shadowTexDesc.MipLevels = 1;
    shadowTexDesc.ArraySize = 1;
    shadowTexDesc.Format = DXGI_FORMAT_R32_TYPELESS;
    shadowTexDesc.SampleDesc.Count = 1;
    shadowTexDesc.SampleDesc.Quality = 0;
    shadowTexDesc.Usage = D3D11_USAGE_DEFAULT;
    shadowTexDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
    shadowTexDesc.CPUAccessFlags = 0;
    shadowTexDesc.MiscFlags = 0;

    D3D11_DEPTH_STENCIL_VIEW_DESC shadowDsv;
    shadowDsv.Format = shadowTexDesc.Format;
    shadowDsv.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
    shadowDsv.Texture2D.MipSlice = 0;

    D3D11_SHADER_RESOURCE_VIEW_DESC shadowSrv;
    shadowSrv.Format = DXGI_FORMAT_R32_FLOAT;
    shadowSrv.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
    shadowSrv.Texture2D.MipLevels = shadowTexDesc.MipLevels;
    shadowSrv.Texture2D.MostDetailedMip = 0;

    hr = _pd3dDevice->CreateTexture2D(&shadowTexDesc, nullptr, &_shadowMapTexture);
    hr = _pd3dDevice->CreateDepthStencilView(_shadowMapTexture, &shadowDsv, &_shadowMapStencil);
    hr = _pd3dDevice->CreateShaderResourceView(_shadowMapTexture, &shadowSrv, &_shadowMapRV);

EDIT: This is the console output when CreateDepthStencilView() is called. Thank you for the help so far.

D3D11 ERROR: ID3D11Device::CreateDepthStencilView: The Format (0x27, R32_TYPELESS) is invalid, when creating a View; it is not a fully qualified Format castable from the Format of the Resource (0x27, R32_TYPELESS). [ STATE_CREATION ERROR #144: CREATEDEPTHSTENCILVIEW_INVALIDFORMAT]
D3D11 ERROR: ID3D11Device::CreateDepthStencilView: The format (0x27, R32_TYPELESS) cannot be used with a DepthStencil view. [ STATE_CREATION ERROR #144: CREATEDEPTHSTENCILVIEW_INVALIDFORMAT]
D3D11 ERROR: ID3D11Device::CreateDepthStencilView: There were unrecognized flags specified in the DepthStencilView Flags field. The flags value was 0xcccccccc, while the valid flags are limited to 0x3. [ STATE_CREATION ERROR #2097153: CREATEDEPTHSTENCILVIEW_INVALIDFLAGS]
Exception thrown at 0x76D235D2 in DX11 Framework.exe: Microsoft C++ exception: _com_error at memory location 0x00B3EE98.
D3D11 ERROR: ID3D11Device::CreateDepthStencilView: Returning E_INVALIDARG, meaning invalid parameters were passed. [ STATE_CREATION ERROR #148: CREATEDEPTHSTENCILVIEW_INVALIDARG_RETURN]

It seems the R32_TYPELESS format is what's causing the error. After looking at the documentation I see there are only a few allowable formats. Does anyone recommend a specific format for a shadow map? I've not done this before so unsure if any of the following would be a good subsitute:

DXGI_FORMAT_D16_UNORM
DXGI_FORMAT_D24_UNORM_S8_UINT
DXGI_FORMAT_D32_FLOAT
DXGI_FORMAT_D32_FLOAT_S8X24_UINT
DXGI_FORMAT_UNKNOWN

Thanks again :)


Solution

  • To anyone else having this problem: my fix was to set the D3D11_DEPTH_STENCIL_VIEW_DESC format to DXGI_FORMAT_D32_FLOAT, and the D3D11_DEPTH_STENCIL_VIEW_DESC Flags equal to 0.