doxygen: add newline after \file
[blender.git] / source / blender / draw / engines / eevee / eevee_depth_of_field.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2016, Blender Foundation.
17  */
18
19 /** \file
20  * \ingroup draw_engine
21  *
22  * Depth of field post process effect.
23  */
24
25 #include "DRW_render.h"
26
27 #include "DNA_camera_types.h"
28 #include "DNA_screen_types.h"
29 #include "DNA_view3d_types.h"
30 #include "DNA_world_types.h"
31
32 #include "BKE_camera.h"
33
34 #include "DEG_depsgraph.h"
35 #include "DEG_depsgraph_query.h"
36
37 #include "eevee_private.h"
38 #include "GPU_framebuffer.h"
39 #include "GPU_texture.h"
40
41 static struct {
42         /* Depth Of Field */
43         struct GPUShader *dof_downsample_sh[2];
44         struct GPUShader *dof_scatter_sh[2];
45         struct GPUShader *dof_resolve_sh[2];
46 } e_data = {{NULL}}; /* Engine data */
47
48 extern char datatoc_effect_dof_vert_glsl[];
49 extern char datatoc_effect_dof_frag_glsl[];
50
51 static void eevee_create_shader_depth_of_field(const bool use_alpha)
52 {
53         e_data.dof_downsample_sh[use_alpha] = DRW_shader_create_fullscreen(
54                 datatoc_effect_dof_frag_glsl, use_alpha ?
55                                               "#define USE_ALPHA_DOF\n"
56                                               "#define STEP_DOWNSAMPLE\n" :
57                                               "#define STEP_DOWNSAMPLE\n");
58         e_data.dof_scatter_sh[use_alpha] = DRW_shader_create(
59                 datatoc_effect_dof_vert_glsl, NULL,
60                 datatoc_effect_dof_frag_glsl, use_alpha ?
61                                               "#define USE_ALPHA_DOF\n"
62                                               "#define STEP_SCATTER\n" :
63                                               "#define STEP_SCATTER\n");
64         e_data.dof_resolve_sh[use_alpha] = DRW_shader_create_fullscreen(
65                 datatoc_effect_dof_frag_glsl, use_alpha ?
66                                               "#define USE_ALPHA_DOF\n"
67                                               "#define STEP_RESOLVE\n" :
68                                               "#define STEP_RESOLVE\n");
69 }
70
71 int EEVEE_depth_of_field_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata, Object *camera)
72 {
73         EEVEE_StorageList *stl = vedata->stl;
74         EEVEE_FramebufferList *fbl = vedata->fbl;
75         EEVEE_EffectsInfo *effects = stl->effects;
76
77         const DRWContextState *draw_ctx = DRW_context_state_get();
78         const Scene *scene_eval = DEG_get_evaluated_scene(draw_ctx->depsgraph);
79
80         if (scene_eval->eevee.flag & SCE_EEVEE_DOF_ENABLED) {
81                 RegionView3D *rv3d = draw_ctx->rv3d;
82                 const bool use_alpha = !DRW_state_draw_background();
83
84                 if (!e_data.dof_downsample_sh[use_alpha]) {
85                         eevee_create_shader_depth_of_field(use_alpha);
86                 }
87
88                 if (camera) {
89                         const float *viewport_size = DRW_viewport_size_get();
90                         Camera *cam = (Camera *)camera->data;
91
92                         /* Retrieve Near and Far distance */
93                         effects->dof_near_far[0] = -cam->clip_start;
94                         effects->dof_near_far[1] = -cam->clip_end;
95
96                         int buffer_size[2] = {(int)viewport_size[0] / 2, (int)viewport_size[1] / 2};
97
98                         eGPUTextureFormat down_format = DRW_state_draw_background() ? GPU_R11F_G11F_B10F : GPU_RGBA16F;
99
100                         effects->dof_down_near = DRW_texture_pool_query_2D(buffer_size[0], buffer_size[1], down_format,
101                                                                            &draw_engine_eevee_type);
102                         effects->dof_down_far =  DRW_texture_pool_query_2D(buffer_size[0], buffer_size[1], down_format,
103                                                                            &draw_engine_eevee_type);
104                         effects->dof_coc =       DRW_texture_pool_query_2D(buffer_size[0], buffer_size[1], GPU_RG16F,
105                                                                            &draw_engine_eevee_type);
106
107                         GPU_framebuffer_ensure_config(&fbl->dof_down_fb, {
108                                 GPU_ATTACHMENT_NONE,
109                                 GPU_ATTACHMENT_TEXTURE(effects->dof_down_near),
110                                 GPU_ATTACHMENT_TEXTURE(effects->dof_down_far),
111                                 GPU_ATTACHMENT_TEXTURE(effects->dof_coc)
112                         });
113
114                         /* Go full 32bits for rendering and reduce the color artifacts. */
115                         eGPUTextureFormat fb_format = DRW_state_is_image_render() ? GPU_RGBA32F : GPU_RGBA16F;
116
117                         effects->dof_blur = DRW_texture_pool_query_2D(buffer_size[0] * 2, buffer_size[1], fb_format,
118                                                                       &draw_engine_eevee_type);
119
120                         GPU_framebuffer_ensure_config(&fbl->dof_scatter_fb, {
121                                 GPU_ATTACHMENT_NONE,
122                                 GPU_ATTACHMENT_TEXTURE(effects->dof_blur),
123                         });
124
125                         if (!DRW_state_draw_background()) {
126                                 effects->dof_blur_alpha = DRW_texture_pool_query_2D(buffer_size[0] * 2, buffer_size[1], GPU_R32F,
127                                                                                     &draw_engine_eevee_type);
128                                 GPU_framebuffer_texture_attach(fbl->dof_scatter_fb, effects->dof_blur_alpha, 1, 0);
129                         }
130
131                         /* Parameters */
132                         /* TODO UI Options */
133                         float fstop = cam->gpu_dof.fstop;
134                         float blades = cam->gpu_dof.num_blades;
135                         float rotation = cam->gpu_dof.rotation;
136                         float ratio = 1.0f / cam->gpu_dof.ratio;
137                         float sensor = BKE_camera_sensor_size(cam->sensor_fit, cam->sensor_x, cam->sensor_y);
138                         float focus_dist = BKE_camera_object_dof_distance(camera);
139                         float focal_len = cam->lens;
140
141                         /* this is factor that converts to the scene scale. focal length and sensor are expressed in mm
142                          * unit.scale_length is how many meters per blender unit we have. We want to convert to blender units though
143                          * because the shader reads coordinates in world space, which is in blender units.
144                          * Note however that focus_distance is already in blender units and shall not be scaled here (see T48157). */
145                         float scale = (scene_eval->unit.system) ? scene_eval->unit.scale_length : 1.0f;
146                         float scale_camera = 0.001f / scale;
147                         /* we want radius here for the aperture number  */
148                         float aperture = 0.5f * scale_camera * focal_len / fstop;
149                         float focal_len_scaled = scale_camera * focal_len;
150                         float sensor_scaled = scale_camera * sensor;
151
152                         if (rv3d != NULL) {
153                                 sensor_scaled *= rv3d->viewcamtexcofac[0];
154                         }
155
156                         effects->dof_params[0] = aperture * fabsf(focal_len_scaled / (focus_dist - focal_len_scaled));
157                         effects->dof_params[1] = -focus_dist;
158                         effects->dof_params[2] = viewport_size[0] / sensor_scaled;
159                         effects->dof_bokeh[0] = rotation;
160                         effects->dof_bokeh[1] = ratio;
161                         effects->dof_bokeh[2] = scene_eval->eevee.bokeh_max_size;
162
163                         /* Precompute values to save instructions in fragment shader. */
164                         effects->dof_bokeh_sides[0] = blades;
165                         effects->dof_bokeh_sides[1] = blades > 0.0f ? 2.0f * M_PI / blades : 0.0f;
166                         effects->dof_bokeh_sides[2] = blades / (2.0f * M_PI);
167                         effects->dof_bokeh_sides[3] = blades > 0.0f ? cosf(M_PI / blades) : 0.0f;
168
169                         return EFFECT_DOF | EFFECT_POST_BUFFER;
170                 }
171         }
172
173         /* Cleanup to release memory */
174         GPU_FRAMEBUFFER_FREE_SAFE(fbl->dof_down_fb);
175         GPU_FRAMEBUFFER_FREE_SAFE(fbl->dof_scatter_fb);
176
177         return 0;
178 }
179
180 void EEVEE_depth_of_field_cache_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
181 {
182         EEVEE_PassList *psl = vedata->psl;
183         EEVEE_StorageList *stl = vedata->stl;
184         EEVEE_EffectsInfo *effects = stl->effects;
185         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
186
187         if ((effects->enabled_effects & EFFECT_DOF) != 0) {
188                 /**  Depth of Field algorithm
189                  *
190                  * Overview :
191                  * - Downsample the color buffer into 2 buffers weighted with
192                  *   CoC values. Also output CoC into a texture.
193                  * - Shoot quads for every pixel and expand it depending on the CoC.
194                  *   Do one pass for near Dof and one pass for far Dof.
195                  * - Finally composite the 2 blurred buffers with the original render.
196                  **/
197                 DRWShadingGroup *grp;
198                 struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
199                 const bool use_alpha = !DRW_state_draw_background();
200
201                 psl->dof_down = DRW_pass_create("DoF Downsample", DRW_STATE_WRITE_COLOR);
202
203                 grp = DRW_shgroup_create(e_data.dof_downsample_sh[use_alpha], psl->dof_down);
204                 DRW_shgroup_uniform_texture_ref(grp, "colorBuffer", &effects->source_buffer);
205                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
206                 DRW_shgroup_uniform_vec2(grp, "nearFar", effects->dof_near_far, 1);
207                 DRW_shgroup_uniform_vec3(grp, "dofParams", effects->dof_params, 1);
208                 DRW_shgroup_call_add(grp, quad, NULL);
209
210                 psl->dof_scatter = DRW_pass_create("DoF Scatter", DRW_STATE_WRITE_COLOR | DRW_STATE_ADDITIVE_FULL);
211
212                 /* This create an empty batch of N triangles to be positioned
213                  * by the vertex shader 0.4ms against 6ms with instancing */
214                 const float *viewport_size = DRW_viewport_size_get();
215                 const int sprite_len = ((int)viewport_size[0] / 2) * ((int)viewport_size[1] / 2); /* brackets matters */
216                 grp = DRW_shgroup_empty_tri_batch_create(e_data.dof_scatter_sh[use_alpha], psl->dof_scatter, sprite_len);
217                 DRW_shgroup_uniform_texture_ref(grp, "nearBuffer", &effects->dof_down_near);
218                 DRW_shgroup_uniform_texture_ref(grp, "farBuffer", &effects->dof_down_far);
219                 DRW_shgroup_uniform_texture_ref(grp, "cocBuffer", &effects->dof_coc);
220                 DRW_shgroup_uniform_vec4(grp, "bokehParams", effects->dof_bokeh, 2);
221
222                 psl->dof_resolve = DRW_pass_create("DoF Resolve", DRW_STATE_WRITE_COLOR);
223
224                 grp = DRW_shgroup_create(e_data.dof_resolve_sh[use_alpha], psl->dof_resolve);
225                 DRW_shgroup_uniform_texture_ref(grp, "scatterBuffer", &effects->dof_blur);
226                 DRW_shgroup_uniform_texture_ref(grp, "colorBuffer", &effects->source_buffer);
227                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
228                 DRW_shgroup_uniform_vec2(grp, "nearFar", effects->dof_near_far, 1);
229                 DRW_shgroup_uniform_vec3(grp, "dofParams", effects->dof_params, 1);
230                 DRW_shgroup_call_add(grp, quad, NULL);
231
232                 if (use_alpha) {
233                         DRW_shgroup_uniform_texture_ref(grp, "scatterAlphaBuffer", &effects->dof_blur_alpha);
234                 }
235         }
236 }
237
238 void EEVEE_depth_of_field_draw(EEVEE_Data *vedata)
239 {
240         EEVEE_PassList *psl = vedata->psl;
241         EEVEE_TextureList *txl = vedata->txl;
242         EEVEE_FramebufferList *fbl = vedata->fbl;
243         EEVEE_StorageList *stl = vedata->stl;
244         EEVEE_EffectsInfo *effects = stl->effects;
245
246         /* Depth Of Field */
247         if ((effects->enabled_effects & EFFECT_DOF) != 0) {
248                 float clear_col[4] = {0.0f, 0.0f, 0.0f, 0.0f};
249
250                 /* Downsample */
251                 GPU_framebuffer_bind(fbl->dof_down_fb);
252                 DRW_draw_pass(psl->dof_down);
253
254                 /* Scatter */
255                 GPU_framebuffer_bind(fbl->dof_scatter_fb);
256                 GPU_framebuffer_clear_color(fbl->dof_scatter_fb, clear_col);
257                 DRW_draw_pass(psl->dof_scatter);
258
259                 /* Resolve */
260                 GPU_framebuffer_bind(effects->target_buffer);
261                 DRW_draw_pass(psl->dof_resolve);
262                 SWAP_BUFFERS();
263         }
264 }
265
266 void EEVEE_depth_of_field_free(void)
267 {
268         for (int i = 0; i < 2; ++i) {
269                 DRW_SHADER_FREE_SAFE(e_data.dof_downsample_sh[i]);
270                 DRW_SHADER_FREE_SAFE(e_data.dof_scatter_sh[i]);
271                 DRW_SHADER_FREE_SAFE(e_data.dof_resolve_sh[i]);
272         }
273 }