use crate::device::WgpuDevice;
use crate::device::backend::{BufferBinding, DispatchDescriptor, GpuBackend};
use crate::device::capabilities::WORKGROUP_SIZE_1D;
use crate::error::{BarracudaError, Result};
struct Binding<'a, B: GpuBackend> {
index: u32,
buffer: &'a B::Buffer,
read_only: bool,
is_uniform: bool,
}
pub struct ComputeDispatch<'a, B: GpuBackend = WgpuDevice> {
device: &'a B,
label: &'a str,
bindings: Vec<Binding<'a, B>>,
shader_source: Option<&'a str>,
entry_point: &'a str,
workgroups: (u32, u32, u32),
f64_shader: bool,
df64_shader: bool,
hardware_hint: Option<crate::device::backend::HardwareHint>,
}
impl<'a, B: GpuBackend> ComputeDispatch<'a, B> {
#[must_use]
pub fn new(device: &'a B, label: &'a str) -> Self {
Self {
device,
label,
bindings: Vec::new(),
shader_source: None,
entry_point: "main",
workgroups: (1, 1, 1),
f64_shader: false,
df64_shader: false,
hardware_hint: None,
}
}
#[must_use]
pub fn shader(mut self, source: &'a str, entry_point: &'a str) -> Self {
self.shader_source = Some(source);
self.entry_point = entry_point;
self
}
#[must_use]
pub fn f64(mut self) -> Self {
self.f64_shader = true;
self
}
#[must_use]
pub fn df64(mut self) -> Self {
self.df64_shader = true;
self
}
#[must_use]
pub fn hardware_hint(mut self, hint: crate::device::backend::HardwareHint) -> Self {
self.hardware_hint = Some(hint);
self
}
#[must_use]
pub fn storage_read(mut self, index: u32, buffer: &'a B::Buffer) -> Self {
self.bindings.push(Binding {
index,
buffer,
read_only: true,
is_uniform: false,
});
self
}
#[must_use]
pub fn storage_rw(mut self, index: u32, buffer: &'a B::Buffer) -> Self {
self.bindings.push(Binding {
index,
buffer,
read_only: false,
is_uniform: false,
});
self
}
#[must_use]
pub fn uniform(mut self, index: u32, buffer: &'a B::Buffer) -> Self {
self.bindings.push(Binding {
index,
buffer,
read_only: true,
is_uniform: true,
});
self
}
#[must_use]
pub fn dispatch_1d(mut self, element_count: u32) -> Self {
self.workgroups = (element_count.div_ceil(WORKGROUP_SIZE_1D), 1, 1);
self
}
#[must_use]
pub fn dispatch(mut self, x: u32, y: u32, z: u32) -> Self {
self.workgroups = (x, y, z);
self
}
pub fn build(self) -> Result<DispatchDescriptor<'a, B>> {
let source = self
.shader_source
.ok_or_else(|| BarracudaError::InvalidInput {
message: "ComputeDispatch: shader source required".into(),
})?;
let bindings: Vec<BufferBinding<'_, B>> = self
.bindings
.iter()
.map(|b| BufferBinding {
index: b.index,
buffer: b.buffer,
read_only: b.read_only,
is_uniform: b.is_uniform,
})
.collect();
let resolved_hint = self
.hardware_hint
.unwrap_or(crate::device::backend::HardwareHint::Compute);
Ok(DispatchDescriptor {
label: self.label,
shader_source: source,
entry_point: self.entry_point,
bindings,
workgroups: self.workgroups,
f64_shader: self.f64_shader,
df64_shader: self.df64_shader,
hardware_hint: resolved_hint,
})
}
pub fn submit(self) -> Result<()> {
let device = self.device;
let desc = self.build()?;
device.dispatch_compute(desc)
}
}
pub struct BatchedComputeDispatch<'a, B: GpuBackend = WgpuDevice> {
device: &'a B,
descriptors: Vec<DispatchDescriptor<'a, B>>,
}
impl<'a, B: GpuBackend> BatchedComputeDispatch<'a, B> {
#[must_use]
pub fn new(device: &'a B) -> Self {
Self {
device,
descriptors: Vec::new(),
}
}
pub fn push(&mut self, dispatch: ComputeDispatch<'a, B>) -> Result<()> {
self.descriptors.push(dispatch.build()?);
Ok(())
}
#[must_use]
pub fn len(&self) -> usize {
self.descriptors.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.descriptors.is_empty()
}
pub fn submit(self) -> Result<()> {
self.device.dispatch_compute_batch(self.descriptors)
}
}
#[must_use]
pub fn storage_bgl_entry(binding: u32, read_only: bool) -> wgpu::BindGroupLayoutEntry {
wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}
}
#[must_use]
pub fn uniform_bgl_entry(binding: u32) -> wgpu::BindGroupLayoutEntry {
wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}
}
pub struct BglBuilder {
label: &'static str,
entries: Vec<wgpu::BindGroupLayoutEntry>,
}
impl BglBuilder {
#[must_use]
pub fn new(label: &'static str) -> Self {
Self {
label,
entries: Vec::new(),
}
}
#[must_use]
pub fn storage_read(mut self, index: u32) -> Self {
self.entries.push(storage_bgl_entry(index, true));
self
}
#[must_use]
pub fn storage_rw(mut self, index: u32) -> Self {
self.entries.push(storage_bgl_entry(index, false));
self
}
#[must_use]
pub fn uniform(mut self, index: u32) -> Self {
self.entries.push(uniform_bgl_entry(index));
self
}
#[must_use]
pub fn entry(mut self, entry: wgpu::BindGroupLayoutEntry) -> Self {
self.entries.push(entry);
self
}
#[must_use]
pub fn len(&self) -> usize {
self.entries.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
#[must_use]
pub fn build(self, device: &wgpu::Device) -> wgpu::BindGroupLayout {
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some(self.label),
entries: &self.entries,
})
}
}