[Bf-blender-cvs] [ba2f0ad] viewport_experiments: Viewport DOF:

Antony Riakiotakis noreply at git.blender.org
Wed Oct 29 16:18:31 CET 2014


Commit: ba2f0ad7790523c3a8ee1bb8b03c0716de596242
Author: Antony Riakiotakis
Date:   Wed Oct 29 16:18:19 2014 +0100
Branches: viewport_experiments
https://developer.blender.org/rBba2f0ad7790523c3a8ee1bb8b03c0716de596242

Viewport DOF:

* Scons builds correctly
* Separate vertex shader file for DOF, will make handling main easier
* Downsample color buffer + blur it. Circle of confusion is calculated
from zplane instead of length of distance from camera.

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

M	SConstruct
M	release/scripts/startup/bl_ui/space_view3d.py
M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/SConscript
M	source/blender/gpu/intern/gpu_compositing.c
M	source/blender/gpu/shaders/gpu_shader_fx_dof_frag.glsl
A	source/blender/gpu/shaders/gpu_shader_fx_dof_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_fx_lib.glsl
M	source/blender/gpu/shaders/gpu_shader_fx_ssao_frag.glsl
M	source/blender/gpu/shaders/gpu_shader_fx_vert.glsl

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

diff --git a/SConstruct b/SConstruct
index e53b6d9..ec07720 100644
--- a/SConstruct
+++ b/SConstruct
@@ -756,6 +756,8 @@ if B.targets != ['cudakernels']:
     data_to_c_simple("source/blender/gpu/shaders/gpu_shader_vsm_store_vert.glsl")
     data_to_c_simple("source/blender/gpu/shaders/gpu_shader_fx_ssao_frag.glsl")
     data_to_c_simple("source/blender/gpu/shaders/gpu_shader_fx_dof_frag.glsl")
+    data_to_c_simple("source/blender/gpu/shaders/gpu_shader_fx_dof_vert.glsl")
+    data_to_c_simple("source/blender/gpu/shaders/gpu_shader_fx_lib.glsl")
     data_to_c_simple("source/blender/gpu/shaders/gpu_shader_fx_vert.glsl")
     data_to_c_simple("intern/opencolorio/gpu_shader_display_transform.glsl")
 
diff --git a/release/scripts/startup/bl_ui/space_view3d.py b/release/scripts/startup/bl_ui/space_view3d.py
index 460627e..2da8a60 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -2897,7 +2897,8 @@ class VIEW3D_PT_view3d_shading(Panel):
             if view.depth_of_field:
                 subcol = col.column(align=True)
                 subcol.prop(view, "dof_focus_distance")
