25 VkCommandPool commandPool, VkDevice device)
29 VkCommandBufferAllocateInfo allocInfo {};
30 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
31 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
32 allocInfo.commandPool = commandPool;
33 allocInfo.commandBufferCount = 1;
35 VkCommandBuffer commandBuffer;
36 vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer);
38 VkCommandBufferBeginInfo beginInfo {};
39 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
40 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
42 vkBeginCommandBuffer(commandBuffer, &beginInfo);
79 VkDeviceSize size, VkBufferUsageFlags usage,
80 VkMemoryPropertyFlags properties, VkBuffer* buffer,
81 VkDeviceMemory* bufferMemory)
83 VkBufferCreateInfo bufferInfo {};
84 bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
85 bufferInfo.size = size;
86 bufferInfo.usage = usage;
87 bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
89 if (vkCreateBuffer(device.
device, &bufferInfo,
nullptr, buffer)
91 throw std::runtime_error(
"failed to create buffer!");
94 VkMemoryRequirements memRequirements;
95 vkGetBufferMemoryRequirements(device.
device, *buffer, &memRequirements);
97 VkMemoryAllocateInfo allocInfo {};
98 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
99 allocInfo.allocationSize = memRequirements.size;
101 memRequirements.memoryTypeBits, properties, device.
physicalDevice);
103 if (vkAllocateMemory(device.
device, &allocInfo,
nullptr, bufferMemory)
105 throw std::runtime_error(
"failed to allocate buffer memory!");
108 vkBindBufferMemory(device.
device, *buffer, *bufferMemory, 0);
127 VkPhysicalDevice device, VkSurfaceKHR surface)
130 uint32_t queueFamilyCount = 0;
131 vkGetPhysicalDeviceQueueFamilyProperties(
132 device, &queueFamilyCount,
nullptr);
134 std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
135 vkGetPhysicalDeviceQueueFamilyProperties(
136 device, &queueFamilyCount, queueFamilies.data());
139 for (
const auto& queueFamily : queueFamilies) {
140 if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
143 VkBool32 presentSupport =
false;
144 vkGetPhysicalDeviceSurfaceSupportKHR(
145 device, i, surface, &presentSupport);
146 if (presentSupport) {
176 SwapChain swapChain, VkImageLayout initalLayout, VkImageLayout finalLayout,
177 VkImageLayout msaaFinalLayout,
bool msaa,
bool clearFramebuffer,
180 VkRenderPass renderPass;
181 VkAttachmentDescription colorAttachment {};
184 if (msaa && device.
msaaSamples != VK_SAMPLE_COUNT_1_BIT) {
187 colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
191 if (clearFramebuffer) {
192 colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
194 colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
196 colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
197 colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
198 colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
199 colorAttachment.initialLayout = initalLayout;
200 colorAttachment.finalLayout
203 VkAttachmentDescription colorAttachmentResolve {};
205 colorAttachmentResolve.samples = VK_SAMPLE_COUNT_1_BIT;
206 colorAttachmentResolve.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
207 colorAttachmentResolve.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
208 colorAttachmentResolve.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
209 colorAttachmentResolve.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
210 colorAttachmentResolve.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
211 colorAttachmentResolve.finalLayout
214 VkAttachmentReference colorAttachmentRef {};
215 colorAttachmentRef.attachment = 0;
216 colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
218 VkAttachmentReference colorAttachmentResolveRef {};
219 colorAttachmentResolveRef.attachment = 1;
220 colorAttachmentResolveRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
222 VkSubpassDescription subpass {};
223 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
224 subpass.colorAttachmentCount = 1;
225 subpass.pColorAttachments = &colorAttachmentRef;
226 if (msaa && device.
msaaSamples != VK_SAMPLE_COUNT_1_BIT) {
227 subpass.pResolveAttachments = &colorAttachmentResolveRef;
229 subpass.pResolveAttachments =
nullptr;
233 VkSubpassDependency subpassDependencies[2];
234 subpassDependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL;
235 subpassDependencies[0].dstSubpass = 0;
236 subpassDependencies[0].srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
237 subpassDependencies[0].dstStageMask
238 = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
239 subpassDependencies[0].srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;
240 subpassDependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT
241 | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
242 subpassDependencies[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
244 subpassDependencies[1].srcSubpass = 0;
245 subpassDependencies[1].dstSubpass = VK_SUBPASS_EXTERNAL;
246 subpassDependencies[1].srcStageMask
247 = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
248 subpassDependencies[1].dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
249 subpassDependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT
250 | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
251 subpassDependencies[1].dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
252 subpassDependencies[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
254 std::vector<VkAttachmentDescription> attachments = { colorAttachment };
255 if (msaa && device.
msaaSamples != VK_SAMPLE_COUNT_1_BIT) {
256 attachments.push_back(colorAttachmentResolve);
259 VkRenderPassCreateInfo renderPassInfo {};
260 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
261 renderPassInfo.attachmentCount =
static_cast<uint32_t
>(attachments.size());
262 renderPassInfo.pAttachments = attachments.data();
263 renderPassInfo.subpassCount = 1;
264 renderPassInfo.pSubpasses = &subpass;
266 renderPassInfo.dependencyCount = 2;
267 renderPassInfo.pDependencies = subpassDependencies;
269 if (vkCreateRenderPass(device.
device, &renderPassInfo,
nullptr, &renderPass)
271 throw std::runtime_error(
"failed to create render pass!");
278 VkRenderPass renderPass,
bool msaa)
280 std::vector<VkFramebuffer> framebuffers;
283 std::vector<VkImageView> attachments;
284 if (msaa && device.
msaaSamples != VK_SAMPLE_COUNT_1_BIT) {
290 VkFramebufferCreateInfo framebufferInfo {};
291 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
292 framebufferInfo.renderPass = renderPass;
293 framebufferInfo.attachmentCount
294 =
static_cast<uint32_t
>(attachments.size());
295 framebufferInfo.pAttachments = attachments.data();
298 framebufferInfo.layers = 1;
299 if (vkCreateFramebuffer(
300 device.
device, &framebufferInfo,
nullptr, &framebuffers[i])
302 throw std::runtime_error(
"failed to create framebuffer!");