From f2d9f3e2fa1610e21e19c7d91e365f3ec78bf5a7 Mon Sep 17 00:00:00 2001
From: Alexander Rose <alex.rose@rcsb.org>
Date: Thu, 16 May 2019 17:48:41 -0700
Subject: [PATCH] avoid function overloading in shaders for comaptibility

---
 src/mol-gl/shader/chunks/common.glsl.ts       | 24 ++++++++++++-------
 .../shader/marching-cubes/isosurface.frag.ts  |  2 +-
 src/mol-gl/shader/mesh.vert.ts                |  2 +-
 src/mol-gl/shader/spheres.vert.ts             |  2 +-
 4 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/src/mol-gl/shader/chunks/common.glsl.ts b/src/mol-gl/shader/chunks/common.glsl.ts
index 4000864de..45c85660c 100644
--- a/src/mol-gl/shader/chunks/common.glsl.ts
+++ b/src/mol-gl/shader/chunks/common.glsl.ts
@@ -36,14 +36,14 @@ float decodeFloatRGB(const in vec3 rgb) {
         return m;
     }
 
-    mat2 transpose(const in mat2 m) {
+    mat2 transpose2(const in mat2 m) {
         return mat2(
             m[0][0], m[1][0],
             m[0][1], m[1][1]
         );
     }
 
-    mat3 transpose(const in mat3 m) {
+    mat3 transpose3(const in mat3 m) {
         return mat3(
             m[0][0], m[1][0], m[2][0],
             m[0][1], m[1][1], m[2][1],
@@ -51,7 +51,7 @@ float decodeFloatRGB(const in vec3 rgb) {
         );
     }
 
-    mat4 transpose(const in mat4 m) {
+    mat4 transpose4(const in mat4 m) {
         return mat4(
             m[0][0], m[1][0], m[2][0], m[3][0],
             m[0][1], m[1][1], m[2][1], m[3][1],
@@ -60,18 +60,18 @@ float decodeFloatRGB(const in vec3 rgb) {
         );
     }
 
+    // inverse
+
     float inverse(const in float m) {
         return 1.0 / m;
     }
 
-    // inverse
-
-    mat2 inverse(const in mat2 m) {
+    mat2 inverse2(const in mat2 m) {
         return mat2(m[1][1],-m[0][1],
                 -m[1][0], m[0][0]) / (m[0][0]*m[1][1] - m[0][1]*m[1][0]);
     }
 
-    mat3 inverse(const in mat3 m) {
+    mat3 inverse3(const in mat3 m) {
         float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];
         float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];
         float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];
@@ -87,7 +87,7 @@ float decodeFloatRGB(const in vec3 rgb) {
                     b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;
     }
 
-    mat4 inverse(const in mat4 m) {
+    mat4 inverse4(const in mat4 m) {
         float
             a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3],
             a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3],
@@ -127,5 +127,13 @@ float decodeFloatRGB(const in vec3 rgb) {
             a31 * b01 - a30 * b03 - a32 * b00,
             a20 * b03 - a21 * b01 + a22 * b00) / det;
     }
+#else
+    #define transpose2(m) transpose(m)
+    #define transpose3(m) transpose(m)
+    #define transpose4(m) transpose(m)
+
+    #define inverse2(m) inverse(m)
+    #define inverse3(m) inverse(m)
+    #define inverse4(m) inverse(m)
 #endif
 `
\ No newline at end of file
diff --git a/src/mol-gl/shader/marching-cubes/isosurface.frag.ts b/src/mol-gl/shader/marching-cubes/isosurface.frag.ts
index 5e49f2962..d8cb57a87 100644
--- a/src/mol-gl/shader/marching-cubes/isosurface.frag.ts
+++ b/src/mol-gl/shader/marching-cubes/isosurface.frag.ts
@@ -204,7 +204,7 @@ void main(void) {
         n0.z + t * (n0.z - n1.z)
     );
 
-    mat3 normalMatrix = transpose(inverse(mat3(uGridTransform)));
+    mat3 normalMatrix = transpose3(inverse3(mat3(uGridTransform)));
     gl_FragData[1].xyz = normalMatrix * gl_FragData[1].xyz;
 }
 `
\ No newline at end of file
diff --git a/src/mol-gl/shader/mesh.vert.ts b/src/mol-gl/shader/mesh.vert.ts
index 7918bf506..4601cbd13 100644
--- a/src/mol-gl/shader/mesh.vert.ts
+++ b/src/mol-gl/shader/mesh.vert.ts
@@ -44,7 +44,7 @@ void main(){
         #else
             vec3 normal = aNormal;
         #endif
-        mat3 normalMatrix = transpose(inverse(mat3(modelView)));
+        mat3 normalMatrix = transpose3(inverse3(mat3(modelView)));
         vec3 transformedNormal = normalize(normalMatrix * normalize(normal));
         #if defined(dFlipSided) && !defined(dDoubleSided) // TODO checking dDoubleSided should not be required, ASR
             transformedNormal = -transformedNormal;
diff --git a/src/mol-gl/shader/spheres.vert.ts b/src/mol-gl/shader/spheres.vert.ts
index 86c1afea0..fabd271ad 100644
--- a/src/mol-gl/shader/spheres.vert.ts
+++ b/src/mol-gl/shader/spheres.vert.ts
@@ -52,7 +52,7 @@ void quadraticProjection(const in float radius, const in vec3 position){
         position.x, position.y, position.z, 1.0
     );
 
-    mat4 R = transpose(uProjection * uModelView * aTransform * T);
+    mat4 R = transpose4(uProjection * uModelView * aTransform * T);
     float A = dot(R[3], D * R[3]);
     float B = -2.0 * dot(R[0], D * R[3]);
     float C = dot(R[0], D * R[0]);
-- 
GitLab