Skip to content

Commit

Permalink
RenderTarget::write returns error from callback
Browse files Browse the repository at this point in the history
  • Loading branch information
asny committed Feb 26, 2024
1 parent 5d52c48 commit 8412146
Show file tree
Hide file tree
Showing 23 changed files with 186 additions and 104 deletions.
3 changes: 2 additions & 1 deletion examples/environment/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ pub async fn run() {
.screen()
.clear(ClearState::color_and_depth(0.5, 0.5, 0.5, 1.0, 1.0))
.render(&camera, skybox.into_iter().chain(&model), &[&light])
.write(|| gui.render());
.write(|| gui.render())
.unwrap();

FrameOutput::default()
});
Expand Down
5 changes: 2 additions & 3 deletions examples/image/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ pub async fn run() {
Some(ColorTexture::Single(&target)),
None,
)
.write(|| {
gui.render();
});
.write(|| gui.render())
.unwrap();

FrameOutput::default()
});
Expand Down
2 changes: 1 addition & 1 deletion examples/instanced_shapes/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub fn main() {
screen.render(&camera, &non_instanced_meshes, &[&light0, &light1]);
};

screen.write(|| gui.render());
screen.write(|| gui.render()).unwrap();

FrameOutput::default()
});
Expand Down
83 changes: 46 additions & 37 deletions examples/lighting/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,21 @@ pub async fn run() {
screen.clear(ClearState::default());
match material_type {
MaterialType::Normal => {
screen.write(|| {
model.render_with_material(
&NormalMaterial::from_physical_material(&model.material),
&camera,
&lights,
);
plane.render_with_material(
&NormalMaterial::from_physical_material(&plane.material),
&camera,
&lights,
)
});
screen
.write::<RendererError>(|| {
model.render_with_material(
&NormalMaterial::from_physical_material(&model.material),
&camera,
&lights,
);
plane.render_with_material(
&NormalMaterial::from_physical_material(&plane.material),
&camera,
&lights,
);
Ok(())
})
.unwrap();
}
MaterialType::Depth => {
screen.render_with_material(
Expand All @@ -289,18 +292,21 @@ pub async fn run() {
);
}
MaterialType::Orm => {
screen.write(|| {
model.render_with_material(
&ORMMaterial::from_physical_material(&model.material),
&camera,
&lights,
);
plane.render_with_material(
&ORMMaterial::from_physical_material(&plane.material),
&camera,
&lights,
)
});
screen
.write::<RendererError>(|| {
model.render_with_material(
&ORMMaterial::from_physical_material(&model.material),
&camera,
&lights,
);
plane.render_with_material(
&ORMMaterial::from_physical_material(&plane.material),
&camera,
&lights,
);
Ok(())
})
.unwrap();
}
MaterialType::Position => {
screen.render_with_material(
Expand All @@ -319,18 +325,21 @@ pub async fn run() {
);
}
MaterialType::Color => {
screen.write(|| {
model.render_with_material(
&ColorMaterial::from_physical_material(&model.material),
&camera,
&lights,
);
plane.render_with_material(
&ColorMaterial::from_physical_material(&plane.material),
&camera,
&lights,
)
});
screen
.write::<RendererError>(|| {
model.render_with_material(
&ColorMaterial::from_physical_material(&model.material),
&camera,
&lights,
);
plane.render_with_material(
&ColorMaterial::from_physical_material(&plane.material),
&camera,
&lights,
);
Ok(())
})
.unwrap();
}
MaterialType::Forward => {
screen.render(&camera, model.into_iter().chain(&plane), &lights);
Expand All @@ -343,7 +352,7 @@ pub async fn run() {
);
}
}
screen.write(|| gui.render());
screen.write(|| gui.render()).unwrap();

FrameOutput::default()
});
Expand Down
5 changes: 2 additions & 3 deletions examples/lights/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ pub async fn run() {
lights.iter().map(|l| l.object()).chain(&model),
&lights.iter().map(|l| l.light()).collect::<Vec<_>>(),
)
.write(|| {
gui.render();
});
.write(|| gui.render())
.unwrap();

FrameOutput::default()
});
Expand Down
2 changes: 1 addition & 1 deletion examples/multisample/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ pub fn main() {
};