-                subcol.prop(view, "dof_aperture")
+                #fstop is preferable?..
+                #subcol.prop(view, "dof_aperture")
                 subcol.prop(view, "dof_fstop")
                 subcol.prop(view, "dof_focal_length")
                 subcol.prop(view, "dof_sensor")
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index 555c100..92841e4 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -60,6 +60,7 @@ set(SRC
 	shaders/gpu_shader_fx_lib.glsl
 	shaders/gpu_shader_fx_ssao_frag.glsl
 	shaders/gpu_shader_fx_dof_frag.glsl
+	shaders/gpu_shader_fx_dof_vert.glsl
 	shaders/gpu_shader_fx_vert.glsl
 	shaders/gpu_shader_material.glsl
 	shaders/gpu_shader_sep_gaussian_blur_frag.glsl
@@ -95,6 +96,7 @@ data_to_c_simple(shaders/gpu_shader_vsm_store_vert.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_fx_vert.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_fx_ssao_frag.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_fx_dof_frag.glsl SRC)
+data_to_c_simple(shaders/gpu_shader_fx_dof_vert.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_fx_lib.glsl SRC)
 
 if(WITH_GAMEENGINE)
diff --git a/source/blender/gpu/SConscript b/source/blender/gpu/SConscript
index 2fe64e9..653dbb0 100644
--- a/source/blender/gpu/SConscript
+++ b/source/blender/gpu/SConscript
@@ -65,7 +65,10 @@ import os
 sources.extend((
     os.path.join(env['DATA_SOURCES'], "gpu_shader_simple_frag.glsl.c"),
     os.path.join(env['DATA_SOURCES'], "gpu_shader_simple_vert.glsl.c"),
-    os.path.join(env['DATA_SOURCES'], "gpu_shader_fx_frag.glsl.c"),
+    os.path.join(env['DATA_SOURCES'], "gpu_shader_fx_ssao_frag.glsl.c"),
+    os.path.join(env['DATA_SOURCES'], "gpu_shader_fx_dof_frag.glsl.c"),
+    os.path.join(env['DATA_SOURCES'], "gpu_shader_fx_dof_vert.glsl.c"),
+    os.path.join(env['DATA_SOURCES'], "gpu_shader_fx_lib.glsl.c"),
     os.path.join(env['DATA_SOURCES'], "gpu_shader_fx_vert.glsl.c"),
     os.path.join(env['DATA_SOURCES'], "gpu_shader_material.glsl.c"),
     os.path.join(env['DATA_SOURCES'], "gpu_shader_sep_gaussian_blur_frag.glsl.c"),
diff --git a/source/blender/gpu/intern/gpu_compositing.c b/source/blender/gpu/intern/gpu_compositing.c
index d4a06d0..a73ce17 100644
--- a/source/blender/gpu/intern/gpu_compositing.c
+++ b/source/blender/gpu/intern/gpu_compositing.c
@@ -335,8 +335,6 @@ bool GPU_fx_do_composite_pass(GPUFX *fx, struct View3D *v3d, struct RegionView3D
 	int i;
 	/* number of passes left. when there are no more passes, the result is passed to the frambuffer */
 	int passes_left = fx->num_passes;
-	/* dimensions of screen (used in many shaders)*/
-	float screen_dim[2] = {fx->gbuffer_dim[0], fx->gbuffer_dim[1]};
 	/* view vectors for the corners of the view frustum. Can be used to recreate the world space position easily */
 	float viewvecs[3][4] = {
 	    {-1.0f, -1.0f, -1.0f, 1.0f},
@@ -499,20 +497,22 @@ bool GPU_fx_do_composite_pass(GPUFX *fx, struct View3D *v3d, struct RegionView3D
 		/* pass first, first level of blur in low res buffer */
 		{
 			float dof_params[4];
-			int screendim_uniform, color_uniform, depth_uniform, dof_uniform;
+			int invrendertargetdim_uniform, color_uniform, depth_uniform, dof_uniform;
 			int viewvecs_uniform;
 
 			float scale = scene->unit.system ? scene->unit.scale_length : 1.0f;
 			float scale_camera = 0.001f / scale;
 			float aperture = 2.0f * scale_camera * v3d->dof_focal_length / v3d->dof_fstop; // * v3d->dof_aperture;
 
+			float invrendertargetdim[2] = {1.0f / fx->gbuffer_dim[0], 1.0f / fx->gbuffer_dim[1]};
+
 			dof_params[0] = aperture * fabs(scale_camera * v3d->dof_focal_length / (v3d->dof_focus_distance - scale_camera * v3d->dof_focal_length));
 			dof_params[1] = v3d->dof_focus_distance;
 			dof_params[2] = fx->gbuffer_dim[0] / (scale_camera * v3d->dof_sensor);
 			dof_params[3] = 0.0f;
 
 			dof_uniform = GPU_shader_get_uniform(dof_shader_pass1, "dof_params");
-			screendim_uniform = GPU_shader_get_uniform(dof_shader_pass1, "screendim");
+			invrendertargetdim_uniform = GPU_shader_get_uniform(dof_shader_pass1, "invrendertargetdim");
 			color_uniform = GPU_shader_get_uniform(dof_shader_pass1, "colorbuffer");
 			depth_uniform = GPU_shader_get_uniform(dof_shader_pass1, "depthbuffer");
 			viewvecs_uniform = GPU_shader_get_uniform(dof_shader_pass1, "viewvecs");
@@ -520,7 +520,7 @@ bool GPU_fx_do_composite_pass(GPUFX *fx, struct View3D *v3d, struct RegionView3D
 			GPU_shader_bind(dof_shader_pass1);
 
 			GPU_shader_uniform_vector(dof_shader_pass1, dof_uniform, 4, 1, dof_params);
-			GPU_shader_uniform_vector(dof_shader_pass1, screendim_uniform, 2, 1, screen_dim);
+			GPU_shader_uniform_vector(dof_shader_pass1, invrendertargetdim_uniform, 2, 1, invrendertargetdim);
 			GPU_shader_uniform_vector(dof_shader_pass1, viewvecs_uniform, 4, 3, viewvecs[0]);
 
 			GPU_texture_bind(src, numslots++);
diff --git a/source/blender/gpu/shaders/gpu_shader_fx_dof_frag.glsl b/source/blender/gpu/shaders/gpu_shader_fx_dof_frag.glsl
index 6c03155..a3a47d7 100644
--- a/source/blender/gpu/shaders/gpu_shader_fx_dof_frag.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_fx_dof_frag.glsl
@@ -1,5 +1,5 @@
 // amount of offset to move one pixel left-right
-uniform vec2 screendim;
+uniform vec2 invrendertargetdim;
 // color buffer
 uniform sampler2D colorbuffer;
 //blurred color buffer for DOF effect
@@ -15,44 +15,65 @@ uniform vec4 dof_params;
 
 uniform vec4 viewvecs[3];
 
-float calculate_dof_coc(in vec3 viewposition)
+/* color texture coordinates, offset by a small amount */
+varying vec2 color_uv1;
+varying vec2 color_uv2;
+
+float calculate_dof_coc(in float zdepth)
+{
+	float coc = dof_params.x * abs(1.0 - dof_params.y / zdepth);
+
+	/* multiply by 1.0 / sensor size to get the normalized size */
+	return coc * dof_params.z;
+}
+
+/* near coc only! when distance is nearer than focus plane first term is bigger than one */
+float calculate_near_coc(in float zdepth)
 {
-    float dist = length(viewposition);
-    float coc = dof_params.x * abs(1.0 - dof_params.y / dist);
-    
-    /* multiply by 1.0 / sensor size to get the normalized size */
-    return coc * dof_params.z;
+	float coc = dof_params.x * max(dof_params.y / zdepth - 1.0, 0.0);
+
+	/* multiply by 1.0 / sensor size to get the normalized size */
+	return coc * dof_params.z;
 }
 
 /* first pass blurs the color buffer heavily and gets the near coc only. There are too many texture accesses here but they are done on a
  * lower resolution image */
 void first_pass()
 {
-    float depth = texture2D(depthbuffer, uvcoordsvar.xy).r;
-    vec4 color = texture2D(colorbuffer, uvcoordsvar.xy);
+	float depth = texture2D(depthbuffer, uvcoordsvar.xy).r;
+
+	/* amount to add to uvs so that they move one row further */
+	vec2 offset_row = vec2(0.0, 2.0 * invrendertargetdim.y);
+
+	/* heavily blur the image */
+	vec4 color = texture2D(colorbuffer, color_uv1.xy);
+	color += texture2D(colorbuffer, color_uv1.xy + offset_row);
+	color += texture2D(colorbuffer, color_uv2.xy);
+	color += texture2D(colorbuffer, color_uv2.xy + offset_row);
+	color /= 4.0;
 
-    vec3 position = get_view_space_from_depth(uvcoordsvar.xy, viewvecs[0].xyz, viewvecs[1].xyz, depth);
-    float coc = calculate_dof_coc(position);
+	float zdepth = get_view_space_z_from_depth(viewvecs[0].z, viewvecs[1].z, depth);
+	float coc = calculate_near_coc(zdepth);
 
-    gl_FragColor = vec4(coc * color.rgb, 1.0);
+	gl_FragColor = vec4(color.rgb, coc);
 }
 
 
 /* second pass, just visualize the first pass contents */
 void second_pass()
 {
-    vec4 color = texture2D(colorbuffer, uvcoordsvar.xy);
+	vec4 color = texture2D(colorbuffer, uvcoordsvar.xy);
 
-    gl_FragColor = vec4(color.a * color.rgb, 1.0);
+	gl_FragColor = vec4(color.a * color.rgb, 1.0);
 }
 
 
 void main()
 {
 #ifdef FIRST_PASS
-    first_pass();
+	first_pass();
 #elif defined(SECOND_PASS)
-    second_pass();
+	second_pass();
 #elif defined(THIRD_PASS)
 #elif defined(FOURTH_PASS)
 #endif
diff --git a/source/blender/gpu/shaders/gpu_shader_fx_dof_vert.glsl b/source/blender/gpu/shaders/gpu_shader_fx_dof_vert.glsl
new file mode 100644
index 0000000..ad82d3f
--- /dev/null
+++ b/source/blender/gpu/shaders/gpu_shader_fx_dof_vert.glsl
@@ -0,0 +1,41 @@
+uniform vec2 invrendertargetdim;
+
+//texture coordinates for framebuffer read
+varying vec4 uvcoordsvar;
+
+/* color texture coordinates, offset by a small amount */
+varying vec2 color_uv1;
+varying vec2 color_uv2;
+
+//very simple shader for gull screen FX, just pass values on
+
+void vert_generic()
+{
+	uvcoordsvar = gl_MultiTexCoord0;
+	gl_Position = gl_Vertex;
+}
+
+void vert_dof_first_pass()
+{
+	uvcoordsvar = gl_MultiTexCoord0;
+
+	/* we offset the texture coordinates by 1.5 pixel, then we reuse that to sample the surrounding pixels */
+	color_uv1 = gl_MultiTexCoord0.xy + vec2(-1.5, -1.5) * invrendertargetdim;
+	color_uv1 = gl_MultiTexCoord0.xy + vec2(0.5, -1.5) * invrendertargetdim;
+
+	gl_Position = gl_Vertex;
+}
+
+void main()
+{
+#ifdef FIRST_PASS
+	vert_dof_first_pass();
+#elif defined(SECOND_PASS)
+	vert_generic();
+#el

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list