Skip to content

Commit

Permalink
Make Metal not crash
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Feb 11, 2025
1 parent a140b1b commit 8a2228e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
36 changes: 21 additions & 15 deletions Backends/Graphics5/Metal/Sources/kope/metal/commandlist.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,23 @@ 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<MTLTexture> texture = (__bridge id<MTLTexture>)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;

MTLRenderPassDescriptor *render_pass_descriptor = [MTLRenderPassDescriptor renderPassDescriptor];
render_pass_descriptor.colorAttachments[0].texture = texture;
render_pass_descriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
render_pass_descriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
render_pass_descriptor.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0);
render_pass_descriptor.depthAttachment.clearDepth = 1;
render_pass_descriptor.depthAttachment.loadAction = MTLLoadActionClear;
render_pass_descriptor.depthAttachment.storeAction = MTLStoreActionStore;
render_pass_descriptor.depthAttachment.texture = nil; // depthTexture;
render_pass_descriptor.stencilAttachment.clearStencil = 0;
render_pass_descriptor.stencilAttachment.loadAction = MTLLoadActionDontCare;
render_pass_descriptor.stencilAttachment.storeAction = MTLStoreActionDontCare;
render_pass_descriptor.stencilAttachment.texture = nil; // depthTexture;

id<MTLCommandBuffer> command_buffer = (__bridge id<MTLCommandBuffer>)list->metal.command_buffer;
list->metal.render_command_encoder = (__bridge_retained void *)[command_buffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
list->metal.render_command_encoder = (__bridge_retained void *)[command_buffer renderCommandEncoderWithDescriptor:render_pass_descriptor];
}

void kope_metal_command_list_end_render_pass(kope_g5_command_list *list) {
Expand All @@ -42,7 +43,12 @@ void kope_metal_command_list_end_render_pass(kope_g5_command_list *list) {
}

void kope_metal_command_list_present(kope_g5_command_list *list) {
//[command_buffer presentDrawable:drawable];
id<MTLCommandBuffer> command_buffer = (__bridge id<MTLCommandBuffer>)list->metal.command_buffer;

CAMetalLayer *metal_layer = getMetalLayer();
id<CAMetalDrawable> drawable = [metal_layer nextDrawable];

[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,
Expand Down
10 changes: 8 additions & 2 deletions Backends/Graphics5/Metal/Sources/kope/metal/device.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

#include <assert.h>

CAMetalLayer *getMetalLayer(void);

void kope_metal_device_create(kope_g5_device *device, const kope_g5_device_wishlist *wishlist) {
id<MTLDevice> metal_device = MTLCreateSystemDefaultDevice();
getMetalLayer().device = metal_device;
Expand Down Expand Up @@ -58,9 +56,17 @@ void kope_metal_device_create_texture(kope_g5_device *device, const kope_g5_text
return &framebuffer;
}

kope_g5_texture_format kope_metal_device_framebuffer_format(kope_g5_device *device) {
return KOPE_G5_TEXTURE_FORMAT_RGBA8_UNORM;
}

void kope_metal_device_execute_command_list(kope_g5_device *device, kope_g5_command_list *list) {
id<MTLCommandBuffer> command_buffer = (__bridge id<MTLCommandBuffer>)list->metal.command_buffer;
[command_buffer commit];

id<MTLCommandQueue> command_queue = (__bridge id<MTLCommandQueue>)list->metal.command_queue;
command_buffer = [command_queue commandBuffer];
list->metal.command_buffer = (__bridge_retained void *)[command_queue commandBuffer];
}

void kope_metal_device_wait_until_idle(kope_g5_device *device) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ void kope_metal_device_create_sampler(kope_g5_device *device, const kope_g5_samp

kope_g5_texture *kope_metal_device_get_framebuffer(kope_g5_device *device);

kope_g5_texture_format kope_metal_device_framebuffer_format(kope_g5_device *device);

void kope_metal_device_execute_command_list(kope_g5_device *device, kope_g5_command_list *list);

void kope_metal_device_wait_until_idle(kope_g5_device *device);
Expand Down
2 changes: 2 additions & 0 deletions Backends/Graphics5/Metal/Sources/kope/metal/metalunit.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#import <MetalKit/MTKView.h>

CAMetalLayer *getMetalLayer(void);

#include "buffer.m"
#include "commandlist.m"
#include "descriptorset.m"
Expand Down

0 comments on commit 8a2228e

Please sign in to comment.