Use u32 for scale instead of float in FragmentConfig

This commit is contained in:
offtkp 2024-07-21 17:28:40 +03:00
parent ed67069487
commit b333bf8a0c
2 changed files with 17 additions and 13 deletions

View file

@ -49,8 +49,8 @@ namespace PICA {
BitField<0, 1, u32> enable; BitField<0, 1, u32> enable;
BitField<1, 1, u32> absInput; BitField<1, 1, u32> absInput;
BitField<2, 3, u32> type; BitField<2, 3, u32> type;
BitField<5, 3, u32> scale;
}; };
float scale;
}; };
struct LightingConfig { struct LightingConfig {
@ -142,46 +142,45 @@ namespace PICA {
const u32 lutAbs = regs[InternalRegs::LightLUTAbs]; const u32 lutAbs = regs[InternalRegs::LightLUTAbs];
const u32 lutSelect = regs[InternalRegs::LightLUTSelect]; const u32 lutSelect = regs[InternalRegs::LightLUTSelect];
const u32 lutScale = regs[InternalRegs::LightLUTScale]; 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) { if (d0.enable) {
d0.absInput = Helpers::getBit<1>(lutAbs) == 0; d0.absInput = Helpers::getBit<1>(lutAbs) == 0;
d0.type = Helpers::getBits<0, 3>(lutSelect); 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) { if (d1.enable) {
d1.absInput = Helpers::getBit<5>(lutAbs) == 0; d1.absInput = Helpers::getBit<5>(lutAbs) == 0;
d1.type = Helpers::getBits<4, 3>(lutSelect); 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.absInput = Helpers::getBit<9>(lutAbs) == 0;
sp.type = Helpers::getBits<8, 3>(lutSelect); 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) { if (fr.enable) {
fr.absInput = Helpers::getBit<13>(lutAbs) == 0; fr.absInput = Helpers::getBit<13>(lutAbs) == 0;
fr.type = Helpers::getBits<12, 3>(lutSelect); 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) { if (rb.enable) {
rb.absInput = Helpers::getBit<17>(lutAbs) == 0; rb.absInput = Helpers::getBit<17>(lutAbs) == 0;
rb.type = Helpers::getBits<16, 3>(lutSelect); 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) { if (rg.enable) {
rg.absInput = Helpers::getBit<21>(lutAbs) == 0; rg.absInput = Helpers::getBit<21>(lutAbs) == 0;
rg.type = Helpers::getBits<20, 3>(lutSelect); 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) { if (rr.enable) {
rr.absInput = Helpers::getBit<25>(lutAbs) == 0; rr.absInput = Helpers::getBit<25>(lutAbs) == 0;
rr.type = Helpers::getBits<24, 3>(lutSelect); rr.type = Helpers::getBits<24, 3>(lutSelect);
rr.scale = scales[Helpers::getBits<24, 3>(lutScale)]; rr.scale = Helpers::getBits<24, 3>(lutScale);
} }
} }
}; };

View file

@ -617,7 +617,7 @@ void FragmentGenerator::compileLUTLookup(std::string& shader, const PICA::Fragme
return; return;
} }
float scale = lut.scale; uint scale = lut.scale;
uint inputID = lut.type; uint inputID = lut.type;
bool absEnabled = lut.absInput; bool absEnabled = lut.absInput;
@ -634,17 +634,22 @@ void FragmentGenerator::compileLUTLookup(std::string& shader, const PICA::Fragme
break; break;
} }
static constexpr float scales[] = {1.0f, 2.0f, 4.0f, 8.0f, 0.0f, 0.0f, 0.25f, 0.5f};
if (absEnabled) { if (absEnabled) {
bool twoSidedDiffuse = config.lighting.lights[lightIndex].twoSidedDiffuse; 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 += 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"; 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) { if (scale != 0) {
shader += "lut_lookup_result *= " + std::to_string(scale) + ";\n"; shader += "lut_lookup_result *= " + std::to_string(scales[scale]) + ";\n";
} }
} else { } else {
// Range is [-1, 1] so we need to map it to [0, 1] // 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 += "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 += "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";
}
} }
} }