bevy_pbr/light_probe/irradiance_volume.rs
1//! Irradiance volumes, also known as voxel global illumination.
2//!
3//! An *irradiance volume* is a cuboid voxel region consisting of
4//! regularly-spaced precomputed samples of diffuse indirect light. They're
5//! ideal if you have a dynamic object such as a character that can move about
6//! static non-moving geometry such as a level in a game, and you want that
7//! dynamic object to be affected by the light bouncing off that static
8//! geometry.
9//!
10//! To use irradiance volumes, you need to precompute, or *bake*, the indirect
11//! light in your scene. Bevy doesn't currently come with a way to do this.
12//! Fortunately, [Blender] provides a [baking tool] as part of the Eevee
13//! renderer, and its irradiance volumes are compatible with those used by Bevy.
14//! The [`bevy-baked-gi`] project provides a tool, `export-blender-gi`, that can
15//! extract the baked irradiance volumes from the Blender `.blend` file and
16//! package them up into a `.ktx2` texture for use by the engine. See the
17//! documentation in the `bevy-baked-gi` project for more details on this
18//! workflow.
19//!
20//! Like all light probes in Bevy, irradiance volumes are 1×1×1 cubes that can
21//! be arbitrarily scaled, rotated, and positioned in a scene with the
22//! [`bevy_transform::components::Transform`] component. The 3D voxel grid will
23//! be stretched to fill the interior of the cube, and the illumination from the
24//! irradiance volume will apply to all fragments within that bounding region.
25//!
26//! Bevy's irradiance volumes are based on Valve's [*ambient cubes*] as used in
27//! *Half-Life 2* ([Mitchell 2006, slide 27]). These encode a single color of
28//! light from the six 3D cardinal directions and blend the sides together
29//! according to the surface normal. For an explanation of why ambient cubes
30//! were chosen over spherical harmonics, see [Why ambient cubes?] below.
31//!
32//! If you wish to use a tool other than `export-blender-gi` to produce the
33//! irradiance volumes, you'll need to pack the irradiance volumes in the
34//! following format. The irradiance volume of resolution *(Rx, Ry, Rz)* is
35//! expected to be a 3D texture of dimensions *(Rx, 2Ry, 3Rz)*. The unnormalized
36//! texture coordinate *(s, t, p)* of the voxel at coordinate *(x, y, z)* with
37//! side *S* ∈ *{-X, +X, -Y, +Y, -Z, +Z}* is as follows:
38//!
39//! ```text
40//! s = x
41//!
42//! t = y + ⎰ 0 if S ∈ {-X, -Y, -Z}
43//! ⎱ Ry if S ∈ {+X, +Y, +Z}
44//!
45//! ⎧ 0 if S ∈ {-X, +X}
46//! p = z + ⎨ Rz if S ∈ {-Y, +Y}
47//! ⎩ 2Rz if S ∈ {-Z, +Z}
48//! ```
49//!
50//! Visually, in a left-handed coordinate system with Y up, viewed from the
51//! right, the 3D texture looks like a stacked series of voxel grids, one for
52//! each cube side, in this order:
53//!
54//! | **+X** | **+Y** | **+Z** |
55//! | ------ | ------ | ------ |
56//! | **-X** | **-Y** | **-Z** |
57//!
58//! A terminology note: Other engines may refer to irradiance volumes as *voxel
59//! global illumination*, *VXGI*, or simply as *light probes*. Sometimes *light
60//! probe* refers to what Bevy calls a reflection probe. In Bevy, *light probe*
61//! is a generic term that encompasses all cuboid bounding regions that capture
62//! indirect illumination, whether based on voxels or not.
63//!
64//! Note that, if binding arrays aren't supported (e.g. on WebGPU or WebGL 2),
65//! then only the closest irradiance volume to the view will be taken into
66//! account during rendering. The required `wgpu` features are
67//! [`bevy_render::settings::WgpuFeatures::TEXTURE_BINDING_ARRAY`] and
68//! [`bevy_render::settings::WgpuFeatures::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING`].
69//!
70//! ## Why ambient cubes?
71//!
72//! This section describes the motivation behind the decision to use ambient
73//! cubes in Bevy. It's not needed to use the feature; feel free to skip it
74//! unless you're interested in its internal design.
75//!
76//! Bevy uses *Half-Life 2*-style ambient cubes (usually abbreviated as *HL2*)
77//! as the representation of irradiance for light probes instead of the
78//! more-popular spherical harmonics (*SH*). This might seem to be a surprising
79//! choice, but it turns out to work well for the specific case of voxel
80//! sampling on the GPU. Spherical harmonics have two problems that make them
81//! less ideal for this use case:
82//!
83//! 1. The level 1 spherical harmonic coefficients can be negative. That
84//! prevents the use of the efficient [RGB9E5 texture format], which only
85//! encodes unsigned floating point numbers, and forces the use of the
86//! less-efficient [RGBA16F format] if hardware interpolation is desired.
87//!
88//! 2. As an alternative to RGBA16F, level 1 spherical harmonics can be
89//! normalized and scaled to the SH0 base color, as [Frostbite] does. This
90//! allows them to be packed in standard LDR RGBA8 textures. However, this
91//! prevents the use of hardware trilinear filtering, as the nonuniform scale
92//! factor means that hardware interpolation no longer produces correct results.
93//! The 8 texture fetches needed to interpolate between voxels can be upwards of
94//! twice as slow as the hardware interpolation.
95//!
96//! The following chart summarizes the costs and benefits of ambient cubes,
97//! level 1 spherical harmonics, and level 2 spherical harmonics:
98//!
99//! | Technique | HW-interpolated samples | Texel fetches | Bytes per voxel | Quality |
100//! | ------------------------ | ----------------------- | ------------- | --------------- | ------- |
101//! | Ambient cubes | 3 | 0 | 24 | Medium |
102//! | Level 1 SH, compressed | 0 | 36 | 16 | Low |
103//! | Level 1 SH, uncompressed | 4 | 0 | 24 | Low |
104//! | Level 2 SH, compressed | 0 | 72 | 28 | High |
105//! | Level 2 SH, uncompressed | 9 | 0 | 54 | High |
106//!
107//! (Note that the number of bytes per voxel can be reduced using various
108//! texture compression methods, but the overall ratios remain similar.)
109//!
110//! From these data, we can see that ambient cubes balance fast lookups (from
111//! leveraging hardware interpolation) with relatively-small storage
112//! requirements and acceptable quality. Hence, they were chosen for irradiance
113//! volumes in Bevy.
114//!
115//! [*ambient cubes*]: https://advances.realtimerendering.com/s2006/Mitchell-ShadingInValvesSourceEngine.pdf
116//!
117//! [spherical harmonics]: https://en.wikipedia.org/wiki/Spherical_harmonic_lighting
118//!
119//! [RGB9E5 texture format]: https://www.khronos.org/opengl/wiki/Small_Float_Formats#RGB9_E5
120//!
121//! [RGBA16F format]: https://www.khronos.org/opengl/wiki/Small_Float_Formats#Low-bitdepth_floats
122//!
123//! [Frostbite]: https://media.contentapi.ea.com/content/dam/eacom/frostbite/files/gdc2018-precomputedgiobalilluminationinfrostbite.pdf#page=53
124//!
125//! [Mitchell 2006, slide 27]: https://advances.realtimerendering.com/s2006/Mitchell-ShadingInValvesSourceEngine.pdf#page=27
126//!
127//! [Blender]: http://blender.org/
128//!
129//! [baking tool]: https://docs.blender.org/manual/en/latest/render/eevee/render_settings/indirect_lighting.html
130//!
131//! [`bevy-baked-gi`]: https://github.com/pcwalton/bevy-baked-gi
132//!
133//! [Why ambient cubes?]: #why-ambient-cubes
134
135use bevy_ecs::{component::Component, reflect::ReflectComponent};
136use bevy_image::Image;
137use bevy_render::{
138 render_asset::RenderAssets,
139 render_resource::{
140 binding_types, BindGroupLayoutEntryBuilder, Sampler, SamplerBindingType, Shader,
141 TextureSampleType, TextureView,
142 },
143 renderer::RenderDevice,
144 texture::{FallbackImage, GpuImage},
145};
146use core::{num::NonZero, ops::Deref};
147
148use bevy_asset::{AssetId, Handle};
149use bevy_reflect::{std_traits::ReflectDefault, Reflect};
150
151use crate::{
152 add_cubemap_texture_view, binding_arrays_are_usable, RenderViewLightProbes,
153 MAX_VIEW_LIGHT_PROBES,
154};
155
156use super::LightProbeComponent;
157
158pub const IRRADIANCE_VOLUME_SHADER_HANDLE: Handle<Shader> =
159 Handle::weak_from_u128(160299515939076705258408299184317675488);
160
161/// On WebGL and WebGPU, we must disable irradiance volumes, as otherwise we can
162/// overflow the number of texture bindings when deferred rendering is in use
163/// (see issue #11885).
164pub(crate) const IRRADIANCE_VOLUMES_ARE_USABLE: bool = cfg!(not(target_arch = "wasm32"));
165
166/// The component that defines an irradiance volume.
167///
168/// See [`crate::irradiance_volume`] for detailed information.
169#[derive(Clone, Default, Reflect, Component, Debug)]
170#[reflect(Component, Default, Debug)]
171pub struct IrradianceVolume {
172 /// The 3D texture that represents the ambient cubes, encoded in the format
173 /// described in [`crate::irradiance_volume`].
174 pub voxels: Handle<Image>,
175
176 /// Scale factor applied to the diffuse and specular light generated by this component.
177 ///
178 /// After applying this multiplier, the resulting values should
179 /// be in units of [cd/m^2](https://en.wikipedia.org/wiki/Candela_per_square_metre).
180 ///
181 /// See also <https://google.github.io/filament/Filament.html#lighting/imagebasedlights/iblunit>.
182 pub intensity: f32,
183}
184
185/// All the bind group entries necessary for PBR shaders to access the
186/// irradiance volumes exposed to a view.
187pub(crate) enum RenderViewIrradianceVolumeBindGroupEntries<'a> {
188 /// The version used when binding arrays aren't available on the current platform.
189 Single {
190 /// The texture view of the closest light probe.
191 texture_view: &'a TextureView,
192 /// A sampler used to sample voxels of the irradiance volume.
193 sampler: &'a Sampler,
194 },
195
196 /// The version used when binding arrays are available on the current
197 /// platform.
198 Multiple {
199 /// A texture view of the voxels of each irradiance volume, in the same
200 /// order that they are supplied to the view (i.e. in the same order as
201 /// `binding_index_to_cubemap` in [`RenderViewLightProbes`]).
202 ///
203 /// This is a vector of `wgpu::TextureView`s. But we don't want to import
204 /// `wgpu` in this crate, so we refer to it indirectly like this.
205 texture_views: Vec<&'a <TextureView as Deref>::Target>,
206
207 /// A sampler used to sample voxels of the irradiance volumes.
208 sampler: &'a Sampler,
209 },
210}
211
212impl<'a> RenderViewIrradianceVolumeBindGroupEntries<'a> {
213 /// Looks up and returns the bindings for any irradiance volumes visible in
214 /// the view, as well as the sampler.
215 pub(crate) fn get(
216 render_view_irradiance_volumes: Option<&RenderViewLightProbes<IrradianceVolume>>,
217 images: &'a RenderAssets<GpuImage>,
218 fallback_image: &'a FallbackImage,
219 render_device: &RenderDevice,
220 ) -> RenderViewIrradianceVolumeBindGroupEntries<'a> {
221 if binding_arrays_are_usable(render_device) {
222 RenderViewIrradianceVolumeBindGroupEntries::get_multiple(
223 render_view_irradiance_volumes,
224 images,
225 fallback_image,
226 )
227 } else {
228 RenderViewIrradianceVolumeBindGroupEntries::get_single(
229 render_view_irradiance_volumes,
230 images,
231 fallback_image,
232 )
233 }
234 }
235
236 /// Looks up and returns the bindings for any irradiance volumes visible in
237 /// the view, as well as the sampler. This is the version used when binding
238 /// arrays are available on the current platform.
239 fn get_multiple(
240 render_view_irradiance_volumes: Option<&RenderViewLightProbes<IrradianceVolume>>,
241 images: &'a RenderAssets<GpuImage>,
242 fallback_image: &'a FallbackImage,
243 ) -> RenderViewIrradianceVolumeBindGroupEntries<'a> {
244 let mut texture_views = vec![];
245 let mut sampler = None;
246
247 if let Some(irradiance_volumes) = render_view_irradiance_volumes {
248 for &cubemap_id in &irradiance_volumes.binding_index_to_textures {
249 add_cubemap_texture_view(
250 &mut texture_views,
251 &mut sampler,
252 cubemap_id,
253 images,
254 fallback_image,
255 );
256 }
257 }
258
259 // Pad out the bindings to the size of the binding array using fallback
260 // textures. This is necessary on D3D12 and Metal.
261 texture_views.resize(MAX_VIEW_LIGHT_PROBES, &*fallback_image.d3.texture_view);
262
263 RenderViewIrradianceVolumeBindGroupEntries::Multiple {
264 texture_views,
265 sampler: sampler.unwrap_or(&fallback_image.d3.sampler),
266 }
267 }
268
269 /// Looks up and returns the bindings for any irradiance volumes visible in
270 /// the view, as well as the sampler. This is the version used when binding
271 /// arrays aren't available on the current platform.
272 fn get_single(
273 render_view_irradiance_volumes: Option<&RenderViewLightProbes<IrradianceVolume>>,
274 images: &'a RenderAssets<GpuImage>,
275 fallback_image: &'a FallbackImage,
276 ) -> RenderViewIrradianceVolumeBindGroupEntries<'a> {
277 if let Some(irradiance_volumes) = render_view_irradiance_volumes {
278 if let Some(irradiance_volume) = irradiance_volumes.render_light_probes.first() {
279 if irradiance_volume.texture_index >= 0 {
280 if let Some(image_id) = irradiance_volumes
281 .binding_index_to_textures
282 .get(irradiance_volume.texture_index as usize)
283 {
284 if let Some(image) = images.get(*image_id) {
285 return RenderViewIrradianceVolumeBindGroupEntries::Single {
286 texture_view: &image.texture_view,
287 sampler: &image.sampler,
288 };
289 }
290 }
291 }
292 }
293 }
294
295 RenderViewIrradianceVolumeBindGroupEntries::Single {
296 texture_view: &fallback_image.d3.texture_view,
297 sampler: &fallback_image.d3.sampler,
298 }
299 }
300}
301
302/// Returns the bind group layout entries for the voxel texture and sampler
303/// respectively.
304pub(crate) fn get_bind_group_layout_entries(
305 render_device: &RenderDevice,
306) -> [BindGroupLayoutEntryBuilder; 2] {
307 let mut texture_3d_binding =
308 binding_types::texture_3d(TextureSampleType::Float { filterable: true });
309 if binding_arrays_are_usable(render_device) {
310 texture_3d_binding =
311 texture_3d_binding.count(NonZero::<u32>::new(MAX_VIEW_LIGHT_PROBES as _).unwrap());
312 }
313
314 [
315 texture_3d_binding,
316 binding_types::sampler(SamplerBindingType::Filtering),
317 ]
318}
319
320impl LightProbeComponent for IrradianceVolume {
321 type AssetId = AssetId<Image>;
322
323 // Irradiance volumes can't be attached to the view, so we store nothing
324 // here.
325 type ViewLightProbeInfo = ();
326
327 fn id(&self, image_assets: &RenderAssets<GpuImage>) -> Option<Self::AssetId> {
328 if image_assets.get(&self.voxels).is_none() {
329 None
330 } else {
331 Some(self.voxels.id())
332 }
333 }
334
335 fn intensity(&self) -> f32 {
336 self.intensity
337 }
338
339 fn create_render_view_light_probes(
340 _: Option<&Self>,
341 _: &RenderAssets<GpuImage>,
342 ) -> RenderViewLightProbes<Self> {
343 RenderViewLightProbes::new()
344 }
345}