RealityKit crashes when rendering SpriteKit scene with SKShapeNode in postProcess callback

I'm converting my game from SceneKit to RealityKit. It has a SpriteKit overlay that according to Explore advanced rendering with RealityKit 2 I can add with the code below.

The code runs fine if the SKScene only contains a SKSpriteNode (see the commented out line), but when I add a SKShapeNode with a fillColor instead, the app crashes with this error:

-[MTLDebugRenderCommandEncoder validateCommonDrawErrors:]:5970: failed assertion `Draw Errors Validation
MTLDepthStencilDescriptor uses frontFaceStencil but MTLRenderPassDescriptor has a nil stencilAttachment texture
MTLDepthStencilDescriptor uses backFaceStencil but MTLRenderPassDescriptor has a nil stencilAttachment texture
'

I don't know enough about low-level graphics and stencils yet to figure out a quick solution, so I would appreciate if anyone could share an easy fix or explanation of what's wrong. Thanks!

class ViewController: NSViewController {
    
    var device: MTLDevice!
    var renderer: SKRenderer!
    
    override func loadView() {
        let arView = ARView(frame: NSScreen.main!.frame)
        view = arView
        
        arView.renderCallbacks.prepareWithDevice = { [weak self] device in
            guard let self = self else { return }
            self.device = device
            renderer = SKRenderer(device: MTLCreateSystemDefaultDevice()!)
            let scene = SKScene()
            let shape = SKShapeNode(rectOf: CGSize(width: 10, height: 10))
            shape.fillColor = .red
            scene.addChild(shape)
//            scene.addChild(SKSpriteNode(color: .red, size: CGSize(width: 10, height: 10)))
            renderer.scene = scene
        }
        arView.renderCallbacks.postProcess = { [weak self] context in
            guard let self = self else { return }
            let encoder = context.commandBuffer.makeBlitCommandEncoder()
            encoder?.copy(from: context.sourceColorTexture, to: context.targetColorTexture)
            encoder?.endEncoding()
            renderer.update(atTime: context.time)
            let descriptor = MTLRenderPassDescriptor()
            descriptor.colorAttachments[0].loadAction = .load
            descriptor.colorAttachments[0].storeAction = .store
            descriptor.colorAttachments[0].texture = context.targetColorTexture
            renderer.render(withViewport: CGRect(x: 0, y: 0, width: context.targetColorTexture.width, height: context.targetColorTexture.height), commandBuffer: context.commandBuffer, renderPassDescriptor: descriptor)
        }
    }
    
}

This reminds of an error I got while setting up SKRenderer for this project: SKRenderer Demo.

See code and comment in SKOfflineRenderer.swift:

// If I dont use a depth/stencil texture, rendering crashes on simulator, device, and Mac
// Without it, rendering only works on Xcode Live Preview
let depthStencilDesc = MTLTextureDescriptor()
depthStencilDesc.pixelFormat = .depth32Float_stencil8
depthStencilDesc.width = pixelWidth
depthStencilDesc.height = pixelHeight
depthStencilDesc.usage = .renderTarget
depthStencilDesc.storageMode = .private

The code is inside the init, check how the depthStencilTexture is setup for SKRenderer, it may help you.

Thanks for your input. I added the following code in the arView.renderCallbacks.postProcess callback:

let depthStencilDescriptor = MTLTextureDescriptor()
depthStencilDescriptor.pixelFormat = .depth32Float_stencil8
depthStencilDescriptor.width = context.targetColorTexture.width
depthStencilDescriptor.height = context.targetColorTexture.height
depthStencilDescriptor.usage = .renderTarget
depthStencilDescriptor.storageMode = .private
renderPassDescriptor.stencilAttachment.texture = device!.makeTexture(descriptor: depthStencilDescriptor)
renderPassDescriptor.stencilAttachment.loadAction = .clear
renderPassDescriptor.stencilAttachment.storeAction = .dontCare
renderPassDescriptor.stencilAttachment.clearStencil = 0

but the app now crashes with error

Assertion failed: (destDepthFormat == jet_texture_format_DepthStencil), function create_render_mode, file jet_context_Metal.mm, line 895.
VTPixelTransferSession  420f sid 494 (512.00 x 512.00) [0.00 0.00 512 512] rowbytes( 512, 512 ) Color( kCGColorSpaceSRGB, 0x0, (null), (null), ITU_R_601_4 ) => BGRA sid 554 (512.00 x 512.00) [0.00 0.00 512 512] rowbytes( 2048 ) Color( 0x0, (null), (null), (null) )

which again I have no idea what it means.

RealityKit crashes when rendering SpriteKit scene with SKShapeNode in postProcess callback
 
 
Q