From b6376ce6546291432f5b6bdb4b51af8d9fa4526d Mon Sep 17 00:00:00 2001 From: Torstein Grindvik Date: Fri, 27 Jan 2023 18:41:01 +0000 Subject: [PATCH] Add main_texture_other (#7343) # Objective ## Use Case A render node which calls `post_process_write()` on a `ViewTarget` multiple times during a single run of the node means both main textures of this view target is accessed. If the source texture (which alternate between main textures **a** and **b**) is accessed in a shader during those iterations it means that those textures have to be bound using bind groups. Preparing bind groups for both main textures ahead of time is desired, which means having access to the _other_ main texture is needed. ## Solution Add a method on `ViewTarget` for accessing the other main texture. --- ## Changelog ### Added - `main_texture_other` API on `ViewTarget` --- crates/bevy_render/src/view/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/bevy_render/src/view/mod.rs b/crates/bevy_render/src/view/mod.rs index 011d1971e7..b62222efc8 100644 --- a/crates/bevy_render/src/view/mod.rs +++ b/crates/bevy_render/src/view/mod.rs @@ -182,6 +182,20 @@ impl ViewTarget { } } + /// The _other_ "main" unsampled texture. + /// In most cases you should use [`Self::main_texture`] instead and never this. + /// The textures will naturally be swapped when [`Self::post_process_write`] is called. + /// + /// A use case for this is to be able to prepare a bind group for all main textures + /// ahead of time. + pub fn main_texture_other(&self) -> &TextureView { + if self.main_texture.load(Ordering::SeqCst) == 0 { + &self.main_textures.b + } else { + &self.main_textures.a + } + } + /// The "main" sampled texture. pub fn sampled_main_texture(&self) -> Option<&TextureView> { self.main_textures.sampled.as_ref()