[Bf-blender-cvs] [9489fea07b6] master: GPU: Refactor some shader for a bit more efficiency

Clément Foucault noreply at git.blender.org
Wed May 8 23:31:05 CEST 2019


Commit: 9489fea07b61c83a429b61de887b692dffb6a4f4
Author: Clément Foucault
Date:   Wed May 8 23:23:31 2019 +0200
Branches: master
https://developer.blender.org/rB9489fea07b61c83a429b61de887b692dffb6a4f4

GPU: Refactor some shader for a bit more efficiency

Remove matrices multiplication and use more correct codestyle for variables

===================================================================

M	source/blender/gpu/shaders/gpu_shader_instance_edges_variying_color_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_instance_objectspace_variying_color_vert.glsl

===================================================================

diff --git a/source/blender/gpu/shaders/gpu_shader_instance_edges_variying_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_instance_edges_variying_color_vert.glsl
index d9e73f81c45..b4a6b0de33f 100644
--- a/source/blender/gpu/shaders/gpu_shader_instance_edges_variying_color_vert.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_instance_edges_variying_color_vert.glsl
@@ -26,9 +26,9 @@ out vec3 fCol;
 
 // TODO: in float angle; // [-pi .. +pi], + peak, 0 flat, - valley
 
-bool front(mat3 NormalMatrix, vec3 N, vec3 eye)
+bool front(mat3 normal_matrix, vec3 N, vec3 eye)
 {
-  return dot(NormalMatrix * N, eye) > 0.0;
+  return dot(normal_matrix * N, eye) > 0.0;
 }
 
 void main()
@@ -40,7 +40,7 @@ void main()
   vec4 pos_4d = vec4(pos, 1.0);
   MV_pos = ModelViewMatrix * pos_4d;
 
-  mat3 NormalMatrix = transpose(inverse(mat3(ModelViewMatrix)));
+  mat3 normal_matrix = transpose(inverse(mat3(ModelViewMatrix)));
 
   /* if persp */
   if (ProjectionMatrix[3][3] == 0.0) {
@@ -50,8 +50,8 @@ void main()
     eye = vec3(0.0, 0.0, 1.0);
   }
 
-  bool face_1_front = front(NormalMatrix, N1, eye);
-  bool face_2_front = front(NormalMatrix, N2, eye);
+  bool face_1_front = front(normal_matrix, N1, eye);
+  bool face_2_front = front(normal_matrix, N2, eye);
 
   if (face_1_front && face_2_front)
     edgeClass = 1.0;  // front-facing edge
diff --git a/source/blender/gpu/shaders/gpu_shader_instance_objectspace_variying_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_instance_objectspace_variying_color_vert.glsl
index 9b1a08d8d86..5b3922d7a72 100644
--- a/source/blender/gpu/shaders/gpu_shader_instance_objectspace_variying_color_vert.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_instance_objectspace_variying_color_vert.glsl
@@ -1,5 +1,5 @@
 
-uniform mat4 ViewMatrix;
+uniform mat4 ViewMatrixInverse;
 uniform mat4 ViewProjectionMatrix;
 
 /* ---- Instantiated Attrs ---- */
@@ -15,13 +15,12 @@ flat out vec4 finalColor;
 
 void main()
 {
-  mat4 ModelViewProjectionMatrix = ViewProjectionMatrix * InstanceModelMatrix;
-  /* This is slow and run per vertex, but it's still faster than
-   * doing it per instance on CPU and sending it on via instance attr. */
-  mat3 NormalMatrix = transpose(inverse(mat3(ViewMatrix * InstanceModelMatrix)));
+  gl_Position = ViewProjectionMatrix * (InstanceModelMatrix * vec4(pos, 1.0));
 
-  gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
-  normal = NormalMatrix * nor;
+  /* This is slow and run per vertex, but it's still faster than
+   * doing it per instance on CPU and sending it on via instance attribute. */
+  mat3 normal_mat = transpose(inverse(mat3(InstanceModelMatrix)));
+  normal = normalize((transpose(mat3(ViewMatrixInverse)) * (normal_mat * nor)));
 
   finalColor = color;
 }



More information about the Bf-blender-cvs mailing list