Changeset d2d9286 in opengl-game
- Timestamp:
- Nov 7, 2019, 2:05:17 AM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- f985231
- Parents:
- 87c8f1a
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
graphics-pipeline_vulkan.cpp
r87c8f1a rd2d9286 326 326 &descriptorSets[currentImage], 0, nullptr); 327 327 328 // TODO: Implement once I add vertex and index buffers to the pipeline 329 /* 330 VkBuffer vertexBuffers[] = { info.vertexBuffer }; 328 VkBuffer vertexBuffers[] = { vertexBuffer }; 331 329 VkDeviceSize offsets[] = { 0 }; 332 vkCmdBindVertexBuffers(commandBuffers[currentImage], 0, 1, vertexBuffers, offsets); 333 334 vkCmdBindIndexBuffer(commandBuffers[currentImage], info.indexBuffer, 0, VK_INDEX_TYPE_UINT16); 335 336 vkCmdDrawIndexed(commandBuffers[currentImage], static_cast<uint32_t>(info.numIndices), 1, 0, 0, 0); 337 */ 330 vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets); 331 332 vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT16); 333 334 vkCmdDrawIndexed(commandBuffer, static_cast<uint32_t>(numIndices), 1, 0, 0, 0); 338 335 } 339 336 -
vulkan-game.cpp
r87c8f1a rd2d9286 215 215 UIEvent e; 216 216 bool quit = false; 217 218 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);219 217 220 218 while (!quit) { … … 266 264 267 265 void VulkanGame::renderUI() { 266 // TODO: Since I currently don't use any other render targets, 267 // I may as well set this once before the render loop 268 SDL_SetRenderTarget(renderer, uiOverlay); 269 270 SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00); 268 271 SDL_RenderClear(renderer); 269 SDL_RenderPresent(renderer); 272 273 SDL_Rect rect = {280, 220, 100, 100}; 274 SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF); 275 SDL_RenderFillRect(renderer, &rect); 276 277 SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF); 278 SDL_RenderDrawLine(renderer, 50, 5, 150, 500); 279 280 VulkanUtils::populateVulkanImageFromSDLTexture(device, physicalDevice, commandPool, uiOverlay, renderer, 281 sdlOverlayImage, graphicsQueue); 270 282 } 271 283 … … 274 286 275 287 uint32_t imageIndex; 288 289 VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(), 290 imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex); 291 292 if (result == VK_ERROR_OUT_OF_DATE_KHR) { 293 recreateSwapChain(); 294 return; 295 } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) { 296 throw runtime_error("failed to acquire swap chain image!"); 297 } 298 299 VkSubmitInfo submitInfo = {}; 300 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; 301 302 VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] }; 303 VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT }; 304 305 submitInfo.waitSemaphoreCount = 1; 306 submitInfo.pWaitSemaphores = waitSemaphores; 307 submitInfo.pWaitDstStageMask = waitStages; 308 submitInfo.commandBufferCount = 1; 309 submitInfo.pCommandBuffers = &commandBuffers[imageIndex]; 310 311 VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[currentFrame] }; 312 313 submitInfo.signalSemaphoreCount = 1; 314 submitInfo.pSignalSemaphores = signalSemaphores; 315 316 vkResetFences(device, 1, &inFlightFences[currentFrame]); 317 318 if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) { 319 throw runtime_error("failed to submit draw command buffer!"); 320 } 321 322 VkPresentInfoKHR presentInfo = {}; 323 presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; 324 presentInfo.waitSemaphoreCount = 1; 325 presentInfo.pWaitSemaphores = signalSemaphores; 326 327 VkSwapchainKHR swapChains[] = { swapChain }; 328 presentInfo.swapchainCount = 1; 329 presentInfo.pSwapchains = swapChains; 330 presentInfo.pImageIndices = &imageIndex; 331 presentInfo.pResults = nullptr; 332 333 result = vkQueuePresentKHR(presentQueue, &presentInfo); 334 335 if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized) { 336 framebufferResized = false; 337 recreateSwapChain(); 338 } else if (result != VK_SUCCESS) { 339 throw runtime_error("failed to present swap chain image!"); 340 } 276 341 277 342 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; … … 774 839 vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); 775 840 776 // reateGraphicsPipelineCommands(scenePipeline, i);777 // createGraphicsPipelineCommands(overlayPipeline, i);778 841 for (GraphicsPipeline_Vulkan pipeline : graphicsPipelines) { 779 842 pipeline.createRenderCommands(commandBuffers[i], i); … … 809 872 } 810 873 874 void VulkanGame::recreateSwapChain() { 875 cout << "Recreating swap chain" << endl; 876 gui->refreshWindowSize(); 877 878 while (gui->getWindowWidth() == 0 || gui->getWindowHeight() == 0 || 879 (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0) { 880 SDL_WaitEvent(nullptr); 881 gui->refreshWindowSize(); 882 } 883 884 vkDeviceWaitIdle(device); 885 886 //cleanupSwapChain(); 887 } 888 811 889 void VulkanGame::cleanupSwapChain() { 812 890 VulkanUtils::destroyVulkanImage(device, depthImage); -
vulkan-game.hpp
r87c8f1a rd2d9286 124 124 void createSyncObjects(); 125 125 126 void recreateSwapChain(); 127 126 128 void cleanupSwapChain(); 127 129 -
vulkan-ref.cpp
r87c8f1a rd2d9286 823 823 } 824 824 825 /*** START OF REFACTORED CODE ***/826 825 void addDescriptorInfo(GraphicsPipelineInfo& info, VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData, VkDescriptorImageInfo* imageData) { 827 826 info.descriptorInfoList.push_back({ type, stageFlags, bufferData, imageData }); … … 1160 1159 view = createImageView(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT); 1161 1160 } 1162 /*** END OF REFACTORED CODE ***/1163 1161 1164 1162 void populateImageFromSDLTexture(SDL_Texture* texture, VkImage& image) { … … 1205 1203 } 1206 1204 1207 /*** START OF REFACTORED CODE ***/1208 1205 void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, 1209 1206 VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory) { … … 1653 1650 vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); 1654 1651 1655 /*** END OF REFACTORED CODE ***/1656 1652 createGraphicsPipelineCommands(scenePipeline, i); 1657 1653 createGraphicsPipelineCommands(overlayPipeline, i); 1658 /*** START OF REFACTORED CODE ***/1659 1654 1660 1655 vkCmdEndRenderPass(commandBuffers[i]); … … 1670 1665 vkCmdBindDescriptorSets(commandBuffers[currentImage], VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipelineLayout, 0, 1, 1671 1666 &info.descriptorSets[currentImage], 0, nullptr); 1672 /*** END OF REFACTORED CODE ***/1673 1667 1674 1668 VkBuffer vertexBuffers[] = { info.vertexBuffer }; … … 1679 1673 1680 1674 vkCmdDrawIndexed(commandBuffers[currentImage], static_cast<uint32_t>(info.numIndices), 1, 0, 0, 0); 1681 /*** START OF REFACTORED CODE ***/1682 1675 } 1683 1676 … … 1791 1784 1792 1785 uint32_t imageIndex; 1793 /*** END OF REFACTORED CODE ***/1794 1786 1795 1787 VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(), … … 1802 1794 throw runtime_error("failed to acquire swap chain image!"); 1803 1795 } 1796 /*** END OF REFACTORED CODE ***/ 1804 1797 1805 1798 updateUniformBuffer(imageIndex); 1806 1799 1800 /*** START OF REFACTORED CODE ***/ 1807 1801 VkSubmitInfo submitInfo = {}; 1808 1802 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; … … 1848 1842 } 1849 1843 1850 /*** START OF REFACTORED CODE ***/1851 1844 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; 1852 1845 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; … … 1854 1847 1855 1848 void drawUI() { 1856 /*** END OF REFACTORED CODE ***/1857 1849 // TODO: Since I currently don't use any other render targets, 1858 1850 // I may as well set this once before the render loop … … 1861 1853 SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0x00); 1862 1854 SDL_RenderClear(gRenderer); 1855 /*** END OF REFACTORED CODE ***/ 1863 1856 1864 1857 SDL_Rect rect; … … 1877 1870 SDL_RenderCopy(gRenderer, uiImage, nullptr, &rect); 1878 1871 1872 /*** START OF REFACTORED CODE ***/ 1879 1873 SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0xFF, 0xFF); 1880 1874 SDL_RenderDrawLine(gRenderer, 50, 5, 150, 500); 1881 1875 1882 1876 populateImageFromSDLTexture(uiOverlay, sdlOverlayImage); 1883 /*** START OF REFACTORED CODE ***/1884 1877 } 1885 1878 /*** END OF REFACTORED CODE ***/ … … 1903 1896 } 1904 1897 1898 /*** START OF REFACTORED CODE ***/ 1905 1899 void recreateSwapChain() { 1906 1900 gui->refreshWindowSize(); … … 1913 1907 1914 1908 vkDeviceWaitIdle(device); 1909 /*** END OF REFACTORED CODE ***/ 1915 1910 1916 1911 cleanupSwapChain(); … … 1921 1916 1922 1917 createBufferResources(); 1923 }1924 1925 1918 /*** START OF REFACTORED CODE ***/ 1919 } 1920 1926 1921 void createBufferResources() { 1927 1922 createDepthResources(); … … 2045 2040 vkFreeMemory(device, pipeline.indexBufferMemory, nullptr); 2046 2041 } 2047 /*** END OF REFACTORED CODE ***/2048 2042 2049 2043 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback( … … 2076 2070 }; 2077 2071 2078 /*** START OF REFACTORED CODE ***/2079 2072 int main(int argc, char* argv[]) { 2080 2073 -
vulkan-utils.cpp
r87c8f1a rd2d9286 269 269 VkDeviceMemory stagingBufferMemory; 270 270 271 createBuffer(device, physicalDevice, imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, 271 createBuffer(device, physicalDevice, imageSize, 272 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, 272 273 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 273 274 stagingBuffer, stagingBufferMemory); … … 308 309 309 310 image.imageView = createImageView(device, image.image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT); 311 } 312 313 void VulkanUtils::populateVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice, 314 VkCommandPool commandPool, SDL_Texture* texture, SDL_Renderer* renderer, VulkanImage& image, 315 VkQueue graphicsQueue) { 316 int a, w, h; 317 318 SDL_QueryTexture(texture, nullptr, &a, &w, &h); 319 320 VkDeviceSize imageSize = w * h * 4; 321 unsigned char* pixels = new unsigned char[imageSize]; 322 323 SDL_RenderReadPixels(renderer, nullptr, SDL_PIXELFORMAT_ABGR8888, pixels, w * 4); 324 325 VkBuffer stagingBuffer; 326 VkDeviceMemory stagingBufferMemory; 327 328 createBuffer(device, physicalDevice, imageSize, 329 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT, 330 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 331 stagingBuffer, stagingBufferMemory); 332 333 void* data; 334 335 vkMapMemory(device, stagingBufferMemory, 0, VK_WHOLE_SIZE, 0, &data); 336 memcpy(data, pixels, static_cast<size_t>(imageSize)); 337 338 VkMappedMemoryRange mappedMemoryRange = {}; 339 mappedMemoryRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; 340 mappedMemoryRange.memory = stagingBufferMemory; 341 mappedMemoryRange.offset = 0; 342 mappedMemoryRange.size = VK_WHOLE_SIZE; 343 344 // TODO: Should probably check that the function succeeded 345 vkFlushMappedMemoryRanges(device, 1, &mappedMemoryRange); 346 vkUnmapMemory(device, stagingBufferMemory); 347 348 delete[] pixels; 349 350 transitionImageLayout(device, commandPool, image.image, VK_FORMAT_R8G8B8A8_UNORM, 351 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, graphicsQueue); 352 copyBufferToImage(device, commandPool, stagingBuffer, image.image, 353 static_cast<uint32_t>(w), static_cast<uint32_t>(h), graphicsQueue); 354 transitionImageLayout(device, commandPool, image.image, VK_FORMAT_R8G8B8A8_UNORM, 355 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, graphicsQueue); 356 357 vkDestroyBuffer(device, stagingBuffer, nullptr); 358 vkFreeMemory(device, stagingBufferMemory, nullptr); 310 359 } 311 360 -
vulkan-utils.hpp
r87c8f1a rd2d9286 70 70 static void createVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice, 71 71 SDL_Texture* texture, VulkanImage& image); 72 static void populateVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice, 73 VkCommandPool commandPool, SDL_Texture* texture, SDL_Renderer* renderer, VulkanImage& image, 74 VkQueue graphicsQueue); 72 75 static void createDepthImage(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, 73 76 VkFormat depthFormat, VkExtent2D extent, VulkanImage& image, VkQueue graphicsQueue);
Note:
See TracChangeset
for help on using the changeset viewer.