kinda start to fix render pass descriptors in metal

This commit is contained in:
Gregory Wells
2025-07-24 08:29:05 -04:00
parent ddf23f71fc
commit 1d23dd0b80
11 changed files with 61 additions and 39 deletions

View File

@@ -6,22 +6,15 @@
void metelBeginRenderPass(gnCommandBuffer buffer, gnRenderPassInfo passInfo) { void metelBeginRenderPass(gnCommandBuffer buffer, gnRenderPassInfo passInfo) {
int currentColorAttachment = 0; int currentColorAttachment = 0;
for (int i = 0; i < passInfo.clearValueCount; i++) { for (int i = 0; i < passInfo.clearValueCount; i++) {
gnBool wasDepthStencil = gnFalse; if (passInfo.framebuffer->framebuffer->clearCopies[i].clear) {
if (isDepthFormat(passInfo.renderPassDescriptor->info.attachmentInfos[i].format)) { MTLRenderPassColorAttachmentDescriptor* color = passInfo.framebuffer->framebuffer->clearCopies[i].descriptor;
wasDepthStencil = gnTrue;
}
if (isStencilFormat(passInfo.renderPassDescriptor->info.attachmentInfos[i].format)) {
wasDepthStencil = gnTrue;
}
if(!wasDepthStencil) {
MTLRenderPassColorAttachmentDescriptor* color = passInfo.framebuffer->framebuffer->subpasses[i].colorAttachments[i];
color.clearColor = MTLClearColorMake( color.clearColor = MTLClearColorMake(
passInfo.clearValues[i].red, passInfo.clearValues[i].r,
passInfo.clearValues[i].green, passInfo.clearValues[i].g,
passInfo.clearValues[i].blue, passInfo.clearValues[i].b,
passInfo.clearValues[i].alpha passInfo.clearValues[i].a
); );
currentColorAttachment++;
} }
} }
buffer->commandBuffer->encoder = [buffer->commandBuffer->commandBuffer renderCommandEncoderWithDescriptor:passInfo.framebuffer->framebuffer->subpasses[0]]; buffer->commandBuffer->encoder = [buffer->commandBuffer->commandBuffer renderCommandEncoderWithDescriptor:passInfo.framebuffer->framebuffer->subpasses[0]];

View File

@@ -7,9 +7,18 @@
#import <Metal/Metal.h> #import <Metal/Metal.h>
#import <Metal/MTLRenderPass.h> #import <Metal/MTLRenderPass.h>
typedef struct mtlClearCopy {
gnBool clear;
MTLRenderPassColorAttachmentDescriptor* descriptor;
} mtlClearCopy;
typedef struct gnPlatformFramebuffer_t { typedef struct gnPlatformFramebuffer_t {
uint32_t subpassCount; uint32_t subpassCount;
mtlSubpass* subpasses; mtlSubpass* subpasses;
uint32_t* depthAttachmentIndicies;
uint32_t clearCopyCount;
mtlClearCopy* clearCopies;
} gnPlatformFramebuffer; } gnPlatformFramebuffer;
gnBool isDepthFormat(gnImageFormat format); gnBool isDepthFormat(gnImageFormat format);

View File

@@ -7,10 +7,15 @@
gnReturnCode createMetalFramebuffer(gnFramebuffer framebuffer, gnOutputDevice device, gnFramebufferInfo info) { gnReturnCode createMetalFramebuffer(gnFramebuffer framebuffer, gnOutputDevice device, gnFramebufferInfo info) {
framebuffer->framebuffer = malloc(sizeof(struct gnPlatformFramebuffer_t)); framebuffer->framebuffer = malloc(sizeof(struct gnPlatformFramebuffer_t));
framebuffer->framebuffer->clearCopyCount = info.attachmentCount;
framebuffer->framebuffer->clearCopies = malloc(sizeof(mtlClearCopy) * info.attachmentCount);
for (int i = 0; i < info.attachmentCount; i++) {
framebuffer->framebuffer->clearCopies[i].clear = gnFalse;
}
framebuffer->framebuffer->subpassCount = info.renderPassDescriptor->renderPassDescriptor->subpassCount; framebuffer->framebuffer->subpassCount = info.renderPassDescriptor->renderPassDescriptor->subpassCount;
framebuffer->framebuffer->subpasses = malloc(sizeof(mtlSubpass) * framebuffer->framebuffer->subpassCount); framebuffer->framebuffer->subpasses = malloc(sizeof(mtlSubpass) * framebuffer->framebuffer->subpassCount);
framebuffer->framebuffer->depthAttachmentIndicies = malloc(sizeof(uint32_t) * framebuffer->framebuffer->subpassCount);
for (int i = 0; i < framebuffer->framebuffer->subpassCount; i++) { for (int i = 0; i < framebuffer->framebuffer->subpassCount; i++) {
framebuffer->framebuffer->subpasses[i] = [info.renderPassDescriptor->renderPassDescriptor->subpasses[i] copy]; framebuffer->framebuffer->subpasses[i] = [info.renderPassDescriptor->renderPassDescriptor->subpasses[i] copy];
MTLRenderPassDescriptor* pass = framebuffer->framebuffer->subpasses[i]; MTLRenderPassDescriptor* pass = framebuffer->framebuffer->subpasses[i];
@@ -22,15 +27,20 @@ gnReturnCode createMetalFramebuffer(gnFramebuffer framebuffer, gnOutputDevice de
colorPass.texture = info.attachments[info.renderPassDescriptor->renderPassDescriptor->copyInfos[i].colorAttachments[c].attachmentIndex]->texture->texture; colorPass.texture = info.attachments[info.renderPassDescriptor->renderPassDescriptor->copyInfos[i].colorAttachments[c].attachmentIndex]->texture->texture;
if (info.renderPassDescriptor->renderPassDescriptor->copyInfos[i].colorAttachments[c].resolveAttachmentIndex >= 0) if (info.renderPassDescriptor->renderPassDescriptor->copyInfos[i].colorAttachments[c].resolveAttachmentIndex >= 0)
colorPass.resolveTexture = info.attachments[info.renderPassDescriptor->renderPassDescriptor->copyInfos[i].colorAttachments[c].resolveAttachmentIndex]->texture->texture; colorPass.resolveTexture = info.attachments[info.renderPassDescriptor->renderPassDescriptor->copyInfos[i].colorAttachments[c].resolveAttachmentIndex]->texture->texture;
framebuffer->framebuffer->clearCopies[info.renderPassDescriptor->renderPassDescriptor->copyInfos[i].colorAttachments[c].attachmentIndex].clear = gnTrue;
framebuffer->framebuffer->clearCopies[info.renderPassDescriptor->renderPassDescriptor->copyInfos[i].colorAttachments[c].attachmentIndex].descriptor = colorPass;
} }
if (info.renderPassDescriptor->renderPassDescriptor->copyInfos[i].depthAttachmentIndex >= 0) {
if (info.attachments[i]->info.format == GN_FORMAT_D24S8_UINT || info.attachments[i]->info.format == GN_FORMAT_D32S8_UINT) {
MTLRenderPassDepthAttachmentDescriptor* depthAttachment = framebuffer->framebuffer->subpasses[i].depthAttachment; MTLRenderPassDepthAttachmentDescriptor* depthAttachment = framebuffer->framebuffer->subpasses[i].depthAttachment;
depthAttachment.texture = info.attachments[i]->texture->texture; depthAttachment.texture = info.attachments[info.renderPassDescriptor->renderPassDescriptor->copyInfos[i].depthAttachmentIndex]->texture->texture;
MTLRenderPassStencilAttachmentDescriptor* stencilAttachment = framebuffer->framebuffer->subpasses[i].stencilAttachment;
stencilAttachment.texture = info.attachments[i]->texture->texture;
} }
// if (info.attachments[i]->info.format == GN_FORMAT_D24S8_UINT || info.attachments[i]->info.format == GN_FORMAT_D32S8_UINT) {
// framebuffer->framebuffer->depthAttachmentIndicies[i] = i;
// MTLRenderPassStencilAttachmentDescriptor* stencilAttachment = framebuffer->framebuffer->subpasses[i].stencilAttachment;
// stencilAttachment.texture = info.attachments[i]->texture->texture;
// }
} }
return GN_SUCCESS; return GN_SUCCESS;

View File

@@ -45,14 +45,15 @@ MTLCompareFunction mtlGrypnCompareOperation(gnCompareOperation operation) {
gnReturnCode createMetalGraphicsPipeline(gnGraphicsPipeline graphicsPipeline, gnOutputDevice device, gnGraphicsPipelineInfo info) { gnReturnCode createMetalGraphicsPipeline(gnGraphicsPipeline graphicsPipeline, gnOutputDevice device, gnGraphicsPipelineInfo info) {
graphicsPipeline->graphicsPipeline = malloc(sizeof(struct gnPlatformGraphicsPipeline_t)); graphicsPipeline->graphicsPipeline = malloc(sizeof(struct gnPlatformGraphicsPipeline_t));
MTLRenderPipelineDescriptor* descriptor = [[MTLRenderPipelineDescriptor alloc] init]; MTLRenderPipelineDescriptor* descriptor = [[MTLRenderPipelineDescriptor alloc] init];
descriptor.rasterSampleCount = mtlSampleCount(info.multisample.samples); // descriptor.rasterSampleCount = mtlSampleCount(info.multisample.samples);
struct gnSubpassInfo_t subpass = info.renderPassDescriptor->info.subpassInfos[info.subpassIndex]; descriptor.rasterSampleCount = 1;
descriptor.rasterizationEnabled = YES;
mtlSubpass subpass = info.renderPassDescriptor->renderPassDescriptor->subpasses[info.subpassIndex];
mtlSubpassCopyInfo copyInfo = info.renderPassDescriptor->renderPassDescriptor->copyInfos[info.subpassIndex];
for (uint32_t i = 0; i < copyInfo.colorAttachmentCount; i++) {
MTLRenderPassColorAttachmentDescriptor* colorPass = subpass.colorAttachments[i];
for (uint32_t i = 0; i < subpass.colorAttachmentCount; i++) { descriptor.colorAttachments[i].pixelFormat = copyInfo.colorAttachments[i].format;
gnSubpassAttachmentInfo subpassAtt = subpass.colorAttachments[i];
gnRenderPassAttachmentInfo attInfo = info.renderPassDescriptor->info.attachmentInfos[subpassAtt.index];
descriptor.colorAttachments[i].pixelFormat = mtlGryphnFormatToMetalFormat(attInfo.format);
if (info.colorBlending.enable == gnTrue) { if (info.colorBlending.enable == gnTrue) {
[descriptor.colorAttachments objectAtIndexedSubscript:i].blendingEnabled = YES; [descriptor.colorAttachments objectAtIndexedSubscript:i].blendingEnabled = YES;
[descriptor.colorAttachments objectAtIndexedSubscript:i].rgbBlendOperation = mtlGryphnBlendOperation(info.colorBlending.colorBlendOperation); [descriptor.colorAttachments objectAtIndexedSubscript:i].rgbBlendOperation = mtlGryphnBlendOperation(info.colorBlending.colorBlendOperation);

View File

@@ -31,6 +31,8 @@ gnReturnCode metalPresent(gnOutputDeviceHandle device, gnPresentInfo info) {
[blit endEncoding]; [blit endEncoding];
[drawable texture];
[commandBuffer presentDrawable:drawable]; [commandBuffer presentDrawable:drawable];
[commandBuffer commit]; [commandBuffer commit];
device->outputDevice->executingCommandBuffer = commandBuffer; device->outputDevice->executingCommandBuffer = commandBuffer;

View File

@@ -30,6 +30,7 @@ gnReturnCode createMetalPresentationQueue(gnPresentationQueueHandle presentation
textureDescriptor.width = presentationInfo.imageSize.x; textureDescriptor.width = presentationInfo.imageSize.x;
textureDescriptor.height = presentationInfo.imageSize.y; textureDescriptor.height = presentationInfo.imageSize.y;
textureDescriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead; textureDescriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
textureDescriptor.sampleCount = 1;
textureDescriptor.textureType = MTLTextureType2D; textureDescriptor.textureType = MTLTextureType2D;
presentationQueue->imageCount = presentationInfo.minImageCount; presentationQueue->imageCount = presentationInfo.minImageCount;

View File

@@ -1,10 +1,13 @@
#pragma once #pragma once
#include "renderpass/gryphn_render_pass_descriptor.h" #include "renderpass/gryphn_render_pass_descriptor.h"
#import <Metal/MTLRenderPass.h> #import <Metal/MTLRenderPass.h>
#import <Metal/Metal.h>
typedef MTLRenderPassDescriptor* mtlSubpass; typedef MTLRenderPassDescriptor* mtlSubpass;
typedef struct mtlColorAttachmentCopyInfo { typedef struct mtlColorAttachmentCopyInfo {
MTLPixelFormat format;
uint32_t attachmentIndex; uint32_t attachmentIndex;
int resolveAttachmentIndex; // -1 means no resolve attachment int resolveAttachmentIndex; // -1 means no resolve attachment
} mtlColorAttachmentCopyInfo; } mtlColorAttachmentCopyInfo;
@@ -12,6 +15,7 @@ typedef struct mtlColorAttachmentCopyInfo {
typedef struct mtlSubpassCopyInfo { typedef struct mtlSubpassCopyInfo {
uint32_t colorAttachmentCount; uint32_t colorAttachmentCount;
mtlColorAttachmentCopyInfo* colorAttachments; mtlColorAttachmentCopyInfo* colorAttachments;
int depthAttachmentIndex; // -1 means no depth attachment
} mtlSubpassCopyInfo; } mtlSubpassCopyInfo;
typedef struct gnPlatformRenderPassDescriptor_t { typedef struct gnPlatformRenderPassDescriptor_t {

View File

@@ -45,28 +45,32 @@ gnReturnCode createMetalRenderPass(gnRenderPassDescriptor renderPass, gnDevice d
uint32_t attachmentIndex = info.subpassInfos[i].colorAttachments[c].index; uint32_t attachmentIndex = info.subpassInfos[i].colorAttachments[c].index;
int resolveAttachmentIndex = -1; int resolveAttachmentIndex = -1;
renderPass->renderPassDescriptor->subpasses[i].colorAttachments[c] = [[MTLRenderPassColorAttachmentDescriptor alloc] init];
MTLRenderPassColorAttachmentDescriptor* color = renderPass->renderPassDescriptor->subpasses[i].colorAttachments[c]; MTLRenderPassColorAttachmentDescriptor* color = renderPass->renderPassDescriptor->subpasses[i].colorAttachments[c];
if (resolve) { if (resolve) {
resolveAttachmentIndex = info.subpassInfos[i].resolveAttachments[c].index; resolveAttachmentIndex = info.subpassInfos[i].resolveAttachments[c].index;
color.storeAction = mtlGryphnStoreOperationResolve(info.attachmentInfos[attachmentIndex].storeOperation); color.storeAction = mtlGryphnStoreOperationResolve(info.attachmentInfos[attachmentIndex].storeOperation);
} else { } else
color.storeAction = mtlGryphnStoreOperation(info.attachmentInfos[attachmentIndex].storeOperation); color.storeAction = mtlGryphnStoreOperation(info.attachmentInfos[attachmentIndex].storeOperation);
}
color.loadAction = mtlGryphnLoadOperation(info.attachmentInfos[attachmentIndex].loadOperation); color.loadAction = mtlGryphnLoadOperation(info.attachmentInfos[attachmentIndex].loadOperation);
color.storeAction = MTLStoreActionStoreAndMultisampleResolve;
if (color.loadAction == MTLLoadActionClear) if (color.loadAction == MTLLoadActionClear)
color.clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0); color.clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0);
renderPass->renderPassDescriptor->copyInfos[i].colorAttachments[i].attachmentIndex = attachmentIndex; renderPass->renderPassDescriptor->copyInfos[i].colorAttachments[c].format = MTLPixelFormatBGRA8Unorm_sRGB;
renderPass->renderPassDescriptor->copyInfos[i].colorAttachments[i].resolveAttachmentIndex = resolveAttachmentIndex; renderPass->renderPassDescriptor->copyInfos[i].colorAttachments[c].attachmentIndex = attachmentIndex;
renderPass->renderPassDescriptor->copyInfos[i].colorAttachments[c].resolveAttachmentIndex = resolveAttachmentIndex;
} }
renderPass->renderPassDescriptor->copyInfos[i].depthAttachmentIndex = -1;
if (info.subpassInfos[i].depthAttachment != NULL) { if (info.subpassInfos[i].depthAttachment != NULL) {
MTLRenderPassDepthAttachmentDescriptor* depthAttachment = renderPass->renderPassDescriptor->subpasses[i].depthAttachment; MTLRenderPassDepthAttachmentDescriptor* depthAttachment = renderPass->renderPassDescriptor->subpasses[i].depthAttachment;
uint32_t attachmentIndex = info.subpassInfos[i].depthAttachment->index; uint32_t attachmentIndex = info.subpassInfos[i].depthAttachment->index;
depthAttachment.loadAction = mtlGryphnLoadOperation(info.attachmentInfos[attachmentIndex].loadOperation); depthAttachment.loadAction = mtlGryphnLoadOperation(info.attachmentInfos[attachmentIndex].loadOperation);
depthAttachment.storeAction = mtlGryphnStoreOperation(info.attachmentInfos[attachmentIndex].storeOperation); depthAttachment.storeAction = mtlGryphnStoreOperation(info.attachmentInfos[attachmentIndex].storeOperation);
depthAttachment.clearDepth = 1.0f; depthAttachment.clearDepth = 1.0f;
renderPass->renderPassDescriptor->copyInfos[i].depthAttachmentIndex = attachmentIndex;
} }
} }
return GN_SUCCESS; return GN_SUCCESS;

View File

@@ -24,13 +24,10 @@ gnReturnCode createMetalTexture(gnTexture texture, gnDevice device, const gnText
textureDescriptor.height = info.extent.height; textureDescriptor.height = info.extent.height;
textureDescriptor.pixelFormat = mtlGryphnFormatToMetalFormat(info.format); textureDescriptor.pixelFormat = mtlGryphnFormatToMetalFormat(info.format);
if (textureDescriptor.sampleCount >= 2) { if (textureDescriptor.sampleCount >= 2)
textureDescriptor.storageMode = MTLStorageModeShared;
textureDescriptor.textureType = MTLTextureType2DMultisample; textureDescriptor.textureType = MTLTextureType2DMultisample;
} else
else {
textureDescriptor.textureType = MTLTextureType2D; textureDescriptor.textureType = MTLTextureType2D;
}
MTLSamplerDescriptor *samplerDesc = [[MTLSamplerDescriptor alloc] init]; MTLSamplerDescriptor *samplerDesc = [[MTLSamplerDescriptor alloc] init];
samplerDesc.minFilter = (info.minFilter == GN_FILTER_NEAREST) ? MTLSamplerMinMagFilterNearest : MTLSamplerMinMagFilterLinear; samplerDesc.minFilter = (info.minFilter == GN_FILTER_NEAREST) ? MTLSamplerMinMagFilterNearest : MTLSamplerMinMagFilterLinear;

View File

@@ -2,10 +2,12 @@
#include "output_device/gryphn_output_device.h" #include "output_device/gryphn_output_device.h"
#include "instance/gryphn_instance.h" #include "instance/gryphn_instance.h"
#include "stdio.h"
gnReturnCode gnCreateRenderPassDescriptor(gnRenderPassDescriptorHandle* renderPass, gnOutputDeviceHandle device, gnRenderPassDescriptorInfo info) { gnReturnCode gnCreateRenderPassDescriptor(gnRenderPassDescriptorHandle* renderPass, gnOutputDeviceHandle device, gnRenderPassDescriptorInfo info) {
*renderPass = malloc(sizeof(struct gnRenderPassDescriptor_t)); *renderPass = malloc(sizeof(struct gnRenderPassDescriptor_t));
(*renderPass)->device = device; (*renderPass)->device = device;
(*renderPass)->info = info;
return device->instance->callingLayer->deviceFunctions._gnCreateRenderPassDescriptor(*renderPass, device, info); return device->instance->callingLayer->deviceFunctions._gnCreateRenderPassDescriptor(*renderPass, device, info);
} }

View File

@@ -76,7 +76,6 @@ typedef struct gnRenderPassDescriptorInfo {
#ifdef GN_REVEAL_IMPL #ifdef GN_REVEAL_IMPL
struct gnRenderPassDescriptor_t { struct gnRenderPassDescriptor_t {
struct gnPlatformRenderPassDescriptor_t* renderPassDescriptor; struct gnPlatformRenderPassDescriptor_t* renderPassDescriptor;
gnRenderPassDescriptorInfo info;
gnDeviceHandle device; gnDeviceHandle device;
}; };
#endif #endif