Skip to main content

three_rs/core/
indirect_storage_buffer_attribute.rs

1//! `IndirectStorageBufferAttribute` — `src/renderers/common/IndirectStorageBufferAttribute.js`.
2
3use std::rc::Rc;
4
5use crate::nodes::node::BufferId;
6
7/// `new IndirectStorageBufferAttribute( new Uint32Array( n ), itemSize )`: a
8/// `Uint32Array` that lives on the GPU as one buffer usable both as storage
9/// (a compute kernel writes it through `storage( attribute, struct, count )`)
10/// and as the argument buffer of `drawIndirect` / `drawIndexedIndirect` /
11/// `dispatchWorkgroupsIndirect`.
12///
13/// A handle: clones are the same attribute, and the renderer keys the one GPU
14/// buffer on [`id`](Self::id), which is also the id every storage node built
15/// over it carries — so a kernel writing it and a draw reading it meet on the
16/// same buffer, as they do in three through `backend.get( attribute )`.
17///
18/// The array is the buffer's initial contents only. It is uploaded once, when
19/// the GPU buffer is first made; three.js never reads it back either, and a
20/// kernel's writes are visible through `Renderer::read_indirect_buffer`.
21#[derive(Clone, Debug)]
22pub struct IndirectStorageBufferAttribute {
23    id: BufferId,
24    array: Rc<Vec<u32>>,
25    /// `BufferAttribute.itemSize`.
26    pub item_size: usize,
27}
28
29impl IndirectStorageBufferAttribute {
30    /// `new IndirectStorageBufferAttribute( array, itemSize )`.
31    pub fn new(array: Vec<u32>, item_size: usize) -> Self {
32        Self {
33            id: BufferId::next(),
34            array: Rc::new(array),
35            item_size,
36        }
37    }
38
39    /// The identity the renderer's GPU buffer is keyed on.
40    pub fn id(&self) -> BufferId {
41        self.id
42    }
43
44    /// `attribute.array` — the initial contents.
45    pub fn array(&self) -> Rc<Vec<u32>> {
46        self.array.clone()
47    }
48
49    /// `BufferAttribute.count` — `array.length / itemSize`.
50    pub fn count(&self) -> usize {
51        self.array.len() / self.item_size
52    }
53}