Changeset 2d87297 in opengl-game


Ignore:
Timestamp:
Feb 16, 2020, 8:22:40 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
d25381b
Parents:
5a1ace0
Message:

In VulkanGame, add an ssbo field to SceneObject to hold per-object ssbo info, and add a new SSBOType template parameter to SceneObject and GraphicsPipeline_Vulkan

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • graphics-pipeline_vulkan.hpp

    r5a1ace0 r2d87297  
    3131};
    3232
    33 template<class VertexType>
     33template<class VertexType, class SSBOType>
    3434class GraphicsPipeline_Vulkan : public GraphicsPipeline {
    3535   public:
     
    9999/*** PUBLIC METHODS ***/
    100100
    101 template<class VertexType>
    102 GraphicsPipeline_Vulkan<VertexType>::GraphicsPipeline_Vulkan() {
     101template<class VertexType, class SSBOType>
     102GraphicsPipeline_Vulkan<VertexType, SSBOType>::GraphicsPipeline_Vulkan() {
    103103}
    104104
    105105// TODO: Verify that vertex capacity and index capacity are both > 0
    106 template<class VertexType>
    107 GraphicsPipeline_Vulkan<VertexType>::GraphicsPipeline_Vulkan(VkPhysicalDevice physicalDevice, VkDevice device,
     106template<class VertexType, class SSBOType>
     107GraphicsPipeline_Vulkan<VertexType, SSBOType>::GraphicsPipeline_Vulkan(
     108      VkPhysicalDevice physicalDevice, VkDevice device,
    108109      VkRenderPass renderPass, Viewport viewport, size_t vertexCapacity, size_t indexCapacity) {
    109110   this->physicalDevice = physicalDevice;
     
    134135}
    135136
    136 template<class VertexType>
    137 GraphicsPipeline_Vulkan<VertexType>::~GraphicsPipeline_Vulkan() {
    138 }
    139 
    140 template<class VertexType>
    141 size_t GraphicsPipeline_Vulkan<VertexType>::getNumVertices() {
     137template<class VertexType, class SSBOType>
     138GraphicsPipeline_Vulkan<VertexType, SSBOType>::~GraphicsPipeline_Vulkan() {
     139}
     140
     141template<class VertexType, class SSBOType>
     142size_t GraphicsPipeline_Vulkan<VertexType, SSBOType>::getNumVertices() {
    142143   return numVertices;
    143144}
    144145
    145 template<class VertexType>
    146 void GraphicsPipeline_Vulkan<VertexType>::updateRenderPass(VkRenderPass renderPass) {
     146template<class VertexType, class SSBOType>
     147void GraphicsPipeline_Vulkan<VertexType, SSBOType>::updateRenderPass(VkRenderPass renderPass) {
    147148   this->renderPass = renderPass;
    148149}
    149150
    150 template<class VertexType>
    151 void GraphicsPipeline_Vulkan<VertexType>::addAttribute(VkFormat format, size_t offset) {
     151template<class VertexType, class SSBOType>
     152void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addAttribute(VkFormat format, size_t offset) {
    152153   VkVertexInputAttributeDescription attributeDesc = {};
    153154
     
    160161}
    161162
    162 template<class VertexType>
    163 void GraphicsPipeline_Vulkan<VertexType>::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData) {
     163template<class VertexType, class SSBOType>
     164void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData) {
    164165   this->descriptorInfoList.push_back({ type, stageFlags, bufferData, nullptr });
    165166}
    166167
    167 template<class VertexType>
    168 void GraphicsPipeline_Vulkan<VertexType>::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData) {
     168template<class VertexType, class SSBOType>
     169void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData) {
    169170   this->descriptorInfoList.push_back({ type, stageFlags, nullptr, imageData });
    170171}
    171172
    172 template<class VertexType>
    173 void GraphicsPipeline_Vulkan<VertexType>::createPipeline(string vertShaderFile, string fragShaderFile) {
     173template<class VertexType, class SSBOType>
     174void GraphicsPipeline_Vulkan<VertexType, SSBOType>::createPipeline(string vertShaderFile, string fragShaderFile) {
    174175   vector<char> vertShaderCode = readFile(vertShaderFile);
    175176   vector<char> fragShaderCode = readFile(fragShaderFile);
     
    308309}
    309310
    310 template<class VertexType>
    311 void GraphicsPipeline_Vulkan<VertexType>::createDescriptorSetLayout() {
     311template<class VertexType, class SSBOType>
     312void GraphicsPipeline_Vulkan<VertexType, SSBOType>::createDescriptorSetLayout() {
    312313   vector<VkDescriptorSetLayoutBinding> bindings(this->descriptorInfoList.size());
    313314
     
    330331}
    331332
    332 template<class VertexType>
    333 void GraphicsPipeline_Vulkan<VertexType>::createDescriptorPool(vector<VkImage>& swapChainImages) {
     333template<class VertexType, class SSBOType>
     334void GraphicsPipeline_Vulkan<VertexType, SSBOType>::createDescriptorPool(vector<VkImage>& swapChainImages) {
    334335   vector<VkDescriptorPoolSize> poolSizes(this->descriptorInfoList.size());
    335336
     
    350351}
    351352
    352 template<class VertexType>
    353 void GraphicsPipeline_Vulkan<VertexType>::createDescriptorSets(vector<VkImage>& swapChainImages) {
     353template<class VertexType, class SSBOType>
     354void GraphicsPipeline_Vulkan<VertexType, SSBOType>::createDescriptorSets(vector<VkImage>& swapChainImages) {
    354355   vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), this->descriptorSetLayout);
    355356
     
    396397}
    397398
    398 template<class VertexType>
    399 void GraphicsPipeline_Vulkan<VertexType>::createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage) {
     399template<class VertexType, class SSBOType>
     400void GraphicsPipeline_Vulkan<VertexType, SSBOType>::createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage) {
    400401   vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
    401402   vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1,
     
    411412}
    412413
    413 template<class VertexType>
    414 void GraphicsPipeline_Vulkan<VertexType>::addVertices(const vector<VertexType>& vertices, vector<uint16_t> indices,
     414template<class VertexType, class SSBOType>
     415void GraphicsPipeline_Vulkan<VertexType, SSBOType>::addVertices(const vector<VertexType>& vertices, vector<uint16_t> indices,
    415416      VkCommandPool commandPool, VkQueue graphicsQueue) {
    416417
     
    431432}
    432433
    433 template<class VertexType>
    434 void GraphicsPipeline_Vulkan<VertexType>::cleanup() {
     434template<class VertexType, class SSBOType>
     435void GraphicsPipeline_Vulkan<VertexType, SSBOType>::cleanup() {
    435436   vkDestroyPipeline(device, pipeline, nullptr);
    436437   vkDestroyDescriptorPool(device, descriptorPool, nullptr);
     
    438439}
    439440
    440 template<class VertexType>
    441 void GraphicsPipeline_Vulkan<VertexType>::cleanupBuffers() {
     441template<class VertexType, class SSBOType>
     442void GraphicsPipeline_Vulkan<VertexType, SSBOType>::cleanupBuffers() {
    442443   vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
    443444
     
    450451/*** PRIVATE METHODS ***/
    451452
    452 template<class VertexType>
    453 VkShaderModule GraphicsPipeline_Vulkan<VertexType>::createShaderModule(const vector<char>& code) {
     453template<class VertexType, class SSBOType>
     454VkShaderModule GraphicsPipeline_Vulkan<VertexType, SSBOType>::createShaderModule(const vector<char>& code) {
    454455   VkShaderModuleCreateInfo createInfo = {};
    455456   createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
     
    465466}
    466467
    467 template<class VertexType>
    468 vector<char> GraphicsPipeline_Vulkan<VertexType>::readFile(const string& filename) {
     468template<class VertexType, class SSBOType>
     469vector<char> GraphicsPipeline_Vulkan<VertexType, SSBOType>::readFile(const string& filename) {
    469470   ifstream file(filename, ios::ate | ios::binary);
    470471
     
    484485}
    485486
    486 template<class VertexType>
    487 void GraphicsPipeline_Vulkan<VertexType>::resizeVertexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue) {
     487template<class VertexType, class SSBOType>
     488void GraphicsPipeline_Vulkan<VertexType, SSBOType>::resizeVertexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue) {
    488489   VkBuffer newVertexBuffer;
    489490   VkDeviceMemory newVertexBufferMemory;
     
    503504}
    504505
    505 template<class VertexType>
    506 void GraphicsPipeline_Vulkan<VertexType>::resizeIndexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue) {
     506template<class VertexType, class SSBOType>
     507void GraphicsPipeline_Vulkan<VertexType, SSBOType>::resizeIndexBuffer(VkCommandPool commandPool, VkQueue graphicsQueue) {
    507508   VkBuffer newIndexBuffer;
    508509   VkDeviceMemory newIndexBufferMemory;
  • vulkan-game.cpp

    r5a1ace0 r2d87297  
    202202      uniformBuffers_scenePipeline, uniformBuffersMemory_scenePipeline, uniformBufferInfoList_scenePipeline);
    203203   // TODO: Calculate the size of this buffer (and all the other SSBOs) based on the number of objects
    204    createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     204   createBufferSet(10 * sizeof(SSBO_ModelObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
    205205      storageBuffers_scenePipeline, storageBuffersMemory_scenePipeline, storageBufferInfoList_scenePipeline);
    206206
     
    221221      }), {
    222222         0, 1, 2, 2, 3, 0
     223      }, {
     224         mat4(1.0f)
    223225      });
    224226
     
    231233      }), {
    232234         0, 1, 2, 2, 3, 0
     235      }, {
     236         mat4(1.0f)
    233237      });
    234238
     
    252256      }, {
    253257         0, 1, 2, 2, 3, 0
    254       });
     258      }, {});
    255259
    256260   overlayPipeline.createDescriptorSetLayout();
     
    266270   createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    267271      uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline);
    268    createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     272   createBufferSet(10 * sizeof(SSBO_ModelObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
    269273      storageBuffers_shipPipeline, storageBuffersMemory_shipPipeline, storageBufferInfoList_shipPipeline);
    270274
     
    506510         132, 133, 134,
    507511         135, 136, 137,
     512      }, {
     513         mat4(1.0f)
    508514      });
    509515
     
    520526   createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    521527      uniformBuffers_asteroidPipeline, uniformBuffersMemory_asteroidPipeline, uniformBufferInfoList_asteroidPipeline);
    522    createBufferSet(10 * sizeof(SBO_Asteroid), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     528   createBufferSet(10 * sizeof(SSBO_Asteroid), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
    523529      storageBuffers_asteroidPipeline, storageBuffersMemory_asteroidPipeline, storageBufferInfoList_asteroidPipeline);
    524530
     
    587593         24, 25, 26, 27, 28, 29,
    588594         30, 31, 32, 33, 34, 35,
     595      }, {
     596         mat4(1.0f),
     597         10.0f,
     598         0
    589599      });
    590600
     
    620630
    621631void VulkanGame::initGraphicsPipelines() {
    622    modelPipeline = GraphicsPipeline_Vulkan<ModelVertex>(physicalDevice, device, renderPass,
     632   overlayPipeline = GraphicsPipeline_Vulkan<OverlayVertex, void*>(physicalDevice, device, renderPass,
     633      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 4, 6);
     634
     635   modelPipeline = GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject>(physicalDevice, device, renderPass,
    623636      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 16, 24);
    624637
    625    overlayPipeline = GraphicsPipeline_Vulkan<OverlayVertex>(physicalDevice, device, renderPass,
    626       { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 4, 6);
    627 
    628    shipPipeline = GraphicsPipeline_Vulkan<ShipVertex>(physicalDevice, device, renderPass,
     638   shipPipeline = GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject>(physicalDevice, device, renderPass,
    629639      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 138, 138);
    630640
    631    asteroidPipeline = GraphicsPipeline_Vulkan<AsteroidVertex>(physicalDevice, device, renderPass,
     641   asteroidPipeline = GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid>(physicalDevice, device, renderPass,
    632642      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 24, 36);
    633643}
     
    707717                     }), {
    708718                        0, 1, 2, 2, 3, 0
     719                     }, {
     720                        mat4(1.0f)
    709721                     });
    710722
     
    14491461   createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    14501462      uniformBuffers_scenePipeline, uniformBuffersMemory_scenePipeline, uniformBufferInfoList_scenePipeline);
    1451    createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     1463   createBufferSet(10 * sizeof(SSBO_ModelObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
    14521464      storageBuffers_scenePipeline, storageBuffersMemory_scenePipeline, storageBufferInfoList_scenePipeline);
    14531465
     
    14641476   createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    14651477      uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline);
    1466    createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     1478   createBufferSet(10 * sizeof(SSBO_ModelObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
    14671479      storageBuffers_shipPipeline, storageBuffersMemory_shipPipeline, storageBufferInfoList_shipPipeline);
    14681480
     
    14741486   createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    14751487      uniformBuffers_asteroidPipeline, uniformBuffersMemory_asteroidPipeline, uniformBufferInfoList_asteroidPipeline);
    1476    createBufferSet(10 * sizeof(SBO_Asteroid), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     1488   createBufferSet(10 * sizeof(SSBO_Asteroid), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
    14771489      storageBuffers_asteroidPipeline, storageBuffersMemory_asteroidPipeline, storageBufferInfoList_asteroidPipeline);
    14781490
  • vulkan-game.hpp

    r5a1ace0 r2d87297  
    5050// TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
    5151// TODO: Create a typedef for index type so I can easily change uin16_t to something else later
    52 template<class VertexType>
     52template<class VertexType, class SSBOType>
    5353struct SceneObject {
    5454   vector<VertexType> vertices;
    5555   vector<uint16_t> indices;
     56   SSBOType ssbo;
    5657
    5758   mat4 model_base;
     
    6465};
    6566
    66 struct SBO_SceneObject {
     67struct SSBO_ModelObject {
    6768   alignas(16) mat4 model;
    6869};
    6970
    70 struct SBO_Asteroid {
     71struct SSBO_Asteroid {
    7172   alignas(16) mat4 model;
    7273   alignas(4) float hp;
     
    146147      // TODO: Create a struct that holds the buffers, memory, and info objects (Probably in VulkanUtils)
    147148
    148       GraphicsPipeline_Vulkan<OverlayVertex> overlayPipeline;
    149 
    150       vector<SceneObject<OverlayVertex>> overlayObjects;
     149      GraphicsPipeline_Vulkan<OverlayVertex, void*> overlayPipeline;
     150
     151      vector<SceneObject<OverlayVertex, void*>> overlayObjects;
    151152
    152153      // TODO: Rename all the variables related to modelPipeline to use the same pipelie name
    153154
    154       GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
    155 
    156       vector<SceneObject<ModelVertex>> modelObjects;
     155      GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject> modelPipeline;
     156
     157      vector<SceneObject<ModelVertex, SSBO_ModelObject>> modelObjects;
    157158
    158159      vector<VkBuffer> uniformBuffers_scenePipeline;
     
    165166
    166167      UBO_VP_mats object_VP_mats;
    167       SBO_SceneObject so_Object;
    168 
    169       GraphicsPipeline_Vulkan<ShipVertex> shipPipeline;
    170 
    171       vector<SceneObject<ShipVertex>> shipObjects;
     168      SSBO_ModelObject so_Object;
     169
     170      GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject> shipPipeline;
     171
     172      vector<SceneObject<ShipVertex, SSBO_ModelObject>> shipObjects;
    172173
    173174      vector<VkBuffer> uniformBuffers_shipPipeline;
     
    180181
    181182      UBO_VP_mats ship_VP_mats;
    182       SBO_SceneObject so_Ship;
    183 
    184       GraphicsPipeline_Vulkan<AsteroidVertex> asteroidPipeline;
    185 
    186       vector<SceneObject<AsteroidVertex>> asteroidObjects;
     183      SSBO_ModelObject so_Ship;
     184
     185      GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid> asteroidPipeline;
     186
     187      vector<SceneObject<AsteroidVertex, SSBO_Asteroid>> asteroidObjects;
    187188
    188189      vector<VkBuffer> uniformBuffers_asteroidPipeline;
     
    195196
    196197      UBO_VP_mats asteroid_VP_mats;
    197       SBO_Asteroid so_Asteroid;
     198      SSBO_Asteroid so_Asteroid;
    198199
    199200      Uint64 curTime, prevTime;
     
    231232      void createSyncObjects();
    232233
    233       template<class VertexType>
    234       void addObject(vector<SceneObject<VertexType>>& objects, GraphicsPipeline_Vulkan<VertexType>& pipeline,
    235          const vector<VertexType>& vertices, vector<uint16_t> indices);
     234      template<class VertexType, class SSBOType>
     235      void addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
     236         GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
     237         const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo);
    236238
    237239      template<class VertexType>
     
    244246      vector<VertexType> centerObject(vector<VertexType> vertices);
    245247
    246       template<class VertexType>
    247       void transformObject(SceneObject<VertexType>& obj, mat4 mat);
     248      template<class VertexType, class SSBOType>
     249      void transformObject(SceneObject<VertexType, SSBOType>& obj, mat4 mat);
    248250
    249251      void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
     
    261263};
    262264
    263 template<class VertexType>
    264 void VulkanGame::addObject(vector<SceneObject<VertexType>>& objects, GraphicsPipeline_Vulkan<VertexType>& pipeline,
    265       const vector<VertexType>& vertices, vector<uint16_t> indices) {
     265template<class VertexType, class SSBOType>
     266void VulkanGame::addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
     267      GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
     268      const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo) {
    266269   size_t numVertices = pipeline.getNumVertices();
    267270
     
    270273   }
    271274
    272    objects.push_back({ vertices, indices, mat4(1.0f), mat4(1.0f) });
     275   objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f) });
    273276
    274277   pipeline.addVertices(vertices, indices, commandPool, graphicsQueue);
     
    341344}
    342345
    343 template<class VertexType>
    344 void VulkanGame::transformObject(SceneObject<VertexType>& obj, mat4 mat) {
     346template<class VertexType, class SSBOType>
     347void VulkanGame::transformObject(SceneObject<VertexType, SSBOType>& obj, mat4 mat) {
    345348   obj.model_transform = mat * obj.model_transform;
    346349}
Note: See TracChangeset for help on using the changeset viewer.