[Bf-blender-cvs] [33e7e1d] viewport_experiments: SSAO works again. Unfortunately it's unavoidable to have it work without explicit attenuation control (by using just max distance).

Antony Riakiotakis noreply at git.blender.org
Wed Oct 22 16:38:20 CEST 2014


Commit: 33e7e1ddb2e3c162cfe8afdfd8fd5ade2d65d624
Author: Antony Riakiotakis
Date:   Wed Oct 22 11:29:11 2014 +0200
Branches: viewport_experiments
https://developer.blender.org/rB33e7e1ddb2e3c162cfe8afdfd8fd5ade2d65d624

SSAO works again. Unfortunately it's unavoidable to have it work without
explicit attenuation control (by using just max distance).

Also it now supports orthographic cameras properly. The maximum distance is
now defined in world space units so it should be slightly easier to understand
and uniform across perspective and orthographic cameras.

The optimizations can be generalized for orthographic cameras but the code is
more complex and the runtime advantage is questionable in that case
(in the end a matrix transform is 4 dot products which are not that terrible)
so commented out the code there for now.

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

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_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 2c9533a..32d8709 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -2902,6 +2902,7 @@ class VIEW3D_PT_view3d_shading(Panel):
                 subcol = col.column(align=True)
                 subcol.prop(view, "ssao_darkening")
                 subcol.prop(view, "ssao_distance_max")
+                subcol.prop(view, "ssao_attenuation")
                 subcol.prop(view, "ssao_color")
 
 
diff --git a/source/blender/gpu/intern/gpu_compositing.c b/source/blender/gpu/intern/gpu_compositing.c
index 1cc3c5d..e475713 100644
--- a/source/blender/gpu/intern/gpu_compositing.c
+++ b/source/blender/gpu/intern/gpu_compositing.c
@@ -199,7 +199,7 @@ bool GPU_fx_do_composite_pass(GPUFX *fx, struct View3D *v3d, struct RegionView3D
 		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};
-		float ssao_params[4] = {v3d->ssao_distance_max, v3d->ssao_darkening, 0.0f, 0.0f};
+		float ssao_params[4] = {v3d->ssao_distance_max, v3d->ssao_darkening, v3d->ssao_attenuation, 0.0f};
 		float screen_dim[2] = {fx->gbuffer_dim[0], fx->gbuffer_dim[1]};
 		
 		float invproj[4][4];
diff --git a/source/blender/gpu/shaders/gpu_shader_fx_frag.glsl b/source/blender/gpu/shaders/gpu_shader_fx_frag.glsl
index c8d59fb..31b7361 100644
--- a/source/blender/gpu/shaders/gpu_shader_fx_frag.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_fx_frag.glsl
@@ -35,16 +35,20 @@ vec3 calculate_view_space_normal(in vec3 viewposition)
 /* projective matrix version */
 vec3 get_view_space_from_depth(in vec2 uvcoords, float depth)
 {
-    /* convert depth to non-normalized range */
+    /* simple depth reconstruction, see http://www.derschmale.com/2014/01/26/reconstructing-positions-from-the-depth-buffer
+     * we change the factors from the article to fit the OpennGL model.
     float d = 2.0 * depth - 1.0;
 
-    /* simple depth reconstruction, see http://www.derschmale.com/2014/01/26/reconstructing-positions-from-the-depth-buffer
-     * we change the factors from the article to fit the OpennGL model. */
     float zview = -gl_ProjectionMatrix[2][3] / (d + gl_ProjectionMatrix[2][2]);
 
     vec3 pos = vec3(zview * (ssao_viewvecs[0].xy + uvcoords * ssao_viewvecs[1].xy), zview);
+    */
+    vec4 pos = 2.0 * vec4(uvcoords.xy, depth, 1.0) - vec4(1.0);
+
+    pos = gl_ProjectionMatrixInverse * pos;
+    pos /= pos.w;
 
-    return pos;
+    return pos.xyz;
 }
 
 float calculate_dof_coc(in vec4 viewposition, inout vec3 normal)
@@ -58,6 +62,7 @@ float calculate_dof_coc(in vec4 viewposition, inout vec3 normal)
 }
 
 
