2014-03-30 00:11:01 +01:00
|
|
|
|
// Copyright (c) 2014- PPSSPP Project.
|
2014-03-29 21:58:38 +01:00
|
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
2014-03-30 00:11:01 +01:00
|
|
|
|
#include "base/logging.h"
|
2014-03-29 21:58:38 +01:00
|
|
|
|
#include "Common/Log.h"
|
2014-03-30 17:37:15 +02:00
|
|
|
|
#include "Core/Reporting.h"
|
2014-03-29 21:58:38 +01:00
|
|
|
|
#include "DepalettizeShader.h"
|
|
|
|
|
#include "GPU/GPUState.h"
|
|
|
|
|
#include "GPU/GLES/TextureCache.h"
|
|
|
|
|
|
2014-03-30 15:37:51 +02:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#define SHADERLOG
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static const char *depalVShader100 =
|
|
|
|
|
#ifdef USING_GLES
|
2014-03-29 21:58:38 +01:00
|
|
|
|
"#version 100\n"
|
2014-03-30 00:11:01 +01:00
|
|
|
|
"precision highp float;\n"
|
2014-03-30 15:37:51 +02:00
|
|
|
|
#endif
|
2014-03-29 21:58:38 +01:00
|
|
|
|
"attribute vec4 a_position;\n"
|
|
|
|
|
"attribute vec2 a_texcoord0;\n"
|
|
|
|
|
"varying vec2 v_texcoord0;\n"
|
|
|
|
|
"void main() {\n"
|
|
|
|
|
" v_texcoord0 = a_texcoord0;\n"
|
|
|
|
|
" gl_Position = a_position;\n"
|
|
|
|
|
"}\n";
|
|
|
|
|
|
2014-03-30 15:37:51 +02:00
|
|
|
|
static const char *depalVShader300 =
|
|
|
|
|
#ifdef USING_GLES
|
|
|
|
|
"#version 300 es\n"
|
|
|
|
|
"precision highp float;\n"
|
|
|
|
|
#else
|
|
|
|
|
"#version 330\n"
|
|
|
|
|
#endif
|
|
|
|
|
"in vec4 a_position;\n"
|
|
|
|
|
"in vec2 a_texcoord0;\n"
|
|
|
|
|
"out vec2 v_texcoord0;\n"
|
|
|
|
|
"void main() {\n"
|
|
|
|
|
" v_texcoord0 = a_texcoord0;\n"
|
|
|
|
|
" gl_Position = a_position;\n"
|
|
|
|
|
"}\n";
|
|
|
|
|
|
2014-03-29 21:58:38 +01:00
|
|
|
|
|
|
|
|
|
static bool CheckShaderCompileSuccess(GLuint shader, const char *code) {
|
|
|
|
|
GLint success;
|
|
|
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
|
|
|
|
if (!success) {
|
|
|
|
|
#define MAX_INFO_LOG_SIZE 2048
|
|
|
|
|
GLchar infoLog[MAX_INFO_LOG_SIZE];
|
|
|
|
|
GLsizei len;
|
|
|
|
|
glGetShaderInfoLog(shader, MAX_INFO_LOG_SIZE, &len, infoLog);
|
|
|
|
|
infoLog[len] = '\0';
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
|
ELOG("Error in shader compilation! %s\n", infoLog);
|
|
|
|
|
ELOG("Shader source:\n%s\n", (const char *)code);
|
|
|
|
|
#endif
|
|
|
|
|
ERROR_LOG(G3D, "Error in shader compilation!\n");
|
|
|
|
|
ERROR_LOG(G3D, "Info log: %s\n", infoLog);
|
|
|
|
|
ERROR_LOG(G3D, "Shader source:\n%s\n", (const char *)code);
|
|
|
|
|
#ifdef SHADERLOG
|
|
|
|
|
OutputDebugStringUTF8(infoLog);
|
|
|
|
|
#endif
|
|
|
|
|
shader = 0;
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
DEBUG_LOG(G3D, "Compiled shader:\n%s\n", (const char *)code);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DepalShaderCache::DepalShaderCache() {
|
|
|
|
|
// Pre-build the vertex program
|
|
|
|
|
vertexShader_ = glCreateShader(GL_VERTEX_SHADER);
|
2014-03-30 15:37:51 +02:00
|
|
|
|
glShaderSource(vertexShader_, 1, &depalVShader100, 0);
|
2014-03-29 21:58:38 +01:00
|
|
|
|
glCompileShader(vertexShader_);
|
|
|
|
|
|
2014-03-30 15:37:51 +02:00
|
|
|
|
if (CheckShaderCompileSuccess(vertexShader_, depalVShader100)) {
|
2014-03-29 21:58:38 +01:00
|
|
|
|
// ...
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DepalShaderCache::~DepalShaderCache() {
|
|
|
|
|
Clear();
|
|
|
|
|
glDeleteShader(vertexShader_);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-30 15:37:51 +02:00
|
|
|
|
void GenerateDepalShader100(char *buffer, GEBufferFormat pixelFormat) {
|
2014-03-29 21:58:38 +01:00
|
|
|
|
char *p = buffer;
|
|
|
|
|
#define WRITE p+=sprintf
|
|
|
|
|
|
|
|
|
|
char lookupMethod[128] = "index.r";
|
|
|
|
|
char offset[128] = "";
|
|
|
|
|
|
|
|
|
|
const GEPaletteFormat clutFormat = gstate.getClutPaletteFormat();
|
|
|
|
|
const u32 clutBase = gstate.getClutIndexStartPos();
|
|
|
|
|
|
|
|
|
|
int shift = gstate.getClutIndexShift();
|
|
|
|
|
int mask = gstate.getClutIndexMask();
|
|
|
|
|
|
2014-03-30 00:11:01 +01:00
|
|
|
|
float multiplier = 1.0f;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
// pixelformat is the format of the texture we are sampling.
|
2014-03-31 00:30:40 +02:00
|
|
|
|
bool formatOK = true;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
switch (pixelFormat) {
|
|
|
|
|
case GE_FORMAT_8888:
|
2014-03-30 01:24:23 +01:00
|
|
|
|
if ((mask & 0xF) == 0xF) {
|
2014-03-29 21:58:38 +01:00
|
|
|
|
switch (shift) { // bgra?
|
|
|
|
|
case 0: strcpy(lookupMethod, "index.r"); break;
|
2014-03-30 01:24:23 +01:00
|
|
|
|
case 4: strcpy(lookupMethod, "index.r"); multiplier = (1.0f / 16.0f); break;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
case 8: strcpy(lookupMethod, "index.g"); break;
|
2014-03-30 01:24:23 +01:00
|
|
|
|
case 12: strcpy(lookupMethod, "index.g"); multiplier = (1.0f / 16.0f); break;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
case 16: strcpy(lookupMethod, "index.b"); break;
|
2014-03-30 01:24:23 +01:00
|
|
|
|
case 20: strcpy(lookupMethod, "index.b"); multiplier = (1.0f / 16.0f); break;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
case 24: strcpy(lookupMethod, "index.a"); break;
|
2014-03-30 01:24:23 +01:00
|
|
|
|
case 28: strcpy(lookupMethod, "index.a"); multiplier = (1.0f / 16.0f); break;
|
2014-03-31 00:30:40 +02:00
|
|
|
|
default:
|
|
|
|
|
formatOK = false;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-03-31 00:30:40 +02:00
|
|
|
|
formatOK = false;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case GE_FORMAT_4444:
|
|
|
|
|
if ((mask & 0xF) == 0xF) {
|
|
|
|
|
switch (shift) { // bgra?
|
|
|
|
|
case 0: strcpy(lookupMethod, "index.r"); break;
|
|
|
|
|
case 4: strcpy(lookupMethod, "index.g"); break;
|
|
|
|
|
case 8: strcpy(lookupMethod, "index.b"); break;
|
|
|
|
|
case 12: strcpy(lookupMethod, "index.a"); break;
|
2014-03-31 00:30:40 +02:00
|
|
|
|
default:
|
|
|
|
|
formatOK = false;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
}
|
2014-03-30 11:43:23 +02:00
|
|
|
|
multiplier = 1.0f / 16.0f;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
} else {
|
2014-03-31 00:30:40 +02:00
|
|
|
|
formatOK = false;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case GE_FORMAT_565:
|
|
|
|
|
if ((mask & 0x3f) == 0x3F) {
|
|
|
|
|
switch (shift) { // bgra?
|
2014-03-30 01:06:11 +01:00
|
|
|
|
case 0: strcpy(lookupMethod, "index.r"); multiplier = 1.0f / 32.0f; break;
|
|
|
|
|
case 5: strcpy(lookupMethod, "index.g"); multiplier = 1.0f / 64.0f; break;
|
|
|
|
|
case 11: strcpy(lookupMethod, "index.b"); multiplier = 1.0f / 32.0f; break;
|
2014-03-31 00:30:40 +02:00
|
|
|
|
default:
|
|
|
|
|
formatOK = false;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-03-31 00:30:40 +02:00
|
|
|
|
formatOK = false;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case GE_FORMAT_5551:
|
|
|
|
|
if ((mask & 0x1F) == 0x1F) {
|
|
|
|
|
switch (shift) { // bgra?
|
2014-03-30 01:06:11 +01:00
|
|
|
|
case 0: strcpy(lookupMethod, "index.r"); multiplier = 1.0f / 32.0f; break;
|
2014-03-30 17:37:15 +02:00
|
|
|
|
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;
|
2014-03-31 00:30:40 +02:00
|
|
|
|
default:
|
|
|
|
|
formatOK = false;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-03-31 00:30:40 +02:00
|
|
|
|
formatOK = false;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-31 00:30:40 +02:00
|
|
|
|
if (!formatOK) {
|
|
|
|
|
ERROR_LOG_REPORT_ONCE(depal, G3D, "%i depal unsupported: shift=%i mask=%02x", pixelFormat, shift, mask);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-30 11:43:23 +02:00
|
|
|
|
// Offset by half a texel (plus clutBase) to turn NEAREST filtering into FLOOR.
|
2014-03-30 15:37:51 +02:00
|
|
|
|
sprintf(offset, " + %f", (float)clutBase / 256.0f - 0.5f / 256.0f);
|
2014-03-29 21:58:38 +01:00
|
|
|
|
|
2014-03-30 15:37:51 +02:00
|
|
|
|
#ifdef USING_GLES
|
2014-03-30 01:24:23 +01:00
|
|
|
|
WRITE(p, "#version 100\n");
|
|
|
|
|
WRITE(p, "precision mediump float;\n");
|
2014-03-30 15:37:51 +02:00
|
|
|
|
#else
|
|
|
|
|
WRITE(p, "#version 110\n");
|
|
|
|
|
#endif
|
2014-03-30 01:24:23 +01:00
|
|
|
|
WRITE(p, "varying vec2 v_texcoord0;\n");
|
|
|
|
|
WRITE(p, "uniform sampler2D tex;\n");
|
|
|
|
|
WRITE(p, "uniform sampler2D pal;\n");
|
|
|
|
|
WRITE(p, "void main() {\n");
|
|
|
|
|
WRITE(p, " vec4 index = texture2D(tex, v_texcoord0);\n");
|
2014-03-30 01:06:11 +01:00
|
|
|
|
WRITE(p, " gl_FragColor = texture2D(pal, vec2((%s * %f)%s, 0.0));\n", lookupMethod, multiplier, offset);
|
2014-03-29 21:58:38 +01:00
|
|
|
|
WRITE(p, "}\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u32 DepalShaderCache::GenerateShaderID(GEBufferFormat pixelFormat) {
|
|
|
|
|
return (gstate.clutformat & 0xFFFFFF) | (pixelFormat << 24);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GLuint DepalShaderCache::GetClutTexture(const u32 clutID, u32 *rawClut) {
|
|
|
|
|
auto oldtex = texCache_.find(clutID);
|
|
|
|
|
if (oldtex != texCache_.end()) {
|
|
|
|
|
return oldtex->second->texture;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GLuint dstFmt = getClutDestFormat(gstate.getClutPaletteFormat());
|
|
|
|
|
|
|
|
|
|
DepalTexture *tex = new DepalTexture();
|
|
|
|
|
glGenTextures(1, &tex->texture);
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, tex->texture);
|
|
|
|
|
GLuint components = dstFmt == GL_UNSIGNED_SHORT_5_6_5 ? GL_RGB : GL_RGBA;
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, components, 256, 1, 0, components, dstFmt, (void *)rawClut);
|
2014-03-30 01:06:11 +01:00
|
|
|
|
|
2014-03-29 21:58:38 +01:00
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
|
|
|
|
|
|
texCache_[clutID] = tex;
|
|
|
|
|
return tex->texture;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DepalShaderCache::Clear() {
|
2014-03-30 11:43:23 +02:00
|
|
|
|
for (auto shader = cache_.begin(); shader != cache_.end(); ++shader) {
|
|
|
|
|
glDeleteShader(shader->second->fragShader);
|
|
|
|
|
glDeleteProgram(shader->second->program);
|
|
|
|
|
delete shader->second;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
}
|
2014-03-30 17:37:15 +02:00
|
|
|
|
cache_.clear();
|
2014-03-30 11:43:23 +02:00
|
|
|
|
for (auto tex = texCache_.begin(); tex != texCache_.end(); ++tex) {
|
|
|
|
|
glDeleteTextures(1, &tex->second->texture);
|
|
|
|
|
delete tex->second;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
}
|
2014-03-30 17:37:15 +02:00
|
|
|
|
texCache_.clear();
|
2014-03-29 21:58:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DepalShaderCache::Decimate() {
|
|
|
|
|
// TODO
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GLuint DepalShaderCache::GetDepalettizeShader(GEBufferFormat pixelFormat) {
|
|
|
|
|
u32 id = GenerateShaderID(pixelFormat);
|
|
|
|
|
|
|
|
|
|
auto shader = cache_.find(id);
|
|
|
|
|
if (shader != cache_.end()) {
|
|
|
|
|
return shader->second->program;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-30 00:11:01 +01:00
|
|
|
|
char *buffer = new char[2048];
|
2014-03-29 21:58:38 +01:00
|
|
|
|
|
2014-03-30 15:37:51 +02:00
|
|
|
|
GenerateDepalShader100(buffer, pixelFormat);
|
2014-03-29 21:58:38 +01:00
|
|
|
|
|
2014-03-30 00:11:01 +01:00
|
|
|
|
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
|
|
|
|
|
|
const char *buf = buffer;
|
|
|
|
|
glShaderSource(fragShader, 1, &buf, 0);
|
|
|
|
|
glCompileShader(fragShader);
|
|
|
|
|
|
|
|
|
|
CheckShaderCompileSuccess(fragShader, buffer);
|
|
|
|
|
|
2014-03-29 21:58:38 +01:00
|
|
|
|
GLuint program = glCreateProgram();
|
|
|
|
|
glAttachShader(program, vertexShader_);
|
|
|
|
|
glAttachShader(program, fragShader);
|
|
|
|
|
|
|
|
|
|
glBindAttribLocation(program, 0, "a_position");
|
|
|
|
|
glBindAttribLocation(program, 1, "a_texcoord0");
|
|
|
|
|
|
|
|
|
|
glLinkProgram(program);
|
|
|
|
|
glUseProgram(program);
|
|
|
|
|
|
|
|
|
|
GLint u_tex = glGetUniformLocation(program, "tex");
|
|
|
|
|
GLint u_pal = glGetUniformLocation(program, "pal");
|
|
|
|
|
|
2014-03-30 01:06:11 +01:00
|
|
|
|
glUniform1i(u_tex, 0);
|
|
|
|
|
glUniform1i(u_pal, 1);
|
2014-03-29 21:58:38 +01:00
|
|
|
|
|
|
|
|
|
GLint linkStatus = GL_FALSE;
|
|
|
|
|
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
|
|
|
|
|
if (linkStatus != GL_TRUE) {
|
|
|
|
|
GLint bufLength = 0;
|
|
|
|
|
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
|
|
|
|
|
if (bufLength) {
|
2014-03-30 15:37:51 +02:00
|
|
|
|
char* errorbuf = new char[bufLength];
|
|
|
|
|
glGetProgramInfoLog(program, bufLength, NULL, errorbuf);
|
|
|
|
|
#ifdef SHADERLOG
|
|
|
|
|
OutputDebugStringUTF8(buffer);
|
|
|
|
|
OutputDebugStringUTF8(errorbuf);
|
|
|
|
|
#endif
|
|
|
|
|
ERROR_LOG(G3D, "Could not link program:\n %s \n\n %s", errorbuf, buf);
|
|
|
|
|
delete[] errorbuf; // we're dead!
|
2014-03-29 21:58:38 +01:00
|
|
|
|
}
|
2014-03-30 11:43:23 +02:00
|
|
|
|
|
|
|
|
|
delete[] buffer;
|
2014-03-29 21:58:38 +01:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DepalShader *depal = new DepalShader();
|
|
|
|
|
depal->program = program;
|
|
|
|
|
depal->fragShader = fragShader;
|
|
|
|
|
|
|
|
|
|
cache_[id] = depal;
|
2014-03-30 11:43:23 +02:00
|
|
|
|
|
|
|
|
|
delete[] buffer;
|
|
|
|
|
|
2014-03-29 21:58:38 +01:00
|
|
|
|
return depal->program;
|
|
|
|
|
}
|