Skip to main content

scry_gpu/
kernel.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Precompiled compute kernels for reuse across dispatches.
3
4use crate::backend::BackendKernel;
5
6/// A compiled GPU compute kernel, ready for repeated dispatch.
7///
8/// Created via [`Device::compile`]. Holds all GPU objects needed to
9/// dispatch a shader — SPIR-V, Vulkan pipeline, layouts — so that
10/// repeated dispatches skip compilation entirely.
11///
12/// # Example
13///
14/// ```ignore
15/// let kernel = gpu.compile(SHADER_SRC)?;
16/// for batch in &batches {
17///     gpu.run(&kernel, &[&batch.input, &batch.output], batch.len)?;
18/// }
19/// ```
20pub struct Kernel {
21    pub(crate) inner: BackendKernel,
22    /// Number of storage buffer bindings the shader expects.
23    pub(crate) binding_count: usize,
24    /// Workgroup size extracted from the shader's `@workgroup_size`.
25    pub(crate) workgroup_size: [u32; 3],
26    /// Entry point name.
27    pub(crate) entry_point: String,
28}
29
30impl Kernel {
31    /// Number of buffer bindings the shader expects.
32    pub const fn binding_count(&self) -> usize {
33        self.binding_count
34    }
35
36    /// The entry point name compiled into this kernel.
37    pub fn entry_point(&self) -> &str {
38        &self.entry_point
39    }
40
41    /// Workgroup size `[x, y, z]` declared in the shader.
42    pub const fn workgroup_size(&self) -> [u32; 3] {
43        self.workgroup_size
44    }
45}
46
47impl std::fmt::Debug for Kernel {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        f.debug_struct("Kernel")
50            .field("entry_point", &self.entry_point)
51            .field("binding_count", &self.binding_count)
52            .field("workgroup_size", &self.workgroup_size)
53            .finish_non_exhaustive()
54    }
55}