Changeset 2da64ef in opengl-game


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

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • graphics-pipeline_vulkan.hpp

    r860a0da r2da64ef  
    7474         VkQueue graphicsQueue);
    7575
     76      void updateObject(size_t objIndex, SSBOType& ssbo);
     77
    7678      void cleanup();
    7779      void cleanupBuffers();
    78 
    79       StorageBufferSet storageBufferSet;
    8080   
    8181   private:
     
    108108      size_t numObjects;
    109109      size_t objectCapacity;
     110
     111      StorageBufferSet storageBufferSet;
    110112
    111113      VkShaderModule createShaderModule(const vector<char>& code);
     
    491493
    492494template<class VertexType, class SSBOType>
     495void GraphicsPipeline_Vulkan<VertexType, SSBOType>::updateObject(size_t objIndex, SSBOType& ssbo) {
     496   if (!is_same_v<SSBOType, void*>) {
     497      for (size_t i = 0; i < storageBufferSet.memory.size(); i++) {
     498         VulkanUtils::copyDataToMemory(device, storageBufferSet.memory[i], objIndex, ssbo);
     499      }
     500   }
     501}
     502
     503template<class VertexType, class SSBOType>
    493504void GraphicsPipeline_Vulkan<VertexType, SSBOType>::cleanup() {
    494505   vkDestroyPipeline(device, pipeline, nullptr);
  • 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);
  • vulkan-game.hpp

    r860a0da r2da64ef  
    7575};
    7676
     77// Have to figure out how to include an optional ssbo parameter for each object
     78// Could probably use the same approach to make indices optional
     79
    7780class VulkanGame {
    7881   public:
     
    158161
    159162      UBO_VP_mats object_VP_mats;
    160       SSBO_ModelObject so_Object;
    161163
    162164      GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject> shipPipeline;
     
    168170
    169171      UBO_VP_mats ship_VP_mats;
    170       SSBO_ModelObject so_Ship;
    171172
    172173      GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid> asteroidPipeline;
     
    178179
    179180      UBO_VP_mats asteroid_VP_mats;
    180       SSBO_Asteroid so_Asteroid;
    181181
    182182      Uint64 curTime, prevTime;
     
    219219         const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo);
    220220
     221      template<class VertexType, class SSBOType>
     222      void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
     223         GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index);
     224
    221225      template<class VertexType>
    222226      vector<VertexType> addVertexNormals(vector<VertexType> vertices);
     
    227231      template<class VertexType>
    228232      vector<VertexType> centerObject(vector<VertexType> vertices);
    229 
    230       template<class VertexType, class SSBOType>
    231       void transformObject(SceneObject<VertexType, SSBOType>& obj, mat4 mat);
    232233
    233234      void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
     
    258259
    259260   pipeline.addVertices(vertices, indices, commandPool, graphicsQueue);
     261}
     262
     263template<class VertexType, class SSBOType>
     264void VulkanGame::updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
     265      GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index) {
     266   SceneObject<VertexType, SSBOType>& obj = objects[index];
     267
     268   obj.ssbo.model = obj.model_transform * obj.model_base;
     269
     270   // could probably re-calculate the object center here based on model
     271   // I think the center should be calculated by using model * vec3(0, 0, 0)
     272   // model_base is currently only used to set the original location of the ship and asteroids
     273
     274   pipeline.updateObject(index, obj.ssbo);
    260275}
    261276
     
    326341}
    327342
    328 template<class VertexType, class SSBOType>
    329 void VulkanGame::transformObject(SceneObject<VertexType, SSBOType>& obj, mat4 mat) {
    330    obj.model_transform = mat * obj.model_transform;
    331 }
    332 
    333343#endif // _VULKAN_GAME_H
Note: See TracChangeset for help on using the changeset viewer.