Skip to content

Commit 1a5c2b2

Browse files
mhawrylukiwoplaza
andauthored
feat: Pipelines .pipe (#1414)
* feat: Pipelines `.pipe` * More specific type for pipes --------- Co-authored-by: Iwo Plaza <[email protected]>
1 parent a01aa6d commit 1a5c2b2

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

apps/typegpu-docs/src/content/examples/rendering/perlin-noise/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@ const mainFragment = tgpu['~unstable'].fragmentFn({
5858

5959
// Configuring a dynamic (meaning it's size can change) cache
6060
// for perlin noise gradients.
61-
const PerlinCacheConfig = perlin3d.dynamicCacheConfig();
61+
const perlinCacheConfig = perlin3d.dynamicCacheConfig();
6262

6363
/** Contains all resources that the perlin cache needs access to */
64-
const dynamicLayout = tgpu.bindGroupLayout({ ...PerlinCacheConfig.layout });
64+
const dynamicLayout = tgpu.bindGroupLayout({ ...perlinCacheConfig.layout });
6565

6666
const root = await tgpu.init();
6767
const device = root.device;
6868

6969
// Instantiating the cache with an initial size.
70-
const perlinCache = PerlinCacheConfig.instance(root, d.vec3u(4, 4, DEPTH));
70+
const perlinCache = perlinCacheConfig.instance(root, d.vec3u(4, 4, DEPTH));
7171

7272
const canvas = document.querySelector('canvas') as HTMLCanvasElement;
7373
const context = canvas.getContext('webgpu') as GPUCanvasContext;
@@ -86,8 +86,7 @@ const renderPipelineBase = root['~unstable']
8686
.with(gridSizeAccess, gridSize)
8787
.with(timeAccess, time)
8888
.with(sharpnessAccess, sharpness)
89-
.with(perlin3d.getJunctionGradientSlot, PerlinCacheConfig.getJunctionGradient)
90-
.with(PerlinCacheConfig.valuesSlot, dynamicLayout.$);
89+
.pipe(perlinCacheConfig.inject(dynamicLayout.$));
9190

9291
const renderPipelines = {
9392
exponential: renderPipelineBase

packages/typegpu-noise/src/perlin-3d/dynamic-cache.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import tgpu, {
2+
type Configurable,
23
type StorageFlag,
34
type TgpuBuffer,
45
type TgpuFn,
@@ -9,7 +10,10 @@ import tgpu, {
910
import * as d from 'typegpu/data';
1011
import { allEq } from 'typegpu/std';
1112
import type { PrefixKeys, Prettify } from '../utils.ts';
12-
import { computeJunctionGradient } from './algorithm.ts';
13+
import {
14+
computeJunctionGradient,
15+
getJunctionGradientSlot,
16+
} from './algorithm.ts';
1317

1418
const MemorySchema = (n: number) => d.arrayOf(d.vec3f, n);
1519

@@ -46,6 +50,10 @@ export interface DynamicPerlin3DCacheConfig<Prefix extends string> {
4650
root: TgpuRoot,
4751
initialSize: d.v3u,
4852
): DynamicPerlin3DCache<Prefix>;
53+
54+
inject(
55+
layoutValue: LayoutValue<Prefix>,
56+
): (cfg: Configurable) => Configurable;
4957
}
5058

5159
export interface DynamicPerlin3DCache<Prefix extends string> {
@@ -243,5 +251,10 @@ export function dynamicCacheConfig<Prefix extends string>(
243251
valuesSlot,
244252
getJunctionGradient,
245253
instance,
254+
255+
inject: (layoutValue) => (cfg) =>
256+
cfg
257+
.with(getJunctionGradientSlot, getJunctionGradient)
258+
.with(valuesSlot, layoutValue),
246259
};
247260
}

packages/typegpu/src/core/root/init.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ import {
8787
type TgpuVertexLayout,
8888
} from '../vertexLayout/vertexLayout.ts';
8989
import type {
90+
Configurable,
9091
CreateTextureOptions,
9192
CreateTextureResult,
9293
ExperimentalTgpuRoot,
@@ -140,6 +141,10 @@ class WithBindingImpl implements WithBinding {
140141
multisampleState: undefined,
141142
});
142143
}
144+
145+
pipe(transform: (cfg: Configurable) => Configurable): Configurable {
146+
return transform(this);
147+
}
143148
}
144149

145150
class WithComputeImpl implements WithCompute {

packages/typegpu/src/core/root/rootTypes.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export interface WithFragment<
111111
createPipeline(): TgpuRenderPipeline<Output>;
112112
}
113113

114-
export interface WithBinding {
114+
export interface Configurable {
115115
with<T>(slot: TgpuSlot<T>, value: Eventual<T>): WithBinding;
116116
with<T extends AnyWgslData>(
117117
accessor: TgpuAccessor<T>,
@@ -122,6 +122,10 @@ export interface WithBinding {
122122
| Infer<T>,
123123
): WithBinding;
124124

125+
pipe(transform: (cfg: Configurable) => Configurable): Configurable;
126+
}
127+
128+
export interface WithBinding extends Configurable {
125129
withCompute<ComputeIn extends IORecord<AnyComputeBuiltin>>(
126130
entryFn: TgpuComputeFn<ComputeIn>,
127131
): WithCompute;

packages/typegpu/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export { isTgpuFn } from './core/function/tgpuFn.ts';
9191
// types
9292

9393
export type {
94+
Configurable,
9495
TgpuRoot,
9596
WithBinding,
9697
WithCompute,

0 commit comments

Comments
 (0)