// Render GUI to screen
frame_input.screen().write(|| gui.render());
frame_input.screen().write(|| gui.render()).unwrap();

FrameOutput::default()
});
Expand Down
5 changes: 3 additions & 2 deletions examples/pbr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ pub async fn run() {
),
};
model.render_with_material(&material, &camera, &[&light]);
gui.render();
});
gui.render()
})
.unwrap();

FrameOutput::default()
});
Expand Down
3 changes: 2 additions & 1 deletion examples/screen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ pub fn main() {
ClearState::color(0.5, 0.5, 0.5, 1.0),
)
.render_partially(scissor_box_zoomed, &camera, &model, &[])
.write(|| gui.render());
.write(|| gui.render())
.unwrap();

// Secondary view
let secondary_viewport = Viewport {
Expand Down
5 changes: 3 additions & 2 deletions examples/statues/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ pub async fn run() {
bounding_box.render(camera, &[]);
}
}
gui.render();
});
gui.render()
})
.unwrap();

FrameOutput::default()
});
Expand Down
5 changes: 2 additions & 3 deletions examples/terrain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,8 @@ pub async fn run() {
Some(ColorTexture::Single(&color_texture)),
Some(DepthTexture::Single(&depth_texture)),
)
.write(|| {
gui.render();
});
.write(|| gui.render())
.unwrap();

FrameOutput::default()
});
Expand Down
7 changes: 5 additions & 2 deletions examples/triangle_core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use three_d::core::{
degrees, radians, vec3, ClearState, Context, Mat4, Program, RenderStates, Srgba, VertexBuffer,
};
use three_d::window::{FrameOutput, Window, WindowSettings};
use three_d::CoreError;
use three_d_asset::Camera;

pub fn main() {
Expand Down Expand Up @@ -59,7 +60,7 @@ pub fn main() {
.screen()
// Clear the color and depth of the screen render target
.clear(ClearState::color_and_depth(0.8, 0.8, 0.8, 1.0, 1.0))
.write(|| {
.write::<CoreError>(|| {
let time = frame_input.accumulated_time as f32;
program.use_uniform("model", Mat4::from_angle_y(radians(time * 0.005)));
program.use_uniform("viewProjection", camera.projection() * camera.view());
Expand All @@ -70,7 +71,9 @@ pub fn main() {
frame_input.viewport,
positions.vertex_count(),
);
});
Ok(())
})
.unwrap();

FrameOutput::default()
});
Expand Down
3 changes: 2 additions & 1 deletion examples/volume/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ pub async fn run() {
&voxel_grid,
&[&ambient, &directional1, &directional2],
)
.write(|| gui.render());
.write(|| gui.render())
.unwrap();

