[Bf-blender-cvs] [224f148e224] blender2.8: 2D dashed line shader: changed to use viewport side instead of scale.

Bastien Montagne noreply at git.blender.org
Wed Apr 26 21:00:58 CEST 2017


Commit: 224f148e224086c61e819525cd811572d4383ad1
Author: Bastien Montagne
Date:   Wed Apr 26 20:51:17 2017 +0200
Branches: blender2.8
https://developer.blender.org/rB224f148e224086c61e819525cd811572d4383ad1

2D dashed line shader: changed to use viewport side instead of scale.

The scale version was working(ish), but it was not really extendable to
a 3D line version of the shader.

Also note that sequencer view still keeps its 'UI scale' adaptation
(dashes grow together with UI scale setting). Would be nice to do that
everywhere ultimately imho, but nothing urgent here.

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

M	source/blender/editors/space_sequencer/sequencer_draw.c
M	source/blender/editors/space_view3d/view3d_draw.c
M	source/blender/editors/space_view3d/view3d_ruler.c
M	source/blender/gpu/shaders/gpu_shader_2D_line_dashed_vert.glsl
M	source/blender/windowmanager/intern/wm_gesture.c

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

diff --git a/source/blender/editors/space_sequencer/sequencer_draw.c b/source/blender/editors/space_sequencer/sequencer_draw.c
index 0dcfe2d2076..6a9437bb892 100644
--- a/source/blender/editors/space_sequencer/sequencer_draw.c
+++ b/source/blender/editors/space_sequencer/sequencer_draw.c
@@ -1033,10 +1033,6 @@ static void sequencer_draw_borders(const SpaceSeq *sseq, const View2D *v2d, cons
 	glLineWidth(1.0f);
 
 	/* border */
-	float mvp[4][4];
-	gpuGetModelViewProjectionMatrix(mvp);
-	const float view_scale = mat4_to_xy_scale(mvp) * (U.pixelsize * 72.0f);
-
 	VertexFormat *format = immVertexFormat();
 	unsigned int pos = VertexFormat_add_attrib(format, "pos", COMP_F32, 2, KEEP_FLOAT);
 	unsigned int line_origin = VertexFormat_add_attrib(format, "line_origin", COMP_F32, 2, KEEP_FLOAT);
@@ -1047,13 +1043,15 @@ static void sequencer_draw_borders(const SpaceSeq *sseq, const View2D *v2d, cons
 
 	immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR);
 
-	immUniform1f("view_scale", view_scale);
+	float viewport_size[4];
+	glGetFloatv(GL_VIEWPORT, viewport_size);
+	immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC);
 
 	UI_GetThemeColor4fv(TH_BACK, color1);
 	immUniform4fv("color1", color1);
 	immUniform4f("color2", 0.0f, 0.0f, 0.0f, 0.0f);
-	immUniform1f("dash_width", 1.0f);
-	immUniform1f("dash_width_on", 0.5f);
+	immUniform1f("dash_width", 6.0f);
+	immUniform1f("dash_width_on", 3.0f);
 
 	imm_draw_line_box_dashed(pos, line_origin, x1 - 0.5f, y1 - 0.5f, x2 + 0.5f, y2 + 0.5f);
 
diff --git a/source/blender/editors/space_view3d/view3d_draw.c b/source/blender/editors/space_view3d/view3d_draw.c
index df4723f3df3..1490de0fb48 100644
--- a/source/blender/editors/space_view3d/view3d_draw.c
+++ b/source/blender/editors/space_view3d/view3d_draw.c
@@ -596,7 +596,10 @@ static void drawviewborder(Scene *scene, ARegion *ar, View3D *v3d)
 
 		immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR);
 
-		immUniform1f("view_scale", 1.0f);
+		float viewport_size[4];
+		glGetFloatv(GL_VIEWPORT, viewport_size);
+		immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
+
 		immUniform4f("color2", 0.0f, 0.0f, 0.0f, 0.0f);
 		immUniform1f("dash_width", 6.0f);
 		immUniform1f("dash_width_on", 3.0f);
@@ -770,7 +773,9 @@ static void drawrenderborder(ARegion *ar, View3D *v3d)
 
 	immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR);
 
-	immUniform1f("view_scale", 1.0f);
+	float viewport_size[4];
+	glGetFloatv(GL_VIEWPORT, viewport_size);
+	immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
 
 	immUniform4f("color1", 1.0f, 0.25f, 0.25f, 1.0f);
 	immUniform4f("color2", 0.0f, 0.0f, 0.0f, 0.0f);
diff --git a/source/blender/editors/space_view3d/view3d_ruler.c b/source/blender/editors/space_view3d/view3d_ruler.c
index 11579e6a670..9bdc75802ba 100644
--- a/source/blender/editors/space_view3d/view3d_ruler.c
+++ b/source/blender/editors/space_view3d/view3d_ruler.c
@@ -465,11 +465,14 @@ static void ruler_info_draw_pixel(const struct bContext *C, ARegion *ar, void *a
 
 			immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR);
 
-			immUniform1f("view_scale", 1.0f);
+			float viewport_size[4];
+			glGetFloatv(GL_VIEWPORT, viewport_size);
+			immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
+
 			immUniform4f("color1", 0.67f, 0.67f, 0.67f, 1.0f);
 			immUniform4fv("color2", is_act ? color_act : color_base);
-			immUniform1f("dash_width", 8.0f);
-			immUniform1f("dash_width_on", 4.0f);
+			immUniform1f("dash_width", 6.0f);
+			immUniform1f("dash_width_on", 3.0f);
 
 			immBegin(PRIM_LINES, 4);
 
