0
If I try anyting other than bgra8Unorm
, it will crash, saying,
-[MTLDebugRenderCommandEncoder validateFramebufferWithRenderPipelineState:]:1192: failed assertion `For color attachment 0, the render pipeline's pixelFormat (MTLPixelFormatBGRA8Unorm_sRGB) does not match the framebuffer's pixelFormat (MTLPixelFormatBGRA8Unorm).'
How do I change the framebuffer's pixelFormat then? I want to be able to do this:
PipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm_srgb
Instead of this
PipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
Again, this is an attempt to fix this stupid bug.
You need the MetalView defined here:
var MetalView: MTKView {
return view as! MTKView
}
Then in viewDidLoad:
MetalView.colorPixelFormat = .bgra8Unorm_srgb
Then I can do this:
PipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm_srgb
I figured it out myself yay!
textures:如何取消分配MTLBuffer和MTLTexture
-
MTLBuffer
和MTLTexture
是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]
...並通過GeForceMTLDriver
MetalTools`-[MTLToolsObject dealloc]:
0x100ac6c7a <+0>: push rbp
0x100ac6c7b <+1>: mov rbp, rsp
0x100ac6c7e <+4>: push r14
...
一路走来,通過各種
GeForceMTLDriver`___lldb_unnamed_symbol1095$$GeForceMTLDriver: -> 0x7fffd2e57b14 <+0>: push rbp 0x7fffd2e57b15 <+1>: mov rbp, rsp
釋放任何資源 方法。 - 2019-9-30
2 #如果您使用的是自動引用計數,則如公认的答案所述, 設置為零.
但是,如果您使用手動引用計數,則Metal具有两種動態分配的資源(MTLBuffer和&MTLTexture),您可以釋放 他们以下面的方式。
dealloc
關於setPurgeableState,您可以參考鏈接(https://developer.apple.com/documentation/metal/mtlresource/1515898-setpurgeablestate)
id<MTLDevice> m_Device = MTLCreateSystemDefaultDevice(); // allocate Buffer NSUInteger BufferLength = 100; id<MTLBuffer> m_Buffer = [m_Device newBufferWithLength:BufferLength options:0]; // allocate Texture MTLTextureDescriptor *shadowTextureDesc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat: MTLPixelFormatDepth32Float width: 1024 height: 1024 mipmapped: NO]; id<MTLTexture> m_Texture = [m_Device newTextureWithDescriptor:shadowTextureDesc]; // release Buffer [m_Buffer setPurgeableState:MTLPurgeableStateEmpty]; [m_Buffer release]; // release Texture [m_Texture setPurgeableState:MTLPurgeableStateEmpty]; [m_Texture release];