glre 0.29.0 → 0.31.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
@@ -8,12 +8,22 @@ export type Functions = (typeof FUNCTIONS)[number]
8
8
 
9
9
  export type Operators = (typeof OPERATOR_KEYS)[number]
10
10
 
11
+ export interface FnLayout {
12
+ name: string
13
+ type: Constants | 'auto'
14
+ inputs?: Array<{
15
+ name: string
16
+ type: Constants
17
+ }>
18
+ }
19
+
11
20
  export interface NodeProps {
12
21
  id?: string
13
22
  args?: X[]
14
23
  type?: string
15
24
  children?: X[]
16
- value?: number | number[] | boolean
25
+ inferFrom?: X
26
+ layout?: FnLayout
17
27
  }
18
28
 
19
29
  export interface NodeConfig {
@@ -21,7 +31,7 @@ export interface NodeConfig {
21
31
  binding?: number
22
32
  infers?: WeakMap<NodeProxy, Constants>
23
33
  headers?: Map<string, string>
24
- onMount?: (name: string, value: any) => void
34
+ onMount?: (name: string) => void
25
35
  }
26
36
 
27
37
  type _Swizzles<T extends string> = T | `${T}${T}` | `${T}${T}${T}` | `${T}${T}${T}${T}`
@@ -33,26 +43,29 @@ export type Swizzles =
33
43
  | _Swizzles<'s' | 't'>
34
44
 
35
45
  export type NodeTypes =
46
+ // headers
47
+ | 'attribute'
36
48
  | 'uniform'
49
+ | 'varying'
50
+ | 'constant'
51
+ // variables
37
52
  | 'variable'
38
53
  | 'swizzle'
39
- | 'operator'
54
+ | 'ternary'
55
+ | 'builtin'
40
56
  | 'conversion'
57
+ | 'operator'
41
58
  | 'function'
42
- | 'declare'
59
+ // scopes
60
+ | 'scope'
43
61
  | 'assign'
62
+ | 'loop'
44
63
  | 'define'
45
64
  | 'if'
46
- | 'loop'
47
- | 'scope'
48
65
  | 'switch'
49
- | 'ternary'
50
- | 'attribute'
51
- | 'varying'
52
- | 'builtin'
53
- | 'constant'
66
+ | 'declare'
54
67
 
55
- export interface NodeProxy extends Record<Swizzles | Conversions, NodeProxy> {
68
+ export interface NodeProxy extends Record<Swizzles, NodeProxy> {
56
69
  // Operators
57
70
  add(n: X): NodeProxy
58
71
  sub(n: X): NodeProxy
@@ -72,7 +85,6 @@ export interface NodeProxy extends Record<Swizzles | Conversions, NodeProxy> {
72
85
  // Variable manipulation
73
86
  assign(n: X): NodeProxy
74
87
  toVar(name?: string): NodeProxy
75
- toConst(name?: string): NodeProxy
76
88
 
77
89
  // Math function methods
78
90
  abs(): NodeProxy
@@ -121,6 +133,26 @@ export interface NodeProxy extends Record<Swizzles | Conversions, NodeProxy> {
121
133
  fwidth(): NodeProxy
122
134
 
123
135
  // System properties
136
+ toBool(): NodeProxy
137
+ toUint(): NodeProxy
138
+ toInt(): NodeProxy
139
+ toFloat(): NodeProxy
140
+ toBvec2(): NodeProxy
141
+ toIvec2(): NodeProxy
142
+ toUvec2(): NodeProxy
143
+ toVec2(): NodeProxy
144
+ toBvec3(): NodeProxy
145
+ toIvec3(): NodeProxy
146
+ toUvec3(): NodeProxy
147
+ toVec3(): NodeProxy
148
+ toBvec4(): NodeProxy
149
+ toIvec4(): NodeProxy
150
+ toUvec4(): NodeProxy
151
+ toVec4(): NodeProxy
152
+ toColor(): NodeProxy
153
+ toMat2(): NodeProxy
154
+ toMat3(): NodeProxy
155
+ toMat4(): NodeProxy
124
156
  toString(c?: NodeConfig): string
125
157
  type: NodeTypes
126
158
  props: NodeProps
package/src/node/utils.ts CHANGED
@@ -1,9 +1,26 @@
1
- import { NodeProps } from './../../../../node_modules/glre/src/node/types'
2
1
  import { is } from '../utils/helpers'
3
2
  import { code } from './code'
4
- import { CONVERSIONS, FUNCTIONS, OPERATOR_KEYS, OPERATORS, TYPE_MAPPING } from './const'
5
- import type { Conversions, Functions, NodeConfig, NodeProxy, Operators, Swizzles, X } from './types'
6
3
  import { infer } from './infer'
4
+ import {
5
+ CONSTANTS,
6
+ CONVERSIONS,
7
+ FUNCTIONS,
8
+ OPERATOR_KEYS,
9
+ OPERATORS,
10
+ TYPE_MAPPING,
11
+ WGSL_TO_GLSL_BUILTIN,
12
+ } from './const'
13
+ import type {
14
+ Constants,
15
+ Conversions,
16
+ Functions,
17
+ NodeConfig,
18
+ NodeProps,
19
+ NodeProxy,
20
+ Operators,
21
+ Swizzles,
22
+ X,
23
+ } from './types'
7
24
 
8
25
  export const isSwizzle = (key: unknown): key is Swizzles => {
9
26
  return is.str(key) && /^[xyzwrgbastpq]{1,4}$/.test(key)
@@ -27,8 +44,6 @@ export const isNodeProxy = (x: unknown): x is NodeProxy => {
27
44
  return x.isProxy
28
45
  }
29
46
 
30
- let count = 0
31
-
32
47
  export const hex2rgb = (hex: number) => {
33
48
  const r = ((hex >> 16) & 0xff) / 255
34
49
  const g = ((hex >> 8) & 0xff) / 255
@@ -36,6 +51,8 @@ export const hex2rgb = (hex: number) => {
36
51
  return [r, g, b]
37
52
  }
38
53
 
54
+ let count = 0
55
+
39
56
  export const getId = () => `i${count++}`
40
57
 
41
58
  export const joins = (children: X[], c: NodeConfig) => {
@@ -55,6 +72,15 @@ export const getOperator = (op: X) => {
55
72
  return OPERATORS[op as keyof typeof OPERATORS] || op
56
73
  }
57
74
 
75
+ export const getBluiltin = (id: string) => {
76
+ return WGSL_TO_GLSL_BUILTIN[id as keyof typeof WGSL_TO_GLSL_BUILTIN]
77
+ }
78
+
79
+ export const conversionToConstant = (conversionKey: string): Constants => {
80
+ const index = CONVERSIONS.indexOf(conversionKey as Conversions)
81
+ return index !== -1 ? CONSTANTS[index] : 'float'
82
+ }
83
+
58
84
  const generateHead = (c: NodeConfig) => {
59
85
  return Array.from(c.headers!)
60
86
  .map(([, v]) => v)
@@ -62,37 +88,61 @@ const generateHead = (c: NodeConfig) => {
62
88
  }
63
89
 
64
90
  export const generateDefine = (props: NodeProps, c: NodeConfig) => {
65
- const { id, children = [] } = props
91
+ const { id, children = [], layout } = props
66
92
  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)};`)
93
+ const returnType = layout?.type && layout?.type !== 'auto' ? layout?.type : y ? infer(y, c) : 'void'
94
+ const argParams: [name: string, type: string][] = []
95
+ const params: string[] = []
96
+ if (layout?.inputs)
97
+ for (const input of layout.inputs) {
98
+ argParams.push([input.name, input.type])
99
+ }
100
+ else
101
+ for (let i = 0; i < args.length; i++) {
102
+ argParams.push([`p${i}`, infer(args[i], c)])
103
+ }
104
+ let ret = ''
71
105
  if (c?.isWebGL) {
72
- const paramList = params.map(([name, type]) => `${type} ${name}`)
73
- return `${returnType} ${id}(${paramList}) {\n${lines.join('\n')}\n}`
106
+ for (const [id, type] of argParams) params.push(`${type} ${id}`)
107
+ ret += `${returnType} ${id}(${params}) {\n`
108
+ } else {
109
+ for (const [id, type] of argParams) params.push(`${id}: ${formatConversions(type, c)}`)
110
+ ret += `fn ${id}(${params}) -> ${formatConversions(returnType, c)} {\n`
74
111
  }
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}`
112
+ const scopeCode = code(x, c)
113
+ if (scopeCode) ret += scopeCode + '\n'
114
+ if (y) ret += `return ${code(y, c)};`
115
+ ret += '\n}'
116
+ return ret
78
117
  }
79
118
 
80
- const generateFragmentMain = (body: string, head: string, isWebGL = true) => {
81
- if (isWebGL)
82
- return `
119
+ const GLSL_FRAGMENT_HEAD = `
83
120
  #version 300 es
84
121
  precision mediump float;
85
122
  out vec4 fragColor;
86
- ${head}
87
- void main() {
88
- fragColor = ${body};
89
- }`.trim()
90
- return `
91
- ${head}
123
+ `.trim()
124
+
125
+ const WGSL_FRAGMENT_HEAD = `
92
126
  @fragment
93
127
  fn main(@builtin(position) position: vec4f) -> @location(0) vec4f {
94
- return ${body};
95
- }`.trim()
128
+ `.trim()
129
+
130
+ const generateFragmentMain = (body: string, head: string, isWebGL = true) => {
131
+ let ret = ''
132
+ if (isWebGL) ret += GLSL_FRAGMENT_HEAD
133
+ if (head) ret += '\n' + head + '\n'
134
+ if (isWebGL) ret += `void main() {\n fragColor = ${body};`
135
+ else {
136
+ ret += WGSL_FRAGMENT_HEAD + '\n'
137
+ ret += ` return ${body};`
138
+ }
139
+ ret += '\n}'
140
+ return ret
141
+ }
142
+
143
+ const generateVertexMain = (_body: string, _head: string, isWebGL = true) => {
144
+ if (isWebGL) return ``
145
+ return ``
96
146
  }
97
147
 
98
148
  export const fragment = (x: X, c: NodeConfig = {}) => {
@@ -106,5 +156,5 @@ export const fragment = (x: X, c: NodeConfig = {}) => {
106
156
  export const vertex = (x: X, c: NodeConfig) => {
107
157
  const body = code(x, c)
108
158
  const head = generateHead(c)
109
- return generateFragmentMain(body, head, c.isWebGL)
159
+ return generateVertexMain(body, head, c.isWebGL)
110
160
  }