diff options
Diffstat (limited to 'stockton-skeleton/src')
-rw-r--r-- | stockton-skeleton/src/context.rs | 11 | ||||
-rw-r--r-- | stockton-skeleton/src/draw_passes/cons.rs | 127 | ||||
-rw-r--r-- | stockton-skeleton/src/draw_passes/mod.rs | 109 | ||||
-rw-r--r-- | stockton-skeleton/src/lib.rs | 6 | ||||
-rw-r--r-- | stockton-skeleton/src/target.rs | 50 |
5 files changed, 206 insertions, 97 deletions
diff --git a/stockton-skeleton/src/context.rs b/stockton-skeleton/src/context.rs index 802b8ca..5e2d3a3 100644 --- a/stockton-skeleton/src/context.rs +++ b/stockton-skeleton/src/context.rs @@ -19,6 +19,7 @@ use super::{ target::{SwapchainProperties, TargetChain}, }; use crate::{ + draw_passes::Singular, error::{EnvironmentError, LockPoisoned}, types::*, }; @@ -56,7 +57,9 @@ pub struct RenderingContext { impl RenderingContext { /// Create a new RenderingContext for the given window. - pub fn new<IDP: IntoDrawPass<DP>, DP: DrawPass>(window: &Window) -> Result<Self> { + pub fn new<IDP: IntoDrawPass<DP, Singular>, DP: DrawPass<Singular>>( + window: &Window, + ) -> Result<Self> { // Create surface let (instance, surface, mut adapters) = unsafe { let instance = @@ -208,7 +211,11 @@ impl RenderingContext { } /// Draw onto the next frame of the swapchain - pub fn draw_next_frame<DP: DrawPass>(&mut self, session: &Session, dp: &mut DP) -> Result<()> { + pub fn draw_next_frame<DP: DrawPass<Singular>>( + &mut self, + session: &Session, + dp: &mut DP, + ) -> Result<()> { let mut device = self .device .write() 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 {} +} diff --git a/stockton-skeleton/src/lib.rs b/stockton-skeleton/src/lib.rs index 03f6d53..3212873 100644 --- a/stockton-skeleton/src/lib.rs +++ b/stockton-skeleton/src/lib.rs @@ -18,7 +18,7 @@ pub mod types; pub mod utils; use context::RenderingContext; -use draw_passes::{DrawPass, IntoDrawPass}; +use draw_passes::{DrawPass, IntoDrawPass, Singular}; use anyhow::{Context, Result}; @@ -35,9 +35,9 @@ pub struct Renderer<DP> { draw_pass: DP, } -impl<DP: DrawPass> Renderer<DP> { +impl<DP: DrawPass<Singular>> Renderer<DP> { /// Create a new Renderer. - pub fn new<IDP: IntoDrawPass<DP>>( + pub fn new<IDP: IntoDrawPass<DP, Singular>>( window: &Window, session: &mut Session, idp: IDP, diff --git a/stockton-skeleton/src/target.rs b/stockton-skeleton/src/target.rs index d0d2380..c3a212e 100644 --- a/stockton-skeleton/src/target.rs +++ b/stockton-skeleton/src/target.rs @@ -8,18 +8,14 @@ use std::{ use hal::{ command::CommandBufferFlags, - format::{Aspects, ChannelType, Format, ImageFeature}, - image::{ - Access, Extent, FramebufferAttachment, Layout, SubresourceRange, Usage as ImgUsage, - ViewCapabilities, - }, - memory::{Barrier, Dependencies}, - pso::{PipelineStage, Viewport}, + format::{ChannelType, Format, ImageFeature}, + image::{Extent, FramebufferAttachment, Usage as ImgUsage, ViewCapabilities}, + pso::Viewport, window::{CompositeAlphaMode, Extent2D, PresentMode, SwapchainConfig}, }; use super::draw_passes::DrawPass; -use crate::{error::EnvironmentError, types::*}; +use crate::{draw_passes::Singular, error::EnvironmentError, types::*}; use anyhow::{Context, Result}; use stockton_types::Session; @@ -236,7 +232,7 @@ impl TargetChain { unsafe { ManuallyDrop::into_inner(read(&self.surface)) } } - pub fn do_draw_with<'a, DP: DrawPass>( + pub fn do_draw_with<'a, DP: DrawPass<Singular>>( &'a mut self, device: &mut DeviceT, command_queue: &mut QueueT, @@ -270,45 +266,9 @@ impl TargetChain { unsafe { target.cmd_buffer.begin_primary(CommandBufferFlags::empty()); - target.cmd_buffer.pipeline_barrier( - PipelineStage::TOP_OF_PIPE..PipelineStage::TOP_OF_PIPE, - Dependencies::empty(), - once(Barrier::Image { - states: (Access::empty(), Layout::Undefined) - ..(Access::empty(), Layout::ColorAttachmentOptimal), - target: img.borrow(), - range: SubresourceRange { - aspects: Aspects::COLOR, - level_start: 0, - level_count: Some(1), - layer_start: 0, - layer_count: Some(1), - }, - families: None, - }), - ); - dp.queue_draw(session, img.borrow(), &mut target.cmd_buffer) .context("Error in draw pass")?; - target.cmd_buffer.pipeline_barrier( - PipelineStage::BOTTOM_OF_PIPE..PipelineStage::BOTTOM_OF_PIPE, - Dependencies::empty(), - once(Barrier::Image { - states: (Access::empty(), Layout::ColorAttachmentOptimal) - ..(Access::empty(), Layout::Present), - target: img.borrow(), - range: SubresourceRange { - aspects: Aspects::COLOR, - level_start: 0, - level_count: Some(1), - layer_start: 0, - layer_count: Some(1), - }, - families: None, - }), - ); - target.cmd_buffer.finish(); } |