@shopify/react-native-skia 2.4.15 → 2.4.17

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 (226) hide show
  1. package/android/CMakeLists.txt +69 -0
  2. package/android/src/main/java/com/shopify/reactnative/skia/SkiaBaseView.java +19 -7
  3. package/android/src/main/java/com/shopify/reactnative/skia/SkiaBaseViewManager.java +7 -0
  4. package/apple/RNSkApplePlatformContext.mm +4 -0
  5. package/cpp/api/JsiSkApi.h +31 -0
  6. package/cpp/api/JsiSkImageFactory.h +69 -1
  7. package/cpp/api/JsiSkImageFilter.h +1 -0
  8. package/cpp/api/JsiSkPath.h +1 -1
  9. package/cpp/jsi/RuntimeAwareCache.h +0 -2
  10. package/cpp/jsi2/EnumMapper.h +45 -0
  11. package/cpp/jsi2/JSIConverter.h +468 -0
  12. package/cpp/jsi2/NativeObject.h +598 -0
  13. package/cpp/jsi2/Promise.cpp +44 -0
  14. package/cpp/jsi2/Promise.h +35 -0
  15. package/cpp/rnskia/RNDawnContext.h +114 -0
  16. package/cpp/rnskia/RNSkManager.cpp +41 -1
  17. package/cpp/rnwgpu/ArrayBuffer.h +68 -0
  18. package/cpp/rnwgpu/api/Convertors.h +761 -0
  19. package/cpp/rnwgpu/api/External.h +12 -0
  20. package/cpp/rnwgpu/api/GPU.cpp +129 -0
  21. package/cpp/rnwgpu/api/GPU.h +57 -0
  22. package/cpp/rnwgpu/api/GPUAdapter.cpp +178 -0
  23. package/cpp/rnwgpu/api/GPUAdapter.h +59 -0
  24. package/cpp/rnwgpu/api/GPUAdapterInfo.h +57 -0
  25. package/cpp/rnwgpu/api/GPUBindGroup.cpp +3 -0
  26. package/cpp/rnwgpu/api/GPUBindGroup.h +51 -0
  27. package/cpp/rnwgpu/api/GPUBindGroupLayout.h +52 -0
  28. package/cpp/rnwgpu/api/GPUBuffer.cpp +97 -0
  29. package/cpp/rnwgpu/api/GPUBuffer.h +87 -0
  30. package/cpp/rnwgpu/api/GPUCommandBuffer.h +45 -0
  31. package/cpp/rnwgpu/api/GPUCommandEncoder.cpp +179 -0
  32. package/cpp/rnwgpu/api/GPUCommandEncoder.h +111 -0
  33. package/cpp/rnwgpu/api/GPUCompilationInfo.h +79 -0
  34. package/cpp/rnwgpu/api/GPUCompilationMessage.h +36 -0
  35. package/cpp/rnwgpu/api/GPUComputePassEncoder.cpp +60 -0
  36. package/cpp/rnwgpu/api/GPUComputePassEncoder.h +85 -0
  37. package/cpp/rnwgpu/api/GPUComputePipeline.cpp +12 -0
  38. package/cpp/rnwgpu/api/GPUComputePipeline.h +60 -0
  39. package/cpp/rnwgpu/api/GPUDevice.cpp +434 -0
  40. package/cpp/rnwgpu/api/GPUDevice.h +174 -0
  41. package/cpp/rnwgpu/api/GPUDeviceLostInfo.cpp +7 -0
  42. package/cpp/rnwgpu/api/GPUDeviceLostInfo.h +40 -0
  43. package/cpp/rnwgpu/api/GPUError.h +35 -0
  44. package/cpp/rnwgpu/api/GPUExtent3D.h +66 -0
  45. package/cpp/rnwgpu/api/GPUExternalTexture.h +45 -0
  46. package/cpp/rnwgpu/api/GPUFeatures.h +212 -0
  47. package/cpp/rnwgpu/api/GPUInternalError.h +31 -0
  48. package/cpp/rnwgpu/api/GPUOrigin2D.h +55 -0
  49. package/cpp/rnwgpu/api/GPUOrigin3D.h +62 -0
  50. package/cpp/rnwgpu/api/GPUOutOfMemoryError.h +33 -0
  51. package/cpp/rnwgpu/api/GPUPipelineLayout.h +45 -0
  52. package/cpp/rnwgpu/api/GPUQuerySet.cpp +11 -0
  53. package/cpp/rnwgpu/api/GPUQuerySet.h +73 -0
  54. package/cpp/rnwgpu/api/GPUQueue.cpp +169 -0
  55. package/cpp/rnwgpu/api/GPUQueue.h +84 -0
  56. package/cpp/rnwgpu/api/GPURenderBundle.h +44 -0
  57. package/cpp/rnwgpu/api/GPURenderBundleEncoder.cpp +131 -0
  58. package/cpp/rnwgpu/api/GPURenderBundleEncoder.h +110 -0
  59. package/cpp/rnwgpu/api/GPURenderPassEncoder.cpp +165 -0
  60. package/cpp/rnwgpu/api/GPURenderPassEncoder.h +130 -0
  61. package/cpp/rnwgpu/api/GPURenderPipeline.cpp +13 -0
  62. package/cpp/rnwgpu/api/GPURenderPipeline.h +61 -0
  63. package/cpp/rnwgpu/api/GPUSampler.h +44 -0
  64. package/cpp/rnwgpu/api/GPUShaderModule.cpp +51 -0
  65. package/cpp/rnwgpu/api/GPUShaderModule.h +66 -0
  66. package/cpp/rnwgpu/api/GPUSupportedLimits.cpp +129 -0
  67. package/cpp/rnwgpu/api/GPUSupportedLimits.h +131 -0
  68. package/cpp/rnwgpu/api/GPUTexture.cpp +47 -0
  69. package/cpp/rnwgpu/api/GPUTexture.h +150 -0
  70. package/cpp/rnwgpu/api/GPUTextureView.h +44 -0
  71. package/cpp/rnwgpu/api/GPUValidationError.h +32 -0
  72. package/cpp/rnwgpu/api/descriptors/GPUBindGroupDescriptor.h +64 -0
  73. package/cpp/rnwgpu/api/descriptors/GPUBindGroupEntry.h +71 -0
  74. package/cpp/rnwgpu/api/descriptors/GPUBindGroupLayoutDescriptor.h +56 -0
  75. package/cpp/rnwgpu/api/descriptors/GPUBindGroupLayoutEntry.h +97 -0
  76. package/cpp/rnwgpu/api/descriptors/GPUBlendComponent.h +57 -0
  77. package/cpp/rnwgpu/api/descriptors/GPUBlendState.h +52 -0
  78. package/cpp/rnwgpu/api/descriptors/GPUBufferBinding.h +56 -0
  79. package/cpp/rnwgpu/api/descriptors/GPUBufferBindingLayout.h +56 -0
  80. package/cpp/rnwgpu/api/descriptors/GPUBufferDescriptor.h +59 -0
  81. package/cpp/rnwgpu/api/descriptors/GPUBufferUsage.h +39 -0
  82. package/cpp/rnwgpu/api/descriptors/GPUColor.h +66 -0
  83. package/cpp/rnwgpu/api/descriptors/GPUColorTargetState.h +58 -0
  84. package/cpp/rnwgpu/api/descriptors/GPUColorWrite.h +29 -0
  85. package/cpp/rnwgpu/api/descriptors/GPUCommandBufferDescriptor.h +45 -0
  86. package/cpp/rnwgpu/api/descriptors/GPUCommandEncoderDescriptor.h +45 -0
  87. package/cpp/rnwgpu/api/descriptors/GPUComputePassDescriptor.h +56 -0
  88. package/cpp/rnwgpu/api/descriptors/GPUComputePassTimestampWrites.h +58 -0
  89. package/cpp/rnwgpu/api/descriptors/GPUComputePipelineDescriptor.h +67 -0
  90. package/cpp/rnwgpu/api/descriptors/GPUDepthStencilState.h +103 -0
  91. package/cpp/rnwgpu/api/descriptors/GPUDeviceDescriptor.h +99 -0
  92. package/cpp/rnwgpu/api/descriptors/GPUExternalTextureBindingLayout.h +38 -0
  93. package/cpp/rnwgpu/api/descriptors/GPUExternalTextureDescriptor.h +71 -0
  94. package/cpp/rnwgpu/api/descriptors/GPUFragmentState.h +73 -0
  95. package/cpp/rnwgpu/api/descriptors/GPUImageCopyBuffer.h +62 -0
  96. package/cpp/rnwgpu/api/descriptors/GPUImageCopyExternalImage.h +62 -0
  97. package/cpp/rnwgpu/api/descriptors/GPUImageCopyTexture.h +65 -0
  98. package/cpp/rnwgpu/api/descriptors/GPUImageCopyTextureTagged.h +81 -0
  99. package/cpp/rnwgpu/api/descriptors/GPUImageDataLayout.h +54 -0
  100. package/cpp/rnwgpu/api/descriptors/GPUMapMode.h +22 -0
  101. package/cpp/rnwgpu/api/descriptors/GPUMultisampleState.h +54 -0
  102. package/cpp/rnwgpu/api/descriptors/GPUPipelineLayoutDescriptor.h +57 -0
  103. package/cpp/rnwgpu/api/descriptors/GPUPrimitiveState.h +69 -0
  104. package/cpp/rnwgpu/api/descriptors/GPUProgrammableStage.h +61 -0
  105. package/cpp/rnwgpu/api/descriptors/GPUQuerySetDescriptor.h +55 -0
  106. package/cpp/rnwgpu/api/descriptors/GPUQueueDescriptor.h +43 -0
  107. package/cpp/rnwgpu/api/descriptors/GPURenderBundleDescriptor.h +45 -0
  108. package/cpp/rnwgpu/api/descriptors/GPURenderBundleEncoderDescriptor.h +80 -0
  109. package/cpp/rnwgpu/api/descriptors/GPURenderPassColorAttachment.h +81 -0
  110. package/cpp/rnwgpu/api/descriptors/GPURenderPassDepthStencilAttachment.h +101 -0
  111. package/cpp/rnwgpu/api/descriptors/GPURenderPassDescriptor.h +92 -0
  112. package/cpp/rnwgpu/api/descriptors/GPURenderPassTimestampWrites.h +58 -0
  113. package/cpp/rnwgpu/api/descriptors/GPURenderPipelineDescriptor.h +103 -0
  114. package/cpp/rnwgpu/api/descriptors/GPURequestAdapterOptions.h +51 -0
  115. package/cpp/rnwgpu/api/descriptors/GPUSamplerBindingLayout.h +45 -0
  116. package/cpp/rnwgpu/api/descriptors/GPUSamplerDescriptor.h +110 -0
  117. package/cpp/rnwgpu/api/descriptors/GPUShaderModuleCompilationHint.h +60 -0
  118. package/cpp/rnwgpu/api/descriptors/GPUShaderModuleDescriptor.h +62 -0
  119. package/cpp/rnwgpu/api/descriptors/GPUShaderStage.h +25 -0
  120. package/cpp/rnwgpu/api/descriptors/GPUStencilFaceState.h +64 -0
  121. package/cpp/rnwgpu/api/descriptors/GPUStorageTextureBindingLayout.h +59 -0
  122. package/cpp/rnwgpu/api/descriptors/GPUTextureBindingLayout.h +59 -0
  123. package/cpp/rnwgpu/api/descriptors/GPUTextureDescriptor.h +91 -0
  124. package/cpp/rnwgpu/api/descriptors/GPUTextureUsage.h +29 -0
  125. package/cpp/rnwgpu/api/descriptors/GPUTextureViewDescriptor.h +91 -0
  126. package/cpp/rnwgpu/api/descriptors/GPUUncapturedErrorEventInit.h +64 -0
  127. package/cpp/rnwgpu/api/descriptors/GPUVertexAttribute.h +53 -0
  128. package/cpp/rnwgpu/api/descriptors/GPUVertexBufferLayout.h +62 -0
  129. package/cpp/rnwgpu/api/descriptors/GPUVertexState.h +73 -0
  130. package/cpp/rnwgpu/api/descriptors/Unions.h +1984 -0
  131. package/cpp/rnwgpu/async/AsyncDispatcher.h +28 -0
  132. package/cpp/rnwgpu/async/AsyncRunner.cpp +181 -0
  133. package/cpp/rnwgpu/async/AsyncRunner.h +56 -0
  134. package/cpp/rnwgpu/async/AsyncTaskHandle.cpp +181 -0
  135. package/cpp/rnwgpu/async/AsyncTaskHandle.h +55 -0
  136. package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.cpp +23 -0
  137. package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.h +22 -0
  138. package/lib/commonjs/Platform/Platform.web.js +1 -2
  139. package/lib/commonjs/Platform/Platform.web.js.map +1 -1
  140. package/lib/commonjs/external/reanimated/buffers.js.map +1 -1
  141. package/lib/commonjs/external/reanimated/interpolators.d.ts +1 -4
  142. package/lib/commonjs/renderer/Canvas.js +4 -3
  143. package/lib/commonjs/renderer/Canvas.js.map +1 -1
  144. package/lib/commonjs/renderer/components/Group.js +1 -2
  145. package/lib/commonjs/renderer/components/Group.js.map +1 -1
  146. package/lib/commonjs/renderer/components/shapes/FitBox.js +1 -2
  147. package/lib/commonjs/renderer/components/shapes/FitBox.js.map +1 -1
  148. package/lib/commonjs/skia/types/ContourMeasure.js.map +1 -1
  149. package/lib/commonjs/skia/types/Image/ImageFactory.d.ts +20 -0
  150. package/lib/commonjs/skia/types/Image/ImageFactory.js.map +1 -1
  151. package/lib/commonjs/skia/types/RuntimeEffect/RuntimeEffect.js.map +1 -1
  152. package/lib/commonjs/skia/types/Skia.d.ts +17 -0
  153. package/lib/commonjs/skia/types/Skia.js.map +1 -1
  154. package/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +2 -0
  155. package/lib/commonjs/skia/web/JsiSkImageFactory.js +6 -0
  156. package/lib/commonjs/skia/web/JsiSkImageFactory.js.map +1 -1
  157. package/lib/commonjs/skia/web/JsiSkParagraphStyle.js +2 -2
  158. package/lib/commonjs/skia/web/JsiSkParagraphStyle.js.map +1 -1
  159. package/lib/commonjs/skia/web/JsiSkia.js +6 -0
  160. package/lib/commonjs/skia/web/JsiSkia.js.map +1 -1
  161. package/lib/commonjs/sksg/Recorder/commands/ColorFilters.js.map +1 -1
  162. package/lib/commonjs/sksg/Recorder/commands/ImageFilters.js.map +1 -1
  163. package/lib/commonjs/sksg/Recorder/commands/PathEffects.js.map +1 -1
  164. package/lib/commonjs/sksg/Recorder/commands/Shaders.js.map +1 -1
  165. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.d.ts +2 -0
  166. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.js +2 -0
  167. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.js.map +1 -1
  168. package/lib/commonjs/views/SkiaPictureView.web.js +4 -4
  169. package/lib/commonjs/views/SkiaPictureView.web.js.map +1 -1
  170. package/lib/commonjs/web/WithSkiaWeb.js +1 -2
  171. package/lib/commonjs/web/WithSkiaWeb.js.map +1 -1
  172. package/lib/module/external/reanimated/buffers.js.map +1 -1
  173. package/lib/module/external/reanimated/interpolators.d.ts +1 -4
  174. package/lib/module/renderer/Canvas.js +3 -1
  175. package/lib/module/renderer/Canvas.js.map +1 -1
  176. package/lib/module/skia/types/ContourMeasure.js.map +1 -1
  177. package/lib/module/skia/types/Image/ImageFactory.d.ts +20 -0
  178. package/lib/module/skia/types/Image/ImageFactory.js.map +1 -1
  179. package/lib/module/skia/types/RuntimeEffect/RuntimeEffect.js.map +1 -1
  180. package/lib/module/skia/types/Skia.d.ts +17 -0
  181. package/lib/module/skia/types/Skia.js.map +1 -1
  182. package/lib/module/skia/web/JsiSkImageFactory.d.ts +2 -0
  183. package/lib/module/skia/web/JsiSkImageFactory.js +6 -0
  184. package/lib/module/skia/web/JsiSkImageFactory.js.map +1 -1
  185. package/lib/module/skia/web/JsiSkParagraphStyle.js +2 -2
  186. package/lib/module/skia/web/JsiSkParagraphStyle.js.map +1 -1
  187. package/lib/module/skia/web/JsiSkia.js +6 -0
  188. package/lib/module/skia/web/JsiSkia.js.map +1 -1
  189. package/lib/module/sksg/Recorder/commands/ColorFilters.js.map +1 -1
  190. package/lib/module/sksg/Recorder/commands/ImageFilters.js.map +1 -1
  191. package/lib/module/sksg/Recorder/commands/PathEffects.js.map +1 -1
  192. package/lib/module/sksg/Recorder/commands/Shaders.js.map +1 -1
  193. package/lib/module/specs/SkiaPictureViewNativeComponent.d.ts +2 -0
  194. package/lib/module/specs/SkiaPictureViewNativeComponent.js +4 -0
  195. package/lib/module/specs/SkiaPictureViewNativeComponent.js.map +1 -1
  196. package/lib/module/views/SkiaPictureView.web.js +3 -2
  197. package/lib/module/views/SkiaPictureView.web.js.map +1 -1
  198. package/lib/typescript/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +2 -0
  199. package/lib/typescript/lib/commonjs/skia/web/JsiSkia.d.ts +2 -0
  200. package/lib/typescript/lib/module/renderer/Canvas.d.ts +1 -1
  201. package/lib/typescript/lib/module/skia/Skia.web.d.ts +2 -0
  202. package/lib/typescript/lib/module/skia/web/JsiSkImageFactory.d.ts +2 -0
  203. package/lib/typescript/lib/module/skia/web/JsiSkia.d.ts +2 -0
  204. package/lib/typescript/lib/module/views/SkiaPictureView.d.ts +1 -1
  205. package/lib/typescript/src/external/reanimated/interpolators.d.ts +1 -4
  206. package/lib/typescript/src/skia/types/Image/ImageFactory.d.ts +20 -0
  207. package/lib/typescript/src/skia/types/Skia.d.ts +17 -0
  208. package/lib/typescript/src/skia/web/JsiSkImageFactory.d.ts +2 -0
  209. package/lib/typescript/src/specs/SkiaPictureViewNativeComponent.d.ts +2 -0
  210. package/package.json +20 -16
  211. package/react-native-skia.podspec +52 -13
  212. package/scripts/install-skia.mjs +99 -48
  213. package/src/external/reanimated/buffers.ts +1 -1
  214. package/src/renderer/Canvas.tsx +3 -2
  215. package/src/skia/types/ContourMeasure.tsx +1 -2
  216. package/src/skia/types/Image/ImageFactory.ts +22 -0
  217. package/src/skia/types/RuntimeEffect/RuntimeEffect.ts +1 -2
  218. package/src/skia/types/Skia.ts +17 -0
  219. package/src/skia/web/JsiSkImageFactory.ts +8 -0
  220. package/src/skia/web/JsiSkia.ts +6 -0
  221. package/src/sksg/Recorder/commands/ColorFilters.ts +3 -2
  222. package/src/sksg/Recorder/commands/ImageFilters.ts +3 -2
  223. package/src/sksg/Recorder/commands/PathEffects.ts +3 -2
  224. package/src/sksg/Recorder/commands/Shaders.ts +3 -2
  225. package/src/specs/SkiaPictureViewNativeComponent.ts +7 -0
  226. package/src/views/SkiaPictureView.web.tsx +4 -1
