From bb218c158009cde2d689a1b05a7461c3d851ddf5 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Thu, 22 Jul 2021 17:33:30 -0400 Subject: [PATCH 1/3] Check render bundle encoding for RODS --- wgpu-core/src/command/bundle.rs | 26 +++++++++++++++++++++----- wgpu-core/src/device/mod.rs | 2 +- wgpu-types/src/lib.rs | 14 ++++++++++++++ wgpu/examples/msaa-line/main.rs | 2 +- wgpu/src/backend/direct.rs | 2 +- wgpu/src/lib.rs | 6 +++--- 6 files changed, 41 insertions(+), 11 deletions(-) diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 3f62056b9..7f2fd259b 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -46,6 +46,7 @@ use crate::{ hub::{GlobalIdentityHandlerFactory, HalApi, Hub, Resource, Storage, Token}, id, memory_init_tracker::{MemoryInitKind, MemoryInitTrackerAction}, + pipeline::PipelineFlags, track::{TrackerSet, UsageConflict}, validation::check_buffer_usage, Label, LabelHelpers, LifeGuard, Stored, @@ -66,9 +67,9 @@ pub struct RenderBundleEncoderDescriptor<'a> { /// The formats of the color attachments that this render bundle is capable to rendering to. This /// must match the formats of the color attachments in the renderpass this render bundle is executed in. pub color_formats: Cow<'a, [wgt::TextureFormat]>, - /// The formats of the depth attachment that this render bundle is capable to rendering to. This - /// must match the formats of the depth attachments in the renderpass this render bundle is executed in. - pub depth_stencil_format: Option, + /// Information about the depth attachment that this render bundle is capable to rendering to. The format + /// must match the format of the depth attachments in the renderpass this render bundle is executed in. + pub depth_stencil: Option, /// Sample count this render bundle is capable of rendering to. This must match the pipelines and /// the renderpasses it is used in. pub sample_count: u32, @@ -80,6 +81,7 @@ pub struct RenderBundleEncoder { base: BasePass, parent_id: id::DeviceId, pub(crate) context: RenderPassContext, + is_ds_read_only: bool, } impl RenderBundleEncoder { @@ -95,7 +97,7 @@ impl RenderBundleEncoder { attachments: AttachmentData { colors: desc.color_formats.iter().cloned().collect(), resolves: ArrayVec::new(), - depth_stencil: desc.depth_stencil_format, + depth_stencil: desc.depth_stencil.map(|ds| ds.format), }, sample_count: { let sc = desc.sample_count; @@ -105,6 +107,14 @@ impl RenderBundleEncoder { sc }, }, + is_ds_read_only: match desc.depth_stencil { + Some(ds) => { + let aspects = hal::FormatAspects::from(ds.format); + (!aspects.contains(hal::FormatAspects::DEPTH) || ds.depth_read_only) + && (!aspects.contains(hal::FormatAspects::STENCIL) || ds.stencil_read_only) + } + None => false, + }, }) } @@ -120,6 +130,7 @@ impl RenderBundleEncoder { }, sample_count: 0, }, + is_ds_read_only: false, } } @@ -235,7 +246,12 @@ impl RenderBundleEncoder { .map_err(RenderCommandError::IncompatiblePipeline) .map_pass_err(scope)?; - //TODO: check read-only depth + if pipeline.flags.contains(PipelineFlags::WRITES_DEPTH_STENCIL) + && self.is_ds_read_only + { + return Err(RenderCommandError::IncompatibleReadOnlyDepthStencil) + .map_pass_err(scope); + } let layout = &pipeline_layout_guard[pipeline.layout_id.value]; pipeline_layout_id = Some(pipeline.layout_id.value); diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 8ea2c934f..477273303 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -1939,7 +1939,7 @@ impl Device { .iter() .any(|ct| ct.write_mask != first.write_mask || ct.blend != first.blend) } { - log::error!("Color targets: {:?}", color_targets); + log::info!("Color targets: {:?}", color_targets); self.require_downlevel_flags(wgt::DownlevelFlags::INDEPENDENT_BLENDING)?; } diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index 6fc30da7c..94ebea08a 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -2746,6 +2746,20 @@ impl CommandBufferDescriptor { } } +/// Describes the depth/stencil attachment for render bundles. +#[repr(C)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "trace", derive(serde::Serialize))] +#[cfg_attr(feature = "replay", derive(serde::Deserialize))] +pub struct RenderBundleDepthStencil { + /// Format of the attachment. + pub format: TextureFormat, + /// True if the depth aspect is used but not modified. + pub depth_read_only: bool, + /// True if the stencil aspect is used but not modified. + pub stencil_read_only: bool, +} + /// Describes a [`RenderBundle`]. #[repr(C)] #[derive(Clone, Debug, PartialEq, Eq, Hash)] diff --git a/wgpu/examples/msaa-line/main.rs b/wgpu/examples/msaa-line/main.rs index 2430767b6..2b88a4c90 100644 --- a/wgpu/examples/msaa-line/main.rs +++ b/wgpu/examples/msaa-line/main.rs @@ -77,7 +77,7 @@ impl Example { device.create_render_bundle_encoder(&wgpu::RenderBundleEncoderDescriptor { label: None, color_formats: &[sc_desc.format], - depth_stencil_format: None, + depth_stencil: None, sample_count, }); encoder.set_pipeline(&pipeline); diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index 890319482..16e6efd37 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -1291,7 +1291,7 @@ impl crate::Context for Context { let descriptor = wgc::command::RenderBundleEncoderDescriptor { label: desc.label.map(Borrowed), color_formats: Borrowed(desc.color_formats), - depth_stencil_format: desc.depth_stencil_format, + depth_stencil: desc.depth_stencil, sample_count: desc.sample_count, }; match wgc::command::RenderBundleEncoder::new(&descriptor, device.id, None) { diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index fe3d8ea9b..42b0dbf70 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1324,9 +1324,9 @@ pub struct RenderBundleEncoderDescriptor<'a> { /// The formats of the color attachments that this render bundle is capable to rendering to. This /// must match the formats of the color attachments in the renderpass this render bundle is executed in. pub color_formats: &'a [TextureFormat], - /// The formats of the depth attachment that this render bundle is capable to rendering to. This - /// must match the formats of the depth attachments in the renderpass this render bundle is executed in. - pub depth_stencil_format: Option, + /// Information about the depth attachment that this render bundle is capable to rendering to. This + /// must match the format of the depth attachments in the renderpass this render bundle is executed in. + pub depth_stencil: Option, /// Sample count this render bundle is capable of rendering to. This must match the pipelines and /// the renderpasses it is used in. pub sample_count: u32, From 34b68121c610f554fc5d73dc008e27db8fb5faf0 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Thu, 22 Jul 2021 17:45:41 -0400 Subject: [PATCH 2/3] Check bundle RODS against pass RODS --- wgpu-core/src/command/bundle.rs | 12 +++++++----- wgpu-core/src/command/draw.rs | 8 ++++---- wgpu-core/src/command/render.rs | 20 +++++++++++++++----- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 7f2fd259b..1be82838d 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -243,13 +243,13 @@ impl RenderBundleEncoder { self.context .check_compatible(&pipeline.pass_context) - .map_err(RenderCommandError::IncompatiblePipeline) + .map_err(RenderCommandError::IncompatiblePipelineTargets) .map_pass_err(scope)?; if pipeline.flags.contains(PipelineFlags::WRITES_DEPTH_STENCIL) && self.is_ds_read_only { - return Err(RenderCommandError::IncompatibleReadOnlyDepthStencil) + return Err(RenderCommandError::IncompatiblePipelineRods) .map_pass_err(scope); } @@ -517,6 +517,7 @@ impl RenderBundleEncoder { string_data: Vec::new(), push_constant_data: Vec::new(), }, + is_ds_read_only: self.is_ds_read_only, device_id: Stored { value: id::Valid(self.parent_id), ref_count: device.life_guard.add_ref(), @@ -570,10 +571,11 @@ pub struct RenderBundle { // Normalized command stream. It can be executed verbatim, // without re-binding anything on the pipeline change. base: BasePass, + pub(super) is_ds_read_only: bool, pub(crate) device_id: Stored, pub(crate) used: TrackerSet, - pub(crate) buffer_memory_init_actions: Vec>, - pub(crate) context: RenderPassContext, + pub(super) buffer_memory_init_actions: Vec>, + pub(super) context: RenderPassContext, pub(crate) life_guard: LifeGuard, } @@ -590,7 +592,7 @@ impl RenderBundle { /// Note that the function isn't expected to fail, generally. /// All the validation has already been done by this point. /// The only failure condition is if some of the used buffers are destroyed. - pub(crate) unsafe fn execute( + pub(super) unsafe fn execute( &self, raw: &mut A::CommandEncoder, pipeline_layout_guard: &Storage< diff --git a/wgpu-core/src/command/draw.rs b/wgpu-core/src/command/draw.rs index 2d936d14e..e9b266392 100644 --- a/wgpu-core/src/command/draw.rs +++ b/wgpu-core/src/command/draw.rs @@ -72,10 +72,10 @@ pub enum RenderCommandError { InvalidPipeline(id::RenderPipelineId), #[error("QuerySet {0:?} is invalid")] InvalidQuerySet(id::QuerySetId), - #[error("Render pipeline is incompatible with render pass")] - IncompatiblePipeline(#[from] crate::device::RenderPassCompatibilityError), - #[error("pipeline is not compatible with the depth-stencil read-only render pass")] - IncompatibleReadOnlyDepthStencil, + #[error("Render pipeline targets are incompatible with render pass")] + IncompatiblePipelineTargets(#[from] crate::device::RenderPassCompatibilityError), + #[error("pipeline writes to depth/stencil, while the pass has read-only depth/stencil")] + IncompatiblePipelineRods, #[error("buffer {0:?} is in error {1:?}")] Buffer(id::BufferId, BufferError), #[error("buffer {0:?} is destroyed")] diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 133c7b574..ec2ab4c52 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -444,8 +444,10 @@ pub enum RenderPassErrorInner { InvalidPopDebugGroup, #[error(transparent)] ResourceUsageConflict(#[from] UsageConflict), - #[error("render bundle is incompatible, {0}")] - IncompatibleRenderBundle(#[from] RenderPassCompatibilityError), + #[error("render bundle has incompatible targets, {0}")] + IncompatibleBundleTargets(#[from] RenderPassCompatibilityError), + #[error("render bundle has an incompatible read-only depth/stencil flag: bundle is {bundle}, while the pass is {pass}")] + IncompatibleBundleRods { pass: bool, bundle: bool }, #[error(transparent)] RenderCommand(#[from] RenderCommandError), #[error(transparent)] @@ -994,7 +996,7 @@ impl Global { info.context .check_compatible(&pipeline.pass_context) - .map_err(RenderCommandError::IncompatiblePipeline) + .map_err(RenderCommandError::IncompatiblePipelineTargets) .map_pass_err(scope)?; state.pipeline_flags = pipeline.flags; @@ -1002,7 +1004,7 @@ impl Global { if pipeline.flags.contains(PipelineFlags::WRITES_DEPTH_STENCIL) && info.is_ds_read_only { - return Err(RenderCommandError::IncompatibleReadOnlyDepthStencil) + return Err(RenderCommandError::IncompatiblePipelineRods) .map_pass_err(scope); } @@ -1678,9 +1680,17 @@ impl Global { info.context .check_compatible(&bundle.context) - .map_err(RenderPassErrorInner::IncompatibleRenderBundle) + .map_err(RenderPassErrorInner::IncompatibleBundleTargets) .map_pass_err(scope)?; + if info.is_ds_read_only != bundle.is_ds_read_only { + return Err(RenderPassErrorInner::IncompatibleBundleRods { + pass: info.is_ds_read_only, + bundle: bundle.is_ds_read_only, + }) + .map_pass_err(scope); + } + cmd_buf.buffer_memory_init_actions.extend( bundle .buffer_memory_init_actions From 52734bf87a5a69414bae0fa3b40df7ffb930adf1 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Fri, 23 Jul 2021 11:23:04 -0400 Subject: [PATCH 3/3] Fix tracing feature for RODS --- wgpu-core/src/command/bundle.rs | 2 +- wgpu-core/src/device/mod.rs | 1 + wgpu-core/src/device/trace.rs | 10 +++++++- wgpu/src/backend/web.rs | 4 +-- wgpu/src/lib.rs | 45 ++++++++++++++------------------- 5 files changed, 32 insertions(+), 30 deletions(-) diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 1be82838d..3ba7a1e56 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -81,7 +81,7 @@ pub struct RenderBundleEncoder { base: BasePass, parent_id: id::DeviceId, pub(crate) context: RenderPassContext, - is_ds_read_only: bool, + pub(crate) is_ds_read_only: bool, } impl RenderBundleEncoder { diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 477273303..c82a5f310 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -3779,6 +3779,7 @@ impl Global { desc: trace::new_render_bundle_encoder_descriptor( desc.label.clone(), &bundle_encoder.context, + bundle_encoder.is_ds_read_only, ), base: bundle_encoder.to_base_pass(), }); diff --git a/wgpu-core/src/device/trace.rs b/wgpu-core/src/device/trace.rs index a87e84370..7fdd48f27 100644 --- a/wgpu-core/src/device/trace.rs +++ b/wgpu-core/src/device/trace.rs @@ -13,11 +13,19 @@ pub const FILE_NAME: &str = "trace.ron"; pub(crate) fn new_render_bundle_encoder_descriptor<'a>( label: crate::Label<'a>, context: &'a super::RenderPassContext, + is_ds_read_only: bool, ) -> crate::command::RenderBundleEncoderDescriptor<'a> { crate::command::RenderBundleEncoderDescriptor { label, color_formats: Cow::Borrowed(&context.attachments.colors), - depth_stencil_format: context.attachments.depth_stencil, + depth_stencil: context.attachments.depth_stencil.map(|format| { + let aspects = hal::FormatAspects::from(format); + wgt::RenderBundleDepthStencil { + format, + depth_read_only: is_ds_read_only && aspects.contains(hal::FormatAspects::DEPTH), + stencil_read_only: is_ds_read_only && aspects.contains(hal::FormatAspects::STENCIL), + } + }), sample_count: context.sample_count, } } diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index b78665644..f4ec05978 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -1514,8 +1514,8 @@ impl crate::Context for Context { if let Some(ref label) = desc.label { mapped_desc.label(label); } - if let Some(dsf) = desc.depth_stencil_format { - mapped_desc.depth_stencil_format(map_texture_format(dsf)); + if let Some(ds) = desc.depth_stencil { + mapped_desc.depth_stencil_format(map_texture_format(ds.format)); } mapped_desc.sample_count(desc.sample_count); RenderBundleEncoder(device.0.create_render_bundle_encoder(&mapped_desc)) diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 42b0dbf70..1d75d4efb 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -29,16 +29,16 @@ pub use wgt::{ BlendFactor, BlendOperation, BlendState, BufferAddress, BufferBindingType, BufferSize, BufferUsages, Color, ColorTargetState, ColorWrites, CommandBufferDescriptor, CompareFunction, DepthBiasState, DepthStencilState, DeviceType, DownlevelCapabilities, DownlevelFlags, - DynamicOffset, Extent3d, Face, Features, FilterMode, FrontFace, ImageDataLayout, IndexFormat, - Limits, MultisampleState, Origin3d, PipelineStatisticsTypes, PolygonMode, PowerPreference, - PresentMode, PrimitiveState, PrimitiveTopology, PushConstantRange, QueryType, - SamplerBorderColor, ShaderLocation, ShaderModel, ShaderStages, StencilFaceState, - StencilOperation, StencilState, StorageTextureAccess, SwapChainDescriptor, SwapChainStatus, - TextureAspect, TextureDimension, TextureFormat, TextureFormatFeatureFlags, - TextureFormatFeatures, TextureSampleType, TextureUsages, TextureViewDimension, VertexAttribute, - VertexFormat, VertexStepMode, BIND_BUFFER_ALIGNMENT, COPY_BUFFER_ALIGNMENT, - COPY_BYTES_PER_ROW_ALIGNMENT, MAP_ALIGNMENT, PUSH_CONSTANT_ALIGNMENT, QUERY_SET_MAX_QUERIES, - QUERY_SIZE, VERTEX_STRIDE_ALIGNMENT, + DynamicOffset, Extent3d, Face, Features, FilterMode, FrontFace, ImageDataLayout, + ImageSubresourceRange, IndexFormat, Limits, MultisampleState, Origin3d, + PipelineStatisticsTypes, PolygonMode, PowerPreference, PresentMode, PrimitiveState, + PrimitiveTopology, PushConstantRange, QueryType, RenderBundleDepthStencil, SamplerBorderColor, + ShaderLocation, ShaderModel, ShaderStages, StencilFaceState, StencilOperation, StencilState, + StorageTextureAccess, SwapChainDescriptor, SwapChainStatus, TextureAspect, TextureDimension, + TextureFormat, TextureFormatFeatureFlags, TextureFormatFeatures, TextureSampleType, + TextureUsages, TextureViewDimension, VertexAttribute, VertexFormat, VertexStepMode, + BIND_BUFFER_ALIGNMENT, COPY_BUFFER_ALIGNMENT, COPY_BYTES_PER_ROW_ALIGNMENT, MAP_ALIGNMENT, + PUSH_CONSTANT_ALIGNMENT, QUERY_SET_MAX_QUERIES, QUERY_SIZE, VERTEX_STRIDE_ALIGNMENT, }; use backend::{BufferMappedRange, Context as C}; @@ -88,7 +88,7 @@ trait RenderInner { offset: BufferAddress, size: Option, ); - fn set_push_constants(&mut self, stages: wgt::ShaderStages, offset: u32, data: &[u8]); + fn set_push_constants(&mut self, stages: ShaderStages, offset: u32, data: &[u8]); fn draw(&mut self, vertices: Range, instances: Range); fn draw_indexed(&mut self, indices: Range, base_vertex: i32, instances: Range); fn draw_indirect(&mut self, indirect_buffer: &Ctx::BufferId, indirect_offset: BufferAddress); @@ -211,8 +211,8 @@ trait Context: Debug + Send + Sized + Sync { fn adapter_get_texture_format_features( &self, adapter: &Self::AdapterId, - format: wgt::TextureFormat, - ) -> wgt::TextureFormatFeatures; + format: TextureFormat, + ) -> TextureFormatFeatures; fn device_features(&self, device: &Self::DeviceId) -> Features; fn device_limits(&self, device: &Self::DeviceId) -> Limits; @@ -410,7 +410,7 @@ trait Context: Debug + Send + Sized + Sync { &self, encoder: &Self::CommandEncoderId, texture: &Texture, - subresource_range: &wgt::ImageSubresourceRange, + subresource_range: &ImageSubresourceRange, ); fn command_encoder_clear_buffer( &self, @@ -1326,7 +1326,7 @@ pub struct RenderBundleEncoderDescriptor<'a> { pub color_formats: &'a [TextureFormat], /// Information about the depth attachment that this render bundle is capable to rendering to. This /// must match the format of the depth attachments in the renderpass this render bundle is executed in. - pub depth_stencil: Option, + pub depth_stencil: Option, /// Sample count this render bundle is capable of rendering to. This must match the pipelines and /// the renderpasses it is used in. pub sample_count: u32, @@ -1540,10 +1540,7 @@ impl Adapter { /// /// Note that the WebGPU spec further restricts the available usages/features. /// To disable these restrictions on a device, request the [`Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES`] feature. - pub fn get_texture_format_features( - &self, - format: wgt::TextureFormat, - ) -> wgt::TextureFormatFeatures { + pub fn get_texture_format_features(&self, format: TextureFormat) -> TextureFormatFeatures { Context::adapter_get_texture_format_features(&*self.context, &self.id, format) } } @@ -2187,11 +2184,7 @@ impl CommandEncoder { /// - `CLEAR_COMMANDS` extension not enabled /// - Texture does not have `COPY_DST` usage. /// - Range it out of bounds - pub fn clear_texture( - &mut self, - texture: &Texture, - subresource_range: &wgt::ImageSubresourceRange, - ) { + pub fn clear_texture(&mut self, texture: &Texture, subresource_range: &ImageSubresourceRange) { Context::command_encoder_clear_image( &*self.context, self.id.as_ref().unwrap(), @@ -2642,7 +2635,7 @@ impl<'a> RenderPass<'a> { /// /// You would need to upload this in three set_push_constants calls. First for the `Vertex` only range 0..4, second /// for the `Vertex | Fragment` range 4..8, third for the `Fragment` range 8..12. - pub fn set_push_constants(&mut self, stages: wgt::ShaderStages, offset: u32, data: &[u8]) { + pub fn set_push_constants(&mut self, stages: ShaderStages, offset: u32, data: &[u8]) { self.id.set_push_constants(stages, offset, data); } } @@ -2949,7 +2942,7 @@ impl<'a> RenderBundleEncoder<'a> { /// /// You would need to upload this in three set_push_constants calls. First for the `Vertex` only range 0..4, second /// for the `Vertex | Fragment` range 4..8, third for the `Fragment` range 8..12. - pub fn set_push_constants(&mut self, stages: wgt::ShaderStages, offset: u32, data: &[u8]) { + pub fn set_push_constants(&mut self, stages: ShaderStages, offset: u32, data: &[u8]) { self.id.set_push_constants(stages, offset, data); } }