Skip to content

Commit 9d8403f

Browse files
authored
feat: Common fullScreenTriangle vertex function (#1831)
1 parent 3a20653 commit 9d8403f

File tree

17 files changed

+99
-130
lines changed

17 files changed

+99
-130
lines changed

apps/typegpu-docs/src/examples/image-processing/ascii-filter/index.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import tgpu, { type TgpuBindGroup } from 'typegpu';
22
import * as d from 'typegpu/data';
33
import * as std from 'typegpu/std';
4+
import { fullScreenTriangle } from 'typegpu/common';
45

56
const root = await tgpu.init();
67

@@ -45,19 +46,6 @@ const characterFn = tgpu.fn([d.u32, d.vec2f], d.f32)((n, p) => {
4546
return d.f32((n >> a) & 1);
4647
});
4748

48-
const fullScreenTriangle = tgpu['~unstable'].vertexFn({
49-
in: { vertexIndex: d.builtin.vertexIndex },
50-
out: { pos: d.builtin.position, uv: d.vec2f },
51-
})((input) => {
52-
const pos = [d.vec2f(-1, -1), d.vec2f(3, -1), d.vec2f(-1, 3)];
53-
const uv = [d.vec2f(0, 1), d.vec2f(2, 1), d.vec2f(0, -1)];
54-
55-
return {
56-
pos: d.vec4f(pos[input.vertexIndex], 0, 1),
57-
uv: uv[input.vertexIndex],
58-
};
59-
});
60-
6149
/**
6250
* Adapted from the original Shadertoy implementation by movAX13h:
6351
* https://www.shadertoy.com/view/lssGDj

apps/typegpu-docs/src/examples/image-processing/blur/index.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import tgpu from 'typegpu';
55
import * as d from 'typegpu/data';
66
import * as std from 'typegpu/std';
7+
import { fullScreenTriangle } from 'typegpu/common';
78

89
const root = await tgpu.init();
910
const device = root.device;
@@ -127,19 +128,6 @@ const computeFn = tgpu['~unstable'].computeFn({
127128
}
128129
});
129130

130-
const fullScreenTriangle = tgpu['~unstable'].vertexFn({
131-
in: { vertexIndex: d.builtin.vertexIndex },
132-
out: { pos: d.builtin.position, uv: d.vec2f },
133-
})((input) => {
134-
const pos = [d.vec2f(-1, -1), d.vec2f(3, -1), d.vec2f(-1, 3)];
135-
const uv = [d.vec2f(0, 1), d.vec2f(2, 1), d.vec2f(0, -1)];
136-
137-
return {
138-
pos: d.vec4f(pos[input.vertexIndex], 0, 1),
139-
uv: uv[input.vertexIndex],
140-
};
141-
});
142-
143131
const renderFragment = tgpu['~unstable'].fragmentFn({
144132
in: { uv: d.vec2f },
145133
out: d.vec4f,

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
import tgpu from 'typegpu';
22
import * as d from 'typegpu/data';
3+
import { fullScreenTriangle } from 'typegpu/common';
34
import { perlin3d } from '@typegpu/noise';
45
import { abs, mix, mul, pow, sign, tanh } from 'typegpu/std';
56

67
/** The depth of the perlin noise (in time), after which the pattern loops around */
78
const DEPTH = 10;
89

9-
const fullScreenTriangle = tgpu['~unstable'].vertexFn({
10-
in: { vertexIndex: d.builtin.vertexIndex },
11-
out: { pos: d.builtin.position, uv: d.vec2f },
12-
})((input) => {
13-
const pos = [d.vec2f(-1, -1), d.vec2f(3, -1), d.vec2f(-1, 3)];
14-
15-
return {
16-
pos: d.vec4f(pos[input.vertexIndex], 0.0, 1.0),
17-
uv: mul(0.5, pos[input.vertexIndex]),
18-
};
19-
});
20-
2110
const gridSizeAccess = tgpu['~unstable'].accessor(d.f32);
2211
const timeAccess = tgpu['~unstable'].accessor(d.f32);
2312
const sharpnessAccess = tgpu['~unstable'].accessor(d.f32);

apps/typegpu-docs/src/examples/simple/liquid-glass/common.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

apps/typegpu-docs/src/examples/simple/liquid-glass/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import tgpu from 'typegpu';
22
import * as d from 'typegpu/data';
33
import * as std from 'typegpu/std';
4+
import { fullScreenTriangle } from 'typegpu/common';
45
import { sdRoundedBox2d } from '@typegpu/sdf';
5-
import { fullScreenTriangle } from './common.ts';
66

77
const root = await tgpu.init();
88
const device = root.device;

apps/typegpu-docs/src/examples/simulation/slime-mold-3d/index.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import tgpu from 'typegpu';
22
import * as d from 'typegpu/data';
33
import * as std from 'typegpu/std';
4+
import { fullScreenTriangle } from 'typegpu/common';
45
import { randf } from '@typegpu/noise';
56
import * as m from 'wgpu-matrix';
67

@@ -346,19 +347,6 @@ const blur = tgpu['~unstable'].computeFn({
346347
);
347348
});
348349

