Skip to content

Commit ae1466c

Browse files
authored
Merge pull request #3 from iwoplaza/chore/typegpu-0.4.5
Updating to typegpu 0.4.6
2 parents 892da5d + f28591c commit ae1466c

File tree

21 files changed

+174
-123
lines changed

21 files changed

+174
-123
lines changed

apps/phoure-www/astro.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import starlight from '@astrojs/starlight';
33
import tailwind from '@astrojs/tailwind';
44
// @ts-check
55
import { defineConfig } from 'astro/config';
6-
import typegpu from 'rollup-plugin-typegpu';
6+
import typegpu from 'unplugin-typegpu/vite';
77

88
// https://astro.build/config
99
export default defineConfig({

apps/phoure-www/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@
4242
"phoure": "workspace:*",
4343
"react": "^18.0.0",
4444
"react-dom": "^18.0.0",
45-
"rollup-plugin-typegpu": "^0.0.1",
4645
"sharp": "^0.33.5",
4746
"tailwind-merge": "^2.5.2",
4847
"tailwindcss": "^3.4.17",
4948
"tailwindcss-animate": "^1.0.7",
5049
"typegpu": "catalog:",
50+
"unplugin-typegpu": "catalog:",
5151
"wgpu-matrix": "^3.3.0"
5252
},
5353
"devDependencies": {

apps/phoure-www/src/lib-filter/bicubicFilter.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ const layout = tgpu
1212
.$name('Resample - external bind group layout');
1313

1414
const fullScreenQuadVertexFn = tgpu['~unstable']
15-
.vertexFn(
16-
{ idx: builtin.vertexIndex },
17-
{
15+
.vertexFn({
16+
in: { idx: builtin.vertexIndex },
17+
out: {
1818
pos: builtin.position,
1919
uv: vec2f,
2020
texelSizeX: vec2f,
2121
texelSizeY: vec2f,
2222
coordHG: vec2f,
2323
},
24-
)
25-
.does(/* wgsl */ `(@builtin(vertex_index) idx: u32) -> VertexOutput {
24+
})
25+
.does(/* wgsl */ `(input: VertexInput) -> VertexOutput {
2626
const SCREEN_RECT = array<vec2f, 6>(
2727
vec2f(-1.0, -1.0),
2828
vec2f(1.0, -1.0),
@@ -46,13 +46,13 @@ const fullScreenQuadVertexFn = tgpu['~unstable']
4646
let viewport_size = vec2f(textureDimensions(texture));
4747
4848
var output: VertexOutput;
49-
output.pos = vec4f(SCREEN_RECT[idx], 0.0, 1.0);
50-
output.uv = UVS[idx];
49+
output.pos = vec4f(SCREEN_RECT[input.idx], 0.0, 1.0);
50+
output.uv = UVS[input.idx];
5151
output.texelSizeX = vec2f(1. / f32(viewport_size.x), 0);
5252
output.texelSizeY = vec2f(0, 1. / f32(viewport_size.y));
5353
5454
// calc filter texture coordinates where [0,1] is a single texel
55-
output.coordHG = UVS[idx] * viewport_size - vec2f(0.5f, 0.5f); // fetch offsets and weights from filter texture
55+
output.coordHG = UVS[input.idx] * viewport_size - vec2f(0.5f, 0.5f); // fetch offsets and weights from filter texture
5656
5757
return output;
5858
}`)
@@ -63,25 +63,25 @@ const fullScreenQuadVertexFn = tgpu['~unstable']
6363
* https://developer.nvidia.com/gpugems/gpugems2/part-iii-high-quality-rendering/chapter-20-fast-third-order-texture-filtering
6464
*/
6565
const resampleCubic = tgpu['~unstable']
66-
.fragmentFn(
67-
{
66+
.fragmentFn({
67+
in: {
6868
pos: builtin.position,
6969
uv: vec2f,
7070
texelSizeX: vec2f,
7171
texelSizeY: vec2f,
7272
coordHG: vec2f,
7373
},
74-
vec4f,
75-
)
76-
.does(/* wgsl */ `(@location(0) uv: vec2f, @location(1) texelSizeX: vec2f, @location(2) texelSizeY: vec2f, @location(3) coord_hg: vec2f) -> @location(0) vec4f {
77-
var hg_x = textureSample(hgLookup, wrappingSampler, coord_hg.x).xyz;
78-
var hg_y = textureSample(hgLookup, wrappingSampler, coord_hg.y).xyz; // determine linear sampling coordinates
79-
var coord_source10 = uv + hg_x.x * texelSizeX;
80-
var coord_source00 = uv - hg_x.y * texelSizeX;
81-
var coord_source11 = coord_source10 + hg_y.x * texelSizeY;
82-
var coord_source01 = coord_source00 + hg_y.x * texelSizeY;
83-
coord_source10 = coord_source10 - hg_y.y * texelSizeY;
84-
coord_source00 = coord_source00 - hg_y.y * texelSizeY; // fetch four linearly interpolated inputs
74+
out: vec4f,
75+
})
76+
.does(/* wgsl */ `(input: FragmentInput) -> @location(0) vec4f {
77+
var hg_x = textureSample(hgLookup, wrappingSampler, input.coordHG.x).xyz;
78+
var hg_y = textureSample(hgLookup, wrappingSampler, input.coordHG.y).xyz; // determine linear sampling coordinates
79+
var coord_source10 = input.uv + hg_x.x * input.texelSizeX;
80+
var coord_source00 = input.uv - hg_x.y * input.texelSizeX;
81+
var coord_source11 = coord_source10 + hg_y.x * input.texelSizeY;
82+
var coord_source01 = coord_source00 + hg_y.x * input.texelSizeY;
83+
coord_source10 = coord_source10 - hg_y.y * input.texelSizeY;
84+
coord_source00 = coord_source00 - hg_y.y * input.texelSizeY; // fetch four linearly interpolated inputs
8585
var tex_source00 = textureSample(texture, clampingSampler, coord_source00);
8686
var tex_source10 = textureSample(texture, clampingSampler, coord_source10);
8787
var tex_source01 = textureSample(texture, clampingSampler, coord_source01);

apps/phoure-www/src/lib-phoure/combineShader.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ import * as d from 'typegpu/data';
66
export const combinationLayout = tgpu
77
.bindGroupLayout({
88
blurredTexture: { texture: 'float' },
9-
mendedBuffer: { storage: (n) => d.arrayOf(d.f32, n) },
9+
mendedBuffer: { storage: (n: number) => d.arrayOf(d.f32, n) },
1010
})
1111
.$name('combinationLayout');
1212

1313
const { blurredTexture, mendedBuffer } = combinationLayout.bound;
1414

1515
export const combinationEntryFn = tgpu['~unstable']
16-
.fragmentFn({ pos: d.builtin.position, uv: d.vec2f }, d.vec4f)
17-
.does(/* wgsl */ `(@builtin(position) coord_f: vec4f) -> @location(0) vec4f {
18-
let coord = vec2u(floor(coord_f.xy));
16+
.fragmentFn({
17+
in: { coord_f: d.builtin.position, uv: d.vec2f },
18+
out: d.vec4f,
19+
})
20+
.does(/* wgsl */ `(input: FragmentInput) -> @location(0) vec4f {
21+
let coord = vec2u(floor(input.coord_f.xy));
1922
2023
let blurred = textureLoad(
2124
blurredTexture,

apps/phoure-www/src/lib-phoure/menderStep.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { convertRgbToY } from '@typegpu/color';
22
import { accessViewportSize } from '@typegpu/common';
3-
import tgpu, { unstable_asUniform, type TgpuRoot } from 'typegpu';
3+
import tgpu, { type TgpuRoot } from 'typegpu';
44
import * as d from 'typegpu/data';
55

66
import {
@@ -55,8 +55,11 @@ const BLOCK_SIZE = 8;
5555
// }`;
5656

5757
const ioLayout = tgpu.bindGroupLayout({
58-
output_buffer: { storage: (n) => d.arrayOf(d.f32, n), access: 'mutable' },
59-
input_buffer: { storage: (n) => d.arrayOf(d.vec4f, n) },
58+
output_buffer: {
59+
storage: (n: number) => d.arrayOf(d.f32, n),
60+
access: 'mutable',
61+
},
62+
input_buffer: { storage: (n: number) => d.arrayOf(d.vec4f, n) },
6063
blurred_tex: { texture: 'unfilterable-float' },
6164
aux_tex: { texture: 'unfilterable-float' },
6265
});
@@ -147,14 +150,19 @@ const menderConvolveFn = convolveFn({
147150
});
148151

149152
const entryComputeFn = tgpu['~unstable']
150-
.computeFn([], { workgroupSize: [BLOCK_SIZE, BLOCK_SIZE] })
151-
.does(/* wgsl */ `(@builtin(global_invocation_id) gid: vec3u) {
153+
.computeFn({
154+
workgroupSize: [BLOCK_SIZE, BLOCK_SIZE],
155+
in: {
156+
gid: d.builtin.globalInvocationId,
157+
},
158+
})
159+
.does(/* wgsl */ `(input: Input) {
152160
var result: array<f32, OUT_CHANNELS>;
153161
for (var i = 0; i < OUT_CHANNELS; i += 1) {
154162
result[i] = biases[i];
155163
}
156164
157-
menderConvolveFn(gid.xy, &result);
165+
menderConvolveFn(input.gid.xy, &result);
158166
159167
if (reluSlot) {
160168
applyReLU(&result);
@@ -163,8 +171,8 @@ const entryComputeFn = tgpu['~unstable']
163171
let canvasSize = accessViewportSize;
164172
165173
let output_buffer_begin =
166-
(gid.y * u32(canvasSize.x) +
167-
gid.x) * OUT_CHANNELS;
174+
(input.gid.y * u32(canvasSize.x) +
175+
input.gid.x) * OUT_CHANNELS;
168176
169177
for (var i: u32 = 0; i < OUT_CHANNELS; i++) {
170178
output_buffer[output_buffer_begin + i] = result[i];
@@ -232,7 +240,7 @@ export const MenderStep = ({ root, gBuffer, targetTexture }: Options) => {
232240
.with(outChannelsSlot, options.outChannels)
233241
.with(reluSlot, options.relu)
234242
.with(inputFromGBufferSlot, options.inputFromGBuffer)
235-
.with(accessViewportSize, unstable_asUniform(viewportSizeBuffer))
243+
.with(accessViewportSize, viewportSizeBuffer.as('uniform'))
236244
// ---
237245
.withCompute(entryComputeFn)
238246
.createPipeline()
@@ -296,7 +304,7 @@ export const MenderStep = ({ root, gBuffer, targetTexture }: Options) => {
296304
// ---
297305

298306
const combinationPipeline = root['~unstable']
299-
.with(accessViewportSize, unstable_asUniform(viewportSizeBuffer))
307+
.with(accessViewportSize, viewportSizeBuffer.as('uniform'))
300308
.withVertex(fullScreenQuadVertexFn, {})
301309
.withFragment(combinationEntryFn, { format: 'rgba8unorm' })
302310
.createPipeline();

apps/phoure-www/src/lib/GameEngine/blipDifferenceStep/blipDifferenceStep.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ type Options = {
1010
};
1111

1212
const fragFn = tgpu['~unstable']
13-
.fragmentFn({ coordFloat: d.builtin.position }, d.vec4f)
14-
.does(`(@builtin(position) coordFloat: vec4f) -> @location(0) vec4f {
15-
var coord = vec2u(floor(coordFloat.xy));
13+
.fragmentFn({ in: { coordFloat: d.builtin.position }, out: d.vec4f })
14+
.does(`(input: FragmentInput) -> @location(0) vec4f {
15+
var coord = vec2u(floor(input.coordFloat.xy));
1616
1717
let color_a = textureLoad(
1818
texture_a,
@@ -27,8 +27,7 @@ const fragFn = tgpu['~unstable']
2727
);
2828
2929
return vec4f(abs(color_a.rgb - color_b.rgb), 1.0);
30-
}`)
31-
.$uses({});
30+
}`);
3231

3332
export const BlipDifferenceStep = ({
3433
root,

apps/phoure-www/src/lib/GameEngine/gBufferDebugger.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ const layout = tgpu.bindGroupLayout({
1919
});
2020

2121
const mainFragFn = tgpu['~unstable']
22-
.fragmentFn({ pos: d.builtin.position, uv: d.vec2f }, d.vec4f)
23-
.does(/* wgsl */ `(@builtin(position) coord_f: vec4f, @location(0) uv: vec2f) -> @location(0) vec4f {
24-
let coord = vec2<i32>(floor(coord_f.xy));
22+
.fragmentFn({
23+
in: { coord_f: d.builtin.position, uv: d.vec2f },
24+
out: d.vec4f,
25+
})
26+
.does(/* wgsl */ `(input: FragmentInput) -> @location(0) vec4f {
27+
let coord = vec2<i32>(floor(input.coord_f.xy));
2528
let channel_mode = getChannelModeSlot();
2629
2730
let blurred = textureLoad(
@@ -45,7 +48,7 @@ const mainFragFn = tgpu['~unstable']
4548
4649
var result: vec4<f32>;
4750
48-
let c = uv;
51+
let c = input.uv;
4952
if (channel_mode == CHANNEL_SPLIT) {
5053
if (c.x < 0.33) {
5154
// NORMALS

apps/phoure-www/src/lib/GameEngine/postProcessingStep.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ const layout = tgpu
1818
.$name('Post Processing - Bind Group Layout');
1919

2020
const mainFragFn = tgpu['~unstable']
21-
.fragmentFn({ pos: d.builtin.position, uv: d.vec2f }, d.vec4f)
22-
.does(`(@builtin(position) coord_f: vec4f) -> @location(0) vec4f {
23-
var coord = vec2u(floor(coord_f.xy));
21+
.fragmentFn({
22+
in: { coord_f: d.builtin.position, uv: d.vec2f },
23+
out: d.vec4f,
24+
})
25+
.does(`(input: FragmentInput) -> @location(0) vec4f {
26+
var coord = vec2u(floor(input.coord_f.xy));
2427
2528
let color = textureLoad(
2629
sourceTexture,

apps/phoure-www/src/lib/GameEngine/sdfRenderer/sdfRenderer.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,15 @@ const mainLayout = tgpu.bindGroupLayout({
182182
});
183183

184184
const mainComputeFn = tgpu['~unstable']
185-
.computeFn([], { workgroupSize: [BlockSize, BlockSize] })
186-
.does(/* wgsl */ `(@builtin(global_invocation_id) gid: vec3u) {
187-
setupRandomSeed(vec2f(gid.xy) * ${Math.random()} + getRandomSeedPrimer * ${Math.random()});
185+
.computeFn({
186+
workgroupSize: [BlockSize, BlockSize],
187+
in: { gid: d.builtin.globalInvocationId },
188+
})
189+
.does(/* wgsl */ `(input: Input) {
190+
setupRandomSeed(vec2f(input.gid.xy) * ${Math.random()} + getRandomSeedPrimer * ${Math.random()});
188191
189192
let prev_layers = getAccumulatedLayers;
190-
let prev_render = textureLoad(previousRender, gid.xy, 0);
193+
let prev_render = textureLoad(previousRender, input.gid.xy, 0);
191194
192195
var acc = vec3f(0., 0., 0.);
193196
for (var sx = 0u; sx < SUPER_SAMPLES; sx++) {
@@ -197,7 +200,7 @@ const mainComputeFn = tgpu['~unstable']
197200
(f32(sy) + 0.5) * ONE_OVER_SUPER_SAMPLES,
198201
);
199202
200-
acc += renderSubPixel(vec2f(gid.xy) + offset);
203+
acc += renderSubPixel(vec2f(input.gid.xy) + offset);
201204
}
202205
}
203206
@@ -212,7 +215,7 @@ const mainComputeFn = tgpu['~unstable']
212215
new_render = (prev_render * prev_layers + vec4(acc, 1.0)) / (prev_layers + 1);
213216
}
214217
215-
textureStore(mainOutput, gid.xy, new_render);
218+
textureStore(mainOutput, input.gid.xy, new_render);
216219
}`)
217220
.$uses({
218221
SUPER_SAMPLES,
@@ -232,8 +235,11 @@ const auxLayout = tgpu
232235
.$name('SDF Renderer: Aux Bind Group Layout');
233236

234237
const auxComputeFn = tgpu['~unstable']
235-
.computeFn([], { workgroupSize: [BlockSize, BlockSize] })
236-
.does(/* wgsl */ `(@builtin(global_invocation_id) GlobalInvocationID: vec3<u32>) {
238+
.computeFn({
239+
workgroupSize: [BlockSize, BlockSize],
240+
in: { gid: d.builtin.globalInvocationId },
241+
})
242+
.does(/* wgsl */ `(input: Input) {
237243
let offset = vec2f(
238244
0.5,
239245
0.5,
@@ -243,7 +249,7 @@ const auxComputeFn = tgpu['~unstable']
243249
var shape_ctx: ShapeContext;
244250
shape_ctx.rayPos = constructRayPos();
245251
shape_ctx.rayDir = constructRayDir(
246-
vec2f(GlobalInvocationID.xy) + offset
252+
vec2f(input.gid.xy) + offset
247253
);
248254
shape_ctx.rayDistance = 0.;
249255
@@ -282,7 +288,7 @@ const auxComputeFn = tgpu['~unstable']
282288
283289
// TODO: maybe apply gamma correction to the albedo luminance parameter??
284290
285-
textureStore(auxOutput, GlobalInvocationID.xy, aux);
291+
textureStore(auxOutput, input.gid.xy, aux);
286292
}`)
287293
.$uses({
288294
MAX_STEPS: MarchParams.maxSteps,

apps/phoure-www/src/lib/GameEngine/sdfRenderer/worldSdf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const worldMat = tgpu['~unstable']
9090

9191
if (d_left_blob <= sd) {
9292
// left blob
93-
out.albedo = d.vec3f(0.2, 0.2, 1);
93+
out.albedo = d.vec3f(1, 0.5, 0.2);
9494
out.roughness = 0.95;
9595
} else if (d_center_blob <= sd) {
9696
// test light

0 commit comments

Comments
 (0)