@bigmistqke/view.gl 0.6.0 → 0.7.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
@@ -396,6 +396,14 @@ const unbind = buffers.indices.bind()
396
396
  // bound vertex array object.
397
397
  ```
398
398
 
399
+ A large buffer can be sized first and filled in parts. A buffer has no element
400
+ type of its own, so these count in bytes:
401
+
402
+ ```typescript
403
+ buffers.data.allocate(byteLength) // sizes the buffer, contents undefined
404
+ buffers.data.write(chunk, byteOffset) // overwrites a range with bufferSubData
405
+ ```
406
+
399
407
  ##### 📋 BufferSchema
400
408
  A mapping of buffer names to their configuration. Each buffer has a target type and optional usage pattern.
401
409
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigmistqke/view.gl",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "👀 Utilities for managing WebGL resources.",
5
5
  "exports": {
6
6
  ".": {
package/src/index.ts CHANGED
@@ -702,6 +702,14 @@ export function bufferView<T extends BufferSchema>(
702
702
  gl.bindBuffer(gl[target], buffer)
703
703
  gl.bufferData(gl[target], data, gl[usage])
704
704
  },
705
+ allocate(byteLength: number) {
706
+ gl.bindBuffer(gl[target], buffer)
707
+ gl.bufferData(gl[target], byteLength, gl[usage])
708
+ },
709
+ write(data: Float32Array | Uint16Array | Uint32Array, byteOffset: number) {
710
+ gl.bindBuffer(gl[target], buffer)
711
+ gl.bufferSubData(gl[target], byteOffset, data)
712
+ },
705
713
  }
706
714
  })
707
715
 
package/src/types.ts CHANGED
@@ -490,6 +490,16 @@ export type BufferSchema = Record<string | symbol, BufferDefinition>
490
490
 
491
491
  export interface BufferMethods extends VertexArrayParticipant {
492
492
  set(data: Float32Array | Uint16Array | Uint32Array): void
493
+ /**
494
+ * Sizes the buffer to `byteLength` bytes, contents undefined, discarding what
495
+ * it held. In bytes: the buffer has no element type of its own.
496
+ */
497
+ allocate(byteLength: number): void
498
+ /**
499
+ * Overwrites the bytes from `byteOffset` on with `data`, leaving the rest as
500
+ * it is. The range must fit inside the buffer's current size.
501
+ */
502
+ write(data: Float32Array | Uint16Array | Uint32Array, byteOffset: number): void
493
503
  /** Binds the buffer, returning a disposer that restores the previous binding */
494
504
  bind(): () => void
495
505
  dispose(): void
@@ -1256,6 +1256,21 @@ describe('bufferView', () => {
1256
1256
  expect(gl.createBuffer).toHaveBeenCalledTimes(2)
1257
1257
  })
1258
1258
 
1259
+ it('sizes the buffer without data, then fills it in parts, counting in bytes', () => {
1260
+ const { indices } = bufferView(gl, {
1261
+ indices: { target: 'ELEMENT_ARRAY_BUFFER', usage: 'DYNAMIC_DRAW' },
1262
+ })
1263
+ const target = vi.mocked(gl.createBuffer).mock.results.at(-1)!.value
1264
+
1265
+ indices.allocate(64)
1266
+ expect(gl.bindBuffer).toHaveBeenLastCalledWith(gl.ELEMENT_ARRAY_BUFFER, target)
1267
+ expect(gl.bufferData).toHaveBeenLastCalledWith(gl.ELEMENT_ARRAY_BUFFER, 64, gl.DYNAMIC_DRAW)
1268
+
1269
+ const part = new Uint16Array([0, 1, 2])
1270
+ indices.write(part, 12)
1271
+ expect(gl.bufferSubData).toHaveBeenLastCalledWith(gl.ELEMENT_ARRAY_BUFFER, 12, part)
1272
+ })
1273
+
1259
1274
  it('should create buffers with specified target', () => {
1260
1275
  const schema = {
1261
1276
  vertices: { target: 'ARRAY_BUFFER' },