mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-20 04:29:13 +12:00
implement pipeline cache
This commit is contained in:
parent
1ba54b44fb
commit
9241306d4d
4 changed files with 157 additions and 98 deletions
83
include/renderer_mtl/mtl_pipeline_cache.hpp
Normal file
83
include/renderer_mtl/mtl_pipeline_cache.hpp
Normal file
|
@ -0,0 +1,83 @@
|
|||
#pragma once
|
||||
|
||||
#include "pica_to_mtl.hpp"
|
||||
|
||||
using namespace PICA;
|
||||
|
||||
namespace Metal {
|
||||
|
||||
struct PipelineHash {
|
||||
ColorFmt colorFmt;
|
||||
DepthFmt depthFmt;
|
||||
};
|
||||
|
||||
// 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 {
|
||||
public:
|
||||
PipelineCache() = default;
|
||||
|
||||
~PipelineCache() {
|
||||
clear();
|
||||
vertexDescriptor->release();
|
||||
vertexFunction->release();
|
||||
fragmentFunction->release();
|
||||
}
|
||||
|
||||
void set(MTL::Device* dev, MTL::Function* vert, MTL::Function* frag, MTL::VertexDescriptor* vertDesc) {
|
||||
device = dev;
|
||||
vertexFunction = vert;
|
||||
fragmentFunction = frag;
|
||||
vertexDescriptor = vertDesc;
|
||||
}
|
||||
|
||||
MTL::RenderPipelineState* get(PipelineHash hash) {
|
||||
u8 intHash = (u8)hash.colorFmt << 4 | (u8)hash.depthFmt;
|
||||
auto& pipeline = pipelineCache[intHash];
|
||||
if (!pipeline) {
|
||||
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));
|
||||
colorAttachment->setBlendingEnabled(true);
|
||||
colorAttachment->setSourceRGBBlendFactor(MTL::BlendFactorSourceAlpha);
|
||||
colorAttachment->setDestinationRGBBlendFactor(MTL::BlendFactorOneMinusSourceAlpha);
|
||||
colorAttachment->setSourceAlphaBlendFactor(MTL::BlendFactorSourceAlpha);
|
||||
colorAttachment->setDestinationAlphaBlendFactor(MTL::BlendFactorOneMinusSourceAlpha);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<u8, MTL::RenderPipelineState*> pipelineCache;
|
||||
|
||||
MTL::Device* device;
|
||||
MTL::Function* vertexFunction;
|
||||
MTL::Function* fragmentFunction;
|
||||
MTL::VertexDescriptor* vertexDescriptor;
|
||||
};
|
||||
|
||||
} // namespace Metal
|
|
@ -3,28 +3,25 @@
|
|||
#include <Metal/Metal.hpp>
|
||||
#include "PICA/regs.hpp"
|
||||
|
||||
// HACK: both functions return a hardcoded format for now, since render pipeline needs to know the format at creation time
|
||||
namespace PICA {
|
||||
|
||||
inline MTL::PixelFormat toMTLPixelFormatColor(ColorFmt format) {
|
||||
return MTL::PixelFormatRGBA8Unorm;
|
||||
//switch (format) {
|
||||
//case ColorFmt::RGBA8: return MTL::PixelFormatRGBA8Unorm;
|
||||
//case ColorFmt::RGB8: return MTL::PixelFormatRGBA8Unorm; // TODO: return the correct format
|
||||
//case ColorFmt::RGBA5551: return MTL::PixelFormatBGR5A1Unorm;
|
||||
//case ColorFmt::RGB565: return MTL::PixelFormatB5G6R5Unorm; // TODO: check if this is correct
|
||||
//case ColorFmt::RGBA4: return MTL::PixelFormatABGR4Unorm; // TODO: check if this is correct
|
||||
//}
|
||||
switch (format) {
|
||||
case ColorFmt::RGBA8: return MTL::PixelFormatRGBA8Unorm;
|
||||
case ColorFmt::RGB8: return MTL::PixelFormatRGBA8Unorm; // TODO: return the correct format
|
||||
case ColorFmt::RGBA5551: return MTL::PixelFormatBGR5A1Unorm;
|
||||
case ColorFmt::RGB565: return MTL::PixelFormatB5G6R5Unorm; // TODO: check if this is correct
|
||||
case ColorFmt::RGBA4: return MTL::PixelFormatABGR4Unorm; // TODO: check if this is correct
|
||||
}
|
||||
}
|
||||
|
||||
inline MTL::PixelFormat toMTLPixelFormatDepth(DepthFmt format) {
|
||||
return MTL::PixelFormatDepth24Unorm_Stencil8;
|
||||
//switch (format) {
|
||||
//case DepthFmt::Depth16: return MTL::PixelFormatDepth16Unorm;
|
||||
//case DepthFmt::Unknown1: return MTL::PixelFormatInvalid;
|
||||
//case DepthFmt::Depth24: return MTL::PixelFormatDepth32Float; // TODO: is this okay?
|
||||
//case DepthFmt::Depth24Stencil8: return MTL::PixelFormatDepth24Unorm_Stencil8;
|
||||
//}
|
||||
switch (format) {
|
||||
case DepthFmt::Depth16: return MTL::PixelFormatDepth16Unorm;
|
||||
case DepthFmt::Unknown1: return MTL::PixelFormatInvalid;
|
||||
case DepthFmt::Depth24: return MTL::PixelFormatDepth32Float; // TODO: is this okay?
|
||||
case DepthFmt::Depth24Stencil8: return MTL::PixelFormatDepth24Unorm_Stencil8;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace PICA
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include "renderer.hpp"
|
||||
#include "texture.hpp"
|
||||
#include "render_target.hpp"
|
||||
#include "mtl_pipeline_cache.hpp"
|
||||
// HACK: use the OpenGL cache
|
||||
#include "../renderer_gl/surface_cache.hpp"
|
||||
|
||||
|
@ -38,14 +39,14 @@ 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;
|
||||
|
||||
// Helpers
|
||||
MTL::SamplerState* basicSampler;
|
||||
|
||||
// Pipelines
|
||||
MTL::RenderPipelineState* displayPipeline;
|
||||
MTL::RenderPipelineState* blitPipeline;
|
||||
MTL::RenderPipelineState* drawPipeline;
|
||||
|
||||
// Active state
|
||||
MTL::CommandBuffer* commandBuffer = nullptr;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue