Helper to create a generic image with the specified size, format, usage and memory flags
| 1586 | |
| 1587 | // Helper to create a generic image with the specified size, format, usage and memory flags |
| 1588 | bool createImage(std::uint32_t width, |
| 1589 | std::uint32_t height, |
| 1590 | VkFormat format, |
| 1591 | VkImageTiling tiling, |
| 1592 | VkImageUsageFlags usage, |
| 1593 | VkMemoryPropertyFlags properties, |
| 1594 | VkImage& image, |
| 1595 | VkDeviceMemory& imageMemory) |
| 1596 | { |
| 1597 | // We only have a single queue so we can request exclusive access |
| 1598 | VkImageCreateInfo imageCreateInfo = VkImageCreateInfo(); |
| 1599 | imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; |
| 1600 | imageCreateInfo.imageType = VK_IMAGE_TYPE_2D; |
| 1601 | imageCreateInfo.extent.width = width; |
| 1602 | imageCreateInfo.extent.height = height; |
| 1603 | imageCreateInfo.extent.depth = 1; |
| 1604 | imageCreateInfo.mipLevels = 1; |
| 1605 | imageCreateInfo.arrayLayers = 1; |
| 1606 | imageCreateInfo.format = format; |
| 1607 | imageCreateInfo.tiling = tiling; |
| 1608 | imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 1609 | imageCreateInfo.usage = usage; |
| 1610 | imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT; |
| 1611 | imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 1612 | |
| 1613 | // Create the image, this does not allocate any memory for it yet |
| 1614 | if (vkCreateImage(device, &imageCreateInfo, nullptr, &image) != VK_SUCCESS) |
| 1615 | return false; |
| 1616 | |
| 1617 | // Check what kind of memory we need to request from the GPU |
| 1618 | VkMemoryRequirements memoryRequirements = VkMemoryRequirements(); |
| 1619 | vkGetImageMemoryRequirements(device, image, &memoryRequirements); |
| 1620 | |
| 1621 | // Check what GPU memory type is available for us to allocate out of |
| 1622 | VkPhysicalDeviceMemoryProperties memoryProperties = VkPhysicalDeviceMemoryProperties(); |
| 1623 | vkGetPhysicalDeviceMemoryProperties(gpu, &memoryProperties); |
| 1624 | |
| 1625 | std::uint32_t memoryType = 0; |
| 1626 | |
| 1627 | for (; memoryType < memoryProperties.memoryTypeCount; ++memoryType) |
| 1628 | { |
| 1629 | if ((memoryRequirements.memoryTypeBits & static_cast<unsigned int>(1 << memoryType)) && |
| 1630 | ((memoryProperties.memoryTypes[memoryType].propertyFlags & properties) == properties)) |
| 1631 | break; |
| 1632 | } |
| 1633 | |
| 1634 | if (memoryType == memoryProperties.memoryTypeCount) |
| 1635 | return false; |
| 1636 | |
| 1637 | VkMemoryAllocateInfo memoryAllocateInfo = VkMemoryAllocateInfo(); |
| 1638 | memoryAllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
| 1639 | memoryAllocateInfo.allocationSize = memoryRequirements.size; |
| 1640 | memoryAllocateInfo.memoryTypeIndex = memoryType; |
| 1641 | |
| 1642 | // Allocate the memory out of the GPU pool for the required memory type |
| 1643 | if (vkAllocateMemory(device, &memoryAllocateInfo, nullptr, &imageMemory) != VK_SUCCESS) |
| 1644 | return false; |
| 1645 |
nothing calls this directly
no test coverage detected