Skip to content

Commit 66cf812

Browse files
authored
make bevy_render not depend on bevy_light (#20604)
# Objective - Use manual extract to remove a dep i thought was needed cus of orphan rule ## Solution - do it ## Testing - lighting example
1 parent 3d608e7 commit 66cf812

File tree

7 files changed

+74
-56
lines changed

7 files changed

+74
-56
lines changed

crates/bevy_pbr/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ bevy_mesh = { path = "../bevy_mesh", version = "0.17.0-dev" }
4747
bevy_shader = { path = "../bevy_shader", version = "0.17.0-dev" }
4848
bevy_math = { path = "../bevy_math", version = "0.17.0-dev" }
4949
bevy_reflect = { path = "../bevy_reflect", version = "0.17.0-dev" }
50-
bevy_render = { path = "../bevy_render", features = [
51-
"bevy_light",
52-
], version = "0.17.0-dev" }
50+
bevy_render = { path = "../bevy_render", version = "0.17.0-dev" }
5351
bevy_camera = { path = "../bevy_camera", version = "0.17.0-dev" }
5452
bevy_tasks = { path = "../bevy_tasks", version = "0.17.0-dev", optional = true }
5553
bevy_transform = { path = "../bevy_transform", version = "0.17.0-dev" }

crates/bevy_pbr/src/decal/clustered.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ use bevy_ecs::{
2525
query::With,
2626
resource::Resource,
2727
schedule::IntoScheduleConfigs as _,
28-
system::{Query, Res, ResMut},
28+
system::{Commands, Local, Query, Res, ResMut},
2929
};
3030
use bevy_image::Image;
3131
use bevy_light::{ClusteredDecal, DirectionalLightTexture, PointLightTexture, SpotLightTexture};
3232
use bevy_math::Mat4;
3333
use bevy_platform::collections::HashMap;
3434
use bevy_render::{
35-
extract_component::ExtractComponentPlugin,
3635
render_asset::RenderAssets,
3736
render_resource::{
3837
binding_types, BindGroupLayoutEntryBuilder, Buffer, BufferUsages, RawBufferVec, Sampler,
3938
SamplerBindingType, ShaderType, TextureSampleType, TextureView,
4039
},
4140
renderer::{RenderAdapter, RenderDevice, RenderQueue},
41+
sync_component::SyncComponentPlugin,
4242
sync_world::RenderEntity,
4343
texture::{FallbackImage, GpuImage},
4444
Extract, ExtractSchedule, Render, RenderApp, RenderSystems,
@@ -142,7 +142,7 @@ impl Plugin for ClusteredDecalPlugin {
142142
fn build(&self, app: &mut App) {
143143
load_shader_library!(app, "clustered.wgsl");
144144

145-
app.add_plugins(ExtractComponentPlugin::<ClusteredDecal>::default());
145+
app.add_plugins(SyncComponentPlugin::<ClusteredDecal>::default());
146146

147147
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
148148
return;
@@ -151,7 +151,7 @@ impl Plugin for ClusteredDecalPlugin {
151151
render_app
152152
.init_resource::<DecalsBuffer>()
153153
.init_resource::<RenderClusteredDecals>()
154-
.add_systems(ExtractSchedule, extract_decals)
154+
.add_systems(ExtractSchedule, (extract_decals, extract_clustered_decal))
155155
.add_systems(
156156
Render,
157157
prepare_decals
@@ -165,6 +165,21 @@ impl Plugin for ClusteredDecalPlugin {
165165
}
166166
}
167167

168+
// This is needed because of the orphan rule not allowing implementing
169+
// foreign trait ExtractComponent on foreign type ClusteredDecal
170+
fn extract_clustered_decal(
171+
mut commands: Commands,
172+
mut previous_len: Local<usize>,
173+
query: Extract<Query<(RenderEntity, &ClusteredDecal)>>,
174+
) {
175+
let mut values = Vec::with_capacity(*previous_len);
176+
for (entity, query_item) in &query {
177+
values.push((entity, query_item.clone()));
178+
}
179+
*previous_len = values.len();
180+
commands.try_insert_batch(values);
181+
}
182+
168183
/// The GPU data structure that stores information about each decal.
169184
#[derive(Clone, Copy, Default, ShaderType, Pod, Zeroable)]
170185
#[repr(C)]

crates/bevy_pbr/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ use bevy_image::{Image, ImageSampler};
134134
use bevy_render::{
135135
alpha::AlphaMode,
136136
camera::sort_cameras,
137-
extract_component::ExtractComponentPlugin,
138137
extract_resource::ExtractResourcePlugin,
139138
render_graph::RenderGraph,
140139
render_resource::{
@@ -229,10 +228,9 @@ impl Plugin for PbrPlugin {
229228
..Default::default()
230229
},
231230
ScreenSpaceAmbientOcclusionPlugin,
232-
ExtractResourcePlugin::<AmbientLight>::default(),
233231
FogPlugin,
234232
ExtractResourcePlugin::<DefaultOpaqueRendererMethod>::default(),
235-
ExtractComponentPlugin::<ShadowFilteringMethod>::default(),
233+
SyncComponentPlugin::<ShadowFilteringMethod>::default(),
236234
LightmapPlugin,
237235
LightProbePlugin,
238236
LightPlugin,
@@ -248,7 +246,7 @@ impl Plugin for PbrPlugin {
248246
SyncComponentPlugin::<DirectionalLight>::default(),
249247
SyncComponentPlugin::<PointLight>::default(),
250248
SyncComponentPlugin::<SpotLight>::default(),
251-
ExtractComponentPlugin::<AmbientLight>::default(),
249+
SyncComponentPlugin::<AmbientLight>::default(),
252250
))
253251
.add_plugins(AtmospherePlugin)
254252
.configure_sets(
@@ -325,6 +323,9 @@ impl Plugin for PbrPlugin {
325323
(
326324
extract_clusters,
327325
extract_lights,
326+
extract_ambient_light_resource,
327+
extract_ambient_light,
328+
extract_shadow_filtering_method,
328329
late_sweep_material_instances,
329330
),
330331
)

crates/bevy_pbr/src/render/light.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use bevy_light::cluster::GlobalVisibleClusterableObjects;
2525
use bevy_light::{
2626
spot_light_clip_from_view, spot_light_world_from_view, AmbientLight, CascadeShadowConfig,
2727
Cascades, DirectionalLight, DirectionalLightShadowMap, NotShadowCaster, PointLight,
28-
PointLightShadowMap, SpotLight, VolumetricLight,
28+
PointLightShadowMap, ShadowFilteringMethod, SpotLight, VolumetricLight,
2929
};
3030
use bevy_math::{ops, Mat4, UVec4, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles};
3131
use bevy_platform::collections::{HashMap, HashSet};
@@ -224,6 +224,54 @@ pub fn init_shadow_samplers(mut commands: Commands, render_device: Res<RenderDev
224224
});
225225
}
226226

227+
// This is needed because of the orphan rule not allowing implementing
228+
// foreign trait ExtractComponent on foreign type ShadowFilteringMethod
229+
pub fn extract_shadow_filtering_method(
230+
mut commands: Commands,
231+
mut previous_len: Local<usize>,
232+
query: Extract<Query<(RenderEntity, &ShadowFilteringMethod)>>,
233+
) {
234+
let mut values = Vec::with_capacity(*previous_len);
235+
for (entity, query_item) in &query {
236+
values.push((entity, *query_item));
237+
}
238+
*previous_len = values.len();
239+
commands.try_insert_batch(values);
240+
}
241+
242+
// This is needed because of the orphan rule not allowing implementing
243+
// foreign trait ExtractResource on foreign type AmbientLight
244+
pub fn extract_ambient_light_resource(
245+
mut commands: Commands,
246+
main_resource: Extract<Option<Res<AmbientLight>>>,
247+
target_resource: Option<ResMut<AmbientLight>>,
248+
) {
249+
if let Some(main_resource) = main_resource.as_ref() {
250+
if let Some(mut target_resource) = target_resource {
251+
if main_resource.is_changed() {
252+
*target_resource = (*main_resource).clone();
253+
}
254+
} else {
255+
commands.insert_resource((*main_resource).clone());
256+
}
257+
}
258+
}
259+
260+
// This is needed because of the orphan rule not allowing implementing
261+
// foreign trait ExtractComponent on foreign type AmbientLight
262+
pub fn extract_ambient_light(
263+
mut commands: Commands,
264+
mut previous_len: Local<usize>,
265+
query: Extract<Query<(RenderEntity, &AmbientLight)>>,
266+
) {
267+
let mut values = Vec::with_capacity(*previous_len);
268+
for (entity, query_item) in &query {
269+
values.push((entity, query_item.clone()));
270+
}
271+
*previous_len = values.len();
272+
commands.try_insert_batch(values);
273+
}
274+
227275
pub fn extract_lights(
228276
mut commands: Commands,
229277
point_light_shadow_map: Extract<Res<PointLightShadowMap>>,

crates/bevy_render/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ bevy_image = { path = "../bevy_image", version = "0.17.0-dev" }
6868
bevy_mesh = { path = "../bevy_mesh", version = "0.17.0-dev" }
6969
bevy_camera = { path = "../bevy_camera", version = "0.17.0-dev" }
7070
bevy_shader = { path = "../bevy_shader", version = "0.17.0-dev" }
71-
bevy_light = { path = "../bevy_light", optional = true, version = "0.17.0-dev" }
7271
bevy_platform = { path = "../bevy_platform", version = "0.17.0-dev", default-features = false, features = [
7372
"std",
7473
"serialize",

crates/bevy_render/src/extract_impls.rs

Lines changed: 0 additions & 41 deletions
This file was deleted.

crates/bevy_render/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ pub mod diagnostic;
4242
pub mod erased_render_asset;
4343
pub mod experimental;
4444
pub mod extract_component;
45-
#[cfg(feature = "bevy_light")]
46-
mod extract_impls;
4745
pub mod extract_instances;
4846
mod extract_param;
4947
pub mod extract_resource;

0 commit comments

Comments
 (0)