Implemented MSAA

This commit is contained in:
Lorenzo Torres 2025-08-10 15:53:26 +02:00
parent 444a4586ea
commit 83af358d30
6 changed files with 123 additions and 30 deletions

View file

@ -150,17 +150,19 @@ pub const RenderPass = struct {
const Self = @This();
pub fn init(allocator: Allocator, device: Device, surface: Surface, physical_device: PhysicalDevice) !Self {
const swapchain_format = (try Swapchain.pickFormat(allocator, surface, physical_device)).format;
const depth_image, const depth_view , const depth_memory, const depth_format = try createDepthResources(device, physical_device);
const color_attachment: c.VkAttachmentDescription = .{
.format = (try Swapchain.pickFormat(allocator, surface, physical_device)).format,
.samples = c.VK_SAMPLE_COUNT_1_BIT,
.format = swapchain_format,
.samples = device.msaa_samples,
.loadOp = c.VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = c.VK_ATTACHMENT_STORE_OP_STORE,
.stencilLoadOp = c.VK_ATTACHMENT_LOAD_OP_DONT_CARE,
.stencilStoreOp = c.VK_ATTACHMENT_STORE_OP_DONT_CARE,
.initialLayout = c.VK_IMAGE_LAYOUT_UNDEFINED,
.finalLayout = c.VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
.finalLayout = c.VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
};
const color_attachment_reference: c.VkAttachmentReference = .{
@ -168,9 +170,25 @@ pub const RenderPass = struct {
.layout = c.VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
};
const color_attachment_resolve: c.VkAttachmentDescription = .{
.format = swapchain_format,
.samples = c.VK_SAMPLE_COUNT_1_BIT,
.loadOp = c.VK_ATTACHMENT_LOAD_OP_DONT_CARE,
.storeOp = c.VK_ATTACHMENT_STORE_OP_DONT_CARE,
.stencilLoadOp = c.VK_ATTACHMENT_LOAD_OP_DONT_CARE,
.stencilStoreOp = c.VK_ATTACHMENT_STORE_OP_DONT_CARE,
.initialLayout = c.VK_IMAGE_LAYOUT_UNDEFINED,
.finalLayout = c.VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
};
const color_attachment_resolve_reference: c.VkAttachmentReference = .{
.attachment = 2,
.layout = c.VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
};
const depth_attachment: c.VkAttachmentDescription = .{
.format = depth_format,
.samples = c.VK_SAMPLE_COUNT_1_BIT,
.samples = device.msaa_samples,
.loadOp = c.VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = c.VK_ATTACHMENT_STORE_OP_DONT_CARE,
.stencilLoadOp = c.VK_ATTACHMENT_LOAD_OP_DONT_CARE,
@ -189,22 +207,23 @@ pub const RenderPass = struct {
.colorAttachmentCount = 1,
.pColorAttachments = &color_attachment_reference,
.pDepthStencilAttachment = &depth_attachment_reference,
.pResolveAttachments = &color_attachment_resolve_reference,
};
const dependency: c.VkSubpassDependency = .{
.srcSubpass = c.VK_SUBPASS_EXTERNAL,
.dstSubpass = 0,
.srcStageMask = c.VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | c.VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
.srcAccessMask = c.VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
.srcAccessMask = c.VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | c.VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
.dstStageMask = c.VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | c.VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
.dstAccessMask = c.VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | c.VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
};
const attachments = &[_]c.VkAttachmentDescription { color_attachment, depth_attachment };
const attachments = &[_]c.VkAttachmentDescription { color_attachment, depth_attachment, color_attachment_resolve };
const render_pass_info: c.VkRenderPassCreateInfo = .{
.sType = c.VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
.attachmentCount = 2,
.attachmentCount = 3,
.pAttachments = attachments[0..].ptr,
.subpassCount = 1,
.pSubpasses = &subpass,
@ -289,7 +308,7 @@ pub const RenderPass = struct {
.initialLayout = c.VK_IMAGE_LAYOUT_UNDEFINED,
.usage = c.VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
.sharingMode = c.VK_SHARING_MODE_EXCLUSIVE,
.samples = c.VK_SAMPLE_COUNT_1_BIT,
.samples = device.msaa_samples,
.flags = 0,
};