20 RenderPass::RenderPass(
void) {
21 VkPhysicalDevice physicalDevice = VulkanContext::getCurrent()->getPhysicalDevice()->getHandle();
22 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
23 SwapchainData swapchainData = VulkanContext::getCurrent()->getSwapchain()->getSwapchainData();
26 VkFormat depthStencilFormat = VK_FORMAT_UNDEFINED;
27 vector<VkFormat> candidates = {VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT, VK_FORMAT_D16_UNORM_S8_UINT};
30 for (uint32_t i = 0; i < candidates.size(); i++) {
31 depthStencilFormat = candidates[i];
34 VkFormatProperties formatProperties;
35 vkGetPhysicalDeviceFormatProperties(physicalDevice, depthStencilFormat, &formatProperties);
38 if (formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) {
40 }
else if (formatProperties.linearTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) {
44 if (depthStencilFormat == VK_FORMAT_UNDEFINED) {
45 throw runtime_error(
"Could not find a supported depth stencil format!");
49 VkAttachmentDescription colorAttachment = {};
51 colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
52 colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
53 colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
54 colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
55 colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
56 colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
57 colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
60 VkAttachmentReference colorAttachmentReference = {};
61 colorAttachmentReference.attachment = 0;
62 colorAttachmentReference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
65 VkAttachmentDescription depthStencilAttachment = {};
66 depthStencilAttachment.format = depthStencilFormat;
67 depthStencilAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
68 depthStencilAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
69 depthStencilAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
70 depthStencilAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
71 depthStencilAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
72 depthStencilAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
73 depthStencilAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
76 VkAttachmentReference depthStencilAttachmentReference = {};
77 depthStencilAttachmentReference.attachment = 1;
78 depthStencilAttachmentReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
81 VkSubpassDescription subpass = {};
82 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
83 subpass.colorAttachmentCount = 1;
84 subpass.pColorAttachments = &colorAttachmentReference;
85 subpass.pDepthStencilAttachment = &depthStencilAttachmentReference;
88 VkSubpassDependency dependency = {};
89 dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
90 dependency.dstSubpass = 0;
91 dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
92 dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
93 dependency.srcAccessMask = 0;
94 dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
97 vector<VkAttachmentDescription> attachments = {colorAttachment, depthStencilAttachment};
100 VkRenderPassCreateInfo createInfo = {};
101 createInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
102 createInfo.attachmentCount = attachments.size();
103 createInfo.pAttachments = attachments.data();
104 createInfo.subpassCount = 1;
105 createInfo.pSubpasses = &subpass;
106 createInfo.dependencyCount = 1;
107 createInfo.pDependencies = &dependency;
110 if (vkCreateRenderPass(device, &createInfo,
nullptr, &handle) != VK_SUCCESS) {
111 throw runtime_error(
"Could not create a render pass!");
115 RenderPass::~RenderPass(
void) {
116 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
119 vkDestroyRenderPass(device, handle,
nullptr);
122 VkRenderPass RenderPass::getHandle() {
Generic namespace for the SimpleGL framework.
This struct stores relevant swapchain data.
VkFormat imageFormat
The swapchain image format.