Skip to content

Commit 8008acc

Browse files
committed
refactor to create buffers once inside index, remove console.logs, recreate test file
1 parent 5086460 commit 8008acc

File tree

6 files changed

+202
-122
lines changed

6 files changed

+202
-122
lines changed
Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,88 @@
11
import * as d from 'typegpu/data';
2-
import tgpu from 'typegpu';
2+
import tgpu, { type TgpuRoot } from 'typegpu';
33
import { colors } from './geometry.ts';
4-
import { root } from './root.ts';
54
import { createInstanceInfoArray, InstanceInfoArray } from './instanceInfo.ts';
65
import {
76
getGridParams,
87
INITIAL_MIDDLE_SQUARE_SCALE,
98
INITIAL_STEP_ROTATION,
109
} from './params.ts';
1110

12-
const animationProgressUniform = root.createUniform(d.f32);
13-
14-
const shiftedColorsBuffer = root.createReadonly(d.arrayOf(d.vec4f, 3), [
15-
...colors,
16-
]);
11+
const stepRotationAccess = tgpu['~unstable'].accessor(d.f32);
12+
const shiftedColorsAccess = tgpu['~unstable'].accessor(d.arrayOf(d.vec4f, 3));
13+
const animationProgressAccess = tgpu['~unstable'].accessor(d.f32);
14+
const middleSquareScaleAccess = tgpu['~unstable'].accessor(d.f32);
15+
const drawOverNeighborsAccess = tgpu['~unstable'].accessor(d.u32);
16+
const scaleAccess = tgpu['~unstable'].accessor(d.f32);
17+
const aspectRatioAccess = tgpu['~unstable'].accessor(d.f32);
1718

1819
const instanceInfoLayout = tgpu.bindGroupLayout({
1920
instanceInfo: { storage: InstanceInfoArray },
2021
});
2122

22-
let instanceInfoBindGroup = createInstanceInfoBufferAndBindGroup();
23+
function createBuffers(root: TgpuRoot) {
24+
const animationProgressUniform = root.createUniform(d.f32);
2325

24-
function getInstanceInfoBindGroup() {
25-
return instanceInfoBindGroup;
26-
}
26+
const shiftedColorsBuffer = root.createReadonly(d.arrayOf(d.vec4f, 3), [
27+
...colors,
28+
]);
2729

28-
function createInstanceInfoBufferAndBindGroup() {
29-
const instanceInfoBuffer = root.createReadonly(
30-
InstanceInfoArray(getGridParams().triangleCount),
31-
createInstanceInfoArray(),
32-
);
30+
let instanceInfoBindGroup = createInstanceInfoBufferAndBindGroup();
3331

34-
const instanceInfoBindGroup = root.createBindGroup(instanceInfoLayout, {
35-
instanceInfo: instanceInfoBuffer.buffer,
36-
});
32+
function getInstanceInfoBindGroup() {
33+
return instanceInfoBindGroup;
34+
}
3735

38-
return instanceInfoBindGroup;
39-
}
36+
function createInstanceInfoBufferAndBindGroup() {
37+
const instanceInfoBuffer = root.createReadonly(
38+
InstanceInfoArray(getGridParams().triangleCount),
39+
createInstanceInfoArray(),
40+
);
4041

41-
function updateInstanceInfoBufferAndBindGroup() {
42-
instanceInfoBindGroup = createInstanceInfoBufferAndBindGroup();
43-
}
42+
const instanceInfoBindGroup = root.createBindGroup(instanceInfoLayout, {
43+
instanceInfo: instanceInfoBuffer.buffer,
44+
});
4445

45-
const scaleBuffer = root.createUniform(d.f32, getGridParams().tileDensity);
46-
const aspectRatioBuffer = root.createUniform(d.f32, 1);
46+
return instanceInfoBindGroup;
47+
}
4748

48-
const stepRotationBuffer = root.createUniform(d.f32, INITIAL_STEP_ROTATION);
49+
function updateInstanceInfoBufferAndBindGroup() {
50+
instanceInfoBindGroup = createInstanceInfoBufferAndBindGroup();
51+
}
4952

50-
const middleSquareScaleBuffer = root.createUniform(
51-
d.f32,
52-
INITIAL_MIDDLE_SQUARE_SCALE,
53-
);
53+
const scaleBuffer = root.createUniform(d.f32, getGridParams().tileDensity);
54+
const aspectRatioBuffer = root.createUniform(d.f32, 1);
5455

55-
const drawOverNeighborsBuffer = root.createUniform(d.u32, 0);
56+
const stepRotationBuffer = root.createUniform(d.f32, INITIAL_STEP_ROTATION);
57+
58+
const middleSquareScaleBuffer = root.createUniform(
59+
d.f32,
60+
INITIAL_MIDDLE_SQUARE_SCALE,
61+
);
62+
63+
const drawOverNeighborsBuffer = root.createUniform(d.u32, 0);
64+
65+
return {
66+
animationProgressUniform,
67+
aspectRatioBuffer,
68+
drawOverNeighborsBuffer,
69+
getInstanceInfoBindGroup,
70+
middleSquareScaleBuffer,
71+
scaleBuffer,
72+
shiftedColorsBuffer,
73+
stepRotationBuffer,
74+
updateInstanceInfoBufferAndBindGroup,
75+
};
76+
}
5677

