doxygen: add newline after \file
[blender.git] / source / blender / draw / engines / eevee / eevee_screen_raytrace.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  * Screen space reflections and refractions techniques.
23  */
24
25 #include "DRW_render.h"
26
27 #include "BLI_dynstr.h"
28 #include "BLI_string_utils.h"
29
30 #include "DEG_depsgraph_query.h"
31
32 #include "eevee_private.h"
33 #include "GPU_texture.h"
34
35 /* SSR shader variations */
36 enum {
37         SSR_RESOLVE      = (1 << 0),
38         SSR_FULL_TRACE   = (1 << 1),
39         SSR_AO           = (1 << 3),
40         SSR_MAX_SHADER   = (1 << 4),
41 };
42
43 static struct {
44         /* Screen Space Reflection */
45         struct GPUShader *ssr_sh[SSR_MAX_SHADER];
46
47         /* Theses are just references, not actually allocated */
48         struct GPUTexture *depth_src;
49         struct GPUTexture *color_src;
50 } e_data = {{NULL}}; /* Engine data */
51
52 extern char datatoc_ambient_occlusion_lib_glsl[];
53 extern char datatoc_common_view_lib_glsl[];
54 extern char datatoc_common_uniforms_lib_glsl[];
55 extern char datatoc_bsdf_common_lib_glsl[];
56 extern char datatoc_bsdf_sampling_lib_glsl[];
57 extern char datatoc_octahedron_lib_glsl[];
58 extern char datatoc_effect_ssr_frag_glsl[];
59 extern char datatoc_lightprobe_lib_glsl[];
60 extern char datatoc_raytrace_lib_glsl[];
61
62 static struct GPUShader *eevee_effects_screen_raytrace_shader_get(int options)
63 {
64         if (e_data.ssr_sh[options] == NULL) {
65                 char *ssr_shader_str = BLI_string_joinN(
66                         datatoc_common_view_lib_glsl,
67                         datatoc_common_uniforms_lib_glsl,
68                         datatoc_bsdf_common_lib_glsl,
69                         datatoc_bsdf_sampling_lib_glsl,
70                         datatoc_ambient_occlusion_lib_glsl,
71                         datatoc_octahedron_lib_glsl,
72                         datatoc_lightprobe_lib_glsl,
73                         datatoc_raytrace_lib_glsl,
74                         datatoc_effect_ssr_frag_glsl);
75
76                 DynStr *ds_defines = BLI_dynstr_new();
77                 BLI_dynstr_appendf(ds_defines, SHADER_DEFINES);
78                 if (options & SSR_RESOLVE) {
79                         BLI_dynstr_appendf(ds_defines, "#define STEP_RESOLVE\n");
80                 }
81                 else {
82                         BLI_dynstr_appendf(ds_defines, "#define STEP_RAYTRACE\n");
83                         BLI_dynstr_appendf(ds_defines, "#define PLANAR_PROBE_RAYTRACE\n");
84                 }
85                 if (options & SSR_FULL_TRACE) {
86                         BLI_dynstr_appendf(ds_defines, "#define FULLRES\n");
87                 }
88                 if (options & SSR_AO) {
89                         BLI_dynstr_appendf(ds_defines, "#define SSR_AO\n");
90                 }
91                 char *ssr_define_str = BLI_dynstr_get_cstring(ds_defines);
92                 BLI_dynstr_free(ds_defines);
93
94                 e_data.ssr_sh[options] = DRW_shader_create_fullscreen(ssr_shader_str, ssr_define_str);
95
96                 MEM_freeN(ssr_shader_str);
97                 MEM_freeN(ssr_define_str);
98         }
99
100         return e_data.ssr_sh[options];
101 }
102
103 int EEVEE_screen_raytrace_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
104 {
105         EEVEE_CommonUniformBuffer *common_data = &sldata->common_data;
106         EEVEE_StorageList *stl = vedata->stl;
107         EEVEE_FramebufferList *fbl = vedata->fbl;
108         EEVEE_TextureList *txl = vedata->txl;
109         EEVEE_EffectsInfo *effects = stl->effects;
110         const float *viewport_size = DRW_viewport_size_get();
111
112         const DRWContextState *draw_ctx = DRW_context_state_get();
113         const Scene *scene_eval = DEG_get_evaluated_scene(draw_ctx->depsgraph);
114
115         /* Compute pixel size, (shared with contact shadows) */
116         copy_v2_v2(common_data->ssr_pixelsize, viewport_size);
117         invert_v2(common_data->ssr_pixelsize);
118
119         if (scene_eval->eevee.flag & SCE_EEVEE_SSR_ENABLED) {
120                 const bool use_refraction = (scene_eval->eevee.flag & SCE_EEVEE_SSR_REFRACTION) != 0;
121
122                 if (use_refraction) {
123                         /* TODO: Opti: Could be shared. */
124                         DRW_texture_ensure_fullscreen_2D(&txl->refract_color, GPU_R11F_G11F_B10F, DRW_TEX_FILTER | DRW_TEX_MIPMAP);
125
126                         GPU_framebuffer_ensure_config(&fbl->refract_fb, {
127                                 GPU_ATTACHMENT_NONE,
128                                 GPU_ATTACHMENT_TEXTURE(txl->refract_color)
129                         });
130                 }
131
132                 const bool is_persp = DRW_viewport_is_persp_get();
133                 if (effects->ssr_was_persp != is_persp) {
134                         effects->ssr_was_persp = is_persp;
135                         DRW_viewport_request_redraw();
136                         EEVEE_temporal_sampling_reset(vedata);
137                         stl->g_data->valid_double_buffer = false;
138                 }
139
140                 effects->reflection_trace_full = (scene_eval->eevee.flag & SCE_EEVEE_SSR_HALF_RESOLUTION) == 0;
141                 common_data->ssr_thickness = scene_eval->eevee.ssr_thickness;
142                 common_data->ssr_border_fac = scene_eval->eevee.ssr_border_fade;
143                 common_data->ssr_firefly_fac = scene_eval->eevee.ssr_firefly_fac;
144                 common_data->ssr_max_roughness = scene_eval->eevee.ssr_max_roughness;
145                 common_data->ssr_quality = 1.0f - 0.95f * scene_eval->eevee.ssr_quality;
146                 common_data->ssr_brdf_bias = 0.1f + common_data->ssr_quality * 0.6f; /* Range [0.1, 0.7]. */
147
148                 if (common_data->ssr_firefly_fac < 1e-8f) {
149                         common_data->ssr_firefly_fac = FLT_MAX;
150                 }
151
152                 const int divisor = (effects->reflection_trace_full) ? 1 : 2;
153                 int tracing_res[2] = {(int)viewport_size[0] / divisor, (int)viewport_size[1] / divisor};
154                 int size_fs[2] = {(int)viewport_size[0], (int)viewport_size[1]};
155                 const bool high_qual_input = true; /* TODO dither low quality input */
156                 const eGPUTextureFormat format = (high_qual_input) ? GPU_RGBA16F : GPU_RGBA8;
157
158                 /* MRT for the shading pass in order to output needed data for the SSR pass. */
159                 effects->ssr_specrough_input = DRW_texture_pool_query_2D(size_fs[0], size_fs[1], format,
160                                                                          &draw_engine_eevee_type);
161
162                 GPU_framebuffer_texture_attach(fbl->main_fb, effects->ssr_specrough_input, 2, 0);
163
164                 /* Raytracing output */
165                 effects->ssr_hit_output = DRW_texture_pool_query_2D(tracing_res[0], tracing_res[1], GPU_RG16I,
166                                                                     &draw_engine_eevee_type);
167                 effects->ssr_pdf_output = DRW_texture_pool_query_2D(tracing_res[0], tracing_res[1], GPU_R16F,
168                                                                     &draw_engine_eevee_type);
169
170                 GPU_framebuffer_ensure_config(&fbl->screen_tracing_fb, {
171                         GPU_ATTACHMENT_NONE,
172                         GPU_ATTACHMENT_TEXTURE(effects->ssr_hit_output),
173                         GPU_ATTACHMENT_TEXTURE(effects->ssr_pdf_output)
174                 });
175
176                 /* Enable double buffering to be able to read previous frame color */
177                 return EFFECT_SSR | EFFECT_NORMAL_BUFFER | EFFECT_DOUBLE_BUFFER | ((use_refraction) ? EFFECT_REFRACT : 0);
178         }
179
180         /* Cleanup to release memory */
181         GPU_FRAMEBUFFER_FREE_SAFE(fbl->screen_tracing_fb);
182         effects->ssr_specrough_input = NULL;
183         effects->ssr_hit_output = NULL;
184         effects->ssr_pdf_output = NULL;
185
186         return 0;
187 }
188
189 void EEVEE_screen_raytrace_cache_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
190 {
191         EEVEE_PassList *psl = vedata->psl;
192         EEVEE_StorageList *stl = vedata->stl;
193         EEVEE_TextureList *txl = vedata->txl;
194         EEVEE_EffectsInfo *effects = stl->effects;
195         LightCache *lcache = stl->g_data->light_cache;
196
197         struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
198
199         if ((effects->enabled_effects & EFFECT_SSR) != 0) {
200                 int options = (effects->reflection_trace_full) ? SSR_FULL_TRACE : 0;
201                 options |= ((effects->enabled_effects & EFFECT_GTAO) != 0) ? SSR_AO : 0;
202
203                 struct GPUShader *trace_shader = eevee_effects_screen_raytrace_shader_get(options);
204                 struct GPUShader *resolve_shader = eevee_effects_screen_raytrace_shader_get(SSR_RESOLVE | options);
205
206                 /** Screen space raytracing overview
207                  *
208                  * Following Frostbite stochastic SSR.
209                  *
210                  * - First pass Trace rays across the depth buffer. The hit position and pdf are
211                  *   recorded in a RGBA16F render target for each ray (sample).
212                  *
213                  * - We downsample the previous frame color buffer.
214                  *
215                  * - For each final pixel, we gather neighbors rays and choose a color buffer
216                  *   mipmap for each ray using its pdf. (filtered importance sampling)
217                  *   We then evaluate the lighting from the probes and mix the results together.
218                  */
219                 psl->ssr_raytrace = DRW_pass_create("SSR Raytrace", DRW_STATE_WRITE_COLOR);
220                 DRWShadingGroup *grp = DRW_shgroup_create(trace_shader, psl->ssr_raytrace);
221                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &e_data.depth_src);
222                 DRW_shgroup_uniform_texture_ref(grp, "normalBuffer", &effects->ssr_normal_input);
223                 DRW_shgroup_uniform_texture_ref(grp, "specroughBuffer", &effects->ssr_specrough_input);
224                 DRW_shgroup_uniform_texture_ref(grp, "maxzBuffer", &txl->maxzbuffer);
225                 DRW_shgroup_uniform_texture_ref(grp, "planarDepth", &vedata->txl->planar_depth);
226                 DRW_shgroup_uniform_texture(grp, "utilTex", EEVEE_materials_get_util_tex());
227                 DRW_shgroup_uniform_block(grp, "grid_block", sldata->grid_ubo);
228                 DRW_shgroup_uniform_block(grp, "probe_block", sldata->probe_ubo);
229                 DRW_shgroup_uniform_block(grp, "planar_block", sldata->planar_ubo);
230                 DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
231                 if (!effects->reflection_trace_full) {
232                         DRW_shgroup_uniform_ivec2(grp, "halfresOffset", effects->ssr_halfres_ofs, 1);
233                 }
234                 DRW_shgroup_call_add(grp, quad, NULL);
235
236                 psl->ssr_resolve = DRW_pass_create("SSR Resolve", DRW_STATE_WRITE_COLOR | DRW_STATE_ADDITIVE);
237                 grp = DRW_shgroup_create(resolve_shader, psl->ssr_resolve);
238                 DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &e_data.depth_src);
239                 DRW_shgroup_uniform_texture_ref(grp, "normalBuffer", &effects->ssr_normal_input);
240                 DRW_shgroup_uniform_texture_ref(grp, "specroughBuffer", &effects->ssr_specrough_input);
241                 DRW_shgroup_uniform_texture_ref(grp, "probeCubes", &lcache->cube_tx.tex);
242                 DRW_shgroup_uniform_texture_ref(grp, "probePlanars", &vedata->txl->planar_pool);
243                 DRW_shgroup_uniform_texture_ref(grp, "planarDepth", &vedata->txl->planar_depth);
244                 DRW_shgroup_uniform_texture_ref(grp, "hitBuffer", &effects->ssr_hit_output);
245                 DRW_shgroup_uniform_texture_ref(grp, "pdfBuffer", &effects->ssr_pdf_output);
246                 DRW_shgroup_uniform_texture_ref(grp, "prevColorBuffer", &txl->color_double_buffer);
247                 DRW_shgroup_uniform_block(grp, "grid_block", sldata->grid_ubo);
248                 DRW_shgroup_uniform_block(grp, "probe_block", sldata->probe_ubo);
249                 DRW_shgroup_uniform_block(grp, "planar_block", sldata->planar_ubo);
250                 DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
251                 DRW_shgroup_uniform_int(grp, "neighborOffset", &effects->ssr_neighbor_ofs, 1);
252                 if ((effects->enabled_effects & EFFECT_GTAO) != 0) {
253                         DRW_shgroup_uniform_texture(grp, "utilTex", EEVEE_materials_get_util_tex());
254                         DRW_shgroup_uniform_texture_ref(grp, "horizonBuffer", &effects->gtao_horizons);
255                 }
256
257                 DRW_shgroup_call_add(grp, quad, NULL);
258         }
259 }
260
261 void EEVEE_refraction_compute(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
262 {
263         EEVEE_FramebufferList *fbl = vedata->fbl;
264         EEVEE_TextureList *txl = vedata->txl;
265         EEVEE_StorageList *stl = vedata->stl;
266         EEVEE_EffectsInfo *effects = stl->effects;
267
268         if ((effects->enabled_effects & EFFECT_REFRACT) != 0) {
269                 GPU_framebuffer_blit(fbl->main_fb, 0, fbl->refract_fb, 0, GPU_COLOR_BIT);
270                 EEVEE_downsample_buffer(vedata, txl->refract_color, 9);
271
272                 /* Restore */
273                 GPU_framebuffer_bind(fbl->main_fb);
274         }
275 }
276
277 void EEVEE_reflection_compute(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
278 {
279         EEVEE_PassList *psl = vedata->psl;
280         EEVEE_FramebufferList *fbl = vedata->fbl;
281         EEVEE_StorageList *stl = vedata->stl;
282         EEVEE_TextureList *txl = vedata->txl;
283         EEVEE_EffectsInfo *effects = stl->effects;
284
285         if (((effects->enabled_effects & EFFECT_SSR) != 0) && stl->g_data->valid_double_buffer) {
286                 DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
287                 e_data.depth_src = dtxl->depth;
288
289                 DRW_stats_group_start("SSR");
290
291                 /* Raytrace. */
292                 GPU_framebuffer_bind(fbl->screen_tracing_fb);
293                 DRW_draw_pass(psl->ssr_raytrace);
294
295                 EEVEE_downsample_buffer(vedata, txl->color_double_buffer, 9);
296
297                 /* Resolve at fullres */
298                 int sample = (DRW_state_is_image_render()) ? effects->taa_render_sample : effects->taa_current_sample;
299                 /* Doing a neighbor shift only after a few iteration. We wait for a prime number of cycles to avoid
300                  * noise correlation. This reduces variance faster. */
301                 effects->ssr_neighbor_ofs = ((sample / 5) % 8) * 4;
302                 switch ((sample / 11) % 4) {
303                         case 0:
304                                 effects->ssr_halfres_ofs[0] = 0;
305                                 effects->ssr_halfres_ofs[1] = 0;
306                                 break;
307                         case 1:
308                                 effects->ssr_halfres_ofs[0] = 0;
309                                 effects->ssr_halfres_ofs[1] = 1;
310                                 break;
311                         case 2:
312                                 effects->ssr_halfres_ofs[0] = 1;
313                                 effects->ssr_halfres_ofs[1] = 0;
314                                 break;
315                         case 4:
316                                 effects->ssr_halfres_ofs[0] = 1;
317                                 effects->ssr_halfres_ofs[1] = 1;
318                                 break;
319                 }
320                 GPU_framebuffer_bind(fbl->main_color_fb);
321                 DRW_draw_pass(psl->ssr_resolve);
322
323                 /* Restore */
324                 GPU_framebuffer_bind(fbl->main_fb);
325                 DRW_stats_group_end();
326         }
327 }
328
329 void EEVEE_screen_raytrace_free(void)
330 {
331         for (int i = 0; i < SSR_MAX_SHADER; ++i) {
332                 DRW_SHADER_FREE_SAFE(e_data.ssr_sh[i]);
333         }
334 }