Skip to content

Commit fd67137

Browse files
aleksanderkatanAleksander Katan
andauthored
refactor: Remove name method calls from examples and tests (#1407)
* Clean up the examples * Lint * Clean up some tests * Clean up more tests * Reformat 3d Fish, add names in WGSL Resolution * Updated lint --------- Co-authored-by: Aleksander Katan <[email protected]>
1 parent 8105074 commit fd67137

File tree

30 files changed

+775
-906
lines changed

30 files changed

+775
-906
lines changed

apps/typegpu-docs/src/content/examples/image-processing/camera-thresholding/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,7 @@ const sampler = root.device.createSampler({
8686
minFilter: 'linear',
8787
});
8888

89-
const thresholdBuffer = root
90-
.createBuffer(d.f32)
91-
.$name('threshold')
92-
.$usage('uniform');
89+
const thresholdBuffer = root.createBuffer(d.f32).$usage('uniform');
9390

9491
const rareBindGroup = root.createBindGroup(rareLayout, {
9592
sampling: sampler,

apps/typegpu-docs/src/content/examples/image-processing/chroma-keying-next/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,10 @@ const mediaProcessor = new MediaStreamTrackProcessor({
127127
});
128128
const reader = mediaProcessor.readable.getReader();
129129

130-
const thresholdBuffer = root
131-
.createBuffer(d.f32, 0.5)
132-
.$name('threshold')
133-
.$usage('uniform');
130+
const thresholdBuffer = root.createBuffer(d.f32, 0.5).$usage('uniform');
134131

135132
const colorBuffer = root
136133
.createBuffer(d.vec3f, d.vec3f(0, 1.0, 0))
137-
.$name('color')
138134
.$usage('uniform');
139135

140136
const sampler = device.createSampler({

apps/typegpu-docs/src/content/examples/image-processing/chroma-keying/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,10 @@ const mediaProcessor = new MediaStreamTrackProcessor({
131131
});
132132
const reader = mediaProcessor.readable.getReader();
133133

134-
const thresholdBuffer = root
135-
.createBuffer(d.f32, 0.5)
136-
.$name('threshold')
137-
.$usage('uniform');
134+
const thresholdBuffer = root.createBuffer(d.f32, 0.5).$usage('uniform');
138135

139136
const colorBuffer = root
140137
.createBuffer(d.vec3f, d.vec3f(0, 1.0, 0))
141-
.$name('color')
142138
.$usage('uniform');
143139

144140
const sampler = device.createSampler({

apps/typegpu-docs/src/content/examples/rendering/3d-fish/compute.ts

Lines changed: 113 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -5,132 +5,130 @@ import * as p from './params.ts';
55
import { computeBindGroupLayout as layout, ModelData } from './schemas.ts';
66
import { projectPointOnLine } from './tgsl-helpers.ts';
77

8-
export const computeShader = tgpu['~unstable']
9-
.computeFn({
10-
in: { gid: d.builtin.globalInvocationId },
11-
workgroupSize: [p.workGroupSize],
12-
})((input) => {
13-
const fishIndex = input.gid.x;
14-
// TODO: replace it with struct copy when Chromium is fixed
15-
const fishData = ModelData({
16-
position: layout.$.currentFishData[fishIndex].position,
17-
direction: layout.$.currentFishData[fishIndex].direction,
18-
scale: layout.$.currentFishData[fishIndex].scale,
19-
variant: layout.$.currentFishData[fishIndex].variant,
20-
applySeaDesaturation:
21-
layout.$.currentFishData[fishIndex].applySeaDesaturation,
22-
applySeaFog: layout.$.currentFishData[fishIndex].applySeaFog,
23-
applySinWave: layout.$.currentFishData[fishIndex].applySinWave,
24-
});
25-
let separation = d.vec3f();
26-
let alignment = d.vec3f();
27-
let alignmentCount = 0;
28-
let cohesion = d.vec3f();
29-
let cohesionCount = 0;
30-
let wallRepulsion = d.vec3f();
31-
let rayRepulsion = d.vec3f();
8+
export const computeShader = tgpu['~unstable'].computeFn({
9+
in: { gid: d.builtin.globalInvocationId },
10+
workgroupSize: [p.workGroupSize],
11+
})((input) => {
12+
const fishIndex = input.gid.x;
13+
// TODO: replace it with struct copy when Chromium is fixed
14+
const fishData = ModelData({
15+
position: layout.$.currentFishData[fishIndex].position,
16+
direction: layout.$.currentFishData[fishIndex].direction,
17+
scale: layout.$.currentFishData[fishIndex].scale,
18+
variant: layout.$.currentFishData[fishIndex].variant,
19+
applySeaDesaturation:
20+
layout.$.currentFishData[fishIndex].applySeaDesaturation,
21+
applySeaFog: layout.$.currentFishData[fishIndex].applySeaFog,
22+
applySinWave: layout.$.currentFishData[fishIndex].applySinWave,
23+
});
24+
let separation = d.vec3f();
25+
let alignment = d.vec3f();
26+
let alignmentCount = 0;
27+
let cohesion = d.vec3f();
28+
let cohesionCount = 0;
29+
let wallRepulsion = d.vec3f();
30+
let rayRepulsion = d.vec3f();
3231

33-
for (let i = 0; i < p.fishAmount; i += 1) {
34-
if (d.u32(i) === fishIndex) {
35-
continue;
36-
}
32+
for (let i = 0; i < p.fishAmount; i += 1) {
33+
if (d.u32(i) === fishIndex) {
34+
continue;
35+
}
3736

38-
// TODO: replace it with struct copy when Chromium is fixed
39-
const other = ModelData({
40-
position: layout.$.currentFishData[i].position,
41-
direction: layout.$.currentFishData[i].direction,
42-
scale: layout.$.currentFishData[i].scale,
43-
variant: layout.$.currentFishData[i].variant,
44-
applySeaDesaturation: layout.$.currentFishData[i].applySeaDesaturation,
45-
applySeaFog: layout.$.currentFishData[i].applySeaFog,
46-
applySinWave: layout.$.currentFishData[i].applySinWave,
47-
});
48-
const dist = std.length(std.sub(fishData.position, other.position));
49-
if (dist < layout.$.fishBehavior.separationDist) {
50-
separation = std.add(
51-
separation,
52-
std.sub(fishData.position, other.position),
53-
);
54-
}
55-
if (dist < layout.$.fishBehavior.alignmentDist) {
56-
alignment = std.add(alignment, other.direction);
57-
alignmentCount = alignmentCount + 1;
58-
}
59-
if (dist < layout.$.fishBehavior.cohesionDist) {
60-
cohesion = std.add(cohesion, other.position);
61-
cohesionCount = cohesionCount + 1;
62-
}
37+
// TODO: replace it with struct copy when Chromium is fixed
38+
const other = ModelData({
39+
position: layout.$.currentFishData[i].position,
40+
direction: layout.$.currentFishData[i].direction,
41+
scale: layout.$.currentFishData[i].scale,
42+
variant: layout.$.currentFishData[i].variant,
43+
applySeaDesaturation: layout.$.currentFishData[i].applySeaDesaturation,
44+
applySeaFog: layout.$.currentFishData[i].applySeaFog,
45+
applySinWave: layout.$.currentFishData[i].applySinWave,
46+
});
47+
const dist = std.length(std.sub(fishData.position, other.position));
48+
if (dist < layout.$.fishBehavior.separationDist) {
49+
separation = std.add(
50+
separation,
51+
std.sub(fishData.position, other.position),
52+
);
6353
}
64-
if (alignmentCount > 0) {
65-
alignment = std.mul(1 / d.f32(alignmentCount), alignment);
54+
if (dist < layout.$.fishBehavior.alignmentDist) {
55+
alignment = std.add(alignment, other.direction);
56+
alignmentCount = alignmentCount + 1;
6657
}
67-
if (cohesionCount > 0) {
68-
cohesion = std.sub(
69-
std.mul(1 / d.f32(cohesionCount), cohesion),
70-
fishData.position,
71-
);
58+
if (dist < layout.$.fishBehavior.cohesionDist) {
59+
cohesion = std.add(cohesion, other.position);
60+
cohesionCount = cohesionCount + 1;
7261
}
73-
for (let i = 0; i < 3; i += 1) {
74-
const repulsion = d.vec3f();
75-
repulsion[i] = 1.0;
76-
77-
const axisAquariumSize = p.aquariumSize[i] / 2;
78-
const axisPosition = fishData.position[i];
79-
const distance = p.fishWallRepulsionDistance;
62+
}
63+
if (alignmentCount > 0) {
64+
alignment = std.mul(1 / d.f32(alignmentCount), alignment);
65+
}
66+
if (cohesionCount > 0) {
67+
cohesion = std.sub(
68+
std.mul(1 / d.f32(cohesionCount), cohesion),
69+
fishData.position,
70+
);
71+
}
72+
for (let i = 0; i < 3; i += 1) {
73+
const repulsion = d.vec3f();
74+
repulsion[i] = 1.0;
8075

81-
if (axisPosition > axisAquariumSize - distance) {
82-
const str = axisPosition - (axisAquariumSize - distance);
83-
wallRepulsion = std.sub(wallRepulsion, std.mul(str, repulsion));
84-
}
76+
const axisAquariumSize = p.aquariumSize[i] / 2;
77+
const axisPosition = fishData.position[i];
78+
const distance = p.fishWallRepulsionDistance;
8579

86-
if (axisPosition < -axisAquariumSize + distance) {
87-
const str = -axisAquariumSize + distance - axisPosition;
88-
wallRepulsion = std.add(wallRepulsion, std.mul(str, repulsion));
89-
}
80+
if (axisPosition > axisAquariumSize - distance) {
81+
const str = axisPosition - (axisAquariumSize - distance);
82+
wallRepulsion = std.sub(wallRepulsion, std.mul(str, repulsion));
9083
}
9184

92-
if (layout.$.mouseRay.activated === 1) {
93-
const proj = projectPointOnLine(
94-
fishData.position,
95-
layout.$.mouseRay.line,
96-
);
97-
const diff = std.sub(fishData.position, proj);
98-
const limit = p.fishMouseRayRepulsionDistance;
99-
const str = std.pow(2, std.clamp(limit - std.length(diff), 0, limit)) - 1;
100-
rayRepulsion = std.mul(str, std.normalize(diff));
85+
if (axisPosition < -axisAquariumSize + distance) {
86+
const str = -axisAquariumSize + distance - axisPosition;
87+
wallRepulsion = std.add(wallRepulsion, std.mul(str, repulsion));
10188
}
89+
}
10290

103-
fishData.direction = std.add(
104-
fishData.direction,
105-
std.mul(layout.$.fishBehavior.separationStr, separation),
106-
);
107-
fishData.direction = std.add(
108-
fishData.direction,
109-
std.mul(layout.$.fishBehavior.alignmentStr, alignment),
110-
);
111-
fishData.direction = std.add(
112-
fishData.direction,
113-
std.mul(layout.$.fishBehavior.cohesionStr, cohesion),
114-
);
115-
fishData.direction = std.add(
116-
fishData.direction,
117-
std.mul(p.fishWallRepulsionStrength, wallRepulsion),
118-
);
119-
fishData.direction = std.add(
120-
fishData.direction,
121-
std.mul(p.fishMouseRayRepulsionStrength, rayRepulsion),
91+
if (layout.$.mouseRay.activated === 1) {
92+
const proj = projectPointOnLine(
93+
fishData.position,
94+
layout.$.mouseRay.line,
12295
);
96+
const diff = std.sub(fishData.position, proj);
97+
const limit = p.fishMouseRayRepulsionDistance;
98+
const str = std.pow(2, std.clamp(limit - std.length(diff), 0, limit)) - 1;
99+
rayRepulsion = std.mul(str, std.normalize(diff));
100+
}
123101

124-
fishData.direction = std.mul(
125-
std.clamp(std.length(fishData.direction), 0.0, 0.01),
126-
std.normalize(fishData.direction),
127-
);
102+
fishData.direction = std.add(
103+
fishData.direction,
104+
std.mul(layout.$.fishBehavior.separationStr, separation),
105+
);
106+
fishData.direction = std.add(
107+
fishData.direction,
108+
std.mul(layout.$.fishBehavior.alignmentStr, alignment),
109+
);
110+
fishData.direction = std.add(
111+
fishData.direction,
112+
std.mul(layout.$.fishBehavior.cohesionStr, cohesion),
113+
);
114+
fishData.direction = std.add(
115+
fishData.direction,
116+
std.mul(p.fishWallRepulsionStrength, wallRepulsion),
117+
);
118+
fishData.direction = std.add(
119+
fishData.direction,
120+
std.mul(p.fishMouseRayRepulsionStrength, rayRepulsion),
121+
);
128122

129-
const translation = std.mul(
130-
d.f32(std.min(999, layout.$.timePassed)) / 8,
131-
fishData.direction,
132-
);
133-
fishData.position = std.add(fishData.position, translation);
134-
layout.$.nextFishData[fishIndex] = fishData;
135-
})
136-
.$name('compute shader');
123+
fishData.direction = std.mul(
124+
std.clamp(std.length(fishData.direction), 0.0, 0.01),
125+
std.normalize(fishData.direction),
126+
);
127+
128+
const translation = std.mul(
129+
d.f32(std.min(999, layout.$.timePassed)) / 8,
130+
fishData.direction,
131+
);
132+
fishData.position = std.add(fishData.position, translation);
133+
layout.$.nextFishData[fishIndex] = fishData;
134+
});

apps/typegpu-docs/src/content/examples/rendering/3d-fish/index.ts

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -137,33 +137,22 @@ const camera = {
137137
),
138138
};
139139

140-
const cameraBuffer = root
141-
.createBuffer(Camera, camera)
142-
.$usage('uniform')
143-
.$name('camera');
140+
const cameraBuffer = root.createBuffer(Camera, camera).$usage('uniform');
144141

145142
const mouseRayBuffer = root
146143
.createBuffer(MouseRay, {
147144
activated: 0,
148145
line: Line3({ origin: d.vec3f(), dir: d.vec3f() }),
149146
})
150-
.$usage('uniform')
151-
.$name('mouse');
147+
.$usage('uniform');
152148

153-
const timePassedBuffer = root
154-
.createBuffer(d.f32)
155-
.$usage('uniform')
156-
.$name('time passed');
149+
const timePassedBuffer = root.createBuffer(d.f32).$usage('uniform');
157150

158-
const currentTimeBuffer = root
159-
.createBuffer(d.f32)
160-
.$usage('uniform')
161-
.$name('current time');
151+
const currentTimeBuffer = root.createBuffer(d.f32).$usage('uniform');
162152

163153
const fishBehaviorBuffer = root
164154
.createBuffer(FishBehaviorParams, presets.default)
165-
.$usage('uniform')
166-
.$name('fish behavior');
155+
.$usage('uniform');
167156

168157
const oceanFloorDataBuffer = root
169158
.createBuffer(ModelDataArray(1), [
@@ -177,8 +166,7 @@ const oceanFloorDataBuffer = root
177166
applySeaDesaturation: 0,
178167
},
179168
])
180-
.$usage('storage', 'vertex')
181-
.$name('ocean floor');
169+
.$usage('storage', 'vertex');
182170

183171
randomizeFishPositions();
184172

@@ -193,8 +181,7 @@ const renderPipeline = root['~unstable']
193181
depthCompare: 'less',
194182
})
195183
.withPrimitive({ topology: 'triangle-list' })
196-
.createPipeline()
197-
.$name('render');
184+
.createPipeline();
198185

199186
let depthTexture = root.device.createTexture({
200187
size: [canvas.width, canvas.height, 1],
@@ -204,8 +191,7 @@ let depthTexture = root.device.createTexture({
204191

205192
const computePipeline = root['~unstable']
206193
.withCompute(computeShader)
207-
.createPipeline()
208-
.$name('compute');
194+
.createPipeline();
209195

210196
// bind groups
211197

0 commit comments

Comments
 (0)