ppsspp/GPU/GLES/DepalettizeShaderGLES.cpp

209 lines
5.9 KiB
C++
Raw Normal View History

// 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>
#include "base/logging.h"
2014-03-29 21:58:38 +01:00
#include "Common/Log.h"
#include "Common/StringUtils.h"
#include "Core/Reporting.h"
#include "DepalettizeShaderGLES.h"
#include "GPU/GLES/TextureCacheGLES.h"
#include "GPU/Common/DepalettizeShaderCommon.h"
2014-03-29 21:58:38 +01:00
#ifdef _WIN32
#define SHADERLOG
#endif
static const char *depalVShader100 =
#ifdef USING_GLES2
2014-03-29 21:58:38 +01:00
"#version 100\n"
"precision highp float;\n"
#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";
static const char *depalVShader300 =
#ifdef USING_GLES2
"#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";
2017-11-19 17:37:56 +01:00
DepalShaderCacheGLES::DepalShaderCacheGLES(Draw::DrawContext *draw) {
render_ = (GLRenderManager *)draw->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
2014-03-29 21:58:38 +01:00
// Pre-build the vertex program
2014-05-28 08:47:54 +02:00
useGL3_ = gl_extensions.GLES3 || gl_extensions.VersionGEThan(3, 3);
vertexShaderFailed_ = false;
vertexShader_ = 0;
}
DepalShaderCacheGLES::~DepalShaderCacheGLES() {
Clear();
}
bool DepalShaderCacheGLES::CreateVertexShader() {
2017-11-19 17:37:56 +01:00
std::string src(useGL3_ ? depalVShader300 : depalVShader100);
vertexShader_ = render_->CreateShader(GL_VERTEX_SHADER, src, "depal");
2017-11-19 17:37:56 +01:00
return true;
2014-03-29 21:58:38 +01:00
}
2017-11-19 17:37:56 +01:00
GLRTexture *DepalShaderCacheGLES::GetClutTexture(GEPaletteFormat clutFormat, const u32 clutHash, u32 *rawClut) {
2017-11-05 10:40:21 +01:00
u32 clutId = GetClutID(clutFormat, clutHash);
2017-11-05 10:40:21 +01:00
auto oldtex = texCache_.find(clutId);
2014-03-29 21:58:38 +01:00
if (oldtex != texCache_.end()) {
oldtex->second->lastFrame = gpuStats.numFlips;
2014-03-29 21:58:38 +01:00
return oldtex->second->texture;
}
GLuint dstFmt = getClutDestFormat(clutFormat);
int texturePixels = clutFormat == GE_CMODE_32BIT_ABGR8888 ? 256 : 512;
2014-03-29 21:58:38 +01:00
DepalTexture *tex = new DepalTexture();
2017-11-19 17:37:56 +01:00
tex->texture = render_->CreateTexture(GL_TEXTURE_2D);
2014-03-29 21:58:38 +01:00
GLuint components = dstFmt == GL_UNSIGNED_SHORT_5_6_5 ? GL_RGB : GL_RGBA;
GLuint components2 = components;
2014-06-02 12:08:49 +10:00
uint8_t *clutCopy = new uint8_t[1024];
memcpy(clutCopy, rawClut, 1024);
render_->TextureImage(tex->texture, 0, texturePixels, 1, components, components2, dstFmt, clutCopy, false);
2014-03-29 21:58:38 +01:00
tex->lastFrame = gpuStats.numFlips;
2017-11-05 10:40:21 +01:00
texCache_[clutId] = tex;
2014-03-29 21:58:38 +01:00
return tex->texture;
}
void DepalShaderCacheGLES::Clear() {
for (auto shader = cache_.begin(); shader != cache_.end(); ++shader) {
2017-11-19 17:37:56 +01:00
render_->DeleteShader(shader->second->fragShader);
if (shader->second->program) {
2017-11-19 17:37:56 +01:00
render_->DeleteProgram(shader->second->program);
}
delete shader->second;
2014-03-29 21:58:38 +01:00
}
cache_.clear();
for (auto tex = texCache_.begin(); tex != texCache_.end(); ++tex) {
2017-11-19 17:37:56 +01:00
render_->DeleteTexture(tex->second->texture);
delete tex->second;
2014-03-29 21:58:38 +01:00
}
texCache_.clear();
if (vertexShader_) {
2017-11-19 17:37:56 +01:00
render_->DeleteShader(vertexShader_);
vertexShader_ = 0;
}
2014-03-29 21:58:38 +01:00
}
void DepalShaderCacheGLES::Decimate() {
for (auto tex = texCache_.begin(); tex != texCache_.end(); ) {
if (tex->second->lastFrame + DEPAL_TEXTURE_OLD_AGE < gpuStats.numFlips) {
2017-11-19 17:37:56 +01:00
render_->DeleteTexture(tex->second->texture);
delete tex->second;
texCache_.erase(tex++);
} else {
++tex;
}
}
2014-03-29 21:58:38 +01:00
}
DepalShader *DepalShaderCacheGLES::GetDepalettizeShader(uint32_t clutMode, GEBufferFormat pixelFormat) {
u32 id = GenerateShaderID(clutMode, pixelFormat);
2014-03-29 21:58:38 +01:00
auto shader = cache_.find(id);
if (shader != cache_.end()) {
return shader->second;
2014-03-29 21:58:38 +01:00
}
if (vertexShader_ == 0) {
if (!CreateVertexShader()) {
// The vertex shader failed, no need to bother trying the fragment.
return nullptr;
}
}
char *buffer = new char[2048];
2014-03-29 21:58:38 +01:00
GenerateDepalShader(buffer, pixelFormat, useGL3_ ? GLSL_300 : GLSL_140);
2017-11-19 17:37:56 +01:00
std::string src(buffer);
GLRShader *fragShader = render_->CreateShader(GL_FRAGMENT_SHADER, src, "depal");
2014-03-29 21:58:38 +01:00
2017-11-19 17:37:56 +01:00
DepalShader *depal = new DepalShader();
2017-11-19 17:37:56 +01:00
std::vector<GLRProgram::Semantic> semantics;
semantics.push_back({ 0, "a_position" });
semantics.push_back({ 1, "a_texcoord0" });
2017-11-19 17:37:56 +01:00
std::vector<GLRProgram::UniformLocQuery> queries;
queries.push_back({ &depal->u_tex, "tex" });
queries.push_back({ &depal->u_pal, "pal" });
2014-03-29 21:58:38 +01:00
2017-11-19 17:37:56 +01:00
std::vector<GLRProgram::Initializer> initializer;
initializer.push_back({ &depal->u_tex, 0, 0 });
initializer.push_back({ &depal->u_pal, 0, 3 });
2014-03-29 21:58:38 +01:00
2017-11-19 17:37:56 +01:00
std::vector<GLRShader *> shaders{ vertexShader_, fragShader };
2014-03-29 21:58:38 +01:00
2017-11-19 17:37:56 +01:00
GLRProgram *program = render_->CreateProgram(shaders, semantics, queries, initializer, false);
2014-03-29 21:58:38 +01:00
depal->program = program;
depal->fragShader = fragShader;
depal->code = buffer;
cache_[id] = depal;
delete[] buffer;
return depal->program ? depal : nullptr;
2014-03-29 21:58:38 +01:00
}
std::vector<std::string> DepalShaderCacheGLES::DebugGetShaderIDs(DebugShaderType type) {
std::vector<std::string> ids;
for (auto &iter : cache_) {
ids.push_back(StringFromFormat("%08x", iter.first));
}
return ids;
}
std::string DepalShaderCacheGLES::DebugGetShaderString(std::string idstr, DebugShaderType type, DebugShaderStringType stringType) {
uint32_t id;
sscanf(idstr.c_str(), "%08x", &id);
auto iter = cache_.find(id);
if (iter == cache_.end())
return "";
switch (stringType) {
case SHADER_STRING_SHORT_DESC:
return idstr;
case SHADER_STRING_SOURCE_CODE:
return iter->second->code;
default:
return "";
}
}