From b9cbdd3286919310e1b320908660974351117894 Mon Sep 17 00:00:00 2001 From: Greg Wells Date: Sun, 6 Jul 2025 07:21:23 -0400 Subject: [PATCH] redo MTLRenderPassDescriptor creation --- .../src/commands/commands/metal_commands.m | 4 +- .../src/framebuffers/metal_framebuffer.h | 6 ++- .../src/framebuffers/metal_framebuffer.m | 51 +++++++++---------- .../apis/vulkan/src/textures/vulkan_texture.c | 2 +- projects/core/src/textures/gryphn_texture.h | 10 ++-- 5 files changed, 38 insertions(+), 35 deletions(-) diff --git a/projects/apis/metal/src/commands/commands/metal_commands.m b/projects/apis/metal/src/commands/commands/metal_commands.m index be3e39e..ba24f4d 100644 --- a/projects/apis/metal/src/commands/commands/metal_commands.m +++ b/projects/apis/metal/src/commands/commands/metal_commands.m @@ -13,7 +13,7 @@ void metelBeginRenderPass(gnCommandBuffer buffer, gnRenderPassInfo passInfo) { } if(!wasDepthStencil) { - MTLRenderPassColorAttachmentDescriptor* color = passInfo.framebuffer->framebuffer->framebuffer.colorAttachments[i]; + MTLRenderPassColorAttachmentDescriptor* color = passInfo.framebuffer->framebuffer->subpasses[i].colorAttachments[i]; color.clearColor = MTLClearColorMake( passInfo.clearValues[i].red, passInfo.clearValues[i].green, @@ -22,7 +22,7 @@ void metelBeginRenderPass(gnCommandBuffer buffer, gnRenderPassInfo passInfo) { ); } } - buffer->commandBuffer->encoder = [buffer->commandBuffer->commandBuffer renderCommandEncoderWithDescriptor:passInfo.framebuffer->framebuffer->framebuffer]; + buffer->commandBuffer->encoder = [buffer->commandBuffer->commandBuffer renderCommandEncoderWithDescriptor:passInfo.framebuffer->framebuffer->subpasses[0]]; MTLViewport vp = {(double)passInfo.offset.x, (double)passInfo.offset.y, (double)passInfo.size.x, (double)passInfo.size.y, 0.0f, 1.0f}; id encoder = (id)buffer->commandBuffer->encoder; [encoder setViewport:vp]; diff --git a/projects/apis/metal/src/framebuffers/metal_framebuffer.h b/projects/apis/metal/src/framebuffers/metal_framebuffer.h index 6fbbd7d..46a4e65 100644 --- a/projects/apis/metal/src/framebuffers/metal_framebuffer.h +++ b/projects/apis/metal/src/framebuffers/metal_framebuffer.h @@ -2,11 +2,15 @@ #include "framebuffer/gryphn_framebuffer.h" #include "utils/gryphn_bool.h" #include "utils/gryphn_image_format.h" +#include "utils/lists/gryphn_array_list.h" #import #import +typedef MTLRenderPassDescriptor* mtlSubpass; + typedef struct gnPlatformFramebuffer_t { - MTLRenderPassDescriptor* framebuffer; + uint32_t subpassCount; + mtlSubpass* subpasses; } gnPlatformFramebuffer; gnBool isDepthFormat(gnImageFormat format); diff --git a/projects/apis/metal/src/framebuffers/metal_framebuffer.m b/projects/apis/metal/src/framebuffers/metal_framebuffer.m index a70e719..57818d7 100644 --- a/projects/apis/metal/src/framebuffers/metal_framebuffer.m +++ b/projects/apis/metal/src/framebuffers/metal_framebuffer.m @@ -37,38 +37,36 @@ gnReturnCode createMetalFramebuffer(gnFramebuffer framebuffer, gnOutputDevice de return GN_DIVERGENT_RENDERPASS; } - framebuffer->framebuffer->framebuffer = [[MTLRenderPassDescriptor alloc] init]; - [framebuffer->framebuffer->framebuffer setRenderTargetWidth:info.size.x]; - [framebuffer->framebuffer->framebuffer setRenderTargetHeight:info.size.y]; + framebuffer->framebuffer->subpassCount = info.renderPassDescriptor->info.subpassCount; + framebuffer->framebuffer->subpasses = malloc(sizeof(mtlSubpass) * framebuffer->framebuffer->subpassCount); - int colorAttachment = 0; - for (int i = 0; i < info.renderPassDescriptor->info.attachmentCount; i++) { - gnBool wasDepthStencil = gnFalse; - if (isDepthFormat(info.renderPassDescriptor->info.attachmentInfos[i].format)) { - MTLRenderPassDepthAttachmentDescriptor* depthAttachment = framebuffer->framebuffer->framebuffer.depthAttachment; - depthAttachment.texture = info.attachments[i]->texture->texture; - depthAttachment.loadAction = mtlGryphnLoadOperation(info.renderPassDescriptor->info.attachmentInfos[i].loadOperation); - depthAttachment.storeAction = mtlGryphnStoreOperation(info.renderPassDescriptor->info.attachmentInfos[i].storeOperation); - depthAttachment.clearDepth = 1.0f; - wasDepthStencil = gnTrue; - } - if (isStencilFormat(info.renderPassDescriptor->info.attachmentInfos[i].format)) { - MTLRenderPassStencilAttachmentDescriptor* stencilAttachment = framebuffer->framebuffer->framebuffer.stencilAttachment; - stencilAttachment.texture = info.attachments[i]->texture->texture; - wasDepthStencil = gnTrue; - } + for (int i = 0; i < info.renderPassDescriptor->info.subpassCount; i++) { + framebuffer->framebuffer->subpasses[i] = [[MTLRenderPassDescriptor alloc] init]; + [framebuffer->framebuffer->subpasses[i] setRenderTargetWidth:info.size.x]; + [framebuffer->framebuffer->subpasses[i] setRenderTargetHeight:info.size.y]; - if(!wasDepthStencil) { - MTLRenderPassColorAttachmentDescriptor* color = framebuffer->framebuffer->framebuffer.colorAttachments[colorAttachment]; - color.texture = info.attachments[i]->texture->texture; + for (int c = 0; c < info.renderPassDescriptor->info.subpassInfos[i].colorAttachmentCount; c++) { + uint32_t attachmentIndex = info.renderPassDescriptor->info.subpassInfos[i].colorAttachments[i].index; + MTLRenderPassColorAttachmentDescriptor* color = framebuffer->framebuffer->subpasses[i].colorAttachments[c]; + color.texture = info.attachments[attachmentIndex]->texture->texture; - color.loadAction = mtlGryphnLoadOperation(info.renderPassDescriptor->info.attachmentInfos[i].loadOperation); - color.storeAction = mtlGryphnStoreOperation(info.renderPassDescriptor->info.attachmentInfos[i].storeOperation); + color.loadAction = mtlGryphnLoadOperation(info.renderPassDescriptor->info.attachmentInfos[attachmentIndex].loadOperation); + color.storeAction = mtlGryphnStoreOperation(info.renderPassDescriptor->info.attachmentInfos[attachmentIndex].storeOperation); if (color.loadAction == MTLLoadActionClear) color.clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0); + } - colorAttachment++; + if (info.renderPassDescriptor->info.subpassInfos[i].depthAttachment != NULL) { + MTLRenderPassDepthAttachmentDescriptor* depthAttachment = framebuffer->framebuffer->subpasses[i].depthAttachment; + uint32_t attachmentIndex = info.renderPassDescriptor->info.subpassInfos[i].depthAttachment->index; + depthAttachment.texture = info.attachments[attachmentIndex]->texture->texture; + depthAttachment.loadAction = mtlGryphnLoadOperation(info.renderPassDescriptor->info.attachmentInfos[attachmentIndex].loadOperation); + depthAttachment.storeAction = mtlGryphnStoreOperation(info.renderPassDescriptor->info.attachmentInfos[attachmentIndex].storeOperation); + depthAttachment.clearDepth = 1.0f; + + MTLRenderPassStencilAttachmentDescriptor* stencilAttachment = framebuffer->framebuffer->subpasses[attachmentIndex].stencilAttachment; + stencilAttachment.texture = info.attachments[i]->texture->texture; } } @@ -76,6 +74,7 @@ gnReturnCode createMetalFramebuffer(gnFramebuffer framebuffer, gnOutputDevice de } void destroyMetalFramebuffer(gnFramebuffer framebuffer) { - [framebuffer->framebuffer->framebuffer release]; + for (int i = 0; i < framebuffer->framebuffer->subpassCount; i++) [framebuffer->framebuffer->subpasses[i] release]; + free(framebuffer->framebuffer->subpasses); free(framebuffer->framebuffer); } diff --git a/projects/apis/vulkan/src/textures/vulkan_texture.c b/projects/apis/vulkan/src/textures/vulkan_texture.c index 797f909..0ee2298 100644 --- a/projects/apis/vulkan/src/textures/vulkan_texture.c +++ b/projects/apis/vulkan/src/textures/vulkan_texture.c @@ -27,8 +27,8 @@ VkSamplerAddressMode vkGryphnTextureWrap(gnTextureWrap wrap) { VkImageUsageFlags gnImageUsageToVulkan(gnTextureUsageFlags flags) { VkImageUsageFlags vkFlags = 0; - if ((flags & GN_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT) == GN_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT) vkFlags |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; if ((flags & GN_TEXTURE_USAGE_COLOR_ATTACHMENT) == GN_TEXTURE_USAGE_COLOR_ATTACHMENT) vkFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; + if ((flags & GN_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT) == GN_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT) vkFlags |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; if ((flags & GN_TEXTURE_USAGE_SAMPLED) == GN_TEXTURE_USAGE_SAMPLED) vkFlags |= VK_IMAGE_USAGE_SAMPLED_BIT; if ((flags & GN_TEXTURE_USAGE_WRITE_TARGET) == GN_TEXTURE_USAGE_WRITE_TARGET) vkFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT; if ((flags & GN_TEXTURE_RESOLVE_ATTACHMENT) == GN_TEXTURE_RESOLVE_ATTACHMENT) vkFlags |= VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT; diff --git a/projects/core/src/textures/gryphn_texture.h b/projects/core/src/textures/gryphn_texture.h index 4b6346a..478e16a 100644 --- a/projects/core/src/textures/gryphn_texture.h +++ b/projects/core/src/textures/gryphn_texture.h @@ -18,11 +18,11 @@ typedef enum gnTextureWrap { } gnTextureWrap; typedef enum gnTextureUsageFlags { - GN_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT = 1 << 0, - GN_TEXTURE_USAGE_COLOR_ATTACHMENT = 1 << 1, - GN_TEXTURE_USAGE_SAMPLED = 1 << 2, - GN_TEXTURE_USAGE_WRITE_TARGET = 1 << 3, - GN_TEXTURE_RESOLVE_ATTACHMENT = 1 << 4 + GN_TEXTURE_USAGE_COLOR_ATTACHMENT = 1 << 0, + GN_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT = 1 << 1, + GN_TEXTURE_RESOLVE_ATTACHMENT = 1 << 2, + GN_TEXTURE_USAGE_SAMPLED = 1 << 3, + GN_TEXTURE_USAGE_WRITE_TARGET = 1 << 4, } gnTextureUsageFlags; typedef struct gnTextureInfo {