diff options
| author | 2017-04-19 22:09:51 -0700 | |
|---|---|---|
| committer | 2017-04-19 22:09:51 -0700 | |
| commit | 5d852467a25a749a729056edb4597173976a5820 (patch) | |
| tree | 2cb91989eaf317cb0b2fd6fa2fa052aa8836d05d | |
| parent | Merge pull request #2532 from wwylele/ldrro-ipc (diff) | |
| parent | OpenGL: Pass Pica regs via parameter (diff) | |
| download | yuzu-5d852467a25a749a729056edb4597173976a5820.tar.gz yuzu-5d852467a25a749a729056edb4597173976a5820.tar.xz yuzu-5d852467a25a749a729056edb4597173976a5820.zip | |
Merge pull request #2666 from yuriks/gl-cleanups
PicaShaderConfig cleanups
| -rw-r--r-- | src/video_core/renderer_opengl/gl_rasterizer.cpp | 3 | ||||
| -rw-r--r-- | src/video_core/renderer_opengl/gl_rasterizer.h | 201 | ||||
| -rw-r--r-- | src/video_core/renderer_opengl/gl_shader_gen.cpp | 92 | ||||
| -rw-r--r-- | src/video_core/renderer_opengl/gl_shader_gen.h | 122 | ||||
| -rw-r--r-- | src/video_core/renderer_opengl/gl_shader_util.h | 11 |
5 files changed, 215 insertions, 214 deletions
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index de1d5eba7..a47307099 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp | |||
| @@ -20,7 +20,6 @@ | |||
| 20 | #include "video_core/regs_texturing.h" | 20 | #include "video_core/regs_texturing.h" |
| 21 | #include "video_core/renderer_opengl/gl_rasterizer.h" | 21 | #include "video_core/renderer_opengl/gl_rasterizer.h" |
| 22 | #include "video_core/renderer_opengl/gl_shader_gen.h" | 22 | #include "video_core/renderer_opengl/gl_shader_gen.h" |
| 23 | #include "video_core/renderer_opengl/gl_shader_util.h" | ||
| 24 | #include "video_core/renderer_opengl/pica_to_gl.h" | 23 | #include "video_core/renderer_opengl/pica_to_gl.h" |
| 25 | #include "video_core/renderer_opengl/renderer_opengl.h" | 24 | #include "video_core/renderer_opengl/renderer_opengl.h" |
| 26 | 25 | ||
| @@ -1005,7 +1004,7 @@ void RasterizerOpenGL::SamplerInfo::SyncWithConfig( | |||
| 1005 | } | 1004 | } |
| 1006 | 1005 | ||
| 1007 | void RasterizerOpenGL::SetShader() { | 1006 | void RasterizerOpenGL::SetShader() { |
| 1008 | PicaShaderConfig config = PicaShaderConfig::CurrentConfig(); | 1007 | auto config = GLShader::PicaShaderConfig::BuildFromRegs(Pica::g_state.regs); |
| 1009 | std::unique_ptr<PicaShader> shader = std::make_unique<PicaShader>(); | 1008 | std::unique_ptr<PicaShader> shader = std::make_unique<PicaShader>(); |
| 1010 | 1009 | ||
| 1011 | // Find (or generate) the GLSL shader for the current TEV state | 1010 | // Find (or generate) the GLSL shader for the current TEV state |
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h index ecf737438..3e1770d77 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.h +++ b/src/video_core/renderer_opengl/gl_rasterizer.h | |||
| @@ -25,210 +25,13 @@ | |||
| 25 | #include "video_core/regs_texturing.h" | 25 | #include "video_core/regs_texturing.h" |
| 26 | #include "video_core/renderer_opengl/gl_rasterizer_cache.h" | 26 | #include "video_core/renderer_opengl/gl_rasterizer_cache.h" |
| 27 | #include "video_core/renderer_opengl/gl_resource_manager.h" | 27 | #include "video_core/renderer_opengl/gl_resource_manager.h" |
| 28 | #include "video_core/renderer_opengl/gl_shader_gen.h" | ||
| 28 | #include "video_core/renderer_opengl/gl_state.h" | 29 | #include "video_core/renderer_opengl/gl_state.h" |
| 29 | #include "video_core/renderer_opengl/pica_to_gl.h" | 30 | #include "video_core/renderer_opengl/pica_to_gl.h" |
| 30 | #include "video_core/shader/shader.h" | 31 | #include "video_core/shader/shader.h" |
| 31 | 32 | ||
| 32 | struct ScreenInfo; | 33 | struct ScreenInfo; |
| 33 | 34 | ||
| 34 | /** | ||
| 35 | * This struct contains all state used to generate the GLSL shader program that emulates the current | ||
| 36 | * Pica register configuration. This struct is used as a cache key for generated GLSL shader | ||
| 37 | * programs. The functions in gl_shader_gen.cpp should retrieve state from this struct only, not by | ||
| 38 | * directly accessing Pica registers. This should reduce the risk of bugs in shader generation where | ||
| 39 | * Pica state is not being captured in the shader cache key, thereby resulting in (what should be) | ||
| 40 | * two separate shaders sharing the same key. | ||
| 41 | * | ||
| 42 | * We use a union because "implicitly-defined copy/move constructor for a union X copies the object | ||
| 43 | * representation of X." and "implicitly-defined copy assignment operator for a union X copies the | ||
| 44 | * object representation (3.9) of X." = Bytewise copy instead of memberwise copy. This is important | ||
| 45 | * because the padding bytes are included in the hash and comparison between objects. | ||
| 46 | */ | ||
| 47 | union PicaShaderConfig { | ||
| 48 | |||
| 49 | /// Construct a PicaShaderConfig with the current Pica register configuration. | ||
| 50 | static PicaShaderConfig CurrentConfig() { | ||
| 51 | PicaShaderConfig res; | ||
| 52 | |||
| 53 | auto& state = res.state; | ||
| 54 | std::memset(&state, 0, sizeof(PicaShaderConfig::State)); | ||
| 55 | |||
| 56 | const auto& regs = Pica::g_state.regs; | ||
| 57 | |||
| 58 | state.scissor_test_mode = regs.rasterizer.scissor_test.mode; | ||
| 59 | |||
| 60 | state.depthmap_enable = regs.rasterizer.depthmap_enable; | ||
| 61 | |||
| 62 | state.alpha_test_func = regs.framebuffer.output_merger.alpha_test.enable | ||
| 63 | ? regs.framebuffer.output_merger.alpha_test.func.Value() | ||
| 64 | : Pica::FramebufferRegs::CompareFunc::Always; | ||
| 65 | |||
| 66 | state.texture0_type = regs.texturing.texture0.type; | ||
| 67 | |||
| 68 | // Copy relevant tev stages fields. | ||
| 69 | // We don't sync const_color here because of the high variance, it is a | ||
| 70 | // shader uniform instead. | ||
| 71 | const auto& tev_stages = regs.texturing.GetTevStages(); | ||
| 72 | DEBUG_ASSERT(state.tev_stages.size() == tev_stages.size()); | ||
| 73 | for (size_t i = 0; i < tev_stages.size(); i++) { | ||
| 74 | const auto& tev_stage = tev_stages[i]; | ||
| 75 | state.tev_stages[i].sources_raw = tev_stage.sources_raw; | ||
| 76 | state.tev_stages[i].modifiers_raw = tev_stage.modifiers_raw; | ||
| 77 | state.tev_stages[i].ops_raw = tev_stage.ops_raw; | ||
| 78 | state.tev_stages[i].scales_raw = tev_stage.scales_raw; | ||
| 79 | } | ||
| 80 | |||
| 81 | state.fog_mode = regs.texturing.fog_mode; | ||
| 82 | state.fog_flip = regs.texturing.fog_flip != 0; | ||
| 83 | |||
| 84 | state.combiner_buffer_input = | ||
| 85 | regs.texturing.tev_combiner_buffer_input.update_mask_rgb.Value() | | ||
| 86 | regs.texturing.tev_combiner_buffer_input.update_mask_a.Value() << 4; | ||
| 87 | |||
| 88 | // Fragment lighting | ||
| 89 | |||
| 90 | state.lighting.enable = !regs.lighting.disable; | ||
| 91 | state.lighting.src_num = regs.lighting.max_light_index + 1; | ||
| 92 | |||
| 93 | for (unsigned light_index = 0; light_index < state.lighting.src_num; ++light_index) { | ||
| 94 | unsigned num = regs.lighting.light_enable.GetNum(light_index); | ||
| 95 | const auto& light = regs.lighting.light[num]; | ||
| 96 | state.lighting.light[light_index].num = num; | ||
| 97 | state.lighting.light[light_index].directional = light.config.directional != 0; | ||
| 98 | state.lighting.light[light_index].two_sided_diffuse = | ||
| 99 | light.config.two_sided_diffuse != 0; | ||
| 100 | state.lighting.light[light_index].dist_atten_enable = | ||
| 101 | !regs.lighting.IsDistAttenDisabled(num); | ||
| 102 | } | ||
| 103 | |||
| 104 | state.lighting.lut_d0.enable = regs.lighting.config1.disable_lut_d0 == 0; | ||
| 105 | state.lighting.lut_d0.abs_input = regs.lighting.abs_lut_input.disable_d0 == 0; | ||
| 106 | state.lighting.lut_d0.type = regs.lighting.lut_input.d0.Value(); | ||
| 107 | state.lighting.lut_d0.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.d0); | ||
| 108 | |||
| 109 | state.lighting.lut_d1.enable = regs.lighting.config1.disable_lut_d1 == 0; | ||
| 110 | state.lighting.lut_d1.abs_input = regs.lighting.abs_lut_input.disable_d1 == 0; | ||
| 111 | state.lighting.lut_d1.type = regs.lighting.lut_input.d1.Value(); | ||
| 112 | state.lighting.lut_d1.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.d1); | ||
| 113 | |||
| 114 | state.lighting.lut_fr.enable = regs.lighting.config1.disable_lut_fr == 0; | ||
| 115 | state.lighting.lut_fr.abs_input = regs.lighting.abs_lut_input.disable_fr == 0; | ||
| 116 | state.lighting.lut_fr.type = regs.lighting.lut_input.fr.Value(); | ||
| 117 | state.lighting.lut_fr.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.fr); | ||
| 118 | |||
| 119 | state.lighting.lut_rr.enable = regs.lighting.config1.disable_lut_rr == 0; | ||
| 120 | state.lighting.lut_rr.abs_input = regs.lighting.abs_lut_input.disable_rr == 0; | ||
| 121 | state.lighting.lut_rr.type = regs.lighting.lut_input.rr.Value(); | ||
| 122 | state.lighting.lut_rr.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rr); | ||
| 123 | |||
| 124 | state.lighting.lut_rg.enable = regs.lighting.config1.disable_lut_rg == 0; | ||
| 125 | state.lighting.lut_rg.abs_input = regs.lighting.abs_lut_input.disable_rg == 0; | ||
| 126 | state.lighting.lut_rg.type = regs.lighting.lut_input.rg.Value(); | ||
| 127 | state.lighting.lut_rg.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rg); | ||
| 128 | |||
| 129 | state.lighting.lut_rb.enable = regs.lighting.config1.disable_lut_rb == 0; | ||
| 130 | state.lighting.lut_rb.abs_input = regs.lighting.abs_lut_input.disable_rb == 0; | ||
| 131 | state.lighting.lut_rb.type = regs.lighting.lut_input.rb.Value(); | ||
| 132 | state.lighting.lut_rb.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rb); | ||
| 133 | |||
| 134 | state.lighting.config = regs.lighting.config0.config; | ||
| 135 | state.lighting.fresnel_selector = regs.lighting.config0.fresnel_selector; | ||
| 136 | state.lighting.bump_mode = regs.lighting.config0.bump_mode; | ||
| 137 | state.lighting.bump_selector = regs.lighting.config0.bump_selector; | ||
| 138 | state.lighting.bump_renorm = regs.lighting.config0.disable_bump_renorm == 0; | ||
| 139 | state.lighting.clamp_highlights = regs.lighting.config0.clamp_highlights != 0; | ||
| 140 | |||
| 141 | return res; | ||
| 142 | } | ||
| 143 | |||
| 144 | bool TevStageUpdatesCombinerBufferColor(unsigned stage_index) const { | ||
| 145 | return (stage_index < 4) && (state.combiner_buffer_input & (1 << stage_index)); | ||
| 146 | } | ||
| 147 | |||
| 148 | bool TevStageUpdatesCombinerBufferAlpha(unsigned stage_index) const { | ||
| 149 | return (stage_index < 4) && ((state.combiner_buffer_input >> 4) & (1 << stage_index)); | ||
| 150 | } | ||
| 151 | |||
| 152 | bool operator==(const PicaShaderConfig& o) const { | ||
| 153 | return std::memcmp(&state, &o.state, sizeof(PicaShaderConfig::State)) == 0; | ||
| 154 | }; | ||
| 155 | |||
| 156 | // NOTE: MSVC15 (Update 2) doesn't think `delete`'d constructors and operators are TC. | ||
| 157 | // This makes BitField not TC when used in a union or struct so we have to resort | ||
| 158 | // to this ugly hack. | ||
| 159 | // Once that bug is fixed we can use Pica::Regs::TevStageConfig here. | ||
| 160 | // Doesn't include const_color because we don't sync it, see comment in CurrentConfig() | ||
| 161 | struct TevStageConfigRaw { | ||
| 162 | u32 sources_raw; | ||
| 163 | u32 modifiers_raw; | ||
| 164 | u32 ops_raw; | ||
| 165 | u32 scales_raw; | ||
| 166 | explicit operator Pica::TexturingRegs::TevStageConfig() const noexcept { | ||
| 167 | Pica::TexturingRegs::TevStageConfig stage; | ||
| 168 | stage.sources_raw = sources_raw; | ||
| 169 | stage.modifiers_raw = modifiers_raw; | ||
| 170 | stage.ops_raw = ops_raw; | ||
| 171 | stage.const_color = 0; | ||
| 172 | stage.scales_raw = scales_raw; | ||
| 173 | return stage; | ||
| 174 | } | ||
| 175 | }; | ||
| 176 | |||
| 177 | struct State { | ||
| 178 | Pica::FramebufferRegs::CompareFunc alpha_test_func; | ||
| 179 | Pica::RasterizerRegs::ScissorMode scissor_test_mode; | ||
| 180 | Pica::TexturingRegs::TextureConfig::TextureType texture0_type; | ||
| 181 | std::array<TevStageConfigRaw, 6> tev_stages; | ||
| 182 | u8 combiner_buffer_input; | ||
| 183 | |||
| 184 | Pica::RasterizerRegs::DepthBuffering depthmap_enable; | ||
| 185 | Pica::TexturingRegs::FogMode fog_mode; | ||
| 186 | bool fog_flip; | ||
| 187 | |||
| 188 | struct { | ||
| 189 | struct { | ||
| 190 | unsigned num; | ||
| 191 | bool directional; | ||
| 192 | bool two_sided_diffuse; | ||
| 193 | bool dist_atten_enable; | ||
| 194 | } light[8]; | ||
| 195 | |||
| 196 | bool enable; | ||
| 197 | unsigned src_num; | ||
| 198 | Pica::LightingRegs::LightingBumpMode bump_mode; | ||
| 199 | unsigned bump_selector; | ||
| 200 | bool bump_renorm; | ||
| 201 | bool clamp_highlights; | ||
| 202 | |||
| 203 | Pica::LightingRegs::LightingConfig config; | ||
| 204 | Pica::LightingRegs::LightingFresnelSelector fresnel_selector; | ||
| 205 | |||
| 206 | struct { | ||
| 207 | bool enable; | ||
| 208 | bool abs_input; | ||
| 209 | Pica::LightingRegs::LightingLutInput type; | ||
| 210 | float scale; | ||
| 211 | } lut_d0, lut_d1, lut_fr, lut_rr, lut_rg, lut_rb; | ||
| 212 | } lighting; | ||
| 213 | |||
| 214 | } state; | ||
| 215 | }; | ||
| 216 | #if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER) | ||
| 217 | static_assert(std::is_trivially_copyable<PicaShaderConfig::State>::value, | ||
| 218 | "PicaShaderConfig::State must be trivially copyable"); | ||
| 219 | #endif | ||
| 220 | |||
| 221 | namespace std { | ||
| 222 | |||
| 223 | template <> | ||
| 224 | struct hash<PicaShaderConfig> { | ||
| 225 | size_t operator()(const PicaShaderConfig& k) const { | ||
| 226 | return Common::ComputeHash64(&k.state, sizeof(PicaShaderConfig::State)); | ||
| 227 | } | ||
| 228 | }; | ||
| 229 | |||
| 230 | } // namespace std | ||
| 231 | |||
| 232 | class RasterizerOpenGL : public VideoCore::RasterizerInterface { | 35 | class RasterizerOpenGL : public VideoCore::RasterizerInterface { |
| 233 | public: | 36 | public: |
| 234 | RasterizerOpenGL(); | 37 | RasterizerOpenGL(); |
| @@ -437,7 +240,7 @@ private: | |||
| 437 | 240 | ||
| 438 | std::vector<HardwareVertex> vertex_batch; | 241 | std::vector<HardwareVertex> vertex_batch; |
| 439 | 242 | ||
| 440 | std::unordered_map<PicaShaderConfig, std::unique_ptr<PicaShader>> shader_cache; | 243 | std::unordered_map<GLShader::PicaShaderConfig, std::unique_ptr<PicaShader>> shader_cache; |
| 441 | const PicaShader* current_shader = nullptr; | 244 | const PicaShader* current_shader = nullptr; |
| 442 | bool shader_dirty; | 245 | bool shader_dirty; |
| 443 | 246 | ||
diff --git a/src/video_core/renderer_opengl/gl_shader_gen.cpp b/src/video_core/renderer_opengl/gl_shader_gen.cpp index 7abdeba05..54a8dde15 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 @@ | |||
| 4 | 4 | ||
| 5 | #include <array> | 5 | #include <array> |
| 6 | #include <cstddef> | 6 | #include <cstddef> |
| 7 | #include <cstring> | ||
| 7 | #include "common/assert.h" | 8 | #include "common/assert.h" |
| 8 | #include "common/bit_field.h" | 9 | #include "common/bit_field.h" |
| 9 | #include "common/logging/log.h" | 10 | #include "common/logging/log.h" |
| @@ -23,6 +24,97 @@ using TevStageConfig = TexturingRegs::TevStageConfig; | |||
| 23 | 24 | ||
| 24 | namespace GLShader { | 25 | namespace GLShader { |
| 25 | 26 | ||
| 27 | PicaShaderConfig PicaShaderConfig::BuildFromRegs(const Pica::Regs& regs) { | ||
| 28 | PicaShaderConfig res; | ||
| 29 | |||
| 30 | auto& state = res.state; | ||
| 31 | std::memset(&state, 0, sizeof(PicaShaderConfig::State)); | ||
| 32 | |||
| 33 | state.scissor_test_mode = regs.rasterizer.scissor_test.mode; | ||
| 34 | |||
| 35 | state.depthmap_enable = regs.rasterizer.depthmap_enable; | ||
| 36 | |||
| 37 | state.alpha_test_func = regs.framebuffer.output_merger.alpha_test.enable | ||
| 38 | ? regs.framebuffer.output_merger.alpha_test.func.Value() | ||
| 39 | : Pica::FramebufferRegs::CompareFunc::Always; | ||
| 40 | |||
| 41 | state.texture0_type = regs.texturing.texture0.type; | ||
| 42 | |||
| 43 | // Copy relevant tev stages fields. | ||
| 44 | // We don't sync const_color here because of the high variance, it is a | ||
| 45 | // shader uniform instead. | ||
| 46 | const auto& tev_stages = regs.texturing.GetTevStages(); | ||
| 47 | DEBUG_ASSERT(state.tev_stages.size() == tev_stages.size()); | ||
| 48 | for (size_t i = 0; i < tev_stages.size(); i++) { | ||
| 49 | const auto& tev_stage = tev_stages[i]; | ||
| 50 | state.tev_stages[i].sources_raw = tev_stage.sources_raw; | ||
| 51 | state.tev_stages[i].modifiers_raw = tev_stage.modifiers_raw; | ||
| 52 | state.tev_stages[i].ops_raw = tev_stage.ops_raw; | ||
| 53 | state.tev_stages[i].scales_raw = tev_stage.scales_raw; | ||
| 54 | } | ||
| 55 | |||
| 56 | state.fog_mode = regs.texturing.fog_mode; | ||
| 57 | state.fog_flip = regs.texturing.fog_flip != 0; | ||
| 58 | |||
| 59 | state.combiner_buffer_input = regs.texturing.tev_combiner_buffer_input.update_mask_rgb.Value() | | ||
| 60 | regs.texturing.tev_combiner_buffer_input.update_mask_a.Value() | ||
| 61 | << 4; | ||
| 62 | |||
| 63 | // Fragment lighting | ||
| 64 | |||
| 65 | state.lighting.enable = !regs.lighting.disable; | ||
| 66 | state.lighting.src_num = regs.lighting.max_light_index + 1; | ||
| 67 | |||
| 68 | for (unsigned light_index = 0; light_index < state.lighting.src_num; ++light_index) { | ||
| 69 | unsigned num = regs.lighting.light_enable.GetNum(light_index); | ||
| 70 | const auto& light = regs.lighting.light[num]; | ||
| 71 | state.lighting.light[light_index].num = num; | ||
| 72 | state.lighting.light[light_index].directional = light.config.directional != 0; | ||
| 73 | state.lighting.light[light_index].two_sided_diffuse = light.config.two_sided_diffuse != 0; | ||
| 74 | state.lighting.light[light_index].dist_atten_enable = | ||
| 75 | !regs.lighting.IsDistAttenDisabled(num); | ||
| 76 | } | ||
| 77 | |||
| 78 | state.lighting.lut_d0.enable = regs.lighting.config1.disable_lut_d0 == 0; | ||
| 79 | state.lighting.lut_d0.abs_input = regs.lighting.abs_lut_input.disable_d0 == 0; | ||
| 80 | state.lighting.lut_d0.type = regs.lighting.lut_input.d0.Value(); | ||
| 81 | state.lighting.lut_d0.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.d0); | ||
| 82 | |||
| 83 | state.lighting.lut_d1.enable = regs.lighting.config1.disable_lut_d1 == 0; | ||
| 84 | state.lighting.lut_d1.abs_input = regs.lighting.abs_lut_input.disable_d1 == 0; | ||
| 85 | state.lighting.lut_d1.type = regs.lighting.lut_input.d1.Value(); | ||
| 86 | state.lighting.lut_d1.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.d1); | ||
| 87 | |||
| 88 | state.lighting.lut_fr.enable = regs.lighting.config1.disable_lut_fr == 0; | ||
| 89 | state.lighting.lut_fr.abs_input = regs.lighting.abs_lut_input.disable_fr == 0; | ||
| 90 | state.lighting.lut_fr.type = regs.lighting.lut_input.fr.Value(); | ||
| 91 | state.lighting.lut_fr.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.fr); | ||
| 92 | |||
| 93 | state.lighting.lut_rr.enable = regs.lighting.config1.disable_lut_rr == 0; | ||
| 94 | state.lighting.lut_rr.abs_input = regs.lighting.abs_lut_input.disable_rr == 0; | ||
| 95 | state.lighting.lut_rr.type = regs.lighting.lut_input.rr.Value(); | ||
| 96 | state.lighting.lut_rr.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rr); | ||
| 97 | |||
| 98 | state.lighting.lut_rg.enable = regs.lighting.config1.disable_lut_rg == 0; | ||
| 99 | state.lighting.lut_rg.abs_input = regs.lighting.abs_lut_input.disable_rg == 0; | ||
| 100 | state.lighting.lut_rg.type = regs.lighting.lut_input.rg.Value(); | ||
| 101 | state.lighting.lut_rg.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rg); | ||
| 102 | |||
| 103 | state.lighting.lut_rb.enable = regs.lighting.config1.disable_lut_rb == 0; | ||
| 104 | state.lighting.lut_rb.abs_input = regs.lighting.abs_lut_input.disable_rb == 0; | ||
| 105 | state.lighting.lut_rb.type = regs.lighting.lut_input.rb.Value(); | ||
| 106 | state.lighting.lut_rb.scale = regs.lighting.lut_scale.GetScale(regs.lighting.lut_scale.rb); | ||
| 107 | |||
| 108 | state.lighting.config = regs.lighting.config0.config; | ||
| 109 | state.lighting.fresnel_selector = regs.lighting.config0.fresnel_selector; | ||
| 110 | state.lighting.bump_mode = regs.lighting.config0.bump_mode; | ||
| 111 | state.lighting.bump_selector = regs.lighting.config0.bump_selector; | ||
| 112 | state.lighting.bump_renorm = regs.lighting.config0.disable_bump_renorm == 0; | ||
| 113 | state.lighting.clamp_highlights = regs.lighting.config0.clamp_highlights != 0; | ||
| 114 | |||
| 115 | return res; | ||
| 116 | } | ||
| 117 | |||
| 26 | /// Detects if a TEV stage is configured to be skipped (to avoid generating unnecessary code) | 118 | /// Detects if a TEV stage is configured to be skipped (to avoid generating unnecessary code) |
| 27 | static bool IsPassThroughTevStage(const TevStageConfig& stage) { | 119 | static bool IsPassThroughTevStage(const TevStageConfig& stage) { |
| 28 | return (stage.color_op == TevStageConfig::Operation::Replace && | 120 | return (stage.color_op == TevStageConfig::Operation::Replace && |
diff --git a/src/video_core/renderer_opengl/gl_shader_gen.h b/src/video_core/renderer_opengl/gl_shader_gen.h index bef3249cf..921d976a1 100644 --- a/src/video_core/renderer_opengl/gl_shader_gen.h +++ b/src/video_core/renderer_opengl/gl_shader_gen.h | |||
| @@ -4,12 +4,121 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <array> | ||
| 8 | #include <cstring> | ||
| 9 | #include <functional> | ||
| 7 | #include <string> | 10 | #include <string> |
| 8 | 11 | #include <type_traits> | |
| 9 | union PicaShaderConfig; | 12 | #include "video_core/regs.h" |
| 10 | 13 | ||
| 11 | namespace GLShader { | 14 | namespace GLShader { |
| 12 | 15 | ||
| 16 | enum Attributes { | ||
| 17 | ATTRIBUTE_POSITION, | ||
| 18 | ATTRIBUTE_COLOR, | ||
| 19 | ATTRIBUTE_TEXCOORD0, | ||
| 20 | ATTRIBUTE_TEXCOORD1, | ||
| 21 | ATTRIBUTE_TEXCOORD2, | ||
| 22 | ATTRIBUTE_TEXCOORD0_W, | ||
| 23 | ATTRIBUTE_NORMQUAT, | ||
| 24 | ATTRIBUTE_VIEW, | ||
| 25 | }; | ||
| 26 | |||
| 27 | /** | ||
| 28 | * This struct contains all state used to generate the GLSL shader program that emulates the current | ||
| 29 | * Pica register configuration. This struct is used as a cache key for generated GLSL shader | ||
| 30 | * programs. The functions in gl_shader_gen.cpp should retrieve state from this struct only, not by | ||
| 31 | * directly accessing Pica registers. This should reduce the risk of bugs in shader generation where | ||
| 32 | * Pica state is not being captured in the shader cache key, thereby resulting in (what should be) | ||
| 33 | * two separate shaders sharing the same key. | ||
| 34 | * | ||
| 35 | * We use a union because "implicitly-defined copy/move constructor for a union X copies the object | ||
| 36 | * representation of X." and "implicitly-defined copy assignment operator for a union X copies the | ||
| 37 | * object representation (3.9) of X." = Bytewise copy instead of memberwise copy. This is important | ||
| 38 | * because the padding bytes are included in the hash and comparison between objects. | ||
| 39 | */ | ||
| 40 | union PicaShaderConfig { | ||
| 41 | |||
| 42 | /// Construct a PicaShaderConfig with the given Pica register configuration. | ||
| 43 | static PicaShaderConfig BuildFromRegs(const Pica::Regs& regs); | ||
| 44 | |||
| 45 | bool TevStageUpdatesCombinerBufferColor(unsigned stage_index) const { | ||
| 46 | return (stage_index < 4) && (state.combiner_buffer_input & (1 << stage_index)); | ||
| 47 | } | ||
| 48 | |||
| 49 | bool TevStageUpdatesCombinerBufferAlpha(unsigned stage_index) const { | ||
| 50 | return (stage_index < 4) && ((state.combiner_buffer_input >> 4) & (1 << stage_index)); | ||
| 51 | } | ||
| 52 | |||
| 53 | bool operator==(const PicaShaderConfig& o) const { | ||
| 54 | return std::memcmp(&state, &o.state, sizeof(PicaShaderConfig::State)) == 0; | ||
| 55 | }; | ||
| 56 | |||
| 57 | // NOTE: MSVC15 (Update 2) doesn't think `delete`'d constructors and operators are TC. | ||
| 58 | // This makes BitField not TC when used in a union or struct so we have to resort | ||
| 59 | // to this ugly hack. | ||
| 60 | // Once that bug is fixed we can use Pica::Regs::TevStageConfig here. | ||
| 61 | // Doesn't include const_color because we don't sync it, see comment in BuildFromRegs() | ||
| 62 | struct TevStageConfigRaw { | ||
| 63 | u32 sources_raw; | ||
| 64 | u32 modifiers_raw; | ||
| 65 | u32 ops_raw; | ||
| 66 | u32 scales_raw; | ||
| 67 | explicit operator Pica::TexturingRegs::TevStageConfig() const noexcept { | ||
| 68 | Pica::TexturingRegs::TevStageConfig stage; | ||
| 69 | stage.sources_raw = sources_raw; | ||
| 70 | stage.modifiers_raw = modifiers_raw; | ||
| 71 | stage.ops_raw = ops_raw; | ||
| 72 | stage.const_color = 0; | ||
| 73 | stage.scales_raw = scales_raw; | ||
| 74 | return stage; | ||
| 75 | } | ||
| 76 | }; | ||
| 77 | |||
| 78 | struct State { | ||
| 79 | Pica::FramebufferRegs::CompareFunc alpha_test_func; | ||
| 80 | Pica::RasterizerRegs::ScissorMode scissor_test_mode; | ||
| 81 | Pica::TexturingRegs::TextureConfig::TextureType texture0_type; | ||
| 82 | std::array<TevStageConfigRaw, 6> tev_stages; | ||
| 83 | u8 combiner_buffer_input; | ||
| 84 | |||
| 85 | Pica::RasterizerRegs::DepthBuffering depthmap_enable; | ||
| 86 | Pica::TexturingRegs::FogMode fog_mode; | ||
| 87 | bool fog_flip; | ||
| 88 | |||
| 89 | struct { | ||
| 90 | struct { | ||
| 91 | unsigned num; | ||
| 92 | bool directional; | ||
| 93 | bool two_sided_diffuse; | ||
| 94 | bool dist_atten_enable; | ||
| 95 | } light[8]; | ||
| 96 | |||
| 97 | bool enable; | ||
| 98 | unsigned src_num; | ||
| 99 | Pica::LightingRegs::LightingBumpMode bump_mode; | ||
| 100 | unsigned bump_selector; | ||
| 101 | bool bump_renorm; | ||
| 102 | bool clamp_highlights; | ||
| 103 | |||
| 104 | Pica::LightingRegs::LightingConfig config; | ||
| 105 | Pica::LightingRegs::LightingFresnelSelector fresnel_selector; | ||
| 106 | |||
| 107 | struct { | ||
| 108 | bool enable; | ||
| 109 | bool abs_input; | ||
| 110 | Pica::LightingRegs::LightingLutInput type; | ||
| 111 | float scale; | ||
| 112 | } lut_d0, lut_d1, lut_fr, lut_rr, lut_rg, lut_rb; | ||
| 113 | } lighting; | ||
| 114 | |||
| 115 | } state; | ||
| 116 | }; | ||
| 117 | #if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER) | ||
| 118 | static_assert(std::is_trivially_copyable<PicaShaderConfig::State>::value, | ||
| 119 | "PicaShaderConfig::State must be trivially copyable"); | ||
| 120 | #endif | ||
| 121 | |||
| 13 | /** | 122 | /** |
| 14 | * Generates the GLSL vertex shader program source code for the current Pica state | 123 | * Generates the GLSL vertex shader program source code for the current Pica state |
| 15 | * @returns String of the shader source code | 124 | * @returns String of the shader source code |
| @@ -25,3 +134,12 @@ std::string GenerateVertexShader(); | |||
| 25 | std::string GenerateFragmentShader(const PicaShaderConfig& config); | 134 | std::string GenerateFragmentShader(const PicaShaderConfig& config); |
| 26 | 135 | ||
| 27 | } // namespace GLShader | 136 | } // namespace GLShader |
| 137 | |||
| 138 | namespace std { | ||
| 139 | template <> | ||
| 140 | struct hash<GLShader::PicaShaderConfig> { | ||
| 141 | size_t operator()(const GLShader::PicaShaderConfig& k) const { | ||
| 142 | return Common::ComputeHash64(&k.state, sizeof(GLShader::PicaShaderConfig::State)); | ||
| 143 | } | ||
| 144 | }; | ||
| 145 | } // namespace std | ||
diff --git a/src/video_core/renderer_opengl/gl_shader_util.h b/src/video_core/renderer_opengl/gl_shader_util.h index f59912f79..c66e8acd3 100644 --- a/src/video_core/renderer_opengl/gl_shader_util.h +++ b/src/video_core/renderer_opengl/gl_shader_util.h | |||
| @@ -8,17 +8,6 @@ | |||
| 8 | 8 | ||
| 9 | namespace GLShader { | 9 | namespace GLShader { |
| 10 | 10 | ||
| 11 | enum Attributes { | ||
| 12 | ATTRIBUTE_POSITION, | ||
| 13 | ATTRIBUTE_COLOR, | ||
| 14 | ATTRIBUTE_TEXCOORD0, | ||
| 15 | ATTRIBUTE_TEXCOORD1, | ||
| 16 | ATTRIBUTE_TEXCOORD2, | ||
| 17 | ATTRIBUTE_TEXCOORD0_W, | ||
| 18 | ATTRIBUTE_NORMQUAT, | ||
| 19 | ATTRIBUTE_VIEW, | ||
| 20 | }; | ||
| 21 | |||
| 22 | /** | 11 | /** |
| 23 | * Utility function to create and compile an OpenGL GLSL shader program (vertex + fragment shader) | 12 | * Utility function to create and compile an OpenGL GLSL shader program (vertex + fragment shader) |
| 24 | * @param vertex_shader String of the GLSL vertex shader program | 13 | * @param vertex_shader String of the GLSL vertex shader program |