5778
export {
58-
animationProgressUniform,
59-
aspectRatioBuffer,
60-
drawOverNeighborsBuffer,
61-
getInstanceInfoBindGroup,
79+
animationProgressAccess,
80+
aspectRatioAccess,
81+
createBuffers,
82+
drawOverNeighborsAccess,
6283
instanceInfoLayout,
63-
middleSquareScaleBuffer,
64-
scaleBuffer,
65-
shiftedColorsBuffer,
66-
stepRotationBuffer,
67-
updateInstanceInfoBufferAndBindGroup,
84+
middleSquareScaleAccess,
85+
scaleAccess,
86+
shiftedColorsAccess,
87+
stepRotationAccess,
6888
};

apps/typegpu-docs/src/examples/simple/rotating-triangle-tiles/index.ts

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
import { colors } from './geometry.ts';
2-
import {
3-
animationProgressUniform,
4-
drawOverNeighborsBuffer,
5-
getInstanceInfoBindGroup,
6-
shiftedColorsBuffer,
7-
updateInstanceInfoBufferAndBindGroup,
8-
} from './buffers.ts';
92
import { createBezier } from './bezier.ts';
103
import { root } from './root.ts';
114
import {
@@ -30,13 +23,36 @@ import {
3023
updateGridParams,
3124
updateStepRotation,
3225
} from './params.ts';
26+
import {
27+
animationProgressAccess,
28+
aspectRatioAccess,
29+
createBuffers,
30+
drawOverNeighborsAccess,
31+
middleSquareScaleAccess,
32+
scaleAccess,
33+
shiftedColorsAccess,
34+
stepRotationAccess,
35+
} from './buffers.ts';
3336

3437
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
3538
const canvas = document.querySelector('canvas') as HTMLCanvasElement;
3639
const context = canvas.getContext('webgpu') as GPUCanvasContext;
3740

3841
let ease = createBezier(getCubicBezierControlPoints());
39-
updateAspectRatio(canvas.width, canvas.height);
42+
43+
const {
44+
aspectRatioBuffer,
45+
animationProgressUniform,
46+
drawOverNeighborsBuffer,
47+
getInstanceInfoBindGroup,
48+
scaleBuffer,
49+
shiftedColorsBuffer,
50+
middleSquareScaleBuffer,
51+
updateInstanceInfoBufferAndBindGroup,
52+
stepRotationBuffer,
53+
} = createBuffers(root);
54+
55+
updateAspectRatio(canvas.width, canvas.height, aspectRatioBuffer);
4056

4157
context.configure({
4258
device: root.device,
@@ -45,16 +61,30 @@ context.configure({
4561
});
4662

4763
const backgroundPipeline = root['~unstable']
64+
.with(shiftedColorsAccess, shiftedColorsBuffer)
4865
.withVertex(backgroundVertex)
4966
.withFragment(backgroundFragment, { format: presentationFormat })
5067
.createPipeline();
5168

5269
const midgroundPipeline = root['~unstable']
70+
.with(animationProgressAccess, animationProgressUniform)
71+
.with(stepRotationAccess, stepRotationBuffer)
72+
.with(drawOverNeighborsAccess, drawOverNeighborsBuffer)
73+
.with(shiftedColorsAccess, shiftedColorsBuffer)
74+
.with(middleSquareScaleAccess, middleSquareScaleBuffer)
75+
.with(scaleAccess, scaleBuffer)
76+
.with(aspectRatioAccess, aspectRatioBuffer)
5377
.withVertex(midgroundVertex)
5478
.withFragment(midgroundFragment, { format: presentationFormat })
5579
.createPipeline();
5680

5781
const foregroundPipeline = root['~unstable']
82+
.with(drawOverNeighborsAccess, drawOverNeighborsBuffer)
83+
.with(animationProgressAccess, animationProgressUniform)
84+
.with(stepRotationAccess, stepRotationBuffer)
85+
.with(shiftedColorsAccess, shiftedColorsBuffer)
86+
.with(scaleAccess, scaleBuffer)
87+
.with(aspectRatioAccess, aspectRatioBuffer)
5888
.withVertex(foregroundVertex)
5989
.withFragment(foregroundFragment, { format: presentationFormat })
6090
.createPipeline();
@@ -114,8 +144,8 @@ requestAnimationFrame(draw);
114144
// cleanup
115145

116146
const resizeObserver = new ResizeObserver(() => {
117-
updateAspectRatio(canvas.width, canvas.height);
118-
updateGridParams();
147+
updateAspectRatio(canvas.width, canvas.height, aspectRatioBuffer);
148+
updateGridParams(scaleBuffer, updateInstanceInfoBufferAndBindGroup);
119149
updateInstanceInfoBufferAndBindGroup();
120150
});
121151

@@ -136,7 +166,12 @@ export const controls = {
136166
min: 0.01,
137167
max: 1.33,
138168
step: 0.01,
139-
onSliderChange: updateGridParams,
169+
onSliderChange: (newValue: number) =>
170+
updateGridParams(
171+
scaleBuffer,
172+
updateInstanceInfoBufferAndBindGroup,
173+
newValue,
174+
),
140175
},
141176
'Animation duration': {
142177
initial: getAnimationDuration(),
@@ -148,7 +183,8 @@ export const controls = {
148183
'Rotation in degrees': {
149184
initial: INITIAL_STEP_ROTATION,
150185
options: ROTATION_OPTIONS,
151-
onSelectChange: updateStepRotation,
186+
onSelectChange: (newValue: number) =>
187+
updateStepRotation(newValue, stepRotationBuffer, middleSquareScaleBuffer),
152188
},
153189
'Draw over neighbors': {
154190
initial: false,

apps/typegpu-docs/src/examples/simple/rotating-triangle-tiles/params.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import * as d from 'typegpu/data';
2-
import {
3-
aspectRatioBuffer,
4-
middleSquareScaleBuffer,
5-
scaleBuffer,
6-
stepRotationBuffer,
7-
updateInstanceInfoBufferAndBindGroup,
8-
} from './buffers.ts';
92
import { MAGIC_NUMBER } from './geometry.ts';
3+
import type { TgpuUniform } from 'typegpu';
104

115
const INIT_TILE_DENSITY = 0.1;
126
const INITIAL_STEP_ROTATION = 60;
@@ -37,7 +31,6 @@ function parseControlPoints(value: string) {
3731

3832
function parseOneControlPoint(value: string, index: number) {
3933
const parsedNumber = Number(value);
40-
console.log(parsedNumber);
4134
if (isNaN(parsedNumber)) {
4235
throw Error('Cubic Bezier control point must be a number');
4336
}
@@ -50,7 +43,11 @@ function parseOneControlPoint(value: string, index: number) {
5043

5144
let aspectRatio = 1;
5245

53-
function updateAspectRatio(width: number, height: number) {
46+
function updateAspectRatio(
47+
width: number,
48+
height: number,
49+
aspectRatioBuffer: TgpuUniform<d.F32>,
50+
) {
5451
aspectRatio = width / height;
5552
aspectRatioBuffer.write(aspectRatio);
5653
}
@@ -72,7 +69,11 @@ const ROTATION_OPTIONS = ROTATION_TO_MIDDLE_SQUARE_SCALE_ARRAY.flatMap(
7269
(element) => element[0],
7370
);
7471

75-
function updateStepRotation(newValue: number) {
72+
function updateStepRotation(
73+
newValue: number,
74+
stepRotationBuffer: TgpuUniform<d.F32>,
75+
middleSquareScaleBuffer: TgpuUniform<d.F32>,
76+
) {
7677
stepRotationBuffer.write(newValue);
7778

7879
// update middle triangle scale so that it doesn't
@@ -100,7 +101,11 @@ function getGridParams() {
100101
return gridParams;
101102
}
102103

103-
function updateGridParams(newValue?: number) {
104+
function updateGridParams(
105+
scaleBuffer: TgpuUniform<d.F32>,
106+
updateInstanceInfoBufferAndBindGroup: () => void,
107+
newValue?: number,
108+
) {
104109
const value = newValue ?? gridParams.tileDensity;
105110
gridParams = createGridParams(value);
106111
scaleBuffer.write(gridParams.tileDensity);

0 commit comments

Comments
 (0)