react-native-wgpu 0.2.10 → 0.3.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.
Files changed (248) hide show
  1. package/README.md +155 -69
  2. package/android/CMakeLists.txt +4 -5
  3. package/android/build.gradle +7 -18
  4. package/android/src/main/java/com/webgpu/WebGPUViewPackage.java +34 -10
  5. package/apple/MetalView.mm +0 -19
  6. package/apple/WebGPUModule.h +1 -9
  7. package/apple/WebGPUModule.mm +0 -3
  8. package/apple/WebGPUView.h +0 -3
  9. package/apple/WebGPUView.mm +0 -2
  10. package/cpp/WGPULogger.h +10 -0
  11. package/cpp/dawn/dawn_proc_table.h +1 -1
  12. package/cpp/dawn/webgpu.h +4855 -0
  13. package/cpp/dawn/webgpu_cpp.h +10168 -0
  14. package/cpp/dawn/wire/client/webgpu.h +354 -0
  15. package/cpp/dawn/wire/client/webgpu_cpp.h +10343 -0
  16. package/cpp/dawn/wire/client/webgpu_cpp_print.h +2715 -0
  17. package/cpp/jsi/RNFJSIConverter.h +17 -59
  18. package/cpp/rnwgpu/RNWebGPUManager.cpp +1 -9
  19. package/cpp/rnwgpu/api/GPU.cpp +51 -26
  20. package/cpp/rnwgpu/api/GPU.h +5 -18
  21. package/cpp/rnwgpu/api/GPUAdapter.cpp +75 -54
  22. package/cpp/rnwgpu/api/GPUAdapter.h +6 -6
  23. package/cpp/rnwgpu/api/GPUAdapterInfo.h +0 -1
  24. package/cpp/rnwgpu/api/GPUBindGroup.h +1 -3
  25. package/cpp/rnwgpu/api/GPUBindGroupLayout.h +1 -3
  26. package/cpp/rnwgpu/api/GPUBuffer.cpp +35 -32
  27. package/cpp/rnwgpu/api/GPUBuffer.h +9 -7
  28. package/cpp/rnwgpu/api/GPUCanvasContext.cpp +5 -1
  29. package/cpp/rnwgpu/api/GPUCanvasContext.h +0 -2
  30. package/cpp/rnwgpu/api/GPUCommandBuffer.h +1 -3
  31. package/cpp/rnwgpu/api/GPUCommandEncoder.h +1 -3
  32. package/cpp/rnwgpu/api/GPUCompilationInfo.h +0 -2
  33. package/cpp/rnwgpu/api/GPUCompilationMessage.h +1 -3
  34. package/cpp/rnwgpu/api/GPUComputePassEncoder.h +1 -3
  35. package/cpp/rnwgpu/api/GPUComputePipeline.h +1 -3
  36. package/cpp/rnwgpu/api/GPUDevice.cpp +183 -128
  37. package/cpp/rnwgpu/api/GPUDevice.h +22 -21
  38. package/cpp/rnwgpu/api/GPUDeviceLostInfo.h +1 -3
  39. package/cpp/rnwgpu/api/GPUExternalTexture.h +1 -3
  40. package/cpp/rnwgpu/api/GPUPipelineLayout.h +1 -3
  41. package/cpp/rnwgpu/api/GPUQuerySet.h +1 -3
  42. package/cpp/rnwgpu/api/GPUQueue.cpp +19 -8
  43. package/cpp/rnwgpu/api/GPUQueue.h +7 -6
  44. package/cpp/rnwgpu/api/GPURenderBundle.h +1 -3
  45. package/cpp/rnwgpu/api/GPURenderBundleEncoder.h +1 -3
  46. package/cpp/rnwgpu/api/GPURenderPassEncoder.h +1 -3
  47. package/cpp/rnwgpu/api/GPURenderPipeline.h +1 -3
  48. package/cpp/rnwgpu/api/GPUSampler.h +1 -3
  49. package/cpp/rnwgpu/api/GPUShaderModule.cpp +42 -28
  50. package/cpp/rnwgpu/api/GPUShaderModule.h +6 -6
  51. package/cpp/rnwgpu/api/GPUSupportedLimits.h +1 -3
  52. package/cpp/rnwgpu/api/GPUTexture.h +1 -3
  53. package/cpp/rnwgpu/api/GPUTextureView.h +1 -3
  54. package/cpp/rnwgpu/api/RNWebGPU.h +1 -7
  55. package/cpp/rnwgpu/async/AsyncDispatcher.h +28 -0
  56. package/cpp/rnwgpu/async/AsyncRunner.cpp +215 -0
  57. package/cpp/rnwgpu/async/AsyncRunner.h +53 -0
  58. package/cpp/rnwgpu/async/AsyncTaskHandle.cpp +181 -0
  59. package/cpp/rnwgpu/async/AsyncTaskHandle.h +55 -0
  60. package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.cpp +23 -0
  61. package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.h +22 -0
  62. package/cpp/webgpu/webgpu.h +5 -4827
  63. package/cpp/webgpu/webgpu_cpp.h +5 -10140
  64. package/cpp/{dawn/native/WebGPUBackend.h → webgpu/webgpu_cpp_print.h} +4 -20
  65. package/lib/commonjs/Canvas.js +6 -66
  66. package/lib/commonjs/Canvas.js.map +1 -1
  67. package/lib/commonjs/hooks.js +6 -42
  68. package/lib/commonjs/hooks.js.map +1 -1
  69. package/lib/module/Canvas.js +7 -67
  70. package/lib/module/Canvas.js.map +1 -1
  71. package/lib/module/hooks.js +5 -40
  72. package/lib/module/hooks.js.map +1 -1
  73. package/lib/typescript/lib/commonjs/hooks.d.ts +1 -5
  74. package/lib/typescript/lib/commonjs/hooks.d.ts.map +1 -1
  75. package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
  76. package/lib/typescript/lib/module/hooks.d.ts +1 -5
  77. package/lib/typescript/lib/module/hooks.d.ts.map +1 -1
  78. package/lib/typescript/src/Canvas.d.ts +0 -1
  79. package/lib/typescript/src/Canvas.d.ts.map +1 -1
  80. package/lib/typescript/src/hooks.d.ts +2 -7
  81. package/lib/typescript/src/hooks.d.ts.map +1 -1
  82. package/libs/android/arm64-v8a/libwebgpu_dawn.so +0 -0
  83. package/libs/android/armeabi-v7a/libwebgpu_dawn.so +0 -0
  84. package/libs/android/x86/libwebgpu_dawn.so +0 -0
  85. package/libs/android/x86_64/libwebgpu_dawn.so +0 -0
  86. package/libs/apple/libwebgpu_dawn.xcframework/Info.plist +5 -35
  87. package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/libwebgpu_dawn.a +0 -0
  88. package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/libwebgpu_dawn.a +0 -0
  89. package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/libwebgpu_dawn.a +0 -0
  90. package/package.json +4 -3
  91. package/react-native-wgpu.podspec +12 -16
  92. package/src/Canvas.tsx +8 -69
  93. package/src/hooks.tsx +14 -48
  94. package/android/cpp/platform/ThreadUtils.cpp +0 -41
  95. package/android/src/oldarch/com/webgpu/NativeWebGPUModuleSpec.java +0 -23
  96. package/android/src/oldarch/com/webgpu/WebGPUViewManagerSpec.java +0 -12
  97. package/apple/WebGPUViewManager.mm +0 -24
  98. package/apple/platform/ThreadUtils.cpp +0 -34
  99. package/cpp/dawn/dawn_proc.h +0 -50
  100. package/cpp/dawn/dawn_thread_dispatch_proc.h +0 -47
  101. package/cpp/dawn/native/D3D11Backend.h +0 -77
  102. package/cpp/dawn/native/D3D12Backend.h +0 -68
  103. package/cpp/dawn/native/D3DBackend.h +0 -56
  104. package/cpp/dawn/native/MetalBackend.h +0 -56
  105. package/cpp/dawn/platform/DawnPlatform.h +0 -167
  106. package/cpp/dawn/platform/dawn_platform_export.h +0 -49
  107. package/cpp/platform/ThreadUtils.h +0 -30
  108. package/cpp/rnwgpu/api/AsyncRunner.h +0 -30
  109. package/cpp/threading/CallInvokerDispatcher.h +0 -37
  110. package/cpp/threading/Dispatcher.cpp +0 -55
  111. package/cpp/threading/Dispatcher.h +0 -93
  112. package/cpp/threading/ThreadPool.cpp +0 -88
  113. package/cpp/threading/ThreadPool.h +0 -53
  114. package/cpp/webgpu/webgpu_glfw.h +0 -88
  115. package/lib/typescript/src/__tests__/Alpha.spec.d.ts +0 -2
  116. package/lib/typescript/src/__tests__/Alpha.spec.d.ts.map +0 -1
  117. package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts +0 -2
  118. package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts.map +0 -1
  119. package/lib/typescript/src/__tests__/Buffer.spec.d.ts +0 -2
  120. package/lib/typescript/src/__tests__/Buffer.spec.d.ts.map +0 -1
  121. package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts +0 -2
  122. package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts.map +0 -1
  123. package/lib/typescript/src/__tests__/Constants.spec.d.ts +0 -2
  124. package/lib/typescript/src/__tests__/Constants.spec.d.ts.map +0 -1
  125. package/lib/typescript/src/__tests__/Device.spec.d.ts +0 -2
  126. package/lib/typescript/src/__tests__/Device.spec.d.ts.map +0 -1
  127. package/lib/typescript/src/__tests__/ErrorScope.spec.d.ts +0 -2
  128. package/lib/typescript/src/__tests__/ErrorScope.spec.d.ts.map +0 -1
  129. package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts +0 -2
  130. package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts.map +0 -1
  131. package/lib/typescript/src/__tests__/GPU.spec.d.ts +0 -2
  132. package/lib/typescript/src/__tests__/GPU.spec.d.ts.map +0 -1
  133. package/lib/typescript/src/__tests__/ImageData.spec.d.ts +0 -2
  134. package/lib/typescript/src/__tests__/ImageData.spec.d.ts.map +0 -1
  135. package/lib/typescript/src/__tests__/Shaders.spec.d.ts +0 -2
  136. package/lib/typescript/src/__tests__/Shaders.spec.d.ts.map +0 -1
  137. package/lib/typescript/src/__tests__/Texture.spec.d.ts +0 -2
  138. package/lib/typescript/src/__tests__/Texture.spec.d.ts.map +0 -1
  139. package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts +0 -3
  140. package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts.map +0 -1
  141. package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts +0 -29
  142. package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts.map +0 -1
  143. package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts +0 -5
  144. package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts.map +0 -1
  145. package/lib/typescript/src/__tests__/components/cube.d.ts +0 -7
  146. package/lib/typescript/src/__tests__/components/cube.d.ts.map +0 -1
  147. package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts +0 -22
  148. package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts.map +0 -1
  149. package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts +0 -12
  150. package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts.map +0 -1
  151. package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts +0 -7
  152. package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts.map +0 -1
  153. package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts +0 -6
  154. package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts.map +0 -1
  155. package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts +0 -6
  156. package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts.map +0 -1
  157. package/lib/typescript/src/__tests__/components/meshes/utils.d.ts +0 -10
  158. package/lib/typescript/src/__tests__/components/meshes/utils.d.ts.map +0 -1
  159. package/lib/typescript/src/__tests__/components/triangle.d.ts +0 -3
  160. package/lib/typescript/src/__tests__/components/triangle.d.ts.map +0 -1
  161. package/lib/typescript/src/__tests__/config.d.ts +0 -3
  162. package/lib/typescript/src/__tests__/config.d.ts.map +0 -1
  163. package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts +0 -2
  164. package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts.map +0 -1
  165. package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts +0 -2
  166. package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts.map +0 -1
  167. package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts +0 -2
  168. package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts.map +0 -1
  169. package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts +0 -2
  170. package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts.map +0 -1
  171. package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts +0 -2
  172. package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts.map +0 -1
  173. package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts +0 -2
  174. package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts.map +0 -1
  175. package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts +0 -2
  176. package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts.map +0 -1
  177. package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts +0 -2
  178. package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts.map +0 -1
  179. package/lib/typescript/src/__tests__/globalSetup.d.ts +0 -3
  180. package/lib/typescript/src/__tests__/globalSetup.d.ts.map +0 -1
  181. package/lib/typescript/src/__tests__/globalTeardown.d.ts +0 -3
  182. package/lib/typescript/src/__tests__/globalTeardown.d.ts.map +0 -1
  183. package/lib/typescript/src/__tests__/setup.d.ts +0 -63
  184. package/lib/typescript/src/__tests__/setup.d.ts.map +0 -1
  185. package/libs/apple/arm64_iphoneos/libwebgpu_dawn.a +0 -0
  186. package/libs/apple/arm64_iphonesimulator/libwebgpu_dawn.a +0 -0
  187. package/libs/apple/arm64_xros/libwebgpu_dawn.a +0 -0
  188. package/libs/apple/arm64_xrsimulator/libwebgpu_dawn.a +0 -0
  189. package/libs/apple/iphonesimulator/libwebgpu_dawn.a +0 -0
  190. package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64/libwebgpu_dawn.a +0 -0
  191. package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64-simulator/libwebgpu_dawn.a +0 -0
  192. package/libs/apple/universal_macosx/libwebgpu_dawn.a +0 -0
  193. package/libs/apple/x86_64_iphonesimulator/libwebgpu_dawn.a +0 -0
  194. package/libs/dawn.json +0 -4670
  195. package/src/__tests__/Alpha.spec.ts +0 -28
  196. package/src/__tests__/ArrayBuffer.spec.ts +0 -76
  197. package/src/__tests__/Buffer.spec.ts +0 -357
  198. package/src/__tests__/ComputeShader.spec.ts +0 -375
  199. package/src/__tests__/Constants.spec.ts +0 -89
  200. package/src/__tests__/Device.spec.ts +0 -84
  201. package/src/__tests__/ErrorScope.spec.ts +0 -92
  202. package/src/__tests__/ExternalTexture.spec.ts +0 -284
  203. package/src/__tests__/GPU.spec.ts +0 -272
  204. package/src/__tests__/ImageData.spec.ts +0 -26
  205. package/src/__tests__/Shaders.spec.ts +0 -232
  206. package/src/__tests__/Texture.spec.ts +0 -197
  207. package/src/__tests__/assets/Di-3d.png +0 -0
  208. package/src/__tests__/components/Wireframe/Shaders.ts +0 -138
  209. package/src/__tests__/components/Wireframe/models.ts +0 -113
  210. package/src/__tests__/components/Wireframe/utils.ts +0 -22
  211. package/src/__tests__/components/cube.ts +0 -51
  212. package/src/__tests__/components/meshes/mesh.ts +0 -96
  213. package/src/__tests__/components/meshes/sphere.ts +0 -103
  214. package/src/__tests__/components/meshes/stanfordDragon.ts +0 -44
  215. package/src/__tests__/components/meshes/stanfordDragonData.ts +0 -5
  216. package/src/__tests__/components/meshes/teapot.ts +0 -13
  217. package/src/__tests__/components/meshes/utils.ts +0 -235
  218. package/src/__tests__/components/triangle.ts +0 -17
  219. package/src/__tests__/config.ts +0 -2
  220. package/src/__tests__/demos/ABuffer.spec.ts +0 -890
  221. package/src/__tests__/demos/Blur.spec.ts +0 -398
  222. package/src/__tests__/demos/Cube.spec.ts +0 -929
  223. package/src/__tests__/demos/FractalCube.spec.ts +0 -240
  224. package/src/__tests__/demos/OcclusionQuery.spec.ts +0 -376
  225. package/src/__tests__/demos/RenderBundles.spec.ts +0 -580
  226. package/src/__tests__/demos/Triangle.spec.ts +0 -266
  227. package/src/__tests__/demos/Wireframe.spec.ts +0 -188
  228. package/src/__tests__/globalSetup.ts +0 -45
  229. package/src/__tests__/globalTeardown.ts +0 -11
  230. package/src/__tests__/setup.ts +0 -423
  231. package/src/__tests__/snapshots/abuffer.png +0 -0
  232. package/src/__tests__/snapshots/asteroid.png +0 -0
  233. package/src/__tests__/snapshots/blur.png +0 -0
  234. package/src/__tests__/snapshots/buffer.png +0 -0
  235. package/src/__tests__/snapshots/constant-triangle.png +0 -0
  236. package/src/__tests__/snapshots/cube.png +0 -0
  237. package/src/__tests__/snapshots/f.png +0 -0
  238. package/src/__tests__/snapshots/f2.png +0 -0
  239. package/src/__tests__/snapshots/fractal-cubes.png +0 -0
  240. package/src/__tests__/snapshots/instanced-cubes.png +0 -0
  241. package/src/__tests__/snapshots/occlusion-query.png +0 -0
  242. package/src/__tests__/snapshots/ref.png +0 -0
  243. package/src/__tests__/snapshots/semi-opaque-cyan.png +0 -0
  244. package/src/__tests__/snapshots/texture.png +0 -0
  245. package/src/__tests__/snapshots/textured-cube.png +0 -0
  246. package/src/__tests__/snapshots/triangle-msaa.png +0 -0
  247. package/src/__tests__/snapshots/triangle.png +0 -0
  248. package/src/__tests__/snapshots/two-cube.png +0 -0
