@bigmistqke/view.gl 0.1.18 → 0.1.19

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
@@ -204,6 +204,18 @@ attributes.instanceOffset.set(instanceData).bind()
204
204
  gl.drawArraysInstanced(gl.TRIANGLES, 0, 3, 100)
205
205
  ```
206
206
 
207
+ `bind()` returns a disposer that restores the state it changed — here the
208
+ instancing divisor, which belongs to the attribute location rather than to the
209
+ program, and so is inherited by whatever binds there next. Ignore it when the
210
+ view owns its locations for the lifetime of the program; call it when they are
211
+ shared:
212
+
213
+ ```typescript
214
+ const unbind = attributes.instanceOffset.set(instanceData).bind()
215
+ gl.drawArraysInstanced(gl.TRIANGLES, 0, 3, 100)
216
+ unbind()
217
+ ```
218
+
207
219
  ##### 📋 AttributeSchema
208
220
 
209
221
  A mapping of attribute names to their configuration.
@@ -258,6 +270,16 @@ const interleavedAttributes = interleavedAttributeView(gl, program, {
258
270
  interleavedAttributes.vertexData.set(interleavedVertexData).bind()
259
271
  ```
260
272
 
273
+ Where vertex array objects are available this binds one, and it stays selected
274
+ for every later draw — which would then write its own attributes into it. The
275
+ disposer returned by `bind()` selects the previous array again:
276
+
277
+ ```typescript
278
+ const unbind = interleavedAttributes.vertexData.bind()
279
+ gl.drawElements(gl.TRIANGLES, count, gl.UNSIGNED_SHORT, 0)
280
+ unbind()
281
+ ```
282
+
261
283
  ##### 📋 InterleavedAttributeSchema
262
284
 
263
285
  A mapping of interleaved buffer names to their layout configuration. Each layout defines multiple attributes packed into a single buffer.
