glre 0.21.0 → 0.23.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/webgl.ts ADDED
@@ -0,0 +1,78 @@
1
+ import { nested } from 'reev'
2
+ import { glsl } from './code/glsl'
3
+ import { is } from './utils/helpers'
4
+ import type { X } from './node'
5
+ import type { GL } from './types'
6
+ import { activeTexture, createAttrib, createIbo, createProgram, createTexture, createVbo } from './utils/program'
7
+
8
+ export const webgl = async (gl: GL) => {
9
+ let vs = gl.vs || gl.vert || gl.vertex
10
+ let fs = gl.fs || gl.frag || gl.fragment
11
+ if (is.obj(fs)) fs = glsl(fs as X)
12
+ if (is.obj(vs)) vs = glsl(vs as X)
13
+ const c = gl.el.getContext('webgl2')!
14
+ const pg = createProgram(c, vs, fs)
15
+
16
+ let _activeUnit = 0
17
+ const activeUnits = nested(() => _activeUnit++)
18
+
19
+ const locations = nested((key, bool = false) => {
20
+ if (bool) return c.getAttribLocation(pg, key)
21
+ return c.getUniformLocation(pg, key) as WebGLUniformLocation
22
+ })
23
+
24
+ const strides = nested((_, count: number, value: number[], iboValue?: number[]) => {
25
+ if (iboValue) count = Math.max(...iboValue) + 1
26
+ const stride = value.length / count
27
+ if (stride !== Math.floor(stride)) console.warn(`Vertex Stride Error: count ${count} is mismatch`)
28
+ return Math.floor(stride)
29
+ })
30
+
31
+ const uniforms = nested((key, value: number | number[]) => {
32
+ const loc = locations(key)
33
+ if (is.num(value)) return (value: any) => c.uniform1f(loc, value)
34
+ let l = value.length as 3
35
+ if (l <= 4) return (value: any) => c[`uniform${l}fv`](loc, value)
36
+ l = (Math.sqrt(l) << 0) as 3
37
+ return (value: any) => c[`uniformMatrix${l}fv`](loc, false, value)
38
+ })
39
+
40
+ gl('clean', () => c.deleteProgram(pg))
41
+
42
+ gl('render', () => {
43
+ c.useProgram(pg)
44
+ gl.queue.flush()
45
+ c.clear(c.COLOR_BUFFER_BIT)
46
+ c.viewport(0, 0, ...gl.size)
47
+ c.drawArrays(c.TRIANGLES, 0, 3)
48
+ })
49
+
50
+ gl('_attribute', (key = '', value: number[], iboValue: number[]) => {
51
+ const loc = locations(key, true)
52
+ const vbo = createVbo(c, value)
53
+ const ibo = createIbo(c, iboValue)
54
+ const str = strides(key, gl.count, value, iboValue)
55
+ createAttrib(c, str, loc, vbo, ibo)
56
+ })
57
+
58
+ gl('_uniform', (key: string, value: number | number[]) => {
59
+ uniforms(key, value)(value)
60
+ })
61
+
62
+ const _loadFn = (image: HTMLImageElement) => {
63
+ const loc = locations(image.alt)
64
+ const unit = activeUnits(image.alt)
65
+ const tex = createTexture(c, image)
66
+ activeTexture(c, loc, unit, tex)
67
+ }
68
+
69
+ gl('_texture', (alt: string, src: string) => {
70
+ const image = new Image()
71
+ image.addEventListener('load', _loadFn.bind(null, image), false)
72
+ Object.assign(image, { src, alt, crossOrigin: 'anonymous' })
73
+ })
74
+
75
+ gl.webgl = { context: c, program: pg }
76
+
77
+ return gl
78
+ }
package/src/webgpu.ts ADDED
@@ -0,0 +1,70 @@
1
+ import { wgsl } from './code/wgsl'
2
+ import { is } from './utils/helpers'
3
+ import {
4
+ createRenderPipeline,
5
+ createDescriptor,
6
+ // createUniformBuffer,
7
+ // updateBindGroup,
8
+ // createVertexBuffer,
9
+ // createUniform,
10
+ // createDeviceTexture,
11
+ // createSampler,
12
+ } from './utils/pipeline'
13
+ import type { X } from './node'
14
+ import type { GL, GPUPipeline } from './types'
15
+
16
+ const quadVertexCount = 3
17
+
18
+ export const webgpu = async (gl: GL) => {
19
+ let vs = gl.vs || gl.vert || gl.vertex
20
+ let fs = gl.fs || gl.frag || gl.fragment
21
+ if (is.obj(vs)) vs = wgsl(vs as X)
22
+ if (is.obj(fs)) fs = wgsl(fs as X)
23
+ const c = gl.el.getContext('webgpu') as any
24
+ const gpu = (navigator as any).gpu
25
+ const adapter = await gpu.requestAdapter()
26
+ const device = await adapter.requestDevice()
27
+ const format = gpu.getPreferredCanvasFormat()
28
+ c.configure({ device, format, alphaMode: 'opaque' })
29
+
30
+ gl('clean', () => {})
31
+
32
+ let pipeline: GPUPipeline
33
+
34
+ gl('render', () => {
35
+ if (!pipeline) pipeline = createRenderPipeline(device, format, vs, fs, [])
36
+ const encoder = device.createCommandEncoder()
37
+ const pass = encoder.beginRenderPass(createDescriptor(c))
38
+ pass.setPipeline(pipeline)
39
+ pass.draw(quadVertexCount, 1, 0, 0)
40
+ pass.end()
41
+ device.queue.submit([encoder.finish()])
42
+ })
43
+
44
+ gl('_attribute', (key = '', value: number[]) => {
45
+ // @TODO FIX
46
+ // vertexBuffers(key, value)
47
+ })
48
+
49
+ gl('_uniform', (key: string, value = 0) => {
50
+ // @TODO FIX
51
+ // if (!device || !uniformBuffer) return
52
+ // uniforms[key] = value
53
+ // const uniformData = new Float32Array(Object.values(uniforms))
54
+ // createUniform(device, uniformBuffer, uniformData)
55
+ })
56
+
57
+ // const _loadFun = (image: HTMLImageElement, gl: GL) => {
58
+ // const texture = createDeviceTexture(device, image)
59
+ // // bindGroup = updateBindGroup(device, pipeline, uniformBuffer, textures, sampler)
60
+ // }
61
+
62
+ gl('_texture', (alt: string, src: string) => {
63
+ // @TODO FIX
64
+ // const image = new Image()
65
+ // image.addEventListener('load', _loadFun.bind(null, image, gl), false)
66
+ // Object.assign(image, { src, alt, crossOrigin: 'anonymous' })
67
+ })
68
+
69
+ return gl
70
+ }
@@ -1,79 +0,0 @@
1
- import { EventState, Nested } from 'reev';
2
- import { Queue, Frame } from 'refr';
3
-
4
- declare type Uniform = number | number[];
5
- declare type Attribute = number[];
6
- declare type GL = EventState<{
7
- /**
8
- * initial value
9
- */
10
- id: string;
11
- width: number;
12
- height: number;
13
- size: [number, number];
14
- mouse: [number, number];
15
- count: number;
16
- vs: string;
17
- fs: string;
18
- vert: string;
19
- frag: string;
20
- vertex: string;
21
- fragment: string;
22
- varying: string;
23
- int: PrecisionMode;
24
- float: PrecisionMode;
25
- sampler2D: PrecisionMode;
26
- samplerCube: PrecisionMode;
27
- lastActiveUnit: number;
28
- /**
29
- * core state
30
- */
31
- gl: any;
32
- pg: any;
33
- el: any;
34
- queue: Queue;
35
- frame: Frame;
36
- target: any;
37
- stride: Nested<number>;
38
- location: Nested<any>;
39
- activeUnit: Nested<number>;
40
- default: any;
41
- /**
42
- * events
43
- */
44
- ref?: any;
45
- init(varying?: string[]): void;
46
- mount(): void;
47
- clean(): void;
48
- render(): void;
49
- mousemove(e: Event): void;
50
- resize(e?: Event, width?: number, height?: number): void;
51
- load(e?: Event, image?: HTMLImageElement): void;
52
- /**
53
- * setter
54
- */
55
- uniform(key: string, value: Uniform): GL;
56
- uniform(target: {
57
- [key: string]: Uniform;
58
- }): GL;
59
- texture(key: string, value: string): GL;
60
- texture(target: {
61
- [key: string]: string;
62
- }): GL;
63
- attribute(key: string, value: Attribute, iboValue?: Attribute): GL;
64
- attribute(target: {
65
- [key: string]: Attribute;
66
- }): GL;
67
- /**
68
- * short hands
69
- */
70
- clear(key?: GLClearMode): void;
71
- viewport(size?: [number, number]): void;
72
- drawArrays(key?: GLDrawMode): void;
73
- drawElements(key?: GLDrawMode): void;
74
- }>;
75
- declare type PrecisionMode = 'highp' | 'mediump' | 'lowp';
76
- declare type GLClearMode = 'COLOR_BUFFER_BIT' | 'DEPTH_BUFFER_BIT' | 'STENCIL_BUFFER_BIT';
77
- declare type GLDrawMode = 'POINTS' | 'LINE_STRIP' | 'LINE_LOOP' | 'LINES' | 'TRIANGLE_STRIP' | 'TRIANGLE_FAN' | 'TRIANGLES';
78
-
79
- export { Attribute as A, GL as G, PrecisionMode as P, Uniform as U, GLClearMode as a, GLDrawMode as b };