//! Light probes for baked global illumination.12use bevy_app::{App, Plugin};3use bevy_asset::AssetId;4use bevy_camera::{5primitives::{Aabb, Frustum},6Camera3d,7};8use bevy_derive::{Deref, DerefMut};9use bevy_ecs::{10component::Component,11entity::Entity,12query::With,13resource::Resource,14schedule::IntoScheduleConfigs,15system::{Commands, Local, Query, Res, ResMut},16};17use bevy_image::Image;18use bevy_light::{EnvironmentMapLight, IrradianceVolume, LightProbe};19use bevy_math::{Affine3A, FloatOrd, Mat4, Vec3A, Vec4};20use bevy_platform::collections::HashMap;21use bevy_render::{22extract_instances::ExtractInstancesPlugin,23render_asset::RenderAssets,24render_resource::{DynamicUniformBuffer, Sampler, ShaderType, TextureView},25renderer::{RenderAdapter, RenderAdapterInfo, RenderDevice, RenderQueue, WgpuWrapper},26settings::WgpuFeatures,27sync_world::RenderEntity,28texture::{FallbackImage, GpuImage},29view::ExtractedView,30Extract, ExtractSchedule, Render, RenderApp, RenderSystems,31};32use bevy_shader::load_shader_library;33use bevy_transform::{components::Transform, prelude::GlobalTransform};34use tracing::error;3536use core::{hash::Hash, ops::Deref};3738use crate::{39generate::EnvironmentMapGenerationPlugin, light_probe::environment_map::EnvironmentMapIds,40};4142pub mod environment_map;43pub mod generate;44pub mod irradiance_volume;4546/// The maximum number of each type of light probe that each view will consider.47///48/// Because the fragment shader does a linear search through the list for each49/// fragment, this number needs to be relatively small.50pub const MAX_VIEW_LIGHT_PROBES: usize = 8;5152/// How many texture bindings are used in the fragment shader, *not* counting53/// environment maps or irradiance volumes.54const STANDARD_MATERIAL_FRAGMENT_SHADER_MIN_TEXTURE_BINDINGS: usize = 16;5556/// Adds support for light probes: cuboid bounding regions that apply global57/// illumination to objects within them.58///59/// This also adds support for view environment maps: diffuse and specular60/// cubemaps applied to all objects that a view renders.61pub struct LightProbePlugin;6263/// A GPU type that stores information about a light probe.64#[derive(Clone, Copy, ShaderType, Default)]65struct RenderLightProbe {66/// The transform from the world space to the model space. This is used to67/// efficiently check for bounding box intersection.68light_from_world_transposed: [Vec4; 3],6970/// The index of the texture or textures in the appropriate binding array or71/// arrays.72///73/// For example, for reflection probes this is the index of the cubemap in74/// the diffuse and specular texture arrays.75texture_index: i32,7677/// Scale factor applied to the light generated by this light probe.78///79/// See the comment in [`EnvironmentMapLight`] for details.80intensity: f32,8182/// Whether this light probe adds to the diffuse contribution of the83/// irradiance for meshes with lightmaps.84affects_lightmapped_mesh_diffuse: u32,85}8687/// A per-view shader uniform that specifies all the light probes that the view88/// takes into account.89#[derive(ShaderType)]90pub struct LightProbesUniform {91/// The list of applicable reflection probes, sorted from nearest to the92/// camera to the farthest away from the camera.93reflection_probes: [RenderLightProbe; MAX_VIEW_LIGHT_PROBES],9495/// The list of applicable irradiance volumes, sorted from nearest to the96/// camera to the farthest away from the camera.97irradiance_volumes: [RenderLightProbe; MAX_VIEW_LIGHT_PROBES],9899/// The number of reflection probes in the list.100reflection_probe_count: i32,101102/// The number of irradiance volumes in the list.103irradiance_volume_count: i32,104105/// The index of the diffuse and specular environment maps associated with106/// the view itself. This is used as a fallback if no reflection probe in107/// the list contains the fragment.108view_cubemap_index: i32,109110/// The smallest valid mipmap level for the specular environment cubemap111/// associated with the view.112smallest_specular_mip_level_for_view: u32,113114/// The intensity of the environment cubemap associated with the view.115///116/// See the comment in [`EnvironmentMapLight`] for details.117intensity_for_view: f32,118119/// Whether the environment map attached to the view affects the diffuse120/// lighting for lightmapped meshes.121///122/// This will be 1 if the map does affect lightmapped meshes or 0 otherwise.123view_environment_map_affects_lightmapped_mesh_diffuse: u32,124}125126/// A GPU buffer that stores information about all light probes.127#[derive(Resource, Default, Deref, DerefMut)]128pub struct LightProbesBuffer(DynamicUniformBuffer<LightProbesUniform>);129130/// A component attached to each camera in the render world that stores the131/// index of the [`LightProbesUniform`] in the [`LightProbesBuffer`].132#[derive(Component, Default, Deref, DerefMut)]133pub struct ViewLightProbesUniformOffset(u32);134135/// Information that [`gather_light_probes`] keeps about each light probe.136///137/// This information is parameterized by the [`LightProbeComponent`] type. This138/// will either be [`EnvironmentMapLight`] for reflection probes or139/// [`IrradianceVolume`] for irradiance volumes.140struct LightProbeInfo<C>141where142C: LightProbeComponent,143{144// The transform from world space to light probe space.145light_from_world: Affine3A,146147// The transform from light probe space to world space.148world_from_light: Affine3A,149150// Scale factor applied to the diffuse and specular light generated by this151// reflection probe.152//153// See the comment in [`EnvironmentMapLight`] for details.154intensity: f32,155156// Whether this light probe adds to the diffuse contribution of the157// irradiance for meshes with lightmaps.158affects_lightmapped_mesh_diffuse: bool,159160// The IDs of all assets associated with this light probe.161//162// Because each type of light probe component may reference different types163// of assets (e.g. a reflection probe references two cubemap assets while an164// irradiance volume references a single 3D texture asset), this is generic.165asset_id: C::AssetId,166}167168/// A component, part of the render world, that stores the mapping from asset ID169/// or IDs to the texture index in the appropriate binding arrays.170///171/// Cubemap textures belonging to environment maps are collected into binding172/// arrays, and the index of each texture is presented to the shader for runtime173/// lookup. 3D textures belonging to reflection probes are likewise collected174/// into binding arrays, and the shader accesses the 3D texture by index.175///176/// This component is attached to each view in the render world, because each177/// view may have a different set of light probes that it considers and therefore178/// the texture indices are per-view.179#[derive(Component, Default)]180pub struct RenderViewLightProbes<C>181where182C: LightProbeComponent,183{184/// The list of environment maps presented to the shader, in order.185binding_index_to_textures: Vec<C::AssetId>,186187/// The reverse of `binding_index_to_cubemap`: a map from the texture ID to188/// the index in `binding_index_to_cubemap`.189cubemap_to_binding_index: HashMap<C::AssetId, u32>,190191/// Information about each light probe, ready for upload to the GPU, sorted192/// in order from closest to the camera to farthest.193///194/// Note that this is not necessarily ordered by binding index. So don't195/// write code like196/// `render_light_probes[cubemap_to_binding_index[asset_id]]`; instead197/// search for the light probe with the appropriate binding index in this198/// array.199render_light_probes: Vec<RenderLightProbe>,200201/// Information needed to render the light probe attached directly to the202/// view, if applicable.203///204/// A light probe attached directly to a view represents a "global" light205/// probe that affects all objects not in the bounding region of any light206/// probe. Currently, the only light probe type that supports this is the207/// [`EnvironmentMapLight`].208view_light_probe_info: C::ViewLightProbeInfo,209}210211/// A trait implemented by all components that represent light probes.212///213/// Currently, the two light probe types are [`EnvironmentMapLight`] and214/// [`IrradianceVolume`], for reflection probes and irradiance volumes215/// respectively.216///217/// Most light probe systems are written to be generic over the type of light218/// probe. This allows much of the code to be shared and enables easy addition219/// of more light probe types (e.g. real-time reflection planes) in the future.220pub trait LightProbeComponent: Send + Sync + Component + Sized {221/// Holds [`AssetId`]s of the texture or textures that this light probe222/// references.223///224/// This can just be [`AssetId`] if the light probe only references one225/// texture. If it references multiple textures, it will be a structure226/// containing those asset IDs.227type AssetId: Send + Sync + Clone + Eq + Hash;228229/// If the light probe can be attached to the view itself (as opposed to a230/// cuboid region within the scene), this contains the information that will231/// be passed to the GPU in order to render it. Otherwise, this will be232/// `()`.233///234/// Currently, only reflection probes (i.e. [`EnvironmentMapLight`]) can be235/// attached directly to views.236type ViewLightProbeInfo: Send + Sync + Default;237238/// Returns the asset ID or asset IDs of the texture or textures referenced239/// by this light probe.240fn id(&self, image_assets: &RenderAssets<GpuImage>) -> Option<Self::AssetId>;241242/// Returns the intensity of this light probe.243///244/// This is a scaling factor that will be multiplied by the value or values245/// sampled from the texture.246fn intensity(&self) -> f32;247248/// Returns true if this light probe contributes diffuse lighting to meshes249/// with lightmaps or false otherwise.250fn affects_lightmapped_mesh_diffuse(&self) -> bool;251252/// Creates an instance of [`RenderViewLightProbes`] containing all the253/// information needed to render this light probe.254///255/// This is called for every light probe in view every frame.256fn create_render_view_light_probes(257view_component: Option<&Self>,258image_assets: &RenderAssets<GpuImage>,259) -> RenderViewLightProbes<Self>;260}261262/// The uniform struct extracted from [`EnvironmentMapLight`].263/// Will be available for use in the Environment Map shader.264#[derive(Component, ShaderType, Clone)]265pub struct EnvironmentMapUniform {266/// The world space transformation matrix of the sample ray for environment cubemaps.267transform: Mat4,268}269270impl Default for EnvironmentMapUniform {271fn default() -> Self {272EnvironmentMapUniform {273transform: Mat4::IDENTITY,274}275}276}277278/// A GPU buffer that stores the environment map settings for each view.279#[derive(Resource, Default, Deref, DerefMut)]280pub struct EnvironmentMapUniformBuffer(pub DynamicUniformBuffer<EnvironmentMapUniform>);281282/// A component that stores the offset within the283/// [`EnvironmentMapUniformBuffer`] for each view.284#[derive(Component, Default, Deref, DerefMut)]285pub struct ViewEnvironmentMapUniformOffset(u32);286287impl Plugin for LightProbePlugin {288fn build(&self, app: &mut App) {289load_shader_library!(app, "light_probe.wgsl");290load_shader_library!(app, "environment_map.wgsl");291load_shader_library!(app, "irradiance_volume.wgsl");292293app.add_plugins((294EnvironmentMapGenerationPlugin,295ExtractInstancesPlugin::<EnvironmentMapIds>::new(),296));297298let Some(render_app) = app.get_sub_app_mut(RenderApp) else {299return;300};301302render_app303.init_resource::<LightProbesBuffer>()304.init_resource::<EnvironmentMapUniformBuffer>()305.add_systems(ExtractSchedule, gather_environment_map_uniform)306.add_systems(ExtractSchedule, gather_light_probes::<EnvironmentMapLight>)307.add_systems(ExtractSchedule, gather_light_probes::<IrradianceVolume>)308.add_systems(309Render,310(upload_light_probes, prepare_environment_uniform_buffer)311.in_set(RenderSystems::PrepareResources),312);313}314}315316/// Extracts [`EnvironmentMapLight`] from views and creates [`EnvironmentMapUniform`] for them.317///318/// Compared to the `ExtractComponentPlugin`, this implementation will create a default instance319/// if one does not already exist.320fn gather_environment_map_uniform(321view_query: Extract<Query<(RenderEntity, Option<&EnvironmentMapLight>), With<Camera3d>>>,322mut commands: Commands,323) {324for (view_entity, environment_map_light) in view_query.iter() {325let environment_map_uniform = if let Some(environment_map_light) = environment_map_light {326EnvironmentMapUniform {327transform: Transform::from_rotation(environment_map_light.rotation)328.to_matrix()329.inverse(),330}331} else {332EnvironmentMapUniform::default()333};334commands335.get_entity(view_entity)336.expect("Environment map light entity wasn't synced.")337.insert(environment_map_uniform);338}339}340341/// Gathers up all light probes of a single type in the scene and assigns them342/// to views, performing frustum culling and distance sorting in the process.343fn gather_light_probes<C>(344image_assets: Res<RenderAssets<GpuImage>>,345light_probe_query: Extract<Query<(&GlobalTransform, &C), With<LightProbe>>>,346view_query: Extract<347Query<(RenderEntity, &GlobalTransform, &Frustum, Option<&C>), With<Camera3d>>,348>,349mut reflection_probes: Local<Vec<LightProbeInfo<C>>>,350mut view_reflection_probes: Local<Vec<LightProbeInfo<C>>>,351mut commands: Commands,352) where353C: LightProbeComponent,354{355// Create [`LightProbeInfo`] for every light probe in the scene.356reflection_probes.clear();357reflection_probes.extend(358light_probe_query359.iter()360.filter_map(|query_row| LightProbeInfo::new(query_row, &image_assets)),361);362363// Build up the light probes uniform and the key table.364for (view_entity, view_transform, view_frustum, view_component) in view_query.iter() {365// Cull light probes outside the view frustum.366view_reflection_probes.clear();367view_reflection_probes.extend(368reflection_probes369.iter()370.filter(|light_probe_info| light_probe_info.frustum_cull(view_frustum))371.cloned(),372);373374// Sort by distance to camera.375view_reflection_probes.sort_by_cached_key(|light_probe_info| {376light_probe_info.camera_distance_sort_key(view_transform)377});378379// Create the light probes list.380let mut render_view_light_probes =381C::create_render_view_light_probes(view_component, &image_assets);382383// Gather up the light probes in the list.384render_view_light_probes.maybe_gather_light_probes(&view_reflection_probes);385386// Record the per-view light probes.387if render_view_light_probes.is_empty() {388commands389.get_entity(view_entity)390.expect("View entity wasn't synced.")391.remove::<RenderViewLightProbes<C>>();392} else {393commands394.get_entity(view_entity)395.expect("View entity wasn't synced.")396.insert(render_view_light_probes);397}398}399}400401/// Gathers up environment map settings for each applicable view and402/// writes them into a GPU buffer.403pub fn prepare_environment_uniform_buffer(404mut commands: Commands,405views: Query<(Entity, Option<&EnvironmentMapUniform>), With<ExtractedView>>,406mut environment_uniform_buffer: ResMut<EnvironmentMapUniformBuffer>,407render_device: Res<RenderDevice>,408render_queue: Res<RenderQueue>,409) {410let Some(mut writer) =411environment_uniform_buffer.get_writer(views.iter().len(), &render_device, &render_queue)412else {413return;414};415416for (view, environment_uniform) in views.iter() {417let uniform_offset = match environment_uniform {418None => 0,419Some(environment_uniform) => writer.write(environment_uniform),420};421commands422.entity(view)423.insert(ViewEnvironmentMapUniformOffset(uniform_offset));424}425}426427// A system that runs after [`gather_light_probes`] and populates the GPU428// uniforms with the results.429//430// Note that, unlike [`gather_light_probes`], this system is not generic over431// the type of light probe. It collects light probes of all types together into432// a single structure, ready to be passed to the shader.433fn upload_light_probes(434mut commands: Commands,435views: Query<Entity, With<ExtractedView>>,436mut light_probes_buffer: ResMut<LightProbesBuffer>,437mut view_light_probes_query: Query<(438Option<&RenderViewLightProbes<EnvironmentMapLight>>,439Option<&RenderViewLightProbes<IrradianceVolume>>,440)>,441render_device: Res<RenderDevice>,442render_queue: Res<RenderQueue>,443) {444// If there are no views, bail.445if views.is_empty() {446return;447}448449// Initialize the uniform buffer writer.450let mut writer = light_probes_buffer451.get_writer(views.iter().len(), &render_device, &render_queue)452.unwrap();453454// Process each view.455for view_entity in views.iter() {456let Ok((render_view_environment_maps, render_view_irradiance_volumes)) =457view_light_probes_query.get_mut(view_entity)458else {459error!("Failed to find `RenderViewLightProbes` for the view!");460continue;461};462463// Initialize the uniform with only the view environment map, if there464// is one.465let mut light_probes_uniform = LightProbesUniform {466reflection_probes: [RenderLightProbe::default(); MAX_VIEW_LIGHT_PROBES],467irradiance_volumes: [RenderLightProbe::default(); MAX_VIEW_LIGHT_PROBES],468reflection_probe_count: render_view_environment_maps469.map(RenderViewLightProbes::len)470.unwrap_or_default()471.min(MAX_VIEW_LIGHT_PROBES) as i32,472irradiance_volume_count: render_view_irradiance_volumes473.map(RenderViewLightProbes::len)474.unwrap_or_default()475.min(MAX_VIEW_LIGHT_PROBES) as i32,476view_cubemap_index: render_view_environment_maps477.map(|maps| maps.view_light_probe_info.cubemap_index)478.unwrap_or(-1),479smallest_specular_mip_level_for_view: render_view_environment_maps480.map(|maps| maps.view_light_probe_info.smallest_specular_mip_level)481.unwrap_or(0),482intensity_for_view: render_view_environment_maps483.map(|maps| maps.view_light_probe_info.intensity)484.unwrap_or(1.0),485view_environment_map_affects_lightmapped_mesh_diffuse: render_view_environment_maps486.map(|maps| maps.view_light_probe_info.affects_lightmapped_mesh_diffuse as u32)487.unwrap_or(1),488};489490// Add any environment maps that [`gather_light_probes`] found to the491// uniform.492if let Some(render_view_environment_maps) = render_view_environment_maps {493render_view_environment_maps.add_to_uniform(494&mut light_probes_uniform.reflection_probes,495&mut light_probes_uniform.reflection_probe_count,496);497}498499// Add any irradiance volumes that [`gather_light_probes`] found to the500// uniform.501if let Some(render_view_irradiance_volumes) = render_view_irradiance_volumes {502render_view_irradiance_volumes.add_to_uniform(503&mut light_probes_uniform.irradiance_volumes,504&mut light_probes_uniform.irradiance_volume_count,505);506}507508// Queue the view's uniforms to be written to the GPU.509let uniform_offset = writer.write(&light_probes_uniform);510511commands512.entity(view_entity)513.insert(ViewLightProbesUniformOffset(uniform_offset));514}515}516517impl Default for LightProbesUniform {518fn default() -> Self {519Self {520reflection_probes: [RenderLightProbe::default(); MAX_VIEW_LIGHT_PROBES],521irradiance_volumes: [RenderLightProbe::default(); MAX_VIEW_LIGHT_PROBES],522reflection_probe_count: 0,523irradiance_volume_count: 0,524view_cubemap_index: -1,525smallest_specular_mip_level_for_view: 0,526intensity_for_view: 1.0,527view_environment_map_affects_lightmapped_mesh_diffuse: 1,528}529}530}531532impl<C> LightProbeInfo<C>533where534C: LightProbeComponent,535{536/// Given the set of light probe components, constructs and returns537/// [`LightProbeInfo`]. This is done for every light probe in the scene538/// every frame.539fn new(540(light_probe_transform, environment_map): (&GlobalTransform, &C),541image_assets: &RenderAssets<GpuImage>,542) -> Option<LightProbeInfo<C>> {543environment_map.id(image_assets).map(|id| LightProbeInfo {544world_from_light: light_probe_transform.affine(),545light_from_world: light_probe_transform.affine().inverse(),546asset_id: id,547intensity: environment_map.intensity(),548affects_lightmapped_mesh_diffuse: environment_map.affects_lightmapped_mesh_diffuse(),549})550}551552/// Returns true if this light probe is in the viewing frustum of the camera553/// or false if it isn't.554fn frustum_cull(&self, view_frustum: &Frustum) -> bool {555view_frustum.intersects_obb(556&Aabb {557center: Vec3A::default(),558half_extents: Vec3A::splat(0.5),559},560&self.world_from_light,561true,562false,563)564}565566/// Returns the squared distance from this light probe to the camera,567/// suitable for distance sorting.568fn camera_distance_sort_key(&self, view_transform: &GlobalTransform) -> FloatOrd {569FloatOrd(570(self.world_from_light.translation - view_transform.translation_vec3a())571.length_squared(),572)573}574}575576impl<C> RenderViewLightProbes<C>577where578C: LightProbeComponent,579{580/// Creates a new empty list of light probes.581fn new() -> RenderViewLightProbes<C> {582RenderViewLightProbes {583binding_index_to_textures: vec![],584cubemap_to_binding_index: HashMap::default(),585render_light_probes: vec![],586view_light_probe_info: C::ViewLightProbeInfo::default(),587}588}589590/// Returns true if there are no light probes in the list.591pub(crate) fn is_empty(&self) -> bool {592self.binding_index_to_textures.is_empty()593}594595/// Returns the number of light probes in the list.596pub(crate) fn len(&self) -> usize {597self.binding_index_to_textures.len()598}599600/// Adds a cubemap to the list of bindings, if it wasn't there already, and601/// returns its index within that list.602pub(crate) fn get_or_insert_cubemap(&mut self, cubemap_id: &C::AssetId) -> u32 {603*self604.cubemap_to_binding_index605.entry((*cubemap_id).clone())606.or_insert_with(|| {607let index = self.binding_index_to_textures.len() as u32;608self.binding_index_to_textures.push((*cubemap_id).clone());609index610})611}612613/// Adds all the light probes in this structure to the supplied array, which614/// is expected to be shipped to the GPU.615fn add_to_uniform(616&self,617render_light_probes: &mut [RenderLightProbe; MAX_VIEW_LIGHT_PROBES],618render_light_probe_count: &mut i32,619) {620render_light_probes[0..self.render_light_probes.len()]621.copy_from_slice(&self.render_light_probes[..]);622*render_light_probe_count = self.render_light_probes.len() as i32;623}624625/// Gathers up all light probes of the given type in the scene and records626/// them in this structure.627fn maybe_gather_light_probes(&mut self, light_probes: &[LightProbeInfo<C>]) {628for light_probe in light_probes.iter().take(MAX_VIEW_LIGHT_PROBES) {629// Determine the index of the cubemap in the binding array.630let cubemap_index = self.get_or_insert_cubemap(&light_probe.asset_id);631632// Transpose the inverse transform to compress the structure on the633// GPU (from 4 `Vec4`s to 3 `Vec4`s). The shader will transpose it634// to recover the original inverse transform.635let light_from_world_transposed = Mat4::from(light_probe.light_from_world).transpose();636637// Write in the light probe data.638self.render_light_probes.push(RenderLightProbe {639light_from_world_transposed: [640light_from_world_transposed.x_axis,641light_from_world_transposed.y_axis,642light_from_world_transposed.z_axis,643],644texture_index: cubemap_index as i32,645intensity: light_probe.intensity,646affects_lightmapped_mesh_diffuse: light_probe.affects_lightmapped_mesh_diffuse647as u32,648});649}650}651}652653impl<C> Clone for LightProbeInfo<C>654where655C: LightProbeComponent,656{657fn clone(&self) -> Self {658Self {659light_from_world: self.light_from_world,660world_from_light: self.world_from_light,661intensity: self.intensity,662affects_lightmapped_mesh_diffuse: self.affects_lightmapped_mesh_diffuse,663asset_id: self.asset_id.clone(),664}665}666}667668/// Adds a diffuse or specular texture view to the `texture_views` list, and669/// populates `sampler` if this is the first such view.670pub(crate) fn add_cubemap_texture_view<'a>(671texture_views: &mut Vec<&'a <TextureView as Deref>::Target>,672sampler: &mut Option<&'a Sampler>,673image_id: AssetId<Image>,674images: &'a RenderAssets<GpuImage>,675fallback_image: &'a FallbackImage,676) {677match images.get(image_id) {678None => {679// Use the fallback image if the cubemap isn't loaded yet.680texture_views.push(&*fallback_image.cube.texture_view);681}682Some(image) => {683// If this is the first texture view, populate `sampler`.684if sampler.is_none() {685*sampler = Some(&image.sampler);686}687688texture_views.push(&*image.texture_view);689}690}691}692693/// Many things can go wrong when attempting to use texture binding arrays694/// (a.k.a. bindless textures). This function checks for these pitfalls:695///696/// 1. If GLSL support is enabled at the feature level, then in debug mode697/// `naga_oil` will attempt to compile all shader modules under GLSL to check698/// validity of names, even if GLSL isn't actually used. This will cause a crash699/// if binding arrays are enabled, because binding arrays are currently700/// unimplemented in the GLSL backend of Naga. Therefore, we disable binding701/// arrays if the `shader_format_glsl` feature is present.702///703/// 2. If there aren't enough texture bindings available to accommodate all the704/// binding arrays, the driver will panic. So we also bail out if there aren't705/// enough texture bindings available in the fragment shader.706///707/// 3. If binding arrays aren't supported on the hardware, then we obviously708/// can't use them. Adreno <= 610 claims to support bindless, but seems to be709/// too buggy to be usable.710///711/// 4. If binding arrays are supported on the hardware, but they can only be712/// accessed by uniform indices, that's not good enough, and we bail out.713///714/// If binding arrays aren't usable, we disable reflection probes and limit the715/// number of irradiance volumes in the scene to 1.716pub(crate) fn binding_arrays_are_usable(717render_device: &RenderDevice,718render_adapter: &RenderAdapter,719) -> bool {720let adapter_info = RenderAdapterInfo(WgpuWrapper::new(render_adapter.get_info()));721722!cfg!(feature = "shader_format_glsl")723&& bevy_render::get_adreno_model(&adapter_info).is_none_or(|model| model > 610)724&& render_device.limits().max_storage_textures_per_shader_stage725>= (STANDARD_MATERIAL_FRAGMENT_SHADER_MIN_TEXTURE_BINDINGS + MAX_VIEW_LIGHT_PROBES)726as u32727&& render_device.features().contains(728WgpuFeatures::TEXTURE_BINDING_ARRAY729| WgpuFeatures::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING,730)731}732733734