33//! This module provides a thin wrapper over `wgpu::Buffer` plus a small
44//! builder that handles common initialization patterns and keeps label and
55//! usage metadata for debugging/inspection.
6-
7- use crate :: wgpu:: {
8- types as wgpu,
9- types:: util:: DeviceExt ,
6+ use wgpu:: {
7+ self ,
8+ util:: DeviceExt ,
109} ;
1110
12- #[ derive( Clone , Copy , Debug ) ]
11+ use crate :: wgpu:: gpu:: Gpu ;
12+
13+ /// Index format for indexed drawing.
14+ #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
15+ pub enum IndexFormat {
16+ Uint16 ,
17+ Uint32 ,
18+ }
19+
20+ impl IndexFormat {
21+ pub ( crate ) fn to_wgpu ( self ) -> wgpu:: IndexFormat {
22+ return match self {
23+ IndexFormat :: Uint16 => wgpu:: IndexFormat :: Uint16 ,
24+ IndexFormat :: Uint32 => wgpu:: IndexFormat :: Uint32 ,
25+ } ;
26+ }
27+ }
28+
1329/// Platform buffer usage flags.
30+ #[ derive( Clone , Copy , Debug ) ]
1431pub struct Usage ( pub ( crate ) wgpu:: BufferUsages ) ;
1532
1633impl Usage {
@@ -43,8 +60,8 @@ impl Default for Usage {
4360 }
4461}
4562
46- #[ derive( Debug ) ]
4763/// Wrapper around `wgpu::Buffer` with metadata.
64+ #[ derive( Debug ) ]
4865pub struct Buffer {
4966 pub ( crate ) raw : wgpu:: Buffer ,
5067 pub ( crate ) label : Option < String > ,
@@ -54,7 +71,7 @@ pub struct Buffer {
5471
5572impl Buffer {
5673 /// Borrow the underlying `wgpu::Buffer`.
57- pub fn raw ( & self ) -> & wgpu:: Buffer {
74+ pub ( crate ) fn raw ( & self ) -> & wgpu:: Buffer {
5875 return & self . raw ;
5976 }
6077
@@ -64,18 +81,23 @@ impl Buffer {
6481 }
6582
6683 /// Size in bytes at creation time.
67- pub fn size ( & self ) -> wgpu :: BufferAddress {
84+ pub fn size ( & self ) -> u64 {
6885 return self . size ;
6986 }
7087
7188 /// Usage flags used to create the buffer.
72- pub fn usage ( & self ) -> wgpu:: BufferUsages {
73- return self . usage ;
89+ pub fn usage ( & self ) -> Usage {
90+ return Usage ( self . usage ) ;
91+ }
92+
93+ /// Write raw bytes into the buffer at the given offset.
94+ pub fn write_bytes ( & self , gpu : & Gpu , offset : u64 , data : & [ u8 ] ) {
95+ gpu. queue ( ) . write_buffer ( & self . raw , offset, data) ;
7496 }
7597}
7698
77- #[ derive( Default ) ]
7899/// Builder for creating a `Buffer` with optional initial contents.
100+ #[ derive( Default ) ]
79101pub struct BufferBuilder {
80102 label : Option < String > ,
81103 size : usize ,
@@ -119,7 +141,7 @@ impl BufferBuilder {
119141 }
120142
121143 /// Create a buffer initialized with `contents`.
122- pub fn build_init ( self , device : & wgpu :: Device , contents : & [ u8 ] ) -> Buffer {
144+ pub fn build_init ( self , gpu : & Gpu , contents : & [ u8 ] ) -> Buffer {
123145 let size = if self . size == 0 {
124146 contents. len ( )
125147 } else {
@@ -131,11 +153,14 @@ impl BufferBuilder {
131153 usage |= wgpu:: BufferUsages :: COPY_DST ;
132154 }
133155
134- let raw = device. create_buffer_init ( & wgpu:: util:: BufferInitDescriptor {
135- label : self . label . as_deref ( ) ,
136- contents,
137- usage,
138- } ) ;
156+ let raw =
157+ gpu
158+ . device ( )
159+ . create_buffer_init ( & wgpu:: util:: BufferInitDescriptor {
160+ label : self . label . as_deref ( ) ,
161+ contents,
162+ usage,
163+ } ) ;
139164
140165 return Buffer {
141166 raw,
0 commit comments