Changeset 3b84bb6 in opengl-game


Ignore:
Timestamp:
Feb 25, 2020, 6:51:02 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
44f23af
Parents:
2da64ef
Message:

In VulkanGame, call centerObject() on all objects when they are created and move that call inside VulkanGame::addObject(), rename GraphicsPipeline_Vulkan::addVertices() to addObject, add start adding generic support for objects to be added after pipeline creation is complete

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • graphics-pipeline_vulkan.hpp

    r2da64ef r3b84bb6  
    7171      void createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage);
    7272
    73       void addVertices(const vector<VertexType>& vertices, vector<uint16_t> indices, VkCommandPool commandPool,
    74          VkQueue graphicsQueue);
     73      bool addObject(const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType& ssbo,
     74         VkCommandPool commandPool, VkQueue graphicsQueue);
    7575
    7676      void updateObject(size_t objIndex, SSBOType& ssbo);
     
    125125
    126126// TODO: Verify that vertex capacity and index capacity are both > 0
     127// TODO: See if it would be feasible to move code in the createPipeline method
     128// into the constructor. That way, I can also put relevant cleanup code into the destructor
    127129template<class VertexType, class SSBOType>
    128130GraphicsPipeline_Vulkan<VertexType, SSBOType>::GraphicsPipeline_Vulkan(
     
    473475
    474476template<class VertexType, class SSBOType>
    475 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addVertices(const vector<VertexType>& vertices, vector<uint16_t> indices,
    476       VkCommandPool commandPool, VkQueue graphicsQueue) {
     477bool GraphicsPipeline_Vulkan<VertexType, SSBOType>::addObject(
     478      const vector<VertexType>& vertices, vector<uint16_t> indices,
     479      SSBOType& ssbo, VkCommandPool commandPool, VkQueue graphicsQueue) {
     480
     481   // TODO: When resizing the vertex or index buffer, take deleted objects into account.
     482   // Remove their data from the buffer and determine the new size of the bufer based on # of remining objects
     483
     484   // If # non-deleted objects > currentCapacity / 2
     485   //    - resize and double capacity
     486   // else If # non-deleted objects < currentCapacity / 4
     487   //    - resize amd halve capacity
     488   // else
     489   //    - don't resize, but rewrite data in the buffer to only have non-deleted objects
    477490
    478491   if (numVertices + vertices.size() > vertexCapacity) {
     
    490503      graphicsQueue);
    491504   numIndices += indices.size();
     505
     506   bool resizedStorageBuffer = false;
     507
     508   return resizedStorageBuffer;
    492509}
    493510
  • vulkan-game.cpp

    r2da64ef r3b84bb6  
    208208      }, {
    209209         0, 1, 2, 2, 3, 0
    210       }, {});
     210      }, {}, false);
    211211
    212212   overlayPipeline.createDescriptorSetLayout();
     
    240240      }, {
    241241         mat4(1.0f)
    242       });
     242      }, false);
    243243
    244244   addObject(modelObjects, modelPipeline,
     
    252252      }, {
    253253         mat4(1.0f)
    254       });
     254      }, false);
    255255
    256256   modelPipeline.createDescriptorSetLayout();
     
    274274   // the same data. Add an option to make some pipelines not use indexing
    275275   addObject(shipObjects, shipPipeline,
    276       centerObject<ShipVertex>(
    277276      addObjectIndex<ShipVertex>(shipObjects.size(),
    278277      addVertexNormals<ShipVertex>({
     
    473472         {{  1.5f,   0.0f,   0.0f}, {0.0f, 0.0f, 0.3f}},
    474473         {{  1.3f,   0.0f,  -0.3f}, {0.0f, 0.0f, 0.3f}},
    475       }))), {
     474      })), {
    476475           0,   1,   2,   3,   4,   5,
    477476           6,   7,   8,   9,  10,  11,
     
    505504      }, {
    506505         mat4(1.0f)
    507       });
     506      }, false);
    508507
    509508   shipPipeline.createDescriptorSetLayout();
     
    619618                  float zOffset = -2.0f + (0.5f * modelObjects.size());
    620619
    621                   vkDeviceWaitIdle(device);
    622                   vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
    623 
    624620                  addObject(modelObjects, modelPipeline,
    625621                     addObjectIndex<ModelVertex>(modelObjects.size(), {
     
    632628                     }, {
    633629                        mat4(1.0f)
    634                      });
    635 
    636                   createCommandBuffers();
     630                     }, true);
    637631               } else if (e.key.keycode == SDL_SCANCODE_Z) {
    638                   vkDeviceWaitIdle(device);
    639                   vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
    640 
    641632                  addObject(asteroidObjects, asteroidPipeline,
    642633                     addObjectIndex<AsteroidVertex>(asteroidObjects.size(),
     
    701692                        10.0f,
    702693                        0
    703                      });
     694                     }, true);
    704695
    705696                  // translate(mat4(1.0f), vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f))) *
     
    710701
    711702                  updateObject(asteroidObjects, asteroidPipeline, asteroidObjects.size() - 1);
    712                   createCommandBuffers();
    713703               } else {
    714704                  cout << "Key event detected" << endl;
     
    750740      }
    751741
     742      if (gui->keyPressed(SDL_SCANCODE_X)) {
     743         if (asteroidObjects.size() > 0 && !asteroidObjects[0].ssbo.deleted) {
     744            updateObject(asteroidObjects, asteroidPipeline, 0);
     745         }
     746      }
     747
    752748      renderUI();
    753749      renderScene();
  • vulkan-game.hpp

    r2da64ef r3b84bb6  
    5858   mat4 model_base;
    5959   mat4 model_transform;
     60   vec3 center;
     61   float radius;
    6062};
    6163
     
    217219      void addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
    218220         GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
    219          const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo);
     221         const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
     222         bool pipelinesCreated);
    220223
    221224      template<class VertexType, class SSBOType>
     
    229232      vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
    230233
    231       template<class VertexType>
    232       vector<VertexType> centerObject(vector<VertexType> vertices);
     234      template<class VertexType, class SSBOType>
     235      void centerObject(SceneObject<VertexType, SSBOType>& object);
    233236
    234237      void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
     
    246249};
    247250
     251// TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model
     252// and to change the model matrix later by setting model_transform and then calling updateObject()
     253// Figure out a better way to allow the model matrix to be set during objecting creation
    248254template<class VertexType, class SSBOType>
    249255void VulkanGame::addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
    250256      GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
    251       const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo) {
     257      const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
     258      bool pipelinesCreated) {
    252259   size_t numVertices = pipeline.getNumVertices();
    253260
     
    257264
    258265   objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f) });
    259 
    260    pipeline.addVertices(vertices, indices, commandPool, graphicsQueue);
     266   centerObject(objects.back());
     267
     268   bool storageBufferResized = pipeline.addObject(vertices, indices, ssbo, commandPool, graphicsQueue);
     269
     270   if (pipelinesCreated) {
     271      if (storageBufferResized) {
     272      }
     273
     274      createCommandBuffers();
     275   }
    261276}
    262277
     
    302317}
    303318
    304 template<class VertexType>
    305 vector<VertexType> VulkanGame::centerObject(vector<VertexType> vertices) {
     319template<class VertexType, class SSBOType>
     320void VulkanGame::centerObject(SceneObject<VertexType, SSBOType>& object) {
     321   vector<VertexType>& vertices = object.vertices;
     322
    306323   float min_x = vertices[0].pos.x;
    307324   float max_x = vertices[0].pos.x;
     
    313330   // start from the second point
    314331   for (unsigned int i = 1; i < vertices.size(); i++) {
    315       if (min_x > vertices[i].pos.x) {
    316          min_x = vertices[i].pos.x;
    317       } else if (max_x < vertices[i].pos.x) {
    318          max_x = vertices[i].pos.x;
     332      vec3& pos = vertices[i].pos;
     333
     334      if (min_x > pos.x) {
     335         min_x = pos.x;
     336      } else if (max_x < pos.x) {
     337         max_x = pos.x;
    319338      }
    320339
    321       if (min_y > vertices[i].pos.y) {
    322          min_y = vertices[i].pos.y;
    323       } else if (max_y < vertices[i].pos.y) {
    324          max_y = vertices[i].pos.y;
     340      if (min_y > pos.y) {
     341         min_y = pos.y;
     342      } else if (max_y < pos.y) {
     343         max_y = pos.y;
    325344      }
    326345
    327       if (min_z > vertices[i].pos.z) {
    328          min_z = vertices[i].pos.z;
    329       } else if (max_z < vertices[i].pos.z) {
    330          max_z = vertices[i].pos.z;
     346      if (min_z > pos.z) {
     347         min_z = pos.z;
     348      } else if (max_z < pos.z) {
     349         max_z = pos.z;
    331350      }
    332351   }
     
    338357   }
    339358
    340    return vertices;
     359   object.radius = std::max(center.x, center.y);
     360   object.radius = std::max(object.radius, center.z);
     361   object.center = vec3(0.0f, 0.0f, 0.0f);
    341362}
    342363
Note: See TracChangeset for help on using the changeset viewer.