[Bf-blender-cvs] [9d01058d438] greasepencil-object: Migrate scale pixel code to vertex shader

Antonio Vazquez noreply at git.blender.org
Tue May 16 18:37:55 CEST 2017


Commit: 9d01058d438e6ba96dbb69434dad7ade8dcc7569
Author: Antonio Vazquez
Date:   Tue May 16 18:37:41 2017 +0200
Branches: greasepencil-object
https://developer.blender.org/rB9d01058d438e6ba96dbb69434dad7ade8dcc7569

Migrate scale pixel code to vertex shader

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

M	source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
M	source/blender/draw/engines/gpencil/gpencil_engine.c
M	source/blender/draw/engines/gpencil/gpencil_engine.h
M	source/blender/draw/engines/gpencil/gpencil_geom.c
M	source/blender/draw/engines/gpencil/shaders/gpencil_stroke_vert.glsl

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

diff --git a/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c b/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
index f3ca9ccc3d4..6736859714f 100644
--- a/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
+++ b/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
@@ -222,14 +222,24 @@ DRWShadingGroup *DRW_gpencil_shgroup_point_volumetric_create(DRWPass *pass, GPUS
 }
 
 /* create shading group for strokes */
-static DRWShadingGroup *DRW_gpencil_shgroup_stroke_create(DRWPass *pass, GPUShader *shader)
+DRWShadingGroup *DRW_gpencil_shgroup_stroke_create(GPENCIL_Data *vedata, DRWPass *pass, GPUShader *shader)
 {
+	GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
+
 	const float *viewport_size = DRW_viewport_size_get();
+	const DRWContextState *draw_ctx = DRW_context_state_get();
+	RegionView3D *rv3d = draw_ctx->rv3d;
 
 	/* e_data.gpencil_stroke_sh */
 	DRWShadingGroup *grp = DRW_shgroup_create(shader, pass);
 	DRW_shgroup_uniform_vec2(grp, "Viewport", viewport_size, 1);
 
+	DRW_shgroup_uniform_float(grp, "pixsize", &rv3d->pixsize, 1);
+	DRW_shgroup_uniform_float(grp, "pixelsize", &U.pixelsize, 1);
+
+	stl->storage->is_persp = rv3d->is_persp ? 1 : 0;
+	DRW_shgroup_uniform_int(grp, "is_persp", &stl->storage->is_persp, 1);
+
 	return grp;
 }
 
@@ -242,15 +252,6 @@ DRWShadingGroup *DRW_gpencil_shgroup_edit_volumetric_create(DRWPass *pass, GPUSh
 	return grp;
 }
 
