Skip to content

Commit e0f26ee

Browse files
authored
docs: Textures (and samplers I guess) (#1868)
1 parent b669a0a commit e0f26ee

4 files changed

Lines changed: 492 additions & 0 deletions

File tree

apps/typegpu-docs/astro.config.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ export default defineConfig({
140140
label: 'Buffers',
141141
slug: 'fundamentals/buffers',
142142
},
143+
{
144+
label: 'Textures',
145+
slug: 'fundamentals/textures',
146+
badge: { text: 'new' },
147+
},
143148
{
144149
label: 'Variables',
145150
slug: 'fundamentals/variables',

apps/typegpu-docs/ec.config.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ export default {
88
twoslashOptions: {
99
strict: true,
1010
compilerOptions: { moduleResolution: ts.ModuleResolutionKind.Bundler },
11+
extraFiles: {
12+
'global.d.ts': `
13+
/// <reference lib="dom" />
14+
/// <reference types="@webgpu/types" />
15+
`,
16+
},
1117
},
1218
}),
1319
],

apps/typegpu-docs/src/content/docs/fundamentals/data-schemas.mdx

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,116 @@ const array = ArrayPartialSchema(2)([1.2, 19.29]);
343343
// ^?
344344
```
345345

346+
## Textures
347+
348+
Texture schemas serve two main purposes:
349+
- defining texture views (both fixed and in layouts)
350+
- providing argument types for user defined functions
351+
352+
```ts twoslash
353+
import tgpu from 'typegpu';
354+
import * as d from 'typegpu/data';
355+
import * as std from 'typegpu/std';
356+
357+
const root = await tgpu.init();
358+
const texture = root['~unstable'].createTexture({
359+
size: [256, 256],
360+
format: 'rgba8unorm',
361+
}).$usage('sampled', 'storage');
362+
363+
// ---cut---
364+
const storeOnes = (tex: d.textureStorage2d<'rgba8unorm'>, coords: d.v2u) => {
365+
'use gpu';
366+
std.textureStore(tex, coords, d.vec4f(1));
367+
};
368+
369+
const storeOnesShelled = tgpu.fn([d.textureStorage2d('rgba8unorm'), d.vec2u])(
370+
(tex, coords) => {
371+
std.textureStore(tex, coords, d.vec4f(1));
372+
},
373+
);
374+
375+
const sampledView = texture.createView(d.texture2d(d.f32));
376+
// ^?
377+
378+
const storageView = texture.createView(d.textureStorage2d('rgba8unorm', 'read-only'));
379+
// ^?
380+
381+
const layout = tgpu.bindGroupLayout({
382+
// ^?
383+
sampled: { texture: d.texture2d() },
384+
storage: { storageTexture: d.textureStorage2d('rgba8unorm', 'read-only') },
385+
});
386+
```
387+
388+
### Sampled Textures
389+
390+
Sampled texture schemas are created using one of the following constructors:
391+
392+
- **`d.texture1d(sampleType?)`** - A 1D texture
393+
- **`d.texture2d(sampleType?)`** - A 2D texture
394+
- **`d.texture2dArray(sampleType?)`** - A 2D array texture
395+
- **`d.texture3d(sampleType?)`** - A 3D texture
396+
- **`d.textureCube(sampleType?)`** - A cube texture
397+
- **`d.textureCubeArray(sampleType?)`** - A cube array texture
398+
- **`d.textureMultisampled2d(sampleType?)`** - A 2D multisampled texture
399+
400+
The `sampleType` parameter can be `d.f32`, `d.i32`, or `d.u32`, determining how the texture data will be interpreted. If omitted, it defaults to `d.f32`.
401+
402+
```ts twoslash
403+
import * as d from 'typegpu/data';
404+
// ---cut---
405+
const tex1 = d.texture2d(d.f32); // float texture (default)
406+
// ^?
407+
408+
const tex2 = d.texture2d(d.u32); // unsigned integer texture
409+
// ^?
410+
411+
const tex3 = d.texture2dArray(); // defaults to f32
412+
// ^?
413+
```
414+
415+
#### Depth Textures
416+
417+
For depth comparison operations, TypeGPU provides specialized depth texture schemas:
418+
419+
- **`d.textureDepth2d()`** - A 2D depth texture
420+
- **`d.textureDepthMultisampled2d()`** - A 2D multisampled depth texture
421+
- **`d.textureDepth2dArray()`** - A 2D array depth texture
422+
- **`d.textureDepthCube()`** - A cube depth texture
423+
- **`d.textureDepthCubeArray()`** - A cube array depth texture
424+
425+
```ts twoslash
426+
import * as d from 'typegpu/data';
427+
// ---cut---
428+
const depthTex = d.textureDepth2d();
429+
// ^?
430+
```
431+
432+
### Storage Textures
433+
434+
Storage texture schemas are created using dimension-specific constructors, with required `format` and optional `access` parameters:
435+
436+
- **`d.textureStorage1d(format, access?)`** - A 1D storage texture
437+
- **`d.textureStorage2d(format, access?)`** - A 2D storage texture
438+
- **`d.textureStorage2dArray(format, access?)`** - A 2D array storage texture
439+
- **`d.textureStorage3d(format, access?)`** - A 3D storage texture
440+
441+
The `format` parameter specifies the texture format (e.g., `'rgba8unorm'`, `'rgba16float'`, `'r32float'`), and the `access` parameter can be `'write-only'`, `'read-only'`, or `'read-write'`. If `access` is omitted, it defaults to `'write-only'`.
442+
443+
```ts twoslash
444+
import * as d from 'typegpu/data';
445+
// ---cut---
446+
const storageTex1 = d.textureStorage2d('rgba8unorm');
447+
// ^?
448+
449+
const storageTex2 = d.textureStorage2d('rgba8unorm', 'read-only');
450+
// ^?
451+
452+
const storageTex3 = d.textureStorage3d('r32float', 'read-write');
453+
// ^?
454+
```
455+
346456
## Atomics
347457

348458
To create a schema corresponding to an atomic data type, wrap `d.i32` or `d.u32` with `d.atomic`.

0 commit comments

Comments
 (0)