diff options
5 files changed, 76 insertions, 71 deletions
diff --git a/source/blender/draw/engines/gpencil/gpencil_shader.c b/source/blender/draw/engines/gpencil/gpencil_shader.c index 557844534ae..c37225d84de 100644 --- a/source/blender/draw/engines/gpencil/gpencil_shader.c +++ b/source/blender/draw/engines/gpencil/gpencil_shader.c @@ -96,40 +96,9 @@ GPUShader *GPENCIL_shader_antialiasing(int stage) BLI_assert(stage < 3); if (!g_shaders.antialiasing_sh[stage]) { - char stage_define[32]; - BLI_snprintf(stage_define, sizeof(stage_define), "#define SMAA_STAGE %d\n", stage); - - g_shaders.antialiasing_sh[stage] = GPU_shader_create_from_arrays({ - .vert = - (const char *[]){ - "#define SMAA_INCLUDE_VS 1\n", - "#define SMAA_INCLUDE_PS 0\n", - "uniform vec4 viewportMetrics;\n", - datatoc_common_smaa_lib_glsl, - datatoc_gpencil_antialiasing_vert_glsl, - NULL, - }, - .frag = - (const char *[]){ - "#define SMAA_INCLUDE_VS 0\n", - "#define SMAA_INCLUDE_PS 1\n", - "uniform vec4 viewportMetrics;\n", - datatoc_common_smaa_lib_glsl, - datatoc_gpencil_antialiasing_frag_glsl, - NULL, - }, - .defs = - (const char *[]){ - "uniform float lumaWeight;\n", - "#define SMAA_GLSL_3\n", - "#define SMAA_RT_METRICS viewportMetrics\n", - "#define SMAA_PRESET_HIGH\n", - "#define SMAA_LUMA_WEIGHT float4(lumaWeight, lumaWeight, lumaWeight, 0.0)\n", - "#define SMAA_NO_DISCARD\n", - stage_define, - NULL, - }, - }); + char stage_info_name[32]; + SNPRINTF(stage_info_name, "gpencil_antialiasing_stage_%d", stage); + g_shaders.antialiasing_sh[stage] = GPU_shader_create_from_info_name(stage_info_name); } return g_shaders.antialiasing_sh[stage]; } diff --git a/source/blender/draw/engines/gpencil/shaders/gpencil_antialiasing_frag.glsl b/source/blender/draw/engines/gpencil/shaders/gpencil_antialiasing_frag.glsl index 7758fdceb46..bbea8747ed0 100644 --- a/source/blender/draw/engines/gpencil/shaders/gpencil_antialiasing_frag.glsl +++ b/source/blender/draw/engines/gpencil/shaders/gpencil_antialiasing_frag.glsl @@ -1,64 +1,43 @@ -uniform sampler2D edgesTex; -uniform sampler2D areaTex; -uniform sampler2D searchTex; -uniform sampler2D blendTex; -uniform sampler2D colorTex; -uniform sampler2D revealTex; -uniform bool onlyAlpha; -uniform bool doAntiAliasing; - -in vec2 uvs; -in vec2 pixcoord; -in vec4 offset[3]; - -#if SMAA_STAGE == 0 -out vec2 fragColor; -#elif SMAA_STAGE == 1 -out vec4 fragColor; -#elif SMAA_STAGE == 2 -/* Reminder: Blending func is `fragRevealage * DST + fragColor`. */ -layout(location = 0, index = 0) out vec4 outColor; -layout(location = 0, index = 1) out vec4 outReveal; -#endif +#pragma BLENDER_REQUIRE(common_smaa_lib.glsl) void main() { #if SMAA_STAGE == 0 /* Detect edges in color and revealage buffer. */ - fragColor = SMAALumaEdgeDetectionPS(uvs, offset, colorTex); - fragColor = max(fragColor, SMAALumaEdgeDetectionPS(uvs, offset, revealTex)); + out_edges = SMAALumaEdgeDetectionPS(uvs, offset, colorTex); + out_edges = max(out_edges, SMAALumaEdgeDetectionPS(uvs, offset, revealTex)); /* Discard if there is no edge. */ - if (dot(fragColor, float2(1.0, 1.0)) == 0.0) { + if (dot(out_edges, float2(1.0, 1.0)) == 0.0) { discard; } #elif SMAA_STAGE == 1 - fragColor = SMAABlendingWeightCalculationPS( + out_weights = SMAABlendingWeightCalculationPS( uvs, pixcoord, offset, edgesTex, areaTex, searchTex, vec4(0)); #elif SMAA_STAGE == 2 /* Resolve both buffers. */ if (doAntiAliasing) { - outColor = SMAANeighborhoodBlendingPS(uvs, offset[0], colorTex, blendTex); - outReveal = SMAANeighborhoodBlendingPS(uvs, offset[0], revealTex, blendTex); + out_color = SMAANeighborhoodBlendingPS(uvs, offset[0], colorTex, blendTex); + out_reveal = SMAANeighborhoodBlendingPS(uvs, offset[0], revealTex, blendTex); } else { - outColor = texture(colorTex, uvs); - outReveal = texture(revealTex, uvs); + out_color = texture(colorTex, uvs); + out_reveal = texture(revealTex, uvs); } /* Revealage, how much light passes through. */ /* Average for alpha channel. */ - outReveal.a = clamp(dot(outReveal.rgb, vec3(0.333334)), 0.0, 1.0); + out_reveal.a = clamp(dot(out_reveal.rgb, vec3(0.333334)), 0.0, 1.0); /* Color buf is already premultiplied. Just add it to the color. */ /* Add the alpha. */ - outColor.a = 1.0 - outReveal.a; + out_color.a = 1.0 - out_reveal.a; if (onlyAlpha) { /* Special case in wireframe xray mode. */ - outColor = vec4(0.0); - outReveal.rgb = outReveal.aaa; + out_color = vec4(0.0); + out_reveal.rgb = out_reveal.aaa; } #endif } diff --git a/source/blender/draw/engines/gpencil/shaders/gpencil_antialiasing_vert.glsl b/source/blender/draw/engines/gpencil/shaders/gpencil_antialiasing_vert.glsl index 07734d19972..b76433a23e5 100644 --- a/source/blender/draw/engines/gpencil/shaders/gpencil_antialiasing_vert.glsl +++ b/source/blender/draw/engines/gpencil/shaders/gpencil_antialiasing_vert.glsl @@ -1,7 +1,5 @@ -out vec2 uvs; -out vec2 pixcoord; -out vec4 offset[3]; +#pragma BLENDER_REQUIRE(common_smaa_lib.glsl) void main() { diff --git a/source/blender/draw/engines/gpencil/shaders/infos/gpencil_info.hh b/source/blender/draw/engines/gpencil/shaders/infos/gpencil_info.hh new file mode 100644 index 00000000000..748d4fd9a23 --- /dev/null +++ b/source/blender/draw/engines/gpencil/shaders/infos/gpencil_info.hh @@ -0,0 +1,58 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "gpu_shader_create_info.hh" + +/* -------------------------------------------------------------------- */ +/** \name Anti-Aliasing + * \{ */ + +GPU_SHADER_INTERFACE_INFO(gpencil_antialiasing_iface, "") + .smooth(Type::VEC2, "uvs") + .smooth(Type::VEC2, "pixcoord") + .smooth(Type::VEC4, "offset[3]"); + +GPU_SHADER_CREATE_INFO(gpencil_antialiasing) + .define("SMAA_GLSL_3") + .define("SMAA_RT_METRICS", "viewportMetrics") + .define("SMAA_PRESET_HIGH") + .define("SMAA_LUMA_WEIGHT", "float4(lumaWeight, lumaWeight, lumaWeight, 0.0)") + .define("SMAA_NO_DISCARD") + .vertex_out(gpencil_antialiasing_iface) + .push_constant(Type::VEC4, "viewportMetrics") + .push_constant(Type::FLOAT, "lumaWeight") + .vertex_source("gpencil_antialiasing_vert.glsl") + .fragment_source("gpencil_antialiasing_frag.glsl"); + +GPU_SHADER_CREATE_INFO(gpencil_antialiasing_stage_0) + .define("SMAA_STAGE", "0") + .sampler(0, ImageType::FLOAT_2D, "colorTex") + .sampler(1, ImageType::FLOAT_2D, "revealTex") + .fragment_out(0, Type::VEC2, "out_edges") + .additional_info("gpencil_antialiasing") + .do_static_compilation(true); + +GPU_SHADER_CREATE_INFO(gpencil_antialiasing_stage_1) + .define("SMAA_STAGE", "1") + .sampler(0, ImageType::FLOAT_2D, "edgesTex") + .sampler(1, ImageType::FLOAT_2D, "areaTex") + .sampler(2, ImageType::FLOAT_2D, "searchTex") + .fragment_out(0, Type::VEC4, "out_weights") + .additional_info("gpencil_antialiasing") + .do_static_compilation(true); + +GPU_SHADER_CREATE_INFO(gpencil_antialiasing_stage_2) + .define("SMAA_STAGE", "2") + .sampler(0, ImageType::FLOAT_2D, "colorTex") + .sampler(1, ImageType::FLOAT_2D, "revealTex") + .sampler(2, ImageType::FLOAT_2D, "blendTex") + .push_constant(Type::FLOAT, "mixFactor") + .push_constant(Type::FLOAT, "taaAccumulatedWeight") + .push_constant(Type::BOOL, "doAntiAliasing") + .push_constant(Type::BOOL, "onlyAlpha") + /* Reminder: Blending func is `fragRevealage * DST + fragColor`. */ + .fragment_out(0, Type::VEC4, "out_color", DualBlend::SRC_0) + .fragment_out(0, Type::VEC4, "out_reveal", DualBlend::SRC_1) + .additional_info("gpencil_antialiasing") + .do_static_compilation(true); + +/** \} */ diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt index 1fa166a207c..45e614991ca 100644 --- a/source/blender/gpu/CMakeLists.txt +++ b/source/blender/gpu/CMakeLists.txt @@ -388,6 +388,7 @@ list(APPEND SRC ${glsl_source_list_file}) list(APPEND INC ${CMAKE_CURRENT_BINARY_DIR}) set(SRC_SHADER_CREATE_INFOS + ../draw/engines/gpencil/shaders/infos/gpencil_info.hh ../draw/engines/gpencil/shaders/infos/gpencil_vfx_info.hh ../draw/engines/workbench/shaders/infos/workbench_composite_info.hh ../draw/engines/workbench/shaders/infos/workbench_effect_antialiasing_info.hh |