mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-06-08 03:51:39 +12:00
specialize shader
This commit is contained in:
parent
7e8582d985
commit
0c19f5a3ea
6 changed files with 125 additions and 27 deletions
123
include/renderer_mtl/mtl_draw_pipeline_cache.hpp
Normal file
123
include/renderer_mtl/mtl_draw_pipeline_cache.hpp
Normal file
|
@ -0,0 +1,123 @@
|
|||
#pragma once
|
||||
|
||||
#include "pica_to_mtl.hpp"
|
||||
|
||||
using namespace PICA;
|
||||
|
||||
namespace Metal {
|
||||
|
||||
struct DrawPipelineHash {
|
||||
// Formats
|
||||
ColorFmt colorFmt;
|
||||
DepthFmt depthFmt;
|
||||
|
||||
// Blending
|
||||
bool blendEnabled;
|
||||
u32 blendControl;
|
||||
|
||||
// Specialization constants
|
||||
bool lightingEnabled;
|
||||
};
|
||||
|
||||
// Bind the vertex buffer to binding 30 so that it doesn't occupy the lower indices
|
||||
#define VERTEX_BUFFER_BINDING_INDEX 30
|
||||
|
||||
// This pipeline only caches the pipeline with all of its color and depth attachment variations
|
||||
class DrawPipelineCache {
|
||||
public:
|
||||
DrawPipelineCache() = default;
|
||||
|
||||
~DrawPipelineCache() {
|
||||
clear();
|
||||
vertexDescriptor->release();
|
||||
vertexFunction->release();
|
||||
}
|
||||
|
||||
void set(MTL::Device* dev, MTL::Library* lib, MTL::Function* vert, MTL::VertexDescriptor* vertDesc) {
|
||||
device = dev;
|
||||
library = lib;
|
||||
vertexFunction = vert;
|
||||
vertexDescriptor = vertDesc;
|
||||
}
|
||||
|
||||
MTL::RenderPipelineState* get(DrawPipelineHash hash) {
|
||||
u64 pipelineHash = ((u64)hash.colorFmt << 37) | ((u64)hash.depthFmt << 34) | ((u64)hash.blendEnabled << 33) | ((u64)hash.blendControl << 1) | (u64)hash.lightingEnabled;
|
||||
auto& pipeline = pipelineCache[pipelineHash];
|
||||
if (!pipeline) {
|
||||
u8 fragmentFunctionHash = (u8)hash.lightingEnabled;
|
||||
auto& fragmentFunction = fragmentFunctionCache[fragmentFunctionHash];
|
||||
if (!fragmentFunction) {
|
||||
MTL::FunctionConstantValues* constants = MTL::FunctionConstantValues::alloc()->init();
|
||||
constants->setConstantValue(&hash.lightingEnabled, MTL::DataTypeBool, NS::UInteger(0));
|
||||
|
||||
NS::Error* error = nullptr;
|
||||
fragmentFunction = library->newFunction(NS::String::string("fragmentDraw", NS::ASCIIStringEncoding), constants, &error);
|
||||
if (error) {
|
||||
Helpers::panic("Error creating draw fragment function: %s", error->description()->cString(NS::ASCIIStringEncoding));
|
||||
}
|
||||
constants->release();
|
||||
fragmentFunctionCache[fragmentFunctionHash] = fragmentFunction;
|
||||
}
|
||||
|
||||
MTL::RenderPipelineDescriptor* desc = MTL::RenderPipelineDescriptor::alloc()->init();
|
||||
desc->setVertexFunction(vertexFunction);
|
||||
desc->setFragmentFunction(fragmentFunction);
|
||||
desc->setVertexDescriptor(vertexDescriptor);
|
||||
|
||||
auto colorAttachment = desc->colorAttachments()->object(0);
|
||||
colorAttachment->setPixelFormat(toMTLPixelFormatColor(hash.colorFmt));
|
||||
if (hash.blendEnabled) {
|
||||
const u8 rgbEquation = hash.blendControl & 0x7;
|
||||
const u8 alphaEquation = Helpers::getBits<8, 3>(hash.blendControl);
|
||||
|
||||
// Get blending functions
|
||||
const u8 rgbSourceFunc = Helpers::getBits<16, 4>(hash.blendControl);
|
||||
const u8 rgbDestFunc = Helpers::getBits<20, 4>(hash.blendControl);
|
||||
const u8 alphaSourceFunc = Helpers::getBits<24, 4>(hash.blendControl);
|
||||
const u8 alphaDestFunc = Helpers::getBits<28, 4>(hash.blendControl);
|
||||
|
||||
colorAttachment->setBlendingEnabled(true);
|
||||
colorAttachment->setRgbBlendOperation(toMTLBlendOperation(rgbEquation));
|
||||
colorAttachment->setAlphaBlendOperation(toMTLBlendOperation(alphaEquation));
|
||||
colorAttachment->setSourceRGBBlendFactor(toMTLBlendFactor(rgbSourceFunc));
|
||||
colorAttachment->setDestinationRGBBlendFactor(toMTLBlendFactor(rgbDestFunc));
|
||||
colorAttachment->setSourceAlphaBlendFactor(toMTLBlendFactor(alphaSourceFunc));
|
||||
colorAttachment->setDestinationAlphaBlendFactor(toMTLBlendFactor(alphaDestFunc));
|
||||
}
|
||||
|
||||
desc->setDepthAttachmentPixelFormat(toMTLPixelFormatDepth(hash.depthFmt));
|
||||
|
||||
NS::Error* error = nullptr;
|
||||
pipeline = device->newRenderPipelineState(desc, &error);
|
||||
if (error) {
|
||||
Helpers::panic("Error creating draw pipeline state: %s", error->description()->cString(NS::ASCIIStringEncoding));
|
||||
}
|
||||
|
||||
desc->release();
|
||||
}
|
||||
|
||||
return pipeline;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
for (auto& pair : pipelineCache) {
|
||||
pair.second->release();
|
||||
}
|
||||
pipelineCache.clear();
|
||||
for (auto& pair : fragmentFunctionCache) {
|
||||
pair.second->release();
|
||||
}
|
||||
fragmentFunctionCache.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<u64, MTL::RenderPipelineState*> pipelineCache;
|
||||
std::unordered_map<u8, MTL::Function*> fragmentFunctionCache;
|
||||
|
||||
MTL::Device* device;
|
||||
MTL::Library* library;
|
||||
MTL::Function* vertexFunction;
|
||||
MTL::VertexDescriptor* vertexDescriptor;
|
||||
};
|
||||
|
||||
} // namespace Metal
|
Loading…
Add table
Add a link
Reference in a new issue