finish redoing render pass creation on metal

This commit is contained in:
Gregory Wells
2025-07-23 15:42:34 -04:00
parent ac0813e7f2
commit bb19d855f5
5 changed files with 111 additions and 74 deletions

View File

@@ -2,8 +2,24 @@
#include "renderpass/gryphn_render_pass_descriptor.h"
#import <Metal/MTLRenderPass.h>
typedef MTLRenderPassDescriptor* mtlSubpass;
typedef struct mtlColorAttachmentCopyInfo {
uint32_t attachmentIndex;
int resolveAttachmentIndex; // -1 means no resolve attachment
} mtlColorAttachmentCopyInfo;
typedef struct mtlSubpassCopyInfo {
uint32_t colorAttachmentCount;
mtlColorAttachmentCopyInfo* colorAttachments;
} mtlSubpassCopyInfo;
typedef struct gnPlatformRenderPassDescriptor_t {
MTLRenderPassDescriptor* passDescriptor;
uint32_t subpassCount;
mtlSubpass* subpasses;
mtlSubpassCopyInfo* copyInfos;
} gnPlatformRenderPassDescriptor;
gnReturnCode createMetalRenderPass(gnRenderPassDescriptor renderPass, gnDevice device, gnRenderPassDescriptorInfo info);

View File

@@ -1,13 +1,83 @@
#include "metal_render_pass.h"
gnBool isDepthFormat(gnImageFormat format) {
return (format == GN_FORMAT_D24S8_UINT) || (format == GN_FORMAT_D32S8_UINT);
}
gnBool isStencilFormat(gnImageFormat format) {
return (format == GN_FORMAT_D24S8_UINT) || (format == GN_FORMAT_D32S8_UINT);
}
MTLLoadAction mtlGryphnLoadOperation(gnLoadOperation loadOperation) {
switch(loadOperation) {
case GN_LOAD_OPERATION_LOAD: return MTLLoadActionLoad;
case GN_LOAD_OPERATION_CLEAR: return MTLLoadActionClear;
case GN_LOAD_OPERATION_DONT_CARE: return MTLLoadActionDontCare;
}
}
MTLStoreAction mtlGryphnStoreOperation(gnStoreOperation storeOperation) {
switch (storeOperation) {
case GN_STORE_OPERATION_STORE: return MTLStoreActionStore;
case GN_STORE_OPERATION_DONT_CARE: return MTLStoreActionDontCare;
}
}
MTLStoreAction mtlGryphnStoreOperationResolve(gnStoreOperation storeOperation) {
switch (storeOperation) {
case GN_STORE_OPERATION_STORE: return MTLStoreActionStoreAndMultisampleResolve;
case GN_STORE_OPERATION_DONT_CARE: return MTLStoreActionDontCare;
}
}
gnReturnCode createMetalRenderPass(gnRenderPassDescriptor renderPass, gnDevice device, gnRenderPassDescriptorInfo info) {
renderPass->renderPassDescriptor = malloc(sizeof(gnPlatformRenderPassDescriptor));
renderPass->renderPassDescriptor->passDescriptor = [[MTLRenderPassDescriptor alloc] init];
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++) {
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++) {
uint32_t attachmentIndex = info.subpassInfos[i].colorAttachments[c].index;
int resolveAttachmentIndex = -1;
MTLRenderPassColorAttachmentDescriptor* color = renderPass->renderPassDescriptor->subpasses[i].colorAttachments[c];
if (resolve) {
resolveAttachmentIndex = info.subpassInfos[i].resolveAttachments[c].index;
color.storeAction = mtlGryphnStoreOperationResolve(info.attachmentInfos[attachmentIndex].storeOperation);
} else {
color.storeAction = mtlGryphnStoreOperation(info.attachmentInfos[attachmentIndex].storeOperation);
}
color.loadAction = mtlGryphnLoadOperation(info.attachmentInfos[attachmentIndex].loadOperation);
if (color.loadAction == MTLLoadActionClear)
color.clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0);
renderPass->renderPassDescriptor->copyInfos[i].colorAttachments[i].attachmentIndex = attachmentIndex;
renderPass->renderPassDescriptor->copyInfos[i].colorAttachments[i].resolveAttachmentIndex = resolveAttachmentIndex;
}
if (info.subpassInfos[i].depthAttachment != NULL) {
MTLRenderPassDepthAttachmentDescriptor* depthAttachment = renderPass->renderPassDescriptor->subpasses[i].depthAttachment;
uint32_t attachmentIndex = info.subpassInfos[i].depthAttachment->index;
depthAttachment.loadAction = mtlGryphnLoadOperation(info.attachmentInfos[attachmentIndex].loadOperation);
depthAttachment.storeAction = mtlGryphnStoreOperation(info.attachmentInfos[attachmentIndex].storeOperation);
depthAttachment.clearDepth = 1.0f;
}
}
return GN_SUCCESS;
}
void destroyMetalRenderPass(gnRenderPassDescriptor renderPass) {
[renderPass->renderPassDescriptor->passDescriptor release];
for (int i = 0; i < renderPass->renderPassDescriptor->subpassCount; i++) {
[renderPass->renderPassDescriptor->subpasses[i] release];
for (int c = 0; c < renderPass->renderPassDescriptor->copyInfos[i].colorAttachmentCount; c++)
free(renderPass->renderPassDescriptor->copyInfos[i].colorAttachments);
}
free(renderPass->renderPassDescriptor->copyInfos);
free(renderPass->renderPassDescriptor);
}