Helper to create a generic buffer with the specified size, usage and memory flags
| 1275 | |
| 1276 | // Helper to create a generic buffer with the specified size, usage and memory flags |
| 1277 | bool createBuffer(VkDeviceSize size, |
| 1278 | VkBufferUsageFlags usage, |
| 1279 | VkMemoryPropertyFlags properties, |
| 1280 | VkBuffer& buffer, |
| 1281 | VkDeviceMemory& memory) |
| 1282 | { |
| 1283 | // We only have a single queue so we can request exclusive access |
| 1284 | VkBufferCreateInfo bufferCreateInfo = VkBufferCreateInfo(); |
| 1285 | bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 1286 | bufferCreateInfo.size = size; |
| 1287 | bufferCreateInfo.usage = usage; |
| 1288 | bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 1289 | |
| 1290 | // Create the buffer, this does not allocate any memory for it yet |
| 1291 | if (vkCreateBuffer(device, &bufferCreateInfo, nullptr, &buffer) != VK_SUCCESS) |
| 1292 | return false; |
| 1293 | |
| 1294 | // Check what kind of memory we need to request from the GPU |
| 1295 | VkMemoryRequirements memoryRequirements = VkMemoryRequirements(); |
| 1296 | vkGetBufferMemoryRequirements(device, buffer, &memoryRequirements); |
| 1297 | |
| 1298 | // Check what GPU memory type is available for us to allocate out of |
| 1299 | VkPhysicalDeviceMemoryProperties memoryProperties = VkPhysicalDeviceMemoryProperties(); |
| 1300 | vkGetPhysicalDeviceMemoryProperties(gpu, &memoryProperties); |
| 1301 | |
| 1302 | std::uint32_t memoryType = 0; |
| 1303 | |
| 1304 | for (; memoryType < memoryProperties.memoryTypeCount; ++memoryType) |
| 1305 | { |
| 1306 | if ((memoryRequirements.memoryTypeBits & static_cast<unsigned int>(1 << memoryType)) && |
| 1307 | ((memoryProperties.memoryTypes[memoryType].propertyFlags & properties) == properties)) |
| 1308 | break; |
| 1309 | } |
| 1310 | |
| 1311 | if (memoryType == memoryProperties.memoryTypeCount) |
| 1312 | return false; |
| 1313 | |
| 1314 | VkMemoryAllocateInfo memoryAllocateInfo = VkMemoryAllocateInfo(); |
| 1315 | memoryAllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
| 1316 | memoryAllocateInfo.allocationSize = memoryRequirements.size; |
| 1317 | memoryAllocateInfo.memoryTypeIndex = memoryType; |
| 1318 | |
| 1319 | // Allocate the memory out of the GPU pool for the required memory type |
| 1320 | if (vkAllocateMemory(device, &memoryAllocateInfo, nullptr, &memory) != VK_SUCCESS) |
| 1321 | return false; |
| 1322 | |
| 1323 | // Bind the allocated memory to our buffer object |
| 1324 | if (vkBindBufferMemory(device, buffer, memory, 0) != VK_SUCCESS) |
| 1325 | return false; |
| 1326 | |
| 1327 | return true; |
| 1328 | } |
| 1329 | |
| 1330 | // Helper to copy the contents of one buffer to another buffer |
| 1331 | bool copyBuffer(VkBuffer dst, VkBuffer src, VkDeviceSize size) |
nothing calls this directly
no test coverage detected