Changeset f97c5e7 in opengl-game


Ignore:
Timestamp:
Nov 22, 2019, 7:58:45 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
aa00bf2
Parents:
8e02b6b
Message:

Create an initGraphicsPipelines() function and templatize and generalize the createUniformBuffers() functionso it can be used to create a uniform buffer of any type

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    r8e02b6b rf97c5e7  
    184184
    185185   createImageResources();
    186 
    187186   createFramebuffers();
    188    createUniformBuffers();
    189 
    190    modelPipeline = GraphicsPipeline_Vulkan<ModelVertex>(physicalDevice, device, renderPass,
    191       { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 16, 24);
     187
     188   // TODO: Figure out how much of ubo creation and associated variables should be in the pipeline class
     189   // Maybe combine the ubo-related objects into a new class
     190
     191   initGraphicsPipelines();
     192
     193   modelPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ModelVertex::pos));
     194   modelPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ModelVertex::color));
     195   modelPipeline.addAttribute(VK_FORMAT_R32G32_SFLOAT, offset_of(&ModelVertex::texCoord));
     196
     197   createUniformBuffers<UBO_MvpMat>(uniformBuffers, uniformBuffersMemory, uniformBufferInfoList);
     198
     199   modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
     200      VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList);
     201   modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
     202      VK_SHADER_STAGE_FRAGMENT_BIT, &floorTextureImageDescriptor);
    192203
    193204   modelPipeline.addObject({
     
    209220      }, commandPool, graphicsQueue);
    210221
    211    modelPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ModelVertex::pos));
    212    modelPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ModelVertex::color));
    213    modelPipeline.addAttribute(VK_FORMAT_R32G32_SFLOAT, offset_of(&ModelVertex::texCoord));
    214 
    215    modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
    216       VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList);
    217    modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
    218       VK_SHADER_STAGE_FRAGMENT_BIT, &floorTextureImageDescriptor);
    219 
    220222   modelPipeline.createDescriptorSetLayout();
    221223   modelPipeline.createPipeline("shaders/scene-vert.spv", "shaders/scene-frag.spv");
     
    223225   modelPipeline.createDescriptorSets(swapChainImages);
    224226
    225    overlayPipeline = GraphicsPipeline_Vulkan<OverlayVertex>(physicalDevice, device, renderPass,
    226       { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 4, 6);
     227   overlayPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&OverlayVertex::pos));
     228   overlayPipeline.addAttribute(VK_FORMAT_R32G32_SFLOAT, offset_of(&OverlayVertex::texCoord));
     229
     230   overlayPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
     231      VK_SHADER_STAGE_FRAGMENT_BIT, &sdlOverlayImageDescriptor);
    227232
    228233   overlayPipeline.addObject({
     
    235240      }, commandPool, graphicsQueue);
    236241
    237    overlayPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&OverlayVertex::pos));
    238    overlayPipeline.addAttribute(VK_FORMAT_R32G32_SFLOAT, offset_of(&OverlayVertex::texCoord));
    239 
    240    overlayPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
    241       VK_SHADER_STAGE_FRAGMENT_BIT, &sdlOverlayImageDescriptor);
    242 
    243242   overlayPipeline.createDescriptorSetLayout();
    244243   overlayPipeline.createPipeline("shaders/overlay-vert.spv", "shaders/overlay-frag.spv");
     
    253252}
    254253
     254void VulkanGame::initGraphicsPipelines() {
     255   modelPipeline = GraphicsPipeline_Vulkan<ModelVertex>(physicalDevice, device, renderPass,
     256      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 16, 24);
     257
     258   overlayPipeline = GraphicsPipeline_Vulkan<OverlayVertex>(physicalDevice, device, renderPass,
     259      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 4, 6);
     260}
     261
    255262// TODO: Maybe changes the name to initScene() or something similar
    256263void VulkanGame::initMatrices() {
     
    263270   mat4 pitch_mat = rotate(mat4(1.0f), radians(-cam_pitch), vec3(1.0f, 0.0f, 0.0f));
    264271
    265    mat4 R = pitch_mat * yaw_mat;
    266    mat4 T = translate(mat4(1.0f), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
    267 
    268    modelMvpMats.view = R * T;
    269 
    270    modelMvpMats.proj = perspective(radians(FOV_ANGLE), (float)swapChainExtent.width / (float)swapChainExtent.height, NEAR_CLIP, FAR_CLIP);
    271    modelMvpMats.proj[1][1] *= -1; // flip the y-axis so that +y is up
     272   mat4 R_view = pitch_mat * yaw_mat;
     273   mat4 T_view = translate(mat4(1.0f), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
     274   mat4 view = R_view * T_view;
     275
     276   mat4 proj = perspective(radians(FOV_ANGLE), (float)swapChainExtent.width / (float)swapChainExtent.height, NEAR_CLIP, FAR_CLIP);
     277   proj[1][1] *= -1; // flip the y-axis so that +y is up
     278
     279   modelMvpMats.model = mat4(1.0f);
     280   modelMvpMats.view = view;
     281   modelMvpMats.proj = proj;
    272282}
    273283
     
    895905}
    896906
    897 void VulkanGame::createUniformBuffers() {
    898    VkDeviceSize bufferSize = sizeof(UBO_MvpMat);
    899 
    900    uniformBuffers.resize(swapChainImages.size());
    901    uniformBuffersMemory.resize(swapChainImages.size());
    902    uniformBufferInfoList.resize(swapChainImages.size());
    903 
    904    for (size_t i = 0; i < swapChainImages.size(); i++) {
    905       VulkanUtils::createBuffer(device, physicalDevice, bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
    906          VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    907          uniformBuffers[i], uniformBuffersMemory[i]);
    908 
    909       uniformBufferInfoList[i].buffer = uniformBuffers[i];
    910       uniformBufferInfoList[i].offset = 0;
    911       uniformBufferInfoList[i].range = sizeof(UBO_MvpMat);
    912    }
    913 }
    914 
    915907void VulkanGame::createCommandBuffers() {
    916908   commandBuffers.resize(swapChainImages.size());
     
    1006998      depthImage, graphicsQueue);
    1007999   createFramebuffers();
    1008    createUniformBuffers();
     1000
     1001   createUniformBuffers<UBO_MvpMat>(uniformBuffers, uniformBuffersMemory, uniformBufferInfoList);
    10091002
    10101003   modelPipeline.updateRenderPass(renderPass);
  • vulkan-game.hpp

    r8e02b6b rf97c5e7  
    8888      VkSampler textureSampler;
    8989
    90       vector<VkDescriptorBufferInfo> uniformBufferInfoList;
    91 
    9290      // These are currently to store the MVP matrix
    9391      // I should figure out if it makes sense to use them for other uniforms in the future
    9492      // If not, I should rename them to better indicate their purpose.
    95       // I should also decide if I can use these for all shaders, or if I need a separapte set of buffers for each one
    9693      vector<VkBuffer> uniformBuffers;
    9794      vector<VkDeviceMemory> uniformBuffersMemory;
     95
     96      vector<VkDescriptorBufferInfo> uniformBufferInfoList;
    9897
    9998      VulkanImage floorTextureImage;
     
    118117      bool initWindow(int width, int height, unsigned char guiFlags);
    119118      void initVulkan();
     119      void initGraphicsPipelines();
    120120      void initMatrices();
    121121      void mainLoop();
     
    143143      void createTextureSampler();
    144144      void createFramebuffers();
    145       void createUniformBuffers();
    146145      void createCommandBuffers();
    147146      void createSyncObjects();
     147
     148      template<class UniformType>
     149      void createUniformBuffers(vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
     150         vector<VkDescriptorBufferInfo>& bufferInfoList);
    148151
    149152      void recreateSwapChain();
     
    158161};
    159162
     163template<class UniformType>
     164void VulkanGame::createUniformBuffers(vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
     165      vector<VkDescriptorBufferInfo>& bufferInfoList) {
     166   buffers.resize(swapChainImages.size());
     167   buffersMemory.resize(swapChainImages.size());
     168   bufferInfoList.resize(swapChainImages.size());
     169
     170   for (size_t i = 0; i < swapChainImages.size(); i++) {
     171      VulkanUtils::createBuffer(device, physicalDevice, sizeof(UniformType), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
     172         VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     173         buffers[i], buffersMemory[i]);
     174
     175      bufferInfoList[i].buffer = buffers[i];
     176      bufferInfoList[i].offset = 0;
     177      bufferInfoList[i].range = sizeof(UniformType);
     178   }
     179}
     180
    160181#endif // _VULKAN_GAME_H
Note: See TracChangeset for help on using the changeset viewer.