diff --git a/Common/GPU/D3D11/thin3d_d3d11.cpp b/Common/GPU/D3D11/thin3d_d3d11.cpp index 263fa088e8..3827b79052 100644 --- a/Common/GPU/D3D11/thin3d_d3d11.cpp +++ b/Common/GPU/D3D11/thin3d_d3d11.cpp @@ -1212,6 +1212,8 @@ public: colorRTView->Release(); if (colorSRView) colorSRView->Release(); + if (depthSRView) + depthSRView->Release(); if (depthStencilTex) depthStencilTex->Release(); if (depthStencilRTView) @@ -1223,6 +1225,7 @@ public: ID3D11Texture2D *colorTex = nullptr; ID3D11RenderTargetView *colorRTView = nullptr; ID3D11ShaderResourceView *colorSRView = nullptr; + ID3D11ShaderResourceView *depthSRView = nullptr; DXGI_FORMAT colorFormat = DXGI_FORMAT_UNKNOWN; ID3D11Texture2D *depthStencilTex = nullptr; @@ -1273,11 +1276,11 @@ Framebuffer *D3D11DrawContext::CreateFramebuffer(const FramebufferDesc &desc) { descDepth.Height = desc.height; descDepth.MipLevels = 1; descDepth.ArraySize = 1; - descDepth.Format = fb->depthStencilFormat; + descDepth.Format = DXGI_FORMAT_R24G8_TYPELESS; // so we can create an R24X8 view of it. descDepth.SampleDesc.Count = 1; descDepth.SampleDesc.Quality = 0; descDepth.Usage = D3D11_USAGE_DEFAULT; - descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL; + descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE; descDepth.CPUAccessFlags = 0; descDepth.MiscFlags = 0; hr = device_->CreateTexture2D(&descDepth, nullptr, &fb->depthStencilTex); @@ -1286,7 +1289,7 @@ Framebuffer *D3D11DrawContext::CreateFramebuffer(const FramebufferDesc &desc) { return nullptr; } D3D11_DEPTH_STENCIL_VIEW_DESC descDSV{}; - descDSV.Format = descDepth.Format; + descDSV.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; descDSV.Texture2D.MipSlice = 0; hr = device_->CreateDepthStencilView(fb->depthStencilTex, &descDSV, &fb->depthStencilRTView); @@ -1294,6 +1297,17 @@ Framebuffer *D3D11DrawContext::CreateFramebuffer(const FramebufferDesc &desc) { delete fb; return nullptr; } + + D3D11_SHADER_RESOURCE_VIEW_DESC depthViewDesc{}; + depthViewDesc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS; + depthViewDesc.Texture2D.MostDetailedMip = 0; + depthViewDesc.Texture2D.MipLevels = 1; + depthViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; + hr = device_->CreateShaderResourceView(fb->depthStencilTex, &depthViewDesc, &fb->depthSRView); + if (FAILED(hr)) { + WARN_LOG(G3D, "Failed to create SRV for depth buffer."); + fb->depthSRView = nullptr; + } } return fb; @@ -1587,7 +1601,18 @@ void D3D11DrawContext::BindFramebufferAsRenderTarget(Framebuffer *fbo, const Ren // color must be 0, for now. void D3D11DrawContext::BindFramebufferAsTexture(Framebuffer *fbo, int binding, FBChannel channelBit, int attachment) { D3D11Framebuffer *fb = (D3D11Framebuffer *)fbo; - context_->PSSetShaderResources(binding, 1, &fb->colorSRView); + switch (channelBit) { + case FBChannel::FB_COLOR_BIT: + context_->PSSetShaderResources(binding, 1, &fb->colorSRView); + break; + case FBChannel::FB_DEPTH_BIT: + if (fb->depthSRView) { + context_->PSSetShaderResources(binding, 1, &fb->depthSRView); + } + break; + default: + break; + } } uintptr_t D3D11DrawContext::GetFramebufferAPITexture(Framebuffer *fbo, int channelBit, int attachment) { diff --git a/GPU/Common/DepalettizeShaderCommon.cpp b/GPU/Common/DepalettizeShaderCommon.cpp index 23b662c672..28449d69b9 100644 --- a/GPU/Common/DepalettizeShaderCommon.cpp +++ b/GPU/Common/DepalettizeShaderCommon.cpp @@ -38,6 +38,12 @@ void GenerateDepalShader300(char *buffer, GEBufferFormat pixelFormat, ShaderLang WRITE(p, "SamplerState texSamp : register(s0);\n"); WRITE(p, "Texture2D tex : register(t0);\n"); WRITE(p, "Texture2D pal : register(t3);\n"); + // Support for depth. + if (pixelFormat == GE_FORMAT_DEPTH16) { + WRITE(p, "cbuffer params : register(b0) {\n"); + WRITE(p, " float z_scale; float z_offset;\n"); + WRITE(p, "};\n"); + } } else if (language == GLSL_VULKAN) { WRITE(p, "#version 450\n"); WRITE(p, "#extension GL_ARB_separate_shader_objects : enable\n"); @@ -53,7 +59,6 @@ void GenerateDepalShader300(char *buffer, GEBufferFormat pixelFormat, ShaderLang WRITE(p, " float z_scale; float z_offset;\n"); WRITE(p, "};\n"); } - } else { if (gl_extensions.IsGLES) { WRITE(p, "#version 300 es\n"); diff --git a/GPU/D3D11/TextureCacheD3D11.cpp b/GPU/D3D11/TextureCacheD3D11.cpp index c04b353aa7..f7f7582ec3 100644 --- a/GPU/D3D11/TextureCacheD3D11.cpp +++ b/GPU/D3D11/TextureCacheD3D11.cpp @@ -25,6 +25,7 @@ #include "Core/Reporting.h" #include "GPU/ge_constants.h" #include "GPU/GPUState.h" +#include "GPU/Common/GPUStateUtils.h" #include "GPU/D3D11/FragmentShaderGeneratorD3D11.h" #include "GPU/D3D11/TextureCacheD3D11.h" #include "GPU/D3D11/FramebufferManagerD3D11.h" @@ -39,6 +40,12 @@ #include "ext/xxhash.h" #include "Common/Math/math_util.h" +// For depth depal +struct DepthPushConstants { + float z_scale; + float z_offset; + float pad[2]; +}; #define INVALID_TEX (ID3D11ShaderResourceView *)(-1LL) @@ -113,6 +120,10 @@ TextureCacheD3D11::TextureCacheD3D11(Draw::DrawContext *draw) isBgraBackend_ = true; lastBoundTexture = INVALID_TEX; + D3D11_BUFFER_DESC desc{ sizeof(DepthPushConstants), D3D11_USAGE_DYNAMIC, D3D11_BIND_CONSTANT_BUFFER, D3D11_CPU_ACCESS_WRITE }; + HRESULT hr = device_->CreateBuffer(&desc, nullptr, &depalConstants_); + _dbg_assert_(SUCCEEDED(hr)); + HRESULT result = 0; SetupTextureDecoder(); @@ -121,6 +132,8 @@ TextureCacheD3D11::TextureCacheD3D11(Draw::DrawContext *draw) } TextureCacheD3D11::~TextureCacheD3D11() { + depalConstants_->Release(); + // pFramebufferVertexDecl->Release(); Clear(true); } @@ -355,8 +368,9 @@ void TextureCacheD3D11::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer, ID3D11PixelShader *pshader = nullptr; uint32_t clutMode = gstate.clutformat & 0xFFFFFF; bool need_depalettize = IsClutFormat(texFormat); + bool depth = channel == NOTIFY_FB_DEPTH; if (need_depalettize && !g_Config.bDisableSlowFramebufEffects) { - pshader = depalShaderCache_->GetDepalettizePixelShader(clutMode, framebuffer->drawnFormat); + pshader = depalShaderCache_->GetDepalettizePixelShader(clutMode, depth ? GE_FORMAT_DEPTH16 : framebuffer->drawnFormat); } if (pshader) { @@ -382,8 +396,20 @@ void TextureCacheD3D11::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer, draw_->BindFramebufferAsRenderTarget(depalFBO, { Draw::RPAction::DONT_CARE, Draw::RPAction::DONT_CARE, Draw::RPAction::DONT_CARE }, "ApplyTextureFramebuffer_DepalShader"); context_->PSSetShaderResources(3, 1, &clutTexture); context_->PSSetSamplers(3, 1, &stockD3D11.samplerPoint2DWrap); - framebufferManagerD3D11_->BindFramebufferAsColorTexture(0, framebuffer, BINDFBCOLOR_SKIP_COPY | BINDFBCOLOR_FORCE_SELF); + draw_->BindFramebufferAsTexture(framebuffer->fbo, 0, depth ? Draw::FB_DEPTH_BIT : Draw::FB_COLOR_BIT, 0); context_->PSSetSamplers(0, 1, &stockD3D11.samplerPoint2DWrap); + + if (depth) { + DepthScaleFactors scaleFactors = GetDepthScaleFactors(); + DepthPushConstants push; + push.z_scale = scaleFactors.scale; + push.z_offset = scaleFactors.offset; + D3D11_MAPPED_SUBRESOURCE map; + context_->Map(depalConstants_, 0, D3D11_MAP_WRITE_DISCARD, 0, &map); + memcpy(map.pData, &push, sizeof(push)); + context_->Unmap(depalConstants_, 0); + context_->PSSetConstantBuffers(0, 1, &depalConstants_); + } shaderApply.Shade(); context_->PSSetShaderResources(0, 1, &nullTexture); // Make D3D11 validation happy. Really of no consequence since we rebind anyway. diff --git a/GPU/D3D11/TextureCacheD3D11.h b/GPU/D3D11/TextureCacheD3D11.h index ceaf64b150..d29bf8fd16 100644 --- a/GPU/D3D11/TextureCacheD3D11.h +++ b/GPU/D3D11/TextureCacheD3D11.h @@ -91,6 +91,7 @@ private: SamplerCacheD3D11 samplerCache_; ID3D11ShaderResourceView *lastBoundTexture; + ID3D11Buffer *depalConstants_; int decimationCounter_; int texelsScaledThisFrame_; @@ -99,6 +100,7 @@ private: FramebufferManagerD3D11 *framebufferManagerD3D11_; DepalShaderCacheD3D11 *depalShaderCache_; ShaderManagerD3D11 *shaderManager_; + }; DXGI_FORMAT GetClutDestFormatD3D11(GEPaletteFormat format);