Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
node_modules
.pnp
.pnp.js
.pnpm-store

# testing
coverage
Expand Down
10 changes: 10 additions & 0 deletions apps/typegpu-docs/src/content/docs/apis/buffers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ const buffer = root.createBuffer(d.u32).$usage('uniform', 'storage');
```
:::

For type annotations, you can use named aliases instead of intersecting with usage flags manually:
`TgpuUniformBuffer`, `TgpuStorageBuffer`, `TgpuVertexBuffer`, and `TgpuIndexBuffer`.

```ts twoslash
import { tgpu, d, type TgpuUniformBuffer } from 'typegpu';
const root = await tgpu.init();
// ---cut---
const buffer: TgpuUniformBuffer<d.U32> = root.createBuffer(d.u32).$usage('uniform');
```

### Additional flags

It is also possible to add any of the `GPUBufferUsage` flags to a typed buffer object, using the `.$addFlags` method.
Expand Down
5 changes: 5 additions & 0 deletions packages/typegpu/src/core/buffer/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ export interface IndirectFlag {
*/
export type Vertex = VertexFlag;

export type TgpuStorageBuffer<T extends BaseData> = TgpuBuffer<T> & StorageFlag;
export type TgpuUniformBuffer<T extends BaseData> = TgpuBuffer<T> & UniformFlag;
export type TgpuVertexBuffer<T extends BaseData> = TgpuBuffer<T> & VertexFlag;
export type TgpuIndexBuffer<T extends BaseData> = TgpuBuffer<T> & IndexFlag;

type UsageLiteral = 'uniform' | 'storage' | 'vertex' | 'index' | 'indirect';

type LiteralToUsageType<T extends UsageLiteral> = T extends 'uniform'
Expand Down
11 changes: 10 additions & 1 deletion packages/typegpu/src/indexNamedExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export type {
ValidUsagesFor,
Vertex,
VertexFlag,
TgpuStorageBuffer,
TgpuUniformBuffer,
TgpuVertexBuffer,
TgpuIndexBuffer,
BufferWriteOptions,
BufferInitCallback,
BufferInitialData,
Expand All @@ -81,7 +85,12 @@ export type {
TgpuBufferReadonly,
TgpuBufferUniform,
} from './core/buffer/bufferUsage.ts';
export type { TgpuMutable, TgpuReadonly, TgpuUniform } from './core/buffer/bufferBinding.ts';
export type {
TgpuBufferBinding,
TgpuMutable,
TgpuReadonly,
TgpuUniform,
} from './core/buffer/bufferBinding.ts';
export type {
Eventual,
TgpuAccessor,
Expand Down
23 changes: 22 additions & 1 deletion packages/typegpu/tests/buffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { attest } from '@ark/attest';
import { describe, expect, expectTypeOf, vi } from 'vitest';
import { d, common } from 'typegpu';
import { sizeOf } from 'typegpu/data';
import type { ValidateBufferSchema, ValidUsagesFor } from 'typegpu';
import type {
ValidateBufferSchema,
ValidUsagesFor,
TgpuUniformBuffer,
TgpuStorageBuffer,
TgpuVertexBuffer,
TgpuIndexBuffer,
} from 'typegpu';
import { it } from 'typegpu-testing-utility';

function toUint8Array(...arrays: Array<ArrayBufferView>): Uint8Array {
Expand Down Expand Up @@ -872,6 +879,20 @@ describe('TgpuBuffer', () => {
]
>();
});

it('.$usage is assignable to named Tgpu*Buffer aliases', ({ root }) => {
const uniformBuf = root.createBuffer(d.u32).$usage('uniform');
expectTypeOf(uniformBuf).toExtend<TgpuUniformBuffer<d.U32>>();

const storageBuf = root.createBuffer(d.u32).$usage('storage');
expectTypeOf(storageBuf).toExtend<TgpuStorageBuffer<d.U32>>();

const vertexBuf = root.createBuffer(d.u32).$usage('vertex');
expectTypeOf(vertexBuf).toExtend<TgpuVertexBuffer<d.U32>>();

const indexBuf = root.createBuffer(d.arrayOf(d.u16, 32)).$usage('index');
expectTypeOf(indexBuf).toExtend<TgpuIndexBuffer<d.WgslArray<d.U16>>>();
});
});

describe('TgpuBuffer (InferInput)', () => {
Expand Down
Loading