Skip to content

Commit 4de88c3

Browse files
committed
🔧
1 parent bbfc9e0 commit 4de88c3

29 files changed

Lines changed: 125 additions & 6 deletions

packages/webgpu/cpp/jsi/RNFHybridObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class HybridObject : public jsi::HostObject, public std::enable_shared_from_this
7777
* Get the memory pressure of this HostObject in bytes.
7878
* This is used to inform the JavaScript runtime about memory usage for garbage collection.
7979
*/
80-
virtual size_t getMemoryPressure() { return 1024; }
80+
virtual size_t getMemoryPressure() = 0;
8181

8282
private:
8383
static constexpr auto TAG = "HybridObject";

packages/webgpu/cpp/jsi/RNFPointerHolder.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ namespace margelo {
1616
namespace jsi = facebook::jsi;
1717

1818
template <typename T> class PointerHolder : public HybridObject {
19+
public:
20+
size_t getMemoryPressure() override {
21+
std::unique_lock lock(_mutex);
22+
if (_pointer == nullptr) {
23+
return 0;
24+
}
25+
// Default to a small-but-nonzero floor so holders contribute to pressure.
26+
return 64 * 1024; // 64KB
27+
}
28+
1929
protected:
2030
// no default constructor
2131
PointerHolder() = delete;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class Canvas : public m::HybridObject {
4444
registerHybridSetter("height", &Canvas::setHeight, this);
4545
}
4646

47+
size_t getMemoryPressure() override { return sizeof(Canvas); }
48+
4749
private:
4850
void *_surface;
4951
int _width;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class GPU : public m::HybridObject {
4848

4949
inline const wgpu::Instance get() { return _instance; }
5050

51+
size_t getMemoryPressure() override {
52+
// Instance discovery spins up adapter caches and driver state.
53+
return 512 * 1024; // 512KB baseline
54+
}
55+
5156
private:
5257
wgpu::Instance _instance;
5358
std::shared_ptr<async::AsyncRunner> _async;

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

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

4949
inline const wgpu::Adapter get() { return _instance; }
5050

51+
size_t getMemoryPressure() override { return 1024; }
52+
5153
private:
5254
wgpu::Adapter _instance;
5355
std::shared_ptr<async::AsyncRunner> _async;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ class GPUAdapterInfo : public m::HybridObject {
4444
&GPUAdapterInfo::getIsFallbackAdapter, this);
4545
}
4646

47+
size_t getMemoryPressure() override {
48+
return sizeof(GPUAdapterInfo) + _vendor.capacity() +
49+
_architecture.capacity() + _device.capacity() +
50+
_description.capacity();
51+
}
52+
4753
private:
4854
std::string _vendor;
4955
std::string _architecture;

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <algorithm>
34
#include <memory>
45
#include <string>
56
#include <utility>
@@ -46,6 +47,32 @@ class GPUCanvasContext : public m::HybridObject {
4647
registerHybridMethod("present", &GPUCanvasContext::present, this);
4748
}
4849

50+
size_t getMemoryPressure() override {
51+
int width = 0;
52+
int height = 0;
53+
54+
if (_surfaceInfo) {
55+
auto size = _surfaceInfo->getSize();
56+
width = size.width;
57+
height = size.height;
58+
}
59+
60+
if (_canvas) {
61+
width = std::max(width, _canvas->getWidth());
62+
height = std::max(height, _canvas->getHeight());
63+
}
64+
65+
if (width <= 0 || height <= 0) {
66+
return 4 * 1024 * 1024; // default to 4MB when size is unknown
67+
}
68+
69+
constexpr size_t kBytesPerPixel = 4; // RGBA8 fallback
70+
constexpr size_t kFloor = 4 * 1024 * 1024;
71+
size_t estimated = static_cast<size_t>(width) *
72+
static_cast<size_t>(height) * kBytesPerPixel;
73+
return std::max(estimated, kFloor);
74+
}
75+
4976
// TODO: is this ok?
5077
inline const wgpu::Surface get() { return nullptr; }
5178
void configure(std::shared_ptr<GPUCanvasConfiguration> configuration);

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

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

3636
inline const wgpu::CommandBuffer get() { return _instance; }
3737

38+
size_t getMemoryPressure() override { return 1024 * 1024; }
39+
3840
private:
3941
wgpu::CommandBuffer _instance;
4042
std::string _label;

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

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

101101
inline const wgpu::CommandEncoder get() { return _instance; }
102102

103+
size_t getMemoryPressure() override { return 1024 * 1024; }
104+
103105
private:
104106
wgpu::CommandEncoder _instance;
105107
std::string _label;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ class GPUCompilationInfo : public m::HybridObject {
3636
registerHybridGetter("messages", &GPUCompilationInfo::getMessages, this);
3737
}
3838

39+
size_t getMemoryPressure() override {
40+
size_t total = sizeof(GPUCompilationInfo) +
41+
_messages.capacity() * sizeof(GPUCompilationMessage);
42+
for (const auto &message : _messages) {
43+
total += message.message.capacity();
44+
}
45+
return total;
46+
}
47+
3948
private:
4049
std::vector<GPUCompilationMessage> _messages;
4150
friend class GPUShaderModule;

0 commit comments

Comments
 (0)