ugly-game 0.15.77 → 0.15.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -377,6 +377,9 @@ export declare class GpuPropsRenderer {
377
377
  * the LOD system rather than merely to the encoder.
378
378
  */
379
379
  private geo;
380
+ private geometrySlots;
381
+ private geometryVertices;
382
+ private geometryIndices;
380
383
  private cullVis;
381
384
  private cullBGs;
382
385
  /** Survivor lists are per (model, cascade) but packed into one buffer per model at this stride. */
@@ -447,7 +450,15 @@ export declare class GpuPropsRenderer {
447
450
  * `setModels` had been given that alpha; back at 1 it is opaque and casts again.
448
451
  */
449
452
  setMaterial(model: number, surface: Pick<PropMesh, 'alpha' | 'roughness' | 'metalness' | 'clearcoat' | 'clearcoatRoughness' | 'directClearcoat' | 'reflectionStrength' | 'specularIntensity' | 'metalFlakes' | 'flakeScale' | 'pearlStrength'>): void;
450
- setModels(meshes: PropMesh[]): void;
453
+ /**
454
+ * Replace the draw list and report the geometry bytes written by this call.
455
+ * Pass retainGeometry for immutable meshes whose identities survive draw-list
456
+ * changes. The arena appends new geometry until full, then compacts to the
457
+ * active list. Callers with mutable mesh arrays use the default upload path.
458
+ */
459
+ setModels(meshes: PropMesh[], retainGeometry?: boolean): number;
460
+ /** Allocated geometry only. Includes unused arena capacity. */
461
+ geometryBufferBytes(): number;
451
462
  /**
452
463
  * Group instances by model and upload the per-model instance buffers.
453
464
  *
@@ -788,6 +788,9 @@ export class GpuPropsRenderer {
788
788
  * the LOD system rather than merely to the encoder.
789
789
  */
790
790
  geo = null;
791
+ geometrySlots = new WeakMap();
792
+ geometryVertices = 0;
793
+ geometryIndices = 0;
791
794
  cullVis = []; // survivor list — per MODEL, see cullPass
792
795
  cullBGs = [];
793
796
  /** Survivor lists are per (model, cascade) but packed into one buffer per model at this stride. */
@@ -1253,7 +1256,13 @@ export class GpuPropsRenderer {
1253
1256
  if (changed)
1254
1257
  this.device.queue.writeBuffer(packed.matBuf, 0, packed.materialValues);
1255
1258
  }
1256
- setModels(meshes) {
1259
+ /**
1260
+ * Replace the draw list and report the geometry bytes written by this call.
1261
+ * Pass retainGeometry for immutable meshes whose identities survive draw-list
1262
+ * changes. The arena appends new geometry until full, then compacts to the
1263
+ * active list. Callers with mutable mesh arrays use the default upload path.
1264
+ */
1265
+ setModels(meshes, retainGeometry = false) {
1257
1266
  this.glassInstances.length = 0;
1258
1267
  if (!meshes.some((m) => m.glassThickness))
1259
1268
  this.releaseGlassSnapshots();
@@ -1309,54 +1318,86 @@ export class GpuPropsRenderer {
1309
1318
  // Indices stay MODEL-LOCAL and are offset by `baseVertex` at draw time rather than being rewritten
1310
1319
  // — rewriting them would work equally well for the camera pass and would silently break the
1311
1320
  // occluder pass, whose index buffer is fed to `drawIndexedIndirect` with args the GPU writes.
1312
- const totalVerts = meshes.reduce((n, m) => n + m.positions.length / 3, 0);
1313
- const totalIdx = meshes.reduce((n, m) => n + m.indices.length, 0);
1314
- // Cooked streams already have the GPU layout. Upload them directly to their
1315
- // arena offsets instead of allocating/copying five more CPU-sized arenas.
1316
- this.geo?.destroy();
1317
- this.geo = {
1318
- pos: dev.createBuffer({
1319
- size: Math.max(12, totalVerts * 12),
1320
- usage: U.VERTEX | U.COPY_DST,
1321
- }),
1322
- nrm: dev.createBuffer({
1323
- size: Math.max(12, totalVerts * 12),
1324
- usage: U.VERTEX | U.COPY_DST,
1325
- }),
1326
- col: dev.createBuffer({
1327
- size: Math.max(12, totalVerts * 12),
1328
- usage: U.VERTEX | U.COPY_DST,
1329
- }),
1330
- uv: dev.createBuffer({
1331
- size: Math.max(8, totalVerts * 8),
1332
- usage: U.VERTEX | U.COPY_DST,
1333
- }),
1334
- idx: dev.createBuffer({
1335
- size: Math.max(4, totalIdx * 4),
1336
- usage: U.INDEX | U.COPY_DST,
1337
- }),
1338
- destroy() {
1339
- for (const buffer of [this.pos, this.nrm, this.col, this.uv, this.idx])
1340
- buffer.destroy();
1341
- },
1342
- };
1321
+ const missing = retainGeometry
1322
+ ? [...new Set(meshes.filter((mesh) => !this.geometrySlots.has(mesh)))]
1323
+ : meshes;
1324
+ const newVertices = missing.reduce((n, m) => n + m.positions.length / 3, 0);
1325
+ const newIndices = missing.reduce((n, m) => n + m.indices.length, 0);
1326
+ const reuseArena = retainGeometry &&
1327
+ meshes.length > 0 &&
1328
+ this.geo &&
1329
+ this.geometryVertices + newVertices <= this.geo.pos.size / 12 &&
1330
+ this.geometryIndices + newIndices <= this.geo.idx.size / 4;
1331
+ if (!reuseArena) {
1332
+ const headroom = retainGeometry ? 2 : 1;
1333
+ const active = retainGeometry ? [...new Set(meshes)] : meshes;
1334
+ const vertices = active.reduce((n, m) => n + m.positions.length / 3, 0);
1335
+ const indices = active.reduce((n, m) => n + m.indices.length, 0);
1336
+ const maxBuffer = dev.limits.maxBufferSize;
1337
+ const vertexCapacity = Math.max(vertices, Math.min(vertices * headroom, Math.floor(maxBuffer / 12)));
1338
+ const indexCapacity = Math.max(indices, Math.min(indices * headroom, Math.floor(maxBuffer / 4)));
1339
+ this.geometrySlots = new WeakMap();
1340
+ this.geometryVertices = 0;
1341
+ this.geometryIndices = 0;
1342
+ this.geo?.destroy();
1343
+ this.geo = {
1344
+ pos: dev.createBuffer({
1345
+ size: Math.max(12, vertexCapacity * 12),
1346
+ usage: U.VERTEX | U.COPY_DST,
1347
+ }),
1348
+ nrm: dev.createBuffer({
1349
+ size: Math.max(12, vertexCapacity * 12),
1350
+ usage: U.VERTEX | U.COPY_DST,
1351
+ }),
1352
+ col: dev.createBuffer({
1353
+ size: Math.max(12, vertexCapacity * 12),
1354
+ usage: U.VERTEX | U.COPY_DST,
1355
+ }),
1356
+ uv: dev.createBuffer({
1357
+ size: Math.max(8, vertexCapacity * 8),
1358
+ usage: U.VERTEX | U.COPY_DST,
1359
+ }),
1360
+ idx: dev.createBuffer({
1361
+ size: Math.max(4, indexCapacity * 4),
1362
+ usage: U.INDEX | U.COPY_DST,
1363
+ }),
1364
+ destroy() {
1365
+ for (const buffer of [
1366
+ this.pos,
1367
+ this.nrm,
1368
+ this.col,
1369
+ this.uv,
1370
+ this.idx,
1371
+ ])
1372
+ buffer.destroy();
1373
+ },
1374
+ };
1375
+ }
1376
+ let uploadedBytes = 0;
1343
1377
  const upload = (buffer, offset, data) => {
1344
- if (data.byteLength)
1345
- dev.queue.writeBuffer(buffer, offset, data);
1378
+ if (!data.byteLength)
1379
+ return;
1380
+ dev.queue.writeBuffer(buffer, offset, data);
1381
+ uploadedBytes += data.byteLength;
1346
1382
  };
1347
- let vCursor = 0, iCursor = 0;
1348
1383
  this.models = meshes.map((m, modelIndex) => {
1349
- const verts = m.positions.length / 3;
1350
- const baseVertex = vCursor, firstIndex = iCursor;
1351
- upload(this.geo.pos, baseVertex * 12, m.positions);
1352
- upload(this.geo.nrm, baseVertex * 12, m.normals);
1353
- upload(this.geo.col, baseVertex * 12, m.colors);
1354
- // Fresh WebGPU allocations are zero initialized, including absent UVs.
1355
- if (m.uvs)
1356
- upload(this.geo.uv, baseVertex * 8, m.uvs);
1357
- upload(this.geo.idx, firstIndex * 4, m.indices);
1358
- vCursor += verts;
1359
- iCursor += m.indices.length;
1384
+ let slot = retainGeometry ? this.geometrySlots.get(m) : undefined;
1385
+ if (!slot) {
1386
+ const baseVertex = this.geometryVertices;
1387
+ const firstIndex = this.geometryIndices;
1388
+ slot = { baseVertex, firstIndex };
1389
+ upload(this.geo.pos, baseVertex * 12, m.positions);
1390
+ upload(this.geo.nrm, baseVertex * 12, m.normals);
1391
+ upload(this.geo.col, baseVertex * 12, m.colors);
1392
+ // Appended ranges are still zero initialized, including absent UVs.
1393
+ if (m.uvs)
1394
+ upload(this.geo.uv, baseVertex * 8, m.uvs);
1395
+ upload(this.geo.idx, firstIndex * 4, m.indices);
1396
+ this.geometryVertices += m.positions.length / 3;
1397
+ this.geometryIndices += m.indices.length;
1398
+ if (retainGeometry)
1399
+ this.geometrySlots.set(m, slot);
1400
+ }
1360
1401
  // A GPUTexture is already uploaded and already mipped (the KTX2 path) — bind it directly.
1361
1402
  // Only an ImageBitmap needs uploadAlbedo's hand-rolled half-scale mip chain.
1362
1403
  const ownsTex = !!m.albedo && !(m.albedo instanceof GPUTexture);
@@ -1416,8 +1457,7 @@ export class GpuPropsRenderer {
1416
1457
  matBuf,
1417
1458
  materialValues,
1418
1459
  ownsTex,
1419
- firstIndex,
1420
- baseVertex,
1460
+ ...slot,
1421
1461
  idxCount: m.indices.length,
1422
1462
  extent: m.extent ?? measureExtent(m.positions),
1423
1463
  tex,
@@ -1437,6 +1477,16 @@ export class GpuPropsRenderer {
1437
1477
  }),
1438
1478
  };
1439
1479
  });
1480
+ for (const buffer of this.instBufs)
1481
+ buffer?.destroy();
1482
+ for (const buffer of this.cullVis)
1483
+ buffer?.destroy();
1484
+ for (const cascade of this.cullU)
1485
+ for (const buffer of cascade)
1486
+ buffer?.destroy();
1487
+ for (const cascade of this.cullArgs)
1488
+ for (const buffer of cascade)
1489
+ buffer?.destroy();
1440
1490
  this.caps = meshes.map(() => 0);
1441
1491
  this.instBufs = meshes.map(() => null);
1442
1492
  this.bgs = meshes.map(() => null);
@@ -1449,6 +1499,14 @@ export class GpuPropsRenderer {
1449
1499
  this.cullVis = meshes.map(() => null);
1450
1500
  this.visStride = meshes.map(() => 0);
1451
1501
  this.cullReady = false;
1502
+ return uploadedBytes;
1503
+ }
1504
+ /** Allocated geometry only. Includes unused arena capacity. */
1505
+ geometryBufferBytes() {
1506
+ const { geo } = this;
1507
+ return geo
1508
+ ? geo.pos.size + geo.nrm.size + geo.col.size + geo.uv.size + geo.idx.size
1509
+ : 0;
1452
1510
  }
1453
1511
  /**
1454
1512
  * Group instances by model and upload the per-model instance buffers.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.77",
3
+ "version": "0.15.78",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {