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.
This commit is contained in:
2022-05-02 00:35:49 +02:00
parent 2a7a01b339
commit 0676963809
14 changed files with 94 additions and 141 deletions

View File

@@ -1135,8 +1135,8 @@ static void ui_draw_colorband_handle(uint shdr_pos,
immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC);
immUniform1i("colors_len", 2); /* "advanced" mode */
immUniformArray4fv(
"colors", (float *)(float[][4]){{0.8f, 0.8f, 0.8f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f}}, 2);
immUniform4f("color", 0.8f, 0.8f, 0.8f, 1.0f);
immUniform4f("color2", 0.0f, 0.0f, 0.0f, 1.0f);
immUniform1f("dash_width", active ? 4.0f : 2.0f);
immUniform1f("dash_factor", 0.5f);

View File

@@ -464,12 +464,12 @@ static void mask_draw_curve_type(const bContext *C,
break;
case MASK_DT_DASH: {
float colors[8];
float colors[2][4];
mask_color_active_tint(rgb_tmp, rgb_spline, is_active);
rgba_uchar_to_float(colors, rgb_tmp);
rgba_uchar_to_float(colors[0], rgb_tmp);
mask_color_active_tint(rgb_tmp, rgb_black, is_active);
rgba_uchar_to_float(colors + 4, rgb_tmp);
rgba_uchar_to_float(colors[1], rgb_tmp);
immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
@@ -478,7 +478,8 @@ static void mask_draw_curve_type(const bContext *C,
immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC);
immUniform1i("colors_len", 2); /* "advanced" mode */
immUniformArray4fv("colors", colors, 2);
immUniform4fv("color", colors[0]);
immUniform4fv("color2", colors[1]);
immUniform1f("dash_width", 4.0f);
immUniform1f("dash_factor", 0.5f);
GPU_line_width(1.0f);

View File

@@ -172,8 +172,8 @@ static void paint_draw_line_cursor(bContext *C, int x, int y, void *customdata)
immUniform1i("colors_len", 2); /* "advanced" mode */
const float alpha = (float)paint->paint_cursor_col[3] / 255.0f;
immUniformArray4fv(
"colors", (float *)(float[][4]){{0.0f, 0.0f, 0.0f, alpha}, {1.0f, 1.0f, 1.0f, alpha}}, 2);
immUniform4f("color", 0.0f, 0.0f, 0.0f, alpha);
immUniform4f("color2", 1.0f, 1.0f, 1.0f, alpha);
immUniform1f("dash_width", 6.0f);
immUniform1f("dash_factor", 0.5f);

View File

@@ -455,8 +455,8 @@ void draw_image_sample_line(SpaceImage *sima)
immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC);
immUniform1i("colors_len", 2); /* Advanced dashes. */
immUniformArray4fv(
"colors", (float *)(float[][4]){{1.0f, 1.0f, 1.0f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f}}, 2);
immUniform4f("color", 1.0f, 1.0f, 1.0f, 1.0f);
immUniform4f("color2", 0.0f, 0.0f, 0.0f, 1.0f);
immUniform1f("dash_width", 2.0f);
immUniform1f("dash_factor", 0.5f);

View File

@@ -689,10 +689,8 @@ static void gizmo_ruler_draw(const bContext *C, wmGizmo *gz)
immUniform1i("colors_len", 2); /* "advanced" mode */
const float *col = is_act ? color_act : color_base;
immUniformArray4fv(
"colors",
(float *)(float[][4]){{0.67f, 0.67f, 0.67f, 1.0f}, {col[0], col[1], col[2], col[3]}},
2);
immUniform4f("color", 0.67f, 0.67f, 0.67f, 1.0f);
immUniform4f("color2", col[0], col[1], col[2], col[3]);
immUniform1f("dash_width", 6.0f);
immUniform1f("dash_factor", 0.5f);
@@ -760,10 +758,8 @@ static void gizmo_ruler_draw(const bContext *C, wmGizmo *gz)
immUniform1i("colors_len", 2); /* "advanced" mode */
const float *col = is_act ? color_act : color_base;
immUniformArray4fv(
"colors",
(float *)(float[][4]){{0.67f, 0.67f, 0.67f, 1.0f}, {col[0], col[1], col[2], col[3]}},
2);
immUniform4f("color", 0.67f, 0.67f, 0.67f, 1.0f);
immUniform4f("color2", col[0], col[1], col[2], col[3]);
immUniform1f("dash_width", 6.0f);
immUniform1f("dash_factor", 0.5f);

View File

@@ -46,8 +46,8 @@ void ED_image_draw_cursor(ARegion *region, const float cursor[2])
immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC);
immUniform1i("colors_len", 2); /* "advanced" mode */
immUniformArray4fv(
"colors", (float *)(float[][4]){{1.0f, 0.0f, 0.0f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
immUniform4f("color", 1.0f, 0.0f, 0.0f, 1.0f);
immUniform4f("color2", 1.0f, 1.0f, 1.0f, 1.0f);
immUniform1f("dash_width", 8.0f);
immUniform1f("dash_factor", 0.5f);
@@ -67,8 +67,8 @@ void ED_image_draw_cursor(ARegion *region, const float cursor[2])
immEnd();
immUniformArray4fv(
"colors", (float *)(float[][4]){{1.0f, 1.0f, 1.0f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f}}, 2);
immUniform4f("color", 1.0f, 1.0f, 1.0f, 1.0f);
immUniform4f("color2", 0.0f, 0.0f, 0.0f, 1.0f);
immUniform1f("dash_width", 2.0f);
immUniform1f("dash_factor", 0.5f);

View File

@@ -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

View File

@@ -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] =
{

View File

@@ -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;
}
}
}

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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);

View File

@@ -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);

View File

@@ -182,8 +182,8 @@ static void wm_gesture_draw_line(wmGesture *gt)
immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
immUniform1i("colors_len", 2); /* "advanced" mode */
immUniformArray4fv(
"colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
immUniform4f("color", 0.4f, 0.4f, 0.4f, 1.0f);
immUniform4f("color2", 1.0f, 1.0f, 1.0f, 1.0f);
immUniform1f("dash_width", 8.0f);
immUniform1f("dash_factor", 0.5f);
@@ -225,8 +225,8 @@ static void wm_gesture_draw_rect(wmGesture *gt)
immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
immUniform1i("colors_len", 2); /* "advanced" mode */
immUniformArray4fv(
"colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
immUniform4f("color", 0.4f, 0.4f, 0.4f, 1.0f);
immUniform4f("color2", 1.0f, 1.0f, 1.0f, 1.0f);
immUniform1f("dash_width", 8.0f);
immUniform1f("dash_factor", 0.5f);
@@ -264,8 +264,8 @@ static void wm_gesture_draw_circle(wmGesture *gt)
immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
immUniform1i("colors_len", 2); /* "advanced" mode */
immUniformArray4fv(
"colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
immUniform4f("color", 0.4f, 0.4f, 0.4f, 1.0f);
immUniform4f("color2", 1.0f, 1.0f, 1.0f, 1.0f);
immUniform1f("dash_width", 4.0f);
immUniform1f("dash_factor", 0.5f);
@@ -368,8 +368,8 @@ static void wm_gesture_draw_lasso(wmGesture *gt, bool filled)
immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
immUniform1i("colors_len", 2); /* "advanced" mode */
immUniformArray4fv(
"colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
immUniform4f("color", 0.4f, 0.4f, 0.4f, 1.0f);
immUniform4f("color2", 1.0f, 1.0f, 1.0f, 1.0f);
immUniform1f("dash_width", 2.0f);
immUniform1f("dash_factor", 0.5f);
@@ -402,8 +402,8 @@ static void wm_gesture_draw_cross(wmWindow *win, wmGesture *gt)
immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
immUniform1i("colors_len", 2); /* "advanced" mode */
immUniformArray4fv(
"colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
immUniform4f("color", 0.4f, 0.4f, 0.4f, 1.0f);
immUniform4f("color2", 1.0f, 1.0f, 1.0f, 1.0f);
immUniform1f("dash_width", 8.0f);
immUniform1f("dash_factor", 0.5f);