glre 0.46.0 → 0.47.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "glre",
3
- "version": "0.46.0",
3
+ "version": "0.47.0",
4
4
  "author": "tseijp",
5
5
  "license": "MIT",
6
6
  "private": false,
@@ -114,7 +114,10 @@ export const code = <T extends C>(target: Y<T>, c?: NodeContext | null): string
114
114
  setupEvent(c, id, varType, target, x)
115
115
  head = parseUniformHead(c, id, varType)
116
116
  }
117
- if (type === 'storage') head = parseStorageHead(c, id, infer(target, c))
117
+ if (type === 'storage') {
118
+ setupEvent(c, id, type, target, x)
119
+ head = parseStorageHead(c, id, infer(target, c))
120
+ }
118
121
  if (type === 'constant') head = parseConstantHead(c, id, infer(target, c), code(x, c))
119
122
  if (head) {
120
123
  c.code?.headers.set(id, head)
@@ -1,6 +1,6 @@
1
1
  import { isConstants, isElement, isX, isSwizzle } from './utils'
2
2
  import { BUILTIN_TYPES, COMPONENT_COUNT_TO_TYPE, FUNCTION_RETURN_TYPES, getOperatorResultType, validateOperatorTypes } from './const'
3
- import { is, getStride } from '../../helpers'
3
+ import { is, getStride, isFloat32 } from '../../helpers'
4
4
  import type { Constants as C, NodeContext, X, Y } from '../types'
5
5
 
6
6
  const inferBuiltin = <T extends C>(id: string | undefined) => {
@@ -16,7 +16,7 @@ export const inferPrimitiveType = <T extends C>(x: Y<T>) => {
16
16
  if (is.bol(x)) return 'bool' as T
17
17
  if (is.str(x)) return 'texture' as T
18
18
  if (is.num(x)) return 'float' as T // @TODO FIX: Number.isInteger(x) ? 'int' : 'float'
19
- if (is.arr(x)) return COMPONENT_COUNT_TO_TYPE[x.length as keyof typeof COMPONENT_COUNT_TO_TYPE] as T
19
+ if (is.arr(x) || isFloat32(x)) return COMPONENT_COUNT_TO_TYPE[x.length as keyof typeof COMPONENT_COUNT_TO_TYPE] as T
20
20
  if (isElement(x)) return 'texture' as T
21
21
  return 'void' as T
22
22
  }
@@ -1,5 +1,5 @@
1
1
  import { CONSTANTS, CONVERSIONS, FUNCTIONS, OPERATOR_KEYS, OPERATORS, TYPE_MAPPING, WGSL_TO_GLSL_BUILTIN } from './const'
2
- import { is } from '../../helpers'
2
+ import { is, isFloat32 } from '../../helpers'
3
3
  import { storageSize } from '../../webgl/utils'
4
4
  import type { Constants as C, Conversions, Functions, NodeContext, Operators, Swizzles, X, Y } from '../types'
5
5
 
@@ -103,28 +103,33 @@ export const addDependency = (c: NodeContext, id = '', type: string) => {
103
103
  */
104
104
  const getEventFun = (c: NodeContext, id: string, type: string) => {
105
105
  if (c.isWebGL) {
106
- if (type === 'attribute') return (value: any) => c.gl?.attribute?.(id, value)
107
- if (type === 'instance') return (value: any) => c.gl?.instance?.(id, value)
108
- if (type === 'texture') return (value: any) => c.gl?.texture?.(id, value)
106
+ if (type === 'attribute') return c.gl?.attribute?.bind(null, id as any)
107
+ if (type === 'instance') return c.gl?.instance?.bind(null, id as any)
108
+ if (type === 'texture') return c.gl?.texture?.bind(null, id as any)
109
+ if (type === 'storage') return c.gl?.storage?.bind(null, id as any)
109
110
  return (value: any) => c.gl?.uniform?.(id, value)
110
111
  }
111
- if (type === 'attribute') return (value: any) => c.gl?._attribute?.(id, value)
112
- if (type === 'instance') return (value: any) => c.gl?._instance?.(id, value)
113
- if (type === 'texture') return (value: any) => c.gl?._texture?.(id, value)
112
+ if (type === 'attribute') return c.gl?._attribute?.bind(null, id)
113
+ if (type === 'instance') return c.gl?._instance?.bind(null, id)
114
+ if (type === 'texture') return c.gl?._texture?.bind(null, id)
115
+ if (type === 'storage') return c.gl?._storage?.bind(null, id)
114
116
  return (value: any) => c.gl?._uniform?.(id, value)
115
117
  }
116
118
 
117
- const safeEventCall = <T extends C>(x: X<T>, fun: (value: unknown) => void) => {
119
+ const safeEventCall = <T extends C>(x: X<T>, fun: (value: any) => void) => {
118
120
  if (is.und(x)) return
119
121
  if (!isX(x)) return fun(x) // for uniform(0) or uniform([0, 1])
120
122
  if (x.type !== 'conversion') return
121
123
  const args = x.props.children?.slice(1)
122
124
  if (is.und(args?.[0])) return // ignore if uniform(vec2())
125
+ if (is.arr(args[0])) return fun(args[0]) // for attribute(vec2([0, 0.73, -1, -1, 1, -1]))
126
+ if (isFloat32(args[0])) return fun(args[0]) // for storage(float(new Float32Array(1024)))
123
127
  fun(args.map((x) => x ?? args[0])) // for uniform(vec2(1)) or uniform(vec2(1, 1))
124
128
  }
125
129
 
126
130
  export const setupEvent = (c: NodeContext, id: string, type: string, target: X, child: X) => {
127
131
  const fun = getEventFun(c, id, type)
132
+ if (!fun) return
128
133
  safeEventCall(child, fun)
129
134
  target.listeners.add(fun)
130
135
  return fun