[Bf-blender-cvs] [d969192] master: Wide lines + line stipple deprecated API replacement

Alexander Romanov noreply at git.blender.org
Fri Apr 8 10:08:36 CEST 2016


Commit: d969192fbe6129021f0399cc35c6c2be6aa72eda
Author: Alexander Romanov
Date:   Fri Apr 8 10:58:40 2016 +0300
Branches: master
https://developer.blender.org/rBd969192fbe6129021f0399cc35c6c2be6aa72eda

Wide lines + line stipple deprecated API replacement

The patch contains an implementation of the wide lines and the line stipple that is necessary for OpenGL upgrade.

For the implementation I have chosen the geometry shader because it required minimum changes for the wrapper calls and such implementation is the best for the "basic shader" architecture.

There are few shortcomings that can be corrected in future. They all are related to the fact that the lines in one strip are not connected with each other. So the stipple pattern is not continuous on the common vertex of two lines. There is also no continuity of form (each line is an independent rectangular).
But the advantage is that even outdated glBegin, glVertex work! Though with the above restrictions.
Continuity of form and stipple can be implemented with additional attributes, and it will require more changes in calls.

At the moment, the patch replaces calls for some "gestures". It works satisfactorily for "cross" or "rectangular" and not so good for "lasso" and "circle" due to the above-mentioned shortcomings.

Don't forget to set USE_GLSL to true for testing.

Alexander Romanov (Blend4Web Team)

Reviewers: merwin, brecht

Reviewed By: merwin, brecht

Subscribers: aligorith, Evgeny_Rodygin, AlexKowel, yurikovelenov

Differential Revision: https://developer.blender.org/D1880

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

M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/GPU_basic_shader.h
M	source/blender/gpu/intern/gpu_basic_shader.c
M	source/blender/gpu/shaders/gpu_shader_basic_frag.glsl
A	source/blender/gpu/shaders/gpu_shader_basic_geom.glsl
M	source/blender/gpu/shaders/gpu_shader_basic_vert.glsl
M	source/blender/windowmanager/intern/wm_draw.c
M	source/blender/windowmanager/intern/wm_gesture.c
M	source/blender/windowmanager/intern/wm_subwindow.c

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

diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index 3b228c1..cfa0831 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -73,6 +73,7 @@ set(SRC
 	shaders/gpu_shader_sep_gaussian_blur_vert.glsl
 	shaders/gpu_shader_basic_frag.glsl
 	shaders/gpu_shader_basic_vert.glsl
+	shaders/gpu_shader_basic_geom.glsl
 	shaders/gpu_shader_vertex.glsl
 	shaders/gpu_shader_vsm_store_frag.glsl
 	shaders/gpu_shader_vsm_store_vert.glsl
@@ -105,6 +106,7 @@ data_to_c_simple(shaders/gpu_shader_sep_gaussian_blur_frag.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_sep_gaussian_blur_vert.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_basic_frag.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_basic_vert.glsl SRC)
+data_to_c_simple(shaders/gpu_shader_basic_geom.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_vertex.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_vertex_world.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_vsm_store_frag.glsl SRC)
diff --git a/source/blender/gpu/GPU_basic_shader.h b/source/blender/gpu/GPU_basic_shader.h
index f376850..df2da97 100644
--- a/source/blender/gpu/GPU_basic_shader.h
+++ b/source/blender/gpu/GPU_basic_shader.h
@@ -33,6 +33,7 @@
 #define __GPU_BASIC_SHADER_H__
 
 #include "BLI_utildefines.h"
+#include "GPU_glew.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -48,7 +49,8 @@ typedef enum GPUBasicShaderOption {
 
 	GPU_SHADER_SOLID_LIGHTING =   (1 << 4),   /* use faster lighting (set automatically) */
 	GPU_SHADER_STIPPLE =          (1 << 5),   /* use stipple */
-	GPU_SHADER_OPTIONS_NUM = 6,
+	GPU_SHADER_LINE =             (1 << 6),   /* draw lines */
+	GPU_SHADER_OPTIONS_NUM = 7,
 	GPU_SHADER_OPTION_COMBINATIONS = (1 << GPU_SHADER_OPTIONS_NUM)
 } GPUBasicShaderOption;
 
@@ -105,6 +107,8 @@ typedef struct GPULightData {
 void GPU_basic_shader_light_set(int light_num, GPULightData *light);
 void GPU_basic_shader_light_set_viewer(bool local);
 void GPU_basic_shader_stipple(GPUBasicShaderStipple stipple_id);
+void GPU_basic_shader_line_stipple(GLint stipple_factor, GLushort stipple_pattern);
+void GPU_basic_shader_line_width(float line_width);
 
 #ifdef __cplusplus
 }
diff --git a/source/blender/gpu/intern/gpu_basic_shader.c b/source/blender/gpu/intern/gpu_basic_shader.c
index 4a27965..abaf194 100644
--- a/source/blender/gpu/intern/gpu_basic_shader.c
+++ b/source/blender/gpu/intern/gpu_basic_shader.c
@@ -61,6 +61,8 @@ static struct {
 
 	int lights_enabled;
 	int lights_directional;
+	float line_width;
+	GLint viewport[4];
 } GPU_MATERIAL_STATE;
 
 
