Add depth bias config to shader

This commit is contained in:
Georg Hagen
2025-01-05 17:36:19 +01:00
parent 56cb508002
commit 4f1d730e30
4 changed files with 41 additions and 26 deletions

View File

@@ -58,6 +58,7 @@ namespace OpenVulkano::Scene
billboardUniformBinding.stageFlags = ShaderProgramType::Type::VERTEX; billboardUniformBinding.stageFlags = ShaderProgramType::Type::VERTEX;
shader.AddDescriptorSetLayoutBinding(billboardUniformBinding, 4); shader.AddDescriptorSetLayoutBinding(billboardUniformBinding, 4);
} }
shader.EnableDepthBias();
return shader; return shader;
} }

View File

@@ -81,7 +81,6 @@ namespace OpenVulkano::Scene
}; };
class Shader final : public RenderResourceHolder<Shader>, public ICloseable class Shader final : public RenderResourceHolder<Shader>, public ICloseable
{ {
public: public:
@@ -96,6 +95,8 @@ namespace OpenVulkano::Scene
bool depthTest = true; bool depthTest = true;
bool depthWrite = true; bool depthWrite = true;
bool dynamicViewport = true; // If disabled the swapchains fullscreen viewport will always be used, regardless of framebuffer or viewport bool dynamicViewport = true; // If disabled the swapchains fullscreen viewport will always be used, regardless of framebuffer or viewport
bool depthBias = false;
float depthBiasClamp = 0.0f, depthBiasSlope = 0.0f, depthBiasConstant = 0.0f;
Shader() = default; Shader() = default;
~Shader() override { Shader::Close(); } ~Shader() override { Shader::Close(); }
@@ -128,6 +129,7 @@ namespace OpenVulkano::Scene
{ {
CheckShaderInitState(); CheckShaderInitState();
if (bindingId < 0) bindingId = static_cast<int>(vertexInputDescriptions.size()); if (bindingId < 0) bindingId = static_cast<int>(vertexInputDescriptions.size());
// ReSharper disable once CppDFALoopConditionNotUpdated
while (bindingId > static_cast<int>(vertexInputDescriptions.size())) while (bindingId > static_cast<int>(vertexInputDescriptions.size()))
{ {
vertexInputDescriptions.emplace_back(0, 0); vertexInputDescriptions.emplace_back(0, 0);
@@ -149,6 +151,7 @@ namespace OpenVulkano::Scene
if (setId < 0) setId = static_cast<int>(descriptorSets.size() + 2); if (setId < 0) setId = static_cast<int>(descriptorSets.size() + 2);
if (setId < 2) throw std::runtime_error("Cant bind set id 0 or 1! They are used for node and camera!"); if (setId < 2) throw std::runtime_error("Cant bind set id 0 or 1! They are used for node and camera!");
setId -= 2; setId -= 2;
// ReSharper disable once CppDFALoopConditionNotUpdated
while (setId >= static_cast<int>(descriptorSets.size())) while (setId >= static_cast<int>(descriptorSets.size()))
{ {
descriptorSets.emplace_back(); descriptorSets.emplace_back();
@@ -163,6 +166,13 @@ namespace OpenVulkano::Scene
pushConstantRanges.push_back(pushConstantRange); pushConstantRanges.push_back(pushConstantRange);
} }
void EnableDepthBias(const float slope = -1.0, const float constant = 0.001f)
{
depthBias = true;
depthBiasSlope = slope;
depthBiasConstant = constant;
}
void Close() override void Close() override
{ {
if (HasRenderResource()) if (HasRenderResource())

View File

@@ -84,6 +84,13 @@ namespace OpenVulkano::Vulkan
vk::PipelineInputAssemblyStateCreateInfo inputAssembly = { {}, static_cast<vk::PrimitiveTopology>(shader->topology), 0 }; vk::PipelineInputAssemblyStateCreateInfo inputAssembly = { {}, static_cast<vk::PrimitiveTopology>(shader->topology), 0 };
vk::PipelineRasterizationStateCreateInfo rasterizer = {}; vk::PipelineRasterizationStateCreateInfo rasterizer = {};
rasterizer.cullMode = static_cast<vk::CullModeFlagBits>(shader->cullMode); rasterizer.cullMode = static_cast<vk::CullModeFlagBits>(shader->cullMode);
if (shader->depthBias)
{
rasterizer.depthBiasEnable = VK_TRUE;
rasterizer.depthBiasClamp = shader->depthBiasClamp;
rasterizer.depthBiasConstantFactor = shader->depthBiasConstant;
rasterizer.depthBiasSlopeFactor = shader->depthBiasSlope;
}
vk::PipelineMultisampleStateCreateInfo msaa = {}; vk::PipelineMultisampleStateCreateInfo msaa = {};
vk::PipelineDepthStencilStateCreateInfo depth = { {}, shader->depthTest, shader->depthWrite, static_cast<vk::CompareOp>(shader->depthCompareOp) }; vk::PipelineDepthStencilStateCreateInfo depth = { {}, shader->depthTest, shader->depthWrite, static_cast<vk::CompareOp>(shader->depthCompareOp) };
depth.maxDepthBounds = 1; depth.maxDepthBounds = 1;

View File

@@ -11,9 +11,7 @@
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
#include <vector> #include <vector>
namespace OpenVulkano namespace OpenVulkano::Vulkan
{
namespace Vulkan
{ {
class Context; class Context;
class IShaderOwner; class IShaderOwner;
@@ -46,4 +44,3 @@ namespace OpenVulkano
void CreatePipelineLayout(); void CreatePipelineLayout();
}; };
} }
}