[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [42311] trunk/blender/source: Slight refactor of VBO code to deal with multiple textures.

Sergey Sharybin sergey.vfx at gmail.com
Thu Dec 1 13:12:50 CET 2011


Revision: 42311
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=42311
Author:   nazgul
Date:     2011-12-01 12:12:39 +0000 (Thu, 01 Dec 2011)
Log Message:
-----------
Slight refactor of VBO code to deal with multiple textures.

Added compareDrawSettings callback to driver mesh's callbacks which are
drawing textured faces (mapped and not mapped). This new callback checks
if two faces are drawing with the same settings (testures, shading etc)
and if they not, flush of faces happens into ogl using glDrawArrays and
next face would be drawn with it's own settings.

Currently implemented compareDrawSettings is used to resolve issue from
bug report only, probably there are extra places where this callback is
needed, but haven't seen configuration where current logic will fail,
so it should be ok.

Also reordered arguments passing to drawMappedFaces DM's callbacks,
so now all drawing callback are accepting list of callbacks and then
userData, instead of using mixed order of callbacks and userData which
was a bit confusing to work with.

This commit fixes:
- #26410: VBO & multitexture doesnt work
- #29464: VBO enabled causes UV coruption

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_DerivedMesh.h
    trunk/blender/source/blender/blenkernel/intern/DerivedMesh.c
    trunk/blender/source/blender/blenkernel/intern/cdderivedmesh.c
    trunk/blender/source/blender/blenkernel/intern/subsurf_ccg.c
    trunk/blender/source/blender/editors/space_view3d/drawmesh.c
    trunk/blender/source/blender/editors/space_view3d/drawobject.c
    trunk/blender/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp

Modified: trunk/blender/source/blender/blenkernel/BKE_DerivedMesh.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_DerivedMesh.h	2011-12-01 10:27:35 UTC (rev 42310)
+++ trunk/blender/source/blender/blenkernel/BKE_DerivedMesh.h	2011-12-01 12:12:39 UTC (rev 42311)
@@ -255,7 +255,11 @@
 	 */
 	void (*drawFacesTex)(DerivedMesh *dm,
 						 int (*setDrawOptions)(struct MTFace *tface,
-						 int has_mcol, int matnr));
+							 int has_mcol, int matnr),
+						int (*compareDrawOptions)(void *userData,
+							 int cur_index,
+							 int next_index),
+						void *userData);
 
 	/* Draw all faces with GLSL materials
 	 *  o setMaterial is called for every different material nr
@@ -280,9 +284,11 @@
 	void (*drawMappedFaces)(DerivedMesh *dm,
 							int (*setDrawOptions)(void *userData, int index,
 												  int *drawSmooth_r),
-							void *userData, int useColors,
 							int (*setMaterial)(int, void *attribs),
-							int (*compareDrawOptions)(void *userData, int cur_index, int next_index));
+							int (*compareDrawOptions)(void *userData,
+							                          int cur_index,
+							                          int next_index),
+							void *userData, int useColors);
 
 	/* Draw mapped faces using MTFace 
 	 *  o Drawing options too complicated to enumerate, look at code.
@@ -290,6 +296,9 @@
 	void (*drawMappedFacesTex)(DerivedMesh *dm,
 							   int (*setDrawOptions)(void *userData,
 													 int index),
+							   int (*compareDrawOptions)(void *userData,
+							                             int cur_index,
+							                             int next_index),
 							   void *userData);
 
 	/* Draw mapped faces with GLSL materials
@@ -299,7 +308,8 @@
 	 */
 	void (*drawMappedFacesGLSL)(DerivedMesh *dm,
 		int (*setMaterial)(int, void *attribs),
-		int (*setDrawOptions)(void *userData, int index), void *userData);
+		int (*setDrawOptions)(void *userData, int index),
+		void *userData);
 
 	/* Draw mapped edges as lines
 	 *  o Only if !setDrawOptions or setDrawOptions(userData, mapped-edge)

Modified: trunk/blender/source/blender/blenkernel/intern/DerivedMesh.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/DerivedMesh.c	2011-12-01 10:27:35 UTC (rev 42310)
+++ trunk/blender/source/blender/blenkernel/intern/DerivedMesh.c	2011-12-01 12:12:39 UTC (rev 42311)
@@ -641,8 +641,11 @@
 }
 
 /* note, material function is ignored for now. */
