@@ -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
2929public:
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
76156private:
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