[Bf-blender-cvs] [d7d4bca23be] blender2.8: Reworked version of dashed line shader.

Bastien Montagne noreply at git.blender.org
Mon May 1 16:34:35 CEST 2017


Commit: d7d4bca23be91ec5b0ce562d47a34ee49dd337b8
Author: Bastien Montagne
Date:   Mon May 1 16:21:53 2017 +0200
Branches: blender2.8
https://developer.blender.org/rBd7d4bca23be91ec5b0ce562d47a34ee49dd337b8

Reworked version of dashed line shader.

Using geometry shader allows us to get rid of the 'line origin' extra
vertex attribute, which means dashed shader no longer requires fiddling
with those vertex attributes definition, and, most importantly, does not
require anymore special drawing code!

As you can see, this makes code much simpler, and much less verbose,
especially in complex cases.

In addition, changed how dashes are handled, to have two 'modes', a
simple one with single color (using default "color" uniform name), and a
more advanced one allowing more complex and multi-color patterns.

Note that since GLSL 1.2 does not support geometry shaders, a hack was
added for now (which gives solid lines, but at least does not make
Blender crash).

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

M	intern/gawain/gawain/immediate.h
M	intern/gawain/src/immediate.c
M	source/blender/editors/animation/anim_markers.c
M	source/blender/editors/gpencil/gpencil_paint.c
M	source/blender/editors/include/UI_interface.h
M	source/blender/editors/interface/interface_draw.c
M	source/blender/editors/sculpt_paint/paint_stroke.c
M	source/blender/editors/space_clip/clip_draw.c
M	source/blender/editors/space_image/image_draw.c
M	source/blender/editors/space_nla/nla_draw.c
M	source/blender/editors/space_sequencer/sequencer_draw.c
M	source/blender/editors/space_text/text_draw.c
M	source/blender/editors/space_view3d/drawarmature.c
M	source/blender/editors/space_view3d/view3d_draw.c
M	source/blender/editors/space_view3d/view3d_ruler.c
M	source/blender/editors/transform/transform.c
M	source/blender/editors/transform/transform_constraints.c
M	source/blender/editors/util/ed_util.c
M	source/blender/editors/uvedit/uvedit_draw.c
M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/intern/gpu_shader.c
M	source/blender/gpu/shaders/gpu_shader_2D_line_dashed_frag.glsl
A	source/blender/gpu/shaders/gpu_shader_2D_line_dashed_geom.glsl
A	source/blender/gpu/shaders/gpu_shader_2D_line_dashed_legacy_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_2D_line_dashed_vert.glsl
D	source/blender/gpu/shaders/gpu_shader_3D_line_dashed_frag.glsl
A	source/blender/gpu/shaders/gpu_shader_3D_line_dashed_legacy_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_3D_line_dashed_vert.glsl
M	source/blender/windowmanager/intern/wm_gesture.c

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

diff --git a/intern/gawain/gawain/immediate.h b/intern/gawain/gawain/immediate.h
index 471d2468aea..be09c9baef8 100644
--- a/intern/gawain/gawain/immediate.h
+++ b/intern/gawain/gawain/immediate.h
@@ -90,6 +90,7 @@ void immUniform3fv(const char* name, const float data[3]);
 void immUniformArray3fv(const char* name, const float *data, int count);
 void immUniform4f(const char* name, float x, float y, float z, float w);
 void immUniform4fv(const char* name, const float data[4]);
+void immUniformArray4fv(const char* bare_name, const float *data, int count);
 void immUniformMatrix4fv(const char* name, const float data[4][4]);
 
 
diff --git a/intern/gawain/src/immediate.c b/intern/gawain/src/immediate.c
index f7c2a172c9b..4b39cfb63f7 100644
--- a/intern/gawain/src/immediate.c
+++ b/intern/gawain/src/immediate.c
@@ -807,6 +807,24 @@ void immUniform4fv(const char* name, const float data[4])
 	glUniform4fv(uniform->location, 1, data);
 	}
 