@@ -296,6 +318,10 @@ const buffers = bufferView(gl, {
296
318
 
297
319
  buffers.indices.set(indexData).bind()
298
320
  buffers.data.set(dynamicData).bind()
321
+
322
+ // bind() returns a disposer restoring the previous binding for that target.
323
+ // Worth calling for ELEMENT_ARRAY_BUFFER, whose binding is recorded in the
324
+ // bound vertex array object.
299
325
  ```
300
326
 
301
327
  ##### 📋 BufferSchema
@@ -72,4 +72,4 @@ export {
72
72
  getInstancedArrays,
73
73
  getVertexArrayObject
74
74
  };
75
- //# sourceMappingURL=chunk-WSNE4JVQ.mjs.map
75
+ //# sourceMappingURL=chunk-CJTT7WPV.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/gl.ts"],"sourcesContent":["import { RemoveSuffix } from './type-utils'\nimport type { GL } from './types'\n\nfunction isWebGL2RenderingContext(\n gl: WebGLRenderingContext | WebGL2RenderingContext,\n): gl is WebGL2RenderingContext {\n return typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext\n}\n\n/**********************************************************************************/\n/* */\n/* Create Shader */\n/* */\n/**********************************************************************************/\n\nexport function createShader(gl: GL, type: number, source: string): WebGLShader {\n const shader = gl.createShader(type)\n if (!shader) throw new Error('Failed to create shader')\n\n gl.shaderSource(shader, source)\n gl.compileShader(shader)\n\n if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {\n const info = gl.getShaderInfoLog(shader)\n gl.deleteShader(shader)\n throw new Error(\n `Failed to compile ${type === gl.VERTEX_SHADER ? 'vertex' : 'fragment'} shader: ${info}`,\n )\n }\n\n return shader\n}\n\n/**********************************************************************************/\n/* */\n/* Create Program */\n/* */\n/**********************************************************************************/\n\nexport function createProgram(gl: GL, vertexSource: string, fragmentSource: string): WebGLProgram {\n const program = gl.createProgram()\n if (!program) throw new Error('Failed to create WebGL program')\n\n const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexSource)\n const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentSource)\n\n gl.attachShader(program, vertexShader)\n gl.attachShader(program, fragmentShader)\n gl.linkProgram(program)\n\n if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {\n const info = gl.getProgramInfoLog(program)\n gl.deleteProgram(program)\n gl.deleteShader(vertexShader)\n gl.deleteShader(fragmentShader)\n throw new Error(`Failed to link program: ${info}`)\n }\n\n // Clean up shaders after linking\n gl.deleteShader(vertexShader)\n gl.deleteShader(fragmentShader)\n\n return program\n}\n\n/**********************************************************************************/\n/* */\n/* Get Instanced Arrays */\n/* */\n/**********************************************************************************/\n\nconst INSTANCED_ARRAYS_WRAPPER_MAP = new WeakMap<\n GL,\n RemoveSuffix<\n Pick<\n ANGLE_instanced_arrays,\n 'drawArraysInstancedANGLE' | 'drawElementsInstancedANGLE' | 'vertexAttribDivisorANGLE'\n >,\n 'ANGLE'\n > | null\n>()\nexport function getInstancedArrays(gl: GL) {\n if (isWebGL2RenderingContext(gl)) return gl\n\n const cached = INSTANCED_ARRAYS_WRAPPER_MAP.get(gl)\n if (cached) return cached\n\n const ext = gl.getExtension('ANGLE_instanced_arrays')\n if (!ext) return undefined\n\n const wrapper = {\n drawArraysInstanced: ext.drawArraysInstancedANGLE.bind(ext),\n drawElementsInstanced: ext.drawElementsInstancedANGLE.bind(ext),\n vertexAttribDivisor: ext.vertexAttribDivisorANGLE.bind(ext),\n }\n INSTANCED_ARRAYS_WRAPPER_MAP.set(gl, wrapper)\n\n return wrapper\n}\n\n/**********************************************************************************/\n/* */\n/* Get Vertex Array Object */\n/* */\n/**********************************************************************************/\n\nconst VERTEX_ARRAY_OBJECT_WRAPPER_MAP = new WeakMap<\n GL,\n RemoveSuffix<\n Pick<\n OES_vertex_array_object,\n 'bindVertexArrayOES' | 'createVertexArrayOES' | 'deleteVertexArrayOES'\n >,\n 'OES'\n > | null\n>()\nexport function getVertexArrayObject(gl: GL) {\n if (isWebGL2RenderingContext(gl)) return gl\n\n const cached = VERTEX_ARRAY_OBJECT_WRAPPER_MAP.get(gl)\n if (cached) return cached\n\n const ext = gl.getExtension('OES_vertex_array_object')\n if (!ext) return null\n\n const wrapper = {\n bindVertexArray: ext.bindVertexArrayOES.bind(ext),\n createVertexArray: ext.createVertexArrayOES.bind(ext),\n deleteVertexArray: ext.deleteVertexArrayOES.bind(ext),\n }\n VERTEX_ARRAY_OBJECT_WRAPPER_MAP.set(gl, wrapper)\n\n return wrapper\n}\n"],"mappings":";AAGA,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AE;AAClD,MAAI,OAAQ,QAAO;AAEnB,QAAM,MAAM,GAAG,aAAa,wBAAwB;AACpD,MAAI,CAAC,IAAK,QAAO;AAEjB,QAAM,UAAU;AAAA,IACd,qBAAqB,IAAI,yBAAyB,KAAK,GAAG;AAAA,IAC1D,uBAAuB,IAAI,2BAA2B,KAAK,GAAG;AAAA,IAC9D,qBAAqB,IAAI,yBAAyB,KAAK,GAAG;AAAA,EAC5D;AACA,+BAA6B,IAAI,IAAI,OAAO;AAE5C,SAAO;AACT;AAQA,IAAM,kCAAkC,oBAAI,QAS1C;AACK,SAAS,qBAAqB,IAAQ;AAC3C,MAAI,yBAAyB,EAAE,EAAG,QAAO;AAEzC,QAAM,SAAS,gCAAgC,IAAI,EAAE;AACrD,MAAI,OAAQ,QAAO;AAEnB,QAAM,MAAM,GAAG,aAAa,yBAAyB;AACrD,MAAI,CAAC,IAAK,QAAO;AAEjB,QAAM,UAAU;AAAA,IACd,iBAAiB,IAAI,mBAAmB,KAAK,GAAG;AAAA,IAChD,mBAAmB,IAAI,qBAAqB,KAAK,GAAG;AAAA,IACpD,mBAAmB,IAAI,qBAAqB,KAAK,GAAG;AAAA,EACtD;AACA,kCAAgC,IAAI,IAAI,OAAO;AAE/C,SAAO;AACT;","names":[]}
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  getInstancedArrays,
3
3
  getVertexArrayObject
4
- } from "./chunk-WSNE4JVQ.mjs";
4
+ } from "./chunk-CJTT7WPV.mjs";
5
5
 
6
6
  // src/utils.ts
7
7
  function assertedNotNullish(value, message) {
@@ -195,14 +195,36 @@ function handleAttribute(gl, location, size, stride, offset, glType, isIntegerKi
195
195
  } else {
196
196
  gl.vertexAttribPointer(location, size, gl[glType], normalized, stride, offset);
197
197
  }
198
+ const instancedArrays = getInstancedArrays(gl);
198
199
  if (instanced) {
199
- assertedNotNullish(getInstancedArrays(gl)).vertexAttribDivisor(location, 1);
200
+ assertedNotNullish(instancedArrays).vertexAttribDivisor(location, 1);
201
+ } else if (instancedArrays) {
202
+ instancedArrays.vertexAttribDivisor(location, 0);
200
203
  }
201
204
  }
205
+ var VERTEX_ATTRIB_ARRAY_DIVISOR = 35070;
206
+ var VERTEX_ARRAY_BINDING = 34229;
207
+ function readDivisor(gl, location) {
208
+ return getInstancedArrays(gl) ? gl.getVertexAttrib(location, VERTEX_ATTRIB_ARRAY_DIVISOR) : 0;
209
+ }
210
+ function once(restore) {
211
+ let done = false;
212
+ return () => {
213
+ if (done) return;
214
+ done = true;
215
+ restore();
216
+ };
217
+ }
202
218
  function attributeView(gl, program, schema, { signal } = {}) {
203
219
  const attributes = mapObject(
204
220
  schema,
205
- ({ kind, format, normalized = false, instanced, buffer = assertedNotNullish(gl.createBuffer()) }, key) => {
221
+ ({
222
+ kind,
223
+ format,
224
+ normalized = false,
225
+ instanced,
226
+ buffer = assertedNotNullish(gl.createBuffer())
227
+ }, key) => {
206
228
  const name2 = toID(key);
207
229
  const location = gl.getAttribLocation(program, name2);
208
230
  if (location < 0) {
@@ -215,8 +237,12 @@ function attributeView(gl, program, schema, { signal } = {}) {
215
237
  return {
216
238
  buffer,
217
239
  bind() {
240
+ const previousDivisor = readDivisor(gl, location);
218
241
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
219
242
  handleAttribute(gl, location, size, 0, 0, glType, isIntKind, normalized, instanced);
243
+ return once(() => {
244
+ getInstancedArrays(gl)?.vertexAttribDivisor(location, previousDivisor);
245
+ });
220
246
  },
221
247
  dispose() {
222
248
  gl.deleteBuffer(buffer);
@@ -237,12 +263,14 @@ function attributeView(gl, program, schema, { signal } = {}) {
237
263
  function interleavedAttributeView(gl, program, schema, { signal } = {}) {
238
264
  const interleavedAttributes = mapObject(schema, ({ layout, instanced }) => {
239
265
  let index2 = 0;
266
+ const locations = [];
240
267
  const handles = layout.map((layout2) => {
241
268
  const name2 = toID(layout2.key);
242
269
  const location = gl.getAttribLocation(program, name2);
243
270
  if (location < 0) {
244
271
  throw new Error(`Attribute '${name2}' not found`);
245
272
  }
273
+ locations.push(location);
246
274
  const size = kindToSize(layout2.kind);
247
275
  const offset = index2;
248
276
  const resolvedFormat = layout2.format ?? defaultFormat(layout2.kind);
@@ -250,7 +278,17 @@ function interleavedAttributeView(gl, program, schema, { signal } = {}) {
250
278
  const isIntKind = layout2.kind.startsWith("i") || layout2.kind.startsWith("u");
251
279
  const normalized = layout2.normalized ?? false;
252
280
  index2 += size * FORMAT_BYTE_SIZE[resolvedFormat];
253
- return () => handleAttribute(gl, location, size, stride, offset, glType, isIntKind, normalized, instanced);
281
+ return () => handleAttribute(
282
+ gl,
283
+ location,
284
+ size,
285
+ stride,
286
+ offset,
287
+ glType,
288
+ isIntKind,
289
+ normalized,
290
+ instanced
291
+ );
254
292
  });
255
293
  const stride = index2;
256
294
  const buffer = assertedNotNullish(gl.createBuffer());
@@ -276,8 +314,15 @@ function interleavedAttributeView(gl, program, schema, { signal } = {}) {
276
314
  }
277
315
  };
278
316
  }
317
+ const unbind = () => {
318
+ if (vao) {
319
+ vao.unbind();
320
+ }
321
+ };
279
322
  return {
280
323
  bind() {
324
+ const previousVertexArray = feature ? gl.getParameter(VERTEX_ARRAY_BINDING) : null;
325
+ const previousDivisors = vao ? [] : locations.map((location) => [location, readDivisor(gl, location)]);
281
326
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
282
327
  if (vao) {
283
328
  vao.bind();
@@ -286,12 +331,19 @@ function interleavedAttributeView(gl, program, schema, { signal } = {}) {
286
331
  handle();
287
332
  }
288
333
  }
334
+ return once(() => {
335
+ if (feature) {
336
+ feature.bindVertexArray(previousVertexArray);
337
+ }
338
+ if (!vao) {
339
+ const instancedArrays = getInstancedArrays(gl);
340
+ previousDivisors.forEach(([location, divisor]) => {
341
+ instancedArrays?.vertexAttribDivisor(location, divisor);
342
+ });
343
+ }
344
+ });
289
345
  },
290
- unbind() {
291
- if (vao) {
292
- vao.unbind();
293
- }
294
- },
346
+ unbind,
295
347
  dispose() {
296
348
  gl.deleteBuffer(buffer);
297
349
  if (vao) {
@@ -320,7 +372,11 @@ function bufferView(gl, schema, { signal } = {}) {
320
372
  const buffer = assertedNotNullish(gl.createBuffer());
321
373
  return {
322
374
  bind() {
375
+ const previousBuffer = gl.getParameter(gl[`${target}_BINDING`]);
323
376
  gl.bindBuffer(gl[target], buffer);
377
+ return once(() => {
378
+ gl.bindBuffer(gl[target], previousBuffer);
379
+ });
324
380
  },
325
381
  dispose() {
326
382
  gl.deleteBuffer(buffer);
@@ -346,4 +402,4 @@ export {
346
402
  interleavedAttributeView,
347
403
  bufferView
348
404
  };
349
- //# sourceMappingURL=chunk-CLMGHUTT.mjs.map
405
+ //# sourceMappingURL=chunk-ZP4HP5H7.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":["import type {\n AttributeKind,\n FramebufferDefinition,\n GL,\n KIND_TO_SIZE_MAP,\n KIND_TO_UNIFORM_FN_NAME_MAP,\n TextureDefinition,\n UniformKind,\n} from './types'\n\nexport function assertedNotNullish<T>(value: T, message?: string): NonNullable<T> {\n if (value === undefined || value === null) throw new Error(message)\n return value\n}\n\n/**********************************************************************************/\n/* */\n/* Kind To Uniform Name */\n/* */\n/**********************************************************************************/\n\nexport const kindToUniformFnName = <T extends UniformKind>(\n kind: T,\n): KIND_TO_UNIFORM_FN_NAME_MAP[T] => {\n switch (kind[0]) {\n // mat\n case 'm':\n return ('Matrix' + kind.slice(3) + 'fv') as KIND_TO_UNIFORM_FN_NAME_MAP[T]\n // sampler/booleans/integer\n case 's':\n case 'b':\n case 'i':\n return ((kind.match(/\\d/)?.[0] ?? '1') + 'i') as KIND_TO_UNIFORM_FN_NAME_MAP[T]\n // unsigned integers\n case 'u':\n return ((kind[4] || '1') + 'ui') as KIND_TO_UNIFORM_FN_NAME_MAP[T]\n // vec\n case 'v':\n return (kind[3] + 'f') as KIND_TO_UNIFORM_FN_NAME_MAP[T]\n default:\n switch (kind) {\n case 'float':\n return '1f' as KIND_TO_UNIFORM_FN_NAME_MAP[T]\n case 'uint':\n return '1ui' as KIND_TO_UNIFORM_FN_NAME_MAP[T]\n default:\n return '1i' as KIND_TO_UNIFORM_FN_NAME_MAP[T]\n }\n }\n}\n\n/**********************************************************************************/\n/* */\n/* Kind To Size */\n/* */\n/**********************************************************************************/\n\nexport const kindToSize = <T extends UniformKind>(kind: T): KIND_TO_SIZE_MAP[T] => {\n switch (kind[0]) {\n case 'm':\n const [a, b] = kind.match(/\\d+/g)!.map(Number) as [number, number?]\n return (a * (b ?? a)) as KIND_TO_SIZE_MAP[T]\n case 'v':\n case 'i':\n case 'b':\n case 'u':\n const match = kind.match(/\\d/) as [string] | undefined\n return (match ? +match[0] : 1) as KIND_TO_SIZE_MAP[T]\n default:\n return 1 as KIND_TO_SIZE_MAP[T]\n }\n}\n\n/**********************************************************************************/\n/* */\n/* Guards */\n/* */\n/**********************************************************************************/\n\nexport const isMatKind = <\n T extends UniformKind | AttributeKind,\n TPrefix extends string,\n TPostfix extends string,\n>(\n kind: T,\n): kind is T & `${TPrefix}mat${TPostfix}` => kind.includes('mat')\n\nexport const isSamplerKind = <TPrefix extends string, TPostfix extends string>(\n kind: UniformKind,\n): kind is UniformKind & `${TPrefix}sampler${TPostfix}` => kind.includes('sampler')\n\nexport const isVecKind = <TPrefix extends string, TPostfix extends string>(\n kind: UniformKind,\n): kind is UniformKind & `${TPrefix}vec${TPostfix}` => kind.includes('vec')\n\n/**********************************************************************************/\n/* */\n/* Create Texture */\n/* */\n/**********************************************************************************/\n\nexport function createTexture(\n gl: GL,\n {\n target = 'TEXTURE_2D',\n internalFormat = 'RGBA',\n format = 'RGBA',\n type = 'UNSIGNED_BYTE',\n minFilter = 'NEAREST',\n magFilter = 'NEAREST',\n wrapS = 'CLAMP_TO_EDGE',\n wrapT = 'CLAMP_TO_EDGE',\n width,\n height,\n }: TextureDefinition,\n data?: ArrayBufferView | null,\n): WebGLTexture {\n const texture = gl.createTexture()\n\n function getTextureConstant(name: string) {\n if (!(name in gl)) {\n throw new Error(`Attempted to create webgl2-only texture (${name}) in webgl1`)\n }\n return gl[name as keyof typeof gl] as any\n }\n\n gl.bindTexture(gl[target], texture)\n gl.texImage2D(\n gl[target],\n 0,\n getTextureConstant(internalFormat),\n width,\n height,\n 0,\n getTextureConstant(format),\n getTextureConstant(type),\n data ?? null,\n )\n\n console.log(minFilter)\n // Set texture parameters\n gl.texParameteri(gl[target], gl.TEXTURE_MIN_FILTER, gl[minFilter])\n gl.texParameteri(gl[target], gl.TEXTURE_MAG_FILTER, gl[magFilter])\n gl.texParameteri(gl[target], gl.TEXTURE_WRAP_S, gl[wrapS])\n gl.texParameteri(gl[target], gl.TEXTURE_WRAP_T, gl[wrapT])\n\n return texture\n}\n\n/**********************************************************************************/\n/* */\n/* Create Framebuffer */\n/* */\n/**********************************************************************************/\n\nconst FRAMEBUFFER_ATTACHMENT_MAP = {\n color: 'COLOR_ATTACHMENT0',\n depth: 'DEPTH_ATTACHMENT',\n stencil: 'STENCIL_ATTACHMENT',\n depthStencil: 'DEPTH_STENCIL_ATTACHMENT',\n} as const\n\nclass FramebufferError extends Error {\n constructor(gl: GL, status: number) {\n let errorMessage = `Framebuffer '${name}' not complete. Status: `\n switch (status) {\n case gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:\n errorMessage += 'FRAMEBUFFER_INCOMPLETE_ATTACHMENT'\n break\n case gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:\n errorMessage += 'FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT'\n break\n case gl.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:\n errorMessage += 'FRAMEBUFFER_INCOMPLETE_DIMENSIONS'\n break\n case gl.FRAMEBUFFER_UNSUPPORTED:\n errorMessage += 'FRAMEBUFFER_UNSUPPORTED'\n break\n default:\n errorMessage += `Unknown (${status})`\n }\n super(errorMessage)\n }\n}\n\nexport function createFramebuffer(\n gl: GL,\n { attachment, texture, ...options }: FramebufferDefinition,\n) {\n // Create texture for the framebuffer\n texture ??= createTexture(gl, options)\n\n // Create framebuffer\n const framebuffer = assertedNotNullish(\n gl.createFramebuffer(),\n `Failed to create framebuffer: ${name}`,\n )\n\n gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer)\n\n gl.framebufferTexture2D(\n gl.FRAMEBUFFER,\n // Determine attachment point based on attachment kind\n gl[FRAMEBUFFER_ATTACHMENT_MAP[attachment]],\n gl.TEXTURE_2D,\n texture,\n 0,\n )\n\n // Check framebuffer completeness\n const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER)\n if (status !== gl.FRAMEBUFFER_COMPLETE) {\n throw new FramebufferError(gl, status)\n }\n\n return {\n texture,\n framebuffer,\n }\n}\n\n/**********************************************************************************/\n/* */\n/* Create Upsert */\n/* */\n/**********************************************************************************/\n\ntype UpsertMapKind<T, U> = Map<T, U> | (T extends WeakKey ? WeakMap<T, U> : never)\n\nexport function createUpsertMap<T extends UpsertMapKind<any, any> = Map<any, any>>(\n constructor?: new () => T,\n) {\n const map = new (constructor ?? Map)() as T\n return Object.assign(map, {\n getOrInsert(\n key: T extends UpsertMapKind<infer U, any> ? U : never,\n value: T extends UpsertMapKind<any, infer U> ? () => U : never,\n ) {\n let result = map.get(key)\n if (result) {\n return result\n }\n result = value()\n map.set(key, result)\n return result\n },\n })\n}\n\nexport function forEachObject<T extends Record<string, any>>(\n value: T,\n callback: (value: T[keyof T], key: keyof T, index: number) => void,\n) {\n let index = 0\n for (const key in value) {\n callback(value[key], key, index)\n index++\n }\n for (const key of Object.getOwnPropertySymbols(value)) {\n callback(value[key as keyof T], key as keyof T, index)\n index++\n }\n}\n\nexport function mapObject<T extends Record<string, any>, TReturn>(\n value: T,\n callback: (value: T[keyof T], key: keyof T, index: number) => TReturn,\n): { [TKey in keyof T]: TReturn } {\n const result = {} as { [TKey in keyof T]: TReturn }\n\n forEachObject(value, (value, key, index) => {\n result[key] = callback(value, key, index)\n })\n\n return result\n}\n","import { getInstancedArrays, getVertexArrayObject } from './gl'\nimport type {\n AttributeFormat,\n AttributeKind,\n AttributeMethods,\n AttributeSchema,\n AttributeView,\n BufferSchema,\n BufferView,\n GL,\n UniformView as InferUniformView,\n InterleavedAttributeMethods,\n InterleavedAttributeSchema,\n InterleavedAttributeView,\n UniformSchema,\n View,\n ViewOptions,\n ViewSchema,\n} from './types'\nimport {\n isSamplerKind,\n kindToUniformFnName,\n isMatKind,\n assertedNotNullish,\n kindToSize,\n forEachObject,\n} from './utils'\nimport { createUpsertMap, mapObject } from './utils'\nexport * from './types'\n\n/**********************************************************************************/\n/* */\n/* To ID */\n/* */\n/**********************************************************************************/\n\nlet index = 0\nconst PREFIX = 'VIEW_GL_ALIAS'\n// Firefox has currently a bug where it can not handle symbol as WeakRef / key for WeakMap\n// see https://bugzilla.mozilla.org/show_bug.cgi?id=1710433\nconst HAS_WEAK_SYMBOL = (() => {\n try {\n const symbol = Symbol()\n new WeakMap().set(symbol, '')\n return true\n } catch {\n return false\n }\n})()\n// NOTE: fallback to Map will currently cause a minor memory leak.\nconst SYMBOL_MAP = createUpsertMap(HAS_WEAK_SYMBOL ? WeakMap<symbol, string> : Map<symbol, string>)\n\nexport function toID(key: string | number | symbol) {\n if (typeof key === 'string') {\n return key\n }\n if (typeof key === 'symbol') {\n return SYMBOL_MAP.getOrInsert(key, () => `${PREFIX}_${index++}`)\n }\n return key.toString()\n}\n\n/**********************************************************************************/\n/* */\n/* View */\n/* */\n/**********************************************************************************/\n\nexport function view<TSchema extends ViewSchema>(\n gl: GL,\n program: WebGLProgram,\n schema: TSchema,\n options?: ViewOptions,\n): View<TSchema> {\n return {\n uniforms: !schema.uniforms ? undefined : uniformView(gl, program, schema.uniforms),\n attributes: !schema.attributes\n ? undefined\n : attributeView(gl, program, schema.attributes, options),\n interleavedAttributes: !schema.interleavedAttributes\n ? undefined\n : interleavedAttributeView(gl, program, schema.interleavedAttributes, options),\n buffers: !schema.buffers ? undefined : bufferView(gl, schema.buffers),\n } as View<TSchema>\n}\n\n/**********************************************************************************/\n/* */\n/* Uniform View */\n/* */\n/**********************************************************************************/\n\nexport function uniformView<T extends UniformSchema>(\n gl: GL,\n program: WebGLProgram,\n schema: T,\n): InferUniformView<T> {\n // @ts-expect-error - conditional return type based on size is hard to infer\n return mapObject(schema, ({ kind, size }, key) => {\n const name = toID(key)\n\n if (isSamplerKind(kind)) {\n const location = gl.getUniformLocation(program, name)\n return {\n set(arg: number) {\n gl.uniform1i(location, arg)\n },\n }\n }\n\n // Array uniform - support both bulk set and indexed access\n if (size) {\n const bulkFnName = `uniform${kindToUniformFnName(kind)}v`\n // @ts-ignore FIX WEBGL/WEBGL2 TYPES\n const bulkFn = gl[bulkFnName].bind(gl)\n const bulkLocation = gl.getUniformLocation(program, name)\n\n const elementFnName = `uniform${kindToUniformFnName(kind)}`\n // @ts-ignore FIX WEBGL/WEBGL2 TYPES\n const elementFn = gl[elementFnName].bind(gl)\n\n // Build array of element setters\n const elements: Array<{ set(...args: any[]): void }> = []\n for (let i = 0; i < size; i++) {\n const location = gl.getUniformLocation(program, `${name}[${i}]`)\n elements[i] = {\n set(...args: any[]) {\n elementFn(location, ...args)\n },\n }\n }\n\n // Return array-like object with bulk set\n return Object.assign(elements, {\n set(arg: Float32Array) {\n bulkFn(bulkLocation, arg)\n },\n })\n }\n\n // Non-array uniform\n const location = gl.getUniformLocation(program, name)\n const fnName = `uniform${kindToUniformFnName(kind)}`\n\n // @ts-ignore FIX WEBGL/WEBGL2 TYPES\n const fn = gl[fnName].bind(gl)\n\n if (isMatKind(kind)) {\n return {\n set(...args: any[]) {\n fn(location, false, args[0])\n },\n }\n }\n\n return {\n set(...args: any[]) {\n fn(location, ...args)\n },\n }\n })\n}\n\n/**********************************************************************************/\n/* */\n/* Attribute View */\n/* */\n/**********************************************************************************/\n\ntype GLType =\n | 'FLOAT'\n | 'INT'\n | 'UNSIGNED_INT'\n | 'SHORT'\n | 'UNSIGNED_SHORT'\n | 'BYTE'\n | 'UNSIGNED_BYTE'\n\nconst FORMAT_TO_GL_TYPE: Record<AttributeFormat, GLType> = {\n float32: 'FLOAT',\n int32: 'INT',\n uint32: 'UNSIGNED_INT',\n int16: 'SHORT',\n uint16: 'UNSIGNED_SHORT',\n int8: 'BYTE',\n uint8: 'UNSIGNED_BYTE',\n}\n\nconst FORMAT_BYTE_SIZE: Record<AttributeFormat, number> = {\n float32: 4,\n int32: 4,\n uint32: 4,\n int16: 2,\n uint16: 2,\n int8: 1,\n uint8: 1,\n}\n\nfunction defaultFormat(kind: AttributeKind): AttributeFormat {\n if (kind.startsWith('u')) return 'uint32'\n if (kind.startsWith('i')) return 'int32'\n return 'float32'\n}\n\n// Shared attribute helper functions\n// between attributeView and interleavedAttributeView\nfunction handleAttribute(\n gl: GL,\n location: number,\n size: number,\n stride: number,\n offset: number,\n glType: GLType,\n isIntegerKind: boolean,\n normalized: boolean,\n instanced?: boolean,\n) {\n gl.enableVertexAttribArray(location)\n if (isIntegerKind) {\n ;(gl as WebGL2RenderingContext).vertexAttribIPointer(location, size, gl[glType], stride, offset)\n } else {\n gl.vertexAttribPointer(location, size, gl[glType], normalized, stride, offset)\n }\n\n // Always stated, never inherited: the divisor belongs to the location rather\n // than to the program, so a location left at 1 by an earlier instanced\n // attribute would feed one value to every vertex here.\n // Get instanced-arrays-feature: extension if webgl, gl if webgl2\n const instancedArrays = getInstancedArrays(gl)\n if (instanced) {\n assertedNotNullish(instancedArrays).vertexAttribDivisor(location, 1)\n } else if (instancedArrays) {\n instancedArrays.vertexAttribDivisor(location, 0)\n }\n}\n\n// Same enum in webgl2 and in the webgl1 extensions, which expose no constants\nconst VERTEX_ATTRIB_ARRAY_DIVISOR = 0x88fe\nconst VERTEX_ARRAY_BINDING = 0x85b5\n\n// Reads the divisor currently attached to a location, 0 where instancing is\n// unavailable and nothing can have set one\nfunction readDivisor(gl: GL, location: number): number {\n return getInstancedArrays(gl)\n ? (gl.getVertexAttrib(location, VERTEX_ATTRIB_ARRAY_DIVISOR) as number)\n : 0\n}\n\n// Wraps a restore so it runs once: a disposer is per bind() and undoing twice\n// would put back state a later bind() is relying on\nfunction once(restore: () => void): () => void {\n let done = false\n return () => {\n if (done) return\n done = true\n restore()\n }\n}\n\nexport function attributeView<T extends AttributeSchema>(\n gl: GL,\n program: WebGLProgram,\n schema: T,\n { signal }: ViewOptions = {},\n): AttributeView<T> {\n const attributes = mapObject(\n schema,\n (\n {\n kind,\n format,\n normalized = false,\n instanced,\n buffer = assertedNotNullish(gl.createBuffer()),\n },\n key,\n ): AttributeMethods => {\n const name = toID(key)\n\n const location = gl.getAttribLocation(program, name)\n if (location < 0) {\n throw new Error(`Attribute '${name}' not found`)\n }\n\n const size = kindToSize(kind)\n const resolvedFormat = format ?? defaultFormat(kind)\n const glType = FORMAT_TO_GL_TYPE[resolvedFormat]\n const isIntKind = kind.startsWith('i') || kind.startsWith('u')\n\n return {\n buffer,\n bind() {\n // Snapshot before the change, restore in the disposer\n const previousDivisor = readDivisor(gl, location)\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n handleAttribute(gl, location, size, 0, 0, glType, isIntKind, normalized, instanced)\n return once(() => {\n getInstancedArrays(gl)?.vertexAttribDivisor(location, previousDivisor)\n })\n },\n dispose() {\n gl.deleteBuffer(buffer)\n },\n set(data, usage = 'STATIC_DRAW') {\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n gl.bufferData(gl.ARRAY_BUFFER, data, gl[usage])\n return this\n },\n }\n },\n )\n\n signal?.addEventListener('abort', function dispose() {\n forEachObject(attributes, value => value.dispose())\n })\n\n return attributes\n}\n\n/**********************************************************************************/\n/* */\n/* Interleaved Attribute View */\n/* */\n/**********************************************************************************/\n\nexport function interleavedAttributeView<T extends InterleavedAttributeSchema>(\n gl: GL,\n program: WebGLProgram,\n schema: T,\n { signal }: ViewOptions = {},\n): InterleavedAttributeView<T> {\n // Initialize interleaved attributes\n const interleavedAttributes = mapObject(schema, ({ layout, instanced }) => {\n // Increment number to keep track of offset\n let index = 0\n\n // Calculate layout information\n const locations: number[] = []\n const handles = layout.map(layout => {\n const name = toID(layout.key)\n\n const location = gl.getAttribLocation(program, name)\n\n if (location < 0) {\n throw new Error(`Attribute '${name}' not found`)\n }\n\n locations.push(location)\n\n const size = kindToSize(layout.kind)\n const offset = index\n const resolvedFormat = layout.format ?? defaultFormat(layout.kind)\n const glType = FORMAT_TO_GL_TYPE[resolvedFormat]\n const isIntKind = layout.kind.startsWith('i') || layout.kind.startsWith('u')\n const normalized = layout.normalized ?? false\n index += size * FORMAT_BYTE_SIZE[resolvedFormat]\n\n return () =>\n handleAttribute(\n gl,\n location,\n size,\n stride,\n offset,\n glType,\n isIntKind,\n normalized,\n instanced,\n )\n })\n\n // Set stride to final index\n const stride = index\n\n // Create a buffer\n const buffer = assertedNotNullish(gl.createBuffer())\n\n // Create VAO to cache attribute state\n let vao: { unbind(): void; bind(): void; dispose(): void } | undefined = undefined\n\n // Get VAO-feature: extension if webgl1, gl if webgl2\n const feature = getVertexArrayObject(gl)\n if (feature) {\n const vertexArray = feature.createVertexArray()\n feature.bindVertexArray(vertexArray)\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n for (const handle of handles) {\n handle()\n }\n feature.bindVertexArray(null)\n vao = {\n unbind() {\n feature.bindVertexArray(null)\n },\n bind() {\n feature.bindVertexArray(vertexArray)\n },\n dispose() {\n feature.deleteVertexArray(vertexArray)\n },\n }\n }\n\n // A selected vertex array stays selected for every later draw, which would\n // then write its own attributes into this one\n const unbind = () => {\n if (vao) {\n vao.unbind()\n }\n }\n\n return {\n bind() {\n // Snapshot before the change, restore in the disposer\n const previousVertexArray = feature\n ? (gl.getParameter(VERTEX_ARRAY_BINDING) as WebGLVertexArrayObject | null)\n : null\n const previousDivisors = vao\n ? []\n : locations.map(location => [location, readDivisor(gl, location)] as const)\n\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n if (vao) {\n vao.bind()\n } else {\n // Fallback: manual attribute setup\n for (const handle of handles) {\n handle()\n }\n }\n\n return once(() => {\n if (feature) {\n feature.bindVertexArray(previousVertexArray)\n }\n // Without a vertex array the manual setup wrote divisors straight to\n // the locations, so they are ours to put back\n if (!vao) {\n const instancedArrays = getInstancedArrays(gl)\n previousDivisors.forEach(([location, divisor]) => {\n instancedArrays?.vertexAttribDivisor(location, divisor)\n })\n }\n })\n },\n unbind,\n dispose() {\n gl.deleteBuffer(buffer)\n if (vao) {\n vao.dispose()\n }\n },\n set(value, usage = 'STATIC_DRAW') {\n if (vao) {\n vao.bind()\n }\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n gl.bufferData(gl.ARRAY_BUFFER, value, gl[usage])\n if (vao) {\n vao.unbind()\n }\n },\n } satisfies InterleavedAttributeMethods\n })\n\n signal?.addEventListener('abort', function dispose() {\n forEachObject(interleavedAttributes, value => value.dispose())\n })\n\n return interleavedAttributes\n}\n\n/**********************************************************************************/\n/* */\n/* Buffer View */\n/* */\n/**********************************************************************************/\n\nexport function bufferView<T extends BufferSchema>(\n gl: GL,\n schema: T,\n { signal }: ViewOptions = {},\n): BufferView<T> {\n // Initialize buffers\n const buffers = mapObject(schema, ({ target = 'ARRAY_BUFFER', usage = 'STATIC_DRAW' }) => {\n const buffer = assertedNotNullish(gl.createBuffer())\n\n return {\n bind() {\n // Snapshot before the change, restore in the disposer. Restoring the\n // previous buffer rather than null matters for ELEMENT_ARRAY_BUFFER,\n // whose binding is recorded in the bound vertex array: clearing it\n // would strip the index buffer from an array the caller may not own.\n const previousBuffer = gl.getParameter(gl[`${target}_BINDING`]) as WebGLBuffer | null\n gl.bindBuffer(gl[target], buffer)\n return once(() => {\n gl.bindBuffer(gl[target], previousBuffer)\n })\n },\n dispose() {\n gl.deleteBuffer(buffer)\n },\n set(data: Float32Array) {\n gl.bindBuffer(gl[target], buffer)\n gl.bufferData(gl[target], data, gl[usage])\n },\n }\n })\n\n signal?.addEventListener('abort', function dispose() {\n forEachObject(buffers, value => value.dispose())\n })\n\n return buffers\n}\n"],"mappings":";;;;;;A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AO,GAAG,KAAK,CAAC;AAC/C,YAAI,KAAK;AACP,cAAI,OAAO;AAAA,QACb;AAAA,MACF;AAAA,IACF;AAAA,EACF,CAAC;AAED,UAAQ,iBAAiB,SAAS,SAAS,UAAU;AACnD,kBAAc,uBAAuB,WAAS,MAAM,QAAQ,CAAC;AAAA,EAC/D,CAAC;AAED,SAAO;AACT;AAQO,SAAS,WACd,IACA,QACA,EAAE,OAAO,IAAiB,CAAC,GACZ;AAEf,QAAM,UAAU,UAAU,QAAQ,CAAC,EAAE,SAAS,gBAAgB,QAAQ,cAAc,MAAM;AACxF,UAAM,SAAS,mBAAmB,GAAG,aAAa,CAAC;AAEnD,WAAO;AAAA,MACL,OAAO;AAKL,cAAM,iBAAiB,GAAG,aAAa,GAAG,GAAG,MAAM,UAAU,CAAC;AAC9D,WAAG,WAAW,GAAG,MAAM,GAAG,MAAM;AAChC,eAAO,KAAK,MAAM;AAChB,aAAG,WAAW,GAAG,MAAM,GAAG,cAAc;AAAA,QAC1C,CAAC;AAAA,MACH;AAAA,MACA,UAAU;AACR,WAAG,aAAa,MAAM;AAAA,MACxB;AAAA,MACA,IAAI,MAAoB;AACtB,WAAG,WAAW,GAAG,MAAM,GAAG,MAAM;AAChC,WAAG,WAAW,GAAG,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;AAAA,MAC3C;AAAA,IACF;AAAA,EACF,CAAC;AAED,UAAQ,iBAAiB,SAAS,SAAS,UAAU;AACnD,kBAAc,SAAS,WAAS,MAAM,QAAQ,CAAC;AAAA,EACjD,CAAC;AAED,SAAO;AACT;","names":["index","value","name","location","index","layout"]}
package/dist/gl.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { G as GL, R as RemoveSuffix } from './types-CXqYX05f.mjs';
1
+ import { G as GL, R as RemoveSuffix } from './types-DTSPQclW.mjs';
2
2
 
3
3
  /**********************************************************************************/
4
4
  /**********************************************************************************/
package/dist/gl.mjs CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  createShader,
4
4
  getInstancedArrays,
5
5
  getVertexArrayObject
6
- } from "./chunk-WSNE4JVQ.mjs";
6
+ } from "./chunk-CJTT7WPV.mjs";
7
7
  export {
8
8
  createProgram,
9
9
  createShader,
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { V as ViewSchema, G as GL, a as ViewOptions, b as View, U as UniformSchema, c as UniformView, A as AttributeSchema, d as AttributeView, I as InterleavedAttributeSchema, e as InterleavedAttributeView, B as BufferSchema, f as BufferView } from './types-CXqYX05f.mjs';
2
- export { z as AttributeDefinition, y as AttributeFormat, x as AttributeKind, C as AttributeMethods, w as AttributeShortHand, a6 as AttributeToken, a7 as AttributeTokenFn, H as BufferDefinition, J as BufferMethods, ae as CompileResult, ac as FlattenSlots, Z as Format, M as Framebuffer, L as FramebufferDefinition, a3 as GLSL, a1 as GLSLResult, a2 as GLSLSlot, ab as GLSLSlotsToSchema, a9 as GLSLToSchema, aa as GLSLToView, g as GLTarget, l as GLTextureFilter, j as GLTextureFormat, i as GLTextureTarget, k as GLTextureType, m as GLTextureWrap, h as GLUsage, a8 as InterleaveToken, E as InterleavedAttributeDefinition, D as InterleavedAttributeLayout, F as InterleavedAttributeMethods, Y as InternalFormat, n as KIND_TO_SIZE_MAP, K as KIND_TO_UNIFORM_FN_NAME_MAP, ad as MergeGLSLSchema, _ as PixelType, $ as TexImage2DOptions, T as TextureDefinition, P as TextureMethods, S as TextureParameterName, W as TextureParameterValue, X as TextureParameters, O as TextureSchema, N as TextureTarget, Q as TextureView, t as UniformArrayMethods, r as UniformDefinition, s as UniformElementMethods, o as UniformFnName, p as UniformKind, q as UniformKindMap, u as UniformMethods, a4 as UniformToken, a5 as UniformTokenFn, v as UniformValue, a0 as ViewSchemaPartial } from './types-CXqYX05f.mjs';
1
+ import { V as ViewSchema, G as GL, a as ViewOptions, b as View, U as UniformSchema, c as UniformView, A as AttributeSchema, d as AttributeView, I as InterleavedAttributeSchema, e as InterleavedAttributeView, B as BufferSchema, f as BufferView } from './types-DTSPQclW.mjs';
2
+ export { z as AttributeDefinition, y as AttributeFormat, x as AttributeKind, C as AttributeMethods, w as AttributeShortHand, a6 as AttributeToken, a7 as AttributeTokenFn, H as BufferDefinition, J as BufferMethods, ae as CompileResult, ac as FlattenSlots, Z as Format, M as Framebuffer, L as FramebufferDefinition, a3 as GLSL, a1 as GLSLResult, a2 as GLSLSlot, ab as GLSLSlotsToSchema, a9 as GLSLToSchema, aa as GLSLToView, g as GLTarget, l as GLTextureFilter, j as GLTextureFormat, i as GLTextureTarget, k as GLTextureType, m as GLTextureWrap, h as GLUsage, a8 as InterleaveToken, E as InterleavedAttributeDefinition, D as InterleavedAttributeLayout, F as InterleavedAttributeMethods, Y as InternalFormat, n as KIND_TO_SIZE_MAP, K as KIND_TO_UNIFORM_FN_NAME_MAP, ad as MergeGLSLSchema, _ as PixelType, $ as TexImage2DOptions, T as TextureDefinition, P as TextureMethods, S as TextureParameterName, W as TextureParameterValue, X as TextureParameters, O as TextureSchema, N as TextureTarget, Q as TextureView, t as UniformArrayMethods, r as UniformDefinition, s as UniformElementMethods, o as UniformFnName, p as UniformKind, q as UniformKindMap, u as UniformMethods, a4 as UniformToken, a5 as UniformTokenFn, v as UniformValue, a0 as ViewSchemaPartial } from './types-DTSPQclW.mjs';
3
3
 
4
4
  declare function toID(key: string | number | symbol): any;
5
5
  /**********************************************************************************/
package/dist/index.mjs CHANGED
@@ -5,8 +5,8 @@ import {
5
5
  toID,
6
6
  uniformView,
7
7
  view
8
- } from "./chunk-CLMGHUTT.mjs";
9
- import "./chunk-WSNE4JVQ.mjs";
8
+ } from "./chunk-ZP4HP5H7.mjs";
9
+ import "./chunk-CJTT7WPV.mjs";
10
10
  export {
11
11
  attributeView,
12
12
  bufferView,
package/dist/tag.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { a2 as GLSLSlot, a3 as GLSL, a5 as UniformTokenFn, a7 as AttributeTokenFn, a6 as AttributeToken, x as AttributeKind, z as AttributeDefinition, a8 as InterleaveToken, a0 as ViewSchemaPartial, ae as CompileResult, af as Prettify, a9 as GLSLToSchema, G as GL } from './types-CXqYX05f.mjs';
1
+ import { a2 as GLSLSlot, a3 as GLSL, a5 as UniformTokenFn, a7 as AttributeTokenFn, a6 as AttributeToken, x as AttributeKind, z as AttributeDefinition, a8 as InterleaveToken, a0 as ViewSchemaPartial, ae as CompileResult, af as Prettify, a9 as GLSLToSchema, G as GL } from './types-DTSPQclW.mjs';
2
2
 
3
3
  declare function glsl<TSlot extends GLSLSlot, const TSlots extends TSlot[]>(template: TemplateStringsArray, ...slots: [...TSlots]): GLSL<TSlots>;
4
4
  declare const uniform: {
package/dist/tag.mjs CHANGED
@@ -2,10 +2,10 @@ import {
2
2
  createUpsertMap,
3
3
  toID,
4
4
  view
5
- } from "./chunk-CLMGHUTT.mjs";
5
+ } from "./chunk-ZP4HP5H7.mjs";
6
6
  import {
7
7
  createProgram
8
- } from "./chunk-WSNE4JVQ.mjs";
8
+ } from "./chunk-CJTT7WPV.mjs";
9
9
 
10
10
  // src/tag.ts
11
11
  function glsl(template, ...slots) {
@@ -212,9 +212,10 @@ type FormatToArray = {
212
212
  };
213
213
  interface AttributeMethods<T extends AttributeDefinition = AttributeDefinition> {
214
214
  buffer: WebGLBuffer;
215
- bind(): void;
215
+ /** Binds the attribute, returning a disposer that restores the divisor */
216
+ bind(): () => void;
216
217
  set(data: FormatToArray[ResolvedFormat<T>], usage?: GLUsage): {
217
- bind(): void;
218
+ bind(): () => void;
218
219
  };
219
220
  dispose(): void;
220
221
  }
@@ -235,7 +236,8 @@ interface InterleavedAttributeDefinition {
235
236
  }
236
237
  type InterleavedAttributeSchema = Record<string | symbol, InterleavedAttributeDefinition>;
237
238
  interface InterleavedAttributeMethods<T extends InterleavedAttributeLayout[] = InterleavedAttributeLayout[]> {
238
- bind(): void;
239
+ /** Binds the vertex array, returning a disposer that restores the previous one */
240
+ bind(): () => void;
239
241
  unbind(): void;
240
242
  set(data: Float32Array, usage?: GLUsage): void;
241
243
  dispose(): void;
@@ -252,7 +254,8 @@ interface BufferDefinition {
252
254
  type BufferSchema = Record<string | symbol, BufferDefinition>;
253
255
  interface BufferMethods {
254
256
  set(data: Float32Array | Uint16Array | Uint32Array): void;
255
- bind(): void;
257
+ /** Binds the buffer, returning a disposer that restores the previous binding */
258
+ bind(): () => void;
256
259
  dispose(): void;
257
260
  }
258
261
  type BufferView<T extends BufferSchema> = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigmistqke/view.gl",
3
- "version": "0.1.18",
3
+ "version": "0.1.19",
4
4
  "description": "👀 Utilities for managing WebGL resources.",
5
5
  "exports": {
6
6
  ".": {
package/src/gl.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { RemoveSuffix } from './type-utils'
2
2
  import type { GL } from './types'
3
3
 
4
- function isWebGL2RenderingContext(gl: WebGLRenderingContext | WebGL2RenderingContext): gl is WebGL2RenderingContext {
4
+ function isWebGL2RenderingContext(
5
+ gl: WebGLRenderingContext | WebGL2RenderingContext,
6
+ ): gl is WebGL2RenderingContext {
5
7
  return typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext
6
8
  }
7
9
 
package/src/index.ts CHANGED
@@ -222,9 +222,38 @@ function handleAttribute(
222
222
  gl.vertexAttribPointer(location, size, gl[glType], normalized, stride, offset)
223
223
  }
224
224
 
225
+ // Always stated, never inherited: the divisor belongs to the location rather
226
+ // than to the program, so a location left at 1 by an earlier instanced
227
+ // attribute would feed one value to every vertex here.
228
+ // Get instanced-arrays-feature: extension if webgl, gl if webgl2
229
+ const instancedArrays = getInstancedArrays(gl)
225
230
  if (instanced) {
226
- // Get instanced-arrays-feature: extension if webgl, gl if webgl2
227
- assertedNotNullish(getInstancedArrays(gl)).vertexAttribDivisor(location, 1)
231
+ assertedNotNullish(instancedArrays).vertexAttribDivisor(location, 1)
232
+ } else if (instancedArrays) {
233
+ instancedArrays.vertexAttribDivisor(location, 0)
234
+ }
235
+ }
236
+
237
+ // Same enum in webgl2 and in the webgl1 extensions, which expose no constants
238
+ const VERTEX_ATTRIB_ARRAY_DIVISOR = 0x88fe
239
+ const VERTEX_ARRAY_BINDING = 0x85b5
240
+
241
+ // Reads the divisor currently attached to a location, 0 where instancing is
242
+ // unavailable and nothing can have set one
243
+ function readDivisor(gl: GL, location: number): number {
244
+ return getInstancedArrays(gl)
245
+ ? (gl.getVertexAttrib(location, VERTEX_ATTRIB_ARRAY_DIVISOR) as number)
246
+ : 0
247
+ }
248
+
249
+ // Wraps a restore so it runs once: a disposer is per bind() and undoing twice
250
+ // would put back state a later bind() is relying on
251
+ function once(restore: () => void): () => void {
252
+ let done = false
253
+ return () => {
254
+ if (done) return
255
+ done = true
256
+ restore()
228
257
  }
229
258
  }
230
259
 
@@ -237,7 +266,13 @@ export function attributeView<T extends AttributeSchema>(
237
266
  const attributes = mapObject(
238
267
  schema,
239
268
  (
240
- { kind, format, normalized = false, instanced, buffer = assertedNotNullish(gl.createBuffer()) },
269
+ {
270
+ kind,
271
+ format,
272
+ normalized = false,
273
+ instanced,
274
+ buffer = assertedNotNullish(gl.createBuffer()),
275
+ },
241
276
  key,
242
277
  ): AttributeMethods => {
243
278
  const name = toID(key)
@@ -255,8 +290,13 @@ export function attributeView<T extends AttributeSchema>(
255
290
  return {
256
291
  buffer,
257
292
  bind() {
293
+ // Snapshot before the change, restore in the disposer
294
+ const previousDivisor = readDivisor(gl, location)
258
295
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
259
296
  handleAttribute(gl, location, size, 0, 0, glType, isIntKind, normalized, instanced)
297
+ return once(() => {
298
+ getInstancedArrays(gl)?.vertexAttribDivisor(location, previousDivisor)
299
+ })
260
300
  },
261
301
  dispose() {
262
302
  gl.deleteBuffer(buffer)
@@ -295,6 +335,7 @@ export function interleavedAttributeView<T extends InterleavedAttributeSchema>(
295
335
  let index = 0
296
336
 
297
337
  // Calculate layout information
338
+ const locations: number[] = []
298
339
  const handles = layout.map(layout => {
299
340
  const name = toID(layout.key)
300
341
 
@@ -304,6 +345,8 @@ export function interleavedAttributeView<T extends InterleavedAttributeSchema>(
304
345
  throw new Error(`Attribute '${name}' not found`)
305
346
  }
306
347
 
348
+ locations.push(location)
349
+
307
350
  const size = kindToSize(layout.kind)
308
351
  const offset = index
309
352
  const resolvedFormat = layout.format ?? defaultFormat(layout.kind)
@@ -312,7 +355,18 @@ export function interleavedAttributeView<T extends InterleavedAttributeSchema>(
312
355
  const normalized = layout.normalized ?? false
313
356
  index += size * FORMAT_BYTE_SIZE[resolvedFormat]
314
357
 
315
- return () => handleAttribute(gl, location, size, stride, offset, glType, isIntKind, normalized, instanced)
358
+ return () =>
359
+ handleAttribute(
360
+ gl,
361
+ location,
362
+ size,
363
+ stride,
364
+ offset,
365
+ glType,
366
+ isIntKind,
367
+ normalized,
368
+ instanced,
369
+ )
316
370
  })
317
371
 
318
372
  // Set stride to final index
@@ -347,8 +401,24 @@ export function interleavedAttributeView<T extends InterleavedAttributeSchema>(
347
401
  }
348
402
  }
349
403
 
404
+ // A selected vertex array stays selected for every later draw, which would
405
+ // then write its own attributes into this one
406
+ const unbind = () => {
407
+ if (vao) {
408
+ vao.unbind()
409
+ }
410
+ }
411
+
350
412
  return {
351
413
  bind() {
414
+ // Snapshot before the change, restore in the disposer
415
+ const previousVertexArray = feature
416
+ ? (gl.getParameter(VERTEX_ARRAY_BINDING) as WebGLVertexArrayObject | null)
417
+ : null
418
+ const previousDivisors = vao
419
+ ? []
420
+ : locations.map(location => [location, readDivisor(gl, location)] as const)
421
+
352
422
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
353
423
  if (vao) {
354
424
  vao.bind()
@@ -358,12 +428,22 @@ export function interleavedAttributeView<T extends InterleavedAttributeSchema>(
358
428
  handle()
359
429
  }
360
430
  }
431
+
432
+ return once(() => {
433
+ if (feature) {
434
+ feature.bindVertexArray(previousVertexArray)
435
+ }
436
+ // Without a vertex array the manual setup wrote divisors straight to
437
+ // the locations, so they are ours to put back
438
+ if (!vao) {
439
+ const instancedArrays = getInstancedArrays(gl)
440
+ previousDivisors.forEach(([location, divisor]) => {
441
+ instancedArrays?.vertexAttribDivisor(location, divisor)
442
+ })
443
+ }
444
+ })
361
445
  },
362
- unbind() {
363
- if (vao) {
364
- vao.unbind()
365
- }
366
- },
446
+ unbind,
367
447
  dispose() {
368
448
  gl.deleteBuffer(buffer)
369
449
  if (vao) {
@@ -404,9 +484,18 @@ export function bufferView<T extends BufferSchema>(
404
484
  // Initialize buffers
405
485
  const buffers = mapObject(schema, ({ target = 'ARRAY_BUFFER', usage = 'STATIC_DRAW' }) => {
406
486
  const buffer = assertedNotNullish(gl.createBuffer())
487
+
407
488
  return {
408
489
  bind() {
490
+ // Snapshot before the change, restore in the disposer. Restoring the
491
+ // previous buffer rather than null matters for ELEMENT_ARRAY_BUFFER,
492
+ // whose binding is recorded in the bound vertex array: clearing it
493
+ // would strip the index buffer from an array the caller may not own.
494
+ const previousBuffer = gl.getParameter(gl[`${target}_BINDING`]) as WebGLBuffer | null
409
495
  gl.bindBuffer(gl[target], buffer)
496
+ return once(() => {
497
+ gl.bindBuffer(gl[target], previousBuffer)
498
+ })
410
499
  },
411
500
  dispose() {
412
501
  gl.deleteBuffer(buffer)
package/src/types.ts CHANGED
@@ -285,14 +285,7 @@ export type AttributeKind =
285
285
  | 'uvec3'
286
286
  | 'uvec4'
287
287
 
288
- export type AttributeFormat =
289
- | 'float32'
290
- | 'int32'
291
- | 'uint32'
292
- | 'int16'
293
- | 'uint16'
294
- | 'int8'
295
- | 'uint8'
288
+ export type AttributeFormat = 'float32' | 'int32' | 'uint32' | 'int16' | 'uint16' | 'int8' | 'uint8'
296
289
 
297
290
  export interface AttributeDefinition {
298
291
  kind: AttributeKind
@@ -313,7 +306,9 @@ type DefaultFormat<K extends AttributeKind> = K extends IntKind
313
306
  ? 'uint32'
314
307
  : 'float32'
315
308
 
316
- type ResolvedFormat<T extends AttributeDefinition> = T extends { format: infer F extends AttributeFormat }
309
+ type ResolvedFormat<T extends AttributeDefinition> = T extends {
310
+ format: infer F extends AttributeFormat
311
+ }
317
312
  ? F
318
313
  : DefaultFormat<T['kind']>
319
314
 
@@ -329,8 +324,9 @@ type FormatToArray = {
329
324
 
330
325
  export interface AttributeMethods<T extends AttributeDefinition = AttributeDefinition> {
331
326
  buffer: WebGLBuffer
332
- bind(): void
333
- set(data: FormatToArray[ResolvedFormat<T>], usage?: GLUsage): { bind(): void }
327
+ /** Binds the attribute, returning a disposer that restores the divisor */
328
+ bind(): () => void
329
+ set(data: FormatToArray[ResolvedFormat<T>], usage?: GLUsage): { bind(): () => void }
334
330
  dispose(): void
335
331
  }
336
332
 
@@ -361,7 +357,8 @@ export type InterleavedAttributeSchema = Record<string | symbol, InterleavedAttr
361
357
  export interface InterleavedAttributeMethods<
362
358
  T extends InterleavedAttributeLayout[] = InterleavedAttributeLayout[],
363
359
  > {
364
- bind(): void
360
+ /** Binds the vertex array, returning a disposer that restores the previous one */
361
+ bind(): () => void
365
362
  unbind(): void
366
363
  set(data: Float32Array, usage?: GLUsage): void
367
364
  dispose(): void
@@ -386,7 +383,8 @@ export type BufferSchema = Record<string | symbol, BufferDefinition>
386
383
 
387
384
  export interface BufferMethods {
388
385
  set(data: Float32Array | Uint16Array | Uint32Array): void
389
- bind(): void
386
+ /** Binds the buffer, returning a disposer that restores the previous binding */
387
+ bind(): () => void
390
388
  dispose(): void
391
389
  }
392
390
 
@@ -409,6 +409,64 @@ describe('attributeView', () => {
409
409
  expect(gl.vertexAttribDivisor).toHaveBeenCalledWith(expect.any(Number), 1)
410
410
  })
411
411
 
412
+ it('should return a disposer from bind that clears the instancing divisor', () => {
413
+ const schema = {
414
+ a_instancePosition: { kind: 'vec3', instanced: true },
415
+ } satisfies AttributeSchema
416
+
417
+ const attributes = attributeView(gl, program, schema)
418
+
419
+ // The divisor is global to the location rather than to the program, so
420
+ // leaving it at 1 makes the next non-instanced attribute bound there read
421
+ // a single value for every vertex.
422
+ const unbind = attributes.a_instancePosition.bind()
423
+ expect(gl.vertexAttribDivisor).toHaveBeenCalledWith(expect.any(Number), 1)
424
+
425
+ unbind()
426
+ expect(gl.vertexAttribDivisor).toHaveBeenLastCalledWith(expect.any(Number), 0)
427
+ })
428
+
429
+ it('should state a zero divisor for non-instanced attributes rather than inherit one', () => {
430
+ const schema = {
431
+ a_position: { kind: 'vec2' },
432
+ } satisfies AttributeSchema
433
+
434
+ const attributes = attributeView(gl, program, schema)
435
+
436
+ // A location left at 1 by an earlier instanced attribute would otherwise
437
+ // feed a single value to every vertex here.
438
+ attributes.a_position.bind()
439
+ expect(gl.vertexAttribDivisor).toHaveBeenCalledWith(expect.any(Number), 0)
440
+ })
441
+
442
+ it('should restore the previous divisor rather than assume it was zero', () => {
443
+ const schema = {
444
+ a_instancePosition: { kind: 'vec3', instanced: true },
445
+ } satisfies AttributeSchema
446
+
447
+ const attributes = attributeView(gl, program, schema)
448
+ ;(gl.getVertexAttrib as any).mockReturnValue(2)
449
+
450
+ const unbind = attributes.a_instancePosition.bind()
451
+ unbind()
452
+ expect(gl.vertexAttribDivisor).toHaveBeenLastCalledWith(expect.any(Number), 2)
453
+ })
454
+
455
+ it('should return a disposer that only restores once', () => {
456
+ const schema = {
457
+ a_instancePosition: { kind: 'vec3', instanced: true },
458
+ } satisfies AttributeSchema
459
+
460
+ const attributes = attributeView(gl, program, schema)
461
+
462
+ const unbind = attributes.a_instancePosition.bind()
463
+ unbind()
464
+ const callsAfterFirst = (gl.vertexAttribDivisor as any).mock.calls.length
465
+ // Undoing twice would put back state a later bind() is relying on.
466
+ unbind()
467
+ expect((gl.vertexAttribDivisor as any).mock.calls).toHaveLength(callsAfterFirst)
468
+ })
469
+
412
470
  it('should use custom buffer if provided', () => {
413
471
  const customBuffer = gl.createBuffer()!
414
472
  const schema = {
@@ -726,6 +784,34 @@ describe('interleavedAttributeView', () => {
726
784
  expect(interleavedAttributes.vertexData.set).toBeInstanceOf(Function)
727
785
  })
728
786
 
787
+ it('should return a disposer from bind that restores the default vertex array', () => {
788
+ const schema = {
789
+ vertexData: {
790
+ layout: [
791
+ { key: 'a_position', kind: 'vec3' },
792
+ { key: 'a_color', kind: 'vec4' },
793
+ ],
794
+ instanced: true,
795
+ },
796
+ } satisfies InterleavedAttributeSchema
797
+
798
+ const interleavedAttributes = interleavedAttributeView(gl, program, schema)
799
+
800
+ // A bound vertex array stays selected for everything drawn afterwards, so
801
+ // the next draw would write its own attributes into THIS array and corrupt
802
+ // the layer that owns it. The disposer hands the default back.
803
+ // Restoring the previous array rather than null keeps nested binds working.
804
+ const previous = { id: 'previously-bound-vao' }
805
+ ;(gl.getParameter as any).mockReturnValue(previous)
806
+
807
+ const unbind = interleavedAttributes.vertexData.bind()
808
+ expect(unbind).toBeInstanceOf(Function)
809
+
810
+ ;(gl.bindVertexArray as any).mockClear()
811
+ unbind()
812
+ expect(gl.bindVertexArray).toHaveBeenCalledWith(previous)
813
+ })
814
+
729
815
  it('should calculate correct stride and offsets', () => {
730
816
  const schema = {
731
817
  vertexData: {
@@ -1080,6 +1166,26 @@ describe('bufferView', () => {
1080
1166
  expect(gl.bindBuffer).toHaveBeenCalledWith(gl.ELEMENT_ARRAY_BUFFER, expect.any(Object))
1081
1167
  })
1082
1168
 
1169
+ it('should return a disposer from bind that releases the target', () => {
1170
+ const schema = {
1171
+ indices: { target: 'ELEMENT_ARRAY_BUFFER' },
1172
+ } satisfies BufferSchema
1173
+
1174
+ const buffers = bufferView(gl, schema)
1175
+
1176
+ // An ELEMENT_ARRAY_BUFFER binding is recorded in whichever vertex array is
1177
+ // bound, so restoring the previous buffer rather than null keeps the
1178
+ // disposer from stripping the index buffer off an array we do not own.
1179
+ const previous = { id: 'previously-bound' }
1180
+ ;(gl.getParameter as any).mockReturnValue(previous)
1181
+
1182
+ const unbind = buffers.indices.bind()
1183
+ expect(unbind).toBeInstanceOf(Function)
1184
+
1185
+ unbind()
1186
+ expect(gl.bindBuffer).toHaveBeenLastCalledWith(gl.ELEMENT_ARRAY_BUFFER, previous)
1187
+ })
1188
+
1083
1189
  it('should set buffer data with specified usage', () => {
1084
1190
  const schema = {
1085
1191
  vertices: { target: 'ARRAY_BUFFER', usage: 'STATIC_DRAW' },
@@ -38,6 +38,8 @@ export function createMockGL() {
38
38
  // Constants
39
39
  ARRAY_BUFFER: 0x8892,
40
40
  ELEMENT_ARRAY_BUFFER: 0x8893,
41
+ ARRAY_BUFFER_BINDING: 0x8894,
42
+ ELEMENT_ARRAY_BUFFER_BINDING: 0x8895,
41
43
  STATIC_DRAW: 0x88e4,
42
44
  DYNAMIC_DRAW: 0x88e8,
43
45
  FLOAT: 0x1406,
@@ -127,6 +129,12 @@ export function createMockGL() {
127
129
  vertexAttribIPointer: vi.fn(),
128
130
  vertexAttribDivisor: vi.fn(),
129
131
 
132
+ // State queries. Real contexts always have these; the disposers returned by
133
+ // bind() snapshot through them. Default to "nothing bound / no divisor",
134
+ // which tests override where the previous value is what is under test.
135
+ getParameter: vi.fn(() => null),
136
+ getVertexAttrib: vi.fn(() => 0),
137
+
130
138
  // Uniform methods
131
139
  getUniformLocation: vi.fn((program: MockWebGLProgram, name: string) => {
132
140
  return { id: nextId++, name } as WebGLUniformLocation
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":["import type {\n AttributeKind,\n FramebufferDefinition,\n GL,\n KIND_TO_SIZE_MAP,\n KIND_TO_UNIFORM_FN_NAME_MAP,\n TextureDefinition,\n UniformKind,\n} from './types'\n\nexport function assertedNotNullish<T>(value: T, message?: string): NonNullable<T> {\n if (value === undefined || value === null) throw new Error(message)\n return value\n}\n\n/**********************************************************************************/\n/* */\n/* Kind To Uniform Name */\n/* */\n/**********************************************************************************/\n\nexport const kindToUniformFnName = <T extends UniformKind>(\n kind: T,\n): KIND_TO_UNIFORM_FN_NAME_MAP[T] => {\n switch (kind[0]) {\n // mat\n case 'm':\n return ('Matrix' + kind.slice(3) + 'fv') as KIND_TO_UNIFORM_FN_NAME_MAP[T]\n // sampler/booleans/integer\n case 's':\n case 'b':\n case 'i':\n return ((kind.match(/\\d/)?.[0] ?? '1') + 'i') as KIND_TO_UNIFORM_FN_NAME_MAP[T]\n // unsigned integers\n case 'u':\n return ((kind[4] || '1') + 'ui') as KIND_TO_UNIFORM_FN_NAME_MAP[T]\n // vec\n case 'v':\n return (kind[3] + 'f') as KIND_TO_UNIFORM_FN_NAME_MAP[T]\n default:\n switch (kind) {\n case 'float':\n return '1f' as KIND_TO_UNIFORM_FN_NAME_MAP[T]\n case 'uint':\n return '1ui' as KIND_TO_UNIFORM_FN_NAME_MAP[T]\n default:\n return '1i' as KIND_TO_UNIFORM_FN_NAME_MAP[T]\n }\n }\n}\n\n/**********************************************************************************/\n/* */\n/* Kind To Size */\n/* */\n/**********************************************************************************/\n\nexport const kindToSize = <T extends UniformKind>(kind: T): KIND_TO_SIZE_MAP[T] => {\n switch (kind[0]) {\n case 'm':\n const [a, b] = kind.match(/\\d+/g)!.map(Number) as [number, number?]\n return (a * (b ?? a)) as KIND_TO_SIZE_MAP[T]\n case 'v':\n case 'i':\n case 'b':\n case 'u':\n const match = kind.match(/\\d/) as [string] | undefined\n return (match ? +match[0] : 1) as KIND_TO_SIZE_MAP[T]\n default:\n return 1 as KIND_TO_SIZE_MAP[T]\n }\n}\n\n/**********************************************************************************/\n/* */\n/* Guards */\n/* */\n/**********************************************************************************/\n\nexport const isMatKind = <\n T extends UniformKind | AttributeKind,\n TPrefix extends string,\n TPostfix extends string,\n>(\n kind: T,\n): kind is T & `${TPrefix}mat${TPostfix}` => kind.includes('mat')\n\nexport const isSamplerKind = <TPrefix extends string, TPostfix extends string>(\n kind: UniformKind,\n): kind is UniformKind & `${TPrefix}sampler${TPostfix}` => kind.includes('sampler')\n\nexport const isVecKind = <TPrefix extends string, TPostfix extends string>(\n kind: UniformKind,\n): kind is UniformKind & `${TPrefix}vec${TPostfix}` => kind.includes('vec')\n\n/**********************************************************************************/\n/* */\n/* Create Texture */\n/* */\n/**********************************************************************************/\n\nexport function createTexture(\n gl: GL,\n {\n target = 'TEXTURE_2D',\n internalFormat = 'RGBA',\n format = 'RGBA',\n type = 'UNSIGNED_BYTE',\n minFilter = 'NEAREST',\n magFilter = 'NEAREST',\n wrapS = 'CLAMP_TO_EDGE',\n wrapT = 'CLAMP_TO_EDGE',\n width,\n height,\n }: TextureDefinition,\n data?: ArrayBufferView | null,\n): WebGLTexture {\n const texture = gl.createTexture()\n\n function getTextureConstant(name: string) {\n if (!(name in gl)) {\n throw new Error(`Attempted to create webgl2-only texture (${name}) in webgl1`)\n }\n return gl[name as keyof typeof gl] as any\n }\n\n gl.bindTexture(gl[target], texture)\n gl.texImage2D(\n gl[target],\n 0,\n getTextureConstant(internalFormat),\n width,\n height,\n 0,\n getTextureConstant(format),\n getTextureConstant(type),\n data ?? null,\n )\n\n console.log(minFilter)\n // Set texture parameters\n gl.texParameteri(gl[target], gl.TEXTURE_MIN_FILTER, gl[minFilter])\n gl.texParameteri(gl[target], gl.TEXTURE_MAG_FILTER, gl[magFilter])\n gl.texParameteri(gl[target], gl.TEXTURE_WRAP_S, gl[wrapS])\n gl.texParameteri(gl[target], gl.TEXTURE_WRAP_T, gl[wrapT])\n\n return texture\n}\n\n/**********************************************************************************/\n/* */\n/* Create Framebuffer */\n/* */\n/**********************************************************************************/\n\nconst FRAMEBUFFER_ATTACHMENT_MAP = {\n color: 'COLOR_ATTACHMENT0',\n depth: 'DEPTH_ATTACHMENT',\n stencil: 'STENCIL_ATTACHMENT',\n depthStencil: 'DEPTH_STENCIL_ATTACHMENT',\n} as const\n\nclass FramebufferError extends Error {\n constructor(gl: GL, status: number) {\n let errorMessage = `Framebuffer '${name}' not complete. Status: `\n switch (status) {\n case gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:\n errorMessage += 'FRAMEBUFFER_INCOMPLETE_ATTACHMENT'\n break\n case gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:\n errorMessage += 'FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT'\n break\n case gl.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:\n errorMessage += 'FRAMEBUFFER_INCOMPLETE_DIMENSIONS'\n break\n case gl.FRAMEBUFFER_UNSUPPORTED:\n errorMessage += 'FRAMEBUFFER_UNSUPPORTED'\n break\n default:\n errorMessage += `Unknown (${status})`\n }\n super(errorMessage)\n }\n}\n\nexport function createFramebuffer(\n gl: GL,\n { attachment, texture, ...options }: FramebufferDefinition,\n) {\n // Create texture for the framebuffer\n texture ??= createTexture(gl, options)\n\n // Create framebuffer\n const framebuffer = assertedNotNullish(\n gl.createFramebuffer(),\n `Failed to create framebuffer: ${name}`,\n )\n\n gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer)\n\n gl.framebufferTexture2D(\n gl.FRAMEBUFFER,\n // Determine attachment point based on attachment kind\n gl[FRAMEBUFFER_ATTACHMENT_MAP[attachment]],\n gl.TEXTURE_2D,\n texture,\n 0,\n )\n\n // Check framebuffer completeness\n const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER)\n if (status !== gl.FRAMEBUFFER_COMPLETE) {\n throw new FramebufferError(gl, status)\n }\n\n return {\n texture,\n framebuffer,\n }\n}\n\n/**********************************************************************************/\n/* */\n/* Create Upsert */\n/* */\n/**********************************************************************************/\n\ntype UpsertMapKind<T, U> = Map<T, U> | (T extends WeakKey ? WeakMap<T, U> : never)\n\nexport function createUpsertMap<T extends UpsertMapKind<any, any> = Map<any, any>>(\n constructor?: new () => T,\n) {\n const map = new (constructor ?? Map)() as T\n return Object.assign(map, {\n getOrInsert(\n key: T extends UpsertMapKind<infer U, any> ? U : never,\n value: T extends UpsertMapKind<any, infer U> ? () => U : never,\n ) {\n let result = map.get(key)\n if (result) {\n return result\n }\n result = value()\n map.set(key, result)\n return result\n },\n })\n}\n\nexport function forEachObject<T extends Record<string, any>>(\n value: T,\n callback: (value: T[keyof T], key: keyof T, index: number) => void,\n) {\n let index = 0\n for (const key in value) {\n callback(value[key], key, index)\n index++\n }\n for (const key of Object.getOwnPropertySymbols(value)) {\n callback(value[key as keyof T], key as keyof T, index)\n index++\n }\n}\n\nexport function mapObject<T extends Record<string, any>, TReturn>(\n value: T,\n callback: (value: T[keyof T], key: keyof T, index: number) => TReturn,\n): { [TKey in keyof T]: TReturn } {\n const result = {} as { [TKey in keyof T]: TReturn }\n\n forEachObject(value, (value, key, index) => {\n result[key] = callback(value, key, index)\n })\n\n return result\n}\n","import { getInstancedArrays, getVertexArrayObject } from './gl'\nimport type {\n AttributeFormat,\n AttributeKind,\n AttributeMethods,\n AttributeSchema,\n AttributeView,\n BufferSchema,\n BufferView,\n GL,\n UniformView as InferUniformView,\n InterleavedAttributeMethods,\n InterleavedAttributeSchema,\n InterleavedAttributeView,\n UniformSchema,\n View,\n ViewOptions,\n ViewSchema,\n} from './types'\nimport {\n isSamplerKind,\n kindToUniformFnName,\n isMatKind,\n assertedNotNullish,\n kindToSize,\n forEachObject,\n} from './utils'\nimport { createUpsertMap, mapObject } from './utils'\nexport * from './types'\n\n/**********************************************************************************/\n/* */\n/* To ID */\n/* */\n/**********************************************************************************/\n\nlet index = 0\nconst PREFIX = 'VIEW_GL_ALIAS'\n// Firefox has currently a bug where it can not handle symbol as WeakRef / key for WeakMap\n// see https://bugzilla.mozilla.org/show_bug.cgi?id=1710433\nconst HAS_WEAK_SYMBOL = (() => {\n try {\n const symbol = Symbol()\n new WeakMap().set(symbol, '')\n return true\n } catch {\n return false\n }\n})()\n// NOTE: fallback to Map will currently cause a minor memory leak.\nconst SYMBOL_MAP = createUpsertMap(HAS_WEAK_SYMBOL ? WeakMap<symbol, string> : Map<symbol, string>)\n\nexport function toID(key: string | number | symbol) {\n if (typeof key === 'string') {\n return key\n }\n if (typeof key === 'symbol') {\n return SYMBOL_MAP.getOrInsert(key, () => `${PREFIX}_${index++}`)\n }\n return key.toString()\n}\n\n/**********************************************************************************/\n/* */\n/* View */\n/* */\n/**********************************************************************************/\n\nexport function view<TSchema extends ViewSchema>(\n gl: GL,\n program: WebGLProgram,\n schema: TSchema,\n options?: ViewOptions,\n): View<TSchema> {\n return {\n uniforms: !schema.uniforms ? undefined : uniformView(gl, program, schema.uniforms),\n attributes: !schema.attributes\n ? undefined\n : attributeView(gl, program, schema.attributes, options),\n interleavedAttributes: !schema.interleavedAttributes\n ? undefined\n : interleavedAttributeView(gl, program, schema.interleavedAttributes, options),\n buffers: !schema.buffers ? undefined : bufferView(gl, schema.buffers),\n } as View<TSchema>\n}\n\n/**********************************************************************************/\n/* */\n/* Uniform View */\n/* */\n/**********************************************************************************/\n\nexport function uniformView<T extends UniformSchema>(\n gl: GL,\n program: WebGLProgram,\n schema: T,\n): InferUniformView<T> {\n // @ts-expect-error - conditional return type based on size is hard to infer\n return mapObject(schema, ({ kind, size }, key) => {\n const name = toID(key)\n\n if (isSamplerKind(kind)) {\n const location = gl.getUniformLocation(program, name)\n return {\n set(arg: number) {\n gl.uniform1i(location, arg)\n },\n }\n }\n\n // Array uniform - support both bulk set and indexed access\n if (size) {\n const bulkFnName = `uniform${kindToUniformFnName(kind)}v`\n // @ts-ignore FIX WEBGL/WEBGL2 TYPES\n const bulkFn = gl[bulkFnName].bind(gl)\n const bulkLocation = gl.getUniformLocation(program, name)\n\n const elementFnName = `uniform${kindToUniformFnName(kind)}`\n // @ts-ignore FIX WEBGL/WEBGL2 TYPES\n const elementFn = gl[elementFnName].bind(gl)\n\n // Build array of element setters\n const elements: Array<{ set(...args: any[]): void }> = []\n for (let i = 0; i < size; i++) {\n const location = gl.getUniformLocation(program, `${name}[${i}]`)\n elements[i] = {\n set(...args: any[]) {\n elementFn(location, ...args)\n },\n }\n }\n\n // Return array-like object with bulk set\n return Object.assign(elements, {\n set(arg: Float32Array) {\n bulkFn(bulkLocation, arg)\n },\n })\n }\n\n // Non-array uniform\n const location = gl.getUniformLocation(program, name)\n const fnName = `uniform${kindToUniformFnName(kind)}`\n\n // @ts-ignore FIX WEBGL/WEBGL2 TYPES\n const fn = gl[fnName].bind(gl)\n\n if (isMatKind(kind)) {\n return {\n set(...args: any[]) {\n fn(location, false, args[0])\n },\n }\n }\n\n return {\n set(...args: any[]) {\n fn(location, ...args)\n },\n }\n })\n}\n\n/**********************************************************************************/\n/* */\n/* Attribute View */\n/* */\n/**********************************************************************************/\n\ntype GLType =\n | 'FLOAT'\n | 'INT'\n | 'UNSIGNED_INT'\n | 'SHORT'\n | 'UNSIGNED_SHORT'\n | 'BYTE'\n | 'UNSIGNED_BYTE'\n\nconst FORMAT_TO_GL_TYPE: Record<AttributeFormat, GLType> = {\n float32: 'FLOAT',\n int32: 'INT',\n uint32: 'UNSIGNED_INT',\n int16: 'SHORT',\n uint16: 'UNSIGNED_SHORT',\n int8: 'BYTE',\n uint8: 'UNSIGNED_BYTE',\n}\n\nconst FORMAT_BYTE_SIZE: Record<AttributeFormat, number> = {\n float32: 4,\n int32: 4,\n uint32: 4,\n int16: 2,\n uint16: 2,\n int8: 1,\n uint8: 1,\n}\n\nfunction defaultFormat(kind: AttributeKind): AttributeFormat {\n if (kind.startsWith('u')) return 'uint32'\n if (kind.startsWith('i')) return 'int32'\n return 'float32'\n}\n\n// Shared attribute helper functions\n// between attributeView and interleavedAttributeView\nfunction handleAttribute(\n gl: GL,\n location: number,\n size: number,\n stride: number,\n offset: number,\n glType: GLType,\n isIntegerKind: boolean,\n normalized: boolean,\n instanced?: boolean,\n) {\n gl.enableVertexAttribArray(location)\n if (isIntegerKind) {\n ;(gl as WebGL2RenderingContext).vertexAttribIPointer(location, size, gl[glType], stride, offset)\n } else {\n gl.vertexAttribPointer(location, size, gl[glType], normalized, stride, offset)\n }\n\n if (instanced) {\n // Get instanced-arrays-feature: extension if webgl, gl if webgl2\n assertedNotNullish(getInstancedArrays(gl)).vertexAttribDivisor(location, 1)\n }\n}\n\nexport function attributeView<T extends AttributeSchema>(\n gl: GL,\n program: WebGLProgram,\n schema: T,\n { signal }: ViewOptions = {},\n): AttributeView<T> {\n const attributes = mapObject(\n schema,\n (\n { kind, format, normalized = false, instanced, buffer = assertedNotNullish(gl.createBuffer()) },\n key,\n ): AttributeMethods => {\n const name = toID(key)\n\n const location = gl.getAttribLocation(program, name)\n if (location < 0) {\n throw new Error(`Attribute '${name}' not found`)\n }\n\n const size = kindToSize(kind)\n const resolvedFormat = format ?? defaultFormat(kind)\n const glType = FORMAT_TO_GL_TYPE[resolvedFormat]\n const isIntKind = kind.startsWith('i') || kind.startsWith('u')\n\n return {\n buffer,\n bind() {\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n handleAttribute(gl, location, size, 0, 0, glType, isIntKind, normalized, instanced)\n },\n dispose() {\n gl.deleteBuffer(buffer)\n },\n set(data, usage = 'STATIC_DRAW') {\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n gl.bufferData(gl.ARRAY_BUFFER, data, gl[usage])\n return this\n },\n }\n },\n )\n\n signal?.addEventListener('abort', function dispose() {\n forEachObject(attributes, value => value.dispose())\n })\n\n return attributes\n}\n\n/**********************************************************************************/\n/* */\n/* Interleaved Attribute View */\n/* */\n/**********************************************************************************/\n\nexport function interleavedAttributeView<T extends InterleavedAttributeSchema>(\n gl: GL,\n program: WebGLProgram,\n schema: T,\n { signal }: ViewOptions = {},\n): InterleavedAttributeView<T> {\n // Initialize interleaved attributes\n const interleavedAttributes = mapObject(schema, ({ layout, instanced }) => {\n // Increment number to keep track of offset\n let index = 0\n\n // Calculate layout information\n const handles = layout.map(layout => {\n const name = toID(layout.key)\n\n const location = gl.getAttribLocation(program, name)\n\n if (location < 0) {\n throw new Error(`Attribute '${name}' not found`)\n }\n\n const size = kindToSize(layout.kind)\n const offset = index\n const resolvedFormat = layout.format ?? defaultFormat(layout.kind)\n const glType = FORMAT_TO_GL_TYPE[resolvedFormat]\n const isIntKind = layout.kind.startsWith('i') || layout.kind.startsWith('u')\n const normalized = layout.normalized ?? false\n index += size * FORMAT_BYTE_SIZE[resolvedFormat]\n\n return () => handleAttribute(gl, location, size, stride, offset, glType, isIntKind, normalized, instanced)\n })\n\n // Set stride to final index\n const stride = index\n\n // Create a buffer\n const buffer = assertedNotNullish(gl.createBuffer())\n\n // Create VAO to cache attribute state\n let vao: { unbind(): void; bind(): void; dispose(): void } | undefined = undefined\n\n // Get VAO-feature: extension if webgl1, gl if webgl2\n const feature = getVertexArrayObject(gl)\n if (feature) {\n const vertexArray = feature.createVertexArray()\n feature.bindVertexArray(vertexArray)\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n for (const handle of handles) {\n handle()\n }\n feature.bindVertexArray(null)\n vao = {\n unbind() {\n feature.bindVertexArray(null)\n },\n bind() {\n feature.bindVertexArray(vertexArray)\n },\n dispose() {\n feature.deleteVertexArray(vertexArray)\n },\n }\n }\n\n return {\n bind() {\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n if (vao) {\n vao.bind()\n } else {\n // Fallback: manual attribute setup\n for (const handle of handles) {\n handle()\n }\n }\n },\n unbind() {\n if (vao) {\n vao.unbind()\n }\n },\n dispose() {\n gl.deleteBuffer(buffer)\n if (vao) {\n vao.dispose()\n }\n },\n set(value, usage = 'STATIC_DRAW') {\n if (vao) {\n vao.bind()\n }\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n gl.bufferData(gl.ARRAY_BUFFER, value, gl[usage])\n if (vao) {\n vao.unbind()\n }\n },\n } satisfies InterleavedAttributeMethods\n })\n\n signal?.addEventListener('abort', function dispose() {\n forEachObject(interleavedAttributes, value => value.dispose())\n })\n\n return interleavedAttributes\n}\n\n/**********************************************************************************/\n/* */\n/* Buffer View */\n/* */\n/**********************************************************************************/\n\nexport function bufferView<T extends BufferSchema>(\n gl: GL,\n schema: T,\n { signal }: ViewOptions = {},\n): BufferView<T> {\n // Initialize buffers\n const buffers = mapObject(schema, ({ target = 'ARRAY_BUFFER', usage = 'STATIC_DRAW' }) => {\n const buffer = assertedNotNullish(gl.createBuffer())\n return {\n bind() {\n gl.bindBuffer(gl[target], buffer)\n },\n dispose() {\n gl.deleteBuffer(buffer)\n },\n set(data: Float32Array) {\n gl.bindBuffer(gl[target], buffer)\n gl.bufferData(gl[target], data, gl[usage])\n },\n }\n })\n\n signal?.addEventListener('abort', function dispose() {\n forEachObject(buffers, value => value.dispose())\n })\n\n return buffers\n}\n"],"mappings":";;;;;;AAUO,SAAS,mBAAsB,OAAU,SAAkC;AAChF,MAAI,UAAU,UAAa,UAAU,KAAM,OAAM,IAAI,MAAM,OAAO;AAClE,SAAO;AACT;AAQO,IAAM,sBAAsB,CACjC,SACmC;AACnC,UAAQ,KAAK,CAAC,GAAG;AAAA;AAAA,IAEf,KAAK;AACH,aAAQ,WAAW,KAAK,MAAM,CAAC,IAAI;AAAA;AAAA,IAErC,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AACH,cAAS,KAAK,MAAM,IAAI,IAAI,CAAC,KAAK,OAAO;AAAA;AAAA,IAE3C,KAAK;AACH,cAAS,KAAK,CAAC,KAAK,OAAO;AAAA;AAAA,IAE7B,KAAK;AACH,aAAQ,KAAK,CAAC,IAAI;AAAA,IACpB;AACE,cAAQ,MAAM;AAAA,QACZ,KAAK;AACH,iBAAO;AAAA,QACT,KAAK;AACH,iBAAO;AAAA,QACT;AACE,iBAAO;AAAA,MACX;AAAA,EACJ;AACF;AAQO,IAAM,aAAa,CAAwB,SAAiC;AACjF,UAAQ,KAAK,CAAC,GAAG;AAAA,IACf,KAAK;AACH,YAAM,CAAC,GAAG,CAAC,IAAI,KAAK,MAAM,MAAM,EAAG,IAAI,MAAM;AAC7C,aAAQ,KAAK,KAAK;AAAA,IACpB,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AACH,YAAM,QAAQ,KAAK,MAAM,IAAI;AAC7B,aAAQ,QAAQ,CAAC,MAAM,CAAC,IAAI;AAAA,IAC9B;AACE,aAAO;AAAA,EACX;AACF;AAQO,IAAM,YAAY,CAKvB,SAC2C,KAAK,SAAS,KAAK;AAEzD,IAAM,gBAAgB,CAC3B,SACyD,K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sources":["../src/gl.ts"],"sourcesContent":["import { RemoveSuffix } from './type-utils'\nimport type { GL } from './types'\n\nfunction isWebGL2RenderingContext(gl: WebGLRenderingContext | WebGL2RenderingContext): gl is WebGL2RenderingContext {\n return typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext\n}\n\n/**********************************************************************************/\n/* */\n/* Create Shader */\n/* */\n/**********************************************************************************/\n\nexport function createShader(gl: GL, type: number, source: string): WebGLShader {\n const shader = gl.createShader(type)\n if (!shader) throw new Error('Failed to create shader')\n\n gl.shaderSource(shader, source)\n gl.compileShader(shader)\n\n if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {\n const info = gl.getShaderInfoLog(shader)\n gl.deleteShader(shader)\n throw new Error(\n `Failed to compile ${type === gl.VERTEX_SHADER ? 'vertex' : 'fragment'} shader: ${info}`,\n )\n }\n\n return shader\n}\n\n/**********************************************************************************/\n/* */\n/* Create Program */\n/* */\n/**********************************************************************************/\n\nexport function createProgram(gl: GL, vertexSource: string, fragmentSource: string): WebGLProgram {\n const program = gl.createProgram()\n if (!program) throw new Error('Failed to create WebGL program')\n\n const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexSource)\n const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentSource)\n\n gl.attachShader(program, vertexShader)\n gl.attachShader(program, fragmentShader)\n gl.linkProgram(program)\n\n if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {\n const info = gl.getProgramInfoLog(program)\n gl.deleteProgram(program)\n gl.deleteShader(vertexShader)\n gl.deleteShader(fragmentShader)\n throw new Error(`Failed to link program: ${info}`)\n }\n\n // Clean up shaders after linking\n gl.deleteShader(vertexShader)\n gl.deleteShader(fragmentShader)\n\n return program\n}\n\n/**********************************************************************************/\n/* */\n/* Get Instanced Arrays */\n/* */\n/**********************************************************************************/\n\nconst INSTANCED_ARRAYS_WRAPPER_MAP = new WeakMap<\n GL,\n RemoveSuffix<\n Pick<\n ANGLE_instanced_arrays,\n 'drawArraysInstancedANGLE' | 'drawElementsInstancedANGLE' | 'vertexAttribDivisorANGLE'\n >,\n 'ANGLE'\n > | null\n>()\nexport function getInstancedArrays(gl: GL) {\n if (isWebGL2RenderingContext(gl)) return gl\n\n const cached = INSTANCED_ARRAYS_WRAPPER_MAP.get(gl)\n if (cached) return cached\n\n const ext = gl.getExtension('ANGLE_instanced_arrays')\n if (!ext) return undefined\n\n const wrapper = {\n drawArraysInstanced: ext.drawArraysInstancedANGLE.bind(ext),\n drawElementsInstanced: ext.drawElementsInstancedANGLE.bind(ext),\n vertexAttribDivisor: ext.vertexAttribDivisorANGLE.bind(ext),\n }\n INSTANCED_ARRAYS_WRAPPER_MAP.set(gl, wrapper)\n\n return wrapper\n}\n\n/**********************************************************************************/\n/* */\n/* Get Vertex Array Object */\n/* */\n/**********************************************************************************/\n\nconst VERTEX_ARRAY_OBJECT_WRAPPER_MAP = new WeakMap<\n GL,\n RemoveSuffix<\n Pick<\n OES_vertex_array_object,\n 'bindVertexArrayOES' | 'createVertexArrayOES' | 'deleteVertexArrayOES'\n >,\n 'OES'\n > | null\n>()\nexport function getVertexArrayObject(gl: GL) {\n if (isWebGL2RenderingContext(gl)) return gl\n\n const cached = VERTEX_ARRAY_OBJECT_WRAPPER_MAP.get(gl)\n if (cached) return cached\n\n const ext = gl.getExtension('OES_vertex_array_object')\n if (!ext) return null\n\n const wrapper = {\n bindVertexArray: ext.bindVertexArrayOES.bind(ext),\n createVertexArray: ext.createVertexArrayOES.bind(ext),\n deleteVertexArray: ext.deleteVertexArrayOES.bind(ext),\n }\n VERTEX_ARRAY_OBJECT_WRAPPER_MAP.set(gl, wrapper)\n\n return wrapper\n}\n"],"mappings":";AAGA,SAAS,yBAAyB,IAAkF;AAClH,SAAO,OAAO,2BAA2B,eAAe,cAAc;AACxE;AAQO,SAAS,aAAa,IAAQ,MAAc,QAA6B;AAC9E,QAAM,SAAS,GAAG,aAAa,IAAI;AACnC,MAAI,CAAC,OAAQ,OAAM,IA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