Eevee: DOF: Optimization: Move some operations out of the shader
authorClément Foucault <foucault.clem@gmail.com>
Fri, 15 Mar 2019 21:17:07 +0000 (22:17 +0100)
committerClément Foucault <foucault.clem@gmail.com>
Fri, 15 Mar 2019 21:33:02 +0000 (22:33 +0100)
source/blender/draw/engines/eevee/eevee_depth_of_field.c
source/blender/draw/engines/eevee/eevee_private.h
source/blender/draw/engines/eevee/shaders/effect_dof_frag.glsl

index 597cb83..7f754a5 100644 (file)
@@ -153,9 +153,10 @@ int EEVEE_depth_of_field_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *v
                                sensor_scaled *= rv3d->viewcamtexcofac[0];
                        }
 
-                       effects->dof_params[0] = aperture * fabsf(focal_len_scaled / (focus_dist - focal_len_scaled));
-                       effects->dof_params[1] = -focus_dist;
-                       effects->dof_params[2] = viewport_size[0] / sensor_scaled;
+                       effects->dof_params[1] = aperture * fabsf(focal_len_scaled / (focus_dist - focal_len_scaled));
+                       effects->dof_params[1] *= viewport_size[0] / sensor_scaled;
+                       effects->dof_params[0] = -focus_dist * effects->dof_params[1];
+
                        effects->dof_bokeh[0] = rotation;
                        effects->dof_bokeh[1] = ratio;
                        effects->dof_bokeh[2] = scene_eval->eevee.bokeh_max_size;
@@ -204,7 +205,7 @@ void EEVEE_depth_of_field_cache_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_
                DRW_shgroup_uniform_texture_ref(grp, "colorBuffer", &effects->source_buffer);
                DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
                DRW_shgroup_uniform_vec2(grp, "nearFar", effects->dof_near_far, 1);
-               DRW_shgroup_uniform_vec3(grp, "dofParams", effects->dof_params, 1);
+               DRW_shgroup_uniform_vec2(grp, "dofParams", effects->dof_params, 1);
                DRW_shgroup_call_add(grp, quad, NULL);
 
                psl->dof_scatter = DRW_pass_create("DoF Scatter", DRW_STATE_WRITE_COLOR | DRW_STATE_ADDITIVE_FULL);
@@ -226,7 +227,7 @@ void EEVEE_depth_of_field_cache_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_
                DRW_shgroup_uniform_texture_ref(grp, "colorBuffer", &effects->source_buffer);
                DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
                DRW_shgroup_uniform_vec2(grp, "nearFar", effects->dof_near_far, 1);
-               DRW_shgroup_uniform_vec3(grp, "dofParams", effects->dof_params, 1);
+               DRW_shgroup_uniform_vec2(grp, "dofParams", effects->dof_params, 1);
                DRW_shgroup_call_add(grp, quad, NULL);
 
                if (use_alpha) {
index 7ffe7f4..e0ee689 100644 (file)
@@ -574,7 +574,7 @@ typedef struct EEVEE_EffectsInfo {
        struct GPUTexture *velocity_tx; /* Texture from pool */
        /* Depth Of Field */
        float dof_near_far[2];
-       float dof_params[3];
+       float dof_params[2];
        float dof_bokeh[4];
        float dof_bokeh_sides[4];
        int dof_target_size[2];
index 6aa771b..91a4ca9 100644 (file)
@@ -4,11 +4,10 @@ uniform mat4 ProjectionMatrix;
 uniform sampler2D colorBuffer;
 uniform sampler2D depthBuffer;
 
-uniform vec3 dofParams;
+uniform vec2 dofParams;
 
-#define dof_aperturesize    dofParams.x
-#define dof_distance        dofParams.y
-#define dof_invsensorsize   dofParams.z
+#define dof_mul    dofParams.x /* distance * aperturesize * invsensorsize */
+#define dof_bias   dofParams.y /* aperturesize * invsensorsize */
 
 uniform vec4 bokehParams[2];
 
@@ -25,7 +24,7 @@ uniform vec2 nearFar; /* Near & far view depths values */
 /* -------------- Utils ------------- */
 
 /* divide by sensor size to get the normalized size */
-#define calculate_coc(zdepth) (dof_aperturesize * (dof_distance / zdepth - 1.0) * dof_invsensorsize)
+#define calculate_coc(zdepth) (dof_mul / zdepth - dof_bias)
 
 #define linear_depth(z) ((ProjectionMatrix[3][3] == 0.0) \
                ? (nearFar.x  * nearFar.y) / (z * (nearFar.x - nearFar.y) + nearFar.y) \