繁体   English   中英

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

[英]Antialiasing a SceneKit rendering with Metal

我是金属的新手。 我正在使用此 Apple 示例代码使用 Metal 渲染 SceneKit 场景。 TLDR; 它调用SCNRenderer's render函数并传入命令缓冲区。 我正在为 Big Sur 编译。

它有效,但没有抗锯齿。 我尝试了几种方法来实现它,正如您在下面的更新中看到的那样。

如果没有 Metal,我只需在isJitteringEnabled设置为true就可以得到漂亮(且缓慢)的 96-ish-pass 渲染。 如果我尝试用 Metal 来做这件事,我会得到奇怪的像素格式不匹配,所以我怀疑这两者不兼容。

使用 Metal,据我所知,实现抗锯齿的最简单方法是在渲染管道中启用多重采样(我知道如何做到这一点)——使用多重采样纹理( MTLTextureType.type2DMultisample )。 这个部分答案支持了我的假设。

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

我的完整来源在这里

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

(我认为这可能使用着色器实现。我也接受该解决方案,但我不知道从哪里开始。此示例无法编译,而此示例适用于 GSLS)


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

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

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

        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)")
        }

尝试:更改纹理描述符

由于我使用CVMetalTextureCacheCreateTextureFromImageCVPixelbuffer创建我的 Metal 纹理,我无法弄清楚如何设置它的属性并使其成为多样本。

尝试:尝试 H264

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

尝试:启用多重采样

我能够让我的管道接收到我想要的多重采样,但是由于没有为多重采样设置纹理(更准确地说是 .2DMultisample 类型的.2DMultisample文档),它崩溃了)

尝试:复制 Core Video 创建的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,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM