[Bf-blender-cvs] [4bf0c9793af] tmp-overlay-engine: GPU: Remove The use of geom shader for dashed lines
Clément Foucault
noreply at git.blender.org
Wed Nov 20 20:54:25 CET 2019
Commit: 4bf0c9793af2615ce0e9734cce18b9e92a46a271
Author: Clément Foucault
Date: Wed Nov 20 02:47:58 2019 +0100
Branches: tmp-overlay-engine
https://developer.blender.org/rB4bf0c9793af2615ce0e9734cce18b9e92a46a271
GPU: Remove The use of geom shader for dashed lines
Use the same trick used in overlay engine.
Also fixed some undefined behavior with missing viewport uniform in some
calls.
===================================================================
M source/blender/editors/space_sequencer/sequencer_draw.c
M source/blender/editors/transform/transform_snap.c
M source/blender/gpu/CMakeLists.txt
M source/blender/gpu/intern/gpu_shader.c
M source/blender/gpu/shaders/gpu_shader_2D_line_dashed_frag.glsl
D source/blender/gpu/shaders/gpu_shader_2D_line_dashed_geom.glsl
M source/blender/gpu/shaders/gpu_shader_2D_line_dashed_uniform_color_vert.glsl
D source/blender/gpu/shaders/gpu_shader_2D_line_dashed_width_geom.glsl
D source/blender/gpu/shaders/gpu_shader_3D_line_dashed_uniform_color_legacy_vert.glsl
M source/blender/gpu/shaders/gpu_shader_3D_line_dashed_uniform_color_vert.glsl
===================================================================
diff --git a/source/blender/editors/space_sequencer/sequencer_draw.c b/source/blender/editors/space_sequencer/sequencer_draw.c
index 5b2ea9d4793..5ca4ae7b666 100644
--- a/source/blender/editors/space_sequencer/sequencer_draw.c
+++ b/source/blender/editors/space_sequencer/sequencer_draw.c
@@ -2087,8 +2087,10 @@ void draw_timeline_seq(const bContext *C, ARegion *ar)
scene->r.cfra + scene->ed->over_ofs;
uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
- immBindBuiltinProgram(GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR);
-
+ immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
+ float viewport_size[4];
+ GPU_viewport_size_get_f(viewport_size);
+ immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
immUniform1f("dash_width", 20.0f * U.pixelsize);
immUniform1f("dash_factor", 0.5f);
immUniformThemeColor(TH_CFRAME);
diff --git a/source/blender/editors/transform/transform_snap.c b/source/blender/editors/transform/transform_snap.c
index 15208c1a7d2..99429db6af7 100644
--- a/source/blender/editors/transform/transform_snap.c
+++ b/source/blender/editors/transform/transform_snap.c
@@ -256,6 +256,9 @@ void drawSnapping(const struct bContext *C, TransInfo *t)
immUnbindProgram();
immBindBuiltinProgram(GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR);
+ float viewport_size[4];
+ GPU_viewport_size_get_f(viewport_size);
+ immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
immUniform1f("dash_width", 6.0f * U.pixelsize);
immUniform1f("dash_factor", 1.0f / 4.0f);
immUniformColor4ubv(col);
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index 63b51215d50..f889ce7cff5 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -158,7 +158,6 @@ data_to_c_simple(shaders/gpu_shader_2D_nodelink_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_2D_flat_color_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_2D_line_dashed_uniform_color_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_2D_line_dashed_frag.glsl SRC)
-data_to_c_simple(shaders/gpu_shader_2D_line_dashed_geom.glsl SRC)
data_to_c_simple(shaders/gpu_shader_2D_smooth_color_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_2D_smooth_color_frag.glsl SRC)
data_to_c_simple(shaders/gpu_shader_2D_smooth_color_dithered_frag.glsl SRC)
diff --git a/source/blender/gpu/intern/gpu_shader.c b/source/blender/gpu/intern/gpu_shader.c
index 3fe52d03b88..d323c1b74e4 100644
--- a/source/blender/gpu/intern/gpu_shader.c
+++ b/source/blender/gpu/intern/gpu_shader.c
@@ -128,7 +128,6 @@ extern char datatoc_gpu_shader_2D_edituvs_stretch_vert_glsl[];
extern char datatoc_gpu_shader_2D_line_dashed_uniform_color_vert_glsl[];
extern char datatoc_gpu_shader_2D_line_dashed_frag_glsl[];
-extern char datatoc_gpu_shader_2D_line_dashed_geom_glsl[];
extern char datatoc_gpu_shader_3D_line_dashed_uniform_color_vert_glsl[];
extern char datatoc_gpu_shader_text_vert_glsl[];
@@ -1095,13 +1094,11 @@ static const GPUShaderStages builtin_shader_stages[GPU_SHADER_BUILTIN_LEN] = {
[GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR] =
{
.vert = datatoc_gpu_shader_2D_line_dashed_uniform_color_vert_glsl,
- .geom = datatoc_gpu_shader_2D_line_dashed_geom_glsl,
.frag = datatoc_gpu_shader_2D_line_dashed_frag_glsl,
},
[GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR] =
{
.vert = datatoc_gpu_shader_3D_line_dashed_uniform_color_vert_glsl,
- .geom = datatoc_gpu_shader_2D_line_dashed_geom_glsl,
.frag = datatoc_gpu_shader_2D_line_dashed_frag_glsl,
},
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 a29335046f2..44a9db76834 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
@@ -17,13 +17,16 @@ uniform float dash_factor; /* if > 1.0, solid line. */
uniform int colors_len; /* Enabled if > 0, 1 for solid line. */
uniform vec4 colors[32];
-noperspective in float distance_along_line;
-noperspective in vec4 color_geom;
+flat in vec4 color_vert;
+
+noperspective in vec2 stipple_pos;
+flat in vec2 stipple_start;
out vec4 fragColor;
void main()
{
+ float distance_along_line = distance(stipple_pos, stipple_start);
/* Multi-color option. */
if (colors_len > 0) {
/* Solid line case, simple. */
@@ -40,13 +43,13 @@ void main()
else {
/* Solid line case, simple. */
if (dash_factor >= 1.0f) {
- fragColor = color_geom;
+ fragColor = color_vert;
}
/* Actually dashed line... */
else {
float normalized_distance = fract(distance_along_line / dash_width);
if (normalized_distance <= dash_factor) {
- fragColor = color_geom;
+ fragColor = color_vert;
}
else {
discard;
diff --git a/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_geom.glsl b/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_geom.glsl
deleted file mode 100644
index 584a179a9ac..00000000000
--- a/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_geom.glsl
+++ /dev/null
@@ -1,65 +0,0 @@
-
-/*
- * Geometry Shader for dashed lines, with uniform multi-color(s),
- * or any single-color, and unary thickness.
- *
- * Dashed is performed in screen space.
- */
-
-/* Make to be used with dynamic batching so no Model Matrix needed */
-uniform mat4 ModelViewProjectionMatrix;
-uniform vec2 viewport_size;
-
-/* Uniforms from fragment shader,
- * used here to optimize out useless computation in case of solid line. */
-uniform float dash_factor; /* if > 1.0, solid line. */
-uniform int colors_len; /* Enabled if > 0, 1 for solid line. */
-
-layout(lines) in;
-
-in vec4 color_vert[];
-
-layout(line_strip, max_vertices = 2) out;
-noperspective out float distance_along_line;
-noperspective out vec4 color_geom;
-
-void main()
-{
- vec4 v1 = gl_in[0].gl_Position;
- vec4 v2 = gl_in[1].gl_Position;
-
- gl_Position = v1;
- color_geom = color_vert[0];
- distance_along_line = 0.0f;
-
-#ifdef USE_WORLD_CLIP_PLANES
- world_clip_planes_set_clip_distance(gl_in[0].gl_ClipDistance);
-#endif
- EmitVertex();
-
- gl_Position = v2;
- color_geom = color_vert[1];
- if ((colors_len == 1) || (dash_factor >= 1.0f)) {
- /* Solid line, optimize out distance computation! */
- distance_along_line = 0.0f;
- }
- else {
- vec2 p1 = (v1.xy / v1.w) * 0.5 + 0.5; // <- device coordinates in [0..1] range.
- p1 = p1 * viewport_size; // <- 'virtual' screen coordinates.
-
- vec2 p2 = (v2.xy / v2.w) * 0.5 + 0.5; // <- device coordinates in [0..1] range.
- p2 = p2 * viewport_size; // <- 'virtual' screen coordinates.
-
- distance_along_line = distance(p1, p2);
- }
-
-#ifdef USE_WORLD_CLIP_PLANES
- world_clip_planes_set_clip_distance(gl_in[1].gl_ClipDistance);
-#endif
- EmitVertex();
-
- EndPrimitive();
-
- /* Note: we could also use similar approach as diag_stripes_frag,
- * but this would give us dashed 'anchored' to the screen, and not to one end of the line... */
-}
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 8b5a5cd8c9a..15362d020e4 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
@@ -9,13 +9,19 @@
uniform mat4 ModelViewProjectionMatrix;
uniform vec4 color;
+uniform vec2 viewport_size;
in vec2 pos;
-out vec4 color_vert;
+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 */
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_2D_line_dashed_width_geom.glsl b/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_width_geom.glsl
deleted file mode 100644
index 336f310837e..00000000000
--- a/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_width_geom.glsl
+++ /dev/null
@@ -1,60 +0,0 @@
-
-// Draw dashed lines, perforated in screen space, with non-unary width.
-
-/* Make to be used with dynamic batching so no Model Matrix needed */
-uniform mat4 ModelViewProjectionMatrix;
-uniform vec2 viewport_size;
-
-/* Width of the generated 'line'. */
-uniform float width; /* in pixels, screen space. */
-
-/* Uniforms from fragment shader,
- * used here to optimize out useless computation in case of solid line. */
-uniform float dash_factor; /* if > 1.0, solid line. */
-uniform int colors_len; /* Enabled if > 0, 1 for solid line. */
-
-layout(lines) in;
-
-layout(triangle_strip, max_vertices = 4) out;
-noperspective out float distance_along_line;
-
-void main()
-{
- vec4 v1 = gl_in[0].gl_Position;
- vec4 v2 = gl_in[1].gl_Position;
-
- /* Width, from 2D screen space in pixels, to ModelViewProjection space of each input vertices. */
- float w1 = (width / viewport_size) * v1.w * 2.0;
- float w2 = (width / viewport_size) * v2.w * 2.0;
-
- /* Normalized vector parallel to screen and orthogonal to line. */
- vec4 wdir = normalize(vec4(v1.y - v2.y, v2.x - v1.x, 0.0, 0.0))
-
- distance_along_line = 0.0f;
- gl_Position = v1 + (wdir * w1);
- EmitVertex();
-
- gl_Position = v1 - (wdir * w1);
- EmitVertex();
-
- if ((colors_len == 1) || (dash_factor >= 1.0f)) {
- /* Solid line, optimize out distance computation! */
- distance_along_line = 0.0f;
- }
- else {
- vec2 p1 = (v1.xy / v1.w) * 0.5 + 0.5; // <- device coordinates in [0..1] range.
- p1 = p1 * viewport_size; // <- 'virtual' screen coordinates.
-
- vec2 p2 = (v2.xy / v2.w) * 0.5 + 0.5; // <- device coordinates
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list