Commit: 8f25b6d6c846100f97da5eb1bf131500992c5905
Author: Antony Riakiotakis
Date:   Tue Oct 28 18:28:58 2014 +0100
Branches: viewport_experiments
https://developer.blender.org/rB8f25b6d6c846100f97da5eb1bf131500992c5905

More attempts to synchronize viewport and camera dof - probably will
need fixes in the rendering systems. Reserve textures for the dof
effect.

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

M       release/scripts/startup/bl_ui/space_view3d.py
M       source/blender/editors/space_view3d/view3d_draw.c
M       source/blender/gpu/GPU_compositing.h
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 683e8f8..460627e 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -2900,6 +2900,7 @@ class VIEW3D_PT_view3d_shading(Panel):
                 subcol.prop(view, "dof_aperture")
                 subcol.prop(view, "dof_fstop")
                 subcol.prop(view, "dof_focal_length")
+                subcol.prop(view, "dof_sensor")
             col.prop(view, "ssao")
             if view.ssao:
                 subcol = col.column(align=True)
diff --git a/source/blender/editors/space_view3d/view3d_draw.c 
b/source/blender/editors/space_view3d/view3d_draw.c
index bc2cf04..d549c84 100644
--- a/source/blender/editors/space_view3d/view3d_draw.c
+++ b/source/blender/editors/space_view3d/view3d_draw.c
@@ -3449,7 +3449,7 @@ static void view3d_main_area_draw_objects(const bContext 
*C, Scene *scene, View3
 
        /* post process */
        if (do_compositing) {
-               GPU_fx_do_composite_pass(rv3d->compositor, v3d, rv3d);
+               GPU_fx_do_composite_pass(rv3d->compositor, v3d, rv3d, scene);
        }
        
        /* Disable back anti-aliasing */
diff --git a/source/blender/gpu/GPU_compositing.h 
b/source/blender/gpu/GPU_compositing.h
index a669508..a745801 100644
--- a/source/blender/gpu/GPU_compositing.h
+++ b/source/blender/gpu/GPU_compositing.h
@@ -38,6 +38,7 @@ typedef struct GPUFX GPUFX;
 struct RegionView3D;
 struct View3D;
 struct rcti;
+struct Scene;
 
 /**** Public API *****/
 
@@ -59,6 +60,6 @@ void GPU_destroy_fx_compositor(GPUFX *fx);
 bool GPU_initialize_fx_passes(GPUFX *fx, struct rcti *rect, rcti 
*scissor_rect, int fxflags);
 
 /* do compositing on the fx passes that have been initialized */
-bool GPU_fx_do_composite_pass(GPUFX *fx, struct View3D *v3d, struct 
RegionView3D *rv3d);
+bool GPU_fx_do_composite_pass(GPUFX *fx, struct View3D *v3d, struct 
RegionView3D *rv3d, struct Scene *scene);
 
 #endif // __GPU_COMPOSITING_H__
diff --git a/source/blender/gpu/intern/gpu_compositing.c 
b/source/blender/gpu/intern/gpu_compositing.c
index bc9916d..2bd45f9 100644
--- a/source/blender/gpu/intern/gpu_compositing.c
+++ b/source/blender/gpu/intern/gpu_compositing.c
@@ -38,6 +38,9 @@
 
 #include "DNA_vec_types.h"
 #include "DNA_view3d_types.h"
+#include "DNA_scene_types.h"
+#include "DNA_object_types.h"
+#include "DNA_camera_types.h"
 
 #include "GPU_extensions.h"
 #include "GPU_compositing.h"
@@ -60,8 +63,17 @@ 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;
+       /* all those buffers below have to coexist. Fortunately they are all 
quarter sized (1/16th of memory) of original framebuffer */
+
+       float dof_near_w;
+       float dof_near_h;
+
+       /* texture used for near coc and color blurring calculation */
+       GPUTexture *dof_near_coc_buffer;
+       /* blurred near coc buffer. */
+       GPUTexture *dof_near_coc_blurred_buffer;
+       /* final near coc buffer. */
+       GPUTexture *dof_near_coc_final_buffer;
 
        /* texture bound to the depth attachment of the gbuffer */
        GPUTexture *depth_buffer;
@@ -99,6 +111,19 @@ static void cleanup_fx_gl_data(GPUFX *fx, bool do_fbo)
                fx->depth_buffer = NULL;
        }               
 
