Cleanup descriptor layout handling

This commit is contained in:
2023-09-03 17:07:23 +02:00
parent b328db0075
commit 446e11e3b8
7 changed files with 49 additions and 20 deletions

View File

@@ -162,6 +162,21 @@ namespace openVulkanoCpp::Math
return { r / factor, g / factor, b / factor, a * 255 / MAX_ALPHA_VALUE };
}
operator Math::Vector4i() const
{
return { r, g, b, a };
}
operator Math::Vector3i_SIMD() const
{
return { r, g, b };
}
operator Math::Vector3i() const
{
return { r, g, b };
}
RGB10A2& operator =(const RGB10A2& rhs)
{
this->value = rhs.value;

View File

@@ -17,11 +17,15 @@ namespace openVulkanoCpp::Scene
{
public:
ICloseable* renderCamera = nullptr;
static constexpr inline size_t SIZE = sizeof(Math::Matrix4f) * 3 + sizeof(Math::Vector4f) + sizeof(float) * 8 + 16;
static constexpr inline DescriptorSetLayoutBinding DESCRIPTOR_SET_LAYOUT_BINDING = { 0, DescriptorSetLayoutBinding::Type::TYPE_UNIFORM_BUFFER_DYNAMIC, 1, ShaderProgramType::ALL_GRAPHICS };
protected:
Math::Matrix4f m_viewProjection{1}, m_view{1}, m_projection{1};
Math::Vector4f m_camPosition{};
float m_nearPlane, m_farPlane, m_width, m_height;
float m_fov = 0, m_aspect = 0, m_scaleFactor = 0, m_perPixelScaleFactor = 0;
std::array<uint8_t, 16> m_userData{};
Camera() : m_nearPlane(0), m_farPlane(0), m_width(0), m_height(0) {}
@@ -35,11 +39,6 @@ namespace openVulkanoCpp::Scene
//if (renderCamera) renderCamera->Close();
}
public:
static constexpr size_t SIZE = sizeof(Math::Matrix4f) * 3 + sizeof(Math::Vector4f) + sizeof(float) * 8;
void* GetData() { return &m_viewProjection; }
void Init(float width, float height, float nearPlane, float farPlane)
{
m_width = width;
@@ -50,6 +49,9 @@ namespace openVulkanoCpp::Scene
UpdateProjectionMatrix();
}
public:
void* GetData() { return &m_viewProjection; }
virtual void SetSize(const float width, const float height)
{
if (m_width == width && m_height == height) return;
@@ -125,13 +127,16 @@ namespace openVulkanoCpp::Scene
{
return {m_viewProjection};
}
/**
* The 16 byte of user data can be used to transmit additional data about the camera to the shader.
* @return reference to the custom data array
*/
[[nodiscard]] std::array<uint8_t, 16>& GetUserData() { return m_userData; }
};
class PerspectiveCamera : public Camera
{
protected:
float m_fov = 0, m_aspect = 0, m_scaleFactor = 0, m_perPixelScaleFactor = 0;
public:
PerspectiveCamera() = default;
~PerspectiveCamera() override = default;
@@ -157,6 +162,11 @@ namespace openVulkanoCpp::Scene
m_perPixelScaleFactor = m_height / m_scaleFactor;
}
[[nodiscard]] int GetPixelPerMeter(float distance) const
{
return static_cast<int>(m_perPixelScaleFactor / distance);
}
void SetSize(const float width, const float height) override
{
m_aspect = width / height;
@@ -221,7 +231,6 @@ namespace openVulkanoCpp::Scene
class OrthographicCamera : public Camera
{
std::array<float, 4> placeholder;
public:
void UpdateProjectionMatrix() final
{

View File

@@ -10,6 +10,7 @@
#include "Math/Math.hpp"
#include "Drawable.hpp"
#include "UpdateFrequency.hpp"
#include "Shader/DescriptorInputDescription.hpp"
#include <vector>
#include <memory>
@@ -22,6 +23,9 @@ namespace openVulkanoCpp::Scene
friend Scene;
public:
static constexpr size_t SIZE = sizeof(Math::Matrix4f);
static constexpr DescriptorSetLayoutBinding DESCRIPTOR_SET_LAYOUT_BINDING = { 0, DescriptorSetLayoutBinding::Type::TYPE_UNIFORM_BUFFER_DYNAMIC, 1, ShaderProgramType::ALL_GRAPHICS };
Math::Matrix4f localMat, worldMat;
bool enabled = true;
Node* parent = nullptr;

View File

@@ -60,7 +60,4 @@ namespace openVulkanoCpp
std::tie(rhs.bindingId, rhs.descriptorType, rhs.descriptorCount, rhs.stageFlags, rhs.immutableSamplers);
}
};
static constexpr inline DescriptorSetLayoutBinding NODE_LAYOUT_BINDING = {0, DescriptorSetLayoutBinding::Type::TYPE_UNIFORM_BUFFER_DYNAMIC, 1, ShaderProgramType::ALL_GRAPHICS};
static constexpr inline DescriptorSetLayoutBinding CAM_LAYOUT_BINDING = {0, DescriptorSetLayoutBinding::Type::TYPE_UNIFORM_BUFFER_DYNAMIC, 1, ShaderProgramType::ALL_GRAPHICS};
}

View File

@@ -5,7 +5,7 @@ rm Shaders.c
rm *.spv
for f in ./* ; do
if [[ "$f" != *.sh ]]
if [[ "$f" != *.sh ]] && [[ "$f" != *.hpp ]] && [[ "$f" != *.cpp ]]
then
glslang -V $f -o $f.spv
fi

View File

@@ -163,10 +163,10 @@ namespace openVulkanoCpp::Vulkan
else
{
vkNode = new VulkanNode();
buffer = CreateDeviceOnlyBufferWithData(sizeof(Math::Matrix4f), vk::BufferUsageFlagBits::eUniformBuffer, &node->worldMat);
buffer = CreateDeviceOnlyBufferWithData(Scene::Node::SIZE, vk::BufferUsageFlagBits::eUniformBuffer, &node->worldMat);
}
uBuffer->Init(buffer, frameSize, allocSize, GetDescriptorLayoutSet(NODE_LAYOUT_BINDING), NODE_LAYOUT_BINDING, 0);
uBuffer->Init(buffer, frameSize, allocSize, GetDescriptorLayoutSet(Scene::Node::DESCRIPTOR_SET_LAYOUT_BINDING), Scene::Node::DESCRIPTOR_SET_LAYOUT_BINDING, 0);
vkNode->Init(node, uBuffer);
node->renderNode = vkNode;
}
@@ -182,7 +182,7 @@ namespace openVulkanoCpp::Vulkan
ManagedBuffer* buffer = CreateBuffer(imgs * allocSize, vk::BufferUsageFlagBits::eUniformBuffer, vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostVisible);
buffer->Map();
UniformBuffer* uBuffer = new UniformBuffer();
uBuffer->Init(buffer, allocSize, allocSize, GetDescriptorLayoutSet(CAM_LAYOUT_BINDING), CAM_LAYOUT_BINDING, 1);
uBuffer->Init(buffer, allocSize, allocSize, GetDescriptorLayoutSet(Scene::Camera::DESCRIPTOR_SET_LAYOUT_BINDING), Scene::Camera::DESCRIPTOR_SET_LAYOUT_BINDING, 1);
VulkanCamera* vkCam = new VulkanCamera();
vkCam->Init(camera, uBuffer);
camera->renderCamera = vkCam;

View File

@@ -129,10 +129,14 @@ namespace openVulkanoCpp::Vulkan
void VulkanShader::CreatePipelineLayout()
{
//vk::PushConstantRange camPushConstantDesc = { vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment, 0, 3 * sizeof(Math::Matrix4f) + sizeof(Math::Vector4f) + 8 * sizeof(float) };
//std::array<vk::PushConstantRange, 1> camPushConstantDescs = { camPushConstantDesc };
std::array<vk::DescriptorSetLayoutBinding, 1> layoutBindings1 = { reinterpret_cast<const vk::DescriptorSetLayoutBinding&>(NODE_LAYOUT_BINDING) };
std::array<vk::DescriptorSetLayoutBinding, 1> layoutBindings2 = { reinterpret_cast<const vk::DescriptorSetLayoutBinding&>(CAM_LAYOUT_BINDING) };
if (!descriptorSetLayouts.empty())
{
for(auto& descriptorSetLayout : descriptorSetLayouts)
device.destroyDescriptorSetLayout(descriptorSetLayout);
descriptorSetLayouts.clear();
}
std::array<vk::DescriptorSetLayoutBinding, 1> layoutBindings1 = { reinterpret_cast<const vk::DescriptorSetLayoutBinding&>(Scene::Camera::DESCRIPTOR_SET_LAYOUT_BINDING) };
std::array<vk::DescriptorSetLayoutBinding, 1> layoutBindings2 = { reinterpret_cast<const vk::DescriptorSetLayoutBinding&>(Scene::Node::DESCRIPTOR_SET_LAYOUT_BINDING) };
descriptorSetLayouts.push_back(device.createDescriptorSetLayout({ {}, layoutBindings1.size(), layoutBindings1.data() }));
descriptorSetLayouts.push_back(device.createDescriptorSetLayout({ {}, layoutBindings2.size(), layoutBindings2.data() }));