@@ -0,0 +1,85 @@
1
+ #pragma once
2
+
3
+ #include <memory>
4
+ #include <optional>
5
+ #include <string>
6
+ #include <variant>
7
+ #include <vector>
8
+
9
+ #include "descriptors/Unions.h"
10
+
11
+ #include "jsi2/NativeObject.h"
12
+
13
+ #include "webgpu/webgpu_cpp.h"
14
+
15
+ #include "GPUBindGroup.h"
16
+ #include "GPUBuffer.h"
17
+ #include "GPUComputePipeline.h"
18
+
19
+ namespace rnwgpu {
20
+
21
+ namespace jsi = facebook::jsi;
22
+
23
+ class GPUComputePassEncoder : public NativeObject<GPUComputePassEncoder> {
24
+ public:
25
+ static constexpr const char *CLASS_NAME = "GPUComputePassEncoder";
26
+
27
+ explicit GPUComputePassEncoder(wgpu::ComputePassEncoder instance,
28
+ std::string label)
29
+ : NativeObject(CLASS_NAME), _instance(instance), _label(label) {}
30
+
31
+ public:
32
+ std::string getBrand() { return CLASS_NAME; }
33
+
34
+ void setPipeline(std::shared_ptr<GPUComputePipeline> pipeline);
35
+ void dispatchWorkgroups(uint32_t workgroupCountX,
36
+ std::optional<uint32_t> workgroupCountY,
37
+ std::optional<uint32_t> workgroupCountZ);
38
+ void dispatchWorkgroupsIndirect(std::shared_ptr<GPUBuffer> indirectBuffer,
39
+ uint64_t indirectOffset);
40
+ void end();
41
+ void pushDebugGroup(std::string groupLabel);
42
+ void popDebugGroup();
43
+ void insertDebugMarker(std::string markerLabel);
44
+ void setBindGroup(
45
+ uint32_t index,
46
+ std::variant<std::nullptr_t, std::shared_ptr<GPUBindGroup>> bindGroup,
47
+ std::optional<std::vector<uint32_t>> dynamicOffsets);
48
+
49
+ std::string getLabel() { return _label; }
50
+ void setLabel(const std::string &label) {
51
+ _label = label;
52
+ _instance.SetLabel(_label.c_str());
53
+ }
54
+
55
+ static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
56
+ installGetter(runtime, prototype, "__brand",
57
+ &GPUComputePassEncoder::getBrand);
58
+ installMethod(runtime, prototype, "setPipeline",
59
+ &GPUComputePassEncoder::setPipeline);
60
+ installMethod(runtime, prototype, "dispatchWorkgroups",
61
+ &GPUComputePassEncoder::dispatchWorkgroups);
62
+ installMethod(runtime, prototype, "dispatchWorkgroupsIndirect",
63
+ &GPUComputePassEncoder::dispatchWorkgroupsIndirect);
64
+ installMethod(runtime, prototype, "end", &GPUComputePassEncoder::end);
65
+ installMethod(runtime, prototype, "pushDebugGroup",
66
+ &GPUComputePassEncoder::pushDebugGroup);
67
+ installMethod(runtime, prototype, "popDebugGroup",
68
+ &GPUComputePassEncoder::popDebugGroup);
69
+ installMethod(runtime, prototype, "insertDebugMarker",
70
+ &GPUComputePassEncoder::insertDebugMarker);
71
+ installMethod(runtime, prototype, "setBindGroup",
72
+ &GPUComputePassEncoder::setBindGroup);
73
+ installGetterSetter(runtime, prototype, "label",
74
+ &GPUComputePassEncoder::getLabel,
75
+ &GPUComputePassEncoder::setLabel);
76
+ }
77
+
78
+ inline const wgpu::ComputePassEncoder get() { return _instance; }
79
+
80
+ private:
81
+ wgpu::ComputePassEncoder _instance;
82
+ std::string _label;
83
+ };
84
+
85
+ } // namespace rnwgpu
@@ -0,0 +1,12 @@
1
+ #include "GPUComputePipeline.h"
2
+ #include <memory>
3
+
4
+ namespace rnwgpu {
5
+
6
+ std::shared_ptr<GPUBindGroupLayout>
7
+ GPUComputePipeline::getBindGroupLayout(uint32_t groupIndex) {
8
+ auto bindGroup = _instance.GetBindGroupLayout(groupIndex);
9
+ return std::make_shared<GPUBindGroupLayout>(bindGroup, "");
10
+ }
11
+
12
+ } // namespace rnwgpu
@@ -0,0 +1,60 @@
1
+ #pragma once
2
+
3
+ #include <memory>
4
+ #include <string>
5
+
6
+ #include "descriptors/Unions.h"
7
+
8
+ #include "jsi2/NativeObject.h"
9
+
10
+ #include "webgpu/webgpu_cpp.h"
11
+
12
+ #include "GPUBindGroupLayout.h"
13
+
14
+ namespace rnwgpu {
15
+
16
+ namespace jsi = facebook::jsi;
17
+
18
+ class GPUComputePipeline : public NativeObject<GPUComputePipeline> {
19
+ public:
20
+ static constexpr const char *CLASS_NAME = "GPUComputePipeline";
21
+
22
+ explicit GPUComputePipeline(wgpu::ComputePipeline instance, std::string label)
23
+ : NativeObject(CLASS_NAME), _instance(instance), _label(label) {}
24
+
25
+ public:
26
+ std::string getBrand() { return CLASS_NAME; }
27
+
28
+ std::shared_ptr<GPUBindGroupLayout> getBindGroupLayout(uint32_t index);
29
+
30
+ std::string getLabel() { return _label; }
31
+ void setLabel(const std::string &label) {
32
+ _label = label;
33
+ _instance.SetLabel(_label.c_str());
34
+ }
35
+
36
+ static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
37
+ installGetter(runtime, prototype, "__brand", &GPUComputePipeline::getBrand);
38
+ installMethod(runtime, prototype, "getBindGroupLayout",
39
+ &GPUComputePipeline::getBindGroupLayout);
40
+ installGetterSetter(runtime, prototype, "label",
41
+ &GPUComputePipeline::getLabel,
42
+ &GPUComputePipeline::setLabel);
43
+ }
44
+
45
+ inline const wgpu::ComputePipeline get() { return _instance; }
46
+
47
+ size_t getMemoryPressure() override {
48
+ // Compute pipelines contain compiled compute shader state and
49
+ // driver-specific optimized code
50
+ // Estimate: 16KB for a typical compute pipeline (single compute shader)
51
+ return 16 * 1024;
52
+ }
53
+
54
+ private:
55
+ wgpu::ComputePipeline _instance;
56
+ std::string _label;
57
+ friend class GPUDevice;
58
+ };
59
+
60
+ } // namespace rnwgpu
@@ -0,0 +1,434 @@
1
+ #include "GPUDevice.h"
2
+
3
+ #include <memory>
4
+ #include <string>
5
+ #include <unordered_set>
6
+ #include <utility>
7
+ #include <vector>
8
+
9
+ #include "Convertors.h"
10
+ #include "jsi2/JSIConverter.h"
11
+
12
+ #include "GPUFeatures.h"
13
+ #include "GPUInternalError.h"
14
+ #include "GPUOutOfMemoryError.h"
15
+ #include "GPUValidationError.h"
16
+
17
+ namespace rnwgpu {
18
+
19
+ void GPUDevice::notifyDeviceLost(wgpu::DeviceLostReason reason,
20
+ std::string message) {
21
+ if (_lostSettled) {
22
+ return;
23
+ }
24
+
25
+ _lostSettled = true;
26
+ _lostInfo = std::make_shared<GPUDeviceLostInfo>(reason, std::move(message));
27
+
28
+ if (_lostResolve.has_value()) {
29
+ auto resolve = std::move(*_lostResolve);
30
+ _lostResolve.reset();
31
+ resolve([info = _lostInfo](jsi::Runtime &runtime) mutable {
32
+ return JSIConverter<std::shared_ptr<GPUDeviceLostInfo>>::toJSI(runtime,
33
+ info);
34
+ });
35
+ }
36
+
37
+ _lostHandle.reset();
38
+ }
39
+
40
+ void GPUDevice::forceLossForTesting() {
41
+ // wgpu::StringView view("forceLossForTesting invoked from JS");
42
+ _instance.ForceLoss(wgpu::DeviceLostReason::Unknown,
43
+ "forceLossForTesting invoked from JS");
44
+ }
45
+
46
+ std::shared_ptr<GPUBuffer>
47
+ GPUDevice::createBuffer(std::shared_ptr<GPUBufferDescriptor> descriptor) {
48
+ wgpu::BufferDescriptor desc;
49
+ Convertor conv;
50
+ if (!conv(desc, descriptor)) {
51
+ throw std::runtime_error(
52
+ "GPUDevice::createBuffer(): Error with GPUBufferDescriptor");
53
+ }
54
+ auto result = _instance.CreateBuffer(&desc);
55
+ return std::make_shared<GPUBuffer>(result, _async,
56
+ descriptor->label.value_or(""));
57
+ }
58
+
59
+ std::shared_ptr<GPUSupportedLimits> GPUDevice::getLimits() {
60
+ wgpu::Limits limits{};
61
+ if (!_instance.GetLimits(&limits)) {
62
+ throw std::runtime_error("failed to get device limits");
63
+ }
64
+ return std::make_shared<GPUSupportedLimits>(limits);
65
+ }
66
+
67
+ std::shared_ptr<GPUQueue> GPUDevice::getQueue() {
68
+ auto result = _instance.GetQueue();
69
+ return std::make_shared<GPUQueue>(result, _async, _label);
70
+ }
71
+
72
+ std::shared_ptr<GPUCommandEncoder> GPUDevice::createCommandEncoder(
73
+ std::optional<std::shared_ptr<GPUCommandEncoderDescriptor>> descriptor) {
74
+ wgpu::CommandEncoderDescriptor desc;
75
+ Convertor conv;
76
+ if (!conv(desc, descriptor)) {
77
+ throw std::runtime_error("Error with GPUCommandEncoderDescriptor");
78
+ }
79
+ auto result = _instance.CreateCommandEncoder(&desc);
80
+ return std::make_shared<GPUCommandEncoder>(
81
+ result,
82
+ descriptor.has_value() ? descriptor.value()->label.value_or("") : "");
83
+ }
84
+
85
+ void GPUDevice::destroy() {
86
+ _instance.Destroy();
87
+ notifyDeviceLost(wgpu::DeviceLostReason::Destroyed, "device was destroyed");
88
+ }
89
+
90
+ std::shared_ptr<GPUTexture>
91
+ GPUDevice::createTexture(std::shared_ptr<GPUTextureDescriptor> descriptor) {
92
+ wgpu::TextureDescriptor desc;
93
+ Convertor conv;
94
+ if (!conv(desc, descriptor)) {
95
+ throw std::runtime_error("Error with GPUTextureDescriptor");
96
+ }
97
+ auto texture = _instance.CreateTexture(&desc);
98
+ return std::make_shared<GPUTexture>(texture, descriptor->label.value_or(""));
99
+ }
100
+
101
+ std::shared_ptr<GPUShaderModule> GPUDevice::createShaderModule(
102
+ std::shared_ptr<GPUShaderModuleDescriptor> descriptor) {
103
+ wgpu::ShaderSourceWGSL wgsl_desc{};
104
+ wgpu::ShaderModuleDescriptor sm_desc{};
105
+ Convertor conv;
106
+ if (!conv(wgsl_desc.code, descriptor->code) ||
107
+ !conv(sm_desc.label, descriptor->label)) {
108
+ return {};
109
+ }
110
+ sm_desc.nextInChain = &wgsl_desc;
111
+ if (descriptor->code.find('\0') != std::string::npos) {
112
+ auto mod = _instance.CreateErrorShaderModule(
113
+ &sm_desc, "The WGSL shader contains an illegal character '\\0'");
114
+ return std::make_shared<GPUShaderModule>(mod, _async, sm_desc.label.data);
115
+ }
116
+ auto module = _instance.CreateShaderModule(&sm_desc);
117
+ return std::make_shared<GPUShaderModule>(module, _async,
118
+ descriptor->label.value_or(""));
119
+ }
120
+
121
+ std::shared_ptr<GPURenderPipeline> GPUDevice::createRenderPipeline(
122
+ std::shared_ptr<GPURenderPipelineDescriptor> descriptor) {
123
+ wgpu::RenderPipelineDescriptor desc{};
124
+ Convertor conv;
125
+ if (!conv(desc, descriptor)) {
126
+ throw std::runtime_error("Error with GPURenderPipelineDescriptor");
127
+ }
128
+ // assert(desc.fragment != nullptr && "Fragment state must not be null");
129
+ auto renderPipeline = _instance.CreateRenderPipeline(&desc);
130
+ return std::make_shared<GPURenderPipeline>(renderPipeline,
131
+ descriptor->label.value_or(""));
132
+ }
133
+
134
+ std::shared_ptr<GPUBindGroup>
135
+ GPUDevice::createBindGroup(std::shared_ptr<GPUBindGroupDescriptor> descriptor) {
136
+ Convertor conv;
137
+ wgpu::BindGroupDescriptor desc{};
138
+ if (!conv(desc.label, descriptor->label) ||
139
+ !conv(desc.layout, descriptor->layout) ||
140
+ !conv(desc.entries, desc.entryCount, descriptor->entries)) {
141
+ throw std::runtime_error(
142
+ "GPUBindGroup::createBindGroup(): Error with GPUBindGroupDescriptor");
143
+ }
144
+ auto bindGroup = _instance.CreateBindGroup(&desc);
145
+ return std::make_shared<GPUBindGroup>(bindGroup,
146
+ descriptor->label.value_or(""));
147
+ }
148
+
149
+ std::shared_ptr<GPUSampler> GPUDevice::createSampler(
150
+ std::optional<std::shared_ptr<GPUSamplerDescriptor>> descriptor) {
151
+ wgpu::SamplerDescriptor desc;
152
+ Convertor conv;
153
+ if (!conv(desc, descriptor)) {
154
+ throw std::runtime_error("GPUDevice::createSampler(): Error with "
155
+ "GPUSamplerDescriptor");
156
+ }
157
+ auto sampler = _instance.CreateSampler(&desc);
158
+ return std::make_shared<GPUSampler>(
159
+ sampler,
160
+ descriptor.has_value() ? descriptor.value()->label.value_or("") : "");
161
+ }
162
+
163
+ std::shared_ptr<GPUComputePipeline> GPUDevice::createComputePipeline(
164
+ std::shared_ptr<GPUComputePipelineDescriptor> descriptor) {
165
+ wgpu::ComputePipelineDescriptor desc;
166
+ Convertor conv;
167
+ if (!conv(desc, descriptor)) {
168
+ throw std::runtime_error("GPUDevice::createComputePipeline(): Error with "
169
+ "GPUComputePipelineDescriptor");
170
+ }
171
+ auto computePipeline = _instance.CreateComputePipeline(&desc);
172
+ return std::make_shared<GPUComputePipeline>(computePipeline,
173
+ descriptor->label.value_or(""));
174
+ }
175
+
176
+ std::shared_ptr<GPUQuerySet>
177
+ GPUDevice::createQuerySet(std::shared_ptr<GPUQuerySetDescriptor> descriptor) {
178
+ wgpu::QuerySetDescriptor desc;
179
+ Convertor conv;
180
+ if (!conv(desc, descriptor)) {
181
+ throw std::runtime_error("GPUDevice::createQuerySet(): Error with "
182
+ "GPUQuerySetDescriptor");
183
+ }
184
+ auto querySet = _instance.CreateQuerySet(&desc);
185
+ return std::make_shared<GPUQuerySet>(querySet,
186
+ descriptor->label.value_or(""));
187
+ }
188
+
189
+ std::shared_ptr<GPURenderBundleEncoder> GPUDevice::createRenderBundleEncoder(
190
+ std::shared_ptr<GPURenderBundleEncoderDescriptor> descriptor) {
191
+ Convertor conv;
192
+
193
+ wgpu::RenderBundleEncoderDescriptor desc{};
194
+ if (!conv(desc.label, descriptor->label) ||
195
+ !conv(desc.colorFormats, desc.colorFormatCount,
196
+ descriptor->colorFormats) ||
197
+ !conv(desc.depthStencilFormat, descriptor->depthStencilFormat) ||
198
+ !conv(desc.sampleCount, descriptor->sampleCount) ||
199
+ !conv(desc.depthReadOnly, descriptor->depthReadOnly) ||
200
+ !conv(desc.stencilReadOnly, descriptor->stencilReadOnly)) {
201
+ return {};
202
+ }
203
+ return std::make_shared<GPURenderBundleEncoder>(
204
+ _instance.CreateRenderBundleEncoder(&desc),
205
+ descriptor->label.value_or(""));
206
+ }
207
+
208
+ std::shared_ptr<GPUBindGroupLayout> GPUDevice::createBindGroupLayout(
209
+ std::shared_ptr<GPUBindGroupLayoutDescriptor> descriptor) {
210
+ Convertor conv;
211
+
212
+ wgpu::BindGroupLayoutDescriptor desc{};
213
+ if (!conv(desc.label, descriptor->label) ||
214
+ !conv(desc.entries, desc.entryCount, descriptor->entries)) {
215
+ return {};
216
+ }
217
+ return std::make_shared<GPUBindGroupLayout>(
218
+ _instance.CreateBindGroupLayout(&desc), descriptor->label.value_or(""));
219
+ }
220
+
221
+ std::shared_ptr<GPUPipelineLayout> GPUDevice::createPipelineLayout(
222
+ std::shared_ptr<GPUPipelineLayoutDescriptor> descriptor) {
223
+ Convertor conv;
224
+
225
+ wgpu::PipelineLayoutDescriptor desc{};
226
+ if (!conv(desc.label, descriptor->label) ||
227
+ !conv(desc.bindGroupLayouts, desc.bindGroupLayoutCount,
228
+ descriptor->bindGroupLayouts)) {
229
+ return {};
230
+ }
231
+ return std::make_shared<GPUPipelineLayout>(
232
+ _instance.CreatePipelineLayout(&desc), descriptor->label.value_or(""));
233
+ }
234
+
235
+ std::shared_ptr<GPUExternalTexture> GPUDevice::importExternalTexture(
236
+ std::shared_ptr<GPUExternalTextureDescriptor> descriptor) {
237
+ throw std::runtime_error(
238
+ "GPUDevice::importExternalTexture(): Not implemented");
239
+ }
240
+
241
+ async::AsyncTaskHandle GPUDevice::createComputePipelineAsync(
242
+ std::shared_ptr<GPUComputePipelineDescriptor> descriptor) {
243
+ wgpu::ComputePipelineDescriptor desc{};
244
+ Convertor conv;
245
+ if (!conv(desc, descriptor)) {
246
+ throw std::runtime_error("GPUDevice::createComputePipeline(): Error with "
247
+ "GPUComputePipelineDescriptor");
248
+ }
249
+
250
+ auto label = std::string(
251
+ descriptor->label.has_value() ? descriptor->label.value() : "");
252
+ auto pipelineHolder = std::make_shared<GPUComputePipeline>(nullptr, label);
253
+
254
+ return _async->postTask([device = _instance, desc, descriptor,
255
+ pipelineHolder](
256
+ const async::AsyncTaskHandle::ResolveFunction
257
+ &resolve,
258
+ const async::AsyncTaskHandle::RejectFunction
259
+ &reject) {
260
+ (void)descriptor;
261
+ device.CreateComputePipelineAsync(
262
+ &desc, wgpu::CallbackMode::AllowProcessEvents,
263
+ [pipelineHolder, resolve,
264
+ reject](wgpu::CreatePipelineAsyncStatus status,
265
+ wgpu::ComputePipeline pipeline, const char *msg) mutable {
266
+ if (status == wgpu::CreatePipelineAsyncStatus::Success && pipeline) {
267
+ pipelineHolder->_instance = pipeline;
268
+ resolve([pipelineHolder](jsi::Runtime &runtime) mutable {
269
+ return JSIConverter<std::shared_ptr<GPUComputePipeline>>::toJSI(
270
+ runtime, pipelineHolder);
271
+ });
272
+ } else {
273
+ std::string error =
274
+ msg ? std::string(msg) : "Failed to create compute pipeline";
275
+ reject(std::move(error));
276
+ }
277
+ });
278
+ });
279
+ }
280
+
281
+ async::AsyncTaskHandle GPUDevice::createRenderPipelineAsync(
282
+ std::shared_ptr<GPURenderPipelineDescriptor> descriptor) {
283
+ wgpu::RenderPipelineDescriptor desc{};
284
+ Convertor conv;
285
+ if (!conv(desc, descriptor)) {
286
+ throw std::runtime_error(
287
+ "GPUDevice::createRenderPipelineAsync(): Error with "
288
+ "GPURenderPipelineDescriptor");
289
+ }
290
+
291
+ auto label = std::string(
292
+ descriptor->label.has_value() ? descriptor->label.value() : "");
293
+ auto pipelineHolder = std::make_shared<GPURenderPipeline>(nullptr, label);
294
+
295
+ return _async->postTask([device = _instance, desc, descriptor,
296
+ pipelineHolder](
297
+ const async::AsyncTaskHandle::ResolveFunction
298
+ &resolve,
299
+ const async::AsyncTaskHandle::RejectFunction
300
+ &reject) {
301
+ (void)descriptor;
302
+ device.CreateRenderPipelineAsync(
303
+ &desc, wgpu::CallbackMode::AllowProcessEvents,
304
+ [pipelineHolder, resolve,
305
+ reject](wgpu::CreatePipelineAsyncStatus status,
306
+ wgpu::RenderPipeline pipeline, const char *msg) mutable {
307
+ if (status == wgpu::CreatePipelineAsyncStatus::Success && pipeline) {
308
+ pipelineHolder->_instance = pipeline;
309
+ resolve([pipelineHolder](jsi::Runtime &runtime) mutable {
310
+ return JSIConverter<std::shared_ptr<GPURenderPipeline>>::toJSI(
311
+ runtime, pipelineHolder);
312
+ });
313
+ } else {
314
+ std::string error =
315
+ msg ? std::string(msg) : "Failed to create render pipeline";
316
+ reject(std::move(error));
317
+ }
318
+ });
319
+ });
320
+ }
321
+
322
+ void GPUDevice::pushErrorScope(wgpu::ErrorFilter filter) {
323
+ _instance.PushErrorScope(filter);
324
+ }
325
+
326
+ async::AsyncTaskHandle GPUDevice::popErrorScope() {
327
+ auto device = _instance;
328
+
329
+ return _async->postTask([device](const async::AsyncTaskHandle::ResolveFunction
330
+ &resolve,
331
+ const async::AsyncTaskHandle::RejectFunction
332
+ &reject) {
333
+ device.PopErrorScope(
334
+ wgpu::CallbackMode::AllowProcessEvents,
335
+ [resolve, reject](wgpu::PopErrorScopeStatus status,
336
+ wgpu::ErrorType type, wgpu::StringView message) {
337
+ if (status == wgpu::PopErrorScopeStatus::Error ||
338
+ status == wgpu::PopErrorScopeStatus::CallbackCancelled) {
339
+ reject("PopErrorScope failed");
340
+ return;
341
+ }
342
+
343
+ std::string messageString =
344
+ message.length ? std::string(message.data, message.length) : "";
345
+
346
+ switch (type) {
347
+ case wgpu::ErrorType::NoError:
348
+ resolve([](jsi::Runtime &runtime) mutable {
349
+ return jsi::Value::null();
350
+ });
351
+ break;
352
+ case wgpu::ErrorType::Validation: {
353
+ auto error = std::make_shared<GPUValidationError>(messageString);
354
+ resolve([error](jsi::Runtime &runtime) mutable {
355
+ return JSIConverter<std::shared_ptr<GPUValidationError>>::toJSI(
356
+ runtime, error);
357
+ });
358
+ break;
359
+ }
360
+ case wgpu::ErrorType::OutOfMemory: {
361
+ auto error = std::make_shared<GPUOutOfMemoryError>(messageString);
362
+ resolve([error](jsi::Runtime &runtime) mutable {
363
+ return JSIConverter<std::shared_ptr<GPUOutOfMemoryError>>::toJSI(
364
+ runtime, error);
365
+ });
366
+ break;
367
+ }
368
+ case wgpu::ErrorType::Internal:
369
+ case wgpu::ErrorType::Unknown: {
370
+ auto error = std::make_shared<GPUInternalError>(messageString);
371
+ resolve([error](jsi::Runtime &runtime) mutable {
372
+ return JSIConverter<std::shared_ptr<GPUInternalError>>::toJSI(
373
+ runtime, error);
374
+ });
375
+ break;
376
+ }
377
+ default:
378
+ reject("Unhandled GPU error type");
379
+ return;
380
+ }
381
+ });
382
+ });
383
+ }
384
+
385
+ std::unordered_set<std::string> GPUDevice::getFeatures() {
386
+ wgpu::SupportedFeatures supportedFeatures;
387
+ _instance.GetFeatures(&supportedFeatures);
388
+ std::unordered_set<std::string> result;
389
+ for (size_t i = 0; i < supportedFeatures.featureCount; ++i) {
390
+ auto feature = supportedFeatures.features[i];
391
+ std::string name;
392
+ convertEnumToJSUnion(feature, &name);
393
+ result.insert(name);
394
+ }
395
+ return result;
396
+ }
397
+
398
+ async::AsyncTaskHandle GPUDevice::getLost() {
399
+ if (_lostHandle.has_value()) {
400
+ return *_lostHandle;
401
+ }
402
+
403
+ if (_lostSettled && _lostInfo) {
404
+ return _async->postTask(
405
+ [info = _lostInfo](
406
+ const async::AsyncTaskHandle::ResolveFunction &resolve,
407
+ const async::AsyncTaskHandle::RejectFunction & /*reject*/) {
408
+ resolve([info](jsi::Runtime &runtime) mutable {
409
+ return JSIConverter<std::shared_ptr<GPUDeviceLostInfo>>::toJSI(
410
+ runtime, info);
411
+ });
412
+ },
413
+ false);
414
+ }
415
+
416
+ auto handle = _async->postTask(
417
+ [this](const async::AsyncTaskHandle::ResolveFunction &resolve,
418
+ const async::AsyncTaskHandle::RejectFunction & /*reject*/) {
419
+ if (_lostSettled && _lostInfo) {
420
+ resolve([info = _lostInfo](jsi::Runtime &runtime) mutable {
421
+ return JSIConverter<std::shared_ptr<GPUDeviceLostInfo>>::toJSI(
422
+ runtime, info);
423
+ });
424
+ return;
425
+ }
426
+
427
+ _lostResolve = resolve;
428
+ },
429
+ false);
430
+
431
+ _lostHandle = handle;
432
+ return handle;
433
+ }
434
+ } // namespace rnwgpu