SDLVulkanWindowAdapter.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #ifndef VKW_SDL_VULKAN_WINDOW_ADAPTER_H
  2. #define VKW_SDL_VULKAN_WINDOW_ADAPTER_H
  3. #if __has_include(<SDL2/SDL2.h>)
  4. #include <SDL2/SDL2.h>
  5. #include <SDL2/SDL_vulkan.h>
  6. #else
  7. #include <SDL.h>
  8. #include <SDL_vulkan.h>
  9. #endif
  10. #include "../vulkan_include.h"
  11. #include <vector>
  12. #include <string>
  13. #include "VulkanWindowAdapter.h"
  14. namespace vkw
  15. {
  16. struct SDLVulkanWindowAdapter : public VulkanWindowAdapater
  17. {
  18. SDL_Window * m_window = nullptr;
  19. void createWindow(const char * title, int x, int y, int w, int h, Uint32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_VULKAN )
  20. {
  21. m_window = SDL_CreateWindow(title, x, y, w, h, flags);
  22. }
  23. void destroy()
  24. {
  25. if(m_window)
  26. {
  27. SDL_DestroyWindow(m_window);
  28. }
  29. m_window = nullptr;
  30. }
  31. //=================================================================================
  32. // These functions must be overidden window manager
  33. //=================================================================================
  34. VkSurfaceKHR createSurface(VkInstance instance) override
  35. {
  36. VkSurfaceKHR surface = VK_NULL_HANDLE;
  37. SDL_Vulkan_CreateSurface(m_window, instance, &surface);
  38. return surface;
  39. }
  40. /**
  41. * @brief getRequiredVulkanExtensions
  42. * @return
  43. *
  44. * Get the required vulkan extensions required to
  45. * be able to present to SDL windows
  46. */
  47. std::vector<std::string> getRequiredVulkanExtensions() override
  48. {
  49. std::vector<std::string> outExtensions;
  50. // Figure out the amount of extensions vulkan needs to interface with the os windowing system
  51. // This is necessary because vulkan is a platform agnostic API and needs to know how to interface with the windowing system
  52. unsigned int ext_count = 0;
  53. if (!SDL_Vulkan_GetInstanceExtensions(m_window, &ext_count, nullptr))
  54. {
  55. std::runtime_error("Unable to query the number of Vulkan instance extensions");
  56. return {};
  57. }
  58. // Use the amount of extensions queried before to retrieve the names of the extensions
  59. std::vector<const char*> ext_names(ext_count);
  60. if (!SDL_Vulkan_GetInstanceExtensions(m_window, &ext_count, ext_names.data()))
  61. {
  62. std::runtime_error("Unable to query the number of Vulkan instance extension names");
  63. }
  64. // Display names
  65. //std::runtime_error("found " << ext_count << " Vulkan instance extensions:\n";
  66. for (unsigned int i = 0; i < ext_count; i++)
  67. {
  68. //std::cout << i << ": " << ext_names[i] << "\n";
  69. outExtensions.emplace_back(ext_names[i]);
  70. }
  71. // Add debug display extension, we need this to relay debug messages
  72. outExtensions.emplace_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
  73. return outExtensions;
  74. }
  75. VkExtent2D getDrawableSize() override
  76. {
  77. int32_t iwidth,iheight = 0;
  78. SDL_Vulkan_GetDrawableSize(m_window, &iwidth, &iheight);
  79. VkExtent2D ext;
  80. ext.width = static_cast<uint32_t>(iwidth);
  81. ext.height = static_cast<uint32_t>(iheight);
  82. return ext;
  83. }
  84. };
  85. }
  86. #endif