From 650bbaeb4259d0d2d4e26327439777a2da756a12 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Sun, 3 Mar 2019 22:09:20 -0500 Subject: [PATCH] Basic render pipeline compatibility check --- wgpu-native/src/command/mod.rs | 29 +++++++----- wgpu-native/src/command/render.rs | 14 +++++- wgpu-native/src/device.rs | 75 ++++++++++++++++++------------- wgpu-native/src/pipeline.rs | 2 + 4 files changed, 77 insertions(+), 43 deletions(-) diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index adc3f2aca..b3617991f 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -10,7 +10,7 @@ pub use self::render::*; pub use self::transfer::*; use crate::device::{ - FramebufferKey, RenderPassKey, + FramebufferKey, RenderPassKey, RenderPassContext, all_buffer_stages, all_image_stages, }; use crate::hub::{HUB, Storage}; @@ -190,7 +190,7 @@ pub fn command_encoder_begin_render_pass( let trackers = &mut cmb.trackers; let swap_chain_links = &mut cmb.swap_chain_links; - let depth_stencil_key = depth_stencil_attachment.map(|at| { + let depth_stencil = depth_stencil_attachment.map(|at| { let view = &view_guard[at.attachment]; if let Some(ex) = extent { assert_eq!(ex, view.extent); @@ -257,7 +257,8 @@ pub fn command_encoder_begin_render_pass( }); RenderPassKey { - attachments: color_keys.chain(depth_stencil_key).collect(), + colors: color_keys.collect(), + depth_stencil, } }; @@ -287,7 +288,7 @@ pub fn command_encoder_begin_render_pass( let pass = unsafe { device .raw - .create_render_pass(&e.key().attachments, &[subpass], &[]) + .create_render_pass(e.key().all(), &[subpass], &[]) } .unwrap(); e.insert(pass) @@ -296,13 +297,12 @@ pub fn command_encoder_begin_render_pass( let mut framebuffer_cache = device.framebuffers.lock(); let fb_key = FramebufferKey { - attachments: color_attachments + colors: color_attachments .iter() .map(|at| at.attachment) - .chain( - depth_stencil_attachment.map(|at| at.attachment), - ) .collect(), + depth_stencil: depth_stencil_attachment + .map(|at| at.attachment), }; let framebuffer = match framebuffer_cache.entry(fb_key) { Entry::Occupied(e) => e.into_mut(), @@ -310,8 +310,7 @@ pub fn command_encoder_begin_render_pass( let fb = { let attachments = e .key() - .attachments - .iter() + .all() .map(|&id| &view_guard[id].raw); unsafe { @@ -362,12 +361,22 @@ pub fn command_encoder_begin_render_pass( })); } + let context = RenderPassContext { + colors: color_attachments + .iter() + .map(|at| view_guard[at.attachment].format) + .collect(), + depth_stencil: depth_stencil_attachment + .map(|at| view_guard[at.attachment].format), + }; + RenderPass::new( current_comb, Stored { value: command_encoder_id, ref_count: cmb.life_guard.ref_count.clone(), }, + context, ) } diff --git a/wgpu-native/src/command/render.rs b/wgpu-native/src/command/render.rs index eeae9379e..1d69bf02f 100644 --- a/wgpu-native/src/command/render.rs +++ b/wgpu-native/src/command/render.rs @@ -1,6 +1,7 @@ use crate::command::bind::Binder; +use crate::device::RenderPassContext; use crate::hub::HUB; -use crate::resource::BufferUsageFlags; +use crate::resource::{BufferUsageFlags}; use crate::track::{Stitch, TrackerSet}; use crate::{ CommandBuffer, Stored, @@ -15,15 +16,21 @@ use std::{iter, slice}; pub struct RenderPass { raw: B::CommandBuffer, cmb_id: Stored, + context: RenderPassContext, binder: Binder, trackers: TrackerSet, } impl RenderPass { - pub(crate) fn new(raw: B::CommandBuffer, cmb_id: Stored) -> Self { + pub(crate) fn new( + raw: B::CommandBuffer, + cmb_id: Stored, + context: RenderPassContext, + ) -> Self { RenderPass { raw, cmb_id, + context, binder: Binder::default(), trackers: TrackerSet::new(), } @@ -202,6 +209,9 @@ pub extern "C" fn wgpu_render_pass_set_pipeline( let pipeline_guard = HUB.render_pipelines.read(); let pipeline = &pipeline_guard[pipeline_id]; + assert_eq!(pass.context, pipeline.pass_context, + "The render pipeline is not compatible with the pass!"); + unsafe { pass.raw.bind_graphics_pipeline(&pipeline.raw); } diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 6e761711c..49659e5c0 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -35,8 +35,6 @@ use std::collections::hash_map::Entry; use std::sync::atomic::Ordering; -type AttachmentVec = ArrayVec<[T; 5]>; - pub fn all_buffer_stages() -> hal::pso::PipelineStage { use hal::pso::PipelineStage as Ps; Ps::DRAW_INDIRECT | @@ -58,17 +56,21 @@ pub fn all_image_stages() -> hal::pso::PipelineStage { Ps::TRANSFER } -#[derive(Hash, PartialEq)] -pub(crate) struct RenderPassKey { - pub attachments: AttachmentVec, +#[derive(Clone, Debug, Hash, PartialEq)] +pub(crate) struct AttachmentData { + pub colors: ArrayVec<[T; 4]>, + pub depth_stencil: Option, +} +impl Eq for AttachmentData {} +impl AttachmentData { + pub(crate) fn all(&self) -> impl Iterator { + self.colors.iter().chain(&self.depth_stencil) + } } -impl Eq for RenderPassKey {} -#[derive(Clone, Hash, PartialEq)] -pub(crate) struct FramebufferKey { - pub attachments: AttachmentVec, -} -impl Eq for FramebufferKey {} +pub(crate) type RenderPassKey = AttachmentData; +pub(crate) type FramebufferKey = AttachmentData; +pub(crate) type RenderPassContext = AttachmentData; #[derive(Debug, PartialEq)] enum ResourceId { @@ -241,7 +243,7 @@ impl DestroyedResources { .keys() .filter_map(|key| { let mut last_submit: SubmissionIndex = 0; - for &at in &key.attachments { + for &at in key.all() { if texture_view_guard.contains(at) { return None } @@ -1216,24 +1218,25 @@ pub fn device_create_render_pipeline( desc.depth_stencil_state.as_ref() }; - let rp_key = { - let color_keys = color_states.iter().map(|at| hal::pass::Attachment { - format: Some(conv::map_texture_format(at.format)), - samples: desc.sample_count as u8, - ops: hal::pass::AttachmentOps::PRESERVE, - stencil_ops: hal::pass::AttachmentOps::DONT_CARE, - layouts: hal::image::Layout::General..hal::image::Layout::General, - }); - let depth_stencil_key = depth_stencil_state.map(|at| hal::pass::Attachment { - format: Some(conv::map_texture_format(at.format)), - samples: desc.sample_count as u8, - ops: hal::pass::AttachmentOps::PRESERVE, - stencil_ops: hal::pass::AttachmentOps::PRESERVE, - layouts: hal::image::Layout::General..hal::image::Layout::General, - }); - RenderPassKey { - attachments: color_keys.chain(depth_stencil_key).collect(), - } + let rp_key = RenderPassKey { + colors: color_states + .iter() + .map(|at| hal::pass::Attachment { + format: Some(conv::map_texture_format(at.format)), + samples: desc.sample_count as u8, + ops: hal::pass::AttachmentOps::PRESERVE, + stencil_ops: hal::pass::AttachmentOps::DONT_CARE, + layouts: hal::image::Layout::General..hal::image::Layout::General, + }) + .collect(), + depth_stencil: depth_stencil_state + .map(|at| hal::pass::Attachment { + format: Some(conv::map_texture_format(at.format)), + samples: desc.sample_count as u8, + ops: hal::pass::AttachmentOps::PRESERVE, + stencil_ops: hal::pass::AttachmentOps::PRESERVE, + layouts: hal::image::Layout::General..hal::image::Layout::General, + }), }; let mut render_pass_cache = device.render_passes.lock(); @@ -1262,7 +1265,7 @@ pub fn device_create_render_pipeline( let pass = unsafe { device .raw - .create_render_pass(&e.key().attachments, &[subpass], &[]) + .create_render_pass(e.key().all(), &[subpass], &[]) } .unwrap(); e.insert(pass) @@ -1395,9 +1398,19 @@ pub fn device_create_render_pipeline( .unwrap() }; + let pass_context = RenderPassContext { + colors: color_states + .iter() + .map(|state| state.format) + .collect(), + depth_stencil: depth_stencil_state + .map(|state| state.format), + }; + pipeline::RenderPipeline { raw: pipeline, layout_id: desc.layout, + pass_context, } } diff --git a/wgpu-native/src/pipeline.rs b/wgpu-native/src/pipeline.rs index 34b94b8d8..2e9293867 100644 --- a/wgpu-native/src/pipeline.rs +++ b/wgpu-native/src/pipeline.rs @@ -1,3 +1,4 @@ +use crate::device::RenderPassContext; use crate::resource; use crate::{ ByteArray, PipelineLayoutId, ShaderModuleId, @@ -280,4 +281,5 @@ pub struct RenderPipelineDescriptor { pub struct RenderPipeline { pub(crate) raw: B::GraphicsPipeline, pub(crate) layout_id: PipelineLayoutId, + pub(crate) pass_context: RenderPassContext, }