react-native-wgpu 0.2.2 → 0.2.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.
package/apple/RNWGUIKit.h CHANGED
@@ -13,4 +13,4 @@ typedef UIView RNWGPlatformView;
13
13
  typedef NSView RNWGPlatformView;
14
14
  #endif
15
15
 
16
- #endif // PACKAGES_WEBGPU_APPLE_RNWGUIKIT_H_
16
+ #endif // PACKAGES_WEBGPU_APPLE_RNWGUIKIT_H_
@@ -65,6 +65,12 @@ public:
65
65
  */
66
66
  virtual std::string toString(jsi::Runtime& runtime);
67
67
 
68
+ /**
69
+ * Get the memory pressure of this HostObject in bytes.
70
+ * This is used to inform the JavaScript runtime about memory usage for garbage collection.
71
+ */
72
+ virtual size_t getMemoryPressure() { return 1024; }
73
+
68
74
  private:
69
75
  static constexpr auto TAG = "HybridObject";
70
76
  int _instanceId = 1;
@@ -446,7 +446,12 @@ template <typename T> struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_h
446
446
  throw jsi::JSError(runtime, "Cannot convert nullptr to HostObject<" + getFriendlyTypename() + ">!");
447
447
  }
448
448
  #endif
449
- return jsi::Object::createFromHostObject(runtime, arg);
449
+ auto result = jsi::Object::createFromHostObject(runtime, arg);
450
+ auto memoryPressure = arg->getMemoryPressure();
451
+ if (memoryPressure > 0) {
452
+ result.setExternalMemoryPressure(runtime, memoryPressure);
453
+ }
454
+ return result;
450
455
  }
451
456
  };
452
457
 
@@ -37,6 +37,13 @@ public:
37
37
 
38
38
  inline const wgpu::BindGroup get() { return _instance; }
39
39
 
40
+ size_t getMemoryPressure() override {
41
+ // Bind groups store resource bindings and descriptor state
42
+ // They reference buffers, textures, samplers, etc.
43
+ // Estimate: 1KB per bind group (descriptor tables and binding state)
44
+ return 1024;
45
+ }
46
+
40
47
  private:
41
48
  wgpu::BindGroup _instance;
42
49
  std::string _label;
@@ -38,6 +38,13 @@ public:
38
38
 
39
39
  inline const wgpu::BindGroupLayout get() { return _instance; }
40
40
 
41
+ size_t getMemoryPressure() override {
42
+ // Bind group layouts define the structure/schema for bind groups
43
+ // They store binding descriptors, types, and validation info
44
+ // Estimate: 512 bytes per layout (smaller than actual bind groups)
45
+ return 512;
46
+ }
47
+
41
48
  private:
42
49
  wgpu::BindGroupLayout _instance;
43
50
  std::string _label;
@@ -62,6 +62,8 @@ public:
62
62
 
63
63
  inline const wgpu::Buffer get() { return _instance; }
64
64
 
65
+ size_t getMemoryPressure() override { return static_cast<size_t>(getSize()); }
66
+
65
67
  private:
66
68
  wgpu::Buffer _instance;
67
69
  std::shared_ptr<AsyncRunner> _async;
@@ -1,7 +1,7 @@
1
1
  #include "GPUCanvasContext.h"
2
- #include <memory>
3
2
  #include "Convertors.h"
4
3
  #include "RNWebGPUManager.h"
4
+ #include <memory>
5
5
 
6
6
  #ifdef __APPLE__
7
7
  #include "dawn/native/MetalBackend.h"
@@ -45,6 +45,13 @@ public:
45
45
 
46
46
  inline const wgpu::ComputePipeline get() { return _instance; }
47
47
 
48
+ size_t getMemoryPressure() override {
49
+ // Compute pipelines contain compiled compute shader state and
50
+ // driver-specific optimized code
51
+ // Estimate: 16KB for a typical compute pipeline (single compute shader)
52
+ return 16 * 1024;
53
+ }
54
+
48
55
  private:
49
56
  wgpu::ComputePipeline _instance;
50
57
  std::string _label;
@@ -44,6 +44,27 @@ public:
44
44
 
45
45
  inline const wgpu::QuerySet get() { return _instance; }
