Skip to content

Commit d985d04

Browse files
committed
🔧
1 parent bfbe325 commit d985d04

5 files changed

Lines changed: 86 additions & 79 deletions

File tree

apps/example/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,7 +1865,7 @@ PODS:
18651865
- ReactCommon/turbomodule/core
18661866
- SocketRocket
18671867
- Yoga
1868-
- react-native-wgpu (0.5.8):
1868+
- react-native-wgpu (0.5.10):
18691869
- boost
18701870
- DoubleConversion
18711871
- fast_float
@@ -2937,8 +2937,8 @@ SPEC CHECKSUMS:
29372937
React-Mapbuffer: 9d2434a42701d6144ca18f0ca1c4507808ca7696
29382938
React-microtasksnativemodule: 75b6604b667d297292345302cc5bfb6b6aeccc1b
29392939
react-native-safe-area-context: c00143b4823773bba23f2f19f85663ae89ceb460
2940-
react-native-skia: 3dab14f6a3de3c479a73d87cb1f9aa9556dcec13
2941-
react-native-wgpu: 32f373d5a9ee83fad1cdddd288bb0738cb97e0ba
2940+
react-native-skia: 888a5bf5ed5008ae9593990e7dc2ea022b9aea11
2941+
react-native-wgpu: 1ec8f2457ce741ac80bf5bf7b33e13c3c76c7b2a
29422942
React-NativeModulesApple: 879fbdc5dcff7136abceb7880fe8a2022a1bd7c3
29432943
React-oscompat: 93b5535ea7f7dff46aaee4f78309a70979bdde9d
29442944
React-perflogger: 5536d2df3d18fe0920263466f7b46a56351c0510

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ std::shared_ptr<GPUTexture> GPUCanvasContext::getCurrentTexture() {
4848
_surfaceInfo->reconfigure(width, height);
4949
}
5050
auto texture = _surfaceInfo->getCurrentTexture();
51-
return std::make_shared<GPUTexture>(texture, "");
51+
// The canvas texture doesn't own the rendering buffer memory, so we pass
52+
// false to avoid reporting large memory pressure that triggers spurious
53+
// Hermes GC cycles every frame.
54+
return std::make_shared<GPUTexture>(texture, "", false);
5255
}
5356

5457
void GPUCanvasContext::present() {

packages/webgpu/cpp/rnwgpu/api/GPUTexture.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,74 @@ double GPUTexture::getUsage() {
4444
return static_cast<double>(_instance.GetUsage());
4545
}
4646

47+
size_t GPUTexture::_computeMemoryPressure() {
48+
uint32_t width = getWidth();
49+
uint32_t height = getHeight();
50+
uint32_t depthOrArrayLayers = getDepthOrArrayLayers();
51+
uint32_t mipLevelCount = getMipLevelCount();
52+
uint32_t sampleCount = getSampleCount();
53+
54+
size_t bytesPerPixel = 4;
55+
wgpu::TextureFormat format = getFormat();
56+
switch (format) {
57+
case wgpu::TextureFormat::R8Unorm:
58+
case wgpu::TextureFormat::R8Snorm:
59+
case wgpu::TextureFormat::R8Uint:
60+
case wgpu::TextureFormat::R8Sint:
61+
bytesPerPixel = 1;
62+
break;
63+
case wgpu::TextureFormat::R16Uint:
64+
case wgpu::TextureFormat::R16Sint:
65+
case wgpu::TextureFormat::R16Float:
66+
case wgpu::TextureFormat::RG8Unorm:
67+
case wgpu::TextureFormat::RG8Snorm:
68+
case wgpu::TextureFormat::RG8Uint:
69+
case wgpu::TextureFormat::RG8Sint:
70+
bytesPerPixel = 2;
71+
break;
72+
case wgpu::TextureFormat::RGBA8Unorm:
73+
case wgpu::TextureFormat::RGBA8UnormSrgb:
74+
case wgpu::TextureFormat::RGBA8Snorm:
75+
case wgpu::TextureFormat::RGBA8Uint:
76+
case wgpu::TextureFormat::RGBA8Sint:
77+
case wgpu::TextureFormat::BGRA8Unorm:
78+
case wgpu::TextureFormat::BGRA8UnormSrgb:
79+
case wgpu::TextureFormat::RGB10A2Unorm:
80+
case wgpu::TextureFormat::R32Float:
81+
case wgpu::TextureFormat::R32Uint:
82+
case wgpu::TextureFormat::R32Sint:
83+
case wgpu::TextureFormat::RG16Uint:
84+
case wgpu::TextureFormat::RG16Sint:
85+
case wgpu::TextureFormat::RG16Float:
86+
bytesPerPixel = 4;
87+
break;
88+
case wgpu::TextureFormat::RG32Float:
89+
case wgpu::TextureFormat::RG32Uint:
90+
case wgpu::TextureFormat::RG32Sint:
91+
case wgpu::TextureFormat::RGBA16Uint:
92+
case wgpu::TextureFormat::RGBA16Sint:
93+
case wgpu::TextureFormat::RGBA16Float:
94+
bytesPerPixel = 8;
95+
break;
96+
case wgpu::TextureFormat::RGBA32Float:
97+
case wgpu::TextureFormat::RGBA32Uint:
98+
case wgpu::TextureFormat::RGBA32Sint:
99+
bytesPerPixel = 16;
100+
break;
101+
default:
102+
bytesPerPixel = 4;
103+
break;
104+
}
105+
106+
size_t totalMemory = 0;
107+
for (uint32_t mip = 0; mip < mipLevelCount; ++mip) {
108+
uint32_t mipWidth = std::max(1u, width >> mip);
109+
uint32_t mipHeight = std::max(1u, height >> mip);
110+
totalMemory += static_cast<size_t>(mipWidth) * mipHeight *
111+
depthOrArrayLayers * bytesPerPixel * sampleCount;
112+
}
113+
114+
return totalMemory;
115+
}
116+
47117
} // namespace rnwgpu

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

