Changes in / [a0da009:40995d3] in opengl-game
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-ref.cpp
ra0da009 r40995d3 102 102 103 103 size_t numVertices; // Currently unused 104 size_t vertexCapacity; 104 105 VkBuffer vertexBuffer; 105 106 VkDeviceMemory vertexBufferMemory; 106 107 107 108 size_t numIndices; 109 size_t indexCapacity; 108 110 VkBuffer indexBuffer; 109 111 VkDeviceMemory indexBufferMemory; … … 226 228 227 229 size_t currentFrame = 0; 230 231 size_t numPlanes = 0; // temp 228 232 229 233 /*** START OF REFACTORED CODE ***/ … … 374 378 createDescriptorSetLayout(scenePipeline); 375 379 380 numPlanes = 2; 376 381 377 382 vector<OverlayVertex> overlayVertices = { … … 813 818 814 819 info.numVertices = numVertices; 815 createVertexBuffer(info, vertexData, vertexSize * numVertices); 820 info.vertexCapacity = numVertices * 2; 821 createVertexBuffer(info, vertexData, vertexSize); 816 822 817 823 info.numIndices = numIndices; 818 createIndexBuffer(info, indexData, indexSize * numIndices); 824 info.indexCapacity = numIndices * 2; 825 createIndexBuffer(info, indexData, indexSize); 819 826 } 820 827 … … 1393 1400 } 1394 1401 1395 void createVertexBuffer(GraphicsPipelineInfo& info, const void* vertexData, VkDeviceSize bufferSize) { 1402 void createVertexBuffer(GraphicsPipelineInfo& info, const void* vertexData, int vertexSize) { 1403 VkDeviceSize bufferSize = info.numVertices * vertexSize; 1404 VkDeviceSize bufferCapacity = info.vertexCapacity * vertexSize; 1405 1396 1406 VkBuffer stagingBuffer; 1397 1407 VkDeviceMemory stagingBufferMemory; … … 1405 1415 vkUnmapMemory(device, stagingBufferMemory); 1406 1416 1407 createBuffer(buffer Size, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,1417 createBuffer(bufferCapacity, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, 1408 1418 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.vertexBuffer, info.vertexBufferMemory); 1409 1419 1410 copyBuffer(stagingBuffer, info.vertexBuffer, bufferSize);1420 copyBuffer(stagingBuffer, info.vertexBuffer, 0, 0, bufferSize); 1411 1421 1412 1422 vkDestroyBuffer(device, stagingBuffer, nullptr); … … 1414 1424 } 1415 1425 1416 void createIndexBuffer(GraphicsPipelineInfo& info, const void* indexData, VkDeviceSize bufferSize) { 1426 void createIndexBuffer(GraphicsPipelineInfo& info, const void* indexData, int indexSize) { 1427 VkDeviceSize bufferSize = info.numIndices * indexSize; 1428 VkDeviceSize bufferCapacity = info.indexCapacity * indexSize; 1429 1417 1430 VkBuffer stagingBuffer; 1418 1431 VkDeviceMemory stagingBufferMemory; … … 1426 1439 vkUnmapMemory(device, stagingBufferMemory); 1427 1440 1428 createBuffer(buffer Size, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,1441 createBuffer(bufferCapacity, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, 1429 1442 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.indexBuffer, info.indexBufferMemory); 1430 1443 1431 copyBuffer(stagingBuffer, info.indexBuffer, bufferSize);1444 copyBuffer(stagingBuffer, info.indexBuffer, 0, 0, bufferSize); 1432 1445 1433 1446 vkDestroyBuffer(device, stagingBuffer, nullptr); … … 1479 1492 } 1480 1493 1481 void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size) { 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) { 1482 1514 VkCommandBuffer commandBuffer = beginSingleTimeCommands(); 1483 1515 1484 VkBufferCopy copyRegion = {}; 1485 copyRegion.size = size; 1516 VkBufferCopy copyRegion = { srcOffset, dstOffset, size }; 1486 1517 vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, ©Region); 1487 1518 … … 1683 1714 } 1684 1715 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 1685 1747 /*** START OF REFACTORED CODE ***/ 1686 1748 void mainLoop() { … … 1695 1757 } 1696 1758 if (e.type == SDL_KEYDOWN) { 1697 if (e.key.keysym.scancode == SDL_SCANCODE_ESCAPE) { 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) { 1698 1777 quit = true; 1699 1778 }
Note:
See TracChangeset
for help on using the changeset viewer.