diff --git a/src/vk.cpp b/src/vk.cpp index b183dd8..69f514a 100644 --- a/src/vk.cpp +++ b/src/vk.cpp @@ -733,4 +733,38 @@ void Vulkan::createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryP { throw std::runtime_error("Unable to create buffer"); } + + // Требования к памяти + VkMemoryRequirements memRequirements; + vkGetBufferMemoryRequirements(logicalDevice, buffer, &memRequirements); + + // Поиск индекса типа подходящей памяти + uint32_t index_memory; + for (index_memory = 0; index_memory < physicalDevice.memory.memoryTypeCount; index_memory++) + { + if ((memRequirements.memoryTypeBits & (1 << index_memory)) + && (physicalDevice.memory.memoryTypes[index_memory].propertyFlags & properties) == properties) + { + break; + } + } + + // Если индекс равен размеру массива - поиск не удался и нужно выдать исключение + if (index_memory == physicalDevice.memory.memoryTypeCount) + throw std::runtime_error("Unable to find suitable memory type"); + + // Информация о выделяемой памяти + VkMemoryAllocateInfo allocInfo{}; + allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; + allocInfo.allocationSize = memRequirements.size; + allocInfo.memoryTypeIndex = index_memory; + + // Выделение памяти + if (vkAllocateMemory(logicalDevice, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) + { + throw std::runtime_error("Unable to allocate buffer memory"); + } + + // Привязка выделенной памяти к буферу + vkBindBufferMemory(logicalDevice, buffer, bufferMemory, 0); }