example_SDLWindow_vulkanProfiles.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include <iostream>
  2. #include <vkw/VKWVulkanWindow.h>
  3. #include <vkw/Adapters/SDLVulkanWindowAdapter.h>
  4. #include <cassert>
  5. #include <vkw/VKWVulkanWindowProfile.h>
  6. #include <vulkan/vk_format_utils.h>
  7. // callback function for validation layers
  8. static VKAPI_ATTR VkBool32 VKAPI_CALL VulkanReportFunc(
  9. VkDebugReportFlagsEXT flags,
  10. VkDebugReportObjectTypeEXT objType,
  11. uint64_t obj,
  12. size_t location,
  13. int32_t code,
  14. const char* layerPrefix,
  15. const char* msg,
  16. void* userData)
  17. {
  18. printf("VULKAN VALIDATION: [%s] %s\n", layerPrefix, msg);
  19. return VK_FALSE;
  20. }
  21. //#define PROFILE_NAME VP_LUNARG_DESKTOP_PORTABILITY_2021_NAME
  22. //#define PROFILE_SPEC_VERSION VP_LUNARG_DESKTOP_PORTABILITY_2021_SPEC_VERSION
  23. #define PROFILE_NAME VP_KHR_ROADMAP_2022_NAME
  24. #define PROFILE_SPEC_VERSION VP_KHR_ROADMAP_2022_SPEC_VERSION
  25. #if defined(__WIN32__)
  26. int SDL_main(int argc, char *argv[])
  27. #else
  28. int main(int argc, char *argv[])
  29. #endif
  30. {
  31. auto window = new vkw::VKWVulkanWindowProfile();
  32. auto sdl_window = new vkw::SDLVulkanWindowAdapter();
  33. sdl_window->createWindow("Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024,768);
  34. window->setWindowAdapater(sdl_window);
  35. //-------------------------------------------------------------
  36. //-------------------------------------------------------------
  37. // Vulkan Initialization part
  38. //-------------------------------------------------------------
  39. const VpProfileProperties profile_properties = {PROFILE_NAME, PROFILE_SPEC_VERSION};
  40. // first get the required extensions we need for the window
  41. auto requiredInstanceExtensions = sdl_window->getRequiredVulkanExtensions();
  42. requiredInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
  43. // create a vulkan instance using the profiles
  44. window->createProfileInstance(profile_properties, requiredInstanceExtensions, {"VK_LAYER_KHRONOS_validation"});
  45. // set the debug callback function
  46. window->setDebugCallback(&VulkanReportFunc);
  47. // create the surface we want to use.
  48. // This is dependent on the window manager
  49. auto surface = sdl_window->createSurface(window->getInstance());
  50. // get a physical device suitable for presenting
  51. // to the surface
  52. auto physicalDevice = window->getPhysicalDevice(window->getInstance(), surface);
  53. window->createProfileDevice(physicalDevice, surface, profile_properties, {VK_KHR_SWAPCHAIN_EXTENSION_NAME});
  54. //-------------------------------------------------------------
  55. bool running=true;
  56. while(running)
  57. {
  58. SDL_Event event;
  59. bool resize=false;
  60. while (SDL_PollEvent(&event))
  61. {
  62. if (event.type == SDL_QUIT)
  63. {
  64. running = false;
  65. }
  66. else if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED
  67. /*&& event.window.windowID == SDL_GetWindowID( window->getSDLWindow()) */ )
  68. {
  69. resize=true;
  70. }
  71. }
  72. if( resize )
  73. {
  74. // If the window has changed size. we need to rebuild the swapchain
  75. // and any other textures (depth texture)
  76. window->rebuildSwapchain();
  77. }
  78. // Get the next available frame.
  79. // the Frame struct is simply a POD containing
  80. // all the information that you need to record a command buffer
  81. auto frame = window->acquireNextFrame();
  82. frame.beginCommandBuffer();
  83. frame.clearColor = {{1.f,0.f,0.f,0.f}};
  84. frame.beginRenderPass( frame.commandBuffer );
  85. // record to frame.commandbuffer
  86. frame.endRenderPass(frame.commandBuffer);
  87. frame.endCommandBuffer();
  88. window->submitFrame(frame);
  89. // Present the frame after you have recorded
  90. // the command buffer;
  91. window->presentFrame(frame);
  92. window->waitForPresent();
  93. }
  94. window->destroy();
  95. sdl_window->destroy();
  96. delete window;
  97. delete sdl_window;
  98. }
  99. #include <vkw/VKWVulkanWindow.inl>