react-native-wgpu 0.2.10 → 0.3.1

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 (258) hide show
  1. package/README.md +155 -69
  2. package/android/CMakeLists.txt +5 -7
  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/RNFHybridObject.cpp +8 -4
  18. package/cpp/jsi/RNFHybridObject.h +7 -2
  19. package/cpp/jsi/RNFJSIConverter.h +13 -102
  20. package/cpp/jsi/RNFJSIHelper.h +5 -3
  21. package/cpp/jsi/RNFRuntimeState.cpp +18 -0
  22. package/cpp/jsi/RNFRuntimeState.h +106 -0
  23. package/cpp/rnwgpu/RNWebGPUManager.cpp +1 -9
  24. package/cpp/rnwgpu/api/GPU.cpp +51 -26
  25. package/cpp/rnwgpu/api/GPU.h +5 -18
  26. package/cpp/rnwgpu/api/GPUAdapter.cpp +79 -54
  27. package/cpp/rnwgpu/api/GPUAdapter.h +6 -6
  28. package/cpp/rnwgpu/api/GPUAdapterInfo.h +0 -1
  29. package/cpp/rnwgpu/api/GPUBindGroup.h +1 -3
  30. package/cpp/rnwgpu/api/GPUBindGroupLayout.h +1 -3
  31. package/cpp/rnwgpu/api/GPUBuffer.cpp +35 -32
  32. package/cpp/rnwgpu/api/GPUBuffer.h +9 -7
  33. package/cpp/rnwgpu/api/GPUCanvasContext.cpp +5 -1
  34. package/cpp/rnwgpu/api/GPUCanvasContext.h +0 -2
  35. package/cpp/rnwgpu/api/GPUCommandBuffer.h +1 -3
  36. package/cpp/rnwgpu/api/GPUCommandEncoder.h +1 -3
  37. package/cpp/rnwgpu/api/GPUCompilationInfo.h +0 -2
  38. package/cpp/rnwgpu/api/GPUCompilationMessage.h +1 -3
  39. package/cpp/rnwgpu/api/GPUComputePassEncoder.h +1 -3
  40. package/cpp/rnwgpu/api/GPUComputePipeline.h +1 -3
  41. package/cpp/rnwgpu/api/GPUDevice.cpp +183 -128
  42. package/cpp/rnwgpu/api/GPUDevice.h +22 -21
  43. package/cpp/rnwgpu/api/GPUDeviceLostInfo.h +1 -3
  44. package/cpp/rnwgpu/api/GPUExternalTexture.h +1 -3
  45. package/cpp/rnwgpu/api/GPUPipelineLayout.h +1 -3
  46. package/cpp/rnwgpu/api/GPUQuerySet.h +1 -3
  47. package/cpp/rnwgpu/api/GPUQueue.cpp +19 -8
  48. package/cpp/rnwgpu/api/GPUQueue.h +7 -6
  49. package/cpp/rnwgpu/api/GPURenderBundle.h +1 -3
  50. package/cpp/rnwgpu/api/GPURenderBundleEncoder.h +1 -3
  51. package/cpp/rnwgpu/api/GPURenderPassEncoder.h +1 -3
  52. package/cpp/rnwgpu/api/GPURenderPipeline.h +1 -3
  53. package/cpp/rnwgpu/api/GPUSampler.h +1 -3
  54. package/cpp/rnwgpu/api/GPUShaderModule.cpp +42 -28
  55. package/cpp/rnwgpu/api/GPUShaderModule.h +6 -6
  56. package/cpp/rnwgpu/api/GPUSupportedLimits.h +1 -3
  57. package/cpp/rnwgpu/api/GPUTexture.h +1 -3
  58. package/cpp/rnwgpu/api/GPUTextureView.h +1 -3
  59. package/cpp/rnwgpu/api/RNWebGPU.h +1 -7
  60. package/cpp/rnwgpu/async/AsyncDispatcher.h +28 -0
  61. package/cpp/rnwgpu/async/AsyncRunner.cpp +215 -0
  62. package/cpp/rnwgpu/async/AsyncRunner.h +53 -0
  63. package/cpp/rnwgpu/async/AsyncTaskHandle.cpp +181 -0
  64. package/cpp/rnwgpu/async/AsyncTaskHandle.h +55 -0
  65. package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.cpp +23 -0
  66. package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.h +22 -0
  67. package/cpp/webgpu/webgpu.h +5 -4827
  68. package/cpp/webgpu/webgpu_cpp.h +5 -10140
  69. package/cpp/{dawn/native/WebGPUBackend.h → webgpu/webgpu_cpp_print.h} +4 -20
  70. package/lib/commonjs/Canvas.js +6 -66
  71. package/lib/commonjs/Canvas.js.map +1 -1
  72. package/lib/commonjs/hooks.js +6 -42
  73. package/lib/commonjs/hooks.js.map +1 -1
  74. package/lib/module/Canvas.js +7 -67
  75. package/lib/module/Canvas.js.map +1 -1
  76. package/lib/module/hooks.js +5 -40
  77. package/lib/module/hooks.js.map +1 -1
  78. package/lib/typescript/lib/commonjs/hooks.d.ts +1 -5
  79. package/lib/typescript/lib/commonjs/hooks.d.ts.map +1 -1
  80. package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
  81. package/lib/typescript/lib/module/hooks.d.ts +1 -5
  82. package/lib/typescript/lib/module/hooks.d.ts.map +1 -1
  83. package/lib/typescript/src/Canvas.d.ts +0 -1
  84. package/lib/typescript/src/Canvas.d.ts.map +1 -1
  85. package/lib/typescript/src/hooks.d.ts +2 -7
  86. package/lib/typescript/src/hooks.d.ts.map +1 -1
  87. package/libs/android/arm64-v8a/libwebgpu_dawn.so +0 -0
  88. package/libs/android/armeabi-v7a/libwebgpu_dawn.so +0 -0
  89. package/libs/android/x86/libwebgpu_dawn.so +0 -0
  90. package/libs/android/x86_64/libwebgpu_dawn.so +0 -0
  91. package/libs/apple/libwebgpu_dawn.xcframework/Info.plist +5 -35
  92. package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/libwebgpu_dawn.a +0 -0
  93. package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/libwebgpu_dawn.a +0 -0
  94. package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/libwebgpu_dawn.a +0 -0
  95. package/package.json +4 -3
  96. package/react-native-wgpu.podspec +12 -16
  97. package/src/Canvas.tsx +8 -69
  98. package/src/hooks.tsx +14 -48
  99. package/android/cpp/platform/ThreadUtils.cpp +0 -41
  100. package/android/src/oldarch/com/webgpu/NativeWebGPUModuleSpec.java +0 -23
  101. package/android/src/oldarch/com/webgpu/WebGPUViewManagerSpec.java +0 -12
  102. package/apple/WebGPUViewManager.mm +0 -24
  103. package/apple/platform/ThreadUtils.cpp +0 -34
  104. package/cpp/dawn/dawn_proc.h +0 -50
  105. package/cpp/dawn/dawn_thread_dispatch_proc.h +0 -47
  106. package/cpp/dawn/native/D3D11Backend.h +0 -77
  107. package/cpp/dawn/native/D3D12Backend.h +0 -68
  108. package/cpp/dawn/native/D3DBackend.h +0 -56
  109. package/cpp/dawn/native/MetalBackend.h +0 -56
  110. package/cpp/dawn/platform/DawnPlatform.h +0 -167
  111. package/cpp/dawn/platform/dawn_platform_export.h +0 -49
  112. package/cpp/jsi/RNFRuntimeCache.cpp +0 -57
  113. package/cpp/jsi/RNFRuntimeCache.h +0 -79
  114. package/cpp/jsi/RNFWorkletRuntimeCollector.h +0 -43
  115. package/cpp/jsi/RNFWorkletRuntimeRegistry.cpp +0 -12
  116. package/cpp/jsi/RNFWorkletRuntimeRegistry.h +0 -44
  117. package/cpp/platform/ThreadUtils.h +0 -30
  118. package/cpp/rnwgpu/api/AsyncRunner.h +0 -30
  119. package/cpp/threading/CallInvokerDispatcher.h +0 -37
  120. package/cpp/threading/Dispatcher.cpp +0 -55
  121. package/cpp/threading/Dispatcher.h +0 -93
  122. package/cpp/threading/ThreadPool.cpp +0 -88
  123. package/cpp/threading/ThreadPool.h +0 -53
  124. package/cpp/webgpu/webgpu_glfw.h +0 -88
  125. package/lib/typescript/src/__tests__/Alpha.spec.d.ts +0 -2
  126. package/lib/typescript/src/__tests__/Alpha.spec.d.ts.map +0 -1
  127. package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts +0 -2
  128. package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts.map +0 -1
  129. package/lib/typescript/src/__tests__/Buffer.spec.d.ts +0 -2
  130. package/lib/typescript/src/__tests__/Buffer.spec.d.ts.map +0 -1
  131. package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts +0 -2
  132. package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts.map +0 -1
  133. package/lib/typescript/src/__tests__/Constants.spec.d.ts +0 -2
  134. package/lib/typescript/src/__tests__/Constants.spec.d.ts.map +0 -1
  135. package/lib/typescript/src/__tests__/Device.spec.d.ts +0 -2
  136. package/lib/typescript/src/__tests__/Device.spec.d.ts.map +0 -1
  137. package/lib/typescript/src/__tests__/ErrorScope.spec.d.ts +0 -2
  138. package/lib/typescript/src/__tests__/ErrorScope.spec.d.ts.map +0 -1
  139. package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts +0 -2
  140. package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts.map +0 -1
  141. package/lib/typescript/src/__tests__/GPU.spec.d.ts +0 -2
  142. package/lib/typescript/src/__tests__/GPU.spec.d.ts.map +0 -1
  143. package/lib/typescript/src/__tests__/ImageData.spec.d.ts +0 -2
  144. package/lib/typescript/src/__tests__/ImageData.spec.d.ts.map +0 -1
  145. package/lib/typescript/src/__tests__/Shaders.spec.d.ts +0 -2
  146. package/lib/typescript/src/__tests__/Shaders.spec.d.ts.map +0 -1
  147. package/lib/typescript/src/__tests__/Texture.spec.d.ts +0 -2
  148. package/lib/typescript/src/__tests__/Texture.spec.d.ts.map +0 -1
  149. package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts +0 -3
  150. package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts.map +0 -1
  151. package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts +0 -29
  152. package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts.map +0 -1
  153. package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts +0 -5
  154. package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts.map +0 -1
  155. package/lib/typescript/src/__tests__/components/cube.d.ts +0 -7
  156. package/lib/typescript/src/__tests__/components/cube.d.ts.map +0 -1
  157. package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts +0 -22
  158. package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts.map +0 -1
  159. package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts +0 -12
  160. package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts.map +0 -1
  161. package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts +0 -7
  162. package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts.map +0 -1
  163. package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts +0 -6
  164. package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts.map +0 -1
  165. package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts +0 -6
  166. package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts.map +0 -1
  167. package/lib/typescript/src/__tests__/components/meshes/utils.d.ts +0 -10
  168. package/lib/typescript/src/__tests__/components/meshes/utils.d.ts.map +0 -1
  169. package/lib/typescript/src/__tests__/components/triangle.d.ts +0 -3
  170. package/lib/typescript/src/__tests__/components/triangle.d.ts.map +0 -1
  171. package/lib/typescript/src/__tests__/config.d.ts +0 -3
  172. package/lib/typescript/src/__tests__/config.d.ts.map +0 -1
  173. package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts +0 -2
  174. package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts.map +0 -1
  175. package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts +0 -2
  176. package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts.map +0 -1
  177. package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts +0 -2
  178. package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts.map +0 -1
  179. package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts +0 -2
  180. package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts.map +0 -1
  181. package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts +0 -2
  182. package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts.map +0 -1
  183. package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts +0 -2
  184. package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts.map +0 -1
  185. package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts +0 -2
  186. package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts.map +0 -1
  187. package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts +0 -2
  188. package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts.map +0 -1
  189. package/lib/typescript/src/__tests__/globalSetup.d.ts +0 -3
  190. package/lib/typescript/src/__tests__/globalSetup.d.ts.map +0 -1
  191. package/lib/typescript/src/__tests__/globalTeardown.d.ts +0 -3
  192. package/lib/typescript/src/__tests__/globalTeardown.d.ts.map +0 -1
  193. package/lib/typescript/src/__tests__/setup.d.ts +0 -63
  194. package/lib/typescript/src/__tests__/setup.d.ts.map +0 -1
  195. package/libs/apple/arm64_iphoneos/libwebgpu_dawn.a +0 -0
  196. package/libs/apple/arm64_iphonesimulator/libwebgpu_dawn.a +0 -0
  197. package/libs/apple/arm64_xros/libwebgpu_dawn.a +0 -0
  198. package/libs/apple/arm64_xrsimulator/libwebgpu_dawn.a +0 -0
  199. package/libs/apple/iphonesimulator/libwebgpu_dawn.a +0 -0
  200. package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64/libwebgpu_dawn.a +0 -0
  201. package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64-simulator/libwebgpu_dawn.a +0 -0
  202. package/libs/apple/universal_macosx/libwebgpu_dawn.a +0 -0
  203. package/libs/apple/x86_64_iphonesimulator/libwebgpu_dawn.a +0 -0
  204. package/libs/dawn.json +0 -4670
  205. package/src/__tests__/Alpha.spec.ts +0 -28
  206. package/src/__tests__/ArrayBuffer.spec.ts +0 -76
  207. package/src/__tests__/Buffer.spec.ts +0 -357
  208. package/src/__tests__/ComputeShader.spec.ts +0 -375
  209. package/src/__tests__/Constants.spec.ts +0 -89
  210. package/src/__tests__/Device.spec.ts +0 -84
  211. package/src/__tests__/ErrorScope.spec.ts +0 -92
  212. package/src/__tests__/ExternalTexture.spec.ts +0 -284
  213. package/src/__tests__/GPU.spec.ts +0 -272
  214. package/src/__tests__/ImageData.spec.ts +0 -26
  215. package/src/__tests__/Shaders.spec.ts +0 -232
  216. package/src/__tests__/Texture.spec.ts +0 -197
  217. package/src/__tests__/assets/Di-3d.png +0 -0
  218. package/src/__tests__/components/Wireframe/Shaders.ts +0 -138
  219. package/src/__tests__/components/Wireframe/models.ts +0 -113
  220. package/src/__tests__/components/Wireframe/utils.ts +0 -22
  221. package/src/__tests__/components/cube.ts +0 -51
  222. package/src/__tests__/components/meshes/mesh.ts +0 -96
  223. package/src/__tests__/components/meshes/sphere.ts +0 -103
  224. package/src/__tests__/components/meshes/stanfordDragon.ts +0 -44
  225. package/src/__tests__/components/meshes/stanfordDragonData.ts +0 -5
  226. package/src/__tests__/components/meshes/teapot.ts +0 -13
  227. package/src/__tests__/components/meshes/utils.ts +0 -235
  228. package/src/__tests__/components/triangle.ts +0 -17
  229. package/src/__tests__/config.ts +0 -2
  230. package/src/__tests__/demos/ABuffer.spec.ts +0 -890
  231. package/src/__tests__/demos/Blur.spec.ts +0 -398
  232. package/src/__tests__/demos/Cube.spec.ts +0 -929
  233. package/src/__tests__/demos/FractalCube.spec.ts +0 -240
  234. package/src/__tests__/demos/OcclusionQuery.spec.ts +0 -376
  235. package/src/__tests__/demos/RenderBundles.spec.ts +0 -580
  236. package/src/__tests__/demos/Triangle.spec.ts +0 -266
  237. package/src/__tests__/demos/Wireframe.spec.ts +0 -188
  238. package/src/__tests__/globalSetup.ts +0 -45
  239. package/src/__tests__/globalTeardown.ts +0 -11
  240. package/src/__tests__/setup.ts +0 -423
  241. package/src/__tests__/snapshots/abuffer.png +0 -0
  242. package/src/__tests__/snapshots/asteroid.png +0 -0
  243. package/src/__tests__/snapshots/blur.png +0 -0
  244. package/src/__tests__/snapshots/buffer.png +0 -0
  245. package/src/__tests__/snapshots/constant-triangle.png +0 -0
  246. package/src/__tests__/snapshots/cube.png +0 -0
  247. package/src/__tests__/snapshots/f.png +0 -0
  248. package/src/__tests__/snapshots/f2.png +0 -0
  249. package/src/__tests__/snapshots/fractal-cubes.png +0 -0
  250. package/src/__tests__/snapshots/instanced-cubes.png +0 -0
  251. package/src/__tests__/snapshots/occlusion-query.png +0 -0
  252. package/src/__tests__/snapshots/ref.png +0 -0
  253. package/src/__tests__/snapshots/semi-opaque-cyan.png +0 -0
  254. package/src/__tests__/snapshots/texture.png +0 -0
  255. package/src/__tests__/snapshots/textured-cube.png +0 -0
  256. package/src/__tests__/snapshots/triangle-msaa.png +0 -0
  257. package/src/__tests__/snapshots/triangle.png +0 -0
  258. package/src/__tests__/snapshots/two-cube.png +0 -0
