Implement copying color to depth on load. However, weird stuff is happening..

This commit is contained in:
Henrik Rydgård 2022-08-17 14:28:34 +02:00
parent 94ade8c751
commit 5d8fe4c2a8
4 changed files with 85 additions and 55 deletions

View file

@ -25,6 +25,7 @@
#include "GPU/Common/DrawEngineCommon.h" #include "GPU/Common/DrawEngineCommon.h"
#include "GPU/Common/FramebufferManagerCommon.h" #include "GPU/Common/FramebufferManagerCommon.h"
#include "GPU/Common/TextureCacheCommon.h" #include "GPU/Common/TextureCacheCommon.h"
#include "GPU/Common/GPUStateUtils.h"
static const InputDef inputs[2] = { static const InputDef inputs[2] = {
{ "vec2", "a_position", Draw::SEM_POSITION }, { "vec2", "a_position", Draw::SEM_POSITION },
@ -58,6 +59,24 @@ RasterChannel GenerateDraw2DDepthFs(ShaderWriter &writer) {
return RASTER_DEPTH; return RASTER_DEPTH;
} }
RasterChannel GenerateDraw2D565ToDepthFs(ShaderWriter &writer) {
writer.DeclareSamplers(samplers);
writer.BeginFSMain(Slice<UniformDef>::empty(), varyings, FSFLAG_WRITEDEPTH);
writer.C(" vec4 outColor = vec4(0.0, 0.0, 0.0, 0.0);\n");
// Unlike when just copying a depth buffer, here we're generating new depth values so we'll
// have to apply the scaling.
DepthScaleFactors factors = GetDepthScaleFactors();
writer.C(" vec3 rgb = ").SampleTexture2D("tex", "v_texcoord.xy").C(".xyz;\n");
writer.F(" highp float depthValue = (floor(rgb.x * 31.99) + floor(rgb.y * 63.99) * 32.0 + floor(rgb.z * 31.99) * 2048.0) / 65535.0; \n");
if (factors.scale != 1.0 || factors.offset != 0.0) {
writer.F(" gl_FragDepth = (depthValue / %f) + %f;\n", factors.scale / 65535.0f, factors.offset);
} else {
writer.C(" gl_FragDepth = depthValue;\n");
}
writer.EndFSMain("outColor", FSFLAG_WRITEDEPTH);
return RASTER_DEPTH;
}
void GenerateDraw2DVS(ShaderWriter &writer) { void GenerateDraw2DVS(ShaderWriter &writer) {
writer.BeginVSMain(inputs, Slice<UniformDef>::empty(), varyings); writer.BeginVSMain(inputs, Slice<UniformDef>::empty(), varyings);
@ -159,22 +178,22 @@ Draw::Pipeline *FramebufferManagerCommon::Create2DPipeline(RasterChannel (*gener
return pipeline; return pipeline;
} }
void FramebufferManagerCommon::DrawStrip2D(Draw::Texture *tex, Draw2DVertex *verts, int vertexCount, bool linearFilter, RasterChannel channel) { void FramebufferManagerCommon::DrawStrip2D(Draw::Texture *tex, Draw2DVertex *verts, int vertexCount, bool linearFilter, Draw2DShader shader) {
using namespace Draw; using namespace Draw;
Ensure2DResources(); Ensure2DResources();
const ShaderLanguageDesc &shaderLanguageDesc = draw_->GetShaderLanguageDesc(); const ShaderLanguageDesc &shaderLanguageDesc = draw_->GetShaderLanguageDesc();
switch (channel) { switch (shader) {
case RASTER_COLOR: case DRAW2D_COPY_COLOR:
if (!draw2DPipelineColor_) { if (!draw2DPipelineColor_) {
draw2DPipelineColor_ = Create2DPipeline(&GenerateDraw2DFs); draw2DPipelineColor_ = Create2DPipeline(&GenerateDraw2DFs);
} }
draw_->BindPipeline(draw2DPipelineColor_); draw_->BindPipeline(draw2DPipelineColor_);
break; break;
case RASTER_DEPTH: case DRAW2D_COPY_DEPTH:
if (!draw_->GetDeviceCaps().fragmentShaderDepthWriteSupported) { if (!draw_->GetDeviceCaps().fragmentShaderDepthWriteSupported) {
// Can't do it // Can't do it
return; return;
@ -184,6 +203,17 @@ void FramebufferManagerCommon::DrawStrip2D(Draw::Texture *tex, Draw2DVertex *ver
} }
draw_->BindPipeline(draw2DPipelineDepth_); draw_->BindPipeline(draw2DPipelineDepth_);
break; break;
case DRAW2D_565_TO_DEPTH:
if (!draw_->GetDeviceCaps().fragmentShaderDepthWriteSupported) {
// Can't do it
return;
}
if (!draw2DPipeline565ToDepth_) {
draw2DPipeline565ToDepth_ = Create2DPipeline(&GenerateDraw2D565ToDepthFs);
}
draw_->BindPipeline(draw2DPipeline565ToDepth_);
break;
} }
if (tex) { if (tex) {

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "GPU/GPU.h"
// For framebuffer copies and similar things that just require passthrough. // For framebuffer copies and similar things that just require passthrough.
struct Draw2DVertex { struct Draw2DVertex {
float x; float x;
@ -7,3 +9,20 @@ struct Draw2DVertex {
float u; float u;
float v; float v;
}; };
enum Draw2DShader {
DRAW2D_COPY_COLOR,
DRAW2D_COPY_DEPTH,
DRAW2D_565_TO_DEPTH,
};
inline RasterChannel Draw2DSourceChannel(Draw2DShader shader) {
switch (shader) {
case DRAW2D_COPY_DEPTH:
return RASTER_DEPTH;
case DRAW2D_COPY_COLOR:
case DRAW2D_565_TO_DEPTH:
default:
return RASTER_COLOR;
}
}

View file

@ -416,9 +416,7 @@ VirtualFramebuffer *FramebufferManagerCommon::DoSetRenderFrameBuffer(const Frame
ResizeFramebufFBO(vfb, drawing_width, drawing_height, true); ResizeFramebufFBO(vfb, drawing_width, drawing_height, true);
NotifyRenderFramebufferCreated(vfb); NotifyRenderFramebufferCreated(vfb);
if (!params.isClearingDepth) { // Note that we do not even think about depth right now.
CopyToDepthFromOverlappingFramebuffers(vfb);
}
SetColorUpdated(vfb, skipDrawReason); SetColorUpdated(vfb, skipDrawReason);
@ -504,16 +502,8 @@ void FramebufferManagerCommon::SetDepthFrameBuffer() {
return; return;
} }
// Looks up by z_address, so if one is found here and not have last pointers equal to this one, // "Resolve" the depth buffer, by copying from any overlapping buffers with fresher content.
// there is another one. CopyToDepthFromOverlappingFramebuffers(currentRenderVfb_);
VirtualFramebuffer *prevDepth = GetLatestDepthBufferAt(currentRenderVfb_->z_address, currentRenderVfb_->z_stride);
if (prevDepth != currentRenderVfb_) {
if (!gstate_c.clearingDepth && prevDepth) {
BlitFramebufferDepth(prevDepth, currentRenderVfb_);
}
prevDepth = currentRenderVfb_;
}
currentRenderVfb_->depthBindSeq = GetBindSeqCount(); currentRenderVfb_->depthBindSeq = GetBindSeqCount();
} }
@ -538,11 +528,10 @@ void FramebufferManagerCommon::CopyToDepthFromOverlappingFramebuffers(VirtualFra
continue; continue;
if (src->fb_address == dest->z_address && src->fb_stride == dest->z_stride && src->format == GE_FORMAT_565) { if (src->fb_address == dest->z_address && src->fb_stride == dest->z_stride && src->format == GE_FORMAT_565) {
if (src->colorBindSeq > dest->depthBindSeq) { if (src->colorBindSeq < dest->depthBindSeq) {
// Source has older data than the current buffer, ignore. // Source has newer data than the current buffer, use it.
continue; sources.push_back(CopySource{ src, RASTER_COLOR });
} }
sources.push_back(CopySource{ src, RASTER_COLOR });
} else if (src->z_address == dest->z_address && src->z_stride == dest->z_stride && src->depthBindSeq > dest->depthBindSeq) { } else if (src->z_address == dest->z_address && src->z_stride == dest->z_stride && src->depthBindSeq > dest->depthBindSeq) {
sources.push_back(CopySource{ src, RASTER_DEPTH }); sources.push_back(CopySource{ src, RASTER_DEPTH });
} else { } else {
@ -550,16 +539,25 @@ void FramebufferManagerCommon::CopyToDepthFromOverlappingFramebuffers(VirtualFra
} }
} }
// TODO: A full depth copy will overwrite anything else. So we can eliminate std::sort(sources.begin(), sources.end());
// TODO: A full copy will overwrite anything else. So we can eliminate
// anything that comes before such a copy. // anything that comes before such a copy.
for (auto &source : sources) { // For now, let's just do the last thing, if there are multiple.
// for (auto &source : sources) {
if (sources.size()) {
auto &source = sources.back();
if (source.channel == RASTER_DEPTH) { if (source.channel == RASTER_DEPTH) {
// Good old depth->depth copy. // Good old depth->depth copy.
BlitFramebufferDepth(source.vfb, dest); BlitFramebufferDepth(source.vfb, dest);
gpuStats.numDepthCopies++; gpuStats.numDepthCopies++;
dest->last_frame_depth_updated = gpuStats.numFlips; dest->last_frame_depth_updated = gpuStats.numFlips;
} else if (source.channel == RASTER_COLOR) {
VirtualFramebuffer *src = source.vfb;
// Copying color to depth.
BlitUsingRaster(src->fbo, 0.0f, 0.0f, src->renderWidth, src->renderHeight, dest->fbo, 0.0f, 0.0f, dest->renderWidth, dest->renderHeight, false, DRAW2D_565_TO_DEPTH, "565_to_depth");
} }
} }
@ -636,24 +634,12 @@ void FramebufferManagerCommon::BlitFramebufferDepth(VirtualFramebuffer *src, Vir
draw_->BlitFramebuffer(src->fbo, 0, 0, w, h, dst->fbo, 0, 0, w, h, Draw::FB_DEPTH_BIT, Draw::FB_BLIT_NEAREST, "BlitFramebufferDepth"); draw_->BlitFramebuffer(src->fbo, 0, 0, w, h, dst->fbo, 0, 0, w, h, Draw::FB_DEPTH_BIT, Draw::FB_BLIT_NEAREST, "BlitFramebufferDepth");
RebindFramebuffer("After BlitFramebufferDepth"); RebindFramebuffer("After BlitFramebufferDepth");
} else if (useRaster) { } else if (useRaster) {
BlitUsingRaster(src->fbo, 0, 0, w, h, dst->fbo, 0, 0, w, h, false, RasterChannel::RASTER_DEPTH); BlitUsingRaster(src->fbo, 0, 0, w, h, dst->fbo, 0, 0, w, h, false, Draw2DShader::DRAW2D_COPY_DEPTH, "BlitDepthRaster");
} }
draw_->InvalidateCachedState(); draw_->InvalidateCachedState();
} }
VirtualFramebuffer *FramebufferManagerCommon::GetLatestDepthBufferAt(u32 z_address, u16 z_stride) {
int maxSeq = -1;
VirtualFramebuffer *latestDepth = nullptr;
for (auto vfb : vfbs_) {
if (vfb->z_address == z_address && vfb->z_stride == z_stride && vfb->depthBindSeq > maxSeq) {
maxSeq = vfb->depthBindSeq;
latestDepth = vfb;
}
}
return latestDepth;
}
void FramebufferManagerCommon::NotifyRenderFramebufferCreated(VirtualFramebuffer *vfb) { void FramebufferManagerCommon::NotifyRenderFramebufferCreated(VirtualFramebuffer *vfb) {
if (!useBufferedRendering_) { if (!useBufferedRendering_) {
// Let's ignore rendering to targets that have not (yet) been displayed. // Let's ignore rendering to targets that have not (yet) been displayed.
@ -702,12 +688,6 @@ void FramebufferManagerCommon::NotifyRenderFramebufferSwitched(VirtualFramebuffe
textureCache_->ForgetLastTexture(); textureCache_->ForgetLastTexture();
shaderManager_->DirtyLastShader(); shaderManager_->DirtyLastShader();
if (!isClearingDepth) {
// TODO: We should do this as part of the bind below. Then we can optimize the RPAction properly,
// and also do the copies using raster.
CopyToDepthFromOverlappingFramebuffers(vfb);
}
if (vfb->drawnFormat != vfb->format) { if (vfb->drawnFormat != vfb->format) {
ReinterpretFramebuffer(vfb, vfb->drawnFormat, vfb->format); ReinterpretFramebuffer(vfb, vfb->drawnFormat, vfb->format);
} }
@ -2355,6 +2335,7 @@ void FramebufferManagerCommon::DeviceLost() {
DoRelease(draw2DVs_); DoRelease(draw2DVs_);
DoRelease(draw2DPipelineColor_); DoRelease(draw2DPipelineColor_);
DoRelease(draw2DPipelineDepth_); DoRelease(draw2DPipelineDepth_);
DoRelease(draw2DPipeline565ToDepth_);
draw_ = nullptr; draw_ = nullptr;
} }
@ -2412,7 +2393,7 @@ void FramebufferManagerCommon::DrawActiveTexture(float x, float y, float w, floa
// Rearrange to strip form. // Rearrange to strip form.
std::swap(coord[2], coord[3]); std::swap(coord[2], coord[3]);
DrawStrip2D(nullptr, coord, 4, (flags & DRAWTEX_LINEAR) != 0, RASTER_COLOR); DrawStrip2D(nullptr, coord, 4, (flags & DRAWTEX_LINEAR) != 0, DRAW2D_COPY_COLOR);
gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS | DIRTY_VERTEXSHADER_STATE | DIRTY_FRAGMENTSHADER_STATE); gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS | DIRTY_VERTEXSHADER_STATE | DIRTY_FRAGMENTSHADER_STATE);
} }
@ -2508,13 +2489,14 @@ void FramebufferManagerCommon::BlitFramebuffer(VirtualFramebuffer *dst, int dstX
draw_->BlitFramebuffer(src->fbo, srcX1, srcY1, srcX2, srcY2, dst->fbo, dstX1, dstY1, dstX2, dstY2, draw_->BlitFramebuffer(src->fbo, srcX1, srcY1, srcX2, srcY2, dst->fbo, dstX1, dstY1, dstX2, dstY2,
channel == RASTER_COLOR ? Draw::FB_COLOR_BIT : Draw::FB_DEPTH_BIT, Draw::FB_BLIT_NEAREST, tag); channel == RASTER_COLOR ? Draw::FB_COLOR_BIT : Draw::FB_DEPTH_BIT, Draw::FB_BLIT_NEAREST, tag);
} else { } else {
Draw2DShader shader = channel == RASTER_COLOR ? DRAW2D_COPY_COLOR : DRAW2D_COPY_DEPTH;
Draw::Framebuffer *srcFBO = src->fbo; Draw::Framebuffer *srcFBO = src->fbo;
if (src == dst) { if (src == dst) {
Draw::Framebuffer *tempFBO = GetTempFBO(TempFBO::BLIT, src->renderWidth, src->renderHeight); Draw::Framebuffer *tempFBO = GetTempFBO(TempFBO::BLIT, src->renderWidth, src->renderHeight);
BlitUsingRaster(src->fbo, srcX1, srcY1, srcX2, srcY2, tempFBO, dstX1, dstY1, dstX2, dstY2, false, channel); BlitUsingRaster(src->fbo, srcX1, srcY1, srcX2, srcY2, tempFBO, dstX1, dstY1, dstX2, dstY2, false, shader, tag);
srcFBO = tempFBO; srcFBO = tempFBO;
} }
BlitUsingRaster(srcFBO, srcX1, srcY1, srcX2, srcY2, dst->fbo, dstX1, dstY1, dstX2, dstY2, false, channel); BlitUsingRaster(srcFBO, srcX1, srcY1, srcX2, srcY2, dst->fbo, dstX1, dstY1, dstX2, dstY2, false, shader, tag);
} }
draw_->InvalidateCachedState(); draw_->InvalidateCachedState();
@ -2526,9 +2508,9 @@ void FramebufferManagerCommon::BlitUsingRaster(
Draw::Framebuffer *src, float srcX1, float srcY1, float srcX2, float srcY2, Draw::Framebuffer *src, float srcX1, float srcY1, float srcX2, float srcY2,
Draw::Framebuffer *dest, float destX1, float destY1, float destX2, float destY2, Draw::Framebuffer *dest, float destX1, float destY1, float destX2, float destY2,
bool linearFilter, bool linearFilter,
RasterChannel channel) { Draw2DShader shader, const char *tag) {
if (channel == RASTER_DEPTH) { if (shader == DRAW2D_COPY_DEPTH || shader == DRAW2D_565_TO_DEPTH) {
_dbg_assert_(draw_->GetDeviceCaps().fragmentShaderDepthWriteSupported); _dbg_assert_(draw_->GetDeviceCaps().fragmentShaderDepthWriteSupported);
} }
@ -2550,13 +2532,13 @@ void FramebufferManagerCommon::BlitUsingRaster(
// Unbind the texture first to avoid the D3D11 hazard check (can't set render target to things bound as textures and vice versa, not even temporarily). // Unbind the texture first to avoid the D3D11 hazard check (can't set render target to things bound as textures and vice versa, not even temporarily).
draw_->BindTexture(0, nullptr); draw_->BindTexture(0, nullptr);
// This will get optimized away in case it's already bound (in VK and GL at least..) // This will get optimized away in case it's already bound (in VK and GL at least..)
draw_->BindFramebufferAsRenderTarget(dest, { Draw::RPAction::KEEP, Draw::RPAction::KEEP, Draw::RPAction::KEEP }, "BlitUsingRaster"); draw_->BindFramebufferAsRenderTarget(dest, { Draw::RPAction::KEEP, Draw::RPAction::KEEP, Draw::RPAction::KEEP }, tag ? tag : "BlitUsingRaster");
draw_->BindFramebufferAsTexture(src, 0, channel == RASTER_COLOR ? Draw::FB_COLOR_BIT : Draw::FB_DEPTH_BIT, 0); draw_->BindFramebufferAsTexture(src, 0, Draw2DSourceChannel(shader) == RASTER_COLOR ? Draw::FB_COLOR_BIT : Draw::FB_DEPTH_BIT, 0);
Draw::Viewport vp{ 0.0f, 0.0f, (float)dest->Width(), (float)dest->Height(), 0.0f, 1.0f }; Draw::Viewport vp{ 0.0f, 0.0f, (float)dest->Width(), (float)dest->Height(), 0.0f, 1.0f };
draw_->SetViewports(1, &vp); draw_->SetViewports(1, &vp);
draw_->SetScissorRect(0, 0, (int)dest->Width(), (int)dest->Height()); draw_->SetScissorRect(0, 0, (int)dest->Width(), (int)dest->Height());
DrawStrip2D(nullptr, vtx, 4, linearFilter, channel); DrawStrip2D(nullptr, vtx, 4, linearFilter, shader);
gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_RASTER_STATE | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_VERTEXSHADER_STATE | DIRTY_FRAGMENTSHADER_STATE); gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_RASTER_STATE | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_VERTEXSHADER_STATE | DIRTY_FRAGMENTSHADER_STATE);
} }

View file

@ -290,8 +290,6 @@ public:
void DownloadFramebufferForClut(u32 fb_address, u32 loadBytes); void DownloadFramebufferForClut(u32 fb_address, u32 loadBytes);
void DrawFramebufferToOutput(const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride); void DrawFramebufferToOutput(const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride);
VirtualFramebuffer *GetLatestDepthBufferAt(u32 z_address, u16 z_stride);
void DrawPixels(VirtualFramebuffer *vfb, int dstX, int dstY, const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride, int width, int height); void DrawPixels(VirtualFramebuffer *vfb, int dstX, int dstY, const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride, int width, int height);
size_t NumVFBs() const { return vfbs_.size(); } size_t NumVFBs() const { return vfbs_.size(); }
@ -382,7 +380,7 @@ protected:
Draw::Texture *MakePixelTexture(const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride, int width, int height); Draw::Texture *MakePixelTexture(const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride, int width, int height);
void DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags); void DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags);
void DrawStrip2D(Draw::Texture *tex, Draw2DVertex *verts, int vertexCount, bool linearFilter, RasterChannel channel); void DrawStrip2D(Draw::Texture *tex, Draw2DVertex *verts, int vertexCount, bool linearFilter, Draw2DShader channel);
void Ensure2DResources(); void Ensure2DResources();
Draw::Pipeline *Create2DPipeline(RasterChannel (*generate)(ShaderWriter &)); Draw::Pipeline *Create2DPipeline(RasterChannel (*generate)(ShaderWriter &));
@ -398,7 +396,7 @@ protected:
void BlitUsingRaster( void BlitUsingRaster(
Draw::Framebuffer *src, float srcX1, float srcY1, float srcX2, float srcY2, Draw::Framebuffer *src, float srcX1, float srcY1, float srcX2, float srcY2,
Draw::Framebuffer *dest, float destX1, float destY1, float destX2, float destY2, bool linearFilter, RasterChannel channel); Draw::Framebuffer *dest, float destX1, float destY1, float destX2, float destY2, bool linearFilter, Draw2DShader shader, const char *tag);
void CopyFramebufferForColorTexture(VirtualFramebuffer *dst, VirtualFramebuffer *src, int flags); void CopyFramebufferForColorTexture(VirtualFramebuffer *dst, VirtualFramebuffer *src, int flags);
@ -517,6 +515,7 @@ protected:
// Draw2D pipelines // Draw2D pipelines
Draw::Pipeline *draw2DPipelineColor_ = nullptr; Draw::Pipeline *draw2DPipelineColor_ = nullptr;
Draw::Pipeline *draw2DPipelineDepth_ = nullptr; Draw::Pipeline *draw2DPipelineDepth_ = nullptr;
Draw::Pipeline *draw2DPipeline565ToDepth_ = nullptr;
Draw::SamplerState *draw2DSamplerLinear_ = nullptr; Draw::SamplerState *draw2DSamplerLinear_ = nullptr;
Draw::SamplerState *draw2DSamplerNearest_ = nullptr; Draw::SamplerState *draw2DSamplerNearest_ = nullptr;
Draw::ShaderModule *draw2DVs_ = nullptr; Draw::ShaderModule *draw2DVs_ = nullptr;