Add samples OcclusionQuery, PipelineCache, PipelineDerivative, PushConstants, and PushDescriptors. (#325)

+ some minor changes in some samples, math, shaders, and utils.
This commit is contained in:
Andreas Süßenbach
2019-05-09 15:25:40 +02:00
committed by Markus Tavenrath
parent 61e92d4842
commit 5ce8ae7fd0
21 changed files with 1489 additions and 62 deletions

View File

@@ -29,7 +29,7 @@ namespace vk
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 projection = glm::perspective(fov, static_cast<float>(extent.width) / static_cast<float>(extent.height), 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;
}

View File

@@ -15,7 +15,7 @@
#include "shaders.hpp"
#include "vulkan/vulkan.hpp"
#include "glslang/StandAlone/ResourceLimits.h"
#include "StandAlone/ResourceLimits.h"
#include "SPIRV/GlslangToSpv.h"
namespace vk

View File

@@ -15,6 +15,7 @@
#include "utils.hpp"
#include "vulkan/vulkan.hpp"
#include <iomanip>
PFN_vkCreateDebugReportCallbackEXT pfnVkCreateDebugReportCallbackEXT;
PFN_vkDestroyDebugReportCallbackEXT pfnVkDestroyDebugReportCallbackEXT;
@@ -64,7 +65,7 @@ namespace vk
return device->createDescriptorPoolUnique(descriptorPoolCreateInfo);
}
vk::UniqueDescriptorSetLayout createDescriptorSetLayout(vk::UniqueDevice &device, vk::DescriptorType descriptorType, bool textured)
vk::UniqueDescriptorSetLayout createDescriptorSetLayout(vk::UniqueDevice &device, vk::DescriptorType descriptorType, bool textured, vk::DescriptorSetLayoutCreateFlags flags)
{
std::vector<vk::DescriptorSetLayoutBinding> bindings;
bindings.push_back(vk::DescriptorSetLayoutBinding(0, descriptorType, 1, vk::ShaderStageFlagBits::eVertex));
@@ -72,8 +73,7 @@ namespace vk
{
bindings.push_back(vk::DescriptorSetLayoutBinding(1, vk::DescriptorType::eCombinedImageSampler, 1, vk::ShaderStageFlagBits::eFragment));
}
vk::DescriptorSetLayoutBinding descriptorSetLayoutBinding(0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex);
return device->createDescriptorSetLayoutUnique(vk::DescriptorSetLayoutCreateInfo({}, checked_cast<uint32_t>(bindings.size()), bindings.data()));
return device->createDescriptorSetLayoutUnique(vk::DescriptorSetLayoutCreateInfo(flags, checked_cast<uint32_t>(bindings.size()), bindings.data()));
}
vk::UniqueDevice createDevice(vk::PhysicalDevice physicalDevice, uint32_t queueFamilyIndex, std::vector<std::string> const& extensions)
@@ -108,7 +108,8 @@ namespace vk
return framebuffers;
}
vk::UniquePipeline createGraphicsPipeline(vk::UniqueDevice &device, vk::UniquePipelineCache &pipelineCache, vk::UniqueShaderModule &vertexShaderModule, vk::UniqueShaderModule &fragmentShaderModule, uint32_t vertexStride, bool depthBuffered, vk::UniquePipelineLayout &pipelineLayout, vk::UniqueRenderPass &renderPass)
vk::UniquePipeline createGraphicsPipeline(vk::UniqueDevice &device, vk::UniquePipelineCache &pipelineCache, vk::UniqueShaderModule &vertexShaderModule,
vk::UniqueShaderModule &fragmentShaderModule, uint32_t vertexStride, bool depthBuffered, bool textured, vk::UniquePipelineLayout &pipelineLayout, vk::UniqueRenderPass &renderPass)
{
vk::PipelineShaderStageCreateInfo pipelineShaderStageCreateInfos[2] =
{
@@ -123,7 +124,7 @@ namespace vk
vk::VertexInputAttributeDescription vertexInputAttributeDescriptions[2] =
{
vk::VertexInputAttributeDescription(0, 0, vk::Format::eR32G32B32A32Sfloat, 0),
vk::VertexInputAttributeDescription(1, 0, vk::Format::eR32G32B32A32Sfloat, 16)
vk::VertexInputAttributeDescription(1, 0, textured ? vk::Format::eR32G32Sfloat : vk::Format::eR32G32B32A32Sfloat, 16)
};
pipelineVertexInputStateCreateInfo.vertexBindingDescriptionCount = 1;
pipelineVertexInputStateCreateInfo.pVertexBindingDescriptions = &vertexInputBindingDescription;
@@ -503,6 +504,27 @@ namespace vk
}
}
MonochromeTextureGenerator::MonochromeTextureGenerator(std::array<unsigned char, 3> const& rgb_)
: rgb(rgb_)
{}
void MonochromeTextureGenerator::operator()(void* data, vk::Extent2D &extent) const
{
// fill in with the monochrome color
unsigned char *pImageMemory = static_cast<unsigned char*>(data);
for (uint32_t row = 0; row < extent.height; row++)
{
for (uint32_t col = 0; col < extent.width; col++)
{
pImageMemory[0] = rgb[0];
pImageMemory[1] = rgb[1];
pImageMemory[2] = rgb[2];
pImageMemory[3] = 255;
pImageMemory += 4;
}
}
}
TextureData::TextureData(vk::PhysicalDevice &physicalDevice, vk::UniqueDevice &device, vk::ImageUsageFlags usageFlags, vk::FormatFeatureFlags formatFeatureFlags)
: format(vk::Format::eR8G8B8A8Unorm)
, extent(256, 256)
@@ -536,6 +558,11 @@ namespace vk
, vk::BorderColor::eFloatOpaqueWhite));
}
UUID::UUID(uint8_t data[VK_UUID_SIZE])
{
memcpy(m_data, data, VK_UUID_SIZE * sizeof(uint8_t));
}
#if defined(VK_USE_PLATFORM_WIN32_KHR)
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
@@ -588,3 +615,18 @@ namespace vk
#endif
}
}
std::ostream& operator<<(std::ostream& os, vk::su::UUID const& uuid)
{
os << std::setfill('0');
for (int j = 0; j < VK_UUID_SIZE; ++j)
{
os << std::hex << std::setw(2) << static_cast<uint32_t>(uuid.m_data[j]);
if (j == 3 || j == 5 || j == 7 || j == 9)
{
std::cout << '-';
}
}
os << std::setfill(' ');
return os;
}

