Introduce DynCommandEncoder, implement index & vertex buffer ops on it

This commit is contained in:
Andreas Reich 2024-07-14 23:13:55 +02:00
parent a4d9d38d03
commit 256ec6e447
3 changed files with 55 additions and 2 deletions

View File

@ -0,0 +1,37 @@
use crate::{BufferBinding, CommandEncoder};
use super::DynBuffer;
pub trait DynCommandEncoder {
unsafe fn set_index_buffer<'a>(
&mut self,
binding: BufferBinding<'a, dyn DynBuffer>,
format: wgt::IndexFormat,
);
unsafe fn set_vertex_buffer<'a>(
&mut self,
index: u32,
binding: BufferBinding<'a, dyn DynBuffer>,
);
}
impl<C: CommandEncoder> DynCommandEncoder for C {
unsafe fn set_index_buffer<'a>(
&mut self,
binding: BufferBinding<'a, dyn DynBuffer>,
format: wgt::IndexFormat,
) {
let binding = binding.expect_downcast();
unsafe { self.set_index_buffer(binding, format) };
}
unsafe fn set_vertex_buffer<'a>(
&mut self,
index: u32,
binding: BufferBinding<'a, dyn DynBuffer>,
) {
let binding = binding.expect_downcast();
unsafe { self.set_vertex_buffer(index, binding) };
}
}

View File

@ -1,7 +1,13 @@
mod command;
pub use self::command::DynCommandEncoder;
use std::any::Any;
use wgt::WasmNotSendSync;
use crate::BufferBinding;
/// Base trait for all resources, allows downcasting via [`Any`].
pub trait DynResource: Any + WasmNotSendSync + 'static {
fn as_any(&self) -> &dyn Any;
@ -81,3 +87,13 @@ impl<R: DynResource + ?Sized> DynResourceExt for R {
}
pub trait DynBuffer: DynResource + std::fmt::Debug {}
impl<'a> BufferBinding<'a, dyn DynBuffer> {
pub fn expect_downcast<B: DynBuffer>(self) -> BufferBinding<'a, B> {
BufferBinding {
buffer: self.buffer.expect_downcast_ref(),
offset: self.offset,
size: self.size,
}
}
}

View File

@ -264,8 +264,8 @@ pub mod api {
mod dynamic;
pub use dynamic::DynBuffer;
pub(crate) use dynamic::{impl_dyn_resource, DynResource};
pub use dynamic::{DynBuffer, DynCommandEncoder};
use std::{
borrow::{Borrow, Cow},
@ -392,7 +392,7 @@ pub trait Api: Clone + fmt::Debug + Sized {
type Device: Device<A = Self>;
type Queue: Queue<A = Self>;
type CommandEncoder: CommandEncoder<A = Self>;
type CommandEncoder: DynCommandEncoder + CommandEncoder<A = Self>;
/// This API's command buffer type.
///