gl_shader_gen: Various cleanups + moved TEV stage generation to its own function.

This commit is contained in:
bunnei 2015-10-06 18:01:53 -04:00
parent c86b9d4242
commit a74774257e

View File

@ -6,268 +6,313 @@
#include "video_core/renderer_opengl/gl_rasterizer.h" #include "video_core/renderer_opengl/gl_rasterizer.h"
#include "video_core/renderer_opengl/gl_shader_gen.h" #include "video_core/renderer_opengl/gl_shader_gen.h"
using Pica::Regs;
using TevStageConfig = Regs::TevStageConfig;
namespace GLShader { namespace GLShader {
static bool IsPassThroughTevStage(const Pica::Regs::TevStageConfig& stage) { static bool IsPassThroughTevStage(const TevStageConfig& stage) {
return (stage.color_op == Pica::Regs::TevStageConfig::Operation::Replace && return (stage.color_op == TevStageConfig::Operation::Replace &&
stage.alpha_op == Pica::Regs::TevStageConfig::Operation::Replace && stage.alpha_op == TevStageConfig::Operation::Replace &&
stage.color_source1 == Pica::Regs::TevStageConfig::Source::Previous && stage.color_source1 == TevStageConfig::Source::Previous &&
stage.alpha_source1 == Pica::Regs::TevStageConfig::Source::Previous && stage.alpha_source1 == TevStageConfig::Source::Previous &&
stage.color_modifier1 == Pica::Regs::TevStageConfig::ColorModifier::SourceColor && stage.color_modifier1 == TevStageConfig::ColorModifier::SourceColor &&
stage.alpha_modifier1 == Pica::Regs::TevStageConfig::AlphaModifier::SourceAlpha && stage.alpha_modifier1 == TevStageConfig::AlphaModifier::SourceAlpha &&
stage.GetColorMultiplier() == 1 && stage.GetColorMultiplier() == 1 &&
stage.GetAlphaMultiplier() == 1); stage.GetAlphaMultiplier() == 1);
} }
static void AppendSource(std::string& shader, Pica::Regs::TevStageConfig::Source source, const std::string& index_name) { static void AppendSource(std::string& out, TevStageConfig::Source source,
using Source = Pica::Regs::TevStageConfig::Source; const std::string& index_name) {
using Source = TevStageConfig::Source;
switch (source) { switch (source) {
case Source::PrimaryColor: case Source::PrimaryColor:
shader += "o[2]"; out += "o[2]";
break; break;
case Source::PrimaryFragmentColor: case Source::PrimaryFragmentColor:
// HACK: Until we implement fragment lighting, use primary_color // HACK: Until we implement fragment lighting, use primary_color
shader += "o[2]"; out += "o[2]";
break; break;
case Source::SecondaryFragmentColor: case Source::SecondaryFragmentColor:
// HACK: Until we implement fragment lighting, use zero // HACK: Until we implement fragment lighting, use zero
shader += "vec4(0.0, 0.0, 0.0, 0.0)"; out += "vec4(0.0, 0.0, 0.0, 0.0)";
break; break;
case Source::Texture0: case Source::Texture0:
shader += "texture(tex[0], o[3].xy)"; out += "texture(tex[0], o[3].xy)";
break; break;
case Source::Texture1: case Source::Texture1:
shader += "texture(tex[1], o[3].zw)"; out += "texture(tex[1], o[3].zw)";
break; break;
case Source::Texture2: // TODO: Unverified case Source::Texture2: // TODO: Unverified
shader += "texture(tex[2], o[5].zw)"; out += "texture(tex[2], o[5].zw)";
break; break;
case Source::PreviousBuffer: case Source::PreviousBuffer:
shader += "g_combiner_buffer"; out += "g_combiner_buffer";
break; break;
case Source::Constant: case Source::Constant:
shader += "const_color[" + index_name + "]"; out += "const_color[" + index_name + "]";
break; break;
case Source::Previous: case Source::Previous:
shader += "g_last_tex_env_out"; out += "g_last_tex_env_out";
break; break;
default: default:
shader += "vec4(0.0)"; out += "vec4(0.0)";
LOG_CRITICAL(Render_OpenGL, "Unknown source op %u", source); LOG_CRITICAL(Render_OpenGL, "Unknown source op %u", source);
break; break;
} }
} }
static void AppendColorModifier(std::string& shader, Pica::Regs::TevStageConfig::ColorModifier modifier, static void AppendColorModifier(std::string& out, TevStageConfig::ColorModifier modifier,
Pica::Regs::TevStageConfig::Source source, const std::string& index_name) { TevStageConfig::Source source, const std::string& index_name) {
using ColorModifier = Pica::Regs::TevStageConfig::ColorModifier; using ColorModifier = TevStageConfig::ColorModifier;
switch (modifier) { switch (modifier) {
case ColorModifier::SourceColor: case ColorModifier::SourceColor:
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".rgb"; out += ".rgb";
break; break;
case ColorModifier::OneMinusSourceColor: case ColorModifier::OneMinusSourceColor:
shader += "vec3(1.0) - "; out += "vec3(1.0) - ";
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".rgb"; out += ".rgb";
break; break;
case ColorModifier::SourceAlpha: case ColorModifier::SourceAlpha:
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".aaa"; out += ".aaa";
break; break;
case ColorModifier::OneMinusSourceAlpha: case ColorModifier::OneMinusSourceAlpha:
shader += "vec3(1.0) - "; out += "vec3(1.0) - ";
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".aaa"; out += ".aaa";
break; break;
case ColorModifier::SourceRed: case ColorModifier::SourceRed:
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".rrr"; out += ".rrr";
break; break;
case ColorModifier::OneMinusSourceRed: case ColorModifier::OneMinusSourceRed:
shader += "vec3(1.0) - "; out += "vec3(1.0) - ";
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".rrr"; out += ".rrr";
break; break;
case ColorModifier::SourceGreen: case ColorModifier::SourceGreen:
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".ggg"; out += ".ggg";
break; break;
case ColorModifier::OneMinusSourceGreen: case ColorModifier::OneMinusSourceGreen:
shader += "vec3(1.0) - "; out += "vec3(1.0) - ";
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".ggg"; out += ".ggg";
break; break;
case ColorModifier::SourceBlue: case ColorModifier::SourceBlue:
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".bbb"; out += ".bbb";
break; break;
case ColorModifier::OneMinusSourceBlue: case ColorModifier::OneMinusSourceBlue:
shader += "vec3(1.0) - "; out += "vec3(1.0) - ";
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".bbb"; out += ".bbb";
break; break;
default: default:
shader += "vec3(0.0)"; out += "vec3(0.0)";
LOG_CRITICAL(Render_OpenGL, "Unknown color modifier op %u", modifier); LOG_CRITICAL(Render_OpenGL, "Unknown color modifier op %u", modifier);
break; break;
} }
} }
static void AppendAlphaModifier(std::string& shader, Pica::Regs::TevStageConfig::AlphaModifier modifier, static void AppendAlphaModifier(std::string& out, TevStageConfig::AlphaModifier modifier,
Pica::Regs::TevStageConfig::Source source, const std::string& index_name) { TevStageConfig::Source source, const std::string& index_name) {
using AlphaModifier = Pica::Regs::TevStageConfig::AlphaModifier; using AlphaModifier = TevStageConfig::AlphaModifier;
switch (modifier) { switch (modifier) {
case AlphaModifier::SourceAlpha: case AlphaModifier::SourceAlpha:
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".a"; out += ".a";
break; break;
case AlphaModifier::OneMinusSourceAlpha: case AlphaModifier::OneMinusSourceAlpha:
shader += "1.0 - "; out += "1.0 - ";
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".a"; out += ".a";
break; break;
case AlphaModifier::SourceRed: case AlphaModifier::SourceRed:
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".r"; out += ".r";
break; break;
case AlphaModifier::OneMinusSourceRed: case AlphaModifier::OneMinusSourceRed:
shader += "1.0 - "; out += "1.0 - ";
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".r"; out += ".r";
break; break;
case AlphaModifier::SourceGreen: case AlphaModifier::SourceGreen:
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".g"; out += ".g";
break; break;
case AlphaModifier::OneMinusSourceGreen: case AlphaModifier::OneMinusSourceGreen:
shader += "1.0 - "; out += "1.0 - ";
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".g"; out += ".g";
break; break;
case AlphaModifier::SourceBlue: case AlphaModifier::SourceBlue:
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".b"; out += ".b";
break; break;
case AlphaModifier::OneMinusSourceBlue: case AlphaModifier::OneMinusSourceBlue:
shader += "1.0 - "; out += "1.0 - ";
AppendSource(shader, source, index_name); AppendSource(out, source, index_name);
shader += ".b"; out += ".b";
break; break;
default: default:
shader += "vec3(0.0)"; out += "vec3(0.0)";
LOG_CRITICAL(Render_OpenGL, "Unknown alpha modifier op %u", modifier); LOG_CRITICAL(Render_OpenGL, "Unknown alpha modifier op %u", modifier);
break; break;
} }
} }
static void AppendColorCombiner(std::string& shader, Pica::Regs::TevStageConfig::Operation operation, static void AppendColorCombiner(std::string& out, TevStageConfig::Operation operation,
const std::string& variable_name) { const std::string& variable_name) {
using Operation = Pica::Regs::TevStageConfig::Operation; using Operation = TevStageConfig::Operation;
switch (operation) { switch (operation) {
case Operation::Replace: case Operation::Replace:
shader += variable_name + "[0]"; out += variable_name + "[0]";
break; break;
case Operation::Modulate: case Operation::Modulate:
shader += variable_name + "[0] * " + variable_name + "[1]"; out += variable_name + "[0] * " + variable_name + "[1]";
break; break;
case Operation::Add: case Operation::Add:
shader += "min(" + variable_name + "[0] + " + variable_name + "[1], vec3(1.0))"; out += "min(" + variable_name + "[0] + " + variable_name + "[1], vec3(1.0))";
break; break;
case Operation::AddSigned: case Operation::AddSigned:
shader += "clamp(" + variable_name + "[0] + " + variable_name + "[1] - vec3(0.5), vec3(0.0), vec3(1.0))"; out += "clamp(" + variable_name + "[0] + " + variable_name + "[1] - vec3(0.5), vec3(0.0), vec3(1.0))";
break; break;
case Operation::Lerp: case Operation::Lerp:
shader += variable_name + "[0] * " + variable_name + "[2] + " + variable_name + "[1] * (vec3(1.0) - " + variable_name + "[2])"; out += variable_name + "[0] * " + variable_name + "[2] + " + variable_name + "[1] * (vec3(1.0) - " + variable_name + "[2])";
break; break;
case Operation::Subtract: case Operation::Subtract:
shader += "max(" + variable_name + "[0] - " + variable_name + "[1], vec3(0.0))"; out += "max(" + variable_name + "[0] - " + variable_name + "[1], vec3(0.0))";
break; break;
case Operation::MultiplyThenAdd: case Operation::MultiplyThenAdd:
shader += "min(" + variable_name + "[0] * " + variable_name + "[1] + " + variable_name + "[2], vec3(1.0))"; out += "min(" + variable_name + "[0] * " + variable_name + "[1] + " + variable_name + "[2], vec3(1.0))";
break; break;
case Operation::AddThenMultiply: case Operation::AddThenMultiply:
shader += "min(" + variable_name + "[0] + " + variable_name + "[1], vec3(1.0)) * " + variable_name + "[2]"; out += "min(" + variable_name + "[0] + " + variable_name + "[1], vec3(1.0)) * " + variable_name + "[2]";
break; break;
default: default:
shader += "vec3(0.0)"; out += "vec3(0.0)";
LOG_CRITICAL(Render_OpenGL, "Unknown color comb op %u", operation); LOG_CRITICAL(Render_OpenGL, "Unknown color combiner operation: %u", operation);
break; break;
} }
} }
static void AppendAlphaCombiner(std::string& shader, Pica::Regs::TevStageConfig::Operation operation, static void AppendAlphaCombiner(std::string& out, TevStageConfig::Operation operation,
const std::string& variable_name) { const std::string& variable_name) {
using Operation = Pica::Regs::TevStageConfig::Operation; using Operation = TevStageConfig::Operation;
switch (operation) { switch (operation) {
case Operation::Replace: case Operation::Replace:
shader += variable_name + "[0]"; out += variable_name + "[0]";
break; break;
case Operation::Modulate: case Operation::Modulate:
shader += variable_name + "[0] * " + variable_name + "[1]"; out += variable_name + "[0] * " + variable_name + "[1]";
break; break;
case Operation::Add: case Operation::Add:
shader += "min(" + variable_name + "[0] + " + variable_name + "[1], 1.0)"; out += "min(" + variable_name + "[0] + " + variable_name + "[1], 1.0)";
break; break;
case Operation::AddSigned: case Operation::AddSigned:
shader += "clamp(" + variable_name + "[0] + " + variable_name + "[1] - 0.5, 0.0, 1.0)"; out += "clamp(" + variable_name + "[0] + " + variable_name + "[1] - 0.5, 0.0, 1.0)";
break; break;
case Operation::Lerp: case Operation::Lerp:
shader += variable_name + "[0] * " + variable_name + "[2] + " + variable_name + "[1] * (1.0 - " + variable_name + "[2])"; out += variable_name + "[0] * " + variable_name + "[2] + " + variable_name + "[1] * (1.0 - " + variable_name + "[2])";
break; break;
case Operation::Subtract: case Operation::Subtract:
shader += "max(" + variable_name + "[0] - " + variable_name + "[1], 0.0)"; out += "max(" + variable_name + "[0] - " + variable_name + "[1], 0.0)";
break; break;
case Operation::MultiplyThenAdd: case Operation::MultiplyThenAdd:
shader += "min(" + variable_name + "[0] * " + variable_name + "[1] + " + variable_name + "[2], 1.0)"; out += "min(" + variable_name + "[0] * " + variable_name + "[1] + " + variable_name + "[2], 1.0)";
break; break;
case Operation::AddThenMultiply: case Operation::AddThenMultiply:
shader += "min(" + variable_name + "[0] + " + variable_name + "[1], 1.0) * " + variable_name + "[2]"; out += "min(" + variable_name + "[0] + " + variable_name + "[1], 1.0) * " + variable_name + "[2]";
break; break;
default: default:
shader += "0.0"; out += "0.0";
LOG_CRITICAL(Render_OpenGL, "Unknown alpha combiner op %u", operation); LOG_CRITICAL(Render_OpenGL, "Unknown alpha combiner operation: %u", operation);
break; break;
} }
} }
static void AppendAlphaTestCondition(std::string& shader, Pica::Regs::CompareFunc func) { static void AppendAlphaTestCondition(std::string& out, Regs::CompareFunc func) {
using CompareFunc = Pica::Regs::CompareFunc; using CompareFunc = Regs::CompareFunc;
switch (func) { switch (func) {
case CompareFunc::Never: case CompareFunc::Never:
shader += "true"; out += "true";
break; break;
case CompareFunc::Always: case CompareFunc::Always:
shader += "false"; out += "false";
break; break;
case CompareFunc::Equal: case CompareFunc::Equal:
shader += "int(g_last_tex_env_out.a * 255.0f) != alphatest_ref"; out += "int(g_last_tex_env_out.a * 255.0f) != alphatest_ref";
break; break;
case CompareFunc::NotEqual: case CompareFunc::NotEqual:
shader += "int(g_last_tex_env_out.a * 255.0f) == alphatest_ref"; out += "int(g_last_tex_env_out.a * 255.0f) == alphatest_ref";
break; break;
case CompareFunc::LessThan: case CompareFunc::LessThan:
shader += "int(g_last_tex_env_out.a * 255.0f) >= alphatest_ref"; out += "int(g_last_tex_env_out.a * 255.0f) >= alphatest_ref";
break; break;
case CompareFunc::LessThanOrEqual: case CompareFunc::LessThanOrEqual:
shader += "int(g_last_tex_env_out.a * 255.0f) > alphatest_ref"; out += "int(g_last_tex_env_out.a * 255.0f) > alphatest_ref";
break; break;
case CompareFunc::GreaterThan: case CompareFunc::GreaterThan:
shader += "int(g_last_tex_env_out.a * 255.0f) <= alphatest_ref"; out += "int(g_last_tex_env_out.a * 255.0f) <= alphatest_ref";
break; break;
case CompareFunc::GreaterThanOrEqual: case CompareFunc::GreaterThanOrEqual:
shader += "int(g_last_tex_env_out.a * 255.0f) < alphatest_ref"; out += "int(g_last_tex_env_out.a * 255.0f) < alphatest_ref";
break; break;
default: default:
shader += "false"; out += "false";
LOG_CRITICAL(Render_OpenGL, "Unknown alpha test condition %u", func); LOG_CRITICAL(Render_OpenGL, "Unknown alpha test condition %u", func);
break; break;
} }
} }
static void WriteTevStage(std::string& out, const ShaderCacheKey& 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 += "g_last_tex_env_out = vec4(min(color_output_" + index_name + " * " +
std::to_string(stage.GetColorMultiplier()) + ".0, 1.0), min(alpha_output_" + index_name + " * " +
std::to_string(stage.GetAlphaMultiplier()) + ".0, 1.0));\n";
}
if (config.TevStageUpdatesCombinerBufferColor(index))
out += "g_combiner_buffer.rgb = g_last_tex_env_out.rgb;\n";
if (config.TevStageUpdatesCombinerBufferAlpha(index))
out += "g_combiner_buffer.a = g_last_tex_env_out.a;\n";
}
std::string GenerateFragmentShader(const ShaderCacheKey& config) { std::string GenerateFragmentShader(const ShaderCacheKey& config) {
std::string shader = R"( std::string out = R"(
#version 150 core #version 150 core
#define NUM_VTX_ATTR 7 #define NUM_VTX_ATTR 7
@ -288,63 +333,27 @@ vec4 g_last_tex_env_out = vec4(0.0, 0.0, 0.0, 0.0);
)"; )";
// Do not do any sort of processing if it's obvious we're not going to pass the alpha test // 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 == Pica::Regs::CompareFunc::Never) { if (config.alpha_test_func == Regs::CompareFunc::Never) {
shader += "discard;"; out += "discard;";
return shader; return out;
} }
auto& tev_stages = config.tev_stages; for (std::size_t index = 0; index < config.tev_stages.size(); ++index)
for (unsigned tev_stage_index = 0; tev_stage_index < tev_stages.size(); ++tev_stage_index) { WriteTevStage(out, config, (unsigned)index);
auto& tev_stage = tev_stages[tev_stage_index];
if (!IsPassThroughTevStage(tev_stage)) {
std::string index_name = std::to_string(tev_stage_index);
shader += "vec3 color_results_" + index_name + "[3] = vec3[3]("; if (config.alpha_test_func != Regs::CompareFunc::Always) {
AppendColorModifier(shader, tev_stage.color_modifier1, tev_stage.color_source1, index_name); out += "if (";
shader += ", "; AppendAlphaTestCondition(out, config.alpha_test_func);
AppendColorModifier(shader, tev_stage.color_modifier2, tev_stage.color_source2, index_name); out += ") {\n discard;\n }\n";
shader += ", ";
AppendColorModifier(shader, tev_stage.color_modifier3, tev_stage.color_source3, index_name);
shader += ");\n";
shader += "vec3 color_output_" + index_name + " = ";
AppendColorCombiner(shader, tev_stage.color_op, "color_results_" + index_name);
shader += ";\n";
shader += "float alpha_results_" + index_name + "[3] = float[3](";
AppendAlphaModifier(shader, tev_stage.alpha_modifier1, tev_stage.alpha_source1, index_name);
shader += ", ";
AppendAlphaModifier(shader, tev_stage.alpha_modifier2, tev_stage.alpha_source2, index_name);
shader += ", ";
AppendAlphaModifier(shader, tev_stage.alpha_modifier3, tev_stage.alpha_source3, index_name);
shader += ");\n";
shader += "float alpha_output_" + index_name + " = ";
AppendAlphaCombiner(shader, tev_stage.alpha_op, "alpha_results_" + index_name);
shader += ";\n";
shader += "g_last_tex_env_out = vec4(min(color_output_" + index_name + " * " + std::to_string(tev_stage.GetColorMultiplier()) + ".0, 1.0), min(alpha_output_" + index_name + " * " + std::to_string(tev_stage.GetAlphaMultiplier()) + ".0, 1.0));\n";
}
if (config.TevStageUpdatesCombinerBufferColor(tev_stage_index))
shader += "g_combiner_buffer.rgb = g_last_tex_env_out.rgb;\n";
if (config.TevStageUpdatesCombinerBufferAlpha(tev_stage_index))
shader += "g_combiner_buffer.a = g_last_tex_env_out.a;\n";
} }
if (config.alpha_test_func != Pica::Regs::CompareFunc::Always) { out += "color = g_last_tex_env_out;\n}";
shader += "if (";
AppendAlphaTestCondition(shader, config.alpha_test_func);
shader += ") {\n discard;\n }\n";
}
shader += "color = g_last_tex_env_out;\n}"; return out;
return shader;
} }
std::string GenerateVertexShader() { std::string GenerateVertexShader() {
static const std::string shader_str = R"( static const std::string out = R"(
#version 150 core #version 150 core
#define NUM_VTX_ATTR 7 #define NUM_VTX_ATTR 7
@ -365,7 +374,7 @@ void main() {
gl_Position = vec4(vert_position.x, -vert_position.y, -vert_position.z, vert_position.w); gl_Position = vec4(vert_position.x, -vert_position.y, -vert_position.z, vert_position.w);
} }
)"; )";
return shader_str; return out;
} }
} // namespace GLShaderGen } // namespace GLShader