MTKViewのcurrentRenderPassDescriptor

MTLRenderPassDescriptor、普通はこういうことをやる:

let renderPassDescriptor = MTLRenderPassDescriptor()
renderPassDescriptor.colorAttachments[0].texture = texture
renderPassDescriptor.colorAttachments[0].loadAction = .clear
renderPassDescriptor.colorAttachments[0].storeAction = .store
let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: passDescriptor)!

しかしMTKViewのcurrentRenderPassDescriptorを使えば、描画先テクスチャがMTKViewのcurrentDrawable.textureである限りは、colorAttachments[0].textureへのセットを省略できる。

let renderPassDescriptor = renderDestination.currentRenderPassDescriptor!
let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor)!

で、正直なことをいうとこのことを知らずになんとなく使っていた。(ついさっきARKitのMetalレンダリングテンプレートのコードを読んでいて気付いた)

で、改めてちゃんとリファレンスを読んでみる。

リファレンスに書いてあったこと

A render pass descriptor generated from the current drawable’s texture, the view’s buffers, and the view’s clear values.

冒頭にいきなり"generated from the current drawable’s texture"って書いてあった。。ちゃんとリファレンス読もう。

で、他にも、"the view’s buffers", "the view’s clear values"とあるので、このへんがデフォルトでセットされているdescriptorのようだ。

以下Discussionパートからのメモ

there is no requirement for your application to use this descriptor, either.

使わなくてもOK

This property is nil if the view’s device is not set. 
This property is also nil if currentDrawable is nil.

このプロパティはdeviceがセットされてないとき、またはcurrentDrawableがnilのときにnilになる。

The color attachment at index 0 of the render pass descriptor points to the texture assigned to the current drawable.

上述した通り、colorAttachments[0].textureにあらかじめcurrent drawableがセットされているという話。

printしてみる

printしたところ、こういう出力になった:

<MTLRenderPassDescriptorInternal: 0x2828998c0>
    Color Attachment 0 
        texture = <AGXA12FamilyTexture: 0x101908400>
            label = Drawable 
            textureType = MTLTextureType2D 
            pixelFormat = MTLPixelFormatBGRA8Unorm 
            width = 1125 
            height = 2436 
            depth = 1 
            arrayLength = 1 
            mipmapLevelCount = 1 
            sampleCount = 1 
            cpuCacheMode = MTLCPUCacheModeDefaultCache 
            storageMode = MTLStorageModeShared 
            resourceOptions = MTLResourceCPUCacheModeDefaultCache MTLResourceStorageModeShared  
            usage = MTLTextureUsageShaderRead MTLTextureUsageShaderWrite MTLTextureUsageRenderTarget MTLTextureUsagePixelFormatView  
            framebufferOnly = 1 
            purgeableState = MTLPurgeableStateNonVolatile 
            swizzle = [MTLTextureSwizzleRed, MTLTextureSwizzleGreen, MTLTextureSwizzleBlue, MTLTextureSwizzleAlpha] 
            isCompressed = 1 
            parentTexture = <null> 
            parentRelativeLevel = 0 
            parentRelativeSlice = 0 
            buffer = <null> 
            bufferOffset = 0 
            bufferBytesPerRow = 0 
            allowGPUOptimizedContents = YES
            label = Drawable 
        level = 0 
        slice = 0 
        depthPlane = 0 
        resolveTexture = <null> 
        resolveLevel = 0 
        resolveSlice = 0 
        resolveDepthPlane = 0 
        loadAction = MTLLoadActionClear 
        storeAction = MTLStoreActionStore 
        storeActionOptions = none 
        clearColor = (0 0 0 1) 
        yInvert = NO 
    Color Attachment 1 
        texture = <null> 
        // 以下略 
    Color Attachment 2 
        texture = <null> 
        // 以下略 
    Color Attachment 3 
        texture = <null> 
        // 以下略 
    Color Attachment 4 
        texture = <null> 
        // 以下略 
    Color Attachment 5 
        texture = <null> 
        // 以下略 
    Color Attachment 6 
        texture = <null> 
        // 以下略 
    Color Attachment 7 
        texture = <null> 
        // 以下略 
    Depth Attachment: 
        texture = <AGXA12FamilyTexture: 0x101925560>
    label = MTKView Depth Stencil 
    textureType = MTLTextureType2D 
    pixelFormat = MTLPixelFormatDepth32Float_Stencil8 
    width = 1125 
    height = 2436 
    depth = 1 
    arrayLength = 1 
    mipmapLevelCount = 1 
    sampleCount = 1 
    cpuCacheMode = MTLCPUCacheModeDefaultCache 
    storageMode = MTLStorageModePrivate 
    resourceOptions = MTLResourceCPUCacheModeDefaultCache MTLResourceStorageModePrivate  
    usage = MTLTextureUsageShaderRead MTLTextureUsageShaderWrite MTLTextureUsageRenderTarget MTLTextureUsagePixelFormatView  
    framebufferOnly = 0 
    purgeableState = MTLPurgeableStateNonVolatile 
    swizzle = [MTLTextureSwizzleRed, MTLTextureSwizzleGreen, MTLTextureSwizzleBlue, MTLTextureSwizzleAlpha] 
    isCompressed = 0 
    parentTexture = <null> 
    parentRelativeLevel = 0 
    parentRelativeSlice = 0 
    buffer = <null> 
    bufferOffset = 0 
    bufferBytesPerRow = 0 
    allowGPUOptimizedContents = YES
    label = MTKView Depth Stencil 
        level = 0 
        slice = 0 
        depthPlane = 0 
        resolveTexture = <null> 
        resolveLevel = 0 
        resolveSlice = 0 
        resolveDepthPlane = 0 
        resolveFilter = MTLMultisampleDepthResolveFilterSample0 
        loadAction = MTLLoadActionClear 
        storeAction = MTLStoreActionDontCare 
        storeActionOptions = none 
        clearDepth = 1 
        yInvert = NO 
    Stencil Attachment: 
        texture = <AGXA12FamilyTexture: 0x101925560>
    label = MTKView Depth Stencil 
    textureType = MTLTextureType2D 
    pixelFormat = MTLPixelFormatDepth32Float_Stencil8 
    width = 1125 
    height = 2436 
    depth = 1 
    arrayLength = 1 
    mipmapLevelCount = 1 
    sampleCount = 1 
    cpuCacheMode = MTLCPUCacheModeDefaultCache 
    storageMode = MTLStorageModePrivate 
    resourceOptions = MTLResourceCPUCacheModeDefaultCache MTLResourceStorageModePrivate  
    usage = MTLTextureUsageShaderRead MTLTextureUsageShaderWrite MTLTextureUsageRenderTarget MTLTextureUsagePixelFormatView  
    framebufferOnly = 0 
    purgeableState = MTLPurgeableStateNonVolatile 
    swizzle = [MTLTextureSwizzleRed, MTLTextureSwizzleGreen, MTLTextureSwizzleBlue, MTLTextureSwizzleAlpha] 
    isCompressed = 0 
    parentTexture = <null> 
    parentRelativeLevel = 0 
    parentRelativeSlice = 0 
    buffer = <null> 
    bufferOffset = 0 
    bufferBytesPerRow = 0 
    allowGPUOptimizedContents = YES
    label = MTKView Depth Stencil 
        level = 0 
        slice = 0 
        depthPlane = 0 
        resolveTexture = <null> 
        resolveLevel = 0 
        resolveSlice = 0 
        resolveDepthPlane = 0 
        loadAction = MTLLoadActionClear 
        storeAction = MTLStoreActionDontCare 
        storeActionOptions = none 
        clearStencil = 0 
        stencilResolveFilter = MTLMultisampleStencilResolveFilterSample0 
        yInvert = NO 
    visibilityResultBuffer = <null> 
    renderTargetArrayLength = 0 
   Custom Sample Positions:
       number of positions = 0

