Blender V5.3
workbench_volume.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "draw_cache.hh"
10#include "draw_common_c.hh"
11
12#include "workbench_private.hh"
13
14#include "BKE_volume.hh"
15#include "BKE_volume_render.hh"
16#include "BLI_math_geom_c.hh"
17#include "BLI_rand_c.hh"
18#include "DNA_fluid_types.h"
19#include "DNA_modifier_types.h"
20
21namespace blender::workbench {
22
24{
25 active_ = false;
26 ps_.init();
27 /* Workaround limitation of PassSortable. Use dummy pass that will be sorted first in all
28 * circumstances. */
29 ps_.sub("Resource Bindings", FLT_MIN).bind_ubo(WB_WORLD_SLOT, resources.world_buf);
30
31 dummy_shadow_tx_.ensure_3d(
32 gpu::TextureFormat::UNORM_8_8_8_8, int3(1), GPU_TEXTURE_USAGE_SHADER_READ, float4(1));
33 dummy_volume_tx_.ensure_3d(
34 gpu::TextureFormat::UNORM_8_8_8_8, int3(1), GPU_TEXTURE_USAGE_SHADER_READ, float4(0));
35 dummy_coba_tx_.ensure_1d(
36 gpu::TextureFormat::UNORM_8_8_8_8, 1, GPU_TEXTURE_USAGE_SHADER_READ, float4(0));
37 dummy_flag_tx_.ensure_3d(gpu::TextureFormat::UINT_8, int3(1), GPU_TEXTURE_USAGE_SHADER_READ);
38}
39
41 SceneResources &resources,
42 const SceneState &scene_state,
43 ObjectRef &ob_ref,
45{
46 Object *ob = ob_ref.object;
47 /* Create 3D textures. */
49 BKE_volume_load(&volume, G.main);
50 const bke::VolumeGridData *volume_grid = BKE_volume_grid_active_get_for_read(&volume);
51 if (volume_grid == nullptr) {
52 return;
53 }
54
56 if (grid == nullptr) {
57 return;
58 }
59
60 active_ = true;
61
62 PassSortable::Sub &sub_ps = ps_.sub(
63 "Volume Object SubPass", dot(ob_ref.object_to_world().location(), scene_state.view_forward));
64
65 const bool use_slice = (volume.display.axis_slice_method == VOLUME_AXIS_SLICE_SINGLE);
66
67 sub_ps.shader_set(
68 ShaderCache::get().volume_get(false, volume.display.interpolation_method, false, use_slice));
69 sub_ps.push_constant("do_depth_test", scene_state.shading.type >= OB_SOLID);
70
71 const float density_fac = volume.display.density *
72 BKE_volume_density_scale(&volume, ob->object_to_world().ptr());
73
74 sub_ps.bind_texture("depth_buffer", &resources.depth_tx);
75 sub_ps.bind_texture("stencil_tx", &stencil_tx_);
76 sub_ps.bind_texture("density_tx", grid->texture);
77 /* TODO: implement shadow texture, see manta_smoke_calc_transparency. */
78 sub_ps.bind_texture("shadow_tx", dummy_shadow_tx_);
79 sub_ps.push_constant("active_color", color);
80 sub_ps.push_constant("density_fac", density_fac);
81 sub_ps.push_constant("volume_object_to_texture", float4x4(grid->object_to_texture));
82 sub_ps.push_constant("volume_texture_to_object", float4x4(grid->texture_to_object));
83
84 if (use_slice) {
85 draw_slice_ps(
86 manager, resources, sub_ps, ob_ref, volume.display.slice_axis, volume.display.slice_depth);
87 }
88 else {
89 float4x4 texture_to_world = ob->object_to_world() * float4x4(grid->texture_to_object);
90 float3 world_size = math::to_scale(texture_to_world);
91
92 int3 resolution;
93 GPU_texture_get_mipmap_size(grid->texture, 0, resolution);
94 float3 slice_count = float3(resolution) * 5.0f;
95
96 draw_volume_ps(
97 manager, resources, sub_ps, ob_ref, scene_state.sample, slice_count, world_size);
98 }
99}
100
102 SceneResources &resources,
103 const SceneState &scene_state,
104 ObjectRef &ob_ref,
105 ModifierData *md)
106{
107 Object *ob = ob_ref.object;
108
109 FluidModifierData *modifier = reinterpret_cast<FluidModifierData *>(md);
110 FluidDomainSettings &settings = *modifier->domain;
111
112 if (!settings.fluid) {
113 return;
114 }
115
116 bool can_load = false;
117 if (settings.use_coba) {
119 can_load = settings.tex_field != nullptr;
120 }
121 else if (settings.type == FLUID_DOMAIN_TYPE_GAS) {
123 can_load = settings.tex_density != nullptr || settings.tex_color != nullptr;
124 }
125
126 if (!can_load) {
127 return;
128 }
129
130 active_ = true;
131
132 PassSortable::Sub &sub_ps = ps_.sub(
133 "Volume Modifier SubPass",
134 dot(ob_ref.object_to_world().location(), scene_state.view_forward));
135
136 const bool use_slice = settings.axis_slice_method == AXIS_SLICE_SINGLE;
137
138 sub_ps.shader_set(
139 ShaderCache::get().volume_get(true, settings.interp_method, settings.use_coba, use_slice));
140 sub_ps.push_constant("do_depth_test", scene_state.shading.type >= OB_SOLID);
141 sub_ps.bind_texture("flame_tx", settings.tex_flame ? settings.tex_flame : dummy_volume_tx_);
142 sub_ps.bind_texture("flame_color_tx",
143 settings.tex_flame ? settings.tex_flame_coba : dummy_coba_tx_);
144
145 if (settings.use_coba) {
146 const bool show_flags = settings.coba_field == FLUID_DOMAIN_FIELD_FLAGS;
147 const bool show_pressure = settings.coba_field == FLUID_DOMAIN_FIELD_PRESSURE;
148 const bool show_phi = ELEM(settings.coba_field,
153
154 sub_ps.push_constant("show_flags", show_flags);
155 sub_ps.push_constant("show_pressure", show_pressure);
156 sub_ps.push_constant("show_phi", show_phi);
157 sub_ps.push_constant("grid_scale", settings.grid_scale);
158
159 if (show_flags) {
160 sub_ps.bind_texture("flag_tx", settings.tex_field);
161 sub_ps.bind_texture("density_tx", dummy_volume_tx_);
162 }
163 else {
164 sub_ps.bind_texture("flag_tx", dummy_flag_tx_);
165 sub_ps.bind_texture("density_tx", settings.tex_field);
166 }
167
168 if (!show_flags && !show_pressure && !show_phi) {
169 sub_ps.bind_texture("transfer_tx", settings.tex_coba);
170 }
171 else {
172 sub_ps.bind_texture("transfer_tx", dummy_coba_tx_);
173 }
174 }
175 else {
176 bool use_constant_color = ((settings.active_fields & FLUID_DOMAIN_ACTIVE_COLORS) == 0 &&
178
179 sub_ps.push_constant("active_color",
180 use_constant_color ? float3(settings.active_color) : float3(1));
181
182 sub_ps.bind_texture("density_tx",
183 settings.tex_color ? settings.tex_color : settings.tex_density);
184 sub_ps.bind_texture("shadow_tx", settings.tex_shadow);
185 }
186
187 sub_ps.push_constant("density_fac", 10.0f * settings.display_thickness);
188 sub_ps.bind_texture("depth_buffer", &resources.depth_tx);
189 sub_ps.bind_texture("stencil_tx", &stencil_tx_);
190
191 if (use_slice) {
192 draw_slice_ps(manager, resources, sub_ps, ob_ref, settings.slice_axis, settings.slice_depth);
193 }
194 else {
195 float3 world_size;
196 BKE_object_dimensions_get(ob, world_size);
197
198 float3 slice_count = float3(settings.res) * std::max(0.001f, settings.slice_per_voxel);
199
200 draw_volume_ps(
201 manager, resources, sub_ps, ob_ref, scene_state.sample, slice_count, world_size);
202 }
203}
204
205void VolumePass::draw(Manager &manager, View &view, SceneResources &resources)
206{
207 if (!active_) {
208 return;
209 }
210
211 stencil_tx_ = resources.depth_tx.ptr()->stencil_view();
212
214 fb_.bind();
215 manager.submit(ps_, view);
216}
217
218void VolumePass::draw_slice_ps(Manager &manager,
219 SceneResources &resources,
220 PassSortable::Sub &ps,
221 ObjectRef &ob_ref,
222 int slice_axis_enum,
223 float slice_depth)
224{
225 float4x4 view_mat_inv = draw::View::default_get().viewinv();
226
227 const int axis = (slice_axis_enum == SLICE_AXIS_AUTO) ?
228 axis_dominant_v3_single(view_mat_inv[2]) :
229 slice_axis_enum - 1;
230
231 float3 dimensions;
232 BKE_object_dimensions_get(ob_ref.object, dimensions);
233 /* 0.05f to achieve somewhat the same opacity as the full view. */
234 float step_length = std::max(1e-16f, dimensions[axis] * 0.05f);
235
237 ps.push_constant("slice_position", slice_depth);
238 ps.push_constant("slice_axis", axis);
239 ps.push_constant("step_length", step_length);
240
241 ps.draw(resources.volume_cube_batch, manager.unique_handle(ob_ref));
242}
243
244void VolumePass::draw_volume_ps(Manager &manager,
245 SceneResources &resources,
246 PassSortable::Sub &ps,
247 ObjectRef &ob_ref,
248 int taa_sample,
249 float3 slice_count,
250 float3 world_size)
251{
252 double noise_offset;
253 BLI_halton_1d(3, 0.0, taa_sample, &noise_offset);
254
255 int max_slice = std::max({UNPACK3(slice_count)});
256 float step_length = math::length((1.0f / slice_count) * world_size);
257
259 ps.push_constant("samples_len", max_slice);
260 ps.push_constant("step_length", step_length);
261 ps.push_constant("noise_ofs", float(noise_offset));
262
263 ps.draw(resources.volume_cube_batch, manager.unique_handle(ob_ref));
264}
265
266} // namespace blender::workbench
Volume data-block.
Volume data-block rendering and viewport drawing utilities.
Random number functions.
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_NONE
#define UNPACK3(a)
#define ELEM(...)
ResourceHandleRange unique_handle(const ObjectRef &ref)
void submit(PassSimple &pass, View &view)
float4x4 object_to_world(int instance_index) const
gpu::Texture * stencil_view(bool cube_as_array=false)
static View & default_get()
Definition draw_view.cc:317
const float4x4 & viewinv(int view_id=0) const
Definition draw_view.hh:142
void sync(SceneResources &resources)
void object_sync_volume(Manager &manager, SceneResources &resources, const SceneState &scene_state, ObjectRef &ob_ref, float3 color)
void draw(Manager &manager, View &view, SceneResources &resources)
void object_sync_modifier(Manager &manager, SceneResources &resources, const SceneState &scene_state, ObjectRef &ob_ref, ModifierData *md)
#define FLT_MIN
float dot(VecOp< float, D >, VecOp< float, D >) RET
DRWVolumeGrid * DRW_volume_batch_cache_get_grid(Volume *volume, const bke::VolumeGridData *volume_grid)
T length(const VecBase< T, Size > &a)
VecBase< T, 3 > to_scale(const MatBase< T, NumCol, NumRow > &mat)
@ FLUID_DOMAIN_FIELD_FLAGS
@ FLUID_DOMAIN_FIELD_PHI_OBSTACLE
@ FLUID_DOMAIN_FIELD_PHI_IN
@ FLUID_DOMAIN_FIELD_PRESSURE
@ FLUID_DOMAIN_FIELD_PHI_OUT
@ FLUID_DOMAIN_FIELD_PHI
@ VOLUME_AXIS_SLICE_SINGLE
void DRW_smoke_ensure_coba_field(FluidModifierData *fmd)
MatBase< float, 4, 4 > float4x4
@ FLUID_DOMAIN_USE_NOISE
@ FLUID_DOMAIN_ACTIVE_COLORS
@ FLUID_DOMAIN_ACTIVE_COLOR_SET
Global G
Definition blender.cc:57
VecBase< float, 4 > float4
void BLI_halton_1d(unsigned int prime, double offset, int n, double *r)
Definition rand.cc:228
VecBase< int32_t, 3 > int3
@ GPU_TEXTURE_USAGE_SHADER_READ
T & DRW_object_get_data_for_drawing(const Object &object)
MINLINE int axis_dominant_v3_single(const float vec[3])
void BKE_object_dimensions_get(const Object *ob, float r_vec[3])
Definition object.cc:3799
bool BKE_volume_load(const Volume *volume, const Main *bmain)
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_BLEND_ALPHA_PREMUL
Definition draw_state.hh:59
@ DRW_STATE_CULL_FRONT
Definition draw_state.hh:46
void DRW_smoke_ensure(FluidModifierData *fmd, int highres)
const bke::VolumeGridData * BKE_volume_grid_active_get_for_read(const Volume *volume)
float BKE_volume_density_scale(const Volume *volume, const float matrix[4][4])
VecBase< float, 3 > float3
void GPU_texture_get_mipmap_size(gpu::Texture *texture, int mip_level, int *r_size)
@ FLUID_DOMAIN_TYPE_GAS
FLUID_DisplayInterpolationMethod interp_method
eFluidDomain_AxisSliceMethod axis_slice_method
eFluidDomain_CobaField coba_field
eFluidDomain_ActiveFields active_fields
eFluidDomain_SliceAxis slice_axis
VolumeDisplayInterpMethod interpolation_method
AxisAlignedSlicingMethod axis_slice_method
VolumeDisplay display
UniformBuffer< WorldData > world_buf
#define WB_WORLD_SLOT