@bigmistqke/view.gl 0.1.5 → 0.1.7
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 +113 -45
- package/dev/src/examples/game-of-life.ts +2 -2
- package/dev/src/examples/pix-sim.ts +2 -2
- package/dist/index.d.mts +3 -2
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +1 -1
- package/dist/tag.d.mts +7 -41
- package/dist/tag.mjs +10 -10
- package/dist/tag.mjs.map +1 -1
- package/dist/types-C4TRuvYh.d.mts +477 -0
- package/package.json +2 -2
- package/src/features.ts +2 -1
- package/src/index.ts +6 -1
- package/src/tag.ts +19 -128
- package/src/type-utils.ts +43 -0
- package/src/types.ts +112 -99
- package/src/utils/index.ts +7 -4
- package/test/tag.test.ts +412 -78
- package/dist/types-DIIqZ8e6.d.mts +0 -289
package/README.md
CHANGED
|
@@ -11,11 +11,16 @@
|
|
|
11
11
|
- [👁️ view.gl](#️-viewgl)
|
|
12
12
|
- [🚀 Basic Usage](#-basic-usage)
|
|
13
13
|
- [👁️ view](#️-view)
|
|
14
|
+
- [📋 ViewSchema](#-viewschema)
|
|
14
15
|
- [👀 Resource Views](#-resource-views)
|
|
15
16
|
- [🎯 uniformView](#-uniformview)
|
|
17
|
+
- [📋 UniformSchema](#-uniformschema)
|
|
16
18
|
- [📝 attributeView](#-attributeview)
|
|
19
|
+
- [📋 AttributeSchema](#-attributeschema)
|
|
17
20
|
- [🔗 interleavedAttributeView](#-interleavedattributeview)
|
|
21
|
+
- [📋 InterleavedAttributeSchema](#-interleavedattributeschema)
|
|
18
22
|
- [🗂️ bufferView](#-bufferview)
|
|
23
|
+
- [📋 BufferSchema](#-bufferschema)
|
|
19
24
|
- [🏷️ view.gl/tag](#️-viewgltag)
|
|
20
25
|
- [🚀 Basic Usage](#-basic-usage-1)
|
|
21
26
|
- [📝 glsl](#-glsl)
|
|
@@ -27,6 +32,8 @@
|
|
|
27
32
|
- [📝 attribute[kind](name, options?)](#-attributekindname-options)
|
|
28
33
|
- [🔗 interleave(name, layout, options?)](#-interleavename-layout-options)
|
|
29
34
|
- [⚙️ compile(gl, vertex, fragment)](#️-compilegl-vertex-fragment)
|
|
35
|
+
- [🔍 compile.toString(shader)](#-compiletostringshader)
|
|
36
|
+
- [📋 compile.toSchema(shader)](#-compiletoschemaschader)
|
|
30
37
|
- [🛠️ Utils](#️-utils)
|
|
31
38
|
- [🏗️ createProgram](#️-createprogram)
|
|
32
39
|
- [🖼️ createTexture](#️-createtexture)
|
|
@@ -87,7 +94,7 @@ const { uniforms, attributes, buffers } = view(gl, program, schema)
|
|
|
87
94
|
|
|
88
95
|
- `gl`: WebGL rendering context
|
|
89
96
|
- `program`: Compiled WebGL program
|
|
90
|
-
- `schema`:
|
|
97
|
+
- `schema`: see [`ViewSchema`](#viewschema)
|
|
91
98
|
|
|
92
99
|
**Returns:**
|
|
93
100
|
|
|
@@ -95,40 +102,22 @@ const { uniforms, attributes, buffers } = view(gl, program, schema)
|
|
|
95
102
|
- `attributes`: Attribute managers with buffer handling
|
|
96
103
|
- `buffers`: Generic buffer managers
|
|
97
104
|
|
|
98
|
-
|
|
99
|
-
The complete schema object that defines all WebGL resources. Contains
|
|
105
|
+
#### 📋 ViewSchema
|
|
106
|
+
The complete schema object that defines all WebGL resources. Contains mappings for:
|
|
107
|
+
- [`UniformSchema`](#-uniformschema) - uniform variable definitions
|
|
108
|
+
- [`AttributeSchema`](#-attributeschema) - vertex attribute definitions
|
|
109
|
+
- [`InterleavedAttributeSchema`](#-interleavedattributeschema) - interleaved vertex data definitions
|
|
110
|
+
- [`BufferSchema`](#-bufferschema) - generic buffer definitions
|
|
100
111
|
|
|
101
112
|
<details>
|
|
102
113
|
<summary>TypeScript Types</summary>
|
|
103
114
|
|
|
104
115
|
```typescript
|
|
105
116
|
interface ViewSchema {
|
|
106
|
-
uniforms
|
|
107
|
-
attributes
|
|
108
|
-
interleavedAttributes
|
|
109
|
-
buffers
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
interface UniformOptions {
|
|
113
|
-
kind: UniformKind
|
|
114
|
-
size?: number // For array uniforms
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
interface AttributeOptions {
|
|
118
|
-
kind: AttributeKind
|
|
119
|
-
instanced?: boolean
|
|
120
|
-
buffer?: WebGLBuffer
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
interface InterleavedAttributeOptions {
|
|
124
|
-
layout: Array<{ key: string | symbol; kind: AttributeKind }>
|
|
125
|
-
instanced?: boolean
|
|
126
|
-
buffer?: WebGLBuffer
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
interface BufferOptions {
|
|
130
|
-
target: 'ARRAY_BUFFER' | 'ELEMENT_ARRAY_BUFFER'
|
|
131
|
-
usage?: 'STATIC_DRAW' | 'DYNAMIC_DRAW' | 'STREAM_DRAW'
|
|
117
|
+
uniforms: UniformSchema
|
|
118
|
+
attributes: AttributeSchema
|
|
119
|
+
interleavedAttributes: InterleavedAttributeSchema
|
|
120
|
+
buffers: BufferSchema
|
|
132
121
|
}
|
|
133
122
|
```
|
|
134
123
|
|
|
@@ -153,8 +142,9 @@ uniforms.time.set(performance.now())
|
|
|
153
142
|
uniforms.lights.set(lightData) // Takes Float32Array for array uniforms
|
|
154
143
|
```
|
|
155
144
|
|
|
156
|
-
|
|
157
|
-
|
|
145
|
+
##### 📋 UniformSchema
|
|
146
|
+
|
|
147
|
+
A mapping of uniform names to their configuration.
|
|
158
148
|
|
|
159
149
|
- `kind`: GLSL type (see [Uniform Types](#-uniform-types) for full list and WebGL compatibility)
|
|
160
150
|
- `size`: Array size (optional) - converts uniform to array type
|
|
@@ -182,12 +172,12 @@ type UniformKind =
|
|
|
182
172
|
| 'sampler2D'
|
|
183
173
|
| 'samplerCube'
|
|
184
174
|
|
|
185
|
-
interface
|
|
175
|
+
interface UniformDefinition {
|
|
186
176
|
kind: UniformKind
|
|
187
177
|
size?: number // Creates array uniform with Float32Array setter
|
|
188
178
|
}
|
|
189
179
|
|
|
190
|
-
type UniformSchema = Record<string | symbol,
|
|
180
|
+
type UniformSchema = Record<string | symbol, UniformDefinition>
|
|
191
181
|
```
|
|
192
182
|
|
|
193
183
|
</details>
|
|
@@ -208,12 +198,13 @@ attributes.instanceOffset.set(instanceData).bind()
|
|
|
208
198
|
gl.drawArraysInstanced(gl.TRIANGLES, 0, 3, 100)
|
|
209
199
|
```
|
|
210
200
|
|
|
211
|
-
|
|
212
|
-
|
|
201
|
+
##### 📋 AttributeSchema
|
|
202
|
+
|
|
203
|
+
A mapping of attribute names to their configuration.
|
|
213
204
|
|
|
214
205
|
- `kind`: GLSL type (see [Attribute Types](#-attribute-types) for full list and WebGL compatibility)
|
|
215
|
-
- `instanced`: Boolean - enables instanced rendering
|
|
216
|
-
- `buffer`: Custom WebGLBuffer (optional)
|
|
206
|
+
- `instanced`: Boolean (optional) - enables instanced rendering
|
|
207
|
+
- `buffer`: Custom WebGLBuffer (optional) - by default it gets created automatically during compilation
|
|
217
208
|
|
|
218
209
|
<details>
|
|
219
210
|
<summary>TypeScript Types</summary>
|
|
@@ -232,13 +223,13 @@ type AttributeKind =
|
|
|
232
223
|
| 'ivec3'
|
|
233
224
|
| 'ivec4'
|
|
234
225
|
|
|
235
|
-
interface
|
|
226
|
+
interface AttributeDefinition {
|
|
236
227
|
kind: AttributeKind
|
|
237
228
|
instanced?: boolean // Enables vertexAttribDivisor
|
|
238
229
|
buffer?: WebGLBuffer // Custom buffer, auto-created if not provided
|
|
239
230
|
}
|
|
240
231
|
|
|
241
|
-
type AttributeSchema = Record<string | symbol,
|
|
232
|
+
type AttributeSchema = Record<string | symbol, AttributeDefinition>
|
|
242
233
|
```
|
|
243
234
|
|
|
244
235
|
</details>
|
|
@@ -261,17 +252,19 @@ const interleavedAttributes = interleavedAttributeView(gl, program, {
|
|
|
261
252
|
interleavedAttributes.vertexData.set(interleavedVertexData).bind()
|
|
262
253
|
```
|
|
263
254
|
|
|
264
|
-
|
|
255
|
+
##### 📋 InterleavedAttributeSchema
|
|
256
|
+
|
|
265
257
|
A mapping of interleaved buffer names to their layout configuration. Each layout defines multiple attributes packed into a single buffer.
|
|
266
258
|
|
|
267
259
|
- `layout`: Array of attribute definitions with `key` and `kind` (see [Attribute Types](#-attribute-types))
|
|
268
260
|
- `instanced`: Boolean - applies to all attributes in layout
|
|
261
|
+
- `buffer`: Custom WebGLBuffer (optional) - by default it gets created automatically during compilation
|
|
269
262
|
|
|
270
263
|
<details>
|
|
271
264
|
<summary>TypeScript Types</summary>
|
|
272
265
|
|
|
273
266
|
```typescript
|
|
274
|
-
interface
|
|
267
|
+
interface InterleavedAttributeDefinition {
|
|
275
268
|
layout: Array<{
|
|
276
269
|
key: string | symbol
|
|
277
270
|
kind: AttributeKind
|
|
@@ -280,7 +273,7 @@ interface InterleavedAttributeOptions {
|
|
|
280
273
|
buffer?: WebGLBuffer // Custom buffer for interleaved data
|
|
281
274
|
}
|
|
282
275
|
|
|
283
|
-
type InterleavedAttributeSchema = Record<string | symbol,
|
|
276
|
+
type InterleavedAttributeSchema = Record<string | symbol, InterleavedAttributeDefinition>
|
|
284
277
|
```
|
|
285
278
|
|
|
286
279
|
</details>
|
|
@@ -299,7 +292,7 @@ buffers.indices.set(indexData).bind()
|
|
|
299
292
|
buffers.data.set(dynamicData).bind()
|
|
300
293
|
```
|
|
301
294
|
|
|
302
|
-
|
|
295
|
+
##### 📋 BufferSchema
|
|
303
296
|
A mapping of buffer names to their configuration. Each buffer has a target type and optional usage pattern.
|
|
304
297
|
|
|
305
298
|
- `target`: Buffer target (`'ARRAY_BUFFER'`, `'ELEMENT_ARRAY_BUFFER'`)
|
|
@@ -309,12 +302,12 @@ A mapping of buffer names to their configuration. Each buffer has a target type
|
|
|
309
302
|
<summary>TypeScript Types</summary>
|
|
310
303
|
|
|
311
304
|
```typescript
|
|
312
|
-
interface
|
|
305
|
+
interface BufferDefinition {
|
|
313
306
|
target: 'ARRAY_BUFFER' | 'ELEMENT_ARRAY_BUFFER'
|
|
314
307
|
usage?: 'STATIC_DRAW' | 'DYNAMIC_DRAW' | 'STREAM_DRAW' // Defaults to 'STATIC_DRAW'
|
|
315
308
|
}
|
|
316
309
|
|
|
317
|
-
type BufferSchema = Record<string | symbol,
|
|
310
|
+
type BufferSchema = Record<string | symbol, BufferDefinition>
|
|
318
311
|
```
|
|
319
312
|
|
|
320
313
|
</details>
|
|
@@ -591,6 +584,81 @@ view.buffers.indices.set(indexData).bind()
|
|
|
591
584
|
- `schema`: Merged schema (extracted + override)
|
|
592
585
|
- `view`: Ready-to-use view with type-safe resource access
|
|
593
586
|
|
|
587
|
+
#### 🔍 compile.toString(shader)
|
|
588
|
+
|
|
589
|
+
Converts a GLSL tagged template to a shader string without compilation of the `WebGLProgram`. Useful for debugging or when you need the raw shader code.
|
|
590
|
+
|
|
591
|
+
```typescript
|
|
592
|
+
const vertexShader = glsl`
|
|
593
|
+
${attribute.vec3('position')}
|
|
594
|
+
${uniform.mat4('mvpMatrix')}
|
|
595
|
+
|
|
596
|
+
void main() {
|
|
597
|
+
gl_Position = mvpMatrix * vec4(position, 1.0);
|
|
598
|
+
}
|
|
599
|
+
`
|
|
600
|
+
|
|
601
|
+
const shaderString = compile.toString(vertexShader)
|
|
602
|
+
console.log(shaderString)
|
|
603
|
+
// Output:
|
|
604
|
+
// attribute vec3 position;
|
|
605
|
+
// uniform mat4 mvpMatrix;
|
|
606
|
+
//
|
|
607
|
+
// void main() {
|
|
608
|
+
// gl_Position = mvpMatrix * vec4(position, 1.0);
|
|
609
|
+
// }
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
**Features**:
|
|
613
|
+
- Converts resource tags to GLSL declarations
|
|
614
|
+
- Handles WebGL1/WebGL2 syntax automatically
|
|
615
|
+
- Resolves symbol variables to unique identifiers
|
|
616
|
+
- Processes nested GLSL fragments and arrays
|
|
617
|
+
|
|
618
|
+
#### 📋 compile.toSchema(shader)
|
|
619
|
+
|
|
620
|
+
Extracts the [`ViewSchema`](#viewschema) from a GLSL tagged template without compilation of the `WebGLProgram`. Returns the type-safe schema that could be used by [`view()`](#️-view).
|
|
621
|
+
|
|
622
|
+
```typescript
|
|
623
|
+
const shader = glsl`
|
|
624
|
+
${uniform.float('time')}
|
|
625
|
+
${uniform.vec3('lightPos', { size: 4 })}
|
|
626
|
+
${attribute.vec3('position')}
|
|
627
|
+
${interleave('vertexData', [
|
|
628
|
+
attribute.vec2('uv'),
|
|
629
|
+
attribute.vec4('color')
|
|
630
|
+
])}
|
|
631
|
+
`
|
|
632
|
+
|
|
633
|
+
const schema = compile.toSchema(shader)
|
|
634
|
+
console.log(schema)
|
|
635
|
+
// Output:
|
|
636
|
+
// {
|
|
637
|
+
// uniforms: {
|
|
638
|
+
// time: { kind: 'float' },
|
|
639
|
+
// lightPos: { kind: 'vec3', size: 4 }
|
|
640
|
+
// },
|
|
641
|
+
// attributes: {
|
|
642
|
+
// position: { kind: 'vec3' }
|
|
643
|
+
// },
|
|
644
|
+
// interleavedAttributes: {
|
|
645
|
+
// vertexData: {
|
|
646
|
+
// layout: [
|
|
647
|
+
// { key: 'uv', kind: 'vec2' },
|
|
648
|
+
// { key: 'color', kind: 'vec4' }
|
|
649
|
+
// ],
|
|
650
|
+
// instanced: false
|
|
651
|
+
// }
|
|
652
|
+
// }
|
|
653
|
+
// }
|
|
654
|
+
```
|
|
655
|
+
|
|
656
|
+
**Use Cases**:
|
|
657
|
+
- Analyze shader resources without GL context
|
|
658
|
+
- Generate TypeScript types from shaders
|
|
659
|
+
- Validate shader compatibility before compilation
|
|
660
|
+
- Build tooling around shader resources
|
|
661
|
+
|
|
594
662
|
<details>
|
|
595
663
|
<summary>Usage with vanilla view.gl</summary>
|
|
596
664
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FramebufferDefinition } from 'src/types'
|
|
2
2
|
import { createFramebuffer } from 'src/utils'
|
|
3
3
|
import { attribute, compile, glsl, uniform } from 'view.gl/tag'
|
|
4
4
|
|
|
@@ -169,7 +169,7 @@ const framebufferOptions = {
|
|
|
169
169
|
type: 'UNSIGNED_BYTE',
|
|
170
170
|
magFilter: 'NEAREST',
|
|
171
171
|
minFilter: 'NEAREST',
|
|
172
|
-
} satisfies
|
|
172
|
+
} satisfies FramebufferDefinition
|
|
173
173
|
|
|
174
174
|
let read = createFramebuffer(gl, framebufferOptions)
|
|
175
175
|
let write = createFramebuffer(gl, framebufferOptions)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FramebufferDefinition } from 'src/types'
|
|
2
2
|
import { createFramebuffer } from 'src/utils'
|
|
3
3
|
import { attribute, compile, glsl, uniform } from 'view.gl/tag'
|
|
4
4
|
import { createElement } from '../utils'
|
|
@@ -484,7 +484,7 @@ const framebufferOptions = {
|
|
|
484
484
|
minFilter: 'NEAREST',
|
|
485
485
|
internalFormat: 'RGBA',
|
|
486
486
|
format: 'RGBA',
|
|
487
|
-
} satisfies
|
|
487
|
+
} satisfies FramebufferDefinition
|
|
488
488
|
|
|
489
489
|
let read = createFramebuffer(gl, framebufferOptions)
|
|
490
490
|
let write = createFramebuffer(gl, framebufferOptions)
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +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-
|
|
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-C4TRuvYh.mjs';
|
|
2
|
+
export { v as AttributeDefinition, u as AttributeKind, w as AttributeMethods, t as AttributeShortHand, a1 as AttributeTag, a2 as AttributeTagFn, C as BufferDefinition, D as BufferMethods, a7 as CompileResult, R as Format, E as Framebuffer, F as FramebufferDefinition, Y as GLSLResult, Z as GLSLSlot, a5 as GLSLSlotsToSchema, _ as GLSLTag, a4 as GLSLTagToSchema, g as GLTarget, l as GLTextureFilter, j as GLTextureFormat, i as GLTextureTarget, k as GLTextureType, m as GLTextureWrap, h as GLUsage, a3 as InterleaveTag, y as InterleavedAttributeDefinition, x as InterleavedAttributeLayout, z as InterleavedAttributeMethods, Q as InternalFormat, n as KIND_TO_SIZE_MAP, K as KIND_TO_UNIFORM_FN_NAME_MAP, a6 as MergeGLSLSchema, S as PixelType, W as TexImage2DOptions, T as TextureDefinition, L as TextureMethods, N as TextureParameterName, O as TextureParameterValue, P as TextureParameters, J as TextureSchema, H as TextureTarget, M as TextureView, q as UniformDefinition, o as UniformFnName, p as UniformKind, s as UniformMethods, r as UniformSchemaPartial, $ as UniformTag, a0 as UniformTagFn, X as ViewSchemaPartial } from './types-C4TRuvYh.mjs';
|
|
2
3
|
|
|
3
4
|
declare function toID(key: string | number | symbol): any;
|
|
4
5
|
/**********************************************************************************/
|
|
@@ -15,4 +16,4 @@ declare function interleavedAttributeView<T extends InterleavedAttributeSchema>(
|
|
|
15
16
|
/**********************************************************************************/
|
|
16
17
|
declare function bufferView<T extends BufferSchema>(gl: GL, schema: T, { signal }?: ViewOptions): BufferView<T>;
|
|
17
18
|
|
|
18
|
-
export { attributeView, bufferView, interleavedAttributeView, toID, uniformView, view };
|
|
19
|
+
export { AttributeSchema, AttributeView, BufferSchema, BufferView, GL, InterleavedAttributeSchema, InterleavedAttributeView, UniformSchema, UniformView, View, ViewOptions, ViewSchema, attributeView, bufferView, interleavedAttributeView, toID, uniformView, view };
|
package/dist/index.mjs
CHANGED
|
@@ -128,7 +128,8 @@ function createUpsertMap(constructor) {
|
|
|
128
128
|
// src/index.ts
|
|
129
129
|
var index = 0;
|
|
130
130
|
var PREFIX = "VIEW_GL_ALIAS";
|
|
131
|
-
var
|
|
131
|
+
var IS_FIREFOX = navigator.userAgent.toLowerCase().indexOf("firefox") >= 0;
|
|
132
|
+
var SYMBOL_MAP = createUpsertMap(IS_FIREFOX ? Map : WeakMap);
|
|
132
133
|
function toID(key) {
|
|
133
134
|
if (typeof key === "string") {
|
|
134
135
|
return key;
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/features.ts","../src/utils/object.ts","../src/utils/index.ts","../src/index.ts"],"sourcesContent":["import type { GL, RemoveSuffix } from './types'\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 (gl instanceof WebGL2RenderingContext) 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\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 (gl instanceof WebGL2RenderingContext) 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","export function forEach<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 map<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 forEach(value, (value, key, index) => {\n result[key] = callback(value, key, index)\n })\n\n return result\n}\n","import type {\n AttributeKind,\n FramebufferOptions,\n GL,\n KIND_TO_SIZE_MAP,\n KIND_TO_UNIFORM_FN_NAME_MAP,\n TextureOptions,\n UniformKind,\n} from '../types'\n\nexport * as ObjectUtils from './object'\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\nfunction createGLShader(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\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 = createGLShader(gl, gl.VERTEX_SHADER, vertexSource)\n const fragmentShader = createGLShader(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/* 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 }: TextureOptions,\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(gl: GL, { attachment, texture, ...options }: FramebufferOptions) {\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","import { getInstancedArrays, getVertexArrayObject } from './features'\nimport type {\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 assertedNotNullish,\n createUpsertMap,\n isMatKind,\n isSamplerKind,\n kindToSize,\n kindToUniformFnName,\n ObjectUtils,\n} from './utils'\n\n/**********************************************************************************/\n/* */\n/* To ID */\n/* */\n/**********************************************************************************/\n\nlet index = 0\nconst PREFIX = 'VIEW_GL_ALIAS'\n\nconst SYMBOL_MAP = createUpsertMap(WeakMap<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 return ObjectUtils.map(schema, ({ kind, size }, key) => {\n const name = toID(key)\n\n const location = gl.getUniformLocation(program, name)\n\n if (isSamplerKind(kind)) {\n return {\n set(arg: number) {\n gl.uniform1i(location, arg)\n },\n }\n }\n\n const fnName = `uniform${kindToUniformFnName(kind)}${size ? 'v' : ''}`\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\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 type: 'FLOAT' | 'INT',\n instanced?: boolean,\n) {\n gl.enableVertexAttribArray(location)\n gl.vertexAttribPointer(location, size, gl[type], false, stride, offset)\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 = ObjectUtils.map(\n schema,\n (\n { kind, 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 type = kind.startsWith('i') ? 'INT' : 'FLOAT'\n\n return {\n buffer,\n bind() {\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n handleAttribute(gl, location, size, 0, 0, type, 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 ObjectUtils.forEach(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 = ObjectUtils.map(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 type = layout.kind.startsWith('i') ? 'INT' : 'FLOAT'\n index += size * 4\n\n return () => handleAttribute(gl, location, size, stride, offset, type, 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 } else {\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n }\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 ObjectUtils.forEach(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 = ObjectUtils.map(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 ObjectUtils.forEach(buffers, value => value.dispose())\n })\n\n return buffers\n}\n"],"mappings":";;;;;;;AAEA,IAAM,+BAA+B,oBAAI,QASvC;AACK,SAAS,mBAAmB,IAAQ;AACzC,MAAI,cAAc,uBAAwB,QAAO;AAEjD,QAAM,SAAS,6BAA6B,IAAI,EAAE;AAClD,MAAI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M,GAAG,MAAM,GAAG,KAAK,CAAC;AAAA,MAC3C;AAAA,IACF;AAAA,EACF,CAAC;AAED,UAAQ,iBAAiB,SAAS,SAAS,UAAU;AACnD,mBAAY,QAAQ,SAAS,WAAS,MAAM,QAAQ,CAAC;AAAA,EACvD,CAAC;AAED,SAAO;AACT;","names":["index","value","map","name","index","layout"]}
|
|
1
|
+
{"version":3,"sources":["../src/features.ts","../src/utils/object.ts","../src/utils/index.ts","../src/index.ts"],"sourcesContent":["import { RemoveSuffix } from './type-utils'\nimport type { GL } from './types'\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 (gl instanceof WebGL2RenderingContext) 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\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 (gl instanceof WebGL2RenderingContext) 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","export function forEach<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 map<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 forEach(value, (value, key, index) => {\n result[key] = callback(value, key, index)\n })\n\n return result\n}\n","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 * as ObjectUtils from './object'\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\nfunction createGLShader(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\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 = createGLShader(gl, gl.VERTEX_SHADER, vertexSource)\n const fragmentShader = createGLShader(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/* 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","import { getInstancedArrays, getVertexArrayObject } from './features'\nimport type {\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 assertedNotNullish,\n createUpsertMap,\n isMatKind,\n isSamplerKind,\n kindToSize,\n kindToUniformFnName,\n ObjectUtils,\n} from './utils'\nexport * from './types'\n\n/**********************************************************************************/\n/* */\n/* To ID */\n/* */\n/**********************************************************************************/\n\nlet index = 0\nconst PREFIX = 'VIEW_GL_ALIAS'\nconst IS_FIREFOX = navigator.userAgent.toLowerCase().indexOf('firefox') >= 0\n\n// NOTE: using symbol as key for WeakMap is currently broken in firefox\n// see https://bugzilla.mozilla.org/show_bug.cgi?id=1710433\n// fallback to Map will currently cause a memory leak.\nconst SYMBOL_MAP = createUpsertMap(IS_FIREFOX ? Map<symbol, string> : WeakMap<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 return ObjectUtils.map(schema, ({ kind, size }, key) => {\n const name = toID(key)\n\n const location = gl.getUniformLocation(program, name)\n\n if (isSamplerKind(kind)) {\n return {\n set(arg: number) {\n gl.uniform1i(location, arg)\n },\n }\n }\n\n const fnName = `uniform${kindToUniformFnName(kind)}${size ? 'v' : ''}`\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\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 type: 'FLOAT' | 'INT',\n instanced?: boolean,\n) {\n gl.enableVertexAttribArray(location)\n gl.vertexAttribPointer(location, size, gl[type], false, stride, offset)\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 = ObjectUtils.map(\n schema,\n (\n { kind, 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 type = kind.startsWith('i') ? 'INT' : 'FLOAT'\n\n return {\n buffer,\n bind() {\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n handleAttribute(gl, location, size, 0, 0, type, 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 ObjectUtils.forEach(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 = ObjectUtils.map(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 type = layout.kind.startsWith('i') ? 'INT' : 'FLOAT'\n index += size * 4\n\n return () => handleAttribute(gl, location, size, stride, offset, type, 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 } else {\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n }\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 ObjectUtils.forEach(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 = ObjectUtils.map(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 ObjectUtils.forEach(buffers, value => value.dispose())\n })\n\n return buffers\n}\n"],"mappings":";;;;;;;AAGA,IAAM,+BAA+B,oBAAI,QASvC;AACK,SAAS,mBAAmB,IAAQ;AACzC,MAAI,cAAc,uBAAwB,QAAO;AAEjD,QAAM,SAAS,6BAA6B,IAAI,EAAE;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;AAEA,IAAM,kCAAkC,oBAAI,QAS1C;AACK,SAAS,qBAAqB,IAAQ;AAC3C,MAAI,cAAc,uBAAwB,QAAO;AAEjD,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;;;AC3DA;AAAA;AAAA;AAAA;AAAA;AAAO,SAAS,QACd,OACA,UACA;AACA,MAAIA,SAAQ;AACZ,aAAW,OAAO,OAAO;AACvB,aAAS,MAAM,GAAG,GAAG,KAAKA,MAAK;AAC/B,IAAAA;AAAA,EACF;AACA,aAAW,OAAO,OAAO,sBAAsB,KAAK,GAAG;A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IAAI,QAAQ,CAAC,EAAE,SAAS,gBAAgB,QAAQ,cAAc,MAAM;AAC9F,UAAM,SAAS,mBAAmB,GAAG,aAAa,CAAC;AACnD,WAAO;AAAA,MACL,OAAO;AACL,WAAG,WAAW,GAAG,MAAM,GAAG,MAAM;AAAA,MAClC;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,mBAAY,QAAQ,SAAS,WAAS,MAAM,QAAQ,CAAC;AAAA,EACvD,CAAC;AAED,SAAO;AACT;","names":["index","value","map","name","index","layout"]}
|
package/dist/tag.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Z as GLSLSlot, _ as GLSLTag, a0 as UniformTagFn, a2 as AttributeTagFn, a1 as AttributeTag, u as AttributeKind, v as AttributeDefinition, a3 as InterleaveTag, X as ViewSchemaPartial, a8 as Prettify, a7 as CompileResult, a4 as GLSLTagToSchema } from './types-C4TRuvYh.mjs';
|
|
2
2
|
|
|
3
3
|
declare function glsl<TSlot extends GLSLSlot, TSlots extends TSlot[]>(template: TemplateStringsArray, ...slots: [...TSlots]): GLSLTag<TSlots>;
|
|
4
4
|
declare const uniform: {
|
|
@@ -66,50 +66,16 @@ declare const attribute: {
|
|
|
66
66
|
mat4x2: AttributeTagFn<"mat4x2">;
|
|
67
67
|
mat4x3: AttributeTagFn<"mat4x3">;
|
|
68
68
|
};
|
|
69
|
-
declare function interleave<const TKey extends string | symbol, const TLayout extends Array<AttributeTag<string | symbol, AttributeKind, undefined>>, const TOptions extends Omit<
|
|
69
|
+
declare function interleave<const TKey extends string | symbol, const TLayout extends Array<AttributeTag<string | symbol, AttributeKind, undefined>>, const TOptions extends Omit<AttributeDefinition, 'kind'>>(key: TKey, layout: TLayout, instanced: TOptions): InterleaveTag<TKey, TLayout, TOptions>;
|
|
70
70
|
declare function interleave<const TKey extends string | symbol, const TLayout extends Array<AttributeTag<string | symbol, AttributeKind, undefined>>>(key: TKey, layout: TLayout): InterleaveTag<TKey, TLayout, {
|
|
71
71
|
instanced: false;
|
|
72
72
|
}>;
|
|
73
|
-
type GLSLSlotsToSchema<TSlots extends Array<GLSLSlot>> = {
|
|
74
|
-
uniforms: {
|
|
75
|
-
[K in Extract<TSlots[number], UniformTag> as K['key']]: Prettify<Omit<K, 'name' | 'type'>>;
|
|
76
|
-
};
|
|
77
|
-
attributes: {
|
|
78
|
-
[K in Extract<TSlots[number], AttributeTag> as K['key']]: Prettify<Omit<K, 'name' | 'type'>>;
|
|
79
|
-
};
|
|
80
|
-
interleavedAttributes: {
|
|
81
|
-
[K in Extract<TSlots[number], InterleaveTag> as K['key']]: Prettify<Omit<K, 'name' | 'type'>>;
|
|
82
|
-
};
|
|
83
|
-
};
|
|
84
|
-
type FlattenSlots<T> = T extends GLSLTag<infer TSlots> ? _FlattenSlots<TSlots> : T extends Array<GLSLSlot> ? _FlattenSlots<T> : never;
|
|
85
|
-
type _FlattenSlots<T extends Array<GLSLSlot>> = T extends [infer First, ...infer Rest] ? First extends GLSLTag ? [...FlattenSlots<First>, ...FlattenSlots<Rest>] : First extends unknown[] ? [...FlattenSlots<First>, ...FlattenSlots<Rest>] : [First, ...FlattenSlots<Rest>] : [];
|
|
86
|
-
type MergeGLSLSchema<TVertex extends ViewSchema, TFragment extends ViewSchema, TOverrides extends ViewSchemaPartial> = {
|
|
87
|
-
uniforms: DeepMerge<[
|
|
88
|
-
ShallowMerge<[TVertex['uniforms'], TFragment['uniforms']]>,
|
|
89
|
-
TOverrides['uniforms']
|
|
90
|
-
]>;
|
|
91
|
-
attributes: DeepMerge<[
|
|
92
|
-
ShallowMerge<[TVertex['attributes'], TFragment['attributes']]>,
|
|
93
|
-
TOverrides['attributes']
|
|
94
|
-
]>;
|
|
95
|
-
interleavedAttributes: DeepMerge<[
|
|
96
|
-
ShallowMerge<[TVertex['interleavedAttributes'], TFragment['interleavedAttributes']]>,
|
|
97
|
-
TOverrides['interleavedAttributes']
|
|
98
|
-
]>;
|
|
99
|
-
};
|
|
100
|
-
type CompileResult<TVertex extends GLSLTag, TFragment extends GLSLTag, TOverrides extends ViewSchemaPartial> = MergeGLSLSchema<glslTagToSchema<TVertex>, glslTagToSchema<TFragment>, TOverrides> extends infer TSchema extends ViewSchema ? {
|
|
101
|
-
program: WebGLProgram;
|
|
102
|
-
schema: TSchema;
|
|
103
|
-
view: View<TSchema>;
|
|
104
|
-
} : never;
|
|
105
|
-
declare function compile<TVertex extends GLSLTag, TFragment extends GLSLTag, TOverride extends ViewSchemaPartial>(gl: WebGLRenderingContext, vertex: TVertex, fragment: TFragment, overrideSchema?: TOverride): CompileResult<TVertex, TFragment, TOverride>;
|
|
106
73
|
/**********************************************************************************/
|
|
107
74
|
/**********************************************************************************/
|
|
108
|
-
|
|
109
|
-
declare
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
};
|
|
75
|
+
declare function compile<TVertex extends GLSLTag, TFragment extends GLSLTag, TOverride extends ViewSchemaPartial>(gl: WebGLRenderingContext, vertex: TVertex, fragment: TFragment, overrideSchema?: TOverride): Prettify<CompileResult<TVertex, TFragment, TOverride>>;
|
|
76
|
+
declare namespace compile {
|
|
77
|
+
var toSchema: <TTag extends GLSLTag>(tag: TTag) => Prettify<GLSLTagToSchema<TTag>>;
|
|
78
|
+
var toString: <TTag extends GLSLTag>({ template: [initial, ...rest], slots }: TTag) => string;
|
|
79
|
+
}
|
|
114
80
|
|
|
115
81
|
export { attribute, compile, glsl, interleave, uniform };
|
package/dist/tag.mjs
CHANGED
|
@@ -161,7 +161,8 @@ function createUpsertMap(constructor) {
|
|
|
161
161
|
// src/index.ts
|
|
162
162
|
var index = 0;
|
|
163
163
|
var PREFIX = "VIEW_GL_ALIAS";
|
|
164
|
-
var
|
|
164
|
+
var IS_FIREFOX = navigator.userAgent.toLowerCase().indexOf("firefox") >= 0;
|
|
165
|
+
var SYMBOL_MAP = createUpsertMap(IS_FIREFOX ? Map : WeakMap);
|
|
165
166
|
function toID(key) {
|
|
166
167
|
if (typeof key === "string") {
|
|
167
168
|
return key;
|
|
@@ -414,7 +415,6 @@ function compile(gl, vertex, fragment, overrideSchema) {
|
|
|
414
415
|
const overrideSchemaKind = overrideSchema[kind];
|
|
415
416
|
for (const key in overrideSchemaKind) {
|
|
416
417
|
schemaKind[key] = {
|
|
417
|
-
// @ts-expect-error
|
|
418
418
|
...schemaKind[key],
|
|
419
419
|
...overrideSchemaKind[key]
|
|
420
420
|
};
|
|
@@ -429,11 +429,11 @@ function compile(gl, vertex, fragment, overrideSchema) {
|
|
|
429
429
|
}
|
|
430
430
|
function resolveGLSLTag(tag) {
|
|
431
431
|
return {
|
|
432
|
-
template:
|
|
433
|
-
schema:
|
|
432
|
+
template: compile.toString(tag),
|
|
433
|
+
schema: compile.toSchema(tag)
|
|
434
434
|
};
|
|
435
435
|
}
|
|
436
|
-
function
|
|
436
|
+
compile.toSchema = function(tag) {
|
|
437
437
|
const result = {
|
|
438
438
|
uniforms: {},
|
|
439
439
|
attributes: {},
|
|
@@ -448,7 +448,7 @@ function glslTagToSchema(tag) {
|
|
|
448
448
|
return;
|
|
449
449
|
}
|
|
450
450
|
if (slot.type === "glsl") {
|
|
451
|
-
const { uniforms, attributes, interleavedAttributes } =
|
|
451
|
+
const { uniforms, attributes, interleavedAttributes } = compile.toSchema(slot);
|
|
452
452
|
result.uniforms = {
|
|
453
453
|
...result.uniforms,
|
|
454
454
|
...uniforms
|
|
@@ -468,15 +468,15 @@ function glslTagToSchema(tag) {
|
|
|
468
468
|
}
|
|
469
469
|
tag.slots.forEach(handleSlot);
|
|
470
470
|
return result;
|
|
471
|
-
}
|
|
472
|
-
function
|
|
471
|
+
};
|
|
472
|
+
compile.toString = function({ template: [initial, ...rest], slots }) {
|
|
473
473
|
const v300 = !!initial?.startsWith("#version 300 es");
|
|
474
474
|
let template = initial ?? "";
|
|
475
475
|
for (let i = 0; i < rest.length; i++) {
|
|
476
476
|
template += `${glslSlotToString(slots[i], v300)}${rest[i]}`;
|
|
477
477
|
}
|
|
478
478
|
return template;
|
|
479
|
-
}
|
|
479
|
+
};
|
|
480
480
|
function glslSlotToString(slot, v300) {
|
|
481
481
|
if (typeof slot !== "object") {
|
|
482
482
|
return toID(slot);
|
|
@@ -485,7 +485,7 @@ function glslSlotToString(slot, v300) {
|
|
|
485
485
|
return slot.map((slot2) => glslSlotToString(slot2, v300)).join("");
|
|
486
486
|
}
|
|
487
487
|
if (slot.type === "glsl") {
|
|
488
|
-
return
|
|
488
|
+
return compile.toString(slot);
|
|
489
489
|
}
|
|
490
490
|
if (slot.type === "interleavedAttribute") {
|
|
491
491
|
return slot.layout.reduce(
|