Changeset 90880fb in opengl-game


Ignore:
Timestamp:
Jun 17, 2021, 11:59:14 PM (3 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl
Children:
2f4ff8c
Parents:
5ea0a37
git-author:
Dmitry Portnoy <dportnoy@…> (06/13/21 23:57:17)
git-committer:
Dmitry Portnoy <dportnoy@…> (06/17/21 23:59:14)
Message:

Start using the VulkanBuffer class for the non-per-object uniforms (this means the view and projection matrices for all pipelines, as well as the current time for the explosion pipeline)

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • sdl-game.cpp

    r5ea0a37 r90880fb  
    7070                     , gui(nullptr)
    7171                     , window(nullptr)
     72                     , vp_mats_modelPipeline()
    7273                     , objects_modelPipeline()
    7374                     , score(0)
     
    9192   VkPhysicalDeviceProperties deviceProperties;
    9293   vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties);
     94
     95   vp_mats_modelPipeline = VulkanBuffer<UBO_VP_mats>(1, deviceProperties.limits.maxUniformBufferRange,
     96                                                     deviceProperties.limits.minUniformBufferOffsetAlignment);
    9397
    9498   objects_modelPipeline = VulkanBuffer<SSBO_ModelObject>(10, deviceProperties.limits.maxStorageBufferRange,
     
    110114   modelPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&ModelVertex::objIndex));
    111115
    112    createBufferSet(sizeof(UBO_VP_mats),
     116   createBufferSet(vp_mats_modelPipeline.memorySize(),
    113117                   VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    114118                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    115119                   uniformBuffers_modelPipeline);
    116120
    117    createBufferSet(objects_modelPipeline.capacity * sizeof(SSBO_ModelObject),
     121   createBufferSet(objects_modelPipeline.memorySize(),
    118122                   VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT
    119123                   | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     
    289293   projMat = perspective(radians(FOV_ANGLE), (float)swapChainExtent.width / (float)swapChainExtent.height, NEAR_CLIP, FAR_CLIP);
    290294   projMat[1][1] *= -1; // flip the y-axis so that +y is up
    291 
    292    object_VP_mats.view = viewMat;
    293    object_VP_mats.proj = projMat;
    294295}
    295296
     
    444445
    445446void VulkanGame::updateScene() {
    446    // TODO: Probably move the resizing to the VulkanBuffer class
     447   vp_mats_modelPipeline.data()->view = viewMat;
     448   vp_mats_modelPipeline.data()->proj = projMat;
     449
     450   // TODO: Probably move the resizing to the VulkanBuffer class's add() method
     451   // Remember that we want to avoid resizing the actuall ssbo or ubo more than once per frame
     452   // TODO: Figure out a way to make updateDescriptorInfo easier to use, maybe store the binding index in the buffer set
     453
    447454   if (objects_modelPipeline.resized) {
    448455      resizeBufferSet(storageBuffers_modelPipeline, objects_modelPipeline.memorySize(), resourceCommandPool,
     
    467474   }
    468475
     476   VulkanUtils::copyDataToMemory(device, vp_mats_modelPipeline.data(), uniformBuffers_modelPipeline.memory[imageIndex],
     477                                 0, vp_mats_modelPipeline.memorySize(), false);
     478
    469479   VulkanUtils::copyDataToMemory(device, objects_modelPipeline.data(), storageBuffers_modelPipeline.memory[imageIndex],
    470480                                 0, objects_modelPipeline.memorySize(), false);
    471 
    472    VulkanUtils::copyDataToMemory(device, &object_VP_mats, uniformBuffers_modelPipeline.memory[imageIndex], 0,
    473                                  sizeof(object_VP_mats), false);
    474481}
    475482
     
    12751282   createSyncObjects();
    12761283
    1277    createBufferSet(sizeof(UBO_VP_mats),
     1284   createBufferSet(vp_mats_modelPipeline.memorySize(),
    12781285                   VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    12791286                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
  • sdl-game.hpp

    r5ea0a37 r90880fb  
    225225      GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
    226226
     227      BufferSet uniformBuffers_modelPipeline;
    227228      BufferSet storageBuffers_modelPipeline;
     229
     230      VulkanBuffer<UBO_VP_mats> vp_mats_modelPipeline;
    228231      VulkanBuffer<SSBO_ModelObject> objects_modelPipeline;
    229       BufferSet uniformBuffers_modelPipeline;
    230232
    231233      // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
     
    238240
    239241      vector<SceneObject<ModelVertex>> modelObjects;
    240 
    241       UBO_VP_mats object_VP_mats;
    242242
    243243      /*** High-level vars ***/
  • vulkan-buffer.hpp

    r5ea0a37 r90880fb  
    3737      T& get(uint32_t index);
    3838      void add(T obj);
     39
     40      void* mapped(size_t idx);
     41
     42      void map(vector<VkDeviceMemory>& deviceMemory, VkDevice device);
     43      void unmap(vector<VkDeviceMemory>& deviceMemory, VkDevice device);
    3944
    4045      void resize();
     
    182187
    183188template<class T>
     189void* VulkanBuffer<T>::mapped(size_t idx) {
     190   return mappedData[idx];
     191}
     192
     193template<class T>
     194void VulkanBuffer<T>::map(vector<VkDeviceMemory>& deviceMemory, VkDevice device) {
     195   // TODO: Make sure that mappedData initally has size 0. If not, it means the memory is already mapped
     196   // and I should return some kind of error or warning
     197   mappedData.resize(deviceMemory.size());
     198
     199   for (size_t i = 0; i < deviceMemory.size(); i++) {
     200      vkMapMemory(device, deviceMemory[i], 0, memorySize(), 0, &mappedData[i]);
     201   }
     202}
     203
     204template<class T>
     205void VulkanBuffer<T>::unmap(vector<VkDeviceMemory>& deviceMemory, VkDevice device) {
     206   for (size_t i = 0; i < deviceMemory.size(); i++) {
     207      vkUnmapMemory(device, deviceMemory[i]);
     208   }
     209
     210   mappedData.clear();
     211}
     212
     213template<class T>
    184214void VulkanBuffer<T>::resize() {
    185215   resized = false;
  • vulkan-game.cpp

    r5ea0a37 r90880fb  
    7777                     , gui(nullptr)
    7878                     , window(nullptr)
     79                     , vp_mats_modelPipeline()
    7980                     , objects_modelPipeline()
     81                     , vp_mats_shipPipeline()
    8082                     , objects_shipPipeline()
     83                     , vp_mats_asteroidPipeline()
    8184                     , objects_asteroidPipeline()
     85                     , vp_mats_laserPipeline()
    8286                     , objects_laserPipeline()
     87                     , uniforms_explosionPipeline()
    8388                     , objects_explosionPipeline()
    8489                     , score(0)
    8590                     , fps(0.0f) {
    86    object_VP_mats = {};
    87    ship_VP_mats = {};
    88    asteroid_VP_mats = {};
    89    laser_VP_mats = {};
    90    explosion_UBO = {};
    9191}
    9292
     
    110110   vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties);
    111111
     112   vp_mats_modelPipeline = VulkanBuffer<UBO_VP_mats>(1, deviceProperties.limits.maxUniformBufferRange,
     113                                                     deviceProperties.limits.minUniformBufferOffsetAlignment);
     114
    112115   objects_modelPipeline = VulkanBuffer<SSBO_ModelObject>(10, deviceProperties.limits.maxStorageBufferRange,
    113116                                                          deviceProperties.limits.minStorageBufferOffsetAlignment);
     117
     118   vp_mats_shipPipeline = VulkanBuffer<UBO_VP_mats>(1, deviceProperties.limits.maxUniformBufferRange,
     119                                                    deviceProperties.limits.minUniformBufferOffsetAlignment);
     120
    114121   objects_shipPipeline = VulkanBuffer<SSBO_ModelObject>(10, deviceProperties.limits.maxStorageBufferRange,
    115122                                                         deviceProperties.limits.minStorageBufferOffsetAlignment);
     123
     124   vp_mats_asteroidPipeline = VulkanBuffer<UBO_VP_mats>(1, deviceProperties.limits.maxUniformBufferRange,
     125                                                        deviceProperties.limits.minUniformBufferOffsetAlignment);
     126
    116127   objects_asteroidPipeline = VulkanBuffer<SSBO_Asteroid>(10, deviceProperties.limits.maxStorageBufferRange,
    117128                                                          deviceProperties.limits.minStorageBufferOffsetAlignment);
     129
     130   vp_mats_laserPipeline = VulkanBuffer<UBO_VP_mats>(1, deviceProperties.limits.maxUniformBufferRange,
     131                                                     deviceProperties.limits.minUniformBufferOffsetAlignment);
     132
    118133   objects_laserPipeline = VulkanBuffer<SSBO_Laser>(2, deviceProperties.limits.maxStorageBufferRange,
    119134                                                    deviceProperties.limits.minStorageBufferOffsetAlignment);
     135
     136   uniforms_explosionPipeline = VulkanBuffer<UBO_Explosion>(1, deviceProperties.limits.maxUniformBufferRange,
     137                                                            deviceProperties.limits.minUniformBufferOffsetAlignment);
     138
    120139   objects_explosionPipeline = VulkanBuffer<SSBO_Explosion>(2, deviceProperties.limits.maxStorageBufferRange,
    121140                                                            deviceProperties.limits.minStorageBufferOffsetAlignment);
     
    138157   modelPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&ModelVertex::objIndex));
    139158
    140    createBufferSet(sizeof(UBO_VP_mats),
     159   createBufferSet(vp_mats_modelPipeline.memorySize(),
    141160                   VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    142161                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     
    208227   shipPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&ModelVertex::objIndex));
    209228
    210    createBufferSet(sizeof(UBO_VP_mats),
     229   createBufferSet(vp_mats_shipPipeline.memorySize(),
    211230                   VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    212231                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     
    475494   asteroidPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&ModelVertex::objIndex));
    476495
    477    createBufferSet(sizeof(UBO_VP_mats),
     496   createBufferSet(vp_mats_asteroidPipeline.memorySize(),
    478497                   VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    479498                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     
    500519   laserPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&LaserVertex::objIndex));
    501520
    502    createBufferSet(sizeof(UBO_VP_mats),
     521   createBufferSet(vp_mats_laserPipeline.memorySize(),
    503522                   VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    504523                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     
    527546   explosionPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&ExplosionVertex::objIndex));
    528547
    529    createBufferSet(sizeof(UBO_Explosion),
     548   createBufferSet(uniforms_explosionPipeline.memorySize(),
    530549                   VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    531550                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     
    684703   projMat = perspective(radians(FOV_ANGLE), (float)swapChainExtent.width / (float)swapChainExtent.height, NEAR_CLIP, FAR_CLIP);
    685704   projMat[1][1] *= -1; // flip the y-axis so that +y is up
    686 
    687    object_VP_mats.view = viewMat;
    688    object_VP_mats.proj = projMat;
    689 
    690    ship_VP_mats.view = viewMat;
    691    ship_VP_mats.proj = projMat;
    692 
    693    asteroid_VP_mats.view = viewMat;
    694    asteroid_VP_mats.proj = projMat;
    695 
    696    laser_VP_mats.view = viewMat;
    697    laser_VP_mats.proj = projMat;
    698 
    699    explosion_UBO.view = viewMat;
    700    explosion_UBO.proj = projMat;
    701705}
    702706
     
    940944// where it will run just once per frame
    941945void VulkanGame::updateScene() {
     946
     947   vp_mats_modelPipeline.data()->view = viewMat;
     948   vp_mats_modelPipeline.data()->proj = projMat;
     949
     950   vp_mats_shipPipeline.data()->view = viewMat;
     951   vp_mats_shipPipeline.data()->proj = projMat;
     952
     953   vp_mats_asteroidPipeline.data()->view = viewMat;
     954   vp_mats_asteroidPipeline.data()->proj = projMat;
     955
     956   vp_mats_laserPipeline.data()->view = viewMat;
     957   vp_mats_laserPipeline.data()->proj = projMat;
     958
     959   uniforms_explosionPipeline.data()->view = viewMat;
     960   uniforms_explosionPipeline.data()->proj = projMat;
     961   uniforms_explosionPipeline.data()->cur_time = curTime;
     962
     963   for (vector<BaseEffectOverTime*>::iterator it = effects.begin(); it != effects.end(); ) {
     964      if ((*it)->deleted) {
     965         delete *it;
     966         it = effects.erase(it);
     967      } else {
     968         BaseEffectOverTime* eot = *it;
     969
     970         eot->applyEffect(curTime);
     971
     972         it++;
     973      }
     974   }
     975
    942976   if (curTime - lastSpawn_asteroid > spawnRate_asteroid) {
    943977      lastSpawn_asteroid = curTime;
     
    10461080   }
    10471081
     1082   VulkanUtils::copyDataToMemory(device, vp_mats_modelPipeline.data(), uniformBuffers_modelPipeline.memory[imageIndex],
     1083                                 0, vp_mats_modelPipeline.memorySize(), false);
     1084
    10481085   VulkanUtils::copyDataToMemory(device, objects_modelPipeline.data(), storageBuffers_modelPipeline.memory[imageIndex],
    10491086                                 0, objects_modelPipeline.memorySize(), false);
     
    10661103      obj.center = vec3(objData.model * vec4(0.0f, 0.0f, 0.0f, 1.0f));
    10671104   }
     1105
     1106   VulkanUtils::copyDataToMemory(device, vp_mats_shipPipeline.data(), uniformBuffers_shipPipeline.memory[imageIndex],
     1107                                 0, vp_mats_shipPipeline.memorySize(), false);
    10681108
    10691109   VulkanUtils::copyDataToMemory(device, objects_shipPipeline.data(), storageBuffers_shipPipeline.memory[imageIndex],
     
    11111151   }
    11121152
     1153   VulkanUtils::copyDataToMemory(device, vp_mats_asteroidPipeline.data(), uniformBuffers_asteroidPipeline.memory[imageIndex],
     1154                                 0, vp_mats_asteroidPipeline.memorySize(), false);
     1155
    11131156   VulkanUtils::copyDataToMemory(device, objects_asteroidPipeline.data(), storageBuffers_asteroidPipeline.memory[imageIndex],
    11141157                                 0, objects_asteroidPipeline.memorySize(), false);
     
    11371180      obj.center = vec3(objData.model * vec4(0.0f, 0.0f, 0.0f, 1.0f));
    11381181   }
     1182
     1183   VulkanUtils::copyDataToMemory(device, vp_mats_laserPipeline.data(), uniformBuffers_laserPipeline.memory[imageIndex],
     1184                                 0, vp_mats_laserPipeline.memorySize(), false);
    11391185
    11401186   VulkanUtils::copyDataToMemory(device, objects_laserPipeline.data(), storageBuffers_laserPipeline.memory[imageIndex],
     
    11641210   }
    11651211
     1212   VulkanUtils::copyDataToMemory(device, uniforms_explosionPipeline.data(), uniformBuffers_explosionPipeline.memory[imageIndex],
     1213                                 0, uniforms_explosionPipeline.memorySize(), false);
     1214
    11661215   VulkanUtils::copyDataToMemory(device, objects_explosionPipeline.data(), storageBuffers_explosionPipeline.memory[imageIndex],
    11671216                                 0, objects_explosionPipeline.memorySize(), false);
    1168 
    1169    explosion_UBO.cur_time = curTime;
    1170 
    1171    for (vector<BaseEffectOverTime*>::iterator it = effects.begin(); it != effects.end(); ) {
    1172       if ((*it)->deleted) {
    1173          delete *it;
    1174          it = effects.erase(it);
    1175       } else {
    1176          BaseEffectOverTime* eot = *it;
    1177 
    1178          eot->applyEffect(curTime);
    1179 
    1180          it++;
    1181       }
    1182    }
    1183 
    1184    VulkanUtils::copyDataToMemory(device, &object_VP_mats, uniformBuffers_modelPipeline.memory[imageIndex], 0,
    1185                                  sizeof(object_VP_mats), false);
    1186 
    1187    VulkanUtils::copyDataToMemory(device, &ship_VP_mats, uniformBuffers_shipPipeline.memory[imageIndex], 0,
    1188                                  sizeof(ship_VP_mats), false);
    1189 
    1190    VulkanUtils::copyDataToMemory(device, &asteroid_VP_mats, uniformBuffers_asteroidPipeline.memory[imageIndex], 0,
    1191                                  sizeof(asteroid_VP_mats), false);
    1192 
    1193    VulkanUtils::copyDataToMemory(device, &laser_VP_mats, uniformBuffers_laserPipeline.memory[imageIndex], 0,
    1194                                  sizeof(laser_VP_mats), false);
    1195 
    1196    VulkanUtils::copyDataToMemory(device, &explosion_UBO, uniformBuffers_explosionPipeline.memory[imageIndex], 0,
    1197                                  sizeof(explosion_UBO), false);
    11981217}
    11991218
     
    22342253   createSyncObjects();
    22352254
    2236    createBufferSet(sizeof(UBO_VP_mats),
     2255   createBufferSet(vp_mats_modelPipeline.memorySize(),
    22372256                   VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    22382257                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     
    22502269   modelPipeline.createDescriptorSets(swapChainImages.size());
    22512270
    2252    createBufferSet(sizeof(UBO_VP_mats),
     2271   createBufferSet(vp_mats_shipPipeline.memorySize(),
    22532272                   VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    22542273                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     
    22662285   shipPipeline.createDescriptorSets(swapChainImages.size());
    22672286
    2268    createBufferSet(sizeof(UBO_VP_mats),
     2287   createBufferSet(vp_mats_asteroidPipeline.memorySize(),
    22692288                   VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    22702289                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     
    22822301   asteroidPipeline.createDescriptorSets(swapChainImages.size());
    22832302
    2284    createBufferSet(sizeof(UBO_VP_mats),
     2303   createBufferSet(vp_mats_laserPipeline.memorySize(),
    22852304                   VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    22862305                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     
    22982317   laserPipeline.createDescriptorSets(swapChainImages.size());
    22992318
    2300    createBufferSet(sizeof(UBO_Explosion),
     2319   createBufferSet(uniforms_explosionPipeline.memorySize(),
    23012320                   VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    23022321                   VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
  • vulkan-game.hpp

    r5ea0a37 r90880fb  
    311311      GraphicsPipeline_Vulkan<ExplosionVertex> explosionPipeline;
    312312
     313      BufferSet uniformBuffers_modelPipeline;
    313314      BufferSet storageBuffers_modelPipeline;
     315
     316      VulkanBuffer<UBO_VP_mats> vp_mats_modelPipeline;
    314317      VulkanBuffer<SSBO_ModelObject> objects_modelPipeline;
    315       BufferSet uniformBuffers_modelPipeline;
    316 
     318
     319      BufferSet uniformBuffers_shipPipeline;
    317320      BufferSet storageBuffers_shipPipeline;
     321
     322      VulkanBuffer<UBO_VP_mats> vp_mats_shipPipeline;
    318323      VulkanBuffer<SSBO_ModelObject> objects_shipPipeline;
    319       BufferSet uniformBuffers_shipPipeline;
    320 
     324
     325      BufferSet uniformBuffers_asteroidPipeline;
    321326      BufferSet storageBuffers_asteroidPipeline;
     327
     328      VulkanBuffer<UBO_VP_mats> vp_mats_asteroidPipeline;
    322329      VulkanBuffer<SSBO_Asteroid> objects_asteroidPipeline;
    323       BufferSet uniformBuffers_asteroidPipeline;
    324 
     330
     331      BufferSet uniformBuffers_laserPipeline;
    325332      BufferSet storageBuffers_laserPipeline;
     333
     334      VulkanBuffer<UBO_VP_mats> vp_mats_laserPipeline;
    326335      VulkanBuffer<SSBO_Laser> objects_laserPipeline;
    327       BufferSet uniformBuffers_laserPipeline;
    328 
     336
     337      BufferSet uniformBuffers_explosionPipeline;
    329338      BufferSet storageBuffers_explosionPipeline;
     339
     340      VulkanBuffer<UBO_Explosion> uniforms_explosionPipeline;
    330341      VulkanBuffer<SSBO_Explosion> objects_explosionPipeline;
    331       BufferSet uniformBuffers_explosionPipeline;
    332342
    333343      // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
     
    340350
    341351      vector<SceneObject<ModelVertex>> modelObjects;
    342 
    343       UBO_VP_mats object_VP_mats;
    344 
    345352      vector<SceneObject<ModelVertex>> shipObjects;
    346 
    347       UBO_VP_mats ship_VP_mats;
    348 
    349353      vector<SceneObject<ModelVertex>> asteroidObjects;
    350 
    351       UBO_VP_mats asteroid_VP_mats;
    352 
    353354      vector<SceneObject<LaserVertex>> laserObjects;
    354 
    355       UBO_VP_mats laser_VP_mats;
    356 
    357355      vector<SceneObject<ExplosionVertex>> explosionObjects;
    358356
    359       UBO_Explosion explosion_UBO;
     357      //UBO_Explosion explosion_UBO;
    360358
    361359      vector<BaseEffectOverTime*> effects;
Note: See TracChangeset for help on using the changeset viewer.