From 6a3f0911908b3508c89e55109c10a6f03ab6e5f4 Mon Sep 17 00:00:00 2001 From: "re.kovalev" Date: Thu, 3 Mar 2022 15:52:20 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=8B=D0=B4=D0=B5=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BF=D0=B0=D0=BC=D1=8F=D1=82=D0=B8=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B4=20=D0=B1=D1=83=D1=84=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/vk.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) 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); }