Changeset 87c8f1a in opengl-game for graphics-pipeline_vulkan.cpp


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.