diff --git a/GPU/Common/DepalettizeCommon.cpp b/GPU/Common/DepalettizeCommon.cpp index 185eecdbc4..f23e2607ba 100644 --- a/GPU/Common/DepalettizeCommon.cpp +++ b/GPU/Common/DepalettizeCommon.cpp @@ -243,3 +243,52 @@ std::string DepalShaderCache::DebugGetShaderString(std::string idstr, DebugShade return ""; } } + +// TODO: Merge with DepalShaderCache? +void DepalShaderCache::ApplyShader(DepalShader *shader, float bufferW, float bufferH, int renderW, int renderH, const KnownVertexBounds &bounds, u32 uoff, u32 voff) { + Draw2DVertex verts[4] = { + {-1, -1, 0, 0 }, + { 1, -1, 1, 0 }, + {-1, 1, 0, 1 }, + { 1, 1, 1, 1 }, + }; + + // If min is not < max, then we don't have values (wasn't set during decode.) + if (bounds.minV < bounds.maxV) { + const float invWidth = 1.0f / bufferW; + const float invHeight = 1.0f / bufferH; + // Inverse of half = double. + const float invHalfWidth = invWidth * 2.0f; + const float invHalfHeight = invHeight * 2.0f; + + const int u1 = bounds.minU + uoff; + const int v1 = bounds.minV + voff; + const int u2 = bounds.maxU + uoff; + const int v2 = bounds.maxV + voff; + + const float left = u1 * invHalfWidth - 1.0f; + const float right = u2 * invHalfWidth - 1.0f; + const float top = v1 * invHalfHeight - 1.0f; + const float bottom = v2 * invHalfHeight - 1.0f; + + const float uvleft = u1 * invWidth; + const float uvright = u2 * invWidth; + const float uvtop = v1 * invHeight; + const float uvbottom = v2 * invHeight; + + // Points are: BL, BR, TR, TL. + verts[0] = Draw2DVertex{ left, bottom, uvleft, uvbottom }; + verts[1] = Draw2DVertex{ right, bottom, uvright, uvbottom }; + verts[2] = Draw2DVertex{ left, top, uvleft, uvtop }; + verts[3] = Draw2DVertex{ right, top, uvright, uvtop }; + + // We need to reapply the texture next time since we cropped UV. + gstate_c.Dirty(DIRTY_TEXTURE_PARAMS); + } + + Draw::Viewport vp{ 0.0f, 0.0f, (float)renderW, (float)renderH, 0.0f, 1.0f }; + draw_->BindPipeline(shader->pipeline); + draw_->SetViewports(1, &vp); + draw_->SetScissorRect(0, 0, renderW, renderH); + draw_->DrawUP((const uint8_t *)verts, 4); +} diff --git a/GPU/Common/DepalettizeCommon.h b/GPU/Common/DepalettizeCommon.h index 6e3ef680ef..169e7e8b9d 100644 --- a/GPU/Common/DepalettizeCommon.h +++ b/GPU/Common/DepalettizeCommon.h @@ -54,6 +54,8 @@ public: Draw::SamplerState *GetSampler(); + void ApplyShader(DepalShader *shader, float bufferW, float bufferH, int renderW, int renderH, const KnownVertexBounds &bounds, u32 uoff, u32 voff); + void Clear(); void Decimate(); std::vector DebugGetShaderIDs(DebugShaderType type); @@ -79,67 +81,3 @@ private: std::map cache_; std::map texCache_; }; - -// TODO: Merge with DepalShaderCache? -class TextureShaderApplier { -public: - TextureShaderApplier(Draw::DrawContext *draw, DepalShader *shader, float bufferW, float bufferH, int renderW, int renderH) - : draw_(draw), shader_(shader), bufferW_(bufferW), bufferH_(bufferH), renderW_(renderW), renderH_(renderH) { - static const Draw2DVertex defaultVerts[4] = { - {-1, -1, 0, 0 }, - { 1, -1, 1, 0 }, - {-1, 1, 0, 1 }, - { 1, 1, 1, 1 }, - }; - memcpy(verts_, defaultVerts, sizeof(defaultVerts)); - } - - void Shade(const KnownVertexBounds &bounds, u32 uoff, u32 voff) { - // If min is not < max, then we don't have values (wasn't set during decode.) - if (bounds.minV < bounds.maxV) { - const float invWidth = 1.0f / bufferW_; - const float invHeight = 1.0f / bufferH_; - // Inverse of half = double. - const float invHalfWidth = invWidth * 2.0f; - const float invHalfHeight = invHeight * 2.0f; - - const int u1 = bounds.minU + uoff; - const int v1 = bounds.minV + voff; - const int u2 = bounds.maxU + uoff; - const int v2 = bounds.maxV + voff; - - const float left = u1 * invHalfWidth - 1.0f; - const float right = u2 * invHalfWidth - 1.0f; - const float top = v1 * invHalfHeight - 1.0f; - const float bottom = v2 * invHalfHeight - 1.0f; - - const float uvleft = u1 * invWidth; - const float uvright = u2 * invWidth; - const float uvtop = v1 * invHeight; - const float uvbottom = v2 * invHeight; - - // Points are: BL, BR, TR, TL. - verts_[0] = Draw2DVertex{ left, bottom, uvleft, uvbottom }; - verts_[1] = Draw2DVertex{ right, bottom, uvright, uvbottom }; - verts_[2] = Draw2DVertex{ left, top, uvleft, uvtop }; - verts_[3] = Draw2DVertex{ right, top, uvright, uvtop }; - - // We need to reapply the texture next time since we cropped UV. - gstate_c.Dirty(DIRTY_TEXTURE_PARAMS); - } - Draw::Viewport vp{ 0.0f, 0.0f, (float)renderW_, (float)renderH_, 0.0f, 1.0f }; - draw_->BindPipeline(shader_->pipeline); - draw_->SetViewports(1, &vp); - draw_->SetScissorRect(0, 0, renderW_, renderH_); - draw_->DrawUP((const uint8_t *)verts_, 4); - } - -protected: - Draw::DrawContext *draw_; - DepalShader *shader_; - Draw2DVertex verts_[4]; - float bufferW_; - float bufferH_; - int renderW_; - int renderH_; -}; diff --git a/GPU/Common/TextureCacheCommon.cpp b/GPU/Common/TextureCacheCommon.cpp index 3022f1d1c4..9b092f0e7c 100644 --- a/GPU/Common/TextureCacheCommon.cpp +++ b/GPU/Common/TextureCacheCommon.cpp @@ -1929,15 +1929,16 @@ void TextureCacheCommon::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer Draw::Viewport vp{ 0.0f, 0.0f, (float)framebuffer->renderWidth, (float)framebuffer->renderHeight, 0.0f, 1.0f }; draw_->SetViewports(1, &vp); - TextureShaderApplier shaderApply(draw_, depalShader, framebuffer->bufferWidth, framebuffer->bufferHeight, framebuffer->renderWidth, framebuffer->renderHeight); - draw_->BindFramebufferAsTexture(framebuffer->fbo, 0, depth ? Draw::FB_DEPTH_BIT : Draw::FB_COLOR_BIT, 0); draw_->BindTexture(1, clutTexture); Draw::SamplerState *nearest = depalShaderCache_->GetSampler(); draw_->BindSamplerStates(0, 1, &nearest); draw_->BindSamplerStates(1, 1, &nearest); - shaderApply.Shade(gstate_c.vertBounds, gstate_c.curTextureXOffset, gstate_c.curTextureYOffset); + depalShaderCache_->ApplyShader(depalShader, + framebuffer->bufferWidth, framebuffer->bufferHeight, framebuffer->renderWidth, framebuffer->renderHeight, + gstate_c.vertBounds, gstate_c.curTextureXOffset, gstate_c.curTextureYOffset); + draw_->BindTexture(0, nullptr); framebufferManager_->RebindFramebuffer("ApplyTextureFramebuffer");