d3dcoder / d3d12book

Sample code for the book "Introduction to 3D Game Programming with DirectX 12"
1.47k stars 571 forks source link

SSAO sample failing due to invalid SRV format #7

Closed discosultan closed 7 years ago

discosultan commented 7 years ago

SSAO sample (chapter 21: Ambient Occlusion) fails with the following error message:

D3D12 ERROR: ID3D12Device::CreateShaderResourceView: 
For the resource format D24_UNORM_S8_UINT, when making a D3D view, 
the format name for the view can't be R24_UNORM_X8_TYPELESS.  
See documentation for the set of valid view format names for this resource format, 
determining which how the resource (or part of it) will appear to shader. 
[ STATE_CREATION ERROR #28: CREATESHADERRESOURCEVIEW_INVALIDFORMAT]

Edit: The problem is that the depth stencil resource is defined as D24_UNORM_S8_UINT with DSV matching the resource format. The resource should be defined as R24G8_TYPELESS instead and DSV as D24_UNORM_S8_UINT.

Sighman6 commented 7 years ago

Can you please be more specific on what code changes are needed to get this to work.

discosultan commented 7 years ago

Hey!

I found my solution from this StackOverflow thread.

If I remember correctly, you need to change the format for depth stencil resource description in D3DApp.cpp from

https://github.com/d3dcoder/d3d12book/blob/e0cbac0a01173aac6d79df137e309f5b617fdcaa/Common/d3dApp.cpp#L189

to (my impl is in C#)

https://github.com/discosultan/dx12-game-programming/blob/12c40b49818058980622b8873db4d673cd78b033/Common/D3DApp.cs#L212

Sighman6 commented 7 years ago

Thanks discosultan,

Here's how I fixed the C++ code:

In d3dApp.cpp find the D2DApp::OnResize method.

Change

depthStencilDesc.Format = mDepthStencilFormat; to

depthStencilDesc.Format = DXGI_FORMAT_R24G8_TYPELESS; Then before md3dDevice->CreateDepthStencilView add

    D3D12_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc = {};
    depthStencilViewDesc.Format = mDepthStencilFormat;
    depthStencilViewDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;

Finally change

md3dDevice->CreateDepthStencilView(mDepthStencilBuffer.Get(), nullptr, DepthStencilView()); to

md3dDevice->CreateDepthStencilView(mDepthStencilBuffer.Get(), &depthStencilViewDesc, DepthStencilView());

discosultan commented 7 years ago

Fixed by 4cfd00afa59210a272f62caf0660478d18b9ffed