egl/sfopenvg/riMath.cpp
branchEGL_MERGE
changeset 88 a5a3a8cb368e
parent 57 2bf8a359aa2f
--- a/egl/sfopenvg/riMath.cpp	Tue Jun 01 15:04:40 2010 +0100
+++ b/egl/sfopenvg/riMath.cpp	Thu Jun 03 17:45:05 2010 +0100
@@ -4,6 +4,7 @@
  * -----------------------------------
  *
  * Copyright (c) 2007 The Khronos Group Inc.
+ * Portions Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and /or associated documentation files
@@ -33,6 +34,20 @@
 #include "riDefs.h"
 #include "riMath.h"
 
+#if 0
+#include <stdio.h>
+
+static void printMatrix(const Matrix3x3& m)
+{
+    // For tracing a bug in matrix inverse in release-builds.
+    for(int i = 0; i < 3; i++)
+    {
+        printf("[%.4f %.4f %.4f]\n", m[i][0], m[i][1], m[i][2]);
+    }
+}
+
+#endif
+
 namespace OpenVGRI
 {
 
@@ -45,6 +60,7 @@
 
 bool Matrix3x3::invert()
 {
+    // \todo Save computation on affine matrices?
 	bool affine = isAffine();
 	RIfloat det00 = matrix[1][1]*matrix[2][2] - matrix[2][1]*matrix[1][2];
 	RIfloat det01 = matrix[2][0]*matrix[1][2] - matrix[1][0]*matrix[2][2];
@@ -55,15 +71,26 @@
 	d = 1.0f / d;
 
 	Matrix3x3 t;
-	t[0][0] = d * det00;
-	t[1][0] = d * det01;
-	t[2][0] = d * det02;
-	t[0][1] = d * (matrix[2][1]*matrix[0][2] - matrix[0][1]*matrix[2][2]);
-	t[1][1] = d * (matrix[0][0]*matrix[2][2] - matrix[2][0]*matrix[0][2]);
-	t[2][1] = d * (matrix[2][0]*matrix[0][1] - matrix[0][0]*matrix[2][1]);
-	t[0][2] = d * (matrix[0][1]*matrix[1][2] - matrix[1][1]*matrix[0][2]);
-	t[1][2] = d * (matrix[1][0]*matrix[0][2] - matrix[0][0]*matrix[1][2]);
-	t[2][2] = d * (matrix[0][0]*matrix[1][1] - matrix[1][0]*matrix[0][1]);
+
+    // \note There is some bug (in GCC?) in accessing matrix elements: If data
+    // is accessed like: t[i][j], then the following will produce incorrect
+    // resulst on optimized builds. If the data is accessed through t.matrix,
+    // then the output is correct. Debug build works correctly, and if print
+    // calls are inserted, the code also works correctly. The context to get
+    // this bug appear are fill paints (linear and radial gradient test
+    // functions).
+
+	t.matrix[0][0] = d * det00;
+	t.matrix[1][0] = d * det01;
+	t.matrix[2][0] = d * det02;
+    //printf("t\n");
+    //printMatrix(t);
+	t.matrix[0][1] = d * (matrix[2][1]*matrix[0][2] - matrix[0][1]*matrix[2][2]);
+	t.matrix[1][1] = d * (matrix[0][0]*matrix[2][2] - matrix[2][0]*matrix[0][2]);
+	t.matrix[2][1] = d * (matrix[2][0]*matrix[0][1] - matrix[0][0]*matrix[2][1]);
+	t.matrix[0][2] = d * (matrix[0][1]*matrix[1][2] - matrix[1][1]*matrix[0][2]);
+	t.matrix[1][2] = d * (matrix[1][0]*matrix[0][2] - matrix[0][0]*matrix[1][2]);
+	t.matrix[2][2] = d * (matrix[0][0]*matrix[1][1] - matrix[1][0]*matrix[0][1]);
 	if(affine)
 		t[2].set(0,0,1);	//affine matrix stays affine
 	*this = t;