Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

c++ - CreateDepthStencilView()失败(已解决)(CreateDepthStencilView() is failing (SOLVED))

I'm trying to create a shadow map in my DirectX application, but currently having trouble when creating the Depth Stencil view.

(我正在尝试在DirectX应用程序中创建阴影贴图,但是当前在创建深度模具视图时遇到了麻烦。)

CreateDepthStencilView() is leaving my ID3D11DepthStencilView* variable as NULL.

(CreateDepthStencilView()将我的ID3D11DepthStencilView *变量保留为NULL。)

The only error I've managed to get from the HRESULT is "The parameter is incorrect" .

(我从HRESULT设法获得的唯一错误是"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.

(似乎问题出在D3D11_TEXTURE2D_DESC和ID3D11_DEPTH_STENCIL_VIEW_DESC上,因为当我分别用深度纹理和nullptr替换它们时,它可以工作。)

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.

(我也曾尝试设置shadowDsv.Flags = 0 ,如本文所建议 ,但结果相同。)

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.

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

This is my code:

(这是我的代码:)

Application.h

(应用程序)

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

Application.cpp

(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.

(编辑:这是调用CreateDepthStencilView()时的控制台输出。)

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.

(似乎是R32_TYPELESS格式是引起此错误的原因。)

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

(再次感谢 :))

EDIT2: This is now solved, the suggestion to change the format of the D3D11_DEPTH_STENCIL_VIEW_DESC to DXGI_FORMAT_D32_FLOAT was one step, then I also took the advice from the stackoverflow post linked above to set shadowDsv.Flags = 0 , it now works perfectly.

(EDIT2:现在已解决,将D3D11_DEPTH_STENCIL_VIEW_DESC的格式更改为DXGI_FORMAT_D32_FLOAT的建议是一个步骤,然后我还从上面链接的stackoverflow帖子中获取了建议,将shadowDsv.Flags = 0设置为shadowDsv.Flags = 0 ,现在可以正常使用了。)

  ask by Sturdy translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...