react-native-wgpu 0.1.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 (208) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +31 -0
  3. package/android/CMakeLists.txt +74 -0
  4. package/android/build.gradle +166 -0
  5. package/android/cpp/cpp-adapter.cpp +12 -0
  6. package/android/gradle.properties +5 -0
  7. package/android/src/main/AndroidManifest.xml +3 -0
  8. package/android/src/main/AndroidManifestNew.xml +2 -0
  9. package/android/src/main/java/com/webgpu/WebGPUModule.java +39 -0
  10. package/android/src/main/java/com/webgpu/WebGPUView.java +24 -0
  11. package/android/src/main/java/com/webgpu/WebGPUViewManager.java +31 -0
  12. package/android/src/main/java/com/webgpu/WebGPUViewPackage.java +26 -0
  13. package/android/src/newarch/WgpuViewManagerSpec.java +24 -0
  14. package/android/src/oldarch/WebGPUViewManagerSpec.java +11 -0
  15. package/cpp/Logger.h +82 -0
  16. package/cpp/dawn/dawn_proc_table.h +308 -0
  17. package/cpp/dawn/webgpu.h +4201 -0
  18. package/cpp/dawn/webgpu_cpp.h +8985 -0
  19. package/cpp/dawn/webgpu_cpp_print.h +2460 -0
  20. package/cpp/dawn/wire/client/webgpu.h +339 -0
  21. package/cpp/dawn/wire/client/webgpu_cpp.h +9140 -0
  22. package/cpp/jsi/RNFEnumMapper.h +49 -0
  23. package/cpp/jsi/RNFHybridObject.cpp +145 -0
  24. package/cpp/jsi/RNFHybridObject.h +162 -0
  25. package/cpp/jsi/RNFJSIConverter.h +412 -0
  26. package/cpp/jsi/RNFJSIHelper.h +49 -0
  27. package/cpp/jsi/RNFPointerHolder.h +95 -0
  28. package/cpp/jsi/RNFPromise.cpp +45 -0
  29. package/cpp/jsi/RNFPromise.h +38 -0
  30. package/cpp/jsi/RNFRuntimeCache.cpp +57 -0
  31. package/cpp/jsi/RNFRuntimeCache.h +79 -0
  32. package/cpp/jsi/RNFWorkletRuntimeCollector.h +43 -0
  33. package/cpp/jsi/RNFWorkletRuntimeRegistry.cpp +11 -0
  34. package/cpp/jsi/RNFWorkletRuntimeRegistry.h +44 -0
  35. package/cpp/rnwgpu/MutableBuffer.h +48 -0
  36. package/cpp/rnwgpu/RNWebGPUManager.cpp +60 -0
  37. package/cpp/rnwgpu/RNWebGPUManager.h +30 -0
  38. package/cpp/rnwgpu/api/GPU.cpp +40 -0
  39. package/cpp/rnwgpu/api/GPU.h +43 -0
  40. package/cpp/rnwgpu/api/GPUAdapter.cpp +33 -0
  41. package/cpp/rnwgpu/api/GPUAdapter.h +40 -0
  42. package/cpp/rnwgpu/api/GPUAdapterInfo.h +33 -0
  43. package/cpp/rnwgpu/api/GPUBindGroup.h +38 -0
  44. package/cpp/rnwgpu/api/GPUBindGroupLayout.h +39 -0
  45. package/cpp/rnwgpu/api/GPUBuffer.cpp +16 -0
  46. package/cpp/rnwgpu/api/GPUBuffer.h +44 -0
  47. package/cpp/rnwgpu/api/GPUCanvasContext.h +33 -0
  48. package/cpp/rnwgpu/api/GPUCommandBuffer.h +38 -0
  49. package/cpp/rnwgpu/api/GPUCommandEncoder.h +38 -0
  50. package/cpp/rnwgpu/api/GPUCompilationInfo.h +33 -0
  51. package/cpp/rnwgpu/api/GPUCompilationMessage.h +33 -0
  52. package/cpp/rnwgpu/api/GPUComputePassEncoder.h +40 -0
  53. package/cpp/rnwgpu/api/GPUComputePipeline.h +39 -0
  54. package/cpp/rnwgpu/api/GPUDevice.cpp +12 -0
  55. package/cpp/rnwgpu/api/GPUDevice.h +45 -0
  56. package/cpp/rnwgpu/api/GPUDeviceLostInfo.h +33 -0
  57. package/cpp/rnwgpu/api/GPUExternalTexture.h +39 -0
  58. package/cpp/rnwgpu/api/GPUPipelineLayout.h +38 -0
  59. package/cpp/rnwgpu/api/GPUQuerySet.h +38 -0
  60. package/cpp/rnwgpu/api/GPUQueue.h +38 -0
  61. package/cpp/rnwgpu/api/GPURenderBundle.h +38 -0
  62. package/cpp/rnwgpu/api/GPURenderBundleEncoder.h +40 -0
  63. package/cpp/rnwgpu/api/GPURenderPassEncoder.h +40 -0
  64. package/cpp/rnwgpu/api/GPURenderPipeline.h +38 -0
  65. package/cpp/rnwgpu/api/GPUSampler.h +38 -0
  66. package/cpp/rnwgpu/api/GPUShaderModule.h +38 -0
  67. package/cpp/rnwgpu/api/GPUSupportedLimits.h +33 -0
  68. package/cpp/rnwgpu/api/GPUTexture.h +38 -0
  69. package/cpp/rnwgpu/api/GPUTextureView.h +38 -0
  70. package/cpp/rnwgpu/api/GPUUncapturedErrorEvent.h +33 -0
  71. package/cpp/rnwgpu/api/descriptors/GPUBindGroupDescriptor.h +81 -0
  72. package/cpp/rnwgpu/api/descriptors/GPUBindGroupEntry.h +67 -0
  73. package/cpp/rnwgpu/api/descriptors/GPUBindGroupLayoutDescriptor.h +69 -0
  74. package/cpp/rnwgpu/api/descriptors/GPUBindGroupLayoutEntry.h +95 -0
  75. package/cpp/rnwgpu/api/descriptors/GPUBlendComponent.h +56 -0
  76. package/cpp/rnwgpu/api/descriptors/GPUBlendState.h +65 -0
  77. package/cpp/rnwgpu/api/descriptors/GPUBufferBinding.h +72 -0
  78. package/cpp/rnwgpu/api/descriptors/GPUBufferBindingLayout.h +62 -0
  79. package/cpp/rnwgpu/api/descriptors/GPUBufferDescriptor.h +85 -0
  80. package/cpp/rnwgpu/api/descriptors/GPUBufferUsage.h +43 -0
  81. package/cpp/rnwgpu/api/descriptors/GPUCanvasConfiguration.h +93 -0
  82. package/cpp/rnwgpu/api/descriptors/GPUColorDict.h +81 -0
  83. package/cpp/rnwgpu/api/descriptors/GPUColorTargetState.h +69 -0
  84. package/cpp/rnwgpu/api/descriptors/GPUColorWrite.h +31 -0
  85. package/cpp/rnwgpu/api/descriptors/GPUComputePassDescriptor.h +62 -0
  86. package/cpp/rnwgpu/api/descriptors/GPUComputePassTimestampWrites.h +80 -0
  87. package/cpp/rnwgpu/api/descriptors/GPUComputePipelineDescriptor.h +82 -0
  88. package/cpp/rnwgpu/api/descriptors/GPUDepthStencilState.h +126 -0
  89. package/cpp/rnwgpu/api/descriptors/GPUDeviceDescriptor.h +69 -0
  90. package/cpp/rnwgpu/api/descriptors/GPUExtent3DDict.h +76 -0
  91. package/cpp/rnwgpu/api/descriptors/GPUExtent3DDictStrict.h +91 -0
  92. package/cpp/rnwgpu/api/descriptors/GPUExternalTextureBindingLayout.h +44 -0
  93. package/cpp/rnwgpu/api/descriptors/GPUExternalTextureDescriptor.h +75 -0
  94. package/cpp/rnwgpu/api/descriptors/GPUFragmentState.h +85 -0
  95. package/cpp/rnwgpu/api/descriptors/GPUImageCopyBuffer.h +83 -0
  96. package/cpp/rnwgpu/api/descriptors/GPUImageCopyExternalImage.h +66 -0
  97. package/cpp/rnwgpu/api/descriptors/GPUImageCopyTexture.h +74 -0
  98. package/cpp/rnwgpu/api/descriptors/GPUImageCopyTextureTagged.h +88 -0
  99. package/cpp/rnwgpu/api/descriptors/GPUImageDataLayout.h +70 -0
  100. package/cpp/rnwgpu/api/descriptors/GPUMapMode.h +25 -0
  101. package/cpp/rnwgpu/api/descriptors/GPUMultisampleState.h +68 -0
  102. package/cpp/rnwgpu/api/descriptors/GPUOrigin2DDict.h +61 -0
  103. package/cpp/rnwgpu/api/descriptors/GPUOrigin2DDictStrict.h +75 -0
  104. package/cpp/rnwgpu/api/descriptors/GPUOrigin3DDict.h +71 -0
  105. package/cpp/rnwgpu/api/descriptors/GPUPipelineErrorInit.h +54 -0
  106. package/cpp/rnwgpu/api/descriptors/GPUPipelineLayoutDescriptor.h +72 -0
  107. package/cpp/rnwgpu/api/descriptors/GPUPrimitiveState.h +66 -0
  108. package/cpp/rnwgpu/api/descriptors/GPUProgrammableStage.h +72 -0
  109. package/cpp/rnwgpu/api/descriptors/GPUQuerySetDescriptor.h +81 -0
  110. package/cpp/rnwgpu/api/descriptors/GPURenderBundleEncoderDescriptor.h +102 -0
  111. package/cpp/rnwgpu/api/descriptors/GPURenderPassColorAttachment.h +105 -0
  112. package/cpp/rnwgpu/api/descriptors/GPURenderPassDepthStencilAttachment.h +118 -0
  113. package/cpp/rnwgpu/api/descriptors/GPURenderPassDescriptor.h +100 -0
  114. package/cpp/rnwgpu/api/descriptors/GPURenderPassLayout.h +83 -0
  115. package/cpp/rnwgpu/api/descriptors/GPURenderPassTimestampWrites.h +80 -0
  116. package/cpp/rnwgpu/api/descriptors/GPURenderPipelineDescriptor.h +104 -0
  117. package/cpp/rnwgpu/api/descriptors/GPURequestAdapterOptions.h +56 -0
  118. package/cpp/rnwgpu/api/descriptors/GPUSamplerBindingLayout.h +48 -0
  119. package/cpp/rnwgpu/api/descriptors/GPUSamplerDescriptor.h +116 -0
  120. package/cpp/rnwgpu/api/descriptors/GPUShaderModuleCompilationHint.h +64 -0
  121. package/cpp/rnwgpu/api/descriptors/GPUShaderModuleDescriptor.h +88 -0
  122. package/cpp/rnwgpu/api/descriptors/GPUShaderStage.h +27 -0
  123. package/cpp/rnwgpu/api/descriptors/GPUStencilFaceState.h +61 -0
  124. package/cpp/rnwgpu/api/descriptors/GPUStorageTextureBindingLayout.h +67 -0
  125. package/cpp/rnwgpu/api/descriptors/GPUTextureBindingLayout.h +58 -0
  126. package/cpp/rnwgpu/api/descriptors/GPUTextureDescriptor.h +123 -0
  127. package/cpp/rnwgpu/api/descriptors/GPUTextureUsage.h +40 -0
  128. package/cpp/rnwgpu/api/descriptors/GPUTextureViewDescriptor.h +113 -0
  129. package/cpp/rnwgpu/api/descriptors/GPUUncapturedErrorEventInit.h +71 -0
  130. package/cpp/rnwgpu/api/descriptors/GPUVertexAttribute.h +80 -0
  131. package/cpp/rnwgpu/api/descriptors/GPUVertexBufferLayout.h +73 -0
  132. package/cpp/rnwgpu/api/descriptors/GPUVertexState.h +77 -0
  133. package/cpp/rnwgpu/api/descriptors/Unions.h +1645 -0
  134. package/cpp/webgpu/webgpu.h +33 -0
  135. package/cpp/webgpu/webgpu_cpp.h +33 -0
  136. package/cpp/webgpu/webgpu_cpp_chained_struct.h +55 -0
  137. package/cpp/webgpu/webgpu_enum_class_bitmasks.h +161 -0
  138. package/ios/RNFAppleLogger.mm +22 -0
  139. package/ios/Utils.h +5 -0
  140. package/ios/Utils.m +26 -0
  141. package/ios/WebGPUModule.h +18 -0
  142. package/ios/WebGPUModule.mm +77 -0
  143. package/ios/WebGPUView.h +15 -0
  144. package/ios/WebGPUView.mm +58 -0
  145. package/ios/WebGPUViewManager.mm +21 -0
  146. package/lib/commonjs/WebGPUNativeModule.js +10 -0
  147. package/lib/commonjs/WebGPUNativeModule.js.map +1 -0
  148. package/lib/commonjs/WebGPUViewNativeComponent.js +11 -0
  149. package/lib/commonjs/WebGPUViewNativeComponent.js.map +1 -0
  150. package/lib/commonjs/index.js +45 -0
  151. package/lib/commonjs/index.js.map +1 -0
  152. package/lib/module/WebGPUNativeModule.js +4 -0
  153. package/lib/module/WebGPUNativeModule.js.map +1 -0
  154. package/lib/module/WebGPUViewNativeComponent.js +4 -0
  155. package/lib/module/WebGPUViewNativeComponent.js.map +1 -0
  156. package/lib/module/index.js +9 -0
  157. package/lib/module/index.js.map +1 -0
  158. package/lib/typescript/babel.config.d.ts +2 -0
  159. package/lib/typescript/babel.config.d.ts.map +1 -0
  160. package/lib/typescript/jest.config.d.ts +5 -0
  161. package/lib/typescript/jest.config.d.ts.map +1 -0
  162. package/lib/typescript/lib/commonjs/WebGPUNativeModule.d.ts +5 -0
  163. package/lib/typescript/lib/commonjs/WebGPUNativeModule.d.ts.map +1 -0
  164. package/lib/typescript/lib/commonjs/WebGPUViewNativeComponent.d.ts +4 -0
  165. package/lib/typescript/lib/commonjs/WebGPUViewNativeComponent.d.ts.map +1 -0
  166. package/lib/typescript/lib/commonjs/index.d.ts +5 -0
  167. package/lib/typescript/lib/commonjs/index.d.ts.map +1 -0
  168. package/lib/typescript/lib/module/WebGPUNativeModule.d.ts +3 -0
  169. package/lib/typescript/lib/module/WebGPUNativeModule.d.ts.map +1 -0
  170. package/lib/typescript/lib/module/WebGPUViewNativeComponent.d.ts +4 -0
  171. package/lib/typescript/lib/module/WebGPUViewNativeComponent.d.ts.map +1 -0
  172. package/lib/typescript/lib/module/index.d.ts +5 -0
  173. package/lib/typescript/lib/module/index.d.ts.map +1 -0
  174. package/lib/typescript/scripts/build/copy-artifacts.d.ts +2 -0
  175. package/lib/typescript/scripts/build/copy-artifacts.d.ts.map +1 -0
  176. package/lib/typescript/scripts/build/dawn.d.ts +2 -0
  177. package/lib/typescript/scripts/build/dawn.d.ts.map +1 -0
  178. package/lib/typescript/scripts/build/util.d.ts +13 -0
  179. package/lib/typescript/scripts/build/util.d.ts.map +1 -0
  180. package/lib/typescript/scripts/codegen/codegen.d.ts +2 -0
  181. package/lib/typescript/scripts/codegen/codegen.d.ts.map +1 -0
  182. package/lib/typescript/scripts/codegen/model/dawn.d.ts +2 -0
  183. package/lib/typescript/scripts/codegen/model/dawn.d.ts.map +1 -0
  184. package/lib/typescript/scripts/codegen/model/model.d.ts +8 -0
  185. package/lib/typescript/scripts/codegen/model/model.d.ts.map +1 -0
  186. package/lib/typescript/scripts/codegen/templates/Descriptor.d.ts +4 -0
  187. package/lib/typescript/scripts/codegen/templates/Descriptor.d.ts.map +1 -0
  188. package/lib/typescript/scripts/codegen/templates/Enum.d.ts +3 -0
  189. package/lib/typescript/scripts/codegen/templates/Enum.d.ts.map +1 -0
  190. package/lib/typescript/scripts/codegen/templates/HybridObject.d.ts +3 -0
  191. package/lib/typescript/scripts/codegen/templates/HybridObject.d.ts.map +1 -0
  192. package/lib/typescript/scripts/codegen/templates/Unions.d.ts +6 -0
  193. package/lib/typescript/scripts/codegen/templates/Unions.d.ts.map +1 -0
  194. package/lib/typescript/scripts/codegen/templates/common.d.ts +9 -0
  195. package/lib/typescript/scripts/codegen/templates/common.d.ts.map +1 -0
  196. package/lib/typescript/scripts/codegen/util.d.ts +2 -0
  197. package/lib/typescript/scripts/codegen/util.d.ts.map +1 -0
  198. package/lib/typescript/src/WebGPUNativeModule.d.ts +7 -0
  199. package/lib/typescript/src/WebGPUNativeModule.d.ts.map +1 -0
  200. package/lib/typescript/src/WebGPUViewNativeComponent.d.ts +8 -0
  201. package/lib/typescript/src/WebGPUViewNativeComponent.d.ts.map +1 -0
  202. package/lib/typescript/src/index.d.ts +9 -0
  203. package/lib/typescript/src/index.d.ts.map +1 -0
  204. package/package.json +120 -0
  205. package/react-native-webgpu.podspec +56 -0
  206. package/src/WebGPUNativeModule.ts +9 -0
  207. package/src/WebGPUViewNativeComponent.ts +9 -0
  208. package/src/index.tsx +13 -0
