From ca5a29e2cedcc4ced1d3cc321f1babf1efe56e9f Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 6 Jan 2016 23:08:13 -0800 Subject: [PATCH] Correct for some offsets. --- GPU/Common/DepalettizeShaderCommon.cpp | 6 +++--- GPU/Directx9/TextureCacheDX9.cpp | 20 +++++++++++++++----- GPU/GLES/TextureCache.cpp | 20 +++++++++++++++----- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/GPU/Common/DepalettizeShaderCommon.cpp b/GPU/Common/DepalettizeShaderCommon.cpp index b562a44e6ed5..6dd8c0c90ea8 100644 --- a/GPU/Common/DepalettizeShaderCommon.cpp +++ b/GPU/Common/DepalettizeShaderCommon.cpp @@ -111,7 +111,7 @@ void GenerateDepalShader300(char *buffer, GEBufferFormat pixelFormat, ShaderLang WRITE(p, ";\n"); } - WRITE(p, " fragColor0 = texture(pal, vec2((float(index) + 0.5) * (1.0 / %f) * u_offset.x + u_offset.y, 0.0));\n", texturePixels); + WRITE(p, " fragColor0 = texture(pal, vec2((float(index) + 0.5) * %f * u_offset.x + u_offset.y, 0.0));\n", 1.0 / texturePixels); WRITE(p, "}\n"); } @@ -243,7 +243,7 @@ void GenerateDepalShaderFloat(char *buffer, GEBufferFormat pixelFormat, ShaderLa WRITE(p, "uniform vec2 u_offset;\n"); WRITE(p, "void main() {\n"); WRITE(p, " vec4 index = texture2D(tex, v_texcoord0);\n"); - WRITE(p, " float coord = (%s * %f * u_offset.x)%s + u_offset.y;\n", lookupMethod, index_multiplier, offset); + WRITE(p, " float coord = ((%s * %f)%s) * u_offset.x + u_offset.y;\n", lookupMethod, index_multiplier, offset); WRITE(p, " gl_FragColor = texture2D(pal, vec2(coord, 0.0));\n"); WRITE(p, "}\n"); } else if (lang == HLSL_DX9) { @@ -252,7 +252,7 @@ void GenerateDepalShaderFloat(char *buffer, GEBufferFormat pixelFormat, ShaderLa WRITE(p, "float2 u_offset : register(c%i);\n", CONST_PS_DEPAL_OFFSET); WRITE(p, "float4 main(float2 v_texcoord0 : TEXCOORD0) : COLOR0 {\n"); WRITE(p, " float4 index = tex2D(tex, v_texcoord0);\n"); - WRITE(p, " float coord = (%s * %f * u_offset.x)%s + u_offset.y;\n", lookupMethod, index_multiplier, offset); + WRITE(p, " float coord = ((%s * %f)%s) * u_offset.x + u_offset.y;\n", lookupMethod, index_multiplier, offset); WRITE(p, " return tex2D(pal, float2(coord, 0.0)).bgra;\n"); WRITE(p, "}\n"); } diff --git a/GPU/Directx9/TextureCacheDX9.cpp b/GPU/Directx9/TextureCacheDX9.cpp index 497151a660b9..aa2a215712bb 100644 --- a/GPU/Directx9/TextureCacheDX9.cpp +++ b/GPU/Directx9/TextureCacheDX9.cpp @@ -871,6 +871,7 @@ class TextureShaderApplierDX9 { void TextureCacheDX9::ApplyIndexedTexture(TexCacheEntry *entry) { VirtualFramebuffer *clutVfb = nullptr; + const GEPaletteFormat clutFormat = gstate.getClutPaletteFormat(); for (size_t i = 0, n = fbCache_.size(); i < n; ++i) { auto framebuffer = fbCache_[i]; if (framebuffer->fb_address == clutRenderAddress_) { @@ -904,7 +905,8 @@ void TextureCacheDX9::ApplyIndexedTexture(TexCacheEntry *entry) { shaderApply.ApplyBounds(gstate_c.vertBounds, gstate_c.curTextureXOffset, gstate_c.curTextureYOffset, xoff, yoff); shaderApply.Use(depalShaderCache_->GetDepalettizeVertexShader()); - float texel_offset = 0.5f / (float)clutVfb->bufferWidth; + float render_offset = clutRenderOffset_ / (clutFormat == GE_CMODE_32BIT_ABGR8888 ? 4 : 2); + float texel_offset = (0.5f + render_offset) / (float)clutVfb->bufferWidth; if (gstate.getClutPaletteFormat() != GE_CMODE_32BIT_ABGR8888 && (gstate.getClutIndexStartPos() & 0x100) != 0) { // In this case, we truncated the index entries. Apply the offset here. if (clutVfb->renderWidth > 256) { @@ -974,11 +976,19 @@ void TextureCacheDX9::ApplyTextureFramebuffer(TexCacheEntry *entry, VirtualFrame shaderApply.ApplyBounds(gstate_c.vertBounds, gstate_c.curTextureXOffset, gstate_c.curTextureYOffset, xoff, yoff); shaderApply.Use(depalShaderCache_->GetDepalettizeVertexShader()); + float texturePixels = 256.0f; + if (clutFormat != GE_CMODE_32BIT_ABGR8888) + texturePixels = 512.0f; + if (clutVfb) { - // We scale by the width of the CLUT - to map 0.0 -> 0, 1.0 -> 255. - // If the width is 256, 255 is right (with offset.) We aim for the texel centers. - float texel_mult = 255.0f / (float)clutVfb->bufferWidth; - const float f[4] = { texel_mult, 0.0f, 0.0f, 0.0f }; + float render_offset = clutRenderOffset_ / (clutFormat == GE_CMODE_32BIT_ABGR8888 ? 4 : 2); + + // Before this multiplier, (texturePixels - 1) would be near 1.0. + // If our buffer is actually 320 wide, we need to rescale that. + // There's already some accounting for centers. + float texel_mult = texturePixels / (float)clutVfb->bufferWidth; + float texel_offset = render_offset / (float)clutVfb->bufferWidth; + const float f[4] = { texel_mult, texel_offset, 0.0f, 0.0f }; pD3Ddevice->SetPixelShaderConstantF(CONST_PS_DEPAL_OFFSET, f, 1); } else { const float f[4] = { 1.0f, 0.0f, 0.0f, 0.0f }; diff --git a/GPU/GLES/TextureCache.cpp b/GPU/GLES/TextureCache.cpp index 7107ec96ca61..3522ea17d0f4 100644 --- a/GPU/GLES/TextureCache.cpp +++ b/GPU/GLES/TextureCache.cpp @@ -953,6 +953,7 @@ class TextureShaderApplier { void TextureCache::ApplyIndexedTexture(TexCacheEntry *entry) { VirtualFramebuffer *clutVfb = nullptr; + const GEPaletteFormat clutFormat = gstate.getClutPaletteFormat(); for (size_t i = 0, n = fbCache_.size(); i < n; ++i) { auto framebuffer = fbCache_[i]; if (framebuffer->fb_address == clutRenderAddress_) { @@ -983,7 +984,8 @@ void TextureCache::ApplyIndexedTexture(TexCacheEntry *entry) { shaderApply.Use(transformDraw_); if (shader->u_offset != -1) { - float texel_offset = 0.5f / (float)clutVfb->bufferWidth; + float render_offset = clutRenderOffset_ / (clutFormat == GE_CMODE_32BIT_ABGR8888 ? 4 : 2); + float texel_offset = (0.5f + render_offset) / (float)clutVfb->bufferWidth; if (gstate.getClutPaletteFormat() != GE_CMODE_32BIT_ABGR8888 && (gstate.getClutIndexStartPos() & 0x100) != 0) { // In this case, we truncated the index entries. Apply the offset here. if (clutVfb->renderWidth > 256) { @@ -1047,11 +1049,19 @@ void TextureCache::ApplyTextureFramebuffer(TexCacheEntry *entry, VirtualFramebuf shaderApply.Use(transformDraw_); if (depal->u_offset != -1) { + float texturePixels = 256.0f; + if (clutFormat != GE_CMODE_32BIT_ABGR8888) + texturePixels = 512.0f; + if (clutVfb) { - // We scale by the width of the CLUT - to map 0.0 -> 0, 1.0 -> 255. - // If the width is 256, 255 is right (with offset.) We aim for the texel centers. - float texel_mult = 255.0f / (float)clutVfb->bufferWidth; - glUniform2f(depal->u_offset, texel_mult, 0.0f); + float render_offset = clutRenderOffset_ / (clutFormat == GE_CMODE_32BIT_ABGR8888 ? 4 : 2); + + // Before this multiplier, (texturePixels - 1) would be near 1.0. + // If our buffer is actually 320 wide, we need to rescale that. + // There's already some accounting for centers. + float texel_mult = texturePixels / (float)clutVfb->bufferWidth; + float texel_offset = render_offset / (float)clutVfb->bufferWidth; + glUniform2f(depal->u_offset, texel_mult, texel_offset); } else { glUniform2f(depal->u_offset, 1.0f, 0.0f); }