use super::*;
#[derive(Clone)]
pub struct DepthTarget<'a> {
pub(crate) context: Context,
target: Option<DepthTexture<'a>>,
multisample_target: Option<&'a DepthTexture2DMultisample>,
}
impl<'a> DepthTarget<'a> {
pub(in crate::core) fn new_texture2d(context: &Context, texture: &'a DepthTexture2D) -> Self {
Self {
context: context.clone(),
target: Some(DepthTexture::Single(texture)),
multisample_target: None,
}
}
pub(in crate::core) fn new_texture_cube_map(
context: &Context,
texture: &'a DepthTextureCubeMap,
side: CubeMapSide,
) -> Self {
Self {
context: context.clone(),
target: Some(DepthTexture::CubeMap { texture, side }),
multisample_target: None,
}
}
pub(in crate::core) fn new_texture_2d_array(
context: &Context,
texture: &'a DepthTexture2DArray,
layer: u32,
) -> Self {
Self {
context: context.clone(),
target: Some(DepthTexture::Array { texture, layer }),
multisample_target: None,
}
}
pub(in crate::core) fn new_texture_2d_multisample(
context: &Context,
texture: &'a DepthTexture2DMultisample,
) -> Self {
Self {
context: context.clone(),
target: None,
multisample_target: Some(texture),
}
}
pub fn clear(&self, clear_state: ClearState) -> &Self {
self.clear_partially(self.scissor_box(), clear_state)
}
pub fn clear_partially(&self, scissor_box: ScissorBox, clear_state: ClearState) -> &Self {
self.as_render_target().clear_partially(
scissor_box,
ClearState {
depth: clear_state.depth,
..ClearState::none()
},
);
self
}
pub fn write(&self, render: impl FnOnce()) -> &Self {
self.write_partially(self.scissor_box(), render)
}
pub fn write_partially(&self, scissor_box: ScissorBox, render: impl FnOnce()) -> &Self {
self.as_render_target().write_partially(scissor_box, render);
self
}
#[cfg(not(target_arch = "wasm32"))]
pub fn read(&self) -> Vec<f32> {
self.read_partially(self.scissor_box())
}
#[cfg(not(target_arch = "wasm32"))]
pub fn read_partially(&self, scissor_box: ScissorBox) -> Vec<f32> {
self.as_render_target().read_depth_partially(scissor_box)
}
#[deprecated = "use apply_screen_effect with a CopyEffect instead"]
pub fn copy_from(&self, depth_texture: DepthTexture, viewport: Viewport) -> &Self {
#[allow(deprecated)]
self.copy_partially_from(self.scissor_box(), depth_texture, viewport)
}
#[deprecated = "use apply_screen_effect_partially with a CopyEffect instead"]
pub fn copy_partially_from(
&self,
scissor_box: ScissorBox,
depth_texture: DepthTexture,
viewport: Viewport,
) -> &Self {
#[allow(deprecated)]
self.as_render_target()
.copy_partially_from_depth(scissor_box, depth_texture, viewport);
self
}
pub(super) fn as_render_target(&self) -> RenderTarget<'a> {
RenderTarget::new_depth(self.clone())
}
pub fn width(&self) -> u32 {
if let Some(target) = &self.target {
match target {
DepthTexture::Single(texture) => texture.width(),
DepthTexture::Array { texture, .. } => texture.width(),
DepthTexture::CubeMap { texture, .. } => texture.width(),
}
} else {
self.multisample_target.as_ref().unwrap().width()
}
}
pub fn height(&self) -> u32 {
if let Some(target) = &self.target {
match target {
DepthTexture::Single(texture) => texture.height(),
DepthTexture::Array { texture, .. } => texture.height(),
DepthTexture::CubeMap { texture, .. } => texture.height(),
}
} else {
self.multisample_target.as_ref().unwrap().height()
}
}
pub(super) fn bind(&self) {
if let Some(target) = &self.target {
match target {
DepthTexture::Single(texture) => {
texture.bind_as_depth_target();
}
DepthTexture::Array { texture, layer } => {
texture.bind_as_depth_target(*layer);
}
DepthTexture::CubeMap { texture, side } => {
texture.bind_as_depth_target(*side);
}
}
} else {
self.multisample_target
.as_ref()
.unwrap()
.bind_as_depth_target()
}
}
}