react-native-wgpu 0.4.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. package/README.md +31 -0
  2. package/android/CMakeLists.txt +3 -3
  3. package/cpp/jsi/{RNFEnumMapper.h → EnumMapper.h} +2 -6
  4. package/cpp/jsi/{RNFJSIConverter.h → JSIConverter.h} +27 -110
  5. package/cpp/jsi/NativeObject.h +607 -0
  6. package/cpp/jsi/{RNFPromise.cpp → Promise.cpp} +3 -6
  7. package/cpp/jsi/{RNFPromise.h → Promise.h} +2 -5
  8. package/cpp/jsi/RuntimeAwareCache.cpp +7 -0
  9. package/cpp/jsi/RuntimeAwareCache.h +100 -0
  10. package/cpp/jsi/RuntimeLifecycleMonitor.cpp +72 -0
  11. package/cpp/jsi/RuntimeLifecycleMonitor.h +32 -0
  12. package/cpp/rnwgpu/ArrayBuffer.h +8 -12
  13. package/cpp/rnwgpu/RNWebGPUManager.cpp +187 -28
  14. package/cpp/rnwgpu/RNWebGPUManager.h +7 -0
  15. package/cpp/rnwgpu/api/Canvas.h +14 -12
  16. package/cpp/rnwgpu/api/GPU.cpp +4 -6
  17. package/cpp/rnwgpu/api/GPU.h +13 -11
  18. package/cpp/rnwgpu/api/GPUAdapter.cpp +9 -9
  19. package/cpp/rnwgpu/api/GPUAdapter.h +14 -11
  20. package/cpp/rnwgpu/api/GPUAdapterInfo.h +17 -15
  21. package/cpp/rnwgpu/api/GPUBindGroup.h +11 -10
  22. package/cpp/rnwgpu/api/GPUBindGroupLayout.h +12 -11
  23. package/cpp/rnwgpu/api/GPUBuffer.h +19 -16
  24. package/cpp/rnwgpu/api/GPUCanvasContext.h +17 -13
  25. package/cpp/rnwgpu/api/GPUCommandBuffer.h +12 -10
  26. package/cpp/rnwgpu/api/GPUCommandEncoder.h +35 -32
  27. package/cpp/rnwgpu/api/GPUCompilationInfo.h +19 -19
  28. package/cpp/rnwgpu/api/GPUCompilationMessage.h +10 -7
  29. package/cpp/rnwgpu/api/GPUComputePassEncoder.h +28 -27
  30. package/cpp/rnwgpu/api/GPUComputePipeline.h +14 -13
  31. package/cpp/rnwgpu/api/GPUDevice.cpp +111 -95
  32. package/cpp/rnwgpu/api/GPUDevice.h +51 -43
  33. package/cpp/rnwgpu/api/GPUDeviceLostInfo.h +12 -10
  34. package/cpp/rnwgpu/api/GPUError.h +19 -29
  35. package/cpp/rnwgpu/api/GPUExtent3D.h +7 -10
  36. package/cpp/rnwgpu/api/GPUExternalTexture.h +12 -11
  37. package/cpp/rnwgpu/api/GPUInternalError.h +31 -0
  38. package/cpp/rnwgpu/api/GPUOrigin2D.h +6 -10
  39. package/cpp/rnwgpu/api/GPUOrigin3D.h +6 -10
  40. package/cpp/rnwgpu/api/GPUOutOfMemoryError.h +33 -0
  41. package/cpp/rnwgpu/api/GPUPipelineLayout.h +12 -10
  42. package/cpp/rnwgpu/api/GPUQuerySet.h +14 -12
  43. package/cpp/rnwgpu/api/GPUQueue.h +18 -17
  44. package/cpp/rnwgpu/api/GPURenderBundle.h +11 -10
  45. package/cpp/rnwgpu/api/GPURenderBundleEncoder.h +36 -33
  46. package/cpp/rnwgpu/api/GPURenderPassEncoder.h +49 -47
  47. package/cpp/rnwgpu/api/GPURenderPipeline.h +14 -12
  48. package/cpp/rnwgpu/api/GPUSampler.h +11 -10
  49. package/cpp/rnwgpu/api/GPUShaderModule.cpp +7 -8
  50. package/cpp/rnwgpu/api/GPUShaderModule.h +13 -12
  51. package/cpp/rnwgpu/api/GPUSupportedLimits.h +73 -93
  52. package/cpp/rnwgpu/api/GPUTexture.h +24 -20
  53. package/cpp/rnwgpu/api/GPUTextureView.h +11 -10
  54. package/cpp/rnwgpu/api/GPUValidationError.h +32 -0
  55. package/cpp/rnwgpu/api/ImageBitmap.h +10 -6
  56. package/cpp/rnwgpu/api/RNWebGPU.h +21 -21
  57. package/cpp/rnwgpu/api/descriptors/GPUBindGroupDescriptor.h +3 -7
  58. package/cpp/rnwgpu/api/descriptors/GPUBindGroupEntry.h +7 -11
  59. package/cpp/rnwgpu/api/descriptors/GPUBindGroupLayoutDescriptor.h +3 -7
  60. package/cpp/rnwgpu/api/descriptors/GPUBindGroupLayoutEntry.h +3 -7
  61. package/cpp/rnwgpu/api/descriptors/GPUBlendComponent.h +3 -8
  62. package/cpp/rnwgpu/api/descriptors/GPUBlendState.h +3 -7
  63. package/cpp/rnwgpu/api/descriptors/GPUBufferBinding.h +3 -7
  64. package/cpp/rnwgpu/api/descriptors/GPUBufferBindingLayout.h +3 -8
  65. package/cpp/rnwgpu/api/descriptors/GPUBufferDescriptor.h +3 -8
  66. package/cpp/rnwgpu/api/descriptors/GPUBufferUsage.h +28 -32
  67. package/cpp/rnwgpu/api/descriptors/GPUCanvasConfiguration.h +3 -7
  68. package/cpp/rnwgpu/api/descriptors/GPUColor.h +3 -8
  69. package/cpp/rnwgpu/api/descriptors/GPUColorTargetState.h +3 -7
  70. package/cpp/rnwgpu/api/descriptors/GPUColorWrite.h +18 -20
  71. package/cpp/rnwgpu/api/descriptors/GPUCommandBufferDescriptor.h +3 -8
  72. package/cpp/rnwgpu/api/descriptors/GPUCommandEncoderDescriptor.h +3 -8
  73. package/cpp/rnwgpu/api/descriptors/GPUComputePassDescriptor.h +3 -7
  74. package/cpp/rnwgpu/api/descriptors/GPUComputePassTimestampWrites.h +3 -7
  75. package/cpp/rnwgpu/api/descriptors/GPUComputePipelineDescriptor.h +3 -7
  76. package/cpp/rnwgpu/api/descriptors/GPUDepthStencilState.h +3 -7
  77. package/cpp/rnwgpu/api/descriptors/GPUDeviceDescriptor.h +3 -7
  78. package/cpp/rnwgpu/api/descriptors/GPUExternalTextureBindingLayout.h +3 -8
  79. package/cpp/rnwgpu/api/descriptors/GPUExternalTextureDescriptor.h +3 -7
  80. package/cpp/rnwgpu/api/descriptors/GPUFragmentState.h +3 -7
  81. package/cpp/rnwgpu/api/descriptors/GPUImageCopyBuffer.h +3 -7
  82. package/cpp/rnwgpu/api/descriptors/GPUImageCopyExternalImage.h +3 -7
  83. package/cpp/rnwgpu/api/descriptors/GPUImageCopyTexture.h +3 -7
  84. package/cpp/rnwgpu/api/descriptors/GPUImageCopyTextureTagged.h +3 -7
  85. package/cpp/rnwgpu/api/descriptors/GPUImageDataLayout.h +3 -8
  86. package/cpp/rnwgpu/api/descriptors/GPUMapMode.h +11 -14
  87. package/cpp/rnwgpu/api/descriptors/GPUMultisampleState.h +3 -8
  88. package/cpp/rnwgpu/api/descriptors/GPUPipelineLayoutDescriptor.h +3 -7
  89. package/cpp/rnwgpu/api/descriptors/GPUPrimitiveState.h +3 -8
  90. package/cpp/rnwgpu/api/descriptors/GPUProgrammableStage.h +3 -7
  91. package/cpp/rnwgpu/api/descriptors/GPUQuerySetDescriptor.h +3 -8
  92. package/cpp/rnwgpu/api/descriptors/GPUQueueDescriptor.h +3 -8
  93. package/cpp/rnwgpu/api/descriptors/GPURenderBundleDescriptor.h +3 -8
  94. package/cpp/rnwgpu/api/descriptors/GPURenderBundleEncoderDescriptor.h +3 -8
  95. package/cpp/rnwgpu/api/descriptors/GPURenderPassColorAttachment.h +3 -7
  96. package/cpp/rnwgpu/api/descriptors/GPURenderPassDepthStencilAttachment.h +3 -7
  97. package/cpp/rnwgpu/api/descriptors/GPURenderPassDescriptor.h +3 -7
  98. package/cpp/rnwgpu/api/descriptors/GPURenderPassTimestampWrites.h +3 -7
  99. package/cpp/rnwgpu/api/descriptors/GPURenderPipelineDescriptor.h +3 -7
  100. package/cpp/rnwgpu/api/descriptors/GPURequestAdapterOptions.h +3 -8
  101. package/cpp/rnwgpu/api/descriptors/GPUSamplerBindingLayout.h +3 -8
  102. package/cpp/rnwgpu/api/descriptors/GPUSamplerDescriptor.h +3 -8
  103. package/cpp/rnwgpu/api/descriptors/GPUShaderModuleCompilationHint.h +3 -7
  104. package/cpp/rnwgpu/api/descriptors/GPUShaderModuleDescriptor.h +3 -7
  105. package/cpp/rnwgpu/api/descriptors/GPUShaderStage.h +14 -16
  106. package/cpp/rnwgpu/api/descriptors/GPUStencilFaceState.h +3 -8
  107. package/cpp/rnwgpu/api/descriptors/GPUStorageTextureBindingLayout.h +3 -8
  108. package/cpp/rnwgpu/api/descriptors/GPUTextureBindingLayout.h +3 -8
  109. package/cpp/rnwgpu/api/descriptors/GPUTextureDescriptor.h +3 -7
  110. package/cpp/rnwgpu/api/descriptors/GPUTextureUsage.h +18 -29
  111. package/cpp/rnwgpu/api/descriptors/GPUTextureViewDescriptor.h +3 -8
  112. package/cpp/rnwgpu/api/descriptors/GPUUncapturedErrorEventInit.h +3 -7
  113. package/cpp/rnwgpu/api/descriptors/GPUVertexAttribute.h +3 -8
  114. package/cpp/rnwgpu/api/descriptors/GPUVertexBufferLayout.h +3 -7
  115. package/cpp/rnwgpu/api/descriptors/GPUVertexState.h +3 -7
  116. package/cpp/rnwgpu/api/descriptors/Unions.h +3 -3
  117. package/cpp/rnwgpu/async/AsyncTaskHandle.cpp +10 -10
  118. package/cpp/rnwgpu/async/AsyncTaskHandle.h +2 -2
  119. package/lib/commonjs/external/ModuleProxy.js +36 -0
  120. package/lib/commonjs/external/ModuleProxy.js.map +1 -0
  121. package/lib/commonjs/external/index.js +17 -0
  122. package/lib/commonjs/external/index.js.map +1 -0
  123. package/lib/commonjs/external/reanimated/ReanimatedProxy.js +18 -0
  124. package/lib/commonjs/external/reanimated/ReanimatedProxy.js.map +1 -0
  125. package/lib/commonjs/external/reanimated/index.js +21 -0
  126. package/lib/commonjs/external/reanimated/index.js.map +1 -0
  127. package/lib/commonjs/external/reanimated/registerWebGPUForReanimated.js +50 -0
  128. package/lib/commonjs/external/reanimated/registerWebGPUForReanimated.js.map +1 -0
  129. package/lib/commonjs/main/index.js +2 -142
  130. package/lib/commonjs/main/index.js.map +1 -1
  131. package/lib/module/external/ModuleProxy.js +28 -0
  132. package/lib/module/external/ModuleProxy.js.map +1 -0
  133. package/lib/module/external/index.js +2 -0
  134. package/lib/module/external/index.js.map +1 -0
  135. package/lib/module/external/reanimated/ReanimatedProxy.js +12 -0
  136. package/lib/module/external/reanimated/ReanimatedProxy.js.map +1 -0
  137. package/lib/module/external/reanimated/index.js +3 -0
  138. package/lib/module/external/reanimated/index.js.map +1 -0
  139. package/lib/module/external/reanimated/registerWebGPUForReanimated.js +43 -0
  140. package/lib/module/external/reanimated/registerWebGPUForReanimated.js.map +1 -0
  141. package/lib/module/main/index.js +2 -141
  142. package/lib/module/main/index.js.map +1 -1
  143. package/lib/typescript/babel.config.d.ts +1 -0
  144. package/lib/typescript/lib/commonjs/external/ModuleProxy.d.ts +12 -0
  145. package/lib/typescript/lib/commonjs/external/ModuleProxy.d.ts.map +1 -0
  146. package/lib/typescript/lib/commonjs/external/index.d.ts +2 -0
  147. package/lib/typescript/lib/commonjs/external/index.d.ts.map +1 -0
  148. package/lib/typescript/lib/commonjs/external/reanimated/ReanimatedProxy.d.ts +6 -0
  149. package/lib/typescript/lib/commonjs/external/reanimated/ReanimatedProxy.d.ts.map +1 -0
  150. package/lib/typescript/lib/commonjs/external/reanimated/index.d.ts +4 -0
  151. package/lib/typescript/lib/commonjs/external/reanimated/index.d.ts.map +1 -0
  152. package/lib/typescript/lib/commonjs/external/reanimated/registerWebGPUForReanimated.d.ts +9 -0
  153. package/lib/typescript/lib/commonjs/external/reanimated/registerWebGPUForReanimated.d.ts.map +1 -0
  154. package/lib/typescript/lib/module/external/ModuleProxy.d.ts +7 -0
  155. package/lib/typescript/lib/module/external/ModuleProxy.d.ts.map +1 -0
  156. package/lib/typescript/lib/module/external/index.d.ts +2 -0
  157. package/lib/typescript/lib/module/external/index.d.ts.map +1 -0
  158. package/lib/typescript/lib/module/external/reanimated/ReanimatedProxy.d.ts +5 -0
  159. package/lib/typescript/lib/module/external/reanimated/ReanimatedProxy.d.ts.map +1 -0
  160. package/lib/typescript/lib/module/external/reanimated/index.d.ts +3 -0
  161. package/lib/typescript/lib/module/external/reanimated/index.d.ts.map +1 -0
  162. package/lib/typescript/lib/module/external/reanimated/registerWebGPUForReanimated.d.ts +2 -0
  163. package/lib/typescript/lib/module/external/reanimated/registerWebGPUForReanimated.d.ts.map +1 -0
  164. package/lib/typescript/src/external/ModuleProxy.d.ts +11 -0
  165. package/lib/typescript/src/external/ModuleProxy.d.ts.map +1 -0
  166. package/lib/typescript/src/external/index.d.ts +2 -0
  167. package/lib/typescript/src/external/index.d.ts.map +1 -0
  168. package/lib/typescript/src/external/reanimated/ReanimatedProxy.d.ts +4 -0
  169. package/lib/typescript/src/external/reanimated/ReanimatedProxy.d.ts.map +1 -0
  170. package/lib/typescript/src/external/reanimated/index.d.ts +3 -0
  171. package/lib/typescript/src/external/reanimated/index.d.ts.map +1 -0
  172. package/lib/typescript/src/external/reanimated/registerWebGPUForReanimated.d.ts +8 -0
  173. package/lib/typescript/src/external/reanimated/registerWebGPUForReanimated.d.ts.map +1 -0
  174. package/package.json +16 -3
  175. package/src/external/ModuleProxy.ts +30 -0
  176. package/src/external/index.ts +1 -0
  177. package/src/external/reanimated/ReanimatedProxy.ts +19 -0
  178. package/src/external/reanimated/index.ts +2 -0
  179. package/src/external/reanimated/registerWebGPUForReanimated.ts +43 -0
  180. package/src/main/index.tsx +3 -170
  181. package/cpp/jsi/RNFHybridObject.cpp +0 -150
  182. package/cpp/jsi/RNFHybridObject.h +0 -181
  183. package/cpp/jsi/RNFJSIHelper.h +0 -51
  184. package/cpp/jsi/RNFPointerHolder.h +0 -95
  185. package/cpp/jsi/RNFRuntimeState.cpp +0 -18
  186. package/cpp/jsi/RNFRuntimeState.h +0 -106
