tightly pack the pipeline hash

This commit is contained in:
Samuliak 2024-07-06 09:00:08 +02:00
parent 5d5df1931f
commit 559d194cc7
3 changed files with 19 additions and 15 deletions

View file

@ -6,19 +6,21 @@ using namespace PICA;
namespace Metal {
struct DrawPipelineHash {
struct DrawPipelineHash { // 62 bits
// Formats
ColorFmt colorFmt;
DepthFmt depthFmt;
ColorFmt colorFmt; // 3 bits
DepthFmt depthFmt; // 3 bits
// Blending
bool blendEnabled;
u32 blendControl;
bool blendEnabled; // 1 bit
u32 blendControl; // 32 bits
// Specialization constants
bool lightingEnabled;
u8 lightingNumLights;
u8 lightingConfig1;
// Specialization constants (23 bits)
bool lightingEnabled; // 1 bit
u8 lightingNumLights; // 3 bits
u8 lightingConfig1; // 7 bits
// | ref | func | on |
u16 alphaControl; // 12 bits (mask: 11111111 0111 0001)
};
// Bind the vertex buffer to binding 30 so that it doesn't occupy the lower indices
@ -43,8 +45,8 @@ public:
}
MTL::RenderPipelineState* get(DrawPipelineHash hash) {
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;
u32 fragmentFunctionHash = ((u32)hash.lightingEnabled << 22) | ((u32)hash.lightingNumLights << 19) | ((u32)hash.lightingConfig1 << 12) | ((((u32)hash.alphaControl & 0b1111111100000000) >> 8) << 4) | ((((u32)hash.alphaControl & 0b01110000) >> 4) << 1) | ((u32)hash.alphaControl & 0b0001);
u64 pipelineHash = ((u64)hash.colorFmt << 59) | ((u64)hash.depthFmt << 56) | ((u64)hash.blendEnabled << 55) | ((u64)hash.blendControl << 23) | fragmentFunctionHash;
auto& pipeline = pipelineCache[pipelineHash];
if (!pipeline) {
auto& fragmentFunction = fragmentFunctionCache[fragmentFunctionHash];
@ -53,6 +55,7 @@ public:
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));
constants->setConstantValue(&hash.alphaControl, MTL::DataTypeUShort, NS::UInteger(3));
NS::Error* error = nullptr;
fragmentFunction = library->newFunction(NS::String::string("fragmentDraw", NS::ASCIIStringEncoding), constants, &error);
@ -116,7 +119,7 @@ public:
private:
std::unordered_map<u64, MTL::RenderPipelineState*> pipelineCache;
std::unordered_map<u16, MTL::Function*> fragmentFunctionCache;
std::unordered_map<u32, MTL::Function*> fragmentFunctionCache;
MTL::Device* device;
MTL::Library* library;

View file

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

View file

@ -386,6 +386,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)]];
constant uint16_t alphaControl [[function_constant(3)]];
// 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) {
@ -413,7 +414,7 @@ void calcLighting(thread DrawVertexOut& in, constant PicaRegs& picaRegs, texture
bool errorUnimpl = false;
for (uint i = 0u; i < lightingNumLights; i++) {
for (uint i = 0u; i < lightingNumLights + 1; i++) {
uint lightID = extract_bits(GPUREG_LIGHTING_LIGHT_PERMUTATION, int(i * 3u), 3);
uint GPUREG_LIGHTi_SPECULAR0 = picaRegs.read(0x0140u + 0x10u * lightID);
@ -580,7 +581,6 @@ fragment float4 fragmentDraw(DrawVertexOut in [[stage_in]], float4 prevColor [[c
float4 color = performLogicOp(logicOp, globals.tevSources[15], prevColor);
// Perform alpha test
uint alphaControl = picaRegs.read(0x104u);
if ((alphaControl & 1u) != 0u) { // Check if alpha test is on
uint func = (alphaControl >> 4u) & 7u;
float reference = float((alphaControl >> 8u) & 0xffu) / 255.0;