VulkanBase.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #ifndef GAMINGCORE_VULKAN_BASE_HPP
  2. #define GAMINGCORE_VULKAN_BASE_HPP
  3. #include <core/Array.hpp>
  4. #include <core/List.hpp>
  5. #include <core/Types.hpp>
  6. #include <core/Utility.hpp>
  7. #define GLFW_INCLUDE_VULKAN
  8. #include <GLFW/glfw3.h>
  9. #include "core/VulkanUtility.hpp"
  10. namespace Core::Vulkan {
  11. [[maybe_unused]] constexpr u32 INVALID_QUEUE_FAMILY = 0xFFFF'FFFF;
  12. [[maybe_unused]] constexpr u32 INVALID_MEMORY_TYPE = 0xFFFF'FFFF;
  13. struct DeviceQueueData {
  14. u32 queueFamilyIndex = 0;
  15. float priority = 0.0f;
  16. };
  17. class Base {
  18. VkInstance instance;
  19. #ifdef DEBUG_VULKAN
  20. VkDebugUtilsMessengerEXT debugMessenger;
  21. VkDebugReportCallbackEXT debugReportCallback;
  22. #endif
  23. VkSurfaceKHR surface;
  24. VkPhysicalDevice physicalDevice;
  25. VkDevice device;
  26. public:
  27. Base();
  28. ~Base();
  29. Base(const Base&) = delete;
  30. Base(Base&&) = delete;
  31. Base& operator=(const Base&) = delete;
  32. Base& operator=(Base&&) = delete;
  33. bool init();
  34. void destroy();
  35. PFN_vkVoidFunction getFunction(const char* name);
  36. template<typename T>
  37. using PhysicalDeviceSelector = int (*)(Base&, T&);
  38. template<typename T>
  39. bool findPhysicalDevice(T& data, PhysicalDeviceSelector<T> s) {
  40. Core::Array<VkPhysicalDevice, 32> devices;
  41. u32 c = devices.getLength();
  42. VK_CHECK_TRUE(
  43. vkEnumeratePhysicalDevices(instance, &c, &devices[0]));
  44. int bestPoints = 0;
  45. VkPhysicalDevice best = VK_NULL_HANDLE;
  46. for(u32 i = 0; i < c; i++) {
  47. physicalDevice = devices[i];
  48. T userData;
  49. int points = s(*this, userData);
  50. if(points > bestPoints) {
  51. bestPoints = points;
  52. best = physicalDevice;
  53. data = userData;
  54. }
  55. }
  56. physicalDevice = best;
  57. return bestPoints == 0;
  58. }
  59. void getPhysicalDeviceProperties(VkPhysicalDeviceProperties& p);
  60. u32 findQueueFamily(VkQueueFlags flags);
  61. u32 findSurfaceQueueFamily();
  62. bool hasExtension(const char* extension);
  63. bool getSurfaceCapabilities(VkSurfaceCapabilitiesKHR& c);
  64. using SurfaceFormatSelector = int (*)(const VkSurfaceFormatKHR&);
  65. bool findSurfaceFormat(
  66. VkSurfaceFormatKHR& sf, SurfaceFormatSelector sfs);
  67. using SurfacePresentModeSelector = int (*)(VkPresentModeKHR);
  68. bool findSurfacePresentMode(
  69. VkPresentModeKHR& m, SurfacePresentModeSelector spms);
  70. bool initDevice(
  71. const List<DeviceQueueData> data,
  72. const List<const char*>& extensions);
  73. void waitForIdle();
  74. u32 findMemoryType(u32 typeFilter, VkMemoryPropertyFlags flags);
  75. operator VkDevice() {
  76. return device;
  77. }
  78. operator VkSurfaceKHR() {
  79. return surface;
  80. }
  81. private:
  82. bool initInstance();
  83. #ifdef DEBUG_VULKAN
  84. bool initDebugMessenger();
  85. bool initDebugReportCallback();
  86. void destroyDebugMessenger();
  87. void destroyDebugReportCallback();
  88. #endif
  89. bool initSurface();
  90. };
  91. }
  92. #endif