Skip to content

Commit

Permalink
Use u32 for scale instead of float in FragmentConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed Jul 21, 2024
1 parent ed67069 commit b333bf8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
17 changes: 8 additions & 9 deletions include/PICA/pica_frag_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
};
Expand Down
13 changes: 9 additions & 4 deletions src/core/PICA/shader_gen_glsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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";
}
}
}

0 comments on commit b333bf8

Please sign in to comment.