Skip to content

Optional explicit compressed image format support #19190

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 11 additions & 5 deletions crates/bevy_gltf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ extern crate alloc;

use alloc::sync::Arc;
use std::sync::Mutex;
use tracing::warn;

use bevy_platform::collections::HashMap;

use bevy_app::prelude::*;
use bevy_asset::AssetApp;
use bevy_ecs::prelude::Resource;
use bevy_image::{CompressedImageFormats, ImageSamplerDescriptor};
use bevy_image::{CompressedImageFormatSupport, CompressedImageFormats, ImageSamplerDescriptor};
use bevy_mesh::MeshVertexAttribute;
use bevy_render::renderer::RenderDevice;

/// The glTF prelude.
///
Expand Down Expand Up @@ -204,10 +204,16 @@ impl Plugin for GltfPlugin {
}

fn finish(&self, app: &mut App) {
let supported_compressed_formats = match app.world().get_resource::<RenderDevice>() {
Some(render_device) => CompressedImageFormats::from_features(render_device.features()),
None => CompressedImageFormats::NONE,
let supported_compressed_formats = if let Some(resource) =
app.world().get_resource::<CompressedImageFormatSupport>()
{
resource.0
} else {
warn!("CompressedImageFormatSupport resource not found. It should either be initialized in finish() of \
RenderPlugin, or manually if not using the RenderPlugin or the WGPU backend.");
CompressedImageFormats::NONE
};

let default_sampler_resource = DefaultGltfImageSampler::new(&self.default_sampler);
let default_sampler = default_sampler_resource.get_internal();
app.insert_resource(default_sampler_resource);
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_image/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ bevy_color = { path = "../bevy_color", version = "0.16.0-dev", features = [
"serialize",
"wgpu-types",
] }
bevy_ecs = { path = "../bevy_ecs", version = "0.16.0-dev", default-features = false }
Copy link
Contributor

Choose a reason for hiding this comment

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

For the benefit of other reviewers - this is not adding a dependency, just making it explicit. Previously was implicit bevy_image -> bevy_app -> bevy_ecs.

bevy_math = { path = "../bevy_math", version = "0.16.0-dev" }
bevy_reflect = { path = "../bevy_reflect", version = "0.16.0-dev" }
bevy_utils = { path = "../bevy_utils", version = "0.16.0-dev" }
Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_image/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use bevy_reflect::{std_traits::ReflectDefault, Reflect};

use bevy_asset::{Asset, RenderAssetUsages};
use bevy_color::{Color, ColorToComponents, Gray, LinearRgba, Srgba, Xyza};
use bevy_ecs::resource::Resource;
use bevy_math::{AspectRatio, UVec2, UVec3, Vec2};
use core::hash::Hash;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -1651,6 +1652,12 @@ impl CompressedImageFormats {
}
}

/// For defining which compressed image formats are supported. This will be initialized from available device features
/// in `finish()` of the bevy `RenderPlugin`, but is left for the user to specify if not using the `RenderPlugin`, or
/// the WGPU backend.
#[derive(Resource)]
pub struct CompressedImageFormatSupport(pub CompressedImageFormats);

#[cfg(test)]
mod test {
use super::*;
Expand Down
8 changes: 7 additions & 1 deletion crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub mod prelude {
}
use batching::gpu_preprocessing::BatchingPlugin;
use bevy_ecs::schedule::ScheduleBuildSettings;
use bevy_image::{CompressedImageFormatSupport, CompressedImageFormats};
use bevy_utils::prelude::default;
pub use extract_param::Extract;

Expand Down Expand Up @@ -462,10 +463,15 @@ impl Plugin for RenderPlugin {
let RenderResources(device, queue, adapter_info, render_adapter, instance) =
future_render_resources.0.lock().unwrap().take().unwrap();

let compressed_image_format_support = CompressedImageFormatSupport(
CompressedImageFormats::from_features(device.features()),
);

app.insert_resource(device.clone())
.insert_resource(queue.clone())
.insert_resource(adapter_info.clone())
.insert_resource(render_adapter.clone());
.insert_resource(render_adapter.clone())
.insert_resource(compressed_image_format_support);

let render_app = app.sub_app_mut(RenderApp);

Expand Down
20 changes: 14 additions & 6 deletions crates/bevy_render/src/texture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ pub use crate::render_resource::DefaultImageSampler;
use bevy_image::CompressedImageSaver;
#[cfg(feature = "hdr")]
use bevy_image::HdrTextureLoader;
use bevy_image::{CompressedImageFormats, Image, ImageLoader, ImageSamplerDescriptor};
use bevy_image::{
CompressedImageFormatSupport, CompressedImageFormats, Image, ImageLoader,
ImageSamplerDescriptor,
};
pub use fallback_image::*;
pub use gpu_image::*;
pub use texture_attachment::*;
Expand All @@ -20,6 +23,7 @@ use crate::{
use bevy_app::{App, Plugin};
use bevy_asset::{weak_handle, AssetApp, Assets, Handle};
use bevy_ecs::prelude::*;
use tracing::warn;

/// A handle to a 1 x 1 transparent white image.
///
Expand Down Expand Up @@ -111,12 +115,16 @@ impl Plugin for ImagePlugin {

fn finish(&self, app: &mut App) {
if !ImageLoader::SUPPORTED_FORMATS.is_empty() {
let supported_compressed_formats = match app.world().get_resource::<RenderDevice>() {
Some(render_device) => {
CompressedImageFormats::from_features(render_device.features())
}
None => CompressedImageFormats::NONE,
let supported_compressed_formats = if let Some(resource) =
app.world().get_resource::<CompressedImageFormatSupport>()
{
resource.0
} else {
warn!("CompressedImageFormatSupport resource not found. It should either be initialized in finish() of \
RenderPlugin, or manually if not using the RenderPlugin or the WGPU backend.");
CompressedImageFormats::NONE
};

app.register_asset_loader(ImageLoader::new(supported_compressed_formats));
}

Expand Down