From b333bf8a0c66e80a7b0f622ad123eeb8b6f27428 Mon Sep 17 00:00:00 2001 From: offtkp Date: Sun, 21 Jul 2024 17:28:40 +0300 Subject: [PATCH] Use u32 for scale instead of float in FragmentConfig --- include/PICA/pica_frag_config.hpp | 17 ++++++++--------- src/core/PICA/shader_gen_glsl.cpp | 13 +++++++++---- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/include/PICA/pica_frag_config.hpp b/include/PICA/pica_frag_config.hpp index ee18eee00..f4142ef1e 100644 --- a/include/PICA/pica_frag_config.hpp +++ b/include/PICA/pica_frag_config.hpp @@ -49,8 +49,8 @@ namespace PICA { BitField<0, 1, u32> enable; BitField<1, 1, u32> absInput; BitField<2, 3, u32> type; + BitField<5, 3, u32> scale; }; - float scale; }; struct LightingConfig { @@ -142,46 +142,45 @@ namespace PICA { const u32 lutAbs = regs[InternalRegs::LightLUTAbs]; const u32 lutSelect = regs[InternalRegs::LightLUTSelect]; const u32 lutScale = regs[InternalRegs::LightLUTScale]; - static constexpr float scales[] = {1.0f, 2.0f, 4.0f, 8.0f, 0.0f, 0.0f, 0.25f, 0.5f}; if (d0.enable) { d0.absInput = Helpers::getBit<1>(lutAbs) == 0; d0.type = Helpers::getBits<0, 3>(lutSelect); - d0.scale = scales[Helpers::getBits<0, 3>(lutScale)]; + d0.scale = Helpers::getBits<0, 3>(lutScale); } if (d1.enable) { d1.absInput = Helpers::getBit<5>(lutAbs) == 0; d1.type = Helpers::getBits<4, 3>(lutSelect); - d1.scale = scales[Helpers::getBits<4, 3>(lutScale)]; + d1.scale = Helpers::getBits<4, 3>(lutScale); } sp.absInput = Helpers::getBit<9>(lutAbs) == 0; sp.type = Helpers::getBits<8, 3>(lutSelect); - sp.scale = scales[Helpers::getBits<8, 3>(lutScale)]; + sp.scale = Helpers::getBits<8, 3>(lutScale); if (fr.enable) { fr.absInput = Helpers::getBit<13>(lutAbs) == 0; fr.type = Helpers::getBits<12, 3>(lutSelect); - fr.scale = scales[Helpers::getBits<12, 3>(lutScale)]; + fr.scale = Helpers::getBits<12, 3>(lutScale); } if (rb.enable) { rb.absInput = Helpers::getBit<17>(lutAbs) == 0; rb.type = Helpers::getBits<16, 3>(lutSelect); - rb.scale = scales[Helpers::getBits<16, 3>(lutScale)]; + rb.scale = Helpers::getBits<16, 3>(lutScale); } if (rg.enable) { rg.absInput = Helpers::getBit<21>(lutAbs) == 0; rg.type = Helpers::getBits<20, 3>(lutSelect); - rg.scale = scales[Helpers::getBits<20, 3>(lutScale)]; + rg.scale = Helpers::getBits<20, 3>(lutScale); } if (rr.enable) { rr.absInput = Helpers::getBit<25>(lutAbs) == 0; rr.type = Helpers::getBits<24, 3>(lutSelect); - rr.scale = scales[Helpers::getBits<24, 3>(lutScale)]; + rr.scale = Helpers::getBits<24, 3>(lutScale); } } }; diff --git a/src/core/PICA/shader_gen_glsl.cpp b/src/core/PICA/shader_gen_glsl.cpp index 47df58b81..3d688bd2e 100644 --- a/src/core/PICA/shader_gen_glsl.cpp +++ b/src/core/PICA/shader_gen_glsl.cpp @@ -617,7 +617,7 @@ void FragmentGenerator::compileLUTLookup(std::string& shader, const PICA::Fragme return; } - float scale = lut.scale; + uint scale = lut.scale; uint inputID = lut.type; bool absEnabled = lut.absInput; @@ -634,17 +634,22 @@ void FragmentGenerator::compileLUTLookup(std::string& shader, const PICA::Fragme break; } + static constexpr float scales[] = {1.0f, 2.0f, 4.0f, 8.0f, 0.0f, 0.0f, 0.25f, 0.5f}; + if (absEnabled) { bool twoSidedDiffuse = config.lighting.lights[lightIndex].twoSidedDiffuse; shader += twoSidedDiffuse ? "lut_lookup_delta = abs(lut_lookup_delta);\n" : "lut_lookup_delta = max(lut_lookup_delta, 0.0);\n"; shader += "lut_lookup_result = lutLookup(" + std::to_string(lutIndex) + ", int(clamp(floor(lut_lookup_delta * 256.0), 0.0, 255.0)));\n"; - if (scale != 1.0) { - shader += "lut_lookup_result *= " + std::to_string(scale) + ";\n"; + if (scale != 0) { + shader += "lut_lookup_result *= " + std::to_string(scales[scale]) + ";\n"; } } else { // Range is [-1, 1] so we need to map it to [0, 1] shader += "lut_lookup_index = int(clamp(floor(lut_lookup_delta * 128.0), -128.f, 127.f));\n"; shader += "if (lut_lookup_index < 0) lut_lookup_index += 256;\n"; - shader += "lut_lookup_result = lutLookup(" + std::to_string(lutIndex) + ", lut_lookup_index) *" + std::to_string(scale) + ";\n"; + shader += "lut_lookup_result = lutLookup(" + std::to_string(lutIndex) + ", lut_lookup_index);\n"; + if (scale != 0) { + shader += "lut_lookup_result *= " + std::to_string(scales[scale]) + ";\n"; + } } } \ No newline at end of file