PeriDyno 1.0.0
Loading...
Searching...
No Matches
VulkanTools.h
Go to the documentation of this file.
1/*
2* Assorted Vulkan helper functions
3*
4* Copyright (C) 2016 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"
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 <map>
23#include <iostream>
24#include <stdexcept>
25#include <fstream>
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#include "Platform.h"
34#elif defined(__GNUC__) || defined(__clang__)
35#include "Platform.h"
36#endif
37
38// Custom define for better code readability
39#define VK_FLAGS_NONE 0
40// Default fence timeout in nanoseconds
41#define DEFAULT_FENCE_TIMEOUT 100000000000
42
43// Macro to check and display Vulkan return results
44#if defined(__ANDROID__)
45#define VK_CHECK_RESULT(f) \
46{ \
47 VkResult res = (f); \
48 if (res != VK_SUCCESS) \
49 { \
50 LOGE("Fatal : VkResult is \" %s \" in %s at line %d", vks::tools::errorString(res).c_str(), __FILE__, __LINE__); \
51 assert(res == VK_SUCCESS); \
52 } \
53}
54#else
55#define VK_CHECK_RESULT(f) \
56{ \
57 VkResult res = (f); \
58 if (res != VK_SUCCESS) \
59 { \
60 std::cout << "Fatal : VkResult is \"" << vks::tools::errorString(res) << "\" in " << __FILE__ << " at line " << __LINE__ << "\n"; \
61 assert(res == VK_SUCCESS); \
62 } \
63}
64#endif
65
66template<typename T>
67std::string getDynamicSpvFile(const std::string &fileName)
68{
69 std::string typeName;
70 if (typeid(T).name() == typeid(int).name())
71 typeName = "int";
72 else if (typeid(T).name() == typeid(uint32_t).name())
73 typeName = "uint";
74 else if (typeid(T).name() == typeid(float).name())
75 typeName = "float";
76
77 const static std::string suffix = ".comp.spv";
78 std::string outFileName = fileName;
79 unsigned int suffixPos = outFileName.rfind(suffix);
80 if (suffixPos != (outFileName.length() - suffix.length()))
81 {
82 // suffix not ".comp.spv", return origin filename
83 return fileName;
84 }
85
86 // test.comp.spv --> test.int.comp.spv
87 outFileName.insert(suffixPos, "." + typeName);
88 return getAssetPath() + outFileName;
89}
90
91namespace vks
92{
93 namespace tools
94 {
96 extern bool errorModeSilent;
97
99 std::string errorString(VkResult errorCode);
100
102 std::string physicalDeviceTypeString(VkPhysicalDeviceType type);
103
104 // Selected a suitable supported depth format starting with 32 bit down to 16 bit
105 // Returns false if none of the depth formats in the list is supported by the device
106 VkBool32 getSupportedDepthFormat(VkPhysicalDevice physicalDevice, VkFormat *depthFormat);
107
108 // Returns if a given format support LINEAR filtering
109 VkBool32 formatIsFilterable(VkPhysicalDevice physicalDevice, VkFormat format, VkImageTiling tiling);
110
111 // Put an image memory barrier for setting an image layout on the sub resource into the given command buffer
112 void setImageLayout(
113 VkCommandBuffer cmdbuffer,
114 VkImage image,
115 VkImageLayout oldImageLayout,
116 VkImageLayout newImageLayout,
117 VkImageSubresourceRange subresourceRange,
118 VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
119 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
120 // Uses a fixed sub resource layout with first mip level and layer
121 void setImageLayout(
122 VkCommandBuffer cmdbuffer,
123 VkImage image,
124 VkImageAspectFlags aspectMask,
125 VkImageLayout oldImageLayout,
126 VkImageLayout newImageLayout,
127 VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
128 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
129
132 VkCommandBuffer cmdbuffer,
133 VkImage image,
134 VkAccessFlags srcAccessMask,
135 VkAccessFlags dstAccessMask,
136 VkImageLayout oldImageLayout,
137 VkImageLayout newImageLayout,
138 VkPipelineStageFlags srcStageMask,
139 VkPipelineStageFlags dstStageMask,
140 VkImageSubresourceRange subresourceRange);
141
142 // Display error message and exit on fatal error
143 void exitFatal(const std::string& message, int32_t exitCode);
144 void exitFatal(const std::string& message, VkResult resultCode);
145
146 VkShaderModule loadShaderModule(const std::string fileName, VkDevice device);
147 VkShaderModule loadShaderModule(const std::string& fileName, const std::map<std::string, std::string>& macros, const std::string& MD5Encode, VkDevice device);
148
149 // Load a SPIR-V shader (binary)
150#if defined(__ANDROID__)
151 VkShaderModule loadShader(AAssetManager* assetManager, const char *fileName, VkDevice device);
152 VkShaderModule loadShader(AAssetManager* assetManager, const std::string& fileName, const std::map<std::string, std::string>& macros, const std::string& MD5EnCode, VkDevice device);
153#else
154 VkShaderModule loadShader(const char *fileName, VkDevice device);
155 VkShaderModule loadShader(const std::string &fileName, const std::map<std::string, std::string> &macros, const std::string &MD5Encode, VkDevice device);
156#endif
157
159 bool fileExists(const std::string &filename);
160
161 uint32_t alignedSize(uint32_t value, uint32_t alignment);
162 }
163}
std::string getDynamicSpvFile(const std::string &fileName)
Definition VulkanTools.h:67
#define T(t)
bool fileExists(const std::string &filename)
Checks if a file exists.
void insertImageMemoryBarrier(VkCommandBuffer cmdbuffer, VkImage image, VkAccessFlags srcAccessMask, VkAccessFlags dstAccessMask, VkImageLayout oldImageLayout, VkImageLayout newImageLayout, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, VkImageSubresourceRange subresourceRange)
Insert an image memory barrier into the command buffer.
void setImageLayout(VkCommandBuffer cmdbuffer, VkImage image, VkImageLayout oldImageLayout, VkImageLayout newImageLayout, VkImageSubresourceRange subresourceRange, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask)
uint32_t alignedSize(uint32_t value, uint32_t alignment)
std::string errorString(VkResult errorCode)
Returns an error code as a string.
std::string physicalDeviceTypeString(VkPhysicalDeviceType type)
Returns the device type as a string.
VkBool32 getSupportedDepthFormat(VkPhysicalDevice physicalDevice, VkFormat *depthFormat)
VkBool32 formatIsFilterable(VkPhysicalDevice physicalDevice, VkFormat format, VkImageTiling tiling)
VkShaderModule loadShader(const char *fileName, VkDevice device)
bool errorModeSilent
Disable message boxes on fatal errors.
VkShaderModule loadShaderModule(const std::string fileName, VkDevice device)
void exitFatal(const std::string &message, int32_t exitCode)