-static void emDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int UNUSED(useColors), int (*setMaterial)(int, void *attribs),
-			int (*compareDrawOptions)(void *userData, int cur_index, int next_index))
+static void emDM_drawMappedFaces(DerivedMesh *dm,
+			int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r),
+			int (*setMaterial)(int, void *attribs),
+			int (*compareDrawOptions)(void *userData, int cur_index, int next_index),
+			void *userData, int UNUSED(useColors))
 {
 	EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
 	EditFace *efa;
@@ -809,7 +812,8 @@
 static void emDM_drawFacesTex_common(DerivedMesh *dm,
 			   int (*drawParams)(MTFace *tface, int has_mcol, int matnr),
 			   int (*drawParamsMapped)(void *userData, int index),
-			   void *userData) 
+			   int (*compareDrawOptions)(void *userData, int cur_index, int next_index),
+			   void *userData)
 {
 	EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
 	EditMesh *em= emdm->em;
@@ -818,6 +822,8 @@
 	EditFace *efa;
 	int i;
 
+	(void) compareDrawOptions;
+
 	/* always use smooth shading even for flat faces, else vertex colors wont interpolate */
 	glShadeModel(GL_SMOOTH);
 	
@@ -974,19 +980,26 @@
 	}
 }
 
-static void emDM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, int has_mcol, int matnr))
+static void emDM_drawFacesTex(DerivedMesh *dm,
+			   int (*setDrawOptions)(MTFace *tface, int has_mcol, int matnr),
+			   int (*compareDrawOptions)(void *userData, int cur_index, int next_index),
+			   void *userData)
 {
-	emDM_drawFacesTex_common(dm, setDrawOptions, NULL, NULL);
+	emDM_drawFacesTex_common(dm, setDrawOptions, NULL, compareDrawOptions, userData);
 }
 
-static void emDM_drawMappedFacesTex(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData)
+static void emDM_drawMappedFacesTex(DerivedMesh *dm,
+			   int (*setDrawOptions)(void *userData, int index),
+			   int (*compareDrawOptions)(void *userData, int cur_index, int next_index),
+			   void *userData)
 {
-	emDM_drawFacesTex_common(dm, NULL, setDrawOptions, userData);
+	emDM_drawFacesTex_common(dm, NULL, setDrawOptions, compareDrawOptions, userData);
 }
 
 static void emDM_drawMappedFacesGLSL(DerivedMesh *dm,
 			   int (*setMaterial)(int, void *attribs),
-			   int (*setDrawOptions)(void *userData, int index), void *userData) 
+			   int (*setDrawOptions)(void *userData, int index),
+			   void *userData)
 {
 	EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
 	EditMesh *em= emdm->em;
@@ -3168,9 +3181,14 @@
 	glEnable(GL_LIGHTING);
 }
 
-static void navmesh_DM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, int has_mcol, int matnr))
+static void navmesh_DM_drawFacesTex(DerivedMesh *dm,
+			int (*setDrawOptions)(MTFace *tface, int has_mcol, int matnr),
+			int (*compareDrawOptions)(void *userData, int cur_index, int next_index),
+			void *userData)
 {
 	(void) setDrawOptions;
+	(void) compareDrawOptions;
+	(void) userData;
 
 	navmesh_drawColored(dm);
 }

