Skip to content

Commit

Permalink
Deprecate copy_from functionality (#391)
Browse files Browse the repository at this point in the history
* Use CopyEffect in fog example

* Deprecate copy_from

* Possible to apply write mask when using CopyEffect

* Remove full_screen_id

* Avoid copy_from in multisample example
  • Loading branch information
asny committed Aug 9, 2023
1 parent 35eda90 commit c4662d5
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 28 deletions.
17 changes: 11 additions & 6 deletions examples/fog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub async fn run() {
let mut fog_enabled = true;

// main loop
let mut color_texture = Texture2D::new_empty::<[u8; 4]>(
let mut color_texture = Texture2D::new_empty::<[f16; 4]>(
&context,
camera.viewport().width,
camera.viewport().height,
Expand Down Expand Up @@ -106,6 +106,8 @@ pub async fn run() {
Wrapping::ClampToEdge,
);
}
camera.tone_mapping = ToneMapping::None;
camera.target_color_space = ColorSpace::Compute;
RenderTarget::new(
color_texture.as_color_target(None),
depth_texture.as_depth_target(),
Expand All @@ -117,11 +119,14 @@ pub async fn run() {
change |= fog_enabled; // Always render if fog is enabled since it contain animation.

if change {
frame_input.screen().copy_from(
ColorTexture::Single(&color_texture),
DepthTexture::Single(&depth_texture),
frame_input.viewport,
WriteMask::default(),
camera.tone_mapping = ToneMapping::default();
camera.target_color_space = ColorSpace::Srgb;
frame_input.screen().apply_screen_effect(
&CopyEffect::default(),
&camera,
&[],
Some(ColorTexture::Single(&color_texture)),
Some(DepthTexture::Single(&depth_texture)),
);

if fog_enabled {
Expand Down
2 changes: 1 addition & 1 deletion examples/image/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub async fn run() {
.screen()
.clear(ClearState::default())
.apply_screen_effect(
&CopyEffect {},
&CopyEffect::default(),
&camera,
&[],
Some(ColorTexture::Single(&target)),
Expand Down
28 changes: 19 additions & 9 deletions examples/multisample/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ pub fn main() {

window.render_loop(move |mut frame_input| {
camera.set_viewport(frame_input.viewport);
camera.tone_mapping = ToneMapping::default();
camera.target_color_space = ColorSpace::Srgb;

let mut panel_width = 0.0;
gui.update(
Expand Down Expand Up @@ -113,7 +115,7 @@ pub fn main() {

// slowly rotate cube, to better show off aliasing
cube.set_transformation(Mat4::from_angle_y(radians(
(frame_input.accumulated_time * 0.0005) as f32,
(frame_input.accumulated_time * 0.00005) as f32,
)));

// Consistent clear state and iterator of renderable things for each render method
Expand Down Expand Up @@ -159,10 +161,14 @@ pub fn main() {
.clear(clear_state)
.render(&camera, renderable_things, &[]);

frame_input.screen().copy_from_color(
ColorTexture::Single(&color_texture),
frame_input.viewport,
WriteMask::default(),
camera.tone_mapping = ToneMapping::None;
camera.target_color_space = ColorSpace::Compute;
frame_input.screen().apply_screen_effect(
&CopyEffect::default(),
&camera,
&[],
Some(ColorTexture::Single(&color_texture)),
None,
);
}

Expand All @@ -179,10 +185,14 @@ pub fn main() {
.render(&camera, renderable_things, &[])
.resolve_color();

frame_input.screen().clear(clear_state).copy_from_color(
ColorTexture::Single(&color_texture),
frame_input.viewport,
WriteMask::default(),
camera.tone_mapping = ToneMapping::None;
camera.target_color_space = ColorSpace::Compute;
frame_input.screen().clear(clear_state).apply_screen_effect(
&CopyEffect::default(),
&camera,
&[],
Some(ColorTexture::Single(&color_texture)),
None,
);
}
};
Expand Down
2 changes: 1 addition & 1 deletion examples/terrain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ pub async fn run() {
frame_input
.screen()
.apply_screen_effect(
&CopyEffect {},
&CopyEffect::default(),
&camera,
&[],
Some(ColorTexture::Single(&color_texture)),
Expand Down
4 changes: 0 additions & 4 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@ pub fn apply_cube_effect(
.expect("Failed compiling shader");
}

pub(crate) fn full_screen_id() -> u16 {
0b1u16 << 15
}

pub(crate) fn full_screen_draw(
context: &Context,
program: &Program,
Expand Down
15 changes: 12 additions & 3 deletions src/core/render_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,15 @@ impl<'a> RenderTarget<'a> {
/// Copies the content of the color and depth texture as limited by the [WriteMask]
/// to the part of this render target specified by the [Viewport].
///
#[deprecated = "use apply_screen_effect with a CopyEffect instead"]
pub fn copy_from(
&self,
color_texture: ColorTexture,
depth_texture: DepthTexture,
viewport: Viewport,
write_mask: WriteMask,
) -> &Self {
#[allow(deprecated)]
self.copy_partially_from(
self.scissor_box(),
color_texture,
Expand All @@ -229,6 +231,7 @@ impl<'a> RenderTarget<'a> {
/// Copies the content of the color and depth texture as limited by the [ScissorBox] and [WriteMask]
/// to the part of this render target specified by the [Viewport].
///
#[deprecated = "use apply_screen_effect_partially with a CopyEffect instead"]
pub fn copy_partially_from(
&self,
scissor_box: ScissorBox,
Expand All @@ -238,7 +241,7 @@ impl<'a> RenderTarget<'a> {
write_mask: WriteMask,
) -> &Self {
self.write_partially(scissor_box, || {
let mut id = full_screen_id().to_le_bytes().to_vec();
let mut id = (0b1u16 << 15).to_le_bytes().to_vec();
id.extend(
(0b1u16 << 13 | 0b1u16 << 12 | color_texture.id() | depth_texture.id())
.to_le_bytes(),
Expand Down Expand Up @@ -284,19 +287,22 @@ impl<'a> RenderTarget<'a> {
/// Copies the content of the color texture as limited by the [WriteMask]
/// to the part of this render target specified by the [Viewport].
///
#[deprecated = "use apply_screen_effect with a CopyEffect instead"]
pub fn copy_from_color(
&self,
color_texture: ColorTexture,
viewport: Viewport,
write_mask: WriteMask,
) -> &Self {
#[allow(deprecated)]
self.copy_partially_from_color(self.scissor_box(), color_texture, viewport, write_mask)
}

///
/// Copies the content of the color texture as limited by the [ScissorBox] and [WriteMask]
/// to the part of this render target specified by the [Viewport].
///
#[deprecated = "use apply_screen_effect_partially with a CopyEffect instead"]
pub fn copy_partially_from_color(
&self,
scissor_box: ScissorBox,
Expand All @@ -305,7 +311,7 @@ impl<'a> RenderTarget<'a> {
write_mask: WriteMask,
) -> &Self {
self.write_partially(scissor_box, || {
let mut id = full_screen_id().to_le_bytes().to_vec();
let mut id = (0b1u16 << 15).to_le_bytes().to_vec();
id.extend((0b1u16 << 13 | 0b1u16 << 11 | color_texture.id()).to_le_bytes());
let mut programs = self.context.programs.write().unwrap();
let program = programs.entry(id).or_insert_with(|| {
Expand Down Expand Up @@ -344,22 +350,25 @@ impl<'a> RenderTarget<'a> {
/// Copies the content of the depth texture
/// to the part of this render target specified by the [Viewport].
///
#[deprecated = "use apply_screen_effect with a CopyEffect instead"]
pub fn copy_from_depth(&self, depth_texture: DepthTexture, viewport: Viewport) -> &Self {
#[allow(deprecated)]
self.copy_partially_from_depth(self.scissor_box(), depth_texture, viewport)
}

///
/// Copies the content of the depth texture as limited by the [ScissorBox]
/// to the part of this render target specified by the [Viewport].
///
#[deprecated = "use apply_screen_effect_partially with a CopyEffect instead"]
pub fn copy_partially_from_depth(
&self,
scissor_box: ScissorBox,
depth_texture: DepthTexture,
viewport: Viewport,
) -> &Self {
self.write_partially(scissor_box, || {
let mut id = full_screen_id().to_le_bytes().to_vec();
let mut id = (0b1u16 << 15).to_le_bytes().to_vec();
id.extend((0b1u16 << 13 | 0b1u16 << 10 | depth_texture.id()).to_le_bytes());
let mut programs = self.context.programs.write().unwrap();
let program = programs.entry(id).or_insert_with(|| {
Expand Down
4 changes: 4 additions & 0 deletions src/core/render_target/color_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,26 +130,30 @@ impl<'a> ColorTarget<'a> {
/// Copies the content of the color texture as limited by the [WriteMask]
/// to the part of this color target specified by the [Viewport].
///
#[deprecated = "use apply_screen_effect with a CopyEffect instead"]
pub fn copy_from(
&self,
color_texture: ColorTexture,
viewport: Viewport,
write_mask: WriteMask,
) -> &Self {
#[allow(deprecated)]
self.copy_partially_from(self.scissor_box(), color_texture, viewport, write_mask)
}

///
/// Copies the content of the color texture as limited by the [ScissorBox] and [WriteMask]
/// to the part of this color target specified by the [Viewport].
///
#[deprecated = "use apply_screen_effect_partially with a CopyEffect instead"]
pub fn copy_partially_from(
&self,
scissor_box: ScissorBox,
color_texture: ColorTexture,
viewport: Viewport,
write_mask: WriteMask,
) -> &Self {
#[allow(deprecated)]
self.as_render_target().copy_partially_from_color(
scissor_box,
color_texture,
Expand Down
4 changes: 4 additions & 0 deletions src/core/render_target/depth_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,24 @@ impl<'a> DepthTarget<'a> {
/// Copies the content of the depth texture
/// to the part of this depth target specified by the [Viewport].
///
#[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)
}

///
/// Copies the content of the depth texture as limited by the [ScissorBox]
/// to the part of this depth target specified by the [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
Expand Down
4 changes: 2 additions & 2 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ pub fn apply_screen_material(
if fragment_attributes.normal || fragment_attributes.position || fragment_attributes.tangents {
panic!("Not possible to use the given material to render full screen, the full screen geometry only provides uv coordinates and color");
}
let mut id = full_screen_id().to_le_bytes().to_vec();
let mut id = (0b1u16 << 15).to_le_bytes().to_vec();
id.extend(material.id().to_le_bytes());
id.extend(lights.iter().map(|l| l.id()));

Expand Down Expand Up @@ -484,7 +484,7 @@ pub fn apply_screen_effect(
if fragment_attributes.normal || fragment_attributes.position || fragment_attributes.tangents {
panic!("Not possible to use the given effect to render full screen, the full screen geometry only provides uv coordinates and color");
}
let mut id = full_screen_id().to_le_bytes().to_vec();
let mut id = (0b1u16 << 15).to_le_bytes().to_vec();
id.extend(effect.id(color_texture, depth_texture).to_le_bytes());
id.extend(lights.iter().map(|l| l.id()));

Expand Down
8 changes: 6 additions & 2 deletions src/renderer/effect/copy.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::renderer::*;

///
/// Copies the content of the color and/or depth texture.
/// Copies the content of the color and/or depth texture by rendering a quad with those textures applied.
/// In addition, the [ToneMapping] and target [ColorSpace] specified in the [Camera] is applied to the color.
///
#[derive(Clone, Debug, Default)]
pub struct CopyEffect {}
pub struct CopyEffect {
/// Defines which channels (red, green, blue, alpha and depth) to copy.
pub write_mask: WriteMask,
}

impl Effect for CopyEffect {
fn fragment_shader_source(
Expand Down Expand Up @@ -84,6 +87,7 @@ impl Effect for CopyEffect {
RenderStates {
depth_test: DepthTest::Always,
cull: Cull::Back,
write_mask: self.write_mask,
..Default::default()
}
}
Expand Down

0 comments on commit c4662d5

Please sign in to comment.