Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/webgpu/cpp/jsi/RNFHybridObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class HybridObject : public jsi::HostObject, public std::enable_shared_from_this
* Get the memory pressure of this HostObject in bytes.
* This is used to inform the JavaScript runtime about memory usage for garbage collection.
*/
virtual size_t getMemoryPressure() { return 1024; }
virtual size_t getMemoryPressure() = 0;

private:
static constexpr auto TAG = "HybridObject";
Expand Down
10 changes: 10 additions & 0 deletions packages/webgpu/cpp/jsi/RNFPointerHolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ namespace margelo {
namespace jsi = facebook::jsi;

template <typename T> class PointerHolder : public HybridObject {
public:
size_t getMemoryPressure() override {
std::unique_lock lock(_mutex);
if (_pointer == nullptr) {
return 0;
}
// Default to a small-but-nonzero floor so holders contribute to pressure.
return 64 * 1024; // 64KB
}

protected:
// no default constructor
PointerHolder() = delete;
Expand Down
2 changes: 2 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/Canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class Canvas : public m::HybridObject {
registerHybridSetter("height", &Canvas::setHeight, this);
}

size_t getMemoryPressure() override { return sizeof(Canvas); }

private:
void *_surface;
int _width;
Expand Down
5 changes: 5 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class GPU : public m::HybridObject {

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

size_t getMemoryPressure() override {
// Instance discovery spins up adapter caches and driver state.
return 512 * 1024; // 512KB baseline
}

private:
wgpu::Instance _instance;
std::shared_ptr<async::AsyncRunner> _async;
Expand Down
14 changes: 7 additions & 7 deletions packages/webgpu/cpp/rnwgpu/api/GPUAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ async::AsyncTaskHandle GPUAdapter::requestDevice(
auto creationRuntime = getCreationRuntime();
return _async->postTask(
[this, aDescriptor, descriptor, label = std::move(label),
deviceLostBinding, creationRuntime](
const async::AsyncTaskHandle::ResolveFunction &resolve,
const async::AsyncTaskHandle::RejectFunction &reject) {
deviceLostBinding,
creationRuntime](const async::AsyncTaskHandle::ResolveFunction &resolve,
const async::AsyncTaskHandle::RejectFunction &reject) {
(void)descriptor;
_instance.RequestDevice(
&aDescriptor, wgpu::CallbackMode::AllowProcessEvents,
[asyncRunner = _async, resolve, reject, label,
creationRuntime, deviceLostBinding](
wgpu::RequestDeviceStatus status, wgpu::Device device,
wgpu::StringView message) mutable {
[asyncRunner = _async, resolve, reject, label, creationRuntime,
deviceLostBinding](wgpu::RequestDeviceStatus status,
wgpu::Device device,
wgpu::StringView message) mutable {
if (message.length) {
fprintf(stderr, "%s", message.data);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPUAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class GPUAdapter : public m::HybridObject {

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

size_t getMemoryPressure() override { return 1024; }

private:
wgpu::Adapter _instance;
std::shared_ptr<async::AsyncRunner> _async;
Expand Down
6 changes: 6 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPUAdapterInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class GPUAdapterInfo : public m::HybridObject {
&GPUAdapterInfo::getIsFallbackAdapter, this);
}

size_t getMemoryPressure() override {
return sizeof(GPUAdapterInfo) + _vendor.capacity() +
_architecture.capacity() + _device.capacity() +
_description.capacity();
}

private:
std::string _vendor;
std::string _architecture;
Expand Down
27 changes: 27 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPUCanvasContext.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <algorithm>
#include <memory>
#include <string>
#include <utility>
Expand Down Expand Up @@ -46,6 +47,32 @@ class GPUCanvasContext : public m::HybridObject {
registerHybridMethod("present", &GPUCanvasContext::present, this);
}

size_t getMemoryPressure() override {
int width = 0;
int height = 0;

if (_surfaceInfo) {
auto size = _surfaceInfo->getSize();
width = size.width;
height = size.height;
}

if (_canvas) {
width = std::max(width, _canvas->getWidth());
height = std::max(height, _canvas->getHeight());
}

if (width <= 0 || height <= 0) {
return 4 * 1024 * 1024; // default to 4MB when size is unknown
}

constexpr size_t kBytesPerPixel = 4; // RGBA8 fallback
constexpr size_t kFloor = 4 * 1024 * 1024;
size_t estimated = static_cast<size_t>(width) *
static_cast<size_t>(height) * kBytesPerPixel;
return std::max(estimated, kFloor);
}

// TODO: is this ok?
inline const wgpu::Surface get() { return nullptr; }
void configure(std::shared_ptr<GPUCanvasConfiguration> configuration);
Expand Down
2 changes: 2 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPUCommandBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class GPUCommandBuffer : public m::HybridObject {

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

size_t getMemoryPressure() override { return 1024 * 1024; }

private:
wgpu::CommandBuffer _instance;
std::string _label;
Expand Down
2 changes: 2 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPUCommandEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class GPUCommandEncoder : public m::HybridObject {

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

size_t getMemoryPressure() override { return 1024 * 1024; }

private:
wgpu::CommandEncoder _instance;
std::string _label;
Expand Down
9 changes: 9 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPUCompilationInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class GPUCompilationInfo : public m::HybridObject {
registerHybridGetter("messages", &GPUCompilationInfo::getMessages, this);
}

size_t getMemoryPressure() override {
size_t total = sizeof(GPUCompilationInfo) +
_messages.capacity() * sizeof(GPUCompilationMessage);
for (const auto &message : _messages) {
total += message.message.capacity();
}
return total;
}

private:
std::vector<GPUCompilationMessage> _messages;
friend class GPUShaderModule;
Expand Down
4 changes: 4 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPUCompilationMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class GPUCompilationMessage : public m::HybridObject {

inline const wgpu::CompilationMessage get() { return _instance; }

size_t getMemoryPressure() override {
return sizeof(wgpu::CompilationMessage);
}

private:
wgpu::CompilationMessage _instance;
};
Expand Down
2 changes: 2 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPUComputePassEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class GPUComputePassEncoder : public m::HybridObject {

inline const wgpu::ComputePassEncoder get() { return _instance; }

size_t getMemoryPressure() override { return 1024; }

private:
wgpu::ComputePassEncoder _instance;
std::string _label;
Expand Down
7 changes: 3 additions & 4 deletions packages/webgpu/cpp/rnwgpu/api/GPUComputePipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ class GPUComputePipeline : public m::HybridObject {
inline const wgpu::ComputePipeline get() { return _instance; }

size_t getMemoryPressure() override {
// Compute pipelines contain compiled compute shader state and
// driver-specific optimized code
// Estimate: 16KB for a typical compute pipeline (single compute shader)
return 16 * 1024;
// Compute pipelines retain compiled shader state and backend caches.
// Overshoot intentionally to reflect the native resources they fan out to.
return 3 * 1024 * 1024; // 3MB
}

private:
Expand Down
6 changes: 6 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPUDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ class GPUDevice : public m::HybridObject {

inline const wgpu::Device get() { return _instance; }

size_t getMemoryPressure() override {
// Devices keep large driver heaps, pipeline caches and residency tracking.
constexpr size_t kDeviceFloor = 8 * 1024 * 1024; // 8MB baseline
return kDeviceFloor + _label.capacity();
}

private:
friend class GPUAdapter;

Expand Down
4 changes: 4 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPUDeviceLostInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class GPUDeviceLostInfo : public m::HybridObject {
registerHybridGetter("message", &GPUDeviceLostInfo::getMessage, this);
}

size_t getMemoryPressure() override {
return sizeof(wgpu::DeviceLostReason) + _message.capacity();
}

private:
wgpu::DeviceLostReason _reason;
std::string _message;
Expand Down
6 changes: 6 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPUExternalTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class GPUExternalTexture : public m::HybridObject {

inline const wgpu::ExternalTexture get() { return _instance; }

size_t getMemoryPressure() override {
// External textures usually wrap decoder/camera surfaces (multi-MB frames).
constexpr size_t kExternalTextureFloor = 8 * 1024 * 1024; // 8MB baseline
return kExternalTextureFloor + _label.capacity();
}

private:
wgpu::ExternalTexture _instance;
std::string _label;
Expand Down
2 changes: 2 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPUPipelineLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class GPUPipelineLayout : public m::HybridObject {

inline const wgpu::PipelineLayout get() { return _instance; }

size_t getMemoryPressure() override { return 1024; }

private:
wgpu::PipelineLayout _instance;
std::string _label;
Expand Down
17 changes: 10 additions & 7 deletions packages/webgpu/cpp/rnwgpu/api/GPUQuerySet.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,23 @@ class GPUQuerySet : public m::HybridObject {
inline const wgpu::QuerySet get() { return _instance; }

size_t getMemoryPressure() override {
uint32_t count = getCount();
wgpu::QueryType type = getType();
const uint32_t count = getCount();
const wgpu::QueryType type = getType();

// Estimate bytes per query based on type
size_t bytesPerQuery = 8; // Default estimate
size_t bytesPerQuery = 16; // default to an overshoot
switch (type) {
case wgpu::QueryType::Occlusion:
bytesPerQuery = 8; // 64-bit counter
bytesPerQuery = 16; // occlusion result is 64-bit; pad to 16 for safety
break;
case wgpu::QueryType::Timestamp:
bytesPerQuery = 8; // 64-bit timestamp
bytesPerQuery = 16; // timestamps are 64-bit; double to overshoot
break;
case wgpu::QueryType::PipelineStatistics: {
constexpr size_t kAssumedCountersPerQuery = 8;
bytesPerQuery = 8 * kAssumedCountersPerQuery;
break;
}
default:
bytesPerQuery = 8; // Safe default
break;
}

Expand Down
6 changes: 6 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPUQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class GPUQueue : public m::HybridObject {

inline const wgpu::Queue get() { return _instance; }

size_t getMemoryPressure() override {
// Queues retain submitted command buffers and backend scheduling state.
constexpr size_t kQueueFloor = 1 * 1024 * 1024; // 1MB baseline
return kQueueFloor + _label.capacity();
}

private:
wgpu::Queue _instance;
std::shared_ptr<async::AsyncRunner> _async;
Expand Down
2 changes: 2 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPURenderBundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class GPURenderBundle : public m::HybridObject {

inline const wgpu::RenderBundle get() { return _instance; }

size_t getMemoryPressure() override { return 1024 * 1024; }

private:
wgpu::RenderBundle _instance;
std::string _label;
Expand Down
2 changes: 2 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPURenderBundleEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class GPURenderBundleEncoder : public m::HybridObject {

inline const wgpu::RenderBundleEncoder get() { return _instance; }

size_t getMemoryPressure() override { return 1024 * 1024; }

private:
wgpu::RenderBundleEncoder _instance;
std::string _label;
Expand Down
2 changes: 2 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPURenderPassEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ class GPURenderPassEncoder : public m::HybridObject {

inline const wgpu::RenderPassEncoder get() { return _instance; }

size_t getMemoryPressure() override { return 1024 * 1024; }

private:
wgpu::RenderPassEncoder _instance;
std::string _label;
Expand Down
9 changes: 4 additions & 5 deletions packages/webgpu/cpp/rnwgpu/api/GPURenderPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ class GPURenderPipeline : public m::HybridObject {
inline const wgpu::RenderPipeline get() { return _instance; }

size_t getMemoryPressure() override {
// Render pipelines contain compiled shader state, vertex/fragment shaders,
// render state, and driver-specific optimized code
// Estimate: 24KB for a typical render pipeline with vertex + fragment
// shaders
return 24 * 1024;
// Render pipelines combine multiple compiled stages plus fixed-function
// state baked by the driver. Reserve several megabytes to signal that to
// the GC.
return 3 * 1024 * 1024; // 3MB
}

private:
Expand Down
2 changes: 2 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPUSampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class GPUSampler : public m::HybridObject {

inline const wgpu::Sampler get() { return _instance; }

size_t getMemoryPressure() override { return 1024; }

private:
wgpu::Sampler _instance;
std::string _label;
Expand Down
8 changes: 3 additions & 5 deletions packages/webgpu/cpp/rnwgpu/api/GPUShaderModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ class GPUShaderModule : public m::HybridObject {
inline const wgpu::ShaderModule get() { return _instance; }

size_t getMemoryPressure() override {
// Estimate memory usage for compiled shader module
// Shaders can vary widely, but a reasonable estimate is 8-16KB for typical
// shaders Complex shaders (with many uniforms, textures, or computations)
// can be much larger
return 12 * 1024; // 12KB estimate for average shader
// Shader modules can fan out into compiled IR, reflection data and backend
// caches. Report a conservative 1MB to reflect that cost.
return 1 * 1024 * 1024; // 1MB
}

private:
Expand Down
2 changes: 2 additions & 0 deletions packages/webgpu/cpp/rnwgpu/api/GPUSupportedLimits.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ class GPUSupportedLimits : public m::HybridObject {

inline const wgpu::Limits get() { return _instance; }

size_t getMemoryPressure() override { return sizeof(wgpu::Limits); }

private:
wgpu::Limits _instance;
};
Expand Down
Loading
Loading