I was using a supersampled depth buffer during the shadow map pass (something like: screen_width * 4, screen_height * 4) and I wanted to try whether it would be faster to use a multisampled texture instead. However I am having problems resolving it to a non-multisampled texture. Specifically I am getting two errors:
D3D11 ERROR: ID3D11DeviceContext::ResolveSubresource: The Format (0x2d, D24_UNORM_S8_UINT) is never able to resolve multisampled resources. [ RESOURCE_MANIPULATION ERROR #294: DEVICE_RESOLVESUBRESOURCE_FORMAT_INVALID]
D3D11 ERROR: ID3D11DeviceContext::ResolveSubresource: Source Resource must not have the D3D11_BIND_DEPTH_STENCIL flag set. [ RESOURCE_MANIPULATION ERROR #292: DEVICE_RESOLVESUBRESOURCE_SOURCE_INVALID]
The second one seems more menacing since the D3D11_BIND_DEPTH_STENCIL has to be set for me to be able to render into the depth buffer.
The code for shadow render target initialization:
// Multisampled texture to be resolved
D3D11_TEXTURE2D_DESC msDepthStencilDesc = {};
msDepthStencilDesc.Width = (UINT)renderer->screenSize.x;
msDepthStencilDesc.Height = (UINT)renderer->screenSize.y;
msDepthStencilDesc.MipLevels = 1;
msDepthStencilDesc.ArraySize = 1;
msDepthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
msDepthStencilDesc.SampleDesc.Count = 4;
msDepthStencilDesc.SampleDesc.Quality = D3D11_STANDARD_MULTISAMPLE_PATTERN;
msDepthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
msDepthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
msDepthStencilDesc.CPUAccessFlags = 0;
msDepthStencilDesc.MiscFlags = 0;
ID3D11Texture2D* tmpMSDepthStencilTex;
HRESULT hr = renderer->device->CreateTexture2D(&msDepthStencilDesc, NULL, &tmpMSDepthStencilTex);
CHECK_WIN_ERROR(hr, "Error creating Depth Buffer texture\n");
CD3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc(D3D11_DSV_DIMENSION_TEXTURE2DMS, msDepthStencilDesc.Format);
hr = renderer->device->CreateDepthStencilView(tmpMSDepthStencilTex, &depthStencilViewDesc, &renderTarget->depthStencilView);
CHECK_WIN_ERROR(hr, "Error creating depth stencil view\n");
// Texture to be sampled in the scene pass
D3D11_TEXTURE2D_DESC depthStencilDesc = {};
depthStencilDesc.Width = (UINT)renderer->screenSize.x;
depthStencilDesc.Height = (UINT)renderer->screenSize.y;
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format = DXGI_FORMAT_R24G8_TYPELESS;
depthStencilDesc.SampleDesc.Count = 1;
depthStencilDesc.SampleDesc.Quality = 0;
depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
depthStencilDesc.CPUAccessFlags = 0;
depthStencilDesc.MiscFlags = 0;
ID3D11Texture2D* tmpDepthStencilTex;
hr = renderer->device->CreateTexture2D(&depthStencilDesc, NULL, &tmpDepthStencilTex);
CHECK_WIN_ERROR(hr, "Error creating Depth Buffer texture\n");
D3D11_SHADER_RESOURCE_VIEW_DESC depthShaderResourceViewDesc = {};
depthShaderResourceViewDesc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
depthShaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
depthShaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
depthShaderResourceViewDesc.Texture2D.MipLevels = 1;
hr = renderer->device->CreateShaderResourceView(tmpDepthStencilTex, &depthShaderResourceViewDesc, &renderTarget->depthTexture);
CHECK_WIN_ERROR(hr, "Error creating depth texture\n");The MS resolve code:
// Resolve multisample
ID3D11Resource *multisampleTexture;
renderTarget->depthStencilView->GetResource(&multisampleTexture);
ID3D11Resource *texture;
renderTarget->depthTexture->GetResource(&texture);
renderer->context->ResolveSubresource(texture, 0, multisampleTexture, 0, DXGI_FORMAT_D24_UNORM_S8_UINT);
RELEASE_DX_RESOURCE(multisampleTexture);
RELEASE_DX_RESOURCE(texture);
Thanks for your help.