@@ -3,14 +3,43 @@
3
3
  #include <memory>
4
4
  #include <string>
5
5
  #include <unordered_set>
6
+ #include <utility>
6
7
  #include <vector>
7
8
 
8
9
  #include "Convertors.h"
10
+ #include "RNFJSIConverter.h"
9
11
 
10
12
  #include "GPUFeatures.h"
11
13
 
12
14
  namespace rnwgpu {
13
15
 
16
+ void GPUDevice::notifyDeviceLost(wgpu::DeviceLostReason reason,
17
+ std::string message) {
18
+ if (_lostSettled) {
19
+ return;
20
+ }
21
+
22
+ _lostSettled = true;
23
+ _lostInfo = std::make_shared<GPUDeviceLostInfo>(reason, std::move(message));
24
+
25
+ if (_lostResolve.has_value()) {
26
+ auto resolve = std::move(*_lostResolve);
27
+ _lostResolve.reset();
28
+ resolve([info = _lostInfo](jsi::Runtime &runtime) mutable {
29
+ return margelo::JSIConverter<std::shared_ptr<GPUDeviceLostInfo>>::toJSI(
30
+ runtime, info);
31
+ });
32
+ }
33
+
34
+ _lostHandle.reset();
35
+ }
36
+
37
+ void GPUDevice::forceLossForTesting() {
38
+ // wgpu::StringView view("forceLossForTesting invoked from JS");
39
+ _instance.ForceLoss(wgpu::DeviceLostReason::Unknown,
40
+ "forceLossForTesting invoked from JS");
41
+ }
42
+
14
43
  std::shared_ptr<GPUBuffer>
15
44
  GPUDevice::createBuffer(std::shared_ptr<GPUBufferDescriptor> descriptor) {
16
45
  wgpu::BufferDescriptor desc;
@@ -52,9 +81,7 @@ std::shared_ptr<GPUCommandEncoder> GPUDevice::createCommandEncoder(
52
81
 
53
82
  void GPUDevice::destroy() {
54
83
  _instance.Destroy();
55
- auto lostInfo = std::make_shared<GPUDeviceLostInfo>(
56
- wgpu::DeviceLostReason::Destroyed, "device was destroyed");
57
- m_lostPromise->set_value(lostInfo);
84
+ notifyDeviceLost(wgpu::DeviceLostReason::Destroyed, "device was destroyed");
58
85
  }
59
86
 
60
87
  std::shared_ptr<GPUTexture>
@@ -208,139 +235,135 @@ std::shared_ptr<GPUExternalTexture> GPUDevice::importExternalTexture(
208
235
  "GPUDevice::importExternalTexture(): Not implemented");
209
236
  }
210
237
 
211
- std::future<std::shared_ptr<GPUComputePipeline>>
212
- GPUDevice::createComputePipelineAsync(
238
+ async::AsyncTaskHandle GPUDevice::createComputePipelineAsync(
213
239
  std::shared_ptr<GPUComputePipelineDescriptor> descriptor) {
214
- return _async->runAsync([=](wgpu::Instance *instance) {
215
- wgpu::ComputePipelineDescriptor desc{};
216
- Convertor conv;
217
- if (!conv(desc, descriptor)) {
218
- throw std::runtime_error("GPUDevice::createComputePipeline(): Error with "
219
- "GPUComputePipelineDescriptor");
220
- }
221
- wgpu::ComputePipeline computePipeline = nullptr;
222
- auto label = std::string(
223
- descriptor->label.has_value() ? descriptor->label.value() : "");
224
- auto result = std::make_shared<GPUComputePipeline>(computePipeline, label);
225
- auto future = _instance.CreateComputePipelineAsync(
226
- &desc, wgpu::CallbackMode::WaitAnyOnly,
227
- [&result](wgpu::CreatePipelineAsyncStatus status,
228
- wgpu::ComputePipeline pipeline, char const *msg) {
229
- switch (status) {
230
- case wgpu::CreatePipelineAsyncStatus::Success:
231
- result->_instance = pipeline;
232
- break;
233
- default:
234
- throw std::runtime_error(msg);
235
- break;
236
- }
237
- });
238
- instance->WaitAny(future, UINT64_MAX);
239
- return result;
240
- });
240
+ wgpu::ComputePipelineDescriptor desc{};
241
+ Convertor conv;
242
+ if (!conv(desc, descriptor)) {
243
+ throw std::runtime_error("GPUDevice::createComputePipeline(): Error with "
244
+ "GPUComputePipelineDescriptor");
245
+ }
246
+
247
+ auto label = std::string(
248
+ descriptor->label.has_value() ? descriptor->label.value() : "");
249
+ auto pipelineHolder = std::make_shared<GPUComputePipeline>(nullptr, label);
250
+
251
+ return _async->postTask(
252
+ [device = _instance, desc, descriptor,
253
+ pipelineHolder](const async::AsyncTaskHandle::ResolveFunction &resolve,
254
+ const async::AsyncTaskHandle::RejectFunction &reject) {
255
+ (void)descriptor;
256
+ device.CreateComputePipelineAsync(
257
+ &desc, wgpu::CallbackMode::AllowProcessEvents,
258
+ [pipelineHolder, resolve,
259
+ reject](wgpu::CreatePipelineAsyncStatus status,
260
+ wgpu::ComputePipeline pipeline, const char *msg) mutable {
261
+ if (status == wgpu::CreatePipelineAsyncStatus::Success &&
262
+ pipeline) {
263
+ pipelineHolder->_instance = pipeline;
264
+ resolve([pipelineHolder](jsi::Runtime &runtime) mutable {
265
+ return margelo::
266
+ JSIConverter<std::shared_ptr<GPUComputePipeline>>::toJSI(
267
+ runtime, pipelineHolder);
268
+ });
269
+ } else {
270
+ std::string error = msg ? std::string(msg)
271
+ : "Failed to create compute pipeline";
272
+ reject(std::move(error));
273
+ }
274
+ });
275
+ });
241
276
  }
242
277
 
243
- std::future<std::shared_ptr<GPURenderPipeline>>
244
- GPUDevice::createRenderPipelineAsync(
278
+ async::AsyncTaskHandle GPUDevice::createRenderPipelineAsync(
245
279
  std::shared_ptr<GPURenderPipelineDescriptor> descriptor) {
246
- return _async->runAsync([=](wgpu::Instance *instance) {
247
- wgpu::RenderPipelineDescriptor desc{};
248
- Convertor conv;
249
- if (!conv(desc, descriptor)) {
250
- throw std::runtime_error(
251
- "GPUDevice::createRenderPipelineAsync(): Error with "
252
- "GPURenderPipelineDescriptor");
253
- }
254
- wgpu::RenderPipeline renderPipeline = nullptr;
255
- auto label = std::string(
256
- descriptor->label.has_value() ? descriptor->label.value() : "");
257
- auto result = std::make_shared<GPURenderPipeline>(renderPipeline, label);
258
- auto future = _instance.CreateRenderPipelineAsync(
259
- &desc, wgpu::CallbackMode::WaitAnyOnly,
260
- [&result](wgpu::CreatePipelineAsyncStatus status,
261
- wgpu::RenderPipeline pipeline, char const *msg) {
262
- switch (status) {
263
- case wgpu::CreatePipelineAsyncStatus::Success:
264
- result->_instance = pipeline;
265
- break;
266
- default:
267
- throw std::runtime_error(msg);
268
- break;
269
- }
270
- });
271
- instance->WaitAny(future, UINT64_MAX);
272
- return result;
273
- });
280
+ wgpu::RenderPipelineDescriptor desc{};
281
+ Convertor conv;
282
+ if (!conv(desc, descriptor)) {
283
+ throw std::runtime_error(
284
+ "GPUDevice::createRenderPipelineAsync(): Error with "
285
+ "GPURenderPipelineDescriptor");
286
+ }
287
+
288
+ auto label = std::string(
289
+ descriptor->label.has_value() ? descriptor->label.value() : "");
290
+ auto pipelineHolder = std::make_shared<GPURenderPipeline>(nullptr, label);
291
+
292
+ return _async->postTask(
293
+ [device = _instance, desc, descriptor,
294
+ pipelineHolder](const async::AsyncTaskHandle::ResolveFunction &resolve,
295
+ const async::AsyncTaskHandle::RejectFunction &reject) {
296
+ (void)descriptor;
297
+ device.CreateRenderPipelineAsync(
298
+ &desc, wgpu::CallbackMode::AllowProcessEvents,
299
+ [pipelineHolder, resolve,
300
+ reject](wgpu::CreatePipelineAsyncStatus status,
301
+ wgpu::RenderPipeline pipeline, const char *msg) mutable {
302
+ if (status == wgpu::CreatePipelineAsyncStatus::Success &&
303
+ pipeline) {
304
+ pipelineHolder->_instance = pipeline;
305
+ resolve([pipelineHolder](jsi::Runtime &runtime) mutable {
306
+ return margelo::
307
+ JSIConverter<std::shared_ptr<GPURenderPipeline>>::toJSI(
308
+ runtime, pipelineHolder);
309
+ });
310
+ } else {
311
+ std::string error =
312
+ msg ? std::string(msg) : "Failed to create render pipeline";
313
+ reject(std::move(error));
314
+ }
315
+ });
316
+ });
274
317
  }
275
318
 
276
319
  void GPUDevice::pushErrorScope(wgpu::ErrorFilter filter) {
277
320
  _instance.PushErrorScope(filter);
278
321
  }
279
322
 
280
- std::future<std::variant<std::nullptr_t, std::shared_ptr<GPUError>>>
281
- GPUDevice::popErrorScope() {
282
- // Create a promise to return a future, but do the work synchronously on main
283
- // thread
284
- auto promise = std::make_shared<
285
- std::promise<std::variant<std::nullptr_t, std::shared_ptr<GPUError>>>>();
286
- auto future = promise->get_future();
287
-
288
- std::variant<std::nullptr_t, std::shared_ptr<GPUError>> result = nullptr;
289
-
290
- auto wgpu_future = _instance.PopErrorScope(
291
- wgpu::CallbackMode::WaitAnyOnly,
292
- [&result](wgpu::PopErrorScopeStatus status, wgpu::ErrorType type,
293
- wgpu::StringView message) {
294
- switch (status) {
295
- case wgpu::PopErrorScopeStatus::Error:
296
- // PopErrorScope itself failed, e.g. the error scope stack was empty.
297
- return;
298
- case wgpu::PopErrorScopeStatus::CallbackCancelled:
299
- // The instance has been dropped. Shouldn't happen except maybe during
300
- // shutdown.
301
- return;
302
- case wgpu::PopErrorScopeStatus::Success:
303
- // This is the only case where `type` is set to a meaningful value.
304
- break;
305
- }
306
- switch (type) {
307
- case wgpu::ErrorType::NoError:
308
- break;
309
- case wgpu::ErrorType::OutOfMemory: {
310
- result = std::make_shared<GPUError>(wgpu::ErrorType::OutOfMemory,
311
- std::string(message));
312
- break;
313
- }
314
- case wgpu::ErrorType::Validation: {
315
- result = std::make_shared<GPUError>(wgpu::ErrorType::Validation,
316
- std::string(message));
317
- break;
318
- }
319
- case wgpu::ErrorType::Internal: {
320
- result = std::make_shared<GPUError>(wgpu::ErrorType::Internal,
321
- std::string(message));
322
- break;
323
- }
324
- case wgpu::ErrorType::Unknown:
325
- result = std::make_shared<GPUError>(wgpu::ErrorType::Unknown,
326
- std::string(message));
327
- break;
328
- default:
329
- throw std::runtime_error(
330
- "unhandled error type (" +
331
- std::to_string(
332
- static_cast<std::underlying_type<wgpu::ErrorType>::type>(
333
- type)) +
334
- ")");
335
- break;
336
- }
323
+ async::AsyncTaskHandle GPUDevice::popErrorScope() {
324
+ auto device = _instance;
325
+
326
+ return _async->postTask(
327
+ [device](const async::AsyncTaskHandle::ResolveFunction &resolve,
328
+ const async::AsyncTaskHandle::RejectFunction &reject) {
329
+ device.PopErrorScope(
330
+ wgpu::CallbackMode::AllowProcessEvents,
331
+ [resolve, reject](wgpu::PopErrorScopeStatus status,
332
+ wgpu::ErrorType type, wgpu::StringView message) {
333
+ if (status == wgpu::PopErrorScopeStatus::Error ||
334
+ status == wgpu::PopErrorScopeStatus::CallbackCancelled) {
335
+ reject("PopErrorScope failed");
336
+ return;
337
+ }
338
+
339
+ std::variant<std::nullptr_t, std::shared_ptr<GPUError>> result =
340
+ nullptr;
341
+
342
+ switch (type) {
343
+ case wgpu::ErrorType::NoError:
344
+ break;
345
+ case wgpu::ErrorType::OutOfMemory:
346
+ case wgpu::ErrorType::Validation:
347
+ case wgpu::ErrorType::Internal:
348
+ case wgpu::ErrorType::Unknown: {
349
+ std::string messageString =
350
+ message.length ? std::string(message.data, message.length)
351
+ : "";
352
+ result = std::make_shared<GPUError>(type, messageString);
353
+ break;
354
+ }
355
+ default:
356
+ reject("Unhandled GPU error type");
357
+ return;
358
+ }
359
+
360
+ resolve([result =
361
+ std::move(result)](jsi::Runtime &runtime) mutable {
362
+ return margelo::JSIConverter<decltype(result)>::toJSI(runtime,
363
+ result);
364
+ });
365
+ });
337
366
  });
338
-
339
- // Wait synchronously on main thread - both push and pop now on same thread
340
- _async->instance->WaitAny(wgpu_future, UINT64_MAX);
341
-
342
- promise->set_value(result);
343
- return future;
344
367
  }
345
368
 
346
369
  std::unordered_set<std::string> GPUDevice::getFeatures() {
@@ -356,8 +379,40 @@ std::unordered_set<std::string> GPUDevice::getFeatures() {
356
379
  return result;
357
380
  }
358
381
 
359
- std::future<std::shared_ptr<GPUDeviceLostInfo>> GPUDevice::getLost() {
360
- return std::async(std::launch::async,
361
- [=]() { return m_lostSharedFuture->get(); });
382
+ async::AsyncTaskHandle GPUDevice::getLost() {
383
+ if (_lostHandle.has_value()) {
384
+ return *_lostHandle;
385
+ }
386
+
387
+ if (_lostSettled && _lostInfo) {
388
+ return _async->postTask(
389
+ [info = _lostInfo](
390
+ const async::AsyncTaskHandle::ResolveFunction &resolve,
391
+ const async::AsyncTaskHandle::RejectFunction & /*reject*/) {
392
+ resolve([info](jsi::Runtime &runtime) mutable {
393
+ return margelo::JSIConverter<
394
+ std::shared_ptr<GPUDeviceLostInfo>>::toJSI(runtime, info);
395
+ });
396
+ },
397
+ false);
398
+ }
399
+
400
+ auto handle = _async->postTask(
401
+ [this](const async::AsyncTaskHandle::ResolveFunction &resolve,
402
+ const async::AsyncTaskHandle::RejectFunction & /*reject*/) {
403
+ if (_lostSettled && _lostInfo) {
404
+ resolve([info = _lostInfo](jsi::Runtime &runtime) mutable {
405
+ return margelo::JSIConverter<
406
+ std::shared_ptr<GPUDeviceLostInfo>>::toJSI(runtime, info);
407
+ });
408
+ return;
409
+ }
410
+
411
+ _lostResolve = resolve;
412
+ },
413
+ false);
414
+
415
+ _lostHandle = handle;
416
+ return handle;
362
417
  }
363
418
  } // namespace rnwgpu
@@ -1,16 +1,18 @@
1
1
  #pragma once
2
2
 
3
- #include <future>
4
3
  #include <memory>
4
+ #include <optional>
5
5
  #include <string>
6
6
  #include <unordered_set>
7
+ #include <utility>
7
8
  #include <variant>
8
9
 
9
10
  #include "Unions.h"
10
11
 
11
12
  #include "RNFHybridObject.h"
12
13
 
13
- #include "AsyncRunner.h"
14
+ #include "rnwgpu/async/AsyncRunner.h"
15
+ #include "rnwgpu/async/AsyncTaskHandle.h"
14
16
 
15
17
  #include "webgpu/webgpu_cpp.h"
16
18
 
@@ -51,17 +53,11 @@ namespace m = margelo;
51
53
 
52
54
  class GPUDevice : public m::HybridObject {
53
55
  public:
54
- explicit GPUDevice(wgpu::Device instance, std::shared_ptr<AsyncRunner> async,
56
+ explicit GPUDevice(wgpu::Device instance,
57
+ std::shared_ptr<async::AsyncRunner> async,
55
58
  std::string label)
56
59
  : HybridObject("GPUDevice"), _instance(instance), _async(async),
57
- _label(label) {
58
- m_lostPromise =
59
- std::make_shared<std::promise<std::shared_ptr<GPUDeviceLostInfo>>>();
60
-
61
- auto sharedFuture = m_lostPromise->get_future().share();
62
- m_lostSharedFuture = std::make_shared<
63
- std::shared_future<std::shared_ptr<GPUDeviceLostInfo>>>(sharedFuture);
64
- }
60
+ _label(label) {}
65
61
 
66
62
  public:
67
63
  std::string getBrand() { return _name; }
@@ -87,9 +83,9 @@ public:
87
83
  std::shared_ptr<GPUComputePipelineDescriptor> descriptor);
88
84
  std::shared_ptr<GPURenderPipeline>
89
85
  createRenderPipeline(std::shared_ptr<GPURenderPipelineDescriptor> descriptor);
90
- std::future<std::shared_ptr<GPUComputePipeline>> createComputePipelineAsync(
86
+ async::AsyncTaskHandle createComputePipelineAsync(
91
87
  std::shared_ptr<GPUComputePipelineDescriptor> descriptor);
92
- std::future<std::shared_ptr<GPURenderPipeline>> createRenderPipelineAsync(
88
+ async::AsyncTaskHandle createRenderPipelineAsync(
93
89
  std::shared_ptr<GPURenderPipelineDescriptor> descriptor);
94
90
  std::shared_ptr<GPUCommandEncoder> createCommandEncoder(
95
91
  std::optional<std::shared_ptr<GPUCommandEncoderDescriptor>> descriptor);
@@ -98,13 +94,14 @@ public:
98
94
  std::shared_ptr<GPUQuerySet>
99
95
  createQuerySet(std::shared_ptr<GPUQuerySetDescriptor> descriptor);
100
96
  void pushErrorScope(wgpu::ErrorFilter filter);
101
- std::future<std::variant<std::nullptr_t, std::shared_ptr<GPUError>>>
102
- popErrorScope();
97
+ async::AsyncTaskHandle popErrorScope();
103
98
 
104
99
  std::unordered_set<std::string> getFeatures();
105
100
  std::shared_ptr<GPUSupportedLimits> getLimits();
106
101
  std::shared_ptr<GPUQueue> getQueue();
107
- std::future<std::shared_ptr<GPUDeviceLostInfo>> getLost();
102
+ async::AsyncTaskHandle getLost();
103
+ void notifyDeviceLost(wgpu::DeviceLostReason reason, std::string message);
104
+ void forceLossForTesting();
108
105
 
109
106
  std::string getLabel() { return _label; }
110
107
  void setLabel(const std::string &label) {
@@ -148,18 +145,22 @@ public:
148
145
  registerHybridGetter("lost", &GPUDevice::getLost, this);
149
146
  registerHybridGetter("label", &GPUDevice::getLabel, this);
150
147
  registerHybridSetter("label", &GPUDevice::setLabel, this);
148
+ registerHybridMethod("forceLossForTesting", &GPUDevice::forceLossForTesting,
149
+ this);
151
150
  }
152
151
 
153
152
  inline const wgpu::Device get() { return _instance; }
154
153
 
155
154
  private:
155
+ friend class GPUAdapter;
156
+
156
157
  wgpu::Device _instance;
157
- std::shared_ptr<AsyncRunner> _async;
158
+ std::shared_ptr<async::AsyncRunner> _async;
158
159
  std::string _label;
159
- std::shared_ptr<std::promise<std::shared_ptr<GPUDeviceLostInfo>>>
160
- m_lostPromise;
161
- std::shared_ptr<std::shared_future<std::shared_ptr<GPUDeviceLostInfo>>>
162
- m_lostSharedFuture;
160
+ std::optional<async::AsyncTaskHandle> _lostHandle;
161
+ std::shared_ptr<GPUDeviceLostInfo> _lostInfo;
162
+ bool _lostSettled = false;
163
+ std::optional<async::AsyncTaskHandle::ResolveFunction> _lostResolve;
163
164
  };
164
165
 
165
166
  } // namespace rnwgpu
@@ -6,8 +6,6 @@
6
6
 
7
7
  #include "RNFHybridObject.h"
8
8
 
9
- #include "AsyncRunner.h"
10
-
11
9
  #include "webgpu/webgpu_cpp.h"
12
10
 
13
11
  namespace rnwgpu {
@@ -37,4 +35,4 @@ private:
37
35
  std::string _message;
38
36
  };
39
37
 
40
- } // namespace rnwgpu
38
+ } // namespace rnwgpu
@@ -6,8 +6,6 @@
6
6
 
7
7
  #include "RNFHybridObject.h"
8
8
 
9
- #include "AsyncRunner.h"
10
-
11
9
  #include "webgpu/webgpu_cpp.h"
12
10
 
13
11
  namespace rnwgpu {
@@ -43,4 +41,4 @@ private:
43
41
  std::string _label;
44
42
  };
45
43
 
46
- } // namespace rnwgpu
44
+ } // namespace rnwgpu
@@ -6,8 +6,6 @@
6
6
 
7
7
  #include "RNFHybridObject.h"
8
8
 
9
- #include "AsyncRunner.h"
10
-
11
9
  #include "webgpu/webgpu_cpp.h"
12
10
 
13
11
  namespace rnwgpu {
@@ -42,4 +40,4 @@ private:
42
40
  std::string _label;
43
41
  };
44
42
 
45
- } // namespace rnwgpu
43
+ } // namespace rnwgpu
@@ -6,8 +6,6 @@
6
6
 
7
7
  #include "RNFHybridObject.h"
8
8
 
9
- #include "AsyncRunner.h"
10
-
11
9
  #include "webgpu/webgpu_cpp.h"
12
10
 
13
11
  namespace rnwgpu {
@@ -70,4 +68,4 @@ private:
70
68
  std::string _label;
71
69
  };
72
70
 
73
- } // namespace rnwgpu
71
+ } // namespace rnwgpu
@@ -78,14 +78,25 @@ void GPUQueue::writeBuffer(std::shared_ptr<GPUBuffer> buffer,
78
78
  static_cast<size_t>(size64));
79
79
  }
