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
@@ -10,25 +10,25 @@
10
10
  #include "Convertors.h"
11
11
 
12
12
  #include "GPUFeatures.h"
13
+ #include "RNFJSIConverter.h"
13
14
  #include "WGPULogger.h"
14
15
 
15
16
  namespace rnwgpu {
16
17
 
17
- std::future<std::shared_ptr<GPUDevice>> GPUAdapter::requestDevice(
18
+ async::AsyncTaskHandle GPUAdapter::requestDevice(
18
19
  std::optional<std::shared_ptr<GPUDeviceDescriptor>> descriptor) {
19
- std::promise<std::shared_ptr<GPUDevice>> promise;
20
- auto future = promise.get_future();
21
- wgpu::Device device = nullptr;
22
20
  wgpu::DeviceDescriptor aDescriptor;
23
21
  Convertor conv;
24
22
  if (!conv(aDescriptor, descriptor)) {
25
23
  throw std::runtime_error("Failed to convert GPUDeviceDescriptor");
26
24
  }
25
+ auto deviceLostBinding = std::make_shared<std::weak_ptr<GPUDevice>>();
27
26
  // Set device lost callback using new template API
28
27
  aDescriptor.SetDeviceLostCallback(
29
28
  wgpu::CallbackMode::AllowSpontaneous,
30
- [](const wgpu::Device &device, wgpu::DeviceLostReason reason,
31
- wgpu::StringView message) {
29
+ [deviceLostBinding](const wgpu::Device & /*device*/,
30
+ wgpu::DeviceLostReason reason,
31
+ wgpu::StringView message) {
32
32
  const char *lostReason = "";
33
33
  switch (reason) {
34
34
  case wgpu::DeviceLostReason::Destroyed:
@@ -40,8 +40,13 @@ std::future<std::shared_ptr<GPUDevice>> GPUAdapter::requestDevice(
40
40
  default:
41
41
  lostReason = "Unknown";
42
42
  }
43
+ std::string msg =
44
+ message.length ? std::string(message.data, message.length) : "";
43
45
  Logger::logToConsole("GPU Device Lost (%s): %s", lostReason,
44
- message.data);
46
+ msg.c_str());
47
+ if (auto deviceHost = deviceLostBinding->lock()) {
48
+ deviceHost->notifyDeviceLost(reason, std::move(msg));
49
+ }
45
50
  });
46
51
 
47
52
  // Set uncaptured error callback using new template API
@@ -71,55 +76,75 @@ std::future<std::shared_ptr<GPUDevice>> GPUAdapter::requestDevice(
71
76
  : "no message";
72
77
  fprintf(stderr, "%s", fullMessage.c_str());
73
78
  });
74
- _instance.RequestDevice(
75
- &aDescriptor, wgpu::CallbackMode::AllowSpontaneous,
76
- [](wgpu::RequestDeviceStatus status, wgpu::Device device,
77
- wgpu::StringView message, wgpu::Device *userdata) {
78
- if (message.length) {
79
- fprintf(stderr, "%s", message.data);
80
- return;
81
- }
82
- if (status == wgpu::RequestDeviceStatus::Success) {
83
- *userdata = std::move(device);
84
- }
85
- },
86
- &device);
87
-
88
- if (!device) {
89
- throw std::runtime_error("Failed to request device");
90
- }
91
- device.SetLoggingCallback(
92
- [creationRuntime = _creationRuntime](wgpu::LoggingType type,
93
- wgpu::StringView message) {
94
- const char *logLevel = "";
95
- switch (type) {
96
- case wgpu::LoggingType::Warning:
97
- logLevel = "Warning";
98
- Logger::warnToJavascriptConsole(
99
- *creationRuntime, std::string(message.data, message.length));
100
- break;
101
- case wgpu::LoggingType::Error:
102
- logLevel = "Error";
103
- Logger::errorToJavascriptConsole(
104
- *creationRuntime, std::string(message.data, message.length));
105
- break;
106
- case wgpu::LoggingType::Verbose:
107
- logLevel = "Verbose";
108
- break;
109
- case wgpu::LoggingType::Info:
110
- logLevel = "Info";
111
- break;
112
- default:
113
- logLevel = "Unknown";
114
- Logger::logToConsole("%s: %.*s", logLevel,
115
- static_cast<int>(message.length), message.data);
116
- }
117
- });
118
79
  std::string label =
119
80
  descriptor.has_value() ? descriptor.value()->label.value_or("") : "";
120
- promise.set_value(
121
- std::make_shared<GPUDevice>(std::move(device), _async, label));
122
- return future;
81
+
82
+ auto creationRuntime = getCreationRuntime();
83
+ return _async->postTask(
84
+ [this, aDescriptor, descriptor, label = std::move(label),
85
+ deviceLostBinding,
86
+ creationRuntime](const async::AsyncTaskHandle::ResolveFunction &resolve,
87
+ const async::AsyncTaskHandle::RejectFunction &reject) {
88
+ (void)descriptor;
89
+ _instance.RequestDevice(
90
+ &aDescriptor, wgpu::CallbackMode::AllowProcessEvents,
91
+ [asyncRunner = _async, resolve, reject, label, creationRuntime,
92
+ deviceLostBinding](wgpu::RequestDeviceStatus status,
93
+ wgpu::Device device,
94
+ wgpu::StringView message) mutable {
95
+ if (message.length) {
96
+ fprintf(stderr, "%s", message.data);
97
+ }
98
+
99
+ if (status != wgpu::RequestDeviceStatus::Success || !device) {
100
+ std::string error =
101
+ message.length ? std::string(message.data, message.length)
102
+ : "Failed to request device";
103
+ reject(std::move(error));
104
+ return;
105
+ }
106
+
107
+ device.SetLoggingCallback([creationRuntime](
108
+ wgpu::LoggingType type,
109
+ wgpu::StringView msg) {
110
+ if (creationRuntime == nullptr) {
111
+ return;
112
+ }
113
+ const char *logLevel = "";
114
+ switch (type) {
115
+ case wgpu::LoggingType::Warning:
116
+ logLevel = "Warning";
117
+ Logger::warnToJavascriptConsole(
118
+ *creationRuntime, std::string(msg.data, msg.length));
119
+ break;
120
+ case wgpu::LoggingType::Error:
121
+ logLevel = "Error";
122
+ Logger::errorToJavascriptConsole(
123
+ *creationRuntime, std::string(msg.data, msg.length));
124
+ break;
125
+ case wgpu::LoggingType::Verbose:
126
+ logLevel = "Verbose";
127
+ break;
128
+ case wgpu::LoggingType::Info:
129
+ logLevel = "Info";
130
+ break;
131
+ default:
132
+ logLevel = "Unknown";
133
+ Logger::logToConsole("%s: %.*s", logLevel,
134
+ static_cast<int>(msg.length), msg.data);
135
+ }
136
+ });
137
+
138
+ auto deviceHost = std::make_shared<GPUDevice>(std::move(device),
139
+ asyncRunner, label);
140
+ *deviceLostBinding = deviceHost;
141
+ resolve([deviceHost = std::move(deviceHost)](
142
+ jsi::Runtime &runtime) mutable {
143
+ return margelo::JSIConverter<std::shared_ptr<GPUDevice>>::toJSI(
144
+ runtime, deviceHost);
145
+ });
146
+ });
147
+ });
123
148
  }
124
149
 
125
150
  std::unordered_set<std::string> GPUAdapter::getFeatures() {
@@ -1,6 +1,5 @@
1
1
  #pragma once
2
2
 
3
- #include <future>
4
3
  #include <memory>
5
4
  #include <string>
6
5
  #include <unordered_set>
@@ -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,13 +25,13 @@ namespace m = margelo;
25
25
  class GPUAdapter : public m::HybridObject {
26
26
  public:
27
27
  explicit GPUAdapter(wgpu::Adapter instance,
28
- std::shared_ptr<AsyncRunner> async)
28
+ std::shared_ptr<async::AsyncRunner> async)
29
29
  : HybridObject("GPUAdapter"), _instance(instance), _async(async) {}
30
30
 
31
31
  public:
32
32
  std::string getBrand() { return _name; }
33
33
 
34
- std::future<std::shared_ptr<GPUDevice>>
34
+ async::AsyncTaskHandle
35
35
  requestDevice(std::optional<std::shared_ptr<GPUDeviceDescriptor>> descriptor);
36
36
 
37
37
  std::unordered_set<std::string> getFeatures();
@@ -50,7 +50,7 @@ public:
50
50
 
51
51
  private:
52
52
  wgpu::Adapter _instance;
53
- std::shared_ptr<AsyncRunner> _async;
53
+ std::shared_ptr<async::AsyncRunner> _async;
54
54
  };
55
55
 
56
- } // namespace rnwgpu
56
+ } // namespace rnwgpu
@@ -7,7 +7,6 @@
7
7
 
