Skip to content

Commit

Permalink
Apply rendered CLUT to depal as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownbrackets committed Nov 29, 2015
1 parent 14dbef9 commit 86fb081
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
9 changes: 6 additions & 3 deletions GPU/Common/DepalettizeShaderCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void GenerateDepalShader300(char *buffer, GEBufferFormat pixelFormat) {
WRITE(p, "out vec4 fragColor0;\n");
WRITE(p, "uniform sampler2D tex;\n");
WRITE(p, "uniform sampler2D pal;\n");
WRITE(p, "uniform vec2 u_offset;\n");

WRITE(p, "void main() {\n");
WRITE(p, " vec4 color = texture(tex, v_texcoord0);\n");
Expand Down Expand Up @@ -99,7 +100,7 @@ void GenerateDepalShader300(char *buffer, GEBufferFormat pixelFormat) {
WRITE(p, ";\n");
}

WRITE(p, " fragColor0 = texture(pal, vec2((float(index) + 0.5) * (1.0 / %f), 0.0));\n", texturePixels);
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, "}\n");
}

Expand Down Expand Up @@ -228,17 +229,19 @@ void GenerateDepalShaderFloat(char *buffer, GEBufferFormat pixelFormat, ShaderLa
WRITE(p, "varying vec2 v_texcoord0;\n");
WRITE(p, "uniform sampler2D tex;\n");
WRITE(p, "uniform sampler2D pal;\n");
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)%s;\n", lookupMethod, index_multiplier, offset);
WRITE(p, " float coord = (%s * %f * u_offset.x)%s + 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) {
WRITE(p, "sampler tex: register(s0);\n");
WRITE(p, "sampler pal: register(s1);\n");
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)%s;\n", lookupMethod, index_multiplier, offset);
WRITE(p, " float coord = (%s * %f * u_offset.x)%s + u_offset.y;\n", lookupMethod, index_multiplier, offset);
WRITE(p, " return tex2D(pal, float2(coord, 0.0)).bgra;\n");
WRITE(p, "}\n");
}
Expand Down
31 changes: 29 additions & 2 deletions GPU/Directx9/TextureCacheDX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,19 @@ void TextureCacheDX9::ApplyTextureFramebuffer(TexCacheEntry *entry, VirtualFrame
}

if (pshader) {
LPDIRECT3DTEXTURE9 clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBuf_);
LPDIRECT3DTEXTURE9 clutTexture = nullptr;

VirtualFramebuffer *clutVfb = nullptr;
for (size_t i = 0, n = fbCache_.size(); i < n; ++i) {
auto clutFramebuffer = fbCache_[i];
if (clutFramebuffer->fb_address == clutRenderAddress_) {
clutVfb = clutFramebuffer;
}
}

if (!clutVfb) {
clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBuf_);
}

FBO_DX9 *depalFBO = framebufferManager_->GetTempFBO(framebuffer->renderWidth, framebuffer->renderHeight, FBO_8888);
fbo_bind_as_render_target(depalFBO);
Expand All @@ -1014,7 +1026,22 @@ void TextureCacheDX9::ApplyTextureFramebuffer(TexCacheEntry *entry, VirtualFrame

shaderApply.Use(depalShaderCache_->GetDepalettizeVertexShader());

pD3Ddevice->SetTexture(1, clutTexture);
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 };
pD3Ddevice->SetPixelShaderConstantF(CONST_PS_DEPAL_OFFSET, f, 1);
} else {
const float f[4] = { 1.0f, 0.0f, 0.0f, 0.0f };
pD3Ddevice->SetPixelShaderConstantF(CONST_PS_DEPAL_OFFSET, f, 1);
}

if (clutVfb) {
pD3Ddevice->SetTexture(1, fbo_get_color_texture(clutVfb->fbo_dx9));
} else {
pD3Ddevice->SetTexture(1, clutTexture);
}
pD3Ddevice->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_POINT);
pD3Ddevice->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
pD3Ddevice->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
Expand Down
33 changes: 31 additions & 2 deletions GPU/GLES/TextureCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,19 @@ void TextureCache::ApplyTextureFramebuffer(TexCacheEntry *entry, VirtualFramebuf
depal = depalShaderCache_->GetDepalettizeShader(clutFormat, framebuffer->drawnFormat);
}
if (depal) {
GLuint clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBuf_);
GLuint clutTexture = 0;

VirtualFramebuffer *clutVfb = nullptr;
for (size_t i = 0, n = fbCache_.size(); i < n; ++i) {
auto clutFramebuffer = fbCache_[i];
if (clutFramebuffer->fb_address == clutRenderAddress_) {
clutVfb = clutFramebuffer;
}
}

if (!clutVfb) {
clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBuf_);
}
FBO *depalFBO = framebufferManager_->GetTempFBO(framebuffer->renderWidth, framebuffer->renderHeight, FBO_8888);
fbo_bind_as_render_target(depalFBO);
shaderManager_->DirtyLastShader();
Expand All @@ -1077,8 +1089,25 @@ void TextureCache::ApplyTextureFramebuffer(TexCacheEntry *entry, VirtualFramebuf
shaderApply.ApplyBounds(gstate_c.vertBounds, gstate_c.curTextureXOffset, gstate_c.curTextureYOffset);
shaderApply.Use();

if (depal->u_offset != -1) {
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);
} else {
glUniform2f(depal->u_offset, 1.0f, 0.0f);
}
}

glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, clutTexture);
if (clutVfb) {
fbo_bind_color_as_texture(clutVfb->fbo, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
} else {
glBindTexture(GL_TEXTURE_2D, clutTexture);
}
glActiveTexture(GL_TEXTURE0);

framebufferManager_->BindFramebufferColor(GL_TEXTURE0, gstate.getFrameBufRawAddress(), framebuffer, BINDFBCOLOR_SKIP_COPY);
Expand Down

0 comments on commit 86fb081

Please sign in to comment.