Changeset 7563b8a in opengl-game
- Timestamp:
- Oct 1, 2019, 3:03:30 AM (5 years ago)
- 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)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-ref.cpp
rfa9fa1c r7563b8a 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 ***/ … … 369 373 createDescriptorSetLayout(scenePipeline); 370 374 375 numPlanes = 2; 371 376 372 377 vector<OverlayVertex> overlayVertices = { … … 808 813 809 814 info.numVertices = numVertices; 810 createVertexBuffer(info, vertexData, vertexSize * numVertices); 815 info.vertexCapacity = numVertices * 2; 816 createVertexBuffer(info, vertexData, vertexSize); 811 817 812 818 info.numIndices = numIndices; 813 createIndexBuffer(info, indexData, indexSize * numIndices); 819 info.indexCapacity = numIndices * 2; 820 createIndexBuffer(info, indexData, indexSize); 814 821 } 815 822 … … 1388 1395 } 1389 1396 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 1391 1401 VkBuffer stagingBuffer; 1392 1402 VkDeviceMemory stagingBufferMemory; … … 1400 1410 vkUnmapMemory(device, stagingBufferMemory); 1401 1411 1402 createBuffer(buffer Size, 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, 1403 1413 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.vertexBuffer, info.vertexBufferMemory); 1404 1414 1405 copyBuffer(stagingBuffer, info.vertexBuffer, bufferSize);1415 copyBuffer(stagingBuffer, info.vertexBuffer, 0, 0, bufferSize); 1406 1416 1407 1417 vkDestroyBuffer(device, stagingBuffer, nullptr); … … 1409 1419 } 1410 1420 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 1412 1425 VkBuffer stagingBuffer; 1413 1426 VkDeviceMemory stagingBufferMemory; … … 1421 1434 vkUnmapMemory(device, stagingBufferMemory); 1422 1435 1423 createBuffer(buffer Size, 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, 1424 1437 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.indexBuffer, info.indexBufferMemory); 1425 1438 1426 copyBuffer(stagingBuffer, info.indexBuffer, bufferSize);1439 copyBuffer(stagingBuffer, info.indexBuffer, 0, 0, bufferSize); 1427 1440 1428 1441 vkDestroyBuffer(device, stagingBuffer, nullptr); … … 1474 1487 } 1475 1488 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) { 1477 1509 VkCommandBuffer commandBuffer = beginSingleTimeCommands(); 1478 1510 1479 VkBufferCopy copyRegion = {}; 1480 copyRegion.size = size; 1511 VkBufferCopy copyRegion = { srcOffset, dstOffset, size }; 1481 1512 vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, ©Region); 1482 1513 … … 1678 1709 } 1679 1710 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 1680 1742 /*** START OF REFACTORED CODE ***/ 1681 1743 void mainLoop() { … … 1690 1752 } 1691 1753 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) { 1693 1772 quit = true; 1694 1773 }
Note:
See TracChangeset
for help on using the changeset viewer.