@@ -0,0 +1,57 @@
1
+ //
2
+ // Created by Marc Rousavy on 22.02.24.
3
+ //
4
+ #include <jsi/jsi.h>
5
+
6
+ #include <memory>
7
+ #include <unordered_map>
8
+ #include <unordered_set>
9
+ #include <utility>
10
+
11
+ #include "RNFRuntimeCache.h"
12
+
13
+ namespace margelo {
14
+
15
+ static std::unordered_map<jsi::Runtime*, std::unordered_set<RuntimeLifecycleListener*>> listeners;
16
+
17
+ struct RuntimeLifecycleMonitorObject : public jsi::HostObject {
18
+ jsi::Runtime* _rt;
19
+ explicit RuntimeLifecycleMonitorObject(jsi::Runtime* rt) : _rt(rt) {}
20
+ ~RuntimeLifecycleMonitorObject() {
21
+ auto listenersSet = listeners.find(_rt);
22
+ if (listenersSet != listeners.end()) {
23
+ for (auto listener : listenersSet->second) {
24
+ listener->onRuntimeDestroyed(_rt);
25
+ }
26
+ listeners.erase(listenersSet);
27
+ }
28
+ }
29
+ };
30
+
31
+ void RuntimeLifecycleMonitor::addListener(jsi::Runtime& rt, RuntimeLifecycleListener* listener) {
32
+ auto listenersSet = listeners.find(&rt);
33
+ if (listenersSet == listeners.end()) {
34
+ // We install a global host object in the provided runtime, this way we can
35
+ // use that host object destructor to get notified when the runtime is being
36
+ // terminated. We use a unique name for the object as it gets saved with the
37
+ // runtime's global object.
38
+ rt.global().setProperty(rt, "__rnfl_rt_lifecycle_monitor",
39
+ jsi::Object::createFromHostObject(rt, std::make_shared<RuntimeLifecycleMonitorObject>(&rt)));
40
+ std::unordered_set<RuntimeLifecycleListener*> newSet;
41
+ newSet.insert(listener);
42
+ listeners.emplace(&rt, std::move(newSet));
43
+ } else {
44
+ listenersSet->second.insert(listener);
45
+ }
46
+ }
47
+
48
+ void RuntimeLifecycleMonitor::removeListener(jsi::Runtime& rt, RuntimeLifecycleListener* listener) {
49
+ auto listenersSet = listeners.find(&rt);
50
+ if (listenersSet == listeners.end()) {
51
+ // nothing to do here
52
+ } else {
53
+ listenersSet->second.erase(listener);
54
+ }
55
+ }
56
+
57
+ } // namespace margelo
@@ -0,0 +1,79 @@
1
+ //
2
+ // Created by Marc Rousavy on 22.02.24.
3
+ //
4
+ #pragma once
5
+
6
+ #include <jsi/jsi.h>
7
+
8
+ #include <memory>
9
+ #include <unordered_map>
10
+ #include <unordered_set>
11
+ #include <utility>
12
+
13
+ namespace margelo {
14
+
15
+ namespace jsi = facebook::jsi;
16
+
17
+ /**
18
+ * Listener interface that allows for getting notified when a jsi::Runtime
19
+ * instance is destroyed.
20
+ */
21
+ struct RuntimeLifecycleListener {
22
+ virtual ~RuntimeLifecycleListener() {}
23
+ virtual void onRuntimeDestroyed(jsi::Runtime*) = 0;
24
+ };
25
+
26
+ /**
27
+ * This class provides an API via static methods for registering and
28
+ * unregistering runtime lifecycle listeners. The listeners can be used to
29
+ * cleanup any data that references a given jsi::Runtime instance before it gets
30
+ * destroyed.
31
+ */
32
+ struct RuntimeLifecycleMonitor {
33
+ static void addListener(jsi::Runtime& rt, RuntimeLifecycleListener* listener);
34
+ static void removeListener(jsi::Runtime& rt, RuntimeLifecycleListener* listener);
35
+ };
36
+
37
+ /**
38
+ * Provides a way to keep data specific to a jsi::Runtime instance that gets
39
+ * cleaned up when that runtime is destroyed. This is necessary because JSI does
40
+ * not allow for its associated objects to be retained past the runtime
41
+ * lifetime. If an object (e.g. jsi::Values or jsi::Function instances) is kept
42
+ * after the runtime is torn down, its destructor (once it is destroyed
43
+ * eventually) will result in a crash (JSI objects keep a pointer to memory
44
+ * managed by the runtime, accessing that portion of the memory after runtime is
45
+ * deleted is the root cause of that crash).
46
+ */
47
+ template <typename T> class RuntimeAwareCache : public RuntimeLifecycleListener {
48
+
49
+ public:
50
+ void onRuntimeDestroyed(jsi::Runtime* rt) override {
51
+ // A runtime has been destroyed, so destroy the related cache.
52
+ _runtimeCaches.erase(rt);
53
+ }
54
+
55
+ ~RuntimeAwareCache() {
56
+ for (auto& cache : _runtimeCaches) {
57
+ // remove all `onRuntimeDestroyed` listeners.
58
+ RuntimeLifecycleMonitor::removeListener(*cache.first, this);
59
+ }
60
+ }
61
+
62
+ T& get(jsi::Runtime& rt) {
63
+ if (_runtimeCaches.count(&rt) == 0) {
64
+ // This is the first time this Runtime has been accessed.
65
+ // We set up a `onRuntimeDestroyed` listener for it and
66
+ // initialize the cache map.
67
+ RuntimeLifecycleMonitor::addListener(rt, this);
68
+
69
+ T cache;
70
+ _runtimeCaches.emplace(&rt, std::move(cache));
71
+ }
72
+ return _runtimeCaches.at(&rt);
73
+ }
74
+
75
+ private:
76
+ std::unordered_map<jsi::Runtime*, T> _runtimeCaches;
77
+ };
78
+
79
+ } // namespace margelo
@@ -0,0 +1,43 @@
1
+ //
2
+ // Created by Marc Rousavy on 22.02.24.
3
+ //
4
+ #pragma once
5
+
6
+ #include "RNFWorkletRuntimeRegistry.h"
7
+
8
+ #include <jsi/jsi.h>
9
+
10
+ #include <memory>
11
+
12
+ namespace margelo {
13
+
14
+ // From:
15
+ // https://github.com/software-mansion/react-native-reanimated/blob/6cb1a66f1a68cac8079de2b6b305d22359847e51/Common/cpp/ReanimatedRuntime/WorkletRuntimeCollector.h
16
+ class WorkletRuntimeCollector : public jsi::HostObject {
17
+ // When worklet runtime is created, we inject an instance of this class as a
18
+ // `jsi::HostObject` into the global object. When worklet runtime is
19
+ // terminated, the object is garbage-collected, which runs the C++ destructor.
20
+ // In the destructor, we unregister the worklet runtime from the registry.
21
+
22
+ public:
23
+ explicit WorkletRuntimeCollector(jsi::Runtime& runtime) : _runtime(runtime) {
24
+ Logger::log("WorkletRuntimeCollector", "Registering WorkletRuntime %p", &runtime);
25
+ RNFWorkletRuntimeRegistry::registerRuntime(_runtime);
26
+ }
27
+
28
+ ~WorkletRuntimeCollector() {
29
+ Logger::log("WorkletRuntimeCollector", "Unregistering WorkletRuntime %p", &_runtime);
30
+ RNFWorkletRuntimeRegistry::unregisterRuntime(_runtime);
31
+ }
32
+
33
+ static void install(jsi::Runtime& rt) {
34
+ auto collector = std::make_shared<WorkletRuntimeCollector>(rt);
35
+ auto object = jsi::Object::createFromHostObject(rt, collector);
36
+ rt.global().setProperty(rt, "__workletRuntimeCollector", object);
37
+ }
38
+
39
+ private:
40
+ jsi::Runtime& _runtime;
41
+ };
42
+
43
+ } // namespace margelo
@@ -0,0 +1,11 @@
1
+ //
2
+ // Created by Marc Rousavy on 22.02.24.
3
+ //
4
+ #include "RNFWorkletRuntimeRegistry.h"
5
+
6
+ namespace margelo {
7
+
8
+ std::set<jsi::Runtime*> RNFWorkletRuntimeRegistry::registry_{};
9
+ std::mutex RNFWorkletRuntimeRegistry::mutex_{};
10
+
11
+ } // namespace margelo
@@ -0,0 +1,44 @@
1
+ //
2
+ // Created by Marc Rousavy on 22.02.24.
3
+ //
4
+ #pragma once
5
+
6
+ #include <jsi/jsi.h>
7
+
8
+ #include <mutex>
9
+ #include <set>
10
+
11
+ namespace jsi = facebook::jsi;
12
+
13
+ namespace margelo {
14
+
15
+ // From:
16
+ // https://github.com/software-mansion/react-native-reanimated/blob/6cb1a66f1a68cac8079de2b6b305d22359847e51/Common/cpp/ReanimatedRuntime/WorkletRuntimeRegistry.h
17
+ class RNFWorkletRuntimeRegistry {
18
+ private:
19
+ static std::set<jsi::Runtime*> registry_;
20
+ static std::mutex mutex_; // Protects `registry_`.
21
+
22
+ RNFWorkletRuntimeRegistry() {} // private ctor
23
+
24
+ static void registerRuntime(jsi::Runtime& runtime) {
25
+ std::lock_guard<std::mutex> lock(mutex_);
26
+ registry_.insert(&runtime);
27
+ }
28
+
29
+ static void unregisterRuntime(jsi::Runtime& runtime) {
30
+ std::lock_guard<std::mutex> lock(mutex_);
31
+ registry_.erase(&runtime);
32
+ }
33
+
34
+ friend class WorkletRuntimeCollector;
35
+
36
+ public:
37
+ static bool isRuntimeAlive(jsi::Runtime* runtime) {
38
+ assert(runtime != nullptr);
39
+ std::lock_guard<std::mutex> lock(mutex_);
40
+ return registry_.find(runtime) != registry_.end();
41
+ }
42
+ };
43
+
44
+ } // namespace margelo
@@ -0,0 +1,48 @@
1
+ #pragma once
2
+ #include <jsi/jsi.h>
3
+
4
+ #include <memory>
5
+
6
+ #include "RNFJSIConverter.h"
7
+
8
+ namespace rnwgpu {
9
+
10
+ namespace jsi = facebook::jsi;
11
+
12
+ struct MutableJSIBuffer : jsi::MutableBuffer {
13
+ MutableJSIBuffer(void *data, size_t size) : _data(data), _size(size) {}
14
+
15
+ size_t size() const override { return _size; }
16
+
17
+ uint8_t *data() override { return reinterpret_cast<uint8_t *>(_data); }
18
+
19
+ void *_data;
20
+ size_t _size;
21
+ };
22
+
23
+ } // namespace rnwgpu
24
+
25
+ namespace margelo {
26
+
27
+ template <> struct JSIConverter<std::shared_ptr<rnwgpu::MutableJSIBuffer>> {
28
+ static std::shared_ptr<rnwgpu::MutableJSIBuffer>
29
+ fromJSI(jsi::Runtime &runtime, const jsi::Value &arg, bool outOfBound) {
30
+ if (!arg.getObject(runtime).isArrayBuffer(runtime)) {
31
+ throw std::runtime_error(
32
+ "Buffer::fromJSI: argument is not an ArrayBuffer");
33
+ }
34
+ auto data = arg.getObject(runtime).getArrayBuffer(runtime);
35
+ auto result = std::make_unique<rnwgpu::MutableJSIBuffer>(
36
+ data.data(runtime), data.size(runtime));
37
+ return result;
38
+ }
39
+
40
+ static jsi::Value toJSI(jsi::Runtime &runtime,
41
+ std::shared_ptr<rnwgpu::MutableJSIBuffer> arg) {
42
+ auto val = jsi::ArrayBuffer(runtime, arg);
43
+ auto d = val.data(runtime);
44
+ return val;
45
+ }
46
+ };
47
+
48
+ } // namespace margelo
@@ -0,0 +1,60 @@
1
+ #include "RNWebGPUManager.h"
2
+
3
+ #include "GPU.h"
4
+ // Enums
5
+ #include "GPUBufferUsage.h"
6
+ #include "GPUColorWrite.h"
7
+ #include "GPUMapMode.h"
8
+ #include "GPUShaderStage.h"
9
+ #include "GPUTextureUsage.h"
10
+
11
+ #include <memory>
12
+ #include <utility>
13
+
14
+ namespace rnwgpu {
15
+ RNWebGPUManager::RNWebGPUManager(
16
+ jsi::Runtime *jsRuntime,
17
+ std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker)
18
+ : _jsRuntime(jsRuntime), _jsCallInvoker(jsCallInvoker) {
19
+
20
+ wgpu::InstanceDescriptor instanceDesc;
21
+ instanceDesc.features.timedWaitAnyEnable = true;
22
+ instanceDesc.features.timedWaitAnyMaxCount = 64;
23
+ auto gpu =
24
+ std::make_shared<GPU>(std::move(wgpu::CreateInstance(&instanceDesc)));
25
+
26
+ auto bufferUsage = std::make_shared<GPUBufferUsage>();
27
+ _jsRuntime->global().setProperty(
28
+ *_jsRuntime, "GPUBufferUsage",
29
+ jsi::Object::createFromHostObject(*_jsRuntime, std::move(bufferUsage)));
30
+
31
+ auto colorWrite = std::make_shared<GPUColorWrite>();
32
+ _jsRuntime->global().setProperty(
33
+ *_jsRuntime, "GPUColorWrite",
34
+ jsi::Object::createFromHostObject(*_jsRuntime, std::move(colorWrite)));
35
+
36
+ auto mapMode = std::make_shared<GPUMapMode>();
37
+ _jsRuntime->global().setProperty(
38
+ *_jsRuntime, "GPUMapMode",
39
+ jsi::Object::createFromHostObject(*_jsRuntime, std::move(mapMode)));
40
+
41
+ auto shaderStage = std::make_shared<GPUShaderStage>();
42
+ _jsRuntime->global().setProperty(
43
+ *_jsRuntime, "GPUShaderStage",
44
+ jsi::Object::createFromHostObject(*_jsRuntime, std::move(shaderStage)));
45
+
46
+ auto textureUsage = std::make_shared<GPUTextureUsage>();
47
+ _jsRuntime->global().setProperty(
48
+ *_jsRuntime, "GPUTextureUsage",
49
+ jsi::Object::createFromHostObject(*_jsRuntime, std::move(textureUsage)));
50
+
51
+ _jsRuntime->global().setProperty(
52
+ *_jsRuntime, "gpu",
53
+ jsi::Object::createFromHostObject(*_jsRuntime, std::move(gpu)));
54
+ }
55
+
56
+ RNWebGPUManager::~RNWebGPUManager() {
57
+ _jsRuntime = nullptr;
58
+ _jsCallInvoker = nullptr;
59
+ }
60
+ } // namespace rnwgpu
@@ -0,0 +1,30 @@
1
+ #pragma once
2
+
3
+ #include <memory>
4
+
5
+ namespace facebook {
6
+ namespace jsi {
7
+ class Runtime;
8
+ } // namespace jsi
9
+ namespace react {
10
+ class CallInvoker;
11
+ }
12
+ } // namespace facebook
13
+
14
+ namespace rnwgpu {
15
+
16
+ namespace jsi = facebook::jsi;
17
+ namespace react = facebook::react;
18
+
19
+ class RNWebGPUManager {
20
+ public:
21
+ RNWebGPUManager(jsi::Runtime *jsRuntime,
22
+ std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker);
23
+ ~RNWebGPUManager();
24
+
25
+ private:
26
+ jsi::Runtime *_jsRuntime;
27
+ std::shared_ptr<facebook::react::CallInvoker> _jsCallInvoker;
28
+ };
29
+
30
+ } // namespace rnwgpu
@@ -0,0 +1,40 @@
1
+ #include "GPU.h"
2
+ #include <utility>
3
+
4
+ namespace rnwgpu {
5
+
6
+ std::future<std::shared_ptr<GPUAdapter>>
7
+ GPU::requestAdapter(std::shared_ptr<GPURequestAdapterOptions> options) {
8
+ return std::async(std::launch::async, [this, options]() {
9
+ auto aOptions = options->getInstance();
10
+ wgpu::Adapter adapter = nullptr;
11
+ _instance.RequestAdapter(
12
+ aOptions,
13
+ [](WGPURequestAdapterStatus, WGPUAdapter cAdapter, const char *message,
14
+ void *userdata) {
15
+ if (message != nullptr) {
16
+ fprintf(stderr, "%s", message);
17
+ return;
18
+ }
19
+ *static_cast<wgpu::Adapter *>(userdata) =
20
+ wgpu::Adapter::Acquire(cAdapter);
21
+ },
22
+ &adapter);
23
+ // TODO: implement returning null jsi value
24
+ if (!adapter) {
25
+ throw std::runtime_error("Failed to request adapter");
26
+ }
27
+
28
+ return std::make_shared<GPUAdapter>(std::move(adapter));
29
+ });
30
+ }
31
+
32
+ wgpu::TextureFormat GPU::getPreferredCanvasFormat() {
33
+ #if defined(__ANDROID__)
34
+ return wgpu::TextureFormat::RGBA8Unorm;
35
+ #else
36
+ return wgpu::TextureFormat::BGRA8Unorm;
37
+ #endif // defined(__ANDROID__)
38
+ }
39
+
40
+ } // namespace rnwgpu
@@ -0,0 +1,43 @@
1
+ #pragma once
2
+
3
+ #include <future>
4
+ #include <memory>
5
+ #include <string>
6
+
7
+ #include "Unions.h"
8
+ #include <RNFHybridObject.h>
9
+
10
+ #include "MutableBuffer.h"
11
+
12
+ #include "webgpu/webgpu_cpp.h"
13
+
14
+ #include "GPUAdapter.h"
15
+ #include "GPURequestAdapterOptions.h"
16
+
17
+ namespace rnwgpu {
18
+
19
+ namespace m = margelo;
20
+
21
+ class GPU : public m::HybridObject {
22
+ public:
23
+ explicit GPU(wgpu::Instance instance)
24
+ : HybridObject("GPU"), _instance(instance) {}
25
+
26
+ public:
27
+ std::string getBrand() { return _name; }
28
+
29
+ std::future<std::shared_ptr<GPUAdapter>>
30
+ requestAdapter(std::shared_ptr<GPURequestAdapterOptions> options);
31
+ wgpu::TextureFormat getPreferredCanvasFormat();
32
+
33
+ void loadHybridMethods() override {
34
+ registerHybridGetter("__brand", &GPU::getBrand, this);
35
+ registerHybridMethod("requestAdapter", &GPU::requestAdapter, this);
36
+ registerHybridMethod("getPreferredCanvasFormat",
37
+ &GPU::getPreferredCanvasFormat, this);
38
+ }
39
+
40
+ private:
41
+ wgpu::Instance _instance;
42
+ };
43
+ } // namespace rnwgpu
@@ -0,0 +1,33 @@
1
+ // GPUAdapter.cpp
2
+
3
+ #include "GPUAdapter.h"
4
+ #include <utility>
5
+
6
+ namespace rnwgpu {
7
+
8
+ std::future<std::shared_ptr<GPUDevice>>
9
+ GPUAdapter::requestDevice(std::shared_ptr<GPUDeviceDescriptor> descriptor) {
10
+ return std::async(std::launch::async, [this, descriptor]() {
11
+ wgpu::Device device = nullptr;
12
+ auto aDescriptor = descriptor->getInstance();
13
+ _instance.RequestDevice(
14
+ aDescriptor,
15
+ [](WGPURequestDeviceStatus status, WGPUDevice cDevice,
16
+ const char *message, void *userdata) {
17
+ if (message != nullptr) {
18
+ fprintf(stderr, "%s", message);
19
+ return;
20
+ }
21
+ *static_cast<wgpu::Device *>(userdata) =
22
+ wgpu::Device::Acquire(cDevice);
23
+ },
24
+ &device);
25
+
26
+ if (!device) {
27
+ throw std::runtime_error("Failed to request device");
28
+ }
29
+ return std::make_shared<GPUDevice>(std::move(device), descriptor->label);
30
+ });
31
+ }
32
+
33
+ } // namespace rnwgpu
@@ -0,0 +1,40 @@
1
+ #pragma once
2
+
3
+ #include <future>
4
+ #include <memory>
5
+ #include <string>
6
+
7
+ #include "Unions.h"
8
+ #include <RNFHybridObject.h>
9
+
10
+ #include "MutableBuffer.h"
11
+
12
+ #include "webgpu/webgpu_cpp.h"
13
+
14
+ #include "GPUDevice.h"
15
+ #include "GPUDeviceDescriptor.h"
16
+
17
+ namespace rnwgpu {
18
+
19
+ namespace m = margelo;
20
+
21
+ class GPUAdapter : public m::HybridObject {
22
+ public:
23
+ explicit GPUAdapter(wgpu::Adapter instance)
24
+ : HybridObject("GPUAdapter"), _instance(instance) {}
25
+
26
+ public:
27
+ std::string getBrand() { return _name; }
28
+
29
+ std::future<std::shared_ptr<GPUDevice>>
30
+ requestDevice(std::shared_ptr<GPUDeviceDescriptor> options);
31
+
32
+ void loadHybridMethods() override {
33
+ registerHybridGetter("__brand", &GPUAdapter::getBrand, this);
34
+ registerHybridMethod("requestDevice", &GPUAdapter::requestDevice, this);
35
+ }
36
+
37
+ private:
38
+ wgpu::Adapter _instance;
39
+ };
40
+ } // namespace rnwgpu
@@ -0,0 +1,33 @@
1
+ #pragma once
2
+
3
+ #include <future>
4
+ #include <memory>
5
+ #include <string>
6
+
7
+ #include "Unions.h"
8
+ #include <RNFHybridObject.h>
9
+
10
+ #include "MutableBuffer.h"
11
+
12
+ #include "webgpu/webgpu_cpp.h"
13
+
14
+ namespace rnwgpu {
15
+
16
+ namespace m = margelo;
17
+
18
+ class GPUAdapterInfo : public m::HybridObject {
19
+ public:
20
+ explicit GPUAdapterInfo(wgpu::AdapterInfo instance)
21
+ : HybridObject("GPUAdapterInfo"), _instance(instance) {}
22
+
23
+ public:
24
+ std::string getBrand() { return _name; }
25
+
26
+ void loadHybridMethods() override {
27
+ registerHybridGetter("__brand", &GPUAdapterInfo::getBrand, this);
28
+ }
29
+
30
+ private:
31
+ wgpu::AdapterInfo _instance;
32
+ };
33
+ } // namespace rnwgpu
@@ -0,0 +1,38 @@
1
+ #pragma once
2
+
3
+ #include <future>
4
+ #include <memory>
5
+ #include <string>
6
+
7
+ #include "Unions.h"
8
+ #include <RNFHybridObject.h>
9
+
10
+ #include "MutableBuffer.h"
11
+
12
+ #include "webgpu/webgpu_cpp.h"
13
+
14
+ namespace rnwgpu {
15
+
16
+ namespace m = margelo;
17
+
18
+ class GPUBindGroup : public m::HybridObject {
19
+ public:
20
+ explicit GPUBindGroup(wgpu::BindGroup instance, std::string label)
21
+ : HybridObject("GPUBindGroup"), _instance(instance), _label(label) {}
22
+
23
+ public:
24
+ std::string getBrand() { return _name; }
25
+
26
+ std::string getLabel() { return _label; }
27
+
28
+ void loadHybridMethods() override {
29
+ registerHybridGetter("__brand", &GPUBindGroup::getBrand, this);
30
+
31
+ registerHybridGetter("label", &GPUBindGroup::getLabel, this);
32
+ }
33
+
34
+ private:
35
+ wgpu::BindGroup _instance;
36
+ std::string _label;
37
+ };
38
+ } // namespace rnwgpu
@@ -0,0 +1,39 @@
1
+ #pragma once
2
+
3
+ #include <future>
4
+ #include <memory>
5
+ #include <string>
6
+
7
+ #include "Unions.h"
8
+ #include <RNFHybridObject.h>
9
+
10
+ #include "MutableBuffer.h"
11
+
12
+ #include "webgpu/webgpu_cpp.h"
13
+
14
+ namespace rnwgpu {
15
+
16
+ namespace m = margelo;
17
+
18
+ class GPUBindGroupLayout : public m::HybridObject {
19
+ public:
20
+ explicit GPUBindGroupLayout(wgpu::BindGroupLayout instance, std::string label)
21
+ : HybridObject("GPUBindGroupLayout"), _instance(instance), _label(label) {
22
+ }
23
+
24
+ public:
25
+ std::string getBrand() { return _name; }
26
+
27
+ std::string getLabel() { return _label; }
28
+
29
+ void loadHybridMethods() override {
30
+ registerHybridGetter("__brand", &GPUBindGroupLayout::getBrand, this);
31
+
32
+ registerHybridGetter("label", &GPUBindGroupLayout::getLabel, this);
33
+ }
34
+
35
+ private:
36
+ wgpu::BindGroupLayout _instance;
37
+ std::string _label;
38
+ };
39
+ } // namespace rnwgpu
@@ -0,0 +1,16 @@
1
+ #include "GPUBuffer.h"
2
+
3
+ namespace rnwgpu {
4
+
5
+ std::shared_ptr<MutableJSIBuffer>
6
+ GPUBuffer::getMappedRange(std::optional<size_t> offset,
7
+ std::optional<size_t> size) {
8
+ auto aOffset = offset.value_or(0);
9
+ auto aSize = size.value_or(WGPU_WHOLE_MAP_SIZE);
10
+ auto result = _instance.GetMappedRange(aOffset, aSize);
11
+ return std::make_shared<MutableJSIBuffer>(result, _instance.GetSize());
12
+ }
13
+
14
+ void GPUBuffer::unmap() { _instance.Unmap(); }
15
+
16
+ } // namespace rnwgpu