| 6421 | } |
| 6422 | |
| 6423 | static void TestAdvancedDataUploading() { |
| 6424 | wprintf(L"Testing advanced data uploading...\n"); |
| 6425 | |
| 6426 | // Generate some random data to fill the uniform buffer with. |
| 6427 | const VkDeviceSize bufferSize = 65536; |
| 6428 | std::vector<std::uint8_t> bufferData(bufferSize); |
| 6429 | for (auto& bufferByte : bufferData) { |
| 6430 | bufferByte = static_cast<std::uint8_t>(rand() % 256); |
| 6431 | } |
| 6432 | |
| 6433 | VkBufferCreateInfo uniformBufferCI = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; |
| 6434 | uniformBufferCI.size = bufferSize; |
| 6435 | uniformBufferCI.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; // Change this if you want to create another type of buffer. |
| 6436 | |
| 6437 | VmaAllocationCreateInfo uniformBufferAllocCI = {}; |
| 6438 | uniformBufferAllocCI.usage = VMA_MEMORY_USAGE_AUTO; |
| 6439 | uniformBufferAllocCI.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT |
| 6440 | | VMA_ALLOCATION_CREATE_MAPPED_BIT; |
| 6441 | |
| 6442 | VkBuffer uniformBuffer = VK_NULL_HANDLE; |
| 6443 | VmaAllocation uniformBufferAlloc = {}; |
| 6444 | VmaAllocationInfo uniformBufferAllocInfo = {}; |
| 6445 | |
| 6446 | VkResult result = vmaCreateBuffer(g_hAllocator, &uniformBufferCI, &uniformBufferAllocCI, &uniformBuffer, &uniformBufferAlloc, &uniformBufferAllocInfo); |
| 6447 | TEST(result == VK_SUCCESS); |
| 6448 | |
| 6449 | VkMemoryPropertyFlags memPropFlags; |
| 6450 | vmaGetAllocationMemoryProperties(g_hAllocator, uniformBufferAlloc, &memPropFlags); |
| 6451 | |
| 6452 | if (memPropFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { |
| 6453 | // The allocation ended up as mapped memory. |
| 6454 | TEST(uniformBufferAllocInfo.pMappedData != nullptr); |
| 6455 | result = vmaCopyMemoryToAllocation(g_hAllocator, bufferData.data(), uniformBufferAlloc, 0, bufferData.size()); |
| 6456 | TEST(result == VK_SUCCESS); |
| 6457 | |
| 6458 | BeginSingleTimeCommands(); |
| 6459 | |
| 6460 | VkBufferMemoryBarrier bufferMemBarrier = { VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER }; |
| 6461 | bufferMemBarrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT; |
| 6462 | bufferMemBarrier.dstAccessMask = VK_ACCESS_UNIFORM_READ_BIT; // Change this if you want to create another type of buffer. |
| 6463 | bufferMemBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 6464 | bufferMemBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 6465 | bufferMemBarrier.buffer = uniformBuffer; |
| 6466 | bufferMemBarrier.offset = 0; |
| 6467 | bufferMemBarrier.size = VK_WHOLE_SIZE; |
| 6468 | |
| 6469 | vkCmdPipelineBarrier(g_hTemporaryCommandBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT, 0, 0, nullptr, 1, &bufferMemBarrier, 0, nullptr); |
| 6470 | |
| 6471 | EndSingleTimeCommands(); |
| 6472 | } |
| 6473 | else { |
| 6474 | // The allocation did not end up in mapped memory, so we need a staging buffer and a copy operation to update it. |
| 6475 | VkBufferCreateInfo stagingBufferCI = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; |
| 6476 | stagingBufferCI.size = bufferSize; |
| 6477 | stagingBufferCI.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; |
| 6478 | |
| 6479 | VmaAllocationCreateInfo stagingBufferAllocCI = {}; |
| 6480 | stagingBufferAllocCI.usage = VMA_MEMORY_USAGE_AUTO; |
no test coverage detected