Skip to content

Commit a860b36

Browse files
committed
Simplify more code
1 parent 680f4f3 commit a860b36

File tree

2 files changed

+63
-78
lines changed

2 files changed

+63
-78
lines changed

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

Lines changed: 49 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { convertRgbToY } from '@typegpu/color';
21
import { accessViewportSize } from '@typegpu/common';
2+
import { convertRgbToY } from '@typegpu/color';
33
import tgpu, { type TgpuRoot } from 'typegpu';
44
import * as d from 'typegpu/data';
55
import * as std from 'typegpu/std';
@@ -66,62 +66,58 @@ const ioLayout = tgpu.bindGroupLayout({
6666
const { weights, biases } = layerLayout.bound;
6767

6868
const sampleGlobal = tgpu['~unstable'].derived(() => {
69-
return tgpu
70-
.fn([d.i32, d.i32, d.ptrFn(d.arrayOf(d.vec4f, inChannelsQuarter.value))])(
71-
(x, y, result) => {
72-
const canvasSize = accessViewportSize.value;
73-
const coord = d.vec2u(
74-
d.u32(std.max(0, std.min(x, d.i32(canvasSize.x) - 1))),
75-
d.u32(std.max(0, std.min(y, d.i32(canvasSize.y) - 1))),
76-
);
77-
78-
if (inputFromGBufferSlot.$) {
79-
const blurred = std.textureLoad(ioLayout.$.blurred_tex, coord, 0);
80-
81-
const aux = std.textureLoad(ioLayout.$.aux_tex, coord, 0);
82-
83-
result[0] = d.vec4f(
84-
convertRgbToY(blurred.xyz),
85-
aux.z, // albedo luminance
86-
aux.x, // normal.x
87-
aux.y, // normal.y
88-
);
89-
result[1] = d.vec4f(
90-
aux.w, // emission luminance
91-
0, // zero padding
92-
0, // zero padding
93-
0, // zero padding
94-
);
95-
} else {
96-
for (let i = d.u32(0); i < inChannelsQuarter.value; i++) {
97-
const index =
98-
(coord.y * d.u32(canvasSize.x) + coord.x) *
99-
inChannelsQuarter.value +
100-
i;
101-
102-
result[i] = ioLayout.$.input_buffer[index];
103-
}
104-
}
105-
},
106-
)
107-
.$name('sample_global');
69+
return tgpu.fn([
70+
d.i32,
71+
d.i32,
72+
d.ptrFn(d.arrayOf(d.vec4f, inChannelsQuarter.$)),
73+
])((x, y, result) => {
74+
const canvasSize = accessViewportSize.$;
75+
const coord = d.vec2u(
76+
d.u32(std.max(0, std.min(x, d.i32(canvasSize.x) - 1))),
77+
d.u32(std.max(0, std.min(y, d.i32(canvasSize.y) - 1))),
78+
);
79+
80+
if (inputFromGBufferSlot.$) {
81+
const blurred = std.textureLoad(ioLayout.$.blurred_tex, coord, 0);
82+
83+
const aux = std.textureLoad(ioLayout.$.aux_tex, coord, 0);
84+
85+
result[0] = d.vec4f(
86+
convertRgbToY(blurred.xyz),
87+
aux.z, // albedo luminance
88+
aux.x, // normal.x
89+
aux.y, // normal.y
90+
);
91+
result[1] = d.vec4f(
92+
aux.w, // emission luminance
93+
0, // zero padding
94+
0, // zero padding
95+
0, // zero padding
96+
);
97+
} else {
98+
for (let i = d.u32(0); i < inChannelsQuarter.$; i++) {
99+
const index =
100+
(coord.y * d.u32(canvasSize.x) + coord.x) * inChannelsQuarter.$ + i;
101+
102+
result[i] = ioLayout.$.input_buffer[index];
103+
}
104+
}
105+
});
108106
});
109107

110108
const f32Array = (n: number) => d.arrayOf(d.f32, n);
111109

112110
const applyReLU = tgpu['~unstable'].derived(() => {
113-
const outChannels = outChannelsSlot.value;
111+
const outChannels = outChannelsSlot.$;
114112

115-
return tgpu['~unstable']
116-
.fn([d.ptrFn(f32Array(outChannels))])((result) => {
117-
for (let i = 0; i < outChannels; i++) {
118-
result[i] = std.max(0, result[i]);
119-
}
120-
})
121-
.$name('apply_relu');
113+
return tgpu.fn([d.ptrFn(f32Array(outChannels))])((result) => {
114+
for (let i = 0; i < outChannels; i++) {
115+
result[i] = std.max(0, result[i]);
116+
}
117+
});
122118
});
123119

124-
const readKernel = tgpu.fn([d.u32], d.vec4f)((idx) => weights.value[idx]);
120+
const readKernel = tgpu.fn([d.u32], d.vec4f)((idx) => weights.$[idx]);
125121

126122
const menderConvolveFn = convolveFn({
127123
sampleFiller: sampleGlobal,
@@ -168,27 +164,24 @@ const entryComputeFn = tgpu['~unstable'].computeFn({
168164
export const MenderStep = ({ root, gBuffer, targetTexture }: Options) => {
169165
// Resource locators
170166

171-
const viewportSizeBuffer = root.createBuffer(d.vec2f).$usage('uniform');
167+
const viewportSizeUniform = root.createUniform(d.vec2f);
172168

173169
// Textures
174170

175171
const firstWorkBuffer = root
176172
.createBuffer(
177173
d.arrayOf(d.f32, gBuffer.size[0] * gBuffer.size[1] * FIRST_DEPTH),
178174
)
179-
.$name('First Work Buffer')
180175
.$usage('storage');
181176

182177
const secondWorkBuffer = root
183178
.createBuffer(
184179
d.arrayOf(d.f32, gBuffer.size[0] * gBuffer.size[1] * SECOND_DEPTH),
185180
)
186-
.$name('Second Work Buffer')
187181
.$usage('storage');
188182

189183
const mendedResultBuffer = root
190184
.createBuffer(d.arrayOf(d.f32, gBuffer.size[0] * gBuffer.size[1]))
191-
.$name('Mender Result Buffer')
192185
.$usage('storage');
193186

194187
//
@@ -217,7 +210,7 @@ export const MenderStep = ({ root, gBuffer, targetTexture }: Options) => {
217210
.with(outChannelsSlot, options.outChannels)
218211
.with(reluSlot, options.relu)
219212
.with(inputFromGBufferSlot, options.inputFromGBuffer)
220-
.with(accessViewportSize, viewportSizeBuffer.as('uniform'))
213+
.with(accessViewportSize, viewportSizeUniform)
221214
// ---
222215
.withCompute(entryComputeFn)
223216
.createPipeline()
@@ -281,7 +274,7 @@ export const MenderStep = ({ root, gBuffer, targetTexture }: Options) => {
281274
// ---
282275

283276
const combinationPipeline = root['~unstable']
284-
.with(accessViewportSize, viewportSizeBuffer.as('uniform'))
277+
.with(accessViewportSize, viewportSizeUniform)
285278
.withVertex(fullScreenQuadVertexFn, {})
286279
.withFragment(combinationEntryFn, { format: 'rgba8unorm' })
287280
.createPipeline();
@@ -291,7 +284,7 @@ export const MenderStep = ({ root, gBuffer, targetTexture }: Options) => {
291284
mendedBuffer: mendedResultBuffer,
292285
});
293286

294-
viewportSizeBuffer.write(d.vec2f(...gBuffer.size));
287+
viewportSizeUniform.write(d.vec2f(...gBuffer.size));
295288

296289
return {
297290
perform() {

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

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
1+
import * as d from 'typegpu/data';
12
import tgpu, { type Eventual, type TgpuFn } from 'typegpu';
2-
import {
3-
arrayOf,
4-
f32,
5-
type I32,
6-
type Ptr,
7-
ptrFn,
8-
type U32,
9-
vec2u,
10-
type Vec4f,
11-
type WgslArray,
12-
} from 'typegpu/data';
133

144
export type SampleFiller = TgpuFn<
15-
(x: I32, y: I32, outSamplerPtr: Ptr<'function', WgslArray<Vec4f>>) => void
5+
(
6+
x: d.I32,
7+
y: d.I32,
8+
outSamplerPtr: d.Ptr<'function', d.WgslArray<d.Vec4f>, 'read-write'>,
9+
) => d.Void
1610
>;
17-
export type KernelReader = TgpuFn<(idx: U32) => Vec4f>;
11+
export type KernelReader = TgpuFn<(idx: d.U32) => d.Vec4f>;
1812

19-
/**
20-
* Has to be divisible by 4
21-
*/
22-
export const inChannelsSlot = tgpu.slot<number>().$name('in_channels');
23-
export const outChannelsSlot = tgpu.slot<number>().$name('out_channels');
24-
export const kernelRadiusSlot = tgpu.slot<number>().$name('kernel_radius');
25-
const sampleFillerSlot = tgpu.slot<SampleFiller>().$name('sample_filler');
26-
const kernelReaderSlot = tgpu.slot<KernelReader>().$name('kernel_reader');
13+
/** Has to be divisible by 4 */
14+
export const inChannelsSlot = tgpu.slot<number>();
15+
export const outChannelsSlot = tgpu.slot<number>();
16+
export const kernelRadiusSlot = tgpu.slot<number>();
17+
const sampleFillerSlot = tgpu.slot<SampleFiller>();
18+
const kernelReaderSlot = tgpu.slot<KernelReader>();
2719

2820
export const inChannelsQuarter = tgpu['~unstable'].derived(() => {
2921
if (inChannelsSlot.value % 4 !== 0) {
@@ -34,7 +26,7 @@ export const inChannelsQuarter = tgpu['~unstable'].derived(() => {
3426

3527
const _convolveFn = tgpu['~unstable'].derived(() => {
3628
return tgpu
37-
.fn([vec2u, ptrFn(arrayOf(f32, outChannelsSlot.value))])(
29+
.fn([d.vec2u, d.ptrFn(d.arrayOf(d.f32, outChannelsSlot.value))])(
3830
/* wgsl */ `(coord: vec2u, result: ptr<function, array<f32, outChannels>>) {
3931
var sample = array<vec4f, inChannelsQuarter>();
4032

0 commit comments

Comments
 (0)