Color Attachment 0のところに諸々セットされていること、そしてDepth AttachmentやStencil Attachmentにも既にMTLTextureオブジェクトがセットされていることが確認できる。

ちなみにMTKViewのcurrentRenderPassDescriptorではなく、普通に初期化しただけのものを出力してみると、

let passDescriptor = MTLRenderPassDescriptor()
print("\(passDescriptor)")

こうなった:

render pass descriptor: <MTLRenderPassDescriptorInternal: 0x283866700>
    Color Attachment 0 
        texture = <null> 
        level = 0 
        slice = 0 
        depthPlane = 0 
        resolveTexture = <null> 
        resolveLevel = 0 
        resolveSlice = 0 
        resolveDepthPlane = 0 
        loadAction = MTLLoadActionDontCare 
        storeAction = MTLStoreActionStore 
        storeActionOptions = none 
        clearColor = (0 0 0 1) 
        yInvert = NO 
    Color Attachment 1 
        texture = <null> 
        // 以下略 
    Color Attachment 2 
        texture = <null> 
        // 以下略 
    Color Attachment 3 
        texture = <null> 
        // 以下略 
    Color Attachment 4 
        texture = <null> 
        // 以下略 
    Color Attachment 5 
        texture = <null> 
        // 以下略 
    Color Attachment 6 
        texture = <null> 
        // 以下略 
    Color Attachment 7 
        texture = <null> 
        // 以下略 
    Depth Attachment: 
        texture = <null> 
        level = 0 
        slice = 0 
        depthPlane = 0 
        resolveTexture = <null> 
        resolveLevel = 0 
        resolveSlice = 0 
        resolveDepthPlane = 0 
        resolveFilter = MTLMultisampleDepthResolveFilterSample0 
        loadAction = MTLLoadActionClear 
        storeAction = MTLStoreActionDontCare 
        storeActionOptions = none 
        clearDepth = 1 
        yInvert = NO 
    Stencil Attachment: 
        texture = <null> 
        level = 0 
        slice = 0 
        depthPlane = 0 
        resolveTexture = <null> 
        resolveLevel = 0 
        resolveSlice = 0 
        resolveDepthPlane = 0 
        loadAction = MTLLoadActionClear 
        storeAction = MTLStoreActionDontCare 
        storeActionOptions = none 
        clearStencil = 0 
        stencilResolveFilter = MTLMultisampleStencilResolveFilterSample0 
        yInvert = NO 
    visibilityResultBuffer = <null> 
    renderTargetArrayLength = 0 
   Custom Sample Positions:
       number of positions = 0

Color Attachment 0, Depth Attachment, Stencil Attachmentがセットされていない。

というわけで、currentRenderPassDescriptorの使いどころがわかって有意義だった。

(定期購読マガジンに追加する関係で有料設定してますが、この続きはありません。)

この続きをみるには

この続き:0文字
記事を購入する

MTKViewのcurrentRenderPassDescriptor

shu223

100円

この記事が気に入ったら、サポートをしてみませんか?気軽にクリエイターを支援できます。

2

shu223

フリーランスiOSエンジニア兼サンフランシスコのFyusion社勤務。 著書に「iOS×BLE Core Bluetooth プログラミング」「iOSアプリ開発 達人のレシピ100」。GitHubで「iOS Sampler」シリーズ他さまざまなオープンソースを公開している。

日々の学びメモ

技術的なメモ、思いついたアイデア、考えたこと、お金の話等々、頭をよぎった諸々を気軽に垂れ流していきたいと思います。
コメントを投稿するには、 ログイン または 会員登録 をする必要があります。