VulkanUtils.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef GAMINGCORE_VULKAN_UTILS_HPP
  2. #define GAMINGCORE_VULKAN_UTILS_HPP
  3. #define GLFW_INCLUDE_VULKAN
  4. #include <core/Types.hpp>
  5. #include <core/Utility.hpp>
  6. #include <GLFW/glfw3.h>
  7. const char* getVulkanResultString(VkResult r);
  8. #define VK_ASSERT(a) \
  9. do { \
  10. VkResult vkResult = (a); \
  11. if(vkResult != VK_SUCCESS) { \
  12. LOG_ERROR("Vulkan error: %s\n", getVulkanResultString(vkResult)); \
  13. return true; \
  14. } \
  15. } while(false)
  16. bool initVulkanInstance();
  17. void destroyVulkanInstance();
  18. bool initVulkanDebugging();
  19. void destroyVulkanDebugging();
  20. #define INVALID_VULKAN_QUEUE_FAMILY (static_cast<u32>(-1))
  21. u32 findVulkanQueueFamily(VkPhysicalDevice pd, VkQueueFlags flags);
  22. u32 findVulkanSurfaceQueueFamily(VkPhysicalDevice pd, VkSurfaceKHR s);
  23. typedef int (*VulkanSurfaceFormatSelector)(const VkSurfaceFormatKHR* sf);
  24. bool findVulkanSurfaceFormat(
  25. VkSurfaceFormatKHR* sf, VkPhysicalDevice pd, VkSurfaceKHR s,
  26. VulkanSurfaceFormatSelector sfs);
  27. typedef int (*VulkanSurfacePresentModeSelector)(VkPresentModeKHR m);
  28. bool findVulkanSurfacePresentMode(
  29. VkPresentModeKHR* m, VkPhysicalDevice pd, VkSurfaceKHR s,
  30. VulkanSurfacePresentModeSelector spms);
  31. typedef int (*VulkanPhysicalDeviceSelector)(VkPhysicalDevice pd);
  32. bool findVulkanPhysicalDevice(
  33. VkPhysicalDevice* pd, VulkanPhysicalDeviceSelector s);
  34. bool hasVulkanExtension(VkPhysicalDevice pd, const char* extension);
  35. struct VulkanDeviceQueueData {
  36. u32 queueFamilyIndex = 0;
  37. float priority = 0.0f;
  38. };
  39. bool initVulkanDevice(
  40. VkDevice* d, VkPhysicalDevice pd, const VulkanDeviceQueueData* data,
  41. size_t nData, const char** extensions, size_t nExtensions);
  42. void destroyVulkanDevice(VkDevice d);
  43. bool initVulkanSurface(VkSurfaceKHR* s, GLFWwindow* w);
  44. void destroyVulkanSurface(VkSurfaceKHR s);
  45. struct VulkanSwapchainData {
  46. VkPhysicalDevice physicalDevice{};
  47. VkDevice device{};
  48. VkSurfaceKHR surface{};
  49. VkExtent2D size{};
  50. VkSurfaceFormatKHR surfaceFormat{};
  51. VkPresentModeKHR presentMode{};
  52. VkSharingMode sharingMode{};
  53. u32 queueFamilyIndexCount = 0;
  54. u32* queueFamilyIndices = nullptr;
  55. };
  56. bool initVulkanSwapchain(VkSwapchainKHR* sc, VulkanSwapchainData* d);
  57. void destroyVulkanSwapchain(VkSwapchainKHR s, VkDevice d);
  58. struct VulkanSwapchainImages {
  59. u32 amount;
  60. VkImage* images;
  61. VkImageView* imageViews;
  62. };
  63. bool initVulkanSwapchainImages(
  64. VulkanSwapchainImages* si, VkDevice d, VkSwapchainKHR sc, VkFormat format);
  65. void destroyVulkanSwapchainImages(VulkanSwapchainImages* si, VkDevice d);
  66. bool initVulkanShaderModule(VkShaderModule* sm, VkDevice d, const char* path);
  67. void destroyVulkanShaderModule(VkShaderModule sm, VkDevice d);
  68. bool initVulkanPipelineLayout(VkPipelineLayout* pl, VkDevice d);
  69. void destroyVulkanPipelineLayout(VkPipelineLayout pl, VkDevice d);
  70. bool initVulkanFramebuffers(
  71. VkFramebuffer** f, VulkanSwapchainImages* si, VkDevice d, VkRenderPass rp,
  72. u32 width, u32 height);
  73. void destroyVulkanFramebuffers(VkFramebuffer** f, u32 amount, VkDevice d);
  74. bool initCommandVulkanBuffer(VkCommandBuffer* cb, VkDevice d, VkCommandPool cp);
  75. bool initVulkanSemaphore(VkSemaphore* s, VkDevice d);
  76. void destroyVulkanSemaphore(VkSemaphore s, VkDevice d);
  77. bool initVulkanFence(VkFence* f, VkDevice d);
  78. void destroyVulkanFence(VkFence f, VkDevice d);
  79. #endif