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