use super::CachedTexture;
use crate::render_resource::{TextureFormat, TextureView};
use bevy_color::LinearRgba;
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use wgpu::{
LoadOp, Operations, RenderPassColorAttachment, RenderPassDepthStencilAttachment, StoreOp,
};
#[derive(Clone)]
pub struct ColorAttachment {
pub texture: CachedTexture,
pub resolve_target: Option<CachedTexture>,
clear_color: Option<LinearRgba>,
is_first_call: Arc<AtomicBool>,
}
impl ColorAttachment {
pub fn new(
texture: CachedTexture,
resolve_target: Option<CachedTexture>,
clear_color: Option<LinearRgba>,
) -> Self {
Self {
texture,
resolve_target,
clear_color,
is_first_call: Arc::new(AtomicBool::new(true)),
}
}
pub fn get_attachment(&self) -> RenderPassColorAttachment {
if let Some(resolve_target) = self.resolve_target.as_ref() {
let first_call = self.is_first_call.fetch_and(false, Ordering::SeqCst);
RenderPassColorAttachment {
view: &resolve_target.default_view,
resolve_target: Some(&self.texture.default_view),
ops: Operations {
load: match (self.clear_color, first_call) {
(Some(clear_color), true) => LoadOp::Clear(clear_color.into()),
(None, _) | (Some(_), false) => LoadOp::Load,
},
store: StoreOp::Store,
},
}
} else {
self.get_unsampled_attachment()
}
}
pub fn get_unsampled_attachment(&self) -> RenderPassColorAttachment {
let first_call = self.is_first_call.fetch_and(false, Ordering::SeqCst);
RenderPassColorAttachment {
view: &self.texture.default_view,
resolve_target: None,
ops: Operations {
load: match (self.clear_color, first_call) {
(Some(clear_color), true) => LoadOp::Clear(clear_color.into()),
(None, _) | (Some(_), false) => LoadOp::Load,
},
store: StoreOp::Store,
},
}
}
pub(crate) fn mark_as_cleared(&self) {
self.is_first_call.store(false, Ordering::SeqCst);
}
}
pub struct DepthAttachment {
pub view: TextureView,
clear_value: Option<f32>,
is_first_call: Arc<AtomicBool>,
}
impl DepthAttachment {
pub fn new(view: TextureView, clear_value: Option<f32>) -> Self {
Self {
view,
clear_value,
is_first_call: Arc::new(AtomicBool::new(clear_value.is_some())),
}
}
pub fn get_attachment(&self, store: StoreOp) -> RenderPassDepthStencilAttachment {
let first_call = self
.is_first_call
.fetch_and(store != StoreOp::Store, Ordering::SeqCst);
RenderPassDepthStencilAttachment {
view: &self.view,
depth_ops: Some(Operations {
load: if first_call {
LoadOp::Clear(self.clear_value.unwrap())
} else {
LoadOp::Load
},
store,
}),
stencil_ops: None,
}
}
}
#[derive(Clone)]
pub struct OutputColorAttachment {
pub view: TextureView,
pub format: TextureFormat,
is_first_call: Arc<AtomicBool>,
}
impl OutputColorAttachment {
pub fn new(view: TextureView, format: TextureFormat) -> Self {
Self {
view,
format,
is_first_call: Arc::new(AtomicBool::new(true)),
}
}
pub fn get_attachment(&self, clear_color: Option<LinearRgba>) -> RenderPassColorAttachment {
let first_call = self.is_first_call.fetch_and(false, Ordering::SeqCst);
RenderPassColorAttachment {
view: &self.view,
resolve_target: None,
ops: Operations {
load: match (clear_color, first_call) {
(Some(clear_color), true) => LoadOp::Clear(clear_color.into()),
(None, _) | (Some(_), false) => LoadOp::Load,
},
store: StoreOp::Store,
},
}
}
}