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