From 6c27585cf9f662ca08ae49560eab1b171ac214dc Mon Sep 17 00:00:00 2001 From: Luke Street Date: Tue, 15 Apr 2025 09:19:04 -0600 Subject: [PATCH] Simplify lighting in build_uniform --- lib/gfx/gx.cpp | 23 +++++------------------ lib/gfx/gx.hpp | 1 + lib/gfx/gx_shader.cpp | 7 +++---- 3 files changed, 9 insertions(+), 22 deletions(-) diff --git a/lib/gfx/gx.cpp b/lib/gfx/gx.cpp index 8fc85a9..89e7657 100644 --- a/lib/gfx/gx.cpp +++ b/lib/gfx/gx.cpp @@ -180,8 +180,8 @@ static inline wgpu::PrimitiveState to_primitive_state(GXPrimitive gx_prim, GXCul } return { .topology = primitive, - .stripIndexFormat = gx_prim == GX_TRIANGLESTRIP || gx_prim == GX_LINESTRIP ? wgpu::IndexFormat::Uint16 - : wgpu::IndexFormat::Undefined, + .stripIndexFormat = primitive == wgpu::PrimitiveTopology::TriangleStrip ? wgpu::IndexFormat::Uint16 + : wgpu::IndexFormat::Undefined, .frontFace = wgpu::FrontFace::CW, .cullMode = cullMode, }; @@ -328,19 +328,7 @@ Range build_uniform(const ShaderInfo& info) noexcept { } buf.append(g_gxState.colorRegs[i]); } - bool lightingEnabled = false; - for (int i = 0; i < info.sampledColorChannels.size(); ++i) { - if (!info.sampledColorChannels.test(i)) { - continue; - } - const auto& ccc = g_gxState.colorChannelConfig[i * 2]; - const auto& ccca = g_gxState.colorChannelConfig[i * 2 + 1]; - if (ccc.lightingEnabled || ccca.lightingEnabled) { - lightingEnabled = true; - break; - } - } - if (lightingEnabled) { + if (info.lightingEnabled) { // Lights static_assert(sizeof(g_gxState.lights) == 80 * GX::MaxLights); buf.append(g_gxState.lights); @@ -380,12 +368,11 @@ Range build_uniform(const ShaderInfo& info) noexcept { if (!info.usesTexMtx.test(i)) { continue; } - const auto& state = g_gxState; switch (info.texMtxTypes[i]) { DEFAULT_FATAL("unhandled tex mtx type {}", underlying(info.texMtxTypes[i])); case GX_TG_MTX2x4: - if (std::holds_alternative>(state.texMtxs[i])) { - buf.append(std::get>(state.texMtxs[i])); + if (std::holds_alternative>(g_gxState.texMtxs[i])) { + buf.append(std::get>(g_gxState.texMtxs[i])); } else UNLIKELY FATAL("expected 2x4 mtx in idx {}", i); break; diff --git a/lib/gfx/gx.hpp b/lib/gfx/gx.hpp index 7213e11..6a18100 100644 --- a/lib/gfx/gx.hpp +++ b/lib/gfx/gx.hpp @@ -420,6 +420,7 @@ struct ShaderInfo { std::array texMtxTypes{}; u32 uniformSize = 0; bool usesFog : 1 = false; + bool lightingEnabled : 1 = false; }; struct BindGroupRanges { std::array vaRanges{}; diff --git a/lib/gfx/gx_shader.cpp b/lib/gfx/gx_shader.cpp index ad87a00..d947cc2 100644 --- a/lib/gfx/gx_shader.cpp +++ b/lib/gfx/gx_shader.cpp @@ -581,19 +581,18 @@ ShaderInfo build_shader_info(const ShaderConfig& config) noexcept { } } info.uniformSize += info.loadsTevReg.count() * sizeof(Vec4); - bool lightingEnabled = false; for (int i = 0; i < info.sampledColorChannels.size(); ++i) { if (info.sampledColorChannels.test(i)) { const auto& cc = config.colorChannels[i * 2]; const auto& cca = config.colorChannels[i * 2 + 1]; if (cc.lightingEnabled || cca.lightingEnabled) { - lightingEnabled = true; + info.lightingEnabled = true; } } } - if (lightingEnabled) { + if (info.lightingEnabled) { // Lights + light state for all channels - info.uniformSize += sizeof(Vec4) + sizeof(Light) * GX::MaxLights; + info.uniformSize += 16 + sizeof(Light) * GX::MaxLights; } for (int i = 0; i < info.sampledColorChannels.size(); ++i) { if (info.sampledColorChannels.test(i)) {