-/* create shading group for drawing strokes in buffer */
-DRWShadingGroup *DRW_gpencil_shgroup_drawing_stroke_create(DRWPass *pass, GPUShader *shader)
-{
-	/* e_data.gpencil_stroke_sh */
-	DRWShadingGroup *grp = DRW_shgroup_create(shader, pass);
-	DRW_shgroup_uniform_vec2(grp, "Viewport", DRW_viewport_size_get(), 1);
-	return grp;
-}
-
 /* create shading group for drawing fill in buffer */
 DRWShadingGroup *DRW_gpencil_shgroup_drawing_fill_create(DRWPass *pass, GPUShader *shader)
 {
@@ -319,7 +320,7 @@ static void gpencil_draw_strokes(GpencilBatchCache *cache, GPENCIL_e_data *e_dat
 				id = stl->storage->pal_id;
 				stl->storage->materials[id] = gps->palcolor;
 				stl->storage->shgrps_fill[id] = DRW_gpencil_shgroup_fill_create(vedata, psl->stroke_pass, e_data->gpencil_fill_sh, gps->palcolor, id);
-				stl->storage->shgrps_stroke[id] = DRW_gpencil_shgroup_stroke_create(psl->stroke_pass, e_data->gpencil_stroke_sh);
+				stl->storage->shgrps_stroke[id] = DRW_gpencil_shgroup_stroke_create(vedata, psl->stroke_pass, e_data->gpencil_stroke_sh);
 				++stl->storage->pal_id;
 			}
 
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.c b/source/blender/draw/engines/gpencil/gpencil_engine.c
index d433a13e7ca..0e338fffa65 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.c
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.c
@@ -142,7 +142,7 @@ static void GPENCIL_cache_init(void *vedata)
 
 		/* drawing buffer pass */
 		psl->drawing_pass = DRW_pass_create("Gpencil Drawing Pass", state);
-		stl->g_data->shgrps_drawing_stroke = DRW_gpencil_shgroup_drawing_stroke_create(psl->drawing_pass, e_data.gpencil_stroke_sh);
+		stl->g_data->shgrps_drawing_stroke = DRW_gpencil_shgroup_stroke_create(vedata, psl->drawing_pass, e_data.gpencil_stroke_sh);
 		stl->g_data->shgrps_drawing_fill = DRW_gpencil_shgroup_drawing_fill_create(psl->drawing_pass, e_data.gpencil_drawing_fill_sh);
 	}
 }
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.h b/source/blender/draw/engines/gpencil/gpencil_engine.h
index 03559d7d53d..606a91ba815 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.h
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.h
@@ -33,7 +33,7 @@
 
  /* *********** LISTS *********** */
 typedef struct GPENCIL_Storage {
-	int pal_id;
+	int pal_id; /* total elements */
 	int t_mix[MAX_GPENCIL_MAT];
 	int t_flip[MAX_GPENCIL_MAT];
 	int t_clamp[MAX_GPENCIL_MAT];
@@ -42,6 +42,7 @@ typedef struct GPENCIL_Storage {
 	DRWShadingGroup *shgrps_fill[MAX_GPENCIL_MAT];
 	DRWShadingGroup *shgrps_stroke[MAX_GPENCIL_MAT];
 	float unit_matrix[4][4];
+	int is_persp;   /* rv3d->is_persp (1-yes) */
 } GPENCIL_Storage;
 
 /* keep it under MAX_STORAGE */
@@ -117,9 +118,9 @@ typedef struct GpencilBatchCache {
 	int cache_idx;   /* current slot index */
 } GpencilBatchCache;
 
+struct DRWShadingGroup *DRW_gpencil_shgroup_stroke_create(struct GPENCIL_Data *vedata, struct DRWPass *pass, struct GPUShader *shader);
 struct DRWShadingGroup *DRW_gpencil_shgroup_point_volumetric_create(struct DRWPass *pass, struct GPUShader *shader);
 struct DRWShadingGroup *DRW_gpencil_shgroup_edit_volumetric_create(struct DRWPass *pass, struct GPUShader *shader);
-struct DRWShadingGroup *DRW_gpencil_shgroup_drawing_stroke_create(struct DRWPass *pass, struct GPUShader *shader);
 struct DRWShadingGroup *DRW_gpencil_shgroup_drawing_fill_create(struct DRWPass *pass, struct GPUShader *shader);
 
 void DRW_gpencil_populate_datablock(struct GPENCIL_e_data *e_data, void *vedata, struct Scene *scene, struct Object *ob, struct ToolSettings *ts, struct bGPdata *gpd);
diff --git a/source/blender/draw/engines/gpencil/gpencil_geom.c b/source/blender/draw/engines/gpencil/gpencil_geom.c
index e30143f5cbd..8e6d39ab36a 100644
--- a/source/blender/draw/engines/gpencil/gpencil_geom.c
+++ b/source/blender/draw/engines/gpencil/gpencil_geom.c
@@ -49,23 +49,6 @@
 
 #include "gpencil_engine.h"
 
-/* get pixel scale size at stroke point location */
-static float get_pixel_scale(RegionView3D *rv3d, float point[3])
-{
-	const float defaultpixsize = rv3d->pixsize * U.pixelsize;
-	const float pixsize = ED_view3d_pixel_size(rv3d, point);
-	float scale_thickness = 0.0f;
-	if (rv3d->is_persp) {
-		/* need a factor to mimmic old glLine size, 10.0f works fine */
-		scale_thickness = (defaultpixsize / pixsize) * 10.0f;
-	}
-	else {
-		scale_thickness = (1.0f / pixsize) / 100.0f;
-	}
-
-	return scale_thickness;
-}
-
 /* set stroke point to vbo */
 static void gpencil_set_stroke_point(RegionView3D *rv3d, VertexBuffer *vbo, float matrix[4][4], const bGPDspoint *pt, int idx,
 						    unsigned int pos_id, unsigned int color_id,
@@ -81,8 +64,7 @@ static void gpencil_set_stroke_point(RegionView3D *rv3d, VertexBuffer *vbo, floa
 
 	/* the thickness of the stroke must be affected by zoom, so a pixel scale is calculated */
 	mul_v3_m4v3(viewfpt, matrix, &pt->x);
-	float scale_thickness = get_pixel_scale(rv3d, viewfpt);
-	float thick = max_ff(pt->pressure * thickness * scale_thickness, 1.0f);
+	float thick = max_ff(pt->pressure * thickness, 1.0f);
 	VertexBuffer_set_attrib(vbo, thickness_id, idx, &thick);
 	
 	VertexBuffer_set_attrib(vbo, pos_id, idx, &pt->x);
diff --git a/source/blender/draw/engines/gpencil/shaders/gpencil_stroke_vert.glsl b/source/blender/draw/engines/gpencil/shaders/gpencil_stroke_vert.glsl
index feabda2b3a5..d5c33ef40ef 100644
--- a/source/blender/draw/engines/gpencil/shaders/gpencil_stroke_vert.glsl
+++ b/source/blender/draw/engines/gpencil/shaders/gpencil_stroke_vert.glsl
@@ -1,23 +1,43 @@
 uniform mat4 ModelViewProjectionMatrix;
 
-#if __VERSION__ == 120
-	attribute vec3 pos;
-	attribute vec4 color;
-	attribute float thickness;
-
-	varying vec4 finalColor;
-#else
-	in vec3 pos;
-	in vec4 color;
-	in float thickness;
-
-  out vec4 finalColor;
-  out float finalThickness;
-#endif
+uniform float pixsize;   /* rv3d->pixsize */
+uniform float pixelsize; /* U.pixelsize */
+uniform int is_persp;    /* rv3d->is_persp (1-yes) */
+
+in vec3 pos;
+in vec4 color;
+in float thickness;
+
+out vec4 finalColor;
+out float finalThickness;
+
+#define TRUE 1
+
+/* port of c code function */
+float mul_project_m4_v3_zfac(mat4 mat, vec3 co)
+{
+	return (mat[0][3] * co[0]) +
+	       (mat[1][3] * co[1]) +
+	       (mat[2][3] * co[2]) + mat[3][3];
+}
 	
+float defaultpixsize = pixsize * pixelsize;
+
 void main(void)
 {
 	gl_Position = ModelViewProjectionMatrix * vec4( pos, 1.0 );
 	finalColor = color;
-	finalThickness = thickness;
+
+	float pixsize = mul_project_m4_v3_zfac(ModelViewProjectionMatrix, pos) * defaultpixsize;
+	float scale_thickness = 0.0f;
+	
+	if (is_persp == TRUE) {
+		/* need a factor to mimmic old glLine size, 10.0f works fine */
+		scale_thickness = (defaultpixsize / pixsize) * 10.0f;
+	}
+	else {
+		scale_thickness = (1.0f / pixsize) / 100.0f;
+	}
+
+	finalThickness = thickness * max(scale_thickness, 0.01f);
 } 
\ No newline at end of file




More information about the Bf-blender-cvs mailing list