[Bf-blender-cvs] [d57741d91f0] blender2.8: Eevee: Simplify/Fix view_vecs calculation.

Clément Foucault noreply at git.blender.org
Thu Feb 1 20:49:04 CET 2018


Commit: d57741d91f085657031a361ff7922838cb7f0a31
Author: Clément Foucault
Date:   Thu Feb 1 18:07:09 2018 +0100
Branches: blender2.8
https://developer.blender.org/rBd57741d91f085657031a361ff7922838cb7f0a31

Eevee: Simplify/Fix view_vecs calculation.

Now view_vecs[0][2] ALWAYS contains Near clip plane and view_vecs[1][2] = far - near.

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

M	source/blender/draw/engines/eevee/eevee_materials.c
M	source/blender/draw/engines/eevee/eevee_volumes.c
M	source/blender/draw/engines/eevee/shaders/bsdf_common_lib.glsl

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

diff --git a/source/blender/draw/engines/eevee/eevee_materials.c b/source/blender/draw/engines/eevee/eevee_materials.c
index 8c0ba921419..8f2a311c675 100644
--- a/source/blender/draw/engines/eevee/eevee_materials.c
+++ b/source/blender/draw/engines/eevee/eevee_materials.c
@@ -485,6 +485,46 @@ void EEVEE_update_noise(EEVEE_PassList *psl, EEVEE_FramebufferList *fbl, double
 	DRW_framebuffer_texture_detach(e_data.util_tex);
 }
 
+static void EEVEE_update_viewvecs(float invproj[4][4], float winmat[4][4], float (*r_viewvecs)[4])
+{
+	/* view vectors for the corners of the view frustum.
+	 * Can be used to recreate the world space position easily */
+	float view_vecs[4][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},
+	    {-1.0f, -1.0f,  1.0f, 1.0f}
+	};
+
+	/* convert the view vectors to view space */
+	const bool is_persp = (winmat[3][3] == 0.0f);
+	for (int i = 0; i < 4; i++) {
+		mul_project_m4_v3(invproj, 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]);
+		}
+	}
+
+	/**
+	 * If ortho : 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 Persp : 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_v4_v4(r_viewvecs[0], view_vecs[0]);
+
+	/* we need to store the differences */
+	r_viewvecs[1][0] = view_vecs[1][0] - view_vecs[0][0];
+	r_viewvecs[1][1] = view_vecs[2][1] - view_vecs[0][1];
+	r_viewvecs[1][2] = view_vecs[3][2] - view_vecs[0][2];
+}
+
 void EEVEE_materials_init(EEVEE_ViewLayerData *sldata, EEVEE_StorageList *stl, EEVEE_FramebufferList *fbl)
 {
 	if (!e_data.frag_shader_lib) {
@@ -569,44 +609,10 @@ void EEVEE_materials_init(EEVEE_ViewLayerData *sldata, EEVEE_StorageList *stl, E
 	{
 		/* Update view_vecs */
 		float invproj[4][4], winmat[4][4];
-		/* view vectors for the corners of the view frustum.
-		 * Can be used to recreate the world space position easily */
-		float view_vecs[3][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}
-		};
-
-		/* invert the view matrix */
 		DRW_viewport_matrix_get(winmat, DRW_MAT_WIN);
-		invert_m4_m4(invproj, winmat);
-		const bool is_persp = (winmat[3][3] == 0.0f);
-
-		/* convert the view vectors to view space */
-		for (int i = 0; i < 3; i++) {
-			mul_m4_v4(invproj, view_vecs[i]);
-			/* normalized trick see:
-			 * http://www.derschmale.com/2014/01/26/reconstructing-positions-from-the-depth-buffer */
-			mul_v3_fl(view_vecs[i], 1.0f / view_vecs[i][3]);
-			if (is_persp)
-				mul_v3_fl(view_vecs[i], 1.0f / view_vecs[i][2]);
-			view_vecs[i][3] = 1.0;
-		}
-
-		copy_v4_v4(sldata->common_data.view_vecs[0], view_vecs[0]);
-		copy_v4_v4(sldata->common_data.view_vecs[1], view_vecs[1]);
+		DRW_viewport_matrix_get(invproj, DRW_MAT_WININV);
 
-		/* we need to store the differences */
-		sldata->common_data.view_vecs[1][0] -= view_vecs[0][0];
-		sldata->common_data.view_vecs[1][1] = view_vecs[2][1] - view_vecs[0][1];
-
-		/* calculate a depth offset as well */
-		if (!is_persp) {
-			float vec_far[] = {-1.0f, -1.0f, 1.0f, 1.0f};
-			mul_m4_v4(invproj, vec_far);
-			mul_v3_fl(vec_far, 1.0f / vec_far[3]);
-			sldata->common_data.view_vecs[1][2] = vec_far[2] - view_vecs[0][2];
-		}
+		EEVEE_update_viewvecs(invproj, winmat, sldata->common_data.view_vecs);
 	}
 
 	{
diff --git a/source/blender/draw/engines/eevee/eevee_volumes.c b/source/blender/draw/engines/eevee/eevee_volumes.c
index a960682e8c9..b97c091bd43 100644
--- a/source/blender/draw/engines/eevee/eevee_volumes.c
+++ b/source/blender/draw/engines/eevee/eevee_volumes.c
@@ -304,7 +304,7 @@ int EEVEE_volumes_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
 		}
 		else {
 			const float clip_start = common_data->view_vecs[0][2];
-			const float clip_end = common_data->view_vecs[1][2];
+			const float clip_end = clip_start + common_data->view_vecs[1][2];
 			integration_start = min_ff(integration_end, clip_start);
 			integration_end = max_ff(-integration_end, clip_end);
 
diff --git a/source/blender/draw/engines/eevee/shaders/bsdf_common_lib.glsl b/source/blender/draw/engines/eevee/shaders/bsdf_common_lib.glsl
index 84ee0de3f12..9e5f8a33270 100644
--- a/source/blender/draw/engines/eevee/shaders/bsdf_common_lib.glsl
+++ b/source/blender/draw/engines/eevee/shaders/bsdf_common_lib.glsl
@@ -325,7 +325,7 @@ vec2 get_uvs_from_view(vec3 view)
 vec3 get_view_space_from_depth(vec2 uvcoords, float depth)
 {
 	if (ProjectionMatrix[3][3] == 0.0) {
-		return (viewVecs[0].xyz + vec3(uvcoords, 0.0) * viewVecs[1].xyz) * get_view_z_from_depth(depth);
+		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;



More information about the Bf-blender-cvs mailing list