Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply codebase changes in preparation for StandardMaterial transmission #8704

Merged
merged 14 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Introduce FallbackImageZero
  • Loading branch information
coreh committed May 28, 2023
commit a12419df8d0f9e867704942979726f371cd5a94d
37 changes: 34 additions & 3 deletions crates/bevy_render/src/texture/fallback_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@ use crate::{
/// A [`RenderApp`](crate::RenderApp) resource that contains the default "fallback image",
/// which can be used in situations where an image was not explicitly defined. The most common
/// use case is [`AsBindGroup`] implementations (such as materials) that support optional textures.
/// [`FallbackImage`] defaults to a 1x1 fully white texture, making blending colors with it a no-op.
/// [`FallbackImage`] defaults to a 1x1 fully white texture, making multiplying colors with it a no-op.
#[derive(Resource, Deref)]
pub struct FallbackImage(GpuImage);

/// A [`RenderApp`](crate::RenderApp) resource that contains a _zero-filled_ "fallback image",
/// which can be used in place of [`FallbackImage`], when a fully transparent or black fallback
/// is required instead of fully opaque white.
///
/// Defaults to a 1x1 fully transparent black texture (0.0, 0.0, 0.0, 0.0), which makes adding
/// or alpha-blending it to other colors a no-op.
#[derive(Resource, Deref)]
pub struct FallbackImageZero(GpuImage);
Copy link
Contributor

@nicopap nicopap May 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered using "WhiteFallbackImage" and "TransparentBlackFallbackImage"? Though I understand why you'd want to keep the current names over the ones I'm suggesting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... I tried out a bunch of different names/orders, but they all felt weird. I didn't want to rename the existing FallbackImage needlessly since that would introduce a breaking change for no good reason, and since there was already FallbackImageCubemap (with the “qualifier” at the end) I just went with the same pattern for consistency.

I can rename all of them if there's significant demand, but since we might want to be able to (eventually) create arbitrary-color fallbacks, maybe we can save the breaking change for then?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine as-is for now


/// A [`RenderApp`](crate::RenderApp) resource that contains a "cubemap fallback image",
/// which can be used in situations where an image was not explicitly defined. The most common
/// use case is [`AsBindGroup`] implementations (such as materials) that support optional textures.
Expand All @@ -34,9 +43,10 @@ fn fallback_image_new(
format: TextureFormat,
dimension: TextureViewDimension,
samples: u32,
value: u8,
) -> GpuImage {
// TODO make this configurable
let data = vec![255; format.pixel_size()];
// TODO make this configurable per channel
let data = vec![value; format.pixel_size()];

let extents = Extent3d {
width: 1,
Expand Down Expand Up @@ -92,6 +102,24 @@ impl FromWorld for FallbackImage {
TextureFormat::bevy_default(),
TextureViewDimension::D2,
1,
255,
))
}
}

impl FromWorld for FallbackImageZero {
fn from_world(world: &mut bevy_ecs::prelude::World) -> Self {
let render_device = world.resource::<RenderDevice>();
let render_queue = world.resource::<RenderQueue>();
let default_sampler = world.resource::<DefaultImageSampler>();
Self(fallback_image_new(
render_device,
render_queue,
default_sampler,
TextureFormat::bevy_default(),
TextureViewDimension::D2,
1,
0,
))
}
}
Expand All @@ -108,6 +136,7 @@ impl FromWorld for FallbackImageCubemap {
TextureFormat::bevy_default(),
TextureViewDimension::Cube,
1,
255,
))
}
}
Expand Down Expand Up @@ -148,6 +177,7 @@ impl<'w> FallbackImagesMsaa<'w> {
TextureFormat::bevy_default(),
TextureViewDimension::D2,
sample_count,
255,
)
})
}
Expand All @@ -171,6 +201,7 @@ impl<'w> FallbackImagesDepth<'w> {
TextureFormat::Depth32Float,
TextureViewDimension::D2,
sample_count,
255,
)
})
}
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_render/src/texture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ impl Plugin for ImagePlugin {
render_app
.insert_resource(DefaultImageSampler(default_sampler))
.init_resource::<FallbackImage>()
.init_resource::<FallbackImageZero>()
.init_resource::<FallbackImageCubemap>()
.init_resource::<FallbackImageMsaaCache>()
.init_resource::<FallbackImageDepthCache>();
Expand Down