Merge branch 'master' into blender2.8
[blender.git] / source / blender / draw / engines / workbench / workbench_volume.c
1 /*
2  * Copyright 2018, 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 workbench_volume.c
23  *  \ingroup draw_engine
24  */
25
26 #include "workbench_private.h"
27
28 #include "BKE_modifier.h"
29 #include "BKE_object.h"
30
31 #include "BLI_rand.h"
32 #include "BLI_dynstr.h"
33
34 #include "DNA_modifier_types.h"
35 #include "DNA_object_force_types.h"
36 #include "DNA_smoke_types.h"
37
38 #include "GPU_draw.h"
39
40 enum {
41         VOLUME_SH_SLICE = 0,
42         VOLUME_SH_COBA,
43         VOLUME_SH_CUBIC,
44 };
45
46 #define VOLUME_SH_MAX (1 << (VOLUME_SH_CUBIC + 1))
47
48 static struct {
49         struct GPUShader *volume_sh[VOLUME_SH_MAX];
50         struct GPUShader *volume_coba_sh;
51         struct GPUShader *volume_slice_sh;
52         struct GPUShader *volume_slice_coba_sh;
53         struct GPUTexture *dummy_tex;
54         struct GPUTexture *dummy_coba_tex;
55 } e_data = {NULL};
56
57 extern char datatoc_workbench_volume_vert_glsl[];
58 extern char datatoc_workbench_volume_frag_glsl[];
59
60 static GPUShader *volume_shader_get(bool slice, bool coba, bool cubic)
61 {
62         int id = 0;
63         id += (slice) ? (1 << VOLUME_SH_SLICE) : 0;
64         id += (coba) ? (1 << VOLUME_SH_COBA) : 0;
65         id += (cubic) ? (1 << VOLUME_SH_CUBIC) : 0;
66
67         if (!e_data.volume_sh[id]) {
68                 DynStr *ds = BLI_dynstr_new();
69
70                 if (slice) {
71                         BLI_dynstr_append(ds, "#define VOLUME_SLICE\n");
72                 }
73                 if (coba) {
74                         BLI_dynstr_append(ds, "#define USE_COBA\n");
75                 }
76                 if (cubic) {
77                         BLI_dynstr_append(ds, "#define USE_TRICUBIC\n");
78                 }
79
80                 char *defines = BLI_dynstr_get_cstring(ds);
81                 BLI_dynstr_free(ds);
82
83                 e_data.volume_sh[id] = DRW_shader_create(
84                         datatoc_workbench_volume_vert_glsl, NULL,
85                         datatoc_workbench_volume_frag_glsl,
86                         defines);
87
88                 MEM_freeN(defines);
89         }
90
91         return e_data.volume_sh[id];
92 }
93
94 void workbench_volume_engine_init(void)
95 {
96         if (!e_data.dummy_tex) {
97                 float pixel[4] = {0.0f, 0.0f, 0.0f, 0.0f};
98                 e_data.dummy_tex = GPU_texture_create_3D(1, 1, 1, GPU_RGBA8, pixel, NULL);
99                 e_data.dummy_coba_tex = GPU_texture_create_1D(1, GPU_RGBA8, pixel, NULL);
100         }
101 }
102
103 void workbench_volume_engine_free(void)
104 {
105         for (int i = 0; i < VOLUME_SH_MAX; ++i) {
106                 DRW_SHADER_FREE_SAFE(e_data.volume_sh[i]);
107         }
108         DRW_TEXTURE_FREE_SAFE(e_data.dummy_tex);
109         DRW_TEXTURE_FREE_SAFE(e_data.dummy_coba_tex);
110 }
111
112 void workbench_volume_cache_init(WORKBENCH_Data *vedata)
113 {
114         vedata->psl->volume_pass = DRW_pass_create("Volumes", DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_PREMUL | DRW_STATE_CULL_FRONT);
115 }
116
117 void workbench_volume_cache_populate(WORKBENCH_Data *vedata, Scene *scene, Object *ob, ModifierData *md)
118 {
119         SmokeModifierData *smd = (SmokeModifierData *)md;
120         SmokeDomainSettings *sds = smd->domain;
121         WORKBENCH_PrivateData *wpd = vedata->stl->g_data;
122         WORKBENCH_EffectInfo *effect_info = vedata->stl->effects;
123         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
124         DRWShadingGroup *grp = NULL;
125
126         /* Don't show smoke before simulation starts, this could be made an option in the future. */
127         if (!sds->fluid || CFRA < sds->point_cache[0]->startframe) {
128                 return;
129         }
130
131         wpd->volumes_do = true;
132         if (sds->use_coba) {
133                 GPU_create_smoke_coba_field(smd);
134         }
135         else if (!sds->wt || !(sds->viewsettings & MOD_SMOKE_VIEW_SHOWBIG)) {
136                 GPU_create_smoke(smd, 0);
137         }
138         else if (sds->wt && (sds->viewsettings & MOD_SMOKE_VIEW_SHOWBIG)) {
139                 GPU_create_smoke(smd, 1);
140         }
141
142         if ((!sds->use_coba && sds->tex == NULL) ||
143             (sds->use_coba && sds->tex_field == NULL))
144         {
145                 return;
146         }
147
148         const bool use_slice = (sds->slice_method == MOD_SMOKE_SLICE_AXIS_ALIGNED &&
149                                 sds->axis_slice_method == AXIS_SLICE_SINGLE);
150         const bool cubic_interp = (sds->interp_method == VOLUME_INTERP_CUBIC);
151         GPUShader *sh = volume_shader_get(use_slice, sds->use_coba, cubic_interp);
152
153         if (use_slice) {
154                 float invviewmat[4][4];
155                 DRW_viewport_matrix_get(invviewmat, DRW_MAT_VIEWINV);
156
157                 const int axis = (sds->slice_axis == SLICE_AXIS_AUTO)
158                                   ? axis_dominant_v3_single(invviewmat[2])
159                                   : sds->slice_axis - 1;
160                 float dim[3];
161                 BKE_object_dimensions_get(ob, dim);
162                 /* 0.05f to acheive somewhat the same opacity as the full view.  */
163                 float step_length = max_ff(1e-16f, dim[axis] * 0.05f);
164
165                 grp = DRW_shgroup_create(sh, vedata->psl->volume_pass);
166                 DRW_shgroup_uniform_float_copy(grp, "slicePosition", sds->slice_depth);
167                 DRW_shgroup_uniform_int_copy(grp, "sliceAxis", axis);
168                 DRW_shgroup_uniform_float_copy(grp, "stepLength", step_length);
169                 DRW_shgroup_state_disable(grp, DRW_STATE_CULL_FRONT);
170         }
171         else {
172                 double noise_ofs;
173                 BLI_halton_1D(3, 0.0, effect_info->jitter_index, &noise_ofs);
174                 float dim[3], step_length, max_slice;
175                 float slice_ct[3] = {sds->res[0], sds->res[1], sds->res[2]};
176                 mul_v3_fl(slice_ct, max_ff(0.001f, sds->slice_per_voxel));
177                 max_slice = max_fff(slice_ct[0], slice_ct[1], slice_ct[2]);
178                 BKE_object_dimensions_get(ob, dim);
179                 invert_v3(slice_ct);
180                 mul_v3_v3(dim, slice_ct);
181                 step_length = len_v3(dim);
182
183                 grp = DRW_shgroup_create(sh, vedata->psl->volume_pass);
184                 DRW_shgroup_uniform_vec4(grp, "viewvecs[0]", (float *)wpd->viewvecs, 3);
185                 DRW_shgroup_uniform_int_copy(grp, "samplesLen", max_slice);
186                 DRW_shgroup_uniform_float_copy(grp, "stepLength", step_length);
187                 DRW_shgroup_uniform_float_copy(grp, "noiseOfs", noise_ofs);
188                 DRW_shgroup_state_enable(grp, DRW_STATE_CULL_FRONT);
189         }
190
191         if (sds->use_coba) {
192                 DRW_shgroup_uniform_texture(grp, "densityTexture", sds->tex_field);
193                 DRW_shgroup_uniform_texture(grp, "transferTexture", sds->tex_coba);
194         }
195         else {
196                 DRW_shgroup_uniform_texture(grp, "densityTexture", sds->tex);
197                 DRW_shgroup_uniform_texture(grp, "shadowTexture", sds->tex_shadow);
198                 DRW_shgroup_uniform_texture(grp, "flameTexture", (sds->tex_flame) ? sds->tex_flame : e_data.dummy_tex);
199                 DRW_shgroup_uniform_texture(grp, "flameColorTexture", (sds->tex_flame) ? sds->tex_flame_coba : e_data.dummy_coba_tex);
200         }
201         DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
202         DRW_shgroup_uniform_float_copy(grp, "densityScale", 10.0f * sds->display_thickness);
203
204         if (use_slice) {
205                 DRW_shgroup_call_object_add(grp, DRW_cache_quad_get(), ob);
206         }
207         else {
208                 DRW_shgroup_call_object_add(grp, DRW_cache_cube_get(), ob);
209         }
210
211         BLI_addtail(&wpd->smoke_domains, BLI_genericNodeN(smd));
212 }
213
214 void workbench_volume_smoke_textures_free(WORKBENCH_PrivateData *wpd)
215 {
216         /* Free Smoke Textures after rendering */
217         /* XXX This is a waste of processing and GPU bandwidth if nothing
218          * is updated. But the problem is since Textures are stored in the
219          * modifier we don't want them to take precious VRAM if the
220          * modifier is not used for display. We should share them for
221          * all viewport in a redraw at least. */
222         for (LinkData *link = wpd->smoke_domains.first; link; link = link->next) {
223                 SmokeModifierData *smd = (SmokeModifierData *)link->data;
224                 GPU_free_smoke(smd);
225         }
226         BLI_freelistN(&wpd->smoke_domains);
227 }