redo metal fence model

This commit is contained in:
Gregory Wells
2025-08-03 17:49:45 -04:00
parent f331afb8e2
commit c114de2bc4
5 changed files with 31 additions and 34 deletions

View File

@@ -31,17 +31,19 @@ MTLStoreAction mtlGryphnStoreOperationResolve(gnStoreOperation storeOperation) {
}
gnReturnCode createMetalRenderPass(gnRenderPassDescriptor renderPass, gnDevice device, gnRenderPassDescriptorInfo info) {
if (device == GN_NULL_HANDLE) return GN_INVALID_HANDLE;
renderPass->renderPassDescriptor = malloc(sizeof(gnPlatformRenderPassDescriptor));
renderPass->renderPassDescriptor->subpassCount = info.subpassCount;
renderPass->renderPassDescriptor->subpasses = malloc(sizeof(mtlSubpass) * info.subpassCount);
renderPass->renderPassDescriptor->copyInfos = malloc(sizeof(mtlSubpassCopyInfo) * info.subpassCount);
for (int i = 0; i < info.subpassCount; i++) {
for (uint32_t i = 0; i < info.subpassCount; i++) {
renderPass->renderPassDescriptor->subpasses[i] = [[MTLRenderPassDescriptor alloc] init];
gnBool resolve = !(info.subpassInfos[i].resolveAttachments == NULL);
renderPass->renderPassDescriptor->copyInfos[i].colorAttachmentCount = info.subpassInfos[i].colorAttachmentCount;
renderPass->renderPassDescriptor->copyInfos[i].colorAttachments = malloc(sizeof(mtlColorAttachmentCopyInfo) * info.subpassInfos[i].colorAttachmentCount);
for (int c = 0; c < info.subpassInfos[i].colorAttachmentCount; c++) {
for (uint32_t c = 0; c < info.subpassInfos[i].colorAttachmentCount; c++) {
uint32_t attachmentIndex = info.subpassInfos[i].colorAttachments[c].index;
int resolveAttachmentIndex = -1;
@@ -77,9 +79,9 @@ gnReturnCode createMetalRenderPass(gnRenderPassDescriptor renderPass, gnDevice d
}
void destroyMetalRenderPass(gnRenderPassDescriptor renderPass) {
for (int i = 0; i < renderPass->renderPassDescriptor->subpassCount; i++) {
for (uint32_t i = 0; i < renderPass->renderPassDescriptor->subpassCount; i++) {
[renderPass->renderPassDescriptor->subpasses[i] release];
for (int c = 0; c < renderPass->renderPassDescriptor->copyInfos[i].colorAttachmentCount; c++)
for (uint32_t c = 0; c < renderPass->renderPassDescriptor->copyInfos[i].colorAttachmentCount; c++)
free(renderPass->renderPassDescriptor->copyInfos[i].colorAttachments);
}
free(renderPass->renderPassDescriptor->copyInfos);

View File

@@ -1,20 +1,23 @@
#include "metal_submit.h"
#include "synchronization/fence/gryphn_fence.h"
#include "sync/fence/metal_fence.h"
gnReturnCode metalSyncSubmit(gnOutputDevice device, gnSubmitSyncInfo info) {
if (device == GN_NULL_HANDLE) return GN_INVALID_HANDLE;
__block gnFence fenceToSignal = info.fence;
__block atomic_int buffersLeft;
atomic_init(&buffersLeft, info.commandBufferCount);
for (int i = 0; i < info.commandBufferCount; i++) {
for (uint32_t i = 0; i < info.commandBufferCount; i++) {
id <MTLCommandBuffer> buffer = [info.commandBuffers[i]->commandPool->commandPool->commandQueue commandBuffer];
for (int c = 0; c < info.waitCount; c++)
for (uint32_t c = 0; c < info.waitCount; c++)
mtlWaitSemaphore(info.waitSemaphores[c], buffer);
[buffer commit];
id<MTLCommandBuffer> commandBuffer = info.commandBuffers[i]->commandBuffer->commandBuffer;
for (int c = 0; c < info.signalCount; c++)
for (uint32_t c = 0; c < info.signalCount; c++)
mtlSignalSemaphore(info.signalSemaphores[c], commandBuffer);
[info.commandBuffers[i]->commandBuffer->commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
@@ -22,7 +25,7 @@ gnReturnCode metalSyncSubmit(gnOutputDevice device, gnSubmitSyncInfo info) {
NSLog(@"Command buffer error: %s", buffer.error.localizedDescription.UTF8String);
}
if (atomic_fetch_sub_explicit(&buffersLeft, 1, memory_order_acq_rel) == 1) {
fenceToSignal->signaled = GN_TRUE;
[fenceToSignal->fence->event setSignaledValue:fenceToSignal->fence->currentValue];
}
}];
@@ -33,7 +36,9 @@ gnReturnCode metalSyncSubmit(gnOutputDevice device, gnSubmitSyncInfo info) {
}
gnReturnCode metalSubmit(gnOutputDevice device, gnSubmitInfo info) {
for (int i = 0; i < info.commandBufferCount; i++) {
if (device == GN_NULL_HANDLE) return GN_INVALID_HANDLE;
for (uint32_t i = 0; i < info.commandBufferCount; i++) {
id<MTLCommandBuffer> commandBuffer = info.commandBuffers[i]->commandBuffer->commandBuffer;
[commandBuffer commit];
}

View File

@@ -9,6 +9,7 @@
#import <CoreGraphics/CoreGraphics.h>
gnReturnCode createMetalSurface(gnWindowSurface windowSurface, gnInstanceHandle instance, gnMacOSWindowSurfaceInfo createInfo) {
if (instance == GN_NULL_HANDLE) return GN_INVALID_HANDLE;
windowSurface->windowSurface = malloc(sizeof(gnPlatformWindowSurface));
windowSurface->windowSurface->layer = createInfo.layer;
return GN_SUCCESS;
@@ -26,13 +27,14 @@ gnImageFormat mtlMetalFormatToGryphn(MTLPixelFormat format) {
gnSurfaceDetails getMetalSurfaceDetails(
gnWindowSurface windowSurface, gnPhysicalDevice device
) {
if (device == GN_NULL_HANDLE) return (gnSurfaceDetails){ .formatCount = 0 };
gnSurfaceDetails surfaceDetails;
surfaceDetails.formatCount = 1;
surfaceDetails.formats = (gnSurfaceFormat[]){ { GN_FORMAT_BGRA8_SRGB, GN_COLOR_SPACE_SRGB_NONLINEAR } };
surfaceDetails.minImageCount = 2;
surfaceDetails.maxImageCount = 3;
CGSize size = windowSurface->windowSurface->layer.drawableSize;
surfaceDetails.minImageSize = surfaceDetails.maxImageSize = surfaceDetails.currentSize = (gnUInt2){size.width, size.height};
surfaceDetails.minImageSize = surfaceDetails.maxImageSize = surfaceDetails.currentSize = (gnUInt2){.x = size.width, .y = size.height};
return surfaceDetails;
}

View File

@@ -3,10 +3,12 @@
#import <Metal/Metal.h>
#import <Metal/MTLEvent.h>
typedef struct gnPlatformFence_t {} gnPlatformFence;
typedef struct gnPlatformFence_t {
uint32_t currentValue;
id<MTLSharedEvent> event;
} gnPlatformFence;
gnReturnCode createMetalFence(gnFence fence, gnDevice device);
void singalMetalFence(gnFence fence);
void waitForMetalFence(gnFence fence, uint64_t timeout);
void resetMetalFence(gnFence fence);
void destroyMetalFence(gnFence fence);

View File

@@ -1,33 +1,19 @@
#include "metal_fence.h"
#include "devices/metal_output_devices.h"
gnReturnCode createMetalFence(gnFence fence, struct gnOutputDevice_t* device) {
// fence->fence = malloc(sizeof(gnPlatformFence));
// fence->fence->fence = [device->outputDevice->device newSharedEvent];
// fence->fence->listener = [[MTLSharedEventListener alloc] init];
// fence->fence->semaphore = dispatch_semaphore_create(1);
gnReturnCode createMetalFence(gnFence fence, gnDevice device) {
fence->fence = malloc(sizeof(gnPlatformFence));
fence->fence->event = [device->outputDevice->device newSharedEvent];
fence->fence->currentValue = 0;
return GN_SUCCESS;
}
void singalMetalFence(gnFence fence) {
// dispatch_semaphore_signal(fence->fence->semaphore);
}
void waitForMetalFence(gnFence fence, uint64_t timeout) {
// dispatch_semaphore_wait(fence->fence->semaphore, timeout);
while (fence->signaled == GN_FALSE) {}
[fence->fence->event waitUntilSignaledValue:fence->fence->currentValue timeoutMS:timeout];
}
void resetMetalFence(gnFence fence) {
// dispatch_semaphore_signal(fence->fence->semaphore);
// [fence->fence->fence setSignaledValue:0];
// [fence->fence->fence notifyListener:fence->fence->listener
// atValue:1
// block:^(id<MTLSharedEvent> ev, uint64_t val) {
// dispatch_semaphore_signal(fence->fence->semaphore);
// }];
fence->fence->currentValue++;
}
void destroyMetalFence(gnFence fence) {
// [fence->fence->fence release];
// [fence->fence->listener release];
// free(fence->fence);
[fence->fence->event release];
free(fence->fence);
}