mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-20 22:41:07 +01:00
move indirect validation code to its own folder
rename items appropriately internalize indirect buffer bind groups
This commit is contained in:
1 parent
ef622c1061
commit
7d15c511de
12 files changed
+198
-202
No files matched your search
@@ -810,7 +810,7 @@ fn multi_draw_indirect(
|
||||
};
|
||||
let instance_limit = vertex_limits.instance_limit;
|
||||
|
||||
let buffer_uses = if state.device.indirect_draw_validation.is_some() {
|
||||
let buffer_uses = if state.device.indirect_validation.is_some() {
|
||||
wgt::BufferUses::STORAGE_READ_ONLY
|
||||
} else {
|
||||
wgt::BufferUses::INDIRECT
|
||||
@@ -901,8 +901,8 @@ impl RenderBundle {
|
||||
pub(super) unsafe fn execute(
|
||||
&self,
|
||||
raw: &mut dyn hal::DynCommandEncoder,
|
||||
indirect_draw_validation_resources: &mut crate::indirect_draw_validation::IndirectDrawValidationResources,
|
||||
indirect_draw_validation_batcher: &mut crate::indirect_draw_validation::IndirectDrawValidationBatcher,
|
||||
indirect_draw_validation_resources: &mut crate::indirect_validation::DrawResources,
|
||||
indirect_draw_validation_batcher: &mut crate::indirect_validation::DrawBatcher,
|
||||
snatch_guard: &SnatchGuard,
|
||||
) -> Result<(), ExecutionError> {
|
||||
let mut offsets = self.base.dynamic_offsets.as_slice();
|
||||
@@ -1050,7 +1050,7 @@ impl RenderBundle {
|
||||
vertex_or_index_limit,
|
||||
instance_limit,
|
||||
} => {
|
||||
let (buffer, offset) = if self.device.indirect_draw_validation.is_some() {
|
||||
let (buffer, offset) = if self.device.indirect_validation.is_some() {
|
||||
let (dst_resource_index, offset) = indirect_draw_validation_batcher.add(
|
||||
indirect_draw_validation_resources,
|
||||
&self.device,
|
||||
|
||||
@@ -892,7 +892,9 @@ fn dispatch_indirect(
|
||||
));
|
||||
|
||||
if let Some(ref indirect_validation) = state.device.indirect_validation {
|
||||
let params = indirect_validation.params(&state.device.limits, offset, buffer.size);
|
||||
let params = indirect_validation
|
||||
.dispatch
|
||||
.params(&state.device.limits, offset, buffer.size);
|
||||
|
||||
unsafe {
|
||||
state.raw_encoder.set_compute_pipeline(params.pipeline);
|
||||
@@ -921,9 +923,10 @@ fn dispatch_indirect(
|
||||
1,
|
||||
Some(
|
||||
buffer
|
||||
.raw_indirect_validation_bind_group
|
||||
.indirect_validation_bind_groups
|
||||
.get(&state.snatch_guard)
|
||||
.unwrap()
|
||||
.dispatch
|
||||
.as_ref(),
|
||||
),
|
||||
&[params.aligned_offset as u32],
|
||||
|
||||
@@ -436,8 +436,7 @@ pub(crate) struct BakedCommands {
|
||||
pub(crate) encoder: CommandEncoder,
|
||||
pub(crate) trackers: Tracker,
|
||||
pub(crate) temp_resources: Vec<TempResource>,
|
||||
pub(crate) indirect_draw_validation_resources:
|
||||
crate::indirect_draw_validation::IndirectDrawValidationResources,
|
||||
pub(crate) indirect_draw_validation_resources: crate::indirect_validation::DrawResources,
|
||||
buffer_memory_init_actions: Vec<BufferInitTrackerAction>,
|
||||
texture_memory_actions: CommandBufferTextureMemoryActions,
|
||||
}
|
||||
@@ -468,8 +467,7 @@ pub struct CommandBufferMutable {
|
||||
tlas_actions: Vec<TlasAction>,
|
||||
temp_resources: Vec<TempResource>,
|
||||
|
||||
indirect_draw_validation_resources:
|
||||
crate::indirect_draw_validation::IndirectDrawValidationResources,
|
||||
indirect_draw_validation_resources: crate::indirect_validation::DrawResources,
|
||||
|
||||
#[cfg(feature = "trace")]
|
||||
pub(crate) commands: Option<Vec<TraceCommand>>,
|
||||
@@ -559,9 +557,7 @@ impl CommandBuffer {
|
||||
tlas_actions: Default::default(),
|
||||
temp_resources: Default::default(),
|
||||
indirect_draw_validation_resources:
|
||||
crate::indirect_draw_validation::IndirectDrawValidationResources::new(
|
||||
device.clone(),
|
||||
),
|
||||
crate::indirect_validation::DrawResources::new(device.clone()),
|
||||
#[cfg(feature = "trace")]
|
||||
commands: if device.trace.lock().is_some() {
|
||||
Some(Vec::new())
|
||||
|
||||
@@ -1631,8 +1631,7 @@ impl Global {
|
||||
let device = &cmd_buf.device;
|
||||
let snatch_guard = &device.snatchable_lock.read();
|
||||
|
||||
let mut indirect_draw_validation_batcher =
|
||||
crate::indirect_draw_validation::IndirectDrawValidationBatcher::new();
|
||||
let mut indirect_draw_validation_batcher = crate::indirect_validation::DrawBatcher::new();
|
||||
|
||||
let (scope, pending_discard_init_fixups) = {
|
||||
device.check_is_valid().map_pass_err(pass_scope)?;
|
||||
@@ -1997,8 +1996,9 @@ impl Global {
|
||||
|
||||
CommandBuffer::insert_barriers_from_scope(transit, tracker, &scope, snatch_guard);
|
||||
|
||||
if let Some(ref indirect_draw_validation) = device.indirect_draw_validation {
|
||||
indirect_draw_validation
|
||||
if let Some(ref indirect_validation) = device.indirect_validation {
|
||||
indirect_validation
|
||||
.draw
|
||||
.inject_validation_pass(
|
||||
device,
|
||||
snatch_guard,
|
||||
@@ -2494,8 +2494,8 @@ fn draw_indexed(
|
||||
|
||||
fn multi_draw_indirect(
|
||||
state: &mut State,
|
||||
indirect_draw_validation_resources: &mut crate::indirect_draw_validation::IndirectDrawValidationResources,
|
||||
indirect_draw_validation_batcher: &mut crate::indirect_draw_validation::IndirectDrawValidationBatcher,
|
||||
indirect_draw_validation_resources: &mut crate::indirect_validation::DrawResources,
|
||||
indirect_draw_validation_batcher: &mut crate::indirect_validation::DrawBatcher,
|
||||
cmd_buf: &Arc<CommandBuffer>,
|
||||
indirect_buffer: Arc<crate::resource::Buffer>,
|
||||
offset: u64,
|
||||
@@ -2563,7 +2563,7 @@ fn multi_draw_indirect(
|
||||
}
|
||||
}
|
||||
|
||||
if state.device.indirect_draw_validation.is_some() {
|
||||
if state.device.indirect_validation.is_some() {
|
||||
state
|
||||
.info
|
||||
.usage_scope
|
||||
@@ -2580,10 +2580,8 @@ fn multi_draw_indirect(
|
||||
raw_encoder: &'a mut dyn hal::DynCommandEncoder,
|
||||
device: &'a Device,
|
||||
|
||||
indirect_draw_validation_resources:
|
||||
&'a mut crate::indirect_draw_validation::IndirectDrawValidationResources,
|
||||
indirect_draw_validation_batcher:
|
||||
&'a mut crate::indirect_draw_validation::IndirectDrawValidationBatcher,
|
||||
indirect_draw_validation_resources: &'a mut crate::indirect_validation::DrawResources,
|
||||
indirect_draw_validation_batcher: &'a mut crate::indirect_validation::DrawBatcher,
|
||||
|
||||
indirect_buffer: Arc<crate::resource::Buffer>,
|
||||
indexed: bool,
|
||||
@@ -2866,8 +2864,8 @@ fn write_timestamp(
|
||||
|
||||
fn execute_bundle(
|
||||
state: &mut State,
|
||||
indirect_draw_validation_resources: &mut crate::indirect_draw_validation::IndirectDrawValidationResources,
|
||||
indirect_draw_validation_batcher: &mut crate::indirect_draw_validation::IndirectDrawValidationBatcher,
|
||||
indirect_draw_validation_resources: &mut crate::indirect_validation::DrawResources,
|
||||
indirect_draw_validation_batcher: &mut crate::indirect_validation::DrawBatcher,
|
||||
cmd_buf: &Arc<CommandBuffer>,
|
||||
bundle: Arc<super::RenderBundle>,
|
||||
) -> Result<(), RenderPassErrorInner> {
|
||||
|
||||
@@ -268,8 +268,7 @@ pub(crate) struct EncoderInFlight {
|
||||
pub(crate) trackers: Tracker,
|
||||
pub(crate) temp_resources: Vec<TempResource>,
|
||||
/// We only need to keep these resources alive.
|
||||
_indirect_draw_validation_resources:
|
||||
crate::indirect_draw_validation::IndirectDrawValidationResources,
|
||||
_indirect_draw_validation_resources: crate::indirect_validation::DrawResources,
|
||||
|
||||
/// These are the buffers that have been tracked by `PendingWrites`.
|
||||
pub(crate) pending_buffers: FastHashMap<TrackerIndex, Arc<Buffer>>,
|
||||
@@ -380,10 +379,9 @@ impl PendingWrites {
|
||||
},
|
||||
trackers: Tracker::new(),
|
||||
temp_resources: mem::take(&mut self.temp_resources),
|
||||
_indirect_draw_validation_resources:
|
||||
crate::indirect_draw_validation::IndirectDrawValidationResources::new(
|
||||
device.clone(),
|
||||
),
|
||||
_indirect_draw_validation_resources: crate::indirect_validation::DrawResources::new(
|
||||
device.clone(),
|
||||
),
|
||||
pending_buffers,
|
||||
pending_textures,
|
||||
};
|
||||
|
||||
@@ -130,8 +130,6 @@ pub struct Device {
|
||||
pub(crate) usage_scopes: UsageScopePool,
|
||||
pub(crate) last_acceleration_structure_build_command_index: AtomicU64,
|
||||
pub(crate) indirect_validation: Option<crate::indirect_validation::IndirectValidation>,
|
||||
pub(crate) indirect_draw_validation:
|
||||
Option<crate::indirect_draw_validation::IndirectDrawValidation>,
|
||||
// needs to be dropped last
|
||||
#[cfg(feature = "trace")]
|
||||
pub(crate) trace: Mutex<Option<trace::Trace>>,
|
||||
@@ -164,9 +162,6 @@ impl Drop for Device {
|
||||
if let Some(indirect_validation) = self.indirect_validation.take() {
|
||||
indirect_validation.dispose(self.raw.as_ref());
|
||||
}
|
||||
if let Some(indirect_draw_validation) = self.indirect_draw_validation.take() {
|
||||
indirect_draw_validation.dispose(self.raw.as_ref());
|
||||
}
|
||||
unsafe {
|
||||
self.raw.destroy_buffer(zero_buffer);
|
||||
self.raw.destroy_fence(fence);
|
||||
@@ -258,31 +253,11 @@ impl Device {
|
||||
.contains(wgt::DownlevelFlags::INDIRECT_EXECUTION);
|
||||
|
||||
let indirect_validation = if enable_indirect_validation {
|
||||
match crate::indirect_validation::IndirectValidation::new(
|
||||
Some(crate::indirect_validation::IndirectValidation::new(
|
||||
raw_device.as_ref(),
|
||||
&desc.required_limits,
|
||||
) {
|
||||
Ok(indirect_validation) => Some(indirect_validation),
|
||||
Err(e) => {
|
||||
log::error!("indirect-validation error: {e:?}");
|
||||
return Err(DeviceError::Lost);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let indirect_draw_validation = if enable_indirect_validation {
|
||||
match crate::indirect_draw_validation::IndirectDrawValidation::new(
|
||||
raw_device.as_ref(),
|
||||
&desc.required_features,
|
||||
) {
|
||||
Ok(indirect_draw_validation) => Some(indirect_draw_validation),
|
||||
Err(e) => {
|
||||
log::error!("indirect-draw-validation error: {e:?}");
|
||||
return Err(DeviceError::Lost);
|
||||
}
|
||||
}
|
||||
)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -333,7 +308,6 @@ impl Device {
|
||||
// By starting at one, we can put the result in a NonZeroU64.
|
||||
last_acceleration_structure_build_command_index: AtomicU64::new(1),
|
||||
indirect_validation,
|
||||
indirect_draw_validation,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -671,11 +645,8 @@ impl Device {
|
||||
let buffer =
|
||||
unsafe { self.raw().create_buffer(&hal_desc) }.map_err(|e| self.handle_hal_error(e))?;
|
||||
|
||||
let raw_indirect_validation_bind_group =
|
||||
self.create_indirect_validation_bind_group(buffer.as_ref(), desc.size, desc.usage)?;
|
||||
|
||||
let raw_indirect_draw_validation_bind_group = self
|
||||
.create_indirect_draw_validation_bind_group(buffer.as_ref(), desc.size, desc.usage)?;
|
||||
let indirect_validation_bind_groups =
|
||||
self.create_indirect_validation_bind_groups(buffer.as_ref(), desc.size, desc.usage)?;
|
||||
|
||||
let buffer = Buffer {
|
||||
raw: Snatchable::new(buffer),
|
||||
@@ -690,8 +661,7 @@ impl Device {
|
||||
label: desc.label.to_string(),
|
||||
tracking_data: TrackingData::new(self.tracker_indices.buffers.clone()),
|
||||
bind_groups: Mutex::new(rank::BUFFER_BIND_GROUPS, WeakVec::new()),
|
||||
raw_indirect_validation_bind_group,
|
||||
raw_indirect_draw_validation_bind_group,
|
||||
indirect_validation_bind_groups,
|
||||
};
|
||||
|
||||
let buffer = Arc::new(buffer);
|
||||
@@ -773,7 +743,7 @@ impl Device {
|
||||
hal_buffer: Box<dyn hal::DynBuffer>,
|
||||
desc: &resource::BufferDescriptor,
|
||||
) -> (Fallible<Buffer>, Option<resource::CreateBufferError>) {
|
||||
let raw_indirect_validation_bind_group = match self.create_indirect_validation_bind_group(
|
||||
let indirect_validation_bind_groups = match self.create_indirect_validation_bind_groups(
|
||||
hal_buffer.as_ref(),
|
||||
desc.size,
|
||||
desc.usage,
|
||||
@@ -782,13 +752,6 @@ impl Device {
|
||||
Err(e) => return (Fallible::Invalid(Arc::new(desc.label.to_string())), Some(e)),
|
||||
};
|
||||
|
||||
let raw_indirect_draw_validation_bind_group = match self
|
||||
.create_indirect_draw_validation_bind_group(hal_buffer.as_ref(), desc.size, desc.usage)
|
||||
{
|
||||
Ok(ok) => ok,
|
||||
Err(e) => return (Fallible::Invalid(Arc::new(desc.label.to_string())), Some(e)),
|
||||
};
|
||||
|
||||
unsafe { self.raw().add_raw_buffer(&*hal_buffer) };
|
||||
|
||||
let buffer = Buffer {
|
||||
@@ -804,8 +767,7 @@ impl Device {
|
||||
label: desc.label.to_string(),
|
||||
tracking_data: TrackingData::new(self.tracker_indices.buffers.clone()),
|
||||
bind_groups: Mutex::new(rank::BUFFER_BIND_GROUPS, WeakVec::new()),
|
||||
raw_indirect_validation_bind_group,
|
||||
raw_indirect_draw_validation_bind_group,
|
||||
indirect_validation_bind_groups,
|
||||
};
|
||||
|
||||
let buffer = Arc::new(buffer);
|
||||
@@ -818,12 +780,13 @@ impl Device {
|
||||
(Fallible::Valid(buffer), None)
|
||||
}
|
||||
|
||||
fn create_indirect_validation_bind_group(
|
||||
fn create_indirect_validation_bind_groups(
|
||||
&self,
|
||||
raw_buffer: &dyn hal::DynBuffer,
|
||||
buffer_size: u64,
|
||||
usage: wgt::BufferUsages,
|
||||
) -> Result<Snatchable<Box<dyn hal::DynBindGroup>>, resource::CreateBufferError> {
|
||||
) -> Result<Snatchable<crate::indirect_validation::BindGroups>, resource::CreateBufferError>
|
||||
{
|
||||
if !usage.contains(wgt::BufferUsages::INDIRECT) {
|
||||
return Ok(Snatchable::empty());
|
||||
}
|
||||
@@ -832,40 +795,19 @@ impl Device {
|
||||
return Ok(Snatchable::empty());
|
||||
};
|
||||
|
||||
let bind_group = indirect_validation
|
||||
.create_src_bind_group(self.raw(), &self.limits, buffer_size, raw_buffer)
|
||||
.map_err(resource::CreateBufferError::IndirectValidationBindGroup)?;
|
||||
let bind_groups = crate::indirect_validation::BindGroups::new(
|
||||
indirect_validation,
|
||||
self,
|
||||
buffer_size,
|
||||
raw_buffer,
|
||||
)
|
||||
.map_err(resource::CreateBufferError::IndirectValidationBindGroup)?;
|
||||
|
||||
let Some(bind_group) = bind_group else {
|
||||
return Ok(Snatchable::empty());
|
||||
};
|
||||
|
||||
Ok(Snatchable::new(bind_group))
|
||||
}
|
||||
|
||||
fn create_indirect_draw_validation_bind_group(
|
||||
&self,
|
||||
raw_buffer: &dyn hal::DynBuffer,
|
||||
buffer_size: u64,
|
||||
usage: wgt::BufferUsages,
|
||||
) -> Result<Snatchable<Box<dyn hal::DynBindGroup>>, resource::CreateBufferError> {
|
||||
if !usage.contains(wgt::BufferUsages::INDIRECT) {
|
||||
return Ok(Snatchable::empty());
|
||||
if let Some(bind_groups) = bind_groups {
|
||||
Ok(Snatchable::new(bind_groups))
|
||||
} else {
|
||||
Ok(Snatchable::empty())
|
||||
}
|
||||
|
||||
let Some(ref indirect_draw_validation) = self.indirect_draw_validation else {
|
||||
return Ok(Snatchable::empty());
|
||||
};
|
||||
|
||||
let bind_group = indirect_draw_validation
|
||||
.create_src_bind_group(self.raw(), &self.adapter.limits(), buffer_size, raw_buffer)
|
||||
.map_err(resource::CreateBufferError::IndirectValidationBindGroup)?;
|
||||
|
||||
let Some(bind_group) = bind_group else {
|
||||
return Ok(Snatchable::empty());
|
||||
};
|
||||
|
||||
Ok(Snatchable::new(bind_group))
|
||||
}
|
||||
|
||||
pub(crate) fn create_texture(
|
||||
|
||||
+10
-23
@@ -1,23 +1,10 @@
|
||||
use alloc::{boxed::Box, format, string::ToString as _};
|
||||
use core::num::NonZeroU64;
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
use super::CreateIndirectValidationPipelineError;
|
||||
use crate::{
|
||||
device::DeviceError,
|
||||
pipeline::{CreateComputePipelineError, CreateShaderModuleError},
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Error)]
|
||||
#[non_exhaustive]
|
||||
pub enum CreateDispatchIndirectValidationPipelineError {
|
||||
#[error(transparent)]
|
||||
DeviceError(#[from] DeviceError),
|
||||
#[error(transparent)]
|
||||
ShaderModule(#[from] CreateShaderModuleError),
|
||||
#[error(transparent)]
|
||||
ComputePipeline(#[from] CreateComputePipelineError),
|
||||
}
|
||||
use alloc::{boxed::Box, format, string::ToString as _};
|
||||
use core::num::NonZeroU64;
|
||||
|
||||
/// This machinery requires the following limits:
|
||||
///
|
||||
@@ -31,7 +18,7 @@ pub enum CreateDispatchIndirectValidationPipelineError {
|
||||
/// These are all indirectly satisfied by `DownlevelFlags::INDIRECT_EXECUTION`, which is also
|
||||
/// required for this module's functionality to work.
|
||||
#[derive(Debug)]
|
||||
pub struct IndirectValidation {
|
||||
pub(crate) struct Dispatch {
|
||||
module: Box<dyn hal::DynShaderModule>,
|
||||
dst_bind_group_layout: Box<dyn hal::DynBindGroupLayout>,
|
||||
src_bind_group_layout: Box<dyn hal::DynBindGroupLayout>,
|
||||
@@ -50,11 +37,11 @@ pub struct Params<'a> {
|
||||
pub offset_remainder: u64,
|
||||
}
|
||||
|
||||
impl IndirectValidation {
|
||||
pub fn new(
|
||||
impl Dispatch {
|
||||
pub(super) fn new(
|
||||
device: &dyn hal::DynDevice,
|
||||
limits: &wgt::Limits,
|
||||
) -> Result<Self, CreateDispatchIndirectValidationPipelineError> {
|
||||
) -> Result<Self, CreateIndirectValidationPipelineError> {
|
||||
let max_compute_workgroups_per_dimension = limits.max_compute_workgroups_per_dimension;
|
||||
|
||||
let src = format!(
|
||||
@@ -272,7 +259,7 @@ impl IndirectValidation {
|
||||
}
|
||||
|
||||
/// `Ok(None)` will only be returned if `buffer_size` is `0`.
|
||||
pub fn create_src_bind_group(
|
||||
pub(super) fn create_src_bind_group(
|
||||
&self,
|
||||
device: &dyn hal::DynDevice,
|
||||
limits: &wgt::Limits,
|
||||
@@ -341,8 +328,8 @@ impl IndirectValidation {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dispose(self, device: &dyn hal::DynDevice) {
|
||||
let IndirectValidation {
|
||||
pub(super) fn dispose(self, device: &dyn hal::DynDevice) {
|
||||
let Dispatch {
|
||||
module,
|
||||
dst_bind_group_layout,
|
||||
src_bind_group_layout,
|
||||
+27
-36
@@ -1,3 +1,4 @@
|
||||
use super::CreateIndirectValidationPipelineError;
|
||||
use crate::{
|
||||
device::{queue::TempResource, Device, DeviceError},
|
||||
lock::{rank, Mutex},
|
||||
@@ -12,7 +13,6 @@ use core::{
|
||||
mem::{size_of, size_of_val},
|
||||
num::NonZeroU64,
|
||||
};
|
||||
use thiserror::Error;
|
||||
use wgt::Limits;
|
||||
|
||||
/// Note: This needs to be under:
|
||||
@@ -31,17 +31,6 @@ use wgt::Limits;
|
||||
/// - 52428 [`wgt::DrawIndexedIndirectArgs`]
|
||||
const BUFFER_SIZE: wgt::BufferSize = unsafe { wgt::BufferSize::new_unchecked(1_048_560) };
|
||||
|
||||
#[derive(Clone, Debug, Error)]
|
||||
#[non_exhaustive]
|
||||
pub enum CreateDrawIndirectValidationPipelineError {
|
||||
#[error(transparent)]
|
||||
DeviceError(#[from] DeviceError),
|
||||
#[error(transparent)]
|
||||
ShaderModule(#[from] CreateShaderModuleError),
|
||||
#[error(transparent)]
|
||||
ComputePipeline(#[from] CreateComputePipelineError),
|
||||
}
|
||||
|
||||
/// Holds all device-level resources that are needed to validate indirect draws.
|
||||
///
|
||||
/// This machinery requires the following limits:
|
||||
@@ -54,7 +43,7 @@ pub enum CreateDrawIndirectValidationPipelineError {
|
||||
/// These are all indirectly satisfied by `DownlevelFlags::INDIRECT_EXECUTION`, which is also
|
||||
/// required for this module's functionality to work.
|
||||
#[derive(Debug)]
|
||||
pub struct IndirectDrawValidation {
|
||||
pub(crate) struct Draw {
|
||||
module: Box<dyn hal::DynShaderModule>,
|
||||
metadata_bind_group_layout: Box<dyn hal::DynBindGroupLayout>,
|
||||
src_bind_group_layout: Box<dyn hal::DynBindGroupLayout>,
|
||||
@@ -66,11 +55,11 @@ pub struct IndirectDrawValidation {
|
||||
free_metadata_entries: Mutex<Vec<BufferPoolEntry>>,
|
||||
}
|
||||
|
||||
impl IndirectDrawValidation {
|
||||
pub fn new(
|
||||
impl Draw {
|
||||
pub(super) fn new(
|
||||
device: &dyn hal::DynDevice,
|
||||
required_features: &wgt::Features,
|
||||
) -> Result<Self, CreateDrawIndirectValidationPipelineError> {
|
||||
) -> Result<Self, CreateIndirectValidationPipelineError> {
|
||||
let module = create_validation_module(device)?;
|
||||
|
||||
let metadata_bind_group_layout =
|
||||
@@ -121,7 +110,7 @@ impl IndirectDrawValidation {
|
||||
}
|
||||
|
||||
/// `Ok(None)` will only be returned if `buffer_size` is `0`.
|
||||
pub fn create_src_bind_group(
|
||||
pub(super) fn create_src_bind_group(
|
||||
&self,
|
||||
device: &dyn hal::DynDevice,
|
||||
limits: &Limits,
|
||||
@@ -202,10 +191,10 @@ impl IndirectDrawValidation {
|
||||
&self,
|
||||
device: &Arc<Device>,
|
||||
snatch_guard: &SnatchGuard,
|
||||
resources: &mut IndirectDrawValidationResources,
|
||||
resources: &mut DrawResources,
|
||||
temp_resources: &mut Vec<TempResource>,
|
||||
encoder: &mut dyn hal::DynCommandEncoder,
|
||||
batcher: IndirectDrawValidationBatcher,
|
||||
batcher: DrawBatcher,
|
||||
) -> Result<(), DeviceError> {
|
||||
let mut batches = batcher.batches;
|
||||
|
||||
@@ -403,9 +392,10 @@ impl IndirectDrawValidation {
|
||||
|
||||
let src_bind_group = batch
|
||||
.src_buffer
|
||||
.raw_indirect_draw_validation_bind_group
|
||||
.indirect_validation_bind_groups
|
||||
.get(snatch_guard)
|
||||
.unwrap()
|
||||
.draw
|
||||
.as_ref();
|
||||
unsafe {
|
||||
encoder.set_bind_group(
|
||||
@@ -459,8 +449,8 @@ impl IndirectDrawValidation {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn dispose(self, device: &dyn hal::DynDevice) {
|
||||
let IndirectDrawValidation {
|
||||
pub(super) fn dispose(self, device: &dyn hal::DynDevice) {
|
||||
let Draw {
|
||||
module,
|
||||
metadata_bind_group_layout,
|
||||
src_bind_group_layout,
|
||||
@@ -499,8 +489,8 @@ impl IndirectDrawValidation {
|
||||
|
||||
fn create_validation_module(
|
||||
device: &dyn hal::DynDevice,
|
||||
) -> Result<Box<dyn hal::DynShaderModule>, CreateDrawIndirectValidationPipelineError> {
|
||||
let src = include_str!("./indirect_draw_validation.wgsl");
|
||||
) -> Result<Box<dyn hal::DynShaderModule>, CreateIndirectValidationPipelineError> {
|
||||
let src = include_str!("./validate_draw.wgsl");
|
||||
|
||||
#[cfg(feature = "wgsl")]
|
||||
let module = naga::front::wgsl::parse_str(src).map_err(|inner| {
|
||||
@@ -556,7 +546,7 @@ fn create_validation_pipeline(
|
||||
module: &dyn hal::DynShaderModule,
|
||||
pipeline_layout: &dyn hal::DynPipelineLayout,
|
||||
supports_indirect_first_instance: bool,
|
||||
) -> Result<Box<dyn hal::DynComputePipeline>, CreateDrawIndirectValidationPipelineError> {
|
||||
) -> Result<Box<dyn hal::DynComputePipeline>, CreateIndirectValidationPipelineError> {
|
||||
let pipeline_desc = hal::ComputePipelineDescriptor {
|
||||
label: None,
|
||||
layout: pipeline_layout,
|
||||
@@ -593,7 +583,7 @@ fn create_bind_group_layout(
|
||||
read_only: bool,
|
||||
has_dynamic_offset: bool,
|
||||
min_binding_size: wgt::BufferSize,
|
||||
) -> Result<Box<dyn hal::DynBindGroupLayout>, CreateDrawIndirectValidationPipelineError> {
|
||||
) -> Result<Box<dyn hal::DynBindGroupLayout>, CreateIndirectValidationPipelineError> {
|
||||
let bind_group_layout_desc = hal::BindGroupLayoutDescriptor {
|
||||
label: None,
|
||||
flags: hal::BindGroupLayoutFlags::empty(),
|
||||
@@ -727,24 +717,25 @@ struct CurrentEntry {
|
||||
}
|
||||
|
||||
/// Holds all command buffer-level resources that are needed to validate indirect draws.
|
||||
pub(crate) struct IndirectDrawValidationResources {
|
||||
pub(crate) struct DrawResources {
|
||||
device: Arc<Device>,
|
||||
dst_entries: Vec<BufferPoolEntry>,
|
||||
metadata_entries: Vec<BufferPoolEntry>,
|
||||
}
|
||||
|
||||
impl Drop for IndirectDrawValidationResources {
|
||||
impl Drop for DrawResources {
|
||||
fn drop(&mut self) {
|
||||
if let Some(ref indirect_draw_validation) = self.device.indirect_draw_validation {
|
||||
if let Some(ref indirect_validation) = self.device.indirect_validation {
|
||||
let indirect_draw_validation = &indirect_validation.draw;
|
||||
indirect_draw_validation.release_dst_entries(self.dst_entries.drain(..));
|
||||
indirect_draw_validation.release_metadata_entries(self.metadata_entries.drain(..));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IndirectDrawValidationResources {
|
||||
impl DrawResources {
|
||||
pub(crate) fn new(device: Arc<Device>) -> Self {
|
||||
IndirectDrawValidationResources {
|
||||
DrawResources {
|
||||
device,
|
||||
dst_entries: Vec::new(),
|
||||
metadata_entries: Vec::new(),
|
||||
@@ -776,7 +767,7 @@ impl IndirectDrawValidationResources {
|
||||
size: u64,
|
||||
current_entry: &mut Option<CurrentEntry>,
|
||||
) -> Result<(usize, u64), DeviceError> {
|
||||
let indirect_draw_validation = self.device.indirect_draw_validation.as_ref().unwrap();
|
||||
let indirect_draw_validation = &self.device.indirect_validation.as_ref().unwrap().draw;
|
||||
let ensure_entry = |index: usize| {
|
||||
if self.dst_entries.len() <= index {
|
||||
let entry = indirect_draw_validation.acquire_dst_entry(self.device.raw())?;
|
||||
@@ -793,7 +784,7 @@ impl IndirectDrawValidationResources {
|
||||
size: u64,
|
||||
current_entry: &mut Option<CurrentEntry>,
|
||||
) -> Result<(usize, u64), DeviceError> {
|
||||
let indirect_draw_validation = self.device.indirect_draw_validation.as_ref().unwrap();
|
||||
let indirect_draw_validation = &self.device.indirect_validation.as_ref().unwrap().draw;
|
||||
let ensure_entry = |index: usize| {
|
||||
if self.metadata_entries.len() <= index {
|
||||
let entry = indirect_draw_validation.acquire_metadata_entry(self.device.raw())?;
|
||||
@@ -918,12 +909,12 @@ impl DrawIndirectValidationBatch {
|
||||
}
|
||||
|
||||
/// Accumulates all needed data needed to validate indirect draws.
|
||||
pub(crate) struct IndirectDrawValidationBatcher {
|
||||
pub(crate) struct DrawBatcher {
|
||||
batches: FastHashMap<(TrackerIndex, u64, usize), DrawIndirectValidationBatch>,
|
||||
current_dst_entry: Option<CurrentEntry>,
|
||||
}
|
||||
|
||||
impl IndirectDrawValidationBatcher {
|
||||
impl DrawBatcher {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self {
|
||||
batches: FastHashMap::default(),
|
||||
@@ -937,7 +928,7 @@ impl IndirectDrawValidationBatcher {
|
||||
/// and the offset to be used for the draw.
|
||||
pub(crate) fn add<'a>(
|
||||
&mut self,
|
||||
indirect_draw_validation_resources: &'a mut IndirectDrawValidationResources,
|
||||
indirect_draw_validation_resources: &'a mut DrawResources,
|
||||
device: &Device,
|
||||
src_buffer: &Arc<crate::resource::Buffer>,
|
||||
offset: u64,
|
||||
@@ -0,0 +1,104 @@
|
||||
use crate::{
|
||||
device::DeviceError,
|
||||
pipeline::{CreateComputePipelineError, CreateShaderModuleError},
|
||||
};
|
||||
use alloc::boxed::Box;
|
||||
use thiserror::Error;
|
||||
|
||||
mod dispatch;
|
||||
mod draw;
|
||||
|
||||
pub(crate) use dispatch::Dispatch;
|
||||
pub(crate) use draw::{Draw, DrawBatcher, DrawResources};
|
||||
|
||||
#[derive(Clone, Debug, Error)]
|
||||
#[non_exhaustive]
|
||||
enum CreateIndirectValidationPipelineError {
|
||||
#[error(transparent)]
|
||||
DeviceError(#[from] DeviceError),
|
||||
#[error(transparent)]
|
||||
ShaderModule(#[from] CreateShaderModuleError),
|
||||
#[error(transparent)]
|
||||
ComputePipeline(#[from] CreateComputePipelineError),
|
||||
}
|
||||
|
||||
pub(crate) struct IndirectValidation {
|
||||
pub(crate) dispatch: Dispatch,
|
||||
pub(crate) draw: Draw,
|
||||
}
|
||||
|
||||
impl IndirectValidation {
|
||||
pub(crate) fn new(
|
||||
device: &dyn hal::DynDevice,
|
||||
required_limits: &wgt::Limits,
|
||||
required_features: &wgt::Features,
|
||||
) -> Result<Self, DeviceError> {
|
||||
let dispatch = match Dispatch::new(device, required_limits) {
|
||||
Ok(dispatch) => dispatch,
|
||||
Err(e) => {
|
||||
log::error!("indirect-validation error: {e:?}");
|
||||
return Err(DeviceError::Lost);
|
||||
}
|
||||
};
|
||||
let draw = match Draw::new(device, required_features) {
|
||||
Ok(draw) => draw,
|
||||
Err(e) => {
|
||||
log::error!("indirect-draw-validation error: {e:?}");
|
||||
return Err(DeviceError::Lost);
|
||||
}
|
||||
};
|
||||
Ok(Self { dispatch, draw })
|
||||
}
|
||||
|
||||
pub(crate) fn dispose(self, device: &dyn hal::DynDevice) {
|
||||
let Self { dispatch, draw } = self;
|
||||
|
||||
dispatch.dispose(device);
|
||||
draw.dispose(device);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct BindGroups {
|
||||
pub(crate) dispatch: Box<dyn hal::DynBindGroup>,
|
||||
draw: Box<dyn hal::DynBindGroup>,
|
||||
}
|
||||
|
||||
impl BindGroups {
|
||||
/// `Ok(None)` will only be returned if `buffer_size` is `0`.
|
||||
pub(crate) fn new(
|
||||
indirect_validation: &IndirectValidation,
|
||||
device: &crate::device::Device,
|
||||
buffer_size: u64,
|
||||
buffer: &dyn hal::DynBuffer,
|
||||
) -> Result<Option<Self>, DeviceError> {
|
||||
let dispatch = indirect_validation.dispatch.create_src_bind_group(
|
||||
device.raw(),
|
||||
&device.limits,
|
||||
buffer_size,
|
||||
buffer,
|
||||
)?;
|
||||
let draw = indirect_validation.draw.create_src_bind_group(
|
||||
device.raw(),
|
||||
&device.adapter.limits(),
|
||||
buffer_size,
|
||||
buffer,
|
||||
)?;
|
||||
|
||||
match (dispatch, draw) {
|
||||
(None, None) => Ok(None),
|
||||
(None, Some(_)) => unreachable!(),
|
||||
(Some(_), None) => unreachable!(),
|
||||
(Some(dispatch), Some(draw)) => Ok(Some(Self { dispatch, draw })),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn dispose(self, device: &dyn hal::DynDevice) {
|
||||
let Self { dispatch, draw } = self;
|
||||
|
||||
unsafe {
|
||||
device.destroy_bind_group(dispatch);
|
||||
device.destroy_bind_group(draw);
|
||||
}
|
||||
}
|
||||
}
|
||||
File renamed without changes.
@@ -76,7 +76,6 @@ mod hash_utils;
|
||||
pub mod hub;
|
||||
pub mod id;
|
||||
pub mod identity;
|
||||
mod indirect_draw_validation;
|
||||
mod indirect_validation;
|
||||
mod init_tracker;
|
||||
pub mod instance;
|
||||
|
||||
@@ -366,21 +366,13 @@ pub struct Buffer {
|
||||
pub(crate) tracking_data: TrackingData,
|
||||
pub(crate) map_state: Mutex<BufferMapState>,
|
||||
pub(crate) bind_groups: Mutex<WeakVec<BindGroup>>,
|
||||
pub(crate) raw_indirect_validation_bind_group: Snatchable<Box<dyn hal::DynBindGroup>>,
|
||||
pub(crate) raw_indirect_draw_validation_bind_group: Snatchable<Box<dyn hal::DynBindGroup>>,
|
||||
pub(crate) indirect_validation_bind_groups: Snatchable<crate::indirect_validation::BindGroups>,
|
||||
}
|
||||
|
||||
impl Drop for Buffer {
|
||||
fn drop(&mut self) {
|
||||
if let Some(raw) = self.raw_indirect_validation_bind_group.take() {
|
||||
unsafe {
|
||||
self.device.raw().destroy_bind_group(raw);
|
||||
}
|
||||
}
|
||||
if let Some(raw) = self.raw_indirect_draw_validation_bind_group.take() {
|
||||
unsafe {
|
||||
self.device.raw().destroy_bind_group(raw);
|
||||
}
|
||||
if let Some(raw) = self.indirect_validation_bind_groups.take() {
|
||||
raw.dispose(self.device.raw());
|
||||
}
|
||||
if let Some(raw) = self.raw.take() {
|
||||
resource_log!("Destroy raw {}", self.error_ident());
|
||||
@@ -716,12 +708,8 @@ impl Buffer {
|
||||
}
|
||||
};
|
||||
|
||||
let raw_indirect_validation_bind_group = self
|
||||
.raw_indirect_validation_bind_group
|
||||
.snatch(&mut snatch_guard);
|
||||
|
||||
let raw_indirect_draw_validation_bind_group = self
|
||||
.raw_indirect_draw_validation_bind_group
|
||||
let indirect_validation_bind_groups = self
|
||||
.indirect_validation_bind_groups
|
||||
.snatch(&mut snatch_guard);
|
||||
|
||||
drop(snatch_guard);
|
||||
@@ -736,8 +724,7 @@ impl Buffer {
|
||||
device: Arc::clone(&self.device),
|
||||
label: self.label().to_owned(),
|
||||
bind_groups,
|
||||
raw_indirect_validation_bind_group,
|
||||
raw_indirect_draw_validation_bind_group,
|
||||
indirect_validation_bind_groups,
|
||||
})
|
||||
};
|
||||
|
||||
@@ -792,8 +779,7 @@ pub struct DestroyedBuffer {
|
||||
device: Arc<Device>,
|
||||
label: String,
|
||||
bind_groups: WeakVec<BindGroup>,
|
||||
raw_indirect_validation_bind_group: Option<Box<dyn hal::DynBindGroup>>,
|
||||
raw_indirect_draw_validation_bind_group: Option<Box<dyn hal::DynBindGroup>>,
|
||||
indirect_validation_bind_groups: Option<crate::indirect_validation::BindGroups>,
|
||||
}
|
||||
|
||||
impl DestroyedBuffer {
|
||||
@@ -810,16 +796,8 @@ impl Drop for DestroyedBuffer {
|
||||
)));
|
||||
drop(deferred);
|
||||
|
||||
if let Some(raw) = self.raw_indirect_validation_bind_group.take() {
|
||||
unsafe {
|
||||
self.device.raw().destroy_bind_group(raw);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(raw) = self.raw_indirect_draw_validation_bind_group.take() {
|
||||
unsafe {
|
||||
self.device.raw().destroy_bind_group(raw);
|
||||
}
|
||||
if let Some(raw) = self.indirect_validation_bind_groups.take() {
|
||||
raw.dispose(self.device.raw());
|
||||
}
|
||||
|
||||
resource_log!("Destroy raw Buffer (destroyed) {:?}", self.label());
|
||||
|
||||
Reference in new issue
Block a user