Skip to content

Commit

Permalink
Correct for some offsets.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownbrackets committed May 31, 2016
1 parent 0b7ebce commit 7bf36a8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
6 changes: 3 additions & 3 deletions GPU/Common/DepalettizeShaderCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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");
}
Expand Down
20 changes: 15 additions & 5 deletions GPU/Directx9/TextureCacheDX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 };
Expand Down
20 changes: 15 additions & 5 deletions GPU/GLES/TextureCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 7bf36a8

Please sign in to comment.