DRW: Remove ModelViewMatrix Usage
authorClément Foucault <foucault.clem@gmail.com>
Thu, 9 May 2019 20:10:38 +0000 (22:10 +0200)
committerClément Foucault <foucault.clem@gmail.com>
Fri, 10 May 2019 10:14:41 +0000 (12:14 +0200)
12 files changed:
source/blender/draw/engines/eevee/shaders/prepass_vert.glsl
source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl
source/blender/draw/engines/workbench/shaders/workbench_volume_vert.glsl
source/blender/draw/engines/workbench/workbench_volume.c
source/blender/draw/intern/draw_manager_data.c
source/blender/draw/modes/object_mode.c
source/blender/draw/modes/shaders/common_view_lib.glsl
source/blender/draw/modes/shaders/edit_mesh_overlay_vert.glsl
source/blender/draw/modes/shaders/object_outline_prepass_geom.glsl
source/blender/draw/modes/shaders/object_outline_prepass_vert.glsl
source/blender/draw/modes/shaders/object_particle_dot_vert.glsl
source/blender/draw/modes/shaders/object_particle_prim_vert.glsl

index 883862855f39a39857680a2f27ae43749648985b..7f154c9130abb7c97493f6ffd3a2cce5b054108c 100644 (file)
@@ -1,5 +1,4 @@
 
-uniform mat4 ModelViewProjectionMatrix;
 uniform mat4 ModelMatrix;
 uniform mat4 ModelMatrixInverse;
 
index a1c269d5a6535bf9b653e7dc3e4b09aa8ead2118..cef405d12d3f5088206f1d6d9f514885d1339afb 100644 (file)
@@ -1,8 +1,7 @@
 
-uniform mat4 ProjectionMatrix;
-uniform mat4 ModelMatrixInverse;
-uniform mat4 ModelViewMatrixInverse;
 uniform mat4 ModelMatrix;
+uniform mat4 ModelMatrixInverse;
+
 uniform vec3 OrcoTexCoFactors[2];
 
 uniform sampler2D depthBuffer;
@@ -219,9 +218,11 @@ void main()
   vec3 vs_ray_dir = (is_persp) ? (vs_ray_end - vs_ray_ori) : vec3(0.0, 0.0, -1.0);
   vs_ray_dir /= abs(vs_ray_dir.z);
 
-  vec3 ls_ray_dir = mat3(ModelViewMatrixInverse) * vs_ray_dir * OrcoTexCoFactors[1] * 2.0;
-  vec3 ls_ray_ori = (ModelViewMatrixInverse * vec4(vs_ray_ori, 1.0)).xyz;
-  vec3 ls_ray_end = (ModelViewMatrixInverse * vec4(vs_ray_end, 1.0)).xyz;
+  /* TODO(fclem) Precompute the matrix/ */
+  vec3 ls_ray_dir = vs_ray_dir * OrcoTexCoFactors[1] * 2.0;
+  ls_ray_dir = mat3(ModelMatrixInverse) * (mat3(ViewMatrixInverse) * ls_ray_dir);
+  vec3 ls_ray_ori = point_view_to_object(vs_ray_ori);
+  vec3 ls_ray_end = point_view_to_object(vs_ray_end);
 
   ls_ray_ori = (OrcoTexCoFactors[0] + ls_ray_ori * OrcoTexCoFactors[1]) * 2.0 - 1.0;
   ls_ray_end = (OrcoTexCoFactors[0] + ls_ray_end * OrcoTexCoFactors[1]) * 2.0 - 1.0;
index 7a418243fd37981a566ccc4d1455cebcca7a91f1..1834173fee9a764294cf823f44cc0ccfe4c0db90 100644 (file)
@@ -1,5 +1,6 @@
 
-uniform mat4 ModelViewProjectionMatrix;
+uniform mat4 ModelMatrix;
+
 uniform vec3 OrcoTexCoFactors[2];
 uniform float slicePosition;
 uniform int sliceAxis; /* -1 is no slice, 0 is X, 1 is Y, 2 is Z. */
@@ -29,5 +30,5 @@ void main()
   vec3 final_pos = pos;
 #endif
   final_pos = ((final_pos * 0.5 + 0.5) - OrcoTexCoFactors[0]) / OrcoTexCoFactors[1];
-  gl_Position = ModelViewProjectionMatrix * vec4(final_pos, 1.0);
+  gl_Position = point_object_to_ndc(final_pos);
 }
