[Bf-blender-cvs] [7edf993ba6e] greasepencil-object: Try to use dynamic memory for shading groups

Antonio Vazquez noreply at git.blender.org
Thu Jun 1 10:37:42 CEST 2017


Commit: 7edf993ba6e07ced0e6e26723a4ae64bcdf2c18c
Author: Antonio Vazquez
Date:   Thu Jun 1 10:35:24 2017 +0200
Branches: greasepencil-object
https://developer.blender.org/rB7edf993ba6e07ced0e6e26723a4ae64bcdf2c18c

Try to use dynamic memory for shading groups

The reallocation of the memory produce weird things, so we keep allocation maximum number while we found better solution.

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

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

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

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 294b924c587..f3a07f6bcf8 100644
--- a/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
+++ b/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
@@ -235,8 +235,8 @@ static DRWShadingGroup *DRW_gpencil_shgroup_fill_create(GPENCIL_Data *vedata, DR
 	/* e_data.gpencil_fill_sh */
 	DRWShadingGroup *grp = DRW_shgroup_create(shader, pass);
 	DRW_shgroup_uniform_vec4(grp, "color2", palcolor->scolor, 1);
-	stl->storage->shgroups[id].fill_style = palcolor->fill_style;
-	DRW_shgroup_uniform_int(grp, "fill_type", &stl->storage->shgroups[id].fill_style, 1);
+	stl->shgroups[id].fill_style = palcolor->fill_style;
+	DRW_shgroup_uniform_int(grp, "fill_type", &stl->shgroups[id].fill_style, 1);
 	DRW_shgroup_uniform_float(grp, "mix_factor", &palcolor->mix_factor, 1);
 
 	DRW_shgroup_uniform_float(grp, "g_angle", &palcolor->g_angle, 1);
@@ -250,11 +250,11 @@ static DRWShadingGroup *DRW_gpencil_shgroup_fill_create(GPENCIL_Data *vedata, DR
 	DRW_shgroup_uniform_vec2(grp, "t_shift", palcolor->t_shift, 1);
 	DRW_shgroup_uniform_float(grp, "t_opacity", &palcolor->t_opacity, 1);
 
-	stl->storage->shgroups[id].t_mix = palcolor->flag & PAC_COLOR_TEX_MIX ? 1 : 0;
-	DRW_shgroup_uniform_int(grp, "t_mix", &stl->storage->shgroups[id].t_mix, 1);
+	stl->shgroups[id].t_mix = palcolor->flag & PAC_COLOR_TEX_MIX ? 1 : 0;
+	DRW_shgroup_uniform_int(grp, "t_mix", &stl->shgroups[id].t_mix, 1);
 
-	stl->storage->shgroups[id].t_flip = palcolor->flag & PAC_COLOR_FLIP_FILL ? 1 : 0;
-	DRW_shgroup_uniform_int(grp, "t_flip", &stl->storage->shgroups[id].t_flip, 1);
+	stl->shgroups[id].t_flip = palcolor->flag & PAC_COLOR_FLIP_FILL ? 1 : 0;
+	DRW_shgroup_uniform_int(grp, "t_flip", &stl->shgroups[id].t_flip, 1);
 
 	DRW_shgroup_uniform_int(grp, "xraymode", (const int *) &gpd->xray_mode, 1);
 
@@ -276,8 +276,8 @@ static DRWShadingGroup *DRW_gpencil_shgroup_fill_create(GPENCIL_Data *vedata, DR
 			GPUTexture *texture = GPU_texture_from_blender(palcolor->ima, &iuser, GL_TEXTURE_2D, true, 0.0, 0);
 			DRW_shgroup_uniform_texture(grp, "myTexture", texture);
 
-			stl->storage->shgroups[id].t_clamp = palcolor->flag & PAC_COLOR_TEX_CLAMP ? 1 : 0;
-			DRW_shgroup_uniform_int(grp, "t_clamp", &stl->storage->shgroups[id].t_clamp, 1);
+			stl->shgroups[id].t_clamp = palcolor->flag & PAC_COLOR_TEX_CLAMP ? 1 : 0;
+			DRW_shgroup_uniform_int(grp, "t_clamp", &stl->shgroups[id].t_clamp, 1);
 
 			BKE_image_release_ibuf(image, ibuf, NULL);
 		}
@@ -452,26 +452,34 @@ static void gpencil_draw_strokes(GpencilBatchCache *cache, GPENCIL_e_data *e_dat
 	/* get parent matrix and save as static data */
 	ED_gpencil_parent_location(ob, gpd, gpl, viewmatrix);
 	copy_m4_m4(gpf->viewmatrix, viewmatrix);
-	
+
 	for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
 		/* check if stroke can be drawn */
 		if (gpencil_can_draw_stroke(rv3d, gpf, gps) == false) {
 			continue;
 		}
-
 		/* limit the number of shading groups */
 		if (stl->storage->pal_id >= GPENCIL_MAX_SHGROUPS) {
 			continue;
 		}
-
+#if 0   /* if we use the reallocate the shading group is doing weird thing, so disable while find a solution 
+		   and allocate the max size on cache_init */
+		/* realloc memory */
+		GPENCIL_shgroup *p = NULL;
+		int size = stl->storage->pal_id + 1;
+		p = MEM_recallocN(stl->shgroups, sizeof(struct GPENCIL_shgroup) * size);
+		if (p != NULL) {
+			stl->shgroups = p;
+		}
+#endif
 		if (gps->totpoints > 1) {
 			int id = stl->storage->pal_id;
-			stl->storage->shgroups[id].shgrps_fill = DRW_gpencil_shgroup_fill_create(vedata, psl->stroke_pass, e_data->gpencil_fill_sh, gpd, gps->palcolor, id);
-			stl->storage->shgroups[id].shgrps_stroke = DRW_gpencil_shgroup_stroke_create(vedata, psl->stroke_pass, e_data->gpencil_stroke_sh, gpd);
+			stl->shgroups[id].shgrps_fill = DRW_gpencil_shgroup_fill_create(vedata, psl->stroke_pass, e_data->gpencil_fill_sh, gpd, gps->palcolor, id);
+			stl->shgroups[id].shgrps_stroke = DRW_gpencil_shgroup_stroke_create(vedata, psl->stroke_pass, e_data->gpencil_stroke_sh, gpd);
 			++stl->storage->pal_id;
-
-			fillgrp = stl->storage->shgroups[id].shgrps_fill;
-			strokegrp = stl->storage->shgroups[id].shgrps_stroke;
+			
+			fillgrp = stl->shgroups[id].shgrps_fill;
+			strokegrp = stl->shgroups[id].shgrps_stroke;
 		}
 		/* fill */
 		gpencil_add_fill_shgroup(cache, fillgrp, gpd, gpl, gpf, gps, tintcolor, onion, custonion);
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.c b/source/blender/draw/engines/gpencil/gpencil_engine.c
index ce979a77999..859b4165d5c 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.c
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.c
@@ -107,7 +107,14 @@ static void GPENCIL_cache_init(void *vedata)
 		stl->g_data->scene_draw = false;
 		stl->storage->xray = GP_XRAY_FRONT; /* used for drawing */
 	}
-
+	if (!stl->shgroups) {
+		/* Alloc maximum size because count strokes is very slow and can be very complex due onion skinning
+		   Note: I tried to allocate only one and using realloc, increase the size when read a new strokes 
+		         in cache_finish, but the realloc produce weird things on screen, so we keep as is while we
+				 found a better solution
+		 */
+		stl->shgroups = MEM_mallocN(sizeof(GPENCIL_shgroup) * GPENCIL_MAX_SHGROUPS, "GPENCIL_shgroup");
+	}
 	{
 		/* init gp objects cache */
 		stl->g_data->gp_cache_used = 0;
@@ -118,7 +125,6 @@ static void GPENCIL_cache_init(void *vedata)
 		DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND | DRW_STATE_DEPTH_LESS;
 		psl->stroke_pass = DRW_pass_create("Gpencil Stroke Pass", state);
 		stl->storage->pal_id = 0;
-		memset(stl->storage->shgroups, 0, sizeof(GPENCIL_shgroup *) * GPENCIL_MAX_SHGROUPS);
 		stl->g_data->shgrps_point_volumetric = DRW_gpencil_shgroup_point_volumetric_create(psl->stroke_pass, e_data.gpencil_volumetric_sh);
 
 		state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND;
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.h b/source/blender/draw/engines/gpencil/gpencil_engine.h
index c208efc3121..cd173262ff8 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.h
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.h
@@ -36,7 +36,7 @@
  /* *********** OBJECTS CACHE *********** */
  /* used to sort gpencil objects */
 typedef struct tGPencilObjectCache {
-	Object *ob;
+	struct Object *ob;
 	float zdepth;
 } tGPencilObjectCache;
 
@@ -46,13 +46,12 @@ typedef struct GPENCIL_shgroup {
 	int t_flip;
 	int t_clamp;
 	int fill_style;
-	DRWShadingGroup *shgrps_fill;
-	DRWShadingGroup *shgrps_stroke;
+	struct DRWShadingGroup *shgrps_fill;
+	struct DRWShadingGroup *shgrps_stroke;
 } GPENCIL_shgroup;
 
 typedef struct GPENCIL_Storage {
 	int pal_id; /* total elements */
-	GPENCIL_shgroup shgroups[GPENCIL_MAX_SHGROUPS];
 	float unit_matrix[4][4];
 	int is_persp;   /* rv3d->is_persp (1-yes) */
 	int xray;
@@ -61,6 +60,7 @@ typedef struct GPENCIL_Storage {
 typedef struct GPENCIL_StorageList {
 	struct GPENCIL_Storage *storage;
 	struct g_data *g_data;
+	struct GPENCIL_shgroup *shgroups;
 } GPENCIL_StorageList;
 
 typedef struct GPENCIL_PassList {
@@ -79,23 +79,23 @@ typedef struct GPENCIL_TextureList {
 
 typedef struct GPENCIL_Data {
 	void *engine_type; /* Required */
-	GPENCIL_FramebufferList *fbl;
-	GPENCIL_TextureList *txl;
-	GPENCIL_PassList *psl;
-	GPENCIL_StorageList *stl;
+	struct GPENCIL_FramebufferList *fbl;
+	struct GPENCIL_TextureList *txl;
+	struct GPENCIL_PassList *psl;
+	struct GPENCIL_StorageList *stl;
 } GPENCIL_Data;
 
 /* *********** STATIC *********** */
 typedef struct g_data {
-	DRWShadingGroup *shgrps_edit_volumetric;
-	DRWShadingGroup *shgrps_point_volumetric;
-	DRWShadingGroup *shgrps_drawing_stroke;
-	DRWShadingGroup *shgrps_drawing_fill;
+	struct DRWShadingGroup *shgrps_edit_volumetric;
+	struct DRWShadingGroup *shgrps_point_volumetric;
+	struct DRWShadingGroup *shgrps_drawing_stroke;
+	struct DRWShadingGroup *shgrps_drawing_fill;
 	bool scene_draw;
 
 	int gp_cache_used;
 	int gp_cache_size;
-	tGPencilObjectCache *gp_object_cache;
+	struct tGPencilObjectCache *gp_object_cache;
 } g_data; /* Transient data */
 
 typedef struct GPENCIL_e_data {




More information about the Bf-blender-cvs mailing list