glre 0.32.0 → 0.34.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/index.ts CHANGED
@@ -5,7 +5,7 @@ import { webgpu } from './webgpu'
5
5
  import { is } from './utils/helpers'
6
6
  import type { EventState } from 'reev'
7
7
  import type { GL } from './types'
8
- import { float, fract, int, iResolution, position, uint, vec4, vertexIndex } from './node'
8
+ import { float, fract, int, iResolution, position, vec4, vertexIndex } from './node'
9
9
  export * from './node'
10
10
  export * from './types'
11
11
  export * from './utils/helpers'
package/src/node/code.ts CHANGED
@@ -1,61 +1,57 @@
1
1
  import { is } from '../utils/helpers'
2
2
  import { infer } from './infer'
3
- import { getBluiltin, getOperator, formatConversions, joins } from './utils'
4
- import type { NodeContext, X } from './types'
5
3
  import {
4
+ parseArray,
6
5
  parseAttribHead,
7
6
  parseConstantHead,
8
- parseUniformHead,
9
7
  parseDeclare,
10
8
  parseDefine,
11
9
  parseIf,
10
+ parseStruct,
11
+ parseStructHead,
12
12
  parseSwitch,
13
13
  parseTexture,
14
14
  parseVaryingHead,
15
+ parseUniformHead,
15
16
  } from './parse'
17
+ import { getBluiltin, getOperator, formatConversions, safeEventCall, getEventFun, initNodeContext } from './utils'
18
+ import type { Constants, NodeContext, X } from './types'
16
19
 
