@symbiote-native/gl 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +38 -2
- package/build/angular/gl-view/index.d.ts +18 -0
- package/build/angular/gl-view/index.js +87 -0
- package/build/angular/index.d.ts +2 -0
- package/build/angular/index.js +3 -0
- package/build/core/constants.d.ts +4 -0
- package/build/core/constants.js +4 -0
- package/build/core/gl-context.d.ts +13 -0
- package/build/core/gl-context.js +54 -0
- package/build/core/gl-errors.d.ts +1 -0
- package/build/core/gl-errors.js +8 -0
- package/build/core/gl-utils.d.ts +5 -0
- package/build/core/gl-utils.js +84 -0
- package/build/core/gl-view.d.ts +16 -0
- package/build/core/gl-view.js +99 -0
- package/build/core/index.d.ts +6 -0
- package/build/core/index.js +5 -0
- package/build/core/native-module.d.ts +15 -0
- package/build/core/native-module.js +3 -0
- package/build/core/types.d.ts +75 -0
- package/build/core/types.js +13 -0
- package/build/core/worklet-context-manager.d.ts +6 -0
- package/build/core/worklet-context-manager.js +34 -0
- package/build/react/gl-view.d.ts +8 -0
- package/build/react/gl-view.js +6 -0
- package/build/react/index.d.ts +3 -0
- package/build/react/index.js +3 -0
- package/build/solid/gl-view.d.ts +13 -0
- package/build/solid/gl-view.js +4 -0
- package/build/solid/index.d.ts +2 -0
- package/build/solid/index.js +3 -0
- package/build/svelte/gl-view.svelte +19 -0
- package/build/svelte/gl-view.svelte.d.ts +9 -0
- package/build/svelte/index.d.ts +2 -0
- package/build/svelte/index.js +3 -0
- package/build/vue/gl-view.d.ts +2 -0
- package/build/vue/gl-view.js +4 -0
- package/build/vue/index.d.ts +2 -0
- package/build/vue/index.js +3 -0
- package/build-ngc/angular/gl-view/index.d.ts +21 -0
- package/build-ngc/angular/gl-view/index.js +46 -0
- package/build-ngc/angular/gl-view/index.js.map +1 -0
- package/build-ngc/angular/index.d.ts +2 -0
- package/build-ngc/angular/index.js +4 -0
- package/build-ngc/angular/index.js.map +1 -0
- package/build-ngc/core/constants.d.ts +4 -0
- package/build-ngc/core/constants.js +5 -0
- package/build-ngc/core/constants.js.map +1 -0
- package/build-ngc/core/gl-context.d.ts +13 -0
- package/build-ngc/core/gl-context.js +55 -0
- package/build-ngc/core/gl-context.js.map +1 -0
- package/build-ngc/core/gl-errors.d.ts +1 -0
- package/build-ngc/core/gl-errors.js +9 -0
- package/build-ngc/core/gl-errors.js.map +1 -0
- package/build-ngc/core/gl-utils.d.ts +5 -0
- package/build-ngc/core/gl-utils.js +85 -0
- package/build-ngc/core/gl-utils.js.map +1 -0
- package/build-ngc/core/gl-view.d.ts +16 -0
- package/build-ngc/core/gl-view.js +100 -0
- package/build-ngc/core/gl-view.js.map +1 -0
- package/build-ngc/core/index.d.ts +6 -0
- package/build-ngc/core/index.js +6 -0
- package/build-ngc/core/index.js.map +1 -0
- package/build-ngc/core/native-module.d.ts +15 -0
- package/build-ngc/core/native-module.js +4 -0
- package/build-ngc/core/native-module.js.map +1 -0
- package/build-ngc/core/types.d.ts +75 -0
- package/build-ngc/core/types.js +14 -0
- package/build-ngc/core/types.js.map +1 -0
- package/build-ngc/core/worklet-context-manager.d.ts +6 -0
- package/build-ngc/core/worklet-context-manager.js +35 -0
- package/build-ngc/core/worklet-context-manager.js.map +1 -0
- package/native-link.json +12 -0
- package/package.json +145 -3
- package/src/angular/gl-view/index.ts +50 -0
- package/src/angular/index.ts +4 -0
- package/src/core/constants.ts +6 -0
- package/src/core/gl-context.ts +74 -0
- package/src/core/gl-errors.ts +8 -0
- package/src/core/gl-utils.ts +97 -0
- package/src/core/gl-view.ts +143 -0
- package/src/core/index.ts +21 -0
- package/src/core/native-module.ts +18 -0
- package/src/core/types.ts +95 -0
- package/src/core/worklet-context-manager.ts +42 -0
- package/src/react/gl-view.ts +14 -0
- package/src/react/index.ts +5 -0
- package/src/solid/gl-view.ts +6 -0
- package/src/solid/index.ts +4 -0
- package/src/svelte/gl-view.svelte +19 -0
- package/src/svelte/index.ts +4 -0
- package/src/svelte/svelte-ambient.d.ts +3 -0
- package/src/svelte/svelte-compile.test-helper.ts +6 -0
- package/src/vue/gl-view.ts +5 -0
- package/src/vue/index.ts +4 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 A. Prokopenko
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,39 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @symbiote-native/gl
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[`expo-gl`](https://docs.expo.dev/versions/latest/sdk/gl-view/) for **every**
|
|
4
|
+
[SymbioteNative](../../README.md) adapter: React, Vue, Svelte, Solid and Angular. A `GLView` that
|
|
5
|
+
gives a WebGL2 context to draw into, headless contexts and snapshots.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npx @symbiote-native/cli new my-app --gl # new app
|
|
11
|
+
npx @symbiote-native/cli add --gl # existing app
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Manual: `npm install @symbiote-native/gl`, then wire `expo-modules-autolinking` once per app.
|
|
15
|
+
Never install `expo-gl` or the `expo` meta-package yourself.
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { GLView, type IExpoWebGLRenderingContext } from '@symbiote-native/gl/react';
|
|
21
|
+
|
|
22
|
+
function onContextCreate(gl: IExpoWebGLRenderingContext) {
|
|
23
|
+
gl.clearColor(0, 1, 1, 1);
|
|
24
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
25
|
+
gl.endFrameEXP();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
<GLView style={{ flex: 1 }} onContextCreate={onContextCreate} />;
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The statics of upstream's `GLView` class are plain functions: `createContextAsync`,
|
|
32
|
+
`destroyContextAsync`, `takeSnapshotAsync`, `getWorkletContext`. The handle of a view has
|
|
33
|
+
`exglCtxId`, `createCameraTextureAsync`, `destroyObjectAsync` and `takeSnapshotAsync`, reached by a
|
|
34
|
+
`ref` in React and Solid (`ref={fn}`), a template ref in Vue, `bind:this` in Svelte and a
|
|
35
|
+
`@ViewChild` in Angular. `gl.__expoSetLogging` prints with `console.warn`.
|
|
36
|
+
|
|
37
|
+
## Not ported
|
|
38
|
+
|
|
39
|
+
The web build, the hidden `nativeRef_EXPERIMENTAL` prop and upstream's one test (skipped there).
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { NativeViewBase } from '@symbiote-native/angular';
|
|
2
|
+
import type { IDescriptor } from '@symbiote-native/components';
|
|
3
|
+
import type { IGLViewProps } from '../../core';
|
|
4
|
+
declare const GL_VIEW_INPUTS: readonly ["onContextCreate", "msaaSamples", "enableExperimentalWorkletSupport"];
|
|
5
|
+
export interface GLView extends Partial<Pick<IGLViewProps, (typeof GL_VIEW_INPUTS)[number]>> {
|
|
6
|
+
}
|
|
7
|
+
/** Angular twin of `expo-gl`'s `GLView` */
|
|
8
|
+
export declare class GLView extends NativeViewBase {
|
|
9
|
+
private readonly view;
|
|
10
|
+
readonly createCameraTextureAsync: (camera: import("..").IGLNodeOrTag) => Promise<WebGLTexture>;
|
|
11
|
+
readonly destroyObjectAsync: (glObject: import("..").IGLObject) => Promise<boolean>;
|
|
12
|
+
readonly takeSnapshotAsync: (options?: import("..").IGLSnapshotOptions) => Promise<import("..").IGLSnapshot>;
|
|
13
|
+
get exglCtxId(): number | undefined;
|
|
14
|
+
protected readonly propNames: readonly ["onContextCreate", "msaaSamples", "enableExperimentalWorkletSupport"];
|
|
15
|
+
protected renderView(props: object): IDescriptor | null;
|
|
16
|
+
protected disposeView(): void;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
2
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
3
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
4
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
5
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
6
|
+
var _, done = false;
|
|
7
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
8
|
+
var context = {};
|
|
9
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
10
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
11
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
12
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
13
|
+
if (kind === "accessor") {
|
|
14
|
+
if (result === void 0) continue;
|
|
15
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
16
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
17
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
18
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
19
|
+
}
|
|
20
|
+
else if (_ = accept(result)) {
|
|
21
|
+
if (kind === "field") initializers.unshift(_);
|
|
22
|
+
else descriptor[key] = _;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
26
|
+
done = true;
|
|
27
|
+
};
|
|
28
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
29
|
+
var useValue = arguments.length > 2;
|
|
30
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
31
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
32
|
+
}
|
|
33
|
+
return useValue ? value : void 0;
|
|
34
|
+
};
|
|
35
|
+
import { ChangeDetectionStrategy, Component } from '@angular/core';
|
|
36
|
+
import { DescriptorOutlet, NativeViewBase } from '@symbiote-native/angular';
|
|
37
|
+
import { createGLView } from '../../core/index.js';
|
|
38
|
+
// One list is the inputs of the component and the props it hands to the native view
|
|
39
|
+
const GL_VIEW_INPUTS = [
|
|
40
|
+
'onContextCreate',
|
|
41
|
+
'msaaSamples',
|
|
42
|
+
'enableExperimentalWorkletSupport',
|
|
43
|
+
];
|
|
44
|
+
/** Angular twin of `expo-gl`'s `GLView` */
|
|
45
|
+
let GLView = (() => {
|
|
46
|
+
let _classDecorators = [Component({
|
|
47
|
+
selector: 'GLView',
|
|
48
|
+
standalone: true,
|
|
49
|
+
imports: [DescriptorOutlet],
|
|
50
|
+
inputs: [...GL_VIEW_INPUTS],
|
|
51
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
52
|
+
template: `@if (descriptor; as node) {
|
|
53
|
+
<symbiote-descriptor-outlet [node]="node" />
|
|
54
|
+
}`,
|
|
55
|
+
})];
|
|
56
|
+
let _classDescriptor;
|
|
57
|
+
let _classExtraInitializers = [];
|
|
58
|
+
let _classThis;
|
|
59
|
+
let _classSuper = NativeViewBase;
|
|
60
|
+
var GLView = class extends _classSuper {
|
|
61
|
+
static { _classThis = this; }
|
|
62
|
+
static {
|
|
63
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
64
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
65
|
+
GLView = _classThis = _classDescriptor.value;
|
|
66
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
67
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
68
|
+
}
|
|
69
|
+
view = createGLView(() => this.hostNode());
|
|
70
|
+
// The functions of the view are members of the component, a `@ViewChild` reaches them
|
|
71
|
+
createCameraTextureAsync = this.view.handle.createCameraTextureAsync;
|
|
72
|
+
destroyObjectAsync = this.view.handle.destroyObjectAsync;
|
|
73
|
+
takeSnapshotAsync = this.view.handle.takeSnapshotAsync;
|
|
74
|
+
get exglCtxId() {
|
|
75
|
+
return this.view.handle.exglCtxId;
|
|
76
|
+
}
|
|
77
|
+
propNames = GL_VIEW_INPUTS;
|
|
78
|
+
renderView(props) {
|
|
79
|
+
return this.view.render(props);
|
|
80
|
+
}
|
|
81
|
+
disposeView() {
|
|
82
|
+
this.view.dispose();
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
return GLView = _classThis;
|
|
86
|
+
})();
|
|
87
|
+
export { GLView };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { IExpoWebGLRenderingContext, IGLSnapshot, IGLSnapshotOptions } from './types';
|
|
2
|
+
/** The context of a worklet, `exgl` has to be created with worklet support */
|
|
3
|
+
export declare function getWorkletContext(contextId: number): IExpoWebGLRenderingContext | undefined;
|
|
4
|
+
export declare function unregisterGLContext(exglCtxId: number): void;
|
|
5
|
+
/** The GL interface of a context id the native side created */
|
|
6
|
+
export declare function getGl(exglCtxId: number): IExpoWebGLRenderingContext;
|
|
7
|
+
export declare function getContextId(exgl?: IExpoWebGLRenderingContext | number): number;
|
|
8
|
+
/** A context with no view, which does not present its framebuffer, take a snapshot to see it */
|
|
9
|
+
export declare function createContextAsync(): Promise<IExpoWebGLRenderingContext>;
|
|
10
|
+
/** For a headless context made by `createContextAsync` */
|
|
11
|
+
export declare function destroyContextAsync(exgl?: IExpoWebGLRenderingContext | number): Promise<boolean>;
|
|
12
|
+
/** Saves the framebuffer as a file in the cache directory of the app */
|
|
13
|
+
export declare function takeSnapshotAsync(exgl?: IExpoWebGLRenderingContext | number, options?: IGLSnapshotOptions): Promise<IGLSnapshot>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { CodedError } from 'expo-modules-core';
|
|
2
|
+
import { GL_CONTEXTS_GLOBAL } from './constants.js';
|
|
3
|
+
import { configureLogging } from './gl-utils.js';
|
|
4
|
+
import { glNativeModule } from './native-module.js';
|
|
5
|
+
import { createWorkletContextManager } from './worklet-context-manager.js';
|
|
6
|
+
const workletContextManager = createWorkletContextManager();
|
|
7
|
+
function isContext(value) {
|
|
8
|
+
return typeof value === 'object' && value !== null;
|
|
9
|
+
}
|
|
10
|
+
/** The context of a worklet, `exgl` has to be created with worklet support */
|
|
11
|
+
export function getWorkletContext(contextId) {
|
|
12
|
+
'worklet';
|
|
13
|
+
return workletContextManager.getContext(contextId);
|
|
14
|
+
}
|
|
15
|
+
export function unregisterGLContext(exglCtxId) {
|
|
16
|
+
const contexts = Reflect.get(globalThis, GL_CONTEXTS_GLOBAL);
|
|
17
|
+
if (contexts)
|
|
18
|
+
Reflect.deleteProperty(Object(contexts), String(exglCtxId));
|
|
19
|
+
workletContextManager.unregister?.(exglCtxId);
|
|
20
|
+
}
|
|
21
|
+
/** The GL interface of a context id the native side created */
|
|
22
|
+
export function getGl(exglCtxId) {
|
|
23
|
+
const contexts = Reflect.get(globalThis, GL_CONTEXTS_GLOBAL);
|
|
24
|
+
if (!contexts) {
|
|
25
|
+
throw new CodedError('ERR_GL_NOT_AVAILABLE', 'GL is currently not available. (Have you enabled remote debugging? GL is not available while debugging remotely.)');
|
|
26
|
+
}
|
|
27
|
+
const gl = Reflect.get(Object(contexts), String(exglCtxId));
|
|
28
|
+
if (!isContext(gl))
|
|
29
|
+
throw new Error(`There is no EXGLContext with id ${exglCtxId}`);
|
|
30
|
+
configureLogging(gl);
|
|
31
|
+
return gl;
|
|
32
|
+
}
|
|
33
|
+
export function getContextId(exgl) {
|
|
34
|
+
const exglCtxId = exgl && typeof exgl === 'object' ? exgl.contextId : exgl;
|
|
35
|
+
if (!exglCtxId || typeof exglCtxId !== 'number') {
|
|
36
|
+
throw new Error(`Invalid EXGLContext id: ${String(exglCtxId)}`);
|
|
37
|
+
}
|
|
38
|
+
return exglCtxId;
|
|
39
|
+
}
|
|
40
|
+
/** A context with no view, which does not present its framebuffer, take a snapshot to see it */
|
|
41
|
+
export async function createContextAsync() {
|
|
42
|
+
const { exglCtxId } = await glNativeModule.createContextAsync();
|
|
43
|
+
return getGl(exglCtxId);
|
|
44
|
+
}
|
|
45
|
+
/** For a headless context made by `createContextAsync` */
|
|
46
|
+
export async function destroyContextAsync(exgl) {
|
|
47
|
+
const exglCtxId = getContextId(exgl);
|
|
48
|
+
unregisterGLContext(exglCtxId);
|
|
49
|
+
return glNativeModule.destroyContextAsync(exglCtxId);
|
|
50
|
+
}
|
|
51
|
+
/** Saves the framebuffer as a file in the cache directory of the app */
|
|
52
|
+
export async function takeSnapshotAsync(exgl, options = {}) {
|
|
53
|
+
return glNativeModule.takeSnapshotAsync(getContextId(exgl), options);
|
|
54
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const GL_ERRORS: Readonly<Record<number, string>>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const GL_ERRORS = {
|
|
2
|
+
1280: 'INVALID ENUM: An unacceptable value has been specified for an enumerated argument.',
|
|
3
|
+
1281: 'INVALID_VALUE: A numeric argument is out of range.',
|
|
4
|
+
1282: 'INVALID_OPERATION: The specified command is not allowed for the current state.',
|
|
5
|
+
1285: 'OUT_OF_MEMORY: Not enough memory is left to execute the command.',
|
|
6
|
+
1286: 'INVALID_FRAMEBUFFER_OPERATION: The currently bound framebuffer is not complete when trying to render to or to read from it.',
|
|
7
|
+
37442: 'CONTEXT_LOST_WEBGL: The WebGL context is lost.',
|
|
8
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { IExpoWebGLRenderingContext } from './types';
|
|
2
|
+
type ILoggableGL = Pick<IExpoWebGLRenderingContext, '__expoSetLogging'>;
|
|
3
|
+
/** Adds `__expoSetLogging` to the context, for the logging options useful in debugging GL calls */
|
|
4
|
+
export declare function configureLogging(gl: ILoggableGL): void;
|
|
5
|
+
export {};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { GL_ERRORS } from './gl-errors.js';
|
|
2
|
+
import { GLLoggingOption } from './types.js';
|
|
3
|
+
const MAX_STRING_LENGTH = 20;
|
|
4
|
+
const SET_LOGGING_KEY = '__expoSetLogging';
|
|
5
|
+
const wrappers = new WeakSet();
|
|
6
|
+
function isGLFunction(value) {
|
|
7
|
+
return typeof value === 'function';
|
|
8
|
+
}
|
|
9
|
+
function constantNameOf(gl, value) {
|
|
10
|
+
for (const name in gl) {
|
|
11
|
+
if (Reflect.get(gl, name) === value)
|
|
12
|
+
return name;
|
|
13
|
+
}
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
// Numbers get the name of a constant that holds them, which can mislead for a size, so the
|
|
17
|
+
// number stays. Strings are cut because a shader is long and logging it blocks the bridge
|
|
18
|
+
function describeArg(gl, option, arg) {
|
|
19
|
+
if (option & GLLoggingOption.RESOLVE_CONSTANTS && typeof arg === 'number') {
|
|
20
|
+
const name = constantNameOf(gl, arg);
|
|
21
|
+
if (name)
|
|
22
|
+
return `${arg} (${name})`;
|
|
23
|
+
}
|
|
24
|
+
const isLong = typeof arg === 'string' && arg.length > MAX_STRING_LENGTH;
|
|
25
|
+
if (option & GLLoggingOption.TRUNCATE_STRINGS && isLong) {
|
|
26
|
+
const lastSpace = arg.lastIndexOf(' ', MAX_STRING_LENGTH);
|
|
27
|
+
return `${arg.slice(0, lastSpace >= 0 ? lastSpace : MAX_STRING_LENGTH)}...`;
|
|
28
|
+
}
|
|
29
|
+
return String(arg);
|
|
30
|
+
}
|
|
31
|
+
// The original `getError`, a wrapped one would log itself
|
|
32
|
+
function reportError(gl) {
|
|
33
|
+
const getError = Reflect.get(Reflect.getPrototypeOf(gl) ?? {}, 'getError');
|
|
34
|
+
if (!isGLFunction(getError))
|
|
35
|
+
return;
|
|
36
|
+
const error = getError.call(gl);
|
|
37
|
+
if (typeof error !== 'number' || error === Reflect.get(gl, 'NO_ERROR'))
|
|
38
|
+
return;
|
|
39
|
+
console.warn(`\x1b[31mExpoGL: Error ${GL_ERRORS[error]}\x1b[0m`);
|
|
40
|
+
}
|
|
41
|
+
function wrapMethod(gl, key, original, getOption) {
|
|
42
|
+
const wrapper = (...args) => {
|
|
43
|
+
const option = getOption();
|
|
44
|
+
if (option & GLLoggingOption.METHOD_CALLS) {
|
|
45
|
+
const params = args.map(arg => describeArg(gl, option, arg));
|
|
46
|
+
console.warn(`ExpoGL: ${key}(${params.join(', ')})`);
|
|
47
|
+
}
|
|
48
|
+
const result = original.apply(gl, args);
|
|
49
|
+
if (option & GLLoggingOption.METHOD_CALLS)
|
|
50
|
+
console.warn(`ExpoGL: = ${String(result)}`);
|
|
51
|
+
if (option & GLLoggingOption.GET_ERRORS && key !== 'getError')
|
|
52
|
+
reportError(gl);
|
|
53
|
+
return result;
|
|
54
|
+
};
|
|
55
|
+
wrappers.add(wrapper);
|
|
56
|
+
Reflect.set(gl, key, wrapper);
|
|
57
|
+
}
|
|
58
|
+
function wrapMethods(gl, getOption) {
|
|
59
|
+
for (const [key, original] of Object.entries(Reflect.getPrototypeOf(gl) ?? {})) {
|
|
60
|
+
if (isGLFunction(original) && key !== SET_LOGGING_KEY) {
|
|
61
|
+
wrapMethod(gl, key, original, getOption);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function unwrapMethods(gl) {
|
|
66
|
+
for (const [key, value] of Object.entries(gl)) {
|
|
67
|
+
if (isGLFunction(value) && wrappers.has(value))
|
|
68
|
+
Reflect.deleteProperty(gl, key);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** Adds `__expoSetLogging` to the context, for the logging options useful in debugging GL calls */
|
|
72
|
+
export function configureLogging(gl) {
|
|
73
|
+
let loggingOption = GLLoggingOption.DISABLED;
|
|
74
|
+
gl.__expoSetLogging = (option) => {
|
|
75
|
+
// Both on or both off needs no wrapping or unwrapping, only the new option
|
|
76
|
+
if (!loggingOption !== !option) {
|
|
77
|
+
if (option)
|
|
78
|
+
wrapMethods(gl, () => loggingOption);
|
|
79
|
+
else
|
|
80
|
+
unwrapMethods(gl);
|
|
81
|
+
}
|
|
82
|
+
loggingOption = option;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { IDescriptor } from '@symbiote-native/components';
|
|
2
|
+
import type { ISymbioteNode } from '@symbiote-native/engine';
|
|
3
|
+
import type { IGLViewHandle } from './types';
|
|
4
|
+
export declare const glViewName: () => string;
|
|
5
|
+
export declare const ensureGLViewRegistered: () => boolean;
|
|
6
|
+
export type IGLView = {
|
|
7
|
+
handle: IGLViewHandle;
|
|
8
|
+
/** `null` means the view cannot register and the caller renders nothing */
|
|
9
|
+
render(props: object): IDescriptor | null;
|
|
10
|
+
/** Forgets the context of the surface, called when the view unmounts */
|
|
11
|
+
dispose(): void;
|
|
12
|
+
};
|
|
13
|
+
type IGetNode = () => ISymbioteNode | null | undefined;
|
|
14
|
+
/** One per mounted view, `getNode` is kept for the shape the adapters share */
|
|
15
|
+
export declare function createGLView(_getNode: IGetNode): IGLView;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { Platform, UnavailabilityError, requireNativeViewManager, } from 'expo-modules-core';
|
|
2
|
+
import { el } from '@symbiote-native/components';
|
|
3
|
+
import { defineExpoNativeView, getNativeTag, isDevBuild, isSymbioteNode, } from '@symbiote-native/engine';
|
|
4
|
+
import { GL_MODULE_NAME, GL_PACKAGE_NAME } from './constants.js';
|
|
5
|
+
import { getGl, takeSnapshotAsync, unregisterGLContext } from './gl-context.js';
|
|
6
|
+
import { glNativeModule } from './native-module.js';
|
|
7
|
+
// Registered on render: a barrel side effect is lost in a release build
|
|
8
|
+
const glView = defineExpoNativeView(requireNativeViewManager, GL_MODULE_NAME);
|
|
9
|
+
export const glViewName = glView.name;
|
|
10
|
+
export const ensureGLViewRegistered = glView.ensureRegistered;
|
|
11
|
+
const DEFAULT_MSAA_SAMPLES = 4;
|
|
12
|
+
function isSurfaceCreated(value) {
|
|
13
|
+
return typeof Reflect.get(Object(value), 'exglCtxId') === 'number';
|
|
14
|
+
}
|
|
15
|
+
function isContextCallback(value) {
|
|
16
|
+
return typeof value === 'function';
|
|
17
|
+
}
|
|
18
|
+
// Adapters hand over the props they hold, a missing callback is a view nobody listens to
|
|
19
|
+
function toViewProps(props) {
|
|
20
|
+
const onContextCreate = Reflect.get(props, 'onContextCreate');
|
|
21
|
+
return {
|
|
22
|
+
...props,
|
|
23
|
+
onContextCreate: isContextCallback(onContextCreate) ? onContextCreate : () => undefined,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function nativeTagOf(camera) {
|
|
27
|
+
const tag = isSymbioteNode(camera) ? getNativeTag(camera) : camera;
|
|
28
|
+
if (typeof tag !== 'number')
|
|
29
|
+
throw new Error('The camera view has no native tag yet');
|
|
30
|
+
return tag;
|
|
31
|
+
}
|
|
32
|
+
/** One per mounted view, `getNode` is kept for the shape the adapters share */
|
|
33
|
+
export function createGLView(_getNode) {
|
|
34
|
+
let exglCtxId;
|
|
35
|
+
let latestProps;
|
|
36
|
+
let hasWorkletSupport;
|
|
37
|
+
const onSurfaceCreate = (event) => {
|
|
38
|
+
if (!isSurfaceCreated(event.nativeEvent))
|
|
39
|
+
return;
|
|
40
|
+
exglCtxId = event.nativeEvent.exglCtxId;
|
|
41
|
+
latestProps?.onContextCreate(getGl(exglCtxId));
|
|
42
|
+
};
|
|
43
|
+
function warnOnWorkletChange(enabled) {
|
|
44
|
+
if (hasWorkletSupport !== undefined && hasWorkletSupport !== enabled) {
|
|
45
|
+
console.warn('Updating prop enableExperimentalWorkletSupport is not supported');
|
|
46
|
+
}
|
|
47
|
+
hasWorkletSupport = enabled;
|
|
48
|
+
}
|
|
49
|
+
const handle = {
|
|
50
|
+
get exglCtxId() {
|
|
51
|
+
return exglCtxId;
|
|
52
|
+
},
|
|
53
|
+
async createCameraTextureAsync(camera) {
|
|
54
|
+
if (!glNativeModule.createCameraTextureAsync) {
|
|
55
|
+
throw new UnavailabilityError(GL_PACKAGE_NAME, 'createCameraTextureAsync');
|
|
56
|
+
}
|
|
57
|
+
if (!exglCtxId)
|
|
58
|
+
throw new Error("GLView's surface is not created yet!");
|
|
59
|
+
const { exglObjId } = await glNativeModule.createCameraTextureAsync(exglCtxId, nativeTagOf(camera));
|
|
60
|
+
const texture = { id: exglObjId };
|
|
61
|
+
return texture;
|
|
62
|
+
},
|
|
63
|
+
async destroyObjectAsync(glObject) {
|
|
64
|
+
if (!glNativeModule.destroyObjectAsync) {
|
|
65
|
+
throw new UnavailabilityError(GL_PACKAGE_NAME, 'destroyObjectAsync');
|
|
66
|
+
}
|
|
67
|
+
return glNativeModule.destroyObjectAsync(glObject.id);
|
|
68
|
+
},
|
|
69
|
+
takeSnapshotAsync: (options) => takeSnapshotAsync(exglCtxId, options),
|
|
70
|
+
};
|
|
71
|
+
return {
|
|
72
|
+
handle,
|
|
73
|
+
dispose: () => {
|
|
74
|
+
if (exglCtxId)
|
|
75
|
+
unregisterGLContext(exglCtxId);
|
|
76
|
+
},
|
|
77
|
+
render: props => {
|
|
78
|
+
if (!ensureGLViewRegistered()) {
|
|
79
|
+
if (isDevBuild())
|
|
80
|
+
console.warn("'GLView' is not available.");
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
latestProps = toViewProps(props);
|
|
84
|
+
const { onContextCreate, msaaSamples = DEFAULT_MSAA_SAMPLES, enableExperimentalWorkletSupport = false, ...viewProps } = latestProps;
|
|
85
|
+
warnOnWorkletChange(enableExperimentalWorkletSupport);
|
|
86
|
+
return el('view', viewProps, [
|
|
87
|
+
el(glViewName(), {
|
|
88
|
+
style: {
|
|
89
|
+
flex: 1,
|
|
90
|
+
...Platform.select({ ios: { backgroundColor: 'transparent' }, default: {} }),
|
|
91
|
+
},
|
|
92
|
+
onSurfaceCreate,
|
|
93
|
+
enableExperimentalWorkletSupport,
|
|
94
|
+
msaaSamples: Platform.select({ ios: msaaSamples, default: undefined }),
|
|
95
|
+
}),
|
|
96
|
+
]);
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { GL_MODULE_NAME } from './constants';
|
|
2
|
+
export { createContextAsync, destroyContextAsync, getGl, getWorkletContext, takeSnapshotAsync, } from './gl-context';
|
|
3
|
+
export { configureLogging } from './gl-utils';
|
|
4
|
+
export { createGLView, ensureGLViewRegistered, glViewName, type IGLView } from './gl-view';
|
|
5
|
+
export { GLLoggingOption } from './types';
|
|
6
|
+
export type { IExpoWebGLRenderingContext, IGLNodeOrTag, IGLObject, IGLSnapshot, IGLSnapshotOptions, IGLSurfaceCreateEvent, IGLViewHandle, IGLViewProps, } from './types';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { GL_MODULE_NAME } from './constants.js';
|
|
2
|
+
export { createContextAsync, destroyContextAsync, getGl, getWorkletContext, takeSnapshotAsync, } from './gl-context.js';
|
|
3
|
+
export { configureLogging } from './gl-utils.js';
|
|
4
|
+
export { createGLView, ensureGLViewRegistered, glViewName } from './gl-view.js';
|
|
5
|
+
export { GLLoggingOption } from './types.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { IGLSnapshot, IGLSnapshotOptions } from './types';
|
|
2
|
+
export type INativeGLModule = {
|
|
3
|
+
createContextAsync(): Promise<{
|
|
4
|
+
exglCtxId: number;
|
|
5
|
+
}>;
|
|
6
|
+
destroyContextAsync(exglCtxId: number): Promise<boolean>;
|
|
7
|
+
takeSnapshotAsync(exglCtxId: number, options: IGLSnapshotOptions): Promise<IGLSnapshot>;
|
|
8
|
+
/** Absent where there is no camera texture support */
|
|
9
|
+
createCameraTextureAsync?(exglCtxId: number, cameraTag: number): Promise<{
|
|
10
|
+
exglObjId: number;
|
|
11
|
+
}>;
|
|
12
|
+
/** Absent where objects cannot be destroyed one by one */
|
|
13
|
+
destroyObjectAsync?(exglObjId: number): Promise<boolean>;
|
|
14
|
+
};
|
|
15
|
+
export declare const glNativeModule: INativeGLModule;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { IAccessibilityProps, IAriaProps, IResponderProps } from '@symbiote-native/components';
|
|
2
|
+
import type { IStyleProp, ISymbioteEvent, ISymbioteNode, IViewStyle } from '@symbiote-native/engine';
|
|
3
|
+
export type IGLObject = {
|
|
4
|
+
id: number;
|
|
5
|
+
};
|
|
6
|
+
export type IGLSurfaceCreateEvent = {
|
|
7
|
+
nativeEvent: {
|
|
8
|
+
exglCtxId: number;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export type IGLSnapshotOptions = {
|
|
12
|
+
/** @default false */
|
|
13
|
+
flip?: boolean;
|
|
14
|
+
/** Defaults to the framebuffer shown in the view, or the current one of a headless context */
|
|
15
|
+
framebuffer?: WebGLFramebuffer;
|
|
16
|
+
/** Passed to `glReadPixels` */
|
|
17
|
+
rect?: {
|
|
18
|
+
x: number;
|
|
19
|
+
y: number;
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
};
|
|
23
|
+
/** PNG is lossless but slower, iOS writes a PNG instead of WebP. @default 'jpeg' */
|
|
24
|
+
format?: 'jpeg' | 'png' | 'webp';
|
|
25
|
+
/** From `0` (most compressed) to `1.0` (none). @default 1.0 */
|
|
26
|
+
compress?: number;
|
|
27
|
+
};
|
|
28
|
+
export type IGLSnapshot = {
|
|
29
|
+
uri: string | Blob | null;
|
|
30
|
+
/** Same as `uri`, which makes a snapshot usable in `texImage2D` */
|
|
31
|
+
localUri: string;
|
|
32
|
+
width: number;
|
|
33
|
+
height: number;
|
|
34
|
+
};
|
|
35
|
+
export type IExpoWebGLRenderingContext = WebGL2RenderingContext & {
|
|
36
|
+
contextId: number;
|
|
37
|
+
endFrameEXP(): void;
|
|
38
|
+
flushEXP(): void;
|
|
39
|
+
__expoSetLogging(option: GLLoggingOption): void;
|
|
40
|
+
};
|
|
41
|
+
/** A native tag, or the host node of a view such as a camera */
|
|
42
|
+
export type IGLNodeOrTag = number | ISymbioteNode | null;
|
|
43
|
+
export type IGLViewProps = IAccessibilityProps & IAriaProps & IResponderProps & {
|
|
44
|
+
style?: IStyleProp<IViewStyle>;
|
|
45
|
+
testID?: string;
|
|
46
|
+
nativeID?: string;
|
|
47
|
+
onLayout?: (event: ISymbioteEvent) => void;
|
|
48
|
+
/** Called with the context once the surface exists */
|
|
49
|
+
onContextCreate(gl: IExpoWebGLRenderingContext): void;
|
|
50
|
+
/** Multisampling, `0` turns it off. @default 4 @platform ios */
|
|
51
|
+
msaaSamples?: number;
|
|
52
|
+
/** Lets the Reanimated worklet thread use the `gl` object. @default false */
|
|
53
|
+
enableExperimentalWorkletSupport?: boolean;
|
|
54
|
+
};
|
|
55
|
+
export declare enum GLLoggingOption {
|
|
56
|
+
DISABLED = 0,
|
|
57
|
+
/** Logs calls, their parameters and results */
|
|
58
|
+
METHOD_CALLS = 1,
|
|
59
|
+
/** Calls `getError` after each call, which blocks and slows drawing down */
|
|
60
|
+
GET_ERRORS = 2,
|
|
61
|
+
/** Prints the name of the constant a numeric parameter holds */
|
|
62
|
+
RESOLVE_CONSTANTS = 4,
|
|
63
|
+
/** Cuts long strings such as shaders */
|
|
64
|
+
TRUNCATE_STRINGS = 8,
|
|
65
|
+
ALL = 15
|
|
66
|
+
}
|
|
67
|
+
/** What a ref to the view hands out */
|
|
68
|
+
export type IGLViewHandle = {
|
|
69
|
+
/** The id of the context of the view, `undefined` until the surface exists */
|
|
70
|
+
readonly exglCtxId: number | undefined;
|
|
71
|
+
createCameraTextureAsync(camera: IGLNodeOrTag): Promise<WebGLTexture>;
|
|
72
|
+
destroyObjectAsync(glObject: IGLObject): Promise<boolean>;
|
|
73
|
+
/** A snapshot of the context of this view */
|
|
74
|
+
takeSnapshotAsync(options?: IGLSnapshotOptions): Promise<IGLSnapshot>;
|
|
75
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export var GLLoggingOption;
|
|
2
|
+
(function (GLLoggingOption) {
|
|
3
|
+
GLLoggingOption[GLLoggingOption["DISABLED"] = 0] = "DISABLED";
|
|
4
|
+
/** Logs calls, their parameters and results */
|
|
5
|
+
GLLoggingOption[GLLoggingOption["METHOD_CALLS"] = 1] = "METHOD_CALLS";
|
|
6
|
+
/** Calls `getError` after each call, which blocks and slows drawing down */
|
|
7
|
+
GLLoggingOption[GLLoggingOption["GET_ERRORS"] = 2] = "GET_ERRORS";
|
|
8
|
+
/** Prints the name of the constant a numeric parameter holds */
|
|
9
|
+
GLLoggingOption[GLLoggingOption["RESOLVE_CONSTANTS"] = 4] = "RESOLVE_CONSTANTS";
|
|
10
|
+
/** Cuts long strings such as shaders */
|
|
11
|
+
GLLoggingOption[GLLoggingOption["TRUNCATE_STRINGS"] = 8] = "TRUNCATE_STRINGS";
|
|
12
|
+
GLLoggingOption[GLLoggingOption["ALL"] = 15] = "ALL";
|
|
13
|
+
})(GLLoggingOption || (GLLoggingOption = {}));
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { IExpoWebGLRenderingContext } from './types';
|
|
2
|
+
export type IWorkletContextManager = {
|
|
3
|
+
getContext(contextId: number): IExpoWebGLRenderingContext | undefined;
|
|
4
|
+
unregister?(contextId: number): void;
|
|
5
|
+
};
|
|
6
|
+
export declare function createWorkletContextManager(): IWorkletContextManager;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { GL_CONTEXTS_GLOBAL } from './constants.js';
|
|
2
|
+
function isRunOnUI(value) {
|
|
3
|
+
return typeof value === 'function';
|
|
4
|
+
}
|
|
5
|
+
// Reanimated stays optional, and an import of it in a try block in the module that also holds the
|
|
6
|
+
// view breaks with `inlineRequires`, so the manager lives in its own file
|
|
7
|
+
export function createWorkletContextManager() {
|
|
8
|
+
try {
|
|
9
|
+
// Reanimated has to load before any workletized code is created
|
|
10
|
+
const reanimated = require('react-native-reanimated');
|
|
11
|
+
const runOnUI = Reflect.get(Object(reanimated), 'runOnUI');
|
|
12
|
+
if (!isRunOnUI(runOnUI))
|
|
13
|
+
throw new Error('react-native-reanimated has no runOnUI');
|
|
14
|
+
return {
|
|
15
|
+
getContext: contextId => {
|
|
16
|
+
'worklet';
|
|
17
|
+
return Reflect.get(Object(Reflect.get(globalThis, GL_CONTEXTS_GLOBAL)), String(contextId));
|
|
18
|
+
},
|
|
19
|
+
unregister: contextId => {
|
|
20
|
+
runOnUI(id => {
|
|
21
|
+
'worklet';
|
|
22
|
+
Reflect.deleteProperty(Object(Reflect.get(globalThis, GL_CONTEXTS_GLOBAL)), String(id));
|
|
23
|
+
})(contextId);
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return {
|
|
29
|
+
getContext: () => {
|
|
30
|
+
throw new Error('Worklet runtime is not available');
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|