View File

@@ -71,6 +71,18 @@ namespace vk
void operator()(void* data, vk::Extent2D &extent) const;
};
class MonochromeTextureGenerator
{
public:
MonochromeTextureGenerator(std::array<unsigned char, 3> const& rgb_);
void operator()(void* data, vk::Extent2D &extent) const;
private:
std::array<unsigned char, 3> const& rgb;
};
struct TextureData
{
TextureData(vk::PhysicalDevice &physicalDevice, vk::UniqueDevice &device, vk::ImageUsageFlags usageFlags = {}, vk::FormatFeatureFlags formatFeatureFlags = {});
@@ -108,6 +120,14 @@ namespace vk
vk::UniqueSampler textureSampler;
};
struct UUID
{
public:
UUID(uint8_t data[VK_UUID_SIZE]);
uint8_t m_data[VK_UUID_SIZE];
};
template <typename TargetType, typename SourceType>
VULKAN_HPP_INLINE TargetType checked_cast(SourceType value)
@@ -155,10 +175,11 @@ namespace vk
vk::UniqueCommandPool createCommandPool(vk::UniqueDevice &device, uint32_t queueFamilyIndex);
vk::UniqueDebugReportCallbackEXT createDebugReportCallback(vk::UniqueInstance &instance);
vk::UniqueDescriptorPool createDescriptorPool(vk::UniqueDevice &device, vk::DescriptorType descriptorType = vk::DescriptorType::eUniformBuffer, bool textured = false);
vk::UniqueDescriptorSetLayout createDescriptorSetLayout(vk::UniqueDevice &device, vk::DescriptorType = vk::DescriptorType::eUniformBuffer, bool textured = false);
vk::UniqueDescriptorSetLayout createDescriptorSetLayout(vk::UniqueDevice &device, vk::DescriptorType = vk::DescriptorType::eUniformBuffer, bool textured = false, vk::DescriptorSetLayoutCreateFlags flags = {});
vk::UniqueDevice createDevice(vk::PhysicalDevice physicalDevice, uint32_t queueFamilyIndex, std::vector<std::string> const& extensions = {});
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 &device, vk::UniquePipelineCache &pipelineCache, vk::UniqueShaderModule &vertexShaderModule, vk::UniqueShaderModule &fragmentShaderModule, uint32_t vertexStride, bool depthBuffered, vk::UniquePipelineLayout &pipelineLayout, vk::UniqueRenderPass &renderPass);
vk::UniquePipeline createGraphicsPipeline(vk::UniqueDevice &device, vk::UniquePipelineCache &pipelineCache, vk::UniqueShaderModule &vertexShaderModule,
vk::UniqueShaderModule &fragmentShaderModule, uint32_t vertexStride, bool depthBuffered, bool textured, vk::UniquePipelineLayout &pipelineLayout, vk::UniqueRenderPass &renderPass);
vk::UniqueInstance createInstance(std::string const& appName, std::string const& engineName, 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);
VkBool32 debugReportCallback(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objectType, uint64_t object, size_t location, int32_t messageCode, const char* pLayerPrefix, const char* pMessage, void* pUserData);
@@ -179,3 +200,5 @@ namespace vk
#endif
}
}
std::ostream& operator<<(std::ostream& os, vk::su::UUID const& uuid);