From bebf649705833ad32e064ade083e0758da85dbe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 24 May 2020 20:27:58 +0200 Subject: [PATCH] OpenGL/Vulkan: Rework the contract around dynamic state. Removes some ugly dirtying from the GL render manager. --- GPU/Common/FramebufferCommon.cpp | 13 ++++++------- GPU/GLES/DrawEngineGLES.cpp | 11 +++++++++++ GPU/GLES/DrawEngineGLES.h | 2 ++ GPU/GLES/StateMappingGLES.cpp | 4 ++-- GPU/Vulkan/DrawEngineVulkan.cpp | 13 ++++++++++--- GPU/Vulkan/DrawEngineVulkan.h | 2 ++ ext/native/thin3d/GLRenderManager.h | 23 +++++++++++++++++++++++ ext/native/thin3d/VulkanRenderManager.h | 25 +++++++++++++++++++++++++ 8 files changed, 81 insertions(+), 12 deletions(-) diff --git a/GPU/Common/FramebufferCommon.cpp b/GPU/Common/FramebufferCommon.cpp index b5d899f3af..47a9c2c89a 100644 --- a/GPU/Common/FramebufferCommon.cpp +++ b/GPU/Common/FramebufferCommon.cpp @@ -1,6 +1,6 @@ // Copyright (c) 2012- PPSSPP Project. -4// This program is free software: you can redistribute it and/or modify +// This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, version 2.0 or later versions. @@ -450,13 +450,13 @@ void FramebufferManagerCommon::DestroyFramebuf(VirtualFramebuffer *v) { // Wipe some pointers if (currentRenderVfb_ == v) - currentRenderVfb_ = 0; + currentRenderVfb_ = nullptr; if (displayFramebuf_ == v) - displayFramebuf_ = 0; + displayFramebuf_ = nullptr; if (prevDisplayFramebuf_ == v) - prevDisplayFramebuf_ = 0; + prevDisplayFramebuf_ = nullptr; if (prevPrevDisplayFramebuf_ == v) - prevPrevDisplayFramebuf_ = 0; + prevPrevDisplayFramebuf_ = nullptr; delete v; } @@ -471,7 +471,7 @@ void FramebufferManagerCommon::NotifyRenderFramebufferCreated(VirtualFramebuffer textureCache_->NotifyFramebuffer(vfb->fb_address, vfb, NOTIFY_FB_CREATED); - // ugly... + // Ugly... if (gstate_c.curRTWidth != vfb->width || gstate_c.curRTHeight != vfb->height) { gstate_c.Dirty(DIRTY_PROJTHROUGHMATRIX | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_CULLRANGE); } @@ -1075,7 +1075,6 @@ void FramebufferManagerCommon::ResizeFramebufFBO(VirtualFramebuffer *vfb, int w, } } else { draw_->BindFramebufferAsRenderTarget(vfb->fbo, { Draw::RPAction::CLEAR, Draw::RPAction::CLEAR, Draw::RPAction::CLEAR }, "ResizeFramebufFBO"); - // GLES resets the blend state on clears. gstate_c.Dirty(DIRTY_VIEWPORTSCISSOR_STATE); } diff --git a/GPU/GLES/DrawEngineGLES.cpp b/GPU/GLES/DrawEngineGLES.cpp index ca32d8e508..3d6282771f 100644 --- a/GPU/GLES/DrawEngineGLES.cpp +++ b/GPU/GLES/DrawEngineGLES.cpp @@ -159,6 +159,8 @@ void DrawEngineGLES::BeginFrame() { FrameData &frameData = frameData_[render_->GetCurFrame()]; render_->BeginPushBuffer(frameData.pushIndex); render_->BeginPushBuffer(frameData.pushVertex); + + lastRenderStepId_ = -1; } void DrawEngineGLES::EndFrame() { @@ -305,6 +307,15 @@ void DrawEngineGLES::DoFlush() { gpuStats.numFlushes++; gpuStats.numTrackedVertexArrays = (int)vai_.size(); + // A new render step means we need to flush any dynamic state. Really, any state that is reset in + // GLQueueRunner::PerformRenderPass. + int curRenderStepId = render_->GetCurrentStepId(); + if (lastRenderStepId_ != curRenderStepId) { + // Dirty everything that has dynamic state that will need re-recording. + gstate_c.Dirty(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_BLEND_STATE | DIRTY_RASTER_STATE); + lastRenderStepId_ = curRenderStepId; + } + bool textureNeedsApply = false; if (gstate_c.IsDirty(DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS) && !gstate.isModeClear() && gstate.isTextureMapEnabled()) { textureCache_->SetTexture(); diff --git a/GPU/GLES/DrawEngineGLES.h b/GPU/GLES/DrawEngineGLES.h index 8fd17e0514..51abeb6d24 100644 --- a/GPU/GLES/DrawEngineGLES.h +++ b/GPU/GLES/DrawEngineGLES.h @@ -229,6 +229,8 @@ private: int bufferDecimationCounter_ = 0; + int lastRenderStepId_ = -1; + // Hardware tessellation TessellationDataTransferGLES *tessDataTransferGLES; }; diff --git a/GPU/GLES/StateMappingGLES.cpp b/GPU/GLES/StateMappingGLES.cpp index 0df2077834..4e2f7f36f1 100644 --- a/GPU/GLES/StateMappingGLES.cpp +++ b/GPU/GLES/StateMappingGLES.cpp @@ -320,8 +320,8 @@ void DrawEngineGLES::ApplyDrawState(int prim) { } } -void DrawEngineGLES::ApplyDrawStateLate(bool setStencil, int stencilValue) { - if (setStencil) { +void DrawEngineGLES::ApplyDrawStateLate(bool setStencilValue, int stencilValue) { + if (setStencilValue) { render_->SetStencilFunc(GL_TRUE, GL_ALWAYS, stencilValue, 255); } diff --git a/GPU/Vulkan/DrawEngineVulkan.cpp b/GPU/Vulkan/DrawEngineVulkan.cpp index 62305dd14a..8802e4c064 100644 --- a/GPU/Vulkan/DrawEngineVulkan.cpp +++ b/GPU/Vulkan/DrawEngineVulkan.cpp @@ -259,9 +259,10 @@ void DrawEngineVulkan::DeviceRestore(VulkanContext *vulkan, Draw::DrawContext *d } void DrawEngineVulkan::BeginFrame() { - // Too many issues right now to allow reuse. lastPipeline_ = nullptr; + lastRenderStepId_ = -1; + int curFrame = vulkan_->GetCurFrame(); FrameData *frame = &frame_[curFrame]; @@ -587,10 +588,16 @@ void DrawEngineVulkan::DoFlush() { VulkanRenderManager *renderManager = (VulkanRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER); - // HACK: These two lines should only execute if we started on a new render pass. Can't tell from in here though... + // TODO: Move this into the below if statement. Little scary change though so holding off until 1.11. lastPipeline_ = nullptr; + // Since we have a new cmdbuf, dirty our dynamic state so it gets re-set. - // gstate_c.Dirty(DIRTY_VIEWPORTSCISSOR_STATE|DIRTY_DEPTHSTENCIL_STATE|DIRTY_BLEND_STATE); + int curRenderStepId = renderManager->GetCurrentStepId(); + if (lastRenderStepId_ != curRenderStepId) { + // Dirty everything that has dynamic state that will need re-recording. + gstate_c.Dirty(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_BLEND_STATE); + lastRenderStepId_ = curRenderStepId; + } FrameData *frame = &frame_[vulkan_->GetCurFrame()]; diff --git a/GPU/Vulkan/DrawEngineVulkan.h b/GPU/Vulkan/DrawEngineVulkan.h index cbbaeebb5b..8ea60dec73 100644 --- a/GPU/Vulkan/DrawEngineVulkan.h +++ b/GPU/Vulkan/DrawEngineVulkan.h @@ -302,4 +302,6 @@ private: // Hardware tessellation TessellationDataTransferVulkan *tessDataTransferVulkan; + + int lastRenderStepId_ = -1; }; diff --git a/ext/native/thin3d/GLRenderManager.h b/ext/native/thin3d/GLRenderManager.h index bffa535895..a31ab31b23 100644 --- a/ext/native/thin3d/GLRenderManager.h +++ b/ext/native/thin3d/GLRenderManager.h @@ -501,8 +501,25 @@ public: pushbuffer->End(); } + // This starts a new step (like a "render pass" in Vulkan). + // + // After a "CopyFramebuffer" or the other functions that start "steps", you need to call this beforce + // making any new render state changes or draw calls. + // + // The following state needs to be reset by the caller after calling this (and will thus not safely carry over from + // the previous one): + // * Viewport/Scissor + // * Depth/stencil + // * Blend + // * Raster state like primitive, culling, etc. + // + // It can be useful to use GetCurrentStepId() to figure out when you need to send all this state again, if you're + // not keeping track of your calls to this function on your own. void BindFramebufferAsRenderTarget(GLRFramebuffer *fb, GLRRenderPassAction color, GLRRenderPassAction depth, GLRRenderPassAction stencil, uint32_t clearColor, float clearDepth, uint8_t clearStencil, const char *tag); + + // Binds a framebuffer as a texture, for the following draws. void BindFramebufferAsTexture(GLRFramebuffer *fb, int binding, int aspectBit, int attachment); + bool CopyFramebufferToMemorySync(GLRFramebuffer *src, int aspectBits, int x, int y, int w, int h, Draw::DataFormat destFormat, uint8_t *pixels, int pixelStride, const char *tag); void CopyImageToMemorySync(GLRTexture *texture, int mipLevel, int x, int y, int w, int h, Draw::DataFormat destFormat, uint8_t *pixels, int pixelStride, const char *tag); @@ -920,6 +937,12 @@ public: skipGLCalls_ = true; } + // Gets a frame-unique ID of the current step being recorded. Can be used to figure out + // when the current step has changed, which means the caller will need to re-record its state. + int GetCurrentStepId() const { + return (int)steps_.size(); + } + private: void BeginSubmitFrame(int frame); void EndSubmitFrame(int frame); diff --git a/ext/native/thin3d/VulkanRenderManager.h b/ext/native/thin3d/VulkanRenderManager.h index e12f220fcc..fc4b3fc7da 100644 --- a/ext/native/thin3d/VulkanRenderManager.h +++ b/ext/native/thin3d/VulkanRenderManager.h @@ -105,8 +105,27 @@ public: // Zaps queued up commands. Use if you know there's a risk you've queued up stuff that has already been deleted. Can happen during in-game shutdown. void Wipe(); + // This starts a new step containing a render pass. + // + // After a "CopyFramebuffer" or the other functions that start "steps", you need to call this beforce + // making any new render state changes or draw calls. + // + // The following dynamic state needs to be reset by the caller after calling this (and will thus not safely carry over from + // the previous one): + // * Viewport/Scissor + // * Stencil parameters + // * Blend color + // + // (Most other state is directly decided by your choice of pipeline and descriptor set, so not handled here). + // + // It can be useful to use GetCurrentStepId() to figure out when you need to send all this state again, if you're + // not keeping track of your calls to this function on your own. void BindFramebufferAsRenderTarget(VKRFramebuffer *fb, VKRRenderPassAction color, VKRRenderPassAction depth, VKRRenderPassAction stencil, uint32_t clearColor, float clearDepth, uint8_t clearStencil, const char *tag); + + // Returns an ImageView corresponding to a framebuffer. Is called BindFramebufferAsTexture to maintain a similar interface + // as the other backends, even though there's no actual binding happening here. VkImageView BindFramebufferAsTexture(VKRFramebuffer *fb, int binding, int aspectBit, int attachment); + bool CopyFramebufferToMemorySync(VKRFramebuffer *src, int aspectBits, int x, int y, int w, int h, Draw::DataFormat destFormat, uint8_t *pixels, int pixelStride, const char *tag); void CopyImageToMemorySync(VkImage image, int mipLevel, int x, int y, int w, int h, Draw::DataFormat destFormat, uint8_t *pixels, int pixelStride, const char *tag); @@ -232,6 +251,12 @@ public: } } + // Gets a frame-unique ID of the current step being recorded. Can be used to figure out + // when the current step has changed, which means the caller will need to re-record its state. + int GetCurrentStepId() const { + return (int)steps_.size(); + } + void CreateBackbuffers(); void DestroyBackbuffers();