Introduce usage of clang-format to format vulkan.hpp and the other sources.

This commit is contained in:
asuessenbach
2020-04-12 21:49:12 +02:00
parent ce9fd81bd9
commit f5e59484a6
68 changed files with 56064 additions and 35586 deletions

View File

@@ -19,19 +19,35 @@ namespace vk
{
namespace su
{
glm::mat4x4 createModelViewProjectionClipMatrix(vk::Extent2D const& extent)
glm::mat4x4 createModelViewProjectionClipMatrix( vk::Extent2D const & extent )
{
float fov = glm::radians(45.0f);
if (extent.width > extent.height)
float fov = glm::radians( 45.0f );
if ( extent.width > extent.height )
{
fov *= static_cast<float>(extent.height) / static_cast<float>(extent.width);
fov *= static_cast<float>( extent.height ) / static_cast<float>( extent.width );
}
glm::mat4x4 model = glm::mat4x4(1.0f);
glm::mat4x4 view = glm::lookAt(glm::vec3(-5.0f, 3.0f, -10.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f));
glm::mat4x4 projection = glm::perspective(fov, 1.0f, 0.1f, 100.0f);
glm::mat4x4 clip = glm::mat4x4(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 1.0f); // vulkan clip space has inverted y and half z !
glm::mat4x4 model = glm::mat4x4( 1.0f );
glm::mat4x4 view =
glm::lookAt( glm::vec3( -5.0f, 3.0f, -10.0f ), glm::vec3( 0.0f, 0.0f, 0.0f ), glm::vec3( 0.0f, -1.0f, 0.0f ) );
glm::mat4x4 projection = glm::perspective( fov, 1.0f, 0.1f, 100.0f );
glm::mat4x4 clip = glm::mat4x4( 1.0f,
0.0f,
0.0f,
0.0f,
0.0f,
-1.0f,
0.0f,
0.0f,
0.0f,
0.0f,
0.5f,
0.0f,
0.0f,
0.0f,
0.5f,
1.0f ); // vulkan clip space has inverted y and half z !
return clip * projection * view * model;
}
}
}
} // namespace su
} // namespace vk

View File

@@ -15,13 +15,14 @@
#include <vulkan/vulkan.hpp>
#define GLM_FORCE_RADIANS
#pragma warning(disable:4201) // disable warning C4201: nonstandard extension used: nameless struct/union; needed to get glm/detail/type_vec?.hpp without warnings
#pragma warning( disable : 4201 ) // disable warning C4201: nonstandard extension used: nameless struct/union; needed
// to get glm/detail/type_vec?.hpp without warnings
#include <glm/gtc/matrix_transform.hpp>
namespace vk
{
namespace su
{
glm::mat4x4 createModelViewProjectionClipMatrix(vk::Extent2D const& extent);
glm::mat4x4 createModelViewProjectionClipMatrix( vk::Extent2D const & extent );
}
}
} // namespace vk

View File

