GLFWVulkanWindowAdapter.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #ifndef VKW_GLFW_VULKAN_WINDOW_ADAPTER_H
  2. #define VKW_GLFW_VULKAN_WINDOW_ADAPTER_H
  3. #include "../vulkan_include.h" // this must be included before glfw3
  4. #include <GLFW/glfw3.h>
  5. #include "VulkanWindowAdapter.h"
  6. #include <vector>
  7. #include <string>
  8. namespace vkw
  9. {
  10. struct GLFWVulkanWindowAdapter : public VulkanWindowAdapater
  11. {
  12. GLFWwindow * m_window = nullptr;
  13. struct GLFWUserPointer
  14. {
  15. bool requiresResize = false;
  16. VkExtent2D windowExtent;
  17. };
  18. GLFWUserPointer * m_userPtr = nullptr;
  19. void createWindow(const char * title, int w, int h )
  20. {
  21. glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
  22. glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
  23. m_window = glfwCreateWindow(w, h, title, nullptr, nullptr);
  24. m_userPtr = new GLFWUserPointer();
  25. m_userPtr->requiresResize = false;
  26. m_userPtr->windowExtent = { static_cast<uint32_t>(w), static_cast<uint32_t>(h)};
  27. glfwSetFramebufferSizeCallback(m_window, GLFWVulkanWindowAdapter::framebufferResizeCallback);
  28. glfwSetWindowUserPointer(m_window, m_userPtr);
  29. }
  30. static void framebufferResizeCallback(GLFWwindow* window, int w, int h)
  31. {
  32. auto usrPtr = static_cast<GLFWUserPointer*>(glfwGetWindowUserPointer(window));
  33. usrPtr->requiresResize = true;
  34. usrPtr->windowExtent = { static_cast<uint32_t>(w), static_cast<uint32_t>(h)};
  35. }
  36. bool requiresResize() const
  37. {
  38. return m_userPtr->requiresResize;
  39. }
  40. void clearRequireResize()
  41. {
  42. m_userPtr->requiresResize = false;
  43. }
  44. void destroy()
  45. {
  46. if(m_window)
  47. {
  48. glfwDestroyWindow(m_window);
  49. }
  50. if(m_userPtr)
  51. delete m_userPtr;
  52. m_window = nullptr;
  53. }
  54. //=================================================================================
  55. // These functions must be overidden window manager
  56. //=================================================================================
  57. VkSurfaceKHR createSurface(VkInstance instance) override
  58. {
  59. VkSurfaceKHR surface = VK_NULL_HANDLE;
  60. VkResult err = glfwCreateWindowSurface(instance, m_window, NULL, &surface);
  61. return surface;
  62. }
  63. /**
  64. * @brief getRequiredVulkanExtensions
  65. * @return
  66. *
  67. * Get the required vulkan extensions required to
  68. * be able to present to SDL windows
  69. */
  70. std::vector<std::string> getRequiredVulkanExtensions() override
  71. {
  72. std::vector<std::string> outExtensions;
  73. uint32_t glfwExtensionCount = 0;
  74. auto glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
  75. for(size_t i=0;i<glfwExtensionCount;i++)
  76. {
  77. outExtensions.push_back( glfwExtensions[i] );
  78. }
  79. // Add debug display extension, we need this to relay debug messages
  80. outExtensions.emplace_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
  81. return outExtensions;
  82. }
  83. VkExtent2D getDrawableSize() override
  84. {
  85. int iwidth,iheight = 0;
  86. glfwGetWindowSize(m_window, &iwidth, &iheight);
  87. VkExtent2D ext;
  88. ext.width = static_cast<uint32_t>(iwidth);
  89. ext.height = static_cast<uint32_t>(iheight);
  90. return ext;
  91. }
  92. };
  93. }
  94. #endif