mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-21 12:59:14 +12:00
specialize shader
This commit is contained in:
parent
7e8582d985
commit
0c19f5a3ea
6 changed files with 125 additions and 27 deletions
72
include/renderer_mtl/mtl_blit_pipeline_cache.hpp
Normal file
72
include/renderer_mtl/mtl_blit_pipeline_cache.hpp
Normal file
|
@ -0,0 +1,72 @@
|
|||
#pragma once
|
||||
|
||||
#include "pica_to_mtl.hpp"
|
||||
|
||||
using namespace PICA;
|
||||
|
||||
namespace Metal {
|
||||
|
||||
struct BlitPipelineHash {
|
||||
// Formats
|
||||
ColorFmt colorFmt;
|
||||
DepthFmt depthFmt;
|
||||
};
|
||||
|
||||
// This pipeline only caches the pipeline with all of its color and depth attachment variations
|
||||
class BlitPipelineCache {
|
||||
public:
|
||||
BlitPipelineCache() = default;
|
||||
|
||||
~BlitPipelineCache() {
|
||||
clear();
|
||||
vertexFunction->release();
|
||||
fragmentFunction->release();
|
||||
}
|
||||
|
||||
void set(MTL::Device* dev, MTL::Function* vert, MTL::Function* frag) {
|
||||
device = dev;
|
||||
vertexFunction = vert;
|
||||
fragmentFunction = frag;
|
||||
}
|
||||
|
||||
MTL::RenderPipelineState* get(BlitPipelineHash hash) {
|
||||
u8 intHash = ((u8)hash.colorFmt << 3) | (u8)hash.depthFmt;
|
||||
auto& pipeline = pipelineCache[intHash];
|
||||
if (!pipeline) {
|
||||
MTL::RenderPipelineDescriptor* desc = MTL::RenderPipelineDescriptor::alloc()->init();
|
||||
desc->setVertexFunction(vertexFunction);
|
||||
desc->setFragmentFunction(fragmentFunction);
|
||||
|
||||
auto colorAttachment = desc->colorAttachments()->object(0);
|
||||
colorAttachment->setPixelFormat(toMTLPixelFormatColor(hash.colorFmt));
|
||||
|
||||
desc->setDepthAttachmentPixelFormat(toMTLPixelFormatDepth(hash.depthFmt));
|
||||
|
||||
NS::Error* error = nullptr;
|
||||
pipeline = device->newRenderPipelineState(desc, &error);
|
||||
if (error) {
|
||||
Helpers::panic("Error creating blit pipeline state: %s", error->description()->cString(NS::ASCIIStringEncoding));
|
||||
}
|
||||
|
||||
desc->release();
|
||||
}
|
||||
|
||||
return pipeline;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
for (auto& pair : pipelineCache) {
|
||||
pair.second->release();
|
||||
}
|
||||
pipelineCache.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<u8, MTL::RenderPipelineState*> pipelineCache;
|
||||
|
||||
MTL::Device* device;
|
||||
MTL::Function* vertexFunction;
|
||||
MTL::Function* fragmentFunction;
|
||||
};
|
||||
|
||||
} // namespace Metal
|
|
@ -6,7 +6,7 @@ using namespace PICA;
|
|||
|
||||
namespace Metal {
|
||||
|
||||
struct PipelineHash {
|
||||
struct DrawPipelineHash {
|
||||
// Formats
|
||||
ColorFmt colorFmt;
|
||||
DepthFmt depthFmt;
|
||||
|
@ -14,34 +14,51 @@ struct PipelineHash {
|
|||
// 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 PipelineCache {
|
||||
class DrawPipelineCache {
|
||||
public:
|
||||
PipelineCache() = default;
|
||||
DrawPipelineCache() = default;
|
||||
|
||||
~PipelineCache() {
|
||||
~DrawPipelineCache() {
|
||||
clear();
|
||||
vertexDescriptor->release();
|
||||
vertexFunction->release();
|
||||
fragmentFunction->release();
|
||||
}
|
||||
|
||||
void set(MTL::Device* dev, MTL::Function* vert, MTL::Function* frag, MTL::VertexDescriptor* vertDesc) {
|
||||
void set(MTL::Device* dev, MTL::Library* lib, MTL::Function* vert, MTL::VertexDescriptor* vertDesc) {
|
||||
device = dev;
|
||||
library = lib;
|
||||
vertexFunction = vert;
|
||||
fragmentFunction = frag;
|
||||
vertexDescriptor = vertDesc;
|
||||
}
|
||||
|
||||
MTL::RenderPipelineState* get(PipelineHash hash) {
|
||||
u64 intHash = ((u64)hash.colorFmt << 36) | ((u64)hash.depthFmt << 33) | ((u64)hash.blendEnabled << 32) | (u64)hash.blendControl;
|
||||
auto& pipeline = pipelineCache[intHash];
|
||||
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);
|
||||
|
@ -87,14 +104,19 @@ public:
|
|||
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::Function* fragmentFunction;
|
||||
MTL::VertexDescriptor* vertexDescriptor;
|
||||
};
|
||||
|
|
@ -4,7 +4,8 @@
|
|||
#include "renderer.hpp"
|
||||
#include "mtl_texture.hpp"
|
||||
#include "mtl_render_target.hpp"
|
||||
#include "mtl_pipeline_cache.hpp"
|
||||
#include "mtl_blit_pipeline_cache.hpp"
|
||||
#include "mtl_draw_pipeline_cache.hpp"
|
||||
#include "mtl_depth_stencil_cache.hpp"
|
||||
#include "mtl_vertex_buffer_cache.hpp"
|
||||
// HACK: use the OpenGL cache
|
||||
|
@ -41,8 +42,8 @@ class RendererMTL final : public Renderer {
|
|||
SurfaceCache<Metal::ColorRenderTarget, 16, true> colorRenderTargetCache;
|
||||
SurfaceCache<Metal::DepthStencilRenderTarget, 16, true> depthStencilRenderTargetCache;
|
||||
SurfaceCache<Metal::Texture, 256, true> textureCache;
|
||||
Metal::PipelineCache blitPipelineCache;
|
||||
Metal::PipelineCache drawPipelineCache;
|
||||
Metal::BlitPipelineCache blitPipelineCache;
|
||||
Metal::DrawPipelineCache drawPipelineCache;
|
||||
Metal::DepthStencilCache depthStencilCache;
|
||||
Metal::VertexBufferCache vertexBufferCache;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue