Changeset 87c8f1a in opengl-game


Ignore:
Timestamp:
Nov 1, 2019, 5:11:45 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
d2d9286
Parents:
34bdf3a
git-author:
Dmitry Portnoy <dmitry.portnoy@…> (11/01/19 17:09:16)
git-committer:
Dmitry Portnoy <dmitry.portnoy@…> (11/01/19 17:11:45)
Message:

In vaulkangame, define vertex buffer and index buffer data and transfer it to the gpu

Files:
8 edited

Legend:

Unmodified
Added
Removed
  • crash-logger.cpp

    r34bdf3a r87c8f1a  
    158158      char* begin_name = NULL;
    159159      char* begin_offset = NULL;
    160       char* end_offset = NULL;
    161160
    162161#ifdef MAC
     
    200199      write(fd_out, "\n", 1);
    201200#else
     201      char* end_offset = NULL;
     202
    202203      for (char *p = symbollist[i]; *p; p++) {
    203204         if (*p == '(') {
  • graphics-pipeline_vulkan.cpp

    r34bdf3a r87c8f1a  
    55#include <iostream>
    66
     7#include "vulkan-utils.hpp"
     8
    79using namespace std;
    810
    911// TODO: Remove any instances of cout and instead throw exceptions
    1012
    11 GraphicsPipeline_Vulkan::GraphicsPipeline_Vulkan(VkDevice device, VkRenderPass renderPass, Viewport viewport,
    12       int vertexSize) {
     13GraphicsPipeline_Vulkan::GraphicsPipeline_Vulkan(VkPhysicalDevice physicalDevice, VkDevice device,
     14      VkRenderPass renderPass, Viewport viewport, int vertexSize) {
     15   this->physicalDevice = physicalDevice;
    1316   this->device = device;
    1417   this->renderPass = renderPass;
    1518   this->viewport = viewport;
    1619
     20   // Since there is only one array of vertex data, we use binding = 0
     21   // I'll probably do that for the foreseeable future
     22   // I can calculate the stride myself given info about all the varying attributes
    1723   this->bindingDescription.binding = 0;
    1824   this->bindingDescription.stride = vertexSize;
     
    2127
    2228GraphicsPipeline_Vulkan::~GraphicsPipeline_Vulkan() {
     29}
     30
     31void GraphicsPipeline_Vulkan::createVertexBuffer(const void* bufferData, int vertexSize,
     32      VkCommandPool commandPool, VkQueue graphicsQueue) {
     33   VkDeviceSize bufferSize = numVertices * vertexSize;
     34   VkDeviceSize bufferCapacity = vertexCapacity * vertexSize;
     35
     36   VkBuffer stagingBuffer;
     37   VkDeviceMemory stagingBufferMemory;
     38   VulkanUtils::createBuffer(device, physicalDevice, bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
     39      VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     40      stagingBuffer, stagingBufferMemory);
     41
     42   void* data;
     43   vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
     44   memcpy(data, bufferData, (size_t) bufferSize);
     45   vkUnmapMemory(device, stagingBufferMemory);
     46
     47   VulkanUtils::createBuffer(device, physicalDevice, bufferCapacity,
     48      VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
     49      VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vertexBuffer, vertexBufferMemory);
     50
     51   VulkanUtils::copyBuffer(device, commandPool, stagingBuffer, vertexBuffer, 0, 0, bufferSize, graphicsQueue);
     52
     53   vkDestroyBuffer(device, stagingBuffer, nullptr);
     54   vkFreeMemory(device, stagingBufferMemory, nullptr);
     55}
     56
     57void GraphicsPipeline_Vulkan::createIndexBuffer(const void* bufferData, int indexSize,
     58      VkCommandPool commandPool, VkQueue graphicsQueue) {
     59   VkDeviceSize bufferSize = numIndices * indexSize;
     60   VkDeviceSize bufferCapacity = indexCapacity * indexSize;
     61
     62   VkBuffer stagingBuffer;
     63   VkDeviceMemory stagingBufferMemory;
     64   VulkanUtils::createBuffer(device, physicalDevice, bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
     65      VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     66      stagingBuffer, stagingBufferMemory);
     67
     68   void* data;
     69   vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
     70   memcpy(data, bufferData, (size_t) bufferSize);
     71   vkUnmapMemory(device, stagingBufferMemory);
     72
     73   VulkanUtils::createBuffer(device, physicalDevice, bufferCapacity,
     74      VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
     75      VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexBuffer, indexBufferMemory);
     76
     77   VulkanUtils::copyBuffer(device, commandPool, stagingBuffer, indexBuffer, 0, 0, bufferSize, graphicsQueue);
     78
     79   vkDestroyBuffer(device, stagingBuffer, nullptr);
     80   vkFreeMemory(device, stagingBufferMemory, nullptr);
    2381}
    2482
     
    313371
    314372void GraphicsPipeline_Vulkan::cleanup() {
    315    vkDestroyPipeline(this->device, this->pipeline, nullptr);
    316    vkDestroyDescriptorPool(this->device, this->descriptorPool, nullptr);
    317    vkDestroyPipelineLayout(this->device, this->pipelineLayout, nullptr);
     373   vkDestroyPipeline(device, pipeline, nullptr);
     374   vkDestroyDescriptorPool(device, descriptorPool, nullptr);
     375   vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
    318376}
    319377
    320378void GraphicsPipeline_Vulkan::cleanupBuffers() {
    321    vkDestroyDescriptorSetLayout(this->device, this->descriptorSetLayout, nullptr);
    322 }
     379   vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
     380
     381   vkDestroyBuffer(device, vertexBuffer, nullptr);
     382   vkFreeMemory(device, vertexBufferMemory, nullptr);
     383   vkDestroyBuffer(device, indexBuffer, nullptr);
     384   vkFreeMemory(device, indexBufferMemory, nullptr);
     385}
  • graphics-pipeline_vulkan.hpp

    r34bdf3a r87c8f1a  
    2020class GraphicsPipeline_Vulkan : public GraphicsPipeline {
    2121   public:
    22       GraphicsPipeline_Vulkan(VkDevice device, VkRenderPass renderPass, Viewport viewport, int vertexSize);
     22      GraphicsPipeline_Vulkan(VkPhysicalDevice physicalDevice, VkDevice device, VkRenderPass renderPass,
     23         Viewport viewport, int vertexSize);
    2324      ~GraphicsPipeline_Vulkan();
     25
     26      template<class VertexType, class IndexType>
     27      void bindData(const vector<VertexType>& vertices, const vector<IndexType>& indices,
     28         VkCommandPool commandPool, VkQueue graphicsQueue);
     29
     30      void createVertexBuffer(const void* bufferData, int vertexSize, VkCommandPool commandPool,
     31         VkQueue graphicsQueue);
     32      void createIndexBuffer(const void* bufferData, int indexSize, VkCommandPool commandPool,
     33         VkQueue graphicsQueue);
    2434
    2535      // Maybe I should rename these to addVertexAttribute (addVaryingAttribute) and addUniformAttribute
     
    4454      vector<char> readFile(const string& filename);
    4555
     56      VkPhysicalDevice physicalDevice;
    4657      VkDevice device;
    4758      VkRenderPass renderPass;
     
    5869      VkDescriptorPool descriptorPool;
    5970      vector<VkDescriptorSet> descriptorSets;
     71
     72      size_t numVertices;
     73      size_t vertexCapacity;
     74      VkBuffer vertexBuffer;
     75      VkDeviceMemory vertexBufferMemory;
     76
     77      size_t numIndices;
     78      size_t indexCapacity;
     79      VkBuffer indexBuffer;
     80      VkDeviceMemory indexBufferMemory;
    6081};
    6182
     83// TODO: Probably better to template the whole class and to also combine this function
     84// and the constructor since I call this right after the constructor anyway
     85template<class VertexType, class IndexType>
     86void GraphicsPipeline_Vulkan::bindData(const vector<VertexType>& vertices, const vector<IndexType>& indices,
     87      VkCommandPool commandPool, VkQueue graphicsQueue) {
     88   numVertices = vertices.size();
     89   vertexCapacity = numVertices * 2;
     90   createVertexBuffer(vertices.data(), sizeof(VertexType), commandPool, graphicsQueue);
     91
     92   numIndices = indices.size();
     93   indexCapacity = numIndices * 2;
     94   createIndexBuffer(indices.data(), sizeof(IndexType), commandPool, graphicsQueue);
     95}
     96
    6297#endif // _GRAPHICS_PIPELINE_VULKAN_H
  • vulkan-game.cpp

    r34bdf3a r87c8f1a  
    2121   gui = nullptr;
    2222   window = nullptr;
     23
     24   currentFrame = 0;
     25   framebufferResized = false;
    2326}
    2427
     
    138141   createUniformBuffers();
    139142
    140    graphicsPipelines.push_back(GraphicsPipeline_Vulkan(device, renderPass,
     143   vector<Vertex> sceneVertices = {
     144      {{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
     145      {{ 0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
     146      {{ 0.5f,  0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
     147      {{-0.5f,  0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
     148
     149      {{-0.5f, -0.5f,  0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
     150      {{ 0.5f, -0.5f,  0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
     151      {{ 0.5f,  0.5f,  0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
     152      {{-0.5f,  0.5f,  0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
     153   };
     154   vector<uint16_t> sceneIndices = {
     155      0, 1, 2, 2, 3, 0,
     156      4, 5, 6, 6, 7, 4
     157   };
     158
     159   graphicsPipelines.push_back(GraphicsPipeline_Vulkan(physicalDevice, device, renderPass,
    141160      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, sizeof(Vertex)));
     161
     162   graphicsPipelines.back().bindData(sceneVertices, sceneIndices, commandPool, graphicsQueue);
    142163
    143164   graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::pos));
     
    155176   graphicsPipelines.back().createDescriptorSets(swapChainImages);
    156177
    157    graphicsPipelines.push_back(GraphicsPipeline_Vulkan(device, renderPass,
     178   vector<OverlayVertex> overlayVertices = {
     179      {{-1.0f,  1.0f,  0.0f}, {0.0f, 1.0f}},
     180      {{ 1.0f,  1.0f,  0.0f}, {1.0f, 1.0f}},
     181      {{ 1.0f, -1.0f,  0.0f}, {1.0f, 0.0f}},
     182      {{-1.0f, -1.0f,  0.0f}, {0.0f, 0.0f}}
     183   };
     184   vector<uint16_t> overlayIndices = {
     185      0, 1, 2, 2, 3, 0
     186   };
     187
     188   graphicsPipelines.push_back(GraphicsPipeline_Vulkan(physicalDevice, device, renderPass,
    158189      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, sizeof(OverlayVertex)));
     190
     191   graphicsPipelines.back().bindData(overlayVertices, overlayIndices, commandPool, graphicsQueue);
    159192
    160193   graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&OverlayVertex::pos));
     
    238271
    239272void VulkanGame::renderScene() {
     273   vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max());
     274
     275   uint32_t imageIndex;
     276
     277   currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
     278   currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
    240279}
    241280
  • vulkan-game.hpp

    r34bdf3a r87c8f1a  
    9191      vector<VkFence> inFlightFences;
    9292
    93       bool framebufferResized = false;
     93      size_t currentFrame;
     94
     95      bool framebufferResized;
    9496
    9597      bool initWindow(int width, int height, unsigned char guiFlags);
  • vulkan-ref.cpp

    r34bdf3a r87c8f1a  
    7272   glm::vec2 texCoord;
    7373};
    74 /*** END OF REFACTORED CODE ***/
    7574
    7675struct UniformBufferObject {
     
    8079};
    8180
    82 /*** START OF REFACTORED CODE ***/
    8381struct DescriptorInfo {
    8482   VkDescriptorType type;
     
    8886   VkDescriptorImageInfo* imageData;
    8987};
    90 /*** END OF REFACTORED CODE ***/
    9188
    9289struct GraphicsPipelineInfo {
    93 /*** START OF REFACTORED CODE ***/
    9490   VkPipelineLayout pipelineLayout;
    9591   VkPipeline pipeline;
     
    10399   VkDescriptorSetLayout descriptorSetLayout;
    104100   vector<VkDescriptorSet> descriptorSets;
    105 /*** END OF REFACTORED CODE ***/
    106 
    107    size_t numVertices; // Currently unused
     101
     102   size_t numVertices;
    108103   size_t vertexCapacity;
    109104   VkBuffer vertexBuffer;
     
    116111};
    117112
    118 /*** START OF REFACTORED CODE ***/
    119113VkResult CreateDebugUtilsMessengerEXT(VkInstance instance,
    120114      const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
     
    223217      vector<VkSemaphore> renderFinishedSemaphores;
    224218      vector<VkFence> inFlightFences;
     219
     220      size_t currentFrame = 0;
    225221/*** END OF REFACTORED CODE ***/
    226 
    227       size_t currentFrame = 0;
    228222
    229223      size_t numPlanes = 0; // temp
     
    340334         overlayImageInfo.imageView = sdlOverlayImageView;
    341335         overlayImageInfo.sampler = textureSampler;
    342 /*** END OF REFACTORED CODE ***/
    343336
    344337         // SHADER-SPECIFIC STUFF STARTS HERE
     
    364357            sceneIndices.data(), sizeof(uint16_t), sceneIndices.size());
    365358
    366 /*** START OF REFACTORED CODE ***/
    367359         addAttributeDescription(scenePipeline, VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::pos));
    368360         addAttributeDescription(scenePipeline, VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::color));
     
    373365
    374366         createDescriptorSetLayout(scenePipeline);
    375 /*** END OF REFACTORED CODE ***/
    376367
    377368         numPlanes = 2;
     
    391382            overlayIndices.data(), sizeof(uint16_t), overlayIndices.size());
    392383
    393 /*** START OF REFACTORED CODE ***/
    394384         addAttributeDescription(overlayPipeline, VK_FORMAT_R32G32B32_SFLOAT, offset_of(&OverlayVertex::pos));
    395385         addAttributeDescription(overlayPipeline, VK_FORMAT_R32G32_SFLOAT, offset_of(&OverlayVertex::texCoord));
     
    812802         info.bindingDescription.stride = vertexSize;
    813803         info.bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
    814 /*** END OF REFACTORED CODE ***/
    815804
    816805         info.numVertices = numVertices;
     
    13931382         }
    13941383      }
    1395 /*** END OF REFACTORED CODE ***/
    13961384
    13971385      void createVertexBuffer(GraphicsPipelineInfo& info, const void* vertexData, int vertexSize) {
     
    14431431      }
    14441432
    1445 /*** START OF REFACTORED CODE ***/
    14461433      void createUniformBuffers() {
    14471434         VkDeviceSize bufferSize = sizeof(UniformBufferObject);
     
    15071494      }
    15081495
     1496/*** START OF REFACTORED CODE ***/
    15091497      void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize srcOffset, VkDeviceSize dstOffset,
    15101498            VkDeviceSize size) {
     
    15501538      }
    15511539
    1552 /*** START OF REFACTORED CODE ***/
    15531540      uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) {
    15541541         VkPhysicalDeviceMemoryProperties memProperties;
     
    18011788
    18021789      void drawFrame() {
     1790         vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max());
     1791
     1792         uint32_t imageIndex;
    18031793/*** END OF REFACTORED CODE ***/
    1804          vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max());
    1805 
    1806          uint32_t imageIndex;
    18071794
    18081795         VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(),
     
    18611848         }
    18621849
     1850/*** START OF REFACTORED CODE ***/
    18631851         currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
    18641852         currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
    1865 /*** START OF REFACTORED CODE ***/
    18661853      }
    18671854
     
    20522039      void cleanupPipelineBuffers(GraphicsPipelineInfo& pipeline) {
    20532040         vkDestroyDescriptorSetLayout(device, pipeline.descriptorSetLayout, nullptr);
    2054 /*** END OF REFACTORED CODE ***/
    20552041
    20562042         vkDestroyBuffer(device, pipeline.vertexBuffer, nullptr);
     
    20592045         vkFreeMemory(device, pipeline.indexBufferMemory, nullptr);
    20602046      }
     2047/*** END OF REFACTORED CODE ***/
    20612048
    20622049      static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
  • vulkan-utils.cpp

    r34bdf3a r87c8f1a  
    482482}
    483483
     484void VulkanUtils::copyBuffer(VkDevice device, VkCommandPool commandPool, VkBuffer srcBuffer,
     485      VkBuffer dstBuffer, VkDeviceSize srcOffset, VkDeviceSize dstOffset, VkDeviceSize size,
     486      VkQueue graphicsQueue) {
     487   VkCommandBuffer commandBuffer = beginSingleTimeCommands(device, commandPool);
     488
     489   VkBufferCopy copyRegion = { srcOffset, dstOffset, size };
     490   vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, &copyRegion);
     491
     492   endSingleTimeCommands(device, commandPool, commandBuffer, graphicsQueue);
     493}
     494
    484495bool VulkanUtils::hasStencilComponent(VkFormat format) {
    485496   return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT;
  • vulkan-utils.hpp

    r34bdf3a r87c8f1a  
    8484            uint32_t width, uint32_t height, VkQueue graphicsQueue);
    8585
     86      static void copyBuffer(VkDevice device, VkCommandPool commandPool, VkBuffer srcBuffer, VkBuffer dstBuffer,
     87            VkDeviceSize srcOffset, VkDeviceSize dstOffset, VkDeviceSize size, VkQueue graphicsQueue);
     88
    8689      static bool hasStencilComponent(VkFormat format);
    8790
Note: See TracChangeset for help on using the changeset viewer.