@@ -1,423 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
-
3
- import fs from "fs";
4
- import path from "path";
5
-
6
- import puppeteer from "puppeteer";
7
- import { PNG } from "pngjs";
8
- import pixelmatch from "pixelmatch";
9
- import type { mat4, vec3, mat3 } from "wgpu-matrix";
10
- import type { Server, WebSocket } from "ws";
11
- import type { Browser, Page } from "puppeteer";
12
-
13
- import type { GPUOffscreenCanvas } from "../Offscreen";
14
-
15
- import { cubeVertexArray } from "./components/cube";
16
- import { redFragWGSL, triangleVertWGSL } from "./components/triangle";
17
- import { DEBUG, REFERENCE } from "./config";
18
-
19
- jest.setTimeout(180 * 1000);
20
-
21
- type TestOS = "ios" | "android" | "web" | "node";
22
-
23
- declare global {
24
- var testServer: Server;
25
- var testClient: WebSocket;
26
- var testOS: TestOS;
27
- var testArch: "paper" | "fabric";
28
- }
29
-
30
- interface GPUTestingContext {
31
- gpu: GPU;
32
- device: GPUDevice;
33
- shaders: {
34
- triangleVertWGSL: string;
35
- redFragWGSL: string;
36
- };
37
- urls: {
38
- fTexture: string;
39
- };
40
- assets: {
41
- cubeVertexArray: Float32Array;
42
- di3D: ImageData;
43
- moon: ImageData;
44
- saturn: ImageData;
45
- };
46
- ctx: GPUCanvasContext;
47
- canvas: GPUOffscreenCanvas;
48
- mat4: typeof mat4;
49
- vec3: typeof vec3;
50
- mat3: typeof mat3;
51
- }
52
-
53
- type Ctx = Record<string, unknown>;
54
-
55
- type JSONValue =
56
- | { [key: string]: JSONValue }
57
- | JSONValue[]
58
- | number
59
- | string
60
- | boolean
61
- | null;
62
-
63
- interface TestingClient {
64
- eval<C = Ctx, R = JSONValue>(
65
- fn: (ctx: GPUTestingContext & C) => R | Promise<R>,
66
- ctx?: C,
67
- ): Promise<R>;
68
- OS: TestOS;
69
- arch: "paper" | "fabric";
70
- init(): Promise<void>;
71
- dispose(): Promise<void>;
72
- }
73
-
74
- export let client: TestingClient;
75
-
76
- beforeAll(async () => {
77
- client = REFERENCE ? new ReferenceTestingClient() : new RemoteTestingClient();
78
- await client.init();
79
- });
80
-
81
- afterAll(async () => {
82
- await client.dispose();
83
- });
84
-
85
- class RemoteTestingClient implements TestingClient {
86
- readonly OS = global.testOS;
87
- readonly arch = global.testArch;
88
-
89
- eval<C = Ctx, R = JSONValue>(
90
- fn: (ctx: GPUTestingContext & C) => R | Promise<R>,
91
- context?: C,
92
- ): Promise<R> {
93
- const ctx = this.prepareContext(context ?? {});
94
- const body = { code: fn.toString(), ctx };
95
- return this.handleResponse<R>(JSON.stringify(body));
96
- }
97
-
98
- private handleResponse<R>(body: string): Promise<R> {
99
- return new Promise((resolve) => {
100
- this.client.once("message", (raw: Buffer) => {
101
- resolve(JSON.parse(raw.toString()));
102
- });
103
- this.client.send(body);
104
- });
105
- }
106
-
107
- private get client() {
108
- if (global.testClient === null) {
109
- throw new Error("Client is not connected. Did you call init?");
110
- }
111
- return global.testClient!;
112
- }
113
-
114
- private prepareContext<C extends Ctx>(context?: C): C {
115
- const ctx: any = {};
116
- if (context) {
117
- for (const [key, value] of Object.entries(context)) {
118
- ctx[key] = value;
119
- }
120
- }
121
- return ctx;
122
- }
123
- async init() {}
124
- async dispose() {}
125
- }
126
-
127
- class ReferenceTestingClient implements TestingClient {
128
- readonly OS = "web";
129
- readonly arch = "paper";
130
-
131
- private browser: Browser | null = null;
132
- private page: Page | null = null;
133
-
134
- async eval<C = Ctx, R = JSONValue>(
135
- fn: (ctx: GPUTestingContext & C) => R | Promise<R>,
136
- ctx?: C,
137
- ): Promise<R> {
138
- if (!this.page) {
139
- throw new Error("RemoteSurface not initialized");
140
- }
141
- const fTexturePath = path.join(
142
- __dirname,
143
- "../../../../apps/example/src/assets/f.png",
144
- );
145
- const fTextureData = fs.readFileSync(fTexturePath);
146
- const fTextureBase64 = `data:image/png;base64,${fTextureData.toString("base64")}`;
147
- const source = `(async function Main(){
148
- var global = window;
149
- const r = () => {${fs.readFileSync(path.join(__dirname, "../../../../node_modules/wgpu-matrix/dist/3.x/wgpu-matrix.js"), "utf8")} };
150
- r();
151
- const { mat4, vec3, mat3 } = window.wgpuMatrix;
152
- const { device, adapter, gpu, cubeVertexArray, triangleVertWGSL, redFragWGSL, di3D, saturn, moon } = window;
153
- class DrawingContext {
154
- constructor(device, width, height) {
155
- this.device = device;
156
- this.width = width;
157
- this.height = height;
158
- const bytesPerRow = this.width * 4;
159
- this.texture = device.createTexture({
160
- size: [width, height],
161
- format: gpu.getPreferredCanvasFormat(),
162
- usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
163
- });
164
- this.buffer = device.createBuffer({
165
- size: bytesPerRow * this.height,
166
- usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
167
- });
168
- }
169
- getCurrentTexture() {
170
- return this.texture;
171
- }
172
-
173
- get canvas() {
174
- return {
175
- width: this.width,
176
- height: this.height,
177
- };
178
- }
179
-
180
- getImageData() {
181
- const commandEncoder = this.device.createCommandEncoder();
182
- const bytesPerRow = this.width * 4;
183
- commandEncoder.copyTextureToBuffer({ texture: this.texture }, { buffer: this.buffer, bytesPerRow }, [this.width, this.height]);
184
- this.device.queue.submit([commandEncoder.finish()]);
185
-
186
- return this.buffer.mapAsync(GPUMapMode.READ).then(() => {
187
- const arrayBuffer = this.buffer.getMappedRange();
188
- const uint8Array = new Uint8Array(arrayBuffer);
189
- const data = Array.from(uint8Array);
190
- this.buffer.unmap();
191
- return {
192
- data,
193
- width: this.width,
194
- height: this.height,
195
- format: gpu.getPreferredCanvasFormat(),
196
- };
197
- });
198
- }
199
- }
200
-
201
- const ctx = new DrawingContext(device, 1024, 1024);
202
- return (${fn.toString()})({
203
- device, adapter, gpu,
204
- urls: {
205
- fTexture: "${fTextureBase64}"
206
- },
207
- assets: {
208
- cubeVertexArray,
209
- di3D,
210
- moon,
211
- saturn,
212
- },
213
- shaders: {
214
- triangleVertWGSL,
215
- redFragWGSL,
216
- },
217
- ctx,
218
- canvas: {
219
- getImageData: ctx.getImageData.bind(ctx),
220
- width: ctx.width,
221
- height: ctx.height,
222
- },
223
- mat4,
224
- vec3,
225
- mat3,
226
- ...${JSON.stringify(ctx || {})}
227
- });
228
- })();`;
229
- const data = await this.page.evaluate(source);
230
- return data as R;
231
- }
232
-
233
- async init() {
234
- const browser = await puppeteer.launch({
235
- headless: !DEBUG,
236
- args: ["--enable-unsafe-webgpu"],
237
- });
238
- const page = await browser.newPage();
239
- page.on("console", (msg) => console.log(msg.text()));
240
- page.on("pageerror", (error) => {
241
- console.error(error.message);
242
- });
243
- await page
244
- .goto("chrome://gpu", {
245
- waitUntil: "networkidle0",
246
- timeout: 20 * 60 * 1000,
247
- })
248
- .catch((e) => console.log(e));
249
- await page.waitForNetworkIdle();
250
- const di3D = decodeImage(
251
- path.join(__dirname, "../../../../apps/example/src/assets/Di-3d.png"),
252
- );
253
- const moon = decodeImage(
254
- path.join(__dirname, "../../../../apps/example/src/assets/moon.png"),
255
- );
256
- const saturn = decodeImage(
257
- path.join(__dirname, "../../../../apps/example/src/assets/saturn.png"),
258
- );
259
- await page.evaluate(
260
- `
261
- (async () => {
262
- window.gpu = navigator.gpu;
263
- if (!gpu) {
264
- const canvas = document.createElement('canvas');
265
- const gl = canvas.getContext('webgl');
266
- throw new Error("WebGPU is not available. WebGL: " + !!(gl && gl instanceof WebGLRenderingContext));
267
- }
268
- window.adapter = await gpu.requestAdapter();
269
- if (!adapter) {
270
- throw new Error("No adapter");
271
- }
272
- window.RNWebGPU = {
273
- DecodeToUTF8: (data) => {
274
- return new TextDecoder().decode(data);
275
- }
276
- };
277
- window.device = await adapter.requestDevice();
278
- window.cubeVertexArray = new Float32Array(${JSON.stringify(Array.from(cubeVertexArray))});
279
- window.triangleVertWGSL = \`${triangleVertWGSL}\`;
280
- window.redFragWGSL = \`${redFragWGSL}\`;
281
- const rawDi3D = ${JSON.stringify(di3D)};
282
- window.di3D = new ImageData(
283
- new Uint8ClampedArray(rawDi3D.data),
284
- rawDi3D.width,
285
- rawDi3D.height
286
- );
287
- const rawMoon = ${JSON.stringify(moon)};
288
- window.moon = new ImageData(
289
- new Uint8ClampedArray(rawMoon.data),
290
- rawMoon.width,
291
- rawMoon.height
292
- );
293
- const rawSaturn = ${JSON.stringify(saturn)};
294
- window.saturn = new ImageData(
295
- new Uint8ClampedArray(rawSaturn.data),
296
- rawSaturn.width,
297
- rawSaturn.height
298
- );
299
- })();
300
- `,
301
- );
302
- this.browser = browser;
303
- this.page = page;
304
- }
305
- async dispose() {
306
- if (this.browser && !DEBUG) {
307
- this.browser.close();
308
- }
309
- }
310
- }
311
-
312
- interface BitmapData {
313
- data: number[];
314
- width: number;
315
- height: number;
316
- format: string;
317
- }
318
-
319
- export const encodeImage = (bitmap: BitmapData) => {
320
- const { width, height, format } = bitmap;
321
- let data = new Uint8Array(bitmap.data);
322
- // Convert BGRA to RGBA if necessary
323
- if (format === "bgra8unorm") {
324
- data = new Uint8Array(bitmap.data.length);
325
- for (let i = 0; i < bitmap.data.length; i += 4) {
326
- data[i] = bitmap.data[i + 2]; // R
327
- data[i + 1] = bitmap.data[i + 1]; // G
328
- data[i + 2] = bitmap.data[i]; // B
329
- data[i + 3] = bitmap.data[i + 3]; // A
330
- }
331
- } else if (format !== "rgba8unorm") {
332
- throw new Error(`Unsupported format ${format}`);
333
- }
334
- // Create a new PNG
335
- const png = new PNG({
336
- width: width,
337
- height: height,
338
- filterType: -1,
339
- });
340
- png.data = Buffer.from(data);
341
- return png;
342
- };
343
-
344
- interface CheckImageOptions {
345
- maxPixelDiff?: number;
346
- threshold?: number;
347
- overwrite?: boolean;
348
- mute?: boolean;
349
- shouldFail?: boolean;
350
- }
351
-
352
- // On Github Action, the image decoding is slightly different
353
- // all tests that show the oslo.jpg have small differences but look ok
354
- const defaultCheckImageOptions = {
355
- maxPixelDiff: 200,
356
- threshold: 0.1,
357
- overwrite: false,
358
- mute: false,
359
- shouldFail: false,
360
- };
361
-
362
- export const checkImage = (
363
- toTest: PNG,
364
- relPath: string,
365
- opts?: CheckImageOptions,
366
- ) => {
367
- const options = { ...defaultCheckImageOptions, ...opts };
368
- const { overwrite, threshold, mute, maxPixelDiff, shouldFail } = options;
369
- const p = path.resolve(__dirname, relPath);
370
- if (fs.existsSync(p) && !overwrite) {
371
- const ref = fs.readFileSync(p);
372
- const baseline = PNG.sync.read(ref);
373
- const diffImage = new PNG({
374
- width: baseline.width,
375
- height: baseline.height,
376
- });
377
- if (baseline.width !== toTest.width || baseline.height !== toTest.height) {
378
- throw new Error(
379
- `Image sizes don't match: ${baseline.width}x${baseline.height} vs ${toTest.width}x${toTest.height}`,
380
- );
381
- }
382
- const diffPixelsCount = pixelmatch(
383
- baseline.data,
384
- toTest.data,
385
- diffImage.data,
386
- baseline.width,
387
- baseline.height,
388
- { threshold },
389
- );
390
- if (!mute) {
391
- if (diffPixelsCount > maxPixelDiff && !shouldFail) {
392
- console.log(`${p} didn't match`);
393
- fs.writeFileSync(`${p}.test.png`, PNG.sync.write(toTest));
394
- fs.writeFileSync(`${p}-diff-test.png`, PNG.sync.write(diffImage));
395
- }
396
- if (shouldFail) {
397
- expect(diffPixelsCount).not.toBeLessThanOrEqual(maxPixelDiff);
398
- } else {
399
- expect(diffPixelsCount).toBeLessThanOrEqual(maxPixelDiff);
400
- }
401
- }
402
- return diffPixelsCount;
403
- } else {
404
- const buffer = PNG.sync.write(toTest);
405
- fs.writeFileSync(p, buffer);
406
- }
407
- return 0;
408
- };
409
-
410
- export const decodeImage = (relPath: string): BitmapData => {
411
- const p = path.resolve(__dirname, relPath);
412
- const data = fs.readFileSync(p);
413
- const png = PNG.sync.read(data);
414
-
415
- const bitmap: BitmapData = {
416
- data: Array.from(png.data),
417
- width: png.width,
418
- height: png.height,
419
- format: "rgba8unorm",
420
- };
421
-
422
- return bitmap;
423
- };
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file