[gl2ps] [patch] Stabilize depth sort

David Lonie david.lonie at kitware.com
Mon Jan 18 17:41:23 CET 2016


One last patch :)

While running our tests on some older operating systems there were
inconsistencies when rendering primitives that were at the same depth.
The issue was that GL2PS_SIMPLE_SORT used the unstable qsort method to
rank primitives by depth.

The attached patch stabilizes the qsort call for to ensure consistent
primitive ordering between platforms.

Cheers,
Dave
-------------- next part --------------
Index: gl2ps.c
===================================================================
--- gl2ps.c	(revision 617)
+++ gl2ps.c	(working copy)
@@ -163,6 +163,7 @@
   GLushort pattern;
   char boundary, offset, culled;
   GLint factor;
+  GLint sortid; /* Used to stabilize qsort sorting */
   GLfloat width, ofactor, ounits;
   GL2PSvertex *verts;
   union {
@@ -597,6 +598,15 @@
   qsort(list->array, list->n, list->size, fcmp);
 }
 
+/* Must be a list of GL2PSprimitives. */
+static void gl2psListAssignSortIds(GL2PSlist *list)
+{
+  GLint i;
+  for(i = 0; i < gl2psListNbr(list); i++){
+    (*(GL2PSprimitive**)gl2psListPointer(list, i))->sortid = i;
+  }
+}
+
 static void gl2psListAction(GL2PSlist *list, void (*action)(void *data))
 {
   GLint i;
@@ -1434,7 +1444,8 @@
     return 1;
   }
   else{
-    return 0;
+    /* Ensure that initial ordering is preserved when depths match. */
+    return q->sortid < w->sortid ? -1 : 1;
   }
 }
 
@@ -5657,6 +5668,7 @@
     gl2psListReset(gl2ps->primitives);
     break;
   case GL2PS_SIMPLE_SORT :
+    gl2psListAssignSortIds(gl2ps->primitives);
     gl2psListSort(gl2ps->primitives, gl2psCompareDepth);
     if(gl2ps->options & GL2PS_OCCLUSION_CULL){
       gl2psListActionInverse(gl2ps->primitives, gl2psAddInImageTree);