From 74c0df8b71ec387d2917114f9be47baf175ab937 Mon Sep 17 00:00:00 2001 From: Samuliak Date: Tue, 2 Jul 2024 09:26:52 +0200 Subject: [PATCH] create pipeline states --- src/core/renderer_mtl/renderer_mtl.cpp | 61 +++++++++++++++++++++++++- src/host_shaders/metal_shaders.metal | 3 ++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/core/renderer_mtl/renderer_mtl.cpp b/src/core/renderer_mtl/renderer_mtl.cpp index 10b884b7..c50711f1 100644 --- a/src/core/renderer_mtl/renderer_mtl.cpp +++ b/src/core/renderer_mtl/renderer_mtl.cpp @@ -45,9 +45,68 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) { MTL::TextureDescriptor* descriptor = MTL::TextureDescriptor::texture2DDescriptor(MTL::PixelFormatRGBA8Unorm, 400, 240, false); topScreenTexture = device->newTexture(descriptor); - // Pipelines + // -------- Pipelines -------- + + // Load shaders auto mtlResources = cmrc::RendererMTL::get_filesystem(); auto shaderSource = mtlResources.open("metal_shaders.metal"); + std::string source(shaderSource.begin(), shaderSource.size()); + MTL::CompileOptions* compileOptions = MTL::CompileOptions::alloc()->init(); + NS::Error* error = nullptr; + 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 + MTL::Function* vertexDisplayFunction = library->newFunction(NS::String::string("vertexDisplay", NS::ASCIIStringEncoding)); + MTL::Function* fragmentDisplayFunction = library->newFunction(NS::String::string("fragmentDisplay", NS::ASCIIStringEncoding)); + + MTL::RenderPipelineDescriptor* displayPipelineDescriptor = MTL::RenderPipelineDescriptor::alloc()->init(); + displayPipelineDescriptor->setVertexFunction(vertexDisplayFunction); + displayPipelineDescriptor->setFragmentFunction(fragmentDisplayFunction); + // HACK + auto* colorAttachment = displayPipelineDescriptor->colorAttachments()->object(0); + colorAttachment->setPixelFormat(topScreenTexture->pixelFormat()); + + error = nullptr; + displayPipeline = device->newRenderPipelineState(displayPipelineDescriptor, &error); + if (error) { + Helpers::panic("Error creating display pipeline state: %s", error->description()->cString(NS::ASCIIStringEncoding)); + } + + // Draw + MTL::Function* vertexDrawFunction = library->newFunction(NS::String::string("vertexDraw", NS::ASCIIStringEncoding)); + MTL::Function* fragmentDrawFunction = library->newFunction(NS::String::string("fragmentDraw", NS::ASCIIStringEncoding)); + + MTL::RenderPipelineDescriptor* drawPipelineDescriptor = MTL::RenderPipelineDescriptor::alloc()->init(); + drawPipelineDescriptor->setVertexFunction(vertexDrawFunction); + drawPipelineDescriptor->setFragmentFunction(fragmentDrawFunction); + // HACK + colorAttachment->setPixelFormat(MTL::PixelFormatRGBA8Unorm); + + // Vertex descriptor + // TODO: add all attributes + MTL::VertexDescriptor* vertexDescriptor = MTL::VertexDescriptor::alloc()->init(); + MTL::VertexAttributeDescriptor* positionAttribute = vertexDescriptor->attributes()->object(0); + positionAttribute->setFormat(MTL::VertexFormatFloat4); + positionAttribute->setOffset(0); + positionAttribute->setBufferIndex(0); + MTL::VertexAttributeDescriptor* colorAttribute = vertexDescriptor->attributes()->object(2); + colorAttribute->setFormat(MTL::VertexFormatFloat4); + colorAttribute->setOffset(16); + colorAttribute->setBufferIndex(0); + MTL::VertexBufferLayoutDescriptor* vertexBufferLayout = vertexDescriptor->layouts()->object(0); + vertexBufferLayout->setStride(32); + vertexBufferLayout->setStepFunction(MTL::VertexStepFunctionPerVertex); + vertexBufferLayout->setStepRate(1); + drawPipelineDescriptor->setVertexDescriptor(vertexDescriptor); + + error = nullptr; + drawPipeline = device->newRenderPipelineState(drawPipelineDescriptor, &error); + if (error) { + Helpers::panic("Error creating draw pipeline state: %s", error->description()->cString(NS::ASCIIStringEncoding)); + } } void RendererMTL::clearBuffer(u32 startAddress, u32 endAddress, u32 value, u32 control) { diff --git a/src/host_shaders/metal_shaders.metal b/src/host_shaders/metal_shaders.metal index d4dc1557..011bc98e 100644 --- a/src/host_shaders/metal_shaders.metal +++ b/src/host_shaders/metal_shaders.metal @@ -1,3 +1,6 @@ +#include +using namespace metal; + struct DisplayVertexOut { float4 position [[position]]; float2 uv;