Skip to content

Commit 5bc1098

Browse files
committed
🔧
1 parent 3a576eb commit 5bc1098

17 files changed

Lines changed: 159 additions & 9 deletions

packages/webgpu/apple/RNWGUIKit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ typedef UIView RNWGPlatformView;
1313
typedef NSView RNWGPlatformView;
1414
#endif
1515

16-
#endif // PACKAGES_WEBGPU_APPLE_RNWGUIKIT_H_
16+
#endif // PACKAGES_WEBGPU_APPLE_RNWGUIKIT_H_

packages/webgpu/cpp/jsi/RNFHybridObject.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ class HybridObject : public jsi::HostObject, public std::enable_shared_from_this
6565
*/
6666
virtual std::string toString(jsi::Runtime& runtime);
6767

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+
6874
private:
6975
static constexpr auto TAG = "HybridObject";
7076
int _instanceId = 1;

packages/webgpu/cpp/jsi/RNFJSIConverter.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,12 @@ template <typename T> struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_h
446446
throw jsi::JSError(runtime, "Cannot convert nullptr to HostObject<" + getFriendlyTypename() + ">!");
447447
}
448448
#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;
450455
}
451456
};
452457

packages/webgpu/cpp/rnwgpu/api/GPUBindGroup.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ class GPUBindGroup : public m::HybridObject {
3737

3838
inline const wgpu::BindGroup get() { return _instance; }
3939

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+
4047
private:
4148
wgpu::BindGroup _instance;
4249
std::string _label;

packages/webgpu/cpp/rnwgpu/api/GPUBindGroupLayout.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ class GPUBindGroupLayout : public m::HybridObject {
3838

3939
inline const wgpu::BindGroupLayout get() { return _instance; }
4040

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+
4148
private:
4249
wgpu::BindGroupLayout _instance;
4350
std::string _label;

packages/webgpu/cpp/rnwgpu/api/GPUBuffer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class GPUBuffer : public m::HybridObject {
6262

6363
inline const wgpu::Buffer get() { return _instance; }
6464

65+
size_t getMemoryPressure() override { return static_cast<size_t>(getSize()); }
66+
6567
private:
6668
wgpu::Buffer _instance;
6769
std::shared_ptr<AsyncRunner> _async;

packages/webgpu/cpp/rnwgpu/api/GPUCanvasContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "GPUCanvasContext.h"
2-
#include <memory>
32
#include "Convertors.h"
43
#include "RNWebGPUManager.h"
4+
#include <memory>
55

66
#ifdef __APPLE__
77
#include "dawn/native/MetalBackend.h"

packages/webgpu/cpp/rnwgpu/api/GPUComputePipeline.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class GPUComputePipeline : public m::HybridObject {
4545

4646
inline const wgpu::ComputePipeline get() { return _instance; }
4747

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+
4855
private:
4956
wgpu::ComputePipeline _instance;
5057
std::string _label;

packages/webgpu/cpp/rnwgpu/api/GPUQuerySet.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ class GPUQuerySet : public m::HybridObject {
4444

4545
inline const wgpu::QuerySet get() { return _instance; }
4646

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+
4768
private:
4869
wgpu::QuerySet _instance;
4970
std::string _label;

packages/webgpu/cpp/rnwgpu/api/GPUQueue.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ void GPUQueue::writeBuffer(std::shared_ptr<GPUBuffer> buffer,
4040

4141
// Note that in the JS semantics of WebGPU, writeBuffer works in number of
4242
// 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)) {
4445
throw std::runtime_error("dataOffset is larger than data's size.");
4546
return;
4647
}

0 commit comments

Comments
 (0)