@@ -325,6 +327,8 @@ static GPUShader *gpu_basic_shader(int options)
 	/* glsl code */
 	extern char datatoc_gpu_shader_basic_vert_glsl[];
 	extern char datatoc_gpu_shader_basic_frag_glsl[];
+	extern char datatoc_gpu_shader_basic_geom_glsl[];
+	char *geom_glsl = NULL;
 	GPUShader *shader;
 
 	/* detect if we can do faster lighting for solid draw mode */
@@ -347,7 +351,10 @@ static GPUShader *gpu_basic_shader(int options)
 			strcat(defines, "#define USE_TEXTURE\n");
 		if (options & GPU_SHADER_STIPPLE)
 			strcat(defines, "#define USE_STIPPLE\n");
-
+		if (options & GPU_SHADER_LINE) {
+			strcat(defines, "#define DRAW_LINE\n");
+			geom_glsl = datatoc_gpu_shader_basic_geom_glsl;
+		}
 		if (options & GPU_SHADER_SOLID_LIGHTING)
 			strcat(defines, "#define USE_SOLID_LIGHTING\n");
 		else if (options & GPU_SHADER_LIGHTING)
@@ -356,7 +363,7 @@ static GPUShader *gpu_basic_shader(int options)
 		shader = GPU_shader_create(
 			datatoc_gpu_shader_basic_vert_glsl,
 			datatoc_gpu_shader_basic_frag_glsl,
-			NULL,
+			geom_glsl,
 			NULL,
 			defines, 0, 0, 0);
 		
@@ -377,6 +384,15 @@ static GPUShader *gpu_basic_shader(int options)
 	return shader;
 }
 
+static void GPU_basic_shader_uniform_autoset(GPUShader *shader, int options)
+{
+	if (options & GPU_SHADER_LINE) {
+		glGetIntegerv(GL_VIEWPORT, &GPU_MATERIAL_STATE.viewport[0]);
+		glUniform4iv(GPU_shader_get_uniform(shader, "viewport"), 1, &GPU_MATERIAL_STATE.viewport[0]);
+		glUniform1f(GPU_shader_get_uniform(shader, "line_width"), GPU_MATERIAL_STATE.line_width);
+	}
+}
+
 /* Bind / unbind */
 
 void GPU_basic_shader_bind(int options)
@@ -385,8 +401,10 @@ void GPU_basic_shader_bind(int options)
 		if (options) {
 			GPUShader *shader = gpu_basic_shader(options);
 
-			if (shader)
+			if (shader) {
 				GPU_shader_bind(shader);
+				GPU_basic_shader_uniform_autoset(shader, options);
+			}
 		}
 		else {
 			GPU_shader_unbind();
@@ -424,10 +442,19 @@ void GPU_basic_shader_bind(int options)
 			glDisable(GL_TEXTURE_2D);
 		}
 
-		if (options & GPU_SHADER_STIPPLE)
-			glEnable(GL_POLYGON_STIPPLE);
-		else if (bound_options & GPU_SHADER_STIPPLE)
-			glDisable(GL_POLYGON_STIPPLE);
+		if (options & GPU_SHADER_LINE) {
+			if (options & GPU_SHADER_STIPPLE)
+				glEnable(GL_LINE_STIPPLE);
+			else if (bound_options & GPU_SHADER_STIPPLE)
+				glDisable(GL_LINE_STIPPLE);
+		}
+		else {
+			if (options & GPU_SHADER_STIPPLE)
+				glEnable(GL_POLYGON_STIPPLE);
+			else if (bound_options & GPU_SHADER_STIPPLE)
+				glDisable(GL_POLYGON_STIPPLE);
+		}
+
 	}
 
 	GPU_MATERIAL_STATE.bound_options = options;
@@ -592,3 +619,27 @@ void GPU_basic_shader_stipple(GPUBasicShaderStipple stipple_id)
 		}
 	}
 }
+
+void GPU_basic_shader_line_width(float line_width)
+{
+	if (USE_GLSL) {
+		GPU_MATERIAL_STATE.line_width = line_width;
+		if (GPU_MATERIAL_STATE.bound_options & GPU_SHADER_LINE) {
+			glUniform1f(GPU_shader_get_uniform(gpu_basic_shader(GPU_MATERIAL_STATE.bound_options), "line_width"), line_width);
+		}
+	}
+	else {
+		glLineWidth(line_width);
+	}
+}
+
+void GPU_basic_shader_line_stipple(GLint stipple_factor, GLushort stipple_pattern)
+{
+	if (USE_GLSL) {
+		glUniform1i(GPU_shader_get_uniform(gpu_basic_shader(GPU_MATERIAL_STATE.bound_options), "stipple_factor"), stipple_factor);
+		glUniform1i(GPU_shader_get_uniform(gpu_basic_shader(GPU_MATERIAL_STATE.bound_options), "stipple_pattern"), stipple_pattern);
+	}
+	else {
+		glLineStipple(stipple_factor, stipple_pattern);
+	}
+}
diff --git a/source/blender/gpu/shaders/gpu_shader_basic_frag.glsl b/source/blender/gpu/shaders/gpu_shader_basic_frag.glsl
index 7b4df51..c7b29ee 100644
--- a/source/blender/gpu/shaders/gpu_shader_basic_frag.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_basic_frag.glsl
@@ -45,11 +45,20 @@ uniform sampler2D texture_map;
 
 #ifdef USE_STIPPLE
 uniform int stipple_id;