Lines changed: 8 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ class GPUTexture : public NativeObject<GPUTexture> {
2121
public:
2222
static constexpr const char *CLASS_NAME = "GPUTexture";
2323

24-
explicit GPUTexture(wgpu::Texture instance, std::string label)
25-
: NativeObject(CLASS_NAME), _instance(instance), _label(label) {}
24+
explicit GPUTexture(wgpu::Texture instance, std::string label,
25+
bool ownsMemory = true)
26+
: NativeObject(CLASS_NAME), _instance(instance), _label(label),
27+
_ownsMemory(ownsMemory) {}
2628

2729
public:
2830
std::string getBrand() { return CLASS_NAME; }
@@ -68,83 +70,15 @@ class GPUTexture : public NativeObject<GPUTexture> {
6870
inline const wgpu::Texture get() { return _instance; }
6971

7072
size_t getMemoryPressure() override {
71-
// Calculate approximate memory usage based on texture properties
72-
uint32_t width = getWidth();
73-
uint32_t height = getHeight();
74-
uint32_t depthOrArrayLayers = getDepthOrArrayLayers();
75-
uint32_t mipLevelCount = getMipLevelCount();
76-
uint32_t sampleCount = getSampleCount();
77-
78-
// Estimate bytes per pixel based on format
79-
// This is a simplified estimate - actual values depend on the specific
80-
// format
81-
size_t bytesPerPixel = 4; // Default to RGBA8 format
82-
wgpu::TextureFormat format = getFormat();
83-
switch (format) {
84-
case wgpu::TextureFormat::R8Unorm:
85-
case wgpu::TextureFormat::R8Snorm:
86-
case wgpu::TextureFormat::R8Uint:
87-
case wgpu::TextureFormat::R8Sint:
88-
bytesPerPixel = 1;
89-
break;
90-
case wgpu::TextureFormat::R16Uint:
91-
case wgpu::TextureFormat::R16Sint:
92-
case wgpu::TextureFormat::R16Float:
93-
case wgpu::TextureFormat::RG8Unorm:
94-
case wgpu::TextureFormat::RG8Snorm:
95-
case wgpu::TextureFormat::RG8Uint:
96-
case wgpu::TextureFormat::RG8Sint:
97-
bytesPerPixel = 2;
98-
break;
99-
case wgpu::TextureFormat::RGBA8Unorm:
100-
case wgpu::TextureFormat::RGBA8UnormSrgb:
101-
case wgpu::TextureFormat::RGBA8Snorm:
102-
case wgpu::TextureFormat::RGBA8Uint:
103-
case wgpu::TextureFormat::RGBA8Sint:
104-
case wgpu::TextureFormat::BGRA8Unorm:
105-
case wgpu::TextureFormat::BGRA8UnormSrgb:
106-
case wgpu::TextureFormat::RGB10A2Unorm:
107-
case wgpu::TextureFormat::R32Float:
108-
case wgpu::TextureFormat::R32Uint:
109-
case wgpu::TextureFormat::R32Sint:
110-
case wgpu::TextureFormat::RG16Uint:
111-
case wgpu::TextureFormat::RG16Sint:
112-
case wgpu::TextureFormat::RG16Float:
113-
bytesPerPixel = 4;
114-
break;
115-
case wgpu::TextureFormat::RG32Float:
116-
case wgpu::TextureFormat::RG32Uint:
117-
case wgpu::TextureFormat::RG32Sint:
118-
case wgpu::TextureFormat::RGBA16Uint:
119-
case wgpu::TextureFormat::RGBA16Sint:
120-
case wgpu::TextureFormat::RGBA16Float:
121-
bytesPerPixel = 8;
122-
break;
123-
case wgpu::TextureFormat::RGBA32Float:
124-
case wgpu::TextureFormat::RGBA32Uint:
125-
case wgpu::TextureFormat::RGBA32Sint:
126-
bytesPerPixel = 16;
127-
break;
128-
default:
129-
bytesPerPixel = 4; // Safe default
130-
break;
131-
}
132-
133-
// Calculate total memory for all mip levels
134-
size_t totalMemory = 0;
135-
for (uint32_t mip = 0; mip < mipLevelCount; ++mip) {
136-
uint32_t mipWidth = std::max(1u, width >> mip);
137-
uint32_t mipHeight = std::max(1u, height >> mip);
138-
totalMemory += static_cast<size_t>(mipWidth) * mipHeight *
139-
depthOrArrayLayers * bytesPerPixel * sampleCount;
140-
}
141-
142-
return totalMemory;
73+
return _ownsMemory ? _computeMemoryPressure() : sizeof(GPUTexture);
14374
}
14475

14576
private:
77+
size_t _computeMemoryPressure();
78+
14679
wgpu::Texture _instance;
14780
std::string _label;
81+
bool _ownsMemory = true;
14882
};
14983

15084
} // namespace rnwgpu

packages/webgpu/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-wgpu",
3-
"version": "0.5.9",
3+
"version": "0.5.10",
44
"description": "React Native WebGPU",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

0 commit comments

Comments
 (0)