80
80
 
81
- std::future<void> GPUQueue::onSubmittedWorkDone() {
82
- return _async->runAsync([&] {
83
- return _instance.OnSubmittedWorkDone(
84
- wgpu::CallbackMode::WaitAnyOnly,
85
- [](wgpu::QueueWorkDoneStatus status, wgpu::StringView message) {
86
- // Handle the callback if needed
87
- });
88
- });
81
+ async::AsyncTaskHandle GPUQueue::onSubmittedWorkDone() {
82
+ auto queue = _instance;
83
+ return _async->postTask(
84
+ [queue](const async::AsyncTaskHandle::ResolveFunction &resolve,
85
+ const async::AsyncTaskHandle::RejectFunction &reject) {
86
+ queue.OnSubmittedWorkDone(
87
+ wgpu::CallbackMode::AllowProcessEvents,
88
+ [resolve, reject](wgpu::QueueWorkDoneStatus status,
89
+ wgpu::StringView message) {
90
+ if (status == wgpu::QueueWorkDoneStatus::Success) {
91
+ resolve(nullptr);
92
+ } else {
93
+ std::string error =
94
+ message.length ? std::string(message.data, message.length)
95
+ : "Queue work did not complete successfully";
96
+ reject(std::move(error));
97
+ }
98
+ });
99
+ });
89
100
  }
90
101
 
91
102
  void GPUQueue::copyExternalImageToTexture(
@@ -1,6 +1,5 @@
1
1
  #pragma once
2
2
 
3
- #include <future>
4
3
  #include <memory>
5
4
  #include <string>
6
5
  #include <vector>
@@ -9,7 +8,8 @@
9
8
 
10
9
  #include "RNFHybridObject.h"
11
10
 
12
- #include "AsyncRunner.h"
11
+ #include "rnwgpu/async/AsyncRunner.h"
12
+ #include "rnwgpu/async/AsyncTaskHandle.h"
13
13
 
14
14
  #include "webgpu/webgpu_cpp.h"
15
15
 
@@ -25,7 +25,8 @@ namespace m = margelo;
25
25
 
26
26
  class GPUQueue : public m::HybridObject {
27
27
  public:
28
- explicit GPUQueue(wgpu::Queue instance, std::shared_ptr<AsyncRunner> async,
28
+ explicit GPUQueue(wgpu::Queue instance,
29
+ std::shared_ptr<async::AsyncRunner> async,
29
30
  std::string label)
30
31
  : HybridObject("GPUQueue"), _instance(instance), _async(async),
31
32
  _label(label) {}
@@ -34,7 +35,7 @@ public:
34
35
  std::string getBrand() { return _name; }
35
36
 
36
37
  void submit(std::vector<std::shared_ptr<GPUCommandBuffer>> commandBuffers);
37
- std::future<void> onSubmittedWorkDone();
38
+ async::AsyncTaskHandle onSubmittedWorkDone();
38
39
  void writeBuffer(std::shared_ptr<GPUBuffer> buffer, uint64_t bufferOffset,
39
40
  std::shared_ptr<ArrayBuffer> data,
40
41
  std::optional<uint64_t> dataOffsetElements,
@@ -72,8 +73,8 @@ public:
72
73
 
73
74
  private:
74
75
  wgpu::Queue _instance;
75
- std::shared_ptr<AsyncRunner> _async;
76
+ std::shared_ptr<async::AsyncRunner> _async;
76
77
  std::string _label;
77
78
  };
78
79
 
79
- } // namespace rnwgpu
80
+ } // namespace rnwgpu
@@ -6,8 +6,6 @@
6
6
 
7
7
  #include "RNFHybridObject.h"
8
8
 
9
- #include "AsyncRunner.h"
10
-
11
9
  #include "webgpu/webgpu_cpp.h"
12
10
 
13
11
  namespace rnwgpu {
@@ -42,4 +40,4 @@ private:
42
40
  std::string _label;
43
41
  };
44
42
 
45
- } // namespace rnwgpu
43
+ } // namespace rnwgpu
@@ -10,8 +10,6 @@
10
10
 
