From 5d5df1931f164b5be1bc4537dbd33e432993a7fd Mon Sep 17 00:00:00 2001 From: Samuliak Date: Sat, 6 Jul 2024 08:04:59 +0200 Subject: [PATCH] turn config1 into a spec constant --- include/renderer_mtl/mtl_draw_pipeline_cache.hpp | 8 +++++--- src/core/renderer_mtl/renderer_mtl.cpp | 1 + src/host_shaders/metal_shaders.metal | 8 ++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/include/renderer_mtl/mtl_draw_pipeline_cache.hpp b/include/renderer_mtl/mtl_draw_pipeline_cache.hpp index a28caf1b..c4bf6e22 100644 --- a/include/renderer_mtl/mtl_draw_pipeline_cache.hpp +++ b/include/renderer_mtl/mtl_draw_pipeline_cache.hpp @@ -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 pipelineCache; - std::unordered_map fragmentFunctionCache; + std::unordered_map fragmentFunctionCache; MTL::Device* device; MTL::Library* library; diff --git a/src/core/renderer_mtl/renderer_mtl.cpp b/src/core/renderer_mtl/renderer_mtl.cpp index a7fa208e..30a1fc72 100644 --- a/src/core/renderer_mtl/renderer_mtl.cpp +++ b/src/core/renderer_mtl/renderer_mtl.cpp @@ -386,6 +386,7 @@ void RendererMTL::drawVertices(PICA::PrimType primType, std::span> 16; // Last 16 bits are unused, so skip them // Blending and logic op pipelineHash.blendEnabled = (regs[PICA::InternalRegs::ColourOperation] & (1 << 8)) != 0; diff --git a/src/host_shaders/metal_shaders.metal b/src/host_shaders/metal_shaders.metal index e88b867e..85401a74 100644 --- a/src/host_shaders/metal_shaders.metal +++ b/src/host_shaders/metal_shaders.metal @@ -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 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);