From fbbde4957a601e06c667cc2a420ae81507d91fc9 Mon Sep 17 00:00:00 2001 From: Michal Date: Wed, 5 Aug 2026 11:50:50 +0200 Subject: [PATCH 1/3] feat: Named type aliases for usage-flagged TgpuBuffer --- packages/typegpu/src/core/buffer/buffer.ts | 5 +++++ packages/typegpu/src/indexNamedExports.ts | 11 ++++++++++- packages/typegpu/tests/buffer.test.ts | 23 +++++++++++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/packages/typegpu/src/core/buffer/buffer.ts b/packages/typegpu/src/core/buffer/buffer.ts index cab7ab00af..83639d8932 100644 --- a/packages/typegpu/src/core/buffer/buffer.ts +++ b/packages/typegpu/src/core/buffer/buffer.ts @@ -63,6 +63,11 @@ export interface IndirectFlag { */ export type Vertex = VertexFlag; +export type TgpuStorageBuffer = TgpuBuffer & StorageFlag; +export type TgpuUniformBuffer = TgpuBuffer & UniformFlag; +export type TgpuVertexBuffer = TgpuBuffer & VertexFlag; +export type TgpuIndexBuffer = TgpuBuffer & IndexFlag; + type UsageLiteral = 'uniform' | 'storage' | 'vertex' | 'index' | 'indirect'; type LiteralToUsageType = T extends 'uniform' diff --git a/packages/typegpu/src/indexNamedExports.ts b/packages/typegpu/src/indexNamedExports.ts index 809af0e2bd..8c116af913 100644 --- a/packages/typegpu/src/indexNamedExports.ts +++ b/packages/typegpu/src/indexNamedExports.ts @@ -72,6 +72,10 @@ export type { ValidUsagesFor, Vertex, VertexFlag, + TgpuStorageBuffer, + TgpuUniformBuffer, + TgpuVertexBuffer, + TgpuIndexBuffer, BufferWriteOptions, BufferInitCallback, BufferInitialData, @@ -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, diff --git a/packages/typegpu/tests/buffer.test.ts b/packages/typegpu/tests/buffer.test.ts index 3473bb79ae..6935d92f8f 100644 --- a/packages/typegpu/tests/buffer.test.ts +++ b/packages/typegpu/tests/buffer.test.ts @@ -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): Uint8Array { @@ -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>(); + + const storageBuf = root.createBuffer(d.u32).$usage('storage'); + expectTypeOf(storageBuf).toExtend>(); + + const vertexBuf = root.createBuffer(d.u32).$usage('vertex'); + expectTypeOf(vertexBuf).toExtend>(); + + const indexBuf = root.createBuffer(d.arrayOf(d.u16, 32)).$usage('index'); + expectTypeOf(indexBuf).toExtend>>(); + }); }); describe('TgpuBuffer (InferInput)', () => { From 8d450bb2bddc878395119629515a1420aab21d99 Mon Sep 17 00:00:00 2001 From: Michal Date: Wed, 5 Aug 2026 11:50:50 +0200 Subject: [PATCH 2/3] docs: Document Tgpu*Buffer type aliases for usage annotations --- apps/typegpu-docs/src/content/docs/apis/buffers.mdx | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/apps/typegpu-docs/src/content/docs/apis/buffers.mdx b/apps/typegpu-docs/src/content/docs/apis/buffers.mdx index cca30f307a..6efd7a9ecd 100644 --- a/apps/typegpu-docs/src/content/docs/apis/buffers.mdx +++ b/apps/typegpu-docs/src/content/docs/apis/buffers.mdx @@ -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 = 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. From d4591b0e79f56be207017364c74b9079bb603002 Mon Sep 17 00:00:00 2001 From: Michal Date: Wed, 5 Aug 2026 11:50:50 +0200 Subject: [PATCH 3/3] chore: Ignore .pnpm-store --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index b2e3d3b40b..5650250f1e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ node_modules .pnp .pnp.js +.pnpm-store # testing coverage