use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use crate::runtime::{BufferBinding, Device, PushConstant};
use crate::{Engine, Error, Matrix, Result};
pub struct Kernel {
name: &'static str,
key: Vec<u8>,
bindings: usize,
push_words: usize,
device: Arc<Device>,
}
#[derive(Clone, Copy)]
pub enum KernelBinding<'a> {
Read(&'a Matrix),
Write(&'a Matrix),
ReadWrite(&'a Matrix),
}
#[derive(Clone, Copy, Debug)]
pub enum KernelPush {
U32(u32),
F32(f32),
}
impl Kernel {
pub fn compile(
engine: &Engine,
name: &'static str,
spirv: &[u8],
bindings: u32,
workgroup_size: [u32; 3],
) -> Result<Self> {
let device = engine.handle().device();
let words = crate::runtime::decode_spirv_bytes(spirv)?;
let mut key = DefaultHasher::new();
name.hash(&mut key);
spirv.hash(&mut key);
bindings.hash(&mut key);
workgroup_size.hash(&mut key);
let key = key.finish().to_le_bytes().to_vec();
let pipeline = device.admit_extension_pipeline(key.clone(), &words, workgroup_size)?;
let push_words = usize::try_from(pipeline.push_constant_size()).map_err(|_| {
Error::internal(format!(
"extension kernel {name} push-constant size does not fit this platform"
))
})? / 4;
let push_words = push_words.checked_sub(bindings as usize).ok_or_else(|| {
Error::invalid_argument(format!(
"extension kernel {name} declares {bindings} buffer bindings but its \
push-constant block holds fewer words"
))
})?;
Ok(Self {
name,
key,
bindings: bindings as usize,
push_words,
device,
})
}
pub const fn push_words(&self) -> usize {
self.push_words
}
pub const fn binding_count(&self) -> usize {
self.bindings
}
pub(crate) const fn name(&self) -> &'static str {
self.name
}
pub(crate) fn key(&self) -> &[u8] {
&self.key
}
}
impl Engine {
pub fn dispatch_kernel(
&self,
kernel: &Kernel,
bindings: &[KernelBinding<'_>],
push_constants: &[KernelPush],
workgroups: [u32; 3],
) -> Result<()> {
if bindings.len() != kernel.bindings {
return Err(Error::invalid_argument(format!(
"extension kernel {} declares {} bindings, got {}",
kernel.name,
kernel.bindings,
bindings.len()
)));
}
if push_constants.len() != kernel.push_words {
return Err(Error::invalid_argument(format!(
"extension kernel {} declares {} push words, got {}",
kernel.name,
kernel.push_words,
push_constants.len()
)));
}
let resolved = bindings
.iter()
.map(|binding| match *binding {
KernelBinding::Read(matrix) => BufferBinding::read(matrix.storage()),
KernelBinding::Write(matrix) => BufferBinding::write(matrix.storage()),
KernelBinding::ReadWrite(matrix) => BufferBinding::read_write(matrix.storage()),
})
.collect::<Vec<_>>();
let pushes = push_constants
.iter()
.map(|push| match *push {
KernelPush::U32(value) => PushConstant::U32(value),
KernelPush::F32(value) => PushConstant::F32(value),
})
.collect::<Vec<_>>();
if !std::sync::Arc::ptr_eq(&kernel.device, &self.handle().device()) {
return Err(Error::invalid_argument(format!(
"extension kernel {} was compiled for a different engine",
kernel.name
)));
}
self
.handle()
.record_extension(kernel, &resolved, &pushes, workgroups)
}
}
#[cfg(test)]
#[path = "../../../test/rs/runtime/compute_unit.rs"]
mod tests;