Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClément Foucault <foucault.clem@gmail.com>2022-10-06 19:25:29 +0300
committerClément Foucault <foucault.clem@gmail.com>2022-10-07 13:43:09 +0300
commit8f96d0f73285a3f80a711588555650c696e2b726 (patch)
tree9882257ab334878e8c6ba2974a2ae86d7605ec11 /source/blender
parent03a7f1d7b1103e693a5a3ee95ed5f4bc46b1ca54 (diff)
DRW: Remove view vectors
This is part of the effor to simplify the View struct in order to implement multiview rendering. The viewvecs can easilly be replace by projection matrix operation. Even if slightly more complex, there is no performance impact.
Diffstat (limited to 'source/blender')
-rw-r--r--source/blender/draw/engines/workbench/shaders/workbench_transparent_accum_frag.glsl3
-rw-r--r--source/blender/draw/intern/draw_manager_data.cc46
-rw-r--r--source/blender/draw/intern/draw_shader_shared.h2
-rw-r--r--source/blender/draw/intern/draw_view.cc53
-rw-r--r--source/blender/draw/intern/draw_view.hh1
-rw-r--r--source/blender/draw/intern/shaders/common_view_lib.glsl44
6 files changed, 22 insertions, 127 deletions
diff --git a/source/blender/draw/engines/workbench/shaders/workbench_transparent_accum_frag.glsl b/source/blender/draw/engines/workbench/shaders/workbench_transparent_accum_frag.glsl
index 32191835668..cd5ac21688c 100644
--- a/source/blender/draw/engines/workbench/shaders/workbench_transparent_accum_frag.glsl
+++ b/source/blender/draw/engines/workbench/shaders/workbench_transparent_accum_frag.glsl
@@ -14,7 +14,8 @@ float linear_zdepth(float depth, mat4 proj_mat)
}
else {
/* Return depth from near plane. */
- return depth * drw_view.viewvecs[1].z;
+ float z_delta = -2.0 / proj_mat[2][2];
+ return depth * z_delta;
}
}
diff --git a/source/blender/draw/intern/draw_manager_data.cc b/source/blender/draw/intern/draw_manager_data.cc
index 1d678886380..b7e237231ff 100644
--- a/source/blender/draw/intern/draw_manager_data.cc
+++ b/source/blender/draw/intern/draw_manager_data.cc
@@ -2146,52 +2146,6 @@ static void draw_view_matrix_state_update(DRWView *view,
invert_m4_m4(view->persinv.values, view->persmat.values);
const bool is_persp = (winmat[3][3] == 0.0f);
-
- /* Near clip distance. */
- storage->viewvecs[0][3] = (is_persp) ? -winmat[3][2] / (winmat[2][2] - 1.0f) :
- -(winmat[3][2] + 1.0f) / winmat[2][2];
-
- /* Far clip distance. */
- storage->viewvecs[1][3] = (is_persp) ? -winmat[3][2] / (winmat[2][2] + 1.0f) :
- -(winmat[3][2] - 1.0f) / winmat[2][2];
-
- /* view vectors for the corners of the view frustum.
- * Can be used to recreate the world space position easily */
- float view_vecs[4][3] = {
- {-1.0f, -1.0f, -1.0f},
- {1.0f, -1.0f, -1.0f},
- {-1.0f, 1.0f, -1.0f},
- {-1.0f, -1.0f, 1.0f},
- };
-
- /* convert the view vectors to view space */
- for (int i = 0; i < 4; i++) {
- mul_project_m4_v3(storage->wininv.values, view_vecs[i]);
- /* normalized trick see:
- * http://www.derschmale.com/2014/01/26/reconstructing-positions-from-the-depth-buffer */
- if (is_persp) {
- /* Divide XY by Z. */
- mul_v2_fl(view_vecs[i], 1.0f / view_vecs[i][2]);
- }
- }
-
- /**
- * - When orthographic:
- * `view_vecs[0]` is the near-bottom-left corner of the frustum and
- * `view_vecs[1]` is the vector going from the near-bottom-left corner to
- * the far-top-right corner.
- * - When perspective:
- * `view_vecs[0].xy` and `view_vecs[1].xy` are respectively the bottom-left corner
- * when Z = 1, and top-left corner if `Z = 1`.
- * `view_vecs[0].z` the near clip distance and `view_vecs[1].z` is the (signed)
- * distance from the near plane to the far clip plane.
- */
- copy_v3_v3(storage->viewvecs[0], view_vecs[0]);
-
- /* we need to store the differences */
- storage->viewvecs[1][0] = view_vecs[1][0] - view_vecs[0][0];
- storage->viewvecs[1][1] = view_vecs[2][1] - view_vecs[0][1];
- storage->viewvecs[1][2] = view_vecs[3][2] - view_vecs[0][2];
}
DRWView *DRW_view_create(const float viewmat[4][4],
diff --git a/source/blender/draw/intern/draw_shader_shared.h b/source/blender/draw/intern/draw_shader_shared.h
index b8c2d8890a5..c0c717d910d 100644
--- a/source/blender/draw/intern/draw_shader_shared.h
+++ b/source/blender/draw/intern/draw_shader_shared.h
@@ -68,7 +68,6 @@ struct ViewInfos {
float4x4 wininv;
float4 clip_planes[6];
- float4 viewvecs[2];
float2 viewport_size;
float2 viewport_size_inverse;
@@ -91,7 +90,6 @@ BLI_STATIC_ASSERT_ALIGN(ViewInfos, 16)
# define ProjectionMatrix drw_view.winmat
# define ProjectionMatrixInverse drw_view.wininv
# define clipPlanes drw_view.clip_planes
-# define ViewVecs drw_view.viewvecs
#endif
/** \} */
diff --git a/source/blender/draw/intern/draw_view.cc b/source/blender/draw/intern/draw_view.cc
index 3b16ea74a67..3475b11be89 100644
--- a/source/blender/draw/intern/draw_view.cc
+++ b/source/blender/draw/intern/draw_view.cc
@@ -24,8 +24,6 @@ void View::sync(const float4x4 &view_mat, const float4x4 &win_mat)
data_.is_inverted = (is_negative_m4(view_mat.ptr()) == is_negative_m4(win_mat.ptr()));
- update_view_vectors();
-
BoundBox &bound_box = *reinterpret_cast<BoundBox *>(&culling_.corners);
BoundSphere &bound_sphere = *reinterpret_cast<BoundSphere *>(&culling_.bound_sphere);
frustum_boundbox_calc(bound_box);
@@ -224,57 +222,6 @@ void View::update_viewport_size()
}
}
-void View::update_view_vectors()
-{
- bool is_persp = data_.winmat[3][3] == 0.0f;
-
- /* Near clip distance. */
- data_.viewvecs[0][3] = (is_persp) ? -data_.winmat[3][2] / (data_.winmat[2][2] - 1.0f) :
- -(data_.winmat[3][2] + 1.0f) / data_.winmat[2][2];
-
- /* Far clip distance. */
- data_.viewvecs[1][3] = (is_persp) ? -data_.winmat[3][2] / (data_.winmat[2][2] + 1.0f) :
- -(data_.winmat[3][2] - 1.0f) / data_.winmat[2][2];
-
- /* View vectors for the corners of the view frustum.
- * Can be used to recreate the world space position easily */
- float3 view_vecs[4] = {
- {-1.0f, -1.0f, -1.0f},
- {1.0f, -1.0f, -1.0f},
- {-1.0f, 1.0f, -1.0f},
- {-1.0f, -1.0f, 1.0f},
- };
-
- /* Convert the view vectors to view space */
- for (int i = 0; i < 4; i++) {
- mul_project_m4_v3(data_.wininv.ptr(), view_vecs[i]);
- /* Normalized trick see:
- * http://www.derschmale.com/2014/01/26/reconstructing-positions-from-the-depth-buffer */
- if (is_persp) {
- view_vecs[i].x /= view_vecs[i].z;
- view_vecs[i].y /= view_vecs[i].z;
- }
- }
-
- /**
- * - If orthographic:
- * `view_vecs[0]` is the near-bottom-left corner of the frustum and
- * `view_vecs[1]` is the vector going from the near-bottom-left corner to
- * the far-top-right corner.
- * - If perspective:
- * `view_vecs[0].xy` and `view_vecs[1].xy` are respectively the bottom-left corner
- * when `Z = 1`, and top-left corner if `Z = 1`.
- * `view_vecs[0].z` the near clip distance and `view_vecs[1].z` is the (signed)
- * distance from the near plane to the far clip plane.
- */
- copy_v3_v3(data_.viewvecs[0], view_vecs[0]);
-
- /* we need to store the differences */
- data_.viewvecs[1][0] = view_vecs[1][0] - view_vecs[0][0];
- data_.viewvecs[1][1] = view_vecs[2][1] - view_vecs[0][1];
- data_.viewvecs[1][2] = view_vecs[3][2] - view_vecs[0][2];
-}
-
void View::bind()
{
update_viewport_size();
diff --git a/source/blender/draw/intern/draw_view.hh b/source/blender/draw/intern/draw_view.hh
index d3cdea46956..bc9033c2a65 100644
--- a/source/blender/draw/intern/draw_view.hh
+++ b/source/blender/draw/intern/draw_view.hh
@@ -85,7 +85,6 @@ class View {
void bind();
void compute_visibility(ObjectBoundsBuf &bounds, uint resource_len, bool debug_freeze);
- void update_view_vectors();
void update_viewport_size();
void frustum_boundbox_calc(BoundBox &bbox);
diff --git a/source/blender/draw/intern/shaders/common_view_lib.glsl b/source/blender/draw/intern/shaders/common_view_lib.glsl
index 2382337e782..a09bccc4f2e 100644
--- a/source/blender/draw/intern/shaders/common_view_lib.glsl
+++ b/source/blender/draw/intern/shaders/common_view_lib.glsl
@@ -17,10 +17,6 @@ layout(std140) uniform viewBlock
mat4 ProjectionMatrixInverse;
vec4 clipPlanes[6];
-
- /* View frustum corners [NDC(-1.0, -1.0, -1.0) & NDC(1.0, 1.0, 1.0)].
- * Fourth components are near and far values. */
- vec4 ViewVecs[2];
};
#endif /* USE_GPU_SHADER_CREATE_INFO */
@@ -34,9 +30,6 @@ layout(std140) uniform viewBlock
#define IS_DEBUG_MOUSE_FRAGMENT (ivec2(gl_FragCoord) == drw_view.mouse_pixel)
#define IS_FIRST_INVOCATION (gl_GlobalInvocationID == uvec3(0))
-#define ViewNear (ViewVecs[0].w)
-#define ViewFar (ViewVecs[1].w)
-
#define cameraForward ViewMatrixInverse[2].xyz
#define cameraPos ViewMatrixInverse[3].xyz
vec3 cameraVec(vec3 P)
@@ -310,24 +303,26 @@ float buffer_depth(bool is_persp, float z, float zf, float zn)
float get_view_z_from_depth(float depth)
{
+ float d = 2.0 * depth - 1.0;
if (ProjectionMatrix[3][3] == 0.0) {
- float d = 2.0 * depth - 1.0;
- return -ProjectionMatrix[3][2] / (d + ProjectionMatrix[2][2]);
+ d = -ProjectionMatrix[3][2] / (d + ProjectionMatrix[2][2]);
}
else {
- return ViewVecs[0].z + depth * ViewVecs[1].z;
+ d = (d - ProjectionMatrix[3][2]) / ProjectionMatrix[2][2];
}
+ return d;
}
float get_depth_from_view_z(float z)
{
+ float d;
if (ProjectionMatrix[3][3] == 0.0) {
- float d = (-ProjectionMatrix[3][2] / z) - ProjectionMatrix[2][2];
- return d * 0.5 + 0.5;
+ d = (-ProjectionMatrix[3][2] / z) - ProjectionMatrix[2][2];
}
else {
- return (z - ViewVecs[0].z) / ViewVecs[1].z;
+ d = ProjectionMatrix[2][2] * z + ProjectionMatrix[3][2];
}
+ return d * 0.5 + 0.5;
}
vec2 get_uvs_from_view(vec3 view)
@@ -338,12 +333,9 @@ vec2 get_uvs_from_view(vec3 view)
vec3 get_view_space_from_depth(vec2 uvcoords, float depth)
{
- if (ProjectionMatrix[3][3] == 0.0) {
- return vec3(ViewVecs[0].xy + uvcoords * ViewVecs[1].xy, 1.0) * get_view_z_from_depth(depth);
- }
- else {
- return ViewVecs[0].xyz + vec3(uvcoords, depth) * ViewVecs[1].xyz;
- }
+ vec3 ndc = vec3(uvcoords, depth) * 2.0 - 1.0;
+ vec4 p = ProjectionMatrixInverse * vec4(ndc, 1.0);
+ return p.xyz / p.w;
}
vec3 get_world_space_from_depth(vec2 uvcoords, float depth)
@@ -351,14 +343,18 @@ vec3 get_world_space_from_depth(vec2 uvcoords, float depth)
return (ViewMatrixInverse * vec4(get_view_space_from_depth(uvcoords, depth), 1.0)).xyz;
}
-vec3 get_view_vector_from_screen_uv(vec2 uv)
+vec3 get_view_vector_from_screen_uv(vec2 uvcoords)
{
if (ProjectionMatrix[3][3] == 0.0) {
- return normalize(vec3(ViewVecs[0].xy + uv * ViewVecs[1].xy, 1.0));
- }
- else {
- return vec3(0.0, 0.0, 1.0);
+ vec2 ndc = vec2(uvcoords * 2.0 - 1.0);
+ /* This is the manual inversion of the ProjectionMatrix. */
+ vec3 vV = vec3((-ndc - ProjectionMatrix[2].xy) /
+ vec2(ProjectionMatrix[0][0], ProjectionMatrix[1][1]),
+ -ProjectionMatrix[2][2] - ProjectionMatrix[3][2]);
+ return normalize(vV);
}
+ /* Orthographic case. */
+ return vec3(0.0, 0.0, 1.0);
}
#endif /* COMMON_VIEW_LIB_GLSL */