+void immUniformArray4fv(const char* bare_name, const float *data, int count)
+	{
+	// look up "name[0]" when given "name"
+	const size_t len = strlen(bare_name);
+#if TRUST_NO_ONE
+	assert(len <= MAX_UNIFORM_NAME_LEN);
+#endif
+	char name[MAX_UNIFORM_NAME_LEN];
+	strcpy(name, bare_name);
+	name[len + 0] = '[';
+	name[len + 1] = '0';
+	name[len + 2] = ']';
+	name[len + 3] = '\0';
+
+	GET_UNIFORM
+	glUniform4fv(uniform->location, count, data);
+	}
+
 void immUniformMatrix4fv(const char* name, const float data[4][4])
 	{
 	GET_UNIFORM
diff --git a/source/blender/editors/animation/anim_markers.c b/source/blender/editors/animation/anim_markers.c
index dcf52404a88..4566f0d9837 100644
--- a/source/blender/editors/animation/anim_markers.c
+++ b/source/blender/editors/animation/anim_markers.c
@@ -353,7 +353,6 @@ static void draw_marker(
 	{
 		VertexFormat *format = immVertexFormat();
 		uint pos = VertexFormat_add_attrib(format, "pos", COMP_F32, 2, KEEP_FLOAT);
-		uint line_origin = VertexFormat_add_attrib(format, "line_origin", COMP_F32, 2, KEEP_FLOAT);
 
 		immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR);
 
@@ -362,17 +361,15 @@ static void draw_marker(
 		immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC);
 
 		if (marker->flag & SELECT) {
-			immUniform4f("color1", 1.0f, 1.0f, 1.0f, 0.38f);
+			immUniformColor4f(1.0f, 1.0f, 1.0f, 0.38f);
 		}
 		else {
-			immUniform4f("color1", 0.0f, 0.0f, 0.0f, 0.38f);
+			immUniformColor4f(0.0f, 0.0f, 0.0f, 0.38f);
 		}
-		immUniform4f("color2", 0.0f, 0.0f, 0.0f, 0.0f);
 		immUniform1f("dash_width", 6.0f);
-		immUniform1f("dash_width_on", 3.0f);
+		immUniform1f("dash_factor", 0.5f);
 
 		immBegin(PRIM_LINES, 2);
-		immAttrib2f(line_origin, xpos + 0.5f, 12.0f);
 		immVertex2f(pos, xpos + 0.5f, 12.0f);
 		immVertex2f(pos, xpos + 0.5f, (v2d->cur.ymax + 12.0f) * yscale);
 		immEnd();
diff --git a/source/blender/editors/gpencil/gpencil_paint.c b/source/blender/editors/gpencil/gpencil_paint.c
index b4092a73df8..6659510c6cc 100644
--- a/source/blender/editors/gpencil/gpencil_paint.c
+++ b/source/blender/editors/gpencil/gpencil_paint.c
@@ -1862,25 +1862,21 @@ static void gpencil_draw_eraser(bContext *UNUSED(C), int x, int y, void *p_ptr)
 
 		immUnbindProgram();
 
-		format = immVertexFormat();
-		const uint shdr_dashed_pos = VertexFormat_add_attrib(format, "pos", COMP_F32, 2, KEEP_FLOAT);
-		const uint shdr_dashed_origin = VertexFormat_add_attrib(format, "line_origin", COMP_F32, 2, KEEP_FLOAT);
-
 		immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR);
 
 		float viewport_size[4];
 		glGetFloatv(GL_VIEWPORT, viewport_size);
 		immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
 
-		immUniform4f("color1", 1.0f, 0.39f, 0.39f, 0.78f);
-		immUniform4f("color2", 0.0f, 0.0f, 0.0f, 0.0f);
+		immUniformColor4f(1.0f, 0.39f, 0.39f, 0.78f);
+		immUniform1i("num_colors", 0);  /* "simple" mode */
 		immUniform1f("dash_width", 12.0f);
-		immUniform1f("dash_width_on", 6.0f);
+		immUniform1f("dash_factor", 0.5f);
 
-		imm_draw_circle_wire_dashed(shdr_dashed_pos, shdr_dashed_origin, x, y, p->radius,
-		                            /* XXX Dashed shader gives bad results with sets of small segments currently,
-		                             *     temp hack around the issue. :( */
-		                            max_ii(8, p->radius / 2));  /* was fixed 40 */
+		imm_draw_circle_wire(shdr_pos, x, y, p->radius,
+		                     /* XXX Dashed shader gives bad results with sets of small segments currently,
+		                      *     temp hack around the issue. :( */
+		                     max_ii(8, p->radius / 2));  /* was fixed 40 */
 
 		immUnbindProgram();
 
diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h
index 3293b061e76..904067839e2 100644
--- a/source/blender/editors/include/UI_interface.h
+++ b/source/blender/editors/include/UI_interface.h
@@ -323,8 +323,7 @@ void UI_draw_roundbox_shade_y(bool filled, float minx, float miny, float maxx, f
 void UI_draw_box_shadow(unsigned char alpha, float minx, float miny, float maxx, float maxy);
 void UI_draw_text_underline(int pos_x, int pos_y, int len, int height, const float color[4]);
 
-void UI_draw_safe_areas(
-        uint pos, uint line_origin, float x1, float x2, float y1, float y2,
+void UI_draw_safe_areas(uint pos, float x1, float x2, float y1, float y2,
         const float title_aspect[2], const float action_aspect[2]);
 
 /* state for scrolldrawing */
diff --git a/source/blender/editors/interface/interface_draw.c b/source/blender/editors/interface/interface_draw.c
index bf821c082f2..3553b5f4d4b 100644
--- a/source/blender/editors/interface/interface_draw.c
+++ b/source/blender/editors/interface/interface_draw.c
@@ -585,7 +585,7 @@ void ui_draw_but_IMAGE(ARegion *UNUSED(ar), uiBut *but, uiWidgetColors *UNUSED(w
  * The next 4 parameters are the offsets for the view, not the zones.
  */
 void UI_draw_safe_areas(
-        uint pos, uint line_origin, float x1, float x2, float y1, float y2,
+        uint pos, float x1, float x2, float y1, float y2,
         const float title_aspect[2], const float action_aspect[2])
 {
 	const float size_x_half = (x2 - x1) * 0.5f;
@@ -604,7 +604,7 @@ void UI_draw_safe_areas(
 			float maxx = x2 - margin_x;
 			float maxy = y2 - margin_y;
 
-			imm_draw_line_box_dashed(pos, line_origin, minx, miny, maxx, maxy);
+			imm_draw_line_box(pos, minx, miny, maxx, maxy);
 		}
 	}
 }
@@ -1223,33 +1223,23 @@ static void ui_draw_colorband_handle(
 	if (active || half_width < min_width) {
 		immUnbindProgram();
 
-		VertexFormat *format = immVertexFormat();
-		const uint shdr_dashed_pos = VertexFormat_add_attrib(format, "pos", COMP_F32, 2, KEEP_FLOAT);
-		const uint shdr_dashed_origin = VertexFormat_add_attrib(format, "line_origin", COMP_F32, 2, KEEP_FLOAT);
-
 		immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR);
 
 		float viewport_size[4];
 		glGetFloatv(GL_VIEWPORT, viewport_size);
 		immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC);
 
-		immUniform4f("color1", 0.8f, 0.8f, 0.8f, 1.0f);
-		immUniform4f("color2", 0.0f, 0.0f, 0.0f, 0.0f);
+		immUniform1i("num_colors", 2);  /* "advanced" mode */
+		immUniformArray4fv("colors", (float *)(float[][4]){{0.8f, 0.8f, 0.8f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f}}, 2);
 		immUniform1f("dash_width", active ? 4.0f : 2.0f);
-		immUniform1f("dash_width_on", active ? 2.0f : 1.0f);
 
 		immBegin(PRIM_LINES, 2);
-		immAttrib2f(shdr_dashed_origin, x, y1);
-		immVertex2f(shdr_dashed_pos, x, y1);
-		immVertex2f(shdr_dashed_pos, x, y2);
+		immVertex2f(shdr_pos, x, y1);
+		immVertex2f(shdr_pos, x, y2);
 		immEnd();
 
 		immUnbindProgram();
 
-		const uint pos = VertexFormat_add_attrib(immVertexFormat(), "pos", COMP_F32, 2, KEEP_FLOAT);
-		BLI_assert(pos == shdr_pos);
-		UNUSED_VARS_NDEBUG(pos);
-
 		immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
 
 		/* hide handles when zoomed out too far */
diff --git a/source/blender/editors/sculpt_paint/paint_stroke.c b/source/blender/editors/sculpt_paint/paint_stroke.c
index b6c4febe607..e53cf05bb15 100644
--- a/source/blender/editors/sculpt_paint/paint_stroke.c
+++ b/source/blender/editors/sculpt_paint/paint_stroke.c
@@ -169,9 +169,7 @@ static void paint_draw_line_cursor(bContext *C, int x, int y, void *customdata)
 
 	glEnable(GL_LINE_SMOOTH);
 
-	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);
+	uint shdr_pos = VertexFormat_add_attrib(immVertexFormat(), "pos", COMP_F32, 2, KEEP_FLOAT);
 
 	immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR);
 
@@ -179,23 +177,20 @@ static void paint_draw_line_cursor(bContext *C, int x, int y, void *customdata)
 	glGetFloatv(GL_VIEWPORT, viewport_size);
 	immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
 
+	immUniform1i("num_colors", 2);  /* "advanced" mode */
 	const float alpha = (float)paint->paint_cursor_col[3] / 255.0f;
-	immUniform4f("color1", 0.0f, 0.0f, 0.0f, alpha);
-	immUniform4f("color2", 1.0f, 1.0f, 1.0f, alpha);
+	immUniformArray4fv("colors", (float *)(float[][4]){{0.0f, 0.0f, 0.0f, alpha}, {1.0f, 1.0f, 1.0f, alpha}}, 2);
 	immUniform1f("dash_width", 6.0f);
-	immUniform1f("dash_width_on", 3.0f);
 
 	immBegin(PRIM_LINES, 2);
 
 	if (stroke->constrain_line) {
-		immAttrib2f(line_origin, stroke->last_mouse_position[0], stroke->last_mouse_position[1]);
-		immVertex2f(pos, stroke->last_mouse_position[0], stroke->last_mouse_position[1]);
-		immVertex2f(pos, stroke->constrained_pos[0], stroke->constrained_pos[1]);
+		immVertex2f(shdr_pos, stroke->last_mouse_position[0], stroke->last_mouse_position[1]);
+		immVertex2f(shdr_pos, stroke->constrained_pos[0], stroke->constrained_pos[1]);
 	}
 	else {
-		immAttrib2f(line_origin, stroke->last_mouse_position[0], stroke->last_mouse_position[1]);
-		immVertex2f(pos, stroke->last_mouse_position[0], stroke->last_mouse_position[1]);
-		immVertex2f(pos, x, y);
+		immVertex2f(shdr_pos, stroke->last_mouse_position[0], stroke->last_mouse_position[1]);
+		immVertex2f(shdr_pos, x, y);
 	}
 
 	immEnd();
diff --git a/source/bl

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list