Skip to content

Commit

Permalink
Add util::DrawIndirect and util::DrawIndexedIndirect structs
Browse files Browse the repository at this point in the history
  • Loading branch information
IcanDivideBy0 committed Jan 7, 2022
1 parent 10a07e4 commit 5c335ca
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 91 deletions.
74 changes: 6 additions & 68 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2605,17 +2605,7 @@ impl<'a> RenderPass<'a> {
///
/// The active vertex buffers can be set with [`RenderPass::set_vertex_buffer`].
///
/// The structure expected in `indirect_buffer` is the following:
///
/// ```rust
/// #[repr(C)]
/// struct DrawIndirect {
/// vertex_count: u32, // The number of vertices to draw.
/// instance_count: u32, // The number of instances to draw.
/// first_vertex: u32, // The Index of the first vertex to draw.
/// first_instance: u32, // The instance ID of the first instance to draw.
/// // has to be 0, unless [`Features::INDIRECT_FIRST_INSTANCE`] is enabled.
/// }
/// The structure expected in `indirect_buffer` is a [`DrawIndirect`](crate::util::DrawIndirect).
/// ```
pub fn draw_indirect(&mut self, indirect_buffer: &'a Buffer, indirect_offset: BufferAddress) {
self.id.draw_indirect(&indirect_buffer.id, indirect_offset);
Expand All @@ -2627,18 +2617,7 @@ impl<'a> RenderPass<'a> {
/// The active index buffer can be set with [`RenderPass::set_index_buffer`], while the active
/// vertex buffers can be set with [`RenderPass::set_vertex_buffer`].
///
/// The structure expected in `indirect_buffer` is the following:
///
/// ```rust
/// #[repr(C)]
/// struct DrawIndexedIndirect {
/// vertex_count: u32, // The number of vertices to draw.
/// instance_count: u32, // The number of instances to draw.
/// first_index: u32, // The base index within the index buffer.
/// vertex_offset: i32, // The value added to the vertex index before indexing into the vertex buffer.
/// first_instance: u32, // The instance ID of the first instance to draw.
/// // has to be 0, unless [`Features::INDIRECT_FIRST_INSTANCE`] is enabled.
/// }
/// The structure expected in `indirect_buffer` is a [`DrawIndexedIndirect`](crate::util::DrawIndexedIndirect).
/// ```
pub fn draw_indexed_indirect(
&mut self,
Expand All @@ -2664,17 +2643,7 @@ impl<'a> RenderPass<'a> {
///
/// The active vertex buffers can be set with [`RenderPass::set_vertex_buffer`].
///
/// The structure expected in `indirect_buffer` is the following:
///
/// ```rust
/// #[repr(C)]
/// struct DrawIndirect {
/// vertex_count: u32, // The number of vertices to draw.
/// instance_count: u32, // The number of instances to draw.
/// base_vertex: u32, // The Index of the first vertex to draw.
/// base_instance: u32, // The instance ID of the first instance to draw.
/// }
/// ```
/// The structure expected in `indirect_buffer` is a [`DrawIndirect`](crate::util::DrawIndirect).
///
/// These draw structures are expected to be tightly packed.
pub fn multi_draw_indirect(
Expand Down Expand Up @@ -2728,17 +2697,7 @@ impl<'a> RenderPass<'a> {
///
/// The active vertex buffers can be set with [`RenderPass::set_vertex_buffer`].
///
/// The structure expected in `indirect_buffer` is the following:
///
/// ```rust
/// #[repr(C)]
/// struct DrawIndirect {
/// vertex_count: u32, // The number of vertices to draw.
/// instance_count: u32, // The number of instances to draw.
/// base_vertex: u32, // The Index of the first vertex to draw.
/// base_instance: u32, // The instance ID of the first instance to draw.
/// }
/// ```
/// The structure expected in `indirect_buffer` is a [`DrawIndirect`](crate::util::DrawIndirect).
///
/// These draw structures are expected to be tightly packed.
///
Expand Down Expand Up @@ -3094,17 +3053,7 @@ impl<'a> RenderBundleEncoder<'a> {
///
/// The active vertex buffers can be set with [`RenderBundleEncoder::set_vertex_buffer`].
///
/// The structure expected in `indirect_buffer` is the following:
///
/// ```rust
/// #[repr(C)]
/// struct DrawIndirect {
/// vertex_count: u32, // The number of vertices to draw.
/// instance_count: u32, // The number of instances to draw.
/// base_vertex: u32, // The Index of the first vertex to draw.
/// base_instance: u32, // The instance ID of the first instance to draw.
/// }
/// ```
/// The structure expected in `indirect_buffer` is a [`DrawIndirect`](crate::util::DrawIndirect).
pub fn draw_indirect(&mut self, indirect_buffer: &'a Buffer, indirect_offset: BufferAddress) {
self.id.draw_indirect(&indirect_buffer.id, indirect_offset);
}
Expand All @@ -3115,18 +3064,7 @@ impl<'a> RenderBundleEncoder<'a> {
/// The active index buffer can be set with [`RenderBundleEncoder::set_index_buffer`], while the active
/// vertex buffers can be set with [`RenderBundleEncoder::set_vertex_buffer`].
///
/// The structure expected in `indirect_buffer` is the following:
///
/// ```rust
/// #[repr(C)]
/// struct DrawIndexedIndirect {
/// vertex_count: u32, // The number of vertices to draw.
/// instance_count: u32, // The number of instances to draw.
/// base_index: u32, // The base index within the index buffer.
/// vertex_offset: i32, // The value added to the vertex index before indexing into the vertex buffer.
/// base_instance: u32, // The instance ID of the first instance to draw.
/// }
/// ```
/// The structure expected in `indirect_buffer` is a [`DrawIndexedIndirect`](crate::util::DrawIndexedIndirect).
pub fn draw_indexed_indirect(
&mut self,
indirect_buffer: &'a Buffer,
Expand Down
25 changes: 2 additions & 23 deletions wgpu/src/util/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,7 @@ pub trait RenderEncoder<'a> {
///
/// The active vertex buffers can be set with [`RenderEncoder::set_vertex_buffer`].
///
/// The structure expected in `indirect_buffer` is the following:
///
/// ```rust
/// #[repr(C)]
/// struct DrawIndirect {
/// vertex_count: u32, // The number of vertices to draw.
/// instance_count: u32, // The number of instances to draw.
/// base_vertex: u32, // The Index of the first vertex to draw.
/// base_instance: u32, // The instance ID of the first instance to draw.
/// }
/// ```
/// The structure expected in `indirect_buffer` is a [`DrawIndirect`](crate::util::DrawIndirect).
fn draw_indirect(&mut self, indirect_buffer: &'a Buffer, indirect_offset: BufferAddress);

/// Draws indexed primitives using the active index buffer and the active vertex buffers,
Expand All @@ -69,18 +59,7 @@ pub trait RenderEncoder<'a> {
/// The active index buffer can be set with [`RenderEncoder::set_index_buffer`], while the active
/// vertex buffers can be set with [`RenderEncoder::set_vertex_buffer`].
///
/// The structure expected in `indirect_buffer` is the following:
///
/// ```rust
/// #[repr(C)]
/// struct DrawIndexedIndirect {
/// vertex_count: u32, // The number of vertices to draw.
/// instance_count: u32, // The number of instances to draw.
/// base_index: u32, // The base index within the index buffer.
/// vertex_offset: i32, // The value added to the vertex index before indexing into the vertex buffer.
/// base_instance: u32, // The instance ID of the first instance to draw.
/// }
/// ```
/// The structure expected in `indirect_buffer` is a [`DrawIndexedIndirect`](crate::util::DrawIndexedIndirect).
fn draw_indexed_indirect(
&mut self,
indirect_buffer: &'a Buffer,
Expand Down
55 changes: 55 additions & 0 deletions wgpu/src/util/indirect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/// The structure expected in `indirect_buffer` for [`RenderEncoder::draw_indirect`](crate::util::RenderEncoder::draw_indirect).
#[repr(C)]
#[derive(Copy, Clone, Debug, Default)]
pub struct DrawIndirect {
/// The number of vertices to draw.
pub vertex_count: u32,
/// The number of instances to draw.
pub instance_count: u32,
/// The Index of the first vertex to draw.
pub base_vertex: u32,
/// The instance ID of the first instance to draw.
/// Has to be 0, unless [`Features::INDIRECT_FIRST_INSTANCE`](crate::Features::INDIRECT_FIRST_INSTANCE) is enabled.
pub base_instance: u32,
}

impl DrawIndirect {
/// Returns the bytes representation of the struct, ready to be written in a [`Buffer`](crate::Buffer).
pub fn as_bytes(&self) -> &[u8] {
unsafe {
std::mem::transmute(std::slice::from_raw_parts(
self as *const _ as *const u8,
std::mem::size_of::<Self>(),
))
}
}
}

/// The structure expected in `indirect_buffer` for [`RenderEncoder::draw_indexed_indirect`](crate::util::RenderEncoder::draw_indexed_indirect).
#[repr(C)]
#[derive(Copy, Clone, Debug, Default)]
pub struct DrawIndexedIndirect {
/// The number of vertices to draw.
pub vertex_count: u32,
/// The number of instances to draw.
pub instance_count: u32,
/// The base index within the index buffer.
pub base_index: u32,
/// The value added to the vertex index before indexing into the vertex buffer.
pub vertex_offset: i32,
/// The instance ID of the first instance to draw.
/// Has to be 0, unless [`Features::INDIRECT_FIRST_INSTANCE`](crate::Features::INDIRECT_FIRST_INSTANCE) is enabled.
pub base_instance: u32,
}

impl DrawIndexedIndirect {
/// Returns the bytes representation of the struct, ready to be written in a [`Buffer`](crate::Buffer).
pub fn as_bytes(&self) -> &[u8] {
unsafe {
std::mem::transmute(std::slice::from_raw_parts(
self as *const _ as *const u8,
std::mem::size_of::<Self>(),
))
}
}
}
2 changes: 2 additions & 0 deletions wgpu/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod belt;
mod device;
mod encoder;
mod indirect;
mod init;

use std::future::Future;
Expand All @@ -15,6 +16,7 @@ use std::{
pub use belt::StagingBelt;
pub use device::{BufferInitDescriptor, DeviceExt};
pub use encoder::RenderEncoder;
pub use indirect::*;
pub use init::*;

/// Treat the given byte slice as a SPIR-V module.
Expand Down

0 comments on commit 5c335ca

Please sign in to comment.