Rename any instance of scene layer or render layer in code with view layer
[blender.git] / source / blender / draw / engines / eevee / eevee_depth_of_field.c
1 /*
2  * Copyright 2016, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Blender Institute
19  *
20  */
21
22 /** \file eevee_depth_of_field.c
23  *  \ingroup draw_engine
24  *
25  * Depth of field post process effect.
26  */
27
28 #include "DRW_render.h"
29
30 #include "BLI_dynstr.h"
31 #include "BLI_rand.h"
32
33 #include "DNA_anim_types.h"
34 #include "DNA_camera_types.h"
35 #include "DNA_object_force.h"
36 #include "DNA_screen_types.h"
37 #include "DNA_view3d_types.h"
38 #include "DNA_world_types.h"
39
40 #include "BKE_global.h" /* for G.debug_value */
41 #include "BKE_camera.h"
42 #include "BKE_mesh.h"
43 #include "BKE_object.h"
44 #include "BKE_animsys.h"
45 #include "BKE_screen.h"
46
47 #include "DEG_depsgraph.h"
48
49 #include "eevee_private.h"
50 #include "GPU_extensions.h"
51 #include "GPU_framebuffer.h"
52 #include "GPU_texture.h"
53
54 #include "ED_screen.h"
55
56 static struct {
57         /* Depth Of Field */
58         struct GPUShader *dof_downsample_sh;
59         struct GPUShader *dof_scatter_sh;
60         struct GPUShader *dof_resolve_sh;
61 } e_data = {NULL}; /* Engine data */
62
63 extern char datatoc_effect_dof_vert_glsl[];
64 extern char datatoc_effect_dof_geom_glsl[];
65 extern char datatoc_effect_dof_frag_glsl[];
66
67 static void eevee_create_shader_depth_of_field(void)
68 {
69         e_data.dof_downsample_sh = DRW_shader_create(datatoc_effect_dof_vert_glsl, NULL,
70                                                      datatoc_effect_dof_frag_glsl, "#define STEP_DOWNSAMPLE\n");
71         e_data.dof_scatter_sh = DRW_shader_create(datatoc_effect_dof_vert_glsl, NULL,
72                                                   datatoc_effect_dof_frag_glsl, "#define STEP_SCATTER\n");
73         e_data.dof_resolve_sh = DRW_shader_create(datatoc_effect_dof_vert_glsl, NULL,
74                                                   datatoc_effect_dof_frag_glsl, "#define STEP_RESOLVE\n");
75 }
76
77 int EEVEE_depth_of_field_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
78 {
79         EEVEE_StorageList *stl = vedata->stl;
80         EEVEE_FramebufferList *fbl = vedata->fbl;
81         EEVEE_TextureList *txl = vedata->txl;
82         EEVEE_EffectsInfo *effects = stl->effects;
83
84         const DRWContextState *draw_ctx = DRW_context_state_get();
85         ViewLayer *view_layer = draw_ctx->view_layer;
86         IDProperty *props = BKE_view_layer_engine_evaluated_get(view_layer, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_EEVEE);
87
88         if (BKE_collection_engine_property_value_get_bool(props, "dof_enable")) {
89                 Scene *scene = draw_ctx->scene;
90                 View3D *v3d = draw_ctx->v3d;
91                 RegionView3D *rv3d = draw_ctx->rv3d;
92
93                 if (!e_data.dof_downsample_sh) {
94                         eevee_create_shader_depth_of_field();
95                 }
96
97                 if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
98                         const float *viewport_size = DRW_viewport_size_get();
99                         Camera *cam = (Camera *)v3d->camera->data;
100
101                         /* Retreive Near and Far distance */
102                         effects->dof_near_far[0] = -cam->clipsta;
103                         effects->dof_near_far[1] = -cam->clipend;
104
105                         int buffer_size[2] = {(int)viewport_size[0] / 2, (int)viewport_size[1] / 2};
106
107                         /* Reuse buffer from Bloom if available */
108                         /* WATCH IT : must have the same size */
109                         struct GPUTexture **dof_down_near;
110
111                         if ((effects->enabled_effects & EFFECT_BLOOM) != 0) {
112                                 dof_down_near = &txl->bloom_downsample[0];
113                         }
114                         else {
115                                 dof_down_near = &txl->dof_down_near;
116                         }
117
118                         /* Setup buffers */
119                         DRWFboTexture tex_down[3] = {
120                                 {dof_down_near, DRW_TEX_RGB_11_11_10, DRW_TEX_FILTER}, /* filter to not interfeer with bloom */
121                                 {&txl->dof_down_far, DRW_TEX_RGB_11_11_10, 0},
122                                 {&txl->dof_coc, DRW_TEX_RG_16, 0},
123                         };
124                         DRW_framebuffer_init(
125                                 &fbl->dof_down_fb, &draw_engine_eevee_type,
126                                 buffer_size[0], buffer_size[1], tex_down, 3);
127
128                         DRWFboTexture tex_scatter_far = {&txl->dof_far_blur, DRW_TEX_RGBA_16, DRW_TEX_FILTER};
129                         DRW_framebuffer_init(
130                                 &fbl->dof_scatter_far_fb, &draw_engine_eevee_type,
131                                 buffer_size[0], buffer_size[1], &tex_scatter_far, 1);
132
133                         DRWFboTexture tex_scatter_near = {&txl->dof_near_blur, DRW_TEX_RGBA_16, DRW_TEX_FILTER};
134                         DRW_framebuffer_init(
135                                 &fbl->dof_scatter_near_fb, &draw_engine_eevee_type,
136                                 buffer_size[0], buffer_size[1], &tex_scatter_near, 1);
137
138                         /* Parameters */
139                         /* TODO UI Options */
140                         float fstop = cam->gpu_dof.fstop;
141                         float blades = cam->gpu_dof.num_blades;
142                         float rotation = cam->gpu_dof.rotation;
143                         float ratio = 1.0f / cam->gpu_dof.ratio;
144                         float sensor = BKE_camera_sensor_size(cam->sensor_fit, cam->sensor_x, cam->sensor_y);
145                         float focus_dist = BKE_camera_object_dof_distance(v3d->camera);
146                         float focal_len = cam->lens;
147
148                         UNUSED_VARS(rotation, ratio);
149
150                         /* this is factor that converts to the scene scale. focal length and sensor are expressed in mm
151                          * unit.scale_length is how many meters per blender unit we have. We want to convert to blender units though
152                          * because the shader reads coordinates in world space, which is in blender units.
153                          * Note however that focus_distance is already in blender units and shall not be scaled here (see T48157). */
154                         float scale = (scene->unit.system) ? scene->unit.scale_length : 1.0f;
155                         float scale_camera = 0.001f / scale;
156                         /* we want radius here for the aperture number  */
157                         float aperture = 0.5f * scale_camera * focal_len / fstop;
158                         float focal_len_scaled = scale_camera * focal_len;
159                         float sensor_scaled = scale_camera * sensor;
160
161                         effects->dof_params[0] = aperture * fabsf(focal_len_scaled / (focus_dist - focal_len_scaled));
162                         effects->dof_params[1] = -focus_dist;
163                         effects->dof_params[2] = viewport_size[0] / (rv3d->viewcamtexcofac[0] * sensor_scaled);
164                         effects->dof_bokeh[0] = blades;
165                         effects->dof_bokeh[1] = rotation;
166                         effects->dof_bokeh[2] = ratio;
167                         effects->dof_bokeh[3] = BKE_collection_engine_property_value_get_float(props, "bokeh_max_size");
168
169                         return EFFECT_DOF | EFFECT_POST_BUFFER;
170                 }
171         }
172
173         /* Cleanup to release memory */
174         DRW_TEXTURE_FREE_SAFE(txl->dof_down_near);
175         DRW_TEXTURE_FREE_SAFE(txl->dof_down_far);
176         DRW_TEXTURE_FREE_SAFE(txl->dof_coc);
177         DRW_TEXTURE_FREE_SAFE(txl->dof_far_blur);
178         DRW_TEXTURE_FREE_SAFE(txl->dof_near_blur);
179         DRW_FRAMEBUFFER_FREE_SAFE(fbl->dof_down_fb);
180         DRW_FRAMEBUFFER_FREE_SAFE(fbl->dof_scatter_far_fb);
181         DRW_FRAMEBUFFER_FREE_SAFE(fbl->dof_scatter_near_fb);
182
183         return 0;
184 }
185
186 void EEVEE_depth_of_field_cache_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
187 {
188         EEVEE_PassList *psl = vedata->psl;
189         EEVEE_StorageList *stl = vedata->stl;
190         EEVEE_TextureList *txl = vedata->txl;
191         EEVEE_EffectsInfo *effects = stl->effects;
192         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
193
194         if ((effects->enabled_effects & EFFECT_DOF) != 0) {
195                 /**  Depth of Field algorithm
196                  *
197                  * Overview :
198                  * - Downsample the color buffer into 2 buffers weighted with
199                  *   CoC values. Also output CoC into a texture.
200                  * - Shoot quads for every pixel and expand it depending on the CoC.
201                  *   Do one pass for near Dof and one pass for far Dof.
202                  * - Finally composite the 2 blurred buffers with the original render.
203                  **/
204                 DRWShadingGroup *grp;
205                 struct Gwn_Batch *quad = DRW_cache_fullscreen_quad_get();
206
207                 psl->dof_down = DRW_pass_create("DoF Downsample", DRW_STATE_WRITE_COLOR);
208
209                 grp = DRW_shgroup_create(e_data.dof_downsample_sh, psl->dof_down);
210                 DRW_shgroup_uniform_buffer(grp, "colorBuffer", &effects->source_buffer);
211                 DRW_shgroup_uniform_buffer(grp, "depthBuffer", &dtxl->depth);
212                 DRW_shgroup_uniform_vec2(grp, "nearFar", effects->dof_near_far, 1);
213                 DRW_shgroup_uniform_vec3(grp, "dofParams", effects->dof_params, 1);
214                 DRW_shgroup_call_add(grp, quad, NULL);
215
216                 psl->dof_scatter = DRW_pass_create("DoF Scatter", DRW_STATE_WRITE_COLOR | DRW_STATE_ADDITIVE);
217
218                 /* This create an empty batch of N triangles to be positioned
219                  * by the vertex shader 0.4ms against 6ms with instancing */
220                 const float *viewport_size = DRW_viewport_size_get();
221                 const int sprite_ct = ((int)viewport_size[0] / 2) * ((int)viewport_size[1] / 2); /* brackets matters */
222                 grp = DRW_shgroup_empty_tri_batch_create(e_data.dof_scatter_sh, psl->dof_scatter, sprite_ct);
223
224                 DRW_shgroup_uniform_buffer(grp, "colorBuffer", &effects->unf_source_buffer);
225                 DRW_shgroup_uniform_buffer(grp, "cocBuffer", &txl->dof_coc);
226                 DRW_shgroup_uniform_vec2(grp, "layerSelection", effects->dof_layer_select, 1);
227                 DRW_shgroup_uniform_vec4(grp, "bokehParams", effects->dof_bokeh, 1);
228
229                 psl->dof_resolve = DRW_pass_create("DoF Resolve", DRW_STATE_WRITE_COLOR);
230
231                 grp = DRW_shgroup_create(e_data.dof_resolve_sh, psl->dof_resolve);
232                 DRW_shgroup_uniform_buffer(grp, "colorBuffer", &effects->source_buffer);
233                 DRW_shgroup_uniform_buffer(grp, "nearBuffer", &txl->dof_near_blur);
234                 DRW_shgroup_uniform_buffer(grp, "farBuffer", &txl->dof_far_blur);
235                 DRW_shgroup_uniform_buffer(grp, "depthBuffer", &dtxl->depth);
236                 DRW_shgroup_uniform_vec2(grp, "nearFar", effects->dof_near_far, 1);
237                 DRW_shgroup_uniform_vec3(grp, "dofParams", effects->dof_params, 1);
238                 DRW_shgroup_call_add(grp, quad, NULL);
239         }
240 }
241
242 void EEVEE_depth_of_field_draw(EEVEE_Data *vedata)
243 {
244         EEVEE_PassList *psl = vedata->psl;
245         EEVEE_TextureList *txl = vedata->txl;
246         EEVEE_FramebufferList *fbl = vedata->fbl;
247         EEVEE_StorageList *stl = vedata->stl;
248         EEVEE_EffectsInfo *effects = stl->effects;
249
250         /* Depth Of Field */
251         if ((effects->enabled_effects & EFFECT_DOF) != 0) {
252                 float clear_col[4] = {0.0f, 0.0f, 0.0f, 0.0f};
253
254                 /* Downsample */
255                 DRW_framebuffer_bind(fbl->dof_down_fb);
256                 DRW_draw_pass(psl->dof_down);
257
258                 /* Scatter Far */
259                 effects->unf_source_buffer = txl->dof_down_far;
260                 copy_v2_fl2(effects->dof_layer_select, 0.0f, 1.0f);
261                 DRW_framebuffer_bind(fbl->dof_scatter_far_fb);
262                 DRW_framebuffer_clear(true, false, false, clear_col, 0.0f);
263                 DRW_draw_pass(psl->dof_scatter);
264
265                 /* Scatter Near */
266                 if ((effects->enabled_effects & EFFECT_BLOOM) != 0) {
267                         /* Reuse bloom half res buffer */
268                         effects->unf_source_buffer = txl->bloom_downsample[0];
269                 }
270                 else {
271                         effects->unf_source_buffer = txl->dof_down_near;
272                 }
273                 copy_v2_fl2(effects->dof_layer_select, 1.0f, 0.0f);
274                 DRW_framebuffer_bind(fbl->dof_scatter_near_fb);
275                 DRW_framebuffer_clear(true, false, false, clear_col, 0.0f);
276                 DRW_draw_pass(psl->dof_scatter);
277
278                 /* Resolve */
279                 DRW_framebuffer_bind(effects->target_buffer);
280                 DRW_draw_pass(psl->dof_resolve);
281                 SWAP_BUFFERS();
282         }
283 }
284
285 void EEVEE_depth_of_field_free(void)
286 {
287         DRW_SHADER_FREE_SAFE(e_data.dof_downsample_sh);
288         DRW_SHADER_FREE_SAFE(e_data.dof_scatter_sh);
289         DRW_SHADER_FREE_SAFE(e_data.dof_resolve_sh);
290 }