react-native-wgpu 0.1.0 → 0.1.3

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 (280) hide show
  1. package/README.md +221 -13
  2. package/android/CMakeLists.txt +15 -1
  3. package/android/build.gradle +0 -18
  4. package/android/cpp/AndroidPlatformContext.h +132 -0
  5. package/android/cpp/cpp-adapter.cpp +70 -4
  6. package/android/src/main/java/com/webgpu/WebGPUModule.java +69 -21
  7. package/android/src/main/java/com/webgpu/WebGPUView.java +63 -8
  8. package/android/src/main/java/com/webgpu/WebGPUViewManager.java +5 -6
  9. package/android/src/newarch/{WgpuViewManagerSpec.java → com/webgpu/WebGPUViewManagerSpec.java} +5 -5
  10. package/android/src/oldarch/com/webgpu/NativeWebGPUModuleSpec.java +23 -0
  11. package/android/src/oldarch/{WebGPUViewManagerSpec.java → com/webgpu/WebGPUViewManagerSpec.java} +1 -1
  12. package/cpp/{Logger.h → WGPULogger.h} +13 -6
  13. package/cpp/dawn/webgpu.h +9 -0
  14. package/cpp/dawn/webgpu_cpp.h +95 -43
  15. package/cpp/dawn/webgpu_cpp_print.h +6 -0
  16. package/cpp/dawn/wire/client/webgpu_cpp.h +80 -43
  17. package/cpp/jsi/RNFHybridObject.cpp +1 -1
  18. package/cpp/jsi/RNFHybridObject.h +4 -4
  19. package/cpp/jsi/RNFJSIConverter.h +204 -17
  20. package/cpp/rnwgpu/ArrayBuffer.h +72 -0
  21. package/cpp/rnwgpu/PlatformContext.h +29 -0
  22. package/cpp/rnwgpu/RNWebGPUManager.cpp +13 -11
  23. package/cpp/rnwgpu/RNWebGPUManager.h +9 -1
  24. package/cpp/rnwgpu/SurfaceRegistry.h +36 -0
  25. package/cpp/rnwgpu/api/AsyncRunner.h +30 -0
  26. package/cpp/rnwgpu/api/Canvas.h +55 -0
  27. package/cpp/rnwgpu/api/Convertors.h +765 -0
  28. package/cpp/rnwgpu/api/External.h +12 -0
  29. package/cpp/rnwgpu/api/GPU.cpp +81 -23
  30. package/cpp/rnwgpu/api/GPU.h +24 -6
  31. package/cpp/rnwgpu/api/GPUAdapter.cpp +138 -24
  32. package/cpp/rnwgpu/api/GPUAdapter.h +24 -5
  33. package/cpp/rnwgpu/api/GPUAdapterInfo.h +19 -7
  34. package/cpp/rnwgpu/api/GPUBindGroup.cpp +3 -0
  35. package/cpp/rnwgpu/api/GPUBindGroup.h +11 -4
  36. package/cpp/rnwgpu/api/GPUBindGroupLayout.h +11 -4
  37. package/cpp/rnwgpu/api/GPUBuffer.cpp +82 -7
  38. package/cpp/rnwgpu/api/GPUBuffer.h +44 -8
  39. package/cpp/rnwgpu/api/GPUCanvasContext.cpp +63 -0
  40. package/cpp/rnwgpu/api/GPUCanvasContext.h +51 -8
  41. package/cpp/rnwgpu/api/GPUCommandBuffer.h +11 -4
  42. package/cpp/rnwgpu/api/GPUCommandEncoder.cpp +177 -0
  43. package/cpp/rnwgpu/api/GPUCommandEncoder.h +75 -3
  44. package/cpp/rnwgpu/api/GPUCompilationInfo.h +65 -8
  45. package/cpp/rnwgpu/api/GPUCompilationMessage.h +6 -4
  46. package/cpp/rnwgpu/api/GPUComputePassEncoder.cpp +58 -0
  47. package/cpp/rnwgpu/api/GPUComputePassEncoder.h +49 -3
  48. package/cpp/rnwgpu/api/GPUComputePipeline.cpp +11 -0
  49. package/cpp/rnwgpu/api/GPUComputePipeline.h +18 -3
  50. package/cpp/rnwgpu/api/GPUDevice.cpp +330 -3
  51. package/cpp/rnwgpu/api/GPUDevice.h +119 -5
  52. package/cpp/rnwgpu/api/GPUDeviceLostInfo.cpp +6 -0
  53. package/cpp/rnwgpu/api/GPUDeviceLostInfo.h +14 -7
  54. package/cpp/rnwgpu/api/GPUError.h +42 -0
  55. package/cpp/rnwgpu/api/GPUExtent3D.h +69 -0
  56. package/cpp/rnwgpu/api/GPUExternalTexture.h +11 -4
  57. package/cpp/rnwgpu/api/GPUFeatures.h +221 -0
  58. package/cpp/rnwgpu/api/GPUOrigin2D.h +59 -0
  59. package/cpp/rnwgpu/api/GPUOrigin3D.h +66 -0
  60. package/cpp/rnwgpu/api/GPUPipelineLayout.h +11 -4
  61. package/cpp/rnwgpu/api/GPUQuerySet.cpp +11 -0
  62. package/cpp/rnwgpu/api/GPUQuerySet.h +19 -5
  63. package/cpp/rnwgpu/api/GPUQueue.cpp +153 -0
  64. package/cpp/rnwgpu/api/GPUQueue.h +45 -4
  65. package/cpp/rnwgpu/api/GPURenderBundle.h +11 -4
  66. package/cpp/rnwgpu/api/GPURenderBundleEncoder.cpp +128 -0
  67. package/cpp/rnwgpu/api/GPURenderBundleEncoder.h +72 -3
  68. package/cpp/rnwgpu/api/GPURenderPassEncoder.cpp +161 -0
  69. package/cpp/rnwgpu/api/GPURenderPassEncoder.h +93 -3
  70. package/cpp/rnwgpu/api/GPURenderPipeline.cpp +13 -0
  71. package/cpp/rnwgpu/api/GPURenderPipeline.h +18 -3
  72. package/cpp/rnwgpu/api/GPUSampler.h +11 -4
  73. package/cpp/rnwgpu/api/GPUShaderModule.cpp +39 -0
  74. package/cpp/rnwgpu/api/GPUShaderModule.h +23 -4
  75. package/cpp/rnwgpu/api/GPUSupportedLimits.cpp +133 -0
  76. package/cpp/rnwgpu/api/GPUSupportedLimits.h +128 -4
  77. package/cpp/rnwgpu/api/GPUTexture.cpp +47 -0
  78. package/cpp/rnwgpu/api/GPUTexture.h +38 -4
  79. package/cpp/rnwgpu/api/GPUTextureView.h +11 -4
  80. package/cpp/rnwgpu/api/ImageBitmap.h +34 -0
  81. package/cpp/rnwgpu/api/RNWebGPU.h +100 -0
  82. package/cpp/rnwgpu/api/WebGPUCanvasContextFactory.h +59 -0
  83. package/cpp/rnwgpu/api/descriptors/GPUBindGroupDescriptor.h +31 -43
  84. package/cpp/rnwgpu/api/descriptors/GPUBindGroupEntry.h +40 -31
  85. package/cpp/rnwgpu/api/descriptors/GPUBindGroupLayoutDescriptor.h +24 -32
  86. package/cpp/rnwgpu/api/descriptors/GPUBindGroupLayoutEntry.h +56 -49
  87. package/cpp/rnwgpu/api/descriptors/GPUBlendComponent.h +27 -20
  88. package/cpp/rnwgpu/api/descriptors/GPUBlendState.h +23 -31
  89. package/cpp/rnwgpu/api/descriptors/GPUBufferBinding.h +25 -36
  90. package/cpp/rnwgpu/api/descriptors/GPUBufferBindingLayout.h +25 -25
  91. package/cpp/rnwgpu/api/descriptors/GPUBufferDescriptor.h +26 -46
  92. package/cpp/rnwgpu/api/descriptors/GPUCanvasConfiguration.h +31 -53
  93. package/cpp/rnwgpu/api/descriptors/GPUColor.h +72 -0
  94. package/cpp/rnwgpu/api/descriptors/GPUColorTargetState.h +27 -33
  95. package/cpp/rnwgpu/api/descriptors/GPUCommandBufferDescriptor.h +51 -0
  96. package/cpp/rnwgpu/api/descriptors/GPUCommandEncoderDescriptor.h +51 -0
  97. package/cpp/rnwgpu/api/descriptors/GPUComputePassDescriptor.h +24 -25
  98. package/cpp/rnwgpu/api/descriptors/GPUComputePassTimestampWrites.h +25 -42
  99. package/cpp/rnwgpu/api/descriptors/GPUComputePipelineDescriptor.h +32 -42
  100. package/cpp/rnwgpu/api/descriptors/GPUDepthStencilState.h +58 -76
  101. package/cpp/rnwgpu/api/descriptors/GPUDeviceDescriptor.h +38 -30
  102. package/cpp/rnwgpu/api/descriptors/GPUExternalTextureBindingLayout.h +11 -11
  103. package/cpp/rnwgpu/api/descriptors/GPUExternalTextureDescriptor.h +43 -34
  104. package/cpp/rnwgpu/api/descriptors/GPUFragmentState.h +39 -46
  105. package/cpp/rnwgpu/api/descriptors/GPUImageCopyBuffer.h +29 -45
  106. package/cpp/rnwgpu/api/descriptors/GPUImageCopyExternalImage.h +32 -31
  107. package/cpp/rnwgpu/api/descriptors/GPUImageCopyTexture.h +32 -36
  108. package/cpp/rnwgpu/api/descriptors/GPUImageCopyTextureTagged.h +42 -44
  109. package/cpp/rnwgpu/api/descriptors/GPUImageDataLayout.h +24 -34
  110. package/cpp/rnwgpu/api/descriptors/GPUMultisampleState.h +24 -32
  111. package/cpp/rnwgpu/api/descriptors/GPUPipelineLayoutDescriptor.h +25 -35
  112. package/cpp/rnwgpu/api/descriptors/GPUPrimitiveState.h +35 -26
  113. package/cpp/rnwgpu/api/descriptors/GPUProgrammableStage.h +29 -35
  114. package/cpp/rnwgpu/api/descriptors/GPUQuerySetDescriptor.h +23 -43
  115. package/cpp/rnwgpu/api/descriptors/GPUQueueDescriptor.h +49 -0
  116. package/cpp/rnwgpu/api/descriptors/GPURenderBundleDescriptor.h +51 -0
  117. package/cpp/rnwgpu/api/descriptors/GPURenderBundleEncoderDescriptor.h +41 -57
  118. package/cpp/rnwgpu/api/descriptors/GPURenderPassColorAttachment.h +42 -61
  119. package/cpp/rnwgpu/api/descriptors/GPURenderPassDepthStencilAttachment.h +54 -66
  120. package/cpp/rnwgpu/api/descriptors/GPURenderPassDescriptor.h +52 -55
  121. package/cpp/rnwgpu/api/descriptors/GPURenderPassTimestampWrites.h +25 -42
  122. package/cpp/rnwgpu/api/descriptors/GPURenderPipelineDescriptor.h +60 -56
  123. package/cpp/rnwgpu/api/descriptors/GPURequestAdapterOptions.h +21 -20
  124. package/cpp/rnwgpu/api/descriptors/GPUSamplerBindingLayout.h +17 -14
  125. package/cpp/rnwgpu/api/descriptors/GPUSamplerDescriptor.h +63 -63
  126. package/cpp/rnwgpu/api/descriptors/GPUShaderModuleCompilationHint.h +28 -27
  127. package/cpp/rnwgpu/api/descriptors/GPUShaderModuleDescriptor.h +28 -49
  128. package/cpp/rnwgpu/api/descriptors/GPUStencilFaceState.h +32 -23
  129. package/cpp/rnwgpu/api/descriptors/GPUStorageTextureBindingLayout.h +27 -29
  130. package/cpp/rnwgpu/api/descriptors/GPUTextureBindingLayout.h +27 -20
  131. package/cpp/rnwgpu/api/descriptors/GPUTextureDescriptor.h +49 -76
  132. package/cpp/rnwgpu/api/descriptors/GPUTextureViewDescriptor.h +48 -64
  133. package/cpp/rnwgpu/api/descriptors/GPUUncapturedErrorEventInit.h +29 -31
  134. package/cpp/rnwgpu/api/descriptors/GPUVertexAttribute.h +23 -44
  135. package/cpp/rnwgpu/api/descriptors/GPUVertexBufferLayout.h +30 -36
  136. package/cpp/rnwgpu/api/descriptors/GPUVertexState.h +39 -38
  137. package/cpp/rnwgpu/api/descriptors/Unions.h +57 -2
  138. package/ios/IOSPlatformContext.h +19 -0
  139. package/ios/IOSPlatformContext.mm +68 -0
  140. package/ios/MetalView.h +10 -0
  141. package/ios/MetalView.mm +27 -0
  142. package/ios/SurfaceUtils.h +13 -0
  143. package/ios/SurfaceUtils.mm +24 -0
  144. package/ios/WebGPUModule.h +8 -7
  145. package/ios/WebGPUModule.mm +47 -14
  146. package/ios/WebGPUView.h +6 -3
  147. package/ios/WebGPUView.mm +36 -24
  148. package/ios/WebGPUViewComponentDescriptor.h +61 -0
  149. package/ios/WebGPUViewManager.mm +6 -4
  150. package/lib/commonjs/Canvas.js +47 -0
  151. package/lib/commonjs/Canvas.js.map +1 -0
  152. package/lib/commonjs/{WebGPUNativeModule.js → NativeWebGPUModule.js} +1 -1
  153. package/lib/commonjs/{WebGPUNativeModule.js.map → NativeWebGPUModule.js.map} +1 -1
  154. package/lib/commonjs/WebGPUView.js +2 -0
  155. package/lib/commonjs/WebGPUView.js.map +1 -0
  156. package/lib/commonjs/WebGPUViewNativeComponent.js.map +1 -1
  157. package/lib/commonjs/index.js +183 -19
  158. package/lib/commonjs/index.js.map +1 -1
  159. package/lib/commonjs/utils.js +39 -0
  160. package/lib/commonjs/utils.js.map +1 -0
  161. package/lib/module/Canvas.js +40 -0
  162. package/lib/module/Canvas.js.map +1 -0
  163. package/lib/module/{WebGPUNativeModule.js → NativeWebGPUModule.js} +1 -1
  164. package/lib/module/NativeWebGPUModule.js.map +1 -0
  165. package/lib/module/WebGPUView.js +2 -0
  166. package/lib/module/WebGPUView.js.map +1 -0
  167. package/lib/module/WebGPUViewNativeComponent.js.map +1 -1
  168. package/lib/module/index.js +158 -6
  169. package/lib/module/index.js.map +1 -1
  170. package/lib/module/utils.js +31 -0
  171. package/lib/module/utils.js.map +1 -0
  172. package/lib/typescript/example/src/Triangle/triangle.d.ts +3 -0
  173. package/lib/typescript/example/src/Triangle/triangle.d.ts.map +1 -0
  174. package/lib/typescript/example/src/Wireframe/Shaders.d.ts +3 -0
  175. package/lib/typescript/example/src/Wireframe/Shaders.d.ts.map +1 -0
  176. package/lib/typescript/example/src/Wireframe/models.d.ts +29 -0
  177. package/lib/typescript/example/src/Wireframe/models.d.ts.map +1 -0
  178. package/lib/typescript/example/src/components/DrawingContext.d.ts +13 -0
  179. package/lib/typescript/example/src/components/DrawingContext.d.ts.map +1 -0
  180. package/lib/typescript/example/src/components/cube.d.ts +7 -0
  181. package/lib/typescript/example/src/components/cube.d.ts.map +1 -0
  182. package/lib/typescript/example/src/components/meshes/sphere.d.ts +12 -0
  183. package/lib/typescript/example/src/components/meshes/sphere.d.ts.map +1 -0
  184. package/lib/typescript/example/src/components/meshes/teapot.d.ts +6 -0
  185. package/lib/typescript/example/src/components/meshes/teapot.d.ts.map +1 -0
  186. package/lib/typescript/example/src/components/meshes/utils.d.ts +10 -0
  187. package/lib/typescript/example/src/components/meshes/utils.d.ts.map +1 -0
  188. package/lib/typescript/jest.config.d.ts +4 -0
  189. package/lib/typescript/lib/commonjs/Canvas.d.ts +4 -0
  190. package/lib/typescript/lib/commonjs/Canvas.d.ts.map +1 -0
  191. package/lib/typescript/lib/commonjs/{WebGPUNativeModule.d.ts → NativeWebGPUModule.d.ts} +1 -1
  192. package/lib/typescript/lib/commonjs/NativeWebGPUModule.d.ts.map +1 -0
  193. package/lib/typescript/lib/commonjs/WebGPUView.d.ts +1 -0
  194. package/lib/typescript/lib/commonjs/WebGPUView.d.ts.map +1 -0
  195. package/lib/typescript/lib/commonjs/index.d.ts +0 -2
  196. package/lib/typescript/lib/commonjs/utils.d.ts +5 -0
  197. package/lib/typescript/lib/commonjs/utils.d.ts.map +1 -0
  198. package/lib/typescript/lib/module/Canvas.d.ts +2 -0
  199. package/lib/typescript/lib/module/Canvas.d.ts.map +1 -0
  200. package/lib/typescript/lib/module/{WebGPUNativeModule.d.ts → NativeWebGPUModule.d.ts} +1 -1
  201. package/lib/typescript/lib/module/NativeWebGPUModule.d.ts.map +1 -0
  202. package/lib/typescript/lib/module/WebGPUView.d.ts +1 -0
  203. package/lib/typescript/lib/module/WebGPUView.d.ts.map +1 -0
  204. package/lib/typescript/lib/module/index.d.ts +3 -3
  205. package/lib/typescript/lib/module/utils.d.ts +3 -0
  206. package/lib/typescript/lib/module/utils.d.ts.map +1 -0
  207. package/lib/typescript/src/Canvas.d.ts +29 -0
  208. package/lib/typescript/src/Canvas.d.ts.map +1 -0
  209. package/lib/typescript/src/{WebGPUNativeModule.d.ts → NativeWebGPUModule.d.ts} +2 -1
  210. package/lib/typescript/src/NativeWebGPUModule.d.ts.map +1 -0
  211. package/lib/typescript/src/WebGPUView.d.ts +1 -0
  212. package/lib/typescript/src/WebGPUView.d.ts.map +1 -0
  213. package/lib/typescript/src/WebGPUViewNativeComponent.d.ts +2 -1
  214. package/lib/typescript/src/WebGPUViewNativeComponent.d.ts.map +1 -1
  215. package/lib/typescript/src/index.d.ts +3 -7
  216. package/lib/typescript/src/index.d.ts.map +1 -1
  217. package/lib/typescript/src/utils.d.ts +7 -0
  218. package/lib/typescript/src/utils.d.ts.map +1 -0
  219. package/libs/android/arm64-v8a/libwebgpu_dawn.so +0 -0
  220. package/libs/android/armeabi-v7a/libwebgpu_dawn.so +0 -0
  221. package/libs/android/x86/libwebgpu_dawn.so +0 -0
  222. package/libs/android/x86_64/libwebgpu_dawn.so +0 -0
  223. package/libs/dawn.json +4597 -0
  224. package/libs/ios/arm64_iphoneos/libwebgpu_dawn.a +0 -0
  225. package/libs/ios/arm64_iphonesimulator/libwebgpu_dawn.a +0 -0
  226. package/libs/ios/libwebgpu_dawn.a +0 -0
  227. package/libs/ios/libwebgpu_dawn.xcframework/Info.plist +44 -0
  228. package/libs/ios/libwebgpu_dawn.xcframework/ios-arm64/libwebgpu_dawn.a +0 -0
  229. package/libs/ios/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/libwebgpu_dawn.a +0 -0
  230. package/libs/ios/x86_64_iphonesimulator/libwebgpu_dawn.a +0 -0
  231. package/package.json +25 -8
  232. package/{react-native-webgpu.podspec → react-native-wgpu.podspec} +6 -9
  233. package/src/Canvas.tsx +73 -0
  234. package/src/{WebGPUNativeModule.ts → NativeWebGPUModule.ts} +1 -0
  235. package/src/WebGPUView.tsx +0 -0
  236. package/src/WebGPUViewNativeComponent.ts +2 -1
  237. package/src/index.tsx +191 -8
  238. package/src/utils.ts +40 -0
  239. package/android/src/main/AndroidManifestNew.xml +0 -2
  240. package/cpp/rnwgpu/MutableBuffer.h +0 -48
  241. package/cpp/rnwgpu/api/GPUUncapturedErrorEvent.h +0 -33
  242. package/cpp/rnwgpu/api/descriptors/GPUColorDict.h +0 -81
  243. package/cpp/rnwgpu/api/descriptors/GPUExtent3DDict.h +0 -76
  244. package/cpp/rnwgpu/api/descriptors/GPUExtent3DDictStrict.h +0 -91
  245. package/cpp/rnwgpu/api/descriptors/GPUOrigin2DDict.h +0 -61
  246. package/cpp/rnwgpu/api/descriptors/GPUOrigin2DDictStrict.h +0 -75
  247. package/cpp/rnwgpu/api/descriptors/GPUOrigin3DDict.h +0 -71
  248. package/cpp/rnwgpu/api/descriptors/GPUPipelineErrorInit.h +0 -54
  249. package/cpp/rnwgpu/api/descriptors/GPURenderPassLayout.h +0 -83
  250. package/ios/RNFAppleLogger.mm +0 -22
  251. package/ios/Utils.h +0 -5
  252. package/ios/Utils.m +0 -26
  253. package/lib/module/WebGPUNativeModule.js.map +0 -1
  254. package/lib/typescript/lib/commonjs/WebGPUNativeModule.d.ts.map +0 -1
  255. package/lib/typescript/lib/module/WebGPUNativeModule.d.ts.map +0 -1
  256. package/lib/typescript/scripts/build/copy-artifacts.d.ts +0 -2
  257. package/lib/typescript/scripts/build/copy-artifacts.d.ts.map +0 -1
  258. package/lib/typescript/scripts/build/dawn.d.ts +0 -2
  259. package/lib/typescript/scripts/build/dawn.d.ts.map +0 -1
  260. package/lib/typescript/scripts/build/util.d.ts +0 -13
  261. package/lib/typescript/scripts/build/util.d.ts.map +0 -1
  262. package/lib/typescript/scripts/codegen/codegen.d.ts +0 -2
  263. package/lib/typescript/scripts/codegen/codegen.d.ts.map +0 -1
  264. package/lib/typescript/scripts/codegen/model/dawn.d.ts +0 -2
  265. package/lib/typescript/scripts/codegen/model/dawn.d.ts.map +0 -1
  266. package/lib/typescript/scripts/codegen/model/model.d.ts +0 -8
  267. package/lib/typescript/scripts/codegen/model/model.d.ts.map +0 -1
  268. package/lib/typescript/scripts/codegen/templates/Descriptor.d.ts +0 -4
  269. package/lib/typescript/scripts/codegen/templates/Descriptor.d.ts.map +0 -1
  270. package/lib/typescript/scripts/codegen/templates/Enum.d.ts +0 -3
  271. package/lib/typescript/scripts/codegen/templates/Enum.d.ts.map +0 -1
  272. package/lib/typescript/scripts/codegen/templates/HybridObject.d.ts +0 -3
  273. package/lib/typescript/scripts/codegen/templates/HybridObject.d.ts.map +0 -1
  274. package/lib/typescript/scripts/codegen/templates/Unions.d.ts +0 -6
  275. package/lib/typescript/scripts/codegen/templates/Unions.d.ts.map +0 -1
  276. package/lib/typescript/scripts/codegen/templates/common.d.ts +0 -9
  277. package/lib/typescript/scripts/codegen/templates/common.d.ts.map +0 -1
  278. package/lib/typescript/scripts/codegen/util.d.ts +0 -2
  279. package/lib/typescript/scripts/codegen/util.d.ts.map +0 -1
  280. package/lib/typescript/src/WebGPUNativeModule.d.ts.map +0 -1
