@bigmistqke/view.gl 0.1.6 → 0.1.8

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/src/types.ts CHANGED
@@ -1,72 +1,14 @@
1
- export type Merge<A, B> = {
2
- [K in keyof A | keyof B]: K extends keyof A
3
- ? K extends keyof B
4
- ? A[K] | B[K]
5
- : A[K]
6
- : K extends keyof B
7
- ? B[K]
8
- : never
9
- }
10
-
11
- export type Prettify<T> = {
12
- [K in keyof T]: T[K]
13
- } & {}
14
-
15
- export type RemoveSuffix<T, S extends string> = {
16
- [K in keyof T as K extends `${infer Prefix}${S}` ? Prefix : K]: T[K]
17
- }
1
+ import { DeepMerge, Prettify, ShallowMerge } from './type-utils'
18
2
 
19
3
  export type GL = WebGLRenderingContext | WebGL2RenderingContext
20
4
 
21
- export type DeepMerge<A extends readonly [...any]> = A extends [infer L, ...infer R]
22
- ? DeepMergeTwo<L, DeepMerge<R>>
23
- : unknown
24
-
25
- type DeepMergeTwo<TBase, TOverride> = {
26
- [K in keyof TBase | keyof TOverride]: K extends keyof TOverride
27
- ? K extends keyof TBase
28
- ? ShallowMerge<[TBase[K], TOverride[K]]>
29
- : TOverride[K]
30
- : K extends keyof TBase
31
- ? TBase[K]
32
- : never
33
- }
34
-
35
- // see https://stackoverflow.com/a/49683575
36
- export type ShallowMerge<A extends readonly [...any]> = A extends [infer L, ...infer R]
37
- ? ShallowMergeTwo<L, ShallowMerge<R>>
38
- : unknown
39
-
40
- type OptionalPropertyNames<T> = {
41
- [K in keyof T]-?: {} extends { [P in K]: T[K] } ? K : never
42
- }[keyof T]
43
-
44
- type SpreadProperties<L, R, K extends keyof L & keyof R> = {
45
- [P in K]: L[P] | Exclude<R[P], undefined>
46
- }
47
-
48
- type Id<T> = T extends infer U ? { [K in keyof U]: U[K] } : never
49
-
50
- type ShallowMergeTwo<L, R> = Id<
51
- Pick<L, Exclude<keyof L, keyof R>> &
52
- Pick<R, Exclude<keyof R, OptionalPropertyNames<R>>> &
53
- Pick<R, Exclude<OptionalPropertyNames<R>, keyof L>> &
54
- SpreadProperties<L, R, OptionalPropertyNames<R> & keyof L>
55
- >
56
-
57
- /**********************************************************************************/
58
- /* */
59
- /* Constants */
60
- /* */
61
- /**********************************************************************************/
62
-
63
- type GLTarget = 'ARRAY_BUFFER' | 'ELEMENT_ARRAY_BUFFER'
64
- type GLUsage = 'STATIC_DRAW' | 'DYNAMIC_DRAW' | 'STREAM_DRAW'
65
- type GLTextureTarget = 'TEXTURE_2D' | 'TEXTURE_CUBE_MAP'
66
- type GLTextureFormat = 'RGBA' | 'RGB' | 'RGBA32F' | 'RGB32F' | 'RGBA16F' | 'RGB16F'
67
- type GLTextureType = 'UNSIGNED_BYTE' | 'FLOAT' | 'HALF_FLOAT'
68
- type GLTextureFilter = 'NEAREST' | 'LINEAR'
69
- type GLTextureWrap = 'CLAMP_TO_EDGE' | 'REPEAT' | 'MIRRORED_REPEAT'
5
+ export type GLTarget = 'ARRAY_BUFFER' | 'ELEMENT_ARRAY_BUFFER'
6
+ export type GLUsage = 'STATIC_DRAW' | 'DYNAMIC_DRAW' | 'STREAM_DRAW'
7
+ export type GLTextureTarget = 'TEXTURE_2D' | 'TEXTURE_CUBE_MAP'
8
+ export type GLTextureFormat = 'RGBA' | 'RGB' | 'RGBA32F' | 'RGB32F' | 'RGBA16F' | 'RGB16F'
9
+ export type GLTextureType = 'UNSIGNED_BYTE' | 'FLOAT' | 'HALF_FLOAT'
10
+ export type GLTextureFilter = 'NEAREST' | 'LINEAR'
11
+ export type GLTextureWrap = 'CLAMP_TO_EDGE' | 'REPEAT' | 'MIRRORED_REPEAT'
70
12
 
