Changeset 055750a in opengl-game for vulkan-game.cpp
- Timestamp:
- Dec 4, 2019, 9:33:43 PM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 785333b, cf727ca
- Parents:
- e1308e8
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
re1308e8 r055750a 26 26 framebufferResized = false; 27 27 28 modelMvpMats = {};29 ship MvpMats = {};28 object_VP_mats = {}; 29 ship_VP_mats = {}; 30 30 } 31 31 … … 196 196 modelPipeline.addAttribute(VK_FORMAT_R32G32_SFLOAT, offset_of(&ModelVertex::texCoord)); 197 197 198 createUniformBuffers<UBO_MvpMat>(uniformBuffers, uniformBuffersMemory, uniformBufferInfoList); 198 createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 199 uniformBuffers_scenePipeline, uniformBuffersMemory_scenePipeline, uniformBufferInfoList_scenePipeline); 200 createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 201 storageBuffers_scenePipeline, storageBuffersMemory_scenePipeline, storageBufferInfoList_scenePipeline); 199 202 200 203 modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 201 VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList); 204 VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_scenePipeline); 205 modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 206 VK_SHADER_STAGE_VERTEX_BIT, &storageBufferInfoList_scenePipeline); 207 202 208 modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 203 209 VK_SHADER_STAGE_FRAGMENT_BIT, &floorTextureImageDescriptor); … … 250 256 shipPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ShipVertex::normal)); 251 257 252 createUniformBuffers<UBO_MvpMat>(uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline); 258 createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 259 uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline); 260 createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 261 storageBuffers_shipPipeline, storageBuffersMemory_shipPipeline, storageBufferInfoList_shipPipeline); 253 262 254 263 shipPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 255 264 VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_shipPipeline); 265 shipPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 266 VK_SHADER_STAGE_VERTEX_BIT, &storageBufferInfoList_shipPipeline); 256 267 257 268 // TODO: With the normals, indexing basically becomes pointless since no vertices will have exactly … … 525 536 proj[1][1] *= -1; // flip the y-axis so that +y is up 526 537 527 modelMvpMats.model = mat4(1.0f); 528 modelMvpMats.view = view; 529 modelMvpMats.proj = proj; 530 531 mat4 T_model = translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f)); 532 mat4 R_model(1.0f); 533 534 shipMvpMats.model = T_model * R_model * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f)); 535 shipMvpMats.view = view; 536 shipMvpMats.proj = proj; 538 object_VP_mats.view = view; 539 object_VP_mats.proj = proj; 540 541 ship_VP_mats.view = view; 542 ship_VP_mats.proj = proj; 537 543 } 538 544 … … 607 613 } 608 614 615 // TODO: The view and projection mats only need to be updated once. 616 // Create a separate function that can do this once per Vulkan image at the beginning 609 617 void VulkanGame::updateScene(uint32_t currentImage) { 610 618 static auto startTime = chrono::high_resolution_clock::now(); … … 613 621 float time = chrono::duration<float, chrono::seconds::period>(currentTime - startTime).count(); 614 622 615 modelMvpMats.model =623 so_Object.model = 616 624 translate(mat4(1.0f), vec3(0.0f, -2.0f, -0.0f)) * 617 625 rotate(mat4(1.0f), time * radians(90.0f), vec3(0.0f, 0.0f, 1.0f)); 618 626 619 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory[currentImage], modelMvpMats); 620 621 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_shipPipeline[currentImage], shipMvpMats); 627 so_Ship.model = 628 translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f)) * 629 scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f)); 630 631 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_scenePipeline[currentImage], object_VP_mats); 632 633 VulkanUtils::copyDataToMemory(device, storageBuffersMemory_scenePipeline[currentImage], so_Object); 634 635 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_shipPipeline[currentImage], ship_VP_mats); 636 637 VulkanUtils::copyDataToMemory(device, storageBuffersMemory_shipPipeline[currentImage], so_Ship); 622 638 } 623 639 … … 901 917 902 918 void VulkanGame::createLogicalDevice( 903 const vector<const char*> validationLayers, 904 const vector<const char*>& deviceExtensions) { 919 const vector<const char*> validationLayers, const vector<const char*>& deviceExtensions) { 905 920 QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface); 906 921 … … 1237 1252 } 1238 1253 1254 void VulkanGame::createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags, 1255 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList) { 1256 buffers.resize(swapChainImages.size()); 1257 buffersMemory.resize(swapChainImages.size()); 1258 bufferInfoList.resize(swapChainImages.size()); 1259 1260 for (size_t i = 0; i < swapChainImages.size(); i++) { 1261 VulkanUtils::createBuffer(device, physicalDevice, bufferSize, flags, 1262 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 1263 buffers[i], buffersMemory[i]); 1264 1265 bufferInfoList[i].buffer = buffers[i]; 1266 bufferInfoList[i].offset = 0; // This is the offset from the start of the buffer, so always 0 for now 1267 bufferInfoList[i].range = bufferSize; // Size of the update starting from offset, or VK_WHOLE_SIZE 1268 } 1269 } 1270 1239 1271 // TODO: Fix the crash that happens when alt-tabbing 1240 1272 void VulkanGame::recreateSwapChain() { … … 1260 1292 createFramebuffers(); 1261 1293 1262 createUniformBuffers<UBO_MvpMat>(uniformBuffers, uniformBuffersMemory, uniformBufferInfoList); 1294 createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 1295 uniformBuffers_scenePipeline, uniformBuffersMemory_scenePipeline, uniformBufferInfoList_scenePipeline); 1296 createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 1297 storageBuffers_scenePipeline, storageBuffersMemory_scenePipeline, storageBufferInfoList_scenePipeline); 1263 1298 1264 1299 modelPipeline.updateRenderPass(renderPass); … … 1272 1307 overlayPipeline.createDescriptorSets(swapChainImages); 1273 1308 1274 createUniformBuffers<UBO_MvpMat>(uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline); 1309 createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 1310 uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline); 1311 createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, 1312 storageBuffers_shipPipeline, storageBuffersMemory_shipPipeline, storageBufferInfoList_shipPipeline); 1275 1313 1276 1314 shipPipeline.updateRenderPass(renderPass); … … 1303 1341 vkDestroySwapchainKHR(device, swapChain, nullptr); 1304 1342 1305 for (size_t i = 0; i < uniformBuffers.size(); i++) { 1306 vkDestroyBuffer(device, uniformBuffers[i], nullptr); 1307 vkFreeMemory(device, uniformBuffersMemory[i], nullptr); 1343 for (size_t i = 0; i < uniformBuffers_scenePipeline.size(); i++) { 1344 vkDestroyBuffer(device, uniformBuffers_scenePipeline[i], nullptr); 1345 vkFreeMemory(device, uniformBuffersMemory_scenePipeline[i], nullptr); 1346 } 1347 1348 for (size_t i = 0; i < storageBuffers_scenePipeline.size(); i++) { 1349 vkDestroyBuffer(device, storageBuffers_scenePipeline[i], nullptr); 1350 vkFreeMemory(device, storageBuffersMemory_scenePipeline[i], nullptr); 1308 1351 } 1309 1352 … … 1312 1355 vkFreeMemory(device, uniformBuffersMemory_shipPipeline[i], nullptr); 1313 1356 } 1314 } 1357 1358 for (size_t i = 0; i < storageBuffers_shipPipeline.size(); i++) { 1359 vkDestroyBuffer(device, storageBuffers_shipPipeline[i], nullptr); 1360 vkFreeMemory(device, storageBuffersMemory_shipPipeline[i], nullptr); 1361 } 1362 }
Note:
See TracChangeset
for help on using the changeset viewer.