bitset.txt (4354B)
1 =============================================================================== 2 bitset 3 =============================================================================== 4 5 Example: 6 7 Each flag owns a different bit position, so they never overlap. That lets you 8 store any subset of them in one number: 9 10 INDEX = 1 << 4 = 00_0001_0000 = 16 11 VERTEX = 1 << 5 = 00_0010_0000 = 32 12 UNIFORM = 1 << 6 = 00_0100_0000 = 64 13 STORAGE = 1 << 7 = 00_1000_0000 = 128 14 INDIRECT = 1 << 8 = 01_0000_0000 = 256 15 QUERY_RESOLVE = 1 << 9 = 10_0000_0000 = 512 16 17 Each of those constants is one switch in a row of switches, and a buffer's usage 18 value records which switches are on. 19 20 1. Each usage gets its own bit. INDEX is switch 4, VERTEX is switch 5, UNIFORM 21 is switch 6, and so on. 1 << n means "a number with only switch n turned on." 22 23 2. Combining usages means turning on several switches. If you create a buffer 24 with VERTEX | INDEX | UNIFORM, you get: 25 26 00_0001_0000 INDEX 27 00_0010_0000 VERTEX 28 00_0100_0000 UNIFORM 29 ──────────── OR them together 30 00_0111_0000 = 112 31 32 The single number 112 means "this buffer may be used as an index buffer, 33 a vertex buffer, and a uniform buffer." 34 35 36 37 wgpu-types/src/buffer.rs 38 39 bitflags::bitflags! { 40 /// Different ways that you can use a buffer. 41 /// 42 /// The usages determine what kind of memory the buffer is allocated from and what 43 /// actions the buffer can partake in. 44 /// 45 /// Specifying only usages the application will actually perform may increase performance. 46 /// Additionally, on the WebGL backend, there are restrictions on [`BufferUsages::INDEX`]; 47 /// see [`DownlevelFlags::UNRESTRICTED_INDEX_BUFFER`] for more information. 48 /// 49 /// Corresponds to [WebGPU `GPUBufferUsageFlags`]( 50 /// https://gpuweb.github.io/gpuweb/#typedefdef-gpubufferusageflags). 51 #[repr(transparent)] 52 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] 53 #[cfg_attr(feature = "serde", serde(transparent))] 54 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] 55 pub struct BufferUsages: u32 { 56 /// Allow a buffer to be mapped for reading using [`Buffer::map_async`] + [`Buffer::get_mapped_range`]. 57 /// This does not include creating a buffer with [`BufferDescriptor::mapped_at_creation`] set. 58 /// 59 /// If [`Features::MAPPABLE_PRIMARY_BUFFERS`] isn't enabled, the only other usage a buffer 60 /// may have is COPY_DST. 61 const MAP_READ = 1 << 0; 62 /// Allow a buffer to be mapped for writing using [`Buffer::map_async`] + [`Buffer::get_mapped_range_mut`]. 63 /// This does not include creating a buffer with [`BufferDescriptor::mapped_at_creation`] set. 64 /// 65 /// If [`Features::MAPPABLE_PRIMARY_BUFFERS`] feature isn't enabled, the only other usage a buffer 66 /// may have is COPY_SRC. 67 const MAP_WRITE = 1 << 1; 68 /// Allow a buffer to be the source buffer for a [`CommandEncoder::copy_buffer_to_buffer`] or [`CommandEncoder::copy_buffer_to_texture`] 69 /// operation. 70 const COPY_SRC = 1 << 2; 71 /// Allow a buffer to be the destination buffer for a [`CommandEncoder::copy_buffer_to_buffer`], [`CommandEncoder::copy_texture_to_buffer`], 72 /// [`CommandEncoder::clear_buffer`] or [`Queue::write_buffer`] operation. 73 const COPY_DST = 1 << 3; 74 /// Allow a buffer to be the index buffer in a draw operation. 75 const INDEX = 1 << 4; 76 /// Allow a buffer to be the vertex buffer in a draw operation. 77 const VERTEX = 1 << 5; 78 /// Allow a buffer to be a [`BufferBindingType::Uniform`] inside a bind group. 79 const UNIFORM = 1 << 6; 80 /// Allow a buffer to be a [`BufferBindingType::Storage`] inside a bind group. 81 const STORAGE = 1 << 7; 82 /// Allow a buffer to be the indirect buffer in an indirect draw call. 83 const INDIRECT = 1 << 8; 84 /// Allow a buffer to be the destination buffer for a [`CommandEncoder::resolve_query_set`] operation. 85 const QUERY_RESOLVE = 1 << 9; 86 /// Allows a buffer to be used as input for a bottom level acceleration structure build 87 const BLAS_INPUT = 1 << 10; 88 /// Allows a buffer to be used as input for a top level acceleration structure build 89 const TLAS_INPUT = 1 << 11; 90 } 91 } 92