Blender V5.3
eevee_volume.bsl.hh
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/* Based on Frostbite Unified Volumetric.
6 * https://www.ea.com/frostbite/news/physically-based-unified-volumetric-rendering-in-frostbite */
7
8#pragma once
9
11#include "eevee_hiz.bsl.hh"
12#include "eevee_light_eval.bsl.hh" /* IWYU pragma: export */
14#include "eevee_light_shared.hh"
17#include "eevee_shadow.bsl.hh"
21
22namespace eevee::volume {
23
24float3 volume_light(LightData light, const bool is_directional, LightVector lv)
25{
26 float power = 1.0f;
27 if (!is_directional) {
28 float light_radius = light.local.local.shape_radius;
34 float d = lv.dist;
35 float d_sqr = square(d);
36 float r_sqr = square(light_radius);
37
38 /* Using reformulation that has better numerical precision. */
39 power = 2.0f / (d_sqr + r_sqr + d * sqrt(d_sqr + r_sqr));
40
41 if (light.type == LIGHT_RECT || light.type == LIGHT_ELLIPSE) {
42 /* Modulate by light plane orientation / solid angle. */
43 power *= saturate(dot(light.z_axis(), lv.L));
44 }
45 }
46 return light.color * eevee::light::power_get(light, LIGHT_VOLUME) * power;
47}
48
49#define VOLUMETRIC_SHADOW_MAX_STEP 128.0f
50
52 const ViewMatrices &view,
53 LightData /*ld*/,
54 const bool is_directional,
55 float3 P,
56 LightVector lv,
57 sampler3D extinction_tx)
58{
59 if (uni.uniform_buf.volumes.shadow_steps == 0.0f) {
60 return float3(1.0f);
61 }
62
63 /* Heterogeneous volume shadows. */
64 float dd = lv.dist / uni.uniform_buf.volumes.shadow_steps;
65 float3 L = lv.L * lv.dist / uni.uniform_buf.volumes.shadow_steps;
66
67 if (is_directional) {
68 if (is_panoramic(uni.uniform_buf.camera.type)) {
69 /* Panoramic cameras render 6 identical but differently oriented frustums, so we can't use
70 * NDC box to bound the ray like below. Use distance to the camera instead because it's
71 * view-independent. */
72 float3 L_world = lv.L * distance(P, view.position());
73 L = L_world / uni.uniform_buf.volumes.shadow_steps;
74 dd = length(L);
75 }
76 else {
77 /* For sun light we scan the whole frustum. So we need to get the correct endpoints. */
78 float3 ndcP = view.point_world_to_ndc(P);
79 float3 ndcL = view.point_world_to_ndc(P + lv.L * lv.dist) - ndcP;
80
81 float3 ndc_frustum_isect = ndcP + ndcL * line_unit_box_intersect_dist_safe(ndcP, ndcL);
82
83 L = view.point_ndc_to_world(ndc_frustum_isect) - P;
84 L /= uni.uniform_buf.volumes.shadow_steps;
85 dd = length(L);
86 }
87 }
88
89 /* TODO use shadow maps instead. */
90 float3 shadow = float3(1.0f);
91 for (float t = 1.0f; t < VOLUMETRIC_SHADOW_MAX_STEP && t <= uni.uniform_buf.volumes.shadow_steps;
92 t += 1.0f)
93 {
94 float3 w_pos = P + L * t;
95
96 float3 v_pos = view.point_world_to_view(w_pos);
97 float3 volume_co = volume_view_to_jitter(uni, view, v_pos);
98 /* Let the texture be clamped to edge. This reduce visual glitches. */
99 float3 s_extinction = texture(extinction_tx, volume_co).rgb;
100
101 shadow *= exp(-s_extinction * dd);
102 }
103 return shadow;
104}
105
109
110struct Scatter {
112
118
121 /* Alias of UnifiedVolumeProperties::in_extinction_img but as a sampler. */
122 [[sampler(9), condition(use_volume_light)]] sampler3D extinction_tx;
123
126
127 float3 volume_lightprobe_eval(const Sampling &sampling, float3 P, float3 V, float s_anisotropy)
128 {
130 const Uniform &uni = uniforms;
131
133 SphericalHarmonicL1<float4> volume_radiance_sh = volume_data.sample_probe_no_bias(sampling, P);
134
135 float clamp_indirect = uni.uniform_buf.clamp.volume_indirect;
136 volume_radiance_sh = spherical_harmonics::clamp_energy(volume_radiance_sh, clamp_indirect);
137
138 return spherical_harmonics::dot(volume_radiance_sh, phase_sh).xyz;
139 }
140};
141
147
148 float3 light_eval_single(Scatter &srt, LightData light, const bool is_directional)
149 {
150 ShadowRenderData &srd = srt.shadow_data;
151 const Uniform &uni = srt.uniforms;
152 const draw::View &views = srt.views_;
153
154 /* TODO(fclem): Own light list for volume without lights that have 0 volume influence. */
155 if (eevee::light::power_get(light, LIGHT_VOLUME) == 0.0f) {
156 return float3(0);
157 }
158
159 LightVector lv = LightVector::get_shape_closest(light, is_directional, P);
160
161 float attenuation = light_attenuation_volume(light, is_directional, lv);
162 if (attenuation < LIGHT_ATTENUATION_THRESHOLD) {
163 return float3(0);
164 }
165
166 float visibility = attenuation;
167 if (light.tilemap_index != LIGHT_NO_SHADOW) {
168 float delta = srd.shadow_sample(is_directional, light, P);
169 if (delta > 0.0f) {
170 return float3(0);
171 }
172 }
173 visibility *= volume_phase_function(-V, lv.L, anisotropy);
174 if (visibility < LIGHT_ATTENUATION_THRESHOLD) {
175 return float3(0);
176 }
177
178 float3 Li = volume_light(light, is_directional, lv) * visibility;
179
181 if (light.tilemap_index != LIGHT_NO_SHADOW) {
182 Li *= volume_shadow(uni, views.get(0), light, is_directional, P, lv, srt.extinction_tx);
183 }
184 }
185
186 return Li;
187 }
188
189 void eval_directional(Scatter &srd, uint /*l_idx*/, LightData light)
190 {
191 radiance += light_eval_single(srd, light, true);
192 }
193
194 void eval_local(Scatter &srd, uint /*l_idx*/, LightData light)
195 {
196 radiance += light_eval_single(srd, light, false);
197 }
198};
199
200} // namespace eevee::volume
201
202namespace eevee::light {
205}
206
207namespace eevee::volume {
208
209/* Step 2 : Evaluate all light scattering for each froxels.
210 * Also do the temporal reprojection to fight aliasing artifacts. */
213 [[resource_table]] const Uniform &uni,
214 [[resource_table]] const draw::View &views,
215 [[resource_table]] const Sampling &sampling,
217 [[global_invocation_id]] const uint3 global_id)
218{
219 int3 froxel = int3(global_id);
220
221 if (any(greaterThanEqual(froxel, uni.uniform_buf.volumes.tex_size))) {
222 return;
223 }
224
225 const ViewMatrices view = views.get(0);
226
227 /* Emission. */
228 float3 scattering = imageLoadFast(props.in_emission_img, froxel).rgb;
229 float3 extinction = imageLoadFast(props.in_extinction_img, froxel).rgb;
230
231 float3 s_scattering = imageLoadFast(props.in_scattering_img, froxel).rgb;
232
233 float offset = sampling.rng_1D_get(SAMPLING_VOLUME_W);
234 float jitter = volume_froxel_jitter(froxel.xy, offset);
235 float3 uvw = (float3(froxel) + float3(0.5f, 0.5f, 0.5f - jitter)) *
236 uni.uniform_buf.volumes.inv_tex_size;
237 float3 vP = volume_jitter_to_view(uni, view, uvw);
238
239 float3 P = view.point_view_to_world(vP);
241
242 float phase = imageLoadFast(props.in_phase_img, froxel).r;
243 float phase_weight = imageLoadFast(props.in_phase_weight_img, froxel).r;
244 /* Divide by phase total weight, to compute the mean anisotropy. */
245 float s_anisotropy = phase / max(1.0f, phase_weight);
246
247 float3 direct_radiance = float3(0.0f);
248
249 if (srt.constants.use_volume_light) [[static_branch]] {
250 if (reduce_max(s_scattering) > 0.0f) {
252 .radiance = float3(0.0f),
253 .P = P,
254 .V = V,
255 .anisotropy = s_anisotropy,
256 };
257
258 float2 pixel = ((float2(froxel.xy) + 0.5f) * uni.uniform_buf.volumes.inv_tex_size.xy) *
259 uni.uniform_buf.volumes.main_view_extent;
260
261 light::foreach_visible(srt.light_data, pixel, vP.z, ctx, srt);
262 direct_radiance = ctx.radiance;
263 }
264 }
265
266 float3 indirect_radiance = srt.volume_lightprobe_eval(sampling, P, V, s_anisotropy).xyz;
267
268 direct_radiance *= s_scattering;
269 indirect_radiance *= s_scattering;
270
271 float clamp_direct = uni.uniform_buf.clamp.volume_direct;
272 float clamp_indirect = uni.uniform_buf.clamp.volume_indirect;
273 direct_radiance = colorspace::brightness_clamp_max(direct_radiance, clamp_direct);
274 indirect_radiance = colorspace::brightness_clamp_max(indirect_radiance, clamp_indirect);
275
276 direct_radiance *= uni.uniform_buf.clamp.direct_scale;
277 indirect_radiance *= uni.uniform_buf.clamp.indirect_scale;
278
279 scattering += direct_radiance + indirect_radiance;
280
281 if (uni.uniform_buf.volumes.history_opacity > 0.0f) {
282 /* Temporal reprojection. */
283 float3 uvw_history = volume_history_uvw_get(uni, view, froxel);
284 if (uvw_history.x != -1.0f) {
285 float3 scattering_history = texture(srt.scattering_history_tx, uvw_history).rgb;
286 float3 extinction_history = texture(srt.extinction_history_tx, uvw_history).rgb;
287 scattering = mix(scattering, scattering_history, uni.uniform_buf.volumes.history_opacity);
288 extinction = mix(extinction, extinction_history, uni.uniform_buf.volumes.history_opacity);
289 }
290 }
291
292 /* Catch NaNs. */
293 if (any(isnan(scattering)) || any(isnan(extinction))) {
294 scattering = float3(0.0f);
295 extinction = float3(0.0f);
296 }
297
298 imageStoreFast(srt.out_scattering_img, froxel, float4(scattering, 1.0f));
299 imageStoreFast(srt.out_extinction_img, froxel, float4(extinction, 1.0f));
300}
308
309/* Step 3 : Integrate for each froxel the final amount of light
310 * scattered back to the viewer and the amount of transmittance. */
313 [[resource_table]] const draw::View &views,
314 [[resource_table]] const Uniform &uni,
315 [[global_invocation_id]] const uint3 global_id)
316{
317 int2 texel = int2(global_id.xy);
318
319 if (any(greaterThanEqual(texel, uni.uniform_buf.volumes.tex_size.xy))) {
320 return;
321 }
322
323 const ViewMatrices view = views.get(0);
324
325 /* Start with full transmittance and no scattered light. */
326 float3 scattering = float3(0.0f);
327 float3 transmittance = float3(1.0f);
328
329 /* Compute view ray. Note that jittering the position of the first voxel doesn't bring any
330 * benefit here. */
331 float3 uvw = (float3(float2(texel), 0.0f) + float3(0.5f, 0.5f, 0.0f)) *
332 uni.uniform_buf.volumes.inv_tex_size;
333 float3 view_cell = volume_jitter_to_view(uni, view, uvw);
334
335 const bool uses_radial_depth = volume_uses_radial_depth(uni);
336
337 float prev_ray_len;
338 float orig_ray_len = 1.0f;
339 if (uses_radial_depth) {
340 /* Camera distance at froxel Z = 0, negated: `volume_z_to_view_z` returns a view-space Z
341 * (negative, forward is -Z), but the per-step diff below expects a positive distance. */
342 prev_ray_len = -volume_z_to_view_z(uni, view, 0.0f);
343 }
344 else if (view.is_perspective()) {
345 prev_ray_len = length(view_cell);
346 orig_ray_len = prev_ray_len / view_cell.z;
347 }
348 else {
349 prev_ray_len = view_cell.z;
350 }
351
352 for (int i = 0; i <= uni.uniform_buf.volumes.tex_size.z; i++) {
353 int3 froxel = int3(texel, i);
354
355 float3 froxel_scattering = texelFetch(srt.in_scattering_tx, froxel, 0).rgb;
356 float3 extinction = texelFetch(srt.in_extinction_tx, froxel, 0).rgb;
357
358 float step_len;
359 if (uses_radial_depth) {
360 float ray_len = -volume_z_to_view_z(
361 uni, view, (float(i) + 1.0f) * uni.uniform_buf.volumes.inv_tex_size.z);
362 step_len = abs(ray_len - prev_ray_len);
363 prev_ray_len = ray_len;
364 }
365 else {
366 float cell_depth = volume_z_to_view_z(
367 uni, view, (float(i) + 1.0f) * uni.uniform_buf.volumes.inv_tex_size.z);
368 float ray_len = orig_ray_len * cell_depth;
369
370 /* Evaluate Scattering. */
371 step_len = abs(ray_len - prev_ray_len);
372 prev_ray_len = ray_len;
373 }
374 float3 froxel_transmittance = exp(-extinction * step_len);
375 /* NOTE: Original calculation carries precision issues when compiling for AMD GPUs
376 * and running Metal. This version of the equation retains precision well for all
377 * macOS HW configurations.
378 * Here is the original for reference:
379 * `Lscat = (Lscat - Lscat * Tr) / safe_rcp(s_extinction)` */
380 float3 froxel_opacity = 1.0f - froxel_transmittance;
381 float3 froxel_step_opacity = froxel_opacity * safe_rcp(extinction);
382
383 /* Emission does not work if there is no extinction because
384 * `froxel_transmittance` evaluates to 1.0 leading to `froxel_opacity = 0.0 and 0.4 depth.`.
385 * (See #65771) To avoid fiddling with numerical values, take the limit of
386 * `froxel_step_opacity` as `extinction` approaches zero which is simply `step_len`. */
387 bool3 is_invalid_extinction = equal(extinction, float3(0.0f));
388 froxel_step_opacity = mix(froxel_step_opacity, float3(step_len), is_invalid_extinction);
389
390 /* Integrate along the current step segment. */
391 froxel_scattering = froxel_scattering * froxel_step_opacity;
392
393 /* Accumulate and also take into account the transmittance from previous steps. */
394 scattering += transmittance * froxel_scattering;
395 transmittance *= froxel_transmittance;
396
397 imageStoreFast(srt.out_scattering_img, froxel, float4(scattering, 1.0f));
398 imageStoreFast(srt.out_transmittance_img, froxel, float4(transmittance, 1.0f));
399 }
400}
401
406
407[[vertex]]
408void resolve_vert([[vertex_id]] const int vert_id, [[position]] float4 &out_position)
409{
410 fullscreen_vertex(vert_id, out_position);
411}
412
413/* Step 4 : Apply final integration on top of the scene color.
414 * This is only for opaque geometry.
415 *
416 * Panoramic: each cubemap face resolves its own volumetric contribution independently. No
417 * blending across face edges/corners, so results can disagree right at a face seam. */
418[[fragment]]
420 [[resource_table]] const UnifiedVolumeData &volumes,
421 [[resource_table]] RenderPassOutput &render_passes,
422 [[resource_table]] const HiZ &hiz,
423 [[frag_coord]] const float4 frag_co,
424 [[out]] FragOut &out_frag)
425{
426 float2 uvs = frag_co.xy * uni.uniform_buf.volumes.main_view_extent_inv;
427 float scene_depth = texelFetch(hiz.hiz_tx, int2(frag_co.xy), 0).r;
428
429 VolumeResolveSample vol = volumes.resolve(float3(uvs, scene_depth));
430
431 out_frag.radiance = float4(vol.scattering, 0.0f);
432 out_frag.transmittance = float4(vol.transmittance, saturate(average(vol.transmittance)));
433
434 render_passes.store_color(
435 int2(frag_co.xy), uni.uniform_buf.render_pass.volume_light_id, float4(vol.scattering, 1.0f));
436}
437
439 ScatterConstants{.use_volume_light = false},
440 LightEvalConstants{.light_closure_count_reflect = 1,
441 .light_closure_count_transmit = 1});
443 ScatterConstants{.use_volume_light = true},
444 LightEvalConstants{.light_closure_count_reflect = 1,
445 .light_closure_count_transmit = 1});
447 LightEvalConstants{.light_closure_count_reflect = 1,
448 .light_closure_count_transmit = 1});
451 LightEvalConstants{.light_closure_count_reflect = 1,
452 .light_closure_count_transmit = 1});
453} // namespace eevee::volume
btScalar max
float line_unit_box_intersect_dist_safe(float3 line_origin, float3 line_direction)
#define VOLUME_GROUP_SIZE
#define VOLUME_INTEGRATION_GROUP_SIZE
#define LIGHT_ATTENUATION_THRESHOLD
float light_attenuation_volume(LightData light, const bool is_directional, LightVector lv)
#define LIGHT_NO_SHADOW
#define VOLUMETRIC_SHADOW_MAX_STEP
SphericalHarmonicL1< float4 > volume_phase_function_as_sh_L1(float3 V, float g)
float volume_z_to_view_z(const eevee::Uniform &uni, const ViewMatrices view, float z)
float3 volume_view_to_jitter(const eevee::Uniform &uni, const ViewMatrices view, float3 vP)
float3 volume_jitter_to_view(const eevee::Uniform &uni, const ViewMatrices view, float3 coord)
float volume_phase_function(float3 V, float3 L, float g)
float volume_froxel_jitter(int2 froxel, float offset)
float3 volume_history_uvw_get(const eevee::Uniform &uni, const ViewMatrices view, int3 froxel)
bool volume_uses_radial_depth(const eevee::Uniform &uni)
#define position
#define image(slot, qualifiers, format)
#define index(i)
#define frag_coord
#define compilation_constant
#define resource_table
#define sampler(slot)
#define static_branch
#define global_invocation_id
#define fragment
#define vertex_id
#define compute
#define condition(cond)
#define vertex
#define frag_color(slot)
#define local_size(...)
#define isnan
VecBase< bool, D > greaterThanEqual(VecOp< T, D >, VecOp< T, D >) RET
bool any(VecOp< bool, D >) RET
#define exp
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
float distance(float, float) RET
#define sqrt
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
float length(VecOp< float, D >) RET
ImageBase< float, 3 > image3D
#define imageLoadFast
#define imageStoreFast
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
SamplerBase< float, 3 > sampler3D
VecBase< bool, 3 > bool3
void fullscreen_vertex(int vertex_id, float4 &out_position)
float square(float v)
float safe_rcp(float a)
float average(float2 a)
float reduce_max(float2 a)
ccl_device_inline float2 power(const float2 v, const float e)
#define L
float3 brightness_clamp_max(float3 color, float limit)
template void foreach_visible< volume::LightEvalCtx, volume::Scatter >(const LightRenderData &, float2, float, volume::LightEvalCtx &, volume::Scatter &)
float power_get(LightData light, LightingType type)
void foreach_visible(const LightRenderData &srt, float2 pixel, float linear_view_z, CallbackT &cb, ResourceT &res)
void resolve_frag(const Uniform &uni, const UnifiedVolumeData &volumes, RenderPassOutput &render_passes, const HiZ &hiz, const float4 frag_co, FragOut &out_frag)
PipelineGraphic resolve(resolve_vert, resolve_frag, LightEvalConstants{.light_closure_count_reflect=1,.light_closure_count_transmit=1})
void resolve_vert(const int vert_id, float4 &out_position)
PipelineCompute scatter_with_lights(scatter_main, ScatterConstants{.use_volume_light=true}, LightEvalConstants{.light_closure_count_reflect=1,.light_closure_count_transmit=1})
void scatter_main(Scatter &srt, const Uniform &uni, const draw::View &views, const Sampling &sampling, UnifiedVolumeProperties &props, const uint3 global_id)
float3 volume_light(LightData light, const bool is_directional, LightVector lv)
PipelineCompute integration(integration_main, LightEvalConstants{.light_closure_count_reflect=1,.light_closure_count_transmit=1})
PipelineCompute scatter(scatter_main, ScatterConstants{.use_volume_light=false}, LightEvalConstants{.light_closure_count_reflect=1,.light_closure_count_transmit=1})
void integration_main(Integrate &srt, const draw::View &views, const Uniform &uni, const uint3 global_id)
float3 volume_shadow(const Uniform &uni, const ViewMatrices &view, LightData, const bool is_directional, float3 P, LightVector lv, sampler3D extinction_tx)
SphericalHarmonicL1< float4 > clamp_energy(SphericalHarmonicL1< float4 > sh, float clamp_value)
float4 dot(SphericalHarmonicL1< float4 > a, SphericalHarmonicL1< float4 > b)
#define mix
#define saturate(a)
Definition smaa.cc:319
static LightVector get_shape_closest(LightData light, const bool is_directional, float3 P)
bool is_perspective() const
float3 point_world_to_view(float3 P) const
float3 point_ndc_to_world(float3 ssP) const
float3 point_world_to_ndc(float3 P) const
float3 position() const
float3 point_view_to_world(float3 vP) const
float3 world_incident_vector(float3 P) const
ViewMatrices get(uint view_id) const
SphericalHarmonicL1< float4 > sample_probe_no_bias(const Sampling &sampling, float3 P) const
float shadow_sample(const bool is_directional, LightData light, float3 P) const
const UniformData & uniform_buf
void eval_directional(Scatter &srd, uint, LightData light)
void eval_local(Scatter &srd, uint, LightData light)
float3 light_eval_single(Scatter &srt, LightData light, const bool is_directional)
LightprobeVolumeRenderData lightprobe_volume_data
ShadowRenderData shadow_data
float3 volume_lightprobe_eval(const Sampling &sampling, float3 P, float3 V, float s_anisotropy)
float z
Definition sky_math.h:136
float x
Definition sky_math.h:136
CCL_NAMESPACE_BEGIN struct Window V