2016-02-16 15:07:25 +01:00
|
|
|
/*
|
2017-01-06 22:48:37 +01:00
|
|
|
* Assorted Vulkan helper functions
|
2016-02-16 15:07:25 +01:00
|
|
|
*
|
2016-03-06 11:25:33 +01:00
|
|
|
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
|
2016-02-16 15:07:25 +01:00
|
|
|
*
|
|
|
|
|
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "vulkan/vulkan.h"
|
2017-01-06 22:48:37 +01:00
|
|
|
#include "VulkanInitializers.hpp"
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <iostream>
|
2016-03-17 22:07:42 +01:00
|
|
|
#include <stdexcept>
|
2016-03-20 15:46:06 +01:00
|
|
|
#if defined(_WIN32)
|
2016-02-16 15:07:25 +01:00
|
|
|
#include <windows.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <io.h>
|
2016-03-20 15:46:06 +01:00
|
|
|
#elif defined(__ANDROID__)
|
|
|
|
|
#include "vulkanandroid.h"
|
|
|
|
|
#include <android/asset_manager.h>
|
2016-02-16 15:07:25 +01:00
|
|
|
#endif
|
|
|
|
|
|
2016-03-06 11:25:33 +01:00
|
|
|
// Custom define for better code readability
|
2016-02-16 15:07:25 +01:00
|
|
|
#define VK_FLAGS_NONE 0
|
2016-03-19 16:30:39 +01:00
|
|
|
// Default fence timeout in nanoseconds
|
|
|
|
|
#define DEFAULT_FENCE_TIMEOUT 100000000000
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-05-08 11:18:39 +02:00
|
|
|
// Macro to check and display Vulkan return results
|
2017-01-23 13:44:31 +01:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
|
#define VK_CHECK_RESULT(f) \
|
|
|
|
|
{ \
|
|
|
|
|
VkResult res = (f); \
|
|
|
|
|
if (res != VK_SUCCESS) \
|
|
|
|
|
{ \
|
2017-02-12 13:10:05 +01:00
|
|
|
LOGE("Fatal : VkResult is \" %s \" in %s at line %d", vks::tools::errorString(res).c_str(), __FILE__, __LINE__); \
|
2017-01-23 13:44:31 +01:00
|
|
|
assert(res == VK_SUCCESS); \
|
|
|
|
|
} \
|
|
|
|
|
}
|
|
|
|
|
#else
|
2016-05-08 11:18:39 +02:00
|
|
|
#define VK_CHECK_RESULT(f) \
|
|
|
|
|
{ \
|
|
|
|
|
VkResult res = (f); \
|
|
|
|
|
if (res != VK_SUCCESS) \
|
|
|
|
|
{ \
|
2017-02-12 13:10:05 +01:00
|
|
|
std::cout << "Fatal : VkResult is \"" << vks::tools::errorString(res) << "\" in " << __FILE__ << " at line " << __LINE__ << std::endl; \
|
2016-05-08 11:18:39 +02:00
|
|
|
assert(res == VK_SUCCESS); \
|
|
|
|
|
} \
|
2017-01-23 13:44:31 +01:00
|
|
|
}
|
|
|
|
|
#endif
|
2016-05-08 11:18:39 +02:00
|
|
|
|
2017-02-12 13:10:05 +01:00
|
|
|
namespace vks
|
2016-02-16 15:07:25 +01:00
|
|
|
{
|
2017-02-12 13:10:05 +01:00
|
|
|
namespace tools
|
|
|
|
|
{
|
|
|
|
|
/** @brief Returns an error code as a string */
|
|
|
|
|
std::string errorString(VkResult errorCode);
|
2016-03-17 22:07:42 +01:00
|
|
|
|
2017-02-12 13:10:05 +01:00
|
|
|
/** @brief Returns the device type as a string */
|
|
|
|
|
std::string physicalDeviceTypeString(VkPhysicalDeviceType type);
|
2017-02-04 13:35:40 +01:00
|
|
|
|
2017-02-12 13:10:05 +01:00
|
|
|
// Selected a suitable supported depth format starting with 32 bit down to 16 bit
|
|
|
|
|
// Returns false if none of the depth formats in the list is supported by the device
|
|
|
|
|
VkBool32 getSupportedDepthFormat(VkPhysicalDevice physicalDevice, VkFormat *depthFormat);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2017-02-12 13:10:05 +01:00
|
|
|
// Put an image memory barrier for setting an image layout on the sub resource into the given command buffer
|
|
|
|
|
void setImageLayout(
|
|
|
|
|
VkCommandBuffer cmdbuffer,
|
|
|
|
|
VkImage image,
|
|
|
|
|
VkImageAspectFlags aspectMask,
|
|
|
|
|
VkImageLayout oldImageLayout,
|
|
|
|
|
VkImageLayout newImageLayout,
|
|
|
|
|
VkImageSubresourceRange subresourceRange,
|
|
|
|
|
VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
|
|
|
|
|
VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
|
|
|
|
|
// Uses a fixed sub resource layout with first mip level and layer
|
|
|
|
|
void setImageLayout(
|
|
|
|
|
VkCommandBuffer cmdbuffer,
|
|
|
|
|
VkImage image,
|
|
|
|
|
VkImageAspectFlags aspectMask,
|
|
|
|
|
VkImageLayout oldImageLayout,
|
|
|
|
|
VkImageLayout newImageLayout,
|
|
|
|
|
VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
|
|
|
|
|
VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2017-02-12 13:10:05 +01:00
|
|
|
// Display error message and exit on fatal error
|
|
|
|
|
void exitFatal(std::string message, std::string caption);
|
2016-03-20 15:46:06 +01:00
|
|
|
|
2017-02-12 13:10:05 +01:00
|
|
|
// Load a SPIR-V shader (binary)
|
2016-03-20 15:46:06 +01:00
|
|
|
#if defined(__ANDROID__)
|
2017-02-12 13:10:05 +01:00
|
|
|
VkShaderModule loadShader(AAssetManager* assetManager, const char *fileName, VkDevice device, VkShaderStageFlagBits stage);
|
2016-03-20 15:46:06 +01:00
|
|
|
#else
|
2017-02-12 13:10:05 +01:00
|
|
|
VkShaderModule loadShader(const char *fileName, VkDevice device, VkShaderStageFlagBits stage);
|
2016-03-20 15:46:06 +01:00
|
|
|
#endif
|
|
|
|
|
|
2017-02-12 13:10:05 +01:00
|
|
|
// Load a GLSL shader (text)
|
|
|
|
|
// Note: GLSL support requires vendor-specific extensions to be enabled and is not a core-feature of Vulkan
|
|
|
|
|
VkShaderModule loadShaderGLSL(const char *fileName, VkDevice device, VkShaderStageFlagBits stage);
|
|
|
|
|
}
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|