[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [51759] trunk/blender/source/blender: Fix for own r51737.

Bastien Montagne montagne29 at wanadoo.fr
Tue Oct 30 12:00:10 CET 2012


Revision: 51759
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=51759
Author:   mont29
Date:     2012-10-30 11:00:06 +0000 (Tue, 30 Oct 2012)
Log Message:
-----------
Fix for own r51737.

Refactoring of draw code showed another problem: The MCol we want to draw may change without dm rebuild (e.g. when enabling solid textured option)! Also, choosing which MCol layer to use in GPU code is stupid, different draw modes use different layers/order of precedence!

Solved this by adding a new colType parameter to GPU_color_setup, and removing any 'color choosing' code from gpu_buffers.c.

Revision Links:
--------------
    http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=51737

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/intern/cdderivedmesh.c
    trunk/blender/source/blender/gpu/GPU_buffers.h
    trunk/blender/source/blender/gpu/intern/gpu_buffers.c

Modified: trunk/blender/source/blender/blenkernel/intern/cdderivedmesh.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/cdderivedmesh.c	2012-10-30 09:29:01 UTC (rev 51758)
+++ trunk/blender/source/blender/blenkernel/intern/cdderivedmesh.c	2012-10-30 11:00:06 UTC (rev 51759)
@@ -614,15 +614,23 @@
 	CDDerivedMesh *cddm = (CDDerivedMesh *) dm;
 	MVert *mv = cddm->mvert;
 	MFace *mf = DM_get_tessface_data_layer(dm, CD_MFACE);
-	MCol *realcol = dm->getTessFaceDataArray(dm, CD_TEXTURE_MCOL);
 	float *nors = dm->getTessFaceDataArray(dm, CD_NORMAL);
 	MTFace *tf = DM_get_tessface_data_layer(dm, CD_MTFACE);
+	MCol *mcol;
 	int i, orig, *index = DM_get_tessface_data_layer(dm, CD_ORIGINDEX);
-	int startFace = 0 /*, lastFlag = 0xdeadbeef */ /* UNUSED */;
-	MCol *mcol = dm->getTessFaceDataArray(dm, CD_PREVIEW_MCOL);
-	if (!mcol)
-		mcol = dm->getTessFaceDataArray(dm, CD_MCOL);
+	int colType, startFace = 0;
 
+	colType = CD_TEXTURE_MCOL;
+	mcol = dm->getTessFaceDataArray(dm, colType);
+	if (!mcol) {
+		colType = CD_PREVIEW_MCOL;
+		mcol = dm->getTessFaceDataArray(dm, colType);
+	}
+	if (!mcol) {
+		colType = CD_MCOL;
+		mcol = dm->getTessFaceDataArray(dm, colType);
+	}
+
 	cdDM_update_normals_from_pbvh(dm);
 
 	if (GPU_buffer_legacy(dm)) {
@@ -699,25 +707,11 @@
 		}
 	}
 	else { /* use OpenGL VBOs or Vertex Arrays instead for better, faster rendering */
-		MCol *col = realcol;
-		if (!col)
-			col = mcol;
-
 		GPU_vertex_setup(dm);
 		GPU_normal_setup(dm);
 		GPU_uv_setup(dm);
-		if (col != NULL) {
-#if 0
-			if (realcol && dm->drawObject->colType == CD_TEXTURE_MCOL) {
-				col = 0;
-			}
-			else if (mcol && dm->drawObject->colType == CD_MCOL) {
-				col = 0;
-			}
-			
-			if (col != 0)
-#endif
-			GPU_color_setup(dm);
+		if (mcol) {
+			GPU_color_setup(dm, colType);
 		}
 
 		if (!GPU_buffer_legacy(dm)) {
@@ -764,7 +758,7 @@
 					int count = (i - startFace + (draw_option != DM_DRAW_OPTION_SKIP ? 1 : 0)) * 3;
 
 					if (count) {
-						if (col)
+						if (mcol && draw_option != DM_DRAW_OPTION_NO_MCOL)
 							GPU_color_switch(1);
 						else
 							GPU_color_switch(0);
@@ -799,16 +793,21 @@
 	CDDerivedMesh *cddm = (CDDerivedMesh *) dm;
 	MVert *mv = cddm->mvert;
 	MFace *mf = cddm->mface;
-	MCol *mc;
+	MCol *mcol;
 	float *nors = DM_get_tessface_data_layer(dm, CD_NORMAL);
-	int useColors = flag & DM_DRAW_USE_COLORS;
+	int colType, useColors = flag & DM_DRAW_USE_COLORS;
 	int i, orig, *index = DM_get_tessface_data_layer(dm, CD_ORIGINDEX);
 
-	mc = DM_get_tessface_data_layer(dm, CD_ID_MCOL);
-	if (!mc)
-		mc = DM_get_tessface_data_layer(dm, CD_PREVIEW_MCOL);
-	if (!mc)
-		mc = DM_get_tessface_data_layer(dm, CD_MCOL);
+	colType = CD_ID_MCOL;
+	mcol = DM_get_tessface_data_layer(dm, colType);
+	if (!mcol) {
+		colType = CD_PREVIEW_MCOL;
+		mcol = DM_get_tessface_data_layer(dm, colType);
+	}
+	if (!mcol) {
+		colType = CD_MCOL;
+		mcol = DM_get_tessface_data_layer(dm, colType);
+	}
 
 	cdDM_update_normals_from_pbvh(dm);
 
@@ -830,8 +829,8 @@
 			if (draw_option != DM_DRAW_OPTION_SKIP) {
 				unsigned char *cp = NULL;
 
-				if (useColors && mc)
-					cp = (unsigned char *)&mc[i * 4];
+				if (useColors && mcol)
+					cp = (unsigned char *)&mcol[i * 4];
 
 				/* no need to set shading mode to flat because
 				 *  normals are already used to change shading */
@@ -891,8 +890,8 @@
 		int prevstart = 0;
 		GPU_vertex_setup(dm);
 		GPU_normal_setup(dm);
-		if (useColors && mc) {
-			GPU_color_setup(dm);
+		if (useColors && mcol) {
+			GPU_color_setup(dm, colType);
 		}
 		if (!GPU_buffer_legacy(dm)) {
 			int tottri = dm->drawObject->tot_triangle_point / 3;

Modified: trunk/blender/source/blender/gpu/GPU_buffers.h
===================================================================
--- trunk/blender/source/blender/gpu/GPU_buffers.h	2012-10-30 09:29:01 UTC (rev 51758)
+++ trunk/blender/source/blender/gpu/GPU_buffers.h	2012-10-30 11:00:06 UTC (rev 51759)
@@ -123,35 +123,36 @@
 GPUBuffer *GPU_buffer_alloc(int size);
 void GPU_buffer_free(GPUBuffer *buffer);
 
-GPUDrawObject *GPU_drawobject_new(struct DerivedMesh *dm );
-void GPU_drawobject_free(struct DerivedMesh *dm );
+GPUDrawObject *GPU_drawobject_new(struct DerivedMesh *dm);
+void GPU_drawobject_free(struct DerivedMesh *dm);
 
 /* called before drawing */
-void GPU_vertex_setup(struct DerivedMesh *dm );
-void GPU_normal_setup(struct DerivedMesh *dm );
-void GPU_uv_setup(struct DerivedMesh *dm );
-void GPU_color_setup(struct DerivedMesh *dm );
-void GPU_edge_setup(struct DerivedMesh *dm );	/* does not mix with other data */
-void GPU_uvedge_setup(struct DerivedMesh *dm );
-int GPU_attrib_element_size( GPUAttrib data[], int numdata );
-void GPU_interleaved_attrib_setup( GPUBuffer *buffer, GPUAttrib data[], int numdata );
+void GPU_vertex_setup(struct DerivedMesh *dm);
+void GPU_normal_setup(struct DerivedMesh *dm);
+void GPU_uv_setup(struct DerivedMesh *dm);
+/* colType is the cddata MCol type to use! */
+void GPU_color_setup(struct DerivedMesh *dm, int colType);
+void GPU_edge_setup(struct DerivedMesh *dm); /* does not mix with other data */
+void GPU_uvedge_setup(struct DerivedMesh *dm);
+int GPU_attrib_element_size(GPUAttrib data[], int numdata);
+void GPU_interleaved_attrib_setup(GPUBuffer *buffer, GPUAttrib data[], int numdata);
 
 /* can't lock more than one buffer at once */
-void *GPU_buffer_lock( GPUBuffer *buffer );	
-void *GPU_buffer_lock_stream( GPUBuffer *buffer );
-void GPU_buffer_unlock( GPUBuffer *buffer );
+void *GPU_buffer_lock(GPUBuffer *buffer);
+void *GPU_buffer_lock_stream(GPUBuffer *buffer);
+void GPU_buffer_unlock(GPUBuffer *buffer);
 
 /* switch color rendering on=1/off=0 */
-void GPU_color_switch( int mode );
+void GPU_color_switch(int mode);
 
 /* used for drawing edges */
-void GPU_buffer_draw_elements( GPUBuffer *elements, unsigned int mode, int start, int count );
+void GPU_buffer_draw_elements(GPUBuffer *elements, unsigned int mode, int start, int count);
 
 /* called after drawing */
 void GPU_buffer_unbind(void);
 
 /* used to check whether to use the old (without buffers) code */
-int GPU_buffer_legacy(struct DerivedMesh *dm );
+int GPU_buffer_legacy(struct DerivedMesh *dm);
 
 /* Buffers for non-DerivedMesh drawing */
 typedef struct GPU_Buffers GPU_Buffers;

Modified: trunk/blender/source/blender/gpu/intern/gpu_buffers.c
===================================================================
--- trunk/blender/source/blender/gpu/intern/gpu_buffers.c	2012-10-30 09:29:01 UTC (rev 51758)
+++ trunk/blender/source/blender/gpu/intern/gpu_buffers.c	2012-10-30 11:00:06 UTC (rev 51759)
@@ -792,28 +792,6 @@
 	}
 }
 
-/* get the DerivedMesh's MCols; choose (in decreasing order of
- * preference) from CD_ID_MCOL, CD_PREVIEW_MCOL, or CD_MCOL */
-static MCol *gpu_buffer_color_type(DerivedMesh *dm)
-{
-	MCol *c;
-	int type;
-
-	type = CD_ID_MCOL;
-	c = DM_get_tessface_data_layer(dm, type);
-	if (!c) {
-		type = CD_PREVIEW_MCOL;
-		c = DM_get_tessface_data_layer(dm, type);
-		if (!c) {
-			type = CD_MCOL;
-			c = DM_get_tessface_data_layer(dm, type);
-		}
-	}
-
-	dm->drawObject->colType = type;
-	return c;
-}
-
 typedef enum {
 	GPU_BUFFER_VERTEX = 0,
 	GPU_BUFFER_NORMAL,
@@ -896,7 +874,7 @@
 
 	/* special handling for MCol and UV buffers */
 	if (type == GPU_BUFFER_COLOR) {
-		if (!(user_data = gpu_buffer_color_type(dm)))
+		if (!(user_data = DM_get_tessface_data_layer(dm, dm->drawObject->colType)))
 			return NULL;
 	}
 	else if (type == GPU_BUFFER_UV) {
@@ -978,15 +956,27 @@
 	GLStates |= GPU_BUFFER_TEXCOORD_STATE;
 }
 
-void GPU_color_setup(DerivedMesh *dm)
+void GPU_color_setup(DerivedMesh *dm, int colType)
 {
-	/* In paint mode, dm may stay the same during stroke, however we still want to update colors! */
-	if ((dm->dirty & DM_DIRTY_MCOL_UPDATE_DRAW) && dm->drawObject) {
+	if (!dm->drawObject) {
+		/* XXX Not really nice, but we need a valid gpu draw object to set the colType...
+		 *     Else we would have to add a new param to gpu_buffer_setup_common. */
+		dm->drawObject = GPU_drawobject_new(dm);
+		dm->dirty &= ~DM_DIRTY_MCOL_UPDATE_DRAW;
+		dm->drawObject->colType = colType;
+	}
+	/* In paint mode, dm may stay the same during stroke, however we still want to update colors!
+	 * Also check in case we changed color type (i.e. which MCol cdlayer we use). */
+	else if ((dm->dirty & DM_DIRTY_MCOL_UPDATE_DRAW) || (colType != dm->drawObject->colType)) {
 		GPUBuffer **buf = gpu_drawobject_buffer_from_type(dm->drawObject, GPU_BUFFER_COLOR);
+		/* XXX Freeing this buffer is a bit stupid, as geometry has not changed, size should remain the same.
+		 *     Not sure though it would be worth defining a sort of gpu_buffer_update func - nor whether
+		 *     it is even possible ! */
 		GPU_buffer_free(*buf);
 		*buf = NULL;
+		dm->dirty &= ~DM_DIRTY_MCOL_UPDATE_DRAW;
+		dm->drawObject->colType = colType;
 	}
-	dm->dirty &= ~DM_DIRTY_MCOL_UPDATE_DRAW;
 
 	if (!gpu_buffer_setup_common(dm, GPU_BUFFER_COLOR))
 		return;




More information about the Bf-blender-cvs mailing list