46
46
 
47
+ size_t getMemoryPressure() override {
48
+ uint32_t count = getCount();
49
+ wgpu::QueryType type = getType();
50
+
51
+ // Estimate bytes per query based on type
52
+ size_t bytesPerQuery = 8; // Default estimate
53
+ switch (type) {
54
+ case wgpu::QueryType::Occlusion:
55
+ bytesPerQuery = 8; // 64-bit counter
56
+ break;
57
+ case wgpu::QueryType::Timestamp:
58
+ bytesPerQuery = 8; // 64-bit timestamp
59
+ break;
60
+ default:
61
+ bytesPerQuery = 8; // Safe default
62
+ break;
63
+ }
64
+
65
+ return static_cast<size_t>(count) * bytesPerQuery;
66
+ }
67
+
47
68
  private:
48
69
  wgpu::QuerySet _instance;
49
70
  std::string _label;
@@ -40,7 +40,8 @@ void GPUQueue::writeBuffer(std::shared_ptr<GPUBuffer> buffer,
40
40
 
41
41
  // Note that in the JS semantics of WebGPU, writeBuffer works in number of
42
42
  // elements of the typed arrays.
43
- if (dataOffsetElements > static_cast<uint64_t>(src.size / src.bytesPerElement)) {
43
+ if (dataOffsetElements >
44
+ static_cast<uint64_t>(src.size / src.bytesPerElement)) {
44
45
  throw std::runtime_error("dataOffset is larger than data's size.");
45
46
  return;
46
47
  }
@@ -1,6 +1,6 @@
1
- #include <vector>
2
- #include <string>
3
1
  #include <memory>
2
+ #include <string>
3
+ #include <vector>
4
4
 
5
5
  #include "GPURenderPassEncoder.h"
6
6
  #include "Convertors.h"
@@ -162,4 +162,4 @@ void GPURenderPassEncoder::drawIndexedIndirect(
162
162
  _instance.DrawIndexedIndirect(b, indirectOffset);
163
163
  }
164
164
 
165
- } // namespace rnwgpu
165
+ } // namespace rnwgpu
@@ -44,6 +44,14 @@ public:
44
44
 
45
45
  inline const wgpu::RenderPipeline get() { return _instance; }
46
46
 
47
+ size_t getMemoryPressure() override {
48
+ // Render pipelines contain compiled shader state, vertex/fragment shaders,
49
+ // render state, and driver-specific optimized code
50
+ // Estimate: 24KB for a typical render pipeline with vertex + fragment
51
+ // shaders
52
+ return 24 * 1024;
53
+ }
54
+
47
55
  private:
48
56
  wgpu::RenderPipeline _instance;
49
57
  std::string _label;
@@ -1,7 +1,7 @@
1
1
  #include "GPUShaderModule.h"
2
2
 
3
- #include <utility>
4
3
  #include <memory>
4
+ #include <utility>
5
5
 
