textures - 如何释放 MTLBuffer 和 MTLTexture

标签 textures metal vertex-buffer

我知道如何创建 MTLBuffer 和/或 MTLTexture,但是当不再需要这些资源时如何释放 GPU 内存?

最佳答案

MTLBufferMTLTexture 是 Objective-C 对象,因此可以进行引用计数。如果您在 Objective-C 项目中使用自动引用计数或通过 Swift 使用 Metal,只需确保不再保留对缓冲区或纹理的引用即可释放任何关联的硬件资源。

let texture: MTLTexture? = device.newTexture(with: descriptor)
texture = nil // <- resources will be released

在将 nil 分配给 texture 时,可以通过单步执行关联的程序集来确认这一点,这首先引导我们到 [MTLDebugTexture dealloc]

MetalTools`-[MTLDebugTexture dealloc]:
    ...
->  0x100af569e <+34>: call   0x100af87ee               ; symbol stub for: objc_msgSendSuper2
    0x100af56a3 <+39>: add    rsp, 0x10
    0x100af56a7 <+43>: pop    rbp
    0x100af56a8 <+44>: ret    

...并通过[MTLToolsObject dealloc]

MetalTools`-[MTLToolsObject dealloc]:
    0x100ac6c7a <+0>:   push   rbp
    0x100ac6c7b <+1>:   mov    rbp, rsp
    0x100ac6c7e <+4>:   push   r14
    ...

...并通过 GeForceMTLDriver

GeForceMTLDriver`___lldb_unnamed_symbol1095$$GeForceMTLDriver:
->  0x7fffd2e57b14 <+0>:  push   rbp
    0x7fffd2e57b15 <+1>:  mov    rbp, rsp

一路上,通过各种dealloc方法释放任何资源。

关于textures - 如何释放 MTLBuffer 和 MTLTexture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39158302/

相关文章:

directx - 更新 directX 纹理

c++ - DirectX 渲染到纹理

c++ - 在 OGRE 中创建动态纹理时获取错误的 RGB

c - 为什么我的 openGL 纹理只覆盖了我的四边形的一半?包含来源

ios - 从 AVFoundation 相机流生成 mipmaped MTLTextures

Alpha/透明度和 MTKView?

macos - 如何跨进程同步 MTLTexture 和 IOSurface?

c++ - 具有交错法线的 VertexBufferObject

opengl - 经典的 "nothing is getting rendered"OpenGL 问题

使用布局限定符时未缓冲顶点属性的 OpenGL 默认值