turn config1 into a spec constant

This commit is contained in:
Samuliak 2024-07-06 08:04:59 +02:00
parent 0752a583b5
commit 5d5df1931f
3 changed files with 10 additions and 7 deletions

View file

@ -18,6 +18,7 @@ struct DrawPipelineHash {
// Specialization constants
bool lightingEnabled;
u8 lightingNumLights;
u8 lightingConfig1;
};
// Bind the vertex buffer to binding 30 so that it doesn't occupy the lower indices
@ -42,8 +43,8 @@ public:
}
MTL::RenderPipelineState* get(DrawPipelineHash hash) {
u8 fragmentFunctionHash = ((u8)hash.lightingEnabled << 4) | hash.lightingNumLights;
u64 pipelineHash = ((u64)hash.colorFmt << 44) | ((u64)hash.depthFmt << 41) | ((u64)hash.blendEnabled << 40) | ((u64)hash.blendControl << 8) | fragmentFunctionHash;
u16 fragmentFunctionHash = ((u8)hash.lightingEnabled << 12) | (hash.lightingNumLights << 8) | hash.lightingConfig1;
u64 pipelineHash = ((u64)hash.colorFmt << 52) | ((u64)hash.depthFmt << 49) | ((u64)hash.blendEnabled << 48) | ((u64)hash.blendControl << 16) | fragmentFunctionHash;
auto& pipeline = pipelineCache[pipelineHash];
if (!pipeline) {
auto& fragmentFunction = fragmentFunctionCache[fragmentFunctionHash];
@ -51,6 +52,7 @@ public:
MTL::FunctionConstantValues* constants = MTL::FunctionConstantValues::alloc()->init();
constants->setConstantValue(&hash.lightingEnabled, MTL::DataTypeBool, NS::UInteger(0));
constants->setConstantValue(&hash.lightingNumLights, MTL::DataTypeUChar, NS::UInteger(1));
constants->setConstantValue(&hash.lightingConfig1, MTL::DataTypeUChar, NS::UInteger(2));
NS::Error* error = nullptr;
fragmentFunction = library->newFunction(NS::String::string("fragmentDraw", NS::ASCIIStringEncoding), constants, &error);
@ -114,7 +116,7 @@ public:
private:
std::unordered_map<u64, MTL::RenderPipelineState*> pipelineCache;
std::unordered_map<u8, MTL::Function*> fragmentFunctionCache;
std::unordered_map<u16, MTL::Function*> fragmentFunctionCache;
MTL::Device* device;
MTL::Library* library;

View file

@ -386,6 +386,7 @@ void RendererMTL::drawVertices(PICA::PrimType primType, std::span<const PICA::Ve
}
pipelineHash.lightingEnabled = regs[0x008F] & 1;
pipelineHash.lightingNumLights = (regs[0x01C2] & 0x7) + 1;
pipelineHash.lightingConfig1 = regs[0x01C4u] >> 16; // Last 16 bits are unused, so skip them
// Blending and logic op
pipelineHash.blendEnabled = (regs[PICA::InternalRegs::ColourOperation] & (1 << 8)) != 0;

View file

@ -385,6 +385,7 @@ float3 regToColor(uint reg) {
constant bool lightingEnabled [[function_constant(0)]];
constant uint8_t lightingNumLights [[function_constant(1)]];
constant uint8_t lightingConfig1 [[function_constant(2)]];
// Implements the following algorthm: https://mathb.in/26766
void calcLighting(thread DrawVertexOut& in, constant PicaRegs& picaRegs, texture1d_array<float> texLightingLut, sampler linearSampler, thread float4& primaryColor, thread float4& secondaryColor) {
@ -407,7 +408,6 @@ void calcLighting(thread DrawVertexOut& in, constant PicaRegs& picaRegs, texture
uint GPUREG_LIGHTING_LUTINPUT_ABS = picaRegs.read(0x01D0u);
uint GPUREG_LIGHTING_LUTINPUT_SELECT = picaRegs.read(0x01D1u);
uint GPUREG_LIGHTING_CONFIG0 = picaRegs.read(0x01C3u);
uint GPUREG_LIGHTING_CONFIG1 = picaRegs.read(0x01C4u);
uint GPUREG_LIGHTING_LUTINPUT_SCALE = picaRegs.read(0x01D2u);
float d[7];
@ -443,7 +443,7 @@ void calcLighting(thread DrawVertexOut& in, constant PicaRegs& picaRegs, texture
}
for (int c = 0; c < 7; c++) {
if (extract_bits(GPUREG_LIGHTING_CONFIG1, 16 + c, 1) == 0u) {
if (extract_bits(lightingConfig1, c, 1) == 0u) {
uint scaleID = extract_bits(GPUREG_LIGHTING_LUTINPUT_SCALE, c * 4, 3);
float scale = float(1u << scaleID);
if (scaleID >= 6u) scale /= 256.0;
@ -545,8 +545,8 @@ fragment float4 fragmentDraw(DrawVertexOut in [[stage_in]], float4 prevColor [[c
if (lightingEnabled) {
calcLighting(in, picaRegs, texLightingLut, linearSampler, globals.tevSources[1], globals.tevSources[2]);
} else {
globals.tevSources[1] = float4(0.0);
globals.tevSources[2] = float4(0.0);
globals.tevSources[1] = float4(1.0);
globals.tevSources[2] = float4(1.0);
}
uint textureConfig = picaRegs.read(0x80u);