FrameOutput::default()
});
Expand Down
15 changes: 11 additions & 4 deletions src/core/render_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,28 @@ impl<'a> RenderTarget<'a> {
///
/// Writes whatever rendered in the `render` closure into this render target.
///
pub fn write(&self, render: impl FnOnce()) -> &Self {
pub fn write<E: std::error::Error>(
&self,
render: impl FnOnce() -> Result<(), E>,
) -> Result<&Self, E> {
self.write_partially(self.scissor_box(), render)
}

///
/// Writes whatever rendered in the `render` closure into the part of this render target defined by the scissor box.
///
pub fn write_partially(&self, scissor_box: ScissorBox, render: impl FnOnce()) -> &Self {
pub fn write_partially<E: std::error::Error>(
&self,
scissor_box: ScissorBox,
render: impl FnOnce() -> Result<(), E>,
) -> Result<&Self, E> {
self.context.set_scissor(scissor_box);
self.bind(crate::context::DRAW_FRAMEBUFFER);
render();
render()?;
if let Some(ref color) = self.color {
color.generate_mip_maps();
}
self
Ok(self)
}

///
Expand Down
16 changes: 12 additions & 4 deletions src/core/render_target/color_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,24 @@ impl<'a> ColorTarget<'a> {
///
/// Writes whatever rendered in the `render` closure into this color target.
///
pub fn write(&self, render: impl FnOnce()) -> &Self {
pub fn write<E: std::error::Error>(
&self,
render: impl FnOnce() -> Result<(), E>,
) -> Result<&Self, E> {
self.write_partially(self.scissor_box(), render)
}

///
/// Writes whatever rendered in the `render` closure into the part of this color target defined by the scissor box.
///
pub fn write_partially(&self, scissor_box: ScissorBox, render: impl FnOnce()) -> &Self {
self.as_render_target().write_partially(scissor_box, render);
self
pub fn write_partially<E: std::error::Error>(
&self,
scissor_box: ScissorBox,
render: impl FnOnce() -> Result<(), E>,
) -> Result<&Self, E> {
self.as_render_target()
.write_partially(scissor_box, render)?;
Ok(self)
}

///
Expand Down
16 changes: 12 additions & 4 deletions src/core/render_target/color_target_multisample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,24 @@ impl<C: TextureDataType> ColorTargetMultisample<C> {
///
/// Writes whatever rendered in the `render` closure into this target.
///
pub fn write(&self, render: impl FnOnce()) -> &Self {
pub fn write<E: std::error::Error>(
&self,
render: impl FnOnce() -> Result<(), E>,
) -> Result<&Self, E> {
self.write_partially(self.scissor_box(), render)
}

///
/// Writes whatever rendered in the `render` closure into the part of this target defined by the scissor box.
///
pub fn write_partially(&self, scissor_box: ScissorBox, render: impl FnOnce()) -> &Self {
self.as_render_target().write_partially(scissor_box, render);
self
pub fn write_partially<E: std::error::Error>(
&self,
scissor_box: ScissorBox,
render: impl FnOnce() -> Result<(), E>,
) -> Result<&Self, E> {
self.as_render_target()
.write_partially(scissor_box, render)?;
Ok(self)
}

/// The width of this target.
Expand Down
16 changes: 12 additions & 4 deletions src/core/render_target/depth_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,24 @@ impl<'a> DepthTarget<'a> {
///
/// Writes whatever rendered in the `render` closure into this depth target.
///
pub fn write(&self, render: impl FnOnce()) -> &Self {
pub fn write<E: std::error::Error>(
&self,
render: impl FnOnce() -> Result<(), E>,
) -> Result<&Self, E> {
self.write_partially(self.scissor_box(), render)
}

///
/// Writes whatever rendered in the `render` closure into the part of this depth target defined by the scissor box.
///
pub fn write_partially(&self, scissor_box: ScissorBox, render: impl FnOnce()) -> &Self {
self.as_render_target().write_partially(scissor_box, render);
self
pub fn write_partially<E: std::error::Error>(
&self,
scissor_box: ScissorBox,
render: impl FnOnce() -> Result<(), E>,
) -> Result<&Self, E> {
self.as_render_target()
.write_partially(scissor_box, render)?;
Ok(self)
}

///
Expand Down
16 changes: 12 additions & 4 deletions src/core/render_target/depth_target_multisample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,24 @@ impl<D: DepthTextureDataType> DepthTargetMultisample<D> {
///
/// Writes whatever rendered in the `render` closure into this target.
///
pub fn write(&self, render: impl FnOnce()) -> &Self {
pub fn write<E: std::error::Error>(
&self,
render: impl FnOnce() -> Result<(), E>,
) -> Result<&Self, E> {
self.write_partially(self.scissor_box(), render)
}

///
/// Writes whatever rendered in the `render` closure into the part of this target defined by the scissor box.
///
pub fn write_partially(&self, scissor_box: ScissorBox, render: impl FnOnce()) -> &Self {
self.as_render_target().write_partially(scissor_box, render);
self
pub fn write_partially<E: std::error::Error>(
&self,
scissor_box: ScissorBox,
render: impl FnOnce() -> Result<(), E>,
) -> Result<&Self, E> {
self.as_render_target()
.write_partially(scissor_box, render)?;
Ok(self)
}

/// The width of this target.
Expand Down
Loading

0 comments on commit 8412146

Please sign in to comment.