2
0

example_SDLWindow_vulkanProfiles.cpp 4.0 KB

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