Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClément Foucault <foucault.clem@gmail.com>2020-04-24 00:05:39 +0300
committerClément Foucault <foucault.clem@gmail.com>2020-04-24 00:07:48 +0300
commitd712f1f83af881be536ec0d183b7d3025c172684 (patch)
treeeabecb450ce2d4697468d2758df28fd990c14ed6 /source/blender/gpu
parentd0ff3434cffa2e056e4f191ead21226f32ea8c15 (diff)
GPU: Add Polyline shader (wide line emulation)
This new shader is able to emulate smooth wide lines drawing using a geometry shader. This shader needs viewportSize and lineWidth uniforms to be set. There is multiple variants to replace the usage of wide lines for most shaders. This patch only fix the gizmo_types files and the navigation gizmo. Other areas could be fixed afterward, I just limited the patch size. Fix T57570. Reviewed By: billreynish Differential Revision: https://developer.blender.org/D7487
Diffstat (limited to 'source/blender/gpu')
-rw-r--r--source/blender/gpu/CMakeLists.txt3
-rw-r--r--source/blender/gpu/GPU_shader.h28
-rw-r--r--source/blender/gpu/intern/gpu_shader.c33
-rw-r--r--source/blender/gpu/shaders/gpu_shader_3D_polyline_frag.glsl24
-rw-r--r--source/blender/gpu/shaders/gpu_shader_3D_polyline_geom.glsl68
-rw-r--r--source/blender/gpu/shaders/gpu_shader_3D_polyline_vert.glsl28
6 files changed, 183 insertions, 1 deletions
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index 85e81607225..1d6a5031d7e 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -181,6 +181,9 @@ data_to_c_simple(shaders/gpu_shader_3D_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_3D_normal_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_3D_flat_color_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_3D_line_dashed_uniform_color_vert.glsl SRC)
+data_to_c_simple(shaders/gpu_shader_3D_polyline_frag.glsl SRC)
+data_to_c_simple(shaders/gpu_shader_3D_polyline_geom.glsl SRC)
+data_to_c_simple(shaders/gpu_shader_3D_polyline_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_3D_smooth_color_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_3D_smooth_color_frag.glsl SRC)
data_to_c_simple(shaders/gpu_shader_3D_passthrough_vert.glsl SRC)
diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h
index 1fb0b7c1f6b..dd64b858b35 100644
--- a/source/blender/gpu/GPU_shader.h
+++ b/source/blender/gpu/GPU_shader.h
@@ -156,6 +156,7 @@ typedef enum eGPUBuiltinShader {
* \param pos: in vec3
*/
GPU_SHADER_3D_UNIFORM_COLOR,
+ GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR,
/**
* Take a 3D position and color for each vertex without color interpolation.
*
@@ -171,12 +172,37 @@ typedef enum eGPUBuiltinShader {
*/
GPU_SHADER_3D_SMOOTH_COLOR,
/**
+ * Take a single color for all the vertices and a 3D position for each vertex.
+ * Used for drawing wide lines.
+ *
+ * \param color: uniform vec4
+ * \param pos: in vec3
+ */
+ GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR,
+ GPU_SHADER_3D_POLYLINE_CLIPPED_UNIFORM_COLOR,
+ /**
+ * Take a 3D position and color for each vertex without color interpolation.
+ * Used for drawing wide lines.
+ *
+ * \param color: in vec4
+ * \param pos: in vec3
+ */
+ GPU_SHADER_3D_POLYLINE_FLAT_COLOR,
+ /**
+ * Take a 3D position and color for each vertex with perspective correct interpolation.
+ * Used for drawing wide lines.
+ *
+ * \param color: in vec4
+ * \param pos: in vec3
+ */
+ GPU_SHADER_3D_POLYLINE_SMOOTH_COLOR,
+ /**
* Take a 3D position for each vertex and output only depth.
+ * Used for drawing wide lines.
*
* \param pos: in vec3
*/
GPU_SHADER_3D_DEPTH_ONLY,
- GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR,
/* basic image drawing */
GPU_SHADER_2D_IMAGE_OVERLAYS_MERGE,
GPU_SHADER_2D_IMAGE_OVERLAYS_STEREO_MERGE,
diff --git a/source/blender/gpu/intern/gpu_shader.c b/source/blender/gpu/intern/gpu_shader.c
index 3c2266bd65d..8dfc992ae9f 100644
--- a/source/blender/gpu/intern/gpu_shader.c
+++ b/source/blender/gpu/intern/gpu_shader.c
@@ -88,6 +88,9 @@ extern char datatoc_gpu_shader_image_modulate_alpha_frag_glsl[];
extern char datatoc_gpu_shader_3D_vert_glsl[];
extern char datatoc_gpu_shader_3D_normal_vert_glsl[];
extern char datatoc_gpu_shader_3D_flat_color_vert_glsl[];
+extern char datatoc_gpu_shader_3D_polyline_frag_glsl[];
+extern char datatoc_gpu_shader_3D_polyline_geom_glsl[];
+extern char datatoc_gpu_shader_3D_polyline_vert_glsl[];
extern char datatoc_gpu_shader_3D_smooth_color_vert_glsl[];
extern char datatoc_gpu_shader_3D_smooth_color_frag_glsl[];
extern char datatoc_gpu_shader_3D_passthrough_vert_glsl[];
@@ -1026,6 +1029,36 @@ static const GPUShaderStages builtin_shader_stages[GPU_SHADER_BUILTIN_LEN] = {
.frag = datatoc_gpu_shader_uniform_color_frag_glsl,
},
+ [GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR] =
+ {
+ .vert = datatoc_gpu_shader_3D_polyline_vert_glsl,
+ .geom = datatoc_gpu_shader_3D_polyline_geom_glsl,
+ .frag = datatoc_gpu_shader_3D_polyline_frag_glsl,
+ .defs = "#define UNIFORM\n",
+ },
+ [GPU_SHADER_3D_POLYLINE_CLIPPED_UNIFORM_COLOR] =
+ {
+ .vert = datatoc_gpu_shader_3D_polyline_vert_glsl,
+ .geom = datatoc_gpu_shader_3D_polyline_geom_glsl,
+ .frag = datatoc_gpu_shader_3D_polyline_frag_glsl,
+ .defs = "#define UNIFORM\n"
+ "#define CLIP\n",
+ },
+ [GPU_SHADER_3D_POLYLINE_FLAT_COLOR] =
+ {
+ .vert = datatoc_gpu_shader_3D_polyline_vert_glsl,
+ .geom = datatoc_gpu_shader_3D_polyline_geom_glsl,
+ .frag = datatoc_gpu_shader_3D_polyline_frag_glsl,
+ .defs = "#define FLAT\n",
+ },
+ [GPU_SHADER_3D_POLYLINE_SMOOTH_COLOR] =
+ {
+ .vert = datatoc_gpu_shader_3D_polyline_vert_glsl,
+ .geom = datatoc_gpu_shader_3D_polyline_geom_glsl,
+ .frag = datatoc_gpu_shader_3D_polyline_frag_glsl,
+ .defs = "#define SMOOTH\n",
+ },
+
[GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR] =
{
.vert = datatoc_gpu_shader_2D_line_dashed_uniform_color_vert_glsl,
diff --git a/source/blender/gpu/shaders/gpu_shader_3D_polyline_frag.glsl b/source/blender/gpu/shaders/gpu_shader_3D_polyline_frag.glsl
new file mode 100644
index 00000000000..9c6b109d659
--- /dev/null
+++ b/source/blender/gpu/shaders/gpu_shader_3D_polyline_frag.glsl
@@ -0,0 +1,24 @@
+
+uniform float lineWidth;
+
+in vec4 finalColor;
+noperspective in float smoothline;
+#ifdef CLIP
+in float clip;
+#endif
+
+out vec4 fragColor;
+
+#define SMOOTH_WIDTH 1.0
+
+void main()
+{
+#ifdef CLIP
+ if (clip < 0.0) {
+ discard;
+ }
+#endif
+ fragColor = finalColor;
+ fragColor.a *= clamp((lineWidth + SMOOTH_WIDTH) * 0.5 - abs(smoothline), 0.0, 1.0);
+ fragColor = blender_srgb_to_framebuffer_space(fragColor);
+}
diff --git a/source/blender/gpu/shaders/gpu_shader_3D_polyline_geom.glsl b/source/blender/gpu/shaders/gpu_shader_3D_polyline_geom.glsl
new file mode 100644
index 00000000000..b28205b349e
--- /dev/null
+++ b/source/blender/gpu/shaders/gpu_shader_3D_polyline_geom.glsl
@@ -0,0 +1,68 @@
+
+layout(lines) in;
+layout(triangle_strip, max_vertices = 4) out;
+
+uniform vec4 color;
+uniform vec2 viewportSize;
+uniform float lineWidth;
+
+#if !defined(UNIFORM)
+in vec4 finalColor_g[];
+#endif
+
+#ifdef CLIP
+in float clip_g[];
+out float clip;
+#endif
+
+out vec4 finalColor;
+noperspective out float smoothline;
+
+#define SMOOTH_WIDTH 1.0
+
+void do_vertex(const int i, vec2 ofs)
+{
+#if defined(UNIFORM)
+ finalColor = color;
+
+#elif defined(FLAT)
+ finalColor = finalColor_g[0];
+
+#elif defined(SMOOTH)
+ finalColor = finalColor_g[i];
+#endif
+
+#ifdef CLIP
+ clip = clip_g[i];
+#endif
+
+ smoothline = (lineWidth + SMOOTH_WIDTH) * 0.5;
+ gl_Position = gl_in[i].gl_Position;
+ gl_Position.xy += ofs * gl_Position.w;
+ EmitVertex();
+
+ smoothline = -(lineWidth + SMOOTH_WIDTH) * 0.5;
+ gl_Position = gl_in[i].gl_Position;
+ gl_Position.xy -= ofs * gl_Position.w;
+ EmitVertex();
+}
+
+void main(void)
+{
+ vec2 p0 = gl_in[0].gl_Position.xy / gl_in[0].gl_Position.w;
+ vec2 p1 = gl_in[1].gl_Position.xy / gl_in[1].gl_Position.w;
+ vec2 e = normalize((p1 - p0) * viewportSize.xy);
+#if 0 /* Hard turn when line direction changes quadrant. */
+ e = abs(e);
+ vec2 ofs = (e.x > e.y) ? vec2(0.0, 1.0 / e.x) : vec2(1.0 / e.y, 0.0);
+#else /* Use perpendicular direction. */
+ vec2 ofs = vec2(-e.y, e.x);
+#endif
+ ofs /= viewportSize.xy;
+ ofs *= lineWidth + SMOOTH_WIDTH;
+
+ do_vertex(0, ofs);
+ do_vertex(1, ofs);
+
+ EndPrimitive();
+}
diff --git a/source/blender/gpu/shaders/gpu_shader_3D_polyline_vert.glsl b/source/blender/gpu/shaders/gpu_shader_3D_polyline_vert.glsl
new file mode 100644
index 00000000000..28aa2a4ccc6
--- /dev/null
+++ b/source/blender/gpu/shaders/gpu_shader_3D_polyline_vert.glsl
@@ -0,0 +1,28 @@
+
+uniform mat4 ModelViewProjectionMatrix;
+uniform mat4 ModelMatrix;
+uniform vec4 ClipPlane;
+
+in vec3 pos;
+
+#if !defined(UNIFORM)
+in vec4 color;
+
+out vec4 finalColor_g;
+#endif
+
+#ifdef CLIP
+out float clip_g;
+#endif
+
+void main()
+{
+ gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
+#if !defined(UNIFORM)
+ finalColor_g = color;
+#endif
+
+#ifdef CLIP
+ clip_g = dot(ModelMatrix * vec4(pos, 1.0), ClipPlane);
+#endif
+}