Skip to content

Commit 4450ec9

Browse files
committed
Migrated more functions to TGSL
1 parent 15c44ff commit 4450ec9

File tree

7 files changed

+71
-81
lines changed

7 files changed

+71
-81
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ export const getCameraProps = tgpu['~unstable']
3030

3131
export const constructRayPos = tgpu['~unstable']
3232
.fn(
33-
{},
33+
[],
3434
d.vec3f,
35-
)(/* wgsl */ `{
35+
)(/* wgsl */ `() -> vec3f {
3636
let camera = getCameraProps;
3737
return (camera.inv_view_matrix * vec4(0., 0., 0., 1.)).xyz;
3838
}`)
Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { rgbToYcbcrMatrix, ycbcrToRgbMatrix } from '@typegpu/color';
22
import { accessViewportSize } from '@typegpu/common';
33
import tgpu from 'typegpu';
4+
import * as std from 'typegpu/std';
45
import * as d from 'typegpu/data';
56

67
export const combinationLayout = tgpu
@@ -10,40 +11,26 @@ export const combinationLayout = tgpu
1011
})
1112
.$name('combinationLayout');
1213

13-
const { blurredTexture, mendedBuffer } = combinationLayout.bound;
14+
const layout = combinationLayout;
1415

15-
export const combinationEntryFn = tgpu['~unstable']
16-
.fragmentFn({
17-
in: { coord_f: d.builtin.position, uv: d.vec2f },
18-
out: d.vec4f,
19-
})(/* wgsl */ `{
20-
let coord = vec2u(floor(in.coord_f.xy));
16+
export const combinationEntryFn = tgpu['~unstable'].fragmentFn({
17+
in: { coord_f: d.builtin.position, uv: d.vec2f },
18+
out: d.vec4f,
19+
})((input) => {
20+
const coord = d.vec2u(std.floor(input.coord_f.xy));
21+
const blurred = std.textureLoad(layout.$.blurredTexture, coord, 0);
22+
const blurred_ycbcr = std.mul(blurred.xyz, rgbToYcbcrMatrix.value);
2123

22-
let blurred = textureLoad(
23-
blurredTexture,
24-
coord,
25-
0
26-
);
27-
28-
let blurred_ycbcr = blurred.rgb * rgbToYcbcrMatrix;
29-
30-
let buffer_idx = coord.y * u32(accessViewportSize.x) + coord.x;
31-
let mended_lumi = mendedBuffer[buffer_idx];
32-
33-
let combined_ycbcr = vec3f(
34-
blurred_ycbcr.r + mended_lumi, // Y
35-
blurred_ycbcr.g, // Cb
36-
blurred_ycbcr.b, // Cr
37-
);
38-
39-
let combined = combined_ycbcr * ycbcrToRgbMatrix;
40-
41-
return vec4f(combined, 1.0);
42-
}`)
43-
.$uses({
44-
rgbToYcbcrMatrix,
45-
ycbcrToRgbMatrix,
46-
blurredTexture,
47-
mendedBuffer,
48-
accessViewportSize,
49-
});
24+
const buffer_idx = coord.y * d.u32(accessViewportSize.value.x) + coord.x;
25+
const mended_lumi = layout.$.mendedBuffer[buffer_idx];
26+
27+
const combined_ycbcr = d.vec3f(
28+
blurred_ycbcr.x + mended_lumi, // Y
29+
blurred_ycbcr.y, // Cb
30+
blurred_ycbcr.z, // Cr
31+
);
32+
33+
const combined = std.mul(combined_ycbcr, ycbcrToRgbMatrix.value);
34+
35+
return d.vec4f(combined, 1.0);
36+
});

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,17 @@ const sampleGlobal = tgpu['~unstable'].derived(() => {
109109
.$name('sample_global');
110110
});
111111

112+
const f32Array = (n: number) => d.arrayOf(d.f32, n);
113+
112114
const applyReLU = tgpu['~unstable'].derived(() => {
115+
const outChannels = outChannelsSlot.value;
116+
113117
return tgpu['~unstable']
114-
.fn([
115-
d.ptrFn(d.arrayOf(d.f32, outChannelsSlot.value)),
116-
])(`(result: ptr<function, array<f32, outChannelsSlot>>) {
117-
for (var i = 0u; i < outChannelsSlot; i++) {
118-
(*result)[i] = max(0, (*result)[i]);
118+
.fn([d.ptrFn(f32Array(outChannels))])((result) => {
119+
for (let i = 0; i < outChannels; i++) {
120+
result[i] = std.max(0, result[i]);
119121
}
120-
}`)
121-
.$uses({ outChannelsSlot })
122+
})
122123
.$name('apply_relu');
123124
});
124125

apps/phoure-www/src/lib-ray-marching/marchSdf.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ export const MarchResult = d.struct({
2727
position: d.vec3f,
2828
});
2929

30-
export const march = tgpu['~unstable'].fn({
31-
ctx: d.ptrFn(ShapeContext),
32-
limit: d.u32,
33-
out: d.ptrFn(MarchResult),
34-
})`{
30+
export const march = tgpu['~unstable'].fn([
31+
d.ptrFn(ShapeContext),
32+
d.u32,
33+
d.ptrFn(MarchResult),
34+
])`(ctx: ptr<function, ShapeContext>, limit: u32, out: ptr<function, MarchResult>) {
3535
var pos = (*ctx).rayPos;
3636
var prev_dist = -1.;
3737
var min_dist: f32 = MarchParams.farPlane;
@@ -78,5 +78,7 @@ export const march = tgpu['~unstable'].fn({
7878
(*out).steps = step;
7979
}
8080
`.$uses({
81+
ShapeContext,
82+
MarchResult,
8183
MarchParams,
8284
});

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,12 @@ export const accumulatedLayersAtom = atom(0);
5050
* @param mat_roughness
5151
*/
5252
const reflect = tgpu['~unstable']
53-
.fn(
54-
{
55-
rayDir: d.vec3f,
56-
normal: d.vec3f,
57-
matRoughness: d.f32,
58-
outRoughness: d.ptrFn(d.f32),
59-
},
53+
.fn([
54+
d.vec3f,
6055
d.vec3f,
61-
)(`{
56+
d.f32,
57+
d.ptrFn(d.f32),
58+
])(`(rayDir: vec3f, normal: vec3f, matRoughness: f32, outRoughness: ptr<function, f32>) -> vec3f {
6259
let slope = dot(rayDir, normal);
6360
let dn2 = 2. * slope;
6461
let refl_dir = rayDir - dn2 * normal;

pnpm-lock.yaml

Lines changed: 26 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ packages:
44

55
# Define a catalog of version ranges.
66
catalog:
7-
typegpu: ^0.5.7
7+
typegpu: https://pkg.pr.new/software-mansion/TypeGPU/typegpu@4b33dc38310dd6df3d00f3ddc6cc4bae09bfd3d8
88
unplugin-typegpu: ^0.1.1

0 commit comments

Comments
 (0)