index accc7e91576d2928b58ee33b3c96b66b62306b37..d2f19f57dcfa2d69f1647d620e8f9208556e4a27 100644 (file)
@@ -53,6 +53,7 @@ static struct {
 
 extern char datatoc_workbench_volume_vert_glsl[];
 extern char datatoc_workbench_volume_frag_glsl[];
+extern char datatoc_common_view_lib_glsl[];
 
 static GPUShader *volume_shader_get(bool slice, bool coba, bool cubic)
 {
@@ -77,8 +78,11 @@ static GPUShader *volume_shader_get(bool slice, bool coba, bool cubic)
     char *defines = BLI_dynstr_get_cstring(ds);
     BLI_dynstr_free(ds);
 
-    e_data.volume_sh[id] = DRW_shader_create(
-        datatoc_workbench_volume_vert_glsl, NULL, datatoc_workbench_volume_frag_glsl, defines);
+    e_data.volume_sh[id] = DRW_shader_create_with_lib(datatoc_workbench_volume_vert_glsl,
+                                                      NULL,
+                                                      datatoc_workbench_volume_frag_glsl,
+                                                      datatoc_common_view_lib_glsl,
+                                                      defines);
 
     MEM_freeN(defines);
   }
index e1f9f826ca5c9b5e56765ac8a367ea28866095df..1f48129116d43cf3e9e1da6196c67f0fa3d9457b 100644 (file)
@@ -1205,6 +1205,7 @@ void DRW_shgroup_instance_batch(DRWShadingGroup *shgroup, struct GPUBatch *batch
   /* Note: This WILL break if batch->verts[0] is destroyed and reallocated
    * at the same address. Bindings/VAOs would remain obsolete. */
   // if (shgroup->instancing_geom->inst != batch->verts[0])
+  /* XXX FIXME: THIS IS BROKEN BECAUSE OVEWRITTEN BY DRW_instance_buffer_finish(). */
   GPU_batch_instbuf_set(shgroup->instance_geom, batch->verts[0], false);
 
 #ifdef USE_GPU_SELECT
index c494e222b3d55a2b8c748228bdd938d66efff5a4..7e5586f1b10c7e224fa4a95e49e4d5d364031eff 100644 (file)
@@ -419,8 +419,14 @@ static void OBJECT_engine_init(void *vedata)
         .defs = (const char *[]){sh_cfg_data->def, NULL},
     });
     sh_data->outline_prepass_wire = GPU_shader_create_from_arrays({
-        .vert = (const char *[]){sh_cfg_data->lib, datatoc_object_outline_prepass_vert_glsl, NULL},
-        .geom = (const char *[]){sh_cfg_data->lib, datatoc_object_outline_prepass_geom_glsl, NULL},
+        .vert = (const char *[]){sh_cfg_data->lib,
+                                 datatoc_common_view_lib_glsl,
+                                 datatoc_object_outline_prepass_vert_glsl,
+                                 NULL},
+        .geom = (const char *[]){sh_cfg_data->lib,
+                                 datatoc_common_view_lib_glsl,
+                                 datatoc_object_outline_prepass_geom_glsl,
+                                 NULL},
         .frag = (const char *[]){datatoc_object_outline_prepass_frag_glsl, NULL},
         .defs = (const char *[]){sh_cfg_data->def, NULL},
     });
@@ -486,18 +492,23 @@ static void OBJECT_engine_init(void *vedata)
     });
 
     /* Particles */
