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