Skip to content

Commit f011fea

Browse files
JonahPlusPlusjames7132
authored andcommitted
Make AsBindGroup unsized (bevyengine#6937)
# Objective `AsBindGroup` can't be used as a trait object because of the constraint `Sized` and because of the associated function. This is a problem for [`bevy_atmosphere`](https://github.com/JonahPlusPlus/bevy_atmosphere) because it needs to use a trait that depends on `AsBindGroup` as a trait object, for switching out different shaders at runtime. The current solution it employs is reimplementing the trait and derive macro into that trait, instead of constraining to `AsBindGroup`. ## Solution Remove the `Sized` constraint from `AsBindGroup` and add the constraint `where Self: Sized` to the associated function `bind_group_layout`. Also change `PreparedBindGroup<T: AsBindGroup>` to `PreparedBindGroup<T>` and use it as `PreparedBindGroup<Self::Data>` instead of `PreparedBindGroup<Self>`. This weakens the constraints, but increases the flexibility of `AsBindGroup`. I'm not entirely sure why the `Sized` constraint was there, because it worked fine without it (maybe @cart wasn't aware of use cases for `AsBindGroup` as a trait object or this was just leftover from legacy code?). --- ## Changelog - `AsBindGroup` can be used as a trait object.
1 parent 5fe0adb commit f011fea

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

crates/bevy_render/macros/src/as_bind_group.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ pub fn derive_as_bind_group(ast: syn::DeriveInput) -> Result<TokenStream> {
371371
render_device: &#render_path::renderer::RenderDevice,
372372
images: &#render_path::render_asset::RenderAssets<#render_path::texture::Image>,
373373
fallback_image: &#render_path::texture::FallbackImage,
374-
) -> Result<#render_path::render_resource::PreparedBindGroup<Self>, #render_path::render_resource::AsBindGroupError> {
374+
) -> Result<#render_path::render_resource::PreparedBindGroup<Self::Data>, #render_path::render_resource::AsBindGroupError> {
375375
let bindings = vec![#(#binding_impls,)*];
376376

377377
let bind_group = {

crates/bevy_render/src/render_resource/bind_group.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl Deref for BindGroup {
253253
/// }
254254
/// }
255255
/// ```
256-
pub trait AsBindGroup: Sized {
256+
pub trait AsBindGroup {
257257
/// Data that will be stored alongside the "prepared" bind group.
258258
type Data: Send + Sync;
259259

@@ -264,10 +264,12 @@ pub trait AsBindGroup: Sized {
264264
render_device: &RenderDevice,
265265
images: &RenderAssets<Image>,
266266
fallback_image: &FallbackImage,
267-
) -> Result<PreparedBindGroup<Self>, AsBindGroupError>;
267+
) -> Result<PreparedBindGroup<Self::Data>, AsBindGroupError>;
268268

269269
/// Creates the bind group layout matching all bind groups returned by [`AsBindGroup::as_bind_group`]
270-
fn bind_group_layout(render_device: &RenderDevice) -> BindGroupLayout;
270+
fn bind_group_layout(render_device: &RenderDevice) -> BindGroupLayout
271+
where
272+
Self: Sized;
271273
}
272274

273275
/// An error that occurs during [`AsBindGroup::as_bind_group`] calls.
@@ -277,10 +279,10 @@ pub enum AsBindGroupError {
277279
}
278280

279281
/// A prepared bind group returned as a result of [`AsBindGroup::as_bind_group`].
280-
pub struct PreparedBindGroup<T: AsBindGroup> {
282+
pub struct PreparedBindGroup<T> {
281283
pub bindings: Vec<OwnedBindingResource>,
282284
pub bind_group: BindGroup,
283-
pub data: T::Data,
285+
pub data: T,
284286
}
285287

286288
/// An owned binding resource of any type (ex: a [`Buffer`], [`TextureView`], etc).

examples/3d/skybox.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl AsBindGroup for CubemapMaterial {
227227
render_device: &RenderDevice,
228228
images: &RenderAssets<Image>,
229229
_fallback_image: &FallbackImage,
230-
) -> Result<PreparedBindGroup<Self>, AsBindGroupError> {
230+
) -> Result<PreparedBindGroup<Self::Data>, AsBindGroupError> {
231231
let base_color_texture = self
232232
.base_color_texture
233233
.as_ref()

0 commit comments

Comments
 (0)