Changeset e3bef3a in opengl-game


Ignore:
Timestamp:
Nov 12, 2019, 6:55:07 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
cd487fb
Parents:
5a23277
Message:

Finish the rewrite of the original vulkangame project

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • graphics-pipeline_vulkan.cpp

    r5a23277 re3bef3a  
    44#include <stdexcept>
    55#include <iostream>
    6 
    7 #include "vulkan-utils.hpp"
    86
    97using namespace std;
  • graphics-pipeline_vulkan.hpp

    r5a23277 re3bef3a  
    44#include "graphics-pipeline.hpp"
    55
     6#include <iostream>
    67#include <vector>
    78
    89#include <vulkan/vulkan.h>
     10
     11#include "vulkan-utils.hpp"
     12
     13using namespace std;
     14
     15// TODO: Remove any instances of cout and instead throw exceptions
    916
    1017// TODO: Maybe change the name of this struct so I can call the list something other than descriptorInfoList
     
    2633      void updateRenderPass(VkRenderPass renderPass);
    2734
    28       template<class VertexType, class IndexType>
    29       void bindData(const vector<VertexType>& vertices, const vector<IndexType>& indices,
     35      template<class VertexType>
     36      void bindData(const vector<VertexType>& vertices, const vector<uint16_t>& indices,
    3037         VkCommandPool commandPool, VkQueue graphicsQueue);
    3138
     
    4855
    4956      void createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage);
     57
     58      template<class VertexType>
     59      bool addObject(const vector<VertexType>& vertices, vector<uint16_t>& indices, VkCommandPool commandPool,
     60         VkQueue graphicsQueue);
    5061
    5162      void cleanup();
     
    8394};
    8495
    85 // TODO: Probably better to template the whole class and to also combine this function
    86 // and the constructor since I call this right after the constructor anyway
    87 template<class VertexType, class IndexType>
    88 void GraphicsPipeline_Vulkan::bindData(const vector<VertexType>& vertices, const vector<IndexType>& indices,
     96// TODO: Probably better to template the whole class
     97// TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
     98
     99// TODO: combine this function and the constructor since I call this right after the constructor anyway
     100template<class VertexType>
     101void GraphicsPipeline_Vulkan::bindData(const vector<VertexType>& vertices, const vector<uint16_t>& indices,
    89102      VkCommandPool commandPool, VkQueue graphicsQueue) {
    90103   numVertices = vertices.size();
     
    94107   numIndices = indices.size();
    95108   indexCapacity = numIndices * 2;
    96    createIndexBuffer(indices.data(), sizeof(IndexType), commandPool, graphicsQueue);
     109   createIndexBuffer(indices.data(), sizeof(uint16_t), commandPool, graphicsQueue);
     110}
     111
     112template<class VertexType>
     113bool GraphicsPipeline_Vulkan::addObject(const vector<VertexType>& vertices, vector<uint16_t>& indices,
     114      VkCommandPool commandPool, VkQueue graphicsQueue) {
     115   cout << "Adding object to pipeline..." << endl;
     116
     117   if (numVertices + vertices.size() > vertexCapacity) {
     118      cout << "ERROR: Need to resize vertex buffers" << endl;
     119   } else if (numIndices + indices.size() > indexCapacity) {
     120      cout << "ERROR: Need to resize index buffers" << endl;
     121   } else {
     122      cout << "Added object to scene" << endl;
     123
     124      for (uint16_t& idx : indices) {
     125         idx += numVertices;
     126      }
     127
     128      VulkanUtils::copyDataToBuffer(device, physicalDevice, commandPool, vertices, vertexBuffer, numVertices,
     129         graphicsQueue);
     130      numVertices += vertices.size();
     131
     132      VulkanUtils::copyDataToBuffer(device, physicalDevice, commandPool, indices, indexBuffer, numIndices,
     133         graphicsQueue);
     134      numIndices += indices.size();
     135
     136      return true;
     137   }
     138
     139   return false;
    97140}
    98141
  • vulkan-game.cpp

    r5a23277 re3bef3a  
    254254
    255255   cout << "Created " << graphicsPipelines.size() << " graphics pipelines" << endl;
     256
     257   numPlanes = 2;
    256258
    257259   createCommandBuffers();
     
    281283               framebufferResized = true;
    282284               break;
    283             case UI_EVENT_KEY:
     285            case UI_EVENT_KEYDOWN:
    284286               if (e.key.keycode == SDL_SCANCODE_ESCAPE) {
    285287                  quit = true;
     288               } else if (e.key.keycode == SDL_SCANCODE_SPACE) {
     289                  cout << "Adding a plane" << endl;
     290                  float zOffset = -0.5f + (0.5f * numPlanes);
     291                  vector<Vertex> vertices = {
     292                     {{-0.5f, -0.5f,  zOffset}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
     293                     {{ 0.5f, -0.5f,  zOffset}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
     294                     {{ 0.5f,  0.5f,  zOffset}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
     295                     {{-0.5f,  0.5f,  zOffset}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
     296                  };
     297                  vector<uint16_t> indices = {
     298                     0, 1, 2, 2, 3, 0
     299                  };
     300
     301                  if (graphicsPipelines[0].addObject(vertices, indices, commandPool, graphicsQueue)) {
     302                     vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
     303                     createCommandBuffers();
     304
     305                     numPlanes++;
     306                  }
    286307               } else {
    287308                  cout << "Key event detected" << endl;
     
    943964}
    944965
     966// TODO: Fix the crash that happens when alt-tabbing
    945967void VulkanGame::recreateSwapChain() {
    946968   cout << "Recreating swap chain" << endl;
  • vulkan-game.hpp

    r5a23277 re3bef3a  
    9797
    9898      size_t currentFrame;
     99      size_t numPlanes = 0; // temp
    99100
    100101      bool framebufferResized;
  • vulkan-ref.cpp

    r5a23277 re3bef3a  
    2727using namespace glm;
    2828
    29 /*** START OF REFACTORED CODE ***/
    3029const int SCREEN_WIDTH = 800;
    3130const int SCREEN_HEIGHT = 600;
     
    217216
    218217      size_t currentFrame = 0;
    219 /*** END OF REFACTORED CODE ***/
    220 
    221218      size_t numPlanes = 0; // temp
    222219
    223 /*** START OF REFACTORED CODE ***/
    224220      bool framebufferResized = false;
    225221
     
    14651461         vkBindBufferMemory(device, buffer, bufferMemory, 0);
    14661462      }
    1467 /*** END OF REFACTORED CODE ***/
    14681463
    14691464      void copyDataToBuffer(const void* srcData, VkBuffer dst, VkDeviceSize dstOffset, VkDeviceSize dataSize) {
     
    14851480      }
    14861481
    1487 /*** START OF REFACTORED CODE ***/
    14881482      void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize srcOffset, VkDeviceSize dstOffset,
    14891483            VkDeviceSize size) {
     
    16891683         }
    16901684      }
    1691 /*** END OF REFACTORED CODE ***/
    16921685
    16931686      bool addObjectToScene(GraphicsPipelineInfo& info,
     
    17221715      }
    17231716
    1724 /*** START OF REFACTORED CODE ***/
    17251717      void mainLoop() {
    17261718         // TODO: Create some generic event-handling functions in game-gui-*
     
    20752067   return EXIT_SUCCESS;
    20762068}
    2077 /*** END OF REFACTORED CODE ***/
  • vulkan-utils.hpp

    r5a23277 re3bef3a  
    8787            uint32_t width, uint32_t height, VkQueue graphicsQueue);
    8888
     89      template<class DataType>
     90      static void copyDataToBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool,
     91            const vector<DataType>& srcData, VkBuffer dstBuffer, size_t dstVertexOffset, VkQueue graphicsQueue);
     92
    8993      static void copyBuffer(VkDevice device, VkCommandPool commandPool, VkBuffer srcBuffer, VkBuffer dstBuffer,
    9094            VkDeviceSize srcOffset, VkDeviceSize dstOffset, VkDeviceSize size, VkQueue graphicsQueue);
     
    9599};
    96100
     101template<class DataType>
     102void VulkanUtils::copyDataToBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool,
     103      const vector<DataType>& srcData, VkBuffer dstBuffer, size_t dstVertexOffset, VkQueue graphicsQueue) {
     104   VkDeviceSize srcDataSize = srcData.size() * sizeof(DataType);
     105
     106   VkBuffer stagingBuffer;
     107   VkDeviceMemory stagingBufferMemory;
     108   createBuffer(device, physicalDevice, srcDataSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
     109      VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     110      stagingBuffer, stagingBufferMemory);
     111
     112   void* data;
     113   vkMapMemory(device, stagingBufferMemory, 0, srcDataSize, 0, &data);
     114   memcpy(data, srcData.data(), (size_t)srcDataSize);
     115   vkUnmapMemory(device, stagingBufferMemory);
     116
     117   copyBuffer(device, commandPool, stagingBuffer, dstBuffer, 0, dstVertexOffset * sizeof(DataType), srcDataSize,
     118      graphicsQueue);
     119
     120   vkDestroyBuffer(device, stagingBuffer, nullptr);
     121   vkFreeMemory(device, stagingBufferMemory, nullptr);
     122}
     123
    97124#endif // _VULKAN_UTILS_H
Note: See TracChangeset for help on using the changeset viewer.