Overte C++ Documentation
VulkanTools.h
1 /*
2  * Assorted Vulkan helper functions
3  *
4  * Copyright (C) 2016-2023 by Sascha Willems - www.saschawillems.de
5  *
6  * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
7  */
8 
9 #pragma once
10 
11 #include "vulkan/vulkan.h"
12 #include "VulkanInitializers.hpp"
13 
14 #include <math.h>
15 #include <stdlib.h>
16 #include <string>
17 #include <cstring>
18 #include <fstream>
19 #include <assert.h>
20 #include <stdio.h>
21 #include <vector>
22 #include <iostream>
23 #include <stdexcept>
24 #include <fstream>
25 #include <algorithm>
26 #if defined(_WIN32)
27 #include <windows.h>
28 #include <fcntl.h>
29 #include <io.h>
30 #elif defined(__ANDROID__)
31 #include "VulkanAndroid.h"
32 #include <android/asset_manager.h>
33 #endif
34 
35 // Custom define for better code readability
36 #define VK_FLAGS_NONE 0
37 // Default fence timeout in nanoseconds
38 #define DEFAULT_FENCE_TIMEOUT 100000000000
39 
40 // Macro to check and display Vulkan return results
41 #if defined(__ANDROID__)
42 #define VK_CHECK_RESULT(f) \
43 { \
44  VkResult res = (f); \
45  if (res != VK_SUCCESS) \
46  { \
47  LOGE("Fatal : VkResult is \" %s \" in %s at line %d", vks::tools::errorString(res).c_str(), __FILE__, __LINE__); \
48  assert(res == VK_SUCCESS); \
49  } \
50 }
51 #else
52 #define VK_CHECK_RESULT(f) \
53 { \
54  VkResult res = (f); \
55  if (res != VK_SUCCESS) \
56  { \
57  std::cout << "Fatal : VkResult is \"" << vks::tools::errorString(res) << "\" in " << __FILE__ << " at line " << __LINE__ << "\n" << std::flush; \
58  assert(res == VK_SUCCESS); \
59  } \
60 }
61 #endif
62 
63 const std::string getAssetPath();
64 const std::string getShaderBasePath();
65 
66 namespace vks
67 {
68  namespace tools
69  {
71  extern bool errorModeSilent;
72 
74  std::string errorString(VkResult errorCode);
75 
77  std::string physicalDeviceTypeString(VkPhysicalDeviceType type);
78 
79  // Selected a suitable supported depth format starting with 32 bit down to 16 bit
80  // Returns false if none of the depth formats in the list is supported by the device
81  VkBool32 getSupportedDepthFormat(VkPhysicalDevice physicalDevice, VkFormat *depthFormat);
82  // Same as getSupportedDepthFormat but will only select formats that also have stencil
83  VkBool32 getSupportedDepthStencilFormat(VkPhysicalDevice physicalDevice, VkFormat* depthStencilFormat);
84 
85  // Returns tru a given format support LINEAR filtering
86  VkBool32 formatIsFilterable(VkPhysicalDevice physicalDevice, VkFormat format, VkImageTiling tiling);
87  // Returns true if a given format has a stencil part
88  VkBool32 formatHasStencil(VkFormat format);
89 
90  // Put an image memory barrier for setting an image layout on the sub resource into the given command buffer
91  void setImageLayout(
92  VkCommandBuffer cmdbuffer,
93  VkImage image,
94  VkImageLayout oldImageLayout,
95  VkImageLayout newImageLayout,
96  VkImageSubresourceRange subresourceRange,
97  uint32_t srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
98  uint32_t dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
99  VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
100  VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
101  // Uses a fixed sub resource layout with first mip level and layer
102  void setImageLayout(
103  VkCommandBuffer cmdbuffer,
104  VkImage image,
105  VkImageAspectFlags aspectMask,
106  VkImageLayout oldImageLayout,
107  VkImageLayout newImageLayout,
108  VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
109  VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
110 
112  void insertImageMemoryBarrier(
113  VkCommandBuffer cmdbuffer,
114  VkImage image,
115  VkAccessFlags srcAccessMask,
116  VkAccessFlags dstAccessMask,
117  VkImageLayout oldImageLayout,
118  VkImageLayout newImageLayout,
119  VkPipelineStageFlags srcStageMask,
120  VkPipelineStageFlags dstStageMask,
121  VkImageSubresourceRange subresourceRange);
122 
123  // Display error message and exit on fatal error
124  void exitFatal(const std::string& message, int32_t exitCode);
125  void exitFatal(const std::string& message, VkResult resultCode);
126 
127  // Load a SPIR-V shader (binary)
128 #if defined(__ANDROID__)
129  VkShaderModule loadShader(AAssetManager* assetManager, const char *fileName, VkDevice device);
130 #else
131  VkShaderModule loadShader(const char *fileName, VkDevice device);
132 #endif
133 
135  bool fileExists(const std::string &filename);
136 
137  uint32_t alignedSize(uint32_t value, uint32_t alignment);
138  VkDeviceSize alignedVkSize(VkDeviceSize value, VkDeviceSize alignment);
139  }
140 }