@@ -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 ) ;
0 commit comments