Changeset 87c8f1a in opengl-game
- Timestamp:
- Nov 1, 2019, 5:11:45 PM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- d2d9286
- Parents:
- 34bdf3a
- git-author:
- Dmitry Portnoy <dmitry.portnoy@…> (11/01/19 17:09:16)
- git-committer:
- Dmitry Portnoy <dmitry.portnoy@…> (11/01/19 17:11:45)
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
crash-logger.cpp
r34bdf3a r87c8f1a 158 158 char* begin_name = NULL; 159 159 char* begin_offset = NULL; 160 char* end_offset = NULL;161 160 162 161 #ifdef MAC … … 200 199 write(fd_out, "\n", 1); 201 200 #else 201 char* end_offset = NULL; 202 202 203 for (char *p = symbollist[i]; *p; p++) { 203 204 if (*p == '(') { -
graphics-pipeline_vulkan.cpp
r34bdf3a r87c8f1a 5 5 #include <iostream> 6 6 7 #include "vulkan-utils.hpp" 8 7 9 using namespace std; 8 10 9 11 // TODO: Remove any instances of cout and instead throw exceptions 10 12 11 GraphicsPipeline_Vulkan::GraphicsPipeline_Vulkan(VkDevice device, VkRenderPass renderPass, Viewport viewport, 12 int vertexSize) { 13 GraphicsPipeline_Vulkan::GraphicsPipeline_Vulkan(VkPhysicalDevice physicalDevice, VkDevice device, 14 VkRenderPass renderPass, Viewport viewport, int vertexSize) { 15 this->physicalDevice = physicalDevice; 13 16 this->device = device; 14 17 this->renderPass = renderPass; 15 18 this->viewport = viewport; 16 19 20 // Since there is only one array of vertex data, we use binding = 0 21 // I'll probably do that for the foreseeable future 22 // I can calculate the stride myself given info about all the varying attributes 17 23 this->bindingDescription.binding = 0; 18 24 this->bindingDescription.stride = vertexSize; … … 21 27 22 28 GraphicsPipeline_Vulkan::~GraphicsPipeline_Vulkan() { 29 } 30 31 void GraphicsPipeline_Vulkan::createVertexBuffer(const void* bufferData, int vertexSize, 32 VkCommandPool commandPool, VkQueue graphicsQueue) { 33 VkDeviceSize bufferSize = numVertices * vertexSize; 34 VkDeviceSize bufferCapacity = vertexCapacity * vertexSize; 35 36 VkBuffer stagingBuffer; 37 VkDeviceMemory stagingBufferMemory; 38 VulkanUtils::createBuffer(device, physicalDevice, bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, 39 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 40 stagingBuffer, stagingBufferMemory); 41 42 void* data; 43 vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data); 44 memcpy(data, bufferData, (size_t) bufferSize); 45 vkUnmapMemory(device, stagingBufferMemory); 46 47 VulkanUtils::createBuffer(device, physicalDevice, bufferCapacity, 48 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, 49 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vertexBuffer, vertexBufferMemory); 50 51 VulkanUtils::copyBuffer(device, commandPool, stagingBuffer, vertexBuffer, 0, 0, bufferSize, graphicsQueue); 52 53 vkDestroyBuffer(device, stagingBuffer, nullptr); 54 vkFreeMemory(device, stagingBufferMemory, nullptr); 55 } 56 57 void GraphicsPipeline_Vulkan::createIndexBuffer(const void* bufferData, int indexSize, 58 VkCommandPool commandPool, VkQueue graphicsQueue) { 59 VkDeviceSize bufferSize = numIndices * indexSize; 60 VkDeviceSize bufferCapacity = indexCapacity * indexSize; 61 62 VkBuffer stagingBuffer; 63 VkDeviceMemory stagingBufferMemory; 64 VulkanUtils::createBuffer(device, physicalDevice, bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, 65 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 66 stagingBuffer, stagingBufferMemory); 67 68 void* data; 69 vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data); 70 memcpy(data, bufferData, (size_t) bufferSize); 71 vkUnmapMemory(device, stagingBufferMemory); 72 73 VulkanUtils::createBuffer(device, physicalDevice, bufferCapacity, 74 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, 75 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexBuffer, indexBufferMemory); 76 77 VulkanUtils::copyBuffer(device, commandPool, stagingBuffer, indexBuffer, 0, 0, bufferSize, graphicsQueue); 78 79 vkDestroyBuffer(device, stagingBuffer, nullptr); 80 vkFreeMemory(device, stagingBufferMemory, nullptr); 23 81 } 24 82 … … 313 371 314 372 void GraphicsPipeline_Vulkan::cleanup() { 315 vkDestroyPipeline( this->device, this->pipeline, nullptr);316 vkDestroyDescriptorPool( this->device, this->descriptorPool, nullptr);317 vkDestroyPipelineLayout( this->device, this->pipelineLayout, nullptr);373 vkDestroyPipeline(device, pipeline, nullptr); 374 vkDestroyDescriptorPool(device, descriptorPool, nullptr); 375 vkDestroyPipelineLayout(device, pipelineLayout, nullptr); 318 376 } 319 377 320 378 void GraphicsPipeline_Vulkan::cleanupBuffers() { 321 vkDestroyDescriptorSetLayout(this->device, this->descriptorSetLayout, nullptr); 322 } 379 vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); 380 381 vkDestroyBuffer(device, vertexBuffer, nullptr); 382 vkFreeMemory(device, vertexBufferMemory, nullptr); 383 vkDestroyBuffer(device, indexBuffer, nullptr); 384 vkFreeMemory(device, indexBufferMemory, nullptr); 385 } -
graphics-pipeline_vulkan.hpp
r34bdf3a r87c8f1a 20 20 class GraphicsPipeline_Vulkan : public GraphicsPipeline { 21 21 public: 22 GraphicsPipeline_Vulkan(VkDevice device, VkRenderPass renderPass, Viewport viewport, int vertexSize); 22 GraphicsPipeline_Vulkan(VkPhysicalDevice physicalDevice, VkDevice device, VkRenderPass renderPass, 23 Viewport viewport, int vertexSize); 23 24 ~GraphicsPipeline_Vulkan(); 25 26 template<class VertexType, class IndexType> 27 void bindData(const vector<VertexType>& vertices, const vector<IndexType>& indices, 28 VkCommandPool commandPool, VkQueue graphicsQueue); 29 30 void createVertexBuffer(const void* bufferData, int vertexSize, VkCommandPool commandPool, 31 VkQueue graphicsQueue); 32 void createIndexBuffer(const void* bufferData, int indexSize, VkCommandPool commandPool, 33 VkQueue graphicsQueue); 24 34 25 35 // Maybe I should rename these to addVertexAttribute (addVaryingAttribute) and addUniformAttribute … … 44 54 vector<char> readFile(const string& filename); 45 55 56 VkPhysicalDevice physicalDevice; 46 57 VkDevice device; 47 58 VkRenderPass renderPass; … … 58 69 VkDescriptorPool descriptorPool; 59 70 vector<VkDescriptorSet> descriptorSets; 71 72 size_t numVertices; 73 size_t vertexCapacity; 74 VkBuffer vertexBuffer; 75 VkDeviceMemory vertexBufferMemory; 76 77 size_t numIndices; 78 size_t indexCapacity; 79 VkBuffer indexBuffer; 80 VkDeviceMemory indexBufferMemory; 60 81 }; 61 82 83 // TODO: Probably better to template the whole class and to also combine this function 84 // and the constructor since I call this right after the constructor anyway 85 template<class VertexType, class IndexType> 86 void GraphicsPipeline_Vulkan::bindData(const vector<VertexType>& vertices, const vector<IndexType>& indices, 87 VkCommandPool commandPool, VkQueue graphicsQueue) { 88 numVertices = vertices.size(); 89 vertexCapacity = numVertices * 2; 90 createVertexBuffer(vertices.data(), sizeof(VertexType), commandPool, graphicsQueue); 91 92 numIndices = indices.size(); 93 indexCapacity = numIndices * 2; 94 createIndexBuffer(indices.data(), sizeof(IndexType), commandPool, graphicsQueue); 95 } 96 62 97 #endif // _GRAPHICS_PIPELINE_VULKAN_H -
vulkan-game.cpp
r34bdf3a r87c8f1a 21 21 gui = nullptr; 22 22 window = nullptr; 23 24 currentFrame = 0; 25 framebufferResized = false; 23 26 } 24 27 … … 138 141 createUniformBuffers(); 139 142 140 graphicsPipelines.push_back(GraphicsPipeline_Vulkan(device, renderPass, 143 vector<Vertex> sceneVertices = { 144 {{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}}, 145 {{ 0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}}, 146 {{ 0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}}, 147 {{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, 148 149 {{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}}, 150 {{ 0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}}, 151 {{ 0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}}, 152 {{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}} 153 }; 154 vector<uint16_t> sceneIndices = { 155 0, 1, 2, 2, 3, 0, 156 4, 5, 6, 6, 7, 4 157 }; 158 159 graphicsPipelines.push_back(GraphicsPipeline_Vulkan(physicalDevice, device, renderPass, 141 160 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, sizeof(Vertex))); 161 162 graphicsPipelines.back().bindData(sceneVertices, sceneIndices, commandPool, graphicsQueue); 142 163 143 164 graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::pos)); … … 155 176 graphicsPipelines.back().createDescriptorSets(swapChainImages); 156 177 157 graphicsPipelines.push_back(GraphicsPipeline_Vulkan(device, renderPass, 178 vector<OverlayVertex> overlayVertices = { 179 {{-1.0f, 1.0f, 0.0f}, {0.0f, 1.0f}}, 180 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 1.0f}}, 181 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f}}, 182 {{-1.0f, -1.0f, 0.0f}, {0.0f, 0.0f}} 183 }; 184 vector<uint16_t> overlayIndices = { 185 0, 1, 2, 2, 3, 0 186 }; 187 188 graphicsPipelines.push_back(GraphicsPipeline_Vulkan(physicalDevice, device, renderPass, 158 189 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, sizeof(OverlayVertex))); 190 191 graphicsPipelines.back().bindData(overlayVertices, overlayIndices, commandPool, graphicsQueue); 159 192 160 193 graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&OverlayVertex::pos)); … … 238 271 239 272 void VulkanGame::renderScene() { 273 vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max()); 274 275 uint32_t imageIndex; 276 277 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; 278 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; 240 279 } 241 280 -
vulkan-game.hpp
r34bdf3a r87c8f1a 91 91 vector<VkFence> inFlightFences; 92 92 93 bool framebufferResized = false; 93 size_t currentFrame; 94 95 bool framebufferResized; 94 96 95 97 bool initWindow(int width, int height, unsigned char guiFlags); -
vulkan-ref.cpp
r34bdf3a r87c8f1a 72 72 glm::vec2 texCoord; 73 73 }; 74 /*** END OF REFACTORED CODE ***/75 74 76 75 struct UniformBufferObject { … … 80 79 }; 81 80 82 /*** START OF REFACTORED CODE ***/83 81 struct DescriptorInfo { 84 82 VkDescriptorType type; … … 88 86 VkDescriptorImageInfo* imageData; 89 87 }; 90 /*** END OF REFACTORED CODE ***/91 88 92 89 struct GraphicsPipelineInfo { 93 /*** START OF REFACTORED CODE ***/94 90 VkPipelineLayout pipelineLayout; 95 91 VkPipeline pipeline; … … 103 99 VkDescriptorSetLayout descriptorSetLayout; 104 100 vector<VkDescriptorSet> descriptorSets; 105 /*** END OF REFACTORED CODE ***/ 106 107 size_t numVertices; // Currently unused 101 102 size_t numVertices; 108 103 size_t vertexCapacity; 109 104 VkBuffer vertexBuffer; … … 116 111 }; 117 112 118 /*** START OF REFACTORED CODE ***/119 113 VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, 120 114 const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, … … 223 217 vector<VkSemaphore> renderFinishedSemaphores; 224 218 vector<VkFence> inFlightFences; 219 220 size_t currentFrame = 0; 225 221 /*** END OF REFACTORED CODE ***/ 226 227 size_t currentFrame = 0;228 222 229 223 size_t numPlanes = 0; // temp … … 340 334 overlayImageInfo.imageView = sdlOverlayImageView; 341 335 overlayImageInfo.sampler = textureSampler; 342 /*** END OF REFACTORED CODE ***/343 336 344 337 // SHADER-SPECIFIC STUFF STARTS HERE … … 364 357 sceneIndices.data(), sizeof(uint16_t), sceneIndices.size()); 365 358 366 /*** START OF REFACTORED CODE ***/367 359 addAttributeDescription(scenePipeline, VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::pos)); 368 360 addAttributeDescription(scenePipeline, VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::color)); … … 373 365 374 366 createDescriptorSetLayout(scenePipeline); 375 /*** END OF REFACTORED CODE ***/376 367 377 368 numPlanes = 2; … … 391 382 overlayIndices.data(), sizeof(uint16_t), overlayIndices.size()); 392 383 393 /*** START OF REFACTORED CODE ***/394 384 addAttributeDescription(overlayPipeline, VK_FORMAT_R32G32B32_SFLOAT, offset_of(&OverlayVertex::pos)); 395 385 addAttributeDescription(overlayPipeline, VK_FORMAT_R32G32_SFLOAT, offset_of(&OverlayVertex::texCoord)); … … 812 802 info.bindingDescription.stride = vertexSize; 813 803 info.bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; 814 /*** END OF REFACTORED CODE ***/815 804 816 805 info.numVertices = numVertices; … … 1393 1382 } 1394 1383 } 1395 /*** END OF REFACTORED CODE ***/1396 1384 1397 1385 void createVertexBuffer(GraphicsPipelineInfo& info, const void* vertexData, int vertexSize) { … … 1443 1431 } 1444 1432 1445 /*** START OF REFACTORED CODE ***/1446 1433 void createUniformBuffers() { 1447 1434 VkDeviceSize bufferSize = sizeof(UniformBufferObject); … … 1507 1494 } 1508 1495 1496 /*** START OF REFACTORED CODE ***/ 1509 1497 void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize srcOffset, VkDeviceSize dstOffset, 1510 1498 VkDeviceSize size) { … … 1550 1538 } 1551 1539 1552 /*** START OF REFACTORED CODE ***/1553 1540 uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) { 1554 1541 VkPhysicalDeviceMemoryProperties memProperties; … … 1801 1788 1802 1789 void drawFrame() { 1790 vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max()); 1791 1792 uint32_t imageIndex; 1803 1793 /*** END OF REFACTORED CODE ***/ 1804 vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max());1805 1806 uint32_t imageIndex;1807 1794 1808 1795 VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(), … … 1861 1848 } 1862 1849 1850 /*** START OF REFACTORED CODE ***/ 1863 1851 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; 1864 1852 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; 1865 /*** START OF REFACTORED CODE ***/1866 1853 } 1867 1854 … … 2052 2039 void cleanupPipelineBuffers(GraphicsPipelineInfo& pipeline) { 2053 2040 vkDestroyDescriptorSetLayout(device, pipeline.descriptorSetLayout, nullptr); 2054 /*** END OF REFACTORED CODE ***/2055 2041 2056 2042 vkDestroyBuffer(device, pipeline.vertexBuffer, nullptr); … … 2059 2045 vkFreeMemory(device, pipeline.indexBufferMemory, nullptr); 2060 2046 } 2047 /*** END OF REFACTORED CODE ***/ 2061 2048 2062 2049 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback( -
vulkan-utils.cpp
r34bdf3a r87c8f1a 482 482 } 483 483 484 void VulkanUtils::copyBuffer(VkDevice device, VkCommandPool commandPool, VkBuffer srcBuffer, 485 VkBuffer dstBuffer, VkDeviceSize srcOffset, VkDeviceSize dstOffset, VkDeviceSize size, 486 VkQueue graphicsQueue) { 487 VkCommandBuffer commandBuffer = beginSingleTimeCommands(device, commandPool); 488 489 VkBufferCopy copyRegion = { srcOffset, dstOffset, size }; 490 vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, ©Region); 491 492 endSingleTimeCommands(device, commandPool, commandBuffer, graphicsQueue); 493 } 494 484 495 bool VulkanUtils::hasStencilComponent(VkFormat format) { 485 496 return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT; -
vulkan-utils.hpp
r34bdf3a r87c8f1a 84 84 uint32_t width, uint32_t height, VkQueue graphicsQueue); 85 85 86 static void copyBuffer(VkDevice device, VkCommandPool commandPool, VkBuffer srcBuffer, VkBuffer dstBuffer, 87 VkDeviceSize srcOffset, VkDeviceSize dstOffset, VkDeviceSize size, VkQueue graphicsQueue); 88 86 89 static bool hasStencilComponent(VkFormat format); 87 90
Note:
See TracChangeset
for help on using the changeset viewer.