8
8
  #include "RNFHybridObject.h"
9
9
 
10
- #include "AsyncRunner.h"
11
10
  #include "Convertors.h"
12
11
 
13
12
  #include "webgpu/webgpu_cpp.h"
@@ -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 {
@@ -49,4 +47,4 @@ private:
49
47
  std::string _label;
50
48
  };
51
49
 
52
- } // namespace rnwgpu
50
+ } // 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 {
@@ -50,4 +48,4 @@ private:
50
48
  std::string _label;
51
49
  };
52
50
 
53
- } // namespace rnwgpu
51
+ } // namespace rnwgpu
@@ -37,9 +37,9 @@ GPUBuffer::getMappedRange(std::optional<size_t> o, std::optional<size_t> size) {
37
37
 
38
38
  void GPUBuffer::destroy() { _instance.Destroy(); }
39
39
 
40
- std::future<void> GPUBuffer::mapAsync(uint64_t modeIn,
41
- std::optional<uint64_t> offset,
42
- std::optional<uint64_t> size) {
40
+ async::AsyncTaskHandle GPUBuffer::mapAsync(uint64_t modeIn,
41
+ std::optional<uint64_t> offset,
42
+ std::optional<uint64_t> size) {
43
43
  Convertor conv;
44
44
  wgpu::MapMode mode;
45
45
  if (!conv(mode, modeIn)) {
@@ -48,35 +48,38 @@ std::future<void> GPUBuffer::mapAsync(uint64_t modeIn,
48
48
  uint64_t rangeSize = size.has_value()
49
49
  ? size.value()
50
50
  : (_instance.GetSize() - offset.value_or(0));
51
- return _async->runAsync([=] {
52
- // for (auto& mapping : mappings) {
53
- // if (mapping.Intersects(start, end)) {
54
- // promise.set_exception(std::make_exception_ptr(std::runtime_error("Buffer
55
- // is already mapped"))); return future;
56
- // }
57
- // }
58
- return _instance.MapAsync(
59
- mode, offset.value_or(0), rangeSize, wgpu::CallbackMode::WaitAnyOnly,
60
- [](wgpu::MapAsyncStatus status, wgpu::StringView message) {
61
- switch (status) {
62
- case wgpu::MapAsyncStatus::Success:
63
- break;
64
- case wgpu::MapAsyncStatus::CallbackCancelled:
65
- throw std::runtime_error("MapAsyncStatus::CallbackCancelled");
66
- break;
67
- case wgpu::MapAsyncStatus::Error:
68
- throw std::runtime_error("MapAsyncStatus::Error");
69
- break;
70
- case wgpu::MapAsyncStatus::Aborted:
71
- throw std::runtime_error("MapAsyncStatus::Aborted");
72
- break;
73
- default:
74
- throw std::runtime_error("MapAsyncStatus: " +
75
- std::to_string(static_cast<int>(status)));
76
- break;
77
- }
78
- });
79
- });
51
+ auto bufferHandle = _instance;
52
+ uint64_t resolvedOffset = offset.value_or(0);
53
+
54
+ return _async->postTask(
55
+ [bufferHandle, mode, resolvedOffset,
56
+ rangeSize](const async::AsyncTaskHandle::ResolveFunction &resolve,
57
+ const async::AsyncTaskHandle::RejectFunction &reject) {
58
+ bufferHandle.MapAsync(mode, resolvedOffset, rangeSize,
59
+ wgpu::CallbackMode::AllowProcessEvents,
60
+ [resolve, reject](wgpu::MapAsyncStatus status,
61
+ wgpu::StringView message) {
62
+ switch (status) {
63
+ case wgpu::MapAsyncStatus::Success:
64
+ resolve(nullptr);
65
+ break;
66
+ case wgpu::MapAsyncStatus::CallbackCancelled:
67
+ reject("MapAsyncStatus::CallbackCancelled");
68
+ break;
69
+ case wgpu::MapAsyncStatus::Error:
70
+ reject("MapAsyncStatus::Error");
71
+ break;
72
+ case wgpu::MapAsyncStatus::Aborted:
73
+ reject("MapAsyncStatus::Aborted");
74
+ break;
75
+ default:
76
+ reject(
77
+ "MapAsyncStatus: " +
78
+ std::to_string(static_cast<int>(status)));
79
+ break;
80
+ }
81
+ });
82
+ });
80
83
  }
81
84
 
82
85
  void GPUBuffer::unmap() { _instance.Unmap(); }
@@ -1,6 +1,5 @@
1
1
  #pragma once
2
2
 
3
- #include <future>
4
3
  #include <memory>
5
4
  #include <optional>
6
5
  #include <string>
@@ -10,7 +9,8 @@
10
9
 
11
10
  #include "RNFHybridObject.h"
12
11
 
13
- #include "AsyncRunner.h"
12
+ #include "rnwgpu/async/AsyncRunner.h"
13
+ #include "rnwgpu/async/AsyncTaskHandle.h"
14
14
 
15
15
  #include "webgpu/webgpu_cpp.h"
16
16
 
@@ -22,7 +22,8 @@ namespace m = margelo;
22
22
 
23
23
  class GPUBuffer : public m::HybridObject {
24
24
  public:
25
- explicit GPUBuffer(wgpu::Buffer instance, std::shared_ptr<AsyncRunner> async,
25
+ explicit GPUBuffer(wgpu::Buffer instance,
26
+ std::shared_ptr<async::AsyncRunner> async,
26
27
  std::string label)
27
28
  : HybridObject("GPUBuffer"), _instance(instance), _async(async),
28
29
  _label(label) {}
@@ -30,8 +31,9 @@ public:
30
31
  public:
31
32
  std::string getBrand() { return _name; }
32
33
 
33
- std::future<void> mapAsync(uint64_t modeIn, std::optional<uint64_t> offset,
34
- std::optional<uint64_t> size);
34
+ async::AsyncTaskHandle mapAsync(uint64_t modeIn,
35
+ std::optional<uint64_t> offset,
36
+ std::optional<uint64_t> size);
35
37
  std::shared_ptr<ArrayBuffer> getMappedRange(std::optional<size_t> offset,
36
38
  std::optional<size_t> size);
37
39
  void unmap();
@@ -66,7 +68,7 @@ public:
66
68
 
67
69
  private:
68
70
  wgpu::Buffer _instance;
69
- std::shared_ptr<AsyncRunner> _async;
71
+ std::shared_ptr<async::AsyncRunner> _async;
70
72
  std::string _label;
71
73
  struct Mapping {
72
74
  uint64_t start;
@@ -79,4 +81,4 @@ private:
79
81
  std::vector<Mapping> mappings;
80
82
  };
81
83
 
82
- } // namespace rnwgpu
84
+ } // namespace rnwgpu
@@ -4,7 +4,11 @@
4
4
  #include <memory>
5
5
 
6
6
  #ifdef __APPLE__
7
- #include "dawn/native/MetalBackend.h"
7
+ namespace dawn::native::metal {
8
+
9
+ void WaitForCommandsToBeScheduled(WGPUDevice device);
10
+
11
+ }
8
12
  #endif
9
13
 
10
14
  namespace rnwgpu {
@@ -10,8 +10,6 @@
10
10
 
11
11
  #include "RNFHybridObject.h"
12
12
 
13
- #include "AsyncRunner.h"
14
-
15
13
  #include "Canvas.h"
16
14
  #include "GPU.h"
17
15
  #include "GPUCanvasConfiguration.h"
@@ -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
@@ -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 "GPUBuffer.h"
@@ -107,4 +105,4 @@ private:
107
105
  std::string _label;
108
106
  };
109
107
 
110
- } // namespace rnwgpu
108
+ } // 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
  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 {
@@ -32,4 +30,4 @@ private:
32
30
  wgpu::CompilationMessage _instance;
33
31
  };
34
32
 
35
- } // namespace rnwgpu
33
+ } // 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"
@@ -83,4 +81,4 @@ private:
83
81
  std::string _label;
84
82
  };
85
83
 
86
- } // namespace rnwgpu
84
+ } // 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