349-
const fullScreenTriangle = tgpu['~unstable'].vertexFn({
350-
in: { vertexIndex: d.builtin.vertexIndex },
351-
out: { pos: d.builtin.position, uv: d.vec2f },
352-
})((input) => {
353-
const pos = [d.vec2f(-1, -1), d.vec2f(3, -1), d.vec2f(-1, 3)];
354-
const uv = [d.vec2f(0, 1), d.vec2f(2, 1), d.vec2f(0, -1)];
355-
356-
return {
357-
pos: d.vec4f(pos[input.vertexIndex], 0, 1),
358-
uv: uv[input.vertexIndex],
359-
};
360-
});
361-
362350
const sampler = root['~unstable'].createSampler({
363351
magFilter: canFilter ? 'linear' : 'nearest',
364352
minFilter: canFilter ? 'linear' : 'nearest',

apps/typegpu-docs/src/examples/tests/texture-test/index.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import tgpu from 'typegpu';
22
import * as d from 'typegpu/data';
3+
import { fullScreenTriangle } from 'typegpu/common';
34

45
const root = await tgpu.init();
56
const canvas = document.querySelector('canvas') as HTMLCanvasElement;
@@ -47,18 +48,6 @@ let bindGroup = root.createBindGroup(layout, {
4748
myTexture: texture,
4849
});
4950

50-
const fullScreenTriangle = tgpu['~unstable'].vertexFn({
51-
in: { vertexIndex: d.builtin.vertexIndex },
52-
out: { pos: d.builtin.position, uv: d.vec2f },
53-
})((input) => {
54-
const pos = [d.vec2f(-1, -1), d.vec2f(3, -1), d.vec2f(-1, 3)];
55-
const uv = [d.vec2f(0, 1), d.vec2f(2, 1), d.vec2f(0, -1)];
56-
57-
return {
58-
pos: d.vec4f(pos[input.vertexIndex], 0, 1),
59-
uv: uv[input.vertexIndex],
60-
};
61-
});
6251
const biasUniform = root.createUniform(d.f32);
6352

6453
const fragmentFunction = tgpu['~unstable'].fragmentFn({

packages/typegpu/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"./package.json": "./package.json",
1010
".": "./src/index.ts",
1111
"./data": "./src/data/index.ts",
12-
"./std": "./src/std/index.ts"
12+
"./std": "./src/std/index.ts",
13+
"./common": "./src/common/index.ts"
1314
},
1415
"publishConfig": {
1516
"directory": "dist",
@@ -35,6 +36,12 @@
3536
"module": "./dist/std/index.js",
3637
"import": "./dist/std/index.js",
3738
"default": "./dist/std/index.cjs"
39+
},
40+
"./common": {
41+
"types": "./dist/common/index.d.ts",
42+
"module": "./dist/common/index.js",
43+
"import": "./dist/common/index.js",
44+
"default": "./dist/common/index.cjs"
3845
}
3946
}
4047
},
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { builtin } from '../builtin.ts';
2+
import { vertexFn } from '../core/function/tgpuVertexFn.ts';
3+
import { vec2f } from '../data/vector.ts';
4+
5+
/**
6+
* A vertex function that defines a single full-screen triangle out
7+
* of three points.
8+
*
9+
* @example
10+
* ```ts
11+
* import { fullScreenTriangle } from 'typegpu/common';
12+
*
13+
* const pipeline = root['~unstable']
14+
* .withVertex(fullScreenTriangle)
15+
* .withFragment(yourFragmentShader)
16+
* .createPipeline();
17+
*
18+
* pipeline.draw(3);
19+
* ```
20+
*/
21+
export const fullScreenTriangle = vertexFn({
22+
in: { vertexIndex: builtin.vertexIndex },
23+
out: { pos: builtin.position, uv: vec2f },
24+
})`{
25+
const pos = array<vec2f, 3>(vec2f(-1, -1), vec2f(3, -1), vec2f(-1, 3));
26+
const uv = array<vec2f, 3>(vec2f(0, 1), vec2f(2, 1), vec2f(0, -1));
27+
28+
return Out(vec4f(pos[in.vertexIndex], 0, 1), uv[in.vertexIndex]);
29+
}`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { fullScreenTriangle } from './fullScreenTriangle.ts';

0 commit comments

Comments
 (0)