From 38799cb663ae73e4b243cb798ce974ddf4d8a44a Mon Sep 17 00:00:00 2001 From: Greg Wells Date: Sat, 24 May 2025 16:04:14 -0400 Subject: [PATCH] redo device creation --- .../src/core/devices/metal_output_device.m | 128 ++++++++++-------- .../src/core/devices/metal_output_devices.h | 2 + .../metal/src/core/framebuffer.metal | 33 ----- .../metal/src/core/instance/metal_instance.h | 1 - 4 files changed, 76 insertions(+), 88 deletions(-) delete mode 100644 rendering_api/metal/src/core/framebuffer.metal diff --git a/rendering_api/metal/src/core/devices/metal_output_device.m b/rendering_api/metal/src/core/devices/metal_output_device.m index 5648cf0..5338df9 100644 --- a/rendering_api/metal/src/core/devices/metal_output_device.m +++ b/rendering_api/metal/src/core/devices/metal_output_device.m @@ -3,6 +3,7 @@ #include "metal_output_devices.h" #include "core/instance/metal_instance.h" #include "core/instance/gryphn_instance.h" +#include gnReturnCode gnCreateOutputDeviceFn(gnOutputDevice* outputDevice, gnInstance* instance, struct gnOutputDeviceInfo_t deviceInfo) { outputDevice->outputDevice = malloc(sizeof(gnPlatformOutputDevice)); @@ -13,6 +14,79 @@ gnReturnCode gnCreateOutputDeviceFn(gnOutputDevice* outputDevice, gnInstance* in outputDevice->outputDevice->queues[i] = outputDevice->outputDevice->device.newCommandQueue; } + { + + NSError* error = nil; + MTLCompileOptions* options = nil; + NSString *shaderSource = @"#include \ + using namespace metal;\ + struct VertexOut {\ + float4 position [[position]];\ + float2 uv;\ + };\ + vertex VertexOut vs_main(uint vertexID [[vertex_id]]) {\ + float2 positions[4] = {\ + {-1.0, -1.0},\ + { 1.0, -1.0},\ + {-1.0, 1.0},\ + { 1.0, 1.0}\ + };\ + float2 uvs[4] = {\ + {0.0, 1.0},\ + {1.0, 1.0},\ + {0.0, 0.0},\ + {1.0, 0.0}\ + };\ + VertexOut out;\ + out.position = float4(positions[vertexID], 0.0, 1.0);\ + out.uv = uvs[vertexID];\ + return out;\ + }\ + fragment float4 fs_main(VertexOut in [[stage_in]],\ + texture2d colorTex [[texture(0)]],\ + sampler samp [[sampler(0)]]) {\ + return colorTex.sample(samp, in.uv);\ + }\ + "; + id library = [outputDevice->outputDevice->device newLibraryWithSource:shaderSource options:nil error:&error]; + if (!library) { + gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){ + .message = gnCreateString("Failed to compile framebuffer shader") + }); + return GN_FAILED_TO_CREATE_DEVICE; + } + // id vs = library->newFunction(NS::String::string("vs_main", NS::UTF8StringEncoding)); + id vs = [library newFunctionWithName:@"vs_main"]; + id fs = [library newFunctionWithName:@"fs_main"]; + + if (vs == nil) { + gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){ + .message = gnCreateString("Failed to load frambuffer vertex shader") + }); + return GN_FAILED_TO_CREATE_DEVICE; + } + + if (fs == nil) { + gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){ + .message = gnCreateString("Failed to load frambuffer fragment shader") + }); + return GN_FAILED_TO_CREATE_DEVICE; + } + + MTLRenderPipelineDescriptor* pipelineDesc =[[MTLRenderPipelineDescriptor alloc] init];; + pipelineDesc.vertexFunction = vs; + pipelineDesc.fragmentFunction = fs; + pipelineDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm_sRGB; + + outputDevice->outputDevice->framebuffer = [outputDevice->outputDevice->device newRenderPipelineStateWithDescriptor:pipelineDesc error:&error]; + if (!outputDevice->outputDevice->framebuffer) { + gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){ + .message = gnCreateString("Failed to create frambuffer render pipeline") + }); + return GN_FAILED_TO_CREATE_DEVICE; + } + } + return GN_SUCCESS; } @@ -27,57 +101,3 @@ void gnDestroyOutputDeviceFn(gnOutputDevice* device) { // float x, y; // float u, v; // }; - -// GN_EXPORT gnReturnCode gnRegisterOutputDeviceFn(gnOutputDevice* outputDevice, const gnInstance& instance, const gnPhysicalOutputDevice& physicalDevice) { -// if (outputDevice->outputDevice == nullptr) outputDevice->outputDevice = new gnPlatformOutputDevice(); -// outputDevice->physicalOutputDevice = const_cast(&physicalDevice); - -// // instance.instance->metalLayer->setDevice(physicalDevice.physicalOutputDevice->device); - -// // outputDevice->outputDevice->contentView = instance.instance->metalContentView->retain(); -// outputDevice->outputDevice->device = physicalDevice.physicalOutputDevice->device->retain(); -// outputDevice->outputDevice->commandQueue = outputDevice->outputDevice->device->newCommandQueue(); -// outputDevice->outputDevice->instance = const_cast(&instance); - -// { - -// NS::Error* error = nullptr; -// MTL::CompileOptions* options = nullptr; -// MTL::Library* library = physicalDevice.physicalOutputDevice->device->newLibrary(NS::String::string(shaderSrc, NS::UTF8StringEncoding), options, &error); -// if (!library) { -// return gnReturnError(GN_FAILED_CREATE_DEVICE, error->localizedDescription()->utf8String()); -// } -// MTL::Function* vs = library->newFunction(NS::String::string("vs_main", NS::UTF8StringEncoding)); -// MTL::Function* fs = library->newFunction(NS::String::string("fs_main", NS::UTF8StringEncoding)); - -// MTL::RenderPipelineDescriptor* pipelineDesc = MTL::RenderPipelineDescriptor::alloc()->init(); -// pipelineDesc->setVertexFunction(vs); -// pipelineDesc->setFragmentFunction(fs); -// pipelineDesc->colorAttachments()->object(0)->setPixelFormat(MTL::PixelFormatBGRA8Unorm); - -// instance.instance->framebufferRenderer = outputDevice->outputDevice->device->newRenderPipelineState(pipelineDesc, &error); -// if (!instance.instance->framebufferRenderer) { -// return gnReturnError(GN_FAILED_CREATE_DEVICE, error->localizedDescription()->utf8String()); -// } -// } - -// return GN_SUCCESS; -// } - -// GN_EXPORT void gnWaitForDeviceFn(const gnOutputDevice& device) { -// NS::AutoreleasePool* pool = NS::AutoreleasePool::alloc()->init(); - -// auto mtlDevice = device.physicalOutputDevice->physicalOutputDevice->device; - -// auto commandBuffer = device.outputDevice->commandQueue->commandBuffer(); - -// commandBuffer->commit(); -// commandBuffer->waitUntilCompleted(); - -// pool->release(); -// } - -// GN_EXPORT void gnDestroyOutputDeviceFn(gnOutputDevice& device) { -// device.outputDevice->commandQueue->release(); -// device.physicalOutputDevice->physicalOutputDevice->device->release(); -// } diff --git a/rendering_api/metal/src/core/devices/metal_output_devices.h b/rendering_api/metal/src/core/devices/metal_output_devices.h index 76d78cd..72872db 100644 --- a/rendering_api/metal/src/core/devices/metal_output_devices.h +++ b/rendering_api/metal/src/core/devices/metal_output_devices.h @@ -14,4 +14,6 @@ struct gnPlatformOutputDevice_t { int queueCount; id* queues; + + id framebuffer; } gnPlatformOutputDevice; diff --git a/rendering_api/metal/src/core/framebuffer.metal b/rendering_api/metal/src/core/framebuffer.metal deleted file mode 100644 index dd75014..0000000 --- a/rendering_api/metal/src/core/framebuffer.metal +++ /dev/null @@ -1,33 +0,0 @@ -#include -using namespace metal; - -struct VertexOut { - float4 position [[position]]; - float2 uv; -}; - -vertex VertexOut vs_main(uint vertexID [[vertex_id]]) { - float2 positions[4] = { - {-1.0, -1.0}, - { 1.0, -1.0}, - {-1.0, 1.0}, - { 1.0, 1.0} - }; - float2 uvs[4] = { - {0.0, 1.0}, - {1.0, 1.0}, - {0.0, 0.0}, - {1.0, 0.0} - }; - - VertexOut out; - out.position = float4(positions[vertexID], 0.0, 1.0); - out.uv = uvs[vertexID]; - return out; -} - -fragment float4 fs_main(VertexOut in [[stage_in]], - texture2d colorTex [[texture(0)]], - sampler samp [[sampler(0)]]) { - return colorTex.sample(samp, in.uv); -} diff --git a/rendering_api/metal/src/core/instance/metal_instance.h b/rendering_api/metal/src/core/instance/metal_instance.h index 703406a..38be4cf 100644 --- a/rendering_api/metal/src/core/instance/metal_instance.h +++ b/rendering_api/metal/src/core/instance/metal_instance.h @@ -4,5 +4,4 @@ typedef struct gnPlatformInstance_t { NSView* metalContentView; - id framebufferRenderer; } gnPlatformInstance;