-    sh_data->part_prim = DRW_shader_create(datatoc_object_particle_prim_vert_glsl,
-                                           NULL,
-                                           datatoc_gpu_shader_flat_color_frag_glsl,
-                                           NULL);
-
-    sh_data->part_axis = DRW_shader_create(datatoc_object_particle_prim_vert_glsl,
-                                           NULL,
-                                           datatoc_gpu_shader_flat_color_frag_glsl,
-                                           "#define USE_AXIS\n");
-
-    sh_data->part_dot = DRW_shader_create(
-        datatoc_object_particle_dot_vert_glsl, NULL, datatoc_object_particle_dot_frag_glsl, NULL);
+    sh_data->part_prim = DRW_shader_create_with_lib(datatoc_object_particle_prim_vert_glsl,
+                                                    NULL,
+                                                    datatoc_gpu_shader_flat_color_frag_glsl,
+                                                    datatoc_common_view_lib_glsl,
+                                                    NULL);
+
+    sh_data->part_axis = DRW_shader_create_with_lib(datatoc_object_particle_prim_vert_glsl,
+                                                    NULL,
+                                                    datatoc_gpu_shader_flat_color_frag_glsl,
+                                                    datatoc_common_view_lib_glsl,
+                                                    "#define USE_AXIS\n");
+
+    sh_data->part_dot = DRW_shader_create_with_lib(datatoc_object_particle_dot_vert_glsl,
+                                                   NULL,
+                                                   datatoc_object_particle_dot_frag_glsl,
+                                                   datatoc_common_view_lib_glsl,
+                                                   NULL);
 
     /* Lightprobes */
     sh_data->lightprobe_grid = DRW_shader_create(datatoc_object_lightprobe_grid_vert_glsl,
@@ -2990,7 +3001,6 @@ static void OBJECT_cache_populate_particles(OBJECT_Shaders *sh_data,
     if (draw_as != PART_DRAW_PATH) {
       struct GPUBatch *geom = DRW_cache_particles_get_dots(ob, psys);
       DRWShadingGroup *shgrp = NULL;
-      static int screen_space[2] = {0, 1};
       static float def_prim_col[3] = {0.5f, 0.5f, 0.5f};
       static float def_sec_col[3] = {1.0f, 1.0f, 1.0f};
 
@@ -3016,7 +3026,9 @@ static void OBJECT_cache_populate_particles(OBJECT_Shaders *sh_data,
                                               e_data.particle_format);
           DRW_shgroup_uniform_texture(shgrp, "ramp", G_draw.ramp);
           DRW_shgroup_uniform_vec3(shgrp, "color", ma ? &ma->r : def_prim_col, 1);
-          DRW_shgroup_uniform_int(shgrp, "screen_space", &screen_space[0], 1);
+          DRW_shgroup_uniform_float(shgrp, "draw_size", &part->draw_size, 1);
+          DRW_shgroup_uniform_bool_copy(shgrp, "screen_space", false);
+          DRW_shgroup_instance_batch(shgrp, geom);
           break;
         case PART_DRAW_CIRC:
           shgrp = DRW_shgroup_instance_create(sh_data->part_prim,
@@ -3025,25 +3037,22 @@ static void OBJECT_cache_populate_particles(OBJECT_Shaders *sh_data,
                                               e_data.particle_format);
           DRW_shgroup_uniform_texture(shgrp, "ramp", G_draw.ramp);
           DRW_shgroup_uniform_vec3(shgrp, "color", ma ? &ma->r : def_prim_col, 1);
-          DRW_shgroup_uniform_int(shgrp, "screen_space", &screen_space[1], 1);
+          DRW_shgroup_uniform_float(shgrp, "draw_size", &part->draw_size, 1);
+          DRW_shgroup_uniform_bool_copy(shgrp, "screen_space", true);
+          DRW_shgroup_instance_batch(shgrp, geom);
           break;
         case PART_DRAW_AXIS:
           shgrp = DRW_shgroup_instance_create(sh_data->part_axis,
                                               psl->particle,
                                               DRW_cache_particles_get_prim(PART_DRAW_AXIS),
                                               e_data.particle_format);
-          DRW_shgroup_uniform_int(shgrp, "screen_space", &screen_space[0], 1);
+          DRW_shgroup_uniform_float(shgrp, "draw_size", &part->draw_size, 1);
+          DRW_shgroup_uniform_bool_copy(shgrp, "screen_space", false);
+          DRW_shgroup_instance_batch(shgrp, geom);
           break;
         default:
           break;
       }
-
-      if (shgrp) {
-        if (draw_as != PART_DRAW_DOT) {
-          DRW_shgroup_uniform_float(shgrp, "draw_size", &part->draw_size, 1);
-          DRW_shgroup_instance_batch(shgrp, geom);
-        }
-      }
     }
   }
 }
index 40c43856b43069c304e33178b4562b15bb9c60ec..bdf184cb31e39e98deab2ff6338d77ba8f8cd805 100644 (file)
@@ -31,17 +31,17 @@ layout(std140) uniform viewBlock
    * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse.
    * ViewMatrix * transpose(ModelMatrixInverse)
    **/