+#if defined(DRAW_LINE)
+varying in float t;
+uniform int stipple_pattern;
+#endif
 #endif
 
 void main()
 {
 #if defined(USE_STIPPLE)
+#if defined(DRAW_LINE)
+    /* GLSL 1.3 */
+    if (!bool((1 << int(mod(t, 16))) & stipple_pattern))
+        discard;
+#else
         /* We have to use mod function and integer casting.
          * This can be optimized further with the bitwise operations
          * when GLSL 1.3 is supported. */
@@ -123,9 +132,8 @@ void main()
                     discard;
             }
         }
-
-
-#endif
+#endif /* !DRAW_LINE */
+#endif /* USE_STIPPLE */
 
 #if defined(USE_SOLID_LIGHTING) || defined(USE_SCENE_LIGHTING)
 	/* compute normal */
diff --git a/source/blender/gpu/shaders/gpu_shader_basic_geom.glsl b/source/blender/gpu/shaders/gpu_shader_basic_geom.glsl
new file mode 100644
index 0000000..ffd747a
--- /dev/null
+++ b/source/blender/gpu/shaders/gpu_shader_basic_geom.glsl
@@ -0,0 +1,100 @@
+/*
+* Used the implementation of wide lines of Timo Suoranta (http://neure.dy.fi/wideline.html)
+*/
+
+#define PASSTHROUGH             0
+
+layout(lines) in;
+
+#if defined(DRAW_LINE)
+
+#if PASSTHROUGH
+layout(line_strip, max_vertices = 10) out;
+#else
+layout(triangle_strip, max_vertices = 6) out;
+#endif
+
+varying out float t;
+varying in vec4 varying_vertex_color_line[];
+varying out vec4 varying_vertex_color;
+
+uniform ivec4 viewport;
+uniform float line_width;
+uniform int stipple_factor;
+
+void main(void)
+{
+    vec2 window_size = viewport.zw;
+    vec4 start  = gl_in[0].gl_Position;
+    vec4 end    = gl_in[1].gl_Position;
+#if PASSTHROUGH
+    gl_Position = start; EmitVertex();
+    gl_Position = end; EmitVertex();
+    EndPrimitive();
+    return;
+#endif
+
+/*    t = 0                                 t = ~(len(end - start) + 2*line_width)
+ *    A-------------------------------------B
+ *    |        |                   |        |
+ *    |       side                 |        |
+ *    |        |                   |        |
+ *    |--axis--*start--------------*end-----|
+ *    |        |                   |        |
+ *    |        |                   |        |
+ *    |        |                   |        |
+ *    D-------------------------------------C
+ */
+
+    /* Clip the line before homogenization.
+     * Compute line start and end distances to nearplane in clipspace
+     * Distances are t0 = dot(start, plane) and t1 = dot(end, plane)
+     */
+    float t0 = start.z + start.w;
+    float t1 = end.z + end.w;
+    if(t0 < 0.0) {
+        if(t1 < 0.0) {
+            return;
+        }
+        start = mix(start, end, (0 - t0) / (t1 - t0));
+    }
+    if(t1 < 0.0) {
+        end = mix(start, end, (0 - t0) / (t1 - t0));
+    }
+
+    /* Compute line axis and side vector in screen space */
+    vec2 startInNDC     = start.xy / start.w;                      /* clip to NDC: homogenize and drop z */
+    vec2 endInNDC       = end.xy / end.w;
+    vec2 lineInNDC      = endInNDC - startInNDC;
+    vec2 lineInScreen   = lineInNDC * window_size;                 /* ndc to screen (direction vector) */
+
+    vec2 axisInScreen   = normalize(lineInScreen);
+    vec2 sideInScreen   = vec2(-axisInScreen.y, axisInScreen.x);   /* rotate */
+    vec2 axisInNDC      = axisInScreen / window_size;              /* screen to NDC */
+    vec2 sideInNDC      = sideInScreen / window_size;
+    vec4 axis           = vec4(axisInNDC, 0.0, 0.0) * line_width;  /* NDC to clip (delta vector) */
+    vec4 side           = vec4(sideInNDC, 0.0, 0.0) * line_width;
+
+    vec4 A = (start + (side - axis) * start.w);
+    vec4 B = (end   + (side + axis) * end.w);
+    vec4 C = (end   - (side - axis) * end.w);
+    vec4 D = (start - (side + axis) * start.w);
+
+    /* There is no relation between lines yet */
+    /* TODO Pass here t0 to make continuous pattern. */
+    t0 

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list