glre 0.28.0 → 0.29.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/src/node/types.ts CHANGED
@@ -13,16 +13,15 @@ export interface NodeProps {
13
13
  args?: X[]
14
14
  type?: string
15
15
  children?: X[]
16
- returnType?: Constants
17
16
  value?: number | number[] | boolean
18
- paramInfo?: Array<{ name: string; type: string }>
19
17
  }
20
18
 
21
19
  export interface NodeConfig {
22
20
  isWebGL?: boolean
23
- uniforms?: Set<string>
24
- functions?: Set<string>
25
- onUniform?: (name: string, value: any) => void
21
+ binding?: number
22
+ infers?: WeakMap<NodeProxy, Constants>
23
+ headers?: Map<string, string>
24
+ onMount?: (name: string, value: any) => void
26
25
  }
27
26
 
28
27
  type _Swizzles<T extends string> = T | `${T}${T}` | `${T}${T}${T}` | `${T}${T}${T}${T}`
@@ -38,24 +37,20 @@ export type NodeTypes =
38
37
  | 'variable'
39
38
  | 'swizzle'
40
39
  | 'operator'
41
- | 'conversions'
42
- | 'math_fun'
40
+ | 'conversion'
41
+ | 'function'
43
42
  | 'declare'
44
43
  | 'assign'
45
- | 'fn_def'
46
- | 'fn_run'
44
+ | 'define'
47
45
  | 'if'
48
46
  | 'loop'
49
47
  | 'scope'
50
48
  | 'switch'
51
- | 'case'
52
- | 'default'
53
49
  | 'ternary'
54
50
  | 'attribute'
55
51
  | 'varying'
56
52
  | 'builtin'
57
53
  | 'constant'
58
- | 'vertex_stage'
59
54
 
