framebuffer-clut: Fixes to ES 2.0 path, slight simplification to ES 3.0 path

This commit is contained in:
Henrik Rydgard 2014-05-29 12:30:07 +02:00
parent 8b0043ae57
commit d4c7922c25

View File

@ -107,6 +107,7 @@ DepalShaderCache::~DepalShaderCache() {
#define WRITE p+=sprintf
// Uses integer instructions available since OpenGL 3.0. Suitable for ES 3.0 as well.
void GenerateDepalShader300(char *buffer, GEBufferFormat pixelFormat) {
char *p = buffer;
#ifdef USING_GLES
@ -121,7 +122,7 @@ void GenerateDepalShader300(char *buffer, GEBufferFormat pixelFormat) {
WRITE(p, "uniform sampler2D pal;\n");
WRITE(p, "void main() {\n");
WRITE(p, " vec4 index = texture2D(tex, v_texcoord0);\n");
WRITE(p, " vec4 color = texture2D(tex, v_texcoord0);\n");
int mask = gstate.getClutIndexMask();
int shift = gstate.getClutIndexShift();
@ -134,31 +135,31 @@ void GenerateDepalShader300(char *buffer, GEBufferFormat pixelFormat) {
int shiftedMask = mask << shift;
switch (pixelFormat) {
case GE_FORMAT_8888:
if (shiftedMask & 0xFF) WRITE(p, " int r = int(index.r * 255.99);\n"); else WRITE(p, " int r = 0;\n");
if (shiftedMask & 0xFF00) WRITE(p, " int g = int(index.g * 255.99);\n"); else WRITE(p, " int g = 0;\n");
if (shiftedMask & 0xFF0000) WRITE(p, " int b = int(index.b * 255.99);\n"); else WRITE(p, " int b = 0;\n");
if (shiftedMask & 0xFF000000) WRITE(p, " int a = int(index.a * 255.99);\n"); else WRITE(p, " int a = 0;\n");
WRITE(p, " int color = (a << 24) | (b << 16) | (g << 8) | (r);\n");
if (shiftedMask & 0xFF) WRITE(p, " int r = int(color.r * 255.99);\n"); else WRITE(p, " int r = 0;\n");
if (shiftedMask & 0xFF00) WRITE(p, " int g = int(color.g * 255.99);\n"); else WRITE(p, " int g = 0;\n");
if (shiftedMask & 0xFF0000) WRITE(p, " int b = int(color.b * 255.99);\n"); else WRITE(p, " int b = 0;\n");
if (shiftedMask & 0xFF000000) WRITE(p, " int a = int(color.a * 255.99);\n"); else WRITE(p, " int a = 0;\n");
WRITE(p, " int index = (a << 24) | (b << 16) | (g << 8) | (r);\n");
break;
case GE_FORMAT_4444:
if (shiftedMask & 0xF) WRITE(p, " int r = int(index.r * 15.99);\n"); else WRITE(p, " int r = 0;\n");
if (shiftedMask & 0xF0) WRITE(p, " int g = int(index.g * 15.99);\n"); else WRITE(p, " int g = 0;\n");
if (shiftedMask & 0xF00) WRITE(p, " int b = int(index.b * 15.99);\n"); else WRITE(p, " int b = 0;\n");
if (shiftedMask & 0xF000) WRITE(p, " int a = int(index.a * 15.99);\n"); else WRITE(p, " int a = 0;\n");
WRITE(p, " int color = (a << 12) | (b << 8) | (g << 4) | (r);\n");
if (shiftedMask & 0xF) WRITE(p, " int r = int(color.r * 15.99);\n"); else WRITE(p, " int r = 0;\n");
if (shiftedMask & 0xF0) WRITE(p, " int g = int(color.g * 15.99);\n"); else WRITE(p, " int g = 0;\n");
if (shiftedMask & 0xF00) WRITE(p, " int b = int(color.b * 15.99);\n"); else WRITE(p, " int b = 0;\n");
if (shiftedMask & 0xF000) WRITE(p, " int a = int(color.a * 15.99);\n"); else WRITE(p, " int a = 0;\n");
WRITE(p, " int index = (a << 12) | (b << 8) | (g << 4) | (r);\n");
break;
case GE_FORMAT_565:
if (shiftedMask & 0x1F) WRITE(p, " int r = int(index.r * 31.99);\n"); else WRITE(p, " int r = 0;\n");
if (shiftedMask & 0x7E0) WRITE(p, " int g = int(index.g * 63.99);\n"); else WRITE(p, " int g = 0;\n");
if (shiftedMask & 0xF800) WRITE(p, " int b = int(index.b * 31.99);\n"); else WRITE(p, " int b = 0;\n");
WRITE(p, " int color = (b << 11) | (g << 5) | (r);");
if (shiftedMask & 0x1F) WRITE(p, " int r = int(color.r * 31.99);\n"); else WRITE(p, " int r = 0;\n");
if (shiftedMask & 0x7E0) WRITE(p, " int g = int(color.g * 63.99);\n"); else WRITE(p, " int g = 0;\n");
if (shiftedMask & 0xF800) WRITE(p, " int b = int(color.b * 31.99);\n"); else WRITE(p, " int b = 0;\n");
WRITE(p, " int index = (b << 11) | (g << 5) | (r);");
break;
case GE_FORMAT_5551:
if (shiftedMask & 0x1F) WRITE(p, " int r = int(index.r * 31.99);\n"); else WRITE(p, " int r = 0;\n");
if (shiftedMask & 0x3E0) WRITE(p, " int g = int(index.g * 31.99);\n"); else WRITE(p, " int g = 0;\n");
if (shiftedMask & 0x7C00) WRITE(p, " int b = int(index.b * 31.99);\n"); else WRITE(p, " int b = 0;\n");
if (shiftedMask & 0x8000) WRITE(p, " int a = int(index.a);\n"); else WRITE(p, " int a = 0;\n");
WRITE(p, " int color = (a << 15) | (b << 10) | (g << 5) | (r);");
if (shiftedMask & 0x1F) WRITE(p, " int r = int(color.r * 31.99);\n"); else WRITE(p, " int r = 0;\n");
if (shiftedMask & 0x3E0) WRITE(p, " int g = int(color.g * 31.99);\n"); else WRITE(p, " int g = 0;\n");
if (shiftedMask & 0x7C00) WRITE(p, " int b = int(color.b * 31.99);\n"); else WRITE(p, " int b = 0;\n");
if (shiftedMask & 0x8000) WRITE(p, " int a = int(color.a);\n"); else WRITE(p, " int a = 0;\n");
WRITE(p, " int index = (a << 15) | (b << 10) | (g << 5) | (r);");
break;
}
@ -167,14 +168,21 @@ void GenerateDepalShader300(char *buffer, GEBufferFormat pixelFormat) {
texturePixels = 512;
if (shift) {
WRITE(p, " color = ((color >> %i) & 0x%02x) | %i;\n", shift, mask, offset); // '|' matches what we have in gstate.h
WRITE(p, " index = ((index >> %i) & 0x%02x)", shift, mask);
} else {
WRITE(p, " color = (color & 0x%02x) | %i;\n", mask, offset); // '|' matches what we have in gstate.h
WRITE(p, " index = (index & 0x%02x)", mask);
}
WRITE(p, " fragColor0 = texture2D(pal, vec2((floor(float(color)) + 0.5) * (1.0 / %f), 0.0));\n", texturePixels);
if (offset) {
WRITE(p, " | %i;\n", offset); // '|' matches what we have in gstate.h
} else {
WRITE(p, ";\n");
}
WRITE(p, " fragColor0 = texture2D(pal, vec2((float(index) + 0.5) * (1.0 / %f), 0.0));\n", texturePixels);
WRITE(p, "}\n");
}
// FP only, to suit GL(ES) 2.0
void GenerateDepalShader100(char *buffer, GEBufferFormat pixelFormat) {
char *p = buffer;
@ -187,21 +195,21 @@ void GenerateDepalShader100(char *buffer, GEBufferFormat pixelFormat) {
int shift = gstate.getClutIndexShift();
int mask = gstate.getClutIndexMask();
float multiplier = 1.0f;
float index_multiplier = 1.0f;
// pixelformat is the format of the texture we are sampling.
bool formatOK = true;
switch (pixelFormat) {
case GE_FORMAT_8888:
if ((mask & 0xF) == 0xF) {
switch (shift) { // bgra?
case 0: strcpy(lookupMethod, "index.r"); break;
case 4: strcpy(lookupMethod, "index.r"); multiplier = (1.0f / 16.0f); break;
case 0: strcpy(lookupMethod, "index.r"); break; // Also needs modulo... Same for the other three.
case 4: strcpy(lookupMethod, "index.r"); index_multiplier = (1.0f / 16.0f); break;
case 8: strcpy(lookupMethod, "index.g"); break;
case 12: strcpy(lookupMethod, "index.g"); multiplier = (1.0f / 16.0f); break;
case 12: strcpy(lookupMethod, "index.g"); index_multiplier = (1.0f / 16.0f); break;
case 16: strcpy(lookupMethod, "index.b"); break;
case 20: strcpy(lookupMethod, "index.b"); multiplier = (1.0f / 16.0f); break;
case 20: strcpy(lookupMethod, "index.b"); index_multiplier = (1.0f / 16.0f); break;
case 24: strcpy(lookupMethod, "index.a"); break;
case 28: strcpy(lookupMethod, "index.a"); multiplier = (1.0f / 16.0f); break;
case 28: strcpy(lookupMethod, "index.a"); index_multiplier = (1.0f / 16.0f); break;
default:
formatOK = false;
}
@ -219,7 +227,7 @@ void GenerateDepalShader100(char *buffer, GEBufferFormat pixelFormat) {
default:
formatOK = false;
}
multiplier = 1.0f / 16.0f;
index_multiplier = 15.0f / 256.0f;
} else {
formatOK = false;
}
@ -227,32 +235,39 @@ void GenerateDepalShader100(char *buffer, GEBufferFormat pixelFormat) {
case GE_FORMAT_565:
if ((mask & 0x3f) == 0x3F) {
switch (shift) { // bgra?
case 5: strcpy(lookupMethod, "index.g"); multiplier = 1.0f / 64.0f; break;
case 5: strcpy(lookupMethod, "index.g"); break;
default:
formatOK = false;
}
index_multiplier = 63.f / 256.f;
} else if ((mask & 0x1f) == 0x1f) {
switch (shift) { // bgra?
case 0: strcpy(lookupMethod, "index.r"); multiplier = 1.0f / 32.0f; break;
case 11: strcpy(lookupMethod, "index.b"); multiplier = 1.0f / 32.0f; break;
case 0: strcpy(lookupMethod, "index.r"); break;
case 11: strcpy(lookupMethod, "index.b"); break;
default:
formatOK = false;
}
formatOK = false;
index_multiplier = 31.f / 256.f;
}
break;
case GE_FORMAT_5551:
if ((mask & 0x1F) == 0x1F) {
switch (shift) { // bgra?
case 0: strcpy(lookupMethod, "index.r"); multiplier = 1.0f / 32.0f; break;
case 5: strcpy(lookupMethod, "index.g"); multiplier = 1.0f / 32.0f; break;
case 10: strcpy(lookupMethod, "index.b"); multiplier = 1.0f / 32.0f; break;
case 15: strcpy(lookupMethod, "index.a"); multiplier = 1.0f / 256.0f; break;
case 0: strcpy(lookupMethod, "index.r"); break;
case 5: strcpy(lookupMethod, "index.g"); break;
case 10: strcpy(lookupMethod, "index.b"); break;
default:
formatOK = false;
}
} else {
formatOK = false;
index_multiplier = 31.0f / 256.0f;
} else if ((mask & 0x1) == 0x1) {
switch (shift) { // bgra?
case 15: strcpy(lookupMethod, "index.a");break;
default:
formatOK = false;
}
index_multiplier = 1.0f / 256.0f;
}
break;
}
@ -260,15 +275,19 @@ void GenerateDepalShader100(char *buffer, GEBufferFormat pixelFormat) {
float texturePixels = 256.f;
if (clutFormat != GE_CMODE_32BIT_ABGR8888) {
texturePixels = 512.f;
multiplier *= 0.5f;
index_multiplier *= 0.5f;
}
// Adjust index_multiplier, similar to the use of 15.99 instead of 16 in the ES 3 path.
// index_multiplier -= 0.01f / texturePixels;
if (!formatOK) {
ERROR_LOG_REPORT_ONCE(depal, G3D, "%i depal unsupported: shift=%i mask=%02x offset=%i", pixelFormat, shift, mask, offset);
}
// Offset by half a texel (plus clutBase) to turn NEAREST filtering into FLOOR.
sprintf(offset, " + %f", (float)clutBase / texturePixels + 0.5f / texturePixels);
float texel_offset = ((float)clutBase + 0.5f) / texturePixels;
sprintf(offset, " + %f", texel_offset);
#ifdef USING_GLES
WRITE(p, "#version 100\n");
@ -281,7 +300,8 @@ void GenerateDepalShader100(char *buffer, GEBufferFormat pixelFormat) {
WRITE(p, "uniform sampler2D pal;\n");
WRITE(p, "void main() {\n");
WRITE(p, " vec4 index = texture2D(tex, v_texcoord0);\n");
WRITE(p, " gl_FragColor = texture2D(pal, vec2((%s * %f)%s, 0.0));\n", lookupMethod, multiplier, offset);
WRITE(p, " float coord = (%s * %f)%s;\n", lookupMethod, index_multiplier, offset);
WRITE(p, " gl_FragColor = texture2D(pal, vec2(coord, 0.0));\n");
WRITE(p, "}\n");
}