@@ -1,30 +1,41 @@
1
1
  #pragma once
2
2
 
3
+ #include <map>
3
4
  #include <memory>
4
5
  #include <string>
6
+ #include <variant>
7
+ #include <vector>
5
8
 
6
9
  #include "webgpu/webgpu_cpp.h"
7
10
 
8
- #include "Logger.h"
9
11
  #include "RNFJSIConverter.h"
10
- #include <RNFHybridObject.h>
12
+ #include "WGPULogger.h"
13
+
14
+ #include "GPUShaderModule.h"
15
+ #include "GPUVertexBufferLayout.h"
16
+ #include "RNFHybridObject.h"
11
17
 
12
18
  namespace jsi = facebook::jsi;
19
+ namespace m = margelo;
13
20
 
14
21
  namespace rnwgpu {
15
22
 
16
- class GPUVertexState {
17
- public:
18
- wgpu::VertexState *getInstance() { return &_instance; }
19
-
20
- wgpu::VertexState _instance;
21
-
22
- std::string entryPoint;
23
+ struct GPUVertexState {
24
+ std::optional<std::vector<
25
+ std::variant<std::nullptr_t, std::shared_ptr<GPUVertexBufferLayout>>>>
26
+ buffers; // Iterable<GPUVertexBufferLayout | null>
27
+ std::shared_ptr<GPUShaderModule> module; // GPUShaderModule
28
+ std::optional<std::string> entryPoint; // string
29
+ std::optional<std::map<std::string, double>>
30
+ constants; // Record< string, GPUPipelineConstantValue >
23
31
  };
32
+
24
33
  } // namespace rnwgpu
25
34
 
26
35
  namespace margelo {
27
36
 
37
+ using namespace rnwgpu; // NOLINT(build/namespaces)
38
+
28
39
  template <> struct JSIConverter<std::shared_ptr<rnwgpu::GPUVertexState>> {
29
40
  static std::shared_ptr<rnwgpu::GPUVertexState>
30
41
  fromJSI(jsi::Runtime &runtime, const jsi::Value &arg, bool outOfBounds) {
@@ -32,46 +43,36 @@ template <> struct JSIConverter<std::shared_ptr<rnwgpu::GPUVertexState>> {
32
43
  if (!outOfBounds && arg.isObject()) {
33
44
  auto value = arg.getObject(runtime);
34
45
  if (value.hasProperty(runtime, "buffers")) {
35
- auto buffers = value.getProperty(runtime, "buffers");
46
+ auto prop = value.getProperty(runtime, "buffers");
47
+ result->buffers = JSIConverter<std::optional<std::vector<std::variant<
48
+ std::nullptr_t, std::shared_ptr<GPUVertexBufferLayout>>>>>::
49
+ fromJSI(runtime, prop, false);
36
50
  }
37
51
  if (value.hasProperty(runtime, "module")) {
38
- auto module = value.getProperty(runtime, "module");
39
-
40
- if (module.isUndefined()) {
41
- throw std::runtime_error(
42
- "Property GPUVertexState::module is required");
43
- }
44
- } else {
45
- throw std::runtime_error(
46
- "Property GPUVertexState::module is not defined");
52
+ auto prop = value.getProperty(runtime, "module");
53
+ result->module =
54
+ JSIConverter<std::shared_ptr<GPUShaderModule>>::fromJSI(
55
+ runtime, prop, false);
47
56
  }
48
57
  if (value.hasProperty(runtime, "entryPoint")) {
49
- auto entryPoint = value.getProperty(runtime, "entryPoint");
50
-
51
- if (entryPoint.isString()) {
52
- auto str = entryPoint.asString(runtime).utf8(runtime);
53
- result->entryPoint = str;
54
- result->_instance.entryPoint = result->entryPoint.c_str();
55
- }
58
+ auto prop = value.getProperty(runtime, "entryPoint");
59
+ result->entryPoint = JSIConverter<std::optional<std::string>>::fromJSI(
60
+ runtime, prop, false);
56
61
  }
57
62
  if (value.hasProperty(runtime, "constants")) {
58
- auto constants = value.getProperty(runtime, "constants");
63
+ auto prop = value.getProperty(runtime, "constants");
64
+ result->constants =
65
+ JSIConverter<std::optional<std::map<std::string, double>>>::fromJSI(
66
+ runtime, prop, false);
59
67
  }
60
68
  }
61
- rnwgpu::Logger::logToConsole("GPUVertexState::buffers = %f",
62
- result->_instance.buffers);
63
- rnwgpu::Logger::logToConsole("GPUVertexState::module = %f",
64
- result->_instance.module);
65
- rnwgpu::Logger::logToConsole("GPUVertexState::entryPoint = %f",
66
- result->_instance.entryPoint);
67
- rnwgpu::Logger::logToConsole("GPUVertexState::constants = %f",
68
- result->_instance.constants);
69
+
69
70
  return result;
70
71
  }
71
72
  static jsi::Value toJSI(jsi::Runtime &runtime,
72
73
  std::shared_ptr<rnwgpu::GPUVertexState> arg) {
73
- // No conversions here
74
- return jsi::Value::null();
74
+ throw std::runtime_error("Invalid GPUVertexState::toJSI()");
75
75
  }
76
76
  };
77
- } // namespace margelo
77
+
78
+ } // namespace margelo
@@ -2,9 +2,9 @@
2
2
 
3
3
  #include <string>
4
4
 
5
- #include "webgpu/webgpu_cpp.h"
6
-
5
+ #include "External.h"
7
6
  #include "RNFEnumMapper.h"
7
+ #include "webgpu/webgpu_cpp.h"
8
8
 
9
9
  namespace margelo {
10
10
  namespace EnumMapper {
@@ -1641,5 +1641,60 @@ static void convertEnumToJSUnion(wgpu::VertexStepMode inEnum,
1641
1641
  }
1642
1642
  }
1643
1643
 
1644
+ static void convertJSUnionToEnum(const std::string &inUnion,
1645
+ rnwgpu::PredefinedColorSpace *outEnum) {
1646
+ if (inUnion == "display-p3") {
1647
+ *outEnum = rnwgpu::PredefinedColorSpace::DisplayP3;
1648
+ } else if (inUnion == "srgb") {
1649
+ *outEnum = rnwgpu::PredefinedColorSpace::Srgb;
1650
+ } else {
1651
+ throw invalidUnion(inUnion);
1652
+ }
1653
+ }
1654
+
1655
+ static void convertEnumToJSUnion(rnwgpu::PredefinedColorSpace inEnum,
1656
+ std::string *outUnion) {
1657
+ switch (inEnum) {
1658
+ case rnwgpu::PredefinedColorSpace::DisplayP3:
1659
+ *outUnion = "display-p3";
1660
+ break;
1661
+ case rnwgpu::PredefinedColorSpace::Srgb:
1662
+ *outUnion = "srgb";
1663
+ break;
1664
+ default:
1665
+ throw invalidEnum(inEnum);
1666
+ }
1667
+ }
1668
+
1669
+ static void convertJSUnionToEnum(const std::string &inUnion,
1670
+ rnwgpu::PremultiplyAlpha *outEnum) {
1671
+ if (inUnion == "none") {
1672
+ *outEnum = rnwgpu::PremultiplyAlpha::None;
1673
+ } else if (inUnion == "default") {
1674
+ *outEnum = rnwgpu::PremultiplyAlpha::Default;
1675
+ } else if (inUnion == "premultiply") {
1676
+ *outEnum = rnwgpu::PremultiplyAlpha::Premultiply;
1677
+ } else {
1678
+ throw invalidUnion(inUnion);
1679
+ }
1680
+ }
1681
+
1682
+ static void convertEnumToJSUnion(rnwgpu::PremultiplyAlpha inEnum,
1683
+ std::string *outUnion) {
1684
+ switch (inEnum) {
1685
+ case rnwgpu::PremultiplyAlpha::None:
1686
+ *outUnion = "none";
1687
+ break;
1688
+ case rnwgpu::PremultiplyAlpha::Default:
1689
+ *outUnion = "default";
1690
+ break;
1691
+ case rnwgpu::PremultiplyAlpha::Premultiply:
1692
+ *outUnion = "premultiply";
1693
+ break;
1694
+ default:
1695
+ throw invalidEnum(inEnum);
1696
+ }
1697
+ }
1698
+
1644
1699
  } // namespace EnumMapper
1645
1700
  } // namespace margelo
