diff options
12 files changed, 97 insertions, 81 deletions
diff --git a/source/blender/draw/engines/eevee/shaders/prepass_vert.glsl b/source/blender/draw/engines/eevee/shaders/prepass_vert.glsl index 883862855f3..7f154c9130a 100644 --- a/source/blender/draw/engines/eevee/shaders/prepass_vert.glsl +++ b/source/blender/draw/engines/eevee/shaders/prepass_vert.glsl @@ -1,5 +1,4 @@ -uniform mat4 ModelViewProjectionMatrix; uniform mat4 ModelMatrix; uniform mat4 ModelMatrixInverse; diff --git a/source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl b/source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl index a1c269d5a65..cef405d12d3 100644 --- a/source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl +++ b/source/blender/draw/engines/workbench/shaders/workbench_volume_frag.glsl @@ -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; diff --git a/source/blender/draw/engines/workbench/shaders/workbench_volume_vert.glsl b/source/blender/draw/engines/workbench/shaders/workbench_volume_vert.glsl index 7a418243fd3..1834173fee9 100644 --- a/source/blender/draw/engines/workbench/shaders/workbench_volume_vert.glsl +++ b/source/blender/draw/engines/workbench/shaders/workbench_volume_vert.glsl @@ -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); } diff --git a/source/blender/draw/engines/workbench/workbench_volume.c b/source/blender/draw/engines/workbench/workbench_volume.c index accc7e91576..d2f19f57dcf 100644 --- a/source/blender/draw/engines/workbench/workbench_volume.c +++ b/source/blender/draw/engines/workbench/workbench_volume.c @@ -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); } diff --git a/source/blender/draw/intern/draw_manager_data.c b/source/blender/draw/intern/draw_manager_data.c index e1f9f826ca5..1f48129116d 100644 --- a/source/blender/draw/intern/draw_manager_data.c +++ b/source/blender/draw/intern/draw_manager_data.c @@ -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 diff --git a/source/blender/draw/modes/object_mode.c b/source/blender/draw/modes/object_mode.c index c494e222b3d..7e5586f1b10 100644 --- a/source/blender/draw/modes/object_mode.c +++ b/source/blender/draw/modes/object_mode.c @@ -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); - } - } } } } diff --git a/source/blender/draw/modes/shaders/common_view_lib.glsl b/source/blender/draw/modes/shaders/common_view_lib.glsl index 40c43856b43..bdf184cb31e 100644 --- a/source/blender/draw/modes/shaders/common_view_lib.glsl +++ b/source/blender/draw/modes/shaders/common_view_lib.glsl @@ -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) diff --git a/source/blender/draw/modes/shaders/edit_mesh_overlay_vert.glsl b/source/blender/draw/modes/shaders/edit_mesh_overlay_vert.glsl index d5befc02764..b4476c98382 100644 --- a/source/blender/draw/modes/shaders/edit_mesh_overlay_vert.glsl +++ b/source/blender/draw/modes/shaders/edit_mesh_overlay_vert.glsl @@ -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 } diff --git a/source/blender/draw/modes/shaders/object_outline_prepass_geom.glsl b/source/blender/draw/modes/shaders/object_outline_prepass_geom.glsl index 97d4bdacf07..5a3eb38fb6b 100644 --- a/source/blender/draw/modes/shaders/object_outline_prepass_geom.glsl +++ b/source/blender/draw/modes/shaders/object_outline_prepass_geom.glsl @@ -2,8 +2,6 @@ layout(lines_adjacency) in; layout(line_strip, max_vertices = 2) out; -uniform mat4 ProjectionMatrix; - in vec4 pPos[]; in vec3 vPos[]; diff --git a/source/blender/draw/modes/shaders/object_outline_prepass_vert.glsl b/source/blender/draw/modes/shaders/object_outline_prepass_vert.glsl index 0b5f4733ebc..5928d60b311 100644 --- a/source/blender/draw/modes/shaders/object_outline_prepass_vert.glsl +++ b/source/blender/draw/modes/shaders/object_outline_prepass_vert.glsl @@ -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 } diff --git a/source/blender/draw/modes/shaders/object_particle_dot_vert.glsl b/source/blender/draw/modes/shaders/object_particle_dot_vert.glsl index e51b829adb5..921a0a57218 100644 --- a/source/blender/draw/modes/shaders/object_particle_dot_vert.glsl +++ b/source/blender/draw/modes/shaders/object_particle_dot_vert.glsl @@ -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; } diff --git a/source/blender/draw/modes/shaders/object_particle_prim_vert.glsl b/source/blender/draw/modes/shaders/object_particle_prim_vert.glsl index 0f626626498..f1107de8068 100644 --- a/source/blender/draw/modes/shaders/object_particle_prim_vert.glsl +++ b/source/blender/draw/modes/shaders/object_particle_prim_vert.glsl @@ -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 |