From 4e3a9bc3e3a22b84cc65183560a9cfd130953608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 18 Oct 2017 11:20:58 +0200 Subject: [PATCH] Unify some more framebuffer functions. Leave the GL stuff alone for now due to async. --- GPU/Common/FramebufferCommon.cpp | 42 +++++++++++++++++++ GPU/Common/FramebufferCommon.h | 18 ++++++++- GPU/D3D11/FramebufferManagerD3D11.cpp | 58 --------------------------- GPU/D3D11/FramebufferManagerD3D11.h | 3 -- GPU/Directx9/FramebufferDX9.cpp | 18 +-------- GPU/Directx9/FramebufferDX9.h | 3 +- GPU/GLES/FramebufferManagerGLES.cpp | 4 +- GPU/GLES/FramebufferManagerGLES.h | 2 +- GPU/Vulkan/FramebufferVulkan.cpp | 33 --------------- GPU/Vulkan/FramebufferVulkan.h | 5 --- 10 files changed, 65 insertions(+), 121 deletions(-) diff --git a/GPU/Common/FramebufferCommon.cpp b/GPU/Common/FramebufferCommon.cpp index 1f69f6b1f7..ac8cd42e35 100644 --- a/GPU/Common/FramebufferCommon.cpp +++ b/GPU/Common/FramebufferCommon.cpp @@ -1934,3 +1934,45 @@ bool FramebufferManagerCommon::GetOutputFramebuffer(GPUDebugBuffer &buffer) { return true; } +// This function takes an already correctly-sized framebuffer and packs it into RAM. +// Does not need to account for scaling. +// Color conversion is currently done on CPU but should be done on GPU. +void FramebufferManagerCommon::PackFramebufferSync_(VirtualFramebuffer *vfb, int x, int y, int w, int h) { + if (!vfb->fbo) { + ERROR_LOG_REPORT_ONCE(vfbfbozero, SCEGE, "PackFramebufferD3D11_: vfb->fbo == 0"); + return; + } + + const u32 fb_address = (0x04000000) | vfb->fb_address; + + Draw::DataFormat destFormat = GEFormatToThin3D(vfb->format); + const int dstBpp = (int)DataFormatSizeInBytes(destFormat); + + const int dstByteOffset = (y * vfb->fb_stride + x) * dstBpp; + u8 *destPtr = Memory::GetPointer(fb_address + dstByteOffset); + + // We always need to convert from the framebuffer native format. + // Right now that's always 8888. + DEBUG_LOG(G3D, "Reading framebuffer to mem, fb_address = %08x", fb_address); + + draw_->CopyFramebufferToMemorySync(vfb->fbo, Draw::FB_COLOR_BIT, x, y, w, h, destFormat, destPtr, vfb->fb_stride); +} + +void FramebufferManagerCommon::ReadFramebufferToMemory(VirtualFramebuffer *vfb, bool sync, int x, int y, int w, int h) { + if (vfb) { + // We'll pseudo-blit framebuffers here to get a resized version of vfb. + OptimizeDownloadRange(vfb, x, y, w, h); + if (vfb->renderWidth == vfb->width && vfb->renderHeight == vfb->height) { + // No need to blit + PackFramebufferSync_(vfb, x, y, w, h); + } else { + VirtualFramebuffer *nvfb = FindDownloadTempBuffer(vfb); + BlitFramebuffer(nvfb, x, y, vfb, x, y, w, h, 0); + PackFramebufferSync_(nvfb, x, y, w, h); + } + + textureCache_->ForgetLastTexture(); + RebindFramebuffer(); + } +} + diff --git a/GPU/Common/FramebufferCommon.h b/GPU/Common/FramebufferCommon.h index 6de1b6dbf7..600407f1c8 100644 --- a/GPU/Common/FramebufferCommon.h +++ b/GPU/Common/FramebufferCommon.h @@ -159,6 +159,21 @@ enum DrawTextureFlags { DRAWTEX_KEEP_TEX = 2, }; +inline Draw::DataFormat GEFormatToThin3D(int geFormat) { + switch (geFormat) { + case GE_FORMAT_4444: + return Draw::DataFormat::A4R4G4B4_UNORM_PACK16; + case GE_FORMAT_5551: + return Draw::DataFormat::A1R5G5B5_UNORM_PACK16; + case GE_FORMAT_565: + return Draw::DataFormat::R5G6B5_UNORM_PACK16; + case GE_FORMAT_8888: + return Draw::DataFormat::R8G8B8A8_UNORM; + default: + return Draw::DataFormat::UNDEFINED; + } +} + namespace Draw { class DrawContext; } @@ -210,7 +225,7 @@ public: bool NotifyBlockTransferBefore(u32 dstBasePtr, int dstStride, int dstX, int dstY, u32 srcBasePtr, int srcStride, int srcX, int srcY, int w, int h, int bpp, u32 skipDrawReason); void NotifyBlockTransferAfter(u32 dstBasePtr, int dstStride, int dstX, int dstY, u32 srcBasePtr, int srcStride, int srcX, int srcY, int w, int h, int bpp, u32 skipDrawReason); - virtual void ReadFramebufferToMemory(VirtualFramebuffer *vfb, bool sync, int x, int y, int w, int h) = 0; + virtual void ReadFramebufferToMemory(VirtualFramebuffer *vfb, bool sync, int x, int y, int w, int h); virtual void DownloadFramebufferForClut(u32 fb_address, u32 loadBytes) = 0; void DrawFramebufferToOutput(const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride, bool applyPostShader); @@ -284,6 +299,7 @@ public: virtual bool GetOutputFramebuffer(GPUDebugBuffer &buffer); protected: + virtual void PackFramebufferSync_(VirtualFramebuffer *vfb, int x, int y, int w, int h); virtual void SetViewport2D(int x, int y, int w, int h); void CalculatePostShaderUniforms(int bufferWidth, int bufferHeight, int renderWidth, int renderHeight, PostShaderUniforms *uniforms); virtual void MakePixelTexture(const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride, int width, int height, float &u1, float &v1) = 0; diff --git a/GPU/D3D11/FramebufferManagerD3D11.cpp b/GPU/D3D11/FramebufferManagerD3D11.cpp index 546885ee4f..3b47638f02 100644 --- a/GPU/D3D11/FramebufferManagerD3D11.cpp +++ b/GPU/D3D11/FramebufferManagerD3D11.cpp @@ -580,25 +580,6 @@ void FramebufferManagerD3D11::BindFramebufferAsColorTexture(int stage, VirtualFr } } -void FramebufferManagerD3D11::ReadFramebufferToMemory(VirtualFramebuffer *vfb, bool sync, int x, int y, int w, int h) { - if (vfb) { - // We'll pseudo-blit framebuffers here to get a resized version of vfb. - OptimizeDownloadRange(vfb, x, y, w, h); - if (vfb->renderWidth == vfb->width && vfb->renderHeight == vfb->height) { - // No need to blit - PackFramebufferSync_(vfb, x, y, w, h); - } - else { - VirtualFramebuffer *nvfb = FindDownloadTempBuffer(vfb); - BlitFramebuffer(nvfb, x, y, vfb, x, y, w, h, 0); - PackFramebufferSync_(nvfb, x, y, w, h); - } - - textureCacheD3D11_->ForgetLastTexture(); - RebindFramebuffer(); - } -} - void FramebufferManagerD3D11::DownloadFramebufferForClut(u32 fb_address, u32 loadBytes) { VirtualFramebuffer *vfb = GetVFBAt(fb_address); if (vfb && vfb->fb_stride != 0) { @@ -749,45 +730,6 @@ void FramebufferManagerD3D11::BlitFramebuffer(VirtualFramebuffer *dst, int dstX, false); } -static Draw::DataFormat GEFormatToThin3D(int geFormat) { - switch (geFormat) { - case GE_FORMAT_4444: - return Draw::DataFormat::A4R4G4B4_UNORM_PACK16; - case GE_FORMAT_5551: - return Draw::DataFormat::A1R5G5B5_UNORM_PACK16; - case GE_FORMAT_565: - return Draw::DataFormat::R5G6B5_UNORM_PACK16; - case GE_FORMAT_8888: - return Draw::DataFormat::R8G8B8A8_UNORM; - default: - return Draw::DataFormat::UNDEFINED; - } -} - -// This function takes an already correctly-sized framebuffer and packs it into RAM. -// Does not need to account for scaling. -// Color conversion is currently done on CPU but should be done on GPU. -void FramebufferManagerD3D11::PackFramebufferSync_(VirtualFramebuffer *vfb, int x, int y, int w, int h) { - if (!vfb->fbo) { - ERROR_LOG_REPORT_ONCE(vfbfbozero, SCEGE, "PackFramebufferD3D11_: vfb->fbo == 0"); - return; - } - - const u32 fb_address = (0x04000000) | vfb->fb_address; - - Draw::DataFormat destFormat = GEFormatToThin3D(vfb->format); - const int dstBpp = (int)DataFormatSizeInBytes(destFormat); - - const int dstByteOffset = (y * vfb->fb_stride + x) * dstBpp; - u8 *destPtr = Memory::GetPointer(fb_address + dstByteOffset); - - // We always need to convert from the framebuffer native format. - // Right now that's always 8888. - DEBUG_LOG(G3D, "Reading framebuffer to mem, fb_address = %08x", fb_address); - - draw_->CopyFramebufferToMemorySync(vfb->fbo, Draw::FB_COLOR_BIT, x, y, w, h, destFormat, destPtr, vfb->fb_stride); -} - // Nobody calls this yet. void FramebufferManagerD3D11::PackDepthbuffer(VirtualFramebuffer *vfb, int x, int y, int w, int h) { if (!vfb->fbo) { diff --git a/GPU/D3D11/FramebufferManagerD3D11.h b/GPU/D3D11/FramebufferManagerD3D11.h index cf0798dbf5..cdd2093fc7 100644 --- a/GPU/D3D11/FramebufferManagerD3D11.h +++ b/GPU/D3D11/FramebufferManagerD3D11.h @@ -60,7 +60,6 @@ public: void BindFramebufferAsColorTexture(int stage, VirtualFramebuffer *framebuffer, int flags); - void ReadFramebufferToMemory(VirtualFramebuffer *vfb, bool sync, int x, int y, int w, int h) override; void DownloadFramebufferForClut(u32 fb_address, u32 loadBytes) override; std::vector GetFramebufferList(); @@ -89,13 +88,11 @@ private: void BindPostShader(const PostShaderUniforms &uniforms) override; void Bind2DShader() override; void MakePixelTexture(const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride, int width, int height, float &u1, float &v1) override; - void PackFramebufferSync_(VirtualFramebuffer *vfb, int x, int y, int w, int h); void PackDepthbuffer(VirtualFramebuffer *vfb, int x, int y, int w, int h); void SimpleBlit( Draw::Framebuffer *dest, float destX1, float destY1, float destX2, float destY2, Draw::Framebuffer *src, float srcX1, float srcY1, float srcX2, float srcY2, bool linearFilter); - bool GetDepthStencilBuffer(VirtualFramebuffer *vfb, GPUDebugBuffer &buffer, bool stencil); ID3D11Device *device_; ID3D11DeviceContext *context_; diff --git a/GPU/Directx9/FramebufferDX9.cpp b/GPU/Directx9/FramebufferDX9.cpp index d4e4aa1492..8cbbdb9cad 100644 --- a/GPU/Directx9/FramebufferDX9.cpp +++ b/GPU/Directx9/FramebufferDX9.cpp @@ -473,20 +473,6 @@ static const D3DVERTEXELEMENT9 g_FramebufferVertexElements[] = { } } - void FramebufferManagerDX9::ReadFramebufferToMemory(VirtualFramebuffer *vfb, bool sync, int x, int y, int w, int h) { - if (vfb) { - // We'll pseudo-blit framebuffers here to get a resized version of vfb. - VirtualFramebuffer *nvfb = FindDownloadTempBuffer(vfb); - OptimizeDownloadRange(vfb, x, y, w, h); - BlitFramebuffer(nvfb, x, y, vfb, x, y, w, h, 0); - - PackFramebufferDirectx9_(nvfb, x, y, w, h); - - textureCacheDX9_->ForgetLastTexture(); - RebindFramebuffer(); - } - } - void FramebufferManagerDX9::DownloadFramebufferForClut(u32 fb_address, u32 loadBytes) { VirtualFramebuffer *vfb = GetVFBAt(fb_address); if (vfb && vfb->fb_stride != 0) { @@ -514,7 +500,7 @@ static const D3DVERTEXELEMENT9 g_FramebufferVertexElements[] = { VirtualFramebuffer *nvfb = FindDownloadTempBuffer(vfb); BlitFramebuffer(nvfb, x, y, vfb, x, y, w, h, 0); - PackFramebufferDirectx9_(nvfb, x, y, w, h); + PackFramebufferSync_(nvfb, x, y, w, h); textureCacheDX9_->ForgetLastTexture(); RebindFramebuffer(); @@ -640,7 +626,7 @@ static const D3DVERTEXELEMENT9 g_FramebufferVertexElements[] = { } } - void FramebufferManagerDX9::PackFramebufferDirectx9_(VirtualFramebuffer *vfb, int x, int y, int w, int h) { + void FramebufferManagerDX9::PackFramebufferSync_(VirtualFramebuffer *vfb, int x, int y, int w, int h) { if (!vfb->fbo) { ERROR_LOG_REPORT_ONCE(vfbfbozero, SCEGE, "PackFramebufferDirectx9_: vfb->fbo == 0"); return; diff --git a/GPU/Directx9/FramebufferDX9.h b/GPU/Directx9/FramebufferDX9.h index f8960a4690..4b03fb0565 100644 --- a/GPU/Directx9/FramebufferDX9.h +++ b/GPU/Directx9/FramebufferDX9.h @@ -62,7 +62,6 @@ public: void BindFramebufferAsColorTexture(int stage, VirtualFramebuffer *framebuffer, int flags); - void ReadFramebufferToMemory(VirtualFramebuffer *vfb, bool sync, int x, int y, int w, int h) override; void DownloadFramebufferForClut(u32 fb_address, u32 loadBytes) override; std::vector GetFramebufferList(); @@ -95,7 +94,7 @@ protected: private: void MakePixelTexture(const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride, int width, int height, float &u1, float &v1) override; - void PackFramebufferDirectx9_(VirtualFramebuffer *vfb, int x, int y, int w, int h); + void PackFramebufferSync_(VirtualFramebuffer *vfb, int x, int y, int w, int h) override; void PackDepthbuffer(VirtualFramebuffer *vfb, int x, int y, int w, int h); bool GetRenderTargetFramebuffer(LPDIRECT3DSURFACE9 renderTarget, LPDIRECT3DSURFACE9 offscreen, int w, int h, GPUDebugBuffer &buffer); diff --git a/GPU/GLES/FramebufferManagerGLES.cpp b/GPU/GLES/FramebufferManagerGLES.cpp index 832853568e..97fa51418c 100644 --- a/GPU/GLES/FramebufferManagerGLES.cpp +++ b/GPU/GLES/FramebufferManagerGLES.cpp @@ -948,9 +948,9 @@ void FramebufferManagerGLES::PackFramebufferSync_(VirtualFramebuffer *vfb, int x int dstByteOffset = y * vfb->fb_stride * dstBpp; u8 *dst = Memory::GetPointer(fb_address + dstByteOffset); - GLubyte *packed = nullptr; + u8 *packed = nullptr; if (!convert) { - packed = (GLubyte *)dst; + packed = (u8 *)dst; } else { // End result may be 16-bit but we are reading 32-bit, so there may not be enough space at fb_address if (!convBuf_ || convBufSize_ < bufSize) { diff --git a/GPU/GLES/FramebufferManagerGLES.h b/GPU/GLES/FramebufferManagerGLES.h index 77f58c3afc..b02107f48c 100644 --- a/GPU/GLES/FramebufferManagerGLES.h +++ b/GPU/GLES/FramebufferManagerGLES.h @@ -108,7 +108,7 @@ private: void CompilePostShader(); void PackFramebufferAsync_(VirtualFramebuffer *vfb); // Not used under ES currently - void PackFramebufferSync_(VirtualFramebuffer *vfb, int x, int y, int w, int h); + void PackFramebufferSync_(VirtualFramebuffer *vfb, int x, int y, int w, int h) override; void PackDepthbuffer(VirtualFramebuffer *vfb, int x, int y, int w, int h); // Used by DrawPixels diff --git a/GPU/Vulkan/FramebufferVulkan.cpp b/GPU/Vulkan/FramebufferVulkan.cpp index 98ab15627a..c14705bdc9 100644 --- a/GPU/Vulkan/FramebufferVulkan.cpp +++ b/GPU/Vulkan/FramebufferVulkan.cpp @@ -542,35 +542,6 @@ VulkanTexture *FramebufferManagerVulkan::GetFramebufferColor(u32 fbRawAddress, V } } -void FramebufferManagerVulkan::ReadFramebufferToMemory(VirtualFramebuffer *vfb, bool sync, int x, int y, int w, int h) { - PROFILE_THIS_SCOPE("gpu-readback"); - if (sync) { - // flush async just in case when we go for synchronous update - // Doesn't actually pack when sent a null argument. - PackFramebufferAsync_(nullptr); - } - - if (vfb) { - // We'll pseudo-blit framebuffers here to get a resized version of vfb. - VirtualFramebuffer *nvfb = FindDownloadTempBuffer(vfb); - OptimizeDownloadRange(vfb, x, y, w, h); - BlitFramebuffer(nvfb, x, y, vfb, x, y, w, h, 0); - - // PackFramebufferSync_() - Synchronous pixel data transfer using glReadPixels - // PackFramebufferAsync_() - Asynchronous pixel data transfer using glReadPixels with PBOs - - // TODO: Can we fall back to sync without these? - if (!sync) { - PackFramebufferAsync_(nvfb); - } else { - PackFramebufferSync_(nvfb, x, y, w, h); - } - - textureCacheVulkan_->ForgetLastTexture(); - RebindFramebuffer(); - } -} - void FramebufferManagerVulkan::DownloadFramebufferForClut(u32 fb_address, u32 loadBytes) { PROFILE_THIS_SCOPE("gpu-readback"); // Flush async just in case. @@ -874,10 +845,6 @@ void FramebufferManagerVulkan::PackFramebufferAsync_(VirtualFramebuffer *vfb) { currentPBO_ = nextPBO; } -void FramebufferManagerVulkan::PackFramebufferSync_(VirtualFramebuffer *vfb, int x, int y, int w, int h) { - -} - void FramebufferManagerVulkan::BeginFrameVulkan() { BeginFrame(); diff --git a/GPU/Vulkan/FramebufferVulkan.h b/GPU/Vulkan/FramebufferVulkan.h index 8be21bdf76..72a00646c3 100644 --- a/GPU/Vulkan/FramebufferVulkan.h +++ b/GPU/Vulkan/FramebufferVulkan.h @@ -90,8 +90,6 @@ public: // For use when texturing from a framebuffer. May create a duplicate if target. VulkanTexture *GetFramebufferColor(u32 fbRawAddress, VirtualFramebuffer *framebuffer, int flags); - // Reads a rectangular subregion of a framebuffer to the right position in its backing memory. - void ReadFramebufferToMemory(VirtualFramebuffer *vfb, bool sync, int x, int y, int w, int h) override; void DownloadFramebufferForClut(u32 fb_address, u32 loadBytes) override; std::vector GetFramebufferList(); @@ -123,15 +121,12 @@ protected: void UpdateDownloadTempBuffer(VirtualFramebuffer *nvfb) override; private: - // The returned texture does not need to be free'd, might be returned from a pool (currently single entry) void MakePixelTexture(const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride, int width, int height, float &u1, float &v1) override; - void DoNotifyDraw(); void UpdatePostShaderUniforms(int bufferWidth, int bufferHeight, int renderWidth, int renderHeight); void PackFramebufferAsync_(VirtualFramebuffer *vfb); - void PackFramebufferSync_(VirtualFramebuffer *vfb, int x, int y, int w, int h); void InitDeviceObjects(); void DestroyDeviceObjects();