6
6
  namespace rnwgpu {
7
7
 
@@ -48,6 +48,14 @@ public:
48
48
 
49
49
  inline const wgpu::ShaderModule get() { return _instance; }
50
50
 
51
+ size_t getMemoryPressure() override {
52
+ // Estimate memory usage for compiled shader module
53
+ // Shaders can vary widely, but a reasonable estimate is 8-16KB for typical
54
+ // shaders Complex shaders (with many uniforms, textures, or computations)
55
+ // can be much larger
56
+ return 12 * 1024; // 12KB estimate for average shader
57
+ }
58
+
51
59
  private:
52
60
  wgpu::ShaderModule _instance;
53
61
  std::shared_ptr<AsyncRunner> _async;
@@ -1,5 +1,6 @@
1
1
  #pragma once
2
2
 
3
+ #include <algorithm>
3
4
  #include <memory>
4
5
  #include <string>
5
6
 
@@ -64,6 +65,81 @@ public:
64
65
 
65
66
  inline const wgpu::Texture get() { return _instance; }
66
67
 
68
+ size_t getMemoryPressure() override {
69
+ // Calculate approximate memory usage based on texture properties
70
+ uint32_t width = getWidth();
71
+ uint32_t height = getHeight();
72
+ uint32_t depthOrArrayLayers = getDepthOrArrayLayers();
73
+ uint32_t mipLevelCount = getMipLevelCount();
74
+ uint32_t sampleCount = getSampleCount();
75
+
76
+ // Estimate bytes per pixel based on format
77
+ // This is a simplified estimate - actual values depend on the specific
78
+ // format
79
+ size_t bytesPerPixel = 4; // Default to RGBA8 format
80
+ wgpu::TextureFormat format = getFormat();
81
+ switch (format) {
82
+ case wgpu::TextureFormat::R8Unorm:
83
+ case wgpu::TextureFormat::R8Snorm:
84
+ case wgpu::TextureFormat::R8Uint:
85
+ case wgpu::TextureFormat::R8Sint:
86
+ bytesPerPixel = 1;
87
+ break;
88
+ case wgpu::TextureFormat::R16Uint:
89
+ case wgpu::TextureFormat::R16Sint:
90
+ case wgpu::TextureFormat::R16Float:
91
+ case wgpu::TextureFormat::RG8Unorm:
92
+ case wgpu::TextureFormat::RG8Snorm:
93
+ case wgpu::TextureFormat::RG8Uint:
94
+ case wgpu::TextureFormat::RG8Sint:
95
+ bytesPerPixel = 2;
96
+ break;
97
+ case wgpu::TextureFormat::RGBA8Unorm:
98
+ case wgpu::TextureFormat::RGBA8UnormSrgb:
99
+ case wgpu::TextureFormat::RGBA8Snorm:
100
+ case wgpu::TextureFormat::RGBA8Uint:
101
+ case wgpu::TextureFormat::RGBA8Sint:
102
+ case wgpu::TextureFormat::BGRA8Unorm:
103
+ case wgpu::TextureFormat::BGRA8UnormSrgb:
104
+ case wgpu::TextureFormat::RGB10A2Unorm:
105
+ case wgpu::TextureFormat::R32Float:
106
+ case wgpu::TextureFormat::R32Uint:
107
+ case wgpu::TextureFormat::R32Sint:
108
+ case wgpu::TextureFormat::RG16Uint:
109
+ case wgpu::TextureFormat::RG16Sint:
110
+ case wgpu::TextureFormat::RG16Float:
111
+ bytesPerPixel = 4;
112
+ break;
113
+ case wgpu::TextureFormat::RG32Float:
114
+ case wgpu::TextureFormat::RG32Uint:
115
+ case wgpu::TextureFormat::RG32Sint:
116
+ case wgpu::TextureFormat::RGBA16Uint:
117
+ case wgpu::TextureFormat::RGBA16Sint:
118
+ case wgpu::TextureFormat::RGBA16Float:
119
+ bytesPerPixel = 8;
120
+ break;
121
+ case wgpu::TextureFormat::RGBA32Float:
122
+ case wgpu::TextureFormat::RGBA32Uint:
123
+ case wgpu::TextureFormat::RGBA32Sint:
124
+ bytesPerPixel = 16;
125
+ break;
126
+ default:
127
+ bytesPerPixel = 4; // Safe default
128
+ break;
129
+ }
130
+
131
+ // Calculate total memory for all mip levels
132
+ size_t totalMemory = 0;
133
+ for (uint32_t mip = 0; mip < mipLevelCount; ++mip) {
134
+ uint32_t mipWidth = std::max(1u, width >> mip);
135
+ uint32_t mipHeight = std::max(1u, height >> mip);
136
+ totalMemory += static_cast<size_t>(mipWidth) * mipHeight *
137
+ depthOrArrayLayers * bytesPerPixel * sampleCount;
138
+ }
139
+
140
+ return totalMemory;
141
+ }
142
+
67
143
  private:
68
144
  wgpu::Texture _instance;
69
145
  std::string _label;
@@ -27,6 +27,8 @@ public:
27
27
  registerHybridGetter("height", &ImageBitmap::getHeight, this);
28
28
  }
29
29
 
30
+ size_t getMemoryPressure() override { return getSize(); }
31
+
30
32
  private:
31
33
  ImageData _imageData;
32
34
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-wgpu",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "React Native WebGPU",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",