Skip to content

Commit f2836ee

Browse files
committed
Add a failsafe
1 parent fe4b757 commit f2836ee

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

apps/typegpu-docs/src/content/examples/rendering/game-of-disco/icosphere.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,23 @@ export function createIcosphereShader(
263263
subdivisions: number,
264264
smooth: boolean,
265265
root: TgpuRoot,
266+
maxBufferSize?: number,
266267
): TgpuBuffer<d.Disarray<typeof Vertex>> & VertexFlag {
268+
if (maxBufferSize) {
269+
const vertexSize = getVertexAmount(subdivisions) * d.sizeOf(Vertex);
270+
if (vertexSize > maxBufferSize) {
271+
console.warn(
272+
`Requested icosphere of size ${vertexSize} exceeds max buffer size of ${maxBufferSize} - reducing subdivisions`,
273+
);
274+
return createIcosphereShader(
275+
subdivisions - 1,
276+
smooth,
277+
root,
278+
maxBufferSize,
279+
);
280+
}
281+
}
282+
267283
const key = `${subdivisions}-${smooth}`;
268284
const cached = icoshpereCache.get(key);
269285
if (cached) {
@@ -362,7 +378,6 @@ function subdivide(
362378
.fn([d.vec4f, d.vec4f, d.vec4f, d.u32, d.vec4f], d.vec4f)
363379
.does((v1, v2, v3, smooth, vertexPos) => {
364380
if (smooth === 1) {
365-
// For smooth shading on a sphere, the normal is the same as the normalized position
366381
return vertexPos;
367382
}
368383
const edge1 = d.vec4f(v2.x - v1.x, v2.y - v1.y, v2.z - v1.z, 0);
@@ -446,7 +461,6 @@ function subdivide(
446461
];
447462

448463
const baseIndexNext = triangleIndex * d.u32(12);
449-
// For each of the 12 new vertices, compute and store their values.
450464
for (let i = d.u32(0); i < 12; i++) {
451465
const reprojectedVertex = newVertices[i];
452466

@@ -468,8 +482,6 @@ function subdivide(
468482

469483
const pipeline = root['~unstable'].withCompute(computeFn).createPipeline();
470484

471-
// Calculate the appropriate workgroup dispatch dimensions, splitting across X and Y
472-
// when needed to stay within the 65535 limit
473485
const triangleCount = getVertexAmount(wantedSubdivisions - 1) / 3;
474486
const xGroups = Math.min(triangleCount, 65535);
475487
const yGroups = Math.ceil(triangleCount / 65535);

apps/typegpu-docs/src/content/examples/rendering/game-of-disco/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ let smoothNormals = false;
5151
let subdivisions = 2;
5252

5353
// Initialize with default values
54-
let vertexBuffer = createIcosphereShader(subdivisions, smoothNormals, root);
54+
let vertexBuffer = createIcosphereShader(
55+
subdivisions,
56+
smoothNormals,
57+
root,
58+
maxSize,
59+
);
5560

5661
const cubeVertexBuffer = root
5762
.createBuffer(d.arrayOf(CubeVertex, cubeVertices.length), cubeVertices)
@@ -354,14 +359,14 @@ export const controls = {
354359
step: 1,
355360
onSliderChange(value: number) {
356361
subdivisions = value;
357-
vertexBuffer = createIcosphereShader(value, smoothNormals, root);
362+
vertexBuffer = createIcosphereShader(value, smoothNormals, root, maxSize);
358363
},
359364
},
360365
'smooth normals': {
361366
initial: false,
362367
onToggleChange: (value: boolean) => {
363368
smoothNormals = value;
364-
vertexBuffer = createIcosphereShader(subdivisions, value, root);
369+
vertexBuffer = createIcosphereShader(subdivisions, value, root, maxSize);
365370
},
366371
},
367372
};

0 commit comments

Comments
 (0)