primal-barracuda 0.4.0

barraCuda — BARrier-free Rust Abstracted Cross-platform Unified Dimensional Algebra. Vendor-agnostic GPU compute via WGSL.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// SPDX-License-Identifier: AGPL-3.0-or-later
//! GPU compute pipeline builder — eliminates bind-group/pipeline boilerplate.
//!
//! The ~80 lines of `create_bind_group_layout` → `create_bind_group` →
//! `create_pipeline_layout` → `create_compute_pipeline` → `create_command_encoder` →
//! `begin_compute_pass` → `set_pipeline` → `set_bind_group` → dispatch pattern is
//! repeated in 50+ ops. This module captures that pattern as a builder.
//!
//! `ComputeDispatch` is generic over `GpuBackend`, defaulting to `WgpuDevice`.
//! Existing callers need no changes — the type parameter is inferred. Future
//! backends (e.g. `SovereignDevice`) work through the same builder.
//!
//! # Example
//!
//! ```ignore
//! use barracuda::device::compute_pipeline::ComputeDispatch;
//!
//! ComputeDispatch::new(&device, "MyOp")
//!     .shader(source, "main")
//!     .storage_read(0, &input_buf)
//!     .storage_rw(1, &output_buf)
//!     .uniform(2, &params_buf)
//!     .dispatch_1d(element_count)
//!     .submit()?;
//! ```

use crate::device::WgpuDevice;
use crate::device::backend::{BufferBinding, DispatchDescriptor, GpuBackend};
use crate::device::capabilities::WORKGROUP_SIZE_1D;
use crate::error::{BarracudaError, Result};

/// Buffer binding declaration for the pipeline builder.
struct Binding<'a, B: GpuBackend> {
    index: u32,
    buffer: &'a B::Buffer,
    read_only: bool,
    is_uniform: bool,
}

/// Ergonomic one-shot compute dispatch builder.
///
/// Builds the bind-group layout, bind group, pipeline layout, pipeline,
/// command encoder, and compute pass from a fluent API, then submits.
///
/// Generic over `GpuBackend`, defaulting to `WgpuDevice`. The type parameter
/// is inferred from the `device` argument — existing callers compile unchanged.
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> {
    /// Creates a new compute dispatch builder for the given device and label.
    #[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,
        }
    }

    /// Set the WGSL shader source and entry point.
    #[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
    }

    /// Use f64 shader compilation path (with precision patching + ILP optimizer).
    #[must_use]
    pub fn f64(mut self) -> Self {
        self.f64_shader = true;
        self
    }

    /// Use DF64 shader compilation path (f32-pair arithmetic, ~48-bit mantissa).
    ///
    /// The source must already contain DF64 bridge functions (from naga rewrite).
    /// This method prepends `df64_core` + `df64_transcendentals` and compiles.
    #[must_use]
    pub fn df64(mut self) -> Self {
        self.df64_shader = true;
        self
    }

    /// Override the hardware routing hint for this dispatch.
    ///
    /// If not set, the hint is derived from precision flags:
    /// - `f64()` / `df64()` → `Compute` (FP64 cores / ALU emulation)
    /// - default → `Compute`
    ///
    /// Use this to explicitly route to fixed-function units (e.g., `TensorCore`
    /// for F16/BF16 MMA workloads, `TextureUnit` for lookup tables).
    #[must_use]
    pub fn hardware_hint(mut self, hint: crate::device::backend::HardwareHint) -> Self {
        self.hardware_hint = Some(hint);
        self
    }

    /// Bind a read-only storage buffer at `index`.
    #[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
    }

    /// Bind a read-write storage buffer at `index`.
    #[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
    }

    /// Bind a uniform buffer at `index`.
    #[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
    }

    /// Set dispatch to `ceil(n / WORKGROUP_SIZE_1D)` workgroups in x.
    #[must_use]
    pub fn dispatch_1d(mut self, element_count: u32) -> Self {
        self.workgroups = (element_count.div_ceil(WORKGROUP_SIZE_1D), 1, 1);
        self
    }

    /// Set explicit workgroup counts.
    #[must_use]
    pub fn dispatch(mut self, x: u32, y: u32, z: u32) -> Self {
        self.workgroups = (x, y, z);
        self
    }

    /// Build the dispatch descriptor without submitting.
    ///
    /// Use this with [`BatchedComputeDispatch`] to record multiple dispatches
    /// into a single GPU submission, reducing per-dispatch overhead (~1.8x on
    /// Vulkan for multi-kernel pipelines like MD force+kick+drift).
    ///
    /// # Errors
    /// Returns [`Err`] if shader source was not set via [`shader`](Self::shader).
    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,
        })
    }

    /// Build everything and submit the compute pass to the device queue.
    ///
    /// Delegates to [`GpuBackend::dispatch_compute`], which handles the full
    /// compile → bind → submit lifecycle using the backend's native API.
    ///
    /// # Errors
    /// Returns [`Err`] if shader source was not set via [`shader`](Self::shader),
    /// or if the backend's dispatch fails.
    pub fn submit(self) -> Result<()> {
        let device = self.device;
        let desc = self.build()?;
        device.dispatch_compute(desc)
    }
}