+       if (fx->dof_near_coc_blurred_buffer) {
+               GPU_texture_free(fx->dof_near_coc_blurred_buffer);
+               fx->dof_near_coc_blurred_buffer = NULL;
+       }
+       if (fx->dof_near_coc_buffer) {
+               GPU_texture_free(fx->dof_near_coc_buffer);
+               fx->dof_near_coc_buffer = NULL;
+       }
+       if (fx->dof_near_coc_final_buffer) {
+               GPU_texture_free(fx->dof_near_coc_final_buffer);
+               fx->dof_near_coc_final_buffer = NULL;
+       }
+
        if (fx->jitter_buffer && do_fbo) {
                GPU_texture_free(fx->jitter_buffer);
                fx->jitter_buffer = NULL;
@@ -194,6 +219,22 @@ bool GPU_initialize_fx_passes(GPUFX *fx, rcti *rect, rcti 
*scissor_rect, int fxf
                if (!(fx->depth_buffer = GPU_texture_create_depth(w, h, 
err_out))) {
                        printf("%.256s\n", err_out);
                }
+
+               /* create textures for dof effect */
+               if (fxflags & V3D_FX_DEPTH_OF_FIELD) {
+                       fx->dof_near_w = w / 4;
+                       fx->dof_near_h = h / 4;
+
+                       if (!(fx->dof_near_coc_buffer = 
GPU_texture_create_2D(fx->dof_near_w, fx->dof_near_h, NULL, err_out))) {
+                               printf("%.256s\n", err_out);
+                       }
+                       if (!(fx->dof_near_coc_blurred_buffer = 
GPU_texture_create_2D(fx->dof_near_w, fx->dof_near_h, NULL, err_out))) {
+                               printf("%.256s\n", err_out);
+                       }
+                       if (!(fx->dof_near_coc_final_buffer = 
GPU_texture_create_2D(fx->dof_near_w, fx->dof_near_h, NULL, err_out))) {
+                               printf("%.256s\n", err_out);
+                       }
+               }
                
                /* in case of failure, cleanup */
                if (!fx->color_buffer || !fx->depth_buffer) {
@@ -227,12 +268,12 @@ bool GPU_initialize_fx_passes(GPUFX *fx, rcti *rect, rcti 
*scissor_rect, int fxf
                                  w_sc, h_sc);
        }
        fx->effects = fxflags;
-       
+
        return true;
 }
 
 
-bool GPU_fx_do_composite_pass(GPUFX *fx, struct View3D *v3d, struct 
RegionView3D *rv3d) {
+bool GPU_fx_do_composite_pass(GPUFX *fx, struct View3D *v3d, struct 
RegionView3D *rv3d, struct Scene *scene) {
        GPUShader *fx_shader;
        int numslots = 0;
 
@@ -368,11 +409,18 @@ bool GPU_fx_do_composite_pass(GPUFX *fx, struct View3D 
*v3d, struct RegionView3D
                fx_shader = 
GPU_shader_get_builtin_fx_shader(GPU_SHADER_FX_DEPTH_OF_FIELD);
                if (fx_shader) {
                        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};
+                       float dof_params[4];
                        int screendim_uniform, color_uniform, depth_uniform, 
dof_uniform, blurred_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;
+
+                       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(fx_shader, 
"dof_params");
                        blurred_uniform = GPU_shader_get_uniform(fx_shader, 
"blurredcolorbuffer");
                        screendim_uniform = GPU_shader_get_uniform(fx_shader, 
"screendim");
@@ -381,7 +429,7 @@ bool GPU_fx_do_composite_pass(GPUFX *fx, struct View3D 
*v3d, struct RegionView3D
 
                        GPU_shader_bind(fx_shader);
 
-                       GPU_shader_uniform_vector(fx_shader, dof_uniform, 2, 1, 
dof_params);
+                       GPU_shader_uniform_vector(fx_shader, dof_uniform, 4, 1, 
dof_params);
                        GPU_shader_uniform_vector(fx_shader, screendim_uniform, 
2, 1, screen_dim);
 
                        GPU_texture_bind(fx->color_buffer, 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 a46a6bd..a5a6bfc 100644
--- a/source/blender/gpu/shaders/gpu_shader_fx_dof_frag.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_fx_dof_frag.glsl
@@ -11,7 +11,7 @@ uniform sampler2D depthbuffer;
 varying vec4 uvcoordsvar;
 
 // this includes focal distance in x and aperture size in y
-uniform vec2 dof_params;
+uniform vec4 dof_params;
 
 /* projective matrix version */
 vec3 get_view_space_from_depth(in vec2 uvcoords, float depth)
@@ -34,12 +34,11 @@ vec3 get_view_space_from_depth(in vec2 uvcoords, float 
depth)
 
 float calculate_dof_coc(in vec3 viewposition)
 {
-    float dist = 0.1 * length(viewposition);
+    float dist = length(viewposition);
     float coc = dof_params.x * abs(1.0 - dof_params.y / dist);
     
-    coc = clamp(coc, 0.0, 1.0);
-    
-    return coc;
+    /* multiply by 1.0 / sensor size to get the normalized size */
+    return coc * dof_params.z;
 }
 
 void main()
diff --git a/source/blender/makesdna/DNA_view3d_types.h 
b/source/blender/makesdna/DNA_view3d_types.h
index 6bc7a8f..4cd3f11 100644
--- a/source/blender/makesdna/DNA_view3d_types.h
+++ b/source/blender/makesdna/DNA_view3d_types.h
@@ -224,12 +224,12 @@ typedef struct View3D {
        float dof_aperture;           /* aperture for dof lens (could use fstop 
as well) */
        float dof_fstop;
        float dof_focal_length;
+       float dof_sensor;
        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 552489a..bb851db 100644
--- a/source/blender/makesrna/intern/rna_space.c
+++ b/source/blender/makesrna/intern/rna_space.c
@@ -2205,6 +2205,12 @@ static void rna_def_space_view3d(BlenderRNA *brna)
        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_sensor", PROP_FLOAT, 
PROP_DISTANCE_CAMERA);
+       RNA_def_property_ui_text(prop, "Sensor", "Size of sensor");
+       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, "F/Stop", "FStop for dof effect");
        RNA_def_property_range(prop, 0.0f, FLT_MAX);

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to