@footgun/cobalt 0.4.0 → 0.5.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@footgun/cobalt",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "main": "bundle.js",
6
6
  "description": "A 2D WebGpu renderer",
@@ -57,7 +57,8 @@ async function init (cobalt, node) {
57
57
  // Define vertices and indices for your line represented as two triangles (a rectangle)
58
58
  // For example, this could represent a line segment from (10, 10) to (100, 10) with a thickness of 10 units
59
59
  // Updated vertices in normalized device coordinates (NDC)
60
- const vertices = new Float32Array(300000)
60
+
61
+ const vertices = new Float32Array(1024)
61
62
 
62
63
  const vertexBuffer = device.createBuffer({
63
64
  size: vertices.byteLength,
@@ -65,10 +66,10 @@ async function init (cobalt, node) {
65
66
  //mappedAtCreation: true,
66
67
  })
67
68
 
68
-
69
69
  //new Float32Array(vertexBuffer.getMappedRange()).set(vertices);
70
70
  //vertexBuffer.unmap()
71
71
 
72
+
72
73
  const uniformBuffer = device.createBuffer({
73
74
  size: 64 * 2, // 4x4 matrix with 4 bytes per float32, times 2 matrices (view, projection)
74
75
  usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
@@ -186,9 +187,18 @@ function draw (cobalt, node, commandEncoder) {
186
187
  node.data.dirty = false
187
188
  const stride = 6 * Float32Array.BYTES_PER_ELEMENT // 2 floats per vertex position + 4 floats per vertex color
188
189
 
190
+ // if node.data.vertices has been re-sized, re-create the buffer
191
+ if (node.data.vertices.buffer.byteLength > node.data.vertexBuffer.size) {
192
+ node.data.vertexBuffer.destroy()
193
+ node.data.vertexBuffer = device.createBuffer({
194
+ size: node.data.vertices.byteLength,
195
+ usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
196
+ })
197
+ }
198
+
189
199
  let byteCount = node.data.vertexCount * stride
190
200
  if (byteCount > node.data.vertexBuffer.size) {
191
- console.warn('too many primitives, bailing')
201
+ console.error('too many primitives, bailing')
192
202
  return
193
203
  }
194
204
 
@@ -47,6 +47,12 @@ export default {
47
47
 
48
48
  let i = node.data.vertexCount * 6 // 2 floats position + 4 floats color per vertex
49
49
 
50
+
51
+ const currentElementCount = node.data.vertexCount * 6
52
+ const floatsToAdd = triangles.length * 3 * 6
53
+ node.data.vertices = handleArrayResize(Float32Array, node.data.vertices, currentElementCount, floatsToAdd)
54
+
55
+
50
56
  const pos = vec2.create()
51
57
 
52
58
  for (const tri of triangles) {
@@ -123,6 +129,11 @@ export default {
123
129
  // angle between each segment
124
130
  const deltaAngle = 2 * Math.PI / numSegments
125
131
 
132
+ const currentElementCount = node.data.vertexCount * 6
133
+ const floatsToAdd = numSegments * 3 * 6
134
+ node.data.vertices = handleArrayResize(Float32Array, node.data.vertices, currentElementCount, floatsToAdd)
135
+
136
+
126
137
  const m = node.data.transforms.at(-1)
127
138
 
128
139
  // Generate points for the ellipsoid
@@ -218,6 +229,12 @@ export default {
218
229
  const bottomLeft = vec2.transformMat3([ x - halfWidth, y + halfHeight ], m)
219
230
  const bottomRight = vec2.transformMat3([ x + halfWidth, y + halfHeight ], m)
220
231
 
232
+
233
+ const currentElementCount = node.data.vertexCount * 6
234
+ const floatsToAdd = 6 * 6
235
+ node.data.vertices = handleArrayResize(Float32Array, node.data.vertices, currentElementCount, floatsToAdd)
236
+
237
+
221
238
  let i = node.data.vertexCount * 6 // 2 floats position + 4 floats color per vertex
222
239
 
223
240
 
@@ -314,6 +331,11 @@ function line (cobalt, node, start, end, color, lineWidth=1) {
314
331
 
315
332
  let i = node.data.vertexCount * 6 // 2 floats position + 4 floats color per vertex
316
333
 
334
+
335
+ const currentElementCount = node.data.vertexCount * 6
336
+ const floatsToAdd = 6 * 6
337
+ node.data.vertices = handleArrayResize(Float32Array, node.data.vertices, currentElementCount, floatsToAdd)
338
+
317
339
  // triangle 1
318
340
  // pt 1
319
341
  node.data.vertices[i + 0] = start[0] + perp[0] * halfLineWidth
@@ -383,6 +405,28 @@ function line (cobalt, node, start, end, color, lineWidth=1) {
383
405
  }
384
406
 
385
407
 
408
+ // if the new elements won't fit in the existing vertices array, resize it
409
+ // @param ArrayType ArrayType one of the TypedArray types (Float32Array, Uint8Array, etc.)
410
+ // @param TypedArray arr
411
+ function handleArrayResize (ArrayType, arr, currentElementCount, elementsToAdd) {
412
+ // if the new vertices fit in the existing vertices array bail
413
+ if ((currentElementCount + elementsToAdd) <= arr.length)
414
+ return arr
415
+
416
+ // attempt to double the existing array size when we need more capacity
417
+ const newSize = arr.length * 2
418
+
419
+ const MAX_LENGTH = 16 * 1024 * 1024 / arr.BYTES_PER_ELEMENT
420
+
421
+ if (newSize > MAX_LENGTH)
422
+ throw new Error('vertices exceed max array size')
423
+
424
+ const newArray = new ArrayType(newSize)
425
+ newArray.set(arr)
426
+ return newArray
427
+ }
428
+
429
+
386
430
  // return component of vector perpendicular to a unit basis vector
387
431
  // (IMPORTANT NOTE: assumes "basis" has unit magnitude (length==1))
388
432
  function perpendicularComponent (inp) {