[Bf-blender-cvs] [6e8ba2f] viewport_experiments: DOF calculation shader.

Antony Riakiotakis noreply at git.blender.org
Mon Oct 27 20:46:06 CET 2014


Commit: 6e8ba2f3f55581a0355c19840ec73a84f6938483
Author: Antony Riakiotakis
Date:   Mon Oct 27 20:45:51 2014 +0100
Branches: viewport_experiments
https://developer.blender.org/rB6e8ba2f3f55581a0355c19840ec73a84f6938483

DOF calculation shader.

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

M	release/scripts/startup/bl_ui/space_view3d.py
M	source/blender/gpu/intern/gpu_compositing.c
M	source/blender/gpu/shaders/gpu_shader_fx_dof_frag.glsl
M	source/blender/makesdna/DNA_view3d_types.h
M	source/blender/makesrna/intern/rna_space.c

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

diff --git a/release/scripts/startup/bl_ui/space_view3d.py b/release/scripts/startup/bl_ui/space_view3d.py
index b0e5f9b..683e8f8 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -2896,9 +2896,10 @@ class VIEW3D_PT_view3d_shading(Panel):
             col.prop(view, "depth_of_field")
             if view.depth_of_field:
                 subcol = col.column(align=True)
-                subcol.prop(view, "dof_focal_distance")
+                subcol.prop(view, "dof_focus_distance")
                 subcol.prop(view, "dof_aperture")
                 subcol.prop(view, "dof_fstop")
+                subcol.prop(view, "dof_focal_length")
             col.prop(view, "ssao")
             if view.ssao:
                 subcol = col.column(align=True)
