react-native-wgpu 0.1.7 → 0.1.9

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 (93) hide show
  1. package/android/cpp/cpp-adapter.cpp +13 -35
  2. package/android/src/main/java/com/webgpu/WebGPUModule.java +0 -37
  3. package/android/src/main/java/com/webgpu/WebGPUView.java +0 -1
  4. package/android/src/main/java/com/webgpu/WebGPUViewPackage.java +1 -1
  5. package/cpp/rnwgpu/RNWebGPUManager.h +2 -2
  6. package/cpp/rnwgpu/SurfaceRegistry.h +148 -13
  7. package/cpp/rnwgpu/api/Canvas.h +15 -15
  8. package/cpp/rnwgpu/api/GPUCanvasContext.cpp +81 -23
  9. package/cpp/rnwgpu/api/GPUCanvasContext.h +16 -7
  10. package/cpp/rnwgpu/api/GPUDevice.cpp +6 -3
  11. package/cpp/rnwgpu/api/OffscreenSurface.h +49 -0
  12. package/cpp/rnwgpu/api/RNWebGPU.h +21 -10
  13. package/ios/MetalView.mm +11 -0
  14. package/ios/SurfaceUtils.h +2 -0
  15. package/ios/SurfaceUtils.mm +16 -4
  16. package/ios/WebGPUModule.mm +8 -25
  17. package/ios/WebGPUView.mm +9 -6
  18. package/lib/commonjs/Canvas.js +79 -18
  19. package/lib/commonjs/Canvas.js.map +1 -1
  20. package/lib/commonjs/Offscreen.js +124 -0
  21. package/lib/commonjs/Offscreen.js.map +1 -0
  22. package/lib/commonjs/index.js +12 -0
  23. package/lib/commonjs/index.js.map +1 -1
  24. package/lib/commonjs/utils.js +26 -15
  25. package/lib/commonjs/utils.js.map +1 -1
  26. package/lib/module/Canvas.js +80 -19
  27. package/lib/module/Canvas.js.map +1 -1
  28. package/lib/module/Offscreen.js +117 -0
  29. package/lib/module/Offscreen.js.map +1 -0
  30. package/lib/module/index.js +1 -0
  31. package/lib/module/index.js.map +1 -1
  32. package/lib/module/utils.js +25 -15
  33. package/lib/module/utils.js.map +1 -1
  34. package/lib/typescript/lib/commonjs/Offscreen.d.ts +32 -0
  35. package/lib/typescript/lib/commonjs/Offscreen.d.ts.map +1 -0
  36. package/lib/typescript/lib/commonjs/utils.d.ts +5 -1
  37. package/lib/typescript/lib/commonjs/utils.d.ts.map +1 -1
  38. package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
  39. package/lib/typescript/lib/module/Offscreen.d.ts +31 -0
  40. package/lib/typescript/lib/module/Offscreen.d.ts.map +1 -0
  41. package/lib/typescript/lib/module/index.d.ts +1 -0
  42. package/lib/typescript/lib/module/utils.d.ts +5 -1
  43. package/lib/typescript/lib/module/utils.d.ts.map +1 -1
  44. package/lib/typescript/src/Canvas.d.ts +6 -4
  45. package/lib/typescript/src/Canvas.d.ts.map +1 -1
  46. package/lib/typescript/src/Offscreen.d.ts +41 -0
  47. package/lib/typescript/src/Offscreen.d.ts.map +1 -0
  48. package/lib/typescript/src/__tests__/setup.d.ts +5 -4
  49. package/lib/typescript/src/__tests__/setup.d.ts.map +1 -1
  50. package/lib/typescript/src/index.d.ts +1 -0
  51. package/lib/typescript/src/index.d.ts.map +1 -1
  52. package/lib/typescript/src/utils.d.ts +6 -3
  53. package/lib/typescript/src/utils.d.ts.map +1 -1
  54. package/libs/android/arm64-v8a/libwebgpu_dawn.so +0 -0
  55. package/libs/android/armeabi-v7a/libwebgpu_dawn.so +0 -0
  56. package/libs/android/x86/libwebgpu_dawn.so +0 -0
  57. package/libs/android/x86_64/libwebgpu_dawn.so +0 -0
  58. package/libs/apple/arm64_iphoneos/libwebgpu_dawn.a +0 -0
  59. package/libs/apple/arm64_iphonesimulator/libwebgpu_dawn.a +0 -0
  60. package/libs/apple/arm64_xros/libwebgpu_dawn.a +0 -0
  61. package/libs/apple/arm64_xrsimulator/libwebgpu_dawn.a +0 -0
  62. package/libs/apple/{libwebgpu_dawn.a → iphonesimulator/libwebgpu_dawn.a} +0 -0
  63. package/libs/apple/libwebgpu_dawn.xcframework/Info.plist +45 -0
  64. package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/libwebgpu_dawn.a +0 -0
  65. package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/libwebgpu_dawn.a +0 -0
  66. package/libs/apple/{libwebgpu_dawn_macosx.xcframework → libwebgpu_dawn.xcframework}/macos-arm64_x86_64/libwebgpu_dawn.a +0 -0
  67. package/libs/apple/{libwebgpu_dawn_visionos.xcframework → libwebgpu_dawn.xcframework}/xros-arm64/libwebgpu_dawn.a +0 -0
  68. package/libs/apple/{x86_64_xrsimulator → libwebgpu_dawn.xcframework/xros-arm64-simulator}/libwebgpu_dawn.a +0 -0
  69. package/libs/apple/universal_macosx/libwebgpu_dawn.a +0 -0
  70. package/libs/apple/x86_64_iphonesimulator/libwebgpu_dawn.a +0 -0
  71. package/package.json +1 -1
  72. package/react-native-wgpu.podspec +2 -8
  73. package/src/Canvas.tsx +101 -37
  74. package/src/Offscreen.ts +164 -0
  75. package/src/__tests__/ExternalTexture.spec.ts +4 -4
  76. package/src/__tests__/Texture.spec.ts +8 -2
  77. package/src/__tests__/demos/ABuffer.spec.ts +17 -12
  78. package/src/__tests__/demos/Blur.spec.ts +2 -1
  79. package/src/__tests__/demos/Cube.spec.ts +16 -12
  80. package/src/__tests__/demos/FractalCube.spec.ts +6 -5
  81. package/src/__tests__/demos/OcclusionQuery.spec.ts +3 -3
  82. package/src/__tests__/demos/RenderBundles.spec.ts +4 -3
  83. package/src/__tests__/demos/Triangle.spec.ts +27 -9
  84. package/src/__tests__/setup.ts +25 -10
  85. package/src/index.tsx +1 -0
  86. package/src/utils.ts +28 -18
  87. package/lib/typescript/src/__tests__/components/DrawingContext.d.ts +0 -12
  88. package/lib/typescript/src/__tests__/components/DrawingContext.d.ts.map +0 -1
  89. package/libs/apple/libwebgpu_dawn_macosx.xcframework/Info.plist +0 -28
  90. package/libs/apple/libwebgpu_dawn_visionos.a +0 -0
  91. package/libs/apple/libwebgpu_dawn_visionos.xcframework/Info.plist +0 -44
  92. package/libs/apple/libwebgpu_dawn_visionos.xcframework/xros-arm64_x86_64-simulator/libwebgpu_dawn_visionos.a +0 -0
  93. package/src/__tests__/components/DrawingContext.ts +0 -11
