react-native-webgpu 0.26.0 → 0.27.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/android/CMakeLists.txt +2 -0
- package/cpp/rnwgpu/ArrayBuffer.h +47 -2
- package/cpp/rnwgpu/DawnEventPump.cpp +227 -0
- package/cpp/rnwgpu/DawnEventPump.h +173 -0
- package/cpp/rnwgpu/WebGPURuntime.cpp +6 -8
- package/cpp/rnwgpu/api/GPU.cpp +1 -1
- package/cpp/rnwgpu/api/GPUAdapter.cpp +1 -1
- package/cpp/rnwgpu/api/GPUBuffer.cpp +100 -27
- package/cpp/rnwgpu/api/GPUBuffer.h +55 -6
- package/cpp/rnwgpu/api/GPUDevice.cpp +57 -55
- package/cpp/rnwgpu/api/GPUDevice.h +6 -0
- package/cpp/rnwgpu/api/GPUQueue.cpp +7 -2
- package/cpp/rnwgpu/api/GPUQueue.h +8 -2
- package/cpp/rnwgpu/api/GPUShaderModule.cpp +1 -1
- package/cpp/rnwgpu/api/RNWebGPU.h +3 -3
- package/cpp/rnwgpu/async/AsyncTaskHandle.h +3 -2
- package/cpp/rnwgpu/async/DeviceTicker.cpp +105 -0
- package/cpp/rnwgpu/async/DeviceTicker.h +71 -0
- package/cpp/rnwgpu/async/RuntimeContext.cpp +56 -70
- package/cpp/rnwgpu/async/RuntimeContext.h +37 -18
- package/lib/commonjs/NativeBufferView.js.map +1 -1
- package/lib/commonjs/NativeBufferViewNativeComponent.web.js +114 -0
- package/lib/commonjs/NativeBufferViewNativeComponent.web.js.map +1 -0
- package/lib/commonjs/WebPolyfillGPUModule.js +79 -0
- package/lib/commonjs/WebPolyfillGPUModule.js.map +1 -1
- package/lib/commonjs/main/index.web.js +11 -0
- package/lib/commonjs/main/index.web.js.map +1 -1
- package/lib/commonjs/utils.js +29 -0
- package/lib/commonjs/utils.js.map +1 -1
- package/lib/module/NativeBufferView.js.map +1 -1
- package/lib/module/NativeBufferViewNativeComponent.web.js +107 -0
- package/lib/module/NativeBufferViewNativeComponent.web.js.map +1 -0
- package/lib/module/WebPolyfillGPUModule.js +80 -1
- package/lib/module/WebPolyfillGPUModule.js.map +1 -1
- package/lib/module/main/index.web.js +1 -0
- package/lib/module/main/index.web.js.map +1 -1
- package/lib/module/utils.js +27 -0
- package/lib/module/utils.js.map +1 -1
- package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.web.d.ts +4 -0
- package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.web.d.ts.map +1 -0
- package/lib/typescript/lib/commonjs/utils.d.ts +2 -0
- package/lib/typescript/lib/commonjs/utils.d.ts.map +1 -1
- package/lib/typescript/lib/module/NativeBufferViewNativeComponent.web.d.ts +5 -0
- package/lib/typescript/lib/module/NativeBufferViewNativeComponent.web.d.ts.map +1 -0
- package/lib/typescript/lib/module/main/index.web.d.ts +1 -0
- package/lib/typescript/lib/module/utils.d.ts +2 -0
- package/lib/typescript/lib/module/utils.d.ts.map +1 -1
- package/lib/typescript/src/NativeBufferView.d.ts +6 -1
- package/lib/typescript/src/NativeBufferView.d.ts.map +1 -1
- package/lib/typescript/src/NativeBufferViewNativeComponent.web.d.ts +14 -0
- package/lib/typescript/src/NativeBufferViewNativeComponent.web.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +1 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/main/index.web.d.ts +1 -0
- package/lib/typescript/src/main/index.web.d.ts.map +1 -1
- package/lib/typescript/src/utils.d.ts +2 -0
- package/lib/typescript/src/utils.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/NativeBufferView.tsx +6 -1
- package/src/NativeBufferViewNativeComponent.web.ts +119 -0
- package/src/WebPolyfillGPUModule.ts +75 -1
- package/src/index.tsx +2 -1
- package/src/main/index.web.tsx +1 -0
- package/src/utils.ts +25 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import React, { useEffect, useLayoutEffect, useRef } from "react";
|
|
2
|
+
import { StyleSheet, View } from "react-native";
|
|
3
|
+
import type { CodegenTypes, HostInstance, ViewProps } from "react-native";
|
|
4
|
+
|
|
5
|
+
import { layoutNativeBufferViewCanvas, nativeBufferViewIdToId } from "./utils";
|
|
6
|
+
|
|
7
|
+
type PixelSizeChangeEvent = Readonly<{
|
|
8
|
+
width: CodegenTypes.Int32;
|
|
9
|
+
height: CodegenTypes.Int32;
|
|
10
|
+
}>;
|
|
11
|
+
|
|
12
|
+
export interface NativeProps extends ViewProps {
|
|
13
|
+
viewId: CodegenTypes.Int32;
|
|
14
|
+
resizeMode?: CodegenTypes.WithDefault<
|
|
15
|
+
"stretch" | "contain" | "cover" | "topLeft",
|
|
16
|
+
"stretch"
|
|
17
|
+
>;
|
|
18
|
+
onPixelSizeChange?: CodegenTypes.DirectEventHandler<PixelSizeChangeEvent>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// The size of `entry` in physical pixels. devicePixelContentBoxSize is the
|
|
22
|
+
// size the browser snapped to the pixel grid; without it (Safari), the CSS
|
|
23
|
+
// size times the pixel ratio is the closest there is.
|
|
24
|
+
const pixelSizeOf = (entry: ResizeObserverEntry) => {
|
|
25
|
+
const box = entry.devicePixelContentBoxSize?.[0];
|
|
26
|
+
if (box) {
|
|
27
|
+
return { width: box.inlineSize, height: box.blockSize };
|
|
28
|
+
}
|
|
29
|
+
const dpr = window.devicePixelRatio || 1;
|
|
30
|
+
return {
|
|
31
|
+
width: Math.round(entry.contentRect.width * dpr),
|
|
32
|
+
height: Math.round(entry.contentRect.height * dpr),
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// The web counterpart of the native view: a container that takes the props of
|
|
37
|
+
// a View, and a canvas that holds a copy of the last buffer presented
|
|
38
|
+
// (RNWebGPU.presentNativeBuffer in WebPolyfillGPUModule.ts draws into it).
|
|
39
|
+
// eslint-disable-next-line import/no-default-export
|
|
40
|
+
export default function NativeBufferViewNativeComponent(
|
|
41
|
+
props: NativeProps,
|
|
42
|
+
): React.JSX.Element {
|
|
43
|
+
const { viewId, resizeMode, onPixelSizeChange, style, ...rest } = props;
|
|
44
|
+
const container = useRef<HostInstance>(null);
|
|
45
|
+
const canvas = useRef<HTMLCanvasElement>(null);
|
|
46
|
+
const onPixelSizeChangeRef = useRef(onPixelSizeChange);
|
|
47
|
+
onPixelSizeChangeRef.current = onPixelSizeChange;
|
|
48
|
+
|
|
49
|
+
// A layout effect, so the mode is in place before the first present.
|
|
50
|
+
useLayoutEffect(() => {
|
|
51
|
+
if (canvas.current) {
|
|
52
|
+
canvas.current.dataset.resizeMode = resizeMode ?? "stretch";
|
|
53
|
+
layoutNativeBufferViewCanvas(canvas.current);
|
|
54
|
+
}
|
|
55
|
+
}, [resizeMode]);
|
|
56
|
+
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
// On react-native-web, the ref of a View is its DOM element.
|
|
59
|
+
const element = container.current as unknown as Element | null;
|
|
60
|
+
if (!element || typeof ResizeObserver === "undefined") {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
let width = 0;
|
|
64
|
+
let height = 0;
|
|
65
|
+
const observer = new ResizeObserver((entries) => {
|
|
66
|
+
const entry = entries[entries.length - 1];
|
|
67
|
+
if (!entry) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
// A change of pixel ratio changes what "not scaled" means.
|
|
71
|
+
if (canvas.current) {
|
|
72
|
+
layoutNativeBufferViewCanvas(canvas.current);
|
|
73
|
+
}
|
|
74
|
+
const size = pixelSizeOf(entry);
|
|
75
|
+
if (
|
|
76
|
+
size.width <= 0 ||
|
|
77
|
+
size.height <= 0 ||
|
|
78
|
+
(size.width === width && size.height === height)
|
|
79
|
+
) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
({ width, height } = size);
|
|
83
|
+
onPixelSizeChangeRef.current?.({
|
|
84
|
+
nativeEvent: size,
|
|
85
|
+
} as Parameters<NonNullable<NativeProps["onPixelSizeChange"]>>[0]);
|
|
86
|
+
});
|
|
87
|
+
try {
|
|
88
|
+
observer.observe(element, { box: "device-pixel-content-box" });
|
|
89
|
+
} catch {
|
|
90
|
+
// No device-pixel-content-box (Safari).
|
|
91
|
+
observer.observe(element);
|
|
92
|
+
}
|
|
93
|
+
return () => observer.disconnect();
|
|
94
|
+
}, []);
|
|
95
|
+
|
|
96
|
+
return React.createElement(
|
|
97
|
+
View,
|
|
98
|
+
{ ...rest, ref: container, style: [style, styles.container] },
|
|
99
|
+
React.createElement("canvas", {
|
|
100
|
+
ref: canvas,
|
|
101
|
+
id: nativeBufferViewIdToId(viewId),
|
|
102
|
+
style: canvasStyle,
|
|
103
|
+
}),
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const canvasStyle = {
|
|
108
|
+
position: "absolute",
|
|
109
|
+
left: 0,
|
|
110
|
+
top: 0,
|
|
111
|
+
// The container handles the events, like the native view.
|
|
112
|
+
pointerEvents: "none",
|
|
113
|
+
} as const;
|
|
114
|
+
|
|
115
|
+
const styles = StyleSheet.create({
|
|
116
|
+
container: {
|
|
117
|
+
overflow: "hidden",
|
|
118
|
+
},
|
|
119
|
+
});
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
contextIdToId,
|
|
3
|
+
layoutNativeBufferViewCanvas,
|
|
4
|
+
nativeBufferViewIdToId,
|
|
5
|
+
} from "./utils";
|
|
2
6
|
|
|
3
7
|
const fabric = true;
|
|
4
8
|
|
|
@@ -59,6 +63,70 @@ function makeWebGPUCanvasContext(
|
|
|
59
63
|
}
|
|
60
64
|
|
|
61
65
|
let nextContextId = 1;
|
|
66
|
+
let nextNativeBufferViewId = 1;
|
|
67
|
+
|
|
68
|
+
// The size of a canvas image source in pixels, as drawImage() sees it.
|
|
69
|
+
function sizeOfImageSource(source: CanvasImageSource) {
|
|
70
|
+
if ("displayWidth" in source) {
|
|
71
|
+
// A VideoFrame
|
|
72
|
+
return { width: source.displayWidth, height: source.displayHeight };
|
|
73
|
+
}
|
|
74
|
+
if (source instanceof HTMLVideoElement) {
|
|
75
|
+
return { width: source.videoWidth, height: source.videoHeight };
|
|
76
|
+
}
|
|
77
|
+
if (source instanceof HTMLImageElement) {
|
|
78
|
+
return { width: source.naturalWidth, height: source.naturalHeight };
|
|
79
|
+
}
|
|
80
|
+
if (source instanceof SVGImageElement) {
|
|
81
|
+
return {
|
|
82
|
+
width: source.width.baseVal.value,
|
|
83
|
+
height: source.height.baseVal.value,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return { width: source.width, height: source.height };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// The web counterpart of NativeBufferViewRegistry::present. On the web, the
|
|
90
|
+
// native buffer is a canvas image source, as in drawImage(): an ImageBitmap,
|
|
91
|
+
// an OffscreenCanvas, a canvas, a VideoFrame, or an image or video element.
|
|
92
|
+
// The view keeps a copy of its pixels, so the source can be closed, presented
|
|
93
|
+
// to other views or drawn again right after the call. The fences are not
|
|
94
|
+
// needed: the browser orders the copy after the GPU work that wrote the
|
|
95
|
+
// source.
|
|
96
|
+
function presentNativeBuffer(viewId: number, frame: unknown) {
|
|
97
|
+
const source = frame as CanvasImageSource | null;
|
|
98
|
+
const { width, height } =
|
|
99
|
+
source !== null && typeof source === "object"
|
|
100
|
+
? sizeOfImageSource(source)
|
|
101
|
+
: { width: 0, height: 0 };
|
|
102
|
+
if (source === null || !(width > 0) || !(height > 0)) {
|
|
103
|
+
throw new Error(
|
|
104
|
+
"presentNativeBuffer: on the web, the frame is a canvas image source " +
|
|
105
|
+
"(an ImageBitmap, a canvas, a VideoFrame, an image or a video) that " +
|
|
106
|
+
"has a size",
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
const canvas = document.getElementById(
|
|
110
|
+
nativeBufferViewIdToId(viewId),
|
|
111
|
+
) as HTMLCanvasElement | null;
|
|
112
|
+
if (!canvas) {
|
|
113
|
+
// The view is gone: dropped, as on native.
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
// Setting the size clears the canvas, even to the same value.
|
|
117
|
+
if (canvas.width !== width || canvas.height !== height) {
|
|
118
|
+
canvas.width = width;
|
|
119
|
+
canvas.height = height;
|
|
120
|
+
}
|
|
121
|
+
const context = canvas.getContext("2d");
|
|
122
|
+
if (!context) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
// Replace the pixels, alpha included, rather than blend over the last ones.
|
|
126
|
+
context.globalCompositeOperation = "copy";
|
|
127
|
+
context.drawImage(source, 0, 0, width, height);
|
|
128
|
+
layoutNativeBufferViewCanvas(canvas);
|
|
129
|
+
}
|
|
62
130
|
|
|
63
131
|
// @ts-expect-error - polyfill for RNWebGPU native module
|
|
64
132
|
window.RNWebGPU = {
|
|
@@ -70,5 +138,11 @@ window.RNWebGPU = {
|
|
|
70
138
|
makeWebGPUCanvasContext as typeof RNWebGPU.MakeWebGPUCanvasContext,
|
|
71
139
|
// On web the browser owns the canvas/context lifecycle; nothing to retire.
|
|
72
140
|
destroyContext: (_contextId: number) => {},
|
|
141
|
+
// NativeBufferView: the canvas of the view is the registry entry, and the
|
|
142
|
+
// DOM owns it.
|
|
143
|
+
allocateNativeBufferViewId: () => nextNativeBufferViewId++,
|
|
144
|
+
retainNativeBufferView: (_viewId: number) => {},
|
|
145
|
+
releaseNativeBufferView: (_viewId: number) => {},
|
|
146
|
+
presentNativeBuffer,
|
|
73
147
|
fabric,
|
|
74
148
|
};
|
package/src/index.tsx
CHANGED
|
@@ -142,7 +142,8 @@ declare global {
|
|
|
142
142
|
releaseNativeBufferView: (viewId: number) => void;
|
|
143
143
|
presentNativeBuffer: (
|
|
144
144
|
viewId: number,
|
|
145
|
-
|
|
145
|
+
// On the web, the native buffer is a canvas image source.
|
|
146
|
+
frame: NativeBuffer | CanvasImageSource,
|
|
146
147
|
done?: { fences: GPUSharedFenceState[] } | null,
|
|
147
148
|
) => void;
|
|
148
149
|
};
|
package/src/main/index.web.tsx
CHANGED
package/src/utils.ts
CHANGED
|
@@ -2,3 +2,28 @@
|
|
|
2
2
|
export function contextIdToId(contextId: number) {
|
|
3
3
|
return "rnwgpu-canvas-" + contextId;
|
|
4
4
|
}
|
|
5
|
+
|
|
6
|
+
// Only used on the web
|
|
7
|
+
export function nativeBufferViewIdToId(viewId: number) {
|
|
8
|
+
return "rnwgpu-native-buffer-view-" + viewId;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// Only used on the web: fits the canvas of a NativeBufferView in its
|
|
12
|
+
// container according to its resize mode (data-resize-mode). The drawing
|
|
13
|
+
// buffer of the canvas is the presented buffer, in physical pixels.
|
|
14
|
+
export function layoutNativeBufferViewCanvas(canvas: HTMLCanvasElement) {
|
|
15
|
+
const mode = canvas.dataset.resizeMode;
|
|
16
|
+
const { style } = canvas;
|
|
17
|
+
if (mode === "topLeft") {
|
|
18
|
+
// Not scaled: one pixel of the buffer on one pixel of the display. The
|
|
19
|
+
// container crops what overflows.
|
|
20
|
+
const dpr = window.devicePixelRatio || 1;
|
|
21
|
+
style.width = `${canvas.width / dpr}px`;
|
|
22
|
+
style.height = `${canvas.height / dpr}px`;
|
|
23
|
+
style.objectFit = "fill";
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
style.width = "100%";
|
|
27
|
+
style.height = "100%";
|
|
28
|
+
style.objectFit = mode === "contain" || mode === "cover" ? mode : "fill";
|
|
29
|
+
}
|