60
55
  export interface NodeProxy extends Record<Swizzles | Conversions, NodeProxy> {
61
56
  // Operators
@@ -132,4 +127,4 @@ export interface NodeProxy extends Record<Swizzles | Conversions, NodeProxy> {
132
127
  isProxy: true
133
128
  }
134
129
 
135
- export type X = NodeProxy | number | string | boolean | null | undefined
130
+ export type X = NodeProxy | number | string | boolean | undefined
package/src/node/utils.ts CHANGED
@@ -1,7 +1,9 @@
1
+ import { NodeProps } from './../../../../node_modules/glre/src/node/types'
1
2
  import { is } from '../utils/helpers'
2
3
  import { code } from './code'
3
4
  import { CONVERSIONS, FUNCTIONS, OPERATOR_KEYS, OPERATORS, TYPE_MAPPING } from './const'
4
5
  import type { Conversions, Functions, NodeConfig, NodeProxy, Operators, Swizzles, X } from './types'
6
+ import { infer } from './infer'
5
7
 
6
8
  export const isSwizzle = (key: unknown): key is Swizzles => {
7
9
  return is.str(key) && /^[xyzwrgbastpq]{1,4}$/.test(key)
@@ -21,7 +23,7 @@ export const isConversion = (key: unknown): key is Conversions => {
21
23
 
22
24
  export const isNodeProxy = (x: unknown): x is NodeProxy => {
23
25
  if (!x) return false
24
- if (!is.fun(x)) return false // @ts-ignore
26
+ if (typeof x !== 'object') return false // @ts-ignore
25
27
  return x.isProxy
26
28
  }
27
29
 
@@ -54,14 +56,25 @@ export const getOperator = (op: X) => {
54
56
  }
55
57
 
56
58
  const generateHead = (c: NodeConfig) => {
57
- const uniforms = Array.from(c.uniforms!)
58
- .map((uniform, i) => {
59
- if (c.isWebGL) return `uniform ${uniform};`
60
- else return `@group(0) @binding(${i}) var<uniform> ${uniform};`
61
- })
59
+ return Array.from(c.headers!)
60
+ .map(([, v]) => v)
62
61
  .join('\n')
63
- const functions = Array.from(c.functions!).join('\n')
64
- return `${uniforms}\n${functions}`
62
+ }
63
+
64
+ export const generateDefine = (props: NodeProps, c: NodeConfig) => {
65
+ const { id, children = [] } = props
66
+ const [x, y, ...args] = children
67
+ const returnType = y ? infer(y, c) : 'void'
68
+ const params = args.map((arg, i) => [`p${i}`, infer(arg, c)])
69
+ const lines = [code(x, c)]
70
+ if (y) lines.push(`return ${code(y, c)};`)
71
+ if (c?.isWebGL) {
72
+ const paramList = params.map(([name, type]) => `${type} ${name}`)
73
+ return `${returnType} ${id}(${paramList}) {\n${lines.join('\n')}\n}`
74
+ }
75
+ const wgslReturnType = formatConversions(returnType, c)
76
+ const wgslParams = params.map(([name, type]) => `${name}: ${formatConversions(type, c)}`)
77
+ return `fn ${id}(${wgslParams}) -> ${wgslReturnType} {\n${lines.join('\n')}\n}`
65
78
  }
66
79
 
67
80
  const generateFragmentMain = (body: string, head: string, isWebGL = true) => {
@@ -82,10 +95,12 @@ return ${body};
82
95
  }`.trim()
83
96
  }
84
97
 
85
- export const fragment = (x: X, c: NodeConfig) => {
98
+ export const fragment = (x: X, c: NodeConfig = {}) => {
86
99
  const body = code(x, c)
87
100
  const head = generateHead(c)
88
- return generateFragmentMain(body, head, c.isWebGL)
101
+ const main = generateFragmentMain(body, head, c.isWebGL)
102
+ console.log(`// ↓↓↓ generated ↓↓↓\n\n${main}\n\n`)
103
+ return main
89
104
  }
90
105
 
91
106
  export const vertex = (x: X, c: NodeConfig) => {
package/src/types.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { EventState } from 'reev'
2
2
  import type { Fun, Queue, Frame } from 'refr'
3
- import type { X } from './node'
3
+ import type { NodeProxy } from './node'
4
4
  export type { Fun, Queue, Frame }
5
5
  export type GPUContext = any // GPUCanvasContext https://developer.mozilla.org/en-US/docs/Web/API/GPUCanvasContext
6
6
  export type GPUDevice = any //
@@ -52,12 +52,12 @@ export type GL = EventState<{
52
52
  mouse: [number, number]
53
53
  count: number
54
54
  el: HTMLCanvasElement
55
- vs: string | X
56
- fs: string | X
57
- vert: string | X
58
- frag: string | X
59
- vertex: string | X
60
- fragment: string | X
55
+ vs: string | NodeProxy
56
+ fs: string | NodeProxy
57
+ vert: string | NodeProxy
58
+ frag: string | NodeProxy
59
+ vertex: string | NodeProxy
60
+ fragment: string | NodeProxy
61
61
 
62
62
  /**
63
63
  * core state
@@ -1,5 +1,5 @@
1
- import { fragment, vertex, X } from '../node'
2
- import { is } from './helpers'
1
+ import { fragment, isNodeProxy, vertex } from '../node'
2
+ import type { NodeProxy } from '../node'
3
3
  import type { GPUContext, GPUDevice, GPUPipeline } from '../types'
4
4
 
5
5
  const defaultVertexWGSL = `
@@ -34,11 +34,11 @@ export const createPipeline = (
34
34
  format: string,
35
35
  bufferLayouts: any[],
36
36
  bindGroupLayouts: any[],
37
- vs: string | X = defaultVertexWGSL,
38
- fs: string | X = defaultFragmentWGSL
37
+ vs: string | NodeProxy = defaultVertexWGSL,
38
+ fs: string | NodeProxy = defaultFragmentWGSL
39
39
  ) => {
40
- if (!is.str(fs)) fs = fragment(fs, { isWebGL: false })
41
- if (!is.str(vs)) vs = vertex(vs, { isWebGL: false })
40
+ if (isNodeProxy(vs)) vs = vertex(vs, { isWebGL: false })
41
+ if (isNodeProxy(fs)) fs = fragment(fs, { isWebGL: false })
42
42
  const layout = device.createPipelineLayout({ bindGroupLayouts })
43
43
  return device.createRenderPipeline({
44
44
  vertex: {
@@ -1,5 +1,5 @@
1
- import { fragment, vertex, X } from '../node'
2
- import { is } from './helpers'
1
+ import { fragment, isNodeProxy, vertex } from '../node'
2
+ import type { NodeProxy } from '../node'
3
3
 
4
4
  export const defaultVertexGLSL = /* cpp */ `
5
5
  #version 300 es
@@ -33,12 +33,12 @@ const createShader = (c: WebGLRenderingContext, source: string, type: number) =>
33
33
 
34
34
  export const createProgram = (
35
35
  c: WebGLRenderingContext,
36
- vs: string | X = defaultVertexGLSL,
37
- fs: string | X = defaultFragmentGLSL,
36
+ vs: string | NodeProxy = defaultVertexGLSL,
37
+ fs: string | NodeProxy = defaultFragmentGLSL,
38
38
  onError = () => {}
39
39
  ) => {
40
- if (!is.str(fs)) fs = fragment(fs, { isWebGL: true })
41
- if (!is.str(vs)) vs = vertex(fs, { isWebGL: true })
40
+ if (isNodeProxy(fs)) fs = fragment(fs, { isWebGL: true })
41
+ if (isNodeProxy(vs)) vs = vertex(fs, { isWebGL: true })
42
42
  const pg = c.createProgram()
43
43
  const _vs = createShader(c, vs, c.VERTEX_SHADER)
44
44
  const _fs = createShader(c, fs, c.FRAGMENT_SHADER)
package/src/webgl.ts CHANGED
@@ -6,13 +6,12 @@ import type { GL, WebGLState } from './types'
6
6
  export const webgl = async (gl: Partial<GL>) => {
7
7
  const c = gl.el!.getContext('webgl2')!
8
8
  const pg = createProgram(c, gl.vs, gl.fs, () => void (gl.isLoop = false))!
9
- const state = { context: c, program: pg } as WebGLState
10
9
  c.useProgram(pg)
11
-
12
10
  let _activeUnit = 0
13
11
  const uniforms = cached((key) => c.getUniformLocation(pg, key))
14
12
  const attribs = cached((key) => c.getAttribLocation(pg, key))
15
13
  const units = cached(() => _activeUnit++)
14
+ const state = { context: c, program: pg } as WebGLState
16
15
 
17
16
  const clean = () => c.deleteProgram(pg)
18
17