aboutsummaryrefslogtreecommitdiff
path: root/stockton-skeleton/src/draw_passes
diff options
context:
space:
mode:
Diffstat (limited to 'stockton-skeleton/src/draw_passes')
-rw-r--r--stockton-skeleton/src/draw_passes/cons.rs127
-rw-r--r--stockton-skeleton/src/draw_passes/mod.rs109
2 files changed, 189 insertions, 47 deletions
diff --git a/stockton-skeleton/src/draw_passes/cons.rs b/stockton-skeleton/src/draw_passes/cons.rs
index ad94b1c..cc4f142 100644
--- a/stockton-skeleton/src/draw_passes/cons.rs
+++ b/stockton-skeleton/src/draw_passes/cons.rs
@@ -1,66 +1,103 @@
//! Code for using multiple draw passes in place of just one
//! Note that this can be extended to an arbitrary amount of draw passes.
-use super::{DrawPass, IntoDrawPass};
+use super::{Beginning, DrawPass, End, IntoDrawPass, Middle, Singular};
use crate::{context::RenderingContext, queue_negotiator::QueueNegotiator, types::*};
use stockton_types::Session;
use anyhow::Result;
/// One draw pass, then another.
-pub struct ConsDrawPass<A: DrawPass, B: DrawPass> {
+pub struct ConsDrawPass<A, B> {
pub a: A,
pub b: B,
}
-impl<A: DrawPass, B: DrawPass> DrawPass for ConsDrawPass<A, B> {
- fn queue_draw(
- &mut self,
- session: &Session,
- img_view: &ImageViewT,
- cmd_buffer: &mut CommandBufferT,
- ) -> Result<()> {
- self.a.queue_draw(session, img_view, cmd_buffer)?;
- self.b.queue_draw(session, img_view, cmd_buffer)?;
+macro_rules! cons_shared_impl {
+ () => {
+ fn queue_draw(
+ &mut self,
+ session: &Session,
+ img_view: &ImageViewT,
+ cmd_buffer: &mut CommandBufferT,
+ ) -> Result<()> {
+ self.a.queue_draw(session, img_view, cmd_buffer)?;
+ self.b.queue_draw(session, img_view, cmd_buffer)?;
- Ok(())
- }
+ Ok(())
+ }
+ fn deactivate(self, context: &mut RenderingContext) -> Result<()> {
+ self.a.deactivate(context)?;
+ self.b.deactivate(context)
+ }
- fn deactivate(self, context: &mut RenderingContext) -> Result<()> {
- self.a.deactivate(context)?;
- self.b.deactivate(context)
- }
+ fn handle_surface_change(
+ &mut self,
+ session: &Session,
+ context: &mut RenderingContext,
+ ) -> Result<()> {
+ self.a.handle_surface_change(session, context)?;
+ self.b.handle_surface_change(session, context)
+ }
+ };
+}
+
+impl<A, B> DrawPass<Singular> for ConsDrawPass<A, B>
+where
+ A: DrawPass<Beginning>,
+ B: DrawPass<End>,
+{
+ cons_shared_impl! {}
+}
+
+impl<A, B> DrawPass<End> for ConsDrawPass<A, B>
+where
+ A: DrawPass<Middle>,
+ B: DrawPass<End>,
+{
+ cons_shared_impl! {}
+}
+
+macro_rules! into_shared_impl {
+ () => {
+ fn init(
+ self,
+ session: &mut Session,
+ context: &mut RenderingContext,
+ ) -> Result<ConsDrawPass<A, B>> {
+ Ok(ConsDrawPass {
+ a: self.0.init(session, context)?,
+ b: self.1.init(session, context)?,
+ })
+ }
- fn handle_surface_change(
- &mut self,
- session: &Session,
- context: &mut RenderingContext,
- ) -> Result<()> {
- self.a.handle_surface_change(session, context)?;
- self.b.handle_surface_change(session, context)
- }
+ fn find_aux_queues<'a>(
+ adapter: &'a Adapter,
+ queue_negotiator: &mut QueueNegotiator,
+ ) -> Result<Vec<(&'a QueueFamilyT, Vec<f32>)>> {
+ let mut v = IA::find_aux_queues(adapter, queue_negotiator)?;
+ v.extend(IB::find_aux_queues(adapter, queue_negotiator)?);
+ Ok(v)
+ }
+ };
}
-impl<A: DrawPass, B: DrawPass, IA: IntoDrawPass<A>, IB: IntoDrawPass<B>>
- IntoDrawPass<ConsDrawPass<A, B>> for (IA, IB)
+impl<A, B, IA, IB> IntoDrawPass<ConsDrawPass<A, B>, Singular> for (IA, IB)
+where
+ A: DrawPass<Beginning>,
+ B: DrawPass<End>,
+ IA: IntoDrawPass<A, Beginning>,
+ IB: IntoDrawPass<B, End>,
{
- fn init(
- self,
- session: &mut Session,
- context: &mut RenderingContext,
- ) -> Result<ConsDrawPass<A, B>> {
- Ok(ConsDrawPass {
- a: self.0.init(session, context)?,
- b: self.1.init(session, context)?,
- })
- }
+ into_shared_impl! {}
+}
- fn find_aux_queues<'a>(
- adapter: &'a Adapter,
- queue_negotiator: &mut QueueNegotiator,
- ) -> Result<Vec<(&'a QueueFamilyT, Vec<f32>)>> {
- let mut v = IA::find_aux_queues(adapter, queue_negotiator)?;
- v.extend(IB::find_aux_queues(adapter, queue_negotiator)?);
- Ok(v)
- }
+impl<A, B, IA, IB> IntoDrawPass<ConsDrawPass<A, B>, End> for (IA, IB)
+where
+ A: DrawPass<Middle>,
+ B: DrawPass<End>,
+ IA: IntoDrawPass<A, Middle>,
+ IB: IntoDrawPass<B, End>,
+{
+ into_shared_impl! {}
}
diff --git a/stockton-skeleton/src/draw_passes/mod.rs b/stockton-skeleton/src/draw_passes/mod.rs
index a0dbba5..5a92a45 100644
--- a/stockton-skeleton/src/draw_passes/mod.rs
+++ b/stockton-skeleton/src/draw_passes/mod.rs
@@ -1,6 +1,12 @@
//! Traits and common draw passes.
+use std::ops::Range;
+
use super::{queue_negotiator::QueueNegotiator, RenderingContext};
use crate::types::*;
+use hal::{
+ image::Layout,
+ pass::{AttachmentLoadOp, AttachmentOps, AttachmentStoreOp},
+};
use stockton_types::Session;
use anyhow::Result;
@@ -11,7 +17,7 @@ pub mod util;
pub use cons::ConsDrawPass;
/// One of several 'passes' that draw on each frame.
-pub trait DrawPass {
+pub trait DrawPass<P: PassPosition> {
/// Queue any necessary draw commands to cmd_buffer
/// This should assume the command buffer isn't in the middle of a renderpass, and should leave it as such.
fn queue_draw(
@@ -34,7 +40,7 @@ pub trait DrawPass {
/// A type that can be made into a specific draw pass type.
/// This allows extra data to be used in initialisation without the Renderer needing to worry about it.
-pub trait IntoDrawPass<T: DrawPass> {
+pub trait IntoDrawPass<T: DrawPass<P>, P: PassPosition> {
fn init(self, session: &mut Session, context: &mut RenderingContext) -> Result<T>;
/// This function should ask the queue negotatior to find families for any auxilary operations this draw pass needs to perform
@@ -45,3 +51,102 @@ pub trait IntoDrawPass<T: DrawPass> {
queue_negotiator: &mut QueueNegotiator,
) -> Result<Vec<(&'a QueueFamilyT, Vec<f32>)>>;
}
+
+/// Used so that draw passes can determine what state shared resources are in and how they should be left.
+pub trait PassPosition: private::Sealed {
+ /// The layout the image is in going in.
+ fn layout_in() -> Layout;
+
+ /// The layout the image should be once this drawpass is completed
+ fn layout_out() -> Layout;
+
+ /// Has the layout already been cleared this frame
+ fn is_cleared() -> bool;
+
+ /// Convenience function to get a range from layout_in() to layout_out()
+ fn layout_as_range() -> Range<Layout> {
+ Self::layout_in()..Self::layout_out()
+ }
+
+ /// Convenience function to get the attachment ops that should be used when loading the image attachment.
+ fn attachment_ops() -> AttachmentOps {
+ match Self::is_cleared() {
+ true => AttachmentOps::new(AttachmentLoadOp::Load, AttachmentStoreOp::Store),
+ false => AttachmentOps::new(AttachmentLoadOp::Clear, AttachmentStoreOp::Store),
+ }
+ }
+}
+
+/// Pass is at the beginning of the list
+pub struct Beginning;
+impl PassPosition for Beginning {
+ fn layout_in() -> Layout {
+ Layout::Undefined
+ }
+
+ fn layout_out() -> Layout {
+ Layout::ColorAttachmentOptimal
+ }
+
+ fn is_cleared() -> bool {
+ false
+ }
+}
+
+/// Pass is in the middle of the list
+pub struct Middle;
+impl PassPosition for Middle {
+ fn layout_in() -> Layout {
+ Layout::ColorAttachmentOptimal
+ }
+
+ fn layout_out() -> Layout {
+ Layout::ColorAttachmentOptimal
+ }
+
+ fn is_cleared() -> bool {
+ true
+ }
+}
+
+/// Pass is at the end of the list
+pub struct End;
+impl PassPosition for End {
+ fn layout_in() -> Layout {
+ Layout::ColorAttachmentOptimal
+ }
+
+ fn layout_out() -> Layout {
+ Layout::Present
+ }
+
+ fn is_cleared() -> bool {
+ true
+ }
+}
+
+/// Pass is the only draw pass being used
+pub struct Singular;
+impl PassPosition for Singular {
+ fn layout_in() -> Layout {
+ Layout::Undefined
+ }
+
+ fn layout_out() -> Layout {
+ Layout::Present
+ }
+
+ fn is_cleared() -> bool {
+ false
+ }
+}
+
+mod private {
+ use super::{Beginning, End, Middle, Singular};
+
+ pub trait Sealed {}
+ impl Sealed for Beginning {}
+ impl Sealed for Middle {}
+ impl Sealed for End {}
+ impl Sealed for Singular {}
+}