Skip to content

Commit

Permalink
Merge branch 'main' of ssh://github.com/Kode/Kinc
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Feb 13, 2025
2 parents 22d737b + 6700362 commit 66d79e6
Show file tree
Hide file tree
Showing 15 changed files with 440 additions and 39 deletions.
70 changes: 51 additions & 19 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,21 +43,52 @@ 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,
uint64_t size) {}
uint64_t size) {
list->metal.index_buffer = buffer->metal.buffer;
list->metal.sixteen_bit_indices = index_format == KOPE_G5_INDEX_FORMAT_UINT16 ? true : false;
}

void kope_metal_command_list_set_vertex_buffer(kope_g5_command_list *list, uint32_t slot, kope_metal_buffer *buffer, uint64_t offset, uint64_t size,
uint64_t stride) {}
uint64_t stride) {
id<MTLBuffer> metal_buffer = (__bridge id<MTLBuffer>)buffer->buffer;

id<MTLRenderCommandEncoder> render_command_encoder = (__bridge id<MTLRenderCommandEncoder>)list->metal.render_command_encoder;
[render_command_encoder setVertexBuffer:metal_buffer offset:offset atIndex:slot];
}

void kope_metal_command_list_set_render_pipeline(kope_g5_command_list *list, kope_metal_render_pipeline *pipeline) {}
void kope_metal_command_list_set_render_pipeline(kope_g5_command_list *list, kope_metal_render_pipeline *pipeline) {
id<MTLRenderPipelineState> metal_pipeline = (__bridge id<MTLRenderPipelineState>)pipeline->pipeline;

id<MTLRenderCommandEncoder> render_command_encoder = (__bridge id<MTLRenderCommandEncoder>)list->metal.render_command_encoder;
[render_command_encoder setRenderPipelineState:metal_pipeline];
}

void kope_metal_command_list_draw(kope_g5_command_list *list, uint32_t vertex_count, uint32_t instance_count, uint32_t first_vertex, uint32_t first_instance) {}

void kope_metal_command_list_draw_indexed(kope_g5_command_list *list, uint32_t index_count, uint32_t instance_count, uint32_t first_index, int32_t base_vertex,
uint32_t first_instance) {}
uint32_t first_instance) {
id<MTLBuffer> index_buffer = (__bridge id<MTLBuffer>)list->metal.index_buffer;

id<MTLRenderCommandEncoder> render_command_encoder = (__bridge id<MTLRenderCommandEncoder>)list->metal.render_command_encoder;

[render_command_encoder drawIndexedPrimitives:MTLPrimitiveTypeTriangle
indexCount:index_count
indexType:list->metal.sixteen_bit_indices ? MTLIndexTypeUInt16 : MTLIndexTypeUInt32
indexBuffer:index_buffer
indexBufferOffset:first_index
instanceCount:instance_count
baseVertex:base_vertex
baseInstance:first_instance];
}

void kope_metal_command_list_set_descriptor_table(kope_g5_command_list *list, uint32_t table_index, kope_metal_descriptor_set *set,
kope_g5_buffer **dynamic_buffers, uint32_t *dynamic_offsets, uint32_t *dynamic_sizes) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ typedef struct kope_metal_command_list {
void *command_queue;
void *command_buffer;
void *render_command_encoder;
void *index_buffer;
bool sixteen_bit_indices;
} kope_metal_command_list;

#ifdef __cplusplus
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_BGRA8_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
8 changes: 8 additions & 0 deletions Backends/Graphics5/Metal/Sources/kope/metal/metalunit.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#ifndef KOPE_METAL_UNIT_HEADER
#define KOPE_METAL_UNIT_HEADER

#include <kope/graphics5/device.h>

#include <assert.h>

#import <MetalKit/MTKView.h>

CAMetalLayer *getMetalLayer(void);

#endif
6 changes: 0 additions & 6 deletions Backends/Graphics5/Metal/Sources/kope/metal/metalunit.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#include "metalunit.h"

#include <kope/graphics5/device.h>

#include <assert.h>

#import <MetalKit/MTKView.h>

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

0 comments on commit 66d79e6

Please sign in to comment.