yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
3.6 KiB134 linesraw
1#pragma once
2
3#define VK_NO_PROTOTYPES
4#include "vulkan/vulkan.h"
5
6// This file provides basic loading and helper functions for using
7// the Vulkan API.
8
9// The Vulkan function pointers we will use in this example.
10// clang-format off
11#define VK_API_GLOBAL_PROCS(x) \
12    x(vkGetInstanceProcAddr) \
13    x(vkCreateInstance) \
14    x(vkEnumerateInstanceLayerProperties) \
15    x(vkDestroyInstance) \
16    /* */
17
18#define VK_API_INSTANCE_PROCS_OPT(x) \
19    x(vkGetPhysicalDeviceFeatures2) \
20    x(vkGetPhysicalDeviceProperties2) \
21    x(vkCreateDebugReportCallbackEXT) \
22    x(vkDestroyDebugReportCallbackEXT) \
23    x(vkDebugReportMessageEXT) \
24    /* */
25
26#define VK_API_INSTANCE_PROCS(x) \
27    x(vkCreateDevice) \
28    x(vkDestroyDevice) \
29    x(vkEnumeratePhysicalDevices) \
30    x(vkGetPhysicalDeviceProperties) \
31    x(vkGetPhysicalDeviceFeatures) \
32    x(vkGetPhysicalDeviceMemoryProperties) \
33    x(vkGetPhysicalDeviceQueueFamilyProperties) \
34    x(vkGetPhysicalDeviceFormatProperties) \
35    x(vkGetDeviceProcAddr) \
36    /* */
37
38#define VK_API_DEVICE_PROCS(x) \
39    x(vkCreateDescriptorPool) \
40    x(vkDestroyDescriptorPool) \
41    x(vkGetDeviceQueue) \
42    x(vkQueueSubmit) \
43    x(vkQueueWaitIdle) \
44    x(vkCreateBuffer) \
45    x(vkAllocateMemory) \
46    x(vkMapMemory) \
47    x(vkUnmapMemory) \
48    x(vkCmdCopyBuffer) \
49    x(vkDestroyBuffer) \
50    x(vkFreeMemory) \
51    x(vkCreateDescriptorSetLayout) \
52    x(vkDestroyDescriptorSetLayout) \
53    x(vkAllocateDescriptorSets) \
54    x(vkUpdateDescriptorSets) \
55    x(vkCreatePipelineLayout) \
56    x(vkDestroyPipelineLayout) \
57    x(vkCreateComputePipelines) \
58    x(vkDestroyPipeline) \
59    x(vkCreateShaderModule) \
60    x(vkDestroyShaderModule) \
61    x(vkCreateCommandPool) \
62    x(vkDestroyCommandPool) \
63    \
64    x(vkGetBufferMemoryRequirements) \
65    \
66    x(vkCmdBindPipeline) \
67    x(vkCmdBindDescriptorSets) \
68    x(vkCmdDispatch) \
69    \
70    x(vkFreeCommandBuffers) \
71    x(vkAllocateCommandBuffers) \
72    x(vkBeginCommandBuffer) \
73    x(vkEndCommandBuffer) \
74    x(vkBindBufferMemory) \
75    /* */
76
77#define VK_API_ALL_GLOBAL_PROCS(x) \
78    VK_API_GLOBAL_PROCS(x)
79
80#define VK_API_ALL_INSTANCE_PROCS(x) \
81    VK_API_INSTANCE_PROCS(x) \
82
83#define VK_API_ALL_PROCS(x) \
84    VK_API_ALL_GLOBAL_PROCS(x) \
85    VK_API_ALL_INSTANCE_PROCS(x) \
86    VK_API_DEVICE_PROCS(x) \
87    VK_API_INSTANCE_PROCS_OPT(x) \
88    /* */
89
90#define VK_API_DECLARE_PROC(NAME) PFN_##NAME NAME = nullptr;
91// clang-format on
92
93struct VulkanAPI
94{
95    VkInstance instance = VK_NULL_HANDLE;
96    VkDevice device = VK_NULL_HANDLE;
97    VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
98    VkDebugReportCallbackEXT debugReportCallback = VK_NULL_HANDLE;
99    void* vulkanLibraryHandle = nullptr;
100    VkPhysicalDeviceProperties deviceProperties;
101    VkPhysicalDeviceFeatures deviceFeatures;
102    VkPhysicalDeviceMemoryProperties deviceMemoryProperties;
103    int queueFamilyIndex = -1;
104
105    VK_API_ALL_PROCS(VK_API_DECLARE_PROC)
106
107    /// Initialize the instance functions
108    int initInstanceProcs();
109
110    /// Called before initDevice
111    int initPhysicalDevice(VkPhysicalDevice physicalDevice);
112
113    /// Initialize the device functions
114    int initDeviceProcs();
115
116    /// Finds the proper memory type index for a given requirement.
117    int findMemoryTypeIndex(uint32_t typeBits, VkMemoryPropertyFlags properties);
118
119    /// Clean up and frees device and instance.
120    ~VulkanAPI();
121};
122
123#define RETURN_ON_FAIL(x) \
124    {                     \
125        auto _res = x;    \
126        if (_res != 0)    \
127        {                 \
128            return -1;    \
129        }                 \
130    }
131
132// Loads Vulkan library and creates a VkDevice.
133// Returns 0 if successful.
134int initializeVulkanDevice(VulkanAPI& api);