VulkanBase.cppm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. export module Core.VulkanBase;
  2. export import Core.Types;
  3. export import Core.List;
  4. export import Core.Types;
  5. export import Core.Vulkan;
  6. import Core.Array;
  7. import Core.Utility;
  8. import Core.VulkanUtility;
  9. export namespace Core::Vulkan {
  10. inline constexpr u32 INVALID_QUEUE_FAMILY = 0xFFFF'FFFF;
  11. inline constexpr u32 INVALID_MEMORY_TYPE = 0xFFFF'FFFF;
  12. struct DeviceQueueData {
  13. u32 queueFamilyIndex = 0;
  14. float priority = 0.0f;
  15. };
  16. class Base {
  17. VkInstance instance;
  18. #ifdef DEBUG_VULKAN
  19. VkDebugUtilsMessengerEXT debugMessenger;
  20. VkDebugReportCallbackEXT debugReportCallback;
  21. #endif
  22. VkSurfaceKHR surface;
  23. VkPhysicalDevice physicalDevice;
  24. VkDevice device;
  25. public:
  26. Base();
  27. ~Base();
  28. Base(const Base&) = delete;
  29. Base(Base&&) = delete;
  30. Base& operator=(const Base&) = delete;
  31. Base& operator=(Base&&) = delete;
  32. bool init();
  33. void destroy();
  34. PFN_vkVoidFunction getFunction(const char* name);
  35. template<typename T>
  36. using PhysicalDeviceSelector = int (*)(Base&, T&);
  37. template<typename T>
  38. bool findPhysicalDevice(T& data, PhysicalDeviceSelector<T> s) {
  39. Core::Array<VkPhysicalDevice, 32> devices;
  40. u32 c = devices.getLength();
  41. if(check(vkEnumeratePhysicalDevices(instance, &c, &devices[0]))) {
  42. return true;
  43. }
  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. }