diff options
Diffstat (limited to '')
-rw-r--r-- | src/video_core/renderer_opengl/gl_shader_gen.cpp | 142 |
1 files changed, 126 insertions, 16 deletions
diff --git a/src/video_core/renderer_opengl/gl_shader_gen.cpp b/src/video_core/renderer_opengl/gl_shader_gen.cpp index 7abdeba05..7b44dade8 100644 --- a/src/video_core/renderer_opengl/gl_shader_gen.cpp +++ b/src/video_core/renderer_opengl/gl_shader_gen.cpp @@ -4,6 +4,7 @@ #include <array> #include <cstddef> +#include <cstring> #include "common/assert.h" #include "common/bit_field.h" #include "common/logging/log.h" @@ -23,6 +24,99 @@ using TevStageConfig = TexturingRegs::TevStageConfig; namespace GLShader { +PicaShaderConfig PicaShaderConfig::BuildFromRegs(const Pica::Regs& regs) { + PicaShaderConfig res; + + auto& state = res.state; + std::memset(&state, 0, sizeof(PicaShaderConfig::State)); + + state.scissor_test_mode = regs.rasterizer.scissor_test.mode; + + state.depthmap_enable = regs.rasterizer.depthmap_enable; + + state.alpha_test_func = regs.framebuffer.output_merger.alpha_test.enable + ? regs.framebuffer.output_merger.alpha_test.func.Value() + : Pica::FramebufferRegs::CompareFunc::Always; + + state.texture0_type = regs.texturing.texture0.type; + + state.texture2_use_coord1 = regs.texturing.main_config.texture2_use_coord1 != 0; + + // Copy relevant tev stages fields. + // We don't sync const_color here because of the high variance, it is a + // shader uniform instead. + const auto& tev_stages = regs.texturing.GetTevStages(); + DEBUG_ASSERT(state.tev_stages.size() == tev_stages.size()); + for (size_t i = 0; i < tev_stages.size(); i++) { + const auto& tev_stage = tev_stages[i]; + state.tev_stages[i].sources_raw = tev_stage.sources_raw; + state.tev_stages[i].modifiers_raw = tev_stage.modifiers_raw; + state.tev_stages[i].ops_raw = tev_stage.ops_raw; + state.tev_stages[i].scales_raw = tev_stage.scales_raw; + } + + state.fog_mode = regs.texturing.fog_mode; + state.fog_flip = regs.texturing.fog_flip != 0; + + state.combiner_buffer_input = regs.texturing.tev_combiner_buffer_input.update_mask_rgb.Value() | + regs.texturing.tev_combiner_buffer_input.update_mask_a.Value() + << 4; + + // Fragment lighting + + state.lighting.enable = !regs.lighting.disable; + state.lighting.src_num = regs.lighting.max_light_index + 1; + + for (unsigned light_index = 0; light_index < state.lighting.src_num; ++light_index) { + unsigned num = regs.lighting.light_enable.GetNum(light_index); + const auto& light = regs.lighting.light[num]; + state.lighting.light[light_index].num = num; + state.lighting.light[light_index].directional = light.config.directional != 0; + state.lighting.light[light_index].two_sided_diffuse = light.config.two_sided_diffuse != 0; + state.lighting.light[light_index].dist_atten_enable = + !regs.lighting.IsDistAttenDisabled(num); + } + + state.lighting.lut_d0.enable = regs.lighting.config1.disable_lut_d0 == 0; + state.lighting.lut_d0.abs_input = regs.lighting.abs_lut_input.disable_d0 == 0; + state.lighting.lut_d0.type = regs.lighting.lut_input.d0.Value(); + state.lighting.lut_d0.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.d0); + + state.lighting.lut_d1.enable = regs.lighting.config1.disable_lut_d1 == 0; + state.lighting.lut_d1.abs_input = regs.lighting.abs_lut_input.disable_d1 == 0; + state.lighting.lut_d1.type = regs.lighting.lut_input.d1.Value(); + state.lighting.lut_d1.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.d1); + + state.lighting.lut_fr.enable = regs.lighting.config1.disable_lut_fr == 0; + state.lighting.lut_fr.abs_input = regs.lighting.abs_lut_input.disable_fr == 0; + state.lighting.lut_fr.type = regs.lighting.lut_input.fr.Value(); + state.lighting.lut_fr.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.fr); + + state.lighting.lut_rr.enable = regs.lighting.config1.disable_lut_rr == 0; + state.lighting.lut_rr.abs_input = regs.lighting.abs_lut_input.disable_rr == 0; + state.lighting.lut_rr.type = regs.lighting.lut_input.rr.Value(); + state.lighting.lut_rr.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rr); + + state.lighting.lut_rg.enable = regs.lighting.config1.disable_lut_rg == 0; + state.lighting.lut_rg.abs_input = regs.lighting.abs_lut_input.disable_rg == 0; + state.lighting.lut_rg.type = regs.lighting.lut_input.rg.Value(); + state.lighting.lut_rg.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rg); + + state.lighting.lut_rb.enable = regs.lighting.config1.disable_lut_rb == 0; + state.lighting.lut_rb.abs_input = regs.lighting.abs_lut_input.disable_rb == 0; + state.lighting.lut_rb.type = regs.lighting.lut_input.rb.Value(); + state.lighting.lut_rb.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rb); + + state.lighting.config = regs.lighting.config0.config; + state.lighting.fresnel_selector = regs.lighting.config0.fresnel_selector; + state.lighting.bump_mode = regs.lighting.config0.bump_mode; + state.lighting.bump_selector = regs.lighting.config0.bump_selector; + state.lighting.bump_renorm = regs.lighting.config0.disable_bump_renorm == 0; + state.lighting.clamp_highlights = regs.lighting.config0.clamp_highlights != 0; + + return res; +} + /// 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 && @@ -34,6 +128,15 @@ static bool IsPassThroughTevStage(const TevStageConfig& stage) { stage.GetColorMultiplier() == 1 && stage.GetAlphaMultiplier() == 1); } +static std::string TexCoord(const PicaShaderConfig& config, int texture_unit) { + if (texture_unit == 2 && config.state.texture2_use_coord1) { + return "texcoord[1]"; + } + // TODO: if texture unit 3 (procedural texture) implementation also uses this function, + // config.state.texture3_coordinates should be repected here. + return "texcoord[" + std::to_string(texture_unit) + "]"; +} + /// Writes the specified TEV stage source component(s) static void AppendSource(std::string& out, const PicaShaderConfig& config, TevStageConfig::Source source, const std::string& index_name) { @@ -70,7 +173,7 @@ static void AppendSource(std::string& out, const PicaShaderConfig& config, out += "texture(tex[1], texcoord[1])"; break; case Source::Texture2: - out += "texture(tex[2], texcoord[2])"; + out += "texture(tex[2], " + TexCoord(config, 2) + ")"; break; case Source::PreviousBuffer: out += "combiner_buffer"; @@ -214,8 +317,6 @@ static void AppendColorCombiner(std::string& out, TevStageConfig::Operation oper 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; @@ -230,6 +331,7 @@ static void AppendColorCombiner(std::string& out, TevStageConfig::Operation oper variable_name + "[2]"; break; case Operation::Dot3_RGB: + case Operation::Dot3_RGBA: out += "vec3(dot(" + variable_name + "[0] - vec3(0.5), " + variable_name + "[1] - vec3(0.5)) * 4.0)"; break; @@ -329,17 +431,25 @@ static void WriteTevStage(std::string& out, const PicaShaderConfig& config, unsi AppendColorCombiner(out, stage.color_op, "color_results_" + index_name); out += ";\n"; - out += "float alpha_results_" + index_name + "[3] = float[3]("; - AppendAlphaModifier(out, config, stage.alpha_modifier1, stage.alpha_source1, index_name); - out += ", "; - AppendAlphaModifier(out, config, stage.alpha_modifier2, stage.alpha_source2, index_name); - out += ", "; - AppendAlphaModifier(out, config, 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"; + if (stage.color_op == TevStageConfig::Operation::Dot3_RGBA) { + // result of Dot3_RGBA operation is also placed to the alpha component + out += "float alpha_output_" + index_name + " = color_output_" + index_name + "[0];\n"; + } else { + out += "float alpha_results_" + index_name + "[3] = float[3]("; + AppendAlphaModifier(out, config, stage.alpha_modifier1, stage.alpha_source1, + index_name); + out += ", "; + AppendAlphaModifier(out, config, stage.alpha_modifier2, stage.alpha_source2, + index_name); + out += ", "; + AppendAlphaModifier(out, config, 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_" + @@ -374,8 +484,8 @@ static void WriteLighting(std::string& out, const PicaShaderConfig& config) { // Bump mapping is enabled using a normal map, read perturbation vector from the selected // texture std::string bump_selector = std::to_string(lighting.bump_selector); - out += "vec3 surface_normal = 2.0 * texture(tex[" + bump_selector + "], texcoord[" + - bump_selector + "]).rgb - 1.0;\n"; + out += "vec3 surface_normal = 2.0 * texture(tex[" + bump_selector + "], " + + TexCoord(config, lighting.bump_selector) + ").rgb - 1.0;\n"; // Recompute Z-component of perturbation if 'renorm' is enabled, this provides a higher // precision result |