Changes in vulkan-ref.cpp [a0da009:7563b8a] in opengl-game
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-ref.cpp
ra0da009 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 ***/ … … 326 330 /*** END OF REFACTORED CODE ***/ 327 331 328 // THIS SECTION IS WHERE TEXTURES ARE CREATED, MAYBE SPLIT IT OFF INTO A SEPARATE FUNCTION329 // MAY WANT TO CREATE A STRUCT TO HOLD SIMILAR VARIABLES< LIKE THOSE FOR A TEXTURE330 331 332 createImageResources("textures/texture.jpg", textureImage, textureImageMemory, textureImageView); 332 333 createImageResourcesFromSDLTexture(uiOverlay, sdlOverlayImage, sdlOverlayImageMemory, sdlOverlayImageView); … … 342 343 overlayImageInfo.imageView = sdlOverlayImageView; 343 344 overlayImageInfo.sampler = textureSampler; 344 345 // SHADER-SPECIFIC STUFF STARTS HERE346 345 347 346 vector<Vertex> sceneVertices = { … … 374 373 createDescriptorSetLayout(scenePipeline); 375 374 375 numPlanes = 2; 376 376 377 377 vector<OverlayVertex> overlayVertices = { … … 813 813 814 814 info.numVertices = numVertices; 815 createVertexBuffer(info, vertexData, vertexSize * numVertices); 815 info.vertexCapacity = numVertices * 2; 816 createVertexBuffer(info, vertexData, vertexSize); 816 817 817 818 info.numIndices = numIndices; 818 createIndexBuffer(info, indexData, indexSize * numIndices); 819 info.indexCapacity = numIndices * 2; 820 createIndexBuffer(info, indexData, indexSize); 819 821 } 820 822 … … 1393 1395 } 1394 1396 1395 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 1396 1401 VkBuffer stagingBuffer; 1397 1402 VkDeviceMemory stagingBufferMemory; … … 1405 1410 vkUnmapMemory(device, stagingBufferMemory); 1406 1411 1407 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, 1408 1413 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.vertexBuffer, info.vertexBufferMemory); 1409 1414 1410 copyBuffer(stagingBuffer, info.vertexBuffer, bufferSize);1415 copyBuffer(stagingBuffer, info.vertexBuffer, 0, 0, bufferSize); 1411 1416 1412 1417 vkDestroyBuffer(device, stagingBuffer, nullptr); … … 1414 1419 } 1415 1420 1416 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 1417 1425 VkBuffer stagingBuffer; 1418 1426 VkDeviceMemory stagingBufferMemory; … … 1426 1434 vkUnmapMemory(device, stagingBufferMemory); 1427 1435 1428 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, 1429 1437 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.indexBuffer, info.indexBufferMemory); 1430 1438 1431 copyBuffer(stagingBuffer, info.indexBuffer, bufferSize);1439 copyBuffer(stagingBuffer, info.indexBuffer, 0, 0, bufferSize); 1432 1440 1433 1441 vkDestroyBuffer(device, stagingBuffer, nullptr); … … 1479 1487 } 1480 1488 1481 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) { 1482 1509 VkCommandBuffer commandBuffer = beginSingleTimeCommands(); 1483 1510 1484 VkBufferCopy copyRegion = {}; 1485 copyRegion.size = size; 1511 VkBufferCopy copyRegion = { srcOffset, dstOffset, size }; 1486 1512 vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, ©Region); 1487 1513 … … 1683 1709 } 1684 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 1685 1742 /*** START OF REFACTORED CODE ***/ 1686 1743 void mainLoop() { … … 1695 1752 } 1696 1753 if (e.type == SDL_KEYDOWN) { 1697 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) { 1698 1772 quit = true; 1699 1773 }
Note:
See TracChangeset
for help on using the changeset viewer.