Skip to content

Commit 66093ec

Browse files
authored
docs: Add info about $addFlags method to buffer docs (#720)
1 parent 733d29c commit 66093ec

File tree

1 file changed

+27
-11
lines changed
  • apps/typegpu-docs/src/content/docs/fundamentals

1 file changed

+27
-11
lines changed

apps/typegpu-docs/src/content/docs/fundamentals/buffers.mdx

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ const buffer = root
3838
d.arrayOf(Particle, 100),
3939
// Initial value
4040
Array.from({ length: 100 }).map(() => ({
41-
position: vec3f(Math.random(), 2, Math.random()),
42-
velocity: vec3f(0, 9.8, 0),
41+
position: d.vec3f(Math.random(), 2, Math.random()),
42+
velocity: d.vec3f(0, 9.8, 0),
4343
health: 100,
4444
})),
4545
);
4646
// ^? TgpuBuffer<TgpuArray<TgpuStruct<{
47-
// position: Vec3f,
48-
// velocity: Vec3f,
49-
// health: F32,
47+
// position: d.Vec3f,
48+
// velocity: d.Vec3f,
49+
// health: d.F32,
5050
// }>>>
5151

5252
// -
@@ -57,7 +57,7 @@ const buffer = root
5757

5858
// Reading from the buffer
5959
const value = await buffer.read();
60-
// ^? { position: vec3f, velocity: vec3f, health: number }[]
60+
// ^? { position: d.vec3f, velocity: d.vec3f, health: number }[]
6161

6262
// Using the value
6363
console.log(value);
@@ -102,17 +102,33 @@ const buffer = root.createBuffer(d.u32)
102102

103103
```
104104

105-
import { Code } from '@astrojs/starlight/components';
106-
107105
:::note
108106
Along with passing the appropriate flags to WebGPU, the methods will also embed type information into the buffer.
109107
```ts
110-
// TgpuBuffer<U32> & Uniform & Storage
111-
const buffer = root.createBuffer(u32)
108+
// TgpuBuffer<d.U32> & Uniform & Storage
109+
const buffer = root.createBuffer(d.u32)
112110
.$usage('uniform', 'storage');
113111
```
114112
:::
115113

114+
### Additional flags
115+
116+
It is also possible to add any of the `GPUBufferUsage` flags to a typed buffer object, using the `.$addFlags` method.
117+
Though it shouldn't be necessary in most scenarios as majority of the flags are handled automatically by the library
118+
or indirectly through the `.$usage` method.
119+
120+
```ts
121+
buffer.$addFlags(GPUBufferUsage.QUERY_RESOLVE);
122+
```
123+
124+
:::note
125+
Every typed buffer created without providing existing GPU buffer has `COPY_DST` and `COPY_SRC` flags included by default.
126+
However once the `MAP_READ` flag is provided, the only other flag set is `COPY_DST`. Similarly when setting `MAP_WRITE` it is paired with `COPY_SRC`.
127+
:::
128+
129+
Flags can only be added this way if the typed buffer was not created with an existing GPU buffer.
130+
If it was, then all flags need to be provided to the existing buffer when constructing it.
131+
116132
### Initial value
117133

118134
You can also pass an initial value to the `root.createBuffer` function.
@@ -159,7 +175,7 @@ const Particle = d.struct({
159175

160176
const particleBuffer = root.createBuffer(Particle);
161177

162-
// .write(data: { position: vec2f, health: number })
178+
// .write(data: { position: d.vec2f, health: number })
163179
particleBuffer.write({
164180
position: vec2f(1.0, 2.0),
165181
health: 100,

0 commit comments

Comments
 (0)