doxygen: add newline after \file
[blender.git] / source / blender / draw / engines / eevee / eevee_occlusion.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  * Implementation of the screen space Ground Truth Ambient Occlusion.
23  */
24
25 #include "DRW_render.h"
26
27 #include "BLI_string_utils.h"
28
29
30 #include "DEG_depsgraph_query.h"
31
32 #include "BKE_global.h" /* for G.debug_value */
33
34 #include "eevee_private.h"
35
36 #include "GPU_extensions.h"
37 #include "GPU_state.h"
38
39 static struct {
40         /* Ground Truth Ambient Occlusion */
41         struct GPUShader *gtao_sh;
42         struct GPUShader *gtao_layer_sh;
43         struct GPUShader *gtao_debug_sh;
44         struct GPUTexture *src_depth;
45 } e_data = {NULL}; /* Engine data */
46
47 extern char datatoc_ambient_occlusion_lib_glsl[];
48 extern char datatoc_common_view_lib_glsl[];
49 extern char datatoc_common_uniforms_lib_glsl[];
50 extern char datatoc_bsdf_common_lib_glsl[];
51 extern char datatoc_effect_gtao_frag_glsl[];
52
53 static void eevee_create_shader_occlusion(void)
54 {
55         char *frag_str = BLI_string_joinN(
56                 datatoc_common_view_lib_glsl,
57                 datatoc_common_uniforms_lib_glsl,
58                 datatoc_bsdf_common_lib_glsl,
59                 datatoc_ambient_occlusion_lib_glsl,
60                 datatoc_effect_gtao_frag_glsl);
61
62         e_data.gtao_sh = DRW_shader_create_fullscreen(frag_str, NULL);
63         e_data.gtao_layer_sh = DRW_shader_create_fullscreen(frag_str, "#define LAYERED_DEPTH\n");
64         e_data.gtao_debug_sh = DRW_shader_create_fullscreen(frag_str, "#define DEBUG_AO\n");
65
66         MEM_freeN(frag_str);
67 }
68
69 int EEVEE_occlusion_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
70 {
71         EEVEE_CommonUniformBuffer *common_data = &sldata->common_data;
72         EEVEE_FramebufferList *fbl = vedata->fbl;
73         EEVEE_StorageList *stl = vedata->stl;
74         EEVEE_EffectsInfo *effects = stl->effects;
75
76         const DRWContextState *draw_ctx = DRW_context_state_get();
77         const Scene *scene_eval = DEG_get_evaluated_scene(draw_ctx->depsgraph);
78
79         if (scene_eval->eevee.flag & SCE_EEVEE_GTAO_ENABLED) {
80                 const float *viewport_size = DRW_viewport_size_get();
81                 const int fs_size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
82
83                 /* Shaders */
84                 if (!e_data.gtao_sh) {
85                         eevee_create_shader_occlusion();
86                 }
87
88                 common_data->ao_dist = scene_eval->eevee.gtao_distance;
89                 common_data->ao_factor = scene_eval->eevee.gtao_factor;
90                 common_data->ao_quality = 1.0f - scene_eval->eevee.gtao_quality;
91
92                 common_data->ao_settings = 1.0f; /* USE_AO */
93                 if (scene_eval->eevee.flag & SCE_EEVEE_GTAO_BENT_NORMALS) {
94                         common_data->ao_settings += 2.0f; /* USE_BENT_NORMAL */
95                 }
96                 if (scene_eval->eevee.flag & SCE_EEVEE_GTAO_BOUNCE) {
97                         common_data->ao_settings += 4.0f; /* USE_DENOISE */
98                 }
99
100                 common_data->ao_bounce_fac = (scene_eval->eevee.flag & SCE_EEVEE_GTAO_BOUNCE) ? 1.0f : 0.0f;
101
102                 effects->gtao_horizons = DRW_texture_pool_query_2D(fs_size[0], fs_size[1], GPU_RGBA8,
103                                                                    &draw_engine_eevee_type);
104                 GPU_framebuffer_ensure_config(&fbl->gtao_fb, {
105                         GPU_ATTACHMENT_NONE,
106                         GPU_ATTACHMENT_TEXTURE(effects->gtao_horizons)
107                 });
108
109                 if (G.debug_value == 6) {
110                         effects->gtao_horizons_debug = DRW_texture_pool_query_2D(fs_size[0], fs_size[1], GPU_RGBA8,
111                                                                                  &draw_engine_eevee_type);
112                         GPU_framebuffer_ensure_config(&fbl->gtao_debug_fb, {
113                                 GPU_ATTACHMENT_NONE,
114                                 GPU_ATTACHMENT_TEXTURE(effects->gtao_horizons_debug)
115                         });
116                 }
117                 else {
118                         effects->gtao_horizons_debug = NULL;
119                 }
120
121                 return EFFECT_GTAO | EFFECT_NORMAL_BUFFER;
122         }
123
124         /* Cleanup */
125         effects->gtao_horizons = NULL;
126         GPU_FRAMEBUFFER_FREE_SAFE(fbl->gtao_fb);
127         common_data->ao_settings = 0.0f;
128
129         return 0;
130 }
131
132 void EEVEE_occlusion_output_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
133 {
134         EEVEE_FramebufferList *fbl = vedata->fbl;
135         EEVEE_TextureList *txl = vedata->txl;
136         EEVEE_StorageList *stl = vedata->stl;
137         EEVEE_PassList *psl = vedata->psl;
138         EEVEE_EffectsInfo *effects = stl->effects;
139
140         const DRWContextState *draw_ctx = DRW_context_state_get();
141         const Scene *scene_eval = DEG_get_evaluated_scene(draw_ctx->depsgraph);
142
143         if (scene_eval->eevee.flag & SCE_EEVEE_GTAO_ENABLED) {
144                 DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
145                 float clear[4] = {0.0f, 0.0f, 0.0f, 0.0f};
146
147                 DRW_texture_ensure_fullscreen_2D(&txl->ao_accum, GPU_R32F, 0); /* Should be enough precision for many samples. */
148
149                 GPU_framebuffer_ensure_config(&fbl->ao_accum_fb, {
150                         GPU_ATTACHMENT_NONE,
151                         GPU_ATTACHMENT_TEXTURE(txl->ao_accum)
152                 });
153
154                 /* Clear texture. */
155                 GPU_framebuffer_bind(fbl->ao_accum_fb);
156                 GPU_framebuffer_clear_color(fbl->ao_accum_fb, clear);
157
158                 /* Accumulation pass */
159                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_ADDITIVE;
160                 psl->ao_accum_ps = DRW_pass_create("AO Accum", state);
161                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.gtao_debug_sh, psl->ao_accum_ps);
162                 DRW_shgroup_uniform_texture(grp, "utilTex", EEVEE_materials_get_util_tex());
163                 DRW_shgroup_uniform_texture_ref(grp, "maxzBuffer", &txl->maxzbuffer);
164                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
165                 DRW_shgroup_uniform_texture_ref(grp, "normalBuffer", &effects->ssr_normal_input);
166                 DRW_shgroup_uniform_texture_ref(grp, "horizonBuffer", &effects->gtao_horizons);
167                 DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
168                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
169         }
170         else {
171                 /* Cleanup to release memory */
172                 DRW_TEXTURE_FREE_SAFE(txl->ao_accum);
173                 GPU_FRAMEBUFFER_FREE_SAFE(fbl->ao_accum_fb);
174         }
175 }
176
177 void EEVEE_occlusion_cache_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
178 {
179         EEVEE_PassList *psl = vedata->psl;
180         EEVEE_StorageList *stl = vedata->stl;
181         EEVEE_TextureList *txl = vedata->txl;
182         EEVEE_EffectsInfo *effects = stl->effects;
183         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
184
185         struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
186
187         if ((effects->enabled_effects & EFFECT_GTAO) != 0) {
188                 /**  Occlusion algorithm overview
189                  *
190                  *  We separate the computation into 2 steps.
191                  *
192                  * - First we scan the neighborhood pixels to find the maximum horizon angle.
193                  *   We save this angle in a RG8 array texture.
194                  *
195                  * - Then we use this angle to compute occlusion with the shading normal at
196                  *   the shading stage. This let us do correct shadowing for each diffuse / specular
197                  *   lobe present in the shader using the correct normal.
198                  **/
199                 psl->ao_horizon_search = DRW_pass_create("GTAO Horizon Search", DRW_STATE_WRITE_COLOR);
200                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.gtao_sh, psl->ao_horizon_search);
201                 DRW_shgroup_uniform_texture(grp, "utilTex", EEVEE_materials_get_util_tex());
202                 DRW_shgroup_uniform_texture_ref(grp, "maxzBuffer", &txl->maxzbuffer);
203                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &effects->ao_src_depth);
204                 DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
205                 DRW_shgroup_call_add(grp, quad, NULL);
206
207                 psl->ao_horizon_search_layer = DRW_pass_create("GTAO Horizon Search Layer", DRW_STATE_WRITE_COLOR);
208                 grp = DRW_shgroup_create(e_data.gtao_layer_sh, psl->ao_horizon_search_layer);
209                 DRW_shgroup_uniform_texture(grp, "utilTex", EEVEE_materials_get_util_tex());
210                 DRW_shgroup_uniform_texture_ref(grp, "maxzBuffer", &txl->maxzbuffer);
211                 DRW_shgroup_uniform_texture_ref(grp, "depthBufferLayered", &effects->ao_src_depth);
212                 DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
213                 DRW_shgroup_uniform_int(grp, "layer", &stl->effects->ao_depth_layer, 1);
214                 DRW_shgroup_call_add(grp, quad, NULL);
215
216                 if (G.debug_value == 6) {
217                         psl->ao_horizon_debug = DRW_pass_create("GTAO Horizon Debug", DRW_STATE_WRITE_COLOR);
218                         grp = DRW_shgroup_create(e_data.gtao_debug_sh, psl->ao_horizon_debug);
219                         DRW_shgroup_uniform_texture(grp, "utilTex", EEVEE_materials_get_util_tex());
220                         DRW_shgroup_uniform_texture_ref(grp, "maxzBuffer", &txl->maxzbuffer);
221                         DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
222                         DRW_shgroup_uniform_texture_ref(grp, "normalBuffer", &effects->ssr_normal_input);
223                         DRW_shgroup_uniform_texture_ref(grp, "horizonBuffer", &effects->gtao_horizons);
224                         DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
225                         DRW_shgroup_call_add(grp, quad, NULL);
226                 }
227         }
228 }
229
230 void EEVEE_occlusion_compute(
231         EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata, struct GPUTexture *depth_src, int layer)
232 {
233         EEVEE_PassList *psl = vedata->psl;
234         EEVEE_FramebufferList *fbl = vedata->fbl;
235         EEVEE_StorageList *stl = vedata->stl;
236         EEVEE_EffectsInfo *effects = stl->effects;
237
238         if ((effects->enabled_effects & EFFECT_GTAO) != 0) {
239                 DRW_stats_group_start("GTAO Horizon Scan");
240                 effects->ao_src_depth = depth_src;
241                 effects->ao_depth_layer = layer;
242
243                 GPU_framebuffer_bind(fbl->gtao_fb);
244
245                 if (layer >= 0) {
246                         DRW_draw_pass(psl->ao_horizon_search_layer);
247                 }
248                 else {
249                         DRW_draw_pass(psl->ao_horizon_search);
250                 }
251
252                 if (GPU_mip_render_workaround() ||
253                     GPU_type_matches(GPU_DEVICE_INTEL_UHD, GPU_OS_WIN, GPU_DRIVER_ANY))
254                 {
255                         /* Fix dot corruption on intel HD5XX/HD6XX series. */
256                         GPU_flush();
257                 }
258
259                 /* Restore */
260                 GPU_framebuffer_bind(fbl->main_fb);
261
262                 DRW_stats_group_end();
263         }
264 }
265
266 void EEVEE_occlusion_draw_debug(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
267 {
268         EEVEE_PassList *psl = vedata->psl;
269         EEVEE_FramebufferList *fbl = vedata->fbl;
270         EEVEE_StorageList *stl = vedata->stl;
271         EEVEE_EffectsInfo *effects = stl->effects;
272
273         if (((effects->enabled_effects & EFFECT_GTAO) != 0) && (G.debug_value == 6)) {
274                 DRW_stats_group_start("GTAO Debug");
275
276                 GPU_framebuffer_bind(fbl->gtao_debug_fb);
277                 DRW_draw_pass(psl->ao_horizon_debug);
278
279                 /* Restore */
280                 GPU_framebuffer_bind(fbl->main_fb);
281
282                 DRW_stats_group_end();
283         }
284 }
285
286 void EEVEE_occlusion_output_accumulate(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
287 {
288         EEVEE_FramebufferList *fbl = vedata->fbl;
289         EEVEE_PassList *psl = vedata->psl;
290
291         if (fbl->ao_accum_fb != NULL) {
292                 DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
293
294                 /* Update the min_max/horizon buffers so the refracion materials appear in it. */
295                 EEVEE_create_minmax_buffer(vedata, dtxl->depth, -1);
296                 EEVEE_occlusion_compute(sldata, vedata, dtxl->depth, -1);
297
298                 GPU_framebuffer_bind(fbl->ao_accum_fb);
299                 DRW_draw_pass(psl->ao_accum_ps);
300
301                 /* Restore */
302                 GPU_framebuffer_bind(fbl->main_fb);
303         }
304 }
305
306 void EEVEE_occlusion_free(void)
307 {
308         DRW_SHADER_FREE_SAFE(e_data.gtao_sh);
309         DRW_SHADER_FREE_SAFE(e_data.gtao_layer_sh);
310         DRW_SHADER_FREE_SAFE(e_data.gtao_debug_sh);
311 }