@@ -0,0 +1,19 @@
1
+ #pragma once
2
+
3
+ #include "PlatformContext.h"
4
+
5
+ namespace rnwgpu {
6
+
7
+ class IOSPlatformContext : public PlatformContext {
8
+ public:
9
+ IOSPlatformContext() = default;
10
+ ~IOSPlatformContext() = default;
11
+
12
+ wgpu::Surface makeSurface(wgpu::Instance instance, void *surface, int width,
13
+ int height) override;
14
+
15
+ ImageData createImageBitmap(std::string blobId, double offset,
16
+ double size) override;
17
+ };
18
+
19
+ } // namespace rnwgpu
@@ -0,0 +1,68 @@
1
+ #include "IOSPlatformContext.h"
2
+
3
+ #import <React/RCTBlobManager.h>
4
+ #import <React/RCTBridge+Private.h>
5
+ #import <ReactCommon/RCTTurboModule.h>
6
+
7
+ #include "RNWebGPUManager.h"
8
+ #include "WebGPUModule.h"
9
+
10
+ namespace rnwgpu {
11
+
12
+ wgpu::Surface IOSPlatformContext::makeSurface(wgpu::Instance instance,
13
+ void *surface, int width,
14
+ int height) {
15
+ wgpu::SurfaceDescriptorFromMetalLayer metalSurfaceDesc;
16
+ metalSurfaceDesc.layer = surface;
17
+ wgpu::SurfaceDescriptor surfaceDescriptor;
18
+ surfaceDescriptor.nextInChain = &metalSurfaceDesc;
19
+ return instance.CreateSurface(&surfaceDescriptor);
20
+ }
21
+
22
+ ImageData IOSPlatformContext::createImageBitmap(std::string blobId,
23
+ double offset, double size) {
24
+ RCTBlobManager *blobManager =
25
+ [[RCTBridge currentBridge] moduleForClass:RCTBlobManager.class];
26
+ NSData *blobData =
27
+ [blobManager resolve:[NSString stringWithUTF8String:blobId.c_str()]
28
+ offset:(long)offset
29
+ size:(long)size];
30
+
31
+ if (!blobData) {
32
+ throw std::runtime_error("Couldn't retrive blob data");
33
+ }
34
+
35
+ UIImage *image = [UIImage imageWithData:blobData];
36
+ if (!image) {
37
+ throw std::runtime_error("Couldn't decode image");
38
+ }
39
+
40
+ CGImageRef cgImage = image.CGImage;
41
+ size_t width = CGImageGetWidth(cgImage);
42
+ size_t height = CGImageGetHeight(cgImage);
43
+ size_t bitsPerComponent = 8;
44
+ size_t bytesPerRow = width * 4;
45
+ std::vector<uint8_t> imageData(height * bytesPerRow);
46
+
47
+ CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
48
+ CGContextRef context = CGBitmapContextCreate(
49
+ imageData.data(), width, height, bitsPerComponent, bytesPerRow,
50
+ colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
51
+
52
+ CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgImage);
53
+
54
+ // Now imageData contains a copy of the bitmap data
55
+
56
+ CGContextRelease(context);
57
+ CGColorSpaceRelease(colorSpace);
58
+
59
+ // Use the copied data
60
+ ImageData result;
61
+ result.width = static_cast<int>(width);
62
+ result.height = static_cast<int>(height);
63
+ result.data = imageData;
64
+ result.format = wgpu::TextureFormat::RGBA8Unorm;
65
+ return result;
66
+ }
67
+
68
+ } // namespace rnwgpu
@@ -0,0 +1,10 @@
1
+ #pragma once
2
+
3
+ #import "WebGPUModule.h"
4
+ #import <UIKit/UIKit.h>
5
+
6
+ @interface MetalView : UIView
7
+
8
+ @property NSNumber *contextId;
9
+
10
+ @end
@@ -0,0 +1,27 @@
1
+ #import "MetalView.h"
2
+ #import "SurfaceUtils.h"
3
+ #import "webgpu_cpp.h"
4
+ #import <React/RCTViewManager.h>
5
+
6
+ @implementation MetalView {
7
+ BOOL _isConfigured;
8
+ }
9
+
10
+ + (Class)layerClass {
11
+ return [CAMetalLayer class];
12
+ }
13
+
14
+ // Paper only method
15
+ - (void)reactSetFrame:(CGRect)frame {
16
+ [super reactSetFrame:frame];
17
+ if (!_isConfigured) {
18
+ [SurfaceUtils configureSurface:self.layer
19
+ size:self.frame.size
20
+ contextId:[_contextId intValue]];
21
+ _isConfigured = YES;
22
+ } else {
23
+ [SurfaceUtils updateSurface:[_contextId intValue] size:self.frame.size];
24
+ }
25
+ }
26
+
27
+ @end
@@ -0,0 +1,13 @@
1
+ #pragma once
2
+
3
+ #import <UIKit/UIKit.h>
4
+
5
+ @interface SurfaceUtils : NSObject
6
+
7
+ + (void)configureSurface:(CALayer *)layer
8
+ size:(CGSize)size
9
+ contextId:(int)contextId;
10
+
11
+ + (void)updateSurface:(int)contextId size:(CGSize)size;
12
+
13
+ @end
@@ -0,0 +1,24 @@
1
+ #import "SurfaceUtils.h"
2
+ #import "MetalView.h"
3
+ #import "WebGPUModule.h"
4
+ #import "WebGPUView.h"
5
+
6
+ // TODO: to delete now
7
+ @implementation SurfaceUtils
8
+
9
+ + (void)configureSurface:(CALayer *)layer
10
+ size:(CGSize)size
11
+ contextId:(int)contextId {
12
+ std::shared_ptr<rnwgpu::RNWebGPUManager> manager = [WebGPUModule getManager];
13
+ CGFloat scaleFactor = [UIScreen mainScreen].scale;
14
+ void *nativeSurface = (__bridge void *)layer;
15
+ manager->surfacesRegistry.addSurface(contextId, nativeSurface, size.width,
16
+ size.height);
17
+ }
18
+
19
+ + (void)updateSurface:(int)contextId size:(CGSize)size {
20
+ std::shared_ptr<rnwgpu::RNWebGPUManager> manager = [WebGPUModule getManager];
21
+ manager->surfacesRegistry.updateSurface(contextId, size.width, size.height);
22
+ }
23
+
24
+ @end
@@ -1,18 +1,19 @@
1
1
  #pragma once