@@ -0,0 +1,100 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+
5
+ #include <cassert>
6
+ #include <memory>
7
+ #include <unordered_map>
8
+ #include <utility>
9
+
10
+ #include "RuntimeLifecycleMonitor.h"
11
+
12
+ namespace rnwgpu {
13
+
14
+ namespace jsi = facebook::jsi;
15
+
16
+ class BaseRuntimeAwareCache {
17
+ public:
18
+ static void setMainJsRuntime(jsi::Runtime *rt) { _mainRuntime = rt; }
19
+ static jsi::Runtime *getMainJsRuntime() {
20
+ assert(_mainRuntime != nullptr &&
21
+ "Expected main Javascript runtime to be set in the "
22
+ "BaseRuntimeAwareCache class.");
23
+
24
+ return _mainRuntime;
25
+ }
26
+
27
+ private:
28
+ static jsi::Runtime *_mainRuntime;
29
+ };
30
+
31
+ /**
32
+ * Provides a way to keep data specific to a jsi::Runtime instance that gets
33
+ * cleaned up when that runtime is destroyed. This is necessary because JSI does
34
+ * not allow for its associated objects to be retained past the runtime
35
+ * lifetime. If an object (e.g. jsi::Values or jsi::Function instances) is kept
36
+ * after the runtime is torn down, its destructor (once it is destroyed
37
+ * eventually) will result in a crash (JSI objects keep a pointer to memory
38
+ * managed by the runtime, accessing that portion of the memory after runtime is
39
+ * deleted is the root cause of that crash).
40
+ *
41
+ * In order to provide an efficient implementation that does not add an overhead
42
+ * for the cases when only a single runtime is used, which is the primary
43
+ * usecase, the following assumption has been made: Only for secondary runtimes
44
+ * we track destruction and clean up the store associated with that runtime. For
45
+ * the first runtime we assume that the object holding the store is destroyed
46
+ * prior to the destruction of that runtime.
47
+ *
48
+ * The above assumption makes it work without any overhead when only single
49
+ * runtime is in use. Specifically, we don't perform any additional operations
50
+ * related to tracking runtime lifecycle when only a single runtime is used.
51
+ */
52
+ template <typename T>
53
+ class RuntimeAwareCache : public BaseRuntimeAwareCache,
54
+ public RuntimeLifecycleListener {
55
+
56
+ public:
57
+ void onRuntimeDestroyed(jsi::Runtime *rt) override {
58
+ if (getMainJsRuntime() != rt) {
59
+ // We are removing a secondary runtime
60
+ _secondaryRuntimeCaches.erase(rt);
61
+ }
62
+ }
63
+
64
+ ~RuntimeAwareCache() {
65
+ for (auto &cache : _secondaryRuntimeCaches) {
66
+ RuntimeLifecycleMonitor::removeListener(
67
+ *static_cast<jsi::Runtime *>(cache.first), this);
68
+ }
69
+ }
70
+
71
+ T &get(jsi::Runtime &rt) {
72
+ // We check if we're accessing the main runtime - this is the happy path
73
+ // to avoid us having to lookup by runtime for caches that only has a single
74
+ // runtime
75
+ if (getMainJsRuntime() == &rt) {
76
+ return _primaryCache;
77
+ } else {
78
+ if (_secondaryRuntimeCaches.count(&rt) == 0) {
79
+ // we only add listener when the secondary runtime is used, this assumes
80
+ // that the secondary runtime is terminated first. This lets us avoid
81
+ // additional complexity for the majority of cases when objects are not
82
+ // shared between runtimes. Otherwise we'd have to register all objecrts
83
+ // with the RuntimeMonitor as opposed to only registering ones that are
84
+ // used in secondary runtime. Note that we can't register listener here
85
+ // with the primary runtime as it may run on a separate thread.
86
+ RuntimeLifecycleMonitor::addListener(rt, this);
87
+
88
+ T cache;
89
+ _secondaryRuntimeCaches.emplace(&rt, std::move(cache));
90
+ }
91
+ }
92
+ return _secondaryRuntimeCaches.at(&rt);
93
+ }
94
+
95
+ private:
96
+ std::unordered_map<void *, T> _secondaryRuntimeCaches;
97
+ T _primaryCache;
98
+ };
99
+
100
+ } // namespace rnwgpu
@@ -0,0 +1,72 @@
1
+ #include "RuntimeLifecycleMonitor.h"
2
+
3
+ #include <mutex>
4
+ #include <unordered_map>
5
+ #include <unordered_set>
6
+ #include <utility>
7
+
8
+ namespace rnwgpu {
9
+
10
+ static std::unordered_map<jsi::Runtime *,
11
+ std::unordered_set<RuntimeLifecycleListener *>>
12
+ listeners;
13
+ static std::mutex listenersMutex;
14
+
15
+ struct RuntimeLifecycleMonitorObject : public jsi::HostObject {
16
+ jsi::Runtime *_rt;
17
+ explicit RuntimeLifecycleMonitorObject(jsi::Runtime *rt) : _rt(rt) {}
18
+ ~RuntimeLifecycleMonitorObject() {
19
+ std::unordered_set<RuntimeLifecycleListener *> listenersCopy;
20
+ {
21
+ std::lock_guard<std::mutex> lock(listenersMutex);
22
+ auto listenersSet = listeners.find(_rt);
23
+ if (listenersSet != listeners.end()) {
24
+ listenersCopy = listenersSet->second;
25
+ listeners.erase(listenersSet);
26
+ }
27
+ }
28
+ for (auto listener : listenersCopy) {
29
+ listener->onRuntimeDestroyed(_rt);
30
+ }
31
+ }
32
+ };
33
+
34
+ void RuntimeLifecycleMonitor::addListener(jsi::Runtime &rt,
35
+ RuntimeLifecycleListener *listener) {
36
+ bool shouldInstallMonitor = false;
37
+ {
38
+ std::lock_guard<std::mutex> lock(listenersMutex);
39
+ auto listenersSet = listeners.find(&rt);
40
+ if (listenersSet == listeners.end()) {
41
+ std::unordered_set<RuntimeLifecycleListener *> newSet;
42
+ newSet.insert(listener);
43
+ listeners.emplace(&rt, std::move(newSet));
44
+ shouldInstallMonitor = true;
45
+ } else {
46
+ listenersSet->second.insert(listener);
47
+ }
48
+ }
49
+ if (shouldInstallMonitor) {
50
+ // We install a global host object in the provided runtime, this way we can
51
+ // use that host object destructor to get notified when the runtime is being
52
+ // terminated. We use a unique name for the object as it gets saved with the
53
+ // runtime's global object.
54
+ rt.global().setProperty(
55
+ rt, "__rnwgpu_rt_lifecycle_monitor",
56
+ jsi::Object::createFromHostObject(
57
+ rt, std::make_shared<RuntimeLifecycleMonitorObject>(&rt)));
58
+ }
59
+ }
60
+
61
+ void RuntimeLifecycleMonitor::removeListener(
62
+ jsi::Runtime &rt, RuntimeLifecycleListener *listener) {
63
+ std::lock_guard<std::mutex> lock(listenersMutex);
64
+ auto listenersSet = listeners.find(&rt);
65
+ if (listenersSet == listeners.end()) {
66
+ // nothing to do here
67
+ } else {
68
+ listenersSet->second.erase(listener);
69
+ }
70
+ }
71
+
72
+ } // namespace rnwgpu
@@ -0,0 +1,32 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+
5
+ #include <memory>
6
+
7
+ namespace rnwgpu {
8
+
9
+ namespace jsi = facebook::jsi;
10
+
11
+ /**
12
+ * Listener interface that allows for getting notified when a jsi::Runtime
13
+ * instance is destroyed.
14
+ */
15
+ struct RuntimeLifecycleListener {
16
+ virtual ~RuntimeLifecycleListener() {}
17
+ virtual void onRuntimeDestroyed(jsi::Runtime *) = 0;
18
+ };
19
+
20
+ /**
21
+ * This class provides an API via static methods for registering and
22
+ * unregistering runtime lifecycle listeners. The listeners can be used to
23
+ * cleanup any data that references a given jsi::Runtime instance before it gets
24
+ * destroyed.
25
+ */
26
+ struct RuntimeLifecycleMonitor {
27
+ static void addListener(jsi::Runtime &rt, RuntimeLifecycleListener *listener);
28
+ static void removeListener(jsi::Runtime &rt,
29
+ RuntimeLifecycleListener *listener);
30
+ };
31
+
32
+ } // namespace rnwgpu
@@ -3,7 +3,7 @@
3
3
 
