How do I change the pixelFormat in Metal?

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,只需確保不再儲存對緩衝區或纹理的引用將釋放任何關聯的硬體資源。

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

    可以在分配 nil時逐步執行相關程式集来確认這一點   到維兹維兹 ,這首先將我们引向 texture

     
      
    1. [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

     
      
    1. [MTLToolsObject dealloc]

    ...並通過GeForceMTLDriver

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

    一路走来,通過各種 GeForceMTLDriver`___lldb_unnamed_symbol1095$$GeForceMTLDriver: -> 0x7fffd2e57b14 <+0>: push rbp 0x7fffd2e57b15 <+1>: mov rbp, rsp釋放任何資源   方法。

  • 2019-9-30
    2 #

    如果您使用的是自動引用計數,則如公认的答案所述,   設置為零.

    但是,如果您使用手動引用計數,則Metal具有两種動態分配的資源(MTLBuffer和&MTLTexture),您可以釋放 他们以下面的方式。

     
      
    1. 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];

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值