Upload parsed geometry to device-local GPU buffers.
1150 {
1151 if (device == VK_NULL_HANDLE || physicalDevice == VK_NULL_HANDLE ||
1152 commandPool == VK_NULL_HANDLE || graphicsQueue == VK_NULL_HANDLE) {
1153 throw mxvk::Exception("MXModel::upload requires valid Vulkan handles");
1154 }
1155 if (verticesData.empty() || indicesData.empty()) {
1156 throw mxvk::Exception("MXModel::upload requires loaded geometry");
1157 }
1158
1160
1162
1163 const VkDeviceSize vertexBufferSize = sizeof(VKVertex) * verticesData.size();
1164 const VkDeviceSize indexBufferSize = sizeof(uint32_t) * indicesData.size();
1165
1166 VkBuffer stagingVertexBuffer = VK_NULL_HANDLE;
1167 VkDeviceMemory stagingVertexMemory = VK_NULL_HANDLE;
1168 createBuffer(device, physicalDevice, vertexBufferSize,
1169 VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1170 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1171 stagingVertexBuffer, stagingVertexMemory);
1172
1173 VkBuffer stagingIndexBuffer = VK_NULL_HANDLE;
1174 VkDeviceMemory stagingIndexMemory = VK_NULL_HANDLE;
1175 createBuffer(device, physicalDevice, indexBufferSize,
1176 VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1177 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1178 stagingIndexBuffer, stagingIndexMemory);
1179
1180 void *vertexData = nullptr;
1181 vkMapMemory(device, stagingVertexMemory, 0, vertexBufferSize, 0, &vertexData);
1182 std::memcpy(vertexData, verticesData.data(), static_cast<size_t>(vertexBufferSize));
1183 vkUnmapMemory(device, stagingVertexMemory);
1184
1185 void *indexData = nullptr;
1186 vkMapMemory(device, stagingIndexMemory, 0, indexBufferSize, 0, &indexData);
1187 std::memcpy(indexData, indicesData.data(), static_cast<size_t>(indexBufferSize));
1188 vkUnmapMemory(device, stagingIndexMemory);
1189
1190 createBuffer(device, physicalDevice, vertexBufferSize,
1191 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
1192 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
1193 vertexBufferHandle, vertexBufferMemory);
1194
1195 createBuffer(device, physicalDevice, indexBufferSize,
1196 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
1197 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
1198 indexBufferHandle, indexBufferMemory);
1199
1200 copyBuffer(device, commandPool, graphicsQueue, stagingVertexBuffer, vertexBufferHandle, vertexBufferSize);
1201 copyBuffer(device, commandPool, graphicsQueue, stagingIndexBuffer, indexBufferHandle, indexBufferSize);
1202
1203 vkDestroyBuffer(device, stagingVertexBuffer, nullptr);
1204 vkFreeMemory(device, stagingVertexMemory, nullptr);
1205 vkDestroyBuffer(device, stagingIndexBuffer, nullptr);
1206 vkFreeMemory(device, stagingIndexMemory, nullptr);
1207
1209 }
void cleanup(VkDevice device)
Release owned GPU buffers.