18-05-26 result

This commit is contained in:
Johanness
2026-05-18 01:26:21 +03:00
parent ea7e73d6a4
commit ad215f32bc
34 changed files with 1616 additions and 395 deletions
+98
View File
@@ -26,6 +26,60 @@ uint32_t findMemoryType(
throw std::runtime_error("Failed to find suitable memory type");
}
uint32_t findMemoryTypeWithPreferred(
VkPhysicalDevice physicalDevice,
uint32_t typeFilter,
VkMemoryPropertyFlags requiredProperties,
VkMemoryPropertyFlags preferredProperties,
VkMemoryPropertyFlags& selectedProperties)
{
VkPhysicalDeviceMemoryProperties memProperties;
vkGetPhysicalDeviceMemoryProperties(
physicalDevice,
&memProperties
);
uint32_t fallbackIndex = UINT32_MAX;
VkMemoryPropertyFlags fallbackProperties = 0;
for (uint32_t i = 0; i < memProperties.memoryTypeCount; ++i)
{
if ((typeFilter & (1 << i)) == 0)
{
continue;
}
const VkMemoryPropertyFlags flags =
memProperties.memoryTypes[i].propertyFlags;
if ((flags & requiredProperties) != requiredProperties)
{
continue;
}
if ((flags & preferredProperties) == preferredProperties)
{
selectedProperties = flags;
return i;
}
if (fallbackIndex == UINT32_MAX)
{
fallbackIndex = i;
fallbackProperties = flags;
}
}
if (fallbackIndex != UINT32_MAX)
{
selectedProperties = fallbackProperties;
return fallbackIndex;
}
throw std::runtime_error("Failed to find suitable memory type");
}
void createBuffer(
VkDevice device,
VkPhysicalDevice physicalDevice,
@@ -66,6 +120,50 @@ void createBuffer(
vkBindBufferMemory(device, buffer, bufferMemory, 0);
}
void createBufferWithPreferredMemory(
VkDevice device,
VkPhysicalDevice physicalDevice,
VkDeviceSize size,
VkBufferUsageFlags usage,
VkMemoryPropertyFlags requiredProperties,
VkMemoryPropertyFlags preferredProperties,
VkBuffer& buffer,
VkDeviceMemory& bufferMemory,
VkMemoryPropertyFlags& selectedProperties)
{
VkBufferCreateInfo bufferInfo{};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = size;
bufferInfo.usage = usage;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS)
{
throw std::runtime_error("Failed to create buffer");
}
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = findMemoryTypeWithPreferred(
physicalDevice,
memRequirements.memoryTypeBits,
requiredProperties,
preferredProperties,
selectedProperties
);
if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS)
{
throw std::runtime_error("Failed to allocate buffer memory");
}
vkBindBufferMemory(device, buffer, bufferMemory, 0);
}
VkCommandBuffer beginSingleTimeCommands(
VkDevice device,
VkCommandPool commandPool)