17
- export const code = (target: X, c?: NodeContext | null): string => {
20
+ export const code = <T extends Constants>(target: X<T>, c?: NodeContext | null): string => {
18
21
  if (!c) c = {}
19
- if (!c.headers) c.headers = new Map()
20
- if (!c.vertVaryings) c.vertVaryings = new Map()
21
- if (!c.fragInputs) c.fragInputs = new Map()
22
- if (!c.vertInputs) c.vertInputs = new Map()
23
- if (!c.vertOutputs) {
24
- c.vertOutputs = new Map()
25
- if (!c.isWebGL) {
26
- c.fragInputs.set('position', '@builtin(position) position: vec4f')
27
- c.vertOutputs.set('position', '@builtin(position) position: vec4f')
28
- }
29
- }
22
+ initNodeContext(c)
23
+ if (is.arr(target)) return parseArray(target, c)
30
24
  if (is.str(target)) return target
31
25
  if (is.num(target)) {
32
26
  const ret = `${target}`
33
27
  if (ret.includes('.')) return ret
28
+ // Check if this number should be an integer based on the inferred type
29
+ // For now, keep the original behavior to maintain compatibility
34
30
  return ret + '.0'
35
31
  }
36
32
  if (is.bol(target)) return target ? 'true' : 'false'
37
33
  if (!target) return ''
38
34
  const { type, props } = target
39
- const { id = '', children = [] } = props
35
+ const { id = '', children = [], fields, initialValues } = props
40
36
  const [x, y, z, w] = children
41
37
  /**
42
38
  * variables
43
39
  */
44
40
  if (type === 'variable') return id
45
- if (type === 'swizzle') return `${code(y, c)}.${code(x, c)}`
41
+ if (type === 'member') return `${code(y, c)}.${code(x, c)}`
46
42
  if (type === 'ternary')
47
43
  return c.isWebGL
48
- ? `(${code(x, c)} ? ${code(y, c)} : ${code(z, c)})`
44
+ ? `(${code(z, c)} ? ${code(x, c)} : ${code(y, c)})`
49
45
  : `select(${code(x, c)}, ${code(y, c)}, ${code(z, c)})`
50
- if (type === 'conversion') return `${formatConversions(x, c)}(${joins(children.slice(1), c)})`
46
+ if (type === 'conversion') return `${formatConversions(x, c)}(${parseArray(children.slice(1), c)})`
51
47
  if (type === 'operator') {
52
48
  if (x === 'not' || x === 'bitNot') return `!${code(y, c)}`
53
49
  return `(${code(y, c)} ${getOperator(x)} ${code(z, c)})`
54
50
  }
55
51
  if (type === 'function') {
56
- if (x === 'negate') return `(-${joins(children.slice(1), c)})`
52
+ if (x === 'negate') return `(-${parseArray(children.slice(1), c)})`
57
53
  if (x === 'texture') return parseTexture(c, y, z, w)
58
- return `${x}(${joins(children.slice(1), c)})`
54
+ return `${x}(${parseArray(children.slice(1), c)})`
59
55
  }
60
56
  /**
61
57
  * scopes
@@ -65,26 +61,28 @@ export const code = (target: X, c?: NodeContext | null): string => {
65
61
  if (type === 'return') return `return ${code(x, c)};`
66
62
  if (type === 'loop')
67
63
  return c.isWebGL
68
- ? `for (int i = 0; i < ${x}; i += 1) {\n${code(y, c)}\n}`
69
- : `for (var i: i32 = 0; i < ${x}; i++) {\n${code(y, c)}\n}`
64
+ ? `for (int i = 0; i < ${code(x, c)}; i += 1) {\n${code(y, c)}\n}`
65
+ : `for (var i: i32 = 0; i < ${code(x, c)}; i++) {\n${code(y, c)}\n}`
70
66
  if (type === 'if') return parseIf(c, x, y, children)
71
67
  if (type === 'switch') return parseSwitch(c, x, children)
72
68
  if (type === 'declare') return parseDeclare(c, x, y)
73
69
  if (type === 'define') {
74
- const ret = `${id}(${joins(children.slice(1), c)})`
75
- if (c.headers.has(id)) return ret
76
- c.headers.set(id, parseDefine(c, props, infer(target, c)))
77
- return ret
70
+ if (!c.code?.headers.has(id)) c.code?.headers.set(id, parseDefine(c, props, infer(target, c)))
71
+ return `${id}(${parseArray(children.slice(1), c)})`
72
+ }
73
+ if (type === 'struct') {
74
+ if (!c.code?.headers.has(id)) c.code?.headers.set(id, parseStructHead(c, id, fields))
75
+ return parseStruct(c, id, x.props.id, fields, initialValues)
78
76
  }
79
77
  /**
80
78
  * headers
81
79
  */
82
80
  if (type === 'varying') {
83
- if (c.vertOutputs.has(id)) return c.isWebGL ? `${id}` : `out.${id}`
81
+ if (c.code?.vertOutputs.has(id)) return c.isWebGL ? `${id}` : `out.${id}`
84
82
  const field = parseVaryingHead(c, id, infer(target, c))
85
- c.fragInputs.set(id, field)
86
- c.vertOutputs.set(id, field)
87
- c.vertVaryings.set(id, code(x, c))
83
+ c.code?.fragInputs.set(id, field)
84
+ c.code?.vertOutputs.set(id, field)
85
+ c.code?.vertVaryings.set(id, code(x, c))
88
86
  return c.isWebGL ? `${id}` : `out.${id}`
89
87
  }
90
88
  if (type === 'builtin') {
@@ -92,20 +90,29 @@ export const code = (target: X, c?: NodeContext | null): string => {
92
90
  if (id === 'position') return 'out.position'
93
91
  const field = `@builtin(${id}) ${id}: ${formatConversions(infer(target, c), c)}`
94
92
  if (c.isFrag) {
95
- c.fragInputs.set(id, field)
96
- } else c.vertInputs.set(id, field)
93
+ c.code?.fragInputs.set(id, field)
94
+ } else c.code?.vertInputs.set(id, field)
97
95
  return `in.${id}`
98
96
  }
99
97
  if (type === 'attribute') {
100
- c.vertInputs.set(id, parseAttribHead(c, id, infer(target, c)))
98
+ const fun = getEventFun(c, id, true)
99
+ safeEventCall(x, fun)
100
+ target.listeners.add(fun)
101
+ c.code?.vertInputs.set(id, parseAttribHead(c, id, infer(target, c)))
101
102
  return c.isWebGL ? `${id}` : `in.${id}`
102
103
  }
103
- if (c.headers.has(id)) return id // must last
104
+ if (c.code?.headers.has(id)) return id // must last
104
105
  let head = ''
105
- if (type === 'uniform') head = parseUniformHead(c, id, infer(target, c))
106
+ if (type === 'uniform') {
107
+ const varType = infer(target, c)
108
+ const fun = getEventFun(c, id, false, varType === 'texture')
109
+ safeEventCall(x, fun)
110
+ target.listeners.add(fun)
111
+ head = parseUniformHead(c, id, varType)
112
+ }
106
113
  if (type === 'constant') head = parseConstantHead(c, id, infer(target, c), code(x, c))
107
114
  if (head) {
108
- c.headers.set(id, head)
115
+ c.code?.headers.set(id, head)
109
116
  return id
110
117
  }
111
118
  return code(x, c)
package/src/node/const.ts CHANGED
@@ -1,29 +1,32 @@
1
1
  export const SWIZZLES = ['x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q'] as const
2
2
 
3
- export const CONSTANTS = [
4
- 'bool',
5
- 'uint',
6
- 'int',
7
- 'float',
8
- 'bvec2',
9
- 'ivec2',
10
- 'uvec2',
11
- 'vec2',
12
- 'bvec3',
13
- 'ivec3',
14
- 'uvec3',
15
- 'vec3',
16
- 'bvec4',
17
- 'ivec4',
18
- 'uvec4',
19
- 'vec4',
20
- 'color',
21
- 'mat2',
22
- 'mat3',
23
- 'mat4',
24
- 'texture',
25
- 'sampler2D',
26
- ] as const
3
+ export const TYPE_MAPPING = {
4
+ bool: 'bool',
5
+ uint: 'u32',
6
+ int: 'i32',
7
+ float: 'f32',
8
+ bvec2: 'vec2<bool>',
9
+ ivec2: 'vec2i',
10
+ uvec2: 'vec2u',
11
+ vec2: 'vec2f',
12
+ bvec3: 'vec3<bool>',
13
+ ivec3: 'vec3i',
14
+ uvec3: 'vec3u',
15
+ vec3: 'vec3f',
16
+ bvec4: 'vec4<bool>',
17
+ ivec4: 'vec4i',
18
+ uvec4: 'vec4u',
19
+ vec4: 'vec4f',
20
+ color: 'color',
21
+ mat2: 'mat2x2f',
22
+ mat3: 'mat3x3f',
23
+ mat4: 'mat4x4f',
24
+ texture: 'texture_2d<f32>',
25
+ sampler2D: 'sampler',
26
+ struct: 'struct',
27
+ } as const
28
+
29
+ export const CONSTANTS = Object.keys(TYPE_MAPPING) as unknown as keyof typeof TYPE_MAPPING
27
30
 
28
31
  export const CONVERSIONS = [
29
32
  'toBool',
@@ -71,11 +74,19 @@ export const OPERATORS = {
71
74
 
72
75
  export const OPERATOR_KEYS = Object.keys(OPERATORS) as (keyof typeof OPERATORS)[]
73
76
 
74
- export const SCALAR_RETURN_FUNCTIONS = ['dot', 'distance', 'length', 'lengthSq', 'determinant', 'luminance'] as const
75
-
76
- export const BOOL_RETURN_FUNCTIONS = ['all', 'any'] as const
77
-
78
- export const PRESERVE_TYPE_FUNCTIONS = [
77
+ // All shader functions (type inference now handled by inferFrom)
78
+ export const FUNCTIONS = [
79
+ // Float return functions
80
+ 'dot',
81
+ 'distance',
82
+ 'length',
83
+ 'lengthSq',
84
+ 'determinant',
85
+ 'luminance',
86
+ // Bool return functions
87
+ 'all',
88
+ 'any',
89
+ // Component-wise functions (preserve input type)
79
90
  'abs',
80
91
  'sign',
81
92
  'floor',
@@ -89,6 +100,12 @@ export const PRESERVE_TYPE_FUNCTIONS = [
89
100
  'asin',
90
101
  'acos',
91
102
  'atan',
103
+ 'sinh',
104
+ 'cosh',
105
+ 'tanh',
106
+ 'asinh',
107
+ 'acosh',
108
+ 'atanh',
92
109
  'exp',
93
110
  'exp2',
94
111
  'log',
@@ -103,65 +120,38 @@ export const PRESERVE_TYPE_FUNCTIONS = [
103
120
  'dFdx',
104
121
  'dFdy',
105
122
  'fwidth',
106
- ] as const
107
-
108
- export const VEC3_RETURN_FUNCTIONS = ['cross'] as const
109
-
110
- export const FIRST_ARG_TYPE_FUNCTIONS = ['reflect', 'refract'] as const
111
-
112
- export const HIGHEST_TYPE_FUNCTIONS = ['min', 'max', 'mix', 'clamp', 'step', 'smoothstep'] as const
113
-
114
- export const VEC4_RETURN_FUNCTIONS = ['texture', 'textureLod', 'textureSize', 'cubeTexture'] as const
115
-
116
- export const ADDITIONAL_FUNCTIONS = [
117
- 'atan2',
118
123
  'degrees',
124
+ 'radians',
125
+ // Vector functions
126
+ 'cross',
127
+ 'reflect',
128
+ 'refract',
129
+ // Multi-argument functions
130
+ 'min',
131
+ 'max',
132
+ 'mix',
133
+ 'clamp',
134
+ 'step',
135
+ 'smoothstep',
136
+ 'pow',
137
+ 'atan2',
138
+ // Texture functions
139
+ 'texture',
140
+ 'textureLod',
141
+ 'textureSize',
142
+ 'cubeTexture',
143
+ // Utility functions
119
144
  'faceforward',
120
145
  'bitcast',
121
146
  'cbrt',
122
147
  'difference',
123
148
  'equals',
124
- 'pow',
125
149
  'pow2',
126
150
  'pow3',
127
151
  'pow4',
128
- 'radians',
129
152
  'transformDirection',
130
153
  ] as const
131
154
 
132
- export const FUNCTIONS = [
133
- ...SCALAR_RETURN_FUNCTIONS,
134
- ...BOOL_RETURN_FUNCTIONS,
135
- ...PRESERVE_TYPE_FUNCTIONS,
136
- ...VEC3_RETURN_FUNCTIONS,
137
- ...FIRST_ARG_TYPE_FUNCTIONS,
138
- ...HIGHEST_TYPE_FUNCTIONS,
139
- ...VEC4_RETURN_FUNCTIONS,
140
- ...ADDITIONAL_FUNCTIONS,
141
- ] as const
142
-
143
- export const TYPE_MAPPING = {
144
- float: 'f32',
145
- int: 'i32',
146
- uint: 'u32',
147
- bool: 'bool',
148
- vec2: 'vec2f',
149
- vec3: 'vec3f',
150
- vec4: 'vec4f',
151
- mat2: 'mat2x2f',
152
- mat3: 'mat3x3f',
153
- mat4: 'mat4x4f',
154
- ivec2: 'vec2i',
155
- ivec3: 'vec3i',
156
- ivec4: 'vec4i',
157
- uvec2: 'vec2u',
158
- uvec3: 'vec3u',
159
- uvec4: 'vec4u',
160
- bvec2: 'vec2<bool>',
161
- bvec3: 'vec3<bool>',
162
- bvec4: 'vec4<bool>',
163
- } as const
164
-
165
155
  export const COMPONENT_COUNT_TO_TYPE = {
166
156
  1: 'float',
167
157
  2: 'vec2',
@@ -171,6 +161,24 @@ export const COMPONENT_COUNT_TO_TYPE = {
171
161
  16: 'mat4',
172
162
  } as const
173
163
 
164
+ // Function return type mapping for method chaining
165
+ export const FUNCTION_RETURN_TYPES = {
166
+ // Always return vec4
167
+ texture: 'vec4',
168
+ cubeTexture: 'vec4',
169
+ textureSize: 'vec4',
170
+ // Always return float
171
+ length: 'float',
172
+ lengthSq: 'float',
173
+ distance: 'float',
174
+ dot: 'float',
175
+ // Always return bool
176
+ all: 'bool',
177
+ any: 'bool',
178
+ // Always return vec3
179
+ cross: 'vec3',
180
+ } as const
181
+
174
182
  export const BUILTIN_TYPES = {
175
183
  // WGSL builtin variables
176
184
  position: 'vec4',