Changeset 2da64ef in opengl-game for vulkan-game.cpp


Ignore:
Timestamp:
Feb 19, 2020, 4:47:41 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
3b84bb6
Parents:
860a0da
Message:

In VulkanGame, move the logic of updating per-object data in the SSBO into GraphicsPipeline_Vulkan and remove the SSBO properties from VulkanGame

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    r860a0da r2da64ef  
    524524   asteroidPipeline.addStorageDescriptor();
    525525
    526    addObject(asteroidObjects, asteroidPipeline,
    527       centerObject<AsteroidVertex>(
    528       addObjectIndex<AsteroidVertex>(asteroidObjects.size(),
    529       addVertexNormals<AsteroidVertex>({
    530 
    531          // front
    532          {{ 1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    533          {{-1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    534          {{-1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    535          {{ 1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    536          {{-1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    537          {{ 1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    538 
    539          // top
    540          {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    541          {{-1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    542          {{-1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    543          {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    544          {{-1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    545          {{ 1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    546 
    547          // bottom
    548          {{ 1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    549          {{-1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    550          {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    551          {{ 1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    552          {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    553          {{ 1.0f, -1.0f, -1.0}, {0.4f, 0.4f, 0.4f}},
    554 
    555          // back
    556          {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    557          {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    558          {{-1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    559          {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    560          {{ 1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    561          {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    562 
    563          // right
    564          {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    565          {{ 1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    566          {{ 1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    567          {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    568          {{ 1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    569          {{ 1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    570 
    571          // left
    572          {{-1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    573          {{-1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    574          {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    575          {{-1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    576          {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
    577          {{-1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
    578       }))), {
    579           0,  1,  2,  3,  4,  5,
    580           6,  7,  8,  9, 10, 11,
    581          12, 13, 14, 15, 16, 17,
    582          18, 19, 20, 21, 22, 23,
    583          24, 25, 26, 27, 28, 29,
    584          30, 31, 32, 33, 34, 35,
    585       }, {
    586          mat4(1.0f),
    587          10.0f,
    588          0
    589       });
    590 
    591526   asteroidPipeline.createDescriptorSetLayout();
    592527   asteroidPipeline.createPipeline("shaders/asteroid-vert.spv", "shaders/asteroid-frag.spv");
     
    604539      scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
    605540
    606    // vec3 pos = vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f));
    607    // vec3 pos = vec3(getRandomNum(-1.3f, 1.3f), -1.2f, -2.0f);
    608    // vec3 pos = vec3(1.0f, -1.2f, -1.0f);
    609    vec3 pos = vec3(0.0504826f, -1.2f, 1.0f);
    610 
    611    asteroidObjects[0].model_base =
    612       translate(mat4(1.0f), pos) *
    613       rotate(mat4(1.0f), radians(60.0f), vec3(1.0f, 1.0f, -1.0f)) *
    614       scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
    615    asteroidObjects[0].model_transform = mat4(1.0); // Might not be needed
    616 
    617    so_Asteroid.hp = 10.0f;
    618    so_Asteroid.deleted = 0;
     541   updateObject(shipObjects, shipPipeline, 0);
    619542}
    620543
     
    713636                  createCommandBuffers();
    714637               } else if (e.key.keycode == SDL_SCANCODE_Z) {
    715                   cout << "Deleting asteroid..." << endl;
    716                   so_Asteroid.deleted = so_Asteroid.deleted == 0 ? 1 : 0;
     638                  vkDeviceWaitIdle(device);
     639                  vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
     640
     641                  addObject(asteroidObjects, asteroidPipeline,
     642                     addObjectIndex<AsteroidVertex>(asteroidObjects.size(),
     643                     addVertexNormals<AsteroidVertex>({
     644
     645                        // front
     646                        {{ 1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     647                        {{-1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     648                        {{-1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     649                        {{ 1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     650                        {{-1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     651                        {{ 1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     652
     653                        // top
     654                        {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     655                        {{-1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     656                        {{-1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     657                        {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     658                        {{-1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     659                        {{ 1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     660
     661                        // bottom
     662                        {{ 1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     663                        {{-1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     664                        {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     665                        {{ 1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     666                        {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     667                        {{ 1.0f, -1.0f, -1.0}, {0.4f, 0.4f, 0.4f}},
     668
     669                        // back
     670                        {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     671                        {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     672                        {{-1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     673                        {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     674                        {{ 1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     675                        {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     676
     677                        // right
     678                        {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     679                        {{ 1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     680                        {{ 1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     681                        {{ 1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     682                        {{ 1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     683                        {{ 1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     684
     685                        // left
     686                        {{-1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     687                        {{-1.0f,  1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     688                        {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     689                        {{-1.0f,  1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     690                        {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},
     691                        {{-1.0f, -1.0f,  1.0f}, {0.4f, 0.4f, 0.4f}},
     692                     })), {
     693                        0,  1,  2,  3,  4,  5,
     694                        6,  7,  8,  9, 10, 11,
     695                        12, 13, 14, 15, 16, 17,
     696                        18, 19, 20, 21, 22, 23,
     697                        24, 25, 26, 27, 28, 29,
     698                        30, 31, 32, 33, 34, 35,
     699                     }, {
     700                        mat4(1.0f),
     701                        10.0f,
     702                        0
     703                     });
     704
     705                  // translate(mat4(1.0f), vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f))) *
     706                  asteroidObjects.back().model_base =
     707                     translate(mat4(1.0f), vec3(getRandomNum(-1.3f, 1.3f), -1.2f, -2.0f)) *
     708                     rotate(mat4(1.0f), radians(60.0f), vec3(1.0f, 1.0f, -1.0f)) *
     709                     scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
     710
     711                  updateObject(asteroidObjects, asteroidPipeline, asteroidObjects.size() - 1);
     712                  createCommandBuffers();
    717713               } else {
    718714                  cout << "Key event detected" << endl;
     
    743739
    744740      if (gui->keyPressed(SDL_SCANCODE_LEFT)) {
    745          transformObject(shipObjects[0], translate(mat4(1.0f), vec3(-shipSpeed * elapsedTime, 0.0f, 0.0f)));
     741         shipObjects[0].model_transform = translate(mat4(1.0f), vec3(-shipSpeed * elapsedTime, 0.0f, 0.0f))
     742            * shipObjects[0].model_transform;
     743
     744         updateObject(shipObjects, shipPipeline, 0);
    746745      } else if (gui->keyPressed(SDL_SCANCODE_RIGHT)) {
    747          transformObject(shipObjects[0], translate(mat4(1.0f), vec3(shipSpeed * elapsedTime, 0.0f, 0.0f)));
    748       }
    749 
    750       if (gui->keyPressed(SDL_SCANCODE_DOWN)) {
    751          transformObject(asteroidObjects[0], translate(mat4(1.0f), vec3(0.0f, 0.0f, asteroidSpeed * elapsedTime)));
     746         shipObjects[0].model_transform = translate(mat4(1.0f), vec3(shipSpeed * elapsedTime, 0.0f, 0.0f))
     747            * shipObjects[0].model_transform;
     748
     749         updateObject(shipObjects, shipPipeline, 0);
    752750      }
    753751
     
    759757}
    760758
    761 // TODO: The view and projection mats only need to be updated once.
    762 // Create a separate function that can do this once per Vulkan image at the beginning
     759// TODO: The only updates that need to happen once per Vulkan image are the SSBO ones,
     760// which are already handled by updateObject(). Move this code to a different place,
     761// where it will run just once per frame
    763762void VulkanGame::updateScene(uint32_t currentImage) {
    764763   static auto startTime = chrono::high_resolution_clock::now();
     
    767766   float time = chrono::duration<float, chrono::seconds::period>(currentTime - startTime).count();
    768767
    769    // TODO: Will need to change this to go through all objects of all pipelines and update their model mats
    770 
    771    so_Object.model =
    772       translate(mat4(1.0f), vec3(0.0f, -2.0f, -0.0f)) *
    773       rotate(mat4(1.0f), time * radians(90.0f), vec3(0.0f, 0.0f, 1.0f));
    774 
    775    so_Ship.model = shipObjects[0].model_transform * shipObjects[0].model_base;
    776 
    777    so_Asteroid.model = asteroidObjects[0].model_transform * asteroidObjects[0].model_base;
     768   for (int i = 0; i < modelObjects.size(); i++) {
     769      modelObjects[i].model_transform =
     770         translate(mat4(1.0f), vec3(0.0f, -2.0f, -0.0f)) *
     771         rotate(mat4(1.0f), time * radians(90.0f), vec3(0.0f, 0.0f, 1.0f));
     772
     773      updateObject(modelObjects, modelPipeline, i);
     774   }
    778775
    779776   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_modelPipeline[currentImage], 0, object_VP_mats);
    780777
    781    for (size_t i = 0; i < modelObjects.size(); i++) {
    782       VulkanUtils::copyDataToMemory(device, modelPipeline.storageBufferSet.memory[currentImage],
    783          i, so_Object);
    784    }
    785 
    786778   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_shipPipeline[currentImage], 0, ship_VP_mats);
    787779
    788    VulkanUtils::copyDataToMemory(device, shipPipeline.storageBufferSet.memory[currentImage], 0, so_Ship);
    789 
    790780   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_asteroidPipeline[currentImage], 0, asteroid_VP_mats);
    791 
    792    VulkanUtils::copyDataToMemory(device, asteroidPipeline.storageBufferSet.memory[currentImage], 0, so_Asteroid);
    793781}
    794782
     
    14491437   createFramebuffers();
    14501438
     1439   // TODO: Move UBO creation/management into GraphicsPipeline_Vulkan, like I did with SSBOs
     1440
    14511441   createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    14521442      uniformBuffers_modelPipeline, uniformBuffersMemory_modelPipeline, uniformBufferInfoList_modelPipeline);
Note: See TracChangeset for help on using the changeset viewer.