From 9489fea07b61c83a429b61de887b692dffb6a4f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Wed, 8 May 2019 23:23:31 +0200 Subject: GPU: Refactor some shader for a bit more efficiency Remove matrices multiplication and use more correct codestyle for variables --- .../gpu_shader_instance_edges_variying_color_vert.glsl | 10 +++++----- ...gpu_shader_instance_objectspace_variying_color_vert.glsl | 13 ++++++------- 2 files changed, 11 insertions(+), 12 deletions(-) (limited to 'source/blender/gpu') 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; } -- cgit v1.2.3