@bigmistqke/view.gl 0.5.0 → 0.6.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/README.md CHANGED
@@ -268,6 +268,14 @@ gl.drawArraysInstanced(gl.TRIANGLES, 0, 3, 100)
268
268
  unbind()
269
269
  ```
270
270
 
271
+ A large buffer can be sized first and filled in parts, counting in elements of
272
+ the attribute's format:
273
+
274
+ ```typescript
275
+ attributes.position.allocate(totalElements) // sizes the buffer, contents undefined
276
+ attributes.position.write(chunk, offset) // overwrites a range with bufferSubData
277
+ ```
278
+
271
279
  ##### 📋 AttributeSchema
272
280
 
273
281
  A mapping of attribute names to their configuration.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigmistqke/view.gl",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "👀 Utilities for managing WebGL resources.",
5
5
  "exports": {
6
6
  ".": {
package/src/index.ts CHANGED
@@ -438,6 +438,16 @@ export function attributeView<T extends AttributeSchema>(
438
438
  gl.bufferData(gl.ARRAY_BUFFER, data, gl[usage])
439
439
  return this
440
440
  },
441
+ allocate(length, usage = 'STATIC_DRAW') {
442
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
443
+ gl.bufferData(gl.ARRAY_BUFFER, length * FORMAT_BYTE_SIZE[resolvedFormat], gl[usage])
444
+ return this
445
+ },
446
+ write(data, offset) {
447
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
448
+ gl.bufferSubData(gl.ARRAY_BUFFER, offset * FORMAT_BYTE_SIZE[resolvedFormat], data)
449
+ return this
450
+ },
441
451
  }
442
452
  },
443
453
  )
package/src/types.ts CHANGED
@@ -365,6 +365,17 @@ export interface AttributeMethods<T extends AttributeDefinition = AttributeDefin
365
365
  */
366
366
  bind(): () => void
367
367
  set(data: FormatToArray[ResolvedFormat<T>], usage?: GLUsage): { bind(): () => void }
368
+ /**
369
+ * Sizes the buffer to `length` elements of the attribute's format, contents
370
+ * undefined, discarding what it held.
371
+ */
372
+ allocate(length: number, usage?: GLUsage): { bind(): () => void }
373
+ /**
374
+ * Overwrites the elements from `offset` on with `data`, leaving the rest as
375
+ * it is. Counts in elements of the attribute's format; the range must fit
376
+ * inside the buffer's current size.
377
+ */
378
+ write(data: FormatToArray[ResolvedFormat<T>], offset: number): { bind(): () => void }
368
379
  /**
369
380
  * Delete the buffer, if this attribute is the one that made it.
370
381
  *
@@ -435,6 +435,31 @@ describe('attributeView', () => {
435
435
  program = gl.createProgram()!
436
436
  })
437
437
 
438
+ it('sizes the buffer without data, then fills it in parts, counting in its format', () => {
439
+ const schema = {
440
+ a_position: { kind: 'vec2' },
441
+ a_uv: { kind: 'vec2', format: 'int16' },
442
+ } satisfies AttributeSchema
443
+
444
+ const { a_position, a_uv } = attributeView(gl, program, schema)
445
+
446
+ a_position.allocate(12, 'DYNAMIC_DRAW')
447
+ expect(gl.bindBuffer).toHaveBeenLastCalledWith(gl.ARRAY_BUFFER, a_position.buffer)
448
+ expect(gl.bufferData).toHaveBeenLastCalledWith(gl.ARRAY_BUFFER, 48, gl.DYNAMIC_DRAW)
449
+
450
+ const floats = new Float32Array([1, 2])
451
+ a_position.write(floats, 6)
452
+ expect(gl.bufferSubData).toHaveBeenLastCalledWith(gl.ARRAY_BUFFER, 24, floats)
453
+
454
+ a_uv.allocate(12)
455
+ expect(gl.bindBuffer).toHaveBeenLastCalledWith(gl.ARRAY_BUFFER, a_uv.buffer)
456
+ expect(gl.bufferData).toHaveBeenLastCalledWith(gl.ARRAY_BUFFER, 24, gl.STATIC_DRAW)
457
+
458
+ const shorts = new Int16Array([1, 2])
459
+ a_uv.write(shorts, 6)
460
+ expect(gl.bufferSubData).toHaveBeenLastCalledWith(gl.ARRAY_BUFFER, 12, shorts)
461
+ })
462
+
438
463
  it('should create attribute methods for basic types', () => {
439
464
  const schema = {
440
465
  a_position: { kind: 'vec2' },