@@ -14,85 +14,88 @@
//
#include "shaders.hpp"
#include "vulkan/vulkan.hpp"
#include "StandAlone/ResourceLimits.h"
#include "SPIRV/GlslangToSpv.h"
#include "StandAlone/ResourceLimits.h"
#include "vulkan/vulkan.hpp"
namespace vk
{
namespace su
{
EShLanguage translateShaderStage(vk::ShaderStageFlagBits stage)
EShLanguage translateShaderStage( vk::ShaderStageFlagBits stage )
{
switch (stage)
switch ( stage )
{
case vk::ShaderStageFlagBits::eVertex: return EShLangVertex;
case vk::ShaderStageFlagBits::eTessellationControl: return EShLangTessControl;
case vk::ShaderStageFlagBits::eTessellationEvaluation: return EShLangTessEvaluation;
case vk::ShaderStageFlagBits::eGeometry: return EShLangGeometry;
case vk::ShaderStageFlagBits::eFragment: return EShLangFragment;
case vk::ShaderStageFlagBits::eCompute: return EShLangCompute;
case vk::ShaderStageFlagBits::eRaygenNV: return EShLangRayGenNV;
case vk::ShaderStageFlagBits::eAnyHitNV: return EShLangAnyHitNV;
case vk::ShaderStageFlagBits::eClosestHitNV: return EShLangClosestHitNV;
case vk::ShaderStageFlagBits::eMissNV: return EShLangMissNV;
case vk::ShaderStageFlagBits::eIntersectionNV: return EShLangIntersectNV;
case vk::ShaderStageFlagBits::eCallableNV: return EShLangCallableNV;
case vk::ShaderStageFlagBits::eTaskNV: return EShLangTaskNV;
case vk::ShaderStageFlagBits::eMeshNV: return EShLangMeshNV;
default:
assert(false && "Unknown shader stage");
return EShLangVertex;
case vk::ShaderStageFlagBits::eVertex: return EShLangVertex;
case vk::ShaderStageFlagBits::eTessellationControl: return EShLangTessControl;
case vk::ShaderStageFlagBits::eTessellationEvaluation: return EShLangTessEvaluation;
case vk::ShaderStageFlagBits::eGeometry: return EShLangGeometry;
case vk::ShaderStageFlagBits::eFragment: return EShLangFragment;
case vk::ShaderStageFlagBits::eCompute: return EShLangCompute;
case vk::ShaderStageFlagBits::eRaygenNV: return EShLangRayGenNV;
case vk::ShaderStageFlagBits::eAnyHitNV: return EShLangAnyHitNV;
case vk::ShaderStageFlagBits::eClosestHitNV: return EShLangClosestHitNV;
case vk::ShaderStageFlagBits::eMissNV: return EShLangMissNV;
case vk::ShaderStageFlagBits::eIntersectionNV: return EShLangIntersectNV;
case vk::ShaderStageFlagBits::eCallableNV: return EShLangCallableNV;
case vk::ShaderStageFlagBits::eTaskNV: return EShLangTaskNV;
case vk::ShaderStageFlagBits::eMeshNV: return EShLangMeshNV;
default: assert( false && "Unknown shader stage" ); return EShLangVertex;
}
}
bool GLSLtoSPV(const vk::ShaderStageFlagBits shaderType, std::string const& glslShader, std::vector<unsigned int> &spvShader)
bool GLSLtoSPV( const vk::ShaderStageFlagBits shaderType,
std::string const & glslShader,
std::vector<unsigned int> & spvShader )
{
EShLanguage stage = translateShaderStage(shaderType);
EShLanguage stage = translateShaderStage( shaderType );
const char *shaderStrings[1];
const char * shaderStrings[1];
shaderStrings[0] = glslShader.data();
glslang::TShader shader(stage);
shader.setStrings(shaderStrings, 1);
glslang::TShader shader( stage );
shader.setStrings( shaderStrings, 1 );
// Enable SPIR-V and Vulkan rules when parsing GLSL
EShMessages messages = (EShMessages)(EShMsgSpvRules | EShMsgVulkanRules);
EShMessages messages = ( EShMessages )( EShMsgSpvRules | EShMsgVulkanRules );
if (!shader.parse(&glslang::DefaultTBuiltInResource, 100, false, messages))
if ( !shader.parse( &glslang::DefaultTBuiltInResource, 100, false, messages ) )
{
puts(shader.getInfoLog());
puts(shader.getInfoDebugLog());
puts( shader.getInfoLog() );
puts( shader.getInfoDebugLog() );
return false; // something didn't work
}
glslang::TProgram program;
program.addShader(&shader);
program.addShader( &shader );
//
// Program-level processing...
//
if (!program.link(messages))
if ( !program.link( messages ) )
{
puts(shader.getInfoLog());
puts(shader.getInfoDebugLog());
fflush(stdout);
puts( shader.getInfoLog() );
puts( shader.getInfoDebugLog() );
fflush( stdout );
return false;
}
glslang::GlslangToSpv(*program.getIntermediate(stage), spvShader);
glslang::GlslangToSpv( *program.getIntermediate( stage ), spvShader );
return true;
}
vk::UniqueShaderModule createShaderModule(vk::UniqueDevice &device, vk::ShaderStageFlagBits shaderStage, std::string const& shaderText)
vk::UniqueShaderModule createShaderModule( vk::UniqueDevice & device,
vk::ShaderStageFlagBits shaderStage,
std::string const & shaderText )
{
std::vector<unsigned int> shaderSPV;
bool ok = GLSLtoSPV(shaderStage, shaderText, shaderSPV);
assert(ok);
bool ok = GLSLtoSPV( shaderStage, shaderText, shaderSPV );
assert( ok );
return device->createShaderModuleUnique(vk::ShaderModuleCreateInfo(vk::ShaderModuleCreateFlags(), shaderSPV.size() * sizeof(unsigned int), shaderSPV.data()));
return device->createShaderModuleUnique( vk::ShaderModuleCreateInfo(
vk::ShaderModuleCreateFlags(), shaderSPV.size() * sizeof( unsigned int ), shaderSPV.data() ) );
}
}
}
} // namespace su
} // namespace vk

View File

@@ -14,6 +14,7 @@
//
#include "vulkan/vulkan.hpp"
#include <string>
#include <vector>
@@ -21,12 +22,15 @@ namespace vk
{
namespace su
{
vk::UniqueShaderModule createShaderModule(vk::UniqueDevice &device, vk::ShaderStageFlagBits shaderStage, std::string const& shaderText);
bool GLSLtoSPV(const vk::ShaderStageFlagBits shaderType, std::string const& glslShader, std::vector<unsigned int> &spvShader);
}
}
vk::UniqueShaderModule createShaderModule( vk::UniqueDevice & device,
vk::ShaderStageFlagBits shaderStage,
std::string const & shaderText );
bool GLSLtoSPV( const vk::ShaderStageFlagBits shaderType,
std::string const & glslShader,
std::vector<unsigned int> & spvShader );
} // namespace su
} // namespace vk
// vertex shader with (P)osition and (C)olor in and (C)olor out
const std::string vertexShaderText_PC_C = R"(
@@ -76,7 +80,6 @@ void main()
}
)";
// fragment shader with (C)olor in and (C)olor out
const std::string fragmentShaderText_C_C = R"(
#version 400
@@ -112,4 +115,3 @@ void main()
outColor = texture(tex, inTexCoord);
}
)";