11
11
  #include "RNFHybridObject.h"
12
12
 
13
- #include "AsyncRunner.h"
14
-
15
13
  #include "webgpu/webgpu_cpp.h"
16
14
 
17
15
  #include "GPUBindGroup.h"
@@ -106,4 +104,4 @@ private:
106
104
  std::string _label;
107
105
  };
108
106
 
109
- } // namespace rnwgpu
107
+ } // namespace rnwgpu
@@ -10,8 +10,6 @@
10
10
 
11
11
  #include "RNFHybridObject.h"
12
12
 
13
- #include "AsyncRunner.h"
14
-
15
13
  #include "webgpu/webgpu_cpp.h"
16
14
 
17
15
  #include "GPUBindGroup.h"
@@ -127,4 +125,4 @@ private:
127
125
  std::string _label;
128
126
  };
129
127
 
130
- } // namespace rnwgpu
128
+ } // namespace rnwgpu
@@ -7,8 +7,6 @@
7
7
 
8
8
  #include "RNFHybridObject.h"
9
9
 
10
- #include "AsyncRunner.h"
11
-
12
10
  #include "webgpu/webgpu_cpp.h"
13
11
 
14
12
  #include "GPUBindGroupLayout.h"
@@ -58,4 +56,4 @@ private:
58
56
  friend class GPUDevice;
59
57
  };
60
58
 
61
- } // namespace rnwgpu
59
+ } // namespace rnwgpu