gpt4 book ai didi

swift - 如何将 SCNRenderer 与现有的 MTLCommandBuffer 结合使用?

转载 作者:行者123 更新时间:2023-12-03 09:22:29 30 4
gpt4 key购买 nike

我成功集成了Vuforia SDK Image Target Tracking feature通过将 SDK 提供的 OpenGL 上下文 ( EAGLContext ) 与 SceneKit 的 SCNRenderer 实例结合到 iOS 项目中.这使我能够利用 SceneKit 的 3D API 的简单性,同时受益于 Vuforia 的高精度图像检测。现在,我想通过用 Metal 替换 OpenGL 来做同样的事情。

一些背景故事

我能够使用 在 Vuforia 绘制的实时视频纹理之上绘制 SceneKit 对象。 OpenGL 没有大问题。

这是我使用的简化设置 OpenGL :

func configureRenderer(for context: EAGLContext) {
self.renderer = SCNRenderer(context: context, options: nil)
self.scene = SCNScene()
renderer.scene = scene

// other scenekit setup
}

func render() {
// manipulate scenekit nodes

renderer.render(atTime: CFAbsoluteTimeGetCurrent())
}

Apple 在 iOS 12 上弃用 OpenGL

Apple announced that it is deprecating OpenGL on iOS 12 ,我认为尝试迁移此项目以使用 Metal 是个好主意。而不是 OpenGL .

这在理论上应该很简单,因为 Vuforia 支持 Metal 开箱即用。然而,当我试图整合它时,我碰壁了。

问题

该 View 似乎只渲染 SceneKit 渲染器的结果或 Vuforia 编码的纹理,但从来不会同时渲染两者。这取决于首先编码的内容。我该怎么做才能将两个结果混合在一起?

简而言之,这是有问题的设置:
func configureRenderer(for device: MTLDevice) {
let renderer = SCNRenderer(device: device, options: nil)
self.scene = SCNScene()
renderer.scene = scene

// other scenekit setup
}

func render(viewport: CGRect, commandBuffer: MTLCommandBuffer, drawable: CAMetalDrawable) {
// manipulate scenekit nodes

let renderPassDescriptor = MTLRenderPassDescriptor()
renderPassDescriptor.colorAttachments[0].texture = drawable.texture
renderPassDescriptor.colorAttachments[0].loadAction = .load
renderPassDescriptor.colorAttachments[0].storeAction = .store
renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColor(red: 0.0, green: 0, blue: 0, alpha: 0)

renderer!.render(withViewport: viewport, commandBuffer: commandBuffer, passDescriptor: renderPassDescriptor)
}

我试着打电话 render要么在 encoder.endEncoding之后或之前 commandBuffer.renderCommandEncoderWithDescriptor :
metalDevice = MTLCreateSystemDefaultDevice();
metalCommandQueue = [metalDevice newCommandQueue];
id<MTLCommandBuffer>commandBuffer = [metalCommandQueue commandBuffer];

//// -----> call the `render(viewport:commandBuffer:drawable) here <------- \\\\

id<MTLRenderCommandEncoder> encoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];

// calls to encoder to render textures from Vuforia

[encoder endEncoding];

//// -----> or here <------- \\\\

[commandBuffer presentDrawable:drawable];
[commandBuffer commit];

无论哪种情况,我都只看到 SCNRenderer 的结果 encoder的结果,但永远不会在同一个 View 中。

在我看来好像编码通过了上面,而 SCNRenderer.render , 正在覆盖彼此的缓冲区。

我在这里缺少什么?

最佳答案

我想我已经找到了答案。
我正在渲染 scnrenderer 结束编码 ,但我正在创建一个新的描述符。

    // Pass Metal context data to Vuforia Engine (we may have changed the encoder since
// calling Vuforia::Renderer::begin)
finishRender(UnsafeMutableRawPointer(Unmanaged.passRetained(drawable!.texture).toOpaque()), UnsafeMutableRawPointer(Unmanaged.passRetained(encoder!).toOpaque()))

// ========== Finish Metal rendering ==========
encoder?.endEncoding()

// Commit the rendering commands
// Command completed handler
commandBuffer?.addCompletedHandler { _ in self.mCommandExecutingSemaphore.signal()}
let screenSize = UIScreen.main.bounds.size
let newDescriptor = MTLRenderPassDescriptor()

// Draw to the drawable's texture
newDescriptor.colorAttachments[0].texture = drawable?.texture

// Store the data in the texture when rendering is complete
newDescriptor.colorAttachments[0].storeAction = MTLStoreAction.store
// Use textureDepth for depth operations.
newDescriptor.depthAttachment.texture = mDepthTexture;
renderer?.render(atTime: 0, viewport: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height), commandBuffer: commandBuffer!, passDescriptor: newDescriptor)

// Present the drawable when the command buffer has been executed (Metal
// calls to CoreAnimation to tell it to put the texture on the display when
// the rendering is complete)
commandBuffer?.present(drawable!)

// Commit the command buffer for execution as soon as possible
commandBuffer?.commit()

关于swift - 如何将 SCNRenderer 与现有的 MTLCommandBuffer 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52026883/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com