/// Batched compute dispatch — multiple kernels in a single GPU submission.
///
/// Reduces per-dispatch overhead by recording all compute passes into one
/// command encoder before submitting. On Vulkan this avoids repeated
/// queue submissions (~1.8x overhead measured by hotSpring on RTX 3090).
///
/// Backends that don't support batching (e.g. sovereign/DRM) fall back to
/// sequential `dispatch_compute` calls via the default trait implementation.
///
/// # Example
///
/// ```ignore
/// let mut batch = BatchedComputeDispatch::new(&device);
/// batch.push(
///     ComputeDispatch::new(&device, "force")
///         .shader(FORCE_SHADER, "main").f64()
///         .storage_read(0, &pos).storage_rw(1, &force)
///         .dispatch_1d(n)
/// )?;
/// batch.push(
///     ComputeDispatch::new(&device, "kick_drift")
///         .shader(KD_SHADER, "main").f64()
///         .storage_rw(0, &pos).storage_rw(1, &vel)
///         .dispatch_1d(n)
/// )?;
/// batch.submit()?;
/// ```
pub struct BatchedComputeDispatch<'a, B: GpuBackend = WgpuDevice> {
    device: &'a B,
    descriptors: Vec<DispatchDescriptor<'a, B>>,
}

impl<'a, B: GpuBackend> BatchedComputeDispatch<'a, B> {
    /// Create a new batched dispatch for the given device.
    #[must_use]
    pub fn new(device: &'a B) -> Self {
        Self {
            device,
            descriptors: Vec::new(),
        }
    }

    /// Add a dispatch to the batch.
    ///
    /// The dispatch is built (validated) immediately but not submitted until
    /// [`submit`](Self::submit) is called.
    ///
    /// # Errors
    /// Returns [`Err`] if the dispatch builder is missing a shader source.
    pub fn push(&mut self, dispatch: ComputeDispatch<'a, B>) -> Result<()> {
        self.descriptors.push(dispatch.build()?);
        Ok(())
    }

    /// Number of dispatches queued so far.
    #[must_use]
    pub fn len(&self) -> usize {
        self.descriptors.len()
    }

    /// Whether the batch is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.descriptors.is_empty()
    }

    /// Submit all queued dispatches in a single GPU submission.
    ///
    /// On backends that support batching (e.g. wgpu/Vulkan), all compute
    /// passes are recorded into one command encoder. On backends without
    /// batching support, dispatches are executed sequentially.
    ///
    /// # Errors
    /// Returns [`Err`] if any dispatch fails (compilation, dispatch, or sync).
    pub fn submit(self) -> Result<()> {
        self.device.dispatch_compute_batch(self.descriptors)
    }
}

/// Helper to create a storage bind-group-layout entry.
///
/// Replaces the 7-line `BindGroupLayoutEntry` struct literal that appears
/// in every GPU op. Use for ops that need the BGL separately (e.g.,
/// cached pipelines that outlive a single dispatch).
#[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,
    }
}

/// Helper to create a uniform bind-group-layout entry.
#[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,
    }
}

/// Declarative bind-group layout builder for reusable compute pipelines.
///
/// Many ops create identical bind-group layouts every dispatch. This builder
/// lets callers declare the layout once and cache the resulting
/// `wgpu::BindGroupLayout` for reuse across dispatches.
///
/// # Example
///
/// ```ignore
/// use barracuda::device::compute_pipeline::BglBuilder;
///
/// let bgl = BglBuilder::new("my_op")
///     .storage_read(0)
///     .storage_rw(1)
///     .uniform(2)
///     .build(&device.device);
/// ```
pub struct BglBuilder {
    label: &'static str,
    entries: Vec<wgpu::BindGroupLayoutEntry>,
}

impl BglBuilder {
    /// Create a new builder with the given label.
    #[must_use]
    pub fn new(label: &'static str) -> Self {
        Self {
            label,
            entries: Vec::new(),
        }
    }

    /// Add a read-only storage buffer binding at `index`.
    #[must_use]
    pub fn storage_read(mut self, index: u32) -> Self {
        self.entries.push(storage_bgl_entry(index, true));
        self
    }

    /// Add a read-write storage buffer binding at `index`.
    #[must_use]
    pub fn storage_rw(mut self, index: u32) -> Self {
        self.entries.push(storage_bgl_entry(index, false));
        self
    }

    /// Add a uniform buffer binding at `index`.
    #[must_use]
    pub fn uniform(mut self, index: u32) -> Self {
        self.entries.push(uniform_bgl_entry(index));
        self
    }

    /// Add an arbitrary `BindGroupLayoutEntry` for advanced use cases.
    #[must_use]
    pub fn entry(mut self, entry: wgpu::BindGroupLayoutEntry) -> Self {
        self.entries.push(entry);
        self
    }

    /// Number of entries declared so far.
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Whether no entries have been added.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Build the `BindGroupLayout` on the given device.
    #[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,
        })
    }
}