-#define normal_object_to_view(nor) (mat3(ViewMatrix) * normal_object_to_world(nor))
-#define normal_object_to_world(nor) (transpose(mat3(ModelMatrixInverse)) * nor)
-#define normal_world_to_object(nor) (transpose(mat3(ModelMatrix)) * nor)
-#define normal_world_to_view(nor) (mat3(ViewMatrix) * nor)
+#define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n))
+#define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n)
+#define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n)
+#define normal_world_to_view(n) (mat3(ViewMatrix) * n)
 
-#define point_object_to_ndc(point) (ViewProjectionMatrix * (ModelMatrix * vec4(pt, 1.0)))
-#define point_object_to_view(point) ((ViewMatrix * (ModelMatrix * vec4(pt, 1.0))).xyz)
-#define point_object_to_world(point) ((ModelMatrix * vec4(point, 1.0)).xyz)
-#define point_view_to_ndc(point) (ProjectionMatrix * vec4(point, 1.0))
-#define point_view_to_object(point) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(pt, 1.))).xyz)
-#define point_view_to_world(point) ((ViewMatrixInverse * vec4(point, 1.0)).xyz)
-#define point_world_to_ndc(point) (ViewProjectionMatrix * vec4(point, 1.0))
-#define point_world_to_object(point) ((ModelMatrixInverse * vec4(point, 1.0)).xyz)
-#define point_world_to_view(point) ((ViewMatrix * vec4(point, 1.0)).xyz)
+#define point_object_to_ndc(p) (ViewProjectionMatrix * (ModelMatrix * vec4(p, 1.0)))
+#define point_object_to_view(p) ((ViewMatrix * (ModelMatrix * vec4(p, 1.0))).xyz)
+#define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz)
+#define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0))
+#define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz)
+#define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz)
+#define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0))
+#define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz)
+#define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz)
index d5befc027649e2d9e267bcf9da6a4b07affb4d36..b4476c98382870d4badf6c6cf72cc5c3854f60af 100644 (file)
@@ -1,6 +1,4 @@
 
-uniform mat4 ModelViewMatrix;
-uniform mat4 ModelViewProjectionMatrix;
 uniform mat4 ModelMatrix;
 uniform mat4 ModelMatrixInverse;
 
@@ -25,14 +23,17 @@ out int selectOveride;
 
 void main()
 {
+  vec4 world_pos = ModelMatrix * vec4(pos, 1.0);
+
 #if !defined(FACE)
+  /* TODO override the ViewProjection Matrix for this case. */
   mat4 projmat = ProjectionMatrix;
   projmat[3][2] -= ofs;
 
-  gl_Position = projmat * (ModelViewMatrix * vec4(pos, 1.0));
+  gl_Position = projmat * (ViewMatrix * world_pos);
 #else
 
-  gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
+  gl_Position = ViewProjectionMatrix * world_pos;
 #endif
 
   ivec4 m_data = data & dataMask;
@@ -76,17 +77,16 @@ void main()
 
 #if !defined(FACE)
   /* Facing based color blend */
-  vec4 vpos = ModelViewMatrix * vec4(pos, 1.0);
+  vec3 vpos = point_world_to_view(world_pos.xyz);
   vec3 view_normal = normalize(normal_object_to_view(vnor) + 1e-4);
-  vec3 view_vec = (ProjectionMatrix[3][3] == 0.0) ? normalize(vpos.xyz) : vec3(0.0, 0.0, 1.0);
+  vec3 view_vec = (ProjectionMatrix[3][3] == 0.0) ? normalize(vpos) : vec3(0.0, 0.0, 1.0);
   float facing = dot(view_vec, view_normal);
   facing = 1.0 - abs(facing) * 0.2;
 
   finalColor.rgb = mix(colorEditMeshMiddle.rgb, finalColor.rgb, facing);
-
 #endif
 
 #ifdef USE_WORLD_CLIP_PLANES
-  world_clip_planes_calc_clip_distance((ModelMatrix * vec4(pos, 1.0)).xyz);
+  world_clip_planes_calc_clip_distance(world_pos.xyz);
 #endif
 }
index 97d4bdacf0730101bd840b8327d4eea1df968f1c..5a3eb38fb6bf726000ce8c0eca989719a78f00bf 100644 (file)
@@ -2,8 +2,6 @@
 layout(lines_adjacency) in;
 layout(line_strip, max_vertices = 2) out;
 
-uniform mat4 ProjectionMatrix;
-
 in vec4 pPos[];
 in vec3 vPos[];
 
