use crate::error::Result;
use std::sync::Arc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Vendor {
Nvidia,
Amd,
Intel,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DriverKind {
NvkMesa,
NvidiaProprietary,
RadvMesa,
AmdProprietary,
IntelMesa,
Sovereign,
Software,
Unknown,
}
impl std::fmt::Display for Vendor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Nvidia => f.write_str("NVIDIA"),
Self::Amd => f.write_str("AMD"),
Self::Intel => f.write_str("Intel"),
Self::Unknown => f.write_str("Unknown"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MemoryKind {
Gddr5,
Hbm2,
Hbm3,
Gddr6,
Gddr6x,
Gddr7,
}
#[derive(Debug, Clone)]
pub struct BackendCapabilities {
pub vendor: Vendor,
pub driver_kind: DriverKind,
pub device_name: String,
pub has_hardware_f64: bool,
pub has_hardware_f64_rcp: bool,
pub has_full_rate_fp64: bool,
pub memory_kind: MemoryKind,
}
impl BackendCapabilities {
#[must_use]
pub fn unknown() -> Self {
Self {
vendor: Vendor::Unknown,
driver_kind: DriverKind::Unknown,
device_name: String::from("unknown"),
has_hardware_f64: false,
has_hardware_f64_rcp: false,
has_full_rate_fp64: false,
memory_kind: MemoryKind::Gddr6,
}
}
#[must_use]
pub fn is_mesa(&self) -> bool {
matches!(
self.driver_kind,
DriverKind::NvkMesa | DriverKind::RadvMesa | DriverKind::IntelMesa
)
}
#[must_use]
pub fn is_nvk(&self) -> bool {
matches!(self.driver_kind, DriverKind::NvkMesa)
}
#[must_use]
pub fn is_nvidia_proprietary(&self) -> bool {
matches!(self.driver_kind, DriverKind::NvidiaProprietary)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum HardwareHint {
#[default]
Compute,
TensorCore,
RtCore,
ZBuffer,
TextureUnit,
RopBlend,
}
pub struct BufferBinding<'a, B: GpuBackend + ?Sized> {
pub index: u32,
pub buffer: &'a B::Buffer,
pub read_only: bool,
pub is_uniform: bool,
}
pub struct DispatchDescriptor<'a, B: GpuBackend + ?Sized> {
pub label: &'a str,
pub shader_source: &'a str,
pub entry_point: &'a str,
pub bindings: Vec<BufferBinding<'a, B>>,
pub workgroups: (u32, u32, u32),
pub f64_shader: bool,
pub df64_shader: bool,
pub hardware_hint: HardwareHint,
}
pub trait GpuBackend: Send + Sync {
type Buffer: Send + Sync;
fn name(&self) -> &str;
fn has_f64_shaders(&self) -> bool;
fn is_lost(&self) -> bool;
fn capabilities(&self) -> BackendCapabilities {
BackendCapabilities::unknown()
}
fn alloc_buffer(&self, label: &str, size: u64) -> Result<Self::Buffer>;
fn alloc_buffer_init(&self, label: &str, contents: &[u8]) -> Result<Self::Buffer>;
fn alloc_uniform(&self, label: &str, contents: &[u8]) -> Result<Self::Buffer>;
fn upload(&self, buffer: &Self::Buffer, offset: u64, data: &[u8]);
fn download(&self, buffer: &Self::Buffer, size: u64) -> Result<bytes::Bytes>;
fn dispatch_compute(&self, desc: DispatchDescriptor<'_, Self>) -> Result<()>;
fn dispatch_compute_batch(&self, descs: Vec<DispatchDescriptor<'_, Self>>) -> Result<()> {
for desc in descs {
self.dispatch_compute(desc)?;
}
Ok(())
}
fn dispatch_binary(
&self,
_binary: &[u8],
_bindings: Vec<BufferBinding<'_, Self>>,
_workgroups: (u32, u32, u32),
_entry_point: &str,
) -> Result<()> {
Err(crate::error::BarracudaError::Device(
"dispatch_binary: not supported by this backend".into(),
))
}
fn create_backend_buffer_f32(&self, n: usize) -> Result<Self::Buffer> {
self.alloc_buffer("f32_buffer", (n * std::mem::size_of::<f32>()) as u64)
}
fn create_backend_buffer_f64(&self, n: usize) -> Result<Self::Buffer> {
self.alloc_buffer("f64_buffer", (n * std::mem::size_of::<f64>()) as u64)
}
fn create_backend_buffer_u32(&self, n: usize) -> Result<Self::Buffer> {
self.alloc_buffer("u32_buffer", (n * std::mem::size_of::<u32>()) as u64)
}
fn alloc_buffer_f32_init(&self, label: &str, data: &[f32]) -> Result<Self::Buffer> {
self.alloc_buffer_init(label, bytemuck::cast_slice(data))
}
fn alloc_buffer_f64_init(&self, label: &str, data: &[f64]) -> Result<Self::Buffer> {
self.alloc_buffer_init(label, bytemuck::cast_slice(data))
}
fn upload_f32(&self, buffer: &Self::Buffer, data: &[f32]) {
self.upload(buffer, 0, bytemuck::cast_slice(data));
}
fn upload_f64(&self, buffer: &Self::Buffer, data: &[f64]) {
self.upload(buffer, 0, bytemuck::cast_slice(data));
}
fn download_f32(&self, buffer: &Self::Buffer, n: usize) -> Result<Vec<f32>> {
let bytes = self.download(buffer, (n * std::mem::size_of::<f32>()) as u64)?;
Ok(bytemuck::cast_slice(&bytes).to_vec())
}
fn download_f64(&self, buffer: &Self::Buffer, n: usize) -> Result<Vec<f64>> {
let bytes = self.download(buffer, (n * std::mem::size_of::<f64>()) as u64)?;
Ok(bytemuck::cast_slice(&bytes).to_vec())
}
fn download_u32(&self, buffer: &Self::Buffer, n: usize) -> Result<Vec<u32>> {
let bytes = self.download(buffer, (n * std::mem::size_of::<u32>()) as u64)?;
Ok(bytemuck::cast_slice(&bytes).to_vec())
}
fn download_bytes(&self, buffer: &Self::Buffer, size: u64) -> Result<bytes::Bytes> {
self.download(buffer, size)
}
fn alloc_uniform_pod<T: bytemuck::NoUninit>(
&self,
label: &str,
data: &T,
) -> Result<Self::Buffer> {
self.alloc_uniform(label, bytemuck::bytes_of(data))
}
}
impl<B: GpuBackend> GpuBackend for Arc<B> {
type Buffer = B::Buffer;
fn name(&self) -> &str {
(**self).name()
}
fn has_f64_shaders(&self) -> bool {
(**self).has_f64_shaders()
}
fn is_lost(&self) -> bool {
(**self).is_lost()
}
fn capabilities(&self) -> BackendCapabilities {
(**self).capabilities()
}
fn alloc_buffer(&self, label: &str, size: u64) -> Result<Self::Buffer> {
(**self).alloc_buffer(label, size)
}
fn alloc_buffer_init(&self, label: &str, contents: &[u8]) -> Result<Self::Buffer> {
(**self).alloc_buffer_init(label, contents)
}
fn alloc_uniform(&self, label: &str, contents: &[u8]) -> Result<Self::Buffer> {
(**self).alloc_uniform(label, contents)
}
fn upload(&self, buffer: &Self::Buffer, offset: u64, data: &[u8]) {
(**self).upload(buffer, offset, data);
}
fn download(&self, buffer: &Self::Buffer, size: u64) -> Result<bytes::Bytes> {
(**self).download(buffer, size)
}
fn dispatch_compute(&self, desc: DispatchDescriptor<'_, Self>) -> Result<()> {
(**self).dispatch_compute(reborrow_descriptor(desc))
}
fn dispatch_compute_batch(&self, descs: Vec<DispatchDescriptor<'_, Self>>) -> Result<()> {
let reborrowed = descs.into_iter().map(reborrow_descriptor).collect();
(**self).dispatch_compute_batch(reborrowed)
}
fn dispatch_binary(
&self,
binary: &[u8],
bindings: Vec<BufferBinding<'_, Self>>,
workgroups: (u32, u32, u32),
entry_point: &str,
) -> Result<()> {
let reborrowed = bindings
.iter()
.map(|b| BufferBinding {
index: b.index,
buffer: b.buffer,
read_only: b.read_only,
is_uniform: b.is_uniform,
})
.collect();
(**self).dispatch_binary(binary, reborrowed, workgroups, entry_point)
}
}
fn reborrow_descriptor<B: GpuBackend>(
desc: DispatchDescriptor<'_, Arc<B>>,
) -> DispatchDescriptor<'_, B> {
let bindings = desc
.bindings
.iter()
.map(|b| BufferBinding {
index: b.index,
buffer: b.buffer,
read_only: b.read_only,
is_uniform: b.is_uniform,
})
.collect();
DispatchDescriptor {
label: desc.label,
shader_source: desc.shader_source,
entry_point: desc.entry_point,
bindings,
workgroups: desc.workgroups,
f64_shader: desc.f64_shader,
df64_shader: desc.df64_shader,
hardware_hint: desc.hardware_hint,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hardware_hint_default_is_compute() {
assert_eq!(HardwareHint::default(), HardwareHint::Compute);
}
#[test]
fn hardware_hint_equality() {
assert_eq!(HardwareHint::Compute, HardwareHint::Compute);
assert_eq!(HardwareHint::TensorCore, HardwareHint::TensorCore);
assert_ne!(HardwareHint::Compute, HardwareHint::TensorCore);
assert_ne!(HardwareHint::RtCore, HardwareHint::ZBuffer);
}
#[test]
fn hardware_hint_debug_formatting() {
let s = format!("{:?}", HardwareHint::Compute);
assert!(s.contains("Compute"));
let s = format!("{:?}", HardwareHint::TensorCore);
assert!(s.contains("TensorCore"));
}
}