From fb524d1675cf74e4d9e11e6b070f2158dfd0a57a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Wed, 30 Mar 2022 15:12:11 +0200 Subject: [PATCH] GPUShaderCreateInfo: Add DepthWrite option This option lets specify explicitely how the fragment shader will change the fragment's depth. --- .../gpu/intern/gpu_shader_create_info.cc | 3 +++ .../gpu/intern/gpu_shader_create_info.hh | 20 +++++++++++++++++++ source/blender/gpu/opengl/gl_shader.cc | 17 ++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/source/blender/gpu/intern/gpu_shader_create_info.cc b/source/blender/gpu/intern/gpu_shader_create_info.cc index 350575e9d36..0dd82d4ea44 100644 --- a/source/blender/gpu/intern/gpu_shader_create_info.cc +++ b/source/blender/gpu/intern/gpu_shader_create_info.cc @@ -65,6 +65,9 @@ void ShaderCreateInfo::finalize() if (info.early_fragment_test_) { early_fragment_test_ = true; } + if (info.depth_write_ != DepthWrite::ANY) { + depth_write_ = info.depth_write_; + } validate(info); diff --git a/source/blender/gpu/intern/gpu_shader_create_info.hh b/source/blender/gpu/intern/gpu_shader_create_info.hh index 9984295457c..ad5ca9ce29c 100644 --- a/source/blender/gpu/intern/gpu_shader_create_info.hh +++ b/source/blender/gpu/intern/gpu_shader_create_info.hh @@ -130,6 +130,17 @@ enum class BuiltinBits { }; ENUM_OPERATORS(BuiltinBits, BuiltinBits::WORK_GROUP_SIZE); +/** + * Follow convention described in: + * https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_conservative_depth.txt + */ +enum class DepthWrite { + ANY = 0, + GREATER, + LESS, + UNCHANGED, +}; + /* Samplers & images. */ enum class ImageType { /** Color samplers/image. */ @@ -273,6 +284,8 @@ struct ShaderCreateInfo { bool auto_resource_location_ = false; /** If true, force depth and stencil tests to always happen before fragment shader invocation. */ bool early_fragment_test_ = false; + /** Allow optimisation when fragment shader writes to gl_FragDepth. */ + DepthWrite depth_write_ = DepthWrite::ANY; /** * Maximum length of all the resource names including each null terminator. * Only for names used by gpu::ShaderInterface. @@ -695,6 +708,13 @@ struct ShaderCreateInfo { return *(Self *)this; } + /* Defines how the fragment shader will write to gl_FragDepth. */ + Self &depth_write(DepthWrite value) + { + depth_write_ = value; + return *(Self *)this; + } + Self &auto_resource_location(bool value) { auto_resource_location_ = value; diff --git a/source/blender/gpu/opengl/gl_shader.cc b/source/blender/gpu/opengl/gl_shader.cc index 71428633d79..5938444ce49 100644 --- a/source/blender/gpu/opengl/gl_shader.cc +++ b/source/blender/gpu/opengl/gl_shader.cc @@ -217,6 +217,20 @@ static const char *to_string(const PrimitiveOut &layout) } } +static const char *to_string(const DepthWrite &value) +{ + switch (value) { + case DepthWrite::ANY: + return "depth_any"; + case DepthWrite::GREATER: + return "depth_greater"; + case DepthWrite::LESS: + return "depth_less"; + default: + return "depth_unchanged"; + } +} + static void print_image_type(std::ostream &os, const ImageType &type, const ShaderCreateInfo::Resource::BindType bind_type) @@ -585,6 +599,9 @@ std::string GLShader::fragment_interface_declare(const ShaderCreateInfo &info) c if (info.early_fragment_test_) { ss << "layout(early_fragment_tests) in;\n"; } + if (GLEW_VERSION_4_2 || GLEW_ARB_conservative_depth) { + ss << "layout(" << to_string(info.depth_write_) << ") out float gl_FragDepth;\n"; + } ss << "\n/* Outputs. */\n"; for (const ShaderCreateInfo::FragOut &output : info.fragment_outputs_) { ss << "layout(location = " << output.index;