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.
This commit is contained in:
Clément Foucault 2018-04-05 15:27:53 +02:00
parent 5fba4458e9
commit d48597eb54
5 changed files with 130 additions and 1 deletions

View File

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

View File

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

View File

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

View File

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

View File

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