1 #ifndef MSP_GL_VULKAN_H_
2 #define MSP_GL_VULKAN_H_
4 #include <vulkan/vulkan.h>
5 #include <msp/graphics/vulkancontext.h>
11 template<typename T> struct HandleTraits;
12 template<> struct HandleTraits<VkBuffer> { using NativeHandle = ::VkBuffer; };
13 template<> struct HandleTraits<::VkBuffer> { using Wrapper = VkBuffer; };
14 template<> struct HandleTraits<VkCommandBuffer> { using NativeHandle = ::VkCommandBuffer; };
15 template<> struct HandleTraits<::VkCommandBuffer> { using Wrapper = VkCommandBuffer; };
16 template<> struct HandleTraits<VkCommandPool> { using NativeHandle = ::VkCommandPool; };
17 template<> struct HandleTraits<::VkCommandPool> { using Wrapper = VkCommandPool; };
18 template<> struct HandleTraits<VkDevice> { using NativeHandle = ::VkDevice; };
19 template<> struct HandleTraits<::VkDevice> { using Wrapper = VkDevice; };
20 template<> struct HandleTraits<VkDescriptorPool> { using NativeHandle = ::VkDescriptorPool; };
21 template<> struct HandleTraits<::VkDescriptorPool> { using Wrapper = VkDescriptorPool; };
22 template<> struct HandleTraits<VkDescriptorSet> { using NativeHandle = ::VkDescriptorSet; };
23 template<> struct HandleTraits<::VkDescriptorSet> { using Wrapper = VkDescriptorSet; };
24 template<> struct HandleTraits<VkDescriptorSetLayout> { using NativeHandle = ::VkDescriptorSetLayout; };
25 template<> struct HandleTraits<::VkDescriptorSetLayout> { using Wrapper = VkDescriptorSetLayout; };
26 template<> struct HandleTraits<VkDeviceMemory> { using NativeHandle = ::VkDeviceMemory; };
27 template<> struct HandleTraits<::VkDeviceMemory> { using Wrapper = VkDeviceMemory; };
28 template<> struct HandleTraits<VkFence> { using NativeHandle = ::VkFence; };
29 template<> struct HandleTraits<::VkFence> { using Wrapper = VkFence; };
30 template<> struct HandleTraits<VkFramebuffer> { using NativeHandle = ::VkFramebuffer; };
31 template<> struct HandleTraits<::VkFramebuffer> { using Wrapper = VkFramebuffer; };
32 template<> struct HandleTraits<VkImage> { using NativeHandle = ::VkImage; };
33 template<> struct HandleTraits<::VkImage> { using Wrapper = VkImage; };
34 template<> struct HandleTraits<VkImageView> { using NativeHandle = ::VkImageView; };
35 template<> struct HandleTraits<::VkImageView> { using Wrapper = VkImageView; };
36 template<> struct HandleTraits<VkPhysicalDevice> { using NativeHandle = ::VkPhysicalDevice; };
37 template<> struct HandleTraits<::VkPhysicalDevice> { using Wrapper = VkPhysicalDevice; };
38 template<> struct HandleTraits<VkPipeline> { using NativeHandle = ::VkPipeline; };
39 template<> struct HandleTraits<::VkPipeline> { using Wrapper = VkPipeline; };
40 template<> struct HandleTraits<VkPipelineCache> { using NativeHandle = ::VkPipelineCache; };
41 template<> struct HandleTraits<::VkPipelineCache> { using Wrapper = VkPipelineCache; };
42 template<> struct HandleTraits<VkPipelineLayout> { using NativeHandle = ::VkPipelineLayout; };
43 template<> struct HandleTraits<::VkPipelineLayout> { using Wrapper = VkPipelineLayout; };
44 template<> struct HandleTraits<VkQueue> { using NativeHandle = ::VkQueue; };
45 template<> struct HandleTraits<::VkQueue> { using Wrapper = VkQueue; };
46 template<> struct HandleTraits<VkRenderPass> { using NativeHandle = ::VkRenderPass; };
47 template<> struct HandleTraits<::VkRenderPass> { using Wrapper = VkRenderPass; };
48 template<> struct HandleTraits<VkSampler> { using NativeHandle = ::VkSampler; };
49 template<> struct HandleTraits<::VkSampler> { using Wrapper = VkSampler; };
50 template<> struct HandleTraits<VkSemaphore> { using NativeHandle = ::VkSemaphore; };
51 template<> struct HandleTraits<::VkSemaphore> { using Wrapper = VkSemaphore; };
52 template<> struct HandleTraits<VkShaderModule> { using NativeHandle = ::VkShaderModule; };
53 template<> struct HandleTraits<::VkShaderModule> { using Wrapper = VkShaderModule; };
54 template<> struct HandleTraits<VkSurface> { using NativeHandle = ::VkSurfaceKHR; };
55 template<> struct HandleTraits<::VkSurfaceKHR> { using Wrapper = VkSurface; };
56 template<> struct HandleTraits<VkSwapchain> { using NativeHandle = ::VkSwapchainKHR; };
57 template<> struct HandleTraits<::VkSwapchainKHR> { using Wrapper = VkSwapchain; };
60 T handle_cast(typename HandleTraits<T>::Wrapper handle)
61 { return reinterpret_cast<T>(handle); }
64 T handle_cast(typename HandleTraits<T>::NativeHandle handle)
65 { return reinterpret_cast<T>(handle); }
68 T handle_cast(typename HandleTraits<typename std::remove_pointer<T>::type>::Wrapper *handle)
69 { return reinterpret_cast<T>(handle); }
72 T handle_cast(const typename HandleTraits<typename std::remove_const<typename std::remove_pointer<T>::type>::type>::Wrapper *handle)
73 { return reinterpret_cast<T>(handle); }
84 Result(VkResult r, const char *f): result(r), function(f) { }
85 Result(Result &&r): result(r.result), function(r.function) { r.result = VK_SUCCESS; }
86 ~Result() noexcept(false) { if(!checked) check(); }
88 bool operator==(VkResult r) { bool m = (result==r); checked |= m; return m; }
89 void check() { if(result!=VK_SUCCESS) throw Graphics::vulkan_error(result, function); }
96 const Graphics::VulkanContext &context;
98 ::VkPhysicalDevice physicalDevice;
99 ::VkQueue graphicsQueue;
101 PFN_vkGetPhysicalDeviceProperties vkGetPhysicalDeviceProperties = 0; // 5.1
102 PFN_vkCreateCommandPool vkCreateCommandPool = 0; // 6.2
103 PFN_vkResetCommandPool vkResetCommandPool = 0; // 6.2
104 PFN_vkDestroyCommandPool vkDestroyCommandPool = 0; // 6.2
105 PFN_vkAllocateCommandBuffers vkAllocateCommandBuffers = 0; // 6.3
106 PFN_vkBeginCommandBuffer vkBeginCommandBuffer = 0; // 6.4
107 PFN_vkEndCommandBuffer vkEndCommandBuffer = 0; // 6.4
108 PFN_vkQueueSubmit vkQueueSubmit = 0; // 6.5
109 PFN_vkCmdExecuteCommands vkCmdExecuteCommands = 0; // 6.7
110 PFN_vkCreateFence vkCreateFence = 0; // 7.3
111 PFN_vkDestroyFence vkDestroyFence = 0; // 7.3
112 PFN_vkGetFenceStatus vkGetFenceStatus = 0; // 7.3
113 PFN_vkResetFences vkResetFences = 0; // 7.3
114 PFN_vkWaitForFences vkWaitForFences = 0; // 7.3
115 PFN_vkCreateSemaphore vkCreateSemaphore = 0; // 7.4
116 PFN_vkDestroySemaphore vkDestroySemaphore = 0; // 7.4
117 PFN_vkCmdPipelineBarrier vkCmdPipelineBarrier = 0; // 7.6
118 PFN_vkQueueWaitIdle vkQueueWaitIdle = 0; // 7.8
119 PFN_vkCreateRenderPass vkCreateRenderPass = 0; // 8.1
120 PFN_vkDestroyRenderPass vkDestroyRenderPass = 0; // 8.1
121 PFN_vkCreateFramebuffer vkCreateFramebuffer = 0; // 8.3
122 PFN_vkDestroyFramebuffer vkDestroyFramebuffer = 0; // 8.3
123 PFN_vkCmdBeginRenderPass vkCmdBeginRenderPass = 0; // 8.4
124 PFN_vkCmdEndRenderPass vkCmdEndRenderPass = 0; // 8.4
125 PFN_vkCreateShaderModule vkCreateShaderModule = 0; // 9.1
126 PFN_vkDestroyShaderModule vkDestroyShaderModule = 0; // 9.1
127 PFN_vkCreateGraphicsPipelines vkCreateGraphicsPipelines = 0; // 10.2
128 PFN_vkDestroyPipeline vkDestroyPipeline = 0; // 10.4
129 PFN_vkCmdBindPipeline vkCmdBindPipeline = 0; // 10.10
130 PFN_vkGetPhysicalDeviceMemoryProperties vkGetPhysicalDeviceMemoryProperties = 0; // 11.2.1
131 PFN_vkAllocateMemory vkAllocateMemory = 0; // 11.2.3
132 PFN_vkFreeMemory vkFreeMemory = 0; // 11.2.8
133 PFN_vkMapMemory vkMapMemory = 0; // 11.2.9
134 PFN_vkUnmapMemory vkUnmapMemory = 0; // 11.2.9
135 PFN_vkCreateBuffer vkCreateBuffer = 0; // 12.1
136 PFN_vkDestroyBuffer vkDestroyBuffer = 0; // 12.1
137 PFN_vkCreateImage vkCreateImage = 0; // 12.3
138 PFN_vkDestroyImage vkDestroyImage = 0; // 12.3
139 PFN_vkCreateImageView vkCreateImageView = 0; // 12.5
140 PFN_vkDestroyImageView vkDestroyImageView = 0; // 12.5
141 PFN_vkGetBufferMemoryRequirements vkGetBufferMemoryRequirements = 0; // 12.7
142 PFN_vkGetImageMemoryRequirements vkGetImageMemoryRequirements = 0; // 12.7
143 PFN_vkBindBufferMemory vkBindBufferMemory = 0; // 12.7
144 PFN_vkBindImageMemory vkBindImageMemory = 0; // 12.7
145 PFN_vkCreateSampler vkCreateSampler = 0; // 13
146 PFN_vkDestroySampler vkDestroySampler = 0; // 13
147 PFN_vkCreateDescriptorSetLayout vkCreateDescriptorSetLayout = 0; // 14.2.1
148 PFN_vkDestroyDescriptorSetLayout vkDestroyDescriptorSetLayout = 0; // 14.2.1
149 PFN_vkCreatePipelineLayout vkCreatePipelineLayout = 0; // 14.2.2
150 PFN_vkDestroyPipelineLayout vkDestroyPipelineLayout = 0; // 14.2.2
151 PFN_vkCreateDescriptorPool vkCreateDescriptorPool = 0; // 14.2.3
152 PFN_vkDestroyDescriptorPool vkDestroyDescriptorPool = 0; // 14.2.3
153 PFN_vkAllocateDescriptorSets vkAllocateDescriptorSets = 0; // 14.2.3
154 PFN_vkUpdateDescriptorSets vkUpdateDescriptorSets = 0; // 14.2.4
155 PFN_vkCmdBindDescriptorSets vkCmdBindDescriptorSets = 0; // 14.2.7
156 PFN_vkCmdPushConstants vkCmdPushConstants = 0; // 14.2.10
157 PFN_vkCmdCopyBuffer vkCmdCopyBuffer = 0; // 19.2
158 PFN_vkCmdCopyBufferToImage vkCmdCopyBufferToImage = 0; // 19.4
159 PFN_vkCmdBlitImage vkCmdBlitImage = 0; // 19.5
160 PFN_vkCmdBindIndexBuffer vkCmdBindIndexBuffer = 0; // 20.3
161 PFN_vkCmdDrawIndexed vkCmdDrawIndexed = 0; // 20.3
162 PFN_vkCmdBindVertexBuffers vkCmdBindVertexBuffers = 0; // 21.2
163 PFN_vkCmdSetViewport vkCmdSetViewport = 0; // 24.5
164 PFN_vkCmdSetScissor vkCmdSetScissor = 0; // 26.1
165 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR vkGetPhysicalDeviceSurfaceCapabilities = 0; // 30.5.1
166 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR vkGetPhysicalDeviceSurfaceFormats = 0; // 30.5.2
167 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR vkGetPhysicalDeviceSurfacePresentModes = 0; // 30.5.3
168 PFN_vkCreateSwapchainKHR vkCreateSwapchain = 0; // 30.8
169 PFN_vkDestroySwapchainKHR vkDestroySwapchain = 0; // 30.8
170 PFN_vkGetSwapchainImagesKHR vkGetSwapchainImages = 0; // 30.8
171 PFN_vkAcquireNextImageKHR vkAcquireNextImage = 0; // 30.8
172 PFN_vkQueuePresentKHR vkQueuePresent = 0; // 30.8
173 PFN_vkGetPhysicalDeviceFormatProperties vkGetPhysicalDeviceFormatProperties = 0; // 39.2
174 PFN_vkSetDebugUtilsObjectNameEXT vkSetDebugUtilsObjectName = 0; // 45.1.1
177 VulkanFunctions(const Graphics::VulkanContext &);
179 // Chapter 5: Devices and Queues
180 void GetPhysicalDeviceProperties(VkPhysicalDeviceProperties &rProperties) const
181 { vkGetPhysicalDeviceProperties(physicalDevice, &rProperties); }
183 // Chapter 6: Command Buffers
184 Result CreateCommandPool(const VkCommandPoolCreateInfo &rCreateInfo, VkCommandPool &rCommandPool) const
185 { return { vkCreateCommandPool(device, &rCreateInfo, 0, handle_cast<::VkCommandPool *>(&rCommandPool)), "vkCreateCommandPool" }; }
187 Result ResetCommandPool(VkCommandPool commandPool, VkCommandPoolResetFlags flags) const
188 { return { vkResetCommandPool(device, handle_cast<::VkCommandPool>(commandPool), flags), "vkResetCommandPool" }; }
190 void DestroyCommandPool(VkCommandPool commandPool) const
191 { vkDestroyCommandPool(device, handle_cast<::VkCommandPool>(commandPool), 0); }
193 Result AllocateCommandBuffers(const VkCommandBufferAllocateInfo &rAllocateInfo, VkCommandBuffer *pCommandBuffers) const
194 { return { vkAllocateCommandBuffers(device, &rAllocateInfo, handle_cast<::VkCommandBuffer *>(pCommandBuffers)), "vkAllocateCommandBuffers" }; }
196 Result BeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo &rBeginInfo) const
197 { return { vkBeginCommandBuffer(handle_cast<::VkCommandBuffer>(commandBuffer), &rBeginInfo), "vkBeginCommandBuffer" }; }
199 Result EndCommandBuffer(VkCommandBuffer commandBuffer) const
200 { return { vkEndCommandBuffer(handle_cast<::VkCommandBuffer>(commandBuffer)), "vkEndCommandBuffer" }; }
202 Result QueueSubmit(std::uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence) const
203 { return { vkQueueSubmit(graphicsQueue, submitCount, pSubmits, handle_cast<::VkFence>(fence)), "vkQueueSubmit" }; }
205 void CmdExecuteCommands(VkCommandBuffer commandBuffer, std::uint32_t commandBufferCount, const VkCommandBuffer *pCommandBuffers) const
206 { vkCmdExecuteCommands(handle_cast<::VkCommandBuffer>(commandBuffer), commandBufferCount, handle_cast<const ::VkCommandBuffer *>(pCommandBuffers)); }
208 // Chapter 7: Synchronization and Cache Control
209 Result CreateFence(const VkFenceCreateInfo &rCreateInfo, VkFence &rFence) const
210 { return { vkCreateFence(device, &rCreateInfo, 0, handle_cast<::VkFence *>(&rFence)), "vkCreateFence" }; }
212 void DestroyFence(VkFence fence) const
213 { vkDestroyFence(device, handle_cast<::VkFence>(fence), 0); }
215 Result GetFenceStatus(VkFence fence) const
216 { return { vkGetFenceStatus(device, handle_cast<::VkFence>(fence)), "vkGetFenceStatus" }; }
218 Result ResetFences(std::uint32_t fenceCount, const VkFence *pFences) const
219 { return { vkResetFences(device, fenceCount, handle_cast<const ::VkFence *>(pFences)), "vkResetFences" }; }
221 Result WaitForFences(std::uint32_t fenceCount, const VkFence *pFences, VkBool32 waitAll, std::uint64_t timeout) const
222 { return { vkWaitForFences(device, fenceCount, handle_cast<const ::VkFence *>(pFences), waitAll, timeout), "vkWaitForFences" }; }
224 Result CreateSemaphore(const VkSemaphoreCreateInfo &rCreateInfo, VkSemaphore &rSemaphore) const
225 { return { vkCreateSemaphore(device, &rCreateInfo, 0, handle_cast<::VkSemaphore *>(&rSemaphore)), "vkCreateSemaphore" }; }
227 void DestroySemaphore(VkSemaphore semaphore) const
228 { vkDestroySemaphore(device, handle_cast<::VkSemaphore>(semaphore), 0); }
230 void CmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers) const
231 { vkCmdPipelineBarrier(handle_cast<::VkCommandBuffer>(commandBuffer), srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers); }
233 Result QueueWaitIdle() const
234 { return { vkQueueWaitIdle(graphicsQueue), "vkQueueWaitIdle" }; }
236 // Chapter 8: Render Pass
237 Result CreateRenderPass(const VkRenderPassCreateInfo &rCreateInfo, VkRenderPass &rRenderPass) const
238 { return { vkCreateRenderPass(device, &rCreateInfo, 0, handle_cast<::VkRenderPass *>(&rRenderPass)), "vkCreateRenderPass" }; }
240 void DestroyRenderPass(VkRenderPass renderPass) const
241 { vkDestroyRenderPass(device, handle_cast<::VkRenderPass>(renderPass), 0); }
243 Result CreateFramebuffer(const VkFramebufferCreateInfo &rCreateInfo, VkFramebuffer &rFramebuffer) const
244 { return { vkCreateFramebuffer(device, &rCreateInfo, 0, handle_cast<::VkFramebuffer *>(&rFramebuffer)), "vkCreateFramebuffer" }; }
246 void DestroyFramebuffer(VkFramebuffer framebuffer) const
247 { vkDestroyFramebuffer(device, handle_cast<::VkFramebuffer>(framebuffer), 0); }
249 void CmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo &rRenderPassBegin, VkSubpassContents contents) const
250 { vkCmdBeginRenderPass(handle_cast<::VkCommandBuffer>(commandBuffer), &rRenderPassBegin, contents); }
252 void CmdEndRenderPass(VkCommandBuffer commandBuffer) const
253 { vkCmdEndRenderPass(handle_cast<::VkCommandBuffer>(commandBuffer)); }
255 // Chapter 9: Shaders
256 Result CreateShaderModule(const VkShaderModuleCreateInfo &rCreateInfo, VkShaderModule &rShaderModule) const
257 { return { vkCreateShaderModule(device, &rCreateInfo, 0, handle_cast<::VkShaderModule *>(&rShaderModule)), "vkCreateShaderModule" }; }
259 void DestroyShaderModule(VkShaderModule shaderModule) const
260 { vkDestroyShaderModule(device, handle_cast<::VkShaderModule>(shaderModule), 0); }
262 // Chapter 10: Pipelines
263 Result CreateGraphicsPipelines(VkPipelineCache pipelineCache, std::uint32_t createInfoCount, const VkGraphicsPipelineCreateInfo *pCreateInfos, VkPipeline *pPipelines) const
264 { return { vkCreateGraphicsPipelines(device, handle_cast<::VkPipelineCache>(pipelineCache), createInfoCount, pCreateInfos, 0, handle_cast<::VkPipeline *>(pPipelines)), "vkCreateGraphicsPipelines" }; }
266 void DestroyPipeline(VkPipeline pipeline) const
267 { vkDestroyPipeline(device, handle_cast<::VkPipeline>(pipeline), 0); }
269 void CmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline) const
270 { vkCmdBindPipeline(handle_cast<::VkCommandBuffer>(commandBuffer), pipelineBindPoint, handle_cast<::VkPipeline>(pipeline)); }
272 // Chapter 11: Memory Allocation
273 void GetPhysicalDeviceMemoryProperties(VkPhysicalDeviceMemoryProperties &rMemoryProperties) const
274 { vkGetPhysicalDeviceMemoryProperties(physicalDevice, &rMemoryProperties); }
276 Result AllocateMemory(const VkMemoryAllocateInfo &rAllocateInfo, VkDeviceMemory &rMemory) const
277 { return { vkAllocateMemory(device, &rAllocateInfo, 0, handle_cast<::VkDeviceMemory *>(&rMemory)), "vkAllocateMemory" }; }
279 Result MapMemory(VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags, void **ppData) const
280 { return { vkMapMemory(device, handle_cast<::VkDeviceMemory>(memory), offset, size, flags, ppData), "vkMapMemory" }; }
282 void UnmapMemory(VkDeviceMemory memory) const
283 { vkUnmapMemory(device, handle_cast<::VkDeviceMemory>(memory)); }
285 void FreeMemory(VkDeviceMemory memory) const
286 { vkFreeMemory(device, handle_cast<::VkDeviceMemory>(memory), 0); }
288 // Chapter 12: Resource Creation
289 Result CreateBuffer(const VkBufferCreateInfo &rCreateInfo, VkBuffer &rBuffer) const
290 { return { vkCreateBuffer(device, &rCreateInfo, 0, handle_cast<::VkBuffer *>(&rBuffer)), "vkCreateBuffer" }; }
292 void DestroyBuffer(VkBuffer image) const
293 { vkDestroyBuffer(device, handle_cast<::VkBuffer>(image), 0); }
295 Result CreateImage(const VkImageCreateInfo &rCreateInfo, VkImage &rImage) const
296 { return { vkCreateImage(device, &rCreateInfo, 0, handle_cast<::VkImage *>(&rImage)), "vkCreateImage" }; }
298 void DestroyImage(VkImage image) const
299 { vkDestroyImage(device, handle_cast<::VkImage>(image), 0); }
301 Result CreateImageView(const VkImageViewCreateInfo &rCreateInfo, VkImageView &rView) const
302 { return { vkCreateImageView(device, &rCreateInfo, 0, handle_cast<::VkImageView *>(&rView)), "vkCreateImageView" }; }
304 void DestroyImageView(VkImageView imageView) const
305 { vkDestroyImageView(device, handle_cast<::VkImageView>(imageView), 0); }
307 void GetBufferMemoryRequirements(VkBuffer image, VkMemoryRequirements &rMemoryRequirements) const
308 { vkGetBufferMemoryRequirements(device, handle_cast<::VkBuffer>(image), &rMemoryRequirements); }
310 void GetImageMemoryRequirements(VkImage image, VkMemoryRequirements &rMemoryRequirements) const
311 { vkGetImageMemoryRequirements(device, handle_cast<::VkImage>(image), &rMemoryRequirements); }
313 Result BindBufferMemory(VkBuffer image, VkDeviceMemory memory, VkDeviceSize memoryOffset) const
314 { return { vkBindBufferMemory(device, handle_cast<::VkBuffer>(image), handle_cast<::VkDeviceMemory>(memory), memoryOffset), "vkBindBufferMemory" }; }
316 Result BindImageMemory(VkImage image, VkDeviceMemory memory, VkDeviceSize memoryOffset) const
317 { return { vkBindImageMemory(device, handle_cast<::VkImage>(image), handle_cast<::VkDeviceMemory>(memory), memoryOffset), "vkBindImageMemory" }; }
319 // Chapter 13: Samplers
320 Result CreateSampler(const VkSamplerCreateInfo &rCreateInfo, VkSampler &rSampler) const
321 { return { vkCreateSampler(device, &rCreateInfo, 0, handle_cast<::VkSampler *>(&rSampler)), "vkCreateSampler" }; }
323 void DestroySampler(VkSampler sampler) const
324 { vkDestroySampler(device, handle_cast<::VkSampler>(sampler), 0); }
326 // Chapter 14: Resource Descriptors
327 Result CreateDescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo &rCreateInfo, VkDescriptorSetLayout &rSetLayout) const
328 { return { vkCreateDescriptorSetLayout(device, &rCreateInfo, 0, handle_cast<::VkDescriptorSetLayout *>(&rSetLayout)), "vkCreateDescriptorSetLayout" }; }
330 void DestroyDescriptorSetLayout(VkDescriptorSetLayout descriptorSetLayout) const
331 { vkDestroyDescriptorSetLayout(device, handle_cast<::VkDescriptorSetLayout>(descriptorSetLayout), 0); }
333 Result CreatePipelineLayout(const VkPipelineLayoutCreateInfo &rCreateInfo, VkPipelineLayout &rPipelineLayout) const
334 { return { vkCreatePipelineLayout(device, &rCreateInfo, 0, handle_cast<::VkPipelineLayout *>(&rPipelineLayout)), "vkCreatePipelineLayout" }; }
336 void DestroyPipelineLayout(VkPipelineLayout pipelineLayout) const
337 { vkDestroyPipelineLayout(device, handle_cast<::VkPipelineLayout>(pipelineLayout), 0); }
339 Result CreateDescriptorPool(const VkDescriptorPoolCreateInfo &rCreateInfo, VkDescriptorPool &rDescriptorPool) const
340 { return { vkCreateDescriptorPool(device, &rCreateInfo, 0, handle_cast<::VkDescriptorPool *>(&rDescriptorPool)), "vkCreateDescriptorPool" }; }
342 void DestroyDescriptorPool(VkDescriptorPool descriptorPool) const
343 { vkDestroyDescriptorPool(device, handle_cast<::VkDescriptorPool>(descriptorPool), 0); }
345 Result AllocateDescriptorSets(const VkDescriptorSetAllocateInfo &rAllocateInfo, VkDescriptorSet *pDescriptorSets) const
346 { return { vkAllocateDescriptorSets(device, &rAllocateInfo, handle_cast<::VkDescriptorSet *>(pDescriptorSets)), "vkAllocateDescriptorSets" }; }
348 void UpdateDescriptorSets(std::uint32_t descriptorWriteCount, const VkWriteDescriptorSet *pDescriptorWrites, std::uint32_t descriptorCopyCount, const VkCopyDescriptorSet *pDescriptorCopies) const
349 { vkUpdateDescriptorSets(device, descriptorWriteCount, pDescriptorWrites, descriptorCopyCount, pDescriptorCopies); }
351 void CmdBindDescriptorSets(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout layout, std::uint32_t firstSet, std::uint32_t descriptorSetCount, const VkDescriptorSet *pDescriptorSets, std::uint32_t dynamicOffsetCount, const std::uint32_t *pDynamicOffsets) const
352 { vkCmdBindDescriptorSets(handle_cast<::VkCommandBuffer>(commandBuffer), pipelineBindPoint, handle_cast<::VkPipelineLayout>(layout), firstSet, descriptorSetCount, handle_cast<const ::VkDescriptorSet *>(pDescriptorSets), dynamicOffsetCount, pDynamicOffsets); }
354 void CmdPushConstants(VkCommandBuffer commandBuffer, VkPipelineLayout layout, VkShaderStageFlags stageFlags, std::uint32_t offset, std::uint32_t size, const void *pValues) const
355 { vkCmdPushConstants(handle_cast<::VkCommandBuffer>(commandBuffer), handle_cast<::VkPipelineLayout>(layout), stageFlags, offset, size, pValues); }
357 // Chapter 19: Copy Commands
358 void CmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer, std::uint32_t regionCount, const VkBufferCopy *pRegions) const
359 { vkCmdCopyBuffer(handle_cast<::VkCommandBuffer>(commandBuffer), handle_cast<::VkBuffer>(srcBuffer), handle_cast<::VkBuffer>(dstBuffer), regionCount, pRegions); }
361 void CmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage, VkImageLayout dstImageLayout, std::uint32_t regionCount, const VkBufferImageCopy *pRegions) const
362 { vkCmdCopyBufferToImage(handle_cast<::VkCommandBuffer>(commandBuffer), handle_cast<::VkBuffer>(srcBuffer), handle_cast<::VkImage>(dstImage), dstImageLayout, regionCount, pRegions); }
364 void CmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, const VkImageBlit *pRegions, VkFilter filter) const
365 { vkCmdBlitImage(handle_cast<::VkCommandBuffer>(commandBuffer), handle_cast<::VkImage>(srcImage), srcImageLayout, handle_cast<::VkImage>(dstImage), dstImageLayout, regionCount, pRegions, filter); }
367 // Chapter 20: Drawing Commands
368 void CmdBindIndexBuffer(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, VkIndexType indexType) const
369 { vkCmdBindIndexBuffer(handle_cast<::VkCommandBuffer>(commandBuffer), handle_cast<::VkBuffer>(buffer), offset, indexType); }
371 void CmdDrawIndexed(VkCommandBuffer commandBuffer, std::uint32_t indexCount, std::uint32_t instanceCount, std::uint32_t firstIndex, std::int32_t vertexOffset, std::uint32_t firstInstance) const
372 { vkCmdDrawIndexed(handle_cast<::VkCommandBuffer>(commandBuffer), indexCount, instanceCount, firstIndex, vertexOffset, firstInstance); }
374 // Chapter 21: Fixed-Function Vertex Processing
375 void CmdBindVertexBuffers(VkCommandBuffer commandBuffer, std::uint32_t firstBinding, std::uint32_t bindingCount, const VkBuffer *pBuffers, const VkDeviceSize *pOffsets) const
376 { vkCmdBindVertexBuffers(handle_cast<::VkCommandBuffer>(commandBuffer), firstBinding, bindingCount, handle_cast<const ::VkBuffer *>(pBuffers), pOffsets); }
378 // Chapter 24: Fixed-Function Vertex Post-Processing
379 void CmdSetViewport(VkCommandBuffer commandBuffer, std::uint32_t firstViewport, std::uint32_t viewportCount, const VkViewport *pViewports) const
380 { vkCmdSetViewport(handle_cast<::VkCommandBuffer>(commandBuffer), firstViewport, viewportCount, pViewports); }
382 // Chapter 26: Fragment Operations
383 void CmdSetScissor(VkCommandBuffer commandBuffer, std::uint32_t firstScissor, std::uint32_t scissorCount, const VkRect2D *pScissors) const
384 { vkCmdSetScissor(handle_cast<::VkCommandBuffer>(commandBuffer), firstScissor, scissorCount, pScissors); }
386 // Chapter 30: Window System Integration (WSI)
387 Result GetPhysicalDeviceSurfaceCapabilities(VkSurface surface, VkSurfaceCapabilitiesKHR &rSurfaceCapabilities) const
388 { return { vkGetPhysicalDeviceSurfaceCapabilities(physicalDevice, handle_cast<::VkSurfaceKHR>(surface), &rSurfaceCapabilities), "vkGetPhysicalDeviceSurfaceCapabilities" }; }
390 Result GetPhysicalDeviceSurfaceFormats(VkSurface surface, std::uint32_t &rSurfaceFormatCount, VkSurfaceFormatKHR *pSurfaceFormats) const
391 { return { vkGetPhysicalDeviceSurfaceFormats(physicalDevice, handle_cast<::VkSurfaceKHR>(surface), &rSurfaceFormatCount, pSurfaceFormats), "vkGetPhysicalDeviceSurfaceFormats" }; }
393 Result GetPhysicalDeviceSurfacePresentModes(VkSurface surface, std::uint32_t &rPresentModeCount, VkPresentModeKHR *pPresentModes) const
394 { return { vkGetPhysicalDeviceSurfacePresentModes(physicalDevice, handle_cast<::VkSurfaceKHR>(surface), &rPresentModeCount, pPresentModes), "vkGetPhysicalDeviceSurfacePresentModes" }; }
396 Result CreateSwapchain(const VkSwapchainCreateInfoKHR &rCreateInfo, VkSwapchain &rSwapchain) const
397 { return { vkCreateSwapchain(device, &rCreateInfo, 0, handle_cast<::VkSwapchainKHR *>(&rSwapchain)), "vkCreateSwapchain" }; }
399 void DestroySwapchain(VkSwapchain swapchain) const
400 { vkDestroySwapchain(device, handle_cast<::VkSwapchainKHR>(swapchain), 0); }
402 void GetSwapchainImages(VkSwapchain swapchain, std::uint32_t &rSwapchainImageCount, VkImage *pSwapchainImages) const
403 { vkGetSwapchainImages(device, handle_cast<::VkSwapchainKHR>(swapchain), &rSwapchainImageCount, handle_cast<::VkImage *>(pSwapchainImages)); }
405 Result AcquireNextImage(VkSwapchain swapchain, std::uint64_t timeout, VkSemaphore semaphore, VkFence fence, std::uint32_t &rImageIndex) const
406 { return { vkAcquireNextImageKHR(device, handle_cast<::VkSwapchainKHR>(swapchain), timeout, handle_cast<::VkSemaphore>(semaphore), handle_cast<::VkFence>(fence), &rImageIndex), "vkAcquireNextImageKHR" }; }
408 Result QueuePresent(const VkPresentInfoKHR &rPresentInfo) const
409 { return { vkQueuePresent(graphicsQueue, &rPresentInfo), "vkQueuePresent" }; }
411 // Chapter 39: Formats
412 void GetPhysicalDeviceFormatProperties(VkFormat format, VkFormatProperties &rFormatProperties) const
413 { vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &rFormatProperties); }
415 // Chapter 45: Debugging
416 Result SetDebugUtilsObjectName(const VkDebugUtilsObjectNameInfoEXT &rNameInfo) const
417 { return { vkSetDebugUtilsObjectName(device, &rNameInfo), "vkSetDebugUtilsObjectName" }; }
420 class VulkanCommandRecorder
423 const VulkanFunctions &vk;
424 VkCommandBuffer commandBuffer;
427 VulkanCommandRecorder(const VulkanFunctions &v, VkCommandBuffer b): vk(v), commandBuffer(b) { }
429 void ExecuteCommands(std::uint32_t commandBufferCount, const VkCommandBuffer *pCommandBuffers) const
430 { vk.CmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers); }
432 void PipelineBarrier(VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers) const
433 { vk.CmdPipelineBarrier(commandBuffer, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers); }
435 void BeginRenderPass(const VkRenderPassBeginInfo &rRenderPassBegin, VkSubpassContents contents) const
436 { vk.CmdBeginRenderPass(commandBuffer, rRenderPassBegin, contents); }
438 void EndRenderPass() const
439 { vk.CmdEndRenderPass(commandBuffer); }
441 void BindPipeline(VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline) const
442 { vk.CmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline); }
444 void BindDescriptorSets(VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout layout, std::uint32_t firstSet, std::uint32_t descriptorSetCount, const VkDescriptorSet *pDescriptorSets, std::uint32_t dynamicOffsetCount, const std::uint32_t *pDynamicOffsets) const
445 { vk.CmdBindDescriptorSets(commandBuffer, pipelineBindPoint, layout, firstSet, descriptorSetCount, pDescriptorSets, dynamicOffsetCount, pDynamicOffsets); }
447 void PushConstants(VkPipelineLayout layout, VkShaderStageFlags stageFlags, std::uint32_t offset, std::uint32_t size, const void *pValues) const
448 { vk.CmdPushConstants(commandBuffer, layout, stageFlags, offset, size, pValues); }
450 void CopyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, std::uint32_t regionCount, const VkBufferCopy *pRegions) const
451 { vk.CmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, regionCount, pRegions); }
453 void CopyBufferToImage(VkBuffer srcBuffer, VkImage dstImage, VkImageLayout dstImageLayout, std::uint32_t regionCount, const VkBufferImageCopy *pRegions) const
454 { vk.CmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions); }
456 void BlitImage(VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, const VkImageBlit *pRegions, VkFilter filter) const
457 { vk.CmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter); }
459 void BindIndexBuffer(VkBuffer buffer, VkDeviceSize offset, VkIndexType indexType) const
460 { vk.CmdBindIndexBuffer(commandBuffer, buffer, offset, indexType); }
462 void DrawIndexed(std::uint32_t indexCount, std::uint32_t instanceCount, std::uint32_t firstIndex, std::int32_t vertexOffset, std::uint32_t firstInstance) const
463 { vk.CmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance); }
465 void BindVertexBuffers(std::uint32_t firstBinding, std::uint32_t bindingCount, const VkBuffer *pBuffers, const VkDeviceSize *pOffsets) const
466 { vk.CmdBindVertexBuffers(commandBuffer, firstBinding, bindingCount, pBuffers, pOffsets); }
468 void SetViewport(std::uint32_t firstViewport, std::uint32_t viewportCount, const VkViewport *pViewports) const
469 { vk.CmdSetViewport(commandBuffer, firstViewport, viewportCount, pViewports); }
471 void SetScissor(std::uint32_t firstScissor, std::uint32_t scissorCount, const VkRect2D *pScissors) const
472 { vk.CmdSetScissor(commandBuffer, firstScissor, scissorCount, pScissors); }