使用图层支持的 NSOpenGLView 会大幅减慢速度
我需要在现有应用程序中的 NSOpenGLView 之上显示一些 Cocoa 小部件。我按照 Apple 的 LayerBackedOpenGLView
示例代码中的示例进行操作。使用以下方法为 NSOpenGLView
提供一个支持层:
[glView setWantsLayer:YES]
然后将带有小部件的 Cocoa NSView
添加为 glView
的子视图。这基本上是有效的,并且比我之前的方法快两倍,我之前的方法是将包含小部件的 NSView 添加到包含 glView 的窗口的子窗口(这是我在网络上找到的另一个解决方案) 。
有两个问题。
- 首先是我在混合中使用的一些纹理不再能够正确混合。经过一番搜索后,我似乎需要清除 OpenGLView 的 Alpha 通道。我在绘制框架后调用的这段代码似乎解决了这个问题:
代码:
glColorMask(FALSE, FALSE, FALSE, TRUE); //This ensures that only alpha will be effected
glClearColor(0, 0, 0, 1); //alphaValue - Value to which you need to clear
glClear(GL_COLOR_BUFFER_BIT);
glColorMask(TRUE, TRUE, TRUE, TRUE); //Put color mask back to what it was.
有人可以解释为什么在使用CALayer
时需要这样做,而不是没有吗?
- 第二个问题我没有办法解决。似乎当我平移到观察到问题 #1 的场景部分时,帧速率从 110 FPS 下降到 10 FPS。同样,这种情况只有在我添加背衬层后才开始发生。这种情况并不总是发生。有时,平移场景的这一部分时,FPS 会保持较高水平,但这种情况很少见。我认为它一定与这里的纹理混合方式有关,但我不知道是什么。
有什么想法吗?
I needed to display some Cocoa widgets on top of an NSOpenGLView in an existing app. I followed the example in Apple's LayerBackedOpenGLView
example code. The NSOpenGLView
is given a backing layer using:
[glView setWantsLayer:YES]
Then the Cocoa NSView
with the widgets is added as a subview of the glView
. This is basically working and is twice ad fast as my previous approach where I added the NSView
containing the widgets to a child window of the window containing the glView (this was the other solution I found on the web).
There were two problems.
- The first is that some textures that I use with blending were no longer getting the blend right. After searching around a bit it looked like I might need to clear the alpha channel of the
OpenGLView
. This bit of code that I call after drawing a frame seems to have fixed this problem:
Code:
glColorMask(FALSE, FALSE, FALSE, TRUE); //This ensures that only alpha will be effected
glClearColor(0, 0, 0, 1); //alphaValue - Value to which you need to clear
glClear(GL_COLOR_BUFFER_BIT);
glColorMask(TRUE, TRUE, TRUE, TRUE); //Put color mask back to what it was.
Can someone explain why this is needed when using the CALayer
, but not without?
- The second problem I don't have a solution for. It seems that when I pan to the part of the scene where problem is #1 was observed, the frame rate drops from something like 110 FPS down to 10 FPS. Again, this only started happening after I added the backing layer. This doesn't always happen. Sometimes the FPS stays high when panning over this part of the scene but that is rare. I assume it must have something with how the textures here are blended, but I have no idea what.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我确实找到了解决速度放缓的方法。 OpenGL 视图之上有一个 HUD(平视显示器)视图。我已经安装了另一个 NSView 作为子视图(如果有的话)。 HUD 和子视图都有大量的 alpha 操作,并且由于某种原因,导致合成图层的速度真正减慢。我可以轻松地将此子视图安装为 OpenGL 视图的子视图,当我这样做时,一切都会再次加快。因此,尽管我不完全理解经济放缓的情况,但我确实有一个很好的解决办法。
I did figure out a workaround to the slowdown. The OpenGL view has a HUD (heads up display) view that goes on top of it. I had installed another NSView as a subview if it. Both the HUD and the subview have lots of alpha manipulation and for some reason that tickled a real slowdown in compositing the layers. I could easily instal this subview as a subview of the OpenGL view and when I did this everything sped up again. So although I don't fully understand the slowdown, I do have a good work around for it.