@@ -611,11 +614,14 @@ static void ruler_info_draw_pixel(const struct bContext *C, ARegion *ar, void *a
 
 			immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR);
 
-			immUniform1f("view_scale", 1.0f);
+			float viewport_size[4];
+			glGetFloatv(GL_VIEWPORT, viewport_size);
+			immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
+
 			immUniform4f("color1", 0.67f, 0.67f, 0.67f, 1.0f);
 			immUniform4fv("color2", is_act ? color_act : color_base);
-			immUniform1f("dash_width", 8.0f);
-			immUniform1f("dash_width_on", 4.0f);
+			immUniform1f("dash_width", 6.0f);
+			immUniform1f("dash_width_on", 3.0f);
 
 			immBegin(PRIM_LINES, 2);
 
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
index 94e8de4537b..708871b363f 100644
--- a/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_vert.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_2D_line_dashed_vert.glsl
@@ -3,7 +3,7 @@
 // Based on a (3D) version by Mike Erwin.
 
 uniform mat4 ModelViewProjectionMatrix;
-uniform float view_scale;
+uniform vec2 viewport_size;
 
 #if __VERSION__ == 120
   attribute vec2 pos;
@@ -19,12 +19,15 @@ void main()
 {
 	gl_Position = ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
 
-	distance_along_line = distance(line_origin, pos) * view_scale;
+	vec4 point = ModelViewProjectionMatrix * vec4(line_origin, 0.0, 1.0);
+	vec2 ref_point = (point.xy / point.w) * 0.5 + 0.5;  // <- device coordinates in [0..1] range.
+	ref_point = ref_point * viewport_size;  // <- 'virtual' screen coordinates.
 
-	/* Another solution would be to compute line_origin in fragment coordinate, and then compute distance
-	 * in fragment shader, but we'd need opengl window size for that...
-	 *    vec4 point = ModelViewProjectionMatrix * vec4(line_origin, 0.0, 1.0);
-	 *    ref_point = (point.xy / point.w) * 0.5 + 0.5;  // <- device coordinates in [0..1] range.
-	 *    ref_point = ref_point * window_size;
-	 */
+	vec2 curr_point = (gl_Position.xy / gl_Position.w) * 0.5 + 0.5;  // <- device coordinates in [0..1] range.
+	curr_point = curr_point * viewport_size;  // <- 'virtual' screen coordinates.
+
+	distance_along_line = distance(ref_point, curr_point);
+
+	/* 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/windowmanager/intern/wm_gesture.c b/source/blender/windowmanager/intern/wm_gesture.c
index cfe62846bfd..0ee0e415782 100644
--- a/source/blender/windowmanager/intern/wm_gesture.c
+++ b/source/blender/windowmanager/intern/wm_gesture.c
@@ -178,7 +178,10 @@ static void wm_gesture_draw_line(wmGesture *gt)
 
 	immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR);
 
-	immUniform1f("view_scale", 1.0f);
+	float viewport_size[4];
+	glGetFloatv(GL_VIEWPORT, viewport_size);
+	immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
+
 	immUniform4f("color1", 0.4f, 0.4f, 0.4f, 1.0f);
 	immUniform4f("color2", 1.0f, 1.0f, 1.0f, 1.0f);
 	immUniform1f("dash_width", 8.0f);
@@ -223,7 +226,10 @@ static void wm_gesture_draw_rect(wmGesture *gt)
 
 	immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR);
 
-	immUniform1f("view_scale", 1.0f);
+	float viewport_size[4];
+	glGetFloatv(GL_VIEWPORT, viewport_size);
+	immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
+
 	immUniform4f("color1", 0.4f, 0.4f, 0.4f, 1.0f);
 	immUniform4f("color2", 1.0f, 1.0f, 1.0f, 1.0f);
 	immUniform1f("dash_width", 8.0f);
@@ -285,7 +291,10 @@ static void wm_gesture_draw_circle(wmGesture *gt)
 
 	immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR);
 
-	immUniform1f("view_scale", 1.0f);
+	float viewport_size[4];
+	glGetFloatv(GL_VIEWPORT, viewport_size);
+	immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
+
 	immUniform4f("color1", 0.4f, 0.4f, 0.4f, 1.0f);
 	immUniform4f("color2", 1.0f, 1.0f, 1.0f, 1.0f);
 	immUniform1f("dash_width", 4.0f);
@@ -397,7 +406,10 @@ static void wm_gesture_draw_lasso(wmWindow *win, wmGesture *gt, bool filled)
 
 	immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR);
 
-	immUniform1f("view_scale", 1.0f);
+	float viewport_size[4];
+	glGetFloatv(GL_VIEWPORT, viewport_size);
+	immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
+
 	immUniform4f("color1", 0.4f, 0.4f, 0.4f, 1.0f);
 	immUniform4f("color2", 1.0f, 1.0f, 1.0f, 1.0f);
 	immUniform1f("dash_width", 2.0f);
@@ -442,7 +454,10 @@ static void wm_gesture_draw_cross(wmWindow *win, wmGesture *gt)
 
 	immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR);
 
-	immUniform1f("view_scale", 1.0f);
+	float viewport_size[4];
+	glGetFloatv(GL_VIEWPORT, viewport_size);
+	immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
+
 	immUniform4f("color1", 0.4f, 0.4f, 0.4f, 1.0f);
 	immUniform4f("color2", 1.0f, 1.0f, 1.0f, 1.0f);
 	immUniform1f("dash_width", 8.0f);




More information about the Bf-blender-cvs mailing list