2
2
 
3
+ #import "RNWebGPUManager.h"
3
4
  #import <React/RCTBridgeModule.h>
4
5
  #import <React/RCTEventEmitter.h>
5
6
 
6
- @interface WebGPUModule : NSObject <RCTBridgeModule>
7
+ #ifdef RCT_NEW_ARCH_ENABLED
8
+ #import <RNWgpuViewSpec/RNWgpuViewSpec.h>
9
+ @interface WebGPUModule : RCTEventEmitter <NativeWebGPUModuleSpec>
10
+ #else
11
+ @interface WebGPUModule : RCTEventEmitter <RCTBridgeModule>
12
+ #endif
7
13
 
8
14
  @property(nonatomic, weak) RCTBridge *bridge;
9
15
  @property(nonatomic, weak) RCTModuleRegistry *moduleRegistry;
10
16
 
11
- @end
12
-
13
- #ifdef RCT_NEW_ARCH_ENABLED
14
- #import <rnwebgpu/rnwebgpu.h>
17
+ + (std::shared_ptr<rnwgpu::RNWebGPUManager>)getManager;
15
18
 
16
- @interface WebGPUModule () <WebGPUModuleSpec>
17
19
  @end
18
- #endif
@@ -1,24 +1,25 @@
1
1
  #import "WebGPUModule.h"
