diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2022-05-02 01:35:49 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2022-05-02 01:35:49 +0300 |
commit | 0676963809a4800f1f666cd559c30885d79f66b8 (patch) | |
tree | d7971e9bee7a62e8088f97cfcd475393e457f581 /source/blender/gpu | |
parent | 2a7a01b339ad60aec5ffe265411fa2f0b1589137 (diff) |
GPUShader: Port dashed line shaders to use shaderCreateInfo
This should have no functional changes.
This reduce the complexity of the shader by only supporting 2 colors.
We never use more than 2 color in practice and this makes usage not require
a UBO.
Diffstat (limited to 'source/blender/gpu')
7 files changed, 65 insertions, 109 deletions
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt index 3cb89f0f233..9c1ffe617cf 100644 --- a/source/blender/gpu/CMakeLists.txt +++ b/source/blender/gpu/CMakeLists.txt @@ -503,6 +503,7 @@ set(SRC_SHADER_CREATE_INFOS shaders/infos/gpu_shader_gpencil_stroke_info.hh shaders/infos/gpu_shader_instance_varying_color_varying_size_info.hh shaders/infos/gpu_shader_keyframe_shape_info.hh + shaders/infos/gpu_shader_line_dashed_uniform_color_info.hh shaders/infos/gpu_shader_simple_lighting_info.hh shaders/infos/gpu_shader_text_info.hh shaders/infos/gpu_srgb_to_framebuffer_space_info.hh diff --git a/source/blender/gpu/intern/gpu_shader_builtin.c b/source/blender/gpu/intern/gpu_shader_builtin.c index b0e11d85fb9..0463262b779 100644 --- a/source/blender/gpu/intern/gpu_shader_builtin.c +++ b/source/blender/gpu/intern/gpu_shader_builtin.c @@ -238,18 +238,13 @@ static const GPUShaderStages builtin_shader_stages[GPU_SHADER_BUILTIN_LEN] = { [GPU_SHADER_3D_POLYLINE_SMOOTH_COLOR] = {.name = "GPU_SHADER_3D_POLYLINE_SMOOTH_COLOR", .create_info = "gpu_shader_3D_polyline_smooth_color"}, - [GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR] = - { - .name = "GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR", - .vert = datatoc_gpu_shader_2D_line_dashed_uniform_color_vert_glsl, - .frag = datatoc_gpu_shader_2D_line_dashed_frag_glsl, - }, + [GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR] = {.name = "GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR", + .create_info = + "gpu_shader_2D_line_dashed_uniform_color"}, [GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR] = - { - .name = "GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR", - .vert = datatoc_gpu_shader_3D_line_dashed_uniform_color_vert_glsl, - .frag = datatoc_gpu_shader_2D_line_dashed_frag_glsl, - }, + {.name = "GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR", + .create_info = "gpu_shader_3D_line_dashed_uniform_color", + .clipped_create_info = "gpu_shader_3D_line_dashed_uniform_color_clipped"}, [GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA] = { diff --git a/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_frag.glsl b/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_frag.glsl index af9a24d1280..1ddffaef16e 100644 --- a/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_frag.glsl +++ b/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_frag.glsl @@ -6,57 +6,24 @@ * Dashed is performed in screen space. */ -#ifndef USE_GPU_SHADER_CREATE_INFO - -uniform float dash_width; - -/* Simple mode, discarding non-dash parts (so no need for blending at all). */ -uniform float dash_factor; /* if > 1.0, solid line. */ - -/* More advanced mode, allowing for complex, multi-colored patterns. - * Enabled when colors_len > 0. */ -/* NOTE: max number of steps/colors in pattern is 32! */ -uniform int colors_len; /* Enabled if > 0, 1 for solid line. */ -uniform vec4 colors[32]; - -flat in vec4 color_vert; - -noperspective in vec2 stipple_pos; -flat in vec2 stipple_start; - -out vec4 fragColor; -#endif - void main() { float distance_along_line = distance(stipple_pos, stipple_start); - /* Multi-color option. */ - if (colors_len > 0) { - /* Solid line case, simple. */ - if (colors_len == 1) { - fragColor = colors[0]; - } - /* Actually dashed line... */ - else { - float normalized_distance = fract(distance_along_line / dash_width); - fragColor = colors[int(normalized_distance * colors_len)]; - } + /* Solid line case, simple. */ + if (dash_factor >= 1.0f) { + fragColor = color; } - /* Single color option. */ + /* Actually dashed line... */ else { - /* Solid line case, simple. */ - if (dash_factor >= 1.0f) { - fragColor = color_vert; + float normalized_distance = fract(distance_along_line / dash_width); + if (normalized_distance <= dash_factor) { + fragColor = color; + } + else if (colors_len > 0) { + fragColor = color2; } - /* Actually dashed line... */ else { - float normalized_distance = fract(distance_along_line / dash_width); - if (normalized_distance <= dash_factor) { - fragColor = color_vert; - } - else { - discard; - } + discard; } } } diff --git a/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_uniform_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_uniform_color_vert.glsl index 9d52820188b..7878dc18362 100644 --- a/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_uniform_color_vert.glsl +++ b/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_uniform_color_vert.glsl @@ -5,24 +5,9 @@ * * Dashed is performed in screen space. */ -#ifndef USE_GPU_SHADER_CREATE_INFO -uniform mat4 ModelViewProjectionMatrix; - -uniform vec4 color; -uniform vec2 viewport_size; - -in vec2 pos; - -flat out vec4 color_vert; - -/* We leverage hardware interpolation to compute distance along the line. */ -noperspective out vec2 stipple_pos; /* In screen space */ -flat out vec2 stipple_start; /* In screen space */ -#endif void main() { gl_Position = ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0); stipple_start = stipple_pos = viewport_size * 0.5 * (gl_Position.xy / gl_Position.w); - color_vert = color; } diff --git a/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_uniform_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_uniform_color_vert.glsl index 9328e0a13e9..68835cb1d0f 100644 --- a/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_uniform_color_vert.glsl +++ b/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_uniform_color_vert.glsl @@ -1,3 +1,4 @@ +#pragma BLENDER_REQUIRE(gpu_shader_cfg_world_clip_lib.glsl) /* * Vertex Shader for dashed lines with 3D coordinates, @@ -6,32 +7,11 @@ * Dashed is performed in screen space. */ -#ifndef USE_GPU_SHADER_CREATE_INFO - -uniform mat4 ModelViewProjectionMatrix; - -# ifdef USE_WORLD_CLIP_PLANES -uniform mat4 ModelMatrix; -# endif - -uniform vec4 color; -uniform vec2 viewport_size; - -in vec3 pos; - -flat out vec4 color_vert; - -/* We leverage hardware interpolation to compute distance along the line. */ -noperspective out vec2 stipple_pos; /* In screen space */ -flat out vec2 stipple_start; /* In screen space */ -#endif - void main() { vec4 pos_4d = vec4(pos, 1.0); gl_Position = ModelViewProjectionMatrix * pos_4d; stipple_start = stipple_pos = viewport_size * 0.5 * (gl_Position.xy / gl_Position.w); - color_vert = color; #ifdef USE_WORLD_CLIP_PLANES world_clip_planes_calc_clip_distance((ModelMatrix * pos_4d).xyz); #endif diff --git a/source/blender/gpu/shaders/infos/gpu_shader_2D_line_dashed_uniform_color_info.hh b/source/blender/gpu/shaders/infos/gpu_shader_2D_line_dashed_uniform_color_info.hh deleted file mode 100644 index 81ba8a26213..00000000000 --- a/source/blender/gpu/shaders/infos/gpu_shader_2D_line_dashed_uniform_color_info.hh +++ /dev/null @@ -1,18 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later - * Copyright 2022 Blender Foundation. All rights reserved. */ - -/** \file - * \ingroup gpu - */ - -#include "gpu_interface_info.hh" -#include "gpu_shader_create_info.hh" - -/* TODO(jbakker): Skipped as data doesn't fit as push constant. */ -GPU_SHADER_CREATE_INFO(gpu_shader_2D_line_dashed_uniform_color) - .vertex_in(0, Type::VEC3, "pos") - .vertex_out(flat_color_iface) - .push_constant(Type::MAT4, "ModelViewProjectionMatrix") - .vertex_source("gpu_shader_2D_line_dashed_uniform_color_vert.glsl") - .fragment_source("gpu_shader_2D_line_dashed_frag.glsl") - .do_static_compilation(true); diff --git a/source/blender/gpu/shaders/infos/gpu_shader_line_dashed_uniform_color_info.hh b/source/blender/gpu/shaders/infos/gpu_shader_line_dashed_uniform_color_info.hh new file mode 100644 index 00000000000..57cb02c8484 --- /dev/null +++ b/source/blender/gpu/shaders/infos/gpu_shader_line_dashed_uniform_color_info.hh @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later + * Copyright 2022 Blender Foundation. All rights reserved. */ + +/** \file + * \ingroup gpu + */ + +#include "gpu_interface_info.hh" +#include "gpu_shader_create_info.hh" + +/* We leverage hardware interpolation to compute distance along the line. */ +GPU_SHADER_INTERFACE_INFO(gpu_shader_line_dashed_interface, "") + .no_perspective(Type::VEC2, "stipple_start") /* In screen space */ + .flat(Type::VEC2, "stipple_pos"); /* In screen space */ + +GPU_SHADER_CREATE_INFO(gpu_shader_line_dashed) + .vertex_out(flat_color_iface) + .push_constant(Type::MAT4, "ModelViewProjectionMatrix") + .push_constant(Type::VEC2, "viewport_size") + .push_constant(Type::FLOAT, "dash_width") + .push_constant(Type::FLOAT, "dash_factor") /* if > 1.0, solid line. */ + /* TODO(fclem): Remove this. And decide to discard if color2 alpha is 0. */ + .push_constant(Type::INT, "colors_len") /* Enabled if > 0, 1 for solid line. */ + .push_constant(Type::VEC4, "color") + .push_constant(Type::VEC4, "color2") + .vertex_out(gpu_shader_line_dashed_interface) + .fragment_out(0, Type::VEC4, "fragColor") + .fragment_source("gpu_shader_2D_line_dashed_frag.glsl"); + +GPU_SHADER_CREATE_INFO(gpu_shader_2D_line_dashed_uniform_color) + .vertex_in(0, Type::VEC2, "pos") + .vertex_source("gpu_shader_2D_line_dashed_uniform_color_vert.glsl") + .additional_info("gpu_shader_line_dashed") + .do_static_compilation(true); + +GPU_SHADER_CREATE_INFO(gpu_shader_3D_line_dashed_uniform_color) + .vertex_in(0, Type::VEC3, "pos") + .vertex_source("gpu_shader_3D_line_dashed_uniform_color_vert.glsl") + .additional_info("gpu_shader_line_dashed") + .do_static_compilation(true); + +GPU_SHADER_CREATE_INFO(gpu_shader_3D_line_dashed_uniform_color_clipped) + .push_constant(Type::MAT4, "ModelMatrix") + .additional_info("gpu_shader_3D_line_dashed_uniform_color") + .additional_info("gpu_clip_planes") + .do_static_compilation(true); |