source: opengl-game/vulkan-utils.cpp@ 87c8f1a

feature/imgui-sdl points-test
Last change on this file since 87c8f1a was 87c8f1a, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 4 years ago

In vaulkangame, define vertex buffer and index buffer data and transfer it to the gpu

  • Property mode set to 100644
File size: 19.2 KB
Line 
1#include "vulkan-utils.hpp"
2
3#include <algorithm>
4#include <set>
5#include <stdexcept>
6
7#define STB_IMAGE_IMPLEMENTATION
8#include "stb_image.h" // TODO: Probably switch to SDL_image
9
10// TODO: Remove all instances of auto
11
12bool VulkanUtils::checkValidationLayerSupport(const vector<const char*> &validationLayers) {
13 uint32_t layerCount;
14 vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
15
16 vector<VkLayerProperties> availableLayers(layerCount);
17 vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
18
19 for (const char* layerName : validationLayers) {
20 bool layerFound = false;
21
22 for (const auto& layerProperties : availableLayers) {
23 if (strcmp(layerName, layerProperties.layerName) == 0) {
24 layerFound = true;
25 break;
26 }
27 }
28
29 if (!layerFound) {
30 return false;
31 }
32 }
33
34 return true;
35}
36
37VkResult VulkanUtils::createDebugUtilsMessengerEXT(VkInstance instance,
38 const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
39 const VkAllocationCallbacks* pAllocator,
40 VkDebugUtilsMessengerEXT* pDebugMessenger) {
41 auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
42
43 if (func != nullptr) {
44 return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
45 } else {
46 return VK_ERROR_EXTENSION_NOT_PRESENT;
47 }
48}
49
50void VulkanUtils::destroyDebugUtilsMessengerEXT(VkInstance instance,
51 VkDebugUtilsMessengerEXT debugMessenger,
52 const VkAllocationCallbacks* pAllocator) {
53 auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
54
55 if (func != nullptr) {
56 func(instance, debugMessenger, pAllocator);
57 }
58}
59
60QueueFamilyIndices VulkanUtils::findQueueFamilies(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface) {
61 QueueFamilyIndices indices;
62
63 uint32_t queueFamilyCount = 0;
64 vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, nullptr);
65
66 vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
67 vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, queueFamilies.data());
68
69 int i = 0;
70 for (const auto& queueFamily : queueFamilies) {
71 if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
72 indices.graphicsFamily = i;
73 }
74
75 VkBool32 presentSupport = false;
76 vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, i, surface, &presentSupport);
77
78 if (queueFamily.queueCount > 0 && presentSupport) {
79 indices.presentFamily = i;
80 }
81
82 if (indices.isComplete()) {
83 break;
84 }
85
86 i++;
87 }
88
89 return indices;
90}
91
92bool VulkanUtils::checkDeviceExtensionSupport(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions) {
93 uint32_t extensionCount;
94 vkEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &extensionCount, nullptr);
95
96 vector<VkExtensionProperties> availableExtensions(extensionCount);
97 vkEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &extensionCount, availableExtensions.data());
98
99 set<string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
100
101 for (const auto& extension : availableExtensions) {
102 requiredExtensions.erase(extension.extensionName);
103 }
104
105 return requiredExtensions.empty();
106}
107
108SwapChainSupportDetails VulkanUtils::querySwapChainSupport(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface) {
109 SwapChainSupportDetails details;
110
111 vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &details.capabilities);
112
113 uint32_t formatCount;
114 vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, nullptr);
115
116 if (formatCount != 0) {
117 details.formats.resize(formatCount);
118 vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, details.formats.data());
119 }
120
121 uint32_t presentModeCount;
122 vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, nullptr);
123
124 if (presentModeCount != 0) {
125 details.presentModes.resize(presentModeCount);
126 vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, details.presentModes.data());
127 }
128
129 return details;
130}
131
132VkSurfaceFormatKHR VulkanUtils::chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats) {
133 for (const auto& availableFormat : availableFormats) {
134 if (availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
135 return availableFormat;
136 }
137 }
138
139 return availableFormats[0];
140}
141
142VkPresentModeKHR VulkanUtils::chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes) {
143 VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR;
144
145 for (const auto& availablePresentMode : availablePresentModes) {
146 if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
147 return availablePresentMode;
148 }
149 else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
150 bestMode = availablePresentMode;
151 }
152 }
153
154 return bestMode;
155}
156
157VkExtent2D VulkanUtils::chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, int width, int height) {
158 if (capabilities.currentExtent.width != numeric_limits<uint32_t>::max()) {
159 return capabilities.currentExtent;
160 } else {
161 VkExtent2D actualExtent = {
162 static_cast<uint32_t>(width),
163 static_cast<uint32_t>(height)
164 };
165
166 actualExtent.width = std::max(capabilities.minImageExtent.width, std::min(capabilities.maxImageExtent.width, actualExtent.width));
167 actualExtent.height = std::max(capabilities.minImageExtent.height, std::min(capabilities.maxImageExtent.height, actualExtent.height));
168
169 return actualExtent;
170 }
171}
172
173VkImageView VulkanUtils::createImageView(VkDevice device, VkImage image, VkFormat format, VkImageAspectFlags aspectFlags) {
174 VkImageViewCreateInfo viewInfo = {};
175 viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
176 viewInfo.image = image;
177 viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
178 viewInfo.format = format;
179
180 viewInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
181 viewInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
182 viewInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
183 viewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
184
185 viewInfo.subresourceRange.aspectMask = aspectFlags;
186 viewInfo.subresourceRange.baseMipLevel = 0;
187 viewInfo.subresourceRange.levelCount = 1;
188 viewInfo.subresourceRange.baseArrayLayer = 0;
189 viewInfo.subresourceRange.layerCount = 1;
190
191 VkImageView imageView;
192 if (vkCreateImageView(device, &viewInfo, nullptr, &imageView) != VK_SUCCESS) {
193 throw runtime_error("failed to create image view!");
194 }
195
196 return imageView;
197}
198
199VkFormat VulkanUtils::findSupportedFormat(VkPhysicalDevice physicalDevice, const vector<VkFormat>& candidates,
200 VkImageTiling tiling, VkFormatFeatureFlags features) {
201 for (VkFormat format : candidates) {
202 VkFormatProperties props;
203 vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &props);
204
205 if (tiling == VK_IMAGE_TILING_LINEAR &&
206 (props.linearTilingFeatures & features) == features) {
207 return format;
208 } else if (tiling == VK_IMAGE_TILING_OPTIMAL &&
209 (props.optimalTilingFeatures & features) == features) {
210 return format;
211 }
212 }
213
214 throw runtime_error("failed to find supported format!");
215}
216
217void VulkanUtils::createBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VkDeviceSize size, VkBufferUsageFlags usage,
218 VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory) {
219 VkBufferCreateInfo bufferInfo = {};
220 bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
221 bufferInfo.size = size;
222 bufferInfo.usage = usage;
223 bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
224
225 if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS) {
226 throw runtime_error("failed to create buffer!");
227 }
228
229 VkMemoryRequirements memRequirements;
230 vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
231
232 VkMemoryAllocateInfo allocInfo = {};
233 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
234 allocInfo.allocationSize = memRequirements.size;
235 allocInfo.memoryTypeIndex = findMemoryType(physicalDevice, memRequirements.memoryTypeBits, properties);
236
237 if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) {
238 throw runtime_error("failed to allocate buffer memory!");
239 }
240
241 vkBindBufferMemory(device, buffer, bufferMemory, 0);
242}
243
244uint32_t VulkanUtils::findMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter, VkMemoryPropertyFlags properties) {
245 VkPhysicalDeviceMemoryProperties memProperties;
246 vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
247
248 for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
249 if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
250 return i;
251 }
252 }
253
254 throw runtime_error("failed to find suitable memory type!");
255}
256
257void VulkanUtils::createVulkanImageFromFile(VkDevice device, VkPhysicalDevice physicalDevice,
258 VkCommandPool commandPool, string filename, VulkanImage& image, VkQueue graphicsQueue) {
259 int texWidth, texHeight, texChannels;
260
261 stbi_uc* pixels = stbi_load(filename.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
262 VkDeviceSize imageSize = texWidth * texHeight * 4;
263
264 if (!pixels) {
265 throw runtime_error("failed to load texture image!");
266 }
267
268 VkBuffer stagingBuffer;
269 VkDeviceMemory stagingBufferMemory;
270
271 createBuffer(device, physicalDevice, imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
272 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
273 stagingBuffer, stagingBufferMemory);
274
275 void* data;
276
277 vkMapMemory(device, stagingBufferMemory, 0, imageSize, 0, &data);
278 memcpy(data, pixels, static_cast<size_t>(imageSize));
279 vkUnmapMemory(device, stagingBufferMemory);
280
281 stbi_image_free(pixels);
282
283 createImage(device, physicalDevice, texWidth, texHeight, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL,
284 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, image);
285
286 transitionImageLayout(device, commandPool, image.image, VK_FORMAT_R8G8B8A8_UNORM,
287 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, graphicsQueue);
288 copyBufferToImage(device, commandPool, stagingBuffer, image.image,
289 static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight), graphicsQueue);
290 transitionImageLayout(device, commandPool, image.image, VK_FORMAT_R8G8B8A8_UNORM,
291 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, graphicsQueue);
292
293 vkDestroyBuffer(device, stagingBuffer, nullptr);
294 vkFreeMemory(device, stagingBufferMemory, nullptr);
295
296 image.imageView = createImageView(device, image.image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
297}
298
299void VulkanUtils::createVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice,
300 SDL_Texture* texture, VulkanImage& image) {
301 int a, w, h;
302
303 // I only need this here for the width and height, which are constants, so just use those instead
304 SDL_QueryTexture(texture, nullptr, &a, &w, &h);
305
306 createImage(device, physicalDevice, w, h, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL,
307 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, image);
308
309 image.imageView = createImageView(device, image.image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
310}
311
312void VulkanUtils::createDepthImage(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool,
313 VkFormat depthFormat, VkExtent2D extent, VulkanImage& image, VkQueue graphicsQueue) {
314 createImage(device, physicalDevice, extent.width, extent.height, depthFormat, VK_IMAGE_TILING_OPTIMAL,
315 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, image);
316 image.imageView = createImageView(device, image.image, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
317
318 transitionImageLayout(device, commandPool, image.image, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED,
319 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, graphicsQueue);
320}
321
322void VulkanUtils::createImage(VkDevice device, VkPhysicalDevice physicalDevice, uint32_t width, uint32_t height,
323 VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties,
324 VulkanImage& image) {
325 VkImageCreateInfo imageInfo = {};
326 imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
327 imageInfo.imageType = VK_IMAGE_TYPE_2D;
328 imageInfo.extent.width = width;
329 imageInfo.extent.height = height;
330 imageInfo.extent.depth = 1;
331 imageInfo.mipLevels = 1;
332 imageInfo.arrayLayers = 1;
333 imageInfo.format = format;
334 imageInfo.tiling = tiling;
335 imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
336 imageInfo.usage = usage;
337 imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
338 imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
339
340 if (vkCreateImage(device, &imageInfo, nullptr, &image.image) != VK_SUCCESS) {
341 throw runtime_error("failed to create image!");
342 }
343
344 VkMemoryRequirements memRequirements;
345 vkGetImageMemoryRequirements(device, image.image, &memRequirements);
346
347 VkMemoryAllocateInfo allocInfo = {};
348 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
349 allocInfo.allocationSize = memRequirements.size;
350 allocInfo.memoryTypeIndex = findMemoryType(physicalDevice, memRequirements.memoryTypeBits, properties);
351
352 if (vkAllocateMemory(device, &allocInfo, nullptr, &image.imageMemory) != VK_SUCCESS) {
353 throw runtime_error("failed to allocate image memory!");
354 }
355
356 vkBindImageMemory(device, image.image, image.imageMemory, 0);
357}
358
359void VulkanUtils::transitionImageLayout(VkDevice device, VkCommandPool commandPool, VkImage image,
360 VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout, VkQueue graphicsQueue) {
361 VkCommandBuffer commandBuffer = beginSingleTimeCommands(device, commandPool);
362
363 VkImageMemoryBarrier barrier = {};
364 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
365 barrier.oldLayout = oldLayout;
366 barrier.newLayout = newLayout;
367 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
368 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
369 barrier.image = image;
370
371 if (newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
372 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
373
374 if (hasStencilComponent(format)) {
375 barrier.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
376 }
377 } else {
378 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
379 }
380
381 barrier.subresourceRange.baseMipLevel = 0;
382 barrier.subresourceRange.levelCount = 1;
383 barrier.subresourceRange.baseArrayLayer = 0;
384 barrier.subresourceRange.layerCount = 1;
385
386 VkPipelineStageFlags sourceStage;
387 VkPipelineStageFlags destinationStage;
388
389 if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
390 barrier.srcAccessMask = 0;
391 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
392
393 sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
394 destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
395 } else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
396 barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
397 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
398
399 sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
400 destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
401 } else if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
402 barrier.srcAccessMask = 0;
403 barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
404
405 sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
406 destinationStage = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
407 } else {
408 throw invalid_argument("unsupported layout transition!");
409 }
410
411 vkCmdPipelineBarrier(
412 commandBuffer,
413 sourceStage, destinationStage,
414 0,
415 0, nullptr,
416 0, nullptr,
417 1, &barrier
418 );
419
420 endSingleTimeCommands(device, commandPool, commandBuffer, graphicsQueue);
421}
422
423VkCommandBuffer VulkanUtils::beginSingleTimeCommands(VkDevice device, VkCommandPool commandPool) {
424 VkCommandBufferAllocateInfo allocInfo = {};
425 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
426 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
427 allocInfo.commandPool = commandPool;
428 allocInfo.commandBufferCount = 1;
429
430 VkCommandBuffer commandBuffer;
431 vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer);
432
433 VkCommandBufferBeginInfo beginInfo = {};
434 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
435 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
436
437 vkBeginCommandBuffer(commandBuffer, &beginInfo);
438
439 return commandBuffer;
440}
441
442void VulkanUtils::endSingleTimeCommands(VkDevice device, VkCommandPool commandPool,
443 VkCommandBuffer commandBuffer, VkQueue graphicsQueue) {
444 vkEndCommandBuffer(commandBuffer);
445
446 VkSubmitInfo submitInfo = {};
447 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
448 submitInfo.commandBufferCount = 1;
449 submitInfo.pCommandBuffers = &commandBuffer;
450
451 vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
452 vkQueueWaitIdle(graphicsQueue);
453
454 vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
455}
456
457void VulkanUtils::copyBufferToImage(VkDevice device, VkCommandPool commandPool, VkBuffer buffer,
458 VkImage image, uint32_t width, uint32_t height, VkQueue graphicsQueue) {
459 VkCommandBuffer commandBuffer = beginSingleTimeCommands(device, commandPool);
460
461 VkBufferImageCopy region = {};
462 region.bufferOffset = 0;
463 region.bufferRowLength = 0;
464 region.bufferImageHeight = 0;
465 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
466 region.imageSubresource.mipLevel = 0;
467 region.imageSubresource.baseArrayLayer = 0;
468 region.imageSubresource.layerCount = 1;
469 region.imageOffset = { 0, 0, 0 };
470 region.imageExtent = { width, height, 1 };
471
472 vkCmdCopyBufferToImage(
473 commandBuffer,
474 buffer,
475 image,
476 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
477 1,
478 &region
479 );
480
481 endSingleTimeCommands(device, commandPool, commandBuffer, graphicsQueue);
482}
483
484void VulkanUtils::copyBuffer(VkDevice device, VkCommandPool commandPool, VkBuffer srcBuffer,
485 VkBuffer dstBuffer, VkDeviceSize srcOffset, VkDeviceSize dstOffset, VkDeviceSize size,
486 VkQueue graphicsQueue) {
487 VkCommandBuffer commandBuffer = beginSingleTimeCommands(device, commandPool);
488
489 VkBufferCopy copyRegion = { srcOffset, dstOffset, size };
490 vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, &copyRegion);
491
492 endSingleTimeCommands(device, commandPool, commandBuffer, graphicsQueue);
493}
494
495bool VulkanUtils::hasStencilComponent(VkFormat format) {
496 return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT;
497}
498
499void VulkanUtils::destroyVulkanImage(VkDevice& device, VulkanImage& image) {
500 vkDestroyImageView(device, image.imageView, nullptr);
501 vkDestroyImage(device, image.image, nullptr);
502 vkFreeMemory(device, image.imageMemory, nullptr);
503}
Note: See TracBrowser for help on using the repository browser.