SimpleGL  1.1.0
A framework for platform independent rendering
render_pass.cpp
Go to the documentation of this file.
1 
9 
10 #include <stdexcept>
11 #include <vector>
12 
15 
16 using namespace std;
17 
18 namespace sgl {
19 
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();
24 
25  /* Initialize depth stencil format candidates */
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};
28 
29  /* Find a supported format */
30  for (uint32_t i = 0; i < candidates.size(); i++) {
31  depthStencilFormat = candidates[i];
32 
33  /* Get physical device format properties */
34  VkFormatProperties formatProperties;
35  vkGetPhysicalDeviceFormatProperties(physicalDevice, depthStencilFormat, &formatProperties);
36 
37  /* Check if it is supported */
38  if (formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) {
39  break;
40  } else if (formatProperties.linearTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) {
41  break;
42  }
43  }
44  if (depthStencilFormat == VK_FORMAT_UNDEFINED) {
45  throw runtime_error("Could not find a supported depth stencil format!");
46  }
47 
48  /* Initialize the color attachment description */
49  VkAttachmentDescription colorAttachment = {};
50  colorAttachment.format = swapchainData.imageFormat;
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;
58 
59  /* Initialize the color attachment reference */
60  VkAttachmentReference colorAttachmentReference = {};
61  colorAttachmentReference.attachment = 0;
62  colorAttachmentReference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
63 
64  /* Initialize a depth stencil attachment description */
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;
74 
75  /* Initialize the depth stencil attachment reference */
76  VkAttachmentReference depthStencilAttachmentReference = {};
77  depthStencilAttachmentReference.attachment = 1;
78  depthStencilAttachmentReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
79 
80  /* Initialize a subpass description */
81  VkSubpassDescription subpass = {};
82  subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
83  subpass.colorAttachmentCount = 1;
84  subpass.pColorAttachments = &colorAttachmentReference;
85  subpass.pDepthStencilAttachment = &depthStencilAttachmentReference;
86 
87  /* Initialize a subpass dependency */
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;
95 
96  /* Initialize the attachments vector */
97  vector<VkAttachmentDescription> attachments = {colorAttachment, depthStencilAttachment};
98 
99  /* Initialize a render pass create info */
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;
108 
109  /* Create the render pass */
110  if (vkCreateRenderPass(device, &createInfo, nullptr, &handle) != VK_SUCCESS) {
111  throw runtime_error("Could not create a render pass!");
112  }
113  }
114 
115  RenderPass::~RenderPass(void) {
116  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
117 
118  /* Destroy the render pass */
119  vkDestroyRenderPass(device, handle, nullptr);
120  }
121 
122  VkRenderPass RenderPass::getHandle() {
123  return handle;
124  }
125 }
Generic namespace for the SimpleGL framework.
Definition: application.hpp:18
This struct stores relevant swapchain data.
Definition: swapchain.hpp:28
VkFormat imageFormat
The swapchain image format.
Definition: swapchain.hpp:37