@@ -84,6 +84,7 @@ describe("Cube", () => {
84
84
  vec3,
85
85
  basicVertWGSL,
86
86
  vertexPositionColorWGSL,
87
+ canvas,
87
88
  }) => {
88
89
  const cubeVertexSize = 4 * 10; // Byte size of one cube vertex.
89
90
  const cubePositionOffset = 0;
@@ -156,7 +157,7 @@ describe("Cube", () => {
156
157
  });
157
158
 
158
159
  const depthTexture = device.createTexture({
159
- size: [ctx.width, ctx.height],
160
+ size: [ctx.canvas.width, ctx.canvas.height],
160
161
  format: "depth24plus",
161
162
  usage: GPUTextureUsage.RENDER_ATTACHMENT,
162
163
  });
@@ -196,7 +197,7 @@ describe("Cube", () => {
196
197
  },
197
198
  };
198
199
 
199
- const aspect = ctx.width / ctx.height;
200
+ const aspect = ctx.canvas.width / ctx.canvas.height;
200
201
  const projectionMatrix = mat4.perspective(
201
202
  (2 * Math.PI) / 5,
202
203
  aspect,
@@ -243,7 +244,7 @@ describe("Cube", () => {
243
244
  passEncoder.draw(cubeVertexCount);
244
245
  passEncoder.end();
245
246
  device.queue.submit([commandEncoder.finish()]);
246
- return ctx.getImageData();
247
+ return canvas.getImageData();
247
248
  },
248
249
  {
249
250
  basicVertWGSL: basicVert,
@@ -264,6 +265,7 @@ describe("Cube", () => {
264
265
  vec3,
265
266
  basicVertWGSL,
266
267
  vertexPositionColorWGSL,
268
+ canvas,
267
269
  }) => {
268
270
  const cubeVertexSize = 4 * 10; // Byte size of one cube vertex.
269
271
  const cubePositionOffset = 0;
@@ -334,7 +336,7 @@ describe("Cube", () => {
334
336
  });
335
337
 
336
338
  const depthTexture = device.createTexture({
337
- size: [ctx.width, ctx.height],
339
+ size: [ctx.canvas.width, ctx.canvas.height],
338
340
  format: "depth24plus",
339
341
  usage: GPUTextureUsage.RENDER_ATTACHMENT,
340
342
  });
@@ -395,7 +397,7 @@ describe("Cube", () => {
395
397
  },
396
398
  };
397
399
 
398
- const aspect = ctx.width / ctx.height;
400
+ const aspect = ctx.canvas.width / ctx.canvas.height;
399
401
  const projectionMatrix = mat4.perspective(
400
402
  (2 * Math.PI) / 5,
401
403
  aspect,
@@ -475,7 +477,7 @@ describe("Cube", () => {
475
477
  passEncoder.end();
476
478
  device.queue.submit([commandEncoder.finish()]);
477
479
 
478
- return ctx.getImageData();
480
+ return canvas.getImageData();
479
481
  },
480
482
  {
481
483
  basicVertWGSL: basicVert,
@@ -496,6 +498,7 @@ describe("Cube", () => {
496
498
  vec3,
497
499
  basicVertWGSL,
498
500
  sampleTextureMixColorWGSL,
501
+ canvas,
499
502
  }) => {
500
503
  const cubeVertexSize = 4 * 10; // Byte size of one cube vertex.
501
504
  const cubePositionOffset = 0;
@@ -567,7 +570,7 @@ describe("Cube", () => {
567
570
  });
568
571
 
569
572
  const depthTexture = device.createTexture({
570
- size: [ctx.width, ctx.height],
573
+ size: [ctx.canvas.width, ctx.canvas.height],
571
574
  format: "depth24plus",
572
575
  usage: GPUTextureUsage.RENDER_ATTACHMENT,
573
576
  });
@@ -638,7 +641,7 @@ describe("Cube", () => {
638
641
  },
639
642
  };
640
643
 
641
- const aspect = ctx.width / ctx.height;
644
+ const aspect = ctx.canvas.width / ctx.canvas.height;
642
645
  const projectionMatrix = mat4.perspective(
643
646
  (2 * Math.PI) / 5,
644
647
  aspect,
@@ -686,7 +689,7 @@ describe("Cube", () => {
686
689
  passEncoder.end();
687
690
  device.queue.submit([commandEncoder.finish()]);
688
691
 
689
- return ctx.getImageData();
692
+ return canvas.getImageData();
690
693
  },
691
694
  {
692
695
  basicVertWGSL: basicVert,
@@ -708,6 +711,7 @@ describe("Cube", () => {
708
711
  vec3,
709
712
  vertexPositionColorWGSL,
710
713
  instancedVertWGSL,
714
+ canvas,
711
715
  }) => {
712
716
  const cubeVertexSize = 4 * 10; // Byte size of one cube vertex.
713
717
  const cubePositionOffset = 0;
@@ -778,7 +782,7 @@ describe("Cube", () => {
778
782
  });
779
783
 
780
784
  const depthTexture = device.createTexture({
781
- size: [ctx.width, ctx.height],
785
+ size: [ctx.canvas.width, ctx.canvas.height],
782
786
  format: "depth24plus",
783
787
  usage: GPUTextureUsage.RENDER_ATTACHMENT,
784
788
  });
@@ -809,7 +813,7 @@ describe("Cube", () => {
809
813
  ],
810
814
  });
811
815
 
812
- const aspect = ctx.width / ctx.height;
816
+ const aspect = ctx.canvas.width / ctx.canvas.height;
813
817
  const projectionMatrix = mat4.perspective(
814
818
  (2 * Math.PI) / 5,
815
819
  aspect,
@@ -912,7 +916,7 @@ describe("Cube", () => {
912
916
  passEncoder.draw(cubeVertexCount, numInstances, 0, 0);
913
917
  passEncoder.end();
914
918
  device.queue.submit([commandEncoder.finish()]);
915
- return ctx.getImageData();
919
+ return canvas.getImageData();
916
920
  },
917
921
  {
918
922
  vertexPositionColorWGSL: vertexPositionColor,
@@ -28,6 +28,7 @@ describe("Fractal Cube", () => {
28
28
  vec3,
29
29
  sampleSelfWGSL,
30
30
  basicVertWGSL,
31
+ canvas,
31
32
  }) => {
32
33
  const cubeVertexSize = 4 * 10; // Byte size of one cube vertex.
33
34
  const cubePositionOffset = 0;
@@ -98,7 +99,7 @@ describe("Fractal Cube", () => {
98
99
  });
99
100
 
100
101
  const depthTexture = device.createTexture({
101
- size: [ctx.width, ctx.height],
102
+ size: [ctx.canvas.width, ctx.canvas.height],
102
103
  format: "depth24plus",
103
104
  usage: GPUTextureUsage.RENDER_ATTACHMENT,
104
105
  });
@@ -112,7 +113,7 @@ describe("Fractal Cube", () => {
112
113
  // We will copy the frame's rendering results into this texture and
113
114
  // sample it on the next frame.
114
115
  const cubeTexture = device.createTexture({
115
- size: [ctx.width, ctx.height],
116
+ size: [ctx.canvas.width, ctx.canvas.height],
116
117
  format: presentationFormat,
117
118
  usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
118
119
  });
@@ -163,7 +164,7 @@ describe("Fractal Cube", () => {
163
164
  },
164
165
  };
165
166
 
166
- const aspect = ctx.width / ctx.height;
167
+ const aspect = ctx.canvas.width / ctx.canvas.height;
167
168
  const projectionMatrix = mat4.perspective(
168
169
  (2 * Math.PI) / 5,
169
170
  aspect,
@@ -217,7 +218,7 @@ describe("Fractal Cube", () => {
217
218
  {
218
219
  texture: cubeTexture,
219
220
  },
220
- [ctx.width, ctx.height],
221
+ [ctx.canvas.width, ctx.canvas.height],
221
222
  );
222
223
 
223
224
  device.queue.submit([commandEncoder.finish()]);
@@ -226,7 +227,7 @@ describe("Fractal Cube", () => {
226
227
  for (let i = 0; i < 10; i++) {
227
228
  frame(now + i * 16);
228
229
  }
229
- return ctx.getImageData();
230
+ return canvas.getImageData();
230
231
  },
231
232
  {
232
233
  basicVertWGSL: basicVert,
@@ -56,7 +56,7 @@ export type TypedArrayConstructor =
56
56
  describe("OcclusionQuery", () => {
57
57
  it("occlusionQuery", async () => {
58
58
  const result = await client.eval(
59
- ({ device, ctx, gpu, solidColorLitWGSL, mat4 }) => {
59
+ ({ device, ctx, gpu, solidColorLitWGSL, mat4, canvas }) => {
60
60
  const depthFormat = "depth24plus";
61
61
  const presentationFormat = gpu.getPreferredCanvasFormat();
62
62
  const animate = true;
@@ -262,7 +262,7 @@ describe("OcclusionQuery", () => {
262
262
 
263
263
  const projection = mat4.perspective(
264
264
  (30 * Math.PI) / 180,
265
- ctx.width / ctx.height,
265
+ ctx.canvas.width / ctx.canvas.height,
266
266
  0.5,
267
267
  100,
268
268
  );
@@ -365,7 +365,7 @@ describe("OcclusionQuery", () => {
365
365
  }
366
366
  }
367
367
  render(1721766068905 + 150 * 16);
368
- return ctx.getImageData().then((image) => ({ image, visible }));
368
+ return canvas.getImageData().then((image) => ({ image, visible }));
369
369
  },
370
370
  { solidColorLitWGSL: solidColorLit },
371
371
  );
@@ -75,6 +75,7 @@ describe("Render Bundles", () => {
75
75
  vec3,
76
76
  assets: { saturn, moon },
77
77
  vals,
78
+ canvas,
78
79
  }) => {
79
80
  interface SphereMesh {
80
81
  vertices: Float32Array;
@@ -255,7 +256,7 @@ describe("Render Bundles", () => {
255
256
  });
256
257
 
257
258
  const depthTexture = device.createTexture({
258
- size: [ctx.width, ctx.height],
259
+ size: [ctx.canvas.width, ctx.canvas.height],
259
260
  format: "depth24plus",
260
261
  usage: GPUTextureUsage.RENDER_ATTACHMENT,
261
262
  });
@@ -447,7 +448,7 @@ describe("Render Bundles", () => {
447
448
  },
448
449
  };
449
450
 
450
- const aspect = ctx.width / ctx.height;
451
+ const aspect = ctx.canvas.width / ctx.canvas.height;
451
452
  const projectionMatrix = mat4.perspective(
452
453
  (2 * Math.PI) / 5,
453
454
  aspect,
@@ -569,7 +570,7 @@ describe("Render Bundles", () => {
569
570
  device.queue.submit([commandEncoder.finish()]);
570
571
  }
571
572
  frame();
572
- return ctx.getImageData();
573
+ return canvas.getImageData();
573
574
  },
574
575
  { meshWGSL: mesh, vals: randomValues },
575
576
  );
@@ -3,7 +3,13 @@ import { checkImage, client, encodeImage } from "../setup";
3
3
  describe("Triangle", () => {
4
4
  it("Simple Triangle", async () => {
5
5
  const result = await client.eval(
6
- ({ device, shaders: { triangleVertWGSL, redFragWGSL }, gpu, ctx }) => {
6
+ ({
7
+ device,
8
+ shaders: { triangleVertWGSL, redFragWGSL },
9
+ gpu,
10
+ ctx,
11
+ canvas,
12
+ }) => {
7
13
  const pipeline = device.createRenderPipeline({
8
14
  layout: "auto",
9
15
  vertex: {
@@ -46,7 +52,7 @@ describe("Triangle", () => {
46
52
  passEncoder.draw(3);
47
53
  passEncoder.end();
48
54
  device.queue.submit([commandEncoder.finish()]);
49
- return ctx.getImageData();
55
+ return canvas.getImageData();
50
56
  },
51
57
  );
52
58
  const image = encodeImage(result);
@@ -54,7 +60,13 @@ describe("Triangle", () => {
54
60
  });
55
61
  it("Async Simple Triangle", async () => {
56
62
  const result = await client.eval(
57
- ({ device, shaders: { triangleVertWGSL, redFragWGSL }, gpu, ctx }) => {
63
+ ({
64
+ device,
65
+ shaders: { triangleVertWGSL, redFragWGSL },
66
+ gpu,
67
+ ctx,
68
+ canvas,
69
+ }) => {
58
70
  return device
59
71
  .createRenderPipelineAsync({
60
72
  layout: "auto",
@@ -98,7 +110,7 @@ describe("Triangle", () => {
98
110
  passEncoder.draw(3);
99
111
  passEncoder.end();
100
112
  device.queue.submit([commandEncoder.finish()]);
101
- return ctx.getImageData();
113
+ return canvas.getImageData();
102
114
  });
103
115
  },
104
116
  );
@@ -107,7 +119,13 @@ describe("Triangle", () => {
107
119
  });
108
120
  it("Triangle MSAA", async () => {
109
121
  const result = await client.eval(
110
- ({ device, shaders: { triangleVertWGSL, redFragWGSL }, gpu, ctx }) => {
122
+ ({
123
+ device,
124
+ shaders: { triangleVertWGSL, redFragWGSL },
125
+ gpu,
126
+ ctx,
127
+ canvas,
128
+ }) => {
111
129
  const sampleCount = 4;
112
130
  const presentationFormat = gpu.getPreferredCanvasFormat();
113
131
  const pipeline = device.createRenderPipeline({
@@ -136,7 +154,7 @@ describe("Triangle", () => {
136
154
  });
137
155
 
138
156
  const texture = device.createTexture({
139
- size: [ctx.width, ctx.height],
157
+ size: [ctx.canvas.width, ctx.canvas.height],
140
158
  sampleCount,
141
159
  format: presentationFormat,
142
160
  usage: GPUTextureUsage.RENDER_ATTACHMENT,
@@ -163,7 +181,7 @@ describe("Triangle", () => {
163
181
  passEncoder.draw(3);
164
182
  passEncoder.end();
165
183
  device.queue.submit([commandEncoder.finish()]);
166
- return ctx.getImageData();
184
+ return canvas.getImageData();
167
185
  },
168
186
  );
169
187
  const image = encodeImage(result);
@@ -171,7 +189,7 @@ describe("Triangle", () => {
171
189
  });
172
190
  it("Triangle with constants", async () => {
173
191
  const result = await client.eval(
174
- ({ device, shaders: { triangleVertWGSL }, gpu, ctx }) => {
192
+ ({ device, shaders: { triangleVertWGSL }, gpu, ctx, canvas }) => {
175
193
  const pipeline = device.createRenderPipeline({
176
194
  layout: "auto",
177
195
  vertex: {
@@ -239,7 +257,7 @@ describe("Triangle", () => {
239
257
  passEncoder.draw(3);
240
258
  passEncoder.end();
241
259
  device.queue.submit([commandEncoder.finish()]);
242
- return ctx.getImageData();
260
+ return canvas.getImageData();
243
261
  },
244
262
  );
245
263
  const image = encodeImage(result);
@@ -11,7 +11,8 @@ import type { mat4, vec3, mat3 } from "wgpu-matrix";
11
11
  import type { Server, WebSocket } from "ws";
12
12
  import type { Browser, Page } from "puppeteer";
13
13
 
14
- import type { DrawingContext } from "./components/DrawingContext";
14
+ import type { GPUOffscreenCanvas } from "../Offscreen";
15
+
15
16
  import { cubeVertexArray } from "./components/cube";
16
17
  import { redFragWGSL, triangleVertWGSL } from "./components/triangle";
17
18
  import { DEBUG, REFERENCE } from "./config";
@@ -26,7 +27,7 @@ declare global {
26
27
  var testOS: TestOS;
27
28
  }
28
29
 
29
- interface GPUContext {
30
+ interface GPUTestingContext {
30
31
  gpu: GPU;
31
32
  adapter: GPUAdapter;
32
33
  device: GPUDevice;
@@ -43,7 +44,8 @@ interface GPUContext {
43
44
  moon: ImageData;
44
45
  saturn: ImageData;
45
46
  };
46
- ctx: DrawingContext;
47
+ ctx: GPUCanvasContext;
48
+ canvas: GPUOffscreenCanvas;
47
49
  mat4: typeof mat4;
48
50
  vec3: typeof vec3;
49
51
  mat3: typeof mat3;
@@ -61,7 +63,7 @@ type JSONValue =
61
63
 
62
64
  interface TestingClient {
63
65
  eval<C = Ctx, R = JSONValue>(
64
- fn: (ctx: GPUContext & C) => R | Promise<R>,
66
+ fn: (ctx: GPUTestingContext & C) => R | Promise<R>,
65
67
  ctx?: C,
66
68
  ): Promise<R>;
67
69
  OS: TestOS;
@@ -86,7 +88,7 @@ class RemoteTestingClient implements TestingClient {
86
88
  readonly arch = global.testArch;
87
89
 
88
90
  eval<C = Ctx, R = JSONValue>(
89
- fn: (ctx: GPUContext & C) => R | Promise<R>,
91
+ fn: (ctx: GPUTestingContext & C) => R | Promise<R>,
90
92
  context?: C,
91
93
  ): Promise<R> {
92
94
  const ctx = this.prepareContext(context ?? {});
@@ -131,7 +133,7 @@ class ReferenceTestingClient implements TestingClient {
131
133
  private page: Page | null = null;
132
134
 
133
135
  async eval<C = Ctx, R = JSONValue>(
134
- fn: (ctx: GPUContext & C) => R | Promise<R>,
136
+ fn: (ctx: GPUTestingContext & C) => R | Promise<R>,
135
137
  ctx?: C,
136
138
  ): Promise<R> {
137
139
  if (!this.page) {
@@ -139,7 +141,7 @@ class ReferenceTestingClient implements TestingClient {
139
141
  }
140
142
  const fTexturePath = path.join(
141
143
  __dirname,
142
- "../../../../apps/paper/src/assets/f.png",
144
+ "../../../../apps/example/src/assets/f.png",
143
145
  );
144
146
  const fTextureData = fs.readFileSync(fTexturePath);
145
147
  const fTextureBase64 = `data:image/png;base64,${fTextureData.toString("base64")}`;
@@ -168,6 +170,14 @@ class ReferenceTestingClient implements TestingClient {
168
170
  getCurrentTexture() {
169
171
  return this.texture;
170
172
  }
173
+
174
+ get canvas() {
175
+ return {
176
+ width: this.width,
177
+ height: this.height,
178
+ };
179
+ }
180
+
171
181
  getImageData() {
172
182
  const commandEncoder = this.device.createCommandEncoder();
173
183
  const bytesPerRow = this.width * 4;
@@ -206,6 +216,11 @@ class ReferenceTestingClient implements TestingClient {
206
216
  redFragWGSL,
207
217
  },
208
218
  ctx,
219
+ canvas: {
220
+ getImageData: ctx.getImageData.bind(ctx),
221
+ width: ctx.width,
222
+ height: ctx.height,
223
+ },
209
224
  mat4,
210
225
  vec3,
211
226
  mat3,
@@ -234,13 +249,13 @@ class ReferenceTestingClient implements TestingClient {
234
249
  .catch((e) => console.log(e));
235
250
  await page.waitForNetworkIdle();
236
251
  const di3D = decodeImage(
237
- path.join(__dirname, "../../../../apps/paper/src/assets/Di-3d.png"),
252
+ path.join(__dirname, "../../../../apps/example/src/assets/Di-3d.png"),
238
253
  );
239
254
  const moon = decodeImage(
240
- path.join(__dirname, "../../../../apps/paper/src/assets/moon.png"),
255
+ path.join(__dirname, "../../../../apps/example/src/assets/moon.png"),
241
256
  );
242
257
  const saturn = decodeImage(
243
- path.join(__dirname, "../../../../apps/paper/src/assets/saturn.png"),
258
+ path.join(__dirname, "../../../../apps/example/src/assets/saturn.png"),
244
259
  );
245
260
  await page.evaluate(
246
261
  `
package/src/index.tsx CHANGED
@@ -2,6 +2,7 @@
2
2
  import WebGPUNativeModule from "./NativeWebGPUModule";
3
3
 
4
4
  export * from "./Canvas";
5
+ export * from "./Offscreen";
5
6
  export * from "./WebGPUViewNativeComponent";
6
7
  export * from "./utils";
7
8
  export { default as WebGPUModule } from "./NativeWebGPUModule";
package/src/utils.ts CHANGED
@@ -1,7 +1,6 @@
1
- import type { DependencyList } from "react";
2
- import { useEffect, useRef } from "react";
1
+ import { useEffect, useRef, useState } from "react";
3
2
 
4
- import type { CanvasRef } from "./Canvas";
3
+ import type { RNCanvasContext, CanvasRef } from "./Canvas";
5
4
 
6
5
  type Unsubscribe = () => void;
7
6
 
@@ -13,30 +12,41 @@ export const warnIfNotHardwareAccelerated = (adapter: GPUAdapter) => {
13
12
  }
14
13
  };
15
14
 
15
+ export const useGPUContext = () => {
16
+ const [context, setContext] = useState<RNCanvasContext | null>(null);
17
+ const ref = useCanvasEffect(() => {
18
+ const ctx = ref.current!.getContext("webgpu")!;
19
+ setContext(ctx);
20
+ });
21
+ return { ref, context };
22
+ };
23
+
16
24
  export const useCanvasEffect = (
17
- effect: () => void | Unsubscribe | Promise<void | Unsubscribe>,
18
- deps: DependencyList = [],
25
+ effect: () =>
26
+ | void
27
+ | Unsubscribe
28
+ | Promise<Unsubscribe | void>
29
+ | Promise<void>,
19
30
  ) => {
31
+ const unsub = useRef<Unsubscribe | null | Promise<Unsubscribe | void>>(null);
20
32
  const ref = useRef<CanvasRef>(null);
21
- const unsubscribe = useRef<Unsubscribe>();
22
33
  useEffect(() => {
23
- requestAnimationFrame(async () => {
24
- // const adapter = await navigator.gpu.requestAdapter();
25
- // if (!adapter) {
26
- // return;
27
- // }
28
- // const device = await adapter.requestDevice();
29
- const unsub = await effect();
30
- if (unsub) {
31
- unsubscribe.current = unsub;
34
+ ref.current!.whenReady(async () => {
35
+ const sub = effect();
36
+ if (sub) {
37
+ unsub.current = sub;
32
38
  }
33
39
  });
34
40
  return () => {
35
- if (unsubscribe.current) {
36
- unsubscribe.current();
41
+ if (unsub.current) {
42
+ if (unsub.current instanceof Promise) {
43
+ unsub.current.then((sub) => sub && sub());
44
+ } else {
45
+ unsub.current();
46
+ }
37
47
  }
38
48
  };
39
49
  // eslint-disable-next-line react-hooks/exhaustive-deps
40
- }, deps);
50
+ }, []);
41
51
  return ref;
42
52
  };
@@ -1,12 +0,0 @@
1
- export interface DrawingContext {
2
- width: number;
3
- height: number;
4
- getCurrentTexture(): GPUTexture;
5
- getImageData(): Promise<{
6
- data: number[];
7
- width: number;
8
- height: number;
9
- format: string;
10
- }>;
11
- }
12
- //# sourceMappingURL=DrawingContext.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"DrawingContext.d.ts","sourceRoot":"","sources":["../../../../../src/__tests__/components/DrawingContext.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,iBAAiB,IAAI,UAAU,CAAC;IAChC,YAAY,IAAI,OAAO,CAAC;QACtB,IAAI,EAAE,MAAM,EAAE,CAAC;QACf,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC,CAAC;CACJ"}
@@ -1,28 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
- <plist version="1.0">
4
- <dict>
5
- <key>AvailableLibraries</key>
6
- <array>
7
- <dict>
8
- <key>BinaryPath</key>
9
- <string>libwebgpu_dawn.a</string>
10
- <key>LibraryIdentifier</key>
11
- <string>macos-arm64_x86_64</string>
12
- <key>LibraryPath</key>
13
- <string>libwebgpu_dawn.a</string>
14
- <key>SupportedArchitectures</key>
15
- <array>
16
- <string>arm64</string>
17
- <string>x86_64</string>
18
- </array>
19
- <key>SupportedPlatform</key>
20
- <string>macos</string>
21
- </dict>
22
- </array>
23
- <key>CFBundlePackageType</key>
24
- <string>XFWK</string>
25
- <key>XCFrameworkFormatVersion</key>
26
- <string>1.0</string>
27
- </dict>
28
- </plist>
@@ -1,44 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
- <plist version="1.0">
4
- <dict>
5
- <key>AvailableLibraries</key>
6
- <array>
7
- <dict>
8
- <key>BinaryPath</key>
9
- <string>libwebgpu_dawn.a</string>
10
- <key>LibraryIdentifier</key>
11
- <string>xros-arm64</string>
12
- <key>LibraryPath</key>
13
- <string>libwebgpu_dawn.a</string>
14
- <key>SupportedArchitectures</key>
15
- <array>
16
- <string>arm64</string>
17
- </array>
18
- <key>SupportedPlatform</key>
19
- <string>xros</string>
20
- </dict>
21
- <dict>
22
- <key>BinaryPath</key>
23
- <string>libwebgpu_dawn_visionos.a</string>
24
- <key>LibraryIdentifier</key>
25
- <string>xros-arm64_x86_64-simulator</string>
26
- <key>LibraryPath</key>
27
- <string>libwebgpu_dawn_visionos.a</string>
28
- <key>SupportedArchitectures</key>
29
- <array>
30
- <string>arm64</string>
31
- <string>x86_64</string>
32
- </array>
33
- <key>SupportedPlatform</key>
34
- <string>xros</string>
35
- <key>SupportedPlatformVariant</key>
36
- <string>simulator</string>
37
- </dict>
38
- </array>
39
- <key>CFBundlePackageType</key>
40
- <string>XFWK</string>
41
- <key>XCFrameworkFormatVersion</key>
42
- <string>1.0</string>
43
- </dict>
44
- </plist>
@@ -1,11 +0,0 @@
1
- export interface DrawingContext {
2
- width: number;
3
- height: number;
4
- getCurrentTexture(): GPUTexture;
5
- getImageData(): Promise<{
6
- data: number[];
7
- width: number;
8
- height: number;
9
- format: string;
10
- }>;
11
- }