Skip to content

Commit cc702e9

Browse files
committed
💚
1 parent d985d04 commit cc702e9

3 files changed

Lines changed: 86 additions & 79 deletions

File tree

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ std::shared_ptr<GPUTexture> GPUCanvasContext::getCurrentTexture() {
4848
_surfaceInfo->reconfigure(width, height);
4949
}
5050
auto texture = _surfaceInfo->getCurrentTexture();
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.
51+
// Pass reportsMemoryPressure=false to avoid triggering spurious Hermes GC
52+
// cycles every frame since the canvas texture doesn't own the buffer.
5453
return std::make_shared<GPUTexture>(texture, "", false);
5554
}
5655

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

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -44,74 +44,4 @@ 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-
11747
} // namespace rnwgpu

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

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class GPUTexture : public NativeObject<GPUTexture> {
2222
static constexpr const char *CLASS_NAME = "GPUTexture";
2323

2424
explicit GPUTexture(wgpu::Texture instance, std::string label,
25-
bool ownsMemory = true)
25+
bool reportsMemoryPressure = true)
2626
: NativeObject(CLASS_NAME), _instance(instance), _label(label),
27-
_ownsMemory(ownsMemory) {}
27+
_reportsMemoryPressure(reportsMemoryPressure) {}
2828

2929
public:
3030
std::string getBrand() { return CLASS_NAME; }
@@ -70,15 +70,93 @@ class GPUTexture : public NativeObject<GPUTexture> {
7070
inline const wgpu::Texture get() { return _instance; }
7171

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

76156
private:
77-
size_t _computeMemoryPressure();
78-
79157
wgpu::Texture _instance;
80158
std::string _label;
81-
bool _ownsMemory = true;
159+
bool _reportsMemoryPressure = true;
82160
};
83161

84162
} // namespace rnwgpu

0 commit comments

Comments
 (0)