glre 0.38.0 → 0.39.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/dist/addons.cjs +2 -0
- package/dist/addons.cjs.map +1 -0
- package/dist/addons.d.ts +457 -0
- package/dist/addons.js +2 -0
- package/dist/addons.js.map +1 -0
- package/dist/index.cjs +29 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +153 -307
- package/dist/index.js +29 -41
- package/dist/index.js.map +1 -1
- package/dist/native.cjs +1 -48
- package/dist/native.cjs.map +1 -1
- package/dist/native.d.ts +508 -8
- package/dist/native.js +1 -48
- package/dist/native.js.map +1 -1
- package/dist/node.cjs +40 -0
- package/dist/node.cjs.map +1 -0
- package/dist/node.d.ts +614 -0
- package/dist/node.js +40 -0
- package/dist/node.js.map +1 -0
- package/dist/react.cjs +1 -48
- package/dist/react.cjs.map +1 -1
- package/dist/react.d.ts +506 -4
- package/dist/react.js +1 -48
- package/dist/react.js.map +1 -1
- package/dist/solid.cjs +1 -48
- package/dist/solid.cjs.map +1 -1
- package/dist/solid.d.ts +506 -4
- package/dist/solid.js +1 -48
- package/dist/solid.js.map +1 -1
- package/package.json +56 -3
- package/src/addons/index.ts +6 -0
- package/src/index.ts +6 -22
- package/src/node/{core.ts → build.ts} +3 -7
- package/src/node/create.ts +73 -0
- package/src/node/index.ts +63 -47
- package/src/node/scope.ts +50 -47
- package/src/node/types.ts +207 -177
- package/src/node/utils/const.ts +62 -2
- package/src/node/utils/index.ts +6 -3
- package/src/node/utils/infer.ts +17 -29
- package/src/node/utils/parse.ts +11 -11
- package/src/node/utils/utils.ts +9 -9
- package/src/types.ts +2 -4
- package/src/utils/pipeline.ts +3 -3
- package/src/utils/program.ts +6 -1
- package/src/{webgl.ts → utils/webgl.ts} +24 -14
- package/src/{webgpu.ts → utils/webgpu.ts} +28 -7
- package/src/node/node.ts +0 -65
package/src/node/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { hex2rgb } from './utils'
|
|
2
|
-
import { builtin as b, conversion as c, function_ as f, uniform as u } from './
|
|
2
|
+
import { builtin as b, conversion as c, function_ as f, uniform as u } from './create'
|
|
3
3
|
import { is } from '../utils/helpers'
|
|
4
|
-
import type { Constants as C, X,
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './
|
|
4
|
+
import type { Constants as C, Float, X, Y } from './types'
|
|
5
|
+
export * from './build'
|
|
6
|
+
export * from './create'
|
|
7
7
|
export * from './scope'
|
|
8
8
|
export * from './types'
|
|
9
9
|
|
|
@@ -29,28 +29,28 @@ export const screenCoordinate = b<'vec2'>('screenCoordinate')
|
|
|
29
29
|
export const screenUV = b<'vec2'>('screenUV')
|
|
30
30
|
|
|
31
31
|
// Type constructors with proper type inference
|
|
32
|
-
export const float = (x?:
|
|
33
|
-
export const int = (x?:
|
|
34
|
-
export const uint = (x?:
|
|
35
|
-
export const bool = (x?:
|
|
36
|
-
export const vec2 = (x?:
|
|
37
|
-
export const vec3 = (x?:
|
|
38
|
-
export const vec4 = (x?:
|
|
39
|
-
export const mat2 = (...args:
|
|
40
|
-
export const mat3 = (...args:
|
|
41
|
-
export const mat4 = (...args:
|
|
42
|
-
export const ivec2 = (x?:
|
|
43
|
-
export const ivec3 = (x?:
|
|
44
|
-
export const ivec4 = (x?:
|
|
45
|
-
export const uvec2 = (x?:
|
|
46
|
-
export const uvec3 = (x?:
|
|
47
|
-
export const uvec4 = (x?:
|
|
48
|
-
export const bvec2 = (x?:
|
|
49
|
-
export const bvec3 = (x?:
|
|
50
|
-
export const bvec4 = (x?:
|
|
51
|
-
export const texture2D = (x?:
|
|
32
|
+
export const float = (x?: Y) => c('float', x)
|
|
33
|
+
export const int = (x?: Y) => c('int', x)
|
|
34
|
+
export const uint = (x?: Y) => c('uint', x)
|
|
35
|
+
export const bool = (x?: Y) => c('bool', x)
|
|
36
|
+
export const vec2 = (x?: Y, y?: Y) => c('vec2', x, y)
|
|
37
|
+
export const vec3 = (x?: Y, y?: Y, z?: Y) => c('vec3', x, y, z)
|
|
38
|
+
export const vec4 = (x?: Y, y?: Y, z?: Y, w?: Y) => c('vec4', x, y, z, w)
|
|
39
|
+
export const mat2 = (...args: Y[]) => c('mat2', ...args)
|
|
40
|
+
export const mat3 = (...args: Y[]) => c('mat3', ...args)
|
|
41
|
+
export const mat4 = (...args: Y[]) => c('mat4', ...args)
|
|
42
|
+
export const ivec2 = (x?: Y, y?: Y) => c('ivec2', x, y)
|
|
43
|
+
export const ivec3 = (x?: Y, y?: Y, z?: Y) => c('ivec3', x, y, z)
|
|
44
|
+
export const ivec4 = (x?: Y, y?: Y, z?: Y, w?: Y) => c('ivec4', x, y, z, w)
|
|
45
|
+
export const uvec2 = (x?: Y, y?: Y) => c('uvec2', x, y)
|
|
46
|
+
export const uvec3 = (x?: Y, y?: Y, z?: Y) => c('uvec3', x, y, z)
|
|
47
|
+
export const uvec4 = (x?: Y, y?: Y, z?: Y, w?: Y) => c('uvec4', x, y, z, w)
|
|
48
|
+
export const bvec2 = (x?: Y, y?: Y) => c('bvec2', x, y)
|
|
49
|
+
export const bvec3 = (x?: Y, y?: Y, z?: Y) => c('bvec3', x, y, z)
|
|
50
|
+
export const bvec4 = (x?: Y, y?: Y, z?: Y, w?: Y) => c('bvec4', x, y, z, w)
|
|
51
|
+
export const texture2D = (x?: Y) => c('texture', x)
|
|
52
52
|
export const sampler2D = () => c('sampler2D')
|
|
53
|
-
export const color = (r?:
|
|
53
|
+
export const color = (r?: Y, g?: Y, b?: Y) => {
|
|
54
54
|
if (is.num(r) && is.und(g) && is.und(b)) return vec3(...hex2rgb(r))
|
|
55
55
|
return vec3(r, g, b)
|
|
56
56
|
}
|
|
@@ -64,20 +64,26 @@ export const uv = position.xy.div(iResolution)
|
|
|
64
64
|
/**
|
|
65
65
|
* 1.1. unified with:
|
|
66
66
|
* 2.1. const.ts BUILTIN_VARIABLES and
|
|
67
|
-
* 3.1. types.ts
|
|
67
|
+
* 3.1. types.ts _N
|
|
68
68
|
*/
|
|
69
69
|
// 0. Always return bool
|
|
70
70
|
export const all = <T extends C>(x: X<T>) => f<'bool'>('all', x)
|
|
71
71
|
export const any = <T extends C>(x: X<T>) => f<'bool'>('any', x)
|
|
72
72
|
|
|
73
|
-
// 2. Always return float
|
|
74
|
-
export const
|
|
73
|
+
// 2. Always return float with WGSL-compliant type constraints and unified parameter types
|
|
74
|
+
export const determinant = <T extends 'mat2' | 'mat3' | 'mat4'>(x: X<T>) => f<'float'>('determinant', x)
|
|
75
|
+
export const distance = <T extends 'vec2' | 'vec3' | 'vec4', U extends C>(x: X<T>, y: number | X<U>) =>
|
|
76
|
+
f<'float'>('distance', x, y)
|
|
77
|
+
export const dot = <T extends 'vec2' | 'vec3' | 'vec4' | 'ivec2' | 'ivec3' | 'ivec4', U extends C>(
|
|
78
|
+
x: X<T>,
|
|
79
|
+
y: number | X<U>
|
|
80
|
+
) => f<T extends `ivec${string}` ? 'int' : 'float'>('dot', x, y)
|
|
81
|
+
export const length = <T extends 'vec2' | 'vec3' | 'vec4'>(x: X<T>) => f<'float'>('length', x)
|
|
75
82
|
export const lengthSq = (x: X) => f<'float'>('lengthSq', x)
|
|
76
|
-
export const
|
|
77
|
-
export const dot = (x: X, y: X) => f<'float'>('dot', x, y)
|
|
83
|
+
export const luminance = (x: X) => f<'float'>('luminance', x)
|
|
78
84
|
|
|
79
|
-
// 3. Always return vec3
|
|
80
|
-
export const cross = (x: X<'vec3'>, y: X<
|
|
85
|
+
// 3. Always return vec3 with vec3 constraint and unified parameter types
|
|
86
|
+
export const cross = <U extends C = 'vec3'>(x: X<'vec3'>, y: number | X<U>) => f<'vec3'>('cross', x, y)
|
|
81
87
|
|
|
82
88
|
// 4. Always return vec4
|
|
83
89
|
export const cubeTexture = (x: X, y: X, z?: X) => f<'vec4'>('cubeTexture', x, y, z)
|
|
@@ -88,7 +94,7 @@ export const textureLod = (x: X, y: X, z?: X) => f<'vec4'>('textureLod', x, y, z
|
|
|
88
94
|
/**
|
|
89
95
|
* 1.2. unified with:
|
|
90
96
|
* 2.2. const.ts FUNCTIONS and
|
|
91
|
-
* 3.2. types.ts
|
|
97
|
+
* 3.2. types.ts _N
|
|
92
98
|
*/
|
|
93
99
|
// 0. Component-wise functions
|
|
94
100
|
export const abs = <T extends C>(x: X<T>) => f<T>('abs', x)
|
|
@@ -113,7 +119,7 @@ export const inverseSqrt = <T extends C>(x: X<T>) => f<T>('inverseSqrt', x)
|
|
|
113
119
|
export const log = <T extends C>(x: X<T>) => f<T>('log', x)
|
|
114
120
|
export const log2 = <T extends C>(x: X<T>) => f<T>('log2', x)
|
|
115
121
|
export const negate = <T extends C>(x: X<T>) => f<T>('negate', x)
|
|
116
|
-
export const normalize = <T extends
|
|
122
|
+
export const normalize = <T extends 'vec2' | 'vec3' | 'vec4'>(x: X<T>) => f<T>('normalize', x)
|
|
117
123
|
export const oneMinus = <T extends C>(x: X<T>) => f<T>('oneMinus', x)
|
|
118
124
|
export const radians = <T extends C>(x: X<T>) => f<T>('radians', x)
|
|
119
125
|
export const reciprocal = <T extends C>(x: X<T>) => f<T>('reciprocal', x)
|
|
@@ -127,17 +133,27 @@ export const tan = <T extends C>(x: X<T>) => f<T>('tan', x)
|
|
|
127
133
|
export const tanh = <T extends C>(x: X<T>) => f<T>('tanh', x)
|
|
128
134
|
export const trunc = <T extends C>(x: X<T>) => f<T>('trunc', x)
|
|
129
135
|
|
|
130
|
-
// 1. Functions where first argument determines return type
|
|
131
|
-
export const atan2 = <T extends C>(x: X<T>, y: X) => f<T>('atan2', x, y)
|
|
132
|
-
export const clamp = <T extends C>(x: X<T>,
|
|
133
|
-
|
|
134
|
-
export const
|
|
135
|
-
export const
|
|
136
|
-
export const
|
|
137
|
-
|
|
138
|
-
export const
|
|
136
|
+
// 1. Functions where first argument determines return type with unified parameter types
|
|
137
|
+
export const atan2 = <T extends C, U extends C>(x: X<T>, y: number | X<U>) => f<T>('atan2', x, y)
|
|
138
|
+
export const clamp = <T extends C, U extends C>(x: X<T>, min: number | X<U>, max: number | X<U>) =>
|
|
139
|
+
f<T>('clamp', x, min, max)
|
|
140
|
+
export const max = <T extends C, U extends C>(x: X<T>, y: number | X<U>) => f<T>('max', x, y)
|
|
141
|
+
export const min = <T extends C, U extends C>(x: X<T>, y: number | X<U>) => f<T>('min', x, y)
|
|
142
|
+
export const mix = <T extends C, U extends C>(x: X<T>, y: number | X<U>, a: number | Float | X<U>) =>
|
|
143
|
+
f<T>('mix', x, y, a)
|
|
144
|
+
export const pow = <T extends C, U extends C>(x: X<T>, y: number | X<U>) => f<T>('pow', x, y)
|
|
145
|
+
export const reflect = <T extends 'vec2' | 'vec3' | 'vec4', U extends C>(I: X<T>, N: number | X<U>) =>
|
|
146
|
+
f<T>('reflect', I, N)
|
|
147
|
+
export const refract = <T extends 'vec2' | 'vec3' | 'vec4', U extends C>(
|
|
148
|
+
I: X<T>,
|
|
149
|
+
N: number | X<U>,
|
|
150
|
+
eta: number | Float
|
|
151
|
+
) => f<T>('refract', I, N, eta)
|
|
139
152
|
|
|
140
|
-
// 2. Functions where not first argument determines return type
|
|
141
|
-
export const smoothstep = <T extends C>(e0: X
|
|
142
|
-
|
|
143
|
-
export const
|
|
153
|
+
// 2. Functions where not first argument determines return type with unified parameter types
|
|
154
|
+
export const smoothstep = <T extends C, U extends C>(e0: number | X<U>, e1: number | X<U>, x: X<T>) =>
|
|
155
|
+
f<T>('smoothstep', e0, e1, x)
|
|
156
|
+
export const step = <T extends C, U extends C>(edge: number | X<U>, x: X<T>) => f<T>('step', edge, x)
|
|
157
|
+
export const mod = <T extends C, U extends C>(x: X<T>, y: number | X<U>) => {
|
|
158
|
+
return (x as any).sub((x as any).div(y).floor().mul(y)) as X<T>
|
|
159
|
+
}
|
package/src/node/scope.ts
CHANGED
|
@@ -1,62 +1,69 @@
|
|
|
1
1
|
import { getId } from './utils'
|
|
2
|
-
import { conversion,
|
|
2
|
+
import { conversion, create } from './create'
|
|
3
3
|
import type {
|
|
4
4
|
FnLayout,
|
|
5
5
|
FnType,
|
|
6
|
-
|
|
7
|
-
NodeProxy,
|
|
8
|
-
X,
|
|
9
|
-
Constants,
|
|
6
|
+
Constants as C,
|
|
10
7
|
Int,
|
|
8
|
+
NodeProps,
|
|
9
|
+
Struct,
|
|
11
10
|
StructFactory,
|
|
12
|
-
StructNode,
|
|
13
11
|
StructFields,
|
|
12
|
+
X,
|
|
13
|
+
Y,
|
|
14
14
|
} from './types'
|
|
15
15
|
|
|
16
|
-
let scope:
|
|
17
|
-
let define:
|
|
16
|
+
let scope: X | null = null
|
|
17
|
+
let define: X | null = null
|
|
18
18
|
|
|
19
|
-
const addToScope = (x:
|
|
19
|
+
export const addToScope = <T extends C>(x: X<T>) => {
|
|
20
20
|
if (!scope) return
|
|
21
21
|
if (!scope.props.children) scope.props.children = []
|
|
22
22
|
scope.props.children.push(x)
|
|
23
|
-
if (x.type !== 'return' || !define) return
|
|
23
|
+
if (x.type !== 'return' || !define) return x
|
|
24
24
|
const { props } = define
|
|
25
25
|
if (!props.inferFrom) props.inferFrom = []
|
|
26
26
|
props.inferFrom.push(x)
|
|
27
|
+
return x
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
export function toVar<T extends StructFields>(x:
|
|
30
|
-
export function toVar<T extends
|
|
30
|
+
export function toVar<T extends StructFields>(x: Struct<T>, id?: string): Struct<T>
|
|
31
|
+
export function toVar<T extends C>(x: X<T>, id?: string): X<T> {
|
|
31
32
|
if (!id) id = getId()
|
|
32
|
-
const y =
|
|
33
|
-
const z =
|
|
33
|
+
const y = create<T>('variable', { id, inferFrom: [x] })
|
|
34
|
+
const z = create<T>('declare', null, x as X, y)
|
|
34
35
|
addToScope(z)
|
|
35
36
|
return y
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
export const assign = <T extends
|
|
39
|
-
const z =
|
|
39
|
+
export const assign = <T extends C>(x: X<T>, isScatter = false, y: Y<T>): X<T> => {
|
|
40
|
+
const z = create(isScatter ? 'scatter' : 'assign', null, x, y)
|
|
40
41
|
addToScope(z)
|
|
41
42
|
return x
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
export const Return = <T extends
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
export const Return = <T extends C>(x: Y<T>): Y<T> => {
|
|
46
|
+
return addToScope(create<T>('return', { inferFrom: [x] }, x))
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const Break = (): Y => {
|
|
50
|
+
return addToScope(create('break'))
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const Continue = (): Y => {
|
|
54
|
+
return addToScope(create('continue'))
|
|
48
55
|
}
|
|
49
56
|
|
|
50
57
|
export const struct = <T extends StructFields>(fields: T, id = getId()): StructFactory<T> => {
|
|
51
58
|
return (initialValues: StructFields = {}, instanceId = getId()) => {
|
|
52
|
-
const x =
|
|
53
|
-
const y =
|
|
59
|
+
const x = create('variable', { id: instanceId, inferFrom: [id] })
|
|
60
|
+
const y = create('struct', { id, fields, initialValues }, x)
|
|
54
61
|
addToScope(y)
|
|
55
|
-
return x as
|
|
62
|
+
return x as Struct<T>
|
|
56
63
|
}
|
|
57
64
|
}
|
|
58
65
|
|
|
59
|
-
const scoped = (x:
|
|
66
|
+
export const scoped = (x: X, fun: () => X | void, y = define) => {
|
|
60
67
|
const [_scope, _define] = [scope, define]
|
|
61
68
|
;[scope, define] = [x, y]
|
|
62
69
|
const z = fun()
|
|
@@ -64,20 +71,20 @@ const scoped = (x: NodeProxy, fun: () => NodeProxy | void, y = define) => {
|
|
|
64
71
|
;[scope, define] = [_scope, _define]
|
|
65
72
|
}
|
|
66
73
|
|
|
67
|
-
export const If = (x:
|
|
68
|
-
const y =
|
|
74
|
+
export const If = (x: Y, fun: () => void) => {
|
|
75
|
+
const y = create('scope')
|
|
69
76
|
scoped(y, fun)
|
|
70
|
-
const ifNode =
|
|
77
|
+
const ifNode = create('if', null, x, y)
|
|
71
78
|
addToScope(ifNode)
|
|
72
79
|
const ret = () => ({
|
|
73
80
|
ElseIf: (_x: X, _fun: () => void) => {
|
|
74
|
-
const _y =
|
|
81
|
+
const _y = create('scope')
|
|
75
82
|
scoped(_y, _fun)
|
|
76
83
|
ifNode.props.children!.push(_x, _y)
|
|
77
84
|
return ret()
|
|
78
85
|
},
|
|
79
86
|
Else: (_fun: () => void) => {
|
|
80
|
-
const _x =
|
|
87
|
+
const _x = create('scope')
|
|
81
88
|
scoped(_x, _fun)
|
|
82
89
|
ifNode.props.children!.push(_x)
|
|
83
90
|
},
|
|
@@ -85,29 +92,28 @@ export const If = (x: NodeProxy, fun: () => void) => {
|
|
|
85
92
|
return ret()
|
|
86
93
|
}
|
|
87
94
|
|
|
88
|
-
export const Loop = (x:
|
|
89
|
-
const y =
|
|
95
|
+
export const Loop = (x: Y, fun: (params: { i: Int }) => void) => {
|
|
96
|
+
const y = create('scope')
|
|
90
97
|
const id = getId()
|
|
91
|
-
scoped(y, () => fun({ i:
|
|
92
|
-
const ret =
|
|
93
|
-
addToScope(ret)
|
|
94
|
-
return ret
|
|
98
|
+
scoped(y, () => fun({ i: create<'int'>('variable', { id, inferFrom: [conversion('int', 0)] }) }))
|
|
99
|
+
const ret = create('loop', { id }, x, y)
|
|
100
|
+
return addToScope(ret)
|
|
95
101
|
}
|
|
96
102
|
|
|
97
|
-
export const Switch = (x:
|
|
98
|
-
const switchNode =
|
|
103
|
+
export const Switch = (x: Y) => {
|
|
104
|
+
const switchNode = create('switch', null, x)
|
|
99
105
|
addToScope(switchNode)
|
|
100
106
|
const ret = () => ({
|
|
101
107
|
Case: (...values: X[]) => {
|
|
102
108
|
return (fun: () => void) => {
|
|
103
|
-
const y =
|
|
109
|
+
const y = create('scope')
|
|
104
110
|
scoped(y, fun)
|
|
105
111
|
for (const _x of values) switchNode.props.children!.push(_x, y)
|
|
106
112
|
return ret()
|
|
107
113
|
}
|
|
108
114
|
},
|
|
109
115
|
Default: (fun: () => void) => {
|
|
110
|
-
const scope =
|
|
116
|
+
const scope = create('scope')
|
|
111
117
|
scoped(scope, fun)
|
|
112
118
|
switchNode.props.children!.push(scope)
|
|
113
119
|
},
|
|
@@ -115,15 +121,12 @@ export const Switch = (x: NodeProxy) => {
|
|
|
115
121
|
return ret()
|
|
116
122
|
}
|
|
117
123
|
|
|
118
|
-
export function Fn<T extends
|
|
119
|
-
fun: (args: Args) => T,
|
|
120
|
-
defaultId = getId()
|
|
121
|
-
) {
|
|
124
|
+
export function Fn<T extends X | Struct | void, Args extends any[]>(fun: (args: Args) => T, defaultId = getId()) {
|
|
122
125
|
let layout: FnLayout
|
|
123
126
|
const ret = (...args: any[]) => {
|
|
124
127
|
const id = layout?.name || defaultId
|
|
125
|
-
const x =
|
|
126
|
-
const paramVars:
|
|
128
|
+
const x = create('scope')
|
|
129
|
+
const paramVars: X[] = []
|
|
127
130
|
const paramDefs: NodeProps[] = []
|
|
128
131
|
if (layout?.inputs) {
|
|
129
132
|
for (const input of layout.inputs) {
|
|
@@ -134,8 +137,8 @@ export function Fn<T extends NodeProxy | StructNode | void, Args extends any[]>(
|
|
|
134
137
|
paramDefs.push({ id: `p${i}`, inferFrom: [args[i]] })
|
|
135
138
|
}
|
|
136
139
|
}
|
|
137
|
-
for (const props of paramDefs) paramVars.push(
|
|
138
|
-
const y =
|
|
140
|
+
for (const props of paramDefs) paramVars.push(create('variable', props))
|
|
141
|
+
const y = create('define', { id, layout }, x, ...args)
|
|
139
142
|
scoped(x, () => fun(paramVars as Args) as any, y)
|
|
140
143
|
return y
|
|
141
144
|
}
|