diff --git a/crates/bevy_render/src/camera.rs b/crates/bevy_render/src/camera.rs index 5c9cf002c2c07..84988a2fc6534 100644 --- a/crates/bevy_render/src/camera.rs +++ b/crates/bevy_render/src/camera.rs @@ -213,9 +213,9 @@ impl NormalizedRenderTargetExt for NormalizedRenderTarget { NormalizedRenderTarget::Image(image_target) => images .get(&image_target.handle) .map(|image| image.texture_format), - NormalizedRenderTarget::TextureView(id) => manual_texture_views - .get(id) - .map(|view| view.texture_view.texture().format()), + NormalizedRenderTarget::TextureView(id) => { + manual_texture_views.get(id).map(|tex| tex.format) + } NormalizedRenderTarget::None { .. } => None, } } diff --git a/crates/bevy_render/src/texture/manual_texture_view.rs b/crates/bevy_render/src/texture/manual_texture_view.rs index b03943d3c2750..b9faa84ab78f6 100644 --- a/crates/bevy_render/src/texture/manual_texture_view.rs +++ b/crates/bevy_render/src/texture/manual_texture_view.rs @@ -1,8 +1,10 @@ use bevy_camera::ManualTextureViewHandle; use bevy_ecs::{prelude::Component, resource::Resource}; +use bevy_image::BevyDefault; use bevy_math::UVec2; use bevy_platform::collections::HashMap; use bevy_render_macros::ExtractResource; +use wgpu::TextureFormat; use crate::render_resource::TextureView; @@ -11,11 +13,16 @@ use crate::render_resource::TextureView; pub struct ManualTextureView { pub texture_view: TextureView, pub size: UVec2, + pub format: TextureFormat, } impl ManualTextureView { - pub fn new(texture_view: TextureView, size: UVec2) -> Self { - Self { texture_view, size } + pub fn with_default_format(texture_view: TextureView, size: UVec2) -> Self { + Self { + texture_view, + size, + format: TextureFormat::bevy_default(), + } } } @@ -29,7 +36,7 @@ impl ManualTextureView { /// # world.insert_resource(ManualTextureViews::default()); /// # let texture_view = todo!(); /// let manual_views = world.resource_mut::(); -/// let manual_view = ManualTextureView::new(texture_view, UVec2::new(1024, 1024)); +/// let manual_view = ManualTextureView::with_default_format(texture_view, UVec2::new(1024, 1024)); /// /// // Choose an unused handle value; it's likely only you are inserting manual views. /// const MANUAL_VIEW_HANDLE: ManualTextureViewHandle = ManualTextureViewHandle::new(42); diff --git a/crates/bevy_render/src/texture/texture_attachment.rs b/crates/bevy_render/src/texture/texture_attachment.rs index 26588212683c1..269c2f1422820 100644 --- a/crates/bevy_render/src/texture/texture_attachment.rs +++ b/crates/bevy_render/src/texture/texture_attachment.rs @@ -1,5 +1,5 @@ use super::CachedTexture; -use crate::render_resource::TextureView; +use crate::render_resource::{TextureFormat, TextureView}; use alloc::sync::Arc; use bevy_color::LinearRgba; use core::sync::atomic::{AtomicBool, Ordering}; @@ -127,13 +127,15 @@ impl DepthAttachment { #[derive(Clone)] pub struct OutputColorAttachment { pub view: TextureView, + pub format: TextureFormat, is_first_call: Arc, } impl OutputColorAttachment { - pub fn new(view: TextureView) -> Self { + pub fn new(view: TextureView, format: TextureFormat) -> Self { Self { view, + format, is_first_call: Arc::new(AtomicBool::new(true)), } } diff --git a/crates/bevy_render/src/view/mod.rs b/crates/bevy_render/src/view/mod.rs index cc39266a6ddec..e9898c9deaddf 100644 --- a/crates/bevy_render/src/view/mod.rs +++ b/crates/bevy_render/src/view/mod.rs @@ -848,7 +848,7 @@ impl ViewTarget { /// The format of the final texture this view will render to #[inline] pub fn out_texture_format(&self) -> TextureFormat { - self.out_texture.view.texture().format() + self.out_texture.format } /// This will start a new "post process write", which assumes that the caller @@ -1024,7 +1024,10 @@ pub fn prepare_view_attachments( let Some(attachment) = target .get_texture_view(&windows, &images, &manual_texture_views) .cloned() - .map(OutputColorAttachment::new) + .zip(target.get_texture_format(&windows, &images, &manual_texture_views)) + .map(|(view, format)| { + OutputColorAttachment::new(view.clone(), format.add_srgb_suffix()) + }) else { continue; }; @@ -1093,7 +1096,11 @@ pub fn prepare_view_targets( dimension: TextureDimension::D2, format: main_texture_format, usage: texture_usage.0, - view_formats: &[], + view_formats: match main_texture_format { + TextureFormat::Bgra8Unorm => &[TextureFormat::Bgra8UnormSrgb], + TextureFormat::Rgba8Unorm => &[TextureFormat::Rgba8UnormSrgb], + _ => &[], + }, }; let a = texture_cache.get( &render_device, diff --git a/crates/bevy_render/src/view/window/screenshot.rs b/crates/bevy_render/src/view/window/screenshot.rs index 4c5f60ac34c1c..4aba6c6cee791 100644 --- a/crates/bevy_render/src/view/window/screenshot.rs +++ b/crates/bevy_render/src/view/window/screenshot.rs @@ -295,7 +295,7 @@ fn prepare_screenshots( prepared.insert(*entity, state); view_target_attachments.insert( target.clone(), - OutputColorAttachment::new(texture_view.clone()), + OutputColorAttachment::new(texture_view.clone(), format.add_srgb_suffix()), ); } NormalizedRenderTarget::Image(image) => { @@ -315,7 +315,7 @@ fn prepare_screenshots( prepared.insert(*entity, state); view_target_attachments.insert( target.clone(), - OutputColorAttachment::new(texture_view.clone()), + OutputColorAttachment::new(texture_view.clone(), format.add_srgb_suffix()), ); } NormalizedRenderTarget::TextureView(texture_view) => { @@ -326,7 +326,7 @@ fn prepare_screenshots( ); continue; }; - let format = manual_texture_view.texture_view.texture().format(); + let format = manual_texture_view.format; let size = manual_texture_view.size.to_extents(); let (texture_view, state) = prepare_screenshot_state( size, @@ -339,7 +339,7 @@ fn prepare_screenshots( prepared.insert(*entity, state); view_target_attachments.insert( target.clone(), - OutputColorAttachment::new(texture_view.clone()), + OutputColorAttachment::new(texture_view.clone(), format.add_srgb_suffix()), ); } NormalizedRenderTarget::None { .. } => { @@ -550,7 +550,7 @@ pub(crate) fn submit_screenshot_commands(world: &World, encoder: &mut CommandEnc }; let width = texture_view.size.x; let height = texture_view.size.y; - let texture_format = texture_view.texture_view.texture().format(); + let texture_format = texture_view.format; let texture_view = texture_view.texture_view.deref(); render_screenshot( encoder,