Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / draw / engines / eevee / eevee_temporal_sampling.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 \ingroup draw_engine
20  *
21  * Temporal super sampling technique
22  */
23
24 #include "DRW_render.h"
25
26 #include "ED_screen.h"
27
28 #include "BLI_rand.h"
29
30 #include "DEG_depsgraph_query.h"
31
32 #include "eevee_private.h"
33 #include "GPU_texture.h"
34
35 #define FILTER_CDF_TABLE_SIZE 512
36
37 static struct {
38         /* Pixel filter table: Only blackman-harris for now. */
39         bool inited;
40         float inverted_cdf[FILTER_CDF_TABLE_SIZE];
41 } e_data = {false}; /* Engine data */
42
43 extern char datatoc_common_uniforms_lib_glsl[];
44 extern char datatoc_common_view_lib_glsl[];
45 extern char datatoc_bsdf_common_lib_glsl[];
46
47 static float UNUSED_FUNCTION(filter_box)(float UNUSED(x))
48 {
49         return 1.0f;
50 }
51
52 static float filter_blackman_harris(float x)
53 {
54         /* Hardcoded 1px footprint [-0.5..0.5]. We resize later. */
55         const float width = 1.0f;
56         x = 2.0f * M_PI * (x / width + 0.5f);
57         return 0.35875f - 0.48829f * cosf(x) + 0.14128f * cosf(2.0f * x) - 0.01168f * cosf(3.0f * x);
58 }
59
60 /* Compute cumulative distribution function of a discrete function. */
61 static void compute_cdf(float (*func)(float x), float cdf[FILTER_CDF_TABLE_SIZE])
62 {
63         cdf[0] = 0.0f;
64         /* Actual CDF evaluation. */
65         for (int u = 0; u < FILTER_CDF_TABLE_SIZE - 1; ++u) {
66                 float x = (float)(u + 1) / (float)(FILTER_CDF_TABLE_SIZE - 1);
67                 cdf[u + 1] = cdf[u] + func(x - 0.5f); /* [-0.5..0.5]. We resize later. */
68         }
69         /* Normalize the CDF. */
70         for (int u = 0; u < FILTER_CDF_TABLE_SIZE - 1; u++) {
71                 cdf[u] /= cdf[FILTER_CDF_TABLE_SIZE - 1];
72         }
73         /* Just to make sure. */
74         cdf[FILTER_CDF_TABLE_SIZE - 1] = 1.0f;
75 }
76
77 static void invert_cdf(const float cdf[FILTER_CDF_TABLE_SIZE], float invert_cdf[FILTER_CDF_TABLE_SIZE])
78 {
79         for (int u = 0; u < FILTER_CDF_TABLE_SIZE; u++) {
80                 float x = (float)u / (float)(FILTER_CDF_TABLE_SIZE - 1);
81                 for (int i = 0; i < FILTER_CDF_TABLE_SIZE; ++i) {
82                         if (cdf[i] >= x) {
83                                 if (i == FILTER_CDF_TABLE_SIZE - 1) {
84                                         invert_cdf[u] = 1.0f;
85                                 }
86                                 else {
87                                         float t = (x - cdf[i]) / (cdf[i + 1] - cdf[i]);
88                                         invert_cdf[u] = ((float)i + t) / (float)(FILTER_CDF_TABLE_SIZE - 1);
89                                 }
90                                 break;
91                         }
92                 }
93         }
94 }
95
96 /* Evaluate a discrete function table with linear interpolation. */
97 static float eval_table(float *table, float x)
98 {
99         CLAMP(x, 0.0f, 1.0f);
100         x = x * (FILTER_CDF_TABLE_SIZE - 1);
101
102         int index = min_ii((int)(x), FILTER_CDF_TABLE_SIZE - 1);
103         int nindex = min_ii(index + 1, FILTER_CDF_TABLE_SIZE - 1);
104         float t = x - index;
105
106         return (1.0f - t) * table[index] + t * table[nindex];
107 }
108
109 static void eevee_create_cdf_table_temporal_sampling(void)
110 {
111         float *cdf_table = MEM_mallocN(sizeof(float) * FILTER_CDF_TABLE_SIZE, "Eevee Filter CDF table");
112
113         float filter_width = 2.0f; /* Use a 2 pixel footprint by default. */
114
115         {
116                 /* Use blackman-harris filter. */
117                 filter_width *= 2.0f;
118                 compute_cdf(filter_blackman_harris, cdf_table);
119         }
120
121         invert_cdf(cdf_table, e_data.inverted_cdf);
122
123         /* Scale and offset table. */
124         for (int i = 0; i < FILTER_CDF_TABLE_SIZE; ++i) {
125                 e_data.inverted_cdf[i] = (e_data.inverted_cdf[i] - 0.5f) * filter_width;
126         }
127
128         MEM_freeN(cdf_table);
129         e_data.inited = true;
130 }
131
132 void EEVEE_temporal_sampling_matrices_calc(
133         EEVEE_EffectsInfo *effects, float viewmat[4][4], float persmat[4][4], const double ht_point[2])
134 {
135         const float *viewport_size = DRW_viewport_size_get();
136         const DRWContextState *draw_ctx = DRW_context_state_get();
137         Scene *scene = draw_ctx->scene;
138         RenderData *rd = &scene->r;
139
140         float filter_size = rd->gauss; /* Sigh.. Stupid legacy naming. */
141
142         float ofs_x = eval_table(e_data.inverted_cdf, (float)(ht_point[0])) * filter_size;
143         float ofs_y = eval_table(e_data.inverted_cdf, (float)(ht_point[1])) * filter_size;
144
145         window_translate_m4(
146                 effects->overide_winmat, persmat,
147                 ofs_x / viewport_size[0],
148                 ofs_y / viewport_size[1]);
149
150         mul_m4_m4m4(effects->overide_persmat, effects->overide_winmat, viewmat);
151         invert_m4_m4(effects->overide_persinv, effects->overide_persmat);
152         invert_m4_m4(effects->overide_wininv, effects->overide_winmat);
153 }
154
155 void EEVEE_temporal_sampling_reset(EEVEE_Data *vedata)
156 {
157         vedata->stl->effects->taa_render_sample = 1;
158 }
159
160 int EEVEE_temporal_sampling_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
161 {
162         EEVEE_StorageList *stl = vedata->stl;
163         // EEVEE_FramebufferList *fbl = vedata->fbl;
164         // EEVEE_TextureList *txl = vedata->txl;
165         EEVEE_EffectsInfo *effects = stl->effects;
166         int repro_flag = 0;
167
168         if (!e_data.inited) {
169                 eevee_create_cdf_table_temporal_sampling();
170         }
171
172         /* Reset for each "redraw". When rendering using ogl render,
173          * we accumulate the redraw inside the drawing loop in eevee_draw_background().
174          * But we do NOT accumulate between "redraw" (as in full draw manager drawloop)
175          * because the opengl render already does that. */
176         effects->taa_render_sample = 1;
177
178         const DRWContextState *draw_ctx = DRW_context_state_get();
179         const Scene *scene_eval = DEG_get_evaluated_scene(draw_ctx->depsgraph);
180
181         if (((scene_eval->eevee.taa_samples != 1) &&
182             /* FIXME the motion blur camera evaluation is tagging view_updated
183              * thus making the TAA always reset and never stopping rendering. */
184             (effects->enabled_effects & EFFECT_MOTION_BLUR) == 0) ||
185             DRW_state_is_image_render())
186         {
187                 float persmat[4][4], viewmat[4][4];
188
189                 if (!DRW_state_is_image_render() &&
190                     (scene_eval->eevee.flag & SCE_EEVEE_TAA_REPROJECTION))
191                 {
192                         repro_flag = EFFECT_TAA_REPROJECT | EFFECT_VELOCITY_BUFFER | EFFECT_DEPTH_DOUBLE_BUFFER | EFFECT_DOUBLE_BUFFER | EFFECT_POST_BUFFER;
193                         effects->taa_reproject_sample = ((effects->taa_reproject_sample + 1) % 16);
194                 }
195
196                 /* Until we support reprojection, we need to make sure
197                  * that the history buffer contains correct information. */
198                 bool view_is_valid = stl->g_data->valid_double_buffer;
199
200                 view_is_valid = view_is_valid && (stl->g_data->view_updated == false);
201
202                 if (draw_ctx->evil_C != NULL) {
203                         struct wmWindowManager *wm = CTX_wm_manager(draw_ctx->evil_C);
204                         view_is_valid = view_is_valid && (ED_screen_animation_no_scrub(wm) == NULL);
205                 }
206
207                 effects->taa_total_sample = scene_eval->eevee.taa_samples;
208                 MAX2(effects->taa_total_sample, 0);
209
210                 DRW_viewport_matrix_get(persmat, DRW_MAT_PERS);
211                 DRW_viewport_matrix_get(viewmat, DRW_MAT_VIEW);
212                 DRW_viewport_matrix_get(effects->overide_winmat, DRW_MAT_WIN);
213                 /* The view is jittered by the oglrenderer. So avoid testing in this case. */
214                 if (!DRW_state_is_image_render()) {
215                         view_is_valid = view_is_valid && compare_m4m4(persmat, effects->prev_drw_persmat, FLT_MIN);
216                         copy_m4_m4(effects->prev_drw_persmat, persmat);
217                 }
218
219                 /* Prevent ghosting from probe data. */
220                 view_is_valid = view_is_valid && (effects->prev_drw_support == DRW_state_draw_support());
221                 effects->prev_drw_support = DRW_state_draw_support();
222
223                 if (((effects->taa_total_sample == 0) || (effects->taa_current_sample < effects->taa_total_sample)) ||
224                     DRW_state_is_image_render())
225                 {
226                         if (view_is_valid) {
227                                 /* OGL render already jitter the camera. */
228                                 if (!DRW_state_is_image_render()) {
229                                         effects->taa_current_sample += 1;
230                                         repro_flag = 0;
231
232                                         double ht_point[2];
233                                         double ht_offset[2] = {0.0, 0.0};
234                                         uint ht_primes[2] = {2, 3};
235
236                                         BLI_halton_2D(ht_primes, ht_offset, effects->taa_current_sample - 1, ht_point);
237
238                                         EEVEE_temporal_sampling_matrices_calc(effects, viewmat, persmat, ht_point);
239
240                                         DRW_viewport_matrix_override_set(effects->overide_persmat, DRW_MAT_PERS);
241                                         DRW_viewport_matrix_override_set(effects->overide_persinv, DRW_MAT_PERSINV);
242                                         DRW_viewport_matrix_override_set(effects->overide_winmat, DRW_MAT_WIN);
243                                         DRW_viewport_matrix_override_set(effects->overide_wininv, DRW_MAT_WININV);
244                                 }
245                         }
246                         else {
247                                 effects->taa_current_sample = 1;
248                         }
249                 }
250                 else {
251                         effects->taa_current_sample = 1;
252                 }
253
254                 return repro_flag | EFFECT_TAA | EFFECT_DOUBLE_BUFFER | EFFECT_DEPTH_DOUBLE_BUFFER | EFFECT_POST_BUFFER;
255         }
256
257         effects->taa_current_sample = 1;
258
259         return repro_flag;
260 }
261
262 void EEVEE_temporal_sampling_cache_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
263 {
264         EEVEE_PassList *psl = vedata->psl;
265         EEVEE_StorageList *stl = vedata->stl;
266         EEVEE_TextureList *txl = vedata->txl;
267         EEVEE_EffectsInfo *effects = stl->effects;
268
269         if ((effects->enabled_effects & (EFFECT_TAA | EFFECT_TAA_REPROJECT)) != 0) {
270                 struct GPUShader *sh = EEVEE_shaders_taa_resolve_sh_get(effects->enabled_effects);
271
272                 psl->taa_resolve = DRW_pass_create("Temporal AA Resolve", DRW_STATE_WRITE_COLOR);
273                 DRWShadingGroup *grp = DRW_shgroup_create(sh, psl->taa_resolve);
274
275                 DRW_shgroup_uniform_texture_ref(grp, "colorHistoryBuffer", &txl->taa_history);
276                 DRW_shgroup_uniform_texture_ref(grp, "colorBuffer", &effects->source_buffer);
277                 DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
278
279                 if (effects->enabled_effects & EFFECT_TAA_REPROJECT) {
280                         // DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
281                         DRW_shgroup_uniform_texture_ref(grp, "velocityBuffer", &effects->velocity_tx);
282                 }
283                 else {
284                         DRW_shgroup_uniform_float(grp, "alpha", &effects->taa_alpha, 1);
285                 }
286                 DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
287         }
288 }
289
290 void EEVEE_temporal_sampling_draw(EEVEE_Data *vedata)
291 {
292         EEVEE_PassList *psl = vedata->psl;
293         EEVEE_TextureList *txl = vedata->txl;
294         EEVEE_FramebufferList *fbl = vedata->fbl;
295         EEVEE_StorageList *stl = vedata->stl;
296         EEVEE_EffectsInfo *effects = stl->effects;
297
298         if ((effects->enabled_effects & (EFFECT_TAA | EFFECT_TAA_REPROJECT)) != 0) {
299                 if ((effects->enabled_effects & EFFECT_TAA) != 0 && effects->taa_current_sample != 1) {
300                         if (DRW_state_is_image_render()) {
301                                 /* See EEVEE_temporal_sampling_init() for more details. */
302                                 effects->taa_alpha = 1.0f / (float)(effects->taa_render_sample);
303                         }
304                         else {
305                                 effects->taa_alpha = 1.0f / (float)(effects->taa_current_sample);
306                         }
307
308                         GPU_framebuffer_bind(effects->target_buffer);
309                         DRW_draw_pass(psl->taa_resolve);
310
311                         /* Restore the depth from sample 1. */
312                         if (!DRW_state_is_image_render()) {
313                                 GPU_framebuffer_blit(fbl->double_buffer_depth_fb, 0, fbl->main_fb, 0, GPU_DEPTH_BIT);
314                         }
315
316                         SWAP_BUFFERS_TAA();
317                 }
318                 else {
319                         if (!DRW_state_is_image_render()) {
320                                 /* Save the depth buffer for the next frame.
321                                  * This saves us from doing anything special
322                                  * in the other mode engines. */
323                                 GPU_framebuffer_blit(fbl->main_fb, 0, fbl->double_buffer_depth_fb, 0, GPU_DEPTH_BIT);
324                         }
325
326                         /* Do reprojection for noise reduction */
327                         /* TODO : do AA jitter if in only render view. */
328                         if (!DRW_state_is_image_render() &&
329                             (effects->enabled_effects & EFFECT_TAA_REPROJECT) != 0 &&
330                             stl->g_data->valid_taa_history)
331                         {
332                                 GPU_framebuffer_bind(effects->target_buffer);
333                                 DRW_draw_pass(psl->taa_resolve);
334                                 SWAP_BUFFERS_TAA();
335                         }
336                         else {
337                                 struct GPUFrameBuffer *source_fb = (effects->target_buffer == fbl->main_color_fb) ? fbl->effect_color_fb : fbl->main_color_fb;
338                                 GPU_framebuffer_blit(source_fb, 0, fbl->taa_history_color_fb, 0, GPU_COLOR_BIT);
339                         }
340                 }
341
342                 /* Make each loop count when doing a render. */
343                 if (DRW_state_is_image_render()) {
344                         effects->taa_render_sample += 1;
345                         effects->taa_current_sample += 1;
346                 }
347                 else {
348                         if ((effects->taa_total_sample == 0) ||
349                             (effects->taa_current_sample < effects->taa_total_sample))
350                         {
351                                 DRW_viewport_request_redraw();
352                         }
353                 }
354         }
355 }