CreateDepthStencilView

CreateDepthStencilView

我正在尝试在DirectX应用程序中创建阴影贴图,但是当前在创建深度模具 View 时遇到了麻烦。 CreateDepthStencilView()将我的ID3D11DepthStencilView *变量保留为NULL。

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

我也尝试按照this post的建议设置shadowDsv.Flags = 0,但结果相同。

我正在关注this教程,代码与他的相同(除了我的是DX11而不是DX10),我不确定是什么问题。这是我的代码:

应用程序

    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。

关于c++ - CreateDepthStencilView()失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59126663/

10-09 09:00