Changeset e3bef3a in opengl-game
- Timestamp:
- Nov 12, 2019, 6:55:07 AM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- cd487fb
- Parents:
- 5a23277
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
graphics-pipeline_vulkan.cpp
r5a23277 re3bef3a 4 4 #include <stdexcept> 5 5 #include <iostream> 6 7 #include "vulkan-utils.hpp"8 6 9 7 using namespace std; -
graphics-pipeline_vulkan.hpp
r5a23277 re3bef3a 4 4 #include "graphics-pipeline.hpp" 5 5 6 #include <iostream> 6 7 #include <vector> 7 8 8 9 #include <vulkan/vulkan.h> 10 11 #include "vulkan-utils.hpp" 12 13 using namespace std; 14 15 // TODO: Remove any instances of cout and instead throw exceptions 9 16 10 17 // TODO: Maybe change the name of this struct so I can call the list something other than descriptorInfoList … … 26 33 void updateRenderPass(VkRenderPass renderPass); 27 34 28 template<class VertexType , class IndexType>29 void bindData(const vector<VertexType>& vertices, const vector< IndexType>& indices,35 template<class VertexType> 36 void bindData(const vector<VertexType>& vertices, const vector<uint16_t>& indices, 30 37 VkCommandPool commandPool, VkQueue graphicsQueue); 31 38 … … 48 55 49 56 void createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage); 57 58 template<class VertexType> 59 bool addObject(const vector<VertexType>& vertices, vector<uint16_t>& indices, VkCommandPool commandPool, 60 VkQueue graphicsQueue); 50 61 51 62 void cleanup(); … … 83 94 }; 84 95 85 // TODO: Probably better to template the whole class and to also combine this function 86 // and the constructor since I call this right after the constructor anyway 87 template<class VertexType, class IndexType> 88 void GraphicsPipeline_Vulkan::bindData(const vector<VertexType>& vertices, const vector<IndexType>& indices, 96 // TODO: Probably better to template the whole class 97 // TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference 98 99 // TODO: combine this function and the constructor since I call this right after the constructor anyway 100 template<class VertexType> 101 void GraphicsPipeline_Vulkan::bindData(const vector<VertexType>& vertices, const vector<uint16_t>& indices, 89 102 VkCommandPool commandPool, VkQueue graphicsQueue) { 90 103 numVertices = vertices.size(); … … 94 107 numIndices = indices.size(); 95 108 indexCapacity = numIndices * 2; 96 createIndexBuffer(indices.data(), sizeof(IndexType), commandPool, graphicsQueue); 109 createIndexBuffer(indices.data(), sizeof(uint16_t), commandPool, graphicsQueue); 110 } 111 112 template<class VertexType> 113 bool GraphicsPipeline_Vulkan::addObject(const vector<VertexType>& vertices, vector<uint16_t>& indices, 114 VkCommandPool commandPool, VkQueue graphicsQueue) { 115 cout << "Adding object to pipeline..." << endl; 116 117 if (numVertices + vertices.size() > vertexCapacity) { 118 cout << "ERROR: Need to resize vertex buffers" << endl; 119 } else if (numIndices + indices.size() > indexCapacity) { 120 cout << "ERROR: Need to resize index buffers" << endl; 121 } else { 122 cout << "Added object to scene" << endl; 123 124 for (uint16_t& idx : indices) { 125 idx += numVertices; 126 } 127 128 VulkanUtils::copyDataToBuffer(device, physicalDevice, commandPool, vertices, vertexBuffer, numVertices, 129 graphicsQueue); 130 numVertices += vertices.size(); 131 132 VulkanUtils::copyDataToBuffer(device, physicalDevice, commandPool, indices, indexBuffer, numIndices, 133 graphicsQueue); 134 numIndices += indices.size(); 135 136 return true; 137 } 138 139 return false; 97 140 } 98 141 -
vulkan-game.cpp
r5a23277 re3bef3a 254 254 255 255 cout << "Created " << graphicsPipelines.size() << " graphics pipelines" << endl; 256 257 numPlanes = 2; 256 258 257 259 createCommandBuffers(); … … 281 283 framebufferResized = true; 282 284 break; 283 case UI_EVENT_KEY :285 case UI_EVENT_KEYDOWN: 284 286 if (e.key.keycode == SDL_SCANCODE_ESCAPE) { 285 287 quit = true; 288 } else if (e.key.keycode == SDL_SCANCODE_SPACE) { 289 cout << "Adding a plane" << endl; 290 float zOffset = -0.5f + (0.5f * numPlanes); 291 vector<Vertex> vertices = { 292 {{-0.5f, -0.5f, zOffset}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}}, 293 {{ 0.5f, -0.5f, zOffset}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}}, 294 {{ 0.5f, 0.5f, zOffset}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}}, 295 {{-0.5f, 0.5f, zOffset}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}} 296 }; 297 vector<uint16_t> indices = { 298 0, 1, 2, 2, 3, 0 299 }; 300 301 if (graphicsPipelines[0].addObject(vertices, indices, commandPool, graphicsQueue)) { 302 vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data()); 303 createCommandBuffers(); 304 305 numPlanes++; 306 } 286 307 } else { 287 308 cout << "Key event detected" << endl; … … 943 964 } 944 965 966 // TODO: Fix the crash that happens when alt-tabbing 945 967 void VulkanGame::recreateSwapChain() { 946 968 cout << "Recreating swap chain" << endl; -
vulkan-game.hpp
r5a23277 re3bef3a 97 97 98 98 size_t currentFrame; 99 size_t numPlanes = 0; // temp 99 100 100 101 bool framebufferResized; -
vulkan-ref.cpp
r5a23277 re3bef3a 27 27 using namespace glm; 28 28 29 /*** START OF REFACTORED CODE ***/30 29 const int SCREEN_WIDTH = 800; 31 30 const int SCREEN_HEIGHT = 600; … … 217 216 218 217 size_t currentFrame = 0; 219 /*** END OF REFACTORED CODE ***/220 221 218 size_t numPlanes = 0; // temp 222 219 223 /*** START OF REFACTORED CODE ***/224 220 bool framebufferResized = false; 225 221 … … 1465 1461 vkBindBufferMemory(device, buffer, bufferMemory, 0); 1466 1462 } 1467 /*** END OF REFACTORED CODE ***/1468 1463 1469 1464 void copyDataToBuffer(const void* srcData, VkBuffer dst, VkDeviceSize dstOffset, VkDeviceSize dataSize) { … … 1485 1480 } 1486 1481 1487 /*** START OF REFACTORED CODE ***/1488 1482 void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize srcOffset, VkDeviceSize dstOffset, 1489 1483 VkDeviceSize size) { … … 1689 1683 } 1690 1684 } 1691 /*** END OF REFACTORED CODE ***/1692 1685 1693 1686 bool addObjectToScene(GraphicsPipelineInfo& info, … … 1722 1715 } 1723 1716 1724 /*** START OF REFACTORED CODE ***/1725 1717 void mainLoop() { 1726 1718 // TODO: Create some generic event-handling functions in game-gui-* … … 2075 2067 return EXIT_SUCCESS; 2076 2068 } 2077 /*** END OF REFACTORED CODE ***/ -
vulkan-utils.hpp
r5a23277 re3bef3a 87 87 uint32_t width, uint32_t height, VkQueue graphicsQueue); 88 88 89 template<class DataType> 90 static void copyDataToBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, 91 const vector<DataType>& srcData, VkBuffer dstBuffer, size_t dstVertexOffset, VkQueue graphicsQueue); 92 89 93 static void copyBuffer(VkDevice device, VkCommandPool commandPool, VkBuffer srcBuffer, VkBuffer dstBuffer, 90 94 VkDeviceSize srcOffset, VkDeviceSize dstOffset, VkDeviceSize size, VkQueue graphicsQueue); … … 95 99 }; 96 100 101 template<class DataType> 102 void VulkanUtils::copyDataToBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, 103 const vector<DataType>& srcData, VkBuffer dstBuffer, size_t dstVertexOffset, VkQueue graphicsQueue) { 104 VkDeviceSize srcDataSize = srcData.size() * sizeof(DataType); 105 106 VkBuffer stagingBuffer; 107 VkDeviceMemory stagingBufferMemory; 108 createBuffer(device, physicalDevice, srcDataSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, 109 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 110 stagingBuffer, stagingBufferMemory); 111 112 void* data; 113 vkMapMemory(device, stagingBufferMemory, 0, srcDataSize, 0, &data); 114 memcpy(data, srcData.data(), (size_t)srcDataSize); 115 vkUnmapMemory(device, stagingBufferMemory); 116 117 copyBuffer(device, commandPool, stagingBuffer, dstBuffer, 0, dstVertexOffset * sizeof(DataType), srcDataSize, 118 graphicsQueue); 119 120 vkDestroyBuffer(device, stagingBuffer, nullptr); 121 vkFreeMemory(device, stagingBufferMemory, nullptr); 122 } 123 97 124 #endif // _VULKAN_UTILS_H
Note:
See TracChangeset
for help on using the changeset viewer.