From a16e92f5f2de22249dfca37b0de17458e4afc064 Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Sun, 2 Feb 2025 23:22:38 +0100 Subject: [PATCH] Try to create render passes --- .../Metal/Sources/kope/metal/commandlist.m | 24 ++++++++++++++++--- .../Sources/kope/metal/commandlist_structs.h | 4 +++- .../Metal/Sources/kope/metal/device.m | 18 ++++++++++---- .../Sources/kope/metal/texture_structs.h | 2 +- .../Sources/kinc/backend/BasicOpenGLView.m.h | 5 ---- 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/Backends/Graphics5/Metal/Sources/kope/metal/commandlist.m b/Backends/Graphics5/Metal/Sources/kope/metal/commandlist.m index e3923a555..356c7c019 100644 --- a/Backends/Graphics5/Metal/Sources/kope/metal/commandlist.m +++ b/Backends/Graphics5/Metal/Sources/kope/metal/commandlist.m @@ -18,15 +18,33 @@ void kope_metal_command_list_destroy(kope_g5_command_list *list) { } void kope_metal_command_list_begin_render_pass(kope_g5_command_list *list, const kope_g5_render_pass_parameters *parameters) { - + id texture = (__bridge id)parameters->color_attachments[0].texture.texture->metal.texture; + MTLRenderPassDescriptor *renderPassDescriptor = [MTLRenderPassDescriptor renderPassDescriptor]; + renderPassDescriptor.colorAttachments[0].texture = texture; + renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear; + renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore; + renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0); + renderPassDescriptor.depthAttachment.clearDepth = 1; + renderPassDescriptor.depthAttachment.loadAction = MTLLoadActionClear; + renderPassDescriptor.depthAttachment.storeAction = MTLStoreActionStore; + renderPassDescriptor.depthAttachment.texture = nil; //depthTexture; + renderPassDescriptor.stencilAttachment.clearStencil = 0; + renderPassDescriptor.stencilAttachment.loadAction = MTLLoadActionDontCare; + renderPassDescriptor.stencilAttachment.storeAction = MTLStoreActionDontCare; + renderPassDescriptor.stencilAttachment.texture = nil; //depthTexture; + + id command_buffer = (__bridge id)list->metal.command_buffer; + list->metal.render_command_encoder = (__bridge_retained void *)[command_buffer renderCommandEncoderWithDescriptor:renderPassDescriptor]; } void kope_metal_command_list_end_render_pass(kope_g5_command_list *list) { - + id render_command_encoder = (__bridge id)list->metal.render_command_encoder; + [render_command_encoder endEncoding]; + list->metal.render_command_encoder = NULL; } void kope_metal_command_list_present(kope_g5_command_list *list) { - + //[command_buffer presentDrawable:drawable]; } void kope_metal_command_list_set_index_buffer(kope_g5_command_list *list, kope_g5_buffer *buffer, kope_g5_index_format index_format, uint64_t offset, diff --git a/Backends/Graphics5/Metal/Sources/kope/metal/commandlist_structs.h b/Backends/Graphics5/Metal/Sources/kope/metal/commandlist_structs.h index 61b9a8a5f..ca6509e3a 100644 --- a/Backends/Graphics5/Metal/Sources/kope/metal/commandlist_structs.h +++ b/Backends/Graphics5/Metal/Sources/kope/metal/commandlist_structs.h @@ -20,7 +20,9 @@ typedef struct kope_metal_buffer_access { } kope_metal_buffer_access; typedef struct kope_metal_command_list { - void *queue; + void *command_queue; + void *command_buffer; + void *render_command_encoder; } kope_metal_command_list; #ifdef __cplusplus diff --git a/Backends/Graphics5/Metal/Sources/kope/metal/device.m b/Backends/Graphics5/Metal/Sources/kope/metal/device.m index 898667ac2..147ac3c6d 100644 --- a/Backends/Graphics5/Metal/Sources/kope/metal/device.m +++ b/Backends/Graphics5/Metal/Sources/kope/metal/device.m @@ -10,11 +10,11 @@ #include -CAMetalLayer *get_metal_layer(void); +CAMetalLayer *getMetalLayer(void); void kope_metal_device_create(kope_g5_device *device, const kope_g5_device_wishlist *wishlist) { id metal_device = MTLCreateSystemDefaultDevice(); - get_metal_layer().device = metal_device; + getMetalLayer().device = metal_device; device->metal.device = (__bridge_retained void *)metal_device; device->metal.library = (__bridge_retained void *)[metal_device newDefaultLibrary]; } @@ -46,19 +46,27 @@ void kope_metal_device_create_buffer(kope_g5_device *device, const kope_g5_buffe void kope_metal_device_create_command_list(kope_g5_device *device, kope_g5_command_list_type type, kope_g5_command_list *list) { id metal_device = (__bridge id)device->metal.device; - list->metal.queue = (__bridge_retained void *)[metal_device newCommandQueue]; + id command_queue = [metal_device newCommandQueue]; + list->metal.command_queue = (__bridge_retained void *)command_queue; + list->metal.command_buffer = (__bridge_retained void *)[command_queue commandBuffer]; } void kope_metal_device_create_texture(kope_g5_device *device, const kope_g5_texture_parameters *parameters, kope_g5_texture *texture) { } +static kope_g5_texture framebuffer; + kope_g5_texture *kope_metal_device_get_framebuffer(kope_g5_device *device) { - return NULL; + CAMetalLayer *metal_layer = getMetalLayer(); + id drawable = [metal_layer nextDrawable]; + framebuffer.metal.texture = (__bridge_retained void *)drawable.texture; + return &framebuffer; } void kope_metal_device_execute_command_list(kope_g5_device *device, kope_g5_command_list *list) { - + id command_buffer = (__bridge id)list->metal.command_buffer; + [command_buffer commit]; } void kope_metal_device_wait_until_idle(kope_g5_device *device) { diff --git a/Backends/Graphics5/Metal/Sources/kope/metal/texture_structs.h b/Backends/Graphics5/Metal/Sources/kope/metal/texture_structs.h index 445d515c0..fdb5019ba 100644 --- a/Backends/Graphics5/Metal/Sources/kope/metal/texture_structs.h +++ b/Backends/Graphics5/Metal/Sources/kope/metal/texture_structs.h @@ -6,7 +6,7 @@ extern "C" { #endif typedef struct kope_metal_texture { - int nothing; + void *texture; } kope_metal_texture; #ifdef __cplusplus diff --git a/Backends/System/macOS/Sources/kinc/backend/BasicOpenGLView.m.h b/Backends/System/macOS/Sources/kinc/backend/BasicOpenGLView.m.h index 0a6bf64ed..9fbdad805 100644 --- a/Backends/System/macOS/Sources/kinc/backend/BasicOpenGLView.m.h +++ b/Backends/System/macOS/Sources/kinc/backend/BasicOpenGLView.m.h @@ -486,11 +486,6 @@ static CAMetalLayer *metalLayer = NULL; - (id)metalQueue { return commandQueue; } - -CAMetalLayer *get_metal_layer(void) { - return metalLayer; -} - #endif @end