71
13
  export interface KIND_TO_UNIFORM_FN_NAME_MAP {
72
14
  // Floats (WebGL1 + WebGL2)
@@ -389,21 +331,22 @@ interface UniformKindMap {
389
331
  usampler2DArray: [number];
390
332
  }
391
333
 
392
- export interface UniformOptions {
334
+ export interface UniformDefinition {
393
335
  kind: UniformKind
394
336
  size?: number
395
337
  }
396
338
 
397
- export type UniformSchema = Record<string | symbol, UniformOptions>
398
- export type UniformSchemaPartial = Record<string | symbol, Partial<UniformOptions>>
339
+ export type UniformSchema = Record<string | symbol, UniformDefinition>
340
+ export type UniformSchemaPartial = Record<string | symbol, Partial<UniformDefinition>>
399
341
 
400
- export type UniformMethods<TOptions extends UniformOptions> = TOptions['size'] extends number
401
- ? {
402
- set(arg: Float32Array): void
403
- }
404
- : {
405
- set(...args: UniformKindMap[TOptions['kind']]): void
406
- }
342
+ export type UniformMethods<TDefinition extends UniformDefinition> =
343
+ TDefinition['size'] extends number
344
+ ? {
345
+ set(arg: Float32Array): void
346
+ }
347
+ : {
348
+ set(...args: UniformKindMap[TDefinition['kind']]): void
349
+ }
407
350
 
408
351
  export type UniformView<T extends UniformSchema> = {
409
352
  [K in keyof T]: UniformMethods<T[K]>
@@ -457,13 +400,13 @@ export type AttributeKind =
457
400
  | 'uvec3'
458
401
  | 'uvec4'
459
402
 
460
- export interface AttributeOptions {
403
+ export interface AttributeDefinition {
461
404
  kind: AttributeKind
462
405
  instanced?: boolean
463
406
  buffer?: WebGLBuffer
464
407
  }
465
408
 
466
- export type AttributeSchema = Record<string | symbol, AttributeOptions>
409
+ export type AttributeSchema = Record<string | symbol, AttributeDefinition>
467
410
 
468
411
  export interface AttributeMethods<T = AttributeKind> {
469
412
  buffer: WebGLBuffer
@@ -487,12 +430,12 @@ export interface InterleavedAttributeLayout {
487
430
  kind: AttributeKind
488
431
  }
489
432
 
490
- export interface InterleavedAttributeOptions {
433
+ export interface InterleavedAttributeDefinition {
491
434
  layout: InterleavedAttributeLayout[]
492
435
  instanced?: boolean
493
436
  }
494
437
 
495
- export type InterleavedAttributeSchema = Record<string | symbol, InterleavedAttributeOptions>
438
+ export type InterleavedAttributeSchema = Record<string | symbol, InterleavedAttributeDefinition>
496
439
 
497
440
  export interface InterleavedAttributeMethods<
498
441
  T extends InterleavedAttributeLayout[] = InterleavedAttributeLayout[],
@@ -513,12 +456,12 @@ export type InterleavedAttributeView<T extends InterleavedAttributeSchema> = {
513
456
  /* */
514
457
  /**********************************************************************************/
515
458
 
516
- export interface BufferOptions {
459
+ export interface BufferDefinition {
517
460
  target?: GLTarget
518
461
  usage?: GLUsage
519
462
  }
520
463
 
521
- export type BufferSchema = Record<string | symbol, BufferOptions>
464
+ export type BufferSchema = Record<string | symbol, BufferDefinition>
522
465
 
523
466
  export interface BufferMethods {
524
467
  set(data: Float32Array | Uint16Array | Uint32Array): void
@@ -536,12 +479,12 @@ export type BufferView<T extends BufferSchema> = {
536
479
  /* */
537
480
  /**********************************************************************************/
538
481
 
539
- export interface FramebufferOptions extends Omit<TextureOptions, 'data'> {
482
+ export interface FramebufferDefinition extends Omit<TextureDefinition, 'data'> {
540
483
  attachment: 'color' | 'depth' | 'stencil' | 'depthStencil'
541
484
  texture?: WebGLTexture
542
485
  }
543
486
 
544
- export interface TextureOptions {
487
+ export interface TextureDefinition {
545
488
  target?: GLTextureTarget
546
489
  width: number
547
490
  height: number
@@ -574,7 +517,7 @@ export type TextureTarget =
574
517
  | 'TEXTURE_3D' // WebGL2 only
575
518
  | 'TEXTURE_2D_ARRAY' // WebGL2 only
576
519
 
577
- export type TextureSchema = Record<string | symbol, TextureOptions>
520
+ export type TextureSchema = Record<string | symbol, TextureDefinition>
578
521
 
579
522
  export interface TextureMethods {
580
523
  texture: WebGLTexture
@@ -683,19 +626,21 @@ export interface ViewSchema {
683
626
  }
684
627
 
685
628
  export interface ViewSchemaPartial {
686
- uniforms?: Record<string | symbol, Partial<UniformOptions>>
687
- attributes?: Record<string | symbol, Partial<AttributeOptions>>
688
- interleavedAttributes?: Record<string | symbol, Partial<InterleavedAttributeOptions>>
689
- buffers?: Record<string | symbol, Partial<BufferOptions>>
629
+ uniforms?: Record<string | symbol, Partial<UniformDefinition>>
630
+ attributes?: Record<string | symbol, Partial<AttributeDefinition>>
631
+ interleavedAttributes?: Record<string | symbol, Partial<InterleavedAttributeDefinition>>
632
+ buffers?: Record<string | symbol, Partial<BufferDefinition>>
690
633
  }
691
634
 
692
635
  export type View<T extends ViewSchema = ViewSchema> = {
693
- attributes: T['attributes'] extends AttributeSchema ? AttributeView<T['attributes']> : undefined
636
+ attributes: T['attributes'] extends AttributeSchema
637
+ ? Prettify<AttributeView<T['attributes']>>
638
+ : never
694
639
  interleavedAttributes: T['interleavedAttributes'] extends InterleavedAttributeSchema
695
- ? InterleavedAttributeView<T['interleavedAttributes']>
696
- : undefined
697
- buffers: T['buffers'] extends BufferSchema ? BufferView<T['buffers']> : undefined
698
- uniforms: T['uniforms'] extends UniformSchema ? UniformView<T['uniforms']> : never
640
+ ? Prettify<InterleavedAttributeView<T['interleavedAttributes']>>
641
+ : never
642
+ buffers: T['buffers'] extends BufferSchema ? Prettify<BufferView<T['buffers']>> : never
643
+ uniforms: T['uniforms'] extends UniformSchema ? Prettify<UniformView<T['uniforms']>> : never
699
644
  }
700
645
 
701
646
  /**********************************************************************************/
@@ -709,21 +654,28 @@ export interface GLSLResult {
709
654
  schema: ViewSchema
710
655
  }
711
656
 
712
- type GLSLSlotItem = GLSLTag | UniformTag | AttributeTag | InterleaveTag | string | number | symbol
657
+ type GLSLSlotItem =
658
+ | GLSL
659
+ | UniformToken
660
+ | AttributeToken
661
+ | InterleaveToken
662
+ | string
663
+ | number
664
+ | symbol
713
665
 
714
666
  export type GLSLSlot = GLSLSlotItem | Array<GLSLSlotItem>
715
667
 
716
- export interface GLSLTag<TSlots extends GLSLSlot[] = GLSLSlot[]> {
668
+ export interface GLSL<TSlots extends GLSLSlot[] = GLSLSlot[]> {
717
669
  type: 'glsl'
718
670
  template: TemplateStringsArray
719
671
  slots: TSlots
720
672
  }
721
673
 
722
- export type UniformTag<
674
+ export type UniformToken<
723
675
  TKey extends string | symbol = string | symbol,
724
676
  TKind extends UniformKind = UniformKind,
725
- TOptions extends Partial<UniformOptions> = UniformOptions,
726
- > = TOptions extends { size: infer TSize }
677
+ TDefinition extends Partial<UniformDefinition> = UniformDefinition,
678
+ > = TDefinition extends { size: infer TSize }
727
679
  ? {
728
680
  type: 'uniform'
729
681
  kind: TKind
@@ -736,15 +688,15 @@ export type UniformTag<
736
688
  key: TKey
737
689
  }
738
690
 
739
- export interface UniformTagFn<TKind extends UniformKind> {
740
- <const TKey extends string | symbol, const TOptions extends Omit<UniformOptions, 'kind'>>(
691
+ export interface UniformTokenFn<TKind extends UniformKind> {
692
+ <const TKey extends string | symbol, const TDefinition extends Omit<UniformDefinition, 'kind'>>(
741
693
  key: TKey,
742
- TOptions: TOptions,
743
- ): Prettify<UniformTag<TKey, TKind, TOptions>>
744
- <const TName extends string | symbol>(key: TName): Prettify<UniformTag<TName, TKind>>
694
+ TDefinition: TDefinition,
695
+ ): Prettify<UniformToken<TKey, TKind, TDefinition>>
696
+ <const TName extends string | symbol>(key: TName): Prettify<UniformToken<TName, TKind>>
745
697
  }
746
698
 
747
- export interface AttributeTag<
699
+ export interface AttributeToken<
748
700
  TKey extends string | symbol = string | symbol,
749
701
  TKind extends AttributeKind = AttributeKind,
750
702
  TInstanced extends boolean | undefined = boolean | undefined,
@@ -755,24 +707,98 @@ export interface AttributeTag<
755
707
  key: TKey
756
708
  }
757
709
 
758
- export interface AttributeTagFn<TKey extends AttributeKind> {
759
- <TName extends string | symbol, TOptions extends Omit<AttributeOptions, 'kind'>>(
710
+ export interface AttributeTokenFn<TKey extends AttributeKind> {
711
+ <TName extends string | symbol, TDefinition extends Omit<AttributeDefinition, 'kind'>>(
760
712
  key: TName,
761
- options: TOptions,
762
- ): Prettify<AttributeTag<TName, TKey, TOptions['instanced']>>
763
- <TName extends string | symbol>(key: TName): Prettify<AttributeTag<TName, TKey, undefined>>
713
+ options: TDefinition,
714
+ ): Prettify<AttributeToken<TName, TKey, TDefinition['instanced']>>
715
+ <TName extends string | symbol>(key: TName): Prettify<AttributeToken<TName, TKey, undefined>>
764
716
  }
765
717
 
766
- export type InterleaveTag<
718
+ export type InterleaveToken<
767
719
  TKey extends string | symbol = string | symbol,
768
- TLayout extends Array<AttributeTag<string | symbol, AttributeKind, undefined>> = Array<
769
- AttributeTag<string | symbol, AttributeKind, undefined>
720
+ TLayout extends Array<AttributeToken<string | symbol, AttributeKind, undefined>> = Array<
721
+ AttributeToken<string | symbol, AttributeKind, undefined>
770
722
  >,
771
- TOptions extends Omit<AttributeOptions, 'kind'> = Omit<AttributeOptions, 'kind'>,
772
- > = TOptions & {
723
+ TDefinition extends Omit<AttributeDefinition, 'kind'> = Omit<AttributeDefinition, 'kind'>,
724
+ > = TDefinition & {
773
725
  key: TKey
774
726
  type: 'interleavedAttribute'
775
727
  layout: {
776
728
  [TKey in keyof TLayout]: Prettify<Pick<TLayout[TKey], 'kind' | 'key'>>
777
729
  }
778
730
  }
731
+
732
+ export type GLSLToSchema<T extends GLSL> =
733
+ FlattenSlots<T> extends infer TSlots
734
+ ? TSlots extends Array<any>
735
+ ? GLSLSlotsToSchema<TSlots>
736
+ : never
737
+ : never
738
+
739
+ export type GLSLToView<T extends GLSL> = View<GLSLToSchema<T>>
740
+
741
+ export type GLSLSlotsToSchema<TSlots extends Array<GLSLSlot>> = {
742
+ uniforms: {
743
+ [K in Extract<TSlots[number], UniformToken> as K['key']]: Prettify<Omit<K, 'name' | 'type'>>
744
+ }
745
+ attributes: {
746
+ [K in Extract<TSlots[number], AttributeToken> as K['key']]: Prettify<Omit<K, 'name' | 'type'>>
747
+ }
748
+ interleavedAttributes: {
749
+ [K in Extract<TSlots[number], InterleaveToken> as K['key']]: Prettify<Omit<K, 'name' | 'type'>>
750
+ }
751
+ }
752
+
753
+ type FlattenSlots<T> =
754
+ T extends GLSL<infer TSlots>
755
+ ? _FlattenSlots<TSlots>
756
+ : T extends Array<GLSLSlot>
757
+ ? _FlattenSlots<T>
758
+ : never
759
+
760
+ type _FlattenSlots<T extends Array<GLSLSlot>> = T extends [infer First, ...infer Rest]
761
+ ? First extends GLSL
762
+ ? [...FlattenSlots<First>, ...FlattenSlots<Rest>]
763
+ : First extends unknown[]
764
+ ? [...FlattenSlots<First>, ...FlattenSlots<Rest>]
765
+ : [First, ...FlattenSlots<Rest>]
766
+ : []
767
+
768
+ export type MergeGLSLSchema<
769
+ TVertex extends ViewSchema,
770
+ TFragment extends ViewSchema,
771
+ TOverrides extends ViewSchemaPartial = {},
772
+ > = {
773
+ uniforms: DeepMerge<
774
+ [ShallowMerge<[TVertex['uniforms'], TFragment['uniforms']]>, TOverrides['uniforms']]
775
+ >
776
+ attributes: DeepMerge<
777
+ [ShallowMerge<[TVertex['attributes'], TFragment['attributes']]>, TOverrides['attributes']]
778
+ >
779
+ interleavedAttributes: DeepMerge<
780
+ [
781
+ ShallowMerge<[TVertex['interleavedAttributes'], TFragment['interleavedAttributes']]>,
782
+ TOverrides['interleavedAttributes'],
783
+ ]
784
+ >
785
+ }
786
+
787
+ export type CompileResult<
788
+ TVertex extends GLSL,
789
+ TFragment extends GLSL,
790
+ TOverrideSchema extends ViewSchemaPartial,
791
+ > =
792
+ MergeGLSLSchema<
793
+ GLSLToSchema<TVertex>,
794
+ GLSLToSchema<TFragment>,
795
+ TOverrideSchema
796
+ > extends infer TSchema extends ViewSchema
797
+ ? {
798
+ program: WebGLProgram
799
+ schema: TSchema
800
+ view: Prettify<View<TSchema>>
801
+ vertex: string
802
+ fragment: string
803
+ }
804
+ : never
@@ -1,10 +1,10 @@
1
1
  import type {
2
2
  AttributeKind,
3
- FramebufferOptions,
3
+ FramebufferDefinition,
4
4
  GL,
5
5
  KIND_TO_SIZE_MAP,
6
6
  KIND_TO_UNIFORM_FN_NAME_MAP,
7
- TextureOptions,
7
+ TextureDefinition,
8
8
  UniformKind,
9
9
  } from '../types'
10
10
 
@@ -158,7 +158,7 @@ export function createTexture(
158
158
  wrapT = 'CLAMP_TO_EDGE',
159
159
  width,
160
160
  height,
161
- }: TextureOptions,
161
+ }: TextureDefinition,
162
162
  data?: ArrayBufferView | null,
163
163
  ): WebGLTexture {
164
164
  const texture = gl.createTexture()
@@ -229,7 +229,10 @@ class FramebufferError extends Error {
229
229
  }
230
230
  }
231
231
 
232
- export function createFramebuffer(gl: GL, { attachment, texture, ...options }: FramebufferOptions) {
232
+ export function createFramebuffer(
233
+ gl: GL,
234
+ { attachment, texture, ...options }: FramebufferDefinition,
235
+ ) {
233
236
  // Create texture for the framebuffer
234
237
  texture ??= createTexture(gl, options)
235
238