From 602f57da387da2bf702aa149124c14861b5d27d8 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Sat, 28 Jan 2017 13:58:51 -0800 Subject: [PATCH 1/6] VideoCore: Use union to index into Regs struct Also remove some unused members. --- src/video_core/command_processor.cpp | 12 ++++-- src/video_core/regs.h | 62 +++++++++------------------- 2 files changed, 28 insertions(+), 46 deletions(-) diff --git a/src/video_core/command_processor.cpp b/src/video_core/command_processor.cpp index 91c0ca4e6..ea541200b 100644 --- a/src/video_core/command_processor.cpp +++ b/src/video_core/command_processor.cpp @@ -49,19 +49,23 @@ MICROPROFILE_DEFINE(GPU_Drawing, "GPU", "Drawing", MP_RGB(50, 50, 240)); static void WritePicaReg(u32 id, u32 value, u32 mask) { auto& regs = g_state.regs; - if (id >= regs.NumIds()) + if (id >= Regs::NUM_REGS) { + LOG_ERROR(HW_GPU, + "Commandlist tried to write to invalid register 0x%03X (value: %08X, mask: %X)", + id, value, mask); return; + } // TODO: Figure out how register masking acts on e.g. vs.uniform_setup.set_value - u32 old_value = regs[id]; + u32 old_value = regs.reg_array[id]; const u32 write_mask = expand_bits_to_bytes[mask]; - regs[id] = (old_value & ~write_mask) | (value & write_mask); + regs.reg_array[id] = (old_value & ~write_mask) | (value & write_mask); // Double check for is_pica_tracing to avoid call overhead if (DebugUtils::IsPicaTracing()) { - DebugUtils::OnPicaRegWrite({(u16)id, (u16)mask, regs[id]}); + DebugUtils::OnPicaRegWrite({(u16)id, (u16)mask, regs.reg_array[id]}); } if (g_debug_context) diff --git a/src/video_core/regs.h b/src/video_core/regs.h index f25edde27..e38ab4333 100644 --- a/src/video_core/regs.h +++ b/src/video_core/regs.h @@ -45,46 +45,31 @@ namespace Pica { #endif // _MSC_VER struct Regs { - INSERT_PADDING_WORDS(0x10); - u32 trigger_irq; - INSERT_PADDING_WORDS(0x2f); - RasterizerRegs rasterizer; - TexturingRegs texturing; - FramebufferRegs framebuffer; - LightingRegs lighting; - PipelineRegs pipeline; - ShaderRegs gs; - ShaderRegs vs; - INSERT_PADDING_WORDS(0x20); + static constexpr size_t NUM_REGS = 0x300; + + union { + struct { + INSERT_PADDING_WORDS(0x10); + u32 trigger_irq; + INSERT_PADDING_WORDS(0x2f); + RasterizerRegs rasterizer; + TexturingRegs texturing; + FramebufferRegs framebuffer; + LightingRegs lighting; + PipelineRegs pipeline; + ShaderRegs gs; + ShaderRegs vs; + INSERT_PADDING_WORDS(0x20); + }; + std::array reg_array; + }; // Map register indices to names readable by humans - // Used for debugging purposes, so performance is not an issue here static std::string GetCommandName(int index); - - static constexpr size_t NumIds() { - return sizeof(Regs) / sizeof(u32); - } - - const u32& operator[](int index) const { - const u32* content = reinterpret_cast(this); - return content[index]; - } - - u32& operator[](int index) { - u32* content = reinterpret_cast(this); - return content[index]; - } - -private: - /* - * Most physical addresses which Pica registers refer to are 8-byte aligned. - * This function should be used to get the address from a raw register value. - */ - static inline u32 DecodeAddressRegister(u32 register_value) { - return register_value * 8; - } }; +static_assert(sizeof(Regs) == Regs::NUM_REGS * sizeof(u32), "Regs struct has wrong size"); + // TODO: MSVC does not support using offsetof() on non-static data members even though this // is technically allowed since C++11. This macro should be enabled once MSVC adds // support for that. @@ -154,11 +139,4 @@ ASSERT_REG_POSITION(vs, 0x2b0); #undef ASSERT_REG_POSITION #endif // !defined(_MSC_VER) -// The total number of registers is chosen arbitrarily, but let's make sure it's not some odd value -// anyway. -static_assert(sizeof(Regs) <= 0x300 * sizeof(u32), - "Register set structure larger than it should be"); -static_assert(sizeof(Regs) >= 0x300 * sizeof(u32), - "Register set structure smaller than it should be"); - } // namespace Pica From f241bb72f5b6b0b213053e000051bf3b7e6b4bb0 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Sat, 28 Jan 2017 14:48:13 -0800 Subject: [PATCH 2/6] Pica/Regs: Use binary search to look up reg names This gets rid of the static unordered_map. Also changes the return type const char*, avoiding unnecessary allocations (the result was only used by calling .c_str() on it.) --- .../debugger/graphics/graphics_cmdlists.cpp | 2 +- src/video_core/regs.cpp | 21 +++++++------------ src/video_core/regs.h | 4 ++-- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/citra_qt/debugger/graphics/graphics_cmdlists.cpp b/src/citra_qt/debugger/graphics/graphics_cmdlists.cpp index 536548f36..c68fe753b 100644 --- a/src/citra_qt/debugger/graphics/graphics_cmdlists.cpp +++ b/src/citra_qt/debugger/graphics/graphics_cmdlists.cpp @@ -72,7 +72,7 @@ QVariant GPUCommandListModel::data(const QModelIndex& index, int role) const { if (role == Qt::DisplayRole) { switch (index.column()) { case 0: - return QString::fromLatin1(Pica::Regs::GetCommandName(write.cmd_id).c_str()); + return QString::fromLatin1(Pica::Regs::GetRegisterName(write.cmd_id)); case 1: return QString("%1").arg(write.cmd_id, 3, 16, QLatin1Char('0')); case 2: diff --git a/src/video_core/regs.cpp b/src/video_core/regs.cpp index f47e9e763..2699e710a 100644 --- a/src/video_core/regs.cpp +++ b/src/video_core/regs.cpp @@ -2,8 +2,8 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include -#include #include #include "common/common_types.h" @@ -474,19 +474,14 @@ static const std::pair register_names[] = { {0x2DD, "GPUREG_VSH_OPDESCS_DATA7"}, }; -std::string Regs::GetCommandName(int index) { - static std::unordered_map map; - - if (map.empty()) { - map.insert(std::begin(register_names), std::end(register_names)); - } - - // Return empty string if no match is found - auto it = map.find(index); - if (it != map.end()) { - return it->second; +const char* Regs::GetRegisterName(u16 index) { + auto found = std::lower_bound(std::begin(register_names), std::end(register_names), index, + [](auto p, auto i) { return p.first < i; }); + if (found->first == index) { + return found->second; } else { - return std::string(); + // Return empty string if no match is found + return ""; } } diff --git a/src/video_core/regs.h b/src/video_core/regs.h index e38ab4333..86826088b 100644 --- a/src/video_core/regs.h +++ b/src/video_core/regs.h @@ -64,8 +64,8 @@ struct Regs { std::array reg_array; }; - // Map register indices to names readable by humans - static std::string GetCommandName(int index); + /// Map register indices to names readable by humans + static const char* GetRegisterName(u16 index); }; static_assert(sizeof(Regs) == Regs::NUM_REGS * sizeof(u32), "Regs struct has wrong size"); From 60fc0b086f9ec8d7b955aa9b9c0094dc42ab2fb9 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Sat, 28 Jan 2017 15:12:09 -0800 Subject: [PATCH 3/6] VideoCore: Split regs.h inclusions --- .../debugger/graphics/graphics_surface.cpp | 3 +- src/video_core/clipper.cpp | 1 - src/video_core/command_processor.cpp | 2 ++ src/video_core/debug_utils/debug_utils.cpp | 4 ++- src/video_core/debug_utils/debug_utils.h | 4 ++- src/video_core/pica.cpp | 2 +- src/video_core/rasterizer.cpp | 4 ++- .../renderer_opengl/gl_rasterizer.cpp | 4 ++- .../renderer_opengl/gl_rasterizer.h | 5 +++- .../renderer_opengl/gl_rasterizer_cache.h | 3 +- .../renderer_opengl/gl_shader_gen.cpp | 30 +++++++++++-------- src/video_core/renderer_opengl/pica_to_gl.h | 4 ++- src/video_core/shader/shader.cpp | 3 +- src/video_core/shader/shader.h | 3 +- 14 files changed, 47 insertions(+), 25 deletions(-) diff --git a/src/citra_qt/debugger/graphics/graphics_surface.cpp b/src/citra_qt/debugger/graphics/graphics_surface.cpp index f83c1f96c..47d9924e1 100644 --- a/src/citra_qt/debugger/graphics/graphics_surface.cpp +++ b/src/citra_qt/debugger/graphics/graphics_surface.cpp @@ -17,7 +17,8 @@ #include "core/hw/gpu.h" #include "core/memory.h" #include "video_core/pica_state.h" -#include "video_core/regs.h" +#include "video_core/regs_framebuffer.h" +#include "video_core/regs_texturing.h" #include "video_core/texture/texture_decode.h" #include "video_core/utils.h" diff --git a/src/video_core/clipper.cpp b/src/video_core/clipper.cpp index 0f71bbd06..1e8e751ba 100644 --- a/src/video_core/clipper.cpp +++ b/src/video_core/clipper.cpp @@ -15,7 +15,6 @@ #include "video_core/pica_state.h" #include "video_core/pica_types.h" #include "video_core/rasterizer.h" -#include "video_core/regs.h" #include "video_core/shader/shader.h" using Pica::Rasterizer::Vertex; diff --git a/src/video_core/command_processor.cpp b/src/video_core/command_processor.cpp index ea541200b..2e32ff905 100644 --- a/src/video_core/command_processor.cpp +++ b/src/video_core/command_processor.cpp @@ -21,6 +21,8 @@ #include "video_core/primitive_assembly.h" #include "video_core/rasterizer_interface.h" #include "video_core/regs.h" +#include "video_core/regs_pipeline.h" +#include "video_core/regs_texturing.h" #include "video_core/renderer_base.h" #include "video_core/shader/shader.h" #include "video_core/vertex_loader.h" diff --git a/src/video_core/debug_utils/debug_utils.cpp b/src/video_core/debug_utils/debug_utils.cpp index e164e83a1..47dbc8cc8 100644 --- a/src/video_core/debug_utils/debug_utils.cpp +++ b/src/video_core/debug_utils/debug_utils.cpp @@ -32,7 +32,9 @@ #include "video_core/pica_state.h" #include "video_core/pica_types.h" #include "video_core/rasterizer_interface.h" -#include "video_core/regs.h" +#include "video_core/regs_rasterizer.h" +#include "video_core/regs_shader.h" +#include "video_core/regs_texturing.h" #include "video_core/renderer_base.h" #include "video_core/shader/shader.h" #include "video_core/texture/texture_decode.h" diff --git a/src/video_core/debug_utils/debug_utils.h b/src/video_core/debug_utils/debug_utils.h index fd94bdbb8..89e418e27 100644 --- a/src/video_core/debug_utils/debug_utils.h +++ b/src/video_core/debug_utils/debug_utils.h @@ -17,7 +17,9 @@ #include #include "common/common_types.h" #include "common/vector_math.h" -#include "video_core/regs.h" +#include "video_core/regs_rasterizer.h" +#include "video_core/regs_shader.h" +#include "video_core/regs_texturing.h" namespace CiTrace { class Recorder; diff --git a/src/video_core/pica.cpp b/src/video_core/pica.cpp index 13f0a4ab9..b95148a6a 100644 --- a/src/video_core/pica.cpp +++ b/src/video_core/pica.cpp @@ -5,7 +5,7 @@ #include #include "video_core/pica.h" #include "video_core/pica_state.h" -#include "video_core/regs.h" +#include "video_core/regs_pipeline.h" namespace Pica { diff --git a/src/video_core/rasterizer.cpp b/src/video_core/rasterizer.cpp index ca09c9d0e..83a08ebd7 100644 --- a/src/video_core/rasterizer.cpp +++ b/src/video_core/rasterizer.cpp @@ -19,7 +19,9 @@ #include "video_core/pica_state.h" #include "video_core/pica_types.h" #include "video_core/rasterizer.h" -#include "video_core/regs.h" +#include "video_core/regs_framebuffer.h" +#include "video_core/regs_rasterizer.h" +#include "video_core/regs_texturing.h" #include "video_core/shader/shader.h" #include "video_core/texture/texture_decode.h" #include "video_core/utils.h" diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index 75736c99f..ca818a672 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp @@ -15,7 +15,9 @@ #include "common/vector_math.h" #include "core/hw/gpu.h" #include "video_core/pica_state.h" -#include "video_core/regs.h" +#include "video_core/regs_framebuffer.h" +#include "video_core/regs_rasterizer.h" +#include "video_core/regs_texturing.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_util.h" diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h index bfee911b6..ecf737438 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.h +++ b/src/video_core/renderer_opengl/gl_rasterizer.h @@ -19,7 +19,10 @@ #include "video_core/pica_state.h" #include "video_core/pica_types.h" #include "video_core/rasterizer_interface.h" -#include "video_core/regs.h" +#include "video_core/regs_framebuffer.h" +#include "video_core/regs_lighting.h" +#include "video_core/regs_rasterizer.h" +#include "video_core/regs_texturing.h" #include "video_core/renderer_opengl/gl_rasterizer_cache.h" #include "video_core/renderer_opengl/gl_resource_manager.h" #include "video_core/renderer_opengl/gl_state.h" diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.h b/src/video_core/renderer_opengl/gl_rasterizer_cache.h index 4072ed49e..aea20c693 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer_cache.h +++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.h @@ -21,7 +21,8 @@ #include "common/common_funcs.h" #include "common/common_types.h" #include "core/hw/gpu.h" -#include "video_core/regs.h" +#include "video_core/regs_framebuffer.h" +#include "video_core/regs_texturing.h" #include "video_core/renderer_opengl/gl_resource_manager.h" namespace MathUtil { diff --git a/src/video_core/renderer_opengl/gl_shader_gen.cpp b/src/video_core/renderer_opengl/gl_shader_gen.cpp index 3ea25f302..7abdeba05 100644 --- a/src/video_core/renderer_opengl/gl_shader_gen.cpp +++ b/src/video_core/renderer_opengl/gl_shader_gen.cpp @@ -7,15 +7,19 @@ #include "common/assert.h" #include "common/bit_field.h" #include "common/logging/log.h" -#include "video_core/regs.h" +#include "video_core/regs_framebuffer.h" +#include "video_core/regs_lighting.h" +#include "video_core/regs_rasterizer.h" +#include "video_core/regs_texturing.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_util.h" -using Pica::Regs; -using Pica::RasterizerRegs; +using Pica::FramebufferRegs; using Pica::LightingRegs; -using TevStageConfig = Pica::TexturingRegs::TevStageConfig; +using Pica::RasterizerRegs; +using Pica::TexturingRegs; +using TevStageConfig = TexturingRegs::TevStageConfig; namespace GLShader { @@ -48,10 +52,10 @@ static void AppendSource(std::string& out, const PicaShaderConfig& config, case Source::Texture0: // Only unit 0 respects the texturing type (according to 3DBrew) switch (state.texture0_type) { - case Pica::TexturingRegs::TextureConfig::Texture2D: + case TexturingRegs::TextureConfig::Texture2D: out += "texture(tex[0], texcoord[0])"; break; - case Pica::TexturingRegs::TextureConfig::Projection2D: + case TexturingRegs::TextureConfig::Projection2D: out += "textureProj(tex[0], vec3(texcoord[0], texcoord0_w))"; break; default: @@ -278,8 +282,8 @@ static void AppendAlphaCombiner(std::string& out, TevStageConfig::Operation oper } /// Writes the if-statement condition used to evaluate alpha testing -static void AppendAlphaTestCondition(std::string& out, Pica::FramebufferRegs::CompareFunc func) { - using CompareFunc = Pica::FramebufferRegs::CompareFunc; +static void AppendAlphaTestCondition(std::string& out, FramebufferRegs::CompareFunc func) { + using CompareFunc = FramebufferRegs::CompareFunc; switch (func) { case CompareFunc::Never: out += "true"; @@ -309,7 +313,7 @@ static void AppendAlphaTestCondition(std::string& out, Pica::FramebufferRegs::Co /// Writes the code to emulate the specified TEV stage static void WriteTevStage(std::string& out, const PicaShaderConfig& config, unsigned index) { const auto stage = - static_cast(config.state.tev_stages[index]); + static_cast(config.state.tev_stages[index]); if (!IsPassThroughTevStage(stage)) { std::string index_name = std::to_string(index); @@ -642,7 +646,7 @@ vec4 secondary_fragment_color = vec4(0.0); )"; // Do not do any sort of processing if it's obvious we're not going to pass the alpha test - if (state.alpha_test_func == Pica::FramebufferRegs::CompareFunc::Never) { + if (state.alpha_test_func == FramebufferRegs::CompareFunc::Never) { out += "discard; }"; return out; } @@ -661,7 +665,7 @@ vec4 secondary_fragment_color = vec4(0.0); out += "float z_over_w = 1.0 - gl_FragCoord.z * 2.0;\n"; out += "float depth = z_over_w * depth_scale + depth_offset;\n"; - if (state.depthmap_enable == Pica::RasterizerRegs::DepthBuffering::WBuffering) { + if (state.depthmap_enable == RasterizerRegs::DepthBuffering::WBuffering) { out += "depth /= gl_FragCoord.w;\n"; } @@ -675,14 +679,14 @@ vec4 secondary_fragment_color = vec4(0.0); for (size_t index = 0; index < state.tev_stages.size(); ++index) WriteTevStage(out, config, (unsigned)index); - if (state.alpha_test_func != Pica::FramebufferRegs::CompareFunc::Always) { + if (state.alpha_test_func != FramebufferRegs::CompareFunc::Always) { out += "if ("; AppendAlphaTestCondition(out, state.alpha_test_func); out += ") discard;\n"; } // Append fog combiner - if (state.fog_mode == Pica::TexturingRegs::FogMode::Fog) { + if (state.fog_mode == TexturingRegs::FogMode::Fog) { // Get index into fog LUT if (state.fog_flip) { out += "float fog_index = (1.0 - depth) * 128.0;\n"; diff --git a/src/video_core/renderer_opengl/pica_to_gl.h b/src/video_core/renderer_opengl/pica_to_gl.h index 4b98dafc4..93d7b0b71 100644 --- a/src/video_core/renderer_opengl/pica_to_gl.h +++ b/src/video_core/renderer_opengl/pica_to_gl.h @@ -12,7 +12,9 @@ #include "common/common_funcs.h" #include "common/common_types.h" #include "common/logging/log.h" -#include "video_core/regs.h" +#include "video_core/regs_framebuffer.h" +#include "video_core/regs_lighting.h" +#include "video_core/regs_texturing.h" using GLvec2 = std::array; using GLvec3 = std::array; diff --git a/src/video_core/shader/shader.cpp b/src/video_core/shader/shader.cpp index c860375a1..b42fa3cb4 100644 --- a/src/video_core/shader/shader.cpp +++ b/src/video_core/shader/shader.cpp @@ -8,7 +8,8 @@ #include "common/logging/log.h" #include "common/microprofile.h" #include "video_core/pica_state.h" -#include "video_core/regs.h" +#include "video_core/regs_rasterizer.h" +#include "video_core/regs_shader.h" #include "video_core/shader/shader.h" #include "video_core/shader/shader_interpreter.h" #ifdef ARCHITECTURE_x86_64 diff --git a/src/video_core/shader/shader.h b/src/video_core/shader/shader.h index d52682479..38ea717ab 100644 --- a/src/video_core/shader/shader.h +++ b/src/video_core/shader/shader.h @@ -13,7 +13,8 @@ #include "common/common_types.h" #include "common/vector_math.h" #include "video_core/pica_types.h" -#include "video_core/regs.h" +#include "video_core/regs_rasterizer.h" +#include "video_core/regs_shader.h" using nihstro::RegisterType; using nihstro::SourceRegister; From af65e1c0a058bf479f3eec9233f2f60153597bb6 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Sun, 29 Jan 2017 21:23:35 -0800 Subject: [PATCH 4/6] OpenGL: Remove unused duplicate of IsPassThroughTevStage This copy was left behind when the shader generation code was moved to a separate file. --- src/video_core/renderer_opengl/gl_rasterizer.cpp | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index ca818a672..4b1948a71 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp @@ -28,18 +28,6 @@ MICROPROFILE_DEFINE(OpenGL_Drawing, "OpenGL", "Drawing", MP_RGB(128, 128, 192)); MICROPROFILE_DEFINE(OpenGL_Blits, "OpenGL", "Blits", MP_RGB(100, 100, 255)); MICROPROFILE_DEFINE(OpenGL_CacheManagement, "OpenGL", "Cache Mgmt", MP_RGB(100, 255, 100)); -static bool IsPassThroughTevStage(const Pica::TexturingRegs::TevStageConfig& stage) { - using TevStageConfig = Pica::TexturingRegs::TevStageConfig; - - 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); -} - RasterizerOpenGL::RasterizerOpenGL() : shader_dirty(true) { // Create sampler objects for (size_t i = 0; i < texture_samplers.size(); ++i) { From bfb15313529e41bc80fb57849ec4b53337e655be Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Wed, 8 Feb 2017 23:31:46 -0800 Subject: [PATCH 5/6] VideoCore: Force enum sizes to u32 in LightingRegs All enums that are used with BitField must have their type forced to u32 to ensure correctness. --- src/video_core/regs_lighting.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/video_core/regs_lighting.h b/src/video_core/regs_lighting.h index 548a6c4d5..6793405d9 100644 --- a/src/video_core/regs_lighting.h +++ b/src/video_core/regs_lighting.h @@ -34,7 +34,7 @@ struct LightingRegs { * configurations that require more LUTs, more cycles are required on HW to perform lighting * computations. */ - enum class LightingConfig { + enum class LightingConfig : u32 { Config0 = 0, ///< Reflect Red, Distribution 0, Spotlight Config1 = 1, ///< Reflect Red, Fresnel, Spotlight Config2 = 2, ///< Reflect Red, Distribution 0/1 @@ -48,7 +48,7 @@ struct LightingRegs { }; /// Selects which lighting components are affected by fresnel - enum class LightingFresnelSelector { + enum class LightingFresnelSelector : u32 { None = 0, ///< Fresnel is disabled PrimaryAlpha = 1, ///< Primary (diffuse) lighting alpha is affected by fresnel SecondaryAlpha = 2, ///< Secondary (specular) lighting alpha is affected by fresnel @@ -58,7 +58,7 @@ struct LightingRegs { }; /// Factor used to scale the output of a lighting LUT - enum class LightingScale { + enum class LightingScale : u32 { Scale1 = 0, ///< Scale is 1x Scale2 = 1, ///< Scale is 2x Scale4 = 2, ///< Scale is 4x @@ -68,7 +68,7 @@ struct LightingRegs { Scale1_2 = 7, ///< Scale is 0.5x }; - enum class LightingLutInput { + enum class LightingLutInput : u32 { NH = 0, // Cosine of the angle between the normal and half-angle vectors VH = 1, // Cosine of the angle between the view and half-angle vectors NV = 2, // Cosine of the angle between the normal and the view vector From 553e672777433c99afb0376b963aa5955e4c076f Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Wed, 8 Feb 2017 23:46:00 -0800 Subject: [PATCH 6/6] VideoCore: Split u64 Pica reg unions into 2 separate u32 unions This eliminates UB when aliasing it with the array of u32 regs, and is compatible with non-LE architectures. --- src/video_core/regs_pipeline.h | 76 ++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/src/video_core/regs_pipeline.h b/src/video_core/regs_pipeline.h index 5844a66ee..0a4ec6e1e 100644 --- a/src/video_core/regs_pipeline.h +++ b/src/video_core/regs_pipeline.h @@ -14,7 +14,7 @@ namespace Pica { struct PipelineRegs { - enum class VertexAttributeFormat : u64 { + enum class VertexAttributeFormat : u32 { BYTE = 0, UBYTE = 1, SHORT = 2, @@ -31,34 +31,37 @@ struct PipelineRegs { // Descriptor for internal vertex attributes union { BitField<0, 2, VertexAttributeFormat> format0; // size of one element - BitField<2, 2, u64> size0; // number of elements minus 1 + BitField<2, 2, u32> size0; // number of elements minus 1 BitField<4, 2, VertexAttributeFormat> format1; - BitField<6, 2, u64> size1; + BitField<6, 2, u32> size1; BitField<8, 2, VertexAttributeFormat> format2; - BitField<10, 2, u64> size2; + BitField<10, 2, u32> size2; BitField<12, 2, VertexAttributeFormat> format3; - BitField<14, 2, u64> size3; + BitField<14, 2, u32> size3; BitField<16, 2, VertexAttributeFormat> format4; - BitField<18, 2, u64> size4; + BitField<18, 2, u32> size4; BitField<20, 2, VertexAttributeFormat> format5; - BitField<22, 2, u64> size5; + BitField<22, 2, u32> size5; BitField<24, 2, VertexAttributeFormat> format6; - BitField<26, 2, u64> size6; + BitField<26, 2, u32> size6; BitField<28, 2, VertexAttributeFormat> format7; - BitField<30, 2, u64> size7; - BitField<32, 2, VertexAttributeFormat> format8; - BitField<34, 2, u64> size8; - BitField<36, 2, VertexAttributeFormat> format9; - BitField<38, 2, u64> size9; - BitField<40, 2, VertexAttributeFormat> format10; - BitField<42, 2, u64> size10; - BitField<44, 2, VertexAttributeFormat> format11; - BitField<46, 2, u64> size11; + BitField<30, 2, u32> size7; + }; - BitField<48, 12, u64> attribute_mask; + union { + BitField<0, 2, VertexAttributeFormat> format8; + BitField<2, 2, u32> size8; + BitField<4, 2, VertexAttributeFormat> format9; + BitField<6, 2, u32> size9; + BitField<8, 2, VertexAttributeFormat> format10; + BitField<10, 2, u32> size10; + BitField<12, 2, VertexAttributeFormat> format11; + BitField<14, 2, u32> size11; + + BitField<16, 12, u32> attribute_mask; // number of total attributes minus 1 - BitField<60, 4, u64> max_attribute_index; + BitField<28, 4, u32> max_attribute_index; }; inline VertexAttributeFormat GetFormat(int n) const { @@ -69,7 +72,7 @@ struct PipelineRegs { } inline int GetNumElements(int n) const { - u64 sizes[] = {size0, size1, size2, size3, size4, size5, + u32 sizes[] = {size0, size1, size2, size3, size4, size5, size6, size7, size8, size9, size10, size11}; return (int)sizes[n] + 1; } @@ -99,27 +102,30 @@ struct PipelineRegs { u32 data_offset; union { - BitField<0, 4, u64> comp0; - BitField<4, 4, u64> comp1; - BitField<8, 4, u64> comp2; - BitField<12, 4, u64> comp3; - BitField<16, 4, u64> comp4; - BitField<20, 4, u64> comp5; - BitField<24, 4, u64> comp6; - BitField<28, 4, u64> comp7; - BitField<32, 4, u64> comp8; - BitField<36, 4, u64> comp9; - BitField<40, 4, u64> comp10; - BitField<44, 4, u64> comp11; + BitField<0, 4, u32> comp0; + BitField<4, 4, u32> comp1; + BitField<8, 4, u32> comp2; + BitField<12, 4, u32> comp3; + BitField<16, 4, u32> comp4; + BitField<20, 4, u32> comp5; + BitField<24, 4, u32> comp6; + BitField<28, 4, u32> comp7; + }; + + union { + BitField<0, 4, u32> comp8; + BitField<4, 4, u32> comp9; + BitField<8, 4, u32> comp10; + BitField<12, 4, u32> comp11; // bytes for a single vertex in this loader - BitField<48, 8, u64> byte_count; + BitField<16, 8, u32> byte_count; - BitField<60, 4, u64> component_count; + BitField<28, 4, u32> component_count; }; inline int GetComponent(int n) const { - u64 components[] = {comp0, comp1, comp2, comp3, comp4, comp5, + u32 components[] = {comp0, comp1, comp2, comp3, comp4, comp5, comp6, comp7, comp8, comp9, comp10, comp11}; return (int)components[n]; }