Changeset d2d9286 in opengl-game


Ignore:
Timestamp:
Nov 7, 2019, 2:05:17 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
f985231
Parents:
87c8f1a
Message:

In vulkangame, implement the renderScene function to draw a frame in Vulkan and implement a test UI overlay

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • graphics-pipeline_vulkan.cpp

    r87c8f1a rd2d9286  
    326326      &descriptorSets[currentImage], 0, nullptr);
    327327
    328    // TODO: Implement once I add vertex and index buffers to the pipeline
    329    /*
    330    VkBuffer vertexBuffers[] = { info.vertexBuffer };
     328   VkBuffer vertexBuffers[] = { vertexBuffer };
    331329   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);
    338335}
    339336
  • vulkan-game.cpp

    r87c8f1a rd2d9286  
    215215   UIEvent e;
    216216   bool quit = false;
    217 
    218    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    219217
    220218   while (!quit) {
     
    266264
    267265void 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);
    268271   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);
    270282}
    271283
     
    274286
    275287   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   }
    276341
    277342   currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
     
    774839      vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
    775840
    776       // reateGraphicsPipelineCommands(scenePipeline, i);
    777       // createGraphicsPipelineCommands(overlayPipeline, i);
    778841      for (GraphicsPipeline_Vulkan pipeline : graphicsPipelines) {
    779842         pipeline.createRenderCommands(commandBuffers[i], i);
     
    809872}
    810873
     874void 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
    811889void VulkanGame::cleanupSwapChain() {
    812890   VulkanUtils::destroyVulkanImage(device, depthImage);
  • vulkan-game.hpp

    r87c8f1a rd2d9286  
    124124      void createSyncObjects();
    125125
     126      void recreateSwapChain();
     127
    126128      void cleanupSwapChain();
    127129
  • vulkan-ref.cpp

    r87c8f1a rd2d9286  
    823823      }
    824824
    825 /*** START OF REFACTORED CODE ***/
    826825      void addDescriptorInfo(GraphicsPipelineInfo& info, VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData, VkDescriptorImageInfo* imageData) {
    827826         info.descriptorInfoList.push_back({ type, stageFlags, bufferData, imageData });
     
    11601159         view = createImageView(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
    11611160      }
    1162 /*** END OF REFACTORED CODE ***/
    11631161
    11641162      void populateImageFromSDLTexture(SDL_Texture* texture, VkImage& image) {
     
    12051203      }
    12061204
    1207 /*** START OF REFACTORED CODE ***/
    12081205      void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage,
    12091206            VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory) {
     
    16531650            vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
    16541651
    1655 /*** END OF REFACTORED CODE ***/
    16561652            createGraphicsPipelineCommands(scenePipeline, i);
    16571653            createGraphicsPipelineCommands(overlayPipeline, i);
    1658 /*** START OF REFACTORED CODE ***/
    16591654
    16601655            vkCmdEndRenderPass(commandBuffers[i]);
     
    16701665         vkCmdBindDescriptorSets(commandBuffers[currentImage], VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipelineLayout, 0, 1,
    16711666            &info.descriptorSets[currentImage], 0, nullptr);
    1672 /*** END OF REFACTORED CODE ***/
    16731667
    16741668         VkBuffer vertexBuffers[] = { info.vertexBuffer };
     
    16791673
    16801674         vkCmdDrawIndexed(commandBuffers[currentImage], static_cast<uint32_t>(info.numIndices), 1, 0, 0, 0);
    1681 /*** START OF REFACTORED CODE ***/
    16821675      }
    16831676
     
    17911784
    17921785         uint32_t imageIndex;
    1793 /*** END OF REFACTORED CODE ***/
    17941786
    17951787         VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(),
     
    18021794            throw runtime_error("failed to acquire swap chain image!");
    18031795         }
     1796/*** END OF REFACTORED CODE ***/
    18041797
    18051798         updateUniformBuffer(imageIndex);
    18061799
     1800/*** START OF REFACTORED CODE ***/
    18071801         VkSubmitInfo submitInfo = {};
    18081802         submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
     
    18481842         }
    18491843
    1850 /*** START OF REFACTORED CODE ***/
    18511844         currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
    18521845         currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
     
    18541847
    18551848      void drawUI() {
    1856 /*** END OF REFACTORED CODE ***/
    18571849         // TODO: Since I currently don't use any other render targets,
    18581850         // I may as well set this once before the render loop
     
    18611853         SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0x00);
    18621854         SDL_RenderClear(gRenderer);
     1855/*** END OF REFACTORED CODE ***/
    18631856
    18641857         SDL_Rect rect;
     
    18771870         SDL_RenderCopy(gRenderer, uiImage, nullptr, &rect);
    18781871
     1872/*** START OF REFACTORED CODE ***/
    18791873         SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0xFF, 0xFF);
    18801874         SDL_RenderDrawLine(gRenderer, 50, 5, 150, 500);
    18811875
    18821876         populateImageFromSDLTexture(uiOverlay, sdlOverlayImage);
    1883 /*** START OF REFACTORED CODE ***/
    18841877      }
    18851878/*** END OF REFACTORED CODE ***/
     
    19031896      }
    19041897
     1898/*** START OF REFACTORED CODE ***/
    19051899      void recreateSwapChain() {
    19061900         gui->refreshWindowSize();
     
    19131907
    19141908         vkDeviceWaitIdle(device);
     1909/*** END OF REFACTORED CODE ***/
    19151910
    19161911         cleanupSwapChain();
     
    19211916
    19221917         createBufferResources();
    1923       }
    1924 
    19251918/*** START OF REFACTORED CODE ***/
     1919      }
     1920
    19261921      void createBufferResources() {
    19271922         createDepthResources();
     
    20452040         vkFreeMemory(device, pipeline.indexBufferMemory, nullptr);
    20462041      }
    2047 /*** END OF REFACTORED CODE ***/
    20482042
    20492043      static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
     
    20762070};
    20772071
    2078 /*** START OF REFACTORED CODE ***/
    20792072int main(int argc, char* argv[]) {
    20802073
  • vulkan-utils.cpp

    r87c8f1a rd2d9286  
    269269   VkDeviceMemory stagingBufferMemory;
    270270
    271    createBuffer(device, physicalDevice, imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
     271   createBuffer(device, physicalDevice, imageSize,
     272      VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
    272273      VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    273274      stagingBuffer, stagingBufferMemory);
     
    308309
    309310   image.imageView = createImageView(device, image.image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
     311}
     312
     313void 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);
    310359}
    311360
  • vulkan-utils.hpp

    r87c8f1a rd2d9286  
    7070      static void createVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice,
    7171            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);
    7275      static void createDepthImage(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool,
    7376            VkFormat depthFormat, VkExtent2D extent, VulkanImage& image, VkQueue graphicsQueue);
Note: See TracChangeset for help on using the changeset viewer.