Lugdunum  0.1.0
Vulkan.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <unordered_map>
4 #include <vector>
5 #include <ostream>
6 
8 
9 #if defined(LUG_SYSTEM_WINDOWS)
10  #define VK_USE_PLATFORM_WIN32_KHR
11 #elif defined(LUG_SYSTEM_LINUX)
12  #define VK_USE_PLATFORM_XLIB_KHR
13 #elif defined(LUG_SYSTEM_ANDROID)
14  #define VK_USE_PLATFORM_ANDROID_KHR
15 #endif
16 
17 #define VK_NO_PROTOTYPES
18 #ifndef WIN32_LEAN_AND_MEAN
19  #define WIN32_LEAN_AND_MEAN
20 #endif
21 #define NOMINMAX
22 #include <vulkan/vulkan.h>
23 
24 #if defined(LUG_SYSTEM_LINUX)
25  // Fuck you X11
26  #undef None
27 #endif
28 
29 #define LUG_EXPORTED_VULKAN_FUNCTIONS(macro) \
30  macro(vkGetInstanceProcAddr)
31 
32 #define LUG_CORE_VULKAN_FUNCTIONS(macro) \
33  macro(vkEnumerateInstanceExtensionProperties) \
34  macro(vkEnumerateInstanceLayerProperties) \
35  macro(vkCreateInstance)
36 
37 #if defined(LUG_SYSTEM_WINDOWS)
38  #define LUG_VK_KHR_PLATFORM_SURFACE_EXTENSION_NAME VK_KHR_WIN32_SURFACE_EXTENSION_NAME
39  #define LUG_INSTANCE_VULKAN_FUNCTIONS_PLATFORM(macro) \
40  macro(vkCreateWin32SurfaceKHR) // VK_KHR_win32_surface
41 #elif defined(LUG_SYSTEM_LINUX)
42  #define LUG_VK_KHR_PLATFORM_SURFACE_EXTENSION_NAME VK_KHR_XLIB_SURFACE_EXTENSION_NAME
43  #define LUG_INSTANCE_VULKAN_FUNCTIONS_PLATFORM(macro) \
44  macro(vkCreateXlibSurfaceKHR) // VK_KHR_xlib_surface
45 #elif defined(LUG_SYSTEM_ANDROID)
46  #define LUG_VK_KHR_PLATFORM_SURFACE_EXTENSION_NAME VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
47  #define LUG_INSTANCE_VULKAN_FUNCTIONS_PLATFORM(macro) \
48  macro(vkCreateAndroidSurfaceKHR) // VK_KHR_android_surface
49 #endif
50 
51 /* VK_KHR_surface */
52 #define LUG_INSTANCE_VULKAN_FUNCTIONS_KHR_SURFACE(macro) \
53  macro(vkGetPhysicalDeviceSurfaceSupportKHR) \
54  macro(vkGetPhysicalDeviceSurfaceCapabilitiesKHR) \
55  macro(vkGetPhysicalDeviceSurfaceFormatsKHR) \
56  macro(vkGetPhysicalDeviceSurfacePresentModesKHR) \
57  macro(vkDestroySurfaceKHR)
58 
59 #define LUG_INSTANCE_VULKAN_FUNCTIONS(macro) \
60  macro(vkEnumeratePhysicalDevices) \
61  macro(vkGetPhysicalDeviceProperties) \
62  macro(vkGetPhysicalDeviceFeatures) \
63  macro(vkGetPhysicalDeviceQueueFamilyProperties) \
64  macro(vkGetPhysicalDeviceFormatProperties) \
65  macro(vkGetDeviceProcAddr) \
66  macro(vkCreateDevice) \
67  macro(vkDestroyInstance) \
68  macro(vkEnumerateDeviceExtensionProperties) \
69  macro(vkGetPhysicalDeviceMemoryProperties) \
70  LUG_INSTANCE_VULKAN_FUNCTIONS_PLATFORM(macro) \
71  LUG_INSTANCE_VULKAN_FUNCTIONS_KHR_SURFACE(macro)
72 
73 #define LUG_DEVICE_VULKAN_FUNCTIONS_KHR_SWAPCHAIN(macro) \
74  macro(vkCreateSwapchainKHR) \
75  macro(vkGetSwapchainImagesKHR) \
76  macro(vkAcquireNextImageKHR) \
77  macro(vkQueuePresentKHR) \
78  macro(vkDestroySwapchainKHR) \
79 
80 #define LUG_DEVICE_VULKAN_FUNCTIONS(macro) \
81  macro(vkQueueWaitIdle) \
82  macro(vkGetDeviceQueue) \
83  macro(vkDeviceWaitIdle) \
84  macro(vkDestroyDevice) \
85  macro(vkCreateSemaphore) \
86  macro(vkCreateCommandPool) \
87  macro(vkAllocateCommandBuffers) \
88  macro(vkBeginCommandBuffer) \
89  macro(vkCmdPipelineBarrier) \
90  macro(vkCmdClearColorImage) \
91  macro(vkEndCommandBuffer) \
92  macro(vkResetCommandBuffer) \
93  macro(vkQueueSubmit) \
94  macro(vkFreeCommandBuffers) \
95  macro(vkResetCommandPool) \
96  macro(vkDestroyCommandPool) \
97  macro(vkDestroySemaphore) \
98  macro(vkCreateImageView) \
99  macro(vkCreateRenderPass) \
100  macro(vkCreateFramebuffer) \
101  macro(vkCreateShaderModule) \
102  macro(vkCreatePipelineLayout) \
103  macro(vkCreateGraphicsPipelines) \
104  macro(vkCmdBeginRenderPass) \
105  macro(vkCmdBindPipeline) \
106  macro(vkCmdDraw) \
107  macro(vkCmdDrawIndexed) \
108  macro(vkCmdEndRenderPass) \
109  macro(vkDestroyShaderModule) \
110  macro(vkDestroyPipelineLayout) \
111  macro(vkDestroyPipeline) \
112  macro(vkDestroyRenderPass) \
113  macro(vkDestroyFramebuffer) \
114  macro(vkDestroyImageView) \
115  macro(vkCreateFence) \
116  macro(vkCreateBuffer) \
117  macro(vkGetBufferMemoryRequirements) \
118  macro(vkAllocateMemory) \
119  macro(vkBindBufferMemory) \
120  macro(vkMapMemory) \
121  macro(vkFlushMappedMemoryRanges) \
122  macro(vkUnmapMemory) \
123  macro(vkCmdSetViewport) \
124  macro(vkCmdSetScissor) \
125  macro(vkCmdSetBlendConstants) \
126  macro(vkCmdBindVertexBuffers) \
127  macro(vkCmdBindIndexBuffer) \
128  macro(vkWaitForFences) \
129  macro(vkResetFences) \
130  macro(vkGetFenceStatus) \
131  macro(vkFreeMemory) \
132  macro(vkDestroyBuffer) \
133  macro(vkDestroyFence) \
134  macro(vkCmdCopyBuffer) \
135  macro(vkCreateImage) \
136  macro(vkGetImageMemoryRequirements) \
137  macro(vkBindImageMemory) \
138  macro(vkCreateSampler) \
139  macro(vkCmdCopyImage) \
140  macro(vkCmdCopyBufferToImage) \
141  macro(vkCreateDescriptorSetLayout) \
142  macro(vkCreateDescriptorPool) \
143  macro(vkAllocateDescriptorSets) \
144  macro(vkUpdateDescriptorSets) \
145  macro(vkFreeDescriptorSets) \
146  macro(vkCmdUpdateBuffer) \
147  macro(vkCmdBindDescriptorSets) \
148  macro(vkDestroyDescriptorPool) \
149  macro(vkDestroyDescriptorSetLayout) \
150  macro(vkDestroySampler) \
151  macro(vkDestroyImage) \
152  macro(vkCmdPushConstants) \
153  LUG_DEVICE_VULKAN_FUNCTIONS_KHR_SWAPCHAIN(macro)
154 
155 inline namespace Vulkan {
156 
157 #define LUG_DEFINE_DECLARATION_VULKAN_FUNCTIONS(name) extern PFN_##name LUG_GRAPHICS_API name;
162 #undef LUG_DEFINE_DECLARATION__VULKAN_FUNCTIONS
163 
164 } // Vulkan
165 
166 namespace lug {
167 namespace Graphics {
168 namespace Vulkan {
169 
170 struct InstanceInfo {
171  std::vector<VkExtensionProperties> extensions;
172  std::vector<VkLayerProperties> layers;
173 
174  bool containsExtension(const char* extensionName) const;
175  bool containsLayer(const char* layerName) const;
176 };
177 
179  VkPhysicalDevice handle;
180 
181  VkPhysicalDeviceProperties properties;
182  VkPhysicalDeviceFeatures features;
183 
184  VkPhysicalDeviceMemoryProperties memoryProperties;
185 
186  std::vector<VkQueueFamilyProperties> queueFamilies;
187 
188  std::vector<VkExtensionProperties> extensions;
189 
190  std::unordered_map<VkFormat, VkFormatProperties> formatProperties;
191 
192  struct Swapchain {
193  VkSurfaceCapabilitiesKHR capabilities;
194 
195  std::vector<VkSurfaceFormatKHR> formats;
196  std::vector<VkPresentModeKHR> presentModes;
197  } swapchain;
198 
199  bool containsExtension(const char* extensionName) const;
200  bool containsQueueFlags(VkQueueFlags queueFlags, int8_t& idx) const;
201 };
202 
203 } // Vulkan
204 } // Graphics
205 } // lug
206 
210 inline std::ostream& operator<<(std::ostream& ss, VkResult result) {
211  switch (result) {
212  case VK_SUCCESS:
213  ss << "Success";
214  break;
215  case VK_NOT_READY:
216  ss << "A fence or query has not yet completed";
217  break;
218  case VK_TIMEOUT:
219  ss << "A wait operation has not completed in the specified time";
220  break;
221  case VK_EVENT_SET:
222  ss << "An event is signaled";
223  break;
224  case VK_EVENT_RESET:
225  ss << "An event is unsignaled";
226  break;
227  case VK_INCOMPLETE:
228  ss << "A return array was too small for the result";
229  break;
230  case VK_ERROR_OUT_OF_HOST_MEMORY:
231  ss << "A host memory allocation has failed";
232  break;
233  case VK_ERROR_OUT_OF_DEVICE_MEMORY:
234  ss << "A device memory allocation has failed";
235  break;
236  case VK_ERROR_INITIALIZATION_FAILED:
237  ss << "Initialization of an object could not be completed for implementation-specific reasons";
238  break;
239  case VK_ERROR_DEVICE_LOST:
240  ss << "The logical or physical device has been lost";
241  break;
242  case VK_ERROR_MEMORY_MAP_FAILED:
243  ss << "Mapping of a memory object has failed";
244  break;
245  case VK_ERROR_LAYER_NOT_PRESENT:
246  ss << "A requested layer is not present or could not be loaded";
247  break;
248  case VK_ERROR_EXTENSION_NOT_PRESENT:
249  ss << "A requested extension is not supported";
250  break;
251  case VK_ERROR_FEATURE_NOT_PRESENT:
252  ss << "A requested feature is not supported";
253  break;
254  case VK_ERROR_INCOMPATIBLE_DRIVER:
255  ss << "The requested version of Vulkan is not supported by the driver or is otherwise incompatible";
256  break;
257  case VK_ERROR_TOO_MANY_OBJECTS:
258  ss << "Too many objects of the type have already been created";
259  break;
260  case VK_ERROR_FORMAT_NOT_SUPPORTED:
261  ss << "A requested format is not supported on this device";
262  break;
263  case VK_ERROR_SURFACE_LOST_KHR:
264  ss << "A surface is no longer available";
265  break;
266  case VK_SUBOPTIMAL_KHR:
267  ss << "A swapchain no longer matches the surface properties exactly, but can still be used";
268  break;
269  case VK_ERROR_OUT_OF_DATE_KHR:
270  ss << "A surface has changed in such a way that it is no longer compatible with the swapchain";
271  break;
272  case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR:
273  ss << "The display used by a swapchain does not use the same presentable image layout";
274  break;
275  case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR:
276  ss << "The requested window is already connected to a VkSurfaceKHR, or to some other non-Vulkan API";
277  break;
278  case VK_ERROR_VALIDATION_FAILED_EXT:
279  ss << "A validation layer found an error";
280  break;
281  default:
282  ss << "ERROR: UNKNOWN VULKAN ERROR";
283  break;
284  }
285 
286  return ss;
287 }
VkPhysicalDeviceMemoryProperties memoryProperties
Definition: Vulkan.hpp:184
std::vector< VkQueueFamilyProperties > queueFamilies
Definition: Vulkan.hpp:186
VkPhysicalDeviceFeatures features
Definition: Vulkan.hpp:182
std::vector< VkExtensionProperties > extensions
Definition: Vulkan.hpp:171
std::vector< VkPresentModeKHR > presentModes
Definition: Vulkan.hpp:196
std::vector< VkSurfaceFormatKHR > formats
Definition: Vulkan.hpp:195
#define LUG_DEFINE_DECLARATION_VULKAN_FUNCTIONS(name)
Definition: Vulkan.hpp:157
LUG_DEVICE_VULKAN_FUNCTIONS(LUG_DEFINE_DECLARATION_VULKAN_FUNCTIONS)
std::vector< VkExtensionProperties > extensions
Definition: Vulkan.hpp:188
LUG_CORE_VULKAN_FUNCTIONS(LUG_DEFINE_DECLARATION_VULKAN_FUNCTIONS)
std::unordered_map< VkFormat, VkFormatProperties > formatProperties
Definition: Vulkan.hpp:190
std::ostream & operator<<(std::ostream &ss, VkResult result)
Definition: Vulkan.hpp:210
LUG_INSTANCE_VULKAN_FUNCTIONS(LUG_DEFINE_DECLARATION_VULKAN_FUNCTIONS)
VkPhysicalDeviceProperties properties
Definition: Vulkan.hpp:181
std::vector< VkLayerProperties > layers
Definition: Vulkan.hpp:172
LUG_EXPORTED_VULKAN_FUNCTIONS(LUG_DEFINE_DECLARATION_VULKAN_FUNCTIONS)