Cleanup: misc spelling fixes
[blender.git] / source / blender / draw / engines / eevee / eevee_volumes.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  * Volumetric effects rendering using frostbite approach.
23  */
24
25 #include "DRW_render.h"
26
27 #include "BLI_rand.h"
28 #include "BLI_string_utils.h"
29
30 #include "DNA_object_force_types.h"
31 #include "DNA_smoke_types.h"
32 #include "DNA_world_types.h"
33
34 #include "BKE_modifier.h"
35 #include "BKE_mesh.h"
36 #include "BKE_smoke.h"
37
38 #include "ED_screen.h"
39
40 #include "DEG_depsgraph_query.h"
41
42 #include "eevee_private.h"
43 #include "GPU_draw.h"
44 #include "GPU_texture.h"
45 #include "GPU_material.h"
46
47 static struct {
48   char *volumetric_common_lib;
49   char *volumetric_common_lights_lib;
50
51   struct GPUShader *volumetric_clear_sh;
52   struct GPUShader *scatter_sh;
53   struct GPUShader *scatter_with_lights_sh;
54   struct GPUShader *volumetric_integration_sh;
55   struct GPUShader *volumetric_resolve_sh;
56
57   GPUTexture *color_src;
58   GPUTexture *depth_src;
59
60   GPUTexture *dummy_density;
61   GPUTexture *dummy_flame;
62
63   GPUTexture *dummy_scatter;
64   GPUTexture *dummy_transmit;
65
66   /* List of all smoke domains rendered within this frame. */
67   ListBase smoke_domains;
68 } e_data = {NULL}; /* Engine data */
69
70 extern char datatoc_bsdf_common_lib_glsl[];
71 extern char datatoc_common_uniforms_lib_glsl[];
72 extern char datatoc_common_view_lib_glsl[];
73 extern char datatoc_octahedron_lib_glsl[];
74 extern char datatoc_irradiance_lib_glsl[];
75 extern char datatoc_lights_lib_glsl[];
76 extern char datatoc_volumetric_frag_glsl[];
77 extern char datatoc_volumetric_geom_glsl[];
78 extern char datatoc_volumetric_vert_glsl[];
79 extern char datatoc_volumetric_resolve_frag_glsl[];
80 extern char datatoc_volumetric_scatter_frag_glsl[];
81 extern char datatoc_volumetric_integration_frag_glsl[];
82 extern char datatoc_volumetric_lib_glsl[];
83 extern char datatoc_common_fullscreen_vert_glsl[];
84
85 static void eevee_create_shader_volumes(void)
86 {
87   e_data.volumetric_common_lib = BLI_string_joinN(datatoc_common_view_lib_glsl,
88                                                   datatoc_common_uniforms_lib_glsl,
89                                                   datatoc_bsdf_common_lib_glsl,
90                                                   datatoc_volumetric_lib_glsl);
91
92   e_data.volumetric_common_lights_lib = BLI_string_joinN(datatoc_common_view_lib_glsl,
93                                                          datatoc_common_uniforms_lib_glsl,
94                                                          datatoc_bsdf_common_lib_glsl,
95                                                          datatoc_octahedron_lib_glsl,
96                                                          datatoc_irradiance_lib_glsl,
97                                                          datatoc_lights_lib_glsl,
98                                                          datatoc_volumetric_lib_glsl);
99
100   e_data.volumetric_clear_sh = DRW_shader_create_with_lib(datatoc_volumetric_vert_glsl,
101                                                           datatoc_volumetric_geom_glsl,
102                                                           datatoc_volumetric_frag_glsl,
103                                                           e_data.volumetric_common_lib,
104                                                           "#define VOLUMETRICS\n"
105                                                           "#define CLEAR\n");
106   e_data.scatter_sh = DRW_shader_create_with_lib(datatoc_volumetric_vert_glsl,
107                                                  datatoc_volumetric_geom_glsl,
108                                                  datatoc_volumetric_scatter_frag_glsl,
109                                                  e_data.volumetric_common_lights_lib,
110                                                  SHADER_DEFINES
111                                                  "#define VOLUMETRICS\n"
112                                                  "#define VOLUME_SHADOW\n");
113   e_data.scatter_with_lights_sh = DRW_shader_create_with_lib(datatoc_volumetric_vert_glsl,
114                                                              datatoc_volumetric_geom_glsl,
115                                                              datatoc_volumetric_scatter_frag_glsl,
116                                                              e_data.volumetric_common_lights_lib,
117                                                              SHADER_DEFINES
118                                                              "#define VOLUMETRICS\n"
119                                                              "#define VOLUME_LIGHTING\n"
120                                                              "#define VOLUME_SHADOW\n");
121   e_data.volumetric_integration_sh = DRW_shader_create_with_lib(
122       datatoc_volumetric_vert_glsl,
123       datatoc_volumetric_geom_glsl,
124       datatoc_volumetric_integration_frag_glsl,
125       e_data.volumetric_common_lib,
126       NULL);
127   e_data.volumetric_resolve_sh = DRW_shader_create_with_lib(datatoc_common_fullscreen_vert_glsl,
128                                                             NULL,
129                                                             datatoc_volumetric_resolve_frag_glsl,
130                                                             e_data.volumetric_common_lib,
131                                                             NULL);
132
133   float color[4] = {1.0f, 1.0f, 1.0f, 1.0f};
134   e_data.dummy_density = DRW_texture_create_3d(1, 1, 1, GPU_RGBA8, DRW_TEX_WRAP, color);
135
136   float flame = 0.0f;
137   e_data.dummy_flame = DRW_texture_create_3d(1, 1, 1, GPU_R8, DRW_TEX_WRAP, &flame);
138 }
139
140 void EEVEE_volumes_set_jitter(EEVEE_ViewLayerData *sldata, uint current_sample)
141 {
142   EEVEE_CommonUniformBuffer *common_data = &sldata->common_data;
143
144   double ht_point[3];
145   double ht_offset[3] = {0.0, 0.0};
146   uint ht_primes[3] = {3, 7, 2};
147
148   BLI_halton_3d(ht_primes, ht_offset, current_sample, ht_point);
149
150   common_data->vol_jitter[0] = (float)ht_point[0];
151   common_data->vol_jitter[1] = (float)ht_point[1];
152   common_data->vol_jitter[2] = (float)ht_point[2];
153 }
154
155 void EEVEE_volumes_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
156 {
157   EEVEE_StorageList *stl = vedata->stl;
158   EEVEE_FramebufferList *fbl = vedata->fbl;
159   EEVEE_TextureList *txl = vedata->txl;
160   EEVEE_EffectsInfo *effects = stl->effects;
161   EEVEE_CommonUniformBuffer *common_data = &sldata->common_data;
162
163   const DRWContextState *draw_ctx = DRW_context_state_get();
164   const Scene *scene_eval = DEG_get_evaluated_scene(draw_ctx->depsgraph);
165
166   const float *viewport_size = DRW_viewport_size_get();
167
168   BLI_listbase_clear(&e_data.smoke_domains);
169
170   const int tile_size = scene_eval->eevee.volumetric_tile_size;
171
172   /* Find Froxel Texture resolution. */
173   int tex_size[3];
174
175   tex_size[0] = (int)ceilf(fmaxf(1.0f, viewport_size[0] / (float)tile_size));
176   tex_size[1] = (int)ceilf(fmaxf(1.0f, viewport_size[1] / (float)tile_size));
177   tex_size[2] = max_ii(scene_eval->eevee.volumetric_samples, 1);
178
179   common_data->vol_coord_scale[0] = viewport_size[0] / (float)(tile_size * tex_size[0]);
180   common_data->vol_coord_scale[1] = viewport_size[1] / (float)(tile_size * tex_size[1]);
181   common_data->vol_coord_scale[2] = 1.0f / viewport_size[0];
182   common_data->vol_coord_scale[3] = 1.0f / viewport_size[1];
183
184   /* TODO compute snap to maxZBuffer for clustered rendering */
185   if ((common_data->vol_tex_size[0] != tex_size[0]) ||
186       (common_data->vol_tex_size[1] != tex_size[1]) ||
187       (common_data->vol_tex_size[2] != tex_size[2])) {
188     DRW_TEXTURE_FREE_SAFE(txl->volume_prop_scattering);
189     DRW_TEXTURE_FREE_SAFE(txl->volume_prop_extinction);
190     DRW_TEXTURE_FREE_SAFE(txl->volume_prop_emission);
191     DRW_TEXTURE_FREE_SAFE(txl->volume_prop_phase);
192     DRW_TEXTURE_FREE_SAFE(txl->volume_scatter);
193     DRW_TEXTURE_FREE_SAFE(txl->volume_transmit);
194     DRW_TEXTURE_FREE_SAFE(txl->volume_scatter_history);
195     DRW_TEXTURE_FREE_SAFE(txl->volume_transmit_history);
196     GPU_FRAMEBUFFER_FREE_SAFE(fbl->volumetric_fb);
197     GPU_FRAMEBUFFER_FREE_SAFE(fbl->volumetric_scat_fb);
198     GPU_FRAMEBUFFER_FREE_SAFE(fbl->volumetric_integ_fb);
199     copy_v3_v3_int(common_data->vol_tex_size, tex_size);
200
201     common_data->vol_inv_tex_size[0] = 1.0f / (float)(tex_size[0]);
202     common_data->vol_inv_tex_size[1] = 1.0f / (float)(tex_size[1]);
203     common_data->vol_inv_tex_size[2] = 1.0f / (float)(tex_size[2]);
204   }
205
206   /* Like frostbite's paper, 5% blend of the new frame. */
207   common_data->vol_history_alpha = (txl->volume_prop_scattering == NULL) ? 0.0f : 0.95f;
208
209   /* Temporal Super sampling jitter */
210   uint ht_primes[3] = {3, 7, 2};
211   uint current_sample = 0;
212
213   /* If TAA is in use do not use the history buffer. */
214   bool do_taa = ((effects->enabled_effects & EFFECT_TAA) != 0);
215
216   if (draw_ctx->evil_C != NULL) {
217     struct wmWindowManager *wm = CTX_wm_manager(draw_ctx->evil_C);
218     do_taa = do_taa && (ED_screen_animation_no_scrub(wm) == NULL);
219   }
220
221   if (do_taa) {
222     common_data->vol_history_alpha = 0.0f;
223     current_sample = effects->taa_current_sample - 1;
224     effects->volume_current_sample = -1;
225   }
226   else if (DRW_state_is_image_render()) {
227     const uint max_sample = (ht_primes[0] * ht_primes[1] * ht_primes[2]);
228     current_sample = effects->volume_current_sample = (effects->volume_current_sample + 1) %
229                                                       max_sample;
230     if (current_sample != max_sample - 1) {
231       DRW_viewport_request_redraw();
232     }
233   }
234
235   EEVEE_volumes_set_jitter(sldata, current_sample);
236
237   float integration_start = scene_eval->eevee.volumetric_start;
238   float integration_end = scene_eval->eevee.volumetric_end;
239   common_data->vol_light_clamp = scene_eval->eevee.volumetric_light_clamp;
240   common_data->vol_shadow_steps = (float)scene_eval->eevee.volumetric_shadow_samples;
241   if ((scene_eval->eevee.flag & SCE_EEVEE_VOLUMETRIC_SHADOWS) == 0) {
242     common_data->vol_shadow_steps = 0;
243   }
244
245   /* Update view_vecs */
246   float invproj[4][4], winmat[4][4];
247   DRW_view_winmat_get(NULL, winmat, false);
248   DRW_view_winmat_get(NULL, invproj, true);
249   EEVEE_update_viewvecs(invproj, winmat, sldata->common_data.view_vecs);
250
251   if (DRW_view_is_persp_get(NULL)) {
252     float sample_distribution = scene_eval->eevee.volumetric_sample_distribution;
253     sample_distribution = 4.0f * (1.00001f - sample_distribution);
254
255     const float clip_start = common_data->view_vecs[0][2];
256     /* Negate */
257     float near = integration_start = min_ff(-integration_start, clip_start - 1e-4f);
258     float far = integration_end = min_ff(-integration_end, near - 1e-4f);
259
260     common_data->vol_depth_param[0] = (far - near * exp2(1.0f / sample_distribution)) /
261                                       (far - near);
262     common_data->vol_depth_param[1] = (1.0f - common_data->vol_depth_param[0]) / near;
263     common_data->vol_depth_param[2] = sample_distribution;
264   }
265   else {
266     const float clip_start = common_data->view_vecs[0][2];
267     const float clip_end = clip_start + common_data->view_vecs[1][2];
268     integration_start = min_ff(integration_end, clip_start);
269     integration_end = max_ff(-integration_end, clip_end);
270
271     common_data->vol_depth_param[0] = integration_start;
272     common_data->vol_depth_param[1] = integration_end;
273     common_data->vol_depth_param[2] = 1.0f / (integration_end - integration_start);
274   }
275
276   /* Disable clamp if equal to 0. */
277   if (common_data->vol_light_clamp == 0.0) {
278     common_data->vol_light_clamp = FLT_MAX;
279   }
280
281   common_data->vol_use_lights = (scene_eval->eevee.flag & SCE_EEVEE_VOLUMETRIC_LIGHTS) != 0;
282
283   if (!e_data.dummy_scatter) {
284     float scatter[4] = {0.0f, 0.0f, 0.0f, 0.0f};
285     float transmit[4] = {1.0f, 1.0f, 1.0f, 1.0f};
286     e_data.dummy_scatter = DRW_texture_create_3d(1, 1, 1, GPU_RGBA8, DRW_TEX_WRAP, scatter);
287     e_data.dummy_transmit = DRW_texture_create_3d(1, 1, 1, GPU_RGBA8, DRW_TEX_WRAP, transmit);
288   }
289 }
290
291 void EEVEE_volumes_cache_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
292 {
293   EEVEE_PassList *psl = vedata->psl;
294   EEVEE_StorageList *stl = vedata->stl;
295   EEVEE_EffectsInfo *effects = stl->effects;
296   EEVEE_CommonUniformBuffer *common_data = &sldata->common_data;
297
298   const DRWContextState *draw_ctx = DRW_context_state_get();
299   Scene *scene = draw_ctx->scene;
300   DRWShadingGroup *grp = NULL;
301
302   /* Shaders */
303   if (!e_data.scatter_sh) {
304     eevee_create_shader_volumes();
305   }
306
307   /* Quick breakdown of the Volumetric rendering:
308    *
309    * The rendering is separated in 4 stages:
310    *
311    * - Material Parameters : we collect volume properties of
312    *   all participating media in the scene and store them in
313    *   a 3D texture aligned with the 3D frustum.
314    *   This is done in 2 passes, one that clear the texture
315    *   and/or evaluate the world volumes, and the 2nd one that
316    *   additively render object volumes.
317    *
318    * - Light Scattering : the volume properties then are sampled
319    *   and light scattering is evaluated for each cell of the
320    *   volume texture. Temporal super-sampling (if enabled) occurs here.
321    *
322    * - Volume Integration : the scattered light and extinction is
323    *   integrated (accumulated) along the view-rays. The result is stored
324    *   for every cell in another texture.
325    *
326    * - Full-screen Resolve : From the previous stage, we get two
327    *   3D textures that contains integrated scattered light and extinction
328    *   for "every" positions in the frustum. We only need to sample
329    *   them and blend the scene color with those factors. This also
330    *   work for alpha blended materials.
331    */
332
333   /* World pass is not additive as it also clear the buffer. */
334   DRW_PASS_CREATE(psl->volumetric_world_ps, DRW_STATE_WRITE_COLOR);
335   DRW_PASS_CREATE(psl->volumetric_objects_ps, DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ADD);
336
337   /* World Volumetric */
338   struct World *wo = scene->world;
339   if (wo != NULL && wo->use_nodes && wo->nodetree &&
340       !LOOK_DEV_STUDIO_LIGHT_ENABLED(draw_ctx->v3d)) {
341     struct GPUMaterial *mat = EEVEE_material_world_volume_get(scene, wo);
342
343     if (GPU_material_use_domain_volume(mat)) {
344       grp = DRW_shgroup_material_create(mat, psl->volumetric_world_ps);
345     }
346
347     if (grp) {
348       DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
349       /* TODO (fclem): remove those (need to clean the GLSL files). */
350       DRW_shgroup_uniform_block(grp, "grid_block", sldata->grid_ubo);
351       DRW_shgroup_uniform_block(grp, "probe_block", sldata->probe_ubo);
352       DRW_shgroup_uniform_block(grp, "planar_block", sldata->planar_ubo);
353       DRW_shgroup_uniform_block(grp, "light_block", sldata->light_ubo);
354       DRW_shgroup_uniform_block(grp, "shadow_block", sldata->shadow_ubo);
355
356       /* Fix principle volumetric not working with world materials. */
357       DRW_shgroup_uniform_texture(grp, "sampdensity", e_data.dummy_density);
358       DRW_shgroup_uniform_texture(grp, "sampflame", e_data.dummy_flame);
359       DRW_shgroup_uniform_vec2_copy(grp, "unftemperature", (float[2]){0.0f, 1.0f});
360
361       DRW_shgroup_call_procedural_triangles(grp, NULL, common_data->vol_tex_size[2]);
362
363       effects->enabled_effects |= (EFFECT_VOLUMETRIC | EFFECT_POST_BUFFER);
364     }
365   }
366
367   if (grp == NULL) {
368     /* If no world or volume material is present just clear the buffer with this drawcall */
369     grp = DRW_shgroup_create(e_data.volumetric_clear_sh, psl->volumetric_world_ps);
370     DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
371
372     DRW_shgroup_call_procedural_triangles(grp, NULL, common_data->vol_tex_size[2]);
373   }
374 }
375
376 typedef struct EEVEE_InstanceVolumeMatrix {
377   DrawData dd;
378   float volume_mat[4][4];
379 } EEVEE_InstanceVolumeMatrix;
380
381 void EEVEE_volumes_cache_object_add(EEVEE_ViewLayerData *sldata,
382                                     EEVEE_Data *vedata,
383                                     Scene *scene,
384                                     Object *ob)
385 {
386   const DRWContextState *draw_ctx = DRW_context_state_get();
387   static float white[3] = {1.0f, 1.0f, 1.0f};
388
389   float *texcoloc = NULL;
390   float *texcosize = NULL;
391   struct ModifierData *md = NULL;
392   Material *ma = give_current_material(ob, 1);
393
394   if (ma == NULL) {
395     return;
396   }
397
398   struct GPUMaterial *mat = EEVEE_material_mesh_volume_get(scene, ma);
399   eGPUMaterialStatus status = GPU_material_status(mat);
400
401   if (status == GPU_MAT_QUEUED) {
402     vedata->stl->g_data->queued_shaders_count++;
403   }
404   /* If shader failed to compile or is currently compiling. */
405   if (status != GPU_MAT_SUCCESS) {
406     return;
407   }
408
409   DRWShadingGroup *grp = DRW_shgroup_material_create(mat, vedata->psl->volumetric_objects_ps);
410
411   BKE_mesh_texspace_get_reference((struct Mesh *)ob->data, NULL, &texcoloc, NULL, &texcosize);
412
413   /* TODO(fclem) remove those "unnecessary" UBOs */
414   DRW_shgroup_uniform_block(grp, "planar_block", sldata->planar_ubo);
415   DRW_shgroup_uniform_block(grp, "probe_block", sldata->probe_ubo);
416   DRW_shgroup_uniform_block(grp, "shadow_block", sldata->shadow_ubo);
417   DRW_shgroup_uniform_block(grp, "light_block", sldata->light_ubo);
418   DRW_shgroup_uniform_block(grp, "grid_block", sldata->grid_ubo);
419
420   DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
421   DRW_shgroup_uniform_vec3(grp, "volumeOrcoLoc", texcoloc, 1);
422   DRW_shgroup_uniform_vec3(grp, "volumeOrcoSize", texcosize, 1);
423
424   /* Smoke Simulation */
425   if (((ob->base_flag & BASE_FROM_DUPLI) == 0) &&
426       (md = modifiers_findByType(ob, eModifierType_Smoke)) &&
427       (modifier_isEnabled(scene, md, eModifierMode_Realtime)) &&
428       ((SmokeModifierData *)md)->domain != NULL) {
429     SmokeModifierData *smd = (SmokeModifierData *)md;
430     SmokeDomainSettings *sds = smd->domain;
431
432     /* Don't show smoke before simulation starts, this could be made an option in the future. */
433     const bool show_smoke = ((int)DEG_get_ctime(draw_ctx->depsgraph) >=
434                              sds->point_cache[0]->startframe);
435
436     if (sds->fluid && show_smoke) {
437       const bool show_highres = BKE_smoke_show_highres(scene, sds);
438       if (!sds->wt || !show_highres) {
439         GPU_create_smoke(smd, 0);
440       }
441       else if (sds->wt && show_highres) {
442         GPU_create_smoke(smd, 1);
443       }
444       BLI_addtail(&e_data.smoke_domains, BLI_genericNodeN(smd));
445     }
446
447     DRW_shgroup_uniform_texture_ref(
448         grp, "sampdensity", sds->tex ? &sds->tex : &e_data.dummy_density);
449     DRW_shgroup_uniform_texture_ref(
450         grp, "sampflame", sds->tex_flame ? &sds->tex_flame : &e_data.dummy_flame);
451
452     /* Constant Volume color. */
453     bool use_constant_color = ((sds->active_fields & SM_ACTIVE_COLORS) == 0 &&
454                                (sds->active_fields & SM_ACTIVE_COLOR_SET) != 0);
455
456     DRW_shgroup_uniform_vec3(
457         grp, "volumeColor", (use_constant_color) ? sds->active_color : white, 1);
458
459     /* Output is such that 0..1 maps to 0..1000K */
460     DRW_shgroup_uniform_vec2(grp, "unftemperature", &sds->flame_ignition, 1);
461   }
462   else {
463     DRW_shgroup_uniform_texture(grp, "sampdensity", e_data.dummy_density);
464     DRW_shgroup_uniform_texture(grp, "sampflame", e_data.dummy_flame);
465     DRW_shgroup_uniform_vec3(grp, "volumeColor", white, 1);
466     DRW_shgroup_uniform_vec2(grp, "unftemperature", (float[2]){0.0f, 1.0f}, 1);
467   }
468
469   /* TODO Reduce to number of slices intersecting. */
470   /* TODO Preemptive culling. */
471   DRW_shgroup_call_procedural_triangles(grp, ob, sldata->common_data.vol_tex_size[2]);
472
473   vedata->stl->effects->enabled_effects |= (EFFECT_VOLUMETRIC | EFFECT_POST_BUFFER);
474 }
475
476 void EEVEE_volumes_cache_finish(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
477 {
478   EEVEE_PassList *psl = vedata->psl;
479   EEVEE_TextureList *txl = vedata->txl;
480   EEVEE_EffectsInfo *effects = vedata->stl->effects;
481   LightCache *lcache = vedata->stl->g_data->light_cache;
482   EEVEE_CommonUniformBuffer *common_data = &sldata->common_data;
483
484   if ((effects->enabled_effects & EFFECT_VOLUMETRIC) != 0) {
485     DRWShadingGroup *grp;
486     struct GPUShader *sh;
487
488     DRW_PASS_CREATE(psl->volumetric_scatter_ps, DRW_STATE_WRITE_COLOR);
489     sh = (common_data->vol_use_lights) ? e_data.scatter_with_lights_sh : e_data.scatter_sh;
490     grp = DRW_shgroup_create(sh, psl->volumetric_scatter_ps);
491     DRW_shgroup_uniform_texture_ref(grp, "irradianceGrid", &lcache->grid_tx.tex);
492     DRW_shgroup_uniform_texture_ref(grp, "shadowCubeTexture", &sldata->shadow_cube_pool);
493     DRW_shgroup_uniform_texture_ref(grp, "shadowCascadeTexture", &sldata->shadow_cascade_pool);
494     DRW_shgroup_uniform_texture_ref(grp, "volumeScattering", &txl->volume_prop_scattering);
495     DRW_shgroup_uniform_texture_ref(grp, "volumeExtinction", &txl->volume_prop_extinction);
496     DRW_shgroup_uniform_texture_ref(grp, "volumeEmission", &txl->volume_prop_emission);
497     DRW_shgroup_uniform_texture_ref(grp, "volumePhase", &txl->volume_prop_phase);
498     DRW_shgroup_uniform_texture_ref(grp, "historyScattering", &txl->volume_scatter_history);
499     DRW_shgroup_uniform_texture_ref(grp, "historyTransmittance", &txl->volume_transmit_history);
500     DRW_shgroup_uniform_block(grp, "light_block", sldata->light_ubo);
501     DRW_shgroup_uniform_block(grp, "shadow_block", sldata->shadow_ubo);
502     DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
503
504     DRW_shgroup_call_procedural_triangles(grp, NULL, common_data->vol_tex_size[2]);
505
506     DRW_PASS_CREATE(psl->volumetric_integration_ps, DRW_STATE_WRITE_COLOR);
507     grp = DRW_shgroup_create(e_data.volumetric_integration_sh, psl->volumetric_integration_ps);
508     DRW_shgroup_uniform_texture_ref(grp, "volumeScattering", &txl->volume_scatter);
509     DRW_shgroup_uniform_texture_ref(grp, "volumeExtinction", &txl->volume_transmit);
510     DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
511
512     DRW_shgroup_call_procedural_triangles(grp, NULL, common_data->vol_tex_size[2]);
513
514     DRW_PASS_CREATE(psl->volumetric_resolve_ps, DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_CUSTOM);
515     grp = DRW_shgroup_create(e_data.volumetric_resolve_sh, psl->volumetric_resolve_ps);
516     DRW_shgroup_uniform_texture_ref(grp, "inScattering", &txl->volume_scatter);
517     DRW_shgroup_uniform_texture_ref(grp, "inTransmittance", &txl->volume_transmit);
518     DRW_shgroup_uniform_texture_ref(grp, "inSceneDepth", &e_data.depth_src);
519     DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
520
521     DRW_shgroup_call_procedural_triangles(grp, NULL, 1);
522   }
523 }
524
525 void EEVEE_volumes_draw_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
526 {
527   EEVEE_FramebufferList *fbl = vedata->fbl;
528   EEVEE_TextureList *txl = vedata->txl;
529   EEVEE_EffectsInfo *effects = vedata->stl->effects;
530   EEVEE_CommonUniformBuffer *common_data = &sldata->common_data;
531
532   if ((effects->enabled_effects & EFFECT_VOLUMETRIC) != 0) {
533     int *tex_size = common_data->vol_tex_size;
534
535     if (txl->volume_prop_scattering == NULL) {
536       /* Volume properties: We evaluate all volumetric objects
537        * and store their final properties into each froxel */
538       txl->volume_prop_scattering = DRW_texture_create_3d(
539           tex_size[0], tex_size[1], tex_size[2], GPU_R11F_G11F_B10F, DRW_TEX_FILTER, NULL);
540       txl->volume_prop_extinction = DRW_texture_create_3d(
541           tex_size[0], tex_size[1], tex_size[2], GPU_R11F_G11F_B10F, DRW_TEX_FILTER, NULL);
542       txl->volume_prop_emission = DRW_texture_create_3d(
543           tex_size[0], tex_size[1], tex_size[2], GPU_R11F_G11F_B10F, DRW_TEX_FILTER, NULL);
544       txl->volume_prop_phase = DRW_texture_create_3d(
545           tex_size[0], tex_size[1], tex_size[2], GPU_RG16F, DRW_TEX_FILTER, NULL);
546
547       /* Volume scattering: We compute for each froxel the
548        * Scattered light towards the view. We also resolve temporal
549        * super sampling during this stage. */
550       txl->volume_scatter = DRW_texture_create_3d(
551           tex_size[0], tex_size[1], tex_size[2], GPU_R11F_G11F_B10F, DRW_TEX_FILTER, NULL);
552       txl->volume_transmit = DRW_texture_create_3d(
553           tex_size[0], tex_size[1], tex_size[2], GPU_R11F_G11F_B10F, DRW_TEX_FILTER, NULL);
554
555       /* Final integration: We compute for each froxel the
556        * amount of scattered light and extinction coef at this
557        * given depth. We use these textures as double buffer
558        * for the volumetric history. */
559       txl->volume_scatter_history = DRW_texture_create_3d(
560           tex_size[0], tex_size[1], tex_size[2], GPU_R11F_G11F_B10F, DRW_TEX_FILTER, NULL);
561       txl->volume_transmit_history = DRW_texture_create_3d(
562           tex_size[0], tex_size[1], tex_size[2], GPU_R11F_G11F_B10F, DRW_TEX_FILTER, NULL);
563     }
564
565     GPU_framebuffer_ensure_config(&fbl->volumetric_fb,
566                                   {GPU_ATTACHMENT_NONE,
567                                    GPU_ATTACHMENT_TEXTURE(txl->volume_prop_scattering),
568                                    GPU_ATTACHMENT_TEXTURE(txl->volume_prop_extinction),
569                                    GPU_ATTACHMENT_TEXTURE(txl->volume_prop_emission),
570                                    GPU_ATTACHMENT_TEXTURE(txl->volume_prop_phase)});
571     GPU_framebuffer_ensure_config(&fbl->volumetric_scat_fb,
572                                   {GPU_ATTACHMENT_NONE,
573                                    GPU_ATTACHMENT_TEXTURE(txl->volume_scatter),
574                                    GPU_ATTACHMENT_TEXTURE(txl->volume_transmit)});
575     GPU_framebuffer_ensure_config(&fbl->volumetric_integ_fb,
576                                   {GPU_ATTACHMENT_NONE,
577                                    GPU_ATTACHMENT_TEXTURE(txl->volume_scatter_history),
578                                    GPU_ATTACHMENT_TEXTURE(txl->volume_transmit_history)});
579   }
580   else {
581     DRW_TEXTURE_FREE_SAFE(txl->volume_prop_scattering);
582     DRW_TEXTURE_FREE_SAFE(txl->volume_prop_extinction);
583     DRW_TEXTURE_FREE_SAFE(txl->volume_prop_emission);
584     DRW_TEXTURE_FREE_SAFE(txl->volume_prop_phase);
585     DRW_TEXTURE_FREE_SAFE(txl->volume_scatter);
586     DRW_TEXTURE_FREE_SAFE(txl->volume_transmit);
587     DRW_TEXTURE_FREE_SAFE(txl->volume_scatter_history);
588     DRW_TEXTURE_FREE_SAFE(txl->volume_transmit_history);
589     GPU_FRAMEBUFFER_FREE_SAFE(fbl->volumetric_fb);
590     GPU_FRAMEBUFFER_FREE_SAFE(fbl->volumetric_scat_fb);
591     GPU_FRAMEBUFFER_FREE_SAFE(fbl->volumetric_integ_fb);
592   }
593
594   effects->volume_scatter = e_data.dummy_scatter;
595   effects->volume_transmit = e_data.dummy_transmit;
596 }
597
598 void EEVEE_volumes_compute(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
599 {
600   EEVEE_PassList *psl = vedata->psl;
601   EEVEE_TextureList *txl = vedata->txl;
602   EEVEE_FramebufferList *fbl = vedata->fbl;
603   EEVEE_StorageList *stl = vedata->stl;
604   EEVEE_EffectsInfo *effects = stl->effects;
605   if ((effects->enabled_effects & EFFECT_VOLUMETRIC) != 0) {
606     DRW_stats_group_start("Volumetrics");
607
608     GPU_framebuffer_bind(fbl->volumetric_fb);
609     DRW_draw_pass(psl->volumetric_world_ps);
610     DRW_draw_pass(psl->volumetric_objects_ps);
611
612     GPU_framebuffer_bind(fbl->volumetric_scat_fb);
613     DRW_draw_pass(psl->volumetric_scatter_ps);
614
615     GPU_framebuffer_bind(fbl->volumetric_integ_fb);
616     DRW_draw_pass(psl->volumetric_integration_ps);
617
618     SWAP(struct GPUFrameBuffer *, fbl->volumetric_scat_fb, fbl->volumetric_integ_fb);
619     SWAP(GPUTexture *, txl->volume_scatter, txl->volume_scatter_history);
620     SWAP(GPUTexture *, txl->volume_transmit, txl->volume_transmit_history);
621
622     effects->volume_scatter = txl->volume_scatter;
623     effects->volume_transmit = txl->volume_transmit;
624
625     /* Restore */
626     GPU_framebuffer_bind(fbl->main_fb);
627
628     DRW_stats_group_end();
629   }
630 }
631
632 void EEVEE_volumes_resolve(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
633 {
634   EEVEE_PassList *psl = vedata->psl;
635   EEVEE_FramebufferList *fbl = vedata->fbl;
636   EEVEE_StorageList *stl = vedata->stl;
637   EEVEE_EffectsInfo *effects = stl->effects;
638
639   if ((effects->enabled_effects & EFFECT_VOLUMETRIC) != 0) {
640     DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
641     e_data.depth_src = dtxl->depth;
642
643     /* Apply for opaque geometry. */
644     GPU_framebuffer_bind(fbl->main_color_fb);
645     DRW_draw_pass(psl->volumetric_resolve_ps);
646
647     /* Restore. */
648     GPU_framebuffer_bind(fbl->main_fb);
649   }
650 }
651
652 void EEVEE_volumes_free_smoke_textures(void)
653 {
654   /* Free Smoke Textures after rendering */
655   for (LinkData *link = e_data.smoke_domains.first; link; link = link->next) {
656     SmokeModifierData *smd = (SmokeModifierData *)link->data;
657     GPU_free_smoke(smd);
658   }
659   BLI_freelistN(&e_data.smoke_domains);
660 }
661
662 void EEVEE_volumes_free(void)
663 {
664   MEM_SAFE_FREE(e_data.volumetric_common_lib);
665   MEM_SAFE_FREE(e_data.volumetric_common_lights_lib);
666
667   DRW_TEXTURE_FREE_SAFE(e_data.dummy_scatter);
668   DRW_TEXTURE_FREE_SAFE(e_data.dummy_transmit);
669
670   DRW_TEXTURE_FREE_SAFE(e_data.dummy_density);
671   DRW_TEXTURE_FREE_SAFE(e_data.dummy_flame);
672
673   DRW_SHADER_FREE_SAFE(e_data.volumetric_clear_sh);
674   DRW_SHADER_FREE_SAFE(e_data.scatter_sh);
675   DRW_SHADER_FREE_SAFE(e_data.scatter_with_lights_sh);
676   DRW_SHADER_FREE_SAFE(e_data.volumetric_integration_sh);
677   DRW_SHADER_FREE_SAFE(e_data.volumetric_resolve_sh);
678 }