Skip to content

Commit 53f4ff8

Browse files
authored
fix: More tree-shakeable and convenient exports (#2068)
1 parent ca63a09 commit 53f4ff8

File tree

216 files changed

+521
-884
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

216 files changed

+521
-884
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ API:
138138

139139
```ts
140140
import type { StorageFlag, TgpuBuffer, TgpuRoot } from 'typegpu';
141-
import * as d from 'typegpu/data';
141+
import { d } from 'typegpu';
142142

143143
// We can define schemas, or functions that return schemas...
144144
const HeightMap = (width: number, height: number) =>

apps/bun-example/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import tgpu from 'typegpu';
2-
import * as d from 'typegpu/data';
1+
import tgpu, { d } from 'typegpu';
32
import { randf } from '@typegpu/noise';
43

54
const Boid = d.struct({
65
pos: d.vec3f,
76
});
87

9-
const createRandomBoid = tgpu.fn([], Boid)(() => {
10-
return { pos: randf.inUnitCube() };
11-
});
8+
const createRandomBoid = () => {
9+
'use gpu';
10+
return Boid({ pos: randf.inUnitCube() });
11+
};
1212

1313
const shaderCode = tgpu.resolve([createRandomBoid]);
1414

apps/typegpu-docs/src/components/translator/lib/constants.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ fn fs_main() -> @location(0) vec4<f32> {
2121
return vec4<f32>(1.0, 0.0, 0.0, 1.0);
2222
}`;
2323

24-
export const DEFAULT_TGSL = `import tgpu from 'typegpu';
25-
import * as d from 'typegpu/data';
24+
export const DEFAULT_TGSL = `import tgpu, { d } from 'typegpu';
2625
2726
const Particle = d.struct({
2827
position: d.vec3f,
@@ -51,13 +50,11 @@ export const updateParicle = tgpu.fn([Particle, d.vec3f, d.f32], Particle)(
5150
},
5251
);
5352
54-
export const main = tgpu.fn([])(() => {
55-
for (let i = 0; i < layout.$.systemData.particles.length; i++) {
56-
const particle = layout.$.systemData.particles[i];
57-
layout.$.systemData.particles[i] = updateParicle(
58-
particle,
59-
layout.$.systemData.gravity,
60-
layout.$.systemData.deltaTime,
61-
);
53+
export function main() {
54+
'use gpu';
55+
const data = layout.$.systemData;
56+
for (let i = 0; i < data.particles.length; i++) {
57+
const particle = data.particles[i];
58+
data.particles[i] = updateParicle(particle, data.gravity, data.deltaTime);
6259
}
63-
});`;
60+
}`;

apps/typegpu-docs/src/content/docs/ecosystem/typegpu-noise.mdx

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ Calling utility functions from [TypeGPU functions](/TypeGPU/fundamentals/functio
2424
In the example below, resolving `randomVec2f` into a shader will include the code for `randf.sample` and all of its dependencies.
2525

2626
```ts twoslash
27-
import tgpu from 'typegpu';
28-
import * as d from 'typegpu/data';
27+
import tgpu, { d } from 'typegpu';
2928
// ---cut---
3029
import { randf } from '@typegpu/noise';
3130

@@ -47,7 +46,7 @@ The resolution mechanism handles deduplication out of the box, as well as omits
4746
so only one definition of `sample` will be included in the final shader.
4847

4948
```ts twoslash
50-
import * as d from 'typegpu/data';
49+
import { d } from 'typegpu';
5150
// ---cut---
5251
import { randf } from '@typegpu/noise';
5352
// `typegpu` is necessary to inject library code into your custom shader
@@ -82,8 +81,7 @@ Each call to `randf.sample` returns the next random float in the sequence, allow
8281
using a set of `randf.seedN` functions, where `N` is the number of components our seed has.
8382

8483
```ts twoslash
85-
import tgpu from 'typegpu';
86-
import * as d from 'typegpu/data';
84+
import tgpu, { d } from 'typegpu';
8785
import { randf } from '@typegpu/noise';
8886

8987
const main = tgpu['~unstable'].fragmentFn({
@@ -146,8 +144,7 @@ The package exports an implementation for both 2D and 3D Perlin noise, `perlin2d
146144
Using it is as simple as calling the `.sample` function with the desired coordinates, and it returns a value in the range `[-1, 1]`.
147145

148146
```ts twoslash
149-
import tgpu from 'typegpu';
150-
import * as d from 'typegpu/data';
147+
import tgpu, { d } from 'typegpu';
151148
// ---cut---
152149
import { perlin2d } from '@typegpu/noise';
153150

@@ -168,8 +165,7 @@ To improve performance, you can precompute the gradients using either a *Static*
168165
A static cache presumes that the domain of the noise function is fixed, and cannot change between shader invocations.
169166

170167
```ts twoslash
171-
import tgpu from 'typegpu';
172-
import * as d from 'typegpu/data';
168+
import tgpu, { d } from 'typegpu';
173169
const root = await tgpu.init();
174170
import { perlin3d } from '@typegpu/noise';
175171

@@ -191,7 +187,7 @@ const pipeline = root['~unstable']
191187
Or in WebGPU:
192188
```ts twoslash
193189
/// <reference types="@webgpu/types" />
194-
import * as d from 'typegpu/data';
190+
import { d } from 'typegpu';
195191

196192
declare const device: GPUDevice;
197193
import tgpu from 'typegpu';
@@ -226,8 +222,7 @@ without having to recompile the shader, you have to use a dynamic cache. With it
226222
complex setup.
227223

228224
```ts twoslash
229-
import tgpu from 'typegpu';
230-
import * as d from 'typegpu/data';
225+
import tgpu, { d } from 'typegpu';
231226
import { perlin3d } from '@typegpu/noise';
232227

233228
const main = tgpu['~unstable'].computeFn({ workgroupSize: [1] })(() => {
@@ -273,10 +268,8 @@ The package provides convenient way to change PRNG as needed.
273268
You can easily implement your own PRNG and plug it into the pipeline.
274269

275270
```ts twoslash
276-
import tgpu from 'typegpu';
277-
import * as d from 'typegpu/data';
271+
import tgpu, { d, std } from 'typegpu';
278272
import { randf } from '@typegpu/noise';
279-
import * as std from 'typegpu/std';
280273

281274
const root = await tgpu.init();
282275

apps/typegpu-docs/src/content/docs/ecosystem/typegpu-sdf.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ Position, scale and translation of a shape can be adjusted by applying the inver
2222
Here is an example of a fragment shader that draws a disk of radius 0.25 on the center of a (presumably square) canvas:
2323

2424
```ts twoslash
25-
import tgpu from 'typegpu';
26-
import * as d from 'typegpu/data';
25+
import tgpu, { d } from 'typegpu';
2726
import * as sdf from '@typegpu/sdf';
2827

2928
const mainFragment = tgpu['~unstable'].fragmentFn({

apps/typegpu-docs/src/content/docs/ecosystem/typegpu-three/index.mdx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ Calling `t3.toTSL` with a TypeGPU function will return a TSL node, which can the
6060
<div class="flex-1 w-auto min-w-[0]">
6161

6262
```ts twoslash
63-
import tgpu from 'typegpu';
64-
import * as d from 'typegpu/data';
63+
import tgpu, { d } from 'typegpu';
6564
// ---cut---
6665
import * as THREE from 'three/webgpu';
6766
import * as t3 from '@typegpu/three';
@@ -113,8 +112,7 @@ There are a handful of builtin TSL node accessors in the `t3` namespace:
113112
<div class="flex-1 w-auto min-w-[0]">
114113

115114
```ts twoslash
116-
import tgpu from 'typegpu';
117-
import * as d from 'typegpu/data';
115+
import tgpu, { d } from 'typegpu';
118116
// ---cut---
119117
import * as THREE from 'three/webgpu';
120118
import * as t3 from '@typegpu/three';
@@ -140,9 +138,7 @@ Other TypeGPU functions (user-defined or from libraries) can be called to achiev
140138
<div class="flex-1 w-auto min-w-[0]">
141139

142140
```ts twoslash
143-
import tgpu from 'typegpu';
144-
import * as d from 'typegpu/data';
145-
import * as std from 'typegpu/std';
141+
import tgpu, { d, std } from 'typegpu';
146142
// ---cut---
147143
import { perlin3d } from '@typegpu/noise';
148144
import * as THREE from 'three/webgpu';

apps/typegpu-docs/src/content/docs/fundamentals/bind-groups.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ A bind group is a collection of resources that are bound to a shader. These reso
1212
It's a way to define what resources are available to a shader and how they are accessed.
1313

1414
```ts
15-
import tgpu from 'typegpu';
16-
import * as d from 'typegpu/data';
15+
import tgpu, { d } from 'typegpu';
1716

1817
// Defining the layout of resources we want the shader to
1918
// have access to.

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

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ results of parallel computation back to JS. When creating a buffer, a schema for
1919
As an example, let's create a buffer for storing particles.
2020

2121
```ts twoslash {22-28}
22-
import tgpu from 'typegpu';
23-
import * as d from 'typegpu/data';
22+
import tgpu, { d } from 'typegpu';
2423

2524
// Defining a struct type
2625
const Particle = d.struct({
@@ -67,8 +66,7 @@ To create a buffer, you will need to define its schema by composing data types i
6766
structs and arrays. They will be explored in more detail in [a following chapter](/TypeGPU/fundamentals/data-schemas).
6867

6968
```ts twoslash
70-
import tgpu from 'typegpu';
71-
import * as d from 'typegpu/data';
69+
import tgpu, { d } from 'typegpu';
7270
const root = await tgpu.init();
7371
// ---cut---
7472
const countBuffer = root.createBuffer(d.u32);
@@ -87,8 +85,7 @@ To be able to use these buffers in WGSL shaders, we have to declare their usage
8785

8886
```ts twoslash
8987
// @noErrors
90-
import tgpu from 'typegpu';
91-
import * as d from 'typegpu/data';
88+
import tgpu, { d } from 'typegpu';
9289
const root = await tgpu.init();
9390
// ---cut---
9491
const buffer = root.createBuffer(d.u32)
@@ -101,8 +98,7 @@ You can also add all flags in a single `$usage()`.
10198

10299
```ts twoslash
103100
// @noErrors
104-
import tgpu from 'typegpu';
105-
import * as d from 'typegpu/data';
101+
import tgpu, { d } from 'typegpu';
106102
const root = await tgpu.init();
107103
// ---cut---
108104
const buffer = root.createBuffer(d.u32)
@@ -113,8 +109,7 @@ const buffer = root.createBuffer(d.u32)
113109
:::note
114110
Along with passing the appropriate flags to WebGPU, the methods will also embed type information into the buffer.
115111
```ts twoslash
116-
import tgpu from 'typegpu';
117-
import * as d from 'typegpu/data';
112+
import tgpu, { d } from 'typegpu';
118113
const root = await tgpu.init();
119114
// ---cut---
120115

@@ -131,8 +126,7 @@ or indirectly through the `.$usage` method.
131126

132127
```ts twoslash
133128
/// <reference types="@webgpu/types" />
134-
import tgpu from 'typegpu';
135-
import * as d from 'typegpu/data';
129+
import tgpu, { d } from 'typegpu';
136130
const root = await tgpu.init();
137131
const buffer = root.createBuffer(d.f32);
138132
// ---cut---
@@ -153,8 +147,7 @@ You can also pass an initial value to the `root.createBuffer` function.
153147
When the buffer is created, it will be mapped at creation, and the initial value will be written to the buffer.
154148

155149
```ts twoslash
156-
import tgpu from 'typegpu';
157-
import * as d from 'typegpu/data';
150+
import tgpu, { d } from 'typegpu';
158151
const root = await tgpu.init();
159152
// ---cut---
160153
// Will be initialized to `100`
@@ -173,8 +166,7 @@ You can also create a buffer using an existing WebGPU buffer. This is useful whe
173166

174167
```ts twoslash {7}
175168
/// <reference types="@webgpu/types" />
176-
import tgpu from 'typegpu';
177-
import * as d from 'typegpu/data';
169+
import tgpu, { d } from 'typegpu';
178170
const root = await tgpu.init();
179171
const device = root.device;
180172
// ---cut---
@@ -200,8 +192,7 @@ method's arguments.
200192

201193
```ts twoslash
202194
// @noErrors
203-
import tgpu from 'typegpu';
204-
import * as d from 'typegpu/data';
195+
import tgpu, { d } from 'typegpu';
205196
const root = await tgpu.init();
206197
// ---cut---
207198
const Particle = d.struct({
@@ -240,8 +231,7 @@ The format of the `data` value depends on your schema type:
240231
- `value`: the new value for that element.
241232

242233
```ts twoslash
243-
import tgpu from 'typegpu';
244-
import * as d from 'typegpu/data';
234+
import tgpu, { d } from 'typegpu';
245235
const root = await tgpu.init();
246236
// ---cut---
247237
const Planet = d.struct({
@@ -268,8 +258,7 @@ There's also an option to copy value from another typed buffer using the `.copyF
268258
as long as both buffers have a matching data schema.
269259

270260
```ts twoslash
271-
import tgpu from 'typegpu';
272-
import * as d from 'typegpu/data';
261+
import tgpu, { d } from 'typegpu';
273262
const root = await tgpu.init();
274263

275264
const Particle = d.struct({
@@ -289,8 +278,7 @@ To read data from a buffer on the CPU, you can use the `.read()` method.
289278
It returns a promise that resolves to the data read from the buffer.
290279

291280
```ts twoslash
292-
import tgpu from 'typegpu';
293-
import * as d from 'typegpu/data';
281+
import tgpu, { d } from 'typegpu';
294282
const root = await tgpu.init();
295283
// ---cut---
296284
const buffer = root.createBuffer(d.arrayOf(d.u32, 10));
@@ -321,8 +309,7 @@ TypeGPU also allows for the use of index buffers. An index buffer is a buffer co
321309
You may refer to the [pipelines](/TypeGPU/fundamentals/pipelines/#drawing-with-drawindexed) section for usage details.
322310

323311
```ts twoslash
324-
import tgpu from 'typegpu';
325-
import * as d from 'typegpu/data';
312+
import tgpu, { d } from 'typegpu';
326313

327314
const root = await tgpu.init();
328315
// ---cut---
@@ -343,8 +330,7 @@ The default option is to create bind group layouts and bind your buffers via bin
343330
Read more in the chapter dedicated to [bind groups](/TypeGPU/fundamentals/bind-groups).
344331

345332
```ts twoslash
346-
import tgpu from 'typegpu';
347-
import * as d from 'typegpu/data';
333+
import tgpu, { d } from 'typegpu';
348334

349335
const root = await tgpu.init();
350336
// ---cut---
@@ -382,8 +368,7 @@ Fixed buffers are created using dedicated root methods.
382368
| `var<storage, read_write>` | `root.createMutable()` |
383369

384370
```ts twoslash
385-
import tgpu from 'typegpu';
386-
import * as d from 'typegpu/data';
371+
import tgpu, { d } from 'typegpu';
387372

388373
const root = await tgpu.init();
389374
// ---cut---

0 commit comments

Comments
 (0)