Skip to content

Commit

Permalink
Fix D3D10/11 depth stencil view creation for MSAA and array textures
Browse files Browse the repository at this point in the history
  • Loading branch information
crosire committed Feb 29, 2020
1 parent 8a27116 commit 20161e0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 34 deletions.
2 changes: 2 additions & 0 deletions source/d3d10/buffer_detection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ com_ptr<ID3D10Texture2D> reshade::d3d10::buffer_detection::find_best_depth_textu
if (desc.SampleDesc.Count > 1)
continue; // Ignore MSAA textures, since they would need to be resolved first

assert((desc.BindFlags & D3D10_BIND_SHADER_RESOURCE) != 0);

if (width != 0 && height != 0)
{
const float w = static_cast<float>(width);
Expand Down
47 changes: 31 additions & 16 deletions source/d3d10/d3d10_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ HRESULT STDMETHODCALLTYPE D3D10Device::CreateTexture2D(const D3D10_TEXTURE2D_DES

// Add D3D10_BIND_SHADER_RESOURCE flag to all depth-stencil textures so that we can access them in post-processing shaders
D3D10_TEXTURE2D_DESC new_desc = *pDesc;
if (0 != (new_desc.BindFlags & D3D10_BIND_DEPTH_STENCIL))
if (new_desc.SampleDesc.Count == 1 && // Skip MSAA textures
0 != (new_desc.BindFlags & D3D10_BIND_DEPTH_STENCIL))
{
new_desc.Format = make_dxgi_format_typeless(new_desc.Format);
new_desc.BindFlags |= D3D10_BIND_SHADER_RESOURCE;
Expand Down Expand Up @@ -408,15 +409,18 @@ HRESULT STDMETHODCALLTYPE D3D10Device::CreateShaderResourceView(ID3D10Resource *
texture->GetDesc(&texture_desc);

// Only textures with the depth-stencil bind flag where modified, so skip all others
if (0 != (texture_desc.BindFlags & D3D10_BIND_DEPTH_STENCIL))
if (texture_desc.SampleDesc.Count == 1 &&
0 != (texture_desc.BindFlags & D3D10_BIND_DEPTH_STENCIL))
{
new_desc.Format = make_dxgi_format_normal(texture_desc.Format);

if (pDesc == nullptr) // Only need to set the rest of the fields if the application did not pass in a valid description already
{
new_desc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
new_desc.Texture2D.MipLevels = texture_desc.MipLevels;
new_desc.Texture2D.MostDetailedMip = 0;
new_desc.ViewDimension = texture_desc.ArraySize > 1 ? D3D10_SRV_DIMENSION_TEXTURE2DARRAY : D3D10_SRV_DIMENSION_TEXTURE2D;
new_desc.Texture2DArray.MipLevels = texture_desc.MipLevels;
new_desc.Texture2DArray.MostDetailedMip = 0;
new_desc.Texture2DArray.ArraySize = texture_desc.ArraySize;
new_desc.Texture2DArray.FirstArraySlice = 0;
}

pDesc = &new_desc;
Expand Down Expand Up @@ -450,15 +454,23 @@ HRESULT STDMETHODCALLTYPE D3D10Device::CreateDepthStencilView(ID3D10Resource *pR
{
texture->GetDesc(&texture_desc);

new_desc.Format = make_dxgi_format_dsv(texture_desc.Format);

if (pDesc == nullptr) // Only need to set the rest of the fields if the application did not pass in a valid description already
// Only non-MSAA textures where modified, so skip all others
if (texture_desc.SampleDesc.Count == 1)
{
new_desc.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
new_desc.Texture2D.MipSlice = 0;
}
assert((texture_desc.BindFlags & D3D10_BIND_DEPTH_STENCIL) != 0);

new_desc.Format = make_dxgi_format_dsv(texture_desc.Format);

pDesc = &new_desc;
if (pDesc == nullptr) // Only need to set the rest of the fields if the application did not pass in a valid description already
{
new_desc.ViewDimension = texture_desc.ArraySize > 1 ? D3D10_DSV_DIMENSION_TEXTURE2DARRAY : D3D10_DSV_DIMENSION_TEXTURE2D;
new_desc.Texture2DArray.MipSlice = 0;
new_desc.Texture2DArray.FirstArraySlice = 0;
new_desc.Texture2DArray.ArraySize = texture_desc.ArraySize;
}

pDesc = &new_desc;
}
}
}

Expand Down Expand Up @@ -564,15 +576,18 @@ HRESULT STDMETHODCALLTYPE D3D10Device::CreateShaderResourceView1(ID3D10Resource
{
texture->GetDesc(&texture_desc);

if (0 != (texture_desc.BindFlags & D3D10_BIND_DEPTH_STENCIL))
if (texture_desc.SampleDesc.Count == 1 &&
0 != (texture_desc.BindFlags & D3D10_BIND_DEPTH_STENCIL))
{
new_desc.Format = make_dxgi_format_normal(texture_desc.Format);

if (pDesc == nullptr)
{
new_desc.ViewDimension = D3D10_1_SRV_DIMENSION_TEXTURE2D;
new_desc.Texture2D.MipLevels = texture_desc.MipLevels;
new_desc.Texture2D.MostDetailedMip = 0;
new_desc.ViewDimension = texture_desc.ArraySize > 1 ? D3D10_1_SRV_DIMENSION_TEXTURE2DARRAY : D3D10_1_SRV_DIMENSION_TEXTURE2D;
new_desc.Texture2DArray.MipLevels = texture_desc.MipLevels;
new_desc.Texture2DArray.MostDetailedMip = 0;
new_desc.Texture2DArray.ArraySize = texture_desc.ArraySize;
new_desc.Texture2DArray.FirstArraySlice = 0;
}

pDesc = &new_desc;
Expand Down
2 changes: 2 additions & 0 deletions source/d3d11/buffer_detection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ com_ptr<ID3D11Texture2D> reshade::d3d11::buffer_detection_context::find_best_dep
if (desc.SampleDesc.Count > 1)
continue; // Ignore MSAA textures, since they would need to be resolved first

assert((desc.BindFlags & D3D11_BIND_SHADER_RESOURCE) != 0);

if (width != 0 && height != 0)
{
const float w = static_cast<float>(width);
Expand Down
62 changes: 44 additions & 18 deletions source/d3d11/d3d11_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ HRESULT STDMETHODCALLTYPE D3D11Device::CreateTexture2D(const D3D11_TEXTURE2D_DES

// Add D3D11_BIND_SHADER_RESOURCE flag to all depth-stencil textures so that we can access them in post-processing shaders
D3D11_TEXTURE2D_DESC new_desc = *pDesc;
if (0 != (new_desc.BindFlags & D3D11_BIND_DEPTH_STENCIL))
if (new_desc.SampleDesc.Count == 1 && // Skip MSAA textures
0 != (new_desc.BindFlags & D3D11_BIND_DEPTH_STENCIL))
{
new_desc.Format = make_dxgi_format_typeless(new_desc.Format);
new_desc.BindFlags |= D3D11_BIND_SHADER_RESOURCE;
Expand Down Expand Up @@ -160,15 +161,18 @@ HRESULT STDMETHODCALLTYPE D3D11Device::CreateShaderResourceView(ID3D11Resource *
texture->GetDesc(&texture_desc);

// Only textures with the depth-stencil bind flag where modified, so skip all others
if (0 != (texture_desc.BindFlags & D3D11_BIND_DEPTH_STENCIL))
if (texture_desc.SampleDesc.Count == 1 &&
0 != (texture_desc.BindFlags & D3D11_BIND_DEPTH_STENCIL))
{
new_desc.Format = make_dxgi_format_normal(texture_desc.Format);

if (pDesc == nullptr) // Only need to set the rest of the fields if the application did not pass in a valid description already
{
new_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
new_desc.Texture2D.MipLevels = static_cast<UINT>(-1); // All the mipmap levels from 'MostDetailedMip' on down to least detailed
new_desc.Texture2D.MostDetailedMip = 0;
new_desc.ViewDimension = texture_desc.ArraySize > 1 ? D3D11_SRV_DIMENSION_TEXTURE2DARRAY : D3D11_SRV_DIMENSION_TEXTURE2D;
new_desc.Texture2DArray.MipLevels = static_cast<UINT>(-1); // All the mipmap levels from 'MostDetailedMip' on down to least detailed
new_desc.Texture2DArray.MostDetailedMip = 0;
new_desc.Texture2DArray.ArraySize = texture_desc.ArraySize;
new_desc.Texture2DArray.FirstArraySlice = 0;
}

pDesc = &new_desc;
Expand Down Expand Up @@ -206,15 +210,23 @@ HRESULT STDMETHODCALLTYPE D3D11Device::CreateDepthStencilView(ID3D11Resource *pR
{
texture->GetDesc(&texture_desc);

new_desc.Format = make_dxgi_format_dsv(texture_desc.Format);

if (pDesc == nullptr) // Only need to set the rest of the fields if the application did not pass in a valid description already
// Only non-MSAA textures where modified, so skip all others
if (texture_desc.SampleDesc.Count == 1)
{
new_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
new_desc.Texture2D.MipSlice = 0;
}
assert((texture_desc.BindFlags & D3D11_BIND_DEPTH_STENCIL) != 0);

new_desc.Format = make_dxgi_format_dsv(texture_desc.Format);

pDesc = &new_desc;
if (pDesc == nullptr) // Only need to set the rest of the fields if the application did not pass in a valid description already
{
new_desc.ViewDimension = texture_desc.ArraySize > 1 ? D3D11_DSV_DIMENSION_TEXTURE2DARRAY : D3D11_DSV_DIMENSION_TEXTURE2D;
new_desc.Texture2DArray.MipSlice = 0;
new_desc.Texture2DArray.FirstArraySlice = 0;
new_desc.Texture2DArray.ArraySize = texture_desc.ArraySize;
}

pDesc = &new_desc;
}
}
}

Expand Down Expand Up @@ -489,7 +501,8 @@ HRESULT STDMETHODCALLTYPE D3D11Device::CreateTexture2D1(const D3D11_TEXTURE2D_DE
assert(pDesc1 != nullptr);

D3D11_TEXTURE2D_DESC1 new_desc = *pDesc1;
if (0 != (new_desc.BindFlags & D3D11_BIND_DEPTH_STENCIL))
if (new_desc.SampleDesc.Count == 1 &&
0 != (new_desc.BindFlags & D3D11_BIND_DEPTH_STENCIL))
{
new_desc.Format = make_dxgi_format_typeless(new_desc.Format);
new_desc.BindFlags |= D3D11_BIND_SHADER_RESOURCE;
Expand Down Expand Up @@ -527,16 +540,29 @@ HRESULT STDMETHODCALLTYPE D3D11Device::CreateShaderResourceView1(ID3D11Resource
{
texture->GetDesc(&texture_desc);

if (0 != (texture_desc.BindFlags & D3D11_BIND_DEPTH_STENCIL))
if (texture_desc.SampleDesc.Count == 1 &&
0 != (texture_desc.BindFlags & D3D11_BIND_DEPTH_STENCIL))
{
new_desc.Format = make_dxgi_format_normal(texture_desc.Format);

if (pDesc1 == nullptr)
{
new_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
new_desc.Texture2D.MipLevels = static_cast<UINT>(-1);
new_desc.Texture2D.MostDetailedMip = 0;
new_desc.Texture2D.PlaneSlice = 0;
if (texture_desc.ArraySize > 1)
{
new_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
new_desc.Texture2DArray.MipLevels = static_cast<UINT>(-1);
new_desc.Texture2DArray.MostDetailedMip = 0;
new_desc.Texture2DArray.ArraySize = texture_desc.ArraySize;
new_desc.Texture2DArray.FirstArraySlice = 0;
new_desc.Texture2DArray.PlaneSlice = 0;
}
else
{
new_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
new_desc.Texture2D.MipLevels = static_cast<UINT>(-1);
new_desc.Texture2D.MostDetailedMip = 0;
new_desc.Texture2D.PlaneSlice = 0;
}
}

pDesc1 = &new_desc;
Expand Down

0 comments on commit 20161e0

Please sign in to comment.