2
+ #import "GPUCanvasContext.h"
3
+ #include "IOSPlatformContext.h"
4
+
2
5
  #import <React/RCTBridge+Private.h>
3
6
  #import <React/RCTLog.h>
7
+ #import <React/RCTUIManagerUtils.h>
4
8
  #import <ReactCommon/RCTTurboModule.h>
5
-
6
- #include "RNFLogger.h"
7
- #include "RNWebGPUManager.h"
9
+ #import <jsi/jsi.h>
10
+ #import <memory>
8
11
 
9
12
  namespace jsi = facebook::jsi;
10
13
  namespace react = facebook::react;
11
14
 
12
- @implementation WebGPUModule {
13
- rnwgpu::RNWebGPUManager *_webgpuManager;
14
- }
15
+ @implementation WebGPUModule
15
16
 
16
17
  RCT_EXPORT_MODULE(WebGPUModule)
17
18
 
18
- #pragma Accessors
19
+ static std::shared_ptr<rnwgpu::RNWebGPUManager> webgpuManager;
19
20
 
20
- - (rnwgpu::RNWebGPUManager *)manager {
21
- return _webgpuManager;
21
+ + (std::shared_ptr<rnwgpu::RNWebGPUManager>)getManager {
22
+ return webgpuManager;
22
23
  }
23
24
 
24
25
  #pragma Setup and invalidation
@@ -31,11 +32,15 @@ RCT_EXPORT_MODULE(WebGPUModule)
31
32
  // if (_webgpuManager != nil) {
32
33
  // [_webgpuManager invalidate];
33
34
  // }
34
- _webgpuManager = nil;
35
+ webgpuManager = nil;
36
+ }
37
+
38
+ - (std::shared_ptr<rnwgpu::RNWebGPUManager>)getManager {
39
+ return webgpuManager;
35
40
  }
