create copy pipeline

This commit is contained in:
Samuliak 2024-07-06 09:39:12 +02:00
parent 82e436199c
commit 80bd8f54c3
3 changed files with 31 additions and 10 deletions

View file

@ -54,6 +54,7 @@ class RendererMTL final : public Renderer {
// Pipelines // Pipelines
MTL::RenderPipelineState* displayPipeline; MTL::RenderPipelineState* displayPipeline;
MTL::RenderPipelineState* copyToLutTexturePipeline;
// Active state // Active state
MTL::CommandBuffer* commandBuffer = nullptr; MTL::CommandBuffer* commandBuffer = nullptr;

View file

@ -22,6 +22,18 @@ PICA::ColorFmt ToColorFormat(u32 format) {
} }
} }
MTL::Library* loadLibrary(MTL::Device* device, const cmrc::file& shaderSource) {
//MTL::CompileOptions* compileOptions = MTL::CompileOptions::alloc()->init();
NS::Error* error = nullptr;
MTL::Library* library = device->newLibrary(Metal::createDispatchData(shaderSource.begin(), shaderSource.size()), &error);
//MTL::Library* library = device->newLibrary(NS::String::string(source.c_str(), NS::ASCIIStringEncoding), compileOptions, &error);
if (error) {
Helpers::panic("Error loading shaders: %s", error->description()->cString(NS::ASCIIStringEncoding));
}
return library;
}
RendererMTL::RendererMTL(GPU& gpu, const std::array<u32, regNum>& internalRegs, const std::array<u32, extRegNum>& externalRegs) RendererMTL::RendererMTL(GPU& gpu, const std::array<u32, regNum>& internalRegs, const std::array<u32, extRegNum>& externalRegs)
: Renderer(gpu, internalRegs, externalRegs) {} : Renderer(gpu, internalRegs, externalRegs) {}
RendererMTL::~RendererMTL() {} RendererMTL::~RendererMTL() {}
@ -125,14 +137,8 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
// Load shaders // Load shaders
auto mtlResources = cmrc::RendererMTL::get_filesystem(); auto mtlResources = cmrc::RendererMTL::get_filesystem();
auto shaderSource = mtlResources.open("metal_shaders.metallib"); MTL::Library* library = loadLibrary(device, mtlResources.open("metal_shaders.metallib"));
//MTL::CompileOptions* compileOptions = MTL::CompileOptions::alloc()->init(); MTL::Library* copyToLutTextureLibrary = loadLibrary(device, mtlResources.open("metal_copy_to_lut_texture.metallib"));
NS::Error* error = nullptr;
MTL::Library* library = device->newLibrary(Metal::createDispatchData(shaderSource.begin(), shaderSource.size()), &error);
//MTL::Library* library = device->newLibrary(NS::String::string(source.c_str(), NS::ASCIIStringEncoding), compileOptions, &error);
if (error) {
Helpers::panic("Error loading shaders: %s", error->description()->cString(NS::ASCIIStringEncoding));
}
// Display // Display
MTL::Function* vertexDisplayFunction = library->newFunction(NS::String::string("vertexDisplay", NS::ASCIIStringEncoding)); MTL::Function* vertexDisplayFunction = library->newFunction(NS::String::string("vertexDisplay", NS::ASCIIStringEncoding));
@ -144,7 +150,7 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
auto* displayColorAttachment = displayPipelineDescriptor->colorAttachments()->object(0); auto* displayColorAttachment = displayPipelineDescriptor->colorAttachments()->object(0);
displayColorAttachment->setPixelFormat(MTL::PixelFormat::PixelFormatBGRA8Unorm); displayColorAttachment->setPixelFormat(MTL::PixelFormat::PixelFormatBGRA8Unorm);
error = nullptr; NS::Error* error = nullptr;
displayPipeline = device->newRenderPipelineState(displayPipelineDescriptor, &error); displayPipeline = device->newRenderPipelineState(displayPipelineDescriptor, &error);
if (error) { if (error) {
Helpers::panic("Error creating display pipeline state: %s", error->description()->cString(NS::ASCIIStringEncoding)); Helpers::panic("Error creating display pipeline state: %s", error->description()->cString(NS::ASCIIStringEncoding));
@ -217,6 +223,20 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
drawPipelineCache.set(device, library, vertexDrawFunction, vertexDescriptor); drawPipelineCache.set(device, library, vertexDrawFunction, vertexDescriptor);
// Copy to LUT texture
MTL::Function* vertexCopyToLutTextureFunction = copyToLutTextureLibrary->newFunction(NS::String::string("vertexCopyToLutTexture", NS::ASCIIStringEncoding));
MTL::RenderPipelineDescriptor* copyToLutTexturePipelineDescriptor = MTL::RenderPipelineDescriptor::alloc()->init();
copyToLutTexturePipelineDescriptor->setVertexFunction(vertexDisplayFunction);
auto* copyToLutTextureColorAttachment = copyToLutTexturePipelineDescriptor->colorAttachments()->object(0);
copyToLutTextureColorAttachment->setPixelFormat(MTL::PixelFormat::PixelFormatBGRA8Unorm);
error = nullptr;
copyToLutTexturePipeline = device->newRenderPipelineState(copyToLutTexturePipelineDescriptor, &error);
if (error) {
Helpers::panic("Error creating copy_to_lut_texture pipeline state: %s", error->description()->cString(NS::ASCIIStringEncoding));
}
// Depth stencil cache // Depth stencil cache
depthStencilCache.set(device); depthStencilCache.set(device);

View file

@ -3,6 +3,6 @@ using namespace metal;
constant ushort lutTextureWidth [[function_constant(0)]]; constant ushort lutTextureWidth [[function_constant(0)]];
vertex void vertexCopyToLUTTexture(uint vid [[vertex_id]], constant ushort* data [[buffer(0)]], texture1d_array<ushort, access::write> out [[texture(0)]]) { vertex void vertexCopyToLutTexture(uint vid [[vertex_id]], constant ushort* data [[buffer(0)]], texture1d_array<ushort, access::write> out [[texture(0)]]) {
out.write(data[vid], vid % lutTextureWidth, vid / lutTextureWidth); out.write(data[vid], vid % lutTextureWidth, vid / lutTextureWidth);
} }