react-native-webgpu 0.5.15 → 0.5.17
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/README.md +6 -409
- package/apple/MetalLayerColorSpace.mm +44 -0
- package/cpp/rnwgpu/SurfaceRegistry.h +10 -0
- package/cpp/rnwgpu/api/Canvas.h +18 -0
- package/lib/commonjs/Canvas.js.map +1 -1
- package/lib/commonjs/GPUDeviceProvider.js +33 -0
- package/lib/commonjs/GPUDeviceProvider.js.map +1 -0
- package/lib/commonjs/WebPolyfillGPUModule.js +7 -1
- package/lib/commonjs/WebPolyfillGPUModule.js.map +1 -1
- package/lib/commonjs/constants.js +20 -5
- package/lib/commonjs/constants.js.map +1 -1
- package/lib/commonjs/formats.js +30 -0
- package/lib/commonjs/formats.js.map +1 -0
- package/lib/commonjs/hooks.js +2 -26
- package/lib/commonjs/hooks.js.map +1 -1
- package/lib/commonjs/main/index.js +24 -0
- package/lib/commonjs/main/index.js.map +1 -1
- package/lib/commonjs/main/index.web.js +22 -0
- package/lib/commonjs/main/index.web.js.map +1 -1
- package/lib/module/Canvas.js.map +1 -1
- package/lib/module/GPUDeviceProvider.js +24 -0
- package/lib/module/GPUDeviceProvider.js.map +1 -0
- package/lib/module/WebPolyfillGPUModule.js +7 -1
- package/lib/module/WebPolyfillGPUModule.js.map +1 -1
- package/lib/module/constants.js +15 -5
- package/lib/module/constants.js.map +1 -1
- package/lib/module/formats.js +24 -0
- package/lib/module/formats.js.map +1 -0
- package/lib/module/hooks.js +1 -22
- package/lib/module/hooks.js.map +1 -1
- package/lib/module/main/index.js +2 -0
- package/lib/module/main/index.js.map +1 -1
- package/lib/module/main/index.web.js +2 -0
- package/lib/module/main/index.web.js.map +1 -1
- package/lib/typescript/jest.config.d.ts +1 -0
- package/lib/typescript/lib/commonjs/GPUDeviceProvider.d.ts +8 -0
- package/lib/typescript/lib/commonjs/GPUDeviceProvider.d.ts.map +1 -0
- package/lib/typescript/lib/commonjs/constants.d.ts +5 -1
- package/lib/typescript/lib/commonjs/formats.d.ts +3 -0
- package/lib/typescript/lib/commonjs/formats.d.ts.map +1 -0
- package/lib/typescript/lib/commonjs/hooks.d.ts +4 -9
- package/lib/typescript/lib/commonjs/hooks.d.ts.map +1 -1
- package/lib/typescript/lib/module/GPUDeviceProvider.d.ts +8 -0
- package/lib/typescript/lib/module/GPUDeviceProvider.d.ts.map +1 -0
- package/lib/typescript/lib/module/constants.d.ts.map +1 -1
- package/lib/typescript/lib/module/formats.d.ts +2 -0
- package/lib/typescript/lib/module/formats.d.ts.map +1 -0
- package/lib/typescript/lib/module/hooks.d.ts +2 -9
- package/lib/typescript/lib/module/hooks.d.ts.map +1 -1
- package/lib/typescript/lib/module/main/index.d.ts +2 -0
- package/lib/typescript/lib/module/main/index.web.d.ts +2 -0
- package/lib/typescript/src/Canvas.d.ts +5 -0
- package/lib/typescript/src/Canvas.d.ts.map +1 -1
- package/lib/typescript/src/GPUDeviceProvider.d.ts +15 -0
- package/lib/typescript/src/GPUDeviceProvider.d.ts.map +1 -0
- package/lib/typescript/src/constants.d.ts.map +1 -1
- package/lib/typescript/src/formats.d.ts +2 -0
- package/lib/typescript/src/formats.d.ts.map +1 -0
- package/lib/typescript/src/hooks.d.ts +2 -17
- package/lib/typescript/src/hooks.d.ts.map +1 -1
- package/lib/typescript/src/main/index.d.ts +2 -0
- package/lib/typescript/src/main/index.d.ts.map +1 -1
- package/lib/typescript/src/main/index.web.d.ts +2 -0
- package/lib/typescript/src/main/index.web.d.ts.map +1 -1
- package/lib/typescript/src/types.d.ts +5 -0
- package/lib/typescript/src/types.d.ts.map +1 -1
- package/package.json +12 -3
- package/src/Canvas.tsx +7 -0
- package/src/GPUDeviceProvider.tsx +39 -0
- package/src/WebPolyfillGPUModule.ts +6 -0
- package/src/constants.ts +5 -5
- package/src/formats.ts +23 -0
- package/src/hooks.tsx +3 -41
- package/src/main/index.tsx +2 -0
- package/src/main/index.web.tsx +2 -0
- package/src/types.ts +7 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formats.d.ts","sourceRoot":"","sources":["../../../src/formats.ts"],"names":[],"mappings":"AAoBA,eAAO,MAAM,oCAAoC,QAAO,gBAEvD,CAAC"}
|
|
@@ -1,27 +1,12 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import type { ReactNode } from "react";
|
|
3
1
|
import type { CanvasRef, NativeCanvas } from "./Canvas";
|
|
4
2
|
export declare const warnIfNotHardwareAccelerated: (adapter: GPUAdapter) => void;
|
|
5
|
-
interface DeviceContext {
|
|
6
|
-
device: GPUDevice | null;
|
|
7
|
-
adapter: GPUAdapter | null;
|
|
8
|
-
}
|
|
9
|
-
declare const DeviceContext: React.Context<DeviceContext | null>;
|
|
10
|
-
interface DeviceProviderProps {
|
|
11
|
-
children?: ReactNode | ReactNode[];
|
|
12
|
-
adapterOptions?: GPURequestAdapterOptions;
|
|
13
|
-
deviceDescriptor?: GPUDeviceDescriptor;
|
|
14
|
-
}
|
|
15
|
-
export declare const GPUDeviceProvider: ({ children, adapterOptions, deviceDescriptor, }: DeviceProviderProps) => React.JSX.Element | null;
|
|
16
3
|
export declare const useSurface: () => {
|
|
17
|
-
ref:
|
|
4
|
+
ref: import("react").RefObject<CanvasRef>;
|
|
18
5
|
surface: NativeCanvas | null;
|
|
19
6
|
};
|
|
20
|
-
export declare const
|
|
21
|
-
export declare const useCanvasRef: () => React.RefObject<CanvasRef>;
|
|
7
|
+
export declare const useCanvasRef: () => import("react").RefObject<CanvasRef>;
|
|
22
8
|
export declare const useDevice: (adapterOptions?: GPURequestAdapterOptions, deviceDescriptor?: GPUDeviceDescriptor) => {
|
|
23
9
|
adapter: GPUAdapter | null;
|
|
24
10
|
device: GPUDevice | null;
|
|
25
11
|
};
|
|
26
|
-
export {};
|
|
27
12
|
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../../src/hooks.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../../src/hooks.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAExD,eAAO,MAAM,4BAA4B,GAAI,SAAS,UAAU,SAQ/D,CAAC;AAOF,eAAO,MAAM,UAAU;;;CAQtB,CAAC;AAEF,eAAO,MAAM,YAAY,4CAAgC,CAAC;AAE1D,eAAO,MAAM,SAAS,GACpB,iBAAiB,wBAAwB,EACzC,mBAAmB,mBAAmB;;;CAqBvC,CAAC"}
|
|
@@ -2,5 +2,7 @@ export * from "../Canvas";
|
|
|
2
2
|
export * from "../Offscreen";
|
|
3
3
|
export * from "../WebGPUViewNativeComponent";
|
|
4
4
|
export * from "../hooks";
|
|
5
|
+
export * from "../GPUDeviceProvider";
|
|
6
|
+
export * from "../formats";
|
|
5
7
|
export { default as WebGPUModule } from "../NativeWebGPUModule";
|
|
6
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/main/index.tsx"],"names":[],"mappings":"AAGA,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/main/index.tsx"],"names":[],"mappings":"AAGA,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,UAAU,CAAC;AACzB,cAAc,sBAAsB,CAAC;AACrC,cAAc,YAAY,CAAC;AAE3B,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,uBAAuB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.web.d.ts","sourceRoot":"","sources":["../../../../src/main/index.web.tsx"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AAEjC,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.web.d.ts","sourceRoot":"","sources":["../../../../src/main/index.web.tsx"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AAEjC,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,UAAU,CAAC;AACzB,cAAc,sBAAsB,CAAC;AACrC,cAAc,YAAY,CAAC"}
|
|
@@ -5,6 +5,11 @@ export interface NativeCanvas {
|
|
|
5
5
|
height: number;
|
|
6
6
|
clientWidth: number;
|
|
7
7
|
clientHeight: number;
|
|
8
|
+
addEventListener(type: string, listener: EventListener): void;
|
|
9
|
+
removeEventListener(type: string, listener: EventListener): void;
|
|
10
|
+
dispatchEvent(event: Event): void;
|
|
11
|
+
setPointerCapture(pointerId: number): void;
|
|
12
|
+
releasePointerCapture(pointerId: number): void;
|
|
8
13
|
}
|
|
9
14
|
export type RNCanvasContext = GPUCanvasContext & {
|
|
10
15
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAAA,KAAK,cAAc,GAAG,MAAM,CAAC;AAE7B,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,cAAc,CAAC;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAAA,KAAK,cAAc,GAAG,MAAM,CAAC;AAE7B,MAAM,WAAW,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AAC,OAAO,EAAE,UAAU,GAAG,oCAAoC,CAAC;CACtE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-webgpu",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.17",
|
|
4
4
|
"description": "React Native WebGPU",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
6
|
"module": "lib/module/index",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"lint": "eslint . --ext .ts,.tsx --max-warnings 0 --cache --fix",
|
|
28
28
|
"tsc": "tsc --noEmit",
|
|
29
29
|
"build": "bob build",
|
|
30
|
+
"release": "semantic-release",
|
|
30
31
|
"build-dawn": "tsx scripts/build/dawn.ts",
|
|
31
32
|
"clean-dawn": "rimraf ./libs && rimraf ../../externals/dawn/out",
|
|
32
33
|
"clang-format": "yarn clang-format-ios && yarn clang-format-android && yarn clang-format-common",
|
|
@@ -51,11 +52,17 @@
|
|
|
51
52
|
"bugs": {
|
|
52
53
|
"url": "https://github.com/wcandillon/react-native-webgpu/issues"
|
|
53
54
|
},
|
|
54
|
-
"homepage": "https://github.
|
|
55
|
+
"homepage": "https://wcandillon.github.io/react-native-webgpu/",
|
|
55
56
|
"publishConfig": {
|
|
56
|
-
"registry": "https://registry.npmjs.org/"
|
|
57
|
+
"registry": "https://registry.npmjs.org/",
|
|
58
|
+
"access": "public",
|
|
59
|
+
"provenance": true
|
|
57
60
|
},
|
|
58
61
|
"devDependencies": {
|
|
62
|
+
"@semantic-release/commit-analyzer": "^13.0.0",
|
|
63
|
+
"@semantic-release/exec": "^7.0.3",
|
|
64
|
+
"@semantic-release/github": "^10.3.3",
|
|
65
|
+
"@semantic-release/release-notes-generator": "^14.0.1",
|
|
59
66
|
"@types/jest": "^29.5.12",
|
|
60
67
|
"@types/lodash": "^4.17.5",
|
|
61
68
|
"@types/node": "^20.14.7",
|
|
@@ -87,6 +94,8 @@
|
|
|
87
94
|
"react-native-worklets": "0.8.3",
|
|
88
95
|
"rimraf": "^5.0.7",
|
|
89
96
|
"seedrandom": "^3.0.5",
|
|
97
|
+
"semantic-release": "^24.1.0",
|
|
98
|
+
"semantic-release-yarn": "^3.0.2",
|
|
90
99
|
"teapot": "^1.0.0",
|
|
91
100
|
"ts-morph": "^22.0.0",
|
|
92
101
|
"tsx": "^4.20.5",
|
package/src/Canvas.tsx
CHANGED
|
@@ -17,6 +17,13 @@ export interface NativeCanvas {
|
|
|
17
17
|
height: number;
|
|
18
18
|
clientWidth: number;
|
|
19
19
|
clientHeight: number;
|
|
20
|
+
// No-op DOM-compatibility stubs so web renderers (Three.js,
|
|
21
|
+
// react-three-fiber) can treat the canvas like an HTMLCanvasElement.
|
|
22
|
+
addEventListener(type: string, listener: EventListener): void;
|
|
23
|
+
removeEventListener(type: string, listener: EventListener): void;
|
|
24
|
+
dispatchEvent(event: Event): void;
|
|
25
|
+
setPointerCapture(pointerId: number): void;
|
|
26
|
+
releasePointerCapture(pointerId: number): void;
|
|
20
27
|
}
|
|
21
28
|
|
|
22
29
|
export type RNCanvasContext = GPUCanvasContext & {
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import React, { createContext, useContext } from "react";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
|
|
4
|
+
import { useDevice } from "./hooks";
|
|
5
|
+
|
|
6
|
+
interface DeviceContextValue {
|
|
7
|
+
device: GPUDevice | null;
|
|
8
|
+
adapter: GPUAdapter | null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const DeviceContext = createContext<DeviceContextValue | null>(null);
|
|
12
|
+
|
|
13
|
+
interface DeviceProviderProps {
|
|
14
|
+
children?: ReactNode | ReactNode[];
|
|
15
|
+
adapterOptions?: GPURequestAdapterOptions;
|
|
16
|
+
deviceDescriptor?: GPUDeviceDescriptor;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const GPUDeviceProvider = ({
|
|
20
|
+
children,
|
|
21
|
+
adapterOptions,
|
|
22
|
+
deviceDescriptor,
|
|
23
|
+
}: DeviceProviderProps) => {
|
|
24
|
+
const state = useDevice(adapterOptions, deviceDescriptor);
|
|
25
|
+
if (!state.device) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
return (
|
|
29
|
+
<DeviceContext.Provider value={state}>{children}</DeviceContext.Provider>
|
|
30
|
+
);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const useMainDevice = () => {
|
|
34
|
+
const ctx = useContext(DeviceContext);
|
|
35
|
+
if (!ctx) {
|
|
36
|
+
throw new Error("No DeviceContext found.");
|
|
37
|
+
}
|
|
38
|
+
return ctx;
|
|
39
|
+
};
|
|
@@ -15,6 +15,12 @@ function getNativeSurface(contextId: number) {
|
|
|
15
15
|
width,
|
|
16
16
|
clientHeight: height,
|
|
17
17
|
clientWidth: width,
|
|
18
|
+
// On web the real DOM canvas handles events; delegate to it.
|
|
19
|
+
addEventListener: canvas.addEventListener.bind(canvas),
|
|
20
|
+
removeEventListener: canvas.removeEventListener.bind(canvas),
|
|
21
|
+
dispatchEvent: canvas.dispatchEvent.bind(canvas),
|
|
22
|
+
setPointerCapture: canvas.setPointerCapture.bind(canvas),
|
|
23
|
+
releasePointerCapture: canvas.releasePointerCapture.bind(canvas),
|
|
18
24
|
};
|
|
19
25
|
}
|
|
20
26
|
|
package/src/constants.ts
CHANGED
|
@@ -26,12 +26,12 @@
|
|
|
26
26
|
// re-exports `./main` before `./constants`, and `./main` installs the native
|
|
27
27
|
// module synchronously, so the globals always exist by the time this runs.
|
|
28
28
|
|
|
29
|
-
export const GPUBufferUsage = globalThis
|
|
29
|
+
export const { GPUBufferUsage } = globalThis;
|
|
30
30
|
|
|
31
|
-
export const GPUTextureUsage = globalThis
|
|
31
|
+
export const { GPUTextureUsage } = globalThis;
|
|
32
32
|
|
|
33
|
-
export const GPUShaderStage = globalThis
|
|
33
|
+
export const { GPUShaderStage } = globalThis;
|
|
34
34
|
|
|
35
|
-
export const GPUColorWrite = globalThis
|
|
35
|
+
export const { GPUColorWrite } = globalThis;
|
|
36
36
|
|
|
37
|
-
export const GPUMapMode = globalThis
|
|
37
|
+
export const { GPUMapMode } = globalThis;
|
package/src/formats.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/// <reference types="@webgpu/types" />
|
|
2
|
+
|
|
3
|
+
import { Platform } from "react-native";
|
|
4
|
+
|
|
5
|
+
// Non-spec helper: the canvas texture format most likely to reach the display
|
|
6
|
+
// with more than 8 bits per channel on the current platform. The returned
|
|
7
|
+
// format keeps standard (SDR) sRGB semantics: values are encoded exactly like
|
|
8
|
+
// on an 8-bit canvas, only with more precision, so colors match across
|
|
9
|
+
// formats. This is about bit depth, not HDR (no extended range).
|
|
10
|
+
//
|
|
11
|
+
// - Apple platforms: "rgba16float" (16-bit float per channel). The library
|
|
12
|
+
// tags the CAMetalLayer with the extended sRGB colorspace automatically so
|
|
13
|
+
// the values display identically to an 8-bit canvas.
|
|
14
|
+
// - Android: "rgb10a2unorm". A float16 swapchain works but is tagged as an
|
|
15
|
+
// SDR sRGB layer, and SurfaceFlinger quantizes SDR layers to the display
|
|
16
|
+
// pipeline depth during composition, discarding the extra precision.
|
|
17
|
+
// RGBA_1010102 buffers keep SDR semantics and can pass through composition,
|
|
18
|
+
// including direct scanout on 10-bit panels. Requires the Vulkan surface to
|
|
19
|
+
// expose the 10-bit format (common on modern devices).
|
|
20
|
+
// - Web: "rgba16float", the float canvas format supported by browsers.
|
|
21
|
+
export const getPreferredHighBitDepthCanvasFormat = (): GPUTextureFormat => {
|
|
22
|
+
return Platform.OS === "android" ? "rgb10a2unorm" : "rgba16float";
|
|
23
|
+
};
|
package/src/hooks.tsx
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
createContext,
|
|
3
|
-
useContext,
|
|
4
|
-
useEffect,
|
|
5
|
-
useLayoutEffect,
|
|
6
|
-
useRef,
|
|
7
|
-
useState,
|
|
8
|
-
} from "react";
|
|
9
|
-
import type { ReactNode } from "react";
|
|
1
|
+
import { useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
10
2
|
|
|
11
3
|
import type { CanvasRef, NativeCanvas } from "./Canvas";
|
|
12
4
|
|
|
@@ -20,33 +12,11 @@ export const warnIfNotHardwareAccelerated = (adapter: GPUAdapter) => {
|
|
|
20
12
|
}
|
|
21
13
|
};
|
|
22
14
|
|
|
23
|
-
interface
|
|
15
|
+
interface DeviceState {
|
|
24
16
|
device: GPUDevice | null;
|
|
25
17
|
adapter: GPUAdapter | null;
|
|
26
18
|
}
|
|
27
19
|
|
|
28
|
-
const DeviceContext = createContext<DeviceContext | null>(null);
|
|
29
|
-
|
|
30
|
-
interface DeviceProviderProps {
|
|
31
|
-
children?: ReactNode | ReactNode[];
|
|
32
|
-
adapterOptions?: GPURequestAdapterOptions;
|
|
33
|
-
deviceDescriptor?: GPUDeviceDescriptor;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export const GPUDeviceProvider = ({
|
|
37
|
-
children,
|
|
38
|
-
adapterOptions,
|
|
39
|
-
deviceDescriptor,
|
|
40
|
-
}: DeviceProviderProps) => {
|
|
41
|
-
const state = useDevice(adapterOptions, deviceDescriptor);
|
|
42
|
-
if (!state.device) {
|
|
43
|
-
return null;
|
|
44
|
-
}
|
|
45
|
-
return (
|
|
46
|
-
<DeviceContext.Provider value={state}>{children}</DeviceContext.Provider>
|
|
47
|
-
);
|
|
48
|
-
};
|
|
49
|
-
|
|
50
20
|
export const useSurface = () => {
|
|
51
21
|
const ref = useRef<CanvasRef>(null);
|
|
52
22
|
const [surface, setSurface] = useState<NativeCanvas | null>(null);
|
|
@@ -57,21 +27,13 @@ export const useSurface = () => {
|
|
|
57
27
|
return { ref, surface };
|
|
58
28
|
};
|
|
59
29
|
|
|
60
|
-
export const useMainDevice = () => {
|
|
61
|
-
const ctx = useContext(DeviceContext);
|
|
62
|
-
if (!ctx) {
|
|
63
|
-
throw new Error("No DeviceContext found.");
|
|
64
|
-
}
|
|
65
|
-
return ctx;
|
|
66
|
-
};
|
|
67
|
-
|
|
68
30
|
export const useCanvasRef = () => useRef<CanvasRef>(null);
|
|
69
31
|
|
|
70
32
|
export const useDevice = (
|
|
71
33
|
adapterOptions?: GPURequestAdapterOptions,
|
|
72
34
|
deviceDescriptor?: GPUDeviceDescriptor,
|
|
73
35
|
) => {
|
|
74
|
-
const [state, setState] = useState<
|
|
36
|
+
const [state, setState] = useState<DeviceState | null>(null);
|
|
75
37
|
useEffect(() => {
|
|
76
38
|
(async () => {
|
|
77
39
|
if (!state) {
|
package/src/main/index.tsx
CHANGED
|
@@ -5,6 +5,8 @@ export * from "../Canvas";
|
|
|
5
5
|
export * from "../Offscreen";
|
|
6
6
|
export * from "../WebGPUViewNativeComponent";
|
|
7
7
|
export * from "../hooks";
|
|
8
|
+
export * from "../GPUDeviceProvider";
|
|
9
|
+
export * from "../formats";
|
|
8
10
|
|
|
9
11
|
export { default as WebGPUModule } from "../NativeWebGPUModule";
|
|
10
12
|
|
package/src/main/index.web.tsx
CHANGED
|
@@ -4,5 +4,7 @@ export * from "../Canvas";
|
|
|
4
4
|
export * from "../Offscreen";
|
|
5
5
|
export * from "../WebGPUViewNativeComponent";
|
|
6
6
|
export * from "../hooks";
|
|
7
|
+
export * from "../GPUDeviceProvider";
|
|
8
|
+
export * from "../formats";
|
|
7
9
|
|
|
8
10
|
// We don't need to set all global properties on web, webgpu is already available globally
|
package/src/types.ts
CHANGED
|
@@ -6,6 +6,13 @@ export interface NativeCanvas {
|
|
|
6
6
|
height: number;
|
|
7
7
|
clientWidth: number;
|
|
8
8
|
clientHeight: number;
|
|
9
|
+
// No-op DOM-compatibility stubs so web renderers (Three.js,
|
|
10
|
+
// react-three-fiber) can treat the canvas like an HTMLCanvasElement.
|
|
11
|
+
addEventListener(type: string, listener: EventListener): void;
|
|
12
|
+
removeEventListener(type: string, listener: EventListener): void;
|
|
13
|
+
dispatchEvent(event: Event): void;
|
|
14
|
+
setPointerCapture(pointerId: number): void;
|
|
15
|
+
releasePointerCapture(pointerId: number): void;
|
|
9
16
|
}
|
|
10
17
|
|
|
11
18
|
export type RNCanvasContext = GPUCanvasContext & {
|