mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-23 07:51:08 +01:00
For storage and uniform buffer bindings, the size is a `wgt::BufferSize` (`NonZeroU64`). For vertex and index buffer bindings, the size is a `wgt::BufferAddress` (regular `u64`). Temporarily breaks some zero-size vertex/index binding cases that worked before (end of buffer); will be resolved in an immediate follow-up commit that fully implements zero-size vertex/index binding support.
126 lines
3.5 KiB
Rust
126 lines
3.5 KiB
Rust
use crate::{
|
|
device::DeviceError,
|
|
pipeline::{CreateComputePipelineError, CreateShaderModuleError},
|
|
};
|
|
use alloc::boxed::Box;
|
|
use scopeguard::{guard, ScopeGuard};
|
|
use thiserror::Error;
|
|
|
|
mod dispatch;
|
|
mod draw;
|
|
mod utils;
|
|
|
|
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,
|
|
instance_flags: wgt::InstanceFlags,
|
|
backend: wgt::Backend,
|
|
) -> Result<Self, DeviceError> {
|
|
let dispatch = match Dispatch::new(device, instance_flags, required_limits) {
|
|
Ok(dispatch) => dispatch,
|
|
Err(e) => {
|
|
log::error!("indirect-validation error: {e:?}");
|
|
return Err(DeviceError::Lost);
|
|
}
|
|
};
|
|
let dispatch = guard(dispatch, |dispatch| dispatch.dispose(device));
|
|
|
|
let draw = match Draw::new(
|
|
device,
|
|
required_features,
|
|
instance_flags,
|
|
backend,
|
|
required_limits,
|
|
) {
|
|
Ok(draw) => draw,
|
|
Err(e) => {
|
|
log::error!("indirect-draw-validation error: {e:?}");
|
|
return Err(DeviceError::Lost);
|
|
}
|
|
};
|
|
Ok(Self {
|
|
dispatch: ScopeGuard::into_inner(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 {
|
|
/// Creates the bind groups for indirect validation shaders to read from `buffer`.
|
|
///
|
|
/// `buffer_size` is the user-requested size of the buffer.
|
|
///
|
|
/// `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,
|
|
device.instance_flags,
|
|
)?;
|
|
let draw = indirect_validation.draw.create_src_bind_group(
|
|
device.raw(),
|
|
&device.adapter.limits(),
|
|
buffer_size,
|
|
buffer,
|
|
device.instance_flags,
|
|
)?;
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|