From e924aa1a058045835a0be34fe27360f95c92978a Mon Sep 17 00:00:00 2001 From: Samuel Hurel Date: Fri, 7 Jan 2022 16:15:24 +0100 Subject: [PATCH] Add util::indirect helper structs --- wgpu/src/lib.rs | 113 +++----------------------------------- wgpu/src/util/encoder.rs | 25 +-------- wgpu/src/util/indirect.rs | 81 +++++++++++++++++++++++++++ wgpu/src/util/mod.rs | 2 + 4 files changed, 94 insertions(+), 127 deletions(-) create mode 100644 wgpu/src/util/indirect.rs diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index a0eb7abe9..52db624ea 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -2605,18 +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` must conform to [`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); } @@ -2627,19 +2616,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` must conform to [`DrawIndexedIndirect`](crate::util::DrawIndexedIndirect). pub fn draw_indexed_indirect( &mut self, indirect_buffer: &'a Buffer, @@ -2664,17 +2641,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` must conform to [`DrawIndirect`](crate::util::DrawIndirect). /// /// These draw structures are expected to be tightly packed. pub fn multi_draw_indirect( @@ -2693,18 +2660,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. - /// 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` must conform to [`DrawIndexedIndirect`](crate::util::DrawIndexedIndirect). /// /// These draw structures are expected to be tightly packed. pub fn multi_draw_indexed_indirect( @@ -2728,17 +2684,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` must conform to [`DrawIndirect`](crate::util::DrawIndirect). /// /// These draw structures are expected to be tightly packed. /// @@ -2776,18 +2722,8 @@ 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. - /// 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` must conform to [`DrawIndexedIndirect`](crate::util::DrawIndexedIndirect). /// /// These draw structures are expected to be tightly packed. /// @@ -2938,17 +2874,7 @@ impl<'a> ComputePass<'a> { /// Dispatches compute work operations, based on the contents of the `indirect_buffer`. /// - /// The structure expected in `indirect_buffer` is the following: - /// - /// ```rust - /// // x, y and z denote the number of work groups to dispatch in each dimension. - /// #[repr(C)] - /// struct DispatchIndirect { - /// x: u32, - /// y: u32, - /// z: u32, - /// } - /// ``` + /// The structure expected in `indirect_buffer` must conform to [`DispatchIndirect`](crate::util::DispatchIndirect). pub fn dispatch_indirect( &mut self, indirect_buffer: &'a Buffer, @@ -3094,17 +3020,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` must conform to [`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); } @@ -3115,18 +3031,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` must conform to [`DrawIndexedIndirect`](crate::util::DrawIndexedIndirect). pub fn draw_indexed_indirect( &mut self, indirect_buffer: &'a Buffer, diff --git a/wgpu/src/util/encoder.rs b/wgpu/src/util/encoder.rs index 37b38f4e1..208c8c0a5 100644 --- a/wgpu/src/util/encoder.rs +++ b/wgpu/src/util/encoder.rs @@ -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` must conform to [`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, @@ -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` must conform to [`DrawIndexedIndirect`](crate::util::DrawIndexedIndirect). fn draw_indexed_indirect( &mut self, indirect_buffer: &'a Buffer, diff --git a/wgpu/src/util/indirect.rs b/wgpu/src/util/indirect.rs new file mode 100644 index 000000000..49013adcb --- /dev/null +++ b/wgpu/src/util/indirect.rs @@ -0,0 +1,81 @@ +/// 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::(), + )) + } + } +} + +/// 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::(), + )) + } + } +} + +/// The structure expected in `indirect_buffer` for [`ComputePass::dispatch_indirect`](crate::ComputePass::dispatch_indirect). +/// +/// x, y and z denote the number of work groups to dispatch in each dimension. +#[repr(C)] +#[derive(Copy, Clone, Debug, Default)] +pub struct DispatchIndirect { + /// The number of work groups in X dimension. + pub x: u32, + /// The number of work groups in Y dimension. + pub y: u32, + /// The number of work groups in Z dimension. + pub z: u32, +} + +impl DispatchIndirect { + /// 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::(), + )) + } + } +} diff --git a/wgpu/src/util/mod.rs b/wgpu/src/util/mod.rs index d50ee9796..0a8820a30 100644 --- a/wgpu/src/util/mod.rs +++ b/wgpu/src/util/mod.rs @@ -3,6 +3,7 @@ mod belt; mod device; mod encoder; +mod indirect; mod init; use std::future::Future; @@ -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.