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;
shader.AddDescriptorSetLayoutBinding(billboardUniformBinding, 4);
}
shader.EnableDepthBias();
return shader;
}

View File

@@ -81,7 +81,6 @@ namespace OpenVulkano::Scene
};
class Shader final : public RenderResourceHolder<Shader>, public ICloseable
{
public:
@@ -96,6 +95,8 @@ namespace OpenVulkano::Scene
bool depthTest = true;
bool depthWrite = true;
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() override { Shader::Close(); }
@@ -128,6 +129,7 @@ namespace OpenVulkano::Scene
{
CheckShaderInitState();
if (bindingId < 0) bindingId = static_cast<int>(vertexInputDescriptions.size());
// ReSharper disable once CppDFALoopConditionNotUpdated
while (bindingId > static_cast<int>(vertexInputDescriptions.size()))
{
vertexInputDescriptions.emplace_back(0, 0);
@@ -149,6 +151,7 @@ namespace OpenVulkano::Scene
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!");
setId -= 2;
// ReSharper disable once CppDFALoopConditionNotUpdated
while (setId >= static_cast<int>(descriptorSets.size()))
{
descriptorSets.emplace_back();
@@ -163,6 +166,13 @@ namespace OpenVulkano::Scene
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
{
if (HasRenderResource())

View File

@@ -84,6 +84,13 @@ namespace OpenVulkano::Vulkan
vk::PipelineInputAssemblyStateCreateInfo inputAssembly = { {}, static_cast<vk::PrimitiveTopology>(shader->topology), 0 };
vk::PipelineRasterizationStateCreateInfo rasterizer = {};
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::PipelineDepthStencilStateCreateInfo depth = { {}, shader->depthTest, shader->depthWrite, static_cast<vk::CompareOp>(shader->depthCompareOp) };
depth.maxDepthBounds = 1;

View File

@@ -11,39 +11,36 @@
#include <vulkan/vulkan.hpp>
#include <vector>
namespace OpenVulkano
namespace OpenVulkano::Vulkan
{
namespace Vulkan
{
class Context;
class IShaderOwner;
class Context;
class IShaderOwner;
class VulkanShader final : public IRenderResource<Scene::Shader>, public IRecordable
{
public:
vk::Device device;
std::vector<vk::ShaderModule> shaderModules; // TODO manage live time somewhere else to allow sharing of shader programs
std::vector<vk::PipelineShaderStageCreateInfo> shaderStageCreateInfo;
vk::Pipeline pipeline; // TODO pipeline and shader config should be split
std::vector<vk::DescriptorSetLayout> descriptorSetLayouts;
vk::PipelineLayout pipelineLayout;
IShaderOwner* owner = nullptr;
Context* context = nullptr;
{
public:
vk::Device device;
std::vector<vk::ShaderModule> shaderModules; // TODO manage live time somewhere else to allow sharing of shader programs
std::vector<vk::PipelineShaderStageCreateInfo> shaderStageCreateInfo;
vk::Pipeline pipeline; // TODO pipeline and shader config should be split
std::vector<vk::DescriptorSetLayout> descriptorSetLayouts;
vk::PipelineLayout pipelineLayout;
IShaderOwner* owner = nullptr;
Context* context = nullptr;
VulkanShader(Context* context, Scene::Shader* shader, IShaderOwner* owner);
VulkanShader(Context* context, Scene::Shader* shader, IShaderOwner* owner);
~VulkanShader() override;
~VulkanShader() override;
void Resize();
void Resize();
void Record(VulkanDrawContext* context) override;
void Record(VulkanDrawContext* context) override;
void Release() override;
void Release() override;
private:
void BuildPipeline();
private:
void BuildPipeline();
void CreatePipelineLayout();
};
}
void CreatePipelineLayout();
};
}