From 357ecba0ae00246332447823fddd83b8da89357f Mon Sep 17 00:00:00 2001 From: Luke Street Date: Tue, 15 Apr 2025 19:37:18 -0600 Subject: [PATCH] Lighting channel fixes (COLOR1+) --- lib/dolphin/gx/GXFrameBuffer.cpp | 2 +- lib/gfx/common.cpp | 1 + lib/gfx/gx.cpp | 8 +-- lib/gfx/gx_fmt.hpp | 8 +++ lib/gfx/gx_shader.cpp | 93 ++++++++++++++------------------ 5 files changed, 54 insertions(+), 58 deletions(-) diff --git a/lib/dolphin/gx/GXFrameBuffer.cpp b/lib/dolphin/gx/GXFrameBuffer.cpp index 6d2398f..c942b0d 100644 --- a/lib/dolphin/gx/GXFrameBuffer.cpp +++ b/lib/dolphin/gx/GXFrameBuffer.cpp @@ -30,7 +30,7 @@ void GXSetTexCopySrc(u16 left, u16 top, u16 wd, u16 ht) { g_gxState.texCopySrc = void GXSetDispCopyDst(u16 wd, u16 ht) {} void GXSetTexCopyDst(u16 wd, u16 ht, GXTexFmt fmt, GXBool mipmap) { - CHECK(wd == g_gxState.texCopySrc.width && ht == g_gxState.texCopySrc.height, "Texture copy scaling unimplemented"); + // TODO texture copy scaling (mipmap) g_gxState.texCopyFmt = fmt; } diff --git a/lib/gfx/common.cpp b/lib/gfx/common.cpp index 0e12d82..bffa1af 100644 --- a/lib/gfx/common.cpp +++ b/lib/gfx/common.cpp @@ -624,6 +624,7 @@ void render(wgpu::CommandEncoder& cmd) { } } g_renderPasses.clear(); + g_cachedBindGroups.clear(); } void render_pass(const wgpu::RenderPassEncoder& pass, u32 idx) { diff --git a/lib/gfx/gx.cpp b/lib/gfx/gx.cpp index 89e7657..577d260 100644 --- a/lib/gfx/gx.cpp +++ b/lib/gfx/gx.cpp @@ -341,16 +341,16 @@ Range build_uniform(const ShaderInfo& info) noexcept { if (!info.sampledColorChannels.test(i)) { continue; } - const auto& ccc = g_gxState.colorChannelConfig[i * 2]; - const auto& ccs = g_gxState.colorChannelState[i * 2]; + const auto& ccc = g_gxState.colorChannelConfig[i]; + const auto& ccs = g_gxState.colorChannelState[i]; if (ccc.lightingEnabled && ccc.ambSrc == GX_SRC_REG) { buf.append(ccs.ambColor); } if (ccc.matSrc == GX_SRC_REG) { buf.append(ccs.matColor); } - const auto& ccca = g_gxState.colorChannelConfig[i * 2 + 1]; - const auto& ccsa = g_gxState.colorChannelState[i * 2 + 1]; + const auto& ccca = g_gxState.colorChannelConfig[i + GX_ALPHA0]; + const auto& ccsa = g_gxState.colorChannelState[i + GX_ALPHA0]; if (ccca.lightingEnabled && ccca.ambSrc == GX_SRC_REG) { buf.append(ccsa.ambColor); } diff --git a/lib/gfx/gx_fmt.hpp b/lib/gfx/gx_fmt.hpp index a6ff7dc..13a6825 100644 --- a/lib/gfx/gx_fmt.hpp +++ b/lib/gfx/gx_fmt.hpp @@ -202,6 +202,14 @@ inline std::string format_as(const GXTevKColorSel& sel) { return "GX_TEV_KCSEL_2_8"; case GX_TEV_KCSEL_1_8: return "GX_TEV_KCSEL_1_8"; + case GX_TEV_KCSEL_K0: + return "GX_TEV_KCSEL_K0"; + case GX_TEV_KCSEL_K1: + return "GX_TEV_KCSEL_K1"; + case GX_TEV_KCSEL_K2: + return "GX_TEV_KCSEL_K2"; + case GX_TEV_KCSEL_K3: + return "GX_TEV_KCSEL_K3"; case GX_TEV_KCSEL_K0_R: return "GX_TEV_KCSEL_K0_R"; case GX_TEV_KCSEL_K1_R: diff --git a/lib/gfx/gx_shader.cpp b/lib/gfx/gx_shader.cpp index d947cc2..963be07 100644 --- a/lib/gfx/gx_shader.cpp +++ b/lib/gfx/gx_shader.cpp @@ -583,8 +583,8 @@ ShaderInfo build_shader_info(const ShaderConfig& config) noexcept { info.uniformSize += info.loadsTevReg.count() * sizeof(Vec4); 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]; + const auto& cc = config.colorChannels[i]; + const auto& cca = config.colorChannels[i + GX_ALPHA0]; if (cc.lightingEnabled || cca.lightingEnabled) { info.lightingEnabled = true; } @@ -596,14 +596,14 @@ ShaderInfo build_shader_info(const ShaderConfig& config) noexcept { } for (int i = 0; i < info.sampledColorChannels.size(); ++i) { if (info.sampledColorChannels.test(i)) { - const auto& cc = config.colorChannels[i * 2]; + const auto& cc = config.colorChannels[i]; if (cc.lightingEnabled && cc.ambSrc == GX_SRC_REG) { info.uniformSize += sizeof(Vec4); } if (cc.matSrc == GX_SRC_REG) { info.uniformSize += sizeof(Vec4); } - const auto& cca = config.colorChannels[i * 2 + 1]; + const auto& cca = config.colorChannels[i + GX_ALPHA0]; if (cca.lightingEnabled && cca.ambSrc == GX_SRC_REG) { info.uniformSize += sizeof(Vec4); } @@ -858,7 +858,8 @@ wgpu::ShaderModule build_shader(const ShaderConfig& config, const ShaderInfo& in } for (int i = 0; i < config.colorChannels.size(); ++i) { const auto& chan = config.colorChannels[i]; - Log.info(" colorChannels[{}]: enabled {} mat {} amb {}", i, chan.lightingEnabled, chan.matSrc, chan.ambSrc); + Log.info(" colorChannels[{}]: enabled {} mat {} amb {}", static_cast(i), chan.lightingEnabled, + chan.matSrc, chan.ambSrc); } for (int i = 0; i < config.tcgs.size(); ++i) { const auto& tcg = config.tcgs[i]; @@ -1036,41 +1037,41 @@ wgpu::ShaderModule build_shader(const ShaderConfig& config, const ShaderInfo& in fragmentFnPre += fmt::format("\n var tevreg{0}: vec4f;", i - 1); } } - bool addedLightStruct = false; + + if (info.lightingEnabled) { + uniBufAttrs += fmt::format(FMT_STRING(R"""( + lights: array, + lightState0: u32, + lightState1: u32, + lightState0a: u32, + lightState1a: u32,)"""), + GX::MaxLights); + uniformPre += + "\n" + "struct Light {\n" + " pos: vec3f,\n" + " dir: vec3f,\n" + " color: vec4f,\n" + " cos_att: vec3f,\n" + " dist_att: vec3f,\n" + "};"; + if (UsePerPixelLighting) { + vtxOutAttrs += fmt::format("\n @location({}) mv_pos: vec3f,", vtxOutIdx++); + vtxOutAttrs += fmt::format("\n @location({}) mv_nrm: vec3f,", vtxOutIdx++); + vtxXfrAttrs += fmt::format(FMT_STRING(R"""( + out.mv_pos = mv_pos; + out.mv_nrm = mv_nrm;)""")); + } + } + int vtxColorIdx = 0; for (int i = 0; i < info.sampledColorChannels.size(); ++i) { if (!info.sampledColorChannels.test(i)) { continue; } - const auto& cc = config.colorChannels[i * 2]; - const auto& cca = config.colorChannels[i * 2 + 1]; - - if (!addedLightStruct && (cc.lightingEnabled || cca.lightingEnabled)) { - uniBufAttrs += fmt::format(FMT_STRING("\n lights: array," - "\n lightState0: u32," - "\n lightState0a: u32," - "\n lightState1: u32," - "\n lightState1a: u32,"), - GX::MaxLights); - uniformPre += - "\n" - "struct Light {\n" - " pos: vec3f,\n" - " dir: vec3f,\n" - " color: vec4f,\n" - " cos_att: vec3f,\n" - " dist_att: vec3f,\n" - "};"; - if (UsePerPixelLighting) { - vtxOutAttrs += fmt::format("\n @location({}) mv_pos: vec3f,", vtxOutIdx++); - vtxOutAttrs += fmt::format("\n @location({}) mv_nrm: vec3f,", vtxOutIdx++); - vtxXfrAttrs += fmt::format(FMT_STRING(R"""( - out.mv_pos = mv_pos; - out.mv_nrm = mv_nrm;)""")); - } - addedLightStruct = true; - } + const auto& cc = config.colorChannels[i]; + const auto& cca = config.colorChannels[i + GX_ALPHA0]; if (cc.lightingEnabled && cc.ambSrc == GX_SRC_REG) { uniBufAttrs += fmt::format("\n cc{0}_amb: vec4f,", i); } @@ -1390,31 +1391,21 @@ wgpu::ShaderModule build_shader(const ShaderConfig& config, const ShaderInfo& in const auto shaderSource = fmt::format(R"""( fn fetch_f32_3(p: ptr>, idx: u32) -> vec3 {{ - var start = idx * 3; - return vec3( - p[start], - p[start + 1], - p[start + 2], - ); + var n = idx * 3; + return vec3(p[n], p[n + 1], p[n + 2]); }} fn fetch_u8_2(p: ptr>, idx: u32, frac: u32) -> vec2 {{ var v0 = p[idx / 2]; var r = (idx % 2) != 0; var o0 = select(extractBits(v0, 0, 8), extractBits(v0, 16, 8), r); var o1 = select(extractBits(v0, 8, 8), extractBits(v0, 24, 8), r); - return vec2( - f32(o0) / f32(1 << frac), - f32(o1) / f32(1 << frac), - ); + return vec2(f32(o0), f32(o1)) / f32(1u << frac); }} fn fetch_u16_2(p: ptr>, idx: u32, frac: u32) -> vec2 {{ var v0 = p[idx]; var o0 = extractBits(v0, 0, 16); var o1 = extractBits(v0, 16, 16); - return vec2( - f32(o0) / f32(1 << frac), - f32(o1) / f32(1 << frac), - ); + return vec2(f32(o0), f32(o1)) / f32(1u << frac); }} fn fetch_i16_3(p: ptr>, idx: u32, frac: u32) -> vec3 {{ var n = idx * 3; @@ -1425,11 +1416,7 @@ fn fetch_i16_3(p: ptr>, idx: u32, frac: u32) -> vec3 {{ var o0 = select(extractBits(v0, 0, 16), extractBits(v0, 16, 16), r); var o1 = select(extractBits(v0, 16, 16), extractBits(v1, 0, 16), r); var o2 = select(extractBits(v1, 0, 16), extractBits(v1, 16, 16), r); - return vec3( - f32(o0) / f32(1 << frac), - f32(o1) / f32(1 << frac), - f32(o2) / f32(1 << frac), - ); + return vec3(f32(o0), f32(o1), f32(o2)) / f32(1 << frac); }} {10} struct Uniform {{