File diff suppressed because it is too large Load Diff

View File

@@ -19,7 +19,6 @@
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <iostream>
#include <map>
@@ -30,121 +29,146 @@ namespace vk
const uint64_t FenceTimeout = 100000000;
template <typename Func>
void oneTimeSubmit(vk::UniqueCommandBuffer const& commandBuffer, vk::Queue const& queue, Func const& func)
void oneTimeSubmit( vk::UniqueCommandBuffer const & commandBuffer, vk::Queue const & queue, Func const & func )
{
commandBuffer->begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlagBits::eOneTimeSubmit));
func(commandBuffer);
commandBuffer->begin( vk::CommandBufferBeginInfo( vk::CommandBufferUsageFlagBits::eOneTimeSubmit ) );
func( commandBuffer );
commandBuffer->end();
queue.submit(vk::SubmitInfo(0, nullptr, nullptr, 1, &(*commandBuffer)), nullptr);
queue.submit( vk::SubmitInfo( 0, nullptr, nullptr, 1, &( *commandBuffer ) ), nullptr );
queue.waitIdle();
}
template <typename Func>
void oneTimeSubmit(vk::UniqueDevice const& device, vk::UniqueCommandPool const& commandPool, vk::Queue const& queue, Func const& func)
void oneTimeSubmit( vk::UniqueDevice const & device,
vk::UniqueCommandPool const & commandPool,
vk::Queue const & queue,
Func const & func )
{
vk::UniqueCommandBuffer commandBuffer = std::move(device->allocateCommandBuffersUnique(vk::CommandBufferAllocateInfo(*commandPool, vk::CommandBufferLevel::ePrimary, 1)).front());
oneTimeSubmit(commandBuffer, queue, func);
vk::UniqueCommandBuffer commandBuffer =
std::move( device
->allocateCommandBuffersUnique(
vk::CommandBufferAllocateInfo( *commandPool, vk::CommandBufferLevel::ePrimary, 1 ) )
.front() );
oneTimeSubmit( commandBuffer, queue, func );
}
template <class T>
void copyToDevice(vk::UniqueDevice const& device, vk::UniqueDeviceMemory const& memory, T const* pData, size_t count, vk::DeviceSize stride = sizeof(T))
void copyToDevice( vk::UniqueDevice const & device,
vk::UniqueDeviceMemory const & memory,
T const * pData,
size_t count,
vk::DeviceSize stride = sizeof( T ) )
{
assert(sizeof(T) <= stride);
uint8_t* deviceData = static_cast<uint8_t*>(device->mapMemory(memory.get(), 0, count * stride));
if (stride == sizeof(T))
assert( sizeof( T ) <= stride );
uint8_t * deviceData = static_cast<uint8_t *>( device->mapMemory( memory.get(), 0, count * stride ) );
if ( stride == sizeof( T ) )
{
memcpy(deviceData, pData, count * sizeof(T));
memcpy( deviceData, pData, count * sizeof( T ) );
}
else
{
for (size_t i = 0; i < count; i++)
for ( size_t i = 0; i < count; i++ )
{
memcpy(deviceData, &pData[i], sizeof(T));
memcpy( deviceData, &pData[i], sizeof( T ) );
deviceData += stride;
}
}
device->unmapMemory(memory.get());
device->unmapMemory( memory.get() );
}
template <class T>
void copyToDevice(vk::UniqueDevice const& device, vk::UniqueDeviceMemory const& memory, T const& data)
void copyToDevice( vk::UniqueDevice const & device, vk::UniqueDeviceMemory const & memory, T const & data )
{
copyToDevice<T>(device, memory, &data, 1);
copyToDevice<T>( device, memory, &data, 1 );
}
template<class T>
VULKAN_HPP_INLINE constexpr const T& clamp(const T& v, const T& lo, const T& hi)
template <class T>
VULKAN_HPP_INLINE constexpr const T & clamp( const T & v, const T & lo, const T & hi )
{
return v < lo ? lo : hi < v ? hi : v;
}
void setImageLayout(vk::UniqueCommandBuffer const &commandBuffer, vk::Image image, vk::Format format, vk::ImageLayout oldImageLayout, vk::ImageLayout newImageLayout);
void setImageLayout( vk::UniqueCommandBuffer const & commandBuffer,
vk::Image image,
vk::Format format,
vk::ImageLayout oldImageLayout,
vk::ImageLayout newImageLayout );
struct WindowData
{
WindowData(GLFWwindow *wnd, std::string const& name, vk::Extent2D const& extent);
WindowData(const WindowData &) = delete;
WindowData(WindowData &&other);
WindowData( GLFWwindow * wnd, std::string const & name, vk::Extent2D const & extent );
WindowData( const WindowData & ) = delete;
WindowData( WindowData && other );
~WindowData() noexcept;
GLFWwindow *handle;
std::string name;
GLFWwindow * handle;
std::string name;
vk::Extent2D extent;
};
WindowData createWindow(std::string const &windowName, vk::Extent2D const &extent);
WindowData createWindow( std::string const & windowName, vk::Extent2D const & extent );
struct BufferData
{
BufferData(vk::PhysicalDevice const& physicalDevice, vk::UniqueDevice const& device, vk::DeviceSize size, vk::BufferUsageFlags usage,
vk::MemoryPropertyFlags propertyFlags = vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
BufferData( vk::PhysicalDevice const & physicalDevice,
vk::UniqueDevice const & device,
vk::DeviceSize size,
vk::BufferUsageFlags usage,
vk::MemoryPropertyFlags propertyFlags = vk::MemoryPropertyFlagBits::eHostVisible |
vk::MemoryPropertyFlagBits::eHostCoherent );
template <typename DataType>
void upload(vk::UniqueDevice const& device, DataType const& data) const
void upload( vk::UniqueDevice const & device, DataType const & data ) const
{
assert((m_propertyFlags & vk::MemoryPropertyFlagBits::eHostCoherent) && (m_propertyFlags & vk::MemoryPropertyFlagBits::eHostVisible));
assert(sizeof(DataType) <= m_size);
assert( ( m_propertyFlags & vk::MemoryPropertyFlagBits::eHostCoherent ) &&
( m_propertyFlags & vk::MemoryPropertyFlagBits::eHostVisible ) );
assert( sizeof( DataType ) <= m_size );
void* dataPtr = device->mapMemory(*this->deviceMemory, 0, sizeof(DataType));
memcpy(dataPtr, &data, sizeof(DataType));
device->unmapMemory(*this->deviceMemory);
void * dataPtr = device->mapMemory( *this->deviceMemory, 0, sizeof( DataType ) );
memcpy( dataPtr, &data, sizeof( DataType ) );
device->unmapMemory( *this->deviceMemory );
}
template <typename DataType>
void upload(vk::UniqueDevice const& device, std::vector<DataType> const& data, size_t stride = 0) const
void upload( vk::UniqueDevice const & device, std::vector<DataType> const & data, size_t stride = 0 ) const
{
assert(m_propertyFlags & vk::MemoryPropertyFlagBits::eHostVisible);
assert( m_propertyFlags & vk::MemoryPropertyFlagBits::eHostVisible );
size_t elementSize = stride ? stride : sizeof(DataType);
assert(sizeof(DataType) <= elementSize);
size_t elementSize = stride ? stride : sizeof( DataType );
assert( sizeof( DataType ) <= elementSize );
copyToDevice(device, deviceMemory, data.data(), data.size(), elementSize);
copyToDevice( device, deviceMemory, data.data(), data.size(), elementSize );
}
template <typename DataType>
void upload(vk::PhysicalDevice const& physicalDevice, vk::UniqueDevice const& device, vk::UniqueCommandPool const& commandPool, vk::Queue queue, std::vector<DataType> const& data,
size_t stride) const
void upload( vk::PhysicalDevice const & physicalDevice,
vk::UniqueDevice const & device,
vk::UniqueCommandPool const & commandPool,
vk::Queue queue,
std::vector<DataType> const & data,
size_t stride ) const
{
assert(m_usage & vk::BufferUsageFlagBits::eTransferDst);
assert(m_propertyFlags & vk::MemoryPropertyFlagBits::eDeviceLocal);
assert( m_usage & vk::BufferUsageFlagBits::eTransferDst );
assert( m_propertyFlags & vk::MemoryPropertyFlagBits::eDeviceLocal );
size_t elementSize = stride ? stride : sizeof(DataType);
assert(sizeof(DataType) <= elementSize);
size_t elementSize = stride ? stride : sizeof( DataType );
assert( sizeof( DataType ) <= elementSize );
size_t dataSize = data.size() * elementSize;
assert(dataSize <= m_size);
assert( dataSize <= m_size );
vk::su::BufferData stagingBuffer(physicalDevice, device, dataSize, vk::BufferUsageFlagBits::eTransferSrc);
copyToDevice(device, stagingBuffer.deviceMemory, data.data(), data.size(), elementSize);
vk::su::BufferData stagingBuffer( physicalDevice, device, dataSize, vk::BufferUsageFlagBits::eTransferSrc );
copyToDevice( device, stagingBuffer.deviceMemory, data.data(), data.size(), elementSize );
vk::su::oneTimeSubmit(device, commandPool, queue,
[&](vk::UniqueCommandBuffer const& commandBuffer) { commandBuffer->copyBuffer(*stagingBuffer.buffer, *this->buffer, vk::BufferCopy(0, 0, dataSize)); });
vk::su::oneTimeSubmit( device, commandPool, queue, [&]( vk::UniqueCommandBuffer const & commandBuffer ) {
commandBuffer->copyBuffer( *stagingBuffer.buffer, *this->buffer, vk::BufferCopy( 0, 0, dataSize ) );
} );
}
vk::UniqueBuffer buffer;
vk::UniqueDeviceMemory deviceMemory;
#if !defined(NDEBUG)
private:
vk::UniqueBuffer buffer;
vk::UniqueDeviceMemory deviceMemory;
#if !defined( NDEBUG )
private:
vk::DeviceSize m_size;
vk::BufferUsageFlags m_usage;
vk::MemoryPropertyFlags m_propertyFlags;
@@ -153,104 +177,149 @@ namespace vk
struct ImageData
{
ImageData(vk::PhysicalDevice const& physicalDevice, vk::UniqueDevice const& device, vk::Format format, vk::Extent2D const& extent, vk::ImageTiling tiling, vk::ImageUsageFlags usage
, vk::ImageLayout initialLayout, vk::MemoryPropertyFlags memoryProperties, vk::ImageAspectFlags aspectMask);
ImageData( vk::PhysicalDevice const & physicalDevice,
vk::UniqueDevice const & device,
vk::Format format,
vk::Extent2D const & extent,
vk::ImageTiling tiling,
vk::ImageUsageFlags usage,
vk::ImageLayout initialLayout,
vk::MemoryPropertyFlags memoryProperties,
vk::ImageAspectFlags aspectMask );
vk::Format format;
vk::UniqueImage image;
vk::UniqueDeviceMemory deviceMemory;
vk::UniqueImageView imageView;
vk::Format format;
vk::UniqueImage image;
vk::UniqueDeviceMemory deviceMemory;
vk::UniqueImageView imageView;
};
struct DepthBufferData : public ImageData
{
DepthBufferData(vk::PhysicalDevice &physicalDevice, vk::UniqueDevice & device, vk::Format format, vk::Extent2D const& extent);
DepthBufferData( vk::PhysicalDevice & physicalDevice,
vk::UniqueDevice & device,
vk::Format format,
vk::Extent2D const & extent );
};
struct SurfaceData
{
SurfaceData(vk::UniqueInstance &instance, std::string const& windowName, vk::Extent2D const& extent);
SurfaceData( vk::UniqueInstance & instance, std::string const & windowName, vk::Extent2D const & extent );
vk::Extent2D extent;
WindowData window;
vk::UniqueSurfaceKHR surface;
vk::Extent2D extent;
WindowData window;
vk::UniqueSurfaceKHR surface;
};
struct SwapChainData
{
SwapChainData(vk::PhysicalDevice const& physicalDevice, vk::UniqueDevice const& device, vk::SurfaceKHR const& surface, vk::Extent2D const& extent, vk::ImageUsageFlags usage,
vk::UniqueSwapchainKHR const& oldSwapChain, uint32_t graphicsFamilyIndex, uint32_t presentFamilyIndex);
SwapChainData( vk::PhysicalDevice const & physicalDevice,
vk::UniqueDevice const & device,
vk::SurfaceKHR const & surface,
vk::Extent2D const & extent,
vk::ImageUsageFlags usage,
vk::UniqueSwapchainKHR const & oldSwapChain,
uint32_t graphicsFamilyIndex,
uint32_t presentFamilyIndex );
vk::Format colorFormat;
vk::UniqueSwapchainKHR swapChain;
std::vector<vk::Image> images;
std::vector<vk::UniqueImageView> imageViews;
vk::Format colorFormat;
vk::UniqueSwapchainKHR swapChain;
std::vector<vk::Image> images;
std::vector<vk::UniqueImageView> imageViews;
};
class CheckerboardImageGenerator
{
public:
CheckerboardImageGenerator(std::array<uint8_t, 3> const& rgb0 = {0, 0, 0}, std::array<uint8_t, 3> const& rgb1 = {255, 255, 255});
CheckerboardImageGenerator( std::array<uint8_t, 3> const & rgb0 = { 0, 0, 0 },
std::array<uint8_t, 3> const & rgb1 = { 255, 255, 255 } );
void operator()(void* data, vk::Extent2D &extent) const;
void operator()( void * data, vk::Extent2D & extent ) const;
private:
std::array<uint8_t, 3> const& m_rgb0;
std::array<uint8_t, 3> const& m_rgb1;
std::array<uint8_t, 3> const & m_rgb0;
std::array<uint8_t, 3> const & m_rgb1;
};
class MonochromeImageGenerator
{
public:
MonochromeImageGenerator(std::array<unsigned char, 3> const& rgb);
public:
MonochromeImageGenerator( std::array<unsigned char, 3> const & rgb );
void operator()(void* data, vk::Extent2D &extent) const;
void operator()( void * data, vk::Extent2D & extent ) const;
private:
std::array<unsigned char, 3> const& m_rgb;
private:
std::array<unsigned char, 3> const & m_rgb;
};
class PixelsImageGenerator
{
public:
PixelsImageGenerator(vk::Extent2D const& extent, size_t channels, unsigned char const* pixels);
public:
PixelsImageGenerator( vk::Extent2D const & extent, size_t channels, unsigned char const * pixels );
void operator()(void* data, vk::Extent2D & extent) const;
void operator()( void * data, vk::Extent2D & extent ) const;
private:
private:
vk::Extent2D m_extent;
size_t m_channels;
unsigned char const* m_pixels;
unsigned char const * m_pixels;
};
struct TextureData
{
TextureData(vk::PhysicalDevice const& physicalDevice, vk::UniqueDevice const& device, vk::Extent2D const& extent_ = {256, 256}, vk::ImageUsageFlags usageFlags = {},
vk::FormatFeatureFlags formatFeatureFlags = {}, bool anisotropyEnable = false, bool forceStaging = false);
TextureData( vk::PhysicalDevice const & physicalDevice,
vk::UniqueDevice const & device,
vk::Extent2D const & extent_ = { 256, 256 },
vk::ImageUsageFlags usageFlags = {},
vk::FormatFeatureFlags formatFeatureFlags = {},
bool anisotropyEnable = false,
bool forceStaging = false );
template <typename ImageGenerator>
void setImage(vk::UniqueDevice const& device, vk::UniqueCommandBuffer const& commandBuffer, ImageGenerator const& imageGenerator)
void setImage( vk::UniqueDevice const & device,
vk::UniqueCommandBuffer const & commandBuffer,
ImageGenerator const & imageGenerator )
{
void* data = needsStaging
? device->mapMemory(stagingBufferData->deviceMemory.get(), 0, device->getBufferMemoryRequirements(stagingBufferData->buffer.get()).size)
: device->mapMemory(imageData->deviceMemory.get(), 0, device->getImageMemoryRequirements(imageData->image.get()).size);
imageGenerator(data, extent);
device->unmapMemory(needsStaging ? stagingBufferData->deviceMemory.get() : imageData->deviceMemory.get());
void * data =
needsStaging
? device->mapMemory( stagingBufferData->deviceMemory.get(),
0,
device->getBufferMemoryRequirements( stagingBufferData->buffer.get() ).size )
: device->mapMemory(
imageData->deviceMemory.get(), 0, device->getImageMemoryRequirements( imageData->image.get() ).size );
imageGenerator( data, extent );
device->unmapMemory( needsStaging ? stagingBufferData->deviceMemory.get() : imageData->deviceMemory.get() );
if (needsStaging)
if ( needsStaging )
{
// Since we're going to blit to the texture image, set its layout to eTransferDstOptimal
vk::su::setImageLayout(commandBuffer, imageData->image.get(), imageData->format, vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal);
vk::BufferImageCopy copyRegion(0, extent.width, extent.height, vk::ImageSubresourceLayers(vk::ImageAspectFlagBits::eColor, 0, 0, 1), vk::Offset3D(0, 0, 0), vk::Extent3D(extent, 1));
commandBuffer->copyBufferToImage(stagingBufferData->buffer.get(), imageData->image.get(), vk::ImageLayout::eTransferDstOptimal, copyRegion);
vk::su::setImageLayout( commandBuffer,
imageData->image.get(),
imageData->format,
vk::ImageLayout::eUndefined,
vk::ImageLayout::eTransferDstOptimal );
vk::BufferImageCopy copyRegion( 0,
extent.width,
extent.height,
vk::ImageSubresourceLayers( vk::ImageAspectFlagBits::eColor, 0, 0, 1 ),
vk::Offset3D( 0, 0, 0 ),
vk::Extent3D( extent, 1 ) );
commandBuffer->copyBufferToImage(
stagingBufferData->buffer.get(), imageData->image.get(), vk::ImageLayout::eTransferDstOptimal, copyRegion );
// Set the layout for the texture image from eTransferDstOptimal to SHADER_READ_ONLY
vk::su::setImageLayout(commandBuffer, imageData->image.get(), imageData->format, vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eShaderReadOnlyOptimal);
vk::su::setImageLayout( commandBuffer,
imageData->image.get(),
imageData->format,
vk::ImageLayout::eTransferDstOptimal,
vk::ImageLayout::eShaderReadOnlyOptimal );
}
else
{
// If we can use the linear tiled image as a texture, just do it
vk::su::setImageLayout(commandBuffer, imageData->image.get(), imageData->format, vk::ImageLayout::ePreinitialized, vk::ImageLayout::eShaderReadOnlyOptimal);
vk::su::setImageLayout( commandBuffer,
imageData->image.get(),
imageData->format,
vk::ImageLayout::ePreinitialized,
vk::ImageLayout::eShaderReadOnlyOptimal );
}
}
@@ -264,59 +333,100 @@ namespace vk
struct UUID
{
public:
UUID(uint8_t const data[VK_UUID_SIZE]);
public:
UUID( uint8_t const data[VK_UUID_SIZE] );
uint8_t m_data[VK_UUID_SIZE];
};
template <typename TargetType, typename SourceType>
VULKAN_HPP_INLINE TargetType checked_cast(SourceType value)
VULKAN_HPP_INLINE TargetType checked_cast( SourceType value )
{
static_assert(sizeof(TargetType) <= sizeof(SourceType), "No need to cast from smaller to larger type!");
static_assert(!std::numeric_limits<TargetType>::is_signed, "Only unsigned types supported!");
static_assert(!std::numeric_limits<SourceType>::is_signed, "Only unsigned types supported!");
assert(value <= std::numeric_limits<TargetType>::max());
return static_cast<TargetType>(value);
static_assert( sizeof( TargetType ) <= sizeof( SourceType ), "No need to cast from smaller to larger type!" );
static_assert( !std::numeric_limits<TargetType>::is_signed, "Only unsigned types supported!" );
static_assert( !std::numeric_limits<SourceType>::is_signed, "Only unsigned types supported!" );
assert( value <= std::numeric_limits<TargetType>::max() );
return static_cast<TargetType>( value );
}
vk::UniqueDeviceMemory allocateMemory(vk::UniqueDevice const& device, vk::PhysicalDeviceMemoryProperties const& memoryProperties, vk::MemoryRequirements const& memoryRequirements,
vk::MemoryPropertyFlags memoryPropertyFlags);
bool contains(std::vector<vk::ExtensionProperties> const& extensionProperties, std::string const& extensionName);
vk::UniqueCommandPool createCommandPool(vk::UniqueDevice &device, uint32_t queueFamilyIndex);
vk::UniqueDebugUtilsMessengerEXT createDebugUtilsMessenger(vk::UniqueInstance &instance);
vk::UniqueDescriptorPool createDescriptorPool(vk::UniqueDevice &device, std::vector<vk::DescriptorPoolSize> const& poolSizes);
vk::UniqueDescriptorSetLayout createDescriptorSetLayout(vk::UniqueDevice const& device, std::vector<std::tuple<vk::DescriptorType, uint32_t, vk::ShaderStageFlags>> const& bindingData,
vk::DescriptorSetLayoutCreateFlags flags = {});
vk::UniqueDevice createDevice(vk::PhysicalDevice physicalDevice, uint32_t queueFamilyIndex, std::vector<std::string> const& extensions = {}, vk::PhysicalDeviceFeatures const* physicalDeviceFeatures = nullptr, void const* pNext = nullptr);
std::vector<vk::UniqueFramebuffer> createFramebuffers(vk::UniqueDevice &device, vk::UniqueRenderPass &renderPass, std::vector<vk::UniqueImageView> const& imageViews, vk::UniqueImageView const& depthImageView, vk::Extent2D const& extent);
vk::UniquePipeline createGraphicsPipeline(vk::UniqueDevice const& device, vk::UniquePipelineCache const& pipelineCache,
std::pair<vk::ShaderModule, vk::SpecializationInfo const*> const& vertexShaderData,
std::pair<vk::ShaderModule, vk::SpecializationInfo const*> const& fragmentShaderData, uint32_t vertexStride,
std::vector<std::pair<vk::Format, uint32_t>> const& vertexInputAttributeFormatOffset, vk::FrontFace frontFace, bool depthBuffered,
vk::UniquePipelineLayout const& pipelineLayout, vk::UniqueRenderPass const& renderPass);
vk::UniqueInstance createInstance(std::string const& appName, std::string const& engineName, std::vector<std::string> const& layers = {}, std::vector<std::string> const& extensions = {},
uint32_t apiVersion = VK_API_VERSION_1_0);
vk::UniqueRenderPass createRenderPass(vk::UniqueDevice &device, vk::Format colorFormat, vk::Format depthFormat, vk::AttachmentLoadOp loadOp = vk::AttachmentLoadOp::eClear, vk::ImageLayout colorFinalLayout = vk::ImageLayout::ePresentSrcKHR);
VKAPI_ATTR VkBool32 VKAPI_CALL debugUtilsMessengerCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageTypes, VkDebugUtilsMessengerCallbackDataEXT const * pCallbackData, void * /*pUserData*/);
uint32_t findGraphicsQueueFamilyIndex(std::vector<vk::QueueFamilyProperties> const& queueFamilyProperties);
std::pair<uint32_t, uint32_t> findGraphicsAndPresentQueueFamilyIndex(vk::PhysicalDevice physicalDevice, vk::SurfaceKHR const& surface);
uint32_t findMemoryType(vk::PhysicalDeviceMemoryProperties const& memoryProperties, uint32_t typeBits, vk::MemoryPropertyFlags requirementsMask);
std::vector<std::string> getDeviceExtensions();
std::vector<std::string> getInstanceExtensions();
vk::Format pickDepthFormat(vk::PhysicalDevice const& physicalDevice);
vk::PresentModeKHR pickPresentMode(std::vector<vk::PresentModeKHR> const& presentModes);
vk::SurfaceFormatKHR pickSurfaceFormat(std::vector<vk::SurfaceFormatKHR> const& formats);
void submitAndWait(vk::UniqueDevice &device, vk::Queue queue, vk::UniqueCommandBuffer &commandBuffer);
void updateDescriptorSets(vk::UniqueDevice const& device, vk::UniqueDescriptorSet const& descriptorSet,
std::vector<std::tuple<vk::DescriptorType, vk::UniqueBuffer const&, vk::UniqueBufferView const&>> const& bufferData, vk::su::TextureData const& textureData,
uint32_t bindingOffset = 0);
void updateDescriptorSets(vk::UniqueDevice const& device, vk::UniqueDescriptorSet const& descriptorSet,
std::vector<std::tuple<vk::DescriptorType, vk::UniqueBuffer const&, vk::UniqueBufferView const&>> const& bufferData,
std::vector<vk::su::TextureData> const& textureData, uint32_t bindingOffset = 0);
vk::UniqueDeviceMemory allocateMemory( vk::UniqueDevice const & device,
vk::PhysicalDeviceMemoryProperties const & memoryProperties,
vk::MemoryRequirements const & memoryRequirements,
vk::MemoryPropertyFlags memoryPropertyFlags );
bool contains( std::vector<vk::ExtensionProperties> const & extensionProperties,
std::string const & extensionName );
vk::UniqueCommandPool createCommandPool( vk::UniqueDevice & device, uint32_t queueFamilyIndex );
vk::UniqueDebugUtilsMessengerEXT createDebugUtilsMessenger( vk::UniqueInstance & instance );
vk::UniqueDescriptorPool createDescriptorPool( vk::UniqueDevice & device,
std::vector<vk::DescriptorPoolSize> const & poolSizes );
vk::UniqueDescriptorSetLayout createDescriptorSetLayout(
vk::UniqueDevice const & device,
std::vector<std::tuple<vk::DescriptorType, uint32_t, vk::ShaderStageFlags>> const & bindingData,
vk::DescriptorSetLayoutCreateFlags flags = {} );
vk::UniqueDevice createDevice( vk::PhysicalDevice physicalDevice,
uint32_t queueFamilyIndex,
std::vector<std::string> const & extensions = {},
vk::PhysicalDeviceFeatures const * physicalDeviceFeatures = nullptr,
void const * pNext = nullptr );
std::vector<vk::UniqueFramebuffer> createFramebuffers( vk::UniqueDevice & device,
vk::UniqueRenderPass & renderPass,
std::vector<vk::UniqueImageView> const & imageViews,
vk::UniqueImageView const & depthImageView,
vk::Extent2D const & extent );
vk::UniquePipeline
createGraphicsPipeline( vk::UniqueDevice const & device,
vk::UniquePipelineCache const & pipelineCache,
std::pair<vk::ShaderModule, vk::SpecializationInfo const *> const & vertexShaderData,
std::pair<vk::ShaderModule, vk::SpecializationInfo const *> const & fragmentShaderData,
uint32_t vertexStride,
std::vector<std::pair<vk::Format, uint32_t>> const & vertexInputAttributeFormatOffset,
vk::FrontFace frontFace,
bool depthBuffered,
vk::UniquePipelineLayout const & pipelineLayout,
vk::UniqueRenderPass const & renderPass );
vk::UniqueInstance createInstance( std::string const & appName,
std::string const & engineName,
std::vector<std::string> const & layers = {},
std::vector<std::string> const & extensions = {},
uint32_t apiVersion = VK_API_VERSION_1_0 );
vk::UniqueRenderPass createRenderPass( vk::UniqueDevice & device,
vk::Format colorFormat,
vk::Format depthFormat,
vk::AttachmentLoadOp loadOp = vk::AttachmentLoadOp::eClear,
vk::ImageLayout colorFinalLayout = vk::ImageLayout::ePresentSrcKHR );
VKAPI_ATTR VkBool32 VKAPI_CALL
debugUtilsMessengerCallback( VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
VkDebugUtilsMessageTypeFlagsEXT messageTypes,
VkDebugUtilsMessengerCallbackDataEXT const * pCallbackData,
void * /*pUserData*/ );
uint32_t findGraphicsQueueFamilyIndex( std::vector<vk::QueueFamilyProperties> const & queueFamilyProperties );
std::pair<uint32_t, uint32_t> findGraphicsAndPresentQueueFamilyIndex( vk::PhysicalDevice physicalDevice,
vk::SurfaceKHR const & surface );
uint32_t findMemoryType( vk::PhysicalDeviceMemoryProperties const & memoryProperties,
uint32_t typeBits,
vk::MemoryPropertyFlags requirementsMask );
std::vector<std::string> getDeviceExtensions();
std::vector<std::string> getInstanceExtensions();
vk::Format pickDepthFormat( vk::PhysicalDevice const & physicalDevice );
vk::PresentModeKHR pickPresentMode( std::vector<vk::PresentModeKHR> const & presentModes );
vk::SurfaceFormatKHR pickSurfaceFormat( std::vector<vk::SurfaceFormatKHR> const & formats );
void submitAndWait( vk::UniqueDevice & device, vk::Queue queue, vk::UniqueCommandBuffer & commandBuffer );
void updateDescriptorSets(
vk::UniqueDevice const & device,
vk::UniqueDescriptorSet const & descriptorSet,
std::vector<std::tuple<vk::DescriptorType, vk::UniqueBuffer const &, vk::UniqueBufferView const &>> const &
bufferData,
vk::su::TextureData const & textureData,
uint32_t bindingOffset = 0 );
void updateDescriptorSets(
vk::UniqueDevice const & device,
vk::UniqueDescriptorSet const & descriptorSet,
std::vector<std::tuple<vk::DescriptorType, vk::UniqueBuffer const &, vk::UniqueBufferView const &>> const &
bufferData,
std::vector<vk::su::TextureData> const & textureData,
uint32_t bindingOffset = 0 );
}
}
} // namespace su
} // namespace vk
std::ostream& operator<<(std::ostream& os, vk::su::UUID const& uuid);
std::ostream & operator<<( std::ostream & os, vk::su::UUID const & uuid );