diff --git a/source/blender/gpu/intern/gpu_compositing.c b/source/blender/gpu/intern/gpu_compositing.c
index 9d211b1..bc9916d 100644
--- a/source/blender/gpu/intern/gpu_compositing.c
+++ b/source/blender/gpu/intern/gpu_compositing.c
@@ -60,6 +60,9 @@ struct GPUFX {
 	/* texture bound to the first color attachment of the gbuffer */
 	GPUTexture *color_buffer;
 
+	/* texture used for coc and color blurring */
+	GPUTexture *blur_buffer;
+
 	/* texture bound to the depth attachment of the gbuffer */
 	GPUTexture *depth_buffer;
 
@@ -364,9 +367,10 @@ bool GPU_fx_do_composite_pass(GPUFX *fx, struct View3D *v3d, struct RegionView3D
 	if (fx->effects & V3D_FX_DEPTH_OF_FIELD) {
 		fx_shader = GPU_shader_get_builtin_fx_shader(GPU_SHADER_FX_DEPTH_OF_FIELD);
 		if (fx_shader) {
-			float fac = v3d->dof_fstop * v3d->dof_aperture;
-			float dof_params[2] = {v3d->dof_aperture * fabs(fac / (v3d->dof_focal_distance - fac)),
-			                       v3d->dof_focal_distance};
+			int i;
+			float aperture = 0.001 * v3d->dof_focal_length / v3d->dof_fstop; // * v3d->dof_aperture;
+			float dof_params[2] = {aperture * fabs(0.001  * v3d->dof_focal_length / (0.1 * v3d->dof_focus_distance - 0.001 * v3d->dof_focal_length)),
+			                       0.1 * v3d->dof_focus_distance};
 			int screendim_uniform, color_uniform, depth_uniform, dof_uniform, blurred_uniform;
 
 			dof_uniform = GPU_shader_get_uniform(fx_shader, "dof_params");
@@ -375,10 +379,11 @@ bool GPU_fx_do_composite_pass(GPUFX *fx, struct View3D *v3d, struct RegionView3D
 			color_uniform = GPU_shader_get_uniform(fx_shader, "colorbuffer");
 			depth_uniform = GPU_shader_get_uniform(fx_shader, "depthbuffer");
 
+			GPU_shader_bind(fx_shader);
+
 			GPU_shader_uniform_vector(fx_shader, dof_uniform, 2, 1, dof_params);
 			GPU_shader_uniform_vector(fx_shader, screendim_uniform, 2, 1, screen_dim);
 
-
 			GPU_texture_bind(fx->color_buffer, numslots++);
 			GPU_shader_uniform_texture(fx_shader, blurred_uniform, fx->color_buffer);
 			/* generate mipmaps for the color buffer */
@@ -392,6 +397,21 @@ bool GPU_fx_do_composite_pass(GPUFX *fx, struct View3D *v3d, struct RegionView3D
 			GPU_texture_bind(fx->depth_buffer, numslots++);
 			GPU_depth_texture_mode(fx->depth_buffer, false, true);
 			GPU_shader_uniform_texture(fx_shader, depth_uniform, fx->depth_buffer);
+
+			glDisable(GL_DEPTH_TEST);
+			glDrawArrays(GL_QUADS, 0, 4);
+			/* disable bindings */
+			GPU_texture_unbind(fx->color_buffer);
+			GPU_depth_texture_mode(fx->depth_buffer, true, false);
+			GPU_texture_unbind(fx->depth_buffer);
+
+			/* same texture may be bound to more than one slot. Use this to explicitly disable texturing everywhere */
+			for (i = numslots; i > 0; i--) {
+				glActiveTexture(GL_TEXTURE0 + i - 1);
+				glBindTexture(GL_TEXTURE_2D, 0);
+				glDisable(GL_TEXTURE_2D);
+			}
+			numslots = 0;
 		}
 	}
 
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 526085c..a46a6bd 100644
--- a/source/blender/gpu/shaders/gpu_shader_fx_dof_frag.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_fx_dof_frag.glsl
@@ -32,9 +32,9 @@ vec3 get_view_space_from_depth(in vec2 uvcoords, float depth)
     return pos.xyz;
 }
 
-float calculate_dof_coc(in vec4 viewposition)
+float calculate_dof_coc(in vec3 viewposition)
 {
-    float dist = length(viewposition);
+    float dist = 0.1 * length(viewposition);
     float coc = dof_params.x * abs(1.0 - dof_params.y / dist);
     
     coc = clamp(coc, 0.0, 1.0);
@@ -46,11 +46,8 @@ void main()
 {
     float depth = texture2D(depthbuffer, uvcoordsvar.xy).r;
 
-    vec4 position = get_view_space_from_depth(uvcoordsvar, depth);
+    vec3 position = get_view_space_from_depth(uvcoordsvar.xy, depth);
     float coc = calculate_dof_coc(position);
-    // blend between blurred-non blurred images based on coc	
-    //vec4 color = coc * texture2D(blurredcolorbuffer, framecoords.xy) +
-    //       (1.0 - coc) * texture2D(colorbuffer, framecoords.xy);
 
-    gl_FragColor = vec4(vec3(coc), 1.0);
+    gl_FragColor = vec4(coc, coc, coc, 1.0);
 }
diff --git a/source/blender/makesdna/DNA_view3d_types.h b/source/blender/makesdna/DNA_view3d_types.h
index 7b63fb1..6bc7a8f 100644
--- a/source/blender/makesdna/DNA_view3d_types.h
+++ b/source/blender/makesdna/DNA_view3d_types.h
@@ -220,14 +220,16 @@ typedef struct View3D {
 	/* built-in shader effects */
 	int shader_fx;
 
-	float dof_focal_distance; /* focal distance for depth of field */
+	float dof_focus_distance; /* focal distance for depth of field */
 	float dof_aperture;           /* aperture for dof lens (could use fstop as well) */
 	float dof_fstop;
+	float dof_focal_length;
 	float ssao_darkening;
 	float ssao_color[3];
 	float ssao_distance_max;
 	float ssao_attenuation;
 	int ssao_ray_sample_mode; /* ray samples, we use presets here for easy control instead of */
+	int pad4;
 
 	void *properties_storage;		/* Nkey panel stores stuff here (runtime only!) */
 	struct Material *defmaterial;	/* used by matcap now */
diff --git a/source/blender/makesrna/intern/rna_space.c b/source/blender/makesrna/intern/rna_space.c
index bf014f1..552489a 100644
--- a/source/blender/makesrna/intern/rna_space.c
+++ b/source/blender/makesrna/intern/rna_space.c
@@ -2188,19 +2188,27 @@ static void rna_def_space_view3d(BlenderRNA *brna)
 	RNA_def_property_ui_text(prop, "Depth Of Field", "Use depth of field on viewport using the values from active camera");
 	RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, NULL);
 	
-	prop = RNA_def_property(srna, "dof_focal_distance", PROP_FLOAT, PROP_NONE);
-	RNA_def_property_ui_text(prop, "Focal distance", "Viewport dof focal distance");
-	RNA_def_property_range(prop, 0.0f, 100000.0f);
+	prop = RNA_def_property(srna, "dof_focus_distance", PROP_FLOAT, PROP_DISTANCE);
+	RNA_def_property_ui_text(prop, "Focus distance", "Viewport depth of field focus distance");
+	RNA_def_property_range(prop, 0.0f, FLT_MAX);
+	RNA_def_property_ui_range(prop, 0.0f, 5000.0f, 1, 2);
 	RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, NULL);
 
-	prop = RNA_def_property(srna, "dof_aperture", PROP_FLOAT, PROP_NONE);
+	prop = RNA_def_property(srna, "dof_aperture", PROP_FLOAT, PROP_DISTANCE);
 	RNA_def_property_ui_text(prop, "Aperture", "Aperture for dof effect");
 	RNA_def_property_range(prop, 0.0f, 250.0f);
 	RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, NULL);
 
+	prop = RNA_def_property(srna, "dof_focal_length", PROP_FLOAT, PROP_DISTANCE_CAMERA);
+	RNA_def_property_ui_text(prop, "Focal Length", "Foca Length for dof effect");
+	RNA_def_property_range(prop, 1.0f, FLT_MAX);
+	RNA_def_property_ui_range(prop, 1.0f, 5000.0f, 1, 2);
+	RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, NULL);
+
 	prop = RNA_def_property(srna, "dof_fstop", PROP_FLOAT, PROP_NONE);
-	RNA_def_property_ui_text(prop, "FStop", "FStop for dof effect");
-	RNA_def_property_range(prop, 0.0f, 250.0f);
+	RNA_def_property_ui_text(prop, "F/Stop", "FStop for dof effect");
+	RNA_def_property_range(prop, 0.0f, FLT_MAX);
+	RNA_def_property_ui_range(prop, 0.1f, 64.0f, 10, 1);
 	RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, NULL);
 
 	prop = RNA_def_property(srna, "ssao_darkening", PROP_FLOAT, PROP_NONE);




More information about the Bf-blender-cvs mailing list