36
41
 
37
42
  RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) {
38
- if (_webgpuManager != nil) {
43
+ if (webgpuManager != nil) {
39
44
  // Already initialized, ignore call.
40
45
  return @true;
41
46
  }
@@ -61,16 +66,44 @@ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) {
61
66
  if (!jsInvoker) {
62
67
  jsInvoker = cxxBridge.jsCallInvoker;
63
68
  }
69
+ std::shared_ptr<rnwgpu::PlatformContext> platformContext =
70
+ std::make_shared<rnwgpu::IOSPlatformContext>();
64
71
  // TODO: remove allocation here
65
- _webgpuManager = new rnwgpu::RNWebGPUManager(runtime, jsInvoker);
72
+ webgpuManager = std::make_shared<rnwgpu::RNWebGPUManager>(runtime, jsInvoker,
73
+ platformContext);
74
+ return @true;
75
+ }
76
+
77
+ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(createSurfaceContext
78
+ : (double)contextId) {
79
+ int contextIdInt = contextId;
80
+ RCTCxxBridge *cxxBridge = (RCTCxxBridge *)self.bridge;
81
+ auto runtime = (jsi::Runtime *)cxxBridge.runtime;
82
+ auto webGPUContextRegistry = runtime->global().getPropertyAsObject(
83
+ *runtime, "__WebGPUContextRegistry");
84
+ auto canvas = webgpuManager->surfacesRegistry.getSurface(contextIdInt);
85
+ if (webGPUContextRegistry.hasProperty(*runtime,
86
+ std::to_string(contextIdInt).c_str())) {
87
+ // Context already exists, just update width/height
88
+ auto prop =
89
+ webGPUContextRegistry
90
+ .getPropertyAsObject(*runtime, std::to_string(contextIdInt).c_str())
91
+ .asHostObject<rnwgpu::Canvas>(*runtime);
92
+ prop->setWidth(canvas->getWidth());
93
+ prop->setHeight(canvas->getHeight());
94
+ return @true;
95
+ }
96
+ webGPUContextRegistry.setProperty(
97
+ *runtime, std::to_string(contextIdInt).c_str(),
98
+ facebook::jsi::Object::createFromHostObject(*runtime, canvas));
99
+
66
100
  return @true;
67
101
  }
68
102
 
69
103
  #ifdef RCT_NEW_ARCH_ENABLED
70
104
  - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
71
105
  (const facebook::react::ObjCTurboModule::InitParams &)params {
72
- jsInvoker = params.jsInvoker;
73
- return std::make_shared<facebook::react::NativeSkiaModuleSpecJSI>(params);
106
+ return std::make_shared<facebook::react::NativeWebGPUModuleSpecJSI>(params);
74
107
  }
75
108
  #endif
76
109
 
package/ios/WebGPUView.h CHANGED
@@ -1,13 +1,16 @@
1
- // This guard prevent this file to be compiled in the old architecture.
2
1
  #ifdef RCT_NEW_ARCH_ENABLED
2
+ #pragma once
3
+
4
+ #import "MetalView.h"
3
5
  #import <React/RCTViewComponentView.h>
4
6
  #import <UIKit/UIKit.h>
5
7
 
6
- #pragma once
7
-
8
8
  NS_ASSUME_NONNULL_BEGIN
9
9
 
10
10
  @interface WebGPUView : RCTViewComponentView
11
+ + (void)registerMetalView:(MetalView *)metalView
12
+ withContextId:(NSNumber *)contextId;
13
+ + (bool)isContextRegisterd:(NSNumber *)contextId;
11
14
  @end
12
15
 
13
16
  NS_ASSUME_NONNULL_END
package/ios/WebGPUView.mm CHANGED
@@ -1,13 +1,15 @@
1
1
  #ifdef RCT_NEW_ARCH_ENABLED
2
2
  #import "WebGPUView.h"
3
3
 
4
- #import <react/renderer/components/RNWebGPUViewSpec/ComponentDescriptors.h>
5
- #import <react/renderer/components/RNWebGPUViewSpec/EventEmitters.h>
6
- #import <react/renderer/components/RNWebGPUViewSpec/Props.h>
7
- #import <react/renderer/components/RNWebGPUViewSpec/RCTComponentViewHelpers.h>
4
+ #import <react/renderer/components/RNWgpuViewSpec/EventEmitters.h>
5
+ #import <react/renderer/components/RNWgpuViewSpec/Props.h>
6
+ #import <react/renderer/components/RNWgpuViewSpec/RCTComponentViewHelpers.h>
8
7
 
8
+ #import "MetalView.h"
9
9
  #import "RCTFabricComponentsPlugins.h"
10
10
  #import "Utils.h"
11
+ #import "WebGPUModule.h"
12
+ #import "WebGPUViewComponentDescriptor.h"
11
13
 
12
14
  using namespace facebook::react;
13
15
 
@@ -16,40 +18,50 @@ using namespace facebook::react;
16
18
  @end
17
19
 
18
20
  @implementation WebGPUView {
19
- UIView *_view;
21
+ NSNumber *_contextId;
20
22
  }
21
23
 
24
+ static NSMutableDictionary<NSNumber *, MetalView *> *metalViewRegistry =
25
+ [NSMutableDictionary new];
26
+
22
27
  + (ComponentDescriptorProvider)componentDescriptorProvider {
23
- return concreteComponentDescriptorProvider<WgpuViewComponentDescriptor>();
28
+ return concreteComponentDescriptorProvider<WebGPUViewComponentDescriptor>();
29
+ }
30
+
31
+ + (void)registerMetalView:(MetalView *)metalView
32
+ withContextId:(NSNumber *)contextId {
33
+ metalViewRegistry[contextId] = metalView;
34
+ }
35
+
36
+ + (bool)isContextRegisterd:(NSNumber *)contextId {
37
+ return metalViewRegistry[contextId] != nil;
24
38
  }
25
39
 
26
40
  - (instancetype)initWithFrame:(CGRect)frame {
27
41
  if (self = [super initWithFrame:frame]) {
28
- static const auto defaultProps = std::make_shared<const WgpuViewProps>();
42
+ static const auto defaultProps = std::make_shared<const WebGPUViewProps>();
29
43
  _props = defaultProps;
30
-
31
- _view = [[UIView alloc] init];
32
-
33
- self.contentView = _view;
34
44
  }
35
45
 
36
46
  return self;
37
47
  }
38
48
 
39
- - (void)updateProps:(Props::Shared const &)props
40
- oldProps:(Props::Shared const &)oldProps {
41
- const auto &oldViewProps =
42
- *std::static_pointer_cast<WebGPUViewProps const>(_props);
43
- const auto &newViewProps =
44
- *std::static_pointer_cast<WebGPUViewProps const>(props);
45
-
46
- if (oldViewProps.color != newViewProps.color) {
47
- NSString *colorToConvert =
48
- [[NSString alloc] initWithUTF8String:newViewProps.color.c_str()];
49
- [_view setBackgroundColor:[Utils hexStringToColor:colorToConvert]];
50
- }
49
+ - (void)prepareForRecycle {
50
+ [super prepareForRecycle];
51
+ self.contentView = nil;
52
+ [metalViewRegistry removeObjectForKey:_contextId];
53
+ }
51
54
 
52
- [super updateProps:props oldProps:oldProps];
55
+ - (void)updateLayoutMetrics:
56
+ (const facebook::react::LayoutMetrics &)layoutMetrics
57
+ oldLayoutMetrics:
58
+ (const facebook::react::LayoutMetrics &)oldLayoutMetrics {
59
+ [super updateLayoutMetrics:layoutMetrics oldLayoutMetrics:oldLayoutMetrics];
60
+ if (!self.contentView) {
61
+ const auto &props =
62
+ *std::static_pointer_cast<WebGPUViewProps const>(_props);
63
+ self.contentView = metalViewRegistry[@(props.contextId)];
64
+ }
53
65
  }
54
66
 
55
67
  Class<RCTComponentViewProtocol> WebGPUViewCls(void) { return WebGPUView.class; }
@@ -0,0 +1,61 @@
1
+ #ifdef RCT_NEW_ARCH_ENABLED
2
+
3
+ #pragma once
4
+
5
+ #include <react/debug/react_native_assert.h>
6
+ #include <react/renderer/components/RNWgpuViewSpec/ShadowNodes.h>
7
+ #include <react/renderer/components/view/ConcreteViewShadowNode.h>
8
+ #include <react/renderer/core/ConcreteComponentDescriptor.h>
9
+ #include <react/renderer/core/LayoutContext.h>
10
+
11
+ #import "MetalView.h"
12
+ #import "SurfaceUtils.h"
13
+ #import "WebGPUView.h"
14
+
15
+ namespace facebook {
16
+ namespace react {
17
+
18
+ class WebGPUViewCustomShadowNode final : public WebGPUViewShadowNode {
19
+
20
+ public:
21
+ using ConcreteViewShadowNode::ConcreteViewShadowNode;
22
+
23
+ void layout(LayoutContext layoutContext) override {
24
+ YogaLayoutableShadowNode::layout(layoutContext);
25
+ configureSurface();
26
+ }
27
+
28
+ void configureSurface() {
29
+ const auto &viewProps =
30
+ *std::static_pointer_cast<WebGPUViewProps const>(props_);
31
+ if ([WebGPUView isContextRegisterd:@(viewProps.contextId)]) {
32
+ return;
33
+ }
34
+ __block MetalView *metalView;
35
+ __block CALayer *layer;
36
+ dispatch_sync(dispatch_get_main_queue(), ^{
37
+ metalView = [[MetalView alloc] init];
38
+ layer = metalView.layer;
39
+ });
40
+ [WebGPUView registerMetalView:metalView
41
+ withContextId:@(viewProps.contextId)];
42
+
43
+ // TODO: use physical size
44
+ float width = layoutMetrics_.frame.size.width;
45
+ float height = layoutMetrics_.frame.size.height;
46
+ [SurfaceUtils configureSurface:layer
47
+ size:CGSizeMake(width, height)
48
+ contextId:viewProps.contextId];
49
+ }
50
+ };
51
+
52
+ class WebGPUViewComponentDescriptor final
53
+ : public ConcreteComponentDescriptor<WebGPUViewCustomShadowNode> {
54
+ public:
55
+ using ConcreteComponentDescriptor::ConcreteComponentDescriptor;
56
+ };
57
+
58
+ } // namespace react
59
+ } // namespace facebook
60
+
61
+ #endif // RCT_NEW_ARCH_ENABLED
@@ -1,5 +1,6 @@
1
+ #import "MetalView.h"
1
2
  #import "RCTBridge.h"
2
- #import "Utils.h"
3
+ #import "WebGPUModule.h"
3
4
  #import <React/RCTUIManager.h>
4
5
  #import <React/RCTViewManager.h>
5
6
 
@@ -11,11 +12,12 @@
11
12
  RCT_EXPORT_MODULE(WebGPUView)
12
13
 
13
14
  - (UIView *)view {
14
- return [[UIView alloc] init];
15
+ return [MetalView new];
15
16
  }
16
17
 
17
- RCT_CUSTOM_VIEW_PROPERTY(color, NSString, UIView) {
18
- [view setBackgroundColor:[Utils hexStringToColor:json]];
18
+ RCT_CUSTOM_VIEW_PROPERTY(contextId, NSNumber, UIView) {
19
+ NSNumber *contextId = [RCTConvert NSNumber:json];
20
+ [(MetalView *)view setContextId:contextId];
19
21
  }
20
22
 
21
23
  @end