use crate::buffer::GpuBufferHandle;
use crate::pipeline::compound::CompoundResource;
use crate::pipeline::WgpuPipeline;
use smallvec::SmallVec;
use vyre_driver::{BackendError, DispatchConfig};
#[derive(Clone)]
pub enum GpuResource {
Borrowed(Vec<u8>),
Resident(GpuBufferHandle),
}
impl From<Vec<u8>> for GpuResource {
fn from(bytes: Vec<u8>) -> Self {
Self::Borrowed(bytes)
}
}
impl From<GpuBufferHandle> for GpuResource {
fn from(handle: GpuBufferHandle) -> Self {
Self::Resident(handle)
}
}
#[derive(Default)]
pub struct GpuDispatchGraph {
ops: SmallVec<[GraphOp; 8]>,
}
#[derive(Clone)]
struct GraphOp {
pipeline: WgpuPipeline,
input: GpuResource,
}
impl GpuDispatchGraph {
#[must_use]
pub fn new() -> Self {
Self {
ops: SmallVec::new(),
}
}
pub fn push(&mut self, pipeline: WgpuPipeline, input: impl Into<GpuResource>) {
self.ops.push(GraphOp {
pipeline,
input: input.into(),
});
}
#[must_use]
pub fn len(&self) -> usize {
self.ops.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.ops.is_empty()
}
pub fn dispatch(&self, config: &DispatchConfig) -> Result<Vec<Vec<Vec<u8>>>, BackendError> {
if self.ops.is_empty() {
return Ok(Vec::new());
}
let mut internal_requests: SmallVec<[(&WgpuPipeline, CompoundResource<'_>); 8]> =
SmallVec::with_capacity(self.ops.len());
for op in &self.ops {
let res = match &op.input {
GpuResource::Borrowed(bytes) => CompoundResource::Borrowed(bytes),
GpuResource::Resident(handle) => CompoundResource::Resident(handle.id()),
};
internal_requests.push((&op.pipeline, res));
}
WgpuPipeline::dispatch_compound_borrowed(&internal_requests, config)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LaunchAccounting {
pub sequential_submissions: usize,
pub graph_submissions: usize,
}
impl LaunchAccounting {
#[must_use]
pub fn reduction_factor(self) -> usize {
self.sequential_submissions / self.graph_submissions.max(1)
}
}
#[must_use]
pub fn launch_accounting(op_count: usize) -> LaunchAccounting {
LaunchAccounting {
sequential_submissions: op_count,
graph_submissions: usize::from(op_count > 0),
}
}