diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2017-07-13 17:44:02 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2017-07-13 17:47:58 +0300 |
commit | 474454be3976f51d59aff327491a5fd2ab48abc2 (patch) | |
tree | 895407db5881d98958dd2b56ea4e95b042f7e3d6 /source/blender/gpu | |
parent | c9aef27326d54ed053edb37d27fe5571805139b9 (diff) |
Cleanup/rename etc. dashed line shaders.
Goal is to make them more modular, to allow more variants (variable
single-color, thickness, ...) to be added without having to
copy-and-change-one-line of whole chain of shaders.
Diffstat (limited to 'source/blender/gpu')
-rw-r--r-- | source/blender/gpu/CMakeLists.txt | 6 | ||||
-rw-r--r-- | source/blender/gpu/GPU_shader.h | 4 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader.c | 24 | ||||
-rw-r--r-- | source/blender/gpu/shaders/gpu_shader_2D_line_dashed_frag.glsl | 44 | ||||
-rw-r--r-- | source/blender/gpu/shaders/gpu_shader_2D_line_dashed_geom.glsl | 14 | ||||
-rw-r--r-- | source/blender/gpu/shaders/gpu_shader_2D_line_dashed_uniform_color_vert.glsl | 21 | ||||
-rw-r--r-- | source/blender/gpu/shaders/gpu_shader_2D_line_dashed_vert.glsl | 11 | ||||
-rw-r--r-- | source/blender/gpu/shaders/gpu_shader_2D_line_dashed_width_geom.glsl | 59 | ||||
-rw-r--r-- | source/blender/gpu/shaders/gpu_shader_3D_line_dashed_uniform_color_legacy_vert.glsl (renamed from source/blender/gpu/shaders/gpu_shader_3D_line_dashed_legacy_vert.glsl) | 12 | ||||
-rw-r--r-- | source/blender/gpu/shaders/gpu_shader_3D_line_dashed_uniform_color_vert.glsl | 21 | ||||
-rw-r--r-- | source/blender/gpu/shaders/gpu_shader_3D_line_dashed_vert.glsl | 11 |
11 files changed, 169 insertions, 58 deletions
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt index a754d5b92b0..ec623554567 100644 --- a/source/blender/gpu/CMakeLists.txt +++ b/source/blender/gpu/CMakeLists.txt @@ -135,7 +135,7 @@ 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_flat_color_vert.glsl SRC) -data_to_c_simple(shaders/gpu_shader_2D_line_dashed_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) @@ -154,8 +154,8 @@ data_to_c_simple(shaders/gpu_shader_3D_image_vert.glsl SRC) 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_legacy_vert.glsl SRC) -data_to_c_simple(shaders/gpu_shader_3D_line_dashed_vert.glsl SRC) +data_to_c_simple(shaders/gpu_shader_3D_line_dashed_uniform_color_legacy_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_smooth_color_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_normal_smooth_color_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_smooth_color_frag.glsl SRC) diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h index 05949d759a4..7c8ee07eb88 100644 --- a/source/blender/gpu/GPU_shader.h +++ b/source/blender/gpu/GPU_shader.h @@ -146,8 +146,8 @@ typedef enum GPUBuiltinShader { GPU_SHADER_3D_POINT_VARYING_SIZE_UNIFORM_COLOR, GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR, /* lines */ - GPU_SHADER_2D_LINE_DASHED_COLOR, - GPU_SHADER_3D_LINE_DASHED_COLOR, + GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR, + GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR, /* lamp drawing */ GPU_SHADER_3D_GROUNDPOINT, GPU_SHADER_3D_GROUNDLINE, diff --git a/source/blender/gpu/intern/gpu_shader.c b/source/blender/gpu/intern/gpu_shader.c index f0a1c182713..e67e5bc933d 100644 --- a/source/blender/gpu/intern/gpu_shader.c +++ b/source/blender/gpu/intern/gpu_shader.c @@ -113,11 +113,11 @@ extern char datatoc_gpu_shader_2D_point_uniform_size_aa_vert_glsl[]; extern char datatoc_gpu_shader_2D_point_uniform_size_outline_aa_vert_glsl[]; extern char datatoc_gpu_shader_2D_point_uniform_size_varying_color_outline_aa_vert_glsl[]; -extern char datatoc_gpu_shader_2D_line_dashed_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_legacy_vert_glsl[]; -extern char datatoc_gpu_shader_3D_line_dashed_vert_glsl[]; +extern char datatoc_gpu_shader_3D_line_dashed_uniform_color_legacy_vert_glsl[]; +extern char datatoc_gpu_shader_3D_line_dashed_uniform_color_vert_glsl[]; extern char datatoc_gpu_shader_edges_front_back_persp_vert_glsl[]; extern char datatoc_gpu_shader_edges_front_back_persp_geom_glsl[]; @@ -678,12 +678,12 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader) datatoc_gpu_shader_uniform_color_frag_glsl, datatoc_gpu_shader_3D_groundline_geom_glsl }, - [GPU_SHADER_2D_LINE_DASHED_COLOR] = { datatoc_gpu_shader_2D_line_dashed_vert_glsl, - datatoc_gpu_shader_2D_line_dashed_frag_glsl, - datatoc_gpu_shader_2D_line_dashed_geom_glsl }, - [GPU_SHADER_3D_LINE_DASHED_COLOR] = { datatoc_gpu_shader_3D_line_dashed_vert_glsl, - datatoc_gpu_shader_2D_line_dashed_frag_glsl, - datatoc_gpu_shader_2D_line_dashed_geom_glsl }, + [GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR] = { datatoc_gpu_shader_2D_line_dashed_uniform_color_vert_glsl, + datatoc_gpu_shader_2D_line_dashed_frag_glsl, + datatoc_gpu_shader_2D_line_dashed_geom_glsl }, + [GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR] = { datatoc_gpu_shader_3D_line_dashed_uniform_color_vert_glsl, + datatoc_gpu_shader_2D_line_dashed_frag_glsl, + datatoc_gpu_shader_2D_line_dashed_geom_glsl }, [GPU_SHADER_3D_OBJECTSPACE_SIMPLE_LIGHTING_VARIYING_COLOR] = { datatoc_gpu_shader_instance_objectspace_variying_color_vert_glsl, @@ -744,7 +744,7 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader) datatoc_gpu_shader_instance_edges_variying_color_geom_glsl}, [GPU_SHADER_3D_INSTANCE_BONE_ENVELOPE_SOLID] = { datatoc_gpu_shader_instance_bone_envelope_solid_vert_glsl, - datatoc_gpu_shader_simple_lighting_frag_glsl }, + datatoc_gpu_shader_simple_lighting_frag_glsl }, [GPU_SHADER_3D_INSTANCE_BONE_ENVELOPE_WIRE] = { datatoc_gpu_shader_instance_bone_envelope_wire_vert_glsl, datatoc_gpu_shader_flat_color_frag_glsl }, }; @@ -786,10 +786,10 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader) stages = &legacy_fancy_edges; } - if (shader == GPU_SHADER_3D_LINE_DASHED_COLOR && !GLEW_VERSION_3_2) { + if (shader == GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR && !GLEW_VERSION_3_2) { /* Dashed need geometry shader, which are not supported by legacy OpenGL, fallback to solid lines. */ /* TODO: remove after switch to core profile (maybe) */ - static const GPUShaderStages legacy_dashed_lines = { datatoc_gpu_shader_3D_line_dashed_legacy_vert_glsl, + static const GPUShaderStages legacy_dashed_lines = { datatoc_gpu_shader_3D_line_dashed_uniform_color_legacy_vert_glsl, datatoc_gpu_shader_2D_line_dashed_frag_glsl }; stages = &legacy_dashed_lines; } 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 346d3b3c6d9..7caf00f58fd 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 @@ -1,38 +1,50 @@ -// Draw dashed lines, perforated in screen space. - -noperspective in float distance_along_line; -out vec4 fragColor; +/* + * Fragment Shader for dashed lines, with uniform multi-color(s), or any single-color, and any thickness. + * + * Dashed is performed in screen space. + */ 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. */ -uniform vec4 color; /* More advanced mode, allowing for complex, multi-colored patterns. Enabled when num_colors > 0. */ /* Note: max number of steps/colors in pattern is 32! */ uniform int num_colors; /* Enabled if > 0, 1 for solid line. */ uniform vec4 colors[32]; +noperspective in float distance_along_line; +noperspective in vec4 color_geom; + +out vec4 fragColor; + void main() { - /* Solid line cases, simple. */ - if (num_colors == 1) { - fragColor = colors[0]; - } - else if (dash_factor >= 1.0f) { - fragColor = color; - } - else { + /* Multi-color option. */ + if (num_colors > 0) { + /* Solid line case, simple. */ + if (num_colors == 1) { + fragColor = colors[0]; + } /* Actually dashed line... */ - float normalized_distance = fract(distance_along_line / dash_width); - if (num_colors > 0) { + else { + float normalized_distance = fract(distance_along_line / dash_width); fragColor = colors[int(normalized_distance * num_colors)]; } + } + /* Single color option. */ + else { + /* Solid line case, simple. */ + if (dash_factor >= 1.0f) { + fragColor = color_geom; + } + /* Actually dashed line... */ else { + float normalized_distance = fract(distance_along_line / dash_width); if (normalized_distance <= dash_factor) { - fragColor = color; + fragColor = color_geom; } 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 index e0b618e0b1a..db4bdf0a9f0 100644 --- a/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_geom.glsl +++ b/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_geom.glsl @@ -1,5 +1,10 @@ -// Draw dashed lines, perforated in screen space. +/* + * 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; @@ -11,8 +16,11 @@ uniform int num_colors; /* 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() { @@ -20,12 +28,14 @@ void main() vec4 v2 = gl_in[1].gl_Position; gl_Position = v1; + color_geom = color_vert[0]; distance_along_line = 0.0f; EmitVertex(); gl_Position = v2; + color_geom = color_vert[1]; if ((num_colors == 1) || (dash_factor >= 1.0f)) { - /* Solid line, optimise out distance computation! */ + /* Solid line, optimize out distance computation! */ distance_along_line = 0.0f; } else { 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 new file mode 100644 index 00000000000..f5c611586aa --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_uniform_color_vert.glsl @@ -0,0 +1,21 @@ + +/* + * Vertex Shader for dashed lines with 2D coordinates, with uniform multi-colors or uniform single-color, + * and unary thickness. + * + * Dashed is performed in screen space. + */ + +uniform mat4 ModelViewProjectionMatrix; + +uniform vec4 color; + +in vec2 pos; + +out vec4 color_vert; + +void main() +{ + gl_Position = ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0); + color_vert = color; +} diff --git a/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_vert.glsl deleted file mode 100644 index 14f56d7e0dd..00000000000 --- a/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_vert.glsl +++ /dev/null @@ -1,11 +0,0 @@ - -// Draw dashed lines, perforated in screen space. - -uniform mat4 ModelViewProjectionMatrix; - -in vec2 pos; - -void main() -{ - gl_Position = ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0); -} 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 new file mode 100644 index 00000000000..20c72f4407d --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_width_geom.glsl @@ -0,0 +1,59 @@ + +// 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 num_colors; /* 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 ((num_colors == 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); + } + gl_Position = v2 + (wdir * w2); + EmitVertex(); + + gl_Position = v2 - (wdir * w2); + EmitVertex(); + + EndPrimitive(); +} diff --git a/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_legacy_vert.glsl b/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_uniform_color_legacy_vert.glsl index eab4c1b4543..84fbf977846 100644 --- a/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_legacy_vert.glsl +++ b/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_uniform_color_legacy_vert.glsl @@ -1,11 +1,19 @@ -/* Note: nearly the same code as for 2D version... Maybe we could deduplicate? */ +/* + * Vertex Shader for dashed lines with 3D coordinates, with uniform multi-colors or uniform single-color, + * and unary thickness. + * + * Legacy version, without geometry shader support, always produce solid lines! + */ uniform mat4 ModelViewProjectionMatrix; uniform vec2 viewport_size; +uniform vec4 color; + in vec3 pos; noperspective out float distance_along_line; +noperspective out vec4 color_geom; void main() { @@ -13,4 +21,6 @@ void main() /* Hack - prevent stupid GLSL compiler to optimize out unused viewport_size uniform, which gives crash! */ distance_along_line = viewport_size.x * 0.000001f - viewport_size.x * 0.0000009f; + + color_geom = color; } diff --git a/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_uniform_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_uniform_color_vert.glsl new file mode 100644 index 00000000000..2fe08896585 --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_uniform_color_vert.glsl @@ -0,0 +1,21 @@ + +/* + * Vertex Shader for dashed lines with 3D coordinates, with uniform multi-colors or uniform single-color, + * and unary thickness. + * + * Dashed is performed in screen space. + */ + +uniform mat4 ModelViewProjectionMatrix; + +uniform vec4 color; + +in vec3 pos; + +out vec4 color_vert; + +void main() +{ + gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0); + color_vert = color; +} diff --git a/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_vert.glsl b/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_vert.glsl deleted file mode 100644 index a02b0d219fb..00000000000 --- a/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_vert.glsl +++ /dev/null @@ -1,11 +0,0 @@ - -// Draw dashed lines, perforated in screen space. - -uniform mat4 ModelViewProjectionMatrix; - -in vec3 pos; - -void main() -{ - gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0); -} |