Modified: trunk/blender/source/blender/blenkernel/intern/cdderivedmesh.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/cdderivedmesh.c	2011-12-01 10:27:35 UTC (rev 42310)
+++ trunk/blender/source/blender/blenkernel/intern/cdderivedmesh.c	2011-12-01 12:12:39 UTC (rev 42311)
@@ -647,6 +647,7 @@
 static void cdDM_drawFacesTex_common(DerivedMesh *dm,
 			   int (*drawParams)(MTFace *tface, int has_mcol, int matnr),
 			   int (*drawParamsMapped)(void *userData, int index),
+			   int (*compareDrawOptions)(void *userData, int cur_index, int next_index),
 			   void *userData) 
 {
 	CDDerivedMesh *cddm = (CDDerivedMesh*) dm;
@@ -771,25 +772,19 @@
 		}
 
 		if( !GPU_buffer_legacy(dm) ) {
-			/* warning!, this logic is incorrect, see bug [#27175]
-			 * firstly, there are no checks for changes in context, such as texface image.
-			 * secondly, drawParams() sets the GL context, so checking if there is a change
-			 * from lastFlag is too late once glDrawArrays() runs, since drawing the arrays
-			 * will use the modified, OpenGL settings.
-			 * 
-			 * However its tricky to fix this without duplicating the internal logic
-			 * of drawParams(), perhaps we need an argument like...
-			 * drawParams(..., keep_gl_state_but_return_when_changed) ?.
-			 *
-			 * We could also just disable VBO's here, since texface may be deprecated - campbell.
-			 */
-			
+			int tottri = dm->drawObject->tot_triangle_point/3;
+			int next_actualFace= dm->drawObject->triangle_to_mface[0];
+
 			glShadeModel( GL_SMOOTH );
 			lastFlag = 0;
-			for(i = 0; i < dm->drawObject->tot_triangle_point/3; i++) {
-				int actualFace = dm->drawObject->triangle_to_mface[i];
+			for(i = 0; i < tottri; i++) {
+				int actualFace = next_actualFace;
 				int flag = 1;
+				int flush = 0;
 
+				if(i != tottri-1)
+					next_actualFace= dm->drawObject->triangle_to_mface[i+1];
+
 				if(drawParams) {
 					flag = drawParams(tf? &tf[actualFace]: NULL, (mcol != NULL), mf[actualFace].mat_nr);
 				}
@@ -804,27 +799,36 @@
 						if(drawParamsMapped)
 							flag = drawParamsMapped(userData, actualFace);
 				}
-				if( flag != lastFlag ) {
-					if( startFace < i ) {
-						if( lastFlag != 0 ) { /* if the flag is 0 it means the face is hidden or invisible */
-							if (lastFlag==1 && col)
-								GPU_color_switch(1);
-							else
-								GPU_color_switch(0);
-							glDrawArrays(GL_TRIANGLES,startFace*3,(i-startFace)*3);
-						}
+
+				/* flush buffer if current triangle isn't drawable or it's last triangle */
+				flush= !flag || i == tottri - 1;
+
+				if(!flush && compareDrawOptions) {
+					int next_orig= (index==NULL) ? next_actualFace : index[next_actualFace];
+
+					if(orig==ORIGINDEX_NONE || next_orig==ORIGINDEX_NONE) {
+						flush= 1;
+					} else {
+						/* also compare draw options and flush buffer if they're different
+						   need for face selection highlight in edit mode */
+						flush|= compareDrawOptions(userData, orig, next_orig) == 0;
 					}
-					lastFlag = flag;
-					startFace = i;
 				}
-			}
-			if( startFace < dm->drawObject->tot_triangle_point/3 ) {
-				if( lastFlag != 0 ) { /* if the flag is 0 it means the face is hidden or invisible */
-					if (lastFlag==1 && col)
-						GPU_color_switch(1);
-					else
-						GPU_color_switch(0);
-					glDrawArrays(GL_TRIANGLES, startFace*3, dm->drawObject->tot_triangle_point - startFace*3);
+
+				if(flush) {
+					int first= startFace*3;
+					int count= (i-startFace+(flag ? 1 : 0))*3; /* Add one to the length if we're drawing at the end of the array */
+
+					if(count) {
+						if (col)
+							GPU_color_switch(1);
+						else
+							GPU_color_switch(0);
+
+						glDrawArrays(GL_TRIANGLES, first, count);
+					}
+
+					startFace = i + 1;
 				}
 			}
 		}
@@ -834,13 +838,19 @@
 	}
 }
 
-static void cdDM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, int has_mcol, int matnr))
+static void cdDM_drawFacesTex(DerivedMesh *dm,
+			   int (*setDrawOptions)(MTFace *tface, int has_mcol, int matnr),
+			   int (*compareDrawOptions)(void *userData, int cur_index, int next_index),
+			   void *userData)
 {
-	cdDM_drawFacesTex_common(dm, setDrawOptions, NULL, NULL);
+	cdDM_drawFacesTex_common(dm, setDrawOptions, NULL, compareDrawOptions, userData);
 }
 
-static void cdDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int useColors, int (*setMaterial)(int, void *attribs),
-			int (*compareDrawOptions)(void *userData, int cur_index, int next_index))
+static void cdDM_drawMappedFaces(DerivedMesh *dm,
+			int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r),
+			int (*setMaterial)(int, void *attribs),
+			int (*compareDrawOptions)(void *userData, int cur_index, int next_index),

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list