index 0b5f4733ebcec1bd46cc97305caf7ccb7883a752..5928d60b311130b2d5c6657cbf1cdccbd52b33aa 100644 (file)
@@ -1,6 +1,4 @@
 
-uniform mat4 ModelViewMatrix;
-uniform mat4 ModelViewProjectionMatrix;
 uniform mat4 ModelMatrix;
 
 in vec3 pos;
@@ -10,12 +8,13 @@ out vec3 vPos;
 
 void main()
 {
-  vPos = (ModelViewMatrix * vec4(pos, 1.0)).xyz;
-  pPos = ModelViewProjectionMatrix * vec4(pos, 1.0);
+  vec3 world_pos = point_object_to_world(pos);
+  vPos = point_world_to_view(world_pos);
+  pPos = point_world_to_ndc(world_pos);
   /* Small bias to always be on top of the geom. */
   pPos.z -= 1e-3;
 
 #ifdef USE_WORLD_CLIP_PLANES
-  world_clip_planes_calc_clip_distance((ModelMatrix * vec4(pos, 1.0)).xyz);
+  world_clip_planes_calc_clip_distance(world_pos).xyz);
 #endif
 }
index e51b829adb5b7d6c64d42665f811eb8b7d85b5bb..921a0a5721821a9d45f7ac2db82457f6e3ec24c3 100644 (file)
@@ -1,6 +1,6 @@
 
-uniform mat4 ModelViewMatrix;
-uniform mat4 ProjectionMatrix;
+uniform mat4 ModelMatrix;
+
 uniform float pixel_size;
 uniform float size;
 
@@ -12,10 +12,13 @@ flat out float finalVal;
 
 void main()
 {
-  gl_Position = ModelViewMatrix * vec4(pos, 1.0);
+  vec3 world_pos = point_object_to_world(pos);
+
+  float view_z = dot(ViewMatrixInverse[2].xyz, world_pos - ViewMatrixInverse[3].xyz);
 
-  float psize = (ProjectionMatrix[3][3] == 0.0) ? (size / (-gl_Position.z * pixel_size)) :
-                                                  (size / pixel_size);
+  bool is_persp = (ProjectionMatrix[3][3] == 0.0);
+  float psize = (is_persp) ? (size / (-view_z * pixel_size)) : (size / pixel_size);
+  gl_Position = point_world_to_ndc(world_pos);
 
   gl_PointSize = psize;
 
@@ -31,7 +34,5 @@ void main()
   // convert to PointCoord units
   radii /= psize;
 
-  gl_Position = ProjectionMatrix * gl_Position;
-
   finalVal = val;
 }
index 0f6266264985777f39b3f0fe92e21bcfa4e5eeef..f1107de806862edf8b7d0e0060e82fb2e676cdf9 100644 (file)
@@ -1,18 +1,19 @@
 
-uniform mat4 ModelViewProjectionMatrix;
-uniform mat4 ViewProjectionMatrix;
-uniform mat4 ModelViewMatrix;
-uniform mat4 ProjectionMatrix;
-uniform int screen_space;
+uniform mat4 ModelMatrix;
+
+uniform bool screen_space;
 uniform float draw_size;
 uniform vec3 color;
 uniform sampler1D ramp;
 
+/* ---- Instantiated Attrs ---- */
+in vec3 inst_pos;
+in int axis;
+
+/* ---- Per instance Attrs ---- */
 in vec3 pos;
 in vec4 rot;
 in float val;
-in vec3 inst_pos;
-in int axis;
 
 flat out vec4 finalColor;
 
@@ -24,8 +25,9 @@ vec3 rotate(vec3 vec, vec4 quat)
 
 void main()
 {
-  if (screen_space == 1) {
-    gl_Position = ModelViewMatrix * vec4(pos, 1.0) + vec4(inst_pos * draw_size, 0.0);
+  if (screen_space) {
+    gl_Position = ViewMatrix * (ModelMatrix * vec4(pos, 1.0));
+    gl_Position.xyz += inst_pos * draw_size;
     gl_Position = ProjectionMatrix * gl_Position;
   }
   else {
@@ -35,7 +37,8 @@ void main()
       size *= 2;
     }
 
-    gl_Position = ModelViewProjectionMatrix * vec4(pos + rotate(inst_pos * size, rot), 1.0);
+    vec3 pos_rot = pos + rotate(inst_pos * size, rot);
+    gl_Position = point_object_to_ndc(pos_rot);
   }
 
 #ifdef USE_AXIS