From 77c834ba5cb9985e33340b6ce4abd58f4955734f Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 28 Aug 2022 08:32:00 -0700 Subject: [PATCH 1/4] GPU: Fix possible shift truncation. Also, add algorithm for std::max. --- GPU/Common/TextureShaderCommon.cpp | 2 +- UI/OnScreenDisplay.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/GPU/Common/TextureShaderCommon.cpp b/GPU/Common/TextureShaderCommon.cpp index 2584dc595b..79417a482e 100644 --- a/GPU/Common/TextureShaderCommon.cpp +++ b/GPU/Common/TextureShaderCommon.cpp @@ -191,7 +191,7 @@ Draw2DPipeline *TextureShaderCache::GetDepalettizeShader(uint32_t clutMode, GETe using namespace Draw; // Generate an ID for depal shaders. - u64 id = (depthUpperBits << 32) | (clutMode & 0xFFFFFF) | (textureFormat << 24) | (bufferFormat << 28); + u64 id = ((u64)depthUpperBits << 32) | (clutMode & 0xFFFFFF) | (textureFormat << 24) | (bufferFormat << 28); auto shader = depalCache_.find(id); if (shader != depalCache_.end()) { diff --git a/UI/OnScreenDisplay.cpp b/UI/OnScreenDisplay.cpp index acc0347fd1..1b38c93093 100644 --- a/UI/OnScreenDisplay.cpp +++ b/UI/OnScreenDisplay.cpp @@ -1,3 +1,4 @@ +#include #include #include "UI/OnScreenDisplay.h" From 18e1efece1a8c1a81da94a2180e594a12759428a Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 28 Aug 2022 08:34:48 -0700 Subject: [PATCH 2/4] GPU: Add a way to defer dirty rechecks. These are situations where we have temporary state which will require recalc again later. --- GPU/Common/DrawEngineCommon.h | 3 +++ GPU/D3D11/StateMappingD3D11.cpp | 12 +++++++----- GPU/Directx9/StateMappingDX9.cpp | 7 ++++++- GPU/GLES/StateMappingGLES.cpp | 9 ++++++++- GPU/Vulkan/DrawEngineVulkan.cpp | 12 ++++-------- GPU/Vulkan/StateMappingVulkan.cpp | 8 +++++++- 6 files changed, 35 insertions(+), 16 deletions(-) diff --git a/GPU/Common/DrawEngineCommon.h b/GPU/Common/DrawEngineCommon.h index 65391be14f..fb1218224b 100644 --- a/GPU/Common/DrawEngineCommon.h +++ b/GPU/Common/DrawEngineCommon.h @@ -189,6 +189,9 @@ protected: bool fboTexNeedsBind_ = false; bool fboTexBound_ = false; + // Sometimes, unusual situations mean we need to reset dirty flags after state calc finishes. + uint64_t dirtyRequiresRecheck_ = 0; + // Hardware tessellation TessellationDataTransfer *tessDataTransfer; }; diff --git a/GPU/D3D11/StateMappingD3D11.cpp b/GPU/D3D11/StateMappingD3D11.cpp index b75edef5d5..9ed5385b4a 100644 --- a/GPU/D3D11/StateMappingD3D11.cpp +++ b/GPU/D3D11/StateMappingD3D11.cpp @@ -174,12 +174,15 @@ void DrawEngineD3D11::ApplyDrawState(int prim) { framebufferManager_->RebindFramebuffer("RebindFramebuffer - ApplyDrawState"); // Must dirty blend state here so we re-copy next time. Example: Lunar's spell effects. + dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE; gstate_c.Dirty(DIRTY_BLEND_STATE); } + dirtyRequiresRecheck_ |= DIRTY_FRAGMENTSHADER_STATE; gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE); } else if (blendState.resetFramebufferRead) { ResetFramebufferRead(); + dirtyRequiresRecheck_ |= DIRTY_FRAGMENTSHADER_STATE; gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE); } @@ -193,6 +196,7 @@ void DrawEngineD3D11::ApplyDrawState(int prim) { keys_.blend.destColor = d3d11BlendFactorLookup[(size_t)blendState.dstColor]; keys_.blend.destAlpha = d3d11BlendFactorLookup[(size_t)blendState.dstAlpha]; if (blendState.dirtyShaderBlendFixValues) { + dirtyRequiresRecheck_ |= DIRTY_SHADERBLEND; gstate_c.Dirty(DIRTY_SHADERBLEND); } dynState_.useBlendColor = blendState.useBlendColor; @@ -315,7 +319,7 @@ void DrawEngineD3D11::ApplyDrawState(int prim) { keys_.depthStencil.stencilFailOp = D3D11_STENCIL_OP_ZERO; keys_.depthStencil.stencilDepthFailOp = D3D11_STENCIL_OP_KEEP; - // TODO: Need to set in a way that carries over to the next draw.. + dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE; gstate_c.Dirty(DIRTY_BLEND_STATE); } } else { @@ -464,8 +468,6 @@ void DrawEngineD3D11::ApplyDrawStateLate(bool applyStencilRef, uint8_t stencilRe context_->OMSetDepthStencilState(depthStencilState_, applyStencilRef ? stencilRef : dynState_.stencilRef); } gstate_c.Clean(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_RASTER_STATE | DIRTY_BLEND_STATE); - - // Must dirty blend state here so we re-copy next time. Example: Lunar's spell effects. - if (fboTexBound_) - gstate_c.Dirty(DIRTY_BLEND_STATE); + gstate_c.Dirty(dirtyRequiresRecheck_); + dirtyRequiresRecheck_ = 0; } diff --git a/GPU/Directx9/StateMappingDX9.cpp b/GPU/Directx9/StateMappingDX9.cpp index a82ccb4605..23a7645556 100644 --- a/GPU/Directx9/StateMappingDX9.cpp +++ b/GPU/Directx9/StateMappingDX9.cpp @@ -153,9 +153,11 @@ void DrawEngineDX9::ApplyDrawState(int prim) { device_->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); fboTexBound_ = true; fboTexNeedsBind_ = false; + dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE; gstate_c.Dirty(DIRTY_BLEND_STATE); } + dirtyRequiresRecheck_ |= DIRTY_FRAGMENTSHADER_STATE; gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE); } else if (blendState.resetFramebufferRead) { ResetFramebufferRead(); @@ -169,6 +171,7 @@ void DrawEngineDX9::ApplyDrawState(int prim) { dxBlendFactorLookup[(size_t)blendState.srcColor], dxBlendFactorLookup[(size_t)blendState.dstColor], dxBlendFactorLookup[(size_t)blendState.srcAlpha], dxBlendFactorLookup[(size_t)blendState.dstAlpha]); if (blendState.dirtyShaderBlendFixValues) { + dirtyRequiresRecheck_ |= DIRTY_SHADERBLEND; gstate_c.Dirty(DIRTY_SHADERBLEND); } if (blendState.useBlendColor) { @@ -262,7 +265,7 @@ void DrawEngineDX9::ApplyDrawState(int prim) { // Invert dxstate.stencilOp.set(D3DSTENCILOP_ZERO, D3DSTENCILOP_KEEP, D3DSTENCILOP_ZERO); - // TODO: Need to set in a way that carries over to the next draw.. + dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE; gstate_c.Dirty(DIRTY_BLEND_STATE); } } else { @@ -289,6 +292,8 @@ void DrawEngineDX9::ApplyDrawState(int prim) { } gstate_c.Clean(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_RASTER_STATE | DIRTY_BLEND_STATE); + gstate_c.Dirty(dirtyRequiresRecheck_); + dirtyRequiresRecheck_ = 0; } void DrawEngineDX9::ApplyDrawStateLate() { diff --git a/GPU/GLES/StateMappingGLES.cpp b/GPU/GLES/StateMappingGLES.cpp index a06bac650c..7f98059a68 100644 --- a/GPU/GLES/StateMappingGLES.cpp +++ b/GPU/GLES/StateMappingGLES.cpp @@ -141,6 +141,7 @@ void DrawEngineGLES::ApplyDrawState(int prim) { // Start profiling here to skip SetTexture which is already accounted for PROFILE_THIS_SCOPE("applydrawstate"); + uint64_t dirtyRequiresRecheck_ = 0; bool useBufferedRendering = framebufferManager_->UseBufferedRendering(); if (gstate_c.IsDirty(DIRTY_BLEND_STATE)) { @@ -177,17 +178,21 @@ void DrawEngineGLES::ApplyDrawState(int prim) { framebufferManager_->RebindFramebuffer("RebindFramebuffer - ApplyDrawState"); // Must dirty blend state here so we re-copy next time. Example: Lunar's spell effects. + dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE; gstate_c.Dirty(DIRTY_BLEND_STATE); } + dirtyRequiresRecheck_ |= DIRTY_FRAGMENTSHADER_STATE; gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE); } else if (blendState.resetFramebufferRead) { ResetFramebufferRead(); + dirtyRequiresRecheck_ |= DIRTY_FRAGMENTSHADER_STATE; gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE); } if (blendState.enabled) { if (blendState.dirtyShaderBlendFixValues) { // Not quite sure how necessary this is. + dirtyRequiresRecheck_ |= DIRTY_SHADERBLEND; gstate_c.Dirty(DIRTY_SHADERBLEND); } if (blendState.useBlendColor) { @@ -269,7 +274,7 @@ void DrawEngineGLES::ApplyDrawState(int prim) { renderManager->SetStencilFunc(true, GL_ALWAYS, 0xFF, 0xFF); renderManager->SetStencilOp(0xFF, GL_ZERO, GL_KEEP, GL_ZERO); - // TODO: Need to set in a way that carries over to the next draw.. + dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE; gstate_c.Dirty(DIRTY_BLEND_STATE); } } else { @@ -293,6 +298,8 @@ void DrawEngineGLES::ApplyDrawState(int prim) { } gstate_c.Clean(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_RASTER_STATE | DIRTY_BLEND_STATE); + gstate_c.Dirty(dirtyRequiresRecheck_); + dirtyRequiresRecheck_ = 0; } void DrawEngineGLES::ApplyDrawStateLate(bool setStencilValue, int stencilValue) { diff --git a/GPU/Vulkan/DrawEngineVulkan.cpp b/GPU/Vulkan/DrawEngineVulkan.cpp index 7cbaba3ca6..37243cac85 100644 --- a/GPU/Vulkan/DrawEngineVulkan.cpp +++ b/GPU/Vulkan/DrawEngineVulkan.cpp @@ -796,11 +796,9 @@ void DrawEngineVulkan::DoFlush() { } ApplyDrawStateLate(renderManager, false, 0, pipeline->UsesBlendConstant()); gstate_c.Clean(DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_RASTER_STATE | DIRTY_VIEWPORTSCISSOR_STATE); + gstate_c.Dirty(dirtyRequiresRecheck_); + dirtyRequiresRecheck_ = 0; lastPipeline_ = pipeline; - - // Must dirty blend state here so we re-copy next time. Example: Lunar's spell effects. - if (fboTexBound_) - gstate_c.Dirty(DIRTY_BLEND_STATE); } lastPrim_ = prim; @@ -928,11 +926,9 @@ void DrawEngineVulkan::DoFlush() { } ApplyDrawStateLate(renderManager, result.setStencil, result.stencilValue, pipeline->UsesBlendConstant()); gstate_c.Clean(DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_RASTER_STATE | DIRTY_VIEWPORTSCISSOR_STATE); + gstate_c.Dirty(dirtyRequiresRecheck_); + dirtyRequiresRecheck_ = 0; lastPipeline_ = pipeline; - - // Must dirty blend state here so we re-copy next time. Example: Lunar's spell effects. - if (fboTexBound_) - gstate_c.Dirty(DIRTY_BLEND_STATE); } lastPrim_ = prim; diff --git a/GPU/Vulkan/StateMappingVulkan.cpp b/GPU/Vulkan/StateMappingVulkan.cpp index 9ca4a81b9d..b4e547a77b 100644 --- a/GPU/Vulkan/StateMappingVulkan.cpp +++ b/GPU/Vulkan/StateMappingVulkan.cpp @@ -173,9 +173,11 @@ void DrawEngineVulkan::ConvertStateToVulkanKey(FramebufferManagerVulkan &fbManag ApplyFramebufferRead(&fboTexNeedsBind_); // The shader takes over the responsibility for blending, so recompute. ApplyStencilReplaceAndLogicOpIgnoreBlend(blendState.replaceAlphaWithStencil, blendState); + dirtyRequiresRecheck_ |= DIRTY_FRAGMENTSHADER_STATE; gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE); } else if (blendState.resetFramebufferRead) { ResetFramebufferRead(); + dirtyRequiresRecheck_ |= DIRTY_FRAGMENTSHADER_STATE; gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE); } @@ -188,6 +190,7 @@ void DrawEngineVulkan::ConvertStateToVulkanKey(FramebufferManagerVulkan &fbManag key.destColor = vkBlendFactorLookup[(size_t)blendState.dstColor]; key.destAlpha = vkBlendFactorLookup[(size_t)blendState.dstAlpha]; if (blendState.dirtyShaderBlendFixValues) { + dirtyRequiresRecheck_ |= DIRTY_SHADERBLEND; gstate_c.Dirty(DIRTY_SHADERBLEND); } dynState.useBlendColor = blendState.useBlendColor; @@ -322,7 +325,7 @@ void DrawEngineVulkan::ConvertStateToVulkanKey(FramebufferManagerVulkan &fbManag key.stencilFailOp = VK_STENCIL_OP_ZERO; key.stencilDepthFailOp = VK_STENCIL_OP_KEEP; - // TODO: Need to set in a way that carries over to the next draw.. + dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE; gstate_c.Dirty(DIRTY_BLEND_STATE); } } else { @@ -375,6 +378,9 @@ void DrawEngineVulkan::BindShaderBlendTex() { boundSecondary_ = (VkImageView)draw_->GetNativeObject(Draw::NativeObject::BOUND_TEXTURE1_IMAGEVIEW); fboTexBound_ = true; fboTexNeedsBind_ = false; + + // Must dirty blend state here so we re-copy next time. Example: Lunar's spell effects. + dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE; } } } From a8190f3eb405e92672d41d553631df78d9ec6a72 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 28 Aug 2022 08:39:05 -0700 Subject: [PATCH 3/4] GPU: Recheck depth/stencil after Spongebob hack. Since we swap the compare, we should at least recheck it later. --- GPU/D3D11/StateMappingD3D11.cpp | 4 ++-- GPU/Directx9/StateMappingDX9.cpp | 4 ++-- GPU/GLES/StateMappingGLES.cpp | 4 ++-- GPU/Vulkan/StateMappingVulkan.cpp | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/GPU/D3D11/StateMappingD3D11.cpp b/GPU/D3D11/StateMappingD3D11.cpp index 9ed5385b4a..561ea414b3 100644 --- a/GPU/D3D11/StateMappingD3D11.cpp +++ b/GPU/D3D11/StateMappingD3D11.cpp @@ -319,8 +319,8 @@ void DrawEngineD3D11::ApplyDrawState(int prim) { keys_.depthStencil.stencilFailOp = D3D11_STENCIL_OP_ZERO; keys_.depthStencil.stencilDepthFailOp = D3D11_STENCIL_OP_KEEP; - dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE; - gstate_c.Dirty(DIRTY_BLEND_STATE); + dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE; + gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE); } } else { keys_.depthStencil.stencilTestEnable = false; diff --git a/GPU/Directx9/StateMappingDX9.cpp b/GPU/Directx9/StateMappingDX9.cpp index 23a7645556..1e4d1d348c 100644 --- a/GPU/Directx9/StateMappingDX9.cpp +++ b/GPU/Directx9/StateMappingDX9.cpp @@ -265,8 +265,8 @@ void DrawEngineDX9::ApplyDrawState(int prim) { // Invert dxstate.stencilOp.set(D3DSTENCILOP_ZERO, D3DSTENCILOP_KEEP, D3DSTENCILOP_ZERO); - dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE; - gstate_c.Dirty(DIRTY_BLEND_STATE); + dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE; + gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE); } } else { dxstate.stencilTest.disable(); diff --git a/GPU/GLES/StateMappingGLES.cpp b/GPU/GLES/StateMappingGLES.cpp index 7f98059a68..5f9fe9d135 100644 --- a/GPU/GLES/StateMappingGLES.cpp +++ b/GPU/GLES/StateMappingGLES.cpp @@ -274,8 +274,8 @@ void DrawEngineGLES::ApplyDrawState(int prim) { renderManager->SetStencilFunc(true, GL_ALWAYS, 0xFF, 0xFF); renderManager->SetStencilOp(0xFF, GL_ZERO, GL_KEEP, GL_ZERO); - dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE; - gstate_c.Dirty(DIRTY_BLEND_STATE); + dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE; + gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE); } } else { renderManager->SetStencilDisabled(); diff --git a/GPU/Vulkan/StateMappingVulkan.cpp b/GPU/Vulkan/StateMappingVulkan.cpp index b4e547a77b..ac51debc6a 100644 --- a/GPU/Vulkan/StateMappingVulkan.cpp +++ b/GPU/Vulkan/StateMappingVulkan.cpp @@ -325,8 +325,8 @@ void DrawEngineVulkan::ConvertStateToVulkanKey(FramebufferManagerVulkan &fbManag key.stencilFailOp = VK_STENCIL_OP_ZERO; key.stencilDepthFailOp = VK_STENCIL_OP_KEEP; - dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE; - gstate_c.Dirty(DIRTY_BLEND_STATE); + dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE; + gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE); } } else { key.stencilTestEnable = false; From f5efc1aa56793338fab8848d1ff609aaf6447dfc Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 28 Aug 2022 08:50:16 -0700 Subject: [PATCH 4/4] GPU: Make Spongebob hack checks more readable. Multiline clauses are just a lie programmers tell themselves to make them think they've written readable code, just because it's better than a single line. Also add comments while there, in case this causes problems. --- GPU/Common/GPUStateUtils.h | 57 ++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/GPU/Common/GPUStateUtils.h b/GPU/Common/GPUStateUtils.h index a6c751739e..1bdd7cce5b 100644 --- a/GPU/Common/GPUStateUtils.h +++ b/GPU/Common/GPUStateUtils.h @@ -202,25 +202,40 @@ void ConvertStencilFuncState(GenericStencilFuncState &stencilFuncState); // See issue #15898 inline bool SpongebobDepthInverseConditions(const GenericStencilFuncState &stencilState) { - // Check that the depth/stencil state matches the conditions exactly - return gstate.isDepthTestEnabled() && !gstate.isDepthWriteEnabled() && - gstate.getDepthTestFunction() == GE_COMP_GEQUAL && - stencilState.zFail == GE_STENCILOP_ZERO && stencilState.sFail == GE_STENCILOP_KEEP && stencilState.zPass == GE_STENCILOP_KEEP && - stencilState.testFunc == GE_COMP_ALWAYS && stencilState.writeMask == 0xFF && - // And also verify no color is written. The game does this through simple alpha blending with a constant zero alpha. - // We also check for color mask, since it's more natural, in case another game does it. - ((gstate.isAlphaBlendEnabled() && - !gstate.isTextureMapEnabled() && - gstate.getBlendFuncA() == GE_SRCBLEND_SRCALPHA && - gstate.getBlendFuncB() == GE_DSTBLEND_INVSRCALPHA && - ( - ( // Spongebob - gstate.getMaterialAmbientA() == 0x0 && // our accessor is kinda misnamed here, but material diffuse A is both used as default color and as ambient alpha - gstate.getMaterialUpdate() == 0 - ) || - ( // MX vs ATV : Reflex - gstate.getMaterialUpdate() == 1 // Really should also check vertex data, since that's where the zero is :( - ) - ) - ) || gstate.getColorMask() == 0xFFFFFF00); // note that PSP masks are "inverted" + // Check that the depth/stencil state matches the conditions exactly. + // Always with a depth test that's not writing to the depth buffer (only stencil.) + if (!gstate.isDepthTestEnabled() || gstate.isDepthWriteEnabled()) + return false; + // Always GREATER_EQUAL, which we flip to LESS. + if (gstate.getDepthTestFunction() != GE_COMP_GEQUAL) + return false; + + // The whole purpose here is a depth fail that we need to write to alpha. + if (stencilState.zFail != GE_STENCILOP_ZERO || stencilState.sFail != GE_STENCILOP_KEEP || stencilState.zPass != GE_STENCILOP_KEEP) + return false; + if (stencilState.testFunc != GE_COMP_ALWAYS || stencilState.writeMask != 0xFF) + return false; + + // Lastly, verify no color is written. Natural way is a mask, in case another game uses it. + // Note that the PSP masks are reversed compared to typical APIs. + if (gstate.getColorMask() == 0xFFFFFF00) + return true; + + // These games specifically use simple alpha blending with a constant zero alpha. + if (!gstate.isAlphaBlendEnabled() || gstate.getBlendFuncA() != GE_SRCBLEND_SRCALPHA || gstate.getBlendFuncB() != GE_DSTBLEND_INVSRCALPHA) + return false; + + // Also make sure there's no texture, in case its alpha gets involved. + if (gstate.isTextureMapEnabled()) + return false; + + // Spongebob uses material alpha. + if (gstate.getMaterialAmbientA() == 0x00 && gstate.getMaterialUpdate() == 0) + return true; + // MX vs ATV : Reflex uses vertex colors, should really check them... + if (gstate.getMaterialUpdate() == 1) + return true; + + // Okay, color is most likely being used if we didn't hit the above. + return false; }