redo MTLRenderPassDescriptor creation

This commit is contained in:
Greg Wells
2025-07-06 07:21:23 -04:00
parent df954d8522
commit b9cbdd3286
5 changed files with 38 additions and 35 deletions

View File

@@ -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<MTLRenderCommandEncoder> encoder = (id<MTLRenderCommandEncoder>)buffer->commandBuffer->encoder;
[encoder setViewport:vp];

View File

@@ -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 <Metal/Metal.h>
#import <Metal/MTLRenderPass.h>
typedef MTLRenderPassDescriptor* mtlSubpass;
typedef struct gnPlatformFramebuffer_t {
MTLRenderPassDescriptor* framebuffer;
uint32_t subpassCount;
mtlSubpass* subpasses;
} gnPlatformFramebuffer;
gnBool isDepthFormat(gnImageFormat format);

View File

@@ -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);
}

View File

@@ -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;