Changeset 7563b8a in opengl-game


Ignore:
Timestamp:
Oct 1, 2019, 3:03:30 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
40995d3
Parents:
fa9fa1c
git-author:
Dmitry Portnoy <dmitry.portnoy@…> (09/25/19 00:04:01)
git-committer:
Dmitry Portnoy <dmp1488@…> (10/01/19 03:03:30)
Message:

In vulkanref, implement the ability to add new objects to the scene

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-ref.cpp

    rfa9fa1c r7563b8a  
    102102
    103103   size_t numVertices; // Currently unused
     104   size_t vertexCapacity;
    104105   VkBuffer vertexBuffer;
    105106   VkDeviceMemory vertexBufferMemory;
    106107
    107108   size_t numIndices;
     109   size_t indexCapacity;
    108110   VkBuffer indexBuffer;
    109111   VkDeviceMemory indexBufferMemory;
     
    226228
    227229      size_t currentFrame = 0;
     230
     231      size_t numPlanes = 0; // temp
    228232
    229233/*** START OF REFACTORED CODE ***/
     
    369373         createDescriptorSetLayout(scenePipeline);
    370374
     375         numPlanes = 2;
    371376
    372377         vector<OverlayVertex> overlayVertices = {
     
    808813
    809814         info.numVertices = numVertices;
    810          createVertexBuffer(info, vertexData, vertexSize * numVertices);
     815         info.vertexCapacity = numVertices * 2;
     816         createVertexBuffer(info, vertexData, vertexSize);
    811817
    812818         info.numIndices = numIndices;
    813          createIndexBuffer(info, indexData, indexSize * numIndices);
     819         info.indexCapacity = numIndices * 2;
     820         createIndexBuffer(info, indexData, indexSize);
    814821      }
    815822
     
    13881395      }
    13891396
    1390       void createVertexBuffer(GraphicsPipelineInfo& info, const void* vertexData, VkDeviceSize bufferSize) {
     1397      void createVertexBuffer(GraphicsPipelineInfo& info, const void* vertexData, int vertexSize) {
     1398         VkDeviceSize bufferSize = info.numVertices * vertexSize;
     1399         VkDeviceSize bufferCapacity = info.vertexCapacity * vertexSize;
     1400
    13911401         VkBuffer stagingBuffer;
    13921402         VkDeviceMemory stagingBufferMemory;
     
    14001410         vkUnmapMemory(device, stagingBufferMemory);
    14011411
    1402          createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
     1412         createBuffer(bufferCapacity, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
    14031413            VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.vertexBuffer, info.vertexBufferMemory);
    14041414
    1405          copyBuffer(stagingBuffer, info.vertexBuffer, bufferSize);
     1415         copyBuffer(stagingBuffer, info.vertexBuffer, 0, 0, bufferSize);
    14061416
    14071417         vkDestroyBuffer(device, stagingBuffer, nullptr);
     
    14091419      }
    14101420
    1411       void createIndexBuffer(GraphicsPipelineInfo& info, const void* indexData, VkDeviceSize bufferSize) {
     1421      void createIndexBuffer(GraphicsPipelineInfo& info, const void* indexData, int indexSize) {
     1422         VkDeviceSize bufferSize = info.numIndices * indexSize;
     1423         VkDeviceSize bufferCapacity = info.indexCapacity * indexSize;
     1424
    14121425         VkBuffer stagingBuffer;
    14131426         VkDeviceMemory stagingBufferMemory;
     
    14211434         vkUnmapMemory(device, stagingBufferMemory);
    14221435
    1423          createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
     1436         createBuffer(bufferCapacity, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
    14241437            VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.indexBuffer, info.indexBufferMemory);
    14251438
    1426          copyBuffer(stagingBuffer, info.indexBuffer, bufferSize);
     1439         copyBuffer(stagingBuffer, info.indexBuffer, 0, 0, bufferSize);
    14271440
    14281441         vkDestroyBuffer(device, stagingBuffer, nullptr);
     
    14741487      }
    14751488
    1476       void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size) {
     1489      void copyDataToBuffer(const void* srcData, VkBuffer dst, VkDeviceSize dstOffset, VkDeviceSize dataSize) {
     1490         VkBuffer stagingBuffer;
     1491         VkDeviceMemory stagingBufferMemory;
     1492         createBuffer(dataSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
     1493            VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
     1494            stagingBuffer, stagingBufferMemory);
     1495
     1496         void* data;
     1497         vkMapMemory(device, stagingBufferMemory, 0, dataSize, 0, &data);
     1498         memcpy(data, srcData, (size_t) dataSize);
     1499         vkUnmapMemory(device, stagingBufferMemory);
     1500
     1501         copyBuffer(stagingBuffer, dst, 0, dstOffset, dataSize);
     1502
     1503         vkDestroyBuffer(device, stagingBuffer, nullptr);
     1504         vkFreeMemory(device, stagingBufferMemory, nullptr);
     1505      }
     1506
     1507      void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize srcOffset, VkDeviceSize dstOffset,
     1508            VkDeviceSize size) {
    14771509         VkCommandBuffer commandBuffer = beginSingleTimeCommands();
    14781510
    1479          VkBufferCopy copyRegion = {};
    1480          copyRegion.size = size;
     1511         VkBufferCopy copyRegion = { srcOffset, dstOffset, size };
    14811512         vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, &copyRegion);
    14821513
     
    16781709      }
    16791710
     1711      bool addObjectToScene(GraphicsPipelineInfo& info,
     1712            const void* vertexData, int vertexSize, size_t numVertices,
     1713            vector<uint16_t>& indices, size_t numIndices) {
     1714         int indexSize = sizeof(uint16_t);
     1715
     1716         for (uint16_t& idx : indices) {
     1717            idx += info.numVertices;
     1718         }
     1719
     1720         if (info.numVertices + numVertices > info.vertexCapacity) {
     1721            cout << "ERROR: Need to resize vertex buffers" << endl;
     1722         } else if (info.numIndices + numIndices > info.indexCapacity) {
     1723            cout << "ERROR: Need to resize index buffers" << endl;
     1724         } else {
     1725            cout << "Added object to scene" << endl;
     1726
     1727            copyDataToBuffer(vertexData, info.vertexBuffer, info.numVertices * vertexSize, numVertices * vertexSize);
     1728            info.numVertices += numVertices;
     1729
     1730            copyDataToBuffer(indices.data(), info.indexBuffer, info.numIndices * indexSize, numIndices * indexSize);
     1731            info.numIndices += numIndices;
     1732
     1733            vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
     1734            createCommandBuffers();
     1735
     1736            return true;
     1737         }
     1738
     1739         return false;
     1740      }
     1741
    16801742/*** START OF REFACTORED CODE ***/
    16811743      void mainLoop() {
     
    16901752               }
    16911753               if (e.type == SDL_KEYDOWN) {
    1692                   if (e.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
     1754                  if (e.key.keysym.sym == SDLK_SPACE) {
     1755                     float zOffset = -0.5f + (0.5f * numPlanes);
     1756                     vector<Vertex> vertices = {
     1757                        {{-0.5f, -0.5f,  zOffset}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
     1758                        {{ 0.5f, -0.5f,  zOffset}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
     1759                        {{ 0.5f,  0.5f,  zOffset}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
     1760                        {{-0.5f,  0.5f,  zOffset}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
     1761                     };
     1762                     vector<uint16_t> indices = {
     1763                        0, 1, 2, 2, 3, 0
     1764                     };
     1765
     1766                     if (addObjectToScene(scenePipeline,
     1767                           vertices.data(), sizeof(Vertex), vertices.size(),
     1768                           indices, indices.size())) {
     1769                        numPlanes++;
     1770                     }
     1771                  } else if (e.key.keysym.sym == SDLK_ESCAPE) {
    16931772                     quit = true;
    16941773                  }
Note: See TracChangeset for help on using the changeset viewer.