Port the OpenGL depal path to thin3d

This commit is contained in:
Henrik Rydgård committed 2022-08-06 18:27:02 +02:00
1 parent 5f230ee998
commit 8aebf0dca4
15 files changed
+270 -227

No files matched your search

+2
View File
@@ -6,6 +6,8 @@
// Inspired by Rust's slices and Google's StringPiece.
template <class T>
struct Slice {
Slice() : data_(nullptr), size_(0) {}
// View some memory as a slice.
Slice(const T *data, size_t size) : data_(data), size_(size) {}
+15 -27
View File
@@ -261,9 +261,13 @@ void GLQueueRunner::RunInitSteps(const std::vector<GLRInitStep> &steps, bool ski
// Query all the uniforms.
for (size_t j = 0; j < program->queries_.size(); j++) {
auto &x = program->queries_[j];
_dbg_assert_(x.name);
*x.dest = glGetUniformLocation(program->program, x.name);
auto &query = program->queries_[j];
_dbg_assert_(query.name);
int location = glGetUniformLocation(program->program, query.name);
if (location < 0 && query.required) {
WARN_LOG(G3D, "Required uniform query for '%s' failed", query.name);
}
*query.dest = location;
}
// Run initializers.
@@ -966,18 +970,10 @@ void GLQueueRunner::PerformRenderPass(const GLRStep &step, bool first, bool last
}
if (loc >= 0) {
switch (c.uniform4.count) {
case 1:
glUniform1uiv(loc, 1, (GLuint *)&c.uniform4.v[0]);
break;
case 2:
glUniform2uiv(loc, 1, (GLuint *)c.uniform4.v);
break;
case 3:
glUniform3uiv(loc, 1, (GLuint *)c.uniform4.v);
break;
case 4:
glUniform4uiv(loc, 1, (GLuint *)c.uniform4.v);
break;
case 1: glUniform1uiv(loc, 1, (GLuint *)c.uniform4.v); break;
case 2: glUniform2uiv(loc, 1, (GLuint *)c.uniform4.v); break;
case 3: glUniform3uiv(loc, 1, (GLuint *)c.uniform4.v); break;
case 4: glUniform4uiv(loc, 1, (GLuint *)c.uniform4.v); break;
}
}
CHECK_GL_ERROR_IF_DEBUG();
@@ -992,18 +988,10 @@ void GLQueueRunner::PerformRenderPass(const GLRStep &step, bool first, bool last
}
if (loc >= 0) {
switch (c.uniform4.count) {
case 1:
glUniform1iv(loc, 1, (GLint *)&c.uniform4.v[0]);
break;
case 2:
glUniform2iv(loc, 1, (GLint *)c.uniform4.v);
break;
case 3:
glUniform3iv(loc, 1, (GLint *)c.uniform4.v);
break;
case 4:
glUniform4iv(loc, 1, (GLint *)c.uniform4.v);
break;
case 1: glUniform1iv(loc, 1, (GLint *)c.uniform4.v); break;
case 2: glUniform2iv(loc, 1, (GLint *)c.uniform4.v); break;
case 3: glUniform3iv(loc, 1, (GLint *)c.uniform4.v); break;
case 4: glUniform4iv(loc, 1, (GLint *)c.uniform4.v); break;
}
}
CHECK_GL_ERROR_IF_DEBUG();
+3 -2
View File
@@ -108,6 +108,7 @@ public:
struct UniformLocQuery {
GLint *dest;
const char *name;
bool required;
};
struct Initializer {
@@ -424,13 +425,13 @@ public:
// not be an active render pass.
GLRProgram *CreateProgram(
std::vector<GLRShader *> shaders, std::vector<GLRProgram::Semantic> semantics, std::vector<GLRProgram::UniformLocQuery> queries,
std::vector<GLRProgram::Initializer> initalizers, bool supportDualSource, bool useClipDistance0) {
std::vector<GLRProgram::Initializer> initializers, bool supportDualSource, bool useClipDistance0) {
GLRInitStep step{ GLRInitStepType::CREATE_PROGRAM };
_assert_(shaders.size() <= ARRAY_SIZE(step.create_program.shaders));
step.create_program.program = new GLRProgram();
step.create_program.program->semantics_ = semantics;
step.create_program.program->queries_ = queries;
step.create_program.program->initialize_ = initalizers;
step.create_program.program->initialize_ = initializers;
step.create_program.program->use_clip_distance0 = useClipDistance0;
step.create_program.support_dual_source = supportDualSource;
_assert_msg_(shaders.size() > 0, "Can't create a program with zero shaders");
+2 -22
View File
@@ -33,26 +33,6 @@ bool CompileShader(const char *source, GLuint shader, const char *filename, std:
return true;
}
GLSLProgram *glsl_create(const char *vshader, const char *fshader, std::string *error_message) {
GLSLProgram *program = new GLSLProgram();
program->program_ = 0;
program->vsh_ = 0;
program->fsh_ = 0;
program->vshader_source = 0;
program->fshader_source = 0;
strcpy(program->name, vshader + strlen(vshader) - 15);
strcpy(program->vshader_filename, vshader);
strcpy(program->fshader_filename, fshader);
if (glsl_recompile(program, error_message)) {
active_programs.insert(program);
} else {
ERROR_LOG(G3D, "Failed compiling GLSL program: %s %s", vshader, fshader);
delete program;
return 0;
}
return program;
}
GLSLProgram *glsl_create_source(const char *vshader_src, const char *fshader_src, std::string *error_message) {
GLSLProgram *program = new GLSLProgram();
program->program_ = 0;
@@ -201,8 +181,8 @@ bool glsl_recompile(GLSLProgram *program, std::string *error_message) {
program->sampler1 = glGetUniformLocation(program->program_, "sampler1");
program->a_position = glGetAttribLocation(program->program_, "a_position");
program->a_color = glGetAttribLocation(program->program_, "a_color");
program->a_normal = glGetAttribLocation(program->program_, "a_normal");
program->a_color = glGetAttribLocation(program->program_, "a_color");
program->a_normal = glGetAttribLocation(program->program_, "a_normal");
program->a_texcoord0 = glGetAttribLocation(program->program_, "a_texcoord0");
program->a_texcoord1 = glGetAttribLocation(program->program_, "a_texcoord1");
+25 -6
View File
@@ -294,6 +294,10 @@ public:
std::vector<GLint> dynamicUniformLocs_;
GLRProgram *program_ = nullptr;
// Allow using other sampler names than sampler0, sampler1 etc in shaders.
// If not set, will default to those, though.
Slice<SamplerDef> samplers_;
private:
GLRenderManager *render_;
};
@@ -1079,6 +1083,7 @@ Pipeline *OpenGLContext::CreateGraphicsPipeline(const PipelineDesc &desc) {
pipeline->dynamicUniforms = *desc.uniformDesc;
pipeline->dynamicUniformLocs_.resize(desc.uniformDesc->uniforms.size());
}
pipeline->samplers_ = desc.samplers;
if (pipeline->LinkShaders()) {
// Build the rest of the virtual pipeline object.
pipeline->prim = primToGL[(int)desc.prim];
@@ -1171,17 +1176,31 @@ bool OpenGLPipeline::LinkShaders() {
// For postshaders.
semantics.push_back({ SEM_POSITION, "a_position" });
semantics.push_back({ SEM_TEXCOORD0, "a_texcoord0" });
std::vector<GLRProgram::UniformLocQuery> queries;
queries.push_back({ &samplerLocs_[0], "sampler0" });
queries.push_back({ &samplerLocs_[1], "sampler1" });
queries.push_back({ &samplerLocs_[2], "sampler2" });
_assert_(queries.size() >= MAX_TEXTURE_SLOTS);
if (!samplers_.is_empty()) {
for (int i = 0; i < (int)std::min((const uint32_t)samplers_.size(), MAX_TEXTURE_SLOTS); i++) {
queries.push_back({ &samplerLocs_[i], samplers_[i].name, true });
}
} else {
queries.push_back({ &samplerLocs_[0], "sampler0" });
queries.push_back({ &samplerLocs_[1], "sampler1" });
queries.push_back({ &samplerLocs_[2], "sampler2" });
}
_assert_(queries.size() <= MAX_TEXTURE_SLOTS);
for (size_t i = 0; i < dynamicUniforms.uniforms.size(); ++i) {
queries.push_back({ &dynamicUniformLocs_[i], dynamicUniforms.uniforms[i].name });
}
std::vector<GLRProgram::Initializer> initialize;
for (int i = 0; i < MAX_TEXTURE_SLOTS; ++i)
initialize.push_back({ &samplerLocs_[i], 0, i });
for (int i = 0; i < MAX_TEXTURE_SLOTS; ++i) {
if (i < queries.size()) {
initialize.push_back({ &samplerLocs_[i], 0, i });
} else {
samplerLocs_[i] = -1;
}
}
program_ = render_->CreateProgram(linkShaders, semantics, queries, initialize, false, false);
return true;
}
+6
View File
@@ -85,6 +85,12 @@ struct UniformBufferDesc {
std::vector<UniformDesc> uniforms;
};
struct SamplerDef {
const char *name;
// TODO: Might need unsigned samplers, 3d samplers, or other types in the future.
};
// For passing error messages from shader compilation (and other critical issues) back to the host.
// This can run on any thread - be aware!
// TODO: See if we can find a less generic name for this.
+7
View File
@@ -317,6 +317,13 @@ void ShaderWriter::HighPrecisionFloat() {
}
}
void ShaderWriter::DeclareSamplers(Slice<SamplerDef> samplers) {
for (int i = 0; i < (int)samplers.size(); i++) {
DeclareTexture2D(samplers[i].name,i);
DeclareSampler2D(samplers[i].name, i);
}
}
void ShaderWriter::DeclareTexture2D(const char *name, int binding) {
switch (lang_.shaderLanguage) {
case HLSL_D3D11:
+2
View File
@@ -72,6 +72,8 @@ public:
void DeclareSampler2D(const char *name, int binding);
void DeclareTexture2D(const char *name, int binding);
void DeclareSamplers(Slice<SamplerDef> samplers);
ShaderWriter &SampleTexture2D(const char *texName, const char *samplerName, const char *uv);
// Simple shaders with no special tricks.
+3
View File
@@ -15,6 +15,7 @@
#include "Common/GPU/DataFormat.h"
#include "Common/GPU/Shader.h"
#include "Common/Data/Collections/Slice.h"
namespace Lin {
class Matrix4x4;
@@ -245,6 +246,7 @@ enum class NativeObject {
BOUND_TEXTURE0_IMAGEVIEW,
BOUND_TEXTURE1_IMAGEVIEW,
RENDER_MANAGER,
TEXTURE_IMAGEVIEW,
NULL_IMAGEVIEW,
};
@@ -509,6 +511,7 @@ struct PipelineDesc {
BlendState *blend;
RasterState *raster;
const UniformBufferDesc *uniformDesc;
const Slice<SamplerDef> samplers;
};
struct DeviceCaps {
+4 -3
View File
@@ -19,6 +19,9 @@
#include "Common/GPU/OpenGL/GLFeatures.h"
#include "Common/GPU/Shader.h"
#include "Common/GPU/ShaderWriter.h"
#include "GPU/Common/ShaderId.h"
#include "GPU/Common/ShaderCommon.h"
#include "Common/StringUtils.h"
@@ -30,15 +33,13 @@
#define WRITE p+=sprintf
// TODO: Add a compute shader path. Complete waste of time to set up a graphics state.
// Uses integer instructions available since OpenGL 3.0. Suitable for ES 3.0 as well.
void GenerateDepalShader300(char *buffer, GEBufferFormat pixelFormat, ShaderLanguage language) {
char *p = buffer;
if (language == HLSL_D3D11) {
WRITE(p, "SamplerState texSamp : register(s0);\n");
WRITE(p, "Texture2D<float4> tex : register(t0);\n");
WRITE(p, "Texture2D<float4> pal : register(t3);\n");
WRITE(p, "Texture2D<float4> pal : register(t1);\n");
// Support for depth.
if (pixelFormat == GE_FORMAT_DEPTH16) {
WRITE(p, "cbuffer params : register(b0) {\n");
+121 -85
View File
@@ -19,73 +19,52 @@
#include "Common/Log.h"
#include "Common/StringUtils.h"
#include "Common/GPU/Shader.h"
#include "Common/GPU/ShaderWriter.h"
#include "Core/Reporting.h"
#include "GPU/GLES/DepalettizeShaderGLES.h"
#include "GPU/GLES/DrawEngineGLES.h"
#include "GPU/GLES/TextureCacheGLES.h"
#include "GPU/Common/DrawEngineCommon.h"
#include "GPU/Common/TextureCacheCommon.h"
#include "GPU/Common/DepalettizeShaderCommon.h"
#include "GPU/GLES/DepalettizeShaderGLES.h"
static const char *depalVShader100 = R"(
#ifdef GL_ES
precision highp float;
#endif
attribute vec4 a_position;
attribute vec2 a_texcoord0;
varying vec2 v_texcoord0;
void main() {
v_texcoord0 = a_texcoord0;
gl_Position = a_position;
}
)";
static const InputDef vsInputs[2] = {
{ "vec2", "a_position", Draw::SEM_POSITION, },
{ "vec2", "a_texcoord0", Draw::SEM_TEXCOORD0, },
};
static const char *depalVShader300 = R"(
#ifdef GL_ES
precision highp float;
#endif
in vec4 a_position;
in vec2 a_texcoord0;
out vec2 v_texcoord0;
void main() {
v_texcoord0 = a_texcoord0;
gl_Position = a_position;
}
)";
static const VaryingDef varyings[1] = {
{ "vec2", "v_texcoord0", Draw::SEM_TEXCOORD0, 0, "highp" },
};
DepalShaderCacheGLES::DepalShaderCacheGLES(Draw::DrawContext *draw) {
_assert_(draw);
render_ = (GLRenderManager *)draw->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
useGL3_ = gl_extensions.GLES3 || gl_extensions.VersionGEThan(3, 3);
}
static const SamplerDef samplers[2] = {
{ "tex" },
{ "pal" },
};
void DepalShaderCacheGLES::Init() {
if (!gstate_c.Supports(GPU_SUPPORTS_32BIT_INT_FSHADER)) {
// Use the floating point path, it just can't handle the math.
useGL3_ = false;
}
}
DepalShaderCacheGLES::DepalShaderCacheGLES(Draw::DrawContext *draw) : draw_(draw) { }
DepalShaderCacheGLES::~DepalShaderCacheGLES() {
Clear();
DeviceLost();
}
void DepalShaderCacheGLES::DeviceRestore(Draw::DrawContext *draw) {
render_ = (GLRenderManager *)draw->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
draw_ = draw;
}
bool DepalShaderCacheGLES::CreateVertexShader() {
std::string src(useGL3_ ? depalVShader300 : depalVShader100);
std::string prelude;
if (gl_extensions.IsGLES) {
prelude = useGL3_ ? "#version 300 es\n" : "#version 100\n";
} else {
// We need to add a corresponding #version. Apple drivers fail without an exact match.
prelude = StringFromFormat("#version %d\n", gl_extensions.GLSLVersion());
}
vertexShader_ = render_->CreateShader(GL_VERTEX_SHADER, prelude + src, "depal");
void DepalShaderCacheGLES::DeviceLost() {
Clear();
}
bool DepalShaderCacheGLES::GenerateVertexShader(char *buffer, const ShaderLanguageDesc &lang) {
ShaderWriter writer(buffer, lang, ShaderStage::Vertex, nullptr, 0);
writer.BeginVSMain(vsInputs, Slice<UniformDef>::empty(), varyings);
writer.C(" v_texcoord0 = a_texcoord0;\n");
writer.C(" gl_Position = vec4(a_position, 0.0, 1.0);\n");
writer.EndVSMain(varyings);
return true;
}
GLRTexture *DepalShaderCacheGLES::GetClutTexture(GEPaletteFormat clutFormat, const u32 clutHash, u32 *rawClut) {
Draw::Texture *DepalShaderCacheGLES::GetClutTexture(GEPaletteFormat clutFormat, const u32 clutHash, u32 *rawClut) {
u32 clutId = GetClutID(clutFormat, clutHash);
auto oldtex = texCache_.find(clutId);
@@ -94,45 +73,75 @@ GLRTexture *DepalShaderCacheGLES::GetClutTexture(GEPaletteFormat clutFormat, con
return oldtex->second->texture;
}
Draw::DataFormat dstFmt = getClutDestFormat(clutFormat);
int texturePixels = clutFormat == GE_CMODE_32BIT_ABGR8888 ? 256 : 512;
DepalTexture *tex = new DepalTexture();
tex->texture = render_->CreateTexture(GL_TEXTURE_2D, texturePixels, 1, 1, 1);
uint8_t *clutCopy = new uint8_t[1024];
memcpy(clutCopy, rawClut, 1024);
render_->TextureImage(tex->texture, 0, texturePixels, 1, 1, dstFmt, clutCopy, GLRAllocType::NEW, false);
Draw::TextureDesc desc{};
desc.width = texturePixels;
desc.height = 1;
desc.depth = 1;
desc.mipLevels = 1;
desc.tag = "clut";
desc.type = Draw::TextureType::LINEAR2D; // TODO: Try LINEAR1D?
desc.format = Draw::DataFormat::R8G8B8A8_UNORM; // TODO: Also support an BGR format. We won't bother with the 16-bit formats here.
uint8_t convTemp[2048]{};
switch (clutFormat) {
case GEPaletteFormat::GE_CMODE_32BIT_ABGR8888:
//memcpy(desc.initData.data(), rawClut, texturePixels * 4);
desc.initData.push_back((const uint8_t *)rawClut);
break;
case GEPaletteFormat::GE_CMODE_16BIT_BGR5650:
// TODO
desc.initData.push_back(convTemp);
break;
case GEPaletteFormat::GE_CMODE_16BIT_ABGR5551:
// TODO
desc.initData.push_back(convTemp);
break;
case GEPaletteFormat::GE_CMODE_16BIT_ABGR4444:
// TODO
desc.initData.push_back(convTemp);
break;
}
tex->texture = draw_->CreateTexture(desc);
tex->lastFrame = gpuStats.numFlips;
texCache_[clutId] = tex;
return tex->texture;
}
void DepalShaderCacheGLES::Clear() {
for (auto shader = cache_.begin(); shader != cache_.end(); ++shader) {
render_->DeleteShader(shader->second->fragShader);
if (shader->second->program) {
render_->DeleteProgram(shader->second->program);
shader->second->fragShader->Release();
if (shader->second->pipeline) {
shader->second->pipeline->Release();
}
delete shader->second;
}
cache_.clear();
for (auto tex = texCache_.begin(); tex != texCache_.end(); ++tex) {
render_->DeleteTexture(tex->second->texture);
tex->second->texture->Release();
delete tex->second;
}
texCache_.clear();
if (vertexShader_) {
render_->DeleteShader(vertexShader_);
vertexShader_ = 0;
vertexShader_->Release();
vertexShader_ = nullptr;
}
if (nearestSampler_) {
nearestSampler_->Release();
nearestSampler_ = nullptr;
}
}
void DepalShaderCacheGLES::Decimate() {
for (auto tex = texCache_.begin(); tex != texCache_.end(); ) {
if (tex->second->lastFrame + DEPAL_TEXTURE_OLD_AGE < gpuStats.numFlips) {
render_->DeleteTexture(tex->second->texture);
tex->second->texture->Release();
delete tex->second;
texCache_.erase(tex++);
} else {
@@ -141,57 +150,84 @@ void DepalShaderCacheGLES::Decimate() {
}
}
Draw::SamplerState *DepalShaderCacheGLES::GetSampler() {
if (!nearestSampler_) {
Draw::SamplerStateDesc desc{};
desc.wrapU = Draw::TextureAddressMode::CLAMP_TO_EDGE;
desc.wrapV = Draw::TextureAddressMode::CLAMP_TO_EDGE;
desc.wrapW = Draw::TextureAddressMode::CLAMP_TO_EDGE;
nearestSampler_ = draw_->CreateSamplerState(desc);
}
return nearestSampler_;
}
DepalShader *DepalShaderCacheGLES::GetDepalettizeShader(uint32_t clutMode, GEBufferFormat pixelFormat) {
using namespace Draw;
u32 id = GenerateShaderID(clutMode, pixelFormat);
auto shader = cache_.find(id);
if (shader != cache_.end()) {
DepalShader *depal = shader->second;
// If compile failed previously, try to recover.
if (depal->fragShader->failed || vertexShader_->failed)
return nullptr;
return shader->second;
}
char *buffer = new char[4096];
if (!vertexShader_) {
if (!CreateVertexShader()) {
if (!GenerateVertexShader(buffer, draw_->GetShaderLanguageDesc())) {
// The vertex shader failed, no need to bother trying the fragment.
delete[] buffer;
return nullptr;
}
vertexShader_ = draw_->CreateShaderModule(ShaderStage::Vertex, draw_->GetShaderLanguageDesc().shaderLanguage, (const uint8_t *)buffer, strlen(buffer), "depal_vs");
}
char *buffer = new char[2048];
GenerateDepalShader(buffer, pixelFormat, useGL3_ ? GLSL_3xx : GLSL_1xx);
// TODO: Replace with ShaderWriter-based implementation.
GenerateDepalShader(buffer, pixelFormat, draw_->GetShaderLanguageDesc().shaderLanguage);
std::string src(buffer);
GLRShader *fragShader = render_->CreateShader(GL_FRAGMENT_SHADER, src, "depal");
ShaderModule *fragShader = draw_->CreateShaderModule(ShaderStage::Fragment, draw_->GetShaderLanguageDesc().shaderLanguage, (const uint8_t *)buffer, strlen(buffer), "depal_fs");
DepalShader *depal = new DepalShader();
std::vector<GLRProgram::Semantic> semantics;
semantics.push_back({ 0, "a_position" });
semantics.push_back({ 1, "a_texcoord0" });
static const InputLayoutDesc desc = {
{
{ 16, false },
},
{
{ 0, SEM_POSITION, DataFormat::R32G32_FLOAT, 0 },
{ 0, SEM_TEXCOORD0, DataFormat::R32G32_FLOAT, 8 },
},
};
InputLayout *inputLayout = draw_->CreateInputLayout(desc);
BlendState *blendOff = draw_->CreateBlendState({ false, 0xF });
DepthStencilStateDesc dsDesc{};
DepthStencilState *noDepthStencil = draw_->CreateDepthStencilState(dsDesc);
RasterState *rasterNoCull = draw_->CreateRasterState({});
std::vector<GLRProgram::UniformLocQuery> queries;
queries.push_back({ &depal->u_tex, "tex" });
queries.push_back({ &depal->u_pal, "pal" });
PipelineDesc depalPipelineDesc{
Primitive::TRIANGLE_STRIP, // Could have use a single triangle too (in which case we'd use LIST here) but want to be prepared to do subrectangles.
{ vertexShader_, fragShader },
inputLayout, noDepthStencil, blendOff, rasterNoCull, nullptr, samplers
};
Pipeline *pipeline = draw_->CreateGraphicsPipeline(depalPipelineDesc);
std::vector<GLRProgram::Initializer> initializer;
initializer.push_back({ &depal->u_tex, 0, TEX_SLOT_PSP_TEXTURE });
initializer.push_back({ &depal->u_pal, 0, TEX_SLOT_CLUT });
inputLayout->Release();
blendOff->Release();
noDepthStencil->Release();
rasterNoCull->Release();
std::vector<GLRShader *> shaders{ vertexShader_, fragShader };
_assert_(pipeline);
GLRProgram *program = render_->CreateProgram(shaders, semantics, queries, initializer, false, false);
depal->program = program;
depal->pipeline = pipeline;
depal->fragShader = fragShader;
depal->code = buffer;
cache_[id] = depal;
delete[] buffer;
return depal->program ? depal : nullptr;
return depal->pipeline ? depal : nullptr;
}
std::vector<std::string> DepalShaderCacheGLES::DebugGetShaderIDs(DebugShaderType type) {
+18 -16
View File
@@ -16,27 +16,26 @@
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <map>
#include <vector>
#include <string>
#include "Common/CommonTypes.h"
#include "Common/GPU/OpenGL/GLCommon.h"
#include "Common/GPU/Shader.h"
#include "Common/GPU/thin3d.h"
#include "Common/GPU/OpenGL/GLRenderManager.h"
#include "GPU/ge_constants.h"
#include "GPU/Common/ShaderCommon.h"
#include "GPU/Common/DepalettizeShaderCommon.h"
class DepalShader {
public:
GLRProgram *program;
GLRShader *fragShader;
GLint u_tex;
GLint u_pal;
Draw::ShaderModule *fragShader;
Draw::Pipeline *pipeline;
std::string code;
};
class DepalTexture {
public:
GLRTexture *texture;
Draw::Texture *texture;
int lastFrame;
};
@@ -48,24 +47,27 @@ public:
// This also uploads the palette and binds the correct texture.
DepalShader *GetDepalettizeShader(uint32_t clutMode, GEBufferFormat pixelFormat);
GLRTexture *GetClutTexture(GEPaletteFormat clutFormat, const u32 clutHash, u32 *rawClut);
void Init();
Draw::Texture *GetClutTexture(GEPaletteFormat clutFormat, const u32 clutHash, u32 *rawClut);
Draw::SamplerState *GetSampler();
void Clear();
void Decimate();
std::vector<std::string> DebugGetShaderIDs(DebugShaderType type);
std::string DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType);
void DeviceLost() {
Clear();
}
void DeviceLost();
void DeviceRestore(Draw::DrawContext *draw);
// Exposed for testing.
static bool GenerateVertexShader(char *buffer, const ShaderLanguageDesc &lang);
private:
bool CreateVertexShader();
Draw::DrawContext *draw_;
Draw::ShaderModule *vertexShader_ = nullptr;
Draw::SamplerState *nearestSampler_ = nullptr;
GLRenderManager *render_;
bool useGL3_;
GLRShader *vertexShader_ = nullptr;
std::map<u32, DepalShader *> cache_;
std::map<u32, DepalTexture *> texCache_;
};
-1
View File
@@ -77,7 +77,6 @@ GPU_GLES::GPU_GLES(GraphicsContext *gfxCtx, Draw::DrawContext *draw)
framebufferManagerGL_->SetShaderManager(shaderManagerGL_);
framebufferManagerGL_->SetDrawEngine(&drawEngine_);
framebufferManagerGL_->Init();
depalShaderCache_.Init();
textureCacheGL_->SetFramebufferManager(framebufferManagerGL_);
textureCacheGL_->SetDepalShaderCache(&depalShaderCache_);
textureCacheGL_->SetShaderManager(shaderManagerGL_);
+59 -63
View File
@@ -50,17 +50,9 @@ TextureCacheGLES::TextureCacheGLES(Draw::DrawContext *draw)
render_ = (GLRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
nextTexture_ = nullptr;
std::vector<GLRInputLayout::Entry> entries;
entries.push_back({ 0, 3, GL_FLOAT, GL_FALSE, 20, 0 });
entries.push_back({ 1, 2, GL_FLOAT, GL_FALSE, 20, 12 });
shadeInputLayout_ = render_->CreateInputLayout(entries);
}
TextureCacheGLES::~TextureCacheGLES() {
if (shadeInputLayout_) {
render_->DeleteInputLayout(shadeInputLayout_);
}
Clear(true);
}
@@ -93,7 +85,7 @@ Draw::DataFormat getClutDestFormat(GEPaletteFormat format) {
case GE_CMODE_32BIT_ABGR8888:
return Draw::DataFormat::R8G8B8A8_UNORM;
}
return Draw::DataFormat::UNDEFINED;;
return Draw::DataFormat::UNDEFINED;
}
static const GLuint MinFiltGL[8] = {
@@ -239,33 +231,33 @@ void TextureCacheGLES::Unbind() {
InvalidateLastTexture();
}
// TODO: Move this thing directly into Depal.
class TextureShaderApplier {
public:
struct Pos {
float x;
float y;
float z;
};
struct UV {
float u;
float v;
};
TextureShaderApplier(DepalShader *shader, float bufferW, float bufferH, int renderW, int renderH)
: shader_(shader), bufferW_(bufferW), bufferH_(bufferH), renderW_(renderW), renderH_(renderH) {
TextureShaderApplier(Draw::DrawContext *draw, DepalShader *shader, float bufferW, float bufferH, int renderW, int renderH)
: draw_(draw), shader_(shader), bufferW_(bufferW), bufferH_(bufferH), renderW_(renderW), renderH_(renderH) {
static const Pos pos[4] = {
{-1, -1, -1},
{ 1, -1, -1},
{ 1, 1, -1},
{-1, 1, -1},
{-1, -1 },
{ 1, -1 },
{-1, 1 },
{ 1, 1 },
};
memcpy(pos_, pos, sizeof(pos_));
static const UV uv[4] = {
{0, 0},
{1, 0},
{1, 1},
{0, 1},
{ 0, 0 },
{ 1, 0 },
{ 0, 1 },
{ 1, 1 },
};
memcpy(uv_, uv, sizeof(uv_));
}
@@ -289,10 +281,10 @@ public:
const float top = v1 * invHalfHeight - 1.0f;
const float bottom = v2 * invHalfHeight - 1.0f;
// Points are: BL, BR, TR, TL.
pos_[0] = Pos{ left, bottom, -1.0f };
pos_[1] = Pos{ right, bottom, -1.0f };
pos_[2] = Pos{ right, top, -1.0f };
pos_[3] = Pos{ left, top, -1.0f };
pos_[0] = Pos{ left, bottom };
pos_[1] = Pos{ right, bottom };
pos_[2] = Pos{ left, top };
pos_[3] = Pos{ right, top };
// And also the UVs, same order.
const float uvleft = u1 * invWidth;
@@ -301,53 +293,58 @@ public:
const float uvbottom = v2 * invHeight;
uv_[0] = UV{ uvleft, uvbottom };
uv_[1] = UV{ uvright, uvbottom };
uv_[2] = UV{ uvright, uvtop };
uv_[3] = UV{ uvleft, uvtop };
uv_[2] = UV{ uvleft, uvtop };
uv_[3] = UV{ uvright, uvtop };
// We need to reapply the texture next time since we cropped UV.
gstate_c.Dirty(DIRTY_TEXTURE_PARAMS);
}
}
void Use(GLRenderManager *render, DrawEngineGLES *transformDraw, GLRInputLayout *inputLayout) {
render->BindProgram(shader_->program);
void Use(DrawEngineGLES *transformDraw) {
draw_->BindPipeline(shader_->pipeline);
struct SimpleVertex {
float pos[3];
float pos[2];
float uv[2];
};
uint32_t bindOffset;
GLRBuffer *bindBuffer;
SimpleVertex *verts = (SimpleVertex *)transformDraw->GetPushVertexBuffer()->Push(sizeof(SimpleVertex) * 4, &bindOffset, &bindBuffer);
int order[4] = { 0 ,1, 3, 2 };
for (int i = 0; i < 4; i++) {
memcpy(verts[i].pos, &pos_[order[i]], sizeof(Pos));
memcpy(verts[i].uv, &uv_[order[i]], sizeof(UV));
memcpy(&verts_[i].x, &pos_[i], sizeof(Pos));
memcpy(&verts_[i].u, &uv_[i], sizeof(UV));
}
render->BindVertexBuffer(inputLayout, bindBuffer, bindOffset);
}
void Shade(GLRenderManager *render) {
render->SetViewport(GLRViewport{ 0, 0, (float)renderW_, (float)renderH_, 0.0f, 1.0f });
render->Draw(GL_TRIANGLE_STRIP, 0, 4);
void Shade() {
Draw::Viewport vp{ 0.0f, 0.0f, (float)renderW_, (float)renderH_, 0.0f, 1.0f };
draw_->SetViewports(1, &vp);
draw_->SetScissorRect(0, 0, renderW_, renderH_);
draw_->DrawUP((const uint8_t *)verts_, 4);
}
protected:
Draw::DrawContext *draw_;
DepalShader *shader_;
Pos pos_[4];
UV uv_[4];
Draw2DVertex verts_[4];
float bufferW_;
float bufferH_;
int renderW_;
int renderH_;
};
void TextureCacheGLES::BindAsClutTexture(Draw::Texture *tex) {
draw_->BindTexture(1, tex);
GLRTexture *glrTex = (GLRTexture *)draw_->GetNativeObject(Draw::NativeObject::BOUND_TEXTURE1_IMAGEVIEW);
render_->BindTexture(TEX_SLOT_CLUT, glrTex);
}
void TextureCacheGLES::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer, GETextureFormat texFormat, FramebufferNotificationChannel channel) {
DepalShader *depalShader = nullptr;
uint32_t clutMode = gstate.clutformat & 0xFFFFFF;
bool need_depalettize = IsClutFormat(texFormat);
bool depth = channel == NOTIFY_FB_DEPTH;
bool useShaderDepal = framebufferManager_->GetCurrentRenderVFB() != framebuffer && (gstate_c.Supports(GPU_SUPPORTS_GLSL_ES_300) || gstate_c.Supports(GPU_SUPPORTS_GLSL_330)) && !depth;
bool useShaderDepal = false && framebufferManager_->GetCurrentRenderVFB() != framebuffer && (gstate_c.Supports(GPU_SUPPORTS_GLSL_ES_300) || gstate_c.Supports(GPU_SUPPORTS_GLSL_330)) && !depth;
if (!gstate_c.Supports(GPU_SUPPORTS_32BIT_INT_FSHADER)) {
useShaderDepal = false;
depth = false; // Can't support this
@@ -356,8 +353,10 @@ void TextureCacheGLES::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer,
if (need_depalettize && !g_Config.bDisableSlowFramebufEffects) {
if (useShaderDepal) {
const GEPaletteFormat clutFormat = gstate.getClutPaletteFormat();
GLRTexture *clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBuf_);
render_->BindTexture(TEX_SLOT_CLUT, clutTexture);
// Very icky conflation here of native and thin3d rendering. This will need careful work per backend.
Draw::Texture *clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBuf_);
BindAsClutTexture(clutTexture);
render_->SetTextureSampler(TEX_SLOT_CLUT, GL_REPEAT, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST, 0.0f);
framebufferManagerGL_->BindFramebufferAsColorTexture(0, framebuffer, BINDFBCOLOR_MAY_COPY_WITH_UV | BINDFBCOLOR_APPLY_TEX_OFFSET);
SamplerCacheKey samplerKey = GetFramebufferSamplingParams(framebuffer->bufferWidth, framebuffer->bufferHeight);
@@ -381,26 +380,30 @@ void TextureCacheGLES::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer,
depalShader = depalShaderCache_->GetDepalettizeShader(clutMode, depth ? GE_FORMAT_DEPTH16 : framebuffer->drawnFormat);
gstate_c.SetUseShaderDepal(false);
}
if (depalShader) {
shaderManager_->DirtyLastShader();
// This is the part we can easily make generic.
const GEPaletteFormat clutFormat = gstate.getClutPaletteFormat();
GLRTexture *clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBuf_);
Draw::Framebuffer *depalFBO = framebufferManagerGL_->GetTempFBO(TempFBO::DEPAL, framebuffer->renderWidth, framebuffer->renderHeight);
Draw::Texture *clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBuf_);
Draw::Framebuffer *depalFBO = framebufferManager_->GetTempFBO(TempFBO::DEPAL, framebuffer->renderWidth, framebuffer->renderHeight);
draw_->BindFramebufferAsRenderTarget(depalFBO, { Draw::RPAction::DONT_CARE, Draw::RPAction::DONT_CARE, Draw::RPAction::DONT_CARE }, "Depal");
render_->SetScissor(GLRect2D{ 0, 0, (int)framebuffer->renderWidth, (int)framebuffer->renderHeight });
render_->SetViewport(GLRViewport{ 0.0f, 0.0f, (float)framebuffer->renderWidth, (float)framebuffer->renderHeight, 0.0f, 1.0f });
TextureShaderApplier shaderApply(depalShader, framebuffer->bufferWidth, framebuffer->bufferHeight, framebuffer->renderWidth, framebuffer->renderHeight);
draw_->SetScissorRect(0, 0, (int)framebuffer->renderWidth, (int)framebuffer->renderHeight);
Draw::Viewport vp{ 0.0f, 0.0f, (float)framebuffer->renderWidth, (float)framebuffer->renderHeight, 0.0f, 1.0f };
draw_->SetViewports(1, &vp);
TextureShaderApplier shaderApply(draw_, depalShader, framebuffer->bufferWidth, framebuffer->bufferHeight, framebuffer->renderWidth, framebuffer->renderHeight);
shaderApply.ApplyBounds(gstate_c.vertBounds, gstate_c.curTextureXOffset, gstate_c.curTextureYOffset);
shaderApply.Use(render_, drawEngine_, shadeInputLayout_);
shaderApply.Use(drawEngine_);
draw_->BindFramebufferAsTexture(framebuffer->fbo, 0, depth ? Draw::FB_DEPTH_BIT : Draw::FB_COLOR_BIT, 0);
Draw::SamplerState *nearest = depalShaderCache_->GetSampler();
draw_->BindSamplerStates(0, 1, &nearest);
draw_->BindSamplerStates(1, 1, &nearest);
draw_->BindTexture(1, clutTexture);
render_->BindTexture(TEX_SLOT_CLUT, clutTexture);
render_->SetTextureSampler(TEX_SLOT_CLUT, GL_REPEAT, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST, 0.0f);
shaderApply.Shade(render_);
shaderApply.Shade();
draw_->BindFramebufferAsTexture(depalFBO, 0, Draw::FB_COLOR_BIT, 0);
@@ -409,6 +412,9 @@ void TextureCacheGLES::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer,
CheckAlphaResult alphaStatus = CheckAlpha((const uint8_t *)clutBuf_, getClutDestFormat(clutFormat), clutTotalColors);
gstate_c.SetTextureFullAlpha(alphaStatus == CHECKALPHA_FULL);
draw_->InvalidateCachedState();
shaderManager_->DirtyLastShader();
} else {
framebufferManagerGL_->BindFramebufferAsColorTexture(0, framebuffer, BINDFBCOLOR_MAY_COPY_WITH_UV | BINDFBCOLOR_APPLY_TEX_OFFSET);
@@ -639,10 +645,6 @@ bool TextureCacheGLES::GetCurrentTextureDebug(GPUDebugBuffer &buffer, int level)
}
void TextureCacheGLES::DeviceLost() {
if (shadeInputLayout_) {
render_->DeleteInputLayout(shadeInputLayout_);
shadeInputLayout_ = nullptr;
}
Clear(false);
draw_ = nullptr;
render_ = nullptr;
@@ -651,10 +653,4 @@ void TextureCacheGLES::DeviceLost() {
void TextureCacheGLES::DeviceRestore(Draw::DrawContext *draw) {
draw_ = draw;
render_ = (GLRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
if (!shadeInputLayout_) {
std::vector<GLRInputLayout::Entry> entries;
entries.push_back({ 0, 3, GL_FLOAT, GL_FALSE, 20, 0 });
entries.push_back({ 1, 2, GL_FLOAT, GL_FALSE, 20, 12 });
shadeInputLayout_ = render_->CreateInputLayout(entries);
}
}
+3 -2
View File
@@ -20,6 +20,7 @@
#include "Common/GPU/OpenGL/GLFeatures.h"
#include "Common/GPU/OpenGL/GLCommon.h"
#include "Common/GPU/OpenGL/GLRenderManager.h"
#include "Common/GPU/thin3d.h"
#include "GPU/GPUInterface.h"
#include "GPU/GPUState.h"
#include "GPU/Common/TextureCacheCommon.h"
@@ -67,6 +68,8 @@ protected:
void Unbind() override;
void ReleaseTexture(TexCacheEntry *entry, bool delete_them) override;
void BindAsClutTexture(Draw::Texture *tex); // override
private:
void ApplySamplingParams(const SamplerCacheKey &key);
Draw::DataFormat GetDestFormat(GETextureFormat format, GEPaletteFormat clutFormat) const;
@@ -86,8 +89,6 @@ private:
ShaderManagerGLES *shaderManager_;
DrawEngineGLES *drawEngine_;
GLRInputLayout *shadeInputLayout_ = nullptr;
enum { INVALID_TEX = -1 };
};