aboutsummaryrefslogtreecommitdiff
path: root/src/video_core/renderer_opengl
diff options
context:
space:
mode:
Diffstat (limited to 'src/video_core/renderer_opengl')
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.cpp383
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.h188
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer_cache.cpp7
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer_cache.h8
-rw-r--r--src/video_core/renderer_opengl/gl_resource_manager.h2
-rw-r--r--src/video_core/renderer_opengl/gl_shader_gen.cpp392
-rw-r--r--src/video_core/renderer_opengl/gl_shader_gen.h27
-rw-r--r--src/video_core/renderer_opengl/gl_shader_util.cpp7
-rw-r--r--src/video_core/renderer_opengl/gl_shader_util.h20
-rw-r--r--src/video_core/renderer_opengl/gl_shaders.h337
-rw-r--r--src/video_core/renderer_opengl/gl_state.cpp13
-rw-r--r--src/video_core/renderer_opengl/gl_state.h3
-rw-r--r--src/video_core/renderer_opengl/renderer_opengl.cpp55
13 files changed, 758 insertions, 684 deletions
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp
index a613fe136..092351dce 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp
@@ -8,6 +8,8 @@
#include <glad/glad.h>
#include "common/color.h"
+#include "common/file_util.h"
+#include "common/make_unique.h"
#include "common/math_util.h"
#include "common/microprofile.h"
#include "common/profiler.h"
@@ -19,7 +21,7 @@
#include "video_core/pica.h"
#include "video_core/utils.h"
#include "video_core/renderer_opengl/gl_rasterizer.h"
-#include "video_core/renderer_opengl/gl_shaders.h"
+#include "video_core/renderer_opengl/gl_shader_gen.h"
#include "video_core/renderer_opengl/gl_shader_util.h"
#include "video_core/renderer_opengl/pica_to_gl.h"
@@ -38,69 +40,42 @@ RasterizerOpenGL::RasterizerOpenGL() : last_fb_color_addr(0), last_fb_depth_addr
RasterizerOpenGL::~RasterizerOpenGL() { }
void RasterizerOpenGL::InitObjects() {
- // Create the hardware shader program and get attrib/uniform locations
- shader.Create(GLShaders::g_vertex_shader_hw, GLShaders::g_fragment_shader_hw);
- attrib_position = glGetAttribLocation(shader.handle, "vert_position");
- attrib_color = glGetAttribLocation(shader.handle, "vert_color");
- attrib_texcoords = glGetAttribLocation(shader.handle, "vert_texcoords");
-
- uniform_alphatest_enabled = glGetUniformLocation(shader.handle, "alphatest_enabled");
- uniform_alphatest_func = glGetUniformLocation(shader.handle, "alphatest_func");
- uniform_alphatest_ref = glGetUniformLocation(shader.handle, "alphatest_ref");
-
- uniform_tex = glGetUniformLocation(shader.handle, "tex");
-
- uniform_tev_combiner_buffer_color = glGetUniformLocation(shader.handle, "tev_combiner_buffer_color");
-
- const auto tev_stages = Pica::g_state.regs.GetTevStages();
- for (unsigned tev_stage_index = 0; tev_stage_index < tev_stages.size(); ++tev_stage_index) {
- auto& uniform_tev_cfg = uniform_tev_cfgs[tev_stage_index];
-
- std::string tev_ref_str = "tev_cfgs[" + std::to_string(tev_stage_index) + "]";
- uniform_tev_cfg.enabled = glGetUniformLocation(shader.handle, (tev_ref_str + ".enabled").c_str());
- uniform_tev_cfg.color_sources = glGetUniformLocation(shader.handle, (tev_ref_str + ".color_sources").c_str());
- uniform_tev_cfg.alpha_sources = glGetUniformLocation(shader.handle, (tev_ref_str + ".alpha_sources").c_str());
- uniform_tev_cfg.color_modifiers = glGetUniformLocation(shader.handle, (tev_ref_str + ".color_modifiers").c_str());
- uniform_tev_cfg.alpha_modifiers = glGetUniformLocation(shader.handle, (tev_ref_str + ".alpha_modifiers").c_str());
- uniform_tev_cfg.color_alpha_op = glGetUniformLocation(shader.handle, (tev_ref_str + ".color_alpha_op").c_str());
- uniform_tev_cfg.color_alpha_multiplier = glGetUniformLocation(shader.handle, (tev_ref_str + ".color_alpha_multiplier").c_str());
- uniform_tev_cfg.const_color = glGetUniformLocation(shader.handle, (tev_ref_str + ".const_color").c_str());
- uniform_tev_cfg.updates_combiner_buffer_color_alpha = glGetUniformLocation(shader.handle, (tev_ref_str + ".updates_combiner_buffer_color_alpha").c_str());
- }
-
// Create sampler objects
for (size_t i = 0; i < texture_samplers.size(); ++i) {
texture_samplers[i].Create();
state.texture_units[i].sampler = texture_samplers[i].sampler.handle;
}
- // Generate VBO and VAO
+ // Generate VBO, VAO and UBO
vertex_buffer.Create();
vertex_array.Create();
+ uniform_buffer.Create();
- // Update OpenGL state
state.draw.vertex_array = vertex_array.handle;
state.draw.vertex_buffer = vertex_buffer.handle;
- state.draw.shader_program = shader.handle;
-
+ state.draw.uniform_buffer = uniform_buffer.handle;
state.Apply();
- // Set the texture samplers to correspond to different texture units
- glUniform1i(uniform_tex, 0);
- glUniform1i(uniform_tex + 1, 1);
- glUniform1i(uniform_tex + 2, 2);
+ // Bind the UBO to binding point 0
+ glBindBufferBase(GL_UNIFORM_BUFFER, 0, uniform_buffer.handle);
+
+ uniform_block_data.dirty = true;
// Set vertex attributes
- glVertexAttribPointer(attrib_position, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, position));
- glVertexAttribPointer(attrib_color, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, color));
- glVertexAttribPointer(attrib_texcoords, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord0));
- glVertexAttribPointer(attrib_texcoords + 1, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord1));
- glVertexAttribPointer(attrib_texcoords + 2, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord2));
- glEnableVertexAttribArray(attrib_position);
- glEnableVertexAttribArray(attrib_color);
- glEnableVertexAttribArray(attrib_texcoords);
- glEnableVertexAttribArray(attrib_texcoords + 1);
- glEnableVertexAttribArray(attrib_texcoords + 2);
+ glVertexAttribPointer(GLShader::ATTRIBUTE_POSITION, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, position));
+ glEnableVertexAttribArray(GLShader::ATTRIBUTE_POSITION);
+
+ glVertexAttribPointer(GLShader::ATTRIBUTE_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, color));
+ glEnableVertexAttribArray(GLShader::ATTRIBUTE_COLOR);
+
+ glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD0, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord0));
+ glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD1, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord1));
+ glVertexAttribPointer(GLShader::ATTRIBUTE_TEXCOORD2, 2, GL_FLOAT, GL_FALSE, sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, tex_coord2));
+ glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD0);
+ glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD1);
+ glEnableVertexAttribArray(GLShader::ATTRIBUTE_TEXCOORD2);
+
+ SetShader();
// Create textures for OGL framebuffer that will be rendered to, initially 1x1 to succeed in framebuffer creation
fb_color_texture.texture.Create();
@@ -150,63 +125,17 @@ void RasterizerOpenGL::InitObjects() {
}
void RasterizerOpenGL::Reset() {
- const auto& regs = Pica::g_state.regs;
-
SyncCullMode();
SyncBlendEnabled();
SyncBlendFuncs();
SyncBlendColor();
- SyncAlphaTest();
SyncLogicOp();
SyncStencilTest();
SyncDepthTest();
- // TEV stage 0
- SyncTevSources(0, regs.tev_stage0);
- SyncTevModifiers(0, regs.tev_stage0);
- SyncTevOps(0, regs.tev_stage0);
- SyncTevColor(0, regs.tev_stage0);
- SyncTevMultipliers(0, regs.tev_stage0);
-
- // TEV stage 1
- SyncTevSources(1, regs.tev_stage1);
- SyncTevModifiers(1, regs.tev_stage1);
- SyncTevOps(1, regs.tev_stage1);
- SyncTevColor(1, regs.tev_stage1);
- SyncTevMultipliers(1, regs.tev_stage1);
-
- // TEV stage 2
- SyncTevSources(2, regs.tev_stage2);
- SyncTevModifiers(2, regs.tev_stage2);
- SyncTevOps(2, regs.tev_stage2);
- SyncTevColor(2, regs.tev_stage2);
- SyncTevMultipliers(2, regs.tev_stage2);
-
- // TEV stage 3
- SyncTevSources(3, regs.tev_stage3);
- SyncTevModifiers(3, regs.tev_stage3);
- SyncTevOps(3, regs.tev_stage3);
- SyncTevColor(3, regs.tev_stage3);
- SyncTevMultipliers(3, regs.tev_stage3);
-
- // TEV stage 4
- SyncTevSources(4, regs.tev_stage4);
- SyncTevModifiers(4, regs.tev_stage4);
- SyncTevOps(4, regs.tev_stage4);
- SyncTevColor(4, regs.tev_stage4);
- SyncTevMultipliers(4, regs.tev_stage4);
-
- // TEV stage 5
- SyncTevSources(5, regs.tev_stage5);
- SyncTevModifiers(5, regs.tev_stage5);
- SyncTevOps(5, regs.tev_stage5);
- SyncTevColor(5, regs.tev_stage5);
- SyncTevMultipliers(5, regs.tev_stage5);
+ SetShader();
- SyncCombinerColor();
- SyncCombinerWriteFlags();
-
- res_cache.FullFlush();
+ res_cache.InvalidateAll();
}
void RasterizerOpenGL::AddTriangle(const Pica::Shader::OutputVertex& v0,
@@ -221,6 +150,16 @@ void RasterizerOpenGL::DrawTriangles() {
SyncFramebuffer();
SyncDrawState();
+ if (state.draw.shader_dirty) {
+ SetShader();
+ state.draw.shader_dirty = false;
+ }
+
+ if (uniform_block_data.dirty) {
+ glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformData), &uniform_block_data.data, GL_STATIC_DRAW);
+ uniform_block_data.dirty = false;
+ }
+
glBufferData(GL_ARRAY_BUFFER, vertex_batch.size() * sizeof(HardwareVertex), vertex_batch.data(), GL_STREAM_DRAW);
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertex_batch.size());
@@ -237,11 +176,11 @@ void RasterizerOpenGL::DrawTriangles() {
u32 cur_fb_depth_size = Pica::Regs::BytesPerDepthPixel(regs.framebuffer.depth_format)
* regs.framebuffer.GetWidth() * regs.framebuffer.GetHeight();
- res_cache.NotifyFlush(cur_fb_color_addr, cur_fb_color_size, true);
- res_cache.NotifyFlush(cur_fb_depth_addr, cur_fb_depth_size, true);
+ res_cache.InvalidateInRange(cur_fb_color_addr, cur_fb_color_size, true);
+ res_cache.InvalidateInRange(cur_fb_depth_addr, cur_fb_depth_size, true);
}
-void RasterizerOpenGL::CommitFramebuffer() {
+void RasterizerOpenGL::FlushFramebuffer() {
CommitColorBuffer();
CommitDepthBuffer();
}
@@ -249,9 +188,6 @@ void RasterizerOpenGL::CommitFramebuffer() {
void RasterizerOpenGL::NotifyPicaRegisterChanged(u32 id) {
const auto& regs = Pica::g_state.regs;
- if (!Settings::values.use_hw_renderer)
- return;
-
switch(id) {
// Culling
case PICA_REG_INDEX(cull_mode):
@@ -272,6 +208,7 @@ void RasterizerOpenGL::NotifyPicaRegisterChanged(u32 id) {
// Alpha test
case PICA_REG_INDEX(output_merger.alpha_test):
SyncAlphaTest();
+ state.draw.shader_dirty = true;
break;
// Stencil test
@@ -290,126 +227,63 @@ void RasterizerOpenGL::NotifyPicaRegisterChanged(u32 id) {
SyncLogicOp();
break;
- // TEV stage 0
+ // TEV stages
case PICA_REG_INDEX(tev_stage0.color_source1):
- SyncTevSources(0, regs.tev_stage0);
- break;
case PICA_REG_INDEX(tev_stage0.color_modifier1):
- SyncTevModifiers(0, regs.tev_stage0);
- break;
case PICA_REG_INDEX(tev_stage0.color_op):
- SyncTevOps(0, regs.tev_stage0);
- break;
- case PICA_REG_INDEX(tev_stage0.const_r):
- SyncTevColor(0, regs.tev_stage0);
- break;
case PICA_REG_INDEX(tev_stage0.color_scale):
- SyncTevMultipliers(0, regs.tev_stage0);
- break;
-
- // TEV stage 1
case PICA_REG_INDEX(tev_stage1.color_source1):
- SyncTevSources(1, regs.tev_stage1);
- break;
case PICA_REG_INDEX(tev_stage1.color_modifier1):
- SyncTevModifiers(1, regs.tev_stage1);
- break;
case PICA_REG_INDEX(tev_stage1.color_op):
- SyncTevOps(1, regs.tev_stage1);
- break;
- case PICA_REG_INDEX(tev_stage1.const_r):
- SyncTevColor(1, regs.tev_stage1);
- break;
case PICA_REG_INDEX(tev_stage1.color_scale):
- SyncTevMultipliers(1, regs.tev_stage1);
- break;
-
- // TEV stage 2
case PICA_REG_INDEX(tev_stage2.color_source1):
- SyncTevSources(2, regs.tev_stage2);
- break;
case PICA_REG_INDEX(tev_stage2.color_modifier1):
- SyncTevModifiers(2, regs.tev_stage2);
- break;
case PICA_REG_INDEX(tev_stage2.color_op):
- SyncTevOps(2, regs.tev_stage2);
- break;
- case PICA_REG_INDEX(tev_stage2.const_r):
- SyncTevColor(2, regs.tev_stage2);
- break;
case PICA_REG_INDEX(tev_stage2.color_scale):
- SyncTevMultipliers(2, regs.tev_stage2);
- break;
-
- // TEV stage 3
case PICA_REG_INDEX(tev_stage3.color_source1):
- SyncTevSources(3, regs.tev_stage3);
- break;
case PICA_REG_INDEX(tev_stage3.color_modifier1):
- SyncTevModifiers(3, regs.tev_stage3);
- break;
case PICA_REG_INDEX(tev_stage3.color_op):
- SyncTevOps(3, regs.tev_stage3);
- break;
- case PICA_REG_INDEX(tev_stage3.const_r):
- SyncTevColor(3, regs.tev_stage3);
- break;
case PICA_REG_INDEX(tev_stage3.color_scale):
- SyncTevMultipliers(3, regs.tev_stage3);
- break;
-
- // TEV stage 4
case PICA_REG_INDEX(tev_stage4.color_source1):
- SyncTevSources(4, regs.tev_stage4);
- break;
case PICA_REG_INDEX(tev_stage4.color_modifier1):
- SyncTevModifiers(4, regs.tev_stage4);
- break;
case PICA_REG_INDEX(tev_stage4.color_op):
- SyncTevOps(4, regs.tev_stage4);
+ case PICA_REG_INDEX(tev_stage4.color_scale):
+ case PICA_REG_INDEX(tev_stage5.color_source1):
+ case PICA_REG_INDEX(tev_stage5.color_modifier1):
+ case PICA_REG_INDEX(tev_stage5.color_op):
+ case PICA_REG_INDEX(tev_stage5.color_scale):
+ case PICA_REG_INDEX(tev_combiner_buffer_input):
+ state.draw.shader_dirty = true;
break;
- case PICA_REG_INDEX(tev_stage4.const_r):
- SyncTevColor(4, regs.tev_stage4);
+ case PICA_REG_INDEX(tev_stage0.const_r):
+ SyncTevConstColor(0, regs.tev_stage0);
break;
- case PICA_REG_INDEX(tev_stage4.color_scale):
- SyncTevMultipliers(4, regs.tev_stage4);
+ case PICA_REG_INDEX(tev_stage1.const_r):
+ SyncTevConstColor(1, regs.tev_stage1);
break;
-
- // TEV stage 5
- case PICA_REG_INDEX(tev_stage5.color_source1):
- SyncTevSources(5, regs.tev_stage5);
+ case PICA_REG_INDEX(tev_stage2.const_r):
+ SyncTevConstColor(2, regs.tev_stage2);
break;
- case PICA_REG_INDEX(tev_stage5.color_modifier1):
- SyncTevModifiers(5, regs.tev_stage5);
+ case PICA_REG_INDEX(tev_stage3.const_r):
+ SyncTevConstColor(3, regs.tev_stage3);
break;
- case PICA_REG_INDEX(tev_stage5.color_op):
- SyncTevOps(5, regs.tev_stage5);
+ case PICA_REG_INDEX(tev_stage4.const_r):
+ SyncTevConstColor(4, regs.tev_stage4);
break;
case PICA_REG_INDEX(tev_stage5.const_r):
- SyncTevColor(5, regs.tev_stage5);
- break;
- case PICA_REG_INDEX(tev_stage5.color_scale):
- SyncTevMultipliers(5, regs.tev_stage5);
+ SyncTevConstColor(5, regs.tev_stage5);
break;
// TEV combiner buffer color
case PICA_REG_INDEX(tev_combiner_buffer_color):
SyncCombinerColor();
break;
-
- // TEV combiner buffer write flags
- case PICA_REG_INDEX(tev_combiner_buffer_input):
- SyncCombinerWriteFlags();
- break;
}
}
-void RasterizerOpenGL::NotifyPreRead(PAddr addr, u32 size) {
+void RasterizerOpenGL::FlushRegion(PAddr addr, u32 size) {
const auto& regs = Pica::g_state.regs;
- if (!Settings::values.use_hw_renderer)
- return;
-
PAddr cur_fb_color_addr = regs.framebuffer.GetColorBufferPhysicalAddress();
u32 cur_fb_color_size = Pica::Regs::BytesPerColorPixel(regs.framebuffer.color_format)
* regs.framebuffer.GetWidth() * regs.framebuffer.GetHeight();
@@ -426,12 +300,9 @@ void RasterizerOpenGL::NotifyPreRead(PAddr addr, u32 size) {
CommitDepthBuffer();
}
-void RasterizerOpenGL::NotifyFlush(PAddr addr, u32 size) {
+void RasterizerOpenGL::InvalidateRegion(PAddr addr, u32 size) {
const auto& regs = Pica::g_state.regs;
- if (!Settings::values.use_hw_renderer)
- return;
-
PAddr cur_fb_color_addr = regs.framebuffer.GetColorBufferPhysicalAddress();
u32 cur_fb_color_size = Pica::Regs::BytesPerColorPixel(regs.framebuffer.color_format)
* regs.framebuffer.GetWidth() * regs.framebuffer.GetHeight();
@@ -448,7 +319,7 @@ void RasterizerOpenGL::NotifyFlush(PAddr addr, u32 size) {
ReloadDepthBuffer();
// Notify cache of flush in case the region touches a cached resource
- res_cache.NotifyFlush(addr, size);
+ res_cache.InvalidateInRange(addr, size);
}
void RasterizerOpenGL::SamplerInfo::Create() {
@@ -592,6 +463,47 @@ void RasterizerOpenGL::ReconfigureDepthTexture(DepthTextureInfo& texture, Pica::
state.Apply();
}
+void RasterizerOpenGL::SetShader() {
+ PicaShaderConfig config = PicaShaderConfig::CurrentConfig();
+ std::unique_ptr<PicaShader> shader = Common::make_unique<PicaShader>();
+
+ // Find (or generate) the GLSL shader for the current TEV state
+ auto cached_shader = shader_cache.find(config);
+ if (cached_shader != shader_cache.end()) {
+ current_shader = cached_shader->second.get();
+
+ state.draw.shader_program = current_shader->shader.handle;
+ state.Apply();
+ } else {
+ LOG_DEBUG(Render_OpenGL, "Creating new shader");
+
+ shader->shader.Create(GLShader::GenerateVertexShader().c_str(), GLShader::GenerateFragmentShader(config).c_str());
+
+ state.draw.shader_program = shader->shader.handle;
+ state.Apply();
+
+ // Set the texture samplers to correspond to different texture units
+ GLuint uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[0]");
+ if (uniform_tex != -1) { glUniform1i(uniform_tex, 0); }
+ uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[1]");
+ if (uniform_tex != -1) { glUniform1i(uniform_tex, 1); }
+ uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[2]");
+ if (uniform_tex != -1) { glUniform1i(uniform_tex, 2); }
+
+ current_shader = shader_cache.emplace(config, std::move(shader)).first->second.get();
+
+ unsigned int block_index = glGetUniformBlockIndex(current_shader->shader.handle, "shader_data");
+ glUniformBlockBinding(current_shader->shader.handle, block_index, 0);
+ }
+
+ // Update uniforms
+ SyncAlphaTest();
+ SyncCombinerColor();
+ auto& tev_stages = Pica::g_state.regs.GetTevStages();
+ for (int index = 0; index < tev_stages.size(); ++index)
+ SyncTevConstColor(index, tev_stages[index]);
+}
+
void RasterizerOpenGL::SyncFramebuffer() {
const auto& regs = Pica::g_state.regs;
@@ -675,12 +587,12 @@ void RasterizerOpenGL::SyncCullMode() {
case Pica::Regs::CullMode::KeepClockWise:
state.cull.enabled = true;
- state.cull.mode = GL_BACK;
+ state.cull.front_face = GL_CW;
break;
case Pica::Regs::CullMode::KeepCounterClockWise:
state.cull.enabled = true;
- state.cull.mode = GL_FRONT;
+ state.cull.front_face = GL_CCW;
break;
default:
@@ -712,9 +624,10 @@ void RasterizerOpenGL::SyncBlendColor() {
void RasterizerOpenGL::SyncAlphaTest() {
const auto& regs = Pica::g_state.regs;
- glUniform1i(uniform_alphatest_enabled, regs.output_merger.alpha_test.enable);
- glUniform1i(uniform_alphatest_func, (GLint)regs.output_merger.alpha_test.func.Value());
- glUniform1f(uniform_alphatest_ref, regs.output_merger.alpha_test.ref / 255.0f);
+ if (regs.output_merger.alpha_test.ref != uniform_block_data.data.alphatest_ref) {
+ uniform_block_data.data.alphatest_ref = regs.output_merger.alpha_test.ref;
+ uniform_block_data.dirty = true;
+ }
}
void RasterizerOpenGL::SyncLogicOp() {
@@ -744,55 +657,19 @@ void RasterizerOpenGL::SyncDepthTest() {
state.depth.write_mask = regs.output_merger.depth_write_enable ? GL_TRUE : GL_FALSE;
}
-void RasterizerOpenGL::SyncTevSources(unsigned stage_index, const Pica::Regs::TevStageConfig& config) {
- GLint color_srcs[3] = { (GLint)config.color_source1.Value(),
- (GLint)config.color_source2.Value(),
- (GLint)config.color_source3.Value() };
- GLint alpha_srcs[3] = { (GLint)config.alpha_source1.Value(),
- (GLint)config.alpha_source2.Value(),
- (GLint)config.alpha_source3.Value() };
-
- glUniform3iv(uniform_tev_cfgs[stage_index].color_sources, 1, color_srcs);
- glUniform3iv(uniform_tev_cfgs[stage_index].alpha_sources, 1, alpha_srcs);
-}
-
-void RasterizerOpenGL::SyncTevModifiers(unsigned stage_index, const Pica::Regs::TevStageConfig& config) {
- GLint color_mods[3] = { (GLint)config.color_modifier1.Value(),
- (GLint)config.color_modifier2.Value(),
- (GLint)config.color_modifier3.Value() };
- GLint alpha_mods[3] = { (GLint)config.alpha_modifier1.Value(),
- (GLint)config.alpha_modifier2.Value(),
- (GLint)config.alpha_modifier3.Value() };
-
- glUniform3iv(uniform_tev_cfgs[stage_index].color_modifiers, 1, color_mods);
- glUniform3iv(uniform_tev_cfgs[stage_index].alpha_modifiers, 1, alpha_mods);
-}
-
-void RasterizerOpenGL::SyncTevOps(unsigned stage_index, const Pica::Regs::TevStageConfig& config) {
- glUniform2i(uniform_tev_cfgs[stage_index].color_alpha_op, (GLint)config.color_op.Value(), (GLint)config.alpha_op.Value());
-}
-
-void RasterizerOpenGL::SyncTevColor(unsigned stage_index, const Pica::Regs::TevStageConfig& config) {
- auto const_color = PicaToGL::ColorRGBA8(config.const_color);
- glUniform4fv(uniform_tev_cfgs[stage_index].const_color, 1, const_color.data());
-}
-
-void RasterizerOpenGL::SyncTevMultipliers(unsigned stage_index, const Pica::Regs::TevStageConfig& config) {
- glUniform2i(uniform_tev_cfgs[stage_index].color_alpha_multiplier, config.GetColorMultiplier(), config.GetAlphaMultiplier());
-}
-
void RasterizerOpenGL::SyncCombinerColor() {
auto combiner_color = PicaToGL::ColorRGBA8(Pica::g_state.regs.tev_combiner_buffer_color.raw);
- glUniform4fv(uniform_tev_combiner_buffer_color, 1, combiner_color.data());
+ if (combiner_color != uniform_block_data.data.tev_combiner_buffer_color) {
+ uniform_block_data.data.tev_combiner_buffer_color = combiner_color;
+ uniform_block_data.dirty = true;
+ }
}
-void RasterizerOpenGL::SyncCombinerWriteFlags() {
- const auto& regs = Pica::g_state.regs;
- const auto tev_stages = regs.GetTevStages();
- for (unsigned tev_stage_index = 0; tev_stage_index < tev_stages.size(); ++tev_stage_index) {
- glUniform2i(uniform_tev_cfgs[tev_stage_index].updates_combiner_buffer_color_alpha,
- regs.tev_combiner_buffer_input.TevStageUpdatesCombinerBufferColor(tev_stage_index),
- regs.tev_combiner_buffer_input.TevStageUpdatesCombinerBufferAlpha(tev_stage_index));
+void RasterizerOpenGL::SyncTevConstColor(int stage_index, const Pica::Regs::TevStageConfig& tev_stage) {
+ auto const_color = PicaToGL::ColorRGBA8(tev_stage.const_color);
+ if (const_color != uniform_block_data.data.const_color[stage_index]) {
+ uniform_block_data.data.const_color[stage_index] = const_color;
+ uniform_block_data.dirty = true;
}
}
@@ -806,9 +683,8 @@ void RasterizerOpenGL::SyncDrawState() {
// OpenGL uses different y coordinates, so negate corner offset and flip origin
// TODO: Ensure viewport_corner.x should not be negated or origin flipped
// TODO: Use floating-point viewports for accuracy if supported
- glViewport((GLsizei)static_cast<float>(regs.viewport_corner.x),
- -(GLsizei)static_cast<float>(regs.viewport_corner.y)
- + regs.framebuffer.GetHeight() - viewport_height,
+ glViewport((GLsizei)regs.viewport_corner.x,
+ (GLsizei)regs.viewport_corner.y,
viewport_width, viewport_height);
// Sync bound texture(s), upload if not cached
@@ -824,12 +700,7 @@ void RasterizerOpenGL::SyncDrawState() {
}
}
- // Skip processing TEV stages that simply pass the previous stage results through
- const auto tev_stages = regs.GetTevStages();
- for (unsigned tev_stage_index = 0; tev_stage_index < tev_stages.size(); ++tev_stage_index) {
- glUniform1i(uniform_tev_cfgs[tev_stage_index].enabled, !IsPassThroughTevStage(tev_stages[tev_stage_index]));
- }
-
+ state.draw.uniform_buffer = uniform_buffer.handle;
state.Apply();
}
@@ -852,7 +723,7 @@ void RasterizerOpenGL::ReloadColorBuffer() {
for (int x = 0; x < fb_color_texture.width; ++x) {
const u32 coarse_y = y & ~7;
u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_color_texture.width * bytes_per_pixel;
- u32 gl_pixel_index = (x + y * fb_color_texture.width) * bytes_per_pixel;
+ u32 gl_pixel_index = (x + (fb_color_texture.height - 1 - y) * fb_color_texture.width) * bytes_per_pixel;
u8* pixel = color_buffer + dst_offset;
memcpy(&temp_fb_color_buffer[gl_pixel_index], pixel, bytes_per_pixel);
@@ -898,7 +769,7 @@ void RasterizerOpenGL::ReloadDepthBuffer() {
for (int x = 0; x < fb_depth_texture.width; ++x) {
const u32 coarse_y = y & ~7;
u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_depth_texture.width * bytes_per_pixel;
- u32 gl_pixel_index = (x + y * fb_depth_texture.width);
+ u32 gl_pixel_index = (x + (fb_depth_texture.height - 1 - y) * fb_depth_texture.width);
u8* pixel = depth_buffer + dst_offset;
u32 depth_stencil = *(u32*)pixel;
@@ -910,7 +781,7 @@ void RasterizerOpenGL::ReloadDepthBuffer() {
for (int x = 0; x < fb_depth_texture.width; ++x) {
const u32 coarse_y = y & ~7;
u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_depth_texture.width * bytes_per_pixel;
- u32 gl_pixel_index = (x + y * fb_depth_texture.width) * gl_bpp;
+ u32 gl_pixel_index = (x + (fb_depth_texture.height - 1 - y) * fb_depth_texture.width) * gl_bpp;
u8* pixel = depth_buffer + dst_offset;
memcpy(&temp_fb_depth_data[gl_pixel_index], pixel, bytes_per_pixel);
@@ -965,7 +836,7 @@ void RasterizerOpenGL::CommitColorBuffer() {
for (int x = 0; x < fb_color_texture.width; ++x) {
const u32 coarse_y = y & ~7;
u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_color_texture.width * bytes_per_pixel;
- u32 gl_pixel_index = x * bytes_per_pixel + y * fb_color_texture.width * bytes_per_pixel;
+ u32 gl_pixel_index = x * bytes_per_pixel + (fb_color_texture.height - 1 - y) * fb_color_texture.width * bytes_per_pixel;
u8* pixel = color_buffer + dst_offset;
memcpy(pixel, &temp_gl_color_buffer[gl_pixel_index], bytes_per_pixel);
@@ -1007,7 +878,7 @@ void RasterizerOpenGL::CommitDepthBuffer() {
for (int x = 0; x < fb_depth_texture.width; ++x) {
const u32 coarse_y = y & ~7;
u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_depth_texture.width * bytes_per_pixel;
- u32 gl_pixel_index = (x + y * fb_depth_texture.width);
+ u32 gl_pixel_index = (x + (fb_depth_texture.height - 1 - y) * fb_depth_texture.width);
u8* pixel = depth_buffer + dst_offset;
u32 depth_stencil = ((u32*)temp_gl_depth_data)[gl_pixel_index];
@@ -1019,7 +890,7 @@ void RasterizerOpenGL::CommitDepthBuffer() {
for (int x = 0; x < fb_depth_texture.width; ++x) {
const u32 coarse_y = y & ~7;
u32 dst_offset = VideoCore::GetMortonOffset(x, y, bytes_per_pixel) + coarse_y * fb_depth_texture.width * bytes_per_pixel;
- u32 gl_pixel_index = (x + y * fb_depth_texture.width) * gl_bpp;
+ u32 gl_pixel_index = (x + (fb_depth_texture.height - 1 - y) * fb_depth_texture.width) * gl_bpp;
u8* pixel = depth_buffer + dst_offset;
memcpy(pixel, &temp_gl_depth_data[gl_pixel_index], bytes_per_pixel);
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h
index 1fe307846..92b1f812e 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer.h
@@ -4,60 +4,128 @@
#pragma once
+#include <cstddef>
+#include <cstring>
+#include <memory>
#include <vector>
+#include <unordered_map>
#include "common/common_types.h"
+#include "common/hash.h"
-#include "video_core/hwrasterizer_base.h"
+#include "video_core/pica.h"
+#include "video_core/rasterizer_interface.h"
#include "video_core/renderer_opengl/gl_rasterizer_cache.h"
#include "video_core/renderer_opengl/gl_state.h"
#include "video_core/shader/shader_interpreter.h"
-class RasterizerOpenGL : public HWRasterizer {
+/**
+ * This struct contains all state used to generate the GLSL shader program that emulates the current
+ * Pica register configuration. This struct is used as a cache key for generated GLSL shader
+ * programs. The functions in gl_shader_gen.cpp should retrieve state from this struct only, not by
+ * directly accessing Pica registers. This should reduce the risk of bugs in shader generation where
+ * Pica state is not being captured in the shader cache key, thereby resulting in (what should be)
+ * two separate shaders sharing the same key.
+ */
+struct PicaShaderConfig {
+ /// Construct a PicaShaderConfig with the current Pica register configuration.
+ static PicaShaderConfig CurrentConfig() {
+ PicaShaderConfig res;
+ const auto& regs = Pica::g_state.regs;
+
+ res.alpha_test_func = regs.output_merger.alpha_test.enable ?
+ regs.output_merger.alpha_test.func.Value() : Pica::Regs::CompareFunc::Always;
+
+ // Copy relevant TevStageConfig fields only. We're doing this manually (instead of calling
+ // the GetTevStages() function) because BitField explicitly disables copies.
+
+ res.tev_stages[0].sources_raw = regs.tev_stage0.sources_raw;
+ res.tev_stages[1].sources_raw = regs.tev_stage1.sources_raw;
+ res.tev_stages[2].sources_raw = regs.tev_stage2.sources_raw;
+ res.tev_stages[3].sources_raw = regs.tev_stage3.sources_raw;
+ res.tev_stages[4].sources_raw = regs.tev_stage4.sources_raw;
+ res.tev_stages[5].sources_raw = regs.tev_stage5.sources_raw;
+
+ res.tev_stages[0].modifiers_raw = regs.tev_stage0.modifiers_raw;
+ res.tev_stages[1].modifiers_raw = regs.tev_stage1.modifiers_raw;
+ res.tev_stages[2].modifiers_raw = regs.tev_stage2.modifiers_raw;
+ res.tev_stages[3].modifiers_raw = regs.tev_stage3.modifiers_raw;
+ res.tev_stages[4].modifiers_raw = regs.tev_stage4.modifiers_raw;
+ res.tev_stages[5].modifiers_raw = regs.tev_stage5.modifiers_raw;
+
+ res.tev_stages[0].ops_raw = regs.tev_stage0.ops_raw;
+ res.tev_stages[1].ops_raw = regs.tev_stage1.ops_raw;
+ res.tev_stages[2].ops_raw = regs.tev_stage2.ops_raw;
+ res.tev_stages[3].ops_raw = regs.tev_stage3.ops_raw;
+ res.tev_stages[4].ops_raw = regs.tev_stage4.ops_raw;
+ res.tev_stages[5].ops_raw = regs.tev_stage5.ops_raw;
+
+ res.tev_stages[0].scales_raw = regs.tev_stage0.scales_raw;
+ res.tev_stages[1].scales_raw = regs.tev_stage1.scales_raw;
+ res.tev_stages[2].scales_raw = regs.tev_stage2.scales_raw;
+ res.tev_stages[3].scales_raw = regs.tev_stage3.scales_raw;
+ res.tev_stages[4].scales_raw = regs.tev_stage4.scales_raw;
+ res.tev_stages[5].scales_raw = regs.tev_stage5.scales_raw;
+
+ res.combiner_buffer_input =
+ regs.tev_combiner_buffer_input.update_mask_rgb.Value() |
+ regs.tev_combiner_buffer_input.update_mask_a.Value() << 4;
+
+ return res;
+ }
+
+ bool TevStageUpdatesCombinerBufferColor(unsigned stage_index) const {
+ return (stage_index < 4) && (combiner_buffer_input & (1 << stage_index));
+ }
+
+ bool TevStageUpdatesCombinerBufferAlpha(unsigned stage_index) const {
+ return (stage_index < 4) && ((combiner_buffer_input >> 4) & (1 << stage_index));
+ }
+
+ bool operator ==(const PicaShaderConfig& o) const {
+ return std::memcmp(this, &o, sizeof(PicaShaderConfig)) == 0;
+ };
+
+ Pica::Regs::CompareFunc alpha_test_func;
+ std::array<Pica::Regs::TevStageConfig, 6> tev_stages = {};
+ u8 combiner_buffer_input;
+};
+
+namespace std {
+
+template <>
+struct hash<PicaShaderConfig> {
+ size_t operator()(const PicaShaderConfig& k) const {
+ return Common::ComputeHash64(&k, sizeof(PicaShaderConfig));
+ }
+};
+
+} // namespace std
+
+class RasterizerOpenGL : public VideoCore::RasterizerInterface {
public:
RasterizerOpenGL();
~RasterizerOpenGL() override;
- /// Initialize API-specific GPU objects
void InitObjects() override;
-
- /// Reset the rasterizer, such as flushing all caches and updating all state
void Reset() override;
-
- /// Queues the primitive formed by the given vertices for rendering
void AddTriangle(const Pica::Shader::OutputVertex& v0,
const Pica::Shader::OutputVertex& v1,
const Pica::Shader::OutputVertex& v2) override;
-
- /// Draw the current batch of triangles
void DrawTriangles() override;
-
- /// Commit the rasterizer's framebuffer contents immediately to the current 3DS memory framebuffer
- void CommitFramebuffer() override;
-
- /// Notify rasterizer that the specified PICA register has been changed
+ void FlushFramebuffer() override;
void NotifyPicaRegisterChanged(u32 id) override;
+ void FlushRegion(PAddr addr, u32 size) override;
+ void InvalidateRegion(PAddr addr, u32 size) override;
- /// Notify rasterizer that the specified 3DS memory region will be read from after this notification
- void NotifyPreRead(PAddr addr, u32 size) override;
-
- /// Notify rasterizer that a 3DS memory region has been changed
- void NotifyFlush(PAddr addr, u32 size) override;
+ /// OpenGL shader generated for a given Pica register state
+ struct PicaShader {
+ /// OpenGL shader resource
+ OGLShader shader;
+ };
private:
- /// Structure used for managing texture environment states
- struct TEVConfigUniforms {
- GLuint enabled;
- GLuint color_sources;
- GLuint alpha_sources;
- GLuint color_modifiers;
- GLuint alpha_modifiers;
- GLuint color_alpha_op;
- GLuint color_alpha_multiplier;
- GLuint const_color;
- GLuint updates_combiner_buffer_color_alpha;
- };
/// Structure used for storing information about color textures
struct TextureInfo {
@@ -123,12 +191,27 @@ private:
GLfloat tex_coord2[2];
};
+ /// Uniform structure for the Uniform Buffer Object, all members must be 16-byte aligned
+ struct UniformData {
+ // A vec4 color for each of the six tev stages
+ std::array<GLfloat, 4> const_color[6];
+ std::array<GLfloat, 4> tev_combiner_buffer_color;
+ GLint alphatest_ref;
+ INSERT_PADDING_BYTES(12);
+ };
+
+ static_assert(sizeof(UniformData) == 0x80, "The size of the UniformData structure has changed, update the structure in the shader");
+ static_assert(sizeof(UniformData) < 16000, "UniformData structure must be less than 16kb as per the OpenGL spec");
+
/// Reconfigure the OpenGL color texture to use the given format and dimensions
void ReconfigureColorTexture(TextureInfo& texture, Pica::Regs::ColorFormat format, u32 width, u32 height);
/// Reconfigure the OpenGL depth texture to use the given format and dimensions
void ReconfigureDepthTexture(DepthTextureInfo& texture, Pica::Regs::DepthFormat format, u32 width, u32 height);
+ /// Sets the OpenGL shader in accordance with the current PICA register state
+ void SetShader();
+
/// Syncs the state and contents of the OpenGL framebuffer to match the current PICA framebuffer
void SyncFramebuffer();
@@ -156,27 +239,12 @@ private:
/// Syncs the depth test states to match the PICA register
void SyncDepthTest();
- /// Syncs the specified TEV stage's color and alpha sources to match the PICA register
- void SyncTevSources(unsigned stage_index, const Pica::Regs::TevStageConfig& config);
-
- /// Syncs the specified TEV stage's color and alpha modifiers to match the PICA register
- void SyncTevModifiers(unsigned stage_index, const Pica::Regs::TevStageConfig& config);
-
- /// Syncs the specified TEV stage's color and alpha combiner operations to match the PICA register
- void SyncTevOps(unsigned stage_index, const Pica::Regs::TevStageConfig& config);
-
- /// Syncs the specified TEV stage's constant color to match the PICA register
- void SyncTevColor(unsigned stage_index, const Pica::Regs::TevStageConfig& config);
-
- /// Syncs the specified TEV stage's color and alpha multipliers to match the PICA register
- void SyncTevMultipliers(unsigned stage_index, const Pica::Regs::TevStageConfig& config);
+ /// Syncs the TEV constant color to match the PICA register
+ void SyncTevConstColor(int tev_index, const Pica::Regs::TevStageConfig& tev_stage);
/// Syncs the TEV combiner color buffer to match the PICA register
void SyncCombinerColor();
- /// Syncs the TEV combiner write flags to match the PICA register
- void SyncCombinerWriteFlags();
-
/// Syncs the remaining OpenGL drawing state to match the current PICA state
void SyncDrawState();
@@ -213,21 +281,17 @@ private:
std::array<SamplerInfo, 3> texture_samplers;
TextureInfo fb_color_texture;
DepthTextureInfo fb_depth_texture;
- OGLShader shader;
+
+ std::unordered_map<PicaShaderConfig, std::unique_ptr<PicaShader>> shader_cache;
+ const PicaShader* current_shader = nullptr;
+
+ struct {
+ UniformData data;
+ bool dirty;
+ } uniform_block_data;
+
OGLVertexArray vertex_array;
OGLBuffer vertex_buffer;
+ OGLBuffer uniform_buffer;
OGLFramebuffer framebuffer;
-
- // Hardware vertex shader
- GLuint attrib_position;
- GLuint attrib_color;
- GLuint attrib_texcoords;
-
- // Hardware fragment shader
- GLuint uniform_alphatest_enabled;
- GLuint uniform_alphatest_func;
- GLuint uniform_alphatest_ref;
- GLuint uniform_tex;
- GLuint uniform_tev_combiner_buffer_color;
- TEVConfigUniforms uniform_tev_cfgs[6];
};
diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
index 10d4ab0b6..a9ad46fe0 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
@@ -15,7 +15,7 @@
#include "video_core/renderer_opengl/pica_to_gl.h"
RasterizerCacheOpenGL::~RasterizerCacheOpenGL() {
- FullFlush();
+ InvalidateAll();
}
MICROPROFILE_DEFINE(OpenGL_TextureUpload, "OpenGL", "Texture Upload", MP_RGB(128, 64, 192));
@@ -58,8 +58,7 @@ void RasterizerCacheOpenGL::LoadAndBindTexture(OpenGLState &state, unsigned text
}
}
-void RasterizerCacheOpenGL::NotifyFlush(PAddr addr, u32 size, bool ignore_hash) {
- // Flush any texture that falls in the flushed region
+void RasterizerCacheOpenGL::InvalidateInRange(PAddr addr, u32 size, bool ignore_hash) {
// TODO: Optimize by also inserting upper bound (addr + size) of each texture into the same map and also narrow using lower_bound
auto cache_upper_bound = texture_cache.upper_bound(addr + size);
@@ -77,6 +76,6 @@ void RasterizerCacheOpenGL::NotifyFlush(PAddr addr, u32 size, bool ignore_hash)
}
}
-void RasterizerCacheOpenGL::FullFlush() {
+void RasterizerCacheOpenGL::InvalidateAll() {
texture_cache.clear();
}
diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.h b/src/video_core/renderer_opengl/gl_rasterizer_cache.h
index 98a48ffbe..b69651427 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.h
@@ -23,11 +23,11 @@ public:
LoadAndBindTexture(state, texture_unit, Pica::DebugUtils::TextureInfo::FromPicaRegister(config.config, config.format));
}
- /// Flush any cached resource that touches the flushed region
- void NotifyFlush(PAddr addr, u32 size, bool ignore_hash = false);
+ /// Invalidate any cached resource intersecting the specified region.
+ void InvalidateInRange(PAddr addr, u32 size, bool ignore_hash = false);
- /// Flush all cached OpenGL resources tracked by this cache manager
- void FullFlush();
+ /// Invalidate all cached OpenGL resources tracked by this cache manager
+ void InvalidateAll();
private:
struct CachedTexture {
diff --git a/src/video_core/renderer_opengl/gl_resource_manager.h b/src/video_core/renderer_opengl/gl_resource_manager.h
index 65034d40d..eb128966c 100644
--- a/src/video_core/renderer_opengl/gl_resource_manager.h
+++ b/src/video_core/renderer_opengl/gl_resource_manager.h
@@ -71,7 +71,7 @@ public:
/// Creates a new internal OpenGL resource and stores the handle
void Create(const char* vert_shader, const char* frag_shader) {
if (handle != 0) return;
- handle = ShaderUtil::LoadShaders(vert_shader, frag_shader);
+ handle = GLShader::LoadProgram(vert_shader, frag_shader);
}
/// Deletes the internal OpenGL resource
diff --git a/src/video_core/renderer_opengl/gl_shader_gen.cpp b/src/video_core/renderer_opengl/gl_shader_gen.cpp
new file mode 100644
index 000000000..38de5d469
--- /dev/null
+++ b/src/video_core/renderer_opengl/gl_shader_gen.cpp
@@ -0,0 +1,392 @@
+// Copyright 2015 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include "video_core/pica.h"
+#include "video_core/renderer_opengl/gl_rasterizer.h"
+#include "video_core/renderer_opengl/gl_shader_gen.h"
+
+using Pica::Regs;
+using TevStageConfig = Regs::TevStageConfig;
+
+namespace GLShader {
+
+/// Detects if a TEV stage is configured to be skipped (to avoid generating unnecessary code)
+static bool IsPassThroughTevStage(const TevStageConfig& stage) {
+ return (stage.color_op == TevStageConfig::Operation::Replace &&
+ stage.alpha_op == TevStageConfig::Operation::Replace &&
+ stage.color_source1 == TevStageConfig::Source::Previous &&
+ stage.alpha_source1 == TevStageConfig::Source::Previous &&
+ stage.color_modifier1 == TevStageConfig::ColorModifier::SourceColor &&
+ stage.alpha_modifier1 == TevStageConfig::AlphaModifier::SourceAlpha &&
+ stage.GetColorMultiplier() == 1 &&
+ stage.GetAlphaMultiplier() == 1);
+}
+
+/// Writes the specified TEV stage source component(s)
+static void AppendSource(std::string& out, TevStageConfig::Source source,
+ const std::string& index_name) {
+ using Source = TevStageConfig::Source;
+ switch (source) {
+ case Source::PrimaryColor:
+ out += "primary_color";
+ break;
+ case Source::PrimaryFragmentColor:
+ // HACK: Until we implement fragment lighting, use primary_color
+ out += "primary_color";
+ break;
+ case Source::SecondaryFragmentColor:
+ // HACK: Until we implement fragment lighting, use zero
+ out += "vec4(0.0)";
+ break;
+ case Source::Texture0:
+ out += "texture(tex[0], texcoord[0])";
+ break;
+ case Source::Texture1:
+ out += "texture(tex[1], texcoord[1])";
+ break;
+ case Source::Texture2:
+ out += "texture(tex[2], texcoord[2])";
+ break;
+ case Source::PreviousBuffer:
+ out += "combiner_buffer";
+ break;
+ case Source::Constant:
+ ((out += "const_color[") += index_name) += ']';
+ break;
+ case Source::Previous:
+ out += "last_tex_env_out";
+ break;
+ default:
+ out += "vec4(0.0)";
+ LOG_CRITICAL(Render_OpenGL, "Unknown source op %u", source);
+ break;
+ }
+}
+
+/// Writes the color components to use for the specified TEV stage color modifier
+static void AppendColorModifier(std::string& out, TevStageConfig::ColorModifier modifier,
+ TevStageConfig::Source source, const std::string& index_name) {
+ using ColorModifier = TevStageConfig::ColorModifier;
+ switch (modifier) {
+ case ColorModifier::SourceColor:
+ AppendSource(out, source, index_name);
+ out += ".rgb";
+ break;
+ case ColorModifier::OneMinusSourceColor:
+ out += "vec3(1.0) - ";
+ AppendSource(out, source, index_name);
+ out += ".rgb";
+ break;
+ case ColorModifier::SourceAlpha:
+ AppendSource(out, source, index_name);
+ out += ".aaa";
+ break;
+ case ColorModifier::OneMinusSourceAlpha:
+ out += "vec3(1.0) - ";
+ AppendSource(out, source, index_name);
+ out += ".aaa";
+ break;
+ case ColorModifier::SourceRed:
+ AppendSource(out, source, index_name);
+ out += ".rrr";
+ break;
+ case ColorModifier::OneMinusSourceRed:
+ out += "vec3(1.0) - ";
+ AppendSource(out, source, index_name);
+ out += ".rrr";
+ break;
+ case ColorModifier::SourceGreen:
+ AppendSource(out, source, index_name);
+ out += ".ggg";
+ break;
+ case ColorModifier::OneMinusSourceGreen:
+ out += "vec3(1.0) - ";
+ AppendSource(out, source, index_name);
+ out += ".ggg";
+ break;
+ case ColorModifier::SourceBlue:
+ AppendSource(out, source, index_name);
+ out += ".bbb";
+ break;
+ case ColorModifier::OneMinusSourceBlue:
+ out += "vec3(1.0) - ";
+ AppendSource(out, source, index_name);
+ out += ".bbb";
+ break;
+ default:
+ out += "vec3(0.0)";
+ LOG_CRITICAL(Render_OpenGL, "Unknown color modifier op %u", modifier);
+ break;
+ }
+}
+
+/// Writes the alpha component to use for the specified TEV stage alpha modifier
+static void AppendAlphaModifier(std::string& out, TevStageConfig::AlphaModifier modifier,
+ TevStageConfig::Source source, const std::string& index_name) {
+ using AlphaModifier = TevStageConfig::AlphaModifier;
+ switch (modifier) {
+ case AlphaModifier::SourceAlpha:
+ AppendSource(out, source, index_name);
+ out += ".a";
+ break;
+ case AlphaModifier::OneMinusSourceAlpha:
+ out += "1.0 - ";
+ AppendSource(out, source, index_name);
+ out += ".a";
+ break;
+ case AlphaModifier::SourceRed:
+ AppendSource(out, source, index_name);
+ out += ".r";
+ break;
+ case AlphaModifier::OneMinusSourceRed:
+ out += "1.0 - ";
+ AppendSource(out, source, index_name);
+ out += ".r";
+ break;
+ case AlphaModifier::SourceGreen:
+ AppendSource(out, source, index_name);
+ out += ".g";
+ break;
+ case AlphaModifier::OneMinusSourceGreen:
+ out += "1.0 - ";
+ AppendSource(out, source, index_name);
+ out += ".g";
+ break;
+ case AlphaModifier::SourceBlue:
+ AppendSource(out, source, index_name);
+ out += ".b";
+ break;
+ case AlphaModifier::OneMinusSourceBlue:
+ out += "1.0 - ";
+ AppendSource(out, source, index_name);
+ out += ".b";
+ break;
+ default:
+ out += "0.0";
+ LOG_CRITICAL(Render_OpenGL, "Unknown alpha modifier op %u", modifier);
+ break;
+ }
+}
+
+/// Writes the combiner function for the color components for the specified TEV stage operation
+static void AppendColorCombiner(std::string& out, TevStageConfig::Operation operation,
+ const std::string& variable_name) {
+ out += "clamp(";
+ using Operation = TevStageConfig::Operation;
+ switch (operation) {
+ case Operation::Replace:
+ out += variable_name + "[0]";
+ break;
+ case Operation::Modulate:
+ out += variable_name + "[0] * " + variable_name + "[1]";
+ break;
+ case Operation::Add:
+ out += variable_name + "[0] + " + variable_name + "[1]";
+ break;
+ case Operation::AddSigned:
+ out += variable_name + "[0] + " + variable_name + "[1] - vec3(0.5)";
+ break;
+ case Operation::Lerp:
+ // TODO(bunnei): Verify if HW actually does this per-component, otherwise we can just use builtin lerp
+ out += variable_name + "[0] * " + variable_name + "[2] + " + variable_name + "[1] * (vec3(1.0) - " + variable_name + "[2])";
+ break;
+ case Operation::Subtract:
+ out += variable_name + "[0] - " + variable_name + "[1]";
+ break;
+ case Operation::MultiplyThenAdd:
+ out += variable_name + "[0] * " + variable_name + "[1] + " + variable_name + "[2]";
+ break;
+ case Operation::AddThenMultiply:
+ out += "min(" + variable_name + "[0] + " + variable_name + "[1], vec3(1.0)) * " + variable_name + "[2]";
+ break;
+ default:
+ out += "vec3(0.0)";
+ LOG_CRITICAL(Render_OpenGL, "Unknown color combiner operation: %u", operation);
+ break;
+ }
+ out += ", vec3(0.0), vec3(1.0))"; // Clamp result to 0.0, 1.0
+}
+
+/// Writes the combiner function for the alpha component for the specified TEV stage operation
+static void AppendAlphaCombiner(std::string& out, TevStageConfig::Operation operation,
+ const std::string& variable_name) {
+ out += "clamp(";
+ using Operation = TevStageConfig::Operation;
+ switch (operation) {
+ case Operation::Replace:
+ out += variable_name + "[0]";
+ break;
+ case Operation::Modulate:
+ out += variable_name + "[0] * " + variable_name + "[1]";
+ break;
+ case Operation::Add:
+ out += variable_name + "[0] + " + variable_name + "[1]";
+ break;
+ case Operation::AddSigned:
+ out += variable_name + "[0] + " + variable_name + "[1] - 0.5";
+ break;
+ case Operation::Lerp:
+ out += variable_name + "[0] * " + variable_name + "[2] + " + variable_name + "[1] * (1.0 - " + variable_name + "[2])";
+ break;
+ case Operation::Subtract:
+ out += variable_name + "[0] - " + variable_name + "[1]";
+ break;
+ case Operation::MultiplyThenAdd:
+ out += variable_name + "[0] * " + variable_name + "[1] + " + variable_name + "[2]";
+ break;
+ case Operation::AddThenMultiply:
+ out += "min(" + variable_name + "[0] + " + variable_name + "[1], 1.0) * " + variable_name + "[2]";
+ break;
+ default:
+ out += "0.0";
+ LOG_CRITICAL(Render_OpenGL, "Unknown alpha combiner operation: %u", operation);
+ break;
+ }
+ out += ", 0.0, 1.0)";
+}
+
+/// Writes the if-statement condition used to evaluate alpha testing
+static void AppendAlphaTestCondition(std::string& out, Regs::CompareFunc func) {
+ using CompareFunc = Regs::CompareFunc;
+ switch (func) {
+ case CompareFunc::Never:
+ out += "true";
+ break;
+ case CompareFunc::Always:
+ out += "false";
+ break;
+ case CompareFunc::Equal:
+ case CompareFunc::NotEqual:
+ case CompareFunc::LessThan:
+ case CompareFunc::LessThanOrEqual:
+ case CompareFunc::GreaterThan:
+ case CompareFunc::GreaterThanOrEqual:
+ {
+ static const char* op[] = { "!=", "==", ">=", ">", "<=", "<", };
+ unsigned index = (unsigned)func - (unsigned)CompareFunc::Equal;
+ out += "int(last_tex_env_out.a * 255.0f) " + std::string(op[index]) + " alphatest_ref";
+ break;
+ }
+
+ default:
+ out += "false";
+ LOG_CRITICAL(Render_OpenGL, "Unknown alpha test condition %u", func);
+ break;
+ }
+}
+
+/// Writes the code to emulate the specified TEV stage
+static void WriteTevStage(std::string& out, const PicaShaderConfig& config, unsigned index) {
+ auto& stage = config.tev_stages[index];
+ if (!IsPassThroughTevStage(stage)) {
+ std::string index_name = std::to_string(index);
+
+ out += "vec3 color_results_" + index_name + "[3] = vec3[3](";
+ AppendColorModifier(out, stage.color_modifier1, stage.color_source1, index_name);
+ out += ", ";
+ AppendColorModifier(out, stage.color_modifier2, stage.color_source2, index_name);
+ out += ", ";
+ AppendColorModifier(out, stage.color_modifier3, stage.color_source3, index_name);
+ out += ");\n";
+
+ out += "vec3 color_output_" + index_name + " = ";
+ AppendColorCombiner(out, stage.color_op, "color_results_" + index_name);
+ out += ";\n";
+
+ out += "float alpha_results_" + index_name + "[3] = float[3](";
+ AppendAlphaModifier(out, stage.alpha_modifier1, stage.alpha_source1, index_name);
+ out += ", ";
+ AppendAlphaModifier(out, stage.alpha_modifier2, stage.alpha_source2, index_name);
+ out += ", ";
+ AppendAlphaModifier(out, stage.alpha_modifier3, stage.alpha_source3, index_name);
+ out += ");\n";
+
+ out += "float alpha_output_" + index_name + " = ";
+ AppendAlphaCombiner(out, stage.alpha_op, "alpha_results_" + index_name);
+ out += ";\n";
+
+ out += "last_tex_env_out = vec4("
+ "clamp(color_output_" + index_name + " * " + std::to_string(stage.GetColorMultiplier()) + ".0, vec3(0.0), vec3(1.0)),"
+ "clamp(alpha_output_" + index_name + " * " + std::to_string(stage.GetAlphaMultiplier()) + ".0, 0.0, 1.0));\n";
+ }
+
+ out += "combiner_buffer = next_combiner_buffer;\n";
+
+ if (config.TevStageUpdatesCombinerBufferColor(index))
+ out += "next_combiner_buffer.rgb = last_tex_env_out.rgb;\n";
+
+ if (config.TevStageUpdatesCombinerBufferAlpha(index))
+ out += "next_combiner_buffer.a = last_tex_env_out.a;\n";
+}
+
+std::string GenerateFragmentShader(const PicaShaderConfig& config) {
+ std::string out = R"(
+#version 330 core
+#define NUM_TEV_STAGES 6
+
+in vec4 primary_color;
+in vec2 texcoord[3];
+
+out vec4 color;
+
+layout (std140) uniform shader_data {
+ vec4 const_color[NUM_TEV_STAGES];
+ vec4 tev_combiner_buffer_color;
+ int alphatest_ref;
+};
+
+uniform sampler2D tex[3];
+
+void main() {
+)";
+
+ // Do not do any sort of processing if it's obvious we're not going to pass the alpha test
+ if (config.alpha_test_func == Regs::CompareFunc::Never) {
+ out += "discard; }";
+ return out;
+ }
+
+ out += "vec4 combiner_buffer = vec4(0.0);\n";
+ out += "vec4 next_combiner_buffer = tev_combiner_buffer_color;\n";
+ out += "vec4 last_tex_env_out = vec4(0.0);\n";
+
+ for (size_t index = 0; index < config.tev_stages.size(); ++index)
+ WriteTevStage(out, config, (unsigned)index);
+
+ if (config.alpha_test_func != Regs::CompareFunc::Always) {
+ out += "if (";
+ AppendAlphaTestCondition(out, config.alpha_test_func);
+ out += ") discard;\n";
+ }
+
+ out += "color = last_tex_env_out;\n}";
+
+ return out;
+}
+
+std::string GenerateVertexShader() {
+ std::string out = "#version 330 core\n";
+ out += "layout(location = " + std::to_string((int)ATTRIBUTE_POSITION) + ") in vec4 vert_position;\n";
+ out += "layout(location = " + std::to_string((int)ATTRIBUTE_COLOR) + ") in vec4 vert_color;\n";
+ out += "layout(location = " + std::to_string((int)ATTRIBUTE_TEXCOORD0) + ") in vec2 vert_texcoord0;\n";
+ out += "layout(location = " + std::to_string((int)ATTRIBUTE_TEXCOORD1) + ") in vec2 vert_texcoord1;\n";
+ out += "layout(location = " + std::to_string((int)ATTRIBUTE_TEXCOORD2) + ") in vec2 vert_texcoord2;\n";
+
+ out += R"(
+out vec4 primary_color;
+out vec2 texcoord[3];
+
+void main() {
+ primary_color = vert_color;
+ texcoord[0] = vert_texcoord0;
+ texcoord[1] = vert_texcoord1;
+ texcoord[2] = vert_texcoord2;
+ gl_Position = vec4(vert_position.x, vert_position.y, -vert_position.z, vert_position.w);
+}
+)";
+
+ return out;
+}
+
+} // namespace GLShader
diff --git a/src/video_core/renderer_opengl/gl_shader_gen.h b/src/video_core/renderer_opengl/gl_shader_gen.h
new file mode 100644
index 000000000..0ca9d2879
--- /dev/null
+++ b/src/video_core/renderer_opengl/gl_shader_gen.h
@@ -0,0 +1,27 @@
+// Copyright 2015 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <string>
+
+#include "video_core/renderer_opengl/gl_rasterizer.h"
+
+namespace GLShader {
+
+/**
+ * Generates the GLSL vertex shader program source code for the current Pica state
+ * @returns String of the shader source code
+ */
+std::string GenerateVertexShader();
+
+/**
+ * Generates the GLSL fragment shader program source code for the current Pica state
+ * @param config ShaderCacheKey object generated for the current Pica state, used for the shader
+ * configuration (NOTE: Use state in this struct only, not the Pica registers!)
+ * @returns String of the shader source code
+ */
+std::string GenerateFragmentShader(const PicaShaderConfig& config);
+
+} // namespace GLShader
diff --git a/src/video_core/renderer_opengl/gl_shader_util.cpp b/src/video_core/renderer_opengl/gl_shader_util.cpp
index 4cf246c06..e3f7a5868 100644
--- a/src/video_core/renderer_opengl/gl_shader_util.cpp
+++ b/src/video_core/renderer_opengl/gl_shader_util.cpp
@@ -8,9 +8,9 @@
#include "common/logging/log.h"
#include "video_core/renderer_opengl/gl_shader_util.h"
-namespace ShaderUtil {
+namespace GLShader {
-GLuint LoadShaders(const char* vertex_shader, const char* fragment_shader) {
+GLuint LoadProgram(const char* vertex_shader, const char* fragment_shader) {
// Create the shaders
GLuint vertex_shader_id = glCreateShader(GL_VERTEX_SHADER);
@@ -65,6 +65,7 @@ GLuint LoadShaders(const char* vertex_shader, const char* fragment_shader) {
GLuint program_id = glCreateProgram();
glAttachShader(program_id, vertex_shader_id);
glAttachShader(program_id, fragment_shader_id);
+
glLinkProgram(program_id);
// Check the program
@@ -87,4 +88,4 @@ GLuint LoadShaders(const char* vertex_shader, const char* fragment_shader) {
return program_id;
}
-}
+} // namespace GLShader
diff --git a/src/video_core/renderer_opengl/gl_shader_util.h b/src/video_core/renderer_opengl/gl_shader_util.h
index c9d7cc380..046aae14f 100644
--- a/src/video_core/renderer_opengl/gl_shader_util.h
+++ b/src/video_core/renderer_opengl/gl_shader_util.h
@@ -6,8 +6,22 @@
#include <glad/glad.h>
-namespace ShaderUtil {
+namespace GLShader {
-GLuint LoadShaders(const char* vertex_file_path, const char* fragment_file_path);
+enum Attributes {
+ ATTRIBUTE_POSITION,
+ ATTRIBUTE_COLOR,
+ ATTRIBUTE_TEXCOORD0,
+ ATTRIBUTE_TEXCOORD1,
+ ATTRIBUTE_TEXCOORD2,
+};
-}
+/**
+ * Utility function to create and compile an OpenGL GLSL shader program (vertex + fragment shader)
+ * @param vertex_shader String of the GLSL vertex shader program
+ * @param fragment_shader String of the GLSL fragment shader program
+ * @returns Handle of the newly created OpenGL shader object
+ */
+GLuint LoadProgram(const char* vertex_shader, const char* fragment_shader);
+
+} // namespace
diff --git a/src/video_core/renderer_opengl/gl_shaders.h b/src/video_core/renderer_opengl/gl_shaders.h
deleted file mode 100644
index a8cb2f595..000000000
--- a/src/video_core/renderer_opengl/gl_shaders.h
+++ /dev/null
@@ -1,337 +0,0 @@
-// Copyright 2014 Citra Emulator Project
-// Licensed under GPLv2 or any later version
-// Refer to the license.txt file included.
-
-#pragma once
-
-namespace GLShaders {
-
-const char g_vertex_shader[] = R"(
-#version 150 core
-
-in vec2 vert_position;
-in vec2 vert_tex_coord;
-out vec2 frag_tex_coord;
-
-// This is a truncated 3x3 matrix for 2D transformations:
-// The upper-left 2x2 submatrix performs scaling/rotation/mirroring.
-// The third column performs translation.
-// The third row could be used for projection, which we don't need in 2D. It hence is assumed to
-// implicitly be [0, 0, 1]
-uniform mat3x2 modelview_matrix;
-
-void main() {
- // Multiply input position by the rotscale part of the matrix and then manually translate by
- // the last column. This is equivalent to using a full 3x3 matrix and expanding the vector
- // to `vec3(vert_position.xy, 1.0)`
- gl_Position = vec4(mat2(modelview_matrix) * vert_position + modelview_matrix[2], 0.0, 1.0);
- frag_tex_coord = vert_tex_coord;
-}
-)";
-
-const char g_fragment_shader[] = R"(
-#version 150 core
-
-in vec2 frag_tex_coord;
-out vec4 color;
-
-uniform sampler2D color_texture;
-
-void main() {
- color = texture(color_texture, frag_tex_coord);
-}
-)";
-
-const char g_vertex_shader_hw[] = R"(
-#version 150 core
-
-#define NUM_VTX_ATTR 7
-
-in vec4 vert_position;
-in vec4 vert_color;
-in vec2 vert_texcoords[3];
-
-out vec4 o[NUM_VTX_ATTR];
-
-void main() {
- o[2] = vert_color;
- o[3] = vec4(vert_texcoords[0].xy, vert_texcoords[1].xy);
- o[5] = vec4(0.0, 0.0, vert_texcoords[2].xy);
-
- gl_Position = vec4(vert_position.x, -vert_position.y, -vert_position.z, vert_position.w);
-}
-)";
-
-// TODO: Create a shader constructor and cache that builds this program with minimal conditionals instead of using tev_cfg uniforms
-const char g_fragment_shader_hw[] = R"(
-#version 150 core
-
-#define NUM_VTX_ATTR 7
-#define NUM_TEV_STAGES 6
-
-#define SOURCE_PRIMARYCOLOR 0x0
-#define SOURCE_PRIMARYFRAGMENTCOLOR 0x1
-#define SOURCE_SECONDARYFRAGMENTCOLOR 0x2
-#define SOURCE_TEXTURE0 0x3
-#define SOURCE_TEXTURE1 0x4
-#define SOURCE_TEXTURE2 0x5
-#define SOURCE_TEXTURE3 0x6
-#define SOURCE_PREVIOUSBUFFER 0xd
-#define SOURCE_CONSTANT 0xe
-#define SOURCE_PREVIOUS 0xf
-
-#define COLORMODIFIER_SOURCECOLOR 0x0
-#define COLORMODIFIER_ONEMINUSSOURCECOLOR 0x1
-#define COLORMODIFIER_SOURCEALPHA 0x2
-#define COLORMODIFIER_ONEMINUSSOURCEALPHA 0x3
-#define COLORMODIFIER_SOURCERED 0x4
-#define COLORMODIFIER_ONEMINUSSOURCERED 0x5
-#define COLORMODIFIER_SOURCEGREEN 0x8
-#define COLORMODIFIER_ONEMINUSSOURCEGREEN 0x9
-#define COLORMODIFIER_SOURCEBLUE 0xc
-#define COLORMODIFIER_ONEMINUSSOURCEBLUE 0xd
-
-#define ALPHAMODIFIER_SOURCEALPHA 0x0
-#define ALPHAMODIFIER_ONEMINUSSOURCEALPHA 0x1
-#define ALPHAMODIFIER_SOURCERED 0x2
-#define ALPHAMODIFIER_ONEMINUSSOURCERED 0x3
-#define ALPHAMODIFIER_SOURCEGREEN 0x4
-#define ALPHAMODIFIER_ONEMINUSSOURCEGREEN 0x5
-#define ALPHAMODIFIER_SOURCEBLUE 0x6
-#define ALPHAMODIFIER_ONEMINUSSOURCEBLUE 0x7
-
-#define OPERATION_REPLACE 0
-#define OPERATION_MODULATE 1
-#define OPERATION_ADD 2
-#define OPERATION_ADDSIGNED 3
-#define OPERATION_LERP 4
-#define OPERATION_SUBTRACT 5
-#define OPERATION_MULTIPLYTHENADD 8
-#define OPERATION_ADDTHENMULTIPLY 9
-
-#define COMPAREFUNC_NEVER 0
-#define COMPAREFUNC_ALWAYS 1
-#define COMPAREFUNC_EQUAL 2
-#define COMPAREFUNC_NOTEQUAL 3
-#define COMPAREFUNC_LESSTHAN 4
-#define COMPAREFUNC_LESSTHANOREQUAL 5
-#define COMPAREFUNC_GREATERTHAN 6
-#define COMPAREFUNC_GREATERTHANOREQUAL 7
-
-in vec4 o[NUM_VTX_ATTR];
-out vec4 color;
-
-uniform bool alphatest_enabled;
-uniform int alphatest_func;
-uniform float alphatest_ref;
-
-uniform sampler2D tex[3];
-
-uniform vec4 tev_combiner_buffer_color;
-
-struct TEVConfig
-{
- bool enabled;
- ivec3 color_sources;
- ivec3 alpha_sources;
- ivec3 color_modifiers;
- ivec3 alpha_modifiers;
- ivec2 color_alpha_op;
- ivec2 color_alpha_multiplier;
- vec4 const_color;
- bvec2 updates_combiner_buffer_color_alpha;
-};
-
-uniform TEVConfig tev_cfgs[NUM_TEV_STAGES];
-
-vec4 g_combiner_buffer;
-vec4 g_last_tex_env_out;
-vec4 g_const_color;
-
-vec4 GetSource(int source) {
- if (source == SOURCE_PRIMARYCOLOR) {
- return o[2];
- } else if (source == SOURCE_PRIMARYFRAGMENTCOLOR) {
- // HACK: Until we implement fragment lighting, use primary_color
- return o[2];
- } else if (source == SOURCE_SECONDARYFRAGMENTCOLOR) {
- // HACK: Until we implement fragment lighting, use zero
- return vec4(0.0, 0.0, 0.0, 0.0);
- } else if (source == SOURCE_TEXTURE0) {
- return texture(tex[0], o[3].xy);
- } else if (source == SOURCE_TEXTURE1) {
- return texture(tex[1], o[3].zw);
- } else if (source == SOURCE_TEXTURE2) {
- // TODO: Unverified
- return texture(tex[2], o[5].zw);
- } else if (source == SOURCE_TEXTURE3) {
- // TODO: no 4th texture?
- } else if (source == SOURCE_PREVIOUSBUFFER) {
- return g_combiner_buffer;
- } else if (source == SOURCE_CONSTANT) {
- return g_const_color;
- } else if (source == SOURCE_PREVIOUS) {
- return g_last_tex_env_out;
- }
-
- return vec4(0.0);
-}
-
-vec3 GetColorModifier(int factor, vec4 color) {
- if (factor == COLORMODIFIER_SOURCECOLOR) {
- return color.rgb;
- } else if (factor == COLORMODIFIER_ONEMINUSSOURCECOLOR) {
- return vec3(1.0) - color.rgb;
- } else if (factor == COLORMODIFIER_SOURCEALPHA) {
- return color.aaa;
- } else if (factor == COLORMODIFIER_ONEMINUSSOURCEALPHA) {
- return vec3(1.0) - color.aaa;
- } else if (factor == COLORMODIFIER_SOURCERED) {
- return color.rrr;
- } else if (factor == COLORMODIFIER_ONEMINUSSOURCERED) {
- return vec3(1.0) - color.rrr;
- } else if (factor == COLORMODIFIER_SOURCEGREEN) {
- return color.ggg;
- } else if (factor == COLORMODIFIER_ONEMINUSSOURCEGREEN) {
- return vec3(1.0) - color.ggg;
- } else if (factor == COLORMODIFIER_SOURCEBLUE) {
- return color.bbb;
- } else if (factor == COLORMODIFIER_ONEMINUSSOURCEBLUE) {
- return vec3(1.0) - color.bbb;
- }
-
- return vec3(0.0);
-}
-
-float GetAlphaModifier(int factor, vec4 color) {
- if (factor == ALPHAMODIFIER_SOURCEALPHA) {
- return color.a;
- } else if (factor == ALPHAMODIFIER_ONEMINUSSOURCEALPHA) {
- return 1.0 - color.a;
- } else if (factor == ALPHAMODIFIER_SOURCERED) {
- return color.r;
- } else if (factor == ALPHAMODIFIER_ONEMINUSSOURCERED) {
- return 1.0 - color.r;
- } else if (factor == ALPHAMODIFIER_SOURCEGREEN) {
- return color.g;
- } else if (factor == ALPHAMODIFIER_ONEMINUSSOURCEGREEN) {
- return 1.0 - color.g;
- } else if (factor == ALPHAMODIFIER_SOURCEBLUE) {
- return color.b;
- } else if (factor == ALPHAMODIFIER_ONEMINUSSOURCEBLUE) {
- return 1.0 - color.b;
- }
-
- return 0.0;
-}
-
-vec3 ColorCombine(int op, vec3 color[3]) {
- if (op == OPERATION_REPLACE) {
- return color[0];
- } else if (op == OPERATION_MODULATE) {
- return color[0] * color[1];
- } else if (op == OPERATION_ADD) {
- return min(color[0] + color[1], 1.0);
- } else if (op == OPERATION_ADDSIGNED) {
- return clamp(color[0] + color[1] - vec3(0.5), 0.0, 1.0);
- } else if (op == OPERATION_LERP) {
- return color[0] * color[2] + color[1] * (vec3(1.0) - color[2]);
- } else if (op == OPERATION_SUBTRACT) {
- return max(color[0] - color[1], 0.0);
- } else if (op == OPERATION_MULTIPLYTHENADD) {
- return min(color[0] * color[1] + color[2], 1.0);
- } else if (op == OPERATION_ADDTHENMULTIPLY) {
- return min(color[0] + color[1], 1.0) * color[2];
- }
-
- return vec3(0.0);
-}
-
-float AlphaCombine(int op, float alpha[3]) {
- if (op == OPERATION_REPLACE) {
- return alpha[0];
- } else if (op == OPERATION_MODULATE) {
- return alpha[0] * alpha[1];
- } else if (op == OPERATION_ADD) {
- return min(alpha[0] + alpha[1], 1.0);
- } else if (op == OPERATION_ADDSIGNED) {
- return clamp(alpha[0] + alpha[1] - 0.5, 0.0, 1.0);
- } else if (op == OPERATION_LERP) {
- return alpha[0] * alpha[2] + alpha[1] * (1.0 - alpha[2]);
- } else if (op == OPERATION_SUBTRACT) {
- return max(alpha[0] - alpha[1], 0.0);
- } else if (op == OPERATION_MULTIPLYTHENADD) {
- return min(alpha[0] * alpha[1] + alpha[2], 1.0);
- } else if (op == OPERATION_ADDTHENMULTIPLY) {
- return min(alpha[0] + alpha[1], 1.0) * alpha[2];
- }
-
- return 0.0;
-}
-
-void main(void) {
- g_combiner_buffer = tev_combiner_buffer_color;
-
- for (int tex_env_idx = 0; tex_env_idx < NUM_TEV_STAGES; ++tex_env_idx) {
- if (tev_cfgs[tex_env_idx].enabled) {
- g_const_color = tev_cfgs[tex_env_idx].const_color;
-
- vec3 color_results[3] = vec3[3](GetColorModifier(tev_cfgs[tex_env_idx].color_modifiers.x, GetSource(tev_cfgs[tex_env_idx].color_sources.x)),
- GetColorModifier(tev_cfgs[tex_env_idx].color_modifiers.y, GetSource(tev_cfgs[tex_env_idx].color_sources.y)),
- GetColorModifier(tev_cfgs[tex_env_idx].color_modifiers.z, GetSource(tev_cfgs[tex_env_idx].color_sources.z)));
- vec3 color_output = ColorCombine(tev_cfgs[tex_env_idx].color_alpha_op.x, color_results);
-
- float alpha_results[3] = float[3](GetAlphaModifier(tev_cfgs[tex_env_idx].alpha_modifiers.x, GetSource(tev_cfgs[tex_env_idx].alpha_sources.x)),
- GetAlphaModifier(tev_cfgs[tex_env_idx].alpha_modifiers.y, GetSource(tev_cfgs[tex_env_idx].alpha_sources.y)),
- GetAlphaModifier(tev_cfgs[tex_env_idx].alpha_modifiers.z, GetSource(tev_cfgs[tex_env_idx].alpha_sources.z)));
- float alpha_output = AlphaCombine(tev_cfgs[tex_env_idx].color_alpha_op.y, alpha_results);
-
- g_last_tex_env_out = vec4(min(color_output * tev_cfgs[tex_env_idx].color_alpha_multiplier.x, 1.0), min(alpha_output * tev_cfgs[tex_env_idx].color_alpha_multiplier.y, 1.0));
- }
-
- if (tev_cfgs[tex_env_idx].updates_combiner_buffer_color_alpha.x) {
- g_combiner_buffer.rgb = g_last_tex_env_out.rgb;
- }
-
- if (tev_cfgs[tex_env_idx].updates_combiner_buffer_color_alpha.y) {
- g_combiner_buffer.a = g_last_tex_env_out.a;
- }
- }
-
- if (alphatest_enabled) {
- if (alphatest_func == COMPAREFUNC_NEVER) {
- discard;
- } else if (alphatest_func == COMPAREFUNC_ALWAYS) {
-
- } else if (alphatest_func == COMPAREFUNC_EQUAL) {
- if (g_last_tex_env_out.a != alphatest_ref) {
- discard;
- }
- } else if (alphatest_func == COMPAREFUNC_NOTEQUAL) {
- if (g_last_tex_env_out.a == alphatest_ref) {
- discard;
- }
- } else if (alphatest_func == COMPAREFUNC_LESSTHAN) {
- if (g_last_tex_env_out.a >= alphatest_ref) {
- discard;
- }
- } else if (alphatest_func == COMPAREFUNC_LESSTHANOREQUAL) {
- if (g_last_tex_env_out.a > alphatest_ref) {
- discard;
- }
- } else if (alphatest_func == COMPAREFUNC_GREATERTHAN) {
- if (g_last_tex_env_out.a <= alphatest_ref) {
- discard;
- }
- } else if (alphatest_func == COMPAREFUNC_GREATERTHANOREQUAL) {
- if (g_last_tex_env_out.a < alphatest_ref) {
- discard;
- }
- }
- }
-
- color = g_last_tex_env_out;
-}
-)";
-
-}
diff --git a/src/video_core/renderer_opengl/gl_state.cpp b/src/video_core/renderer_opengl/gl_state.cpp
index 77b2816cb..a82372995 100644
--- a/src/video_core/renderer_opengl/gl_state.cpp
+++ b/src/video_core/renderer_opengl/gl_state.cpp
@@ -11,6 +11,7 @@ OpenGLState::OpenGLState() {
// These all match default OpenGL values
cull.enabled = false;
cull.mode = GL_BACK;
+ cull.front_face = GL_CCW;
depth.test_enabled = false;
depth.test_func = GL_LESS;
@@ -67,6 +68,10 @@ void OpenGLState::Apply() {
glCullFace(cull.mode);
}
+ if (cull.front_face != cur_state.cull.front_face) {
+ glFrontFace(cull.front_face);
+ }
+
// Depth test
if (depth.test_enabled != cur_state.depth.test_enabled) {
if (depth.test_enabled) {
@@ -180,6 +185,11 @@ void OpenGLState::Apply() {
glBindBuffer(GL_ARRAY_BUFFER, draw.vertex_buffer);
}
+ // Uniform buffer
+ if (draw.uniform_buffer != cur_state.draw.uniform_buffer) {
+ glBindBuffer(GL_UNIFORM_BUFFER, draw.uniform_buffer);
+ }
+
// Shader program
if (draw.shader_program != cur_state.draw.shader_program) {
glUseProgram(draw.shader_program);
@@ -214,6 +224,9 @@ void OpenGLState::ResetBuffer(GLuint id) {
if (cur_state.draw.vertex_buffer == id) {
cur_state.draw.vertex_buffer = 0;
}
+ if (cur_state.draw.uniform_buffer == id) {
+ cur_state.draw.uniform_buffer = 0;
+ }
}
void OpenGLState::ResetVertexArray(GLuint id) {
diff --git a/src/video_core/renderer_opengl/gl_state.h b/src/video_core/renderer_opengl/gl_state.h
index 6ecbedbb4..b8ab45bb8 100644
--- a/src/video_core/renderer_opengl/gl_state.h
+++ b/src/video_core/renderer_opengl/gl_state.h
@@ -11,6 +11,7 @@ public:
struct {
bool enabled; // GL_CULL_FACE
GLenum mode; // GL_CULL_FACE_MODE
+ GLenum front_face; // GL_FRONT_FACE
} cull;
struct {
@@ -64,7 +65,9 @@ public:
GLuint framebuffer; // GL_DRAW_FRAMEBUFFER_BINDING
GLuint vertex_array; // GL_VERTEX_ARRAY_BINDING
GLuint vertex_buffer; // GL_ARRAY_BUFFER_BINDING
+ GLuint uniform_buffer; // GL_UNIFORM_BUFFER_BINDING
GLuint shader_program; // GL_CURRENT_PROGRAM
+ bool shader_dirty;
} draw;
OpenGLState();
diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp
index 4202d828c..c34215340 100644
--- a/src/video_core/renderer_opengl/renderer_opengl.cpp
+++ b/src/video_core/renderer_opengl/renderer_opengl.cpp
@@ -21,9 +21,44 @@
#include "video_core/debug_utils/debug_utils.h"
#include "video_core/renderer_opengl/gl_rasterizer.h"
#include "video_core/renderer_opengl/gl_shader_util.h"
-#include "video_core/renderer_opengl/gl_shaders.h"
#include "video_core/renderer_opengl/renderer_opengl.h"
+static const char vertex_shader[] = R"(
+#version 150 core
+
+in vec2 vert_position;
+in vec2 vert_tex_coord;
+out vec2 frag_tex_coord;
+
+// This is a truncated 3x3 matrix for 2D transformations:
+// The upper-left 2x2 submatrix performs scaling/rotation/mirroring.
+// The third column performs translation.
+// The third row could be used for projection, which we don't need in 2D. It hence is assumed to
+// implicitly be [0, 0, 1]
+uniform mat3x2 modelview_matrix;
+
+void main() {
+ // Multiply input position by the rotscale part of the matrix and then manually translate by
+ // the last column. This is equivalent to using a full 3x3 matrix and expanding the vector
+ // to `vec3(vert_position.xy, 1.0)`
+ gl_Position = vec4(mat2(modelview_matrix) * vert_position + modelview_matrix[2], 0.0, 1.0);
+ frag_tex_coord = vert_tex_coord;
+}
+)";
+
+static const char fragment_shader[] = R"(
+#version 150 core
+
+in vec2 frag_tex_coord;
+out vec4 color;
+
+uniform sampler2D color_texture;
+
+void main() {
+ color = texture(color_texture, frag_tex_coord);
+}
+)";
+
/**
* Vertex structure that the drawn screen rectangles are composed of.
*/
@@ -58,7 +93,6 @@ static std::array<GLfloat, 3*2> MakeOrthographicMatrix(const float width, const
/// RendererOpenGL constructor
RendererOpenGL::RendererOpenGL() {
- hw_rasterizer.reset(new RasterizerOpenGL());
resolution_width = std::max(VideoCore::kScreenTopWidth, VideoCore::kScreenBottomWidth);
resolution_height = VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight;
}
@@ -122,15 +156,7 @@ void RendererOpenGL::SwapBuffers() {
profiler.BeginFrame();
- bool hw_renderer_enabled = VideoCore::g_hw_renderer_enabled;
- if (Settings::values.use_hw_renderer != hw_renderer_enabled) {
- // TODO: Save new setting value to config file for next startup
- Settings::values.use_hw_renderer = hw_renderer_enabled;
-
- if (Settings::values.use_hw_renderer) {
- hw_rasterizer->Reset();
- }
- }
+ RefreshRasterizerSetting();
if (Pica::g_debug_context && Pica::g_debug_context->recorder) {
Pica::g_debug_context->recorder->FrameFinished();
@@ -207,7 +233,7 @@ void RendererOpenGL::InitOpenGLObjects() {
glClearColor(Settings::values.bg_red, Settings::values.bg_green, Settings::values.bg_blue, 0.0f);
// Link shaders and get variable locations
- program_id = ShaderUtil::LoadShaders(GLShaders::g_vertex_shader, GLShaders::g_fragment_shader);
+ program_id = GLShader::LoadProgram(vertex_shader, fragment_shader);
uniform_modelview_matrix = glGetUniformLocation(program_id, "modelview_matrix");
uniform_color_texture = glGetUniformLocation(program_id, "color_texture");
attrib_position = glGetAttribLocation(program_id, "vert_position");
@@ -221,6 +247,7 @@ void RendererOpenGL::InitOpenGLObjects() {
state.draw.vertex_array = vertex_array_handle;
state.draw.vertex_buffer = vertex_buffer_handle;
+ state.draw.uniform_buffer = 0;
state.Apply();
// Attach vertex data to VAO
@@ -250,8 +277,6 @@ void RendererOpenGL::InitOpenGLObjects() {
state.texture_units[0].texture_2d = 0;
state.Apply();
-
- hw_rasterizer->InitObjects();
}
void RendererOpenGL::ConfigureFramebufferTexture(TextureInfo& texture,
@@ -440,6 +465,8 @@ void RendererOpenGL::Init() {
LOG_INFO(Render_OpenGL, "GL_VENDOR: %s", glGetString(GL_VENDOR));
LOG_INFO(Render_OpenGL, "GL_RENDERER: %s", glGetString(GL_RENDERER));
InitOpenGLObjects();
+
+ RefreshRasterizerSetting();
}
/// Shutdown the renderer