[Bf-blender-cvs] [d48597eb542] blender2.8: GPUShader: Add 2D Nodelink shader.

Clément Foucault noreply at git.blender.org
Thu Apr 5 16:08:25 CEST 2018


Commit: d48597eb5422b33d5edc80f3b6d8d927f2f40444
Author: Clément Foucault
Date:   Thu Apr 5 15:27:53 2018 +0200
Branches: blender2.8
https://developer.blender.org/rBd48597eb5422b33d5edc80f3b6d8d927f2f40444

GPUShader: Add 2D Nodelink shader.

Special shader to draw nodelinks for the node editor.

We only pass bezier points to the GPU and vertex position is handled inside
the vertex shader.
The arrow is also part of the batch to avoid separate drawcalls for it.

We still draw 2 pass one for shadow and one for the link color on top.

One variation to draw instances of theses links so that we only do one
drawcall.

===================================================================

M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/GPU_shader.h
M	source/blender/gpu/intern/gpu_shader.c
A	source/blender/gpu/shaders/gpu_shader_2D_nodelink_frag.glsl
A	source/blender/gpu/shaders/gpu_shader_2D_nodelink_vert.glsl

===================================================================

diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index b52817c68a8..de4d59ad10b 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -133,6 +133,8 @@ data_to_c_simple(shaders/gpu_shader_flat_color_frag.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_flat_color_alpha_test_0_frag.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_2D_vert.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_2D_widget_base_vert.glsl SRC)
+data_to_c_simple(shaders/gpu_shader_2D_nodelink_frag.glsl SRC)
+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)
diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h
index 5041d524f18..486aea561c4 100644
--- a/source/blender/gpu/GPU_shader.h
+++ b/source/blender/gpu/GPU_shader.h
@@ -171,8 +171,10 @@ typedef enum GPUBuiltinShader {
 	GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE, /* Uniformly scaled */
 	GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SCALE,
 	GPU_SHADER_INSTANCE_EDGES_VARIYING_COLOR,
-	/* specialized for widget drawing */
+	/* specialized for UI drawing */
 	GPU_SHADER_2D_WIDGET_BASE,
+	GPU_SHADER_2D_NODELINK,
+	GPU_SHADER_2D_NODELINK_INST,
 
 	GPU_SHADER_3D_INSTANCE_BONE_ENVELOPE_SOLID,
 	GPU_SHADER_3D_INSTANCE_BONE_ENVELOPE_WIRE,
diff --git a/source/blender/gpu/intern/gpu_shader.c b/source/blender/gpu/intern/gpu_shader.c
index 55184418284..c2f0aaf65de 100644
--- a/source/blender/gpu/intern/gpu_shader.c
+++ b/source/blender/gpu/intern/gpu_shader.c
@@ -68,6 +68,8 @@ extern char datatoc_gpu_shader_2D_image_vert_glsl[];
 extern char datatoc_gpu_shader_2D_image_rect_vert_glsl[];
 extern char datatoc_gpu_shader_2D_image_multi_rect_vert_glsl[];
 extern char datatoc_gpu_shader_2D_widget_base_vert_glsl[];
+extern char datatoc_gpu_shader_2D_nodelink_frag_glsl[];
+extern char datatoc_gpu_shader_2D_nodelink_vert_glsl[];
 
 extern char datatoc_gpu_shader_3D_image_vert_glsl[];
 extern char datatoc_gpu_shader_image_frag_glsl[];
@@ -799,6 +801,10 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
 
 		[GPU_SHADER_2D_WIDGET_BASE] = { datatoc_gpu_shader_2D_widget_base_vert_glsl,
 		                                datatoc_gpu_shader_2D_smooth_color_frag_glsl},
+		[GPU_SHADER_2D_NODELINK] = { datatoc_gpu_shader_2D_nodelink_vert_glsl,
+		                             datatoc_gpu_shader_2D_nodelink_frag_glsl },
+		[GPU_SHADER_2D_NODELINK_INST] = { datatoc_gpu_shader_2D_nodelink_vert_glsl,
+		                                  datatoc_gpu_shader_2D_nodelink_frag_glsl },
 
 		[GPU_SHADER_3D_INSTANCE_BONE_ENVELOPE_SOLID] = { datatoc_gpu_shader_instance_bone_envelope_solid_vert_glsl,
 		                                                 datatoc_gpu_shader_simple_lighting_frag_glsl },
@@ -813,6 +819,9 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
 		/* just a few special cases */
 		const char *defines = NULL;
 		switch (shader) {
+			case GPU_SHADER_2D_NODELINK_INST:
+				defines = "#define USE_INSTANCE;\n";
+				break;
 			case GPU_SHADER_SMOKE_COBA:
 				defines = "#define USE_COBA;\n";
 				break;
diff --git a/source/blender/gpu/shaders/gpu_shader_2D_nodelink_frag.glsl b/source/blender/gpu/shaders/gpu_shader_2D_nodelink_frag.glsl
new file mode 100644
index 00000000000..8dda575107a
--- /dev/null
+++ b/source/blender/gpu/shaders/gpu_shader_2D_nodelink_frag.glsl
@@ -0,0 +1,10 @@
+
+in float colorGradient;
+in vec4 finalColor;
+
+out vec4 fragColor;
+
+void main() {
+	fragColor = finalColor;
+	fragColor.a *= smoothstep(1.0, 0.1, abs(colorGradient));
+}
\ No newline at end of file
diff --git a/source/blender/gpu/shaders/gpu_shader_2D_nodelink_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_nodelink_vert.glsl
new file mode 100644
index 00000000000..3fcae15bf16
--- /dev/null
+++ b/source/blender/gpu/shaders/gpu_shader_2D_nodelink_vert.glsl
@@ -0,0 +1,106 @@
+/**
+ * 2D Quadratic Bezier thick line drawing
+ **/
+
+#define MID_VERTEX 57
+
+/* u is position along the curve, defining the tangent space.
+ * v is "signed" distance (compressed to [0..1] range) from the pos in expand direction */
+in vec2 uv;
+in vec2 pos; /* verts position in the curve tangent space */
+in vec2 expand;
+
+#ifdef USE_INSTANCE
+/* Instance attrib */
+in vec2 P0;
+in vec2 P1;
+in vec2 P2;
+in vec2 P3;
+in ivec4 colid_doarrow;
+
+uniform vec4 colors[6];
+
+#define colStart   colors[colid_doarrow[0]]
+#define colEnd     colors[colid_doarrow[1]]
+#define colShadow  colors[colid_doarrow[2]]
+#define doArrow    (colid_doarrow[3] != 0)
+
+#else
+/* Single curve drawcall, use uniform. */
+uniform vec2 bezierPts[4];
+
+#define P0 bezierPts[0]
+#define P1 bezierPts[1]
+#define P2 bezierPts[2]
+#define P3 bezierPts[3]
+
+uniform vec4 colors[3];
+uniform bool doArrow;
+
+#define colShadow  colors[0]
+#define colStart   colors[1]
+#define colEnd     colors[2]
+
+#endif
+
+uniform float expandSize;
+uniform mat4 ModelViewProjectionMatrix;
+
+out float colorGradient;
+out vec4 finalColor;
+
+void main(void)
+{
+	float t = uv.x;
+	float t2 = t * t;
+	float t2_3 = 3.0 * t2;
+	float one_minus_t = 1.0 - t;
+	float one_minus_t2 = one_minus_t * one_minus_t;
+	float one_minus_t2_3 = 3.0 * one_minus_t2;
+
+	vec2 point = (P0 * one_minus_t2 * one_minus_t +
+	              P1 * one_minus_t2_3 * t +
+	              P2 * t2_3 * one_minus_t +
+	              P3 * t2 * t);
+
+	vec2 tangent = ((P1 - P0) * one_minus_t2_3 +
+	                (P2 - P1) * 6.0 * (t - t2) +
+	                (P3 - P2) * t2_3);
+
+	/* tangent space at t */
+	tangent = normalize(tangent);
+	vec2 normal = tangent.yx * vec2(-1.0, 1.0);
+
+	/* Position vertex on the curve tangent space */
+	point += pos.x * tangent + pos.y * normal;
+
+	gl_Position = ModelViewProjectionMatrix * vec4(point, 0.0, 1.0);
+
+	vec2 exp_axis = expand.x * tangent + expand.y * normal;
+
+	/* rotate & scale the expand axis */
+	exp_axis = ModelViewProjectionMatrix[0].xy * exp_axis.xx +
+	           ModelViewProjectionMatrix[1].xy * exp_axis.yy;
+
+
+	float expand_dist = (uv.y * 2.0 - 1.0);
+	colorGradient = expand_dist;
+
+	if (gl_VertexID < MID_VERTEX) {
+		/* Shadow pass */
+		finalColor = colShadow;
+	}
+	else {
+		/* Second pass */
+		finalColor = mix(colStart, colEnd, uv.x);
+		expand_dist *= 0.5;
+	}
+
+	/* Expand into a line */
+	gl_Position.xy += exp_axis * expandSize * expand_dist;
+
+	/* if arrow */
+	if (expand.y != 1.0 && !doArrow) {
+		gl_Position.xy *= 0.0;
+	}
+}
\ No newline at end of file



More information about the Bf-blender-cvs mailing list