4
4
  #include <memory>
5
5
 
6
- #include "RNFJSIConverter.h"
6
+ #include "JSIConverter.h"
7
7
 
8
8
  namespace rnwgpu {
9
9
 
@@ -24,21 +24,17 @@ struct ArrayBuffer : jsi::MutableBuffer {
24
24
  size_t _bytesPerElement;
25
25
  };
26
26
 
27
- } // namespace rnwgpu
28
-
29
- namespace margelo {
30
-
31
- static std::shared_ptr<rnwgpu::ArrayBuffer>
27
+ static std::shared_ptr<ArrayBuffer>
32
28
  createArrayBufferFromJSI(jsi::Runtime &runtime,
33
29
  const jsi::ArrayBuffer &arrayBuffer,
34
30
  size_t bytesPerElement) {
35
31
  auto size = arrayBuffer.size(runtime);
36
- return std::make_shared<rnwgpu::ArrayBuffer>(arrayBuffer.data(runtime), size,
37
- bytesPerElement);
32
+ return std::make_shared<ArrayBuffer>(arrayBuffer.data(runtime), size,
33
+ bytesPerElement);
38
34
  }
39
35
 
40
- template <> struct JSIConverter<std::shared_ptr<rnwgpu::ArrayBuffer>> {
41
- static std::shared_ptr<rnwgpu::ArrayBuffer>
36
+ template <> struct JSIConverter<std::shared_ptr<ArrayBuffer>> {
37
+ static std::shared_ptr<ArrayBuffer>
42
38
  fromJSI(jsi::Runtime &runtime, const jsi::Value &arg, bool outOfBound) {
43
39
  if (arg.isObject()) {
44
40
  auto obj = arg.getObject(runtime);
@@ -64,9 +60,9 @@ template <> struct JSIConverter<std::shared_ptr<rnwgpu::ArrayBuffer>> {
64
60
  }
65
61
 
66
62
  static jsi::Value toJSI(jsi::Runtime &runtime,
67
- std::shared_ptr<rnwgpu::ArrayBuffer> arg) {
63
+ std::shared_ptr<ArrayBuffer> arg) {
68
64
  return jsi::ArrayBuffer(runtime, arg);
69
65
  }
70
66
  };
71
67
 
72
- } // namespace margelo
68
+ } // namespace rnwgpu
@@ -1,8 +1,42 @@
1
1
  #include "RNWebGPUManager.h"
2
2
 
3
3
  #include "GPU.h"
4
+ #include "NativeObject.h"
4
5
  #include "RNWebGPU.h"
5
6
 
7
+ // GPU API classes (for instanceof support)
8
+ #include "GPUAdapter.h"
9
+ #include "GPUAdapterInfo.h"
10
+ #include "GPUBindGroup.h"
11
+ #include "GPUBindGroupLayout.h"
12
+ #include "GPUBuffer.h"
13
+ #include "GPUCanvasContext.h"
14
+ #include "GPUCommandBuffer.h"
15
+ #include "GPUCommandEncoder.h"
16
+ #include "GPUCompilationInfo.h"
17
+ #include "GPUCompilationMessage.h"
18
+ #include "GPUComputePassEncoder.h"
19
+ #include "GPUComputePipeline.h"
20
+ #include "GPUDevice.h"
21
+ #include "GPUDeviceLostInfo.h"
22
+ #include "GPUError.h"
23
+ #include "GPUExternalTexture.h"
24
+ #include "GPUInternalError.h"
25
+ #include "GPUOutOfMemoryError.h"
26
+ #include "GPUPipelineLayout.h"
27
+ #include "GPUQuerySet.h"
28
+ #include "GPUQueue.h"
29
+ #include "GPURenderBundle.h"
30
+ #include "GPURenderBundleEncoder.h"
31
+ #include "GPURenderPassEncoder.h"
32
+ #include "GPURenderPipeline.h"
33
+ #include "GPUSampler.h"
34
+ #include "GPUShaderModule.h"
35
+ #include "GPUSupportedLimits.h"
36
+ #include "GPUTexture.h"
37
+ #include "GPUTextureView.h"
38
+ #include "GPUValidationError.h"
39
+
6
40
  // Enums
7
41
  #include "GPUBufferUsage.h"
8
42
  #include "GPUColorWrite.h"
@@ -22,37 +56,162 @@ RNWebGPUManager::RNWebGPUManager(
22
56
  : _jsRuntime(jsRuntime), _jsCallInvoker(jsCallInvoker),
23
57
  _platformContext(platformContext) {
24
58
 
59
+ // Register main runtime for RuntimeAwareCache
60
+ BaseRuntimeAwareCache::setMainJsRuntime(_jsRuntime);
61
+
25
62
  auto gpu = std::make_shared<GPU>(*_jsRuntime);
26
63
  auto rnWebGPU = std::make_shared<RNWebGPU>(gpu, _platformContext);
27
64
  _gpu = gpu->get();
28
- _jsRuntime->global().setProperty(
29
- *_jsRuntime, "RNWebGPU",
30
- jsi::Object::createFromHostObject(*_jsRuntime, rnWebGPU));
31
-
32
- auto bufferUsage = std::make_shared<GPUBufferUsage>();
33
- _jsRuntime->global().setProperty(
34
- *_jsRuntime, "GPUBufferUsage",
35
- jsi::Object::createFromHostObject(*_jsRuntime, std::move(bufferUsage)));
36
-
37
- auto colorWrite = std::make_shared<GPUColorWrite>();
38
- _jsRuntime->global().setProperty(
39
- *_jsRuntime, "GPUColorWrite",
40
- jsi::Object::createFromHostObject(*_jsRuntime, std::move(colorWrite)));
41
-
42
- auto mapMode = std::make_shared<GPUMapMode>();
43
- _jsRuntime->global().setProperty(
44
- *_jsRuntime, "GPUMapMode",
45
- jsi::Object::createFromHostObject(*_jsRuntime, std::move(mapMode)));
46
-
47
- auto shaderStage = std::make_shared<GPUShaderStage>();
48
- _jsRuntime->global().setProperty(
49
- *_jsRuntime, "GPUShaderStage",
50
- jsi::Object::createFromHostObject(*_jsRuntime, std::move(shaderStage)));
51
-
52
- auto textureUsage = std::make_shared<GPUTextureUsage>();
53
- _jsRuntime->global().setProperty(
54
- *_jsRuntime, "GPUTextureUsage",
55
- jsi::Object::createFromHostObject(*_jsRuntime, std::move(textureUsage)));
65
+ _jsRuntime->global().setProperty(*_jsRuntime, "RNWebGPU",
66
+ RNWebGPU::create(*_jsRuntime, rnWebGPU));
67
+
68
+ // Install constructors for instanceof support
69
+ GPU::installConstructor(*_jsRuntime);
70
+ GPUAdapter::installConstructor(*_jsRuntime);
71
+ GPUAdapterInfo::installConstructor(*_jsRuntime);
72
+ GPUBindGroup::installConstructor(*_jsRuntime);
73
+ GPUBindGroupLayout::installConstructor(*_jsRuntime);
74
+ GPUBuffer::installConstructor(*_jsRuntime);
75
+ GPUCanvasContext::installConstructor(*_jsRuntime);
76
+ GPUCommandBuffer::installConstructor(*_jsRuntime);
77
+ GPUCommandEncoder::installConstructor(*_jsRuntime);
78
+ GPUCompilationInfo::installConstructor(*_jsRuntime);
79
+ GPUCompilationMessage::installConstructor(*_jsRuntime);
80
+ GPUComputePassEncoder::installConstructor(*_jsRuntime);
81
+ GPUComputePipeline::installConstructor(*_jsRuntime);
82
+ GPUDevice::installConstructor(*_jsRuntime);
83
+ GPUDeviceLostInfo::installConstructor(*_jsRuntime);
84
+ GPUError::installConstructor(*_jsRuntime);
85
+ GPUExternalTexture::installConstructor(*_jsRuntime);
86
+ GPUInternalError::installConstructor(*_jsRuntime);
87
+ GPUOutOfMemoryError::installConstructor(*_jsRuntime);
88
+ GPUValidationError::installConstructor(*_jsRuntime);
89
+ GPUPipelineLayout::installConstructor(*_jsRuntime);
90
+ GPUQuerySet::installConstructor(*_jsRuntime);
91
+ GPUQueue::installConstructor(*_jsRuntime);
92
+ GPURenderBundle::installConstructor(*_jsRuntime);
93
+ GPURenderBundleEncoder::installConstructor(*_jsRuntime);
94
+ GPURenderPassEncoder::installConstructor(*_jsRuntime);
95
+ GPURenderPipeline::installConstructor(*_jsRuntime);
96
+ GPUSampler::installConstructor(*_jsRuntime);
97
+ GPUShaderModule::installConstructor(*_jsRuntime);
98
+ GPUSupportedLimits::installConstructor(*_jsRuntime);
99
+ GPUTexture::installConstructor(*_jsRuntime);
100
+ GPUTextureView::installConstructor(*_jsRuntime);
101
+
102
+ // Install constant objects as plain JS objects with own properties
103
+ _jsRuntime->global().setProperty(*_jsRuntime, "GPUBufferUsage",
104
+ GPUBufferUsage::create(*_jsRuntime));
105
+ _jsRuntime->global().setProperty(*_jsRuntime, "GPUColorWrite",
106
+ GPUColorWrite::create(*_jsRuntime));
107
+ _jsRuntime->global().setProperty(*_jsRuntime, "GPUMapMode",
108
+ GPUMapMode::create(*_jsRuntime));
109
+ _jsRuntime->global().setProperty(*_jsRuntime, "GPUShaderStage",
110
+ GPUShaderStage::create(*_jsRuntime));
111
+ _jsRuntime->global().setProperty(*_jsRuntime, "GPUTextureUsage",
112
+ GPUTextureUsage::create(*_jsRuntime));
113
+
114
+ // Install global helper functions for Worklets serialization
115
+ // These are standalone functions that don't require RNWebGPU instance
116
+ installWebGPUWorkletHelpers(*_jsRuntime);
117
+ }
118
+
119
+ void RNWebGPUManager::installWebGPUWorkletHelpers(jsi::Runtime &runtime) {
120
+ // __webgpuIsWebGPUObject - checks if a value is a WebGPU NativeObject
121
+ auto isWebGPUObjectFunc = jsi::Function::createFromHostFunction(
122
+ runtime, jsi::PropNameID::forUtf8(runtime, "__webgpuIsWebGPUObject"), 1,
123
+ [](jsi::Runtime &rt, const jsi::Value & /*thisVal*/,
124
+ const jsi::Value *args, size_t count) -> jsi::Value {
125
+ if (count < 1 || !args[0].isObject()) {
126
+ return jsi::Value(false);
127
+ }
128
+ auto obj = args[0].getObject(rt);
129
+
130
+ // Check if it has native state
131
+ if (!obj.hasNativeState(rt)) {
132
+ return jsi::Value(false);
133
+ }
134
+
135
+ // Check if it has Symbol.toStringTag on its prototype (WebGPU objects
136
+ // do)
137
+ auto objectCtor = rt.global().getPropertyAsObject(rt, "Object");
138
+ auto getPrototypeOf =
139
+ objectCtor.getPropertyAsFunction(rt, "getPrototypeOf");
140
+ auto proto = getPrototypeOf.call(rt, obj);
141
+
142
+ if (!proto.isObject()) {
143
+ return jsi::Value(false);
144
+ }
145
+
146
+ auto protoObj = proto.getObject(rt);
147
+ auto symbolCtor = rt.global().getPropertyAsObject(rt, "Symbol");
148
+ auto toStringTag = symbolCtor.getProperty(rt, "toStringTag");
149
+ if (toStringTag.isUndefined()) {
150
+ return jsi::Value(false);
151
+ }
152
+
153
+ auto getOwnPropertyDescriptor =
154
+ objectCtor.getPropertyAsFunction(rt, "getOwnPropertyDescriptor");
155
+ auto desc = getOwnPropertyDescriptor.call(rt, protoObj, toStringTag);
156
+ return jsi::Value(desc.isObject());
157
+ });
158
+ runtime.global().setProperty(runtime, "__webgpuIsWebGPUObject",
159
+ std::move(isWebGPUObjectFunc));
160
+
161
+ // __webgpuBox - boxes a WebGPU object for Worklets serialization
162
+ auto boxFunc = jsi::Function::createFromHostFunction(
163
+ runtime, jsi::PropNameID::forUtf8(runtime, "__webgpuBox"), 1,
164
+ [](jsi::Runtime &rt, const jsi::Value & /*thisVal*/,
165
+ const jsi::Value *args, size_t count) -> jsi::Value {
166
+ if (count < 1 || !args[0].isObject()) {
167
+ throw jsi::JSError(rt,
168
+ "__webgpuBox() requires a WebGPU object argument");
169
+ }
170
+
171
+ auto obj = args[0].getObject(rt);
172
+
173
+ // Check if it has native state
174
+ if (!obj.hasNativeState(rt)) {
175
+ throw jsi::JSError(
176
+ rt, "Object has no native state - not a WebGPU object");
177
+ }
178
+
179
+ // Get the brand name from Symbol.toStringTag on the prototype
180
+ auto objectCtor = rt.global().getPropertyAsObject(rt, "Object");
181
+ auto getPrototypeOf =
182
+ objectCtor.getPropertyAsFunction(rt, "getPrototypeOf");
183
+ auto proto = getPrototypeOf.call(rt, obj);
184
+
185
+ std::string brand;
186
+ if (proto.isObject()) {
187
+ auto protoObj = proto.getObject(rt);
188
+ auto symbolCtor = rt.global().getPropertyAsObject(rt, "Symbol");
189
+ auto toStringTag = symbolCtor.getProperty(rt, "toStringTag");
190
+ if (!toStringTag.isUndefined()) {
191
+ auto getOwnPropertyDescriptor = objectCtor.getPropertyAsFunction(
192
+ rt, "getOwnPropertyDescriptor");
193
+ auto desc =
194
+ getOwnPropertyDescriptor.call(rt, protoObj, toStringTag);
195
+ if (desc.isObject()) {
196
+ auto descObj = desc.getObject(rt);
197
+ auto value = descObj.getProperty(rt, "value");
198
+ if (value.isString()) {
199
+ brand = value.getString(rt).utf8(rt);
200
+ }
201
+ }
202
+ }
203
+ }
204
+
205
+ if (brand.empty()) {
206
+ throw jsi::JSError(rt, "Cannot determine WebGPU object type - no "
207
+ "Symbol.toStringTag found");
208
+ }
209
+
210
+ auto nativeState = obj.getNativeState(rt);
211
+ auto boxed = std::make_shared<BoxedWebGPUObject>(nativeState, brand);
212
+ return jsi::Object::createFromHostObject(rt, boxed);
213
+ });
214
+ runtime.global().setProperty(runtime, "__webgpuBox", std::move(boxFunc));
56
215
  }
57
216
 
58
217
  RNWebGPUManager::~RNWebGPUManager() {
@@ -27,6 +27,13 @@ public:
27
27
  std::shared_ptr<PlatformContext> platformContext);
28
28
  ~RNWebGPUManager();
29
29
 
30
+ /**
31
+ * Install global helper functions for Worklets serialization.
32
+ * This installs __webgpuIsWebGPUObject and __webgpuBox on the global object.
33
+ * Can be called on any runtime (main JS, UI, or custom worklet runtimes).
34
+ */
35
+ static void installWebGPUWorkletHelpers(jsi::Runtime &runtime);
36
+
30
37
  private:
31
38
  jsi::Runtime *_jsRuntime;
32
39
  std::shared_ptr<facebook::react::CallInvoker> _jsCallInvoker;
@@ -7,16 +7,18 @@
7
7
 
8
8
  #include "webgpu/webgpu_cpp.h"
9
9
 
10
- #include "RNFHybridObject.h"
10
+ #include "NativeObject.h"
11
11
 
12
12
  namespace rnwgpu {
13
13
 
14
- namespace m = margelo;
14
+ namespace jsi = facebook::jsi;
15
15
 
16
- class Canvas : public m::HybridObject {
16
+ class Canvas : public NativeObject<Canvas> {
17
17
  public:
18
+ static constexpr const char *CLASS_NAME = "Canvas";
19
+
18
20
  explicit Canvas(void *surface, const int width, const int height)
19
- : HybridObject("Canvas"), _surface(surface), _width(width),
21
+ : NativeObject(CLASS_NAME), _surface(surface), _width(width),
20
22
  _height(height), _clientWidth(width), _clientHeight(height) {}
21
23
 
22
24
  int getWidth() { return _width; }
@@ -34,14 +36,14 @@ public:
34
36
 
35
37
  void *getSurface() { return _surface; }
36
38
 
37
- void loadHybridMethods() override {
38
- registerHybridGetter("surface", &Canvas::getSurface, this);
39
- registerHybridGetter("width", &Canvas::getWidth, this);
40
- registerHybridGetter("height", &Canvas::getHeight, this);
41
- registerHybridGetter("clientWidth", &Canvas::getClientWidth, this);
42
- registerHybridGetter("clientHeight", &Canvas::getClientHeight, this);
43
- registerHybridSetter("width", &Canvas::setWidth, this);
44
- registerHybridSetter("height", &Canvas::setHeight, this);
39
+ static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
40
+ installGetter(runtime, prototype, "surface", &Canvas::getSurface);
41
+ installGetterSetter(runtime, prototype, "width", &Canvas::getWidth,
42
+ &Canvas::setWidth);
43
+ installGetterSetter(runtime, prototype, "height", &Canvas::getHeight,
44
+ &Canvas::setHeight);
45
+ installGetter(runtime, prototype, "clientWidth", &Canvas::getClientWidth);
46
+ installGetter(runtime, prototype, "clientHeight", &Canvas::getClientHeight);
45
47
  }
46
48
 
47
49
  private:
@@ -8,12 +8,12 @@
8
8
  #include <vector>
9
9
 
10
10
  #include "Convertors.h"
11
- #include "RNFJSIConverter.h"
11
+ #include "JSIConverter.h"
12
12
  #include "rnwgpu/async/JSIMicrotaskDispatcher.h"
13
13
 
14
14
  namespace rnwgpu {
15
15
 
16
- GPU::GPU(jsi::Runtime &runtime) : HybridObject("GPU") {
16
+ GPU::GPU(jsi::Runtime &runtime) : NativeObject(CLASS_NAME) {
17
17
  static const auto kTimedWaitAny = wgpu::InstanceFeatureName::TimedWaitAny;
18
18
  wgpu::InstanceDescriptor instanceDesc{.requiredFeatureCount = 1,
19
19
  .requiredFeatures = &kTimedWaitAny};
@@ -59,8 +59,7 @@ async::AsyncTaskHandle GPU::requestAdapter(
59
59
  adapterHost);
60
60
  resolve([result =
61
61
  std::move(result)](jsi::Runtime &runtime) mutable {
62
- return margelo::JSIConverter<decltype(result)>::toJSI(runtime,
63
- result);
62
+ return JSIConverter<decltype(result)>::toJSI(runtime, result);
64
63
  });
65
64
  } else {
66
65
  auto result =
@@ -68,8 +67,7 @@ async::AsyncTaskHandle GPU::requestAdapter(
68
67
  nullptr);
69
68
  resolve([result =
70
69
  std::move(result)](jsi::Runtime &runtime) mutable {
71
- return margelo::JSIConverter<decltype(result)>::toJSI(runtime,
72
- result);
70
+ return JSIConverter<decltype(result)>::toJSI(runtime, result);
73
71
  });
74
72
  }
75
73
  });
@@ -7,7 +7,7 @@
7
7
 
8
8
  #include "Unions.h"
9
9
 
10
- #include "RNFHybridObject.h"
10
+ #include "NativeObject.h"
11
11
 
12
12
  #include "rnwgpu/async/AsyncRunner.h"
13
13
  #include "rnwgpu/async/AsyncTaskHandle.h"
@@ -21,14 +21,16 @@
21
21
 
22
22
  namespace rnwgpu {
23
23
 
24
- namespace m = margelo;
24
+ namespace jsi = facebook::jsi;
25
25
 
26
- class GPU : public m::HybridObject {
26
+ class GPU : public NativeObject<GPU> {
27
27
  public:
28
+ static constexpr const char *CLASS_NAME = "GPU";
29
+
28
30
  explicit GPU(jsi::Runtime &runtime);
29
31
 
30
32
  public:
31
- std::string getBrand() { return _name; }
33
+ std::string getBrand() { return CLASS_NAME; }
32
34
 
33
35
  async::AsyncTaskHandle requestAdapter(
34
36
  std::optional<std::shared_ptr<GPURequestAdapterOptions>> options);
@@ -36,13 +38,13 @@ public:
36
38
 
37
39
  std::unordered_set<std::string> getWgslLanguageFeatures();
38
40
 
39
- void loadHybridMethods() override {
40
- registerHybridGetter("__brand", &GPU::getBrand, this);
41
- registerHybridMethod("requestAdapter", &GPU::requestAdapter, this);
42
- registerHybridMethod("getPreferredCanvasFormat",
43
- &GPU::getPreferredCanvasFormat, this);
44
- registerHybridGetter("wgslLanguageFeatures", &GPU::getWgslLanguageFeatures,
45
- this);
41
+ static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
42
+ installGetter(runtime, prototype, "__brand", &GPU::getBrand);
43
+ installMethod(runtime, prototype, "requestAdapter", &GPU::requestAdapter);
44
+ installMethod(runtime, prototype, "getPreferredCanvasFormat",
45
+ &GPU::getPreferredCanvasFormat);
46
+ installGetter(runtime, prototype, "wgslLanguageFeatures",
47
+ &GPU::getWgslLanguageFeatures);
46
48
  }
47
49
 
48
50
  inline const wgpu::Instance get() { return _instance; }
@@ -10,7 +10,7 @@
10
10
  #include "Convertors.h"
11
11
 
12
12
  #include "GPUFeatures.h"
13
- #include "RNFJSIConverter.h"
13
+ #include "JSIConverter.h"
14
14
  #include "WGPULogger.h"
15
15
 
16
16
  namespace rnwgpu {
@@ -82,16 +82,16 @@ async::AsyncTaskHandle GPUAdapter::requestDevice(
82
82
  auto creationRuntime = getCreationRuntime();
83
83
  return _async->postTask(
84
84
  [this, aDescriptor, descriptor, label = std::move(label),
85
- deviceLostBinding, creationRuntime](
86
- const async::AsyncTaskHandle::ResolveFunction &resolve,
87
- const async::AsyncTaskHandle::RejectFunction &reject) {
85
+ deviceLostBinding,
86
+ creationRuntime](const async::AsyncTaskHandle::ResolveFunction &resolve,
87
+ const async::AsyncTaskHandle::RejectFunction &reject) {
88
88
  (void)descriptor;
89
89
  _instance.RequestDevice(
90
90
  &aDescriptor, wgpu::CallbackMode::AllowProcessEvents,
91
- [asyncRunner = _async, resolve, reject, label,
92
- creationRuntime, deviceLostBinding](
93
- wgpu::RequestDeviceStatus status, wgpu::Device device,
94
- wgpu::StringView message) mutable {
91
+ [asyncRunner = _async, resolve, reject, label, creationRuntime,
92
+ deviceLostBinding](wgpu::RequestDeviceStatus status,
93
+ wgpu::Device device,
94
+ wgpu::StringView message) mutable {
95
95
  if (message.length) {
96
96
  fprintf(stderr, "%s", message.data);
97
97
  }
@@ -140,7 +140,7 @@ async::AsyncTaskHandle GPUAdapter::requestDevice(
140
140
  *deviceLostBinding = deviceHost;
141
141
  resolve([deviceHost = std::move(deviceHost)](
142
142
  jsi::Runtime &runtime) mutable {
143
- return margelo::JSIConverter<std::shared_ptr<GPUDevice>>::toJSI(
143
+ return JSIConverter<std::shared_ptr<GPUDevice>>::toJSI(
144
144
  runtime, deviceHost);
145
145
  });
146
146
  });