Use Option<CachedTexture> type, making invalid states not representable

This commit is contained in:
Marco Buono 2023-05-29 19:34:28 -03:00
parent 6103ec084a
commit e61f78d54d

View file

@ -13,7 +13,7 @@ use crate::{
render_phase::ViewRangefinder3d, render_phase::ViewRangefinder3d,
render_resource::{DynamicUniformBuffer, ShaderType, Texture, TextureView}, render_resource::{DynamicUniformBuffer, ShaderType, Texture, TextureView},
renderer::{RenderDevice, RenderQueue}, renderer::{RenderDevice, RenderQueue},
texture::{BevyDefault, TextureCache}, texture::{BevyDefault, CachedTexture, TextureCache},
Render, RenderApp, RenderSet, Render, RenderApp, RenderSet,
}; };
use bevy_app::{App, Plugin}; use bevy_app::{App, Plugin};
@ -205,8 +205,11 @@ impl ViewTarget {
/// Retrieve this target's color attachment. This will use [`Self::sampled_main_texture_view`] and resolve to [`Self::main_texture`] if /// Retrieve this target's color attachment. This will use [`Self::sampled_main_texture_view`] and resolve to [`Self::main_texture`] if
/// the target has sampling enabled. Otherwise it will use [`Self::main_texture`] directly. /// the target has sampling enabled. Otherwise it will use [`Self::main_texture`] directly.
pub fn get_color_attachment(&self, ops: Operations<Color>) -> RenderPassColorAttachment { pub fn get_color_attachment(&self, ops: Operations<Color>) -> RenderPassColorAttachment {
match &self.main_textures.sampled_view { match &self.main_textures.sampled {
Some(sampled_texture_view) => RenderPassColorAttachment { Some(CachedTexture {
default_view: sampled_texture_view,
..
}) => RenderPassColorAttachment {
view: sampled_texture_view, view: sampled_texture_view,
resolve_target: Some(self.main_texture_view()), resolve_target: Some(self.main_texture_view()),
ops, ops,
@ -230,9 +233,9 @@ impl ViewTarget {
/// The "main" unsampled texture. /// The "main" unsampled texture.
pub fn main_texture(&self) -> &Texture { pub fn main_texture(&self) -> &Texture {
if self.main_texture.load(Ordering::SeqCst) == 0 { if self.main_texture.load(Ordering::SeqCst) == 0 {
&self.main_textures.a &self.main_textures.a.texture
} else { } else {
&self.main_textures.b &self.main_textures.b.texture
} }
} }
@ -244,18 +247,18 @@ impl ViewTarget {
/// ahead of time. /// ahead of time.
pub fn main_texture_other(&self) -> &Texture { pub fn main_texture_other(&self) -> &Texture {
if self.main_texture.load(Ordering::SeqCst) == 0 { if self.main_texture.load(Ordering::SeqCst) == 0 {
&self.main_textures.b &self.main_textures.b.texture
} else { } else {
&self.main_textures.a &self.main_textures.a.texture
} }
} }
/// The "main" unsampled texture. /// The "main" unsampled texture.
pub fn main_texture_view(&self) -> &TextureView { pub fn main_texture_view(&self) -> &TextureView {
if self.main_texture.load(Ordering::SeqCst) == 0 { if self.main_texture.load(Ordering::SeqCst) == 0 {
&self.main_textures.a_view &self.main_textures.a.default_view
} else { } else {
&self.main_textures.b_view &self.main_textures.b.default_view
} }
} }
@ -267,20 +270,26 @@ impl ViewTarget {
/// ahead of time. /// ahead of time.
pub fn main_texture_other_view(&self) -> &TextureView { pub fn main_texture_other_view(&self) -> &TextureView {
if self.main_texture.load(Ordering::SeqCst) == 0 { if self.main_texture.load(Ordering::SeqCst) == 0 {
&self.main_textures.b_view &self.main_textures.b.default_view
} else { } else {
&self.main_textures.a_view &self.main_textures.a.default_view
} }
} }
/// The "main" sampled texture. /// The "main" sampled texture.
pub fn sampled_main_texture(&self) -> Option<&Texture> { pub fn sampled_main_texture(&self) -> Option<&Texture> {
self.main_textures.sampled.as_ref() self.main_textures
.sampled
.as_ref()
.map(|sampled| &sampled.texture)
} }
/// The "main" sampled texture view. /// The "main" sampled texture view.
pub fn sampled_main_texture_view(&self) -> Option<&TextureView> { pub fn sampled_main_texture_view(&self) -> Option<&TextureView> {
self.main_textures.sampled_view.as_ref() self.main_textures
.sampled
.as_ref()
.map(|sampled| &sampled.default_view)
} }
#[inline] #[inline]
@ -318,13 +327,13 @@ impl ViewTarget {
// if the old main texture is a, then the post processing must write from a to b // if the old main texture is a, then the post processing must write from a to b
if old_is_a_main_texture == 0 { if old_is_a_main_texture == 0 {
PostProcessWrite { PostProcessWrite {
source: &self.main_textures.a_view, source: &self.main_textures.a.default_view,
destination: &self.main_textures.b_view, destination: &self.main_textures.b.default_view,
} }
} else { } else {
PostProcessWrite { PostProcessWrite {
source: &self.main_textures.b_view, source: &self.main_textures.b.default_view,
destination: &self.main_textures.a_view, destination: &self.main_textures.a.default_view,
} }
} }
} }
@ -385,12 +394,9 @@ pub fn prepare_view_uniforms(
#[derive(Clone)] #[derive(Clone)]
struct MainTargetTextures { struct MainTargetTextures {
a: Texture, a: CachedTexture,
a_view: TextureView, b: CachedTexture,
b: Texture, sampled: Option<CachedTexture>,
b_view: TextureView,
sampled: Option<Texture>,
sampled_view: Option<TextureView>,
/// 0 represents `main_textures.a`, 1 represents `main_textures.b` /// 0 represents `main_textures.a`, 1 represents `main_textures.b`
/// This is shared across view targets with the same render target /// This is shared across view targets with the same render target
main_texture: Arc<AtomicUsize>, main_texture: Arc<AtomicUsize>,
@ -458,7 +464,7 @@ fn prepare_view_targets(
..descriptor ..descriptor
}, },
); );
let (sampled_texture, sampled_view) = if msaa.samples() > 1 { let sampled = if msaa.samples() > 1 {
let sampled = texture_cache.get( let sampled = texture_cache.get(
&render_device, &render_device,
TextureDescriptor { TextureDescriptor {
@ -472,17 +478,14 @@ fn prepare_view_targets(
view_formats: descriptor.view_formats, view_formats: descriptor.view_formats,
}, },
); );
(Some(sampled.texture), Some(sampled.default_view)) Some(sampled)
} else { } else {
(None, None) None
}; };
MainTargetTextures { MainTargetTextures {
a: a.texture, a,
a_view: a.default_view, b,
b: b.texture, sampled,
b_view: b.default_view,
sampled: sampled_texture,
sampled_view,
main_texture: Arc::new(AtomicUsize::new(0)), main_texture: Arc::new(AtomicUsize::new(0)),
} }
}); });