Blender V5.0
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
5#include "draw_cache.hh"
6#include "draw_common_c.hh"
7
9
10#include "BKE_volume.hh"
11#include "BKE_volume_render.hh"
12#include "BLI_math_geom.h"
13#include "BLI_rand.h"
14#include "DNA_fluid_types.h"
15#include "DNA_modifier_types.h"
16
17namespace blender::workbench {
18
20{
21 active_ = false;
22 ps_.init();
23 ps_.bind_ubo(WB_WORLD_SLOT, resources.world_buf);
24
25 dummy_shadow_tx_.ensure_3d(
26 gpu::TextureFormat::UNORM_8_8_8_8, int3(1), GPU_TEXTURE_USAGE_SHADER_READ, float4(1));
27 dummy_volume_tx_.ensure_3d(
28 gpu::TextureFormat::UNORM_8_8_8_8, int3(1), GPU_TEXTURE_USAGE_SHADER_READ, float4(0));
29 dummy_coba_tx_.ensure_1d(
30 gpu::TextureFormat::UNORM_8_8_8_8, 1, GPU_TEXTURE_USAGE_SHADER_READ, float4(0));
31}
32
34 SceneResources &resources,
35 const SceneState &scene_state,
36 ObjectRef &ob_ref,
38{
39 Object *ob = ob_ref.object;
40 /* Create 3D textures. */
42 BKE_volume_load(&volume, G.main);
43 const bke::VolumeGridData *volume_grid = BKE_volume_grid_active_get_for_read(&volume);
44 if (volume_grid == nullptr) {
45 return;
46 }
47
48 DRWVolumeGrid *grid = DRW_volume_batch_cache_get_grid(&volume, volume_grid);
49 if (grid == nullptr) {
50 return;
51 }
52
53 active_ = true;
54
55 PassMain::Sub &sub_ps = ps_.sub("Volume Object SubPass");
56
57 const bool use_slice = (volume.display.axis_slice_method == AXIS_SLICE_SINGLE);
58
59 sub_ps.shader_set(
60 ShaderCache::get().volume_get(false, volume.display.interpolation_method, false, use_slice));
61 sub_ps.push_constant("do_depth_test", scene_state.shading.type >= OB_SOLID);
62
63 const float density_fac = volume.display.density *
64 BKE_volume_density_scale(&volume, ob->object_to_world().ptr());
65
66 sub_ps.bind_texture("depth_buffer", &resources.depth_tx);
67 sub_ps.bind_texture("stencil_tx", &stencil_tx_);
68 sub_ps.bind_texture("density_tx", grid->texture);
69 /* TODO: implement shadow texture, see manta_smoke_calc_transparency. */
70 sub_ps.bind_texture("shadow_tx", dummy_shadow_tx_);
71 sub_ps.push_constant("active_color", color);
72 sub_ps.push_constant("density_fac", density_fac);
73 sub_ps.push_constant("volume_object_to_texture", float4x4(grid->object_to_texture));
74 sub_ps.push_constant("volume_texture_to_object", float4x4(grid->texture_to_object));
75
76 if (use_slice) {
77 draw_slice_ps(
78 manager, resources, sub_ps, ob_ref, volume.display.slice_axis, volume.display.slice_depth);
79 }
80 else {
81 float4x4 texture_to_world = ob->object_to_world() * float4x4(grid->texture_to_object);
82 float3 world_size = math::to_scale(texture_to_world);
83
84 int3 resolution;
85 GPU_texture_get_mipmap_size(grid->texture, 0, resolution);
86 float3 slice_count = float3(resolution) * 5.0f;
87
88 draw_volume_ps(
89 manager, resources, sub_ps, ob_ref, scene_state.sample, slice_count, world_size);
90 }
91}
92
94 SceneResources &resources,
95 const SceneState &scene_state,
96 ObjectRef &ob_ref,
97 ModifierData *md)
98{
99 Object *ob = ob_ref.object;
100
101 FluidModifierData *modifier = reinterpret_cast<FluidModifierData *>(md);
102 FluidDomainSettings &settings = *modifier->domain;
103
104 if (!settings.fluid) {
105 return;
106 }
107
108 bool can_load = false;
109 if (settings.use_coba) {
111 can_load = settings.tex_field != nullptr;
112 }
113 else if (settings.type == FLUID_DOMAIN_TYPE_GAS) {
115 can_load = settings.tex_density != nullptr || settings.tex_color != nullptr;
116 }
117
118 if (!can_load) {
119 return;
120 }
121
122 active_ = true;
123
124 PassMain::Sub &sub_ps = ps_.sub("Volume Modifier SubPass");
125
126 const bool use_slice = settings.axis_slice_method == AXIS_SLICE_SINGLE;
127
128 sub_ps.shader_set(
129 ShaderCache::get().volume_get(true, settings.interp_method, settings.use_coba, use_slice));
130 sub_ps.push_constant("do_depth_test", scene_state.shading.type >= OB_SOLID);
131
132 if (settings.use_coba) {
133 const bool show_flags = settings.coba_field == FLUID_DOMAIN_FIELD_FLAGS;
134 const bool show_pressure = settings.coba_field == FLUID_DOMAIN_FIELD_PRESSURE;
135 const bool show_phi = ELEM(settings.coba_field,
140
141 sub_ps.push_constant("show_flags", show_flags);
142 sub_ps.push_constant("show_pressure", show_pressure);
143 sub_ps.push_constant("show_phi", show_phi);
144 sub_ps.push_constant("grid_scale", settings.grid_scale);
145
146 if (show_flags) {
147 sub_ps.bind_texture("flag_tx", settings.tex_field);
148 }
149 else {
150 sub_ps.bind_texture("density_tx", settings.tex_field);
151 }
152
153 if (!show_flags && !show_pressure && !show_phi) {
154 sub_ps.bind_texture("transfer_tx", settings.tex_coba);
155 }
156 }
157 else {
158 bool use_constant_color = ((settings.active_fields & FLUID_DOMAIN_ACTIVE_COLORS) == 0 &&
160
161 sub_ps.push_constant("active_color",
162 use_constant_color ? float3(settings.active_color) : float3(1));
163
164 sub_ps.bind_texture("density_tx",
165 settings.tex_color ? settings.tex_color : settings.tex_density);
166 sub_ps.bind_texture("flame_tx", settings.tex_flame ? settings.tex_flame : dummy_volume_tx_);
167 sub_ps.bind_texture("flame_color_tx",
168 settings.tex_flame ? settings.tex_flame_coba : dummy_coba_tx_);
169 sub_ps.bind_texture("shadow_tx", settings.tex_shadow);
170 }
171
172 sub_ps.push_constant("density_fac", 10.0f * settings.display_thickness);
173 sub_ps.bind_texture("depth_buffer", &resources.depth_tx);
174 sub_ps.bind_texture("stencil_tx", &stencil_tx_);
175
176 if (use_slice) {
177 draw_slice_ps(manager, resources, sub_ps, ob_ref, settings.slice_axis, settings.slice_depth);
178 }
179 else {
180 float3 world_size;
181 BKE_object_dimensions_get(ob, world_size);
182
183 float3 slice_count = float3(settings.res) * std::max(0.001f, settings.slice_per_voxel);
184
185 draw_volume_ps(
186 manager, resources, sub_ps, ob_ref, scene_state.sample, slice_count, world_size);
187 }
188}
189
190void VolumePass::draw(Manager &manager, View &view, SceneResources &resources)
191{
192 if (!active_) {
193 return;
194 }
195
196 stencil_tx_ = resources.depth_tx.ptr()->stencil_view();
197
199 fb_.bind();
200 manager.submit(ps_, view);
201}
202
203void VolumePass::draw_slice_ps(Manager &manager,
204 SceneResources &resources,
205 PassMain::Sub &ps,
206 ObjectRef &ob_ref,
207 int slice_axis_enum,
208 float slice_depth)
209{
211
212 const int axis = (slice_axis_enum == SLICE_AXIS_AUTO) ?
213 axis_dominant_v3_single(view_mat_inv[2]) :
214 slice_axis_enum - 1;
215
216 float3 dimensions;
217 BKE_object_dimensions_get(ob_ref.object, dimensions);
218 /* 0.05f to achieve somewhat the same opacity as the full view. */
219 float step_length = std::max(1e-16f, dimensions[axis] * 0.05f);
220
222 ps.push_constant("slice_position", slice_depth);
223 ps.push_constant("slice_axis", axis);
224 ps.push_constant("step_length", step_length);
225
226 ps.draw(resources.volume_cube_batch, manager.unique_handle(ob_ref));
227}
228
229void VolumePass::draw_volume_ps(Manager &manager,
230 SceneResources &resources,
231 PassMain::Sub &ps,
232 ObjectRef &ob_ref,
233 int taa_sample,
234 float3 slice_count,
235 float3 world_size)
236{
237 double noise_offset;
238 BLI_halton_1d(3, 0.0, taa_sample, &noise_offset);
239
240 int max_slice = std::max({UNPACK3(slice_count)});
241 float step_length = math::length((1.0f / slice_count) * world_size);
242
244 ps.push_constant("samples_len", max_slice);
245 ps.push_constant("step_length", step_length);
246 ps.push_constant("noise_ofs", float(noise_offset));
247
248 ps.draw(resources.volume_cube_batch, manager.unique_handle(ob_ref));
249}
250
251} // namespace blender::workbench
void BKE_object_dimensions_get(const Object *ob, float r_vec[3])
Volume data-block.
const blender::bke::VolumeGridData * BKE_volume_grid_active_get_for_read(const Volume *volume)
bool BKE_volume_load(const Volume *volume, const Main *bmain)
Volume data-block rendering and viewport drawing utilities.
float BKE_volume_density_scale(const Volume *volume, const float matrix[4][4])
MINLINE int axis_dominant_v3_single(const float vec[3])
Random number functions.
void BLI_halton_1d(unsigned int prime, double offset, int n, double *r)
Definition rand.cc:226
#define UNPACK3(a)
#define ELEM(...)
@ FLUID_DOMAIN_FIELD_PHI_OUT
@ FLUID_DOMAIN_FIELD_PHI_OBSTACLE
@ FLUID_DOMAIN_FIELD_FLAGS
@ FLUID_DOMAIN_FIELD_PHI
@ FLUID_DOMAIN_FIELD_PRESSURE
@ FLUID_DOMAIN_FIELD_PHI_IN
@ FLUID_DOMAIN_TYPE_GAS
@ SLICE_AXIS_AUTO
@ FLUID_DOMAIN_ACTIVE_COLORS
@ FLUID_DOMAIN_ACTIVE_COLOR_SET
@ FLUID_DOMAIN_USE_NOISE
@ AXIS_SLICE_SINGLE
@ OB_SOLID
T & DRW_object_get_data_for_drawing(const Object &object)
static AppView * view
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_NONE
void GPU_texture_get_mipmap_size(blender::gpu::Texture *texture, int mip_level, int *r_size)
@ GPU_TEXTURE_USAGE_SHADER_READ
ResourceHandleRange unique_handle(const ObjectRef &ref)
void submit(PassSimple &pass, View &view)
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 shader_set(gpu::Shader *shader)
void bind_texture(const char *name, gpu::Texture *texture, GPUSamplerState state=sampler_auto)
void state_set(DRWState state, int clip_plane_count=0)
void draw(gpu::Batch *batch, uint instance_len=-1, uint vertex_len=-1, uint vertex_first=-1, ResourceIndexRange res_index={}, uint custom_id=0)
Definition draw_pass.hh:893
void push_constant(const char *name, const float &data)
detail::PassBase< command::DrawMultiBuf > Sub
Definition draw_pass.hh:499
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)
void DRW_smoke_ensure_coba_field(FluidModifierData *fmd)
void DRW_smoke_ensure(FluidModifierData *fmd, int highres)
@ DRW_STATE_CULL_FRONT
Definition draw_state.hh:44
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:30
@ DRW_STATE_BLEND_ALPHA_PREMUL
Definition draw_state.hh:57
#define G(x, y, z)
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)
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< int32_t, 3 > int3
VecBase< float, 3 > float3
GPUTexture * tex_flame_coba
VolumeDisplay display
UniformBuffer< WorldData > world_buf
#define WB_WORLD_SLOT