使用 Metal 对 SceneKit 渲染进行抗锯齿处理

2024-01-03

我是金属新手。我正在使用 Metal 渲染 SceneKit 场景这个苹果示例代码 https://developer.apple.com/documentation/avfoundation/media_playback_and_selection/using_hevc_video_with_alpha。太长了;它称为SCNRenderer's render函数并传入命令缓冲区。我正在为大苏尔编译。

它可以工作,但不能消除锯齿。我尝试了几种方法来实现它,正如您在下面的更新中看到的那样。

没有金属,我只需设置isJitteringEnabled to true在 SCNRenderer 上,我得到了漂亮(且缓慢)的 96 遍渲染。如果我尝试使用 Metal 执行此操作,则会出现奇怪的像素格式不匹配的情况,因此我怀疑两者不兼容。

据我所知,对于 Metal,实现抗锯齿的最简单方法是在渲染管道中启用多重采样(我知道该怎么做) -and使用多重采样纹理(MTLTextureType.type2DMultisample). 这部分答案 https://developer.apple.com/forums/thread/667558?answerId=648944022#648944022支持我的假设。

这就是问题所在。当我从以下位置获取纹理时,我不知道如何更改纹理类型CVMetalTextureCache and CVMetalTextureCacheCreateTextureFromImage。这似乎是 Core Video 的 Metal 支持的限制?

My full 来源在这里 https://github.com/mortenjust/hevc-with-alpha/blob/main/HEVC-Videos-With-Alpha-AssetWriting/HEVC-Videos-With-Alpha-AssetWriting/AppDelegate.swift#L47

就是这样。这篇文章的其余部分是关于我尝试过的东西的更多细节。

(我认为使用着色器可能可以实现这一点。我也愿意接受该解决方案,但我不知道从哪里开始。这个例子 https://metashapes.com/blog/anti-aliasing-shaders/不编译,并且这个例子 https://developer.apple.com/forums/thread/25443适用于 GSLS)


我的像素缓冲区 atts 看起来像这样

        let pixelbufferAttributes = [
            kCVPixelBufferPixelFormatTypeKey : kCVPixelFormatType_32BGRA,
            kCVPixelBufferWidthKey: exportSettings.width,
            kCVPixelBufferHeightKey : exportSettings.height,
        kCVPixelBufferMetalCompatibilityKey: true] as [String: Any]

对于每一帧,它从池中创建一个新的像素缓冲区,将其包装在缓存中的金属纹理中,如下所示

        let pixelFormat = MTLPixelFormat.bgra8Unorm_srgb
        var optionalMetalTexture: CVMetalTexture?
        err = CVMetalTextureCacheCreateTextureFromImage(
            kCFAllocatorDefault,
            metalTextureCache, // object prop
            pixelBuffer,
            nil, // texture attributes
            pixelFormat,
            exportSettings.width,
            exportSettings.height,
            0, // planeIndex
            &optionalMetalTexture)
        guard err == noErr, let metalTexture = optionalMetalTexture else {
            fatalError("Failed to create metal texture wrapper from pixel bufffer \(err)")
        }

尝试:更改纹理描述符

因为我是从创建我的金属纹理CVPixelbuffer with CVMetalTextureCacheCreateTextureFromImage,我不知道如何设置它的属性并使其成为多样本。

尝试:尝试H264

没有改变任何东西。还尝试仅更改 Alpha 质量,使用带有 Alpha 的 HEVC,但没有任何变化。

尝试:启用多重采样

我能够让我的管道识别出我想要多重采样,但由于没有为多重采样设置纹理(更准确地说是 MTLTexture 类型),它崩溃了.2DMultisample (docs https://developer.apple.com/documentation/metal/mtltexturetype/type2dmultisample)

尝试:复制MTLTexture由核心视频创建

我尝试使用MTLBlitCommandEncoder将 Core Video 提供的纹理复制到我使用正确属性设置的纹理中。但它崩溃告诉我属性不匹配。

我开始觉得这个问题没有解决办法吗?


启用多重采样是正确的想法。 以下补丁显示了如何启用它。

--- a/HEVC-Videos-With-Alpha-AssetWriting/HEVC-Videos-With-Alpha-AssetWriting/AppDelegate.swift
+++ b/HEVC-Videos-With-Alpha-AssetWriting/HEVC-Videos-With-Alpha-AssetWriting/AppDelegate.swift
@@ -32,6 +32,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, SCNSceneRendererDelegate {
     let renderer = SCNRenderer(device: nil, options: nil)
     var lampMaterials: SCNNode!
     var metalTextureCache: CVMetalTextureCache!
+    let msaaSampleCount = 1
+    var metalMultisampledTexture: MTLTexture!
     
     // Export
     var frameCounter = 0
@@ -61,6 +63,18 @@ class AppDelegate: NSObject, NSApplicationDelegate, SCNSceneRendererDelegate {
             fatalError("Cannot create metal texture cache: \(err)")
         }
         metalTextureCache = optionalMetalTextureCache
+        
+        if (msaaSampleCount > 1) {
+            let textureDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: MTLPixelFormat.bgra8Unorm_srgb,
+                                                                             width: ExportSettings.width,
+                                                                             height: ExportSettings.height,
+                                                                             mipmapped: false)
+            textureDescriptor.usage = .renderTarget
+            textureDescriptor.storageMode = .private
+            textureDescriptor.textureType = .type2DMultisample
+            textureDescriptor.sampleCount = msaaSampleCount
+            metalMultisampledTexture = renderer.device!.makeTexture(descriptor: textureDescriptor)
+        }
     }
     
     /// Render next frame and call the frame completion handler
@@ -106,7 +120,14 @@ class AppDelegate: NSObject, NSApplicationDelegate, SCNSceneRendererDelegate {
         let renderPassDescriptor = MTLRenderPassDescriptor()
         renderPassDescriptor.colorAttachments[0].loadAction = .clear
         renderPassDescriptor.colorAttachments[0].clearColor = clearColor
-        renderPassDescriptor.colorAttachments[0].texture = CVMetalTextureGetTexture(metalTexture)
+        if (msaaSampleCount > 1) {
+            renderPassDescriptor.colorAttachments[0].texture = metalMultisampledTexture
+            renderPassDescriptor.colorAttachments[0].resolveTexture = CVMetalTextureGetTexture(metalTexture)
+            renderPassDescriptor.colorAttachments[0].storeAction = .multisampleResolve
+        }
+        else {
+            renderPassDescriptor.colorAttachments[0].texture = CVMetalTextureGetTexture(metalTexture)
+        }
         renderer.render(atTime: currentPresentationTime.seconds,
                         viewport: ExportSettings.viewport,
                         commandBuffer: commandBuffer,

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Metal 对 SceneKit 渲染进行抗锯齿处理 的相关文章

随机推荐