@@ -21,8 +21,10 @@ class GPUTexture : public NativeObject<GPUTexture> {
2121public:
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
2729public:
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
14576private:
77+ size_t _computeMemoryPressure ();
78+
14679 wgpu::Texture _instance;
14780 std::string _label;
81+ bool _ownsMemory = true ;
14882};
14983
15084} // namespace rnwgpu
0 commit comments