Changes in / [40995d3:a0da009] in opengl-game
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-ref.cpp
r40995d3 ra0da009 102 102 103 103 size_t numVertices; // Currently unused 104 size_t vertexCapacity;105 104 VkBuffer vertexBuffer; 106 105 VkDeviceMemory vertexBufferMemory; 107 106 108 107 size_t numIndices; 109 size_t indexCapacity;110 108 VkBuffer indexBuffer; 111 109 VkDeviceMemory indexBufferMemory; … … 228 226 229 227 size_t currentFrame = 0; 230 231 size_t numPlanes = 0; // temp232 228 233 229 /*** START OF REFACTORED CODE ***/ … … 378 374 createDescriptorSetLayout(scenePipeline); 379 375 380 numPlanes = 2;381 376 382 377 vector<OverlayVertex> overlayVertices = { … … 818 813 819 814 info.numVertices = numVertices; 820 info.vertexCapacity = numVertices * 2; 821 createVertexBuffer(info, vertexData, vertexSize); 815 createVertexBuffer(info, vertexData, vertexSize * numVertices); 822 816 823 817 info.numIndices = numIndices; 824 info.indexCapacity = numIndices * 2; 825 createIndexBuffer(info, indexData, indexSize); 818 createIndexBuffer(info, indexData, indexSize * numIndices); 826 819 } 827 820 … … 1400 1393 } 1401 1394 1402 void createVertexBuffer(GraphicsPipelineInfo& info, const void* vertexData, int vertexSize) { 1403 VkDeviceSize bufferSize = info.numVertices * vertexSize; 1404 VkDeviceSize bufferCapacity = info.vertexCapacity * vertexSize; 1405 1395 void createVertexBuffer(GraphicsPipelineInfo& info, const void* vertexData, VkDeviceSize bufferSize) { 1406 1396 VkBuffer stagingBuffer; 1407 1397 VkDeviceMemory stagingBufferMemory; … … 1415 1405 vkUnmapMemory(device, stagingBufferMemory); 1416 1406 1417 createBuffer(buffer Capacity, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,1407 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, 1418 1408 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.vertexBuffer, info.vertexBufferMemory); 1419 1409 1420 copyBuffer(stagingBuffer, info.vertexBuffer, 0, 0,bufferSize);1410 copyBuffer(stagingBuffer, info.vertexBuffer, bufferSize); 1421 1411 1422 1412 vkDestroyBuffer(device, stagingBuffer, nullptr); … … 1424 1414 } 1425 1415 1426 void createIndexBuffer(GraphicsPipelineInfo& info, const void* indexData, int indexSize) { 1427 VkDeviceSize bufferSize = info.numIndices * indexSize; 1428 VkDeviceSize bufferCapacity = info.indexCapacity * indexSize; 1429 1416 void createIndexBuffer(GraphicsPipelineInfo& info, const void* indexData, VkDeviceSize bufferSize) { 1430 1417 VkBuffer stagingBuffer; 1431 1418 VkDeviceMemory stagingBufferMemory; … … 1439 1426 vkUnmapMemory(device, stagingBufferMemory); 1440 1427 1441 createBuffer(buffer Capacity, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,1428 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, 1442 1429 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.indexBuffer, info.indexBufferMemory); 1443 1430 1444 copyBuffer(stagingBuffer, info.indexBuffer, 0, 0,bufferSize);1431 copyBuffer(stagingBuffer, info.indexBuffer, bufferSize); 1445 1432 1446 1433 vkDestroyBuffer(device, stagingBuffer, nullptr); … … 1492 1479 } 1493 1480 1494 void copyDataToBuffer(const void* srcData, VkBuffer dst, VkDeviceSize dstOffset, VkDeviceSize dataSize) { 1495 VkBuffer stagingBuffer; 1496 VkDeviceMemory stagingBufferMemory; 1497 createBuffer(dataSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, 1498 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 1499 stagingBuffer, stagingBufferMemory); 1500 1501 void* data; 1502 vkMapMemory(device, stagingBufferMemory, 0, dataSize, 0, &data); 1503 memcpy(data, srcData, (size_t) dataSize); 1504 vkUnmapMemory(device, stagingBufferMemory); 1505 1506 copyBuffer(stagingBuffer, dst, 0, dstOffset, dataSize); 1507 1508 vkDestroyBuffer(device, stagingBuffer, nullptr); 1509 vkFreeMemory(device, stagingBufferMemory, nullptr); 1510 } 1511 1512 void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize srcOffset, VkDeviceSize dstOffset, 1513 VkDeviceSize size) { 1481 void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size) { 1514 1482 VkCommandBuffer commandBuffer = beginSingleTimeCommands(); 1515 1483 1516 VkBufferCopy copyRegion = { srcOffset, dstOffset, size }; 1484 VkBufferCopy copyRegion = {}; 1485 copyRegion.size = size; 1517 1486 vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, ©Region); 1518 1487 … … 1714 1683 } 1715 1684 1716 bool addObjectToScene(GraphicsPipelineInfo& info,1717 const void* vertexData, int vertexSize, size_t numVertices,1718 vector<uint16_t>& indices, size_t numIndices) {1719 int indexSize = sizeof(uint16_t);1720 1721 for (uint16_t& idx : indices) {1722 idx += info.numVertices;1723 }1724 1725 if (info.numVertices + numVertices > info.vertexCapacity) {1726 cout << "ERROR: Need to resize vertex buffers" << endl;1727 } else if (info.numIndices + numIndices > info.indexCapacity) {1728 cout << "ERROR: Need to resize index buffers" << endl;1729 } else {1730 cout << "Added object to scene" << endl;1731 1732 copyDataToBuffer(vertexData, info.vertexBuffer, info.numVertices * vertexSize, numVertices * vertexSize);1733 info.numVertices += numVertices;1734 1735 copyDataToBuffer(indices.data(), info.indexBuffer, info.numIndices * indexSize, numIndices * indexSize);1736 info.numIndices += numIndices;1737 1738 vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());1739 createCommandBuffers();1740 1741 return true;1742 }1743 1744 return false;1745 }1746 1747 1685 /*** START OF REFACTORED CODE ***/ 1748 1686 void mainLoop() { … … 1757 1695 } 1758 1696 if (e.type == SDL_KEYDOWN) { 1759 if (e.key.keysym.sym == SDLK_SPACE) { 1760 float zOffset = -0.5f + (0.5f * numPlanes); 1761 vector<Vertex> vertices = { 1762 {{-0.5f, -0.5f, zOffset}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}}, 1763 {{ 0.5f, -0.5f, zOffset}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}}, 1764 {{ 0.5f, 0.5f, zOffset}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}}, 1765 {{-0.5f, 0.5f, zOffset}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}} 1766 }; 1767 vector<uint16_t> indices = { 1768 0, 1, 2, 2, 3, 0 1769 }; 1770 1771 if (addObjectToScene(scenePipeline, 1772 vertices.data(), sizeof(Vertex), vertices.size(), 1773 indices, indices.size())) { 1774 numPlanes++; 1775 } 1776 } else if (e.key.keysym.sym == SDLK_ESCAPE) { 1697 if (e.key.keysym.scancode == SDL_SCANCODE_ESCAPE) { 1777 1698 quit = true; 1778 1699 }
Note:
See TracChangeset
for help on using the changeset viewer.