Changeset 055750a in opengl-game


Ignore:
Timestamp:
Dec 4, 2019, 9:33:43 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
785333b, cf727ca
Parents:
e1308e8
Message:

In VulkanGame, use SSBOs in the ship and scene shaders to store per-object data (currently just the model matrix)

Files:
7 edited

Legend:

Unmodified
Added
Removed
  • graphics-pipeline_vulkan.hpp

    re1308e8 r055750a  
    376376         switch (descriptorWrites[j].descriptorType) {
    377377            case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
     378            case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
    378379               descriptorWrites[j].pBufferInfo = &(*this->descriptorInfoList[j].bufferDataList)[i];
    379380               break;
  • shaders/scene.frag

    re1308e8 r055750a  
    22#extension GL_ARB_separate_shader_objects : enable
    33
    4 layout(binding = 1) uniform sampler2D texSampler;
     4layout(binding = 2) uniform sampler2D texSampler;
    55
    66layout(location = 0) in vec3 fragColor;
  • shaders/scene.vert

    re1308e8 r055750a  
    22#extension GL_ARB_separate_shader_objects : enable
    33
     4struct Object {
     5   mat4 model;
     6};
     7
    48layout (binding = 0) uniform UniformBufferObject {
    5    mat4 model;
    69   mat4 view;
    710   mat4 proj;
    811} ubo;
     12
     13layout(binding = 1) readonly buffer StorageBufferObject {
     14    Object objects[];
     15} sbo;
    916
    1017layout(location = 0) in vec3 inPosition;
     
    1926   fragTexCoord = inTexCoord;
    2027
    21    gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);
     28   gl_Position = ubo.proj * ubo.view * sbo.objects[0].model * vec4(inPosition, 1.0);
    2229}
  • shaders/ship.vert

    re1308e8 r055750a  
    22#extension GL_ARB_separate_shader_objects : enable
    33
     4struct Object {
     5   mat4 model;
     6};
     7
    48layout (binding = 0) uniform UniformBufferObject {
    5    mat4 model;
    69   mat4 view;
    710   mat4 proj;
    811} ubo;
     12
     13layout(binding = 1) readonly buffer StorageBufferObject {
     14    Object objects[];
     15} sbo;
    916
    1017layout(location = 0) in vec3 vertex_position;
     
    2633// Check Anton's book to see how to fix this
    2734void main() {
    28    position_eye = vec3(ubo.view * ubo.model * vec4(vertex_position, 1.0));
     35   position_eye = vec3(ubo.view * sbo.objects[0].model * vec4(vertex_position, 1.0));
    2936   //position_eye = vec3(view * model_mats[ubo_index] * vec4(vertex_position, 1.0));
    3037
    3138   // Using 0.0 instead of 1.0 means translations won't effect the normal
    32    normal_eye = normalize(vec3(ubo.view * ubo.model * vec4(vertex_normal, 0.0)));
     39   normal_eye = normalize(vec3(ubo.view * sbo.objects[0].model * vec4(vertex_normal, 0.0)));
    3340   //normal_eye = normalize(vec3(view * model_mats[ubo_index] * vec4(vertex_normal, 0.0)));
    3441   color = vertex_color;
  • vulkan-game.cpp

    re1308e8 r055750a  
    2626   framebufferResized = false;
    2727
    28    modelMvpMats = {};
    29    shipMvpMats = {};
     28   object_VP_mats = {};
     29   ship_VP_mats = {};
    3030}
    3131
     
    196196   modelPipeline.addAttribute(VK_FORMAT_R32G32_SFLOAT, offset_of(&ModelVertex::texCoord));
    197197
    198    createUniformBuffers<UBO_MvpMat>(uniformBuffers, uniformBuffersMemory, uniformBufferInfoList);
     198   createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
     199      uniformBuffers_scenePipeline, uniformBuffersMemory_scenePipeline, uniformBufferInfoList_scenePipeline);
     200   createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     201      storageBuffers_scenePipeline, storageBuffersMemory_scenePipeline, storageBufferInfoList_scenePipeline);
    199202
    200203   modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
    201       VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList);
     204      VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_scenePipeline);
     205   modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
     206      VK_SHADER_STAGE_VERTEX_BIT, &storageBufferInfoList_scenePipeline);
     207
    202208   modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
    203209      VK_SHADER_STAGE_FRAGMENT_BIT, &floorTextureImageDescriptor);
     
    250256   shipPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ShipVertex::normal));
    251257
    252    createUniformBuffers<UBO_MvpMat>(uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline);
     258   createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
     259      uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline);
     260   createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     261      storageBuffers_shipPipeline, storageBuffersMemory_shipPipeline, storageBufferInfoList_shipPipeline);
    253262
    254263   shipPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
    255264      VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_shipPipeline);
     265   shipPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
     266      VK_SHADER_STAGE_VERTEX_BIT, &storageBufferInfoList_shipPipeline);
    256267
    257268   // TODO: With the normals, indexing basically becomes pointless since no vertices will have exactly
     
    525536   proj[1][1] *= -1; // flip the y-axis so that +y is up
    526537
    527    modelMvpMats.model = mat4(1.0f);
    528    modelMvpMats.view = view;
    529    modelMvpMats.proj = proj;
    530 
    531    mat4 T_model = translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f));
    532    mat4 R_model(1.0f);
    533 
    534    shipMvpMats.model = T_model * R_model * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
    535    shipMvpMats.view = view;
    536    shipMvpMats.proj = proj;
     538   object_VP_mats.view = view;
     539   object_VP_mats.proj = proj;
     540
     541   ship_VP_mats.view = view;
     542   ship_VP_mats.proj = proj;
    537543}
    538544
     
    607613}
    608614
     615// TODO: The view and projection mats only need to be updated once.
     616// Create a separate function that can do this once per Vulkan image at the beginning
    609617void VulkanGame::updateScene(uint32_t currentImage) {
    610618   static auto startTime = chrono::high_resolution_clock::now();
     
    613621   float time = chrono::duration<float, chrono::seconds::period>(currentTime - startTime).count();
    614622
    615    modelMvpMats.model =
     623   so_Object.model =
    616624      translate(mat4(1.0f), vec3(0.0f, -2.0f, -0.0f)) *
    617625      rotate(mat4(1.0f), time * radians(90.0f), vec3(0.0f, 0.0f, 1.0f));
    618626
    619    VulkanUtils::copyDataToMemory(device, uniformBuffersMemory[currentImage], modelMvpMats);
    620 
    621    VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_shipPipeline[currentImage], shipMvpMats);
     627   so_Ship.model =
     628      translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f)) *
     629      scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
     630
     631   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_scenePipeline[currentImage], object_VP_mats);
     632
     633   VulkanUtils::copyDataToMemory(device, storageBuffersMemory_scenePipeline[currentImage], so_Object);
     634
     635   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_shipPipeline[currentImage], ship_VP_mats);
     636
     637   VulkanUtils::copyDataToMemory(device, storageBuffersMemory_shipPipeline[currentImage], so_Ship);
    622638}
    623639
     
    901917
    902918void VulkanGame::createLogicalDevice(
    903       const vector<const char*> validationLayers,
    904       const vector<const char*>& deviceExtensions) {
     919      const vector<const char*> validationLayers, const vector<const char*>& deviceExtensions) {
    905920   QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
    906921
     
    12371252}
    12381253
     1254void VulkanGame::createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
     1255      vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList) {
     1256   buffers.resize(swapChainImages.size());
     1257   buffersMemory.resize(swapChainImages.size());
     1258   bufferInfoList.resize(swapChainImages.size());
     1259
     1260   for (size_t i = 0; i < swapChainImages.size(); i++) {
     1261      VulkanUtils::createBuffer(device, physicalDevice, bufferSize, flags,
     1262         VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     1263         buffers[i], buffersMemory[i]);
     1264
     1265      bufferInfoList[i].buffer = buffers[i];
     1266      bufferInfoList[i].offset = 0; // This is the offset from the start of the buffer, so always 0 for now
     1267      bufferInfoList[i].range = bufferSize; // Size of the update starting from offset, or VK_WHOLE_SIZE
     1268   }
     1269}
     1270
    12391271// TODO: Fix the crash that happens when alt-tabbing
    12401272void VulkanGame::recreateSwapChain() {
     
    12601292   createFramebuffers();
    12611293
    1262    createUniformBuffers<UBO_MvpMat>(uniformBuffers, uniformBuffersMemory, uniformBufferInfoList);
     1294   createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
     1295      uniformBuffers_scenePipeline, uniformBuffersMemory_scenePipeline, uniformBufferInfoList_scenePipeline);
     1296   createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     1297      storageBuffers_scenePipeline, storageBuffersMemory_scenePipeline, storageBufferInfoList_scenePipeline);
    12631298
    12641299   modelPipeline.updateRenderPass(renderPass);
     
    12721307   overlayPipeline.createDescriptorSets(swapChainImages);
    12731308
    1274    createUniformBuffers<UBO_MvpMat>(uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline);
     1309   createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
     1310      uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline);
     1311   createBufferSet(10 * sizeof(SBO_SceneObject), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
     1312      storageBuffers_shipPipeline, storageBuffersMemory_shipPipeline, storageBufferInfoList_shipPipeline);
    12751313
    12761314   shipPipeline.updateRenderPass(renderPass);
     
    13031341   vkDestroySwapchainKHR(device, swapChain, nullptr);
    13041342
    1305    for (size_t i = 0; i < uniformBuffers.size(); i++) {
    1306       vkDestroyBuffer(device, uniformBuffers[i], nullptr);
    1307       vkFreeMemory(device, uniformBuffersMemory[i], nullptr);
     1343   for (size_t i = 0; i < uniformBuffers_scenePipeline.size(); i++) {
     1344      vkDestroyBuffer(device, uniformBuffers_scenePipeline[i], nullptr);
     1345      vkFreeMemory(device, uniformBuffersMemory_scenePipeline[i], nullptr);
     1346   }
     1347
     1348   for (size_t i = 0; i < storageBuffers_scenePipeline.size(); i++) {
     1349      vkDestroyBuffer(device, storageBuffers_scenePipeline[i], nullptr);
     1350      vkFreeMemory(device, storageBuffersMemory_scenePipeline[i], nullptr);
    13081351   }
    13091352
     
    13121355      vkFreeMemory(device, uniformBuffersMemory_shipPipeline[i], nullptr);
    13131356   }
    1314 }
     1357
     1358   for (size_t i = 0; i < storageBuffers_shipPipeline.size(); i++) {
     1359      vkDestroyBuffer(device, storageBuffers_shipPipeline[i], nullptr);
     1360      vkFreeMemory(device, storageBuffersMemory_shipPipeline[i], nullptr);
     1361   }
     1362}
  • vulkan-game.hpp

    re1308e8 r055750a  
    3535};
    3636
    37 struct UBO_MvpMat {
    38    alignas(16) mat4 model;
     37struct UBO_VP_mats {
    3938   alignas(16) mat4 view;
    4039   alignas(16) mat4 proj;
     40};
     41
     42struct SBO_SceneObject {
     43   alignas(16) mat4 model;
    4144};
    4245
     
    5760      vec3 cam_pos;
    5861
    59       UBO_MvpMat modelMvpMats;
    60       UBO_MvpMat shipMvpMats;
     62      UBO_VP_mats object_VP_mats;
     63      SBO_SceneObject so_Object;
     64
     65      UBO_VP_mats ship_VP_mats;
     66      SBO_SceneObject so_Ship;
    6167
    6268      GameGui* gui;
     
    96102      VkSampler textureSampler;
    97103
    98       // These are currently to store the MVP matrix
    99       // I should figure out if it makes sense to use them for other uniforms in the future
    100       // If not, I should rename them to better indicate their purpose.
    101       vector<VkBuffer> uniformBuffers;
    102       vector<VkDeviceMemory> uniformBuffersMemory;
    103 
    104       vector<VkDescriptorBufferInfo> uniformBufferInfoList;
     104      // TODO: I should probably rename the uniformBuffer* and storageBuffer*
     105      // variables to better reflect the data they hold
     106
     107      vector<VkBuffer> uniformBuffers_scenePipeline;
     108      vector<VkDeviceMemory> uniformBuffersMemory_scenePipeline;
     109
     110      vector<VkDescriptorBufferInfo> uniformBufferInfoList_scenePipeline;
     111
     112      vector<VkBuffer> storageBuffers_scenePipeline;
     113      vector<VkDeviceMemory> storageBuffersMemory_scenePipeline;
     114
     115      vector<VkDescriptorBufferInfo> storageBufferInfoList_scenePipeline;
    105116
    106117      vector<VkBuffer> uniformBuffers_shipPipeline;
     
    108119
    109120      vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
     121
     122      vector<VkBuffer> storageBuffers_shipPipeline;
     123      vector<VkDeviceMemory> storageBuffersMemory_shipPipeline;
     124
     125      vector<VkDescriptorBufferInfo> storageBufferInfoList_shipPipeline;
    110126
    111127      VulkanImage floorTextureImage;
     
    162178      vector<VertexType> addVertexNormals(vector<VertexType> vertices);
    163179
    164       template<class UniformType>
    165       void createUniformBuffers(vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
    166          vector<VkDescriptorBufferInfo>& bufferInfoList);
     180      void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
     181         vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList);
    167182
    168183      void recreateSwapChain();
     
    176191            void* pUserData);
    177192};
    178 
    179 template<class UniformType>
    180 void VulkanGame::createUniformBuffers(vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
    181       vector<VkDescriptorBufferInfo>& bufferInfoList) {
    182    buffers.resize(swapChainImages.size());
    183    buffersMemory.resize(swapChainImages.size());
    184    bufferInfoList.resize(swapChainImages.size());
    185 
    186    for (size_t i = 0; i < swapChainImages.size(); i++) {
    187       VulkanUtils::createBuffer(device, physicalDevice, sizeof(UniformType), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    188          VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    189          buffers[i], buffersMemory[i]);
    190 
    191       bufferInfoList[i].buffer = buffers[i];
    192       bufferInfoList[i].offset = 0;
    193       bufferInfoList[i].range = sizeof(UniformType);
    194    }
    195 }
    196193
    197194template<class VertexType>
  • vulkan-utils.hpp

    re1308e8 r055750a  
    125125}
    126126
     127// TODO: Change copyDataToMemory so it can accept offsets for the data source and destination
    127128template<class DataType>
    128129void VulkanUtils::copyDataToMemory(VkDevice device, VkDeviceMemory bufferMemory, const DataType& srcData) {
Note: See TracChangeset for help on using the changeset viewer.