Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

D3D11 depth texture support #13556

Merged
merged 3 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions Common/GPU/D3D11/thin3d_d3d11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,8 @@ class D3D11Framebuffer : public Framebuffer {
colorRTView->Release();
if (colorSRView)
colorSRView->Release();
if (depthSRView)
depthSRView->Release();
if (depthStencilTex)
depthStencilTex->Release();
if (depthStencilRTView)
Expand All @@ -1223,6 +1225,7 @@ class D3D11Framebuffer : public Framebuffer {
ID3D11Texture2D *colorTex = nullptr;
ID3D11RenderTargetView *colorRTView = nullptr;
ID3D11ShaderResourceView *colorSRView = nullptr;
ID3D11ShaderResourceView *depthSRView = nullptr;
DXGI_FORMAT colorFormat = DXGI_FORMAT_UNKNOWN;

ID3D11Texture2D *depthStencilTex = nullptr;
Expand Down Expand Up @@ -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);
Expand All @@ -1286,14 +1289,25 @@ 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);
if (FAILED(hr)) {
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;
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion GPU/Common/DepalettizeShaderCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ void GenerateDepalShader300(char *buffer, GEBufferFormat pixelFormat, ShaderLang
WRITE(p, "SamplerState texSamp : register(s0);\n");
WRITE(p, "Texture2D<float4> tex : register(t0);\n");
WRITE(p, "Texture2D<float4> 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");
Expand All @@ -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");
Expand Down
30 changes: 28 additions & 2 deletions GPU/D3D11/TextureCacheD3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand Down Expand Up @@ -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();
Expand All @@ -121,6 +132,8 @@ TextureCacheD3D11::TextureCacheD3D11(Draw::DrawContext *draw)
}

TextureCacheD3D11::~TextureCacheD3D11() {
depalConstants_->Release();

// pFramebufferVertexDecl->Release();
Clear(true);
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions GPU/D3D11/TextureCacheD3D11.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class TextureCacheD3D11 : public TextureCacheCommon {
SamplerCacheD3D11 samplerCache_;

ID3D11ShaderResourceView *lastBoundTexture;
ID3D11Buffer *depalConstants_;

int decimationCounter_;
int texelsScaledThisFrame_;
Expand All @@ -99,6 +100,7 @@ class TextureCacheD3D11 : public TextureCacheCommon {
FramebufferManagerD3D11 *framebufferManagerD3D11_;
DepalShaderCacheD3D11 *depalShaderCache_;
ShaderManagerD3D11 *shaderManager_;

};

DXGI_FORMAT GetClutDestFormatD3D11(GEPaletteFormat format);