@bigmistqke/view.gl 0.1.19 → 0.3.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 +82 -27
- package/dev/dist/404.html +19 -0
- package/dev/dist/assets/bezier-DrA4cU_H.js +292 -0
- package/dev/dist/assets/game-of-life-BYlzWhEL.js +186 -0
- package/dev/dist/assets/glsl-composition-D_BaFFij.js +97 -0
- package/dev/dist/assets/grid-DfXHNLBm.js +181 -0
- package/dev/dist/assets/index-C2N3u8wj.css +12 -0
- package/dev/dist/assets/index-D-N0FDYs.js +110 -0
- package/dev/dist/assets/main-CSE2fki3.js +32 -0
- package/dev/dist/assets/overview-IeUhUflE.js +80 -0
- package/dev/dist/assets/pix-sim-DMJJA_WO.js +500 -0
- package/dev/dist/assets/ray-casting-DTQetDPd.js +411 -0
- package/dev/dist/assets/ray-casting-modules-Dkfw4tdc.js +765 -0
- package/dev/dist/assets/sdf-CS0uBonx.js +66 -0
- package/dev/dist/assets/symbol-BL_7GJ0M.js +67 -0
- package/dev/dist/assets/tag-C0Vskpp8.js +833 -0
- package/dev/dist/assets/tetris-D8mokodr.js +344 -0
- package/dev/dist/assets/utils-2dzuv_bW.js +62 -0
- package/dev/dist/index.html +51 -0
- package/dist/chunk-BZDGEIOT.mjs +278 -0
- package/dist/chunk-BZDGEIOT.mjs.map +1 -0
- package/dist/chunk-X4M3LV4R.mjs +433 -0
- package/dist/chunk-X4M3LV4R.mjs.map +1 -0
- package/dist/gl.d.mts +2 -2
- package/dist/gl.mjs +1 -1
- package/dist/index.d.mts +23 -6
- package/dist/index.mjs +9 -2
- package/dist/tag.d.mts +6 -1
- package/dist/tag.mjs +13 -5
- package/dist/tag.mjs.map +1 -1
- package/dist/{types-DTSPQclW.d.mts → types-CNdqh7tB.d.mts} +121 -8
- package/package.json +1 -1
- package/src/gl.ts +13 -2
- package/src/index.ts +294 -122
- package/src/tag.ts +18 -2
- package/src/types.ts +127 -7
- package/src/utils.ts +45 -10
- package/test/index.test.ts +339 -84
- package/test/mocks/webgl.ts +20 -0
- package/test/tag.test.ts +41 -0
- package/dist/chunk-CJTT7WPV.mjs +0 -75
- package/dist/chunk-CJTT7WPV.mjs.map +0 -1
- package/dist/chunk-ZP4HP5H7.mjs +0 -405
- package/dist/chunk-ZP4HP5H7.mjs.map +0 -1
package/dist/tag.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/tag.ts"],"sourcesContent":["import { toID, view } from '.'\nimport { createProgram } from './gl'\nimport type { Prettify } from './type-utils'\nimport type {\n AttributeDefinition,\n AttributeKind,\n AttributeTokenFn as AttributeTagMethod,\n AttributeToken,\n CompileResult,\n GL,\n GLSL,\n GLSLSlot,\n GLSLToSchema,\n InterleaveToken,\n UniformDefinition,\n UniformKind,\n UniformTokenFn as UniformTagMethod,\n ViewSchema,\n ViewSchemaPartial,\n} from './types'\nimport { createUpsertMap } from './utils'\n\nexport function glsl<TSlot extends GLSLSlot, const TSlots extends TSlot[]>(\n template: TemplateStringsArray,\n ...slots: [...TSlots]\n): GLSL<TSlots> {\n return { template, slots, type: 'glsl' }\n}\n\nexport const uniform = new Proxy(\n {} as {\n [TKey in UniformKind]: UniformTagMethod<TKey>\n },\n {\n get(target, property) {\n // @ts-expect-error\n if (typeof property === 'symbol') return target[property]\n return (key: string, options?: Omit<UniformDefinition, 'kind'>) => ({\n type: 'uniform',\n key,\n kind: property,\n ...options,\n })\n },\n },\n)\n\nexport const attribute = new Proxy(\n {} as {\n [TKey in AttributeKind]: AttributeTagMethod<TKey>\n },\n {\n get(target, property) {\n // @ts-expect-error\n if (typeof property === 'symbol') return target[property]\n return (key: string, options?: Omit<AttributeDefinition, 'kind'>) => ({\n type: 'attribute',\n key,\n kind: property,\n ...options,\n })\n },\n },\n)\n\nexport function interleave<\n const TKey extends string | symbol,\n const TLayout extends Array<AttributeToken<string | symbol, AttributeKind, undefined>>,\n const TOptions extends Omit<AttributeDefinition, 'kind'>,\n>(key: TKey, layout: TLayout, instanced: TOptions): InterleaveToken<TKey, TLayout, TOptions>\nexport function interleave<\n const TKey extends string | symbol,\n const TLayout extends Array<AttributeToken<string | symbol, AttributeKind, undefined>>,\n>(key: TKey, layout: TLayout): InterleaveToken<TKey, TLayout, { instanced: false }>\nexport function interleave<\n const TKey extends string | symbol,\n const TLayout extends Array<AttributeToken<string | symbol, AttributeKind, undefined>>,\n const TOptions extends Omit<AttributeDefinition, 'kind'>,\n>(key: TKey, layout: TLayout, { instanced, buffer }: Partial<TOptions> = {}) {\n return {\n type: 'interleavedAttribute',\n key,\n // remove instanced- and type-property\n layout: layout.map(({ key, kind }) => ({\n key,\n kind,\n })),\n instanced: !!instanced,\n buffer,\n } as unknown as InterleaveToken<TKey, TLayout, TOptions>\n}\n\n/**********************************************************************************/\n/* */\n/* Compile */\n/* */\n/**********************************************************************************/\n\nexport interface CompileOptions<TSchema extends ViewSchemaPartial> {\n schema?: TSchema\n webgl2?: boolean\n}\n\nexport function compile<\n TVertex extends GLSL,\n TFragment extends GLSL,\n TSchema extends ViewSchemaPartial,\n>(\n gl: WebGLRenderingContext,\n vertex: TVertex,\n fragment: TFragment,\n options?: CompileOptions<TSchema> | undefined,\n) {\n const _vertex = resolveGLSLTag(vertex, options) as { template: string; schema: ViewSchema }\n const _fragment = resolveGLSLTag(fragment, options) as { template: string; schema: ViewSchema }\n\n const schema = {\n uniforms: {\n ..._vertex.schema.uniforms,\n ..._fragment.schema.uniforms,\n },\n attributes: {\n ..._vertex.schema.attributes,\n ..._fragment.schema.attributes,\n },\n interleavedAttributes: {\n ..._vertex.schema.interleavedAttributes,\n ..._fragment.schema.interleavedAttributes,\n },\n }\n\n // Deep merge options.schema with generated schema\n for (const kind in options?.schema) {\n const schemaKind = schema[kind as keyof typeof schema]\n const configSchemaKind = options?.schema[kind]\n\n for (const key in configSchemaKind) {\n schemaKind[key] = {\n ...schemaKind[key],\n ...configSchemaKind[key],\n }\n }\n }\n\n try {\n const program = createProgram(gl, _vertex.template, _fragment.template)\n\n return {\n program,\n schema,\n view: view(gl, program, schema),\n vertex: _vertex.template,\n fragment: _fragment.template,\n } as unknown as Prettify<CompileResult<TVertex, TFragment, TSchema>>\n } catch (error) {\n console.error('Error while creating WebGLProgram - vertex\\n\\n', _vertex.template)\n console.error('Error while creating WebGLProgram - fragment\\n\\n', _fragment.template)\n\n throw error\n }\n}\n\nfunction resolveGLSLTag<TTag extends GLSL>(tag: TTag, options?: { webgl2?: boolean }) {\n return {\n template: compile.toString(tag, options),\n schema: compile.toSchema(tag),\n }\n}\n\ncompile.toSchema = function <TTag extends GLSL>(tag: TTag) {\n const result = {\n uniforms: {},\n attributes: {},\n interleavedAttributes: {},\n }\n\n tag.slots.forEach(function handleSlot(slot: GLSLSlot) {\n if (typeof slot !== 'object') {\n return\n }\n\n if (Array.isArray(slot)) {\n slot.forEach(handleSlot)\n return\n }\n\n if (slot.type === 'glsl') {\n const { uniforms, attributes, interleavedAttributes } = compile.toSchema(slot)\n\n result.uniforms = {\n ...result.uniforms,\n ...uniforms,\n }\n result.attributes = {\n ...result.attributes,\n ...attributes,\n }\n result.interleavedAttributes = {\n ...result.interleavedAttributes,\n ...interleavedAttributes,\n }\n\n return\n }\n\n const { key: name, type, ...rest } = slot\n\n // @ts-expect-error\n result[`${type}s`][name] = rest\n })\n\n return result as unknown as Prettify<GLSLToSchema<TTag>>\n}\n\ncompile.toString = function <TTag extends GLSL>(\n { template: [initial, ...rest], slots }: TTag,\n config?: { webgl2?: boolean },\n) {\n const v300 = config?.webgl2 ?? !!initial?.startsWith('#version 300 es')\n\n let template = initial ?? ''\n\n for (let i = 0; i < rest.length; i++) {\n template += `${resolveGlslSlotToString(slots[i]!, v300)}${rest[i]}`\n }\n\n return template\n}\n\nfunction resolveGlslSlotToString(slot: GLSLSlot, v300: boolean): string {\n if (typeof slot !== 'object') {\n return toID(slot)\n }\n\n if (Array.isArray(slot)) {\n return slot.map(slot => resolveGlslSlotToString(slot, v300)).join('')\n }\n\n switch (slot.type) {\n case 'glsl':\n return compile.toString(slot)\n case 'interleavedAttribute':\n return slot.layout.reduce(\n (a, v) =>\n v300 ? `${a}in ${v.kind} ${toID(v.key)};\\n` : `${a}attribute ${v.kind} ${toID(v.key)};\\n`,\n '',\n )\n case 'uniform':\n if ('size' in slot) {\n return `${slot.type} ${slot.kind} ${toID(slot.key)}[${slot.size}];`\n }\n return `${slot.type} ${slot.kind} ${toID(slot.key)};`\n }\n\n if (slot.type === 'attribute') {\n return `${v300 ? 'in' : slot.type} ${slot.kind} ${toID(slot.key)};`\n }\n\n throw new Error(`Unexpected slot: ${JSON.stringify(slot)}`)\n}\n\nconst QUAD_FLOAT_ARRAY = new Float32Array([-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1])\nconst QUAD_BUFFER_MAP = createUpsertMap(WeakMap<GL, WebGLBuffer>)\n\ncompile.toQuad = function <TFragment extends GLSL, TSchema extends ViewSchemaPartial>(\n gl: GL,\n fragment: TFragment,\n options?: CompileOptions<TSchema>,\n) {\n const buffer = QUAD_BUFFER_MAP.getOrInsert(gl, gl.createBuffer.bind(gl))\n\n const webgl2 = options?.webgl2 ?? fragment.template[0]?.startsWith('#version 300 es')\n\n const vertex = webgl2\n ? glsl`#version 300 es\nprecision mediump float;\n\n${attribute.vec2('a_quad', { buffer })}\n\nout vec2 v_uv;\n\nvoid main() {\n v_uv = a_quad;\n gl_Position = vec4(v_uv, 0.0, 1.0);\n}`\n : glsl`precision mediump float;\n\n${attribute.vec2('a_quad', { buffer })}\n\nvarying vec2 v_uv;\n\nvoid main() {\n v_uv = a_quad;\n gl_Position = vec4(v_uv, 0.0, 1.0);\n}`\n\n const result = compile(gl, vertex, fragment, options)\n\n // @ts-expect-error\n result.view.attributes.a_quad.set(QUAD_FLOAT_ARRAY)\n\n return result\n}\n"],"mappings":";;;;;;;;;;AAsBO,SAAS,KACd,aACG,OACW;AACd,SAAO,EAAE,UAAU,OAAO,MAAM,OAAO;AACzC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,IAAI,IAAI,KAAK,KAAK,GAAG,CAAC,IAAI,KAAK,IAAI;AAAA,MACjE;AACA,aAAO,GAAG,KAAK,IAAI,IAAI,KAAK,IAAI,IAAI,KAAK,KAAK,GAAG,CAAC;AAAA,EACtD;AAEA,MAAI,KAAK,SAAS,aAAa;AAC7B,WAAO,GAAG,OAAO,OAAO,KAAK,IAAI,IAAI,KAAK,IAAI,IAAI,KAAK,KAAK,GAAG,CAAC;AAAA,EAClE;AAEA,QAAM,IAAI,MAAM,oBAAoB,KAAK,UAAU,IAAI,CAAC,EAAE;AAC5D;AAEA,IAAM,mBAAmB,IAAI,aAAa,CAAC,IAAI,IAAI,GAAG,IAAI,IAAI,GAAG,IAAI,GAAG,GAAG,IAAI,GAAG,CAAC,CAAC;AACpF,IAAM,kBAAkB,gBAAgB,OAAwB;AAEhE,QAAQ,SAAS,SACf,IACA,UACA,SACA;AACA,QAAM,SAAS,gBAAgB,YAAY,IAAI,GAAG,aAAa,KAAK,EAAE,CAAC;AAEvE,QAAM,SAAS,SAAS,UAAU,SAAS,SAAS,CAAC,GAAG,WAAW,iBAAiB;AAEpF,QAAM,SAAS,SACX;AAAA;AAAA;AAAA,EAGJ,UAAU,KAAK,UAAU,EAAE,OAAO,CAAC,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,KAQhC;AAAA;AAAA,EAEJ,UAAU,KAAK,UAAU,EAAE,OAAO,CAAC,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AASpC,QAAM,SAAS,QAAQ,IAAI,QAAQ,UAAU,OAAO;AAGpD,SAAO,KAAK,WAAW,OAAO,IAAI,gBAAgB;AAElD,SAAO;AACT;","names":["key","slot"]}
|
|
1
|
+
{"version":3,"sources":["../src/tag.ts"],"sourcesContent":["import { toID, view } from '.'\nimport { createProgram } from './gl'\nimport type { Prettify } from './type-utils'\nimport type {\n AttributeDefinition,\n AttributeKind,\n AttributeTokenFn as AttributeTagMethod,\n AttributeToken,\n CompileResult,\n GL,\n GLSL,\n GLSLSlot,\n GLSLToSchema,\n InterleaveToken,\n UniformDefinition,\n UniformKind,\n UniformTokenFn as UniformTagMethod,\n ViewSchema,\n ViewSchemaPartial,\n} from './types'\nimport { createUpsertMap, once } from './utils'\n\nexport function glsl<TSlot extends GLSLSlot, const TSlots extends TSlot[]>(\n template: TemplateStringsArray,\n ...slots: [...TSlots]\n): GLSL<TSlots> {\n return { template, slots, type: 'glsl' }\n}\n\nexport const uniform = new Proxy(\n {} as {\n [TKey in UniformKind]: UniformTagMethod<TKey>\n },\n {\n get(target, property) {\n // @ts-expect-error\n if (typeof property === 'symbol') return target[property]\n return (key: string, options?: Omit<UniformDefinition, 'kind'>) => ({\n type: 'uniform',\n key,\n kind: property,\n ...options,\n })\n },\n },\n)\n\nexport const attribute = new Proxy(\n {} as {\n [TKey in AttributeKind]: AttributeTagMethod<TKey>\n },\n {\n get(target, property) {\n // @ts-expect-error\n if (typeof property === 'symbol') return target[property]\n return (key: string, options?: Omit<AttributeDefinition, 'kind'>) => ({\n type: 'attribute',\n key,\n kind: property,\n ...options,\n })\n },\n },\n)\n\nexport function interleave<\n const TKey extends string | symbol,\n const TLayout extends Array<AttributeToken<string | symbol, AttributeKind, undefined>>,\n const TOptions extends Omit<AttributeDefinition, 'kind'>,\n>(key: TKey, layout: TLayout, instanced: TOptions): InterleaveToken<TKey, TLayout, TOptions>\nexport function interleave<\n const TKey extends string | symbol,\n const TLayout extends Array<AttributeToken<string | symbol, AttributeKind, undefined>>,\n>(key: TKey, layout: TLayout): InterleaveToken<TKey, TLayout, { instanced: false }>\nexport function interleave<\n const TKey extends string | symbol,\n const TLayout extends Array<AttributeToken<string | symbol, AttributeKind, undefined>>,\n const TOptions extends Omit<AttributeDefinition, 'kind'>,\n>(key: TKey, layout: TLayout, { instanced, buffer }: Partial<TOptions> = {}) {\n return {\n type: 'interleavedAttribute',\n key,\n // remove instanced- and type-property\n layout: layout.map(({ key, kind }) => ({\n key,\n kind,\n })),\n instanced: !!instanced,\n buffer,\n } as unknown as InterleaveToken<TKey, TLayout, TOptions>\n}\n\n/**********************************************************************************/\n/* */\n/* Compile */\n/* */\n/**********************************************************************************/\n\nexport interface CompileOptions<TSchema extends ViewSchemaPartial> {\n schema?: TSchema\n webgl2?: boolean\n /**\n * Disposes what `compile` made: the program, and the buffers the view created\n * for itself. Buffers named by the schema belong to whoever made them.\n */\n signal?: AbortSignal\n}\n\nexport function compile<\n TVertex extends GLSL,\n TFragment extends GLSL,\n TSchema extends ViewSchemaPartial,\n>(\n gl: WebGLRenderingContext,\n vertex: TVertex,\n fragment: TFragment,\n options?: CompileOptions<TSchema> | undefined,\n) {\n const _vertex = resolveGLSLTag(vertex, options) as { template: string; schema: ViewSchema }\n const _fragment = resolveGLSLTag(fragment, options) as { template: string; schema: ViewSchema }\n\n const schema = {\n uniforms: {\n ..._vertex.schema.uniforms,\n ..._fragment.schema.uniforms,\n },\n attributes: {\n ..._vertex.schema.attributes,\n ..._fragment.schema.attributes,\n },\n interleavedAttributes: {\n ..._vertex.schema.interleavedAttributes,\n ..._fragment.schema.interleavedAttributes,\n },\n }\n\n // Deep merge options.schema with generated schema\n for (const kind in options?.schema) {\n const schemaKind = schema[kind as keyof typeof schema]\n const configSchemaKind = options?.schema[kind]\n\n for (const key in configSchemaKind) {\n schemaKind[key] = {\n ...schemaKind[key],\n ...configSchemaKind[key],\n }\n }\n }\n\n try {\n const program = createProgram(gl, _vertex.template, _fragment.template)\n\n // The same rule the views follow: `compile` made this program, so `compile`\n // is what deletes it. A program handed in from outside would not be ours to\n // delete, and there is no way to hand one in.\n const resolvedView = view(gl, program, schema)\n const dispose = once(() => {\n resolvedView.dispose()\n gl.deleteProgram(program)\n })\n options?.signal?.addEventListener('abort', dispose)\n\n return {\n program,\n schema,\n view: resolvedView,\n dispose,\n vertex: _vertex.template,\n fragment: _fragment.template,\n } as unknown as Prettify<CompileResult<TVertex, TFragment, TSchema>>\n } catch (error) {\n console.error('Error while creating WebGLProgram - vertex\\n\\n', _vertex.template)\n console.error('Error while creating WebGLProgram - fragment\\n\\n', _fragment.template)\n\n throw error\n }\n}\n\nfunction resolveGLSLTag<TTag extends GLSL>(tag: TTag, options?: { webgl2?: boolean }) {\n return {\n template: compile.toString(tag, options),\n schema: compile.toSchema(tag),\n }\n}\n\ncompile.toSchema = function <TTag extends GLSL>(tag: TTag) {\n const result = {\n uniforms: {},\n attributes: {},\n interleavedAttributes: {},\n }\n\n tag.slots.forEach(function handleSlot(slot: GLSLSlot) {\n if (typeof slot !== 'object') {\n return\n }\n\n if (Array.isArray(slot)) {\n slot.forEach(handleSlot)\n return\n }\n\n if (slot.type === 'glsl') {\n const { uniforms, attributes, interleavedAttributes } = compile.toSchema(slot)\n\n result.uniforms = {\n ...result.uniforms,\n ...uniforms,\n }\n result.attributes = {\n ...result.attributes,\n ...attributes,\n }\n result.interleavedAttributes = {\n ...result.interleavedAttributes,\n ...interleavedAttributes,\n }\n\n return\n }\n\n const { key: name, type, ...rest } = slot\n\n // @ts-expect-error\n result[`${type}s`][name] = rest\n })\n\n return result as unknown as Prettify<GLSLToSchema<TTag>>\n}\n\ncompile.toString = function <TTag extends GLSL>(\n { template: [initial, ...rest], slots }: TTag,\n config?: { webgl2?: boolean },\n) {\n const v300 = config?.webgl2 ?? !!initial?.startsWith('#version 300 es')\n\n let template = initial ?? ''\n\n for (let i = 0; i < rest.length; i++) {\n template += `${resolveGlslSlotToString(slots[i]!, v300)}${rest[i]}`\n }\n\n return template\n}\n\nfunction resolveGlslSlotToString(slot: GLSLSlot, v300: boolean): string {\n if (typeof slot !== 'object') {\n return toID(slot)\n }\n\n if (Array.isArray(slot)) {\n return slot.map(slot => resolveGlslSlotToString(slot, v300)).join('')\n }\n\n switch (slot.type) {\n case 'glsl':\n return compile.toString(slot)\n case 'interleavedAttribute':\n return slot.layout.reduce(\n (a, v) =>\n v300 ? `${a}in ${v.kind} ${toID(v.key)};\\n` : `${a}attribute ${v.kind} ${toID(v.key)};\\n`,\n '',\n )\n case 'uniform':\n if ('size' in slot) {\n return `${slot.type} ${slot.kind} ${toID(slot.key)}[${slot.size}];`\n }\n return `${slot.type} ${slot.kind} ${toID(slot.key)};`\n }\n\n if (slot.type === 'attribute') {\n return `${v300 ? 'in' : slot.type} ${slot.kind} ${toID(slot.key)};`\n }\n\n throw new Error(`Unexpected slot: ${JSON.stringify(slot)}`)\n}\n\nconst QUAD_FLOAT_ARRAY = new Float32Array([-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1])\nconst QUAD_BUFFER_MAP = createUpsertMap(WeakMap<GL, WebGLBuffer>)\n\ncompile.toQuad = function <TFragment extends GLSL, TSchema extends ViewSchemaPartial>(\n gl: GL,\n fragment: TFragment,\n options?: CompileOptions<TSchema>,\n) {\n const buffer = QUAD_BUFFER_MAP.getOrInsert(gl, gl.createBuffer.bind(gl))\n\n const webgl2 = options?.webgl2 ?? fragment.template[0]?.startsWith('#version 300 es')\n\n const vertex = webgl2\n ? glsl`#version 300 es\nprecision mediump float;\n\n${attribute.vec2('a_quad', { buffer })}\n\nout vec2 v_uv;\n\nvoid main() {\n v_uv = a_quad;\n gl_Position = vec4(v_uv, 0.0, 1.0);\n}`\n : glsl`precision mediump float;\n\n${attribute.vec2('a_quad', { buffer })}\n\nvarying vec2 v_uv;\n\nvoid main() {\n v_uv = a_quad;\n gl_Position = vec4(v_uv, 0.0, 1.0);\n}`\n\n const result = compile(gl, vertex, fragment, options)\n\n // @ts-expect-error\n result.view.attributes.a_quad.set(QUAD_FLOAT_ARRAY)\n\n return result\n}\n"],"mappings":";;;;;;;;;;;AAsBO,SAAS,KACd,aACG,OACW;AACd,SAAO,EAAE,UAAU,OAAO,MAAM,OAAO;AACzC;AAEO,IAAM,UAAU,IAAI;AAAA,EACzB,CAAC;AAAA,EAGD;AAAA,IACE,IAAI,QAAQ,UAAU;AAEpB,UAAI,OAAO,aAAa,SAAU,QAAO,OAAO,QAAQ;AACxD,aAAO,CAAC,KAAa,aAA+C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I,IAAI,KAAK,KAAK,GAAG,CAAC,IAAI,KAAK,IAAI;AAAA,MACjE;AACA,aAAO,GAAG,KAAK,IAAI,IAAI,KAAK,IAAI,IAAI,KAAK,KAAK,GAAG,CAAC;AAAA,EACtD;AAEA,MAAI,KAAK,SAAS,aAAa;AAC7B,WAAO,GAAG,OAAO,OAAO,KAAK,IAAI,IAAI,KAAK,IAAI,IAAI,KAAK,KAAK,GAAG,CAAC;AAAA,EAClE;AAEA,QAAM,IAAI,MAAM,oBAAoB,KAAK,UAAU,IAAI,CAAC,EAAE;AAC5D;AAEA,IAAM,mBAAmB,IAAI,aAAa,CAAC,IAAI,IAAI,GAAG,IAAI,IAAI,GAAG,IAAI,GAAG,GAAG,IAAI,GAAG,CAAC,CAAC;AACpF,IAAM,kBAAkB,gBAAgB,OAAwB;AAEhE,QAAQ,SAAS,SACf,IACA,UACA,SACA;AACA,QAAM,SAAS,gBAAgB,YAAY,IAAI,GAAG,aAAa,KAAK,EAAE,CAAC;AAEvE,QAAM,SAAS,SAAS,UAAU,SAAS,SAAS,CAAC,GAAG,WAAW,iBAAiB;AAEpF,QAAM,SAAS,SACX;AAAA;AAAA;AAAA,EAGJ,UAAU,KAAK,UAAU,EAAE,OAAO,CAAC,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,KAQhC;AAAA;AAAA,EAEJ,UAAU,KAAK,UAAU,EAAE,OAAO,CAAC,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AASpC,QAAM,SAAS,QAAQ,IAAI,QAAQ,UAAU,OAAO;AAGpD,SAAO,KAAK,WAAW,OAAO,IAAI,gBAAgB;AAElD,SAAO;AACT;","names":["key","slot"]}
|
|
@@ -210,13 +210,58 @@ type FormatToArray = {
|
|
|
210
210
|
int8: Int8Array;
|
|
211
211
|
uint8: Uint8Array;
|
|
212
212
|
};
|
|
213
|
-
|
|
213
|
+
/**
|
|
214
|
+
* What a participant contributes to a vertex array.
|
|
215
|
+
*
|
|
216
|
+
* `enableVertexAttribArray`, `vertexAttribPointer`, `vertexAttribDivisor` and
|
|
217
|
+
* the `ELEMENT_ARRAY_BUFFER` binding all write into whichever vertex array is
|
|
218
|
+
* bound when they run — they name no destination of their own. That is what
|
|
219
|
+
* makes them a participant in something rather than a property of themselves,
|
|
220
|
+
* and why `vao()` exists: it binds the array first, then asks each participant
|
|
221
|
+
* to write itself in.
|
|
222
|
+
*
|
|
223
|
+
* The returned function undoes the write. It is only used where there is no
|
|
224
|
+
* vertex array to hold the state — a WebGL1 context without
|
|
225
|
+
* `OES_vertex_array_object` — since a real vertex array is thrown away whole.
|
|
226
|
+
*/
|
|
227
|
+
interface VertexArrayParticipant {
|
|
228
|
+
applyToVertexArray(): () => void;
|
|
229
|
+
/**
|
|
230
|
+
* State the CONTEXT holds rather than the vertex array, re-applied on every
|
|
231
|
+
* bind because no array can keep it.
|
|
232
|
+
*
|
|
233
|
+
* There is exactly one such thing: the current generic vertex attribute
|
|
234
|
+
* values, which an attribute reads when its array is disabled. The enable
|
|
235
|
+
* flag saying to read them is vertex-array state; the values themselves are
|
|
236
|
+
* not. So a constant source splits across the two hooks — and the tempting
|
|
237
|
+
* optimisation of skipping this when nothing changed would let one draw's
|
|
238
|
+
* constants leak into the next.
|
|
239
|
+
*/
|
|
240
|
+
applyToContext?(): void;
|
|
241
|
+
}
|
|
242
|
+
interface AttributeMethods<T extends AttributeDefinition = AttributeDefinition> extends VertexArrayParticipant {
|
|
214
243
|
buffer: WebGLBuffer;
|
|
215
|
-
/**
|
|
244
|
+
/**
|
|
245
|
+
* Apply this attribute to the DEFAULT vertex array, returning a disposer.
|
|
246
|
+
*
|
|
247
|
+
* Explicitly the default one: these calls have no destination of their own,
|
|
248
|
+
* so without binding first this would edit whichever array happened to be
|
|
249
|
+
* bound — and a vertex array keeps what it is given, so the damage would
|
|
250
|
+
* outlive the call. Use `vao()` to build an array of your own.
|
|
251
|
+
*/
|
|
216
252
|
bind(): () => void;
|
|
217
253
|
set(data: FormatToArray[ResolvedFormat<T>], usage?: GLUsage): {
|
|
218
254
|
bind(): () => void;
|
|
219
255
|
};
|
|
256
|
+
/**
|
|
257
|
+
* Delete the buffer, if this attribute is the one that made it.
|
|
258
|
+
*
|
|
259
|
+
* A buffer named by the schema came from somewhere else and is deleted by
|
|
260
|
+
* whoever made it — a shared buffer is the point of passing one in, and one
|
|
261
|
+
* view cannot know whether the others are done. So this is safe to call
|
|
262
|
+
* twice and safe to call on a borrowed buffer, and it is not a way to free
|
|
263
|
+
* one: dispose the view that created it.
|
|
264
|
+
*/
|
|
220
265
|
dispose(): void;
|
|
221
266
|
}
|
|
222
267
|
type AttributeView<T extends AttributeSchema> = {
|
|
@@ -235,11 +280,48 @@ interface InterleavedAttributeDefinition {
|
|
|
235
280
|
instanced?: boolean;
|
|
236
281
|
}
|
|
237
282
|
type InterleavedAttributeSchema = Record<string | symbol, InterleavedAttributeDefinition>;
|
|
283
|
+
/**
|
|
284
|
+
* A source is where one draw's values for a layout come from. Both kinds are
|
|
285
|
+
* participants, so either can go into a `vao()`.
|
|
286
|
+
*/
|
|
287
|
+
interface Source extends VertexArrayParticipant {
|
|
288
|
+
/**
|
|
289
|
+
* Apply this source to the DEFAULT vertex array, returning a disposer. See
|
|
290
|
+
* `AttributeMethods.bind` — and prefer `vao()`, which is the only way to get
|
|
291
|
+
* a source and its fellow participants into an array of their own.
|
|
292
|
+
*/
|
|
293
|
+
bind(): () => void;
|
|
294
|
+
}
|
|
295
|
+
interface BufferSource extends Source {
|
|
296
|
+
/**
|
|
297
|
+
* The same buffer, stepped once per instance rather than once per vertex.
|
|
298
|
+
*
|
|
299
|
+
* A divisor is a property of the draw, not of the data — the same markers
|
|
300
|
+
* are an instanced fleet in one draw and a single mesh in the next — so it
|
|
301
|
+
* is chosen here rather than declared in the schema.
|
|
302
|
+
*/
|
|
303
|
+
perInstance: Source;
|
|
304
|
+
set(data: Float32Array, usage?: GLUsage): void;
|
|
305
|
+
}
|
|
306
|
+
interface ConstantAttributeMethods {
|
|
307
|
+
set(...values: number[]): void;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Per-key setters over the same layout the buffer source uses — a constant
|
|
311
|
+
* attribute is a uniform in all but name, so it is written like one.
|
|
312
|
+
*/
|
|
313
|
+
type ConstantSource<T extends InterleavedAttributeLayout[]> = Source & {
|
|
314
|
+
[K in T[number] as K['key'] extends string ? K['key'] : never]: ConstantAttributeMethods;
|
|
315
|
+
};
|
|
238
316
|
interface InterleavedAttributeMethods<T extends InterleavedAttributeLayout[] = InterleavedAttributeLayout[]> {
|
|
317
|
+
buffer: BufferSource;
|
|
318
|
+
constant: ConstantSource<T>;
|
|
319
|
+
dispose(): void;
|
|
320
|
+
}
|
|
321
|
+
interface VertexArrayMethods {
|
|
239
322
|
/** Binds the vertex array, returning a disposer that restores the previous one */
|
|
240
323
|
bind(): () => void;
|
|
241
324
|
unbind(): void;
|
|
242
|
-
set(data: Float32Array, usage?: GLUsage): void;
|
|
243
325
|
dispose(): void;
|
|
244
326
|
}
|
|
245
327
|
type InterleavedAttributeView<T extends InterleavedAttributeSchema> = {
|
|
@@ -252,7 +334,7 @@ interface BufferDefinition {
|
|
|
252
334
|
usage?: GLUsage;
|
|
253
335
|
}
|
|
254
336
|
type BufferSchema = Record<string | symbol, BufferDefinition>;
|
|
255
|
-
interface BufferMethods {
|
|
337
|
+
interface BufferMethods extends VertexArrayParticipant {
|
|
256
338
|
set(data: Float32Array | Uint16Array | Uint32Array): void;
|
|
257
339
|
/** Binds the buffer, returning a disposer that restores the previous binding */
|
|
258
340
|
bind(): () => void;
|
|
@@ -285,6 +367,12 @@ interface TextureDefinition {
|
|
|
285
367
|
interface Framebuffer {
|
|
286
368
|
texture: WebGLTexture;
|
|
287
369
|
framebuffer: WebGLFramebuffer;
|
|
370
|
+
/**
|
|
371
|
+
* Delete the framebuffer, and the texture only if `createFramebuffer` made
|
|
372
|
+
* it. A texture handed in belongs to whoever made it. Idempotent, and exactly
|
|
373
|
+
* what aborting the signal does.
|
|
374
|
+
*/
|
|
375
|
+
dispose(): void;
|
|
288
376
|
}
|
|
289
377
|
/**********************************************************************************/
|
|
290
378
|
/**********************************************************************************/
|
|
@@ -320,24 +408,44 @@ type TexImage2DOptions = {
|
|
|
320
408
|
interface ViewOptions {
|
|
321
409
|
signal?: AbortSignal;
|
|
322
410
|
}
|
|
411
|
+
/**
|
|
412
|
+
* What a program has names for. Buffers are absent on purpose: a buffer has no
|
|
413
|
+
* name in the source, so it cannot be resolved against a program the way a
|
|
414
|
+
* uniform or an attribute is, and its lifetime is rarely a program's. Build one
|
|
415
|
+
* with `bufferView` and pass it to `vao()` alongside the attributes.
|
|
416
|
+
*/
|
|
323
417
|
interface ViewSchema {
|
|
324
418
|
uniforms?: UniformSchema;
|
|
325
419
|
attributes?: AttributeSchema;
|
|
326
420
|
interleavedAttributes?: InterleavedAttributeSchema;
|
|
327
|
-
buffers?: BufferSchema;
|
|
328
421
|
}
|
|
329
422
|
type PartialSchema<T> = T extends Record<string | symbol, infer V> ? Record<string | symbol, Partial<V>> : never;
|
|
330
423
|
interface ViewSchemaPartial {
|
|
331
424
|
uniforms?: PartialSchema<UniformSchema>;
|
|
332
425
|
attributes?: PartialSchema<AttributeSchema>;
|
|
333
426
|
interleavedAttributes?: PartialSchema<InterleavedAttributeSchema>;
|
|
334
|
-
buffers?: PartialSchema<BufferSchema>;
|
|
335
427
|
}
|
|
336
428
|
type View<T extends ViewSchema = ViewSchema> = {
|
|
337
429
|
attributes: T['attributes'] extends AttributeSchema ? Prettify<AttributeView<T['attributes']>> : {};
|
|
338
430
|
interleavedAttributes: T['interleavedAttributes'] extends InterleavedAttributeSchema ? Prettify<InterleavedAttributeView<T['interleavedAttributes']>> : {};
|
|
339
|
-
buffers: T['buffers'] extends BufferSchema ? Prettify<BufferView<T['buffers']>> : {};
|
|
340
431
|
uniforms: T['uniforms'] extends UniformSchema ? Prettify<UniformView<T['uniforms']>> : {};
|
|
432
|
+
/**
|
|
433
|
+
* Build a vertex array from the participants one draw needs — its attributes,
|
|
434
|
+
* its interleaved layouts, its element buffer.
|
|
435
|
+
*
|
|
436
|
+
* This is the one member the schema cannot declare, because its input is the
|
|
437
|
+
* schema's own output. It is also the only place a vertex array is created:
|
|
438
|
+
* the participants no longer make one each, so which array they end up in is
|
|
439
|
+
* something you say rather than something you inherit from call order.
|
|
440
|
+
*/
|
|
441
|
+
vao(participants: VertexArrayParticipant[]): VertexArrayMethods;
|
|
442
|
+
/**
|
|
443
|
+
* Delete everything the view made: the buffers behind its attributes and
|
|
444
|
+
* layouts, and any vertex array still held from `vao()`. Buffers named by the
|
|
445
|
+
* schema are left alone — those belong to whoever made them. Idempotent, and
|
|
446
|
+
* exactly what aborting the signal does.
|
|
447
|
+
*/
|
|
448
|
+
dispose(): void;
|
|
341
449
|
};
|
|
342
450
|
/**********************************************************************************/
|
|
343
451
|
/**********************************************************************************/
|
|
@@ -437,6 +545,11 @@ type CompileResult<TVertex extends GLSL, TFragment extends GLSL, TOverrideSchema
|
|
|
437
545
|
view: Prettify<View<TSchema>>;
|
|
438
546
|
vertex: string;
|
|
439
547
|
fragment: string;
|
|
548
|
+
/**
|
|
549
|
+
* Delete the program and everything its view made. Idempotent, and
|
|
550
|
+
* exactly what aborting the signal does.
|
|
551
|
+
*/
|
|
552
|
+
dispose(): void;
|
|
440
553
|
} : never;
|
|
441
554
|
|
|
442
|
-
export type {
|
|
555
|
+
export type { TextureParameterName as $, AttributeSchema as A, BufferSchema as B, AttributeFormat as C, AttributeDefinition as D, AttributeMethods as E, FramebufferDefinition as F, GL as G, InterleavedAttributeLayout as H, InterleavedAttributeSchema as I, InterleavedAttributeDefinition as J, KIND_TO_UNIFORM_FN_NAME_MAP as K, BufferSource as L, ConstantAttributeMethods as M, ConstantSource as N, InterleavedAttributeMethods as O, BufferDefinition as P, BufferMethods as Q, RemoveSuffix as R, Source as S, TextureDefinition as T, UniformSchema as U, ViewOptions as V, Framebuffer as W, TextureTarget as X, TextureSchema as Y, TextureMethods as Z, TextureView as _, ViewSchema as a, TextureParameterValue as a0, TextureParameters as a1, InternalFormat as a2, Format as a3, PixelType as a4, TexImage2DOptions as a5, ViewSchemaPartial as a6, GLSLResult as a7, GLSLSlot as a8, GLSL as a9, UniformToken as aa, UniformTokenFn as ab, AttributeToken as ac, AttributeTokenFn as ad, InterleaveToken as ae, GLSLToSchema as af, GLSLToView as ag, GLSLSlotsToSchema as ah, FlattenSlots as ai, MergeGLSLSchema as aj, CompileResult as ak, Prettify as al, View as b, VertexArrayParticipant as c, VertexArrayMethods as d, UniformView as e, AttributeView as f, InterleavedAttributeView as g, BufferView as h, GLTarget as i, GLUsage as j, GLTextureTarget as k, GLTextureFormat as l, GLTextureType as m, GLTextureFilter as n, GLTextureWrap as o, KIND_TO_SIZE_MAP as p, UniformFnName as q, UniformKind as r, UniformKindMap as s, UniformDefinition as t, UniformElementMethods as u, UniformArrayMethods as v, UniformMethods as w, UniformValue as x, AttributeShortHand as y, AttributeKind as z };
|
package/package.json
CHANGED
package/src/gl.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { RemoveSuffix } from './type-utils'
|
|
2
|
-
import type { GL } from './types'
|
|
2
|
+
import type { GL, ViewOptions } from './types'
|
|
3
|
+
import { once } from './utils'
|
|
3
4
|
|
|
4
5
|
function isWebGL2RenderingContext(
|
|
5
6
|
gl: WebGLRenderingContext | WebGL2RenderingContext,
|
|
@@ -37,7 +38,12 @@ export function createShader(gl: GL, type: number, source: string): WebGLShader
|
|
|
37
38
|
/* */
|
|
38
39
|
/**********************************************************************************/
|
|
39
40
|
|
|
40
|
-
export function createProgram(
|
|
41
|
+
export function createProgram(
|
|
42
|
+
gl: GL,
|
|
43
|
+
vertexSource: string,
|
|
44
|
+
fragmentSource: string,
|
|
45
|
+
{ signal }: ViewOptions = {},
|
|
46
|
+
): WebGLProgram {
|
|
41
47
|
const program = gl.createProgram()
|
|
42
48
|
if (!program) throw new Error('Failed to create WebGL program')
|
|
43
49
|
|
|
@@ -60,6 +66,11 @@ export function createProgram(gl: GL, vertexSource: string, fragmentSource: stri
|
|
|
60
66
|
gl.deleteShader(vertexShader)
|
|
61
67
|
gl.deleteShader(fragmentShader)
|
|
62
68
|
|
|
69
|
+
signal?.addEventListener(
|
|
70
|
+
'abort',
|
|
71
|
+
once(() => gl.deleteProgram(program)),
|
|
72
|
+
)
|
|
73
|
+
|
|
63
74
|
return program
|
|
64
75
|
}
|
|
65
76
|
|