Changeset 5a1ace0 in opengl-game


Ignore:
Timestamp:
Feb 16, 2020, 8:18:50 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
2d87297
Parents:
4ece3bf
git-author:
Dmitry Portnoy <dmitry.portnoy@…> (02/14/20 20:41:35)
git-committer:
Dmitry Portnoy <dmitry.portnoy@…> (02/16/20 20:18:50)
Message:

In VulkanGame, add objIndex to scene objects, use it in the scene shader to index into the ssbo, and change the code that copies data to the ssbo to do so for each scene object, not just the first one

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • shaders/scene.vert

    r4ece3bf r5a1ace0  
    1818layout(location = 1) in vec3 inColor;
    1919layout(location = 2) in vec2 inTexCoord;
     20layout(location = 3) in uint obj_index;
    2021
    2122layout(location = 0) out vec3 fragColor;
     
    2627   fragTexCoord = inTexCoord;
    2728
    28    gl_Position = ubo.proj * ubo.view * sbo.objects[0].model * vec4(inPosition, 1.0);
     29   gl_Position = ubo.proj * ubo.view * sbo.objects[obj_index].model * vec4(inPosition, 1.0);
    2930}
  • vulkan-game.cpp

    r4ece3bf r5a1ace0  
    197197   modelPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ModelVertex::color));
    198198   modelPipeline.addAttribute(VK_FORMAT_R32G32_SFLOAT, offset_of(&ModelVertex::texCoord));
     199   modelPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&ModelVertex::objIndex));
    199200
    200201   createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
     
    213214
    214215   addObject(modelObjects, modelPipeline,
    215       {
     216      addObjectIndex<ModelVertex>(modelObjects.size(), {
    216217         {{-0.5f, -0.5f, -2.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
    217218         {{ 0.5f, -0.5f, -2.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
    218219         {{ 0.5f,  0.5f, -2.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
    219220         {{-0.5f,  0.5f, -2.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
    220       }, {
     221      }), {
    221222         0, 1, 2, 2, 3, 0
    222223      });
    223224
    224225   addObject(modelObjects, modelPipeline,
    225       {
     226      addObjectIndex<ModelVertex>(modelObjects.size(), {
    226227         {{-0.5f, -0.5f, -1.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
    227228         {{ 0.5f, -0.5f, -1.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
    228229         {{ 0.5f,  0.5f, -1.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
    229230         {{-0.5f,  0.5f, -1.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
    230       }, {
     231      }), {
    231232         0, 1, 2, 2, 3, 0
    232233      });
     
    699700
    700701                  addObject(modelObjects, modelPipeline,
    701                      {
     702                     addObjectIndex<ModelVertex>(modelObjects.size(), {
    702703                        {{-0.5f, -0.5f,  zOffset}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
    703704                        {{ 0.5f, -0.5f,  zOffset}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
    704705                        {{ 0.5f,  0.5f,  zOffset}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
    705706                        {{-0.5f,  0.5f,  zOffset}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
    706                      }, {
     707                     }), {
    707708                        0, 1, 2, 2, 3, 0
    708709                     });
     
    774775   so_Asteroid.model = asteroidObjects[0].model_transform * asteroidObjects[0].model_base;
    775776
    776    VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_scenePipeline[currentImage], object_VP_mats);
    777 
    778    VulkanUtils::copyDataToMemory(device, storageBuffersMemory_scenePipeline[currentImage], so_Object);
    779 
    780    VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_shipPipeline[currentImage], ship_VP_mats);
    781 
    782    VulkanUtils::copyDataToMemory(device, storageBuffersMemory_shipPipeline[currentImage], so_Ship);
    783 
    784    VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_asteroidPipeline[currentImage], asteroid_VP_mats);
    785 
    786    VulkanUtils::copyDataToMemory(device, storageBuffersMemory_asteroidPipeline[currentImage], so_Asteroid);
     777   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_scenePipeline[currentImage], 0, object_VP_mats);
     778
     779   for (size_t i = 0; i < modelObjects.size(); i++) {
     780      VulkanUtils::copyDataToMemory(device, storageBuffersMemory_scenePipeline[currentImage],
     781         i, so_Object);
     782   }
     783
     784   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_shipPipeline[currentImage], 0, ship_VP_mats);
     785
     786   VulkanUtils::copyDataToMemory(device, storageBuffersMemory_shipPipeline[currentImage], 0, so_Ship);
     787
     788   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_asteroidPipeline[currentImage], 0, asteroid_VP_mats);
     789
     790   VulkanUtils::copyDataToMemory(device, storageBuffersMemory_asteroidPipeline[currentImage], 0, so_Asteroid);
    787791}
    788792
  • vulkan-game.hpp

    r4ece3bf r5a1ace0  
    2222#endif
    2323
     24struct OverlayVertex {
     25   vec3 pos;
     26   vec2 texCoord;
     27};
     28
    2429struct ModelVertex {
    2530   vec3 pos;
    2631   vec3 color;
    2732   vec2 texCoord;
    28 };
    29 
    30 struct OverlayVertex {
    31    vec3 pos;
    32    vec2 texCoord;
     33   unsigned int objIndex;
    3334};
    3435
  • vulkan-utils.hpp

    r4ece3bf r5a1ace0  
    9595
    9696      template<class DataType>
    97       static void copyDataToMemory(VkDevice device, VkDeviceMemory bufferMemory, const DataType& srcData);
     97      static void copyDataToMemory(VkDevice device, VkDeviceMemory bufferMemory, VkDeviceSize offset,
     98            const DataType& srcData);
    9899
    99100      static bool hasStencilComponent(VkFormat format);
     
    125126}
    126127
    127 // TODO: Change copyDataToMemory so it can accept offsets for the data source and destination
    128128template<class DataType>
    129 void VulkanUtils::copyDataToMemory(VkDevice device, VkDeviceMemory bufferMemory, const DataType& srcData) {
     129void VulkanUtils::copyDataToMemory(VkDevice device, VkDeviceMemory bufferMemory, VkDeviceSize offset,
     130      const DataType& srcData) {
    130131   void* data;
    131    vkMapMemory(device, bufferMemory, 0, sizeof(DataType), 0, &data);
     132
     133   vkMapMemory(device, bufferMemory, offset * sizeof(DataType), sizeof(DataType), 0, &data);
    132134   memcpy(data, &srcData, sizeof(DataType));
    133135   vkUnmapMemory(device, bufferMemory);
Note: See TracChangeset for help on using the changeset viewer.