CreateDepthStencilView() 失败

CreateDepthStencilView() is failing

我正在尝试在我的 DirectX 应用程序中创建阴影贴图,但目前在创建 Depth Stencil 视图时遇到问题。 CreateDepthStencilView() 将我的 ID3D11DepthStencilView* 变量保留为 NULL。

我设法从 HRESULT 得到的唯一错误是 "The parameter is incorrect"。似乎问题出在 D3D11_TEXTURE2D_DESC 和 ID3D11_DEPTH_STENCIL_VIEW_DESC 上,因为当我分别用深度纹理和 nullptr 替换它们时,它起作用了。然后,我一次用阴影贴图值替换了工作函数参数,但两者都不起作用。

我也曾尝试设置 shadowDsv.Flags = 0 ,如 this post 中所建议,但结果相同。

我正在学习this教程,代码和他的一样(除了我的是DX11不是DX10),我不确定是什么问题。这是我的代码:

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

编辑: 这是调用 CreateDepthStencilView() 时的控制台输出。感谢您到目前为止的帮助。

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]

似乎是 R32_TYPELESS 格式导致了错误。查看文档后,我发现只有几种允许的格式。有人推荐阴影贴图的特定格式吗?我以前没有这样做过,所以不确定以下任何一项是否是一个好的替代品:

DXGI_FORMAT_D16_UNORM
DXGI_FORMAT_D24_UNORM_S8_UINT
DXGI_FORMAT_D32_FLOAT
DXGI_FORMAT_D32_FLOAT_S8X24_UINT
DXGI_FORMAT_UNKNOWN

再次感谢:)

对于遇到此问题的任何其他人:我的解决方法是将 D3D11_DEPTH_STENCIL_VIEW_DESC 格式设置为 DXGI_FORMAT_D32_FLOAT,并将 D3D11_DEPTH_STENCIL_VIEW_DESC 标志设置为 0。