+
 float calculate_ssao_factor(float depth)
 {
     /* occlusion is zero in full depth */
@@ -73,25 +78,29 @@ float calculate_ssao_factor(float depth)
     int x, y;
     
     /* convert from -1.0...1.0 range to 0.0..1.0 for easy use with texture coordinates */
-    offset = (offset / offset.w) * 0.5 + vec4(0.5);
+    offset = (offset / offset.w) * 0.5;
 
     for (x = 0; x < NUM_SAMPLES; x++) {
         for (y = 0; y < NUM_SAMPLES; y++) {
-            vec2 uvcoords = uvcoordsvar.xy + (vec2(x,y) - vec2(4.0)) * offset.xy;
+            vec2 uvcoords = uvcoordsvar.xy + (vec2(x,y) - vec2(3.5)) * offset.xy;
 	
-            float depth = texture2D(depthbuffer, uvcoords).r;
-            if (depth != 1.0) {
-                vec3 pos_new = get_view_space_from_depth(uvcoords, depth);
+            float depth_new = texture2D(depthbuffer, uvcoords).r;
+            if (depth_new != 1.0) {
+                vec3 pos_new = get_view_space_from_depth(uvcoords, depth_new);
                 vec3 dir = pos_new - position;
                 float len = length(dir);
-                factor += max(dot(dir, normal) * max(1.0 - len / ssao_params.x, 0.0), 0.0);
+                float f = dot(dir, normal);
+
+                /* use minor bias here to avoid self shadowing */
+                if (f > 0.15 * len)
+                    factor += f / len * 1.0/(1.0 + len * len * ssao_params.z);
             }
         }
     }
 
-    factor /= float(NUM_SAMPLES * NUM_SAMPLES);    
+    factor /= float(NUM_SAMPLES * NUM_SAMPLES);
     
-    return max(0.0, factor * ssao_params.y);
+    return clamp(factor * ssao_params.y, 0.0, 1.0);
 }
 
 void main()
@@ -108,6 +117,6 @@ void main()
 //   vec3 normal = calculate_view_space_normal(position);
 
     vec4 color = mix(texture2D(colorbuffer, uvcoordsvar.xy), ssao_color, calculate_ssao_factor(depth));
-    gl_FragColor = vec4(color.xyz, 1.0);
+    gl_FragColor = vec4(color.rgb, 1.0);
 //    gl_FragColor = vec4(normal * 0.5 + vec3(0.5), 1.0);
 }
diff --git a/source/blender/makesdna/DNA_view3d_types.h b/source/blender/makesdna/DNA_view3d_types.h
index ee72aa9..ad26169 100644
--- a/source/blender/makesdna/DNA_view3d_types.h
+++ b/source/blender/makesdna/DNA_view3d_types.h
@@ -226,7 +226,8 @@ typedef struct View3D {
 	float ssao_darkening;
 	float ssao_color[3];
 	float ssao_distance_max;
-	float pad4[2];
+	float ssao_attenuation;
+	float 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 10d0528..80948d9 100644
--- a/source/blender/makesrna/intern/rna_space.c
+++ b/source/blender/makesrna/intern/rna_space.c
@@ -2204,8 +2204,15 @@ static void rna_def_space_view3d(BlenderRNA *brna)
 	prop = RNA_def_property(srna, "ssao_distance_max", PROP_FLOAT, PROP_NONE);
 	RNA_def_property_ui_text(prop, "Distance", "Distance of object that contribute to the SSAO effect");
 	RNA_def_property_range(prop, 0.0f, 100000.0f);
+	RNA_def_property_ui_range(prop, 0.01f, 100.0f, 1, 3);
 	RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, NULL);
-	
+
+	prop = RNA_def_property(srna, "ssao_attenuation", PROP_FLOAT, PROP_NONE);
+	RNA_def_property_ui_text(prop, "Attenuation", "Attenuation constant");
+	RNA_def_property_range(prop, 1.0f, 100000.0f);
+	RNA_def_property_ui_range(prop, 1.0f, 100.0f, 1, 3);
+	RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, NULL);
+
 	prop = RNA_def_property(srna, "ssao", PROP_BOOLEAN, PROP_NONE);
 	RNA_def_property_boolean_sdna(prop, NULL, "shader_fx", V3D_FX_SSAO);
 	RNA_def_property_ui_text(prop, "SSAO", "Use screen space ambient occlusion of field on viewport");




More information about the Bf-blender-cvs mailing list