跨不同 ViewController/EAGLView 使用相同的 EAGLContext

发布于 2024-12-07 23:22:29 字数 471 浏览 0 评论 0原文

我在 EAGLViews 中使用相同的 EAGLContext 时遇到一些问题。我有两个视图控制器,每个控制器都有一个 EAGLView。

每个视图控制器分配一个新的EAGLContext,并为每个相应的EAGLView创建后续的帧缓冲区/颜色缓冲区,但这会浪费内存资源。

我知道可以通过简单地将不同的帧缓冲区/颜色缓冲区绑定到不同的 EAGLViews 来跨 ViewController 使用相同的 EAGLContext :

使用多个 OpenGL 视图和 UIKit

但到目前为止我还没有做到这一点。

有什么想法吗?

提前致谢。

I'm having some issues using the same EAGLContext across EAGLViews. I have two View Controllers, each one has one EAGLView.

Each View Controller allocates a new EAGLContext, and a subsequent framebuffer/colorbuffer is created for each respective EAGLView, but this is a waste of memory resources.

I know that it is possible to use the same EAGLContext across ViewControllers by simply binding different framebuffers/colorbuffers to different EAGLViews:

Using Multiple OpenGL Views And UIKit

But i didnt manage to achieve that so far.

Any ideas?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皇甫轩 2024-12-14 23:22:29

最终成功解决了问题。

在我使用的视图控制器之一中:

dispatch_async(openGLESContextQueue, ^{

        [(EAGLView *)self.view setFramebuffer];

        (...opengl draw code...)

        [(EAGLView *)self.view presentFramebuffer];

    });

在多线程环境中使用 EAGLContext 时,必须小心防止其他线程同时使用:

@syncronized(context) { ...opengl drawing here...}

并在将控制权传递给另一个 ViewController 之前耗尽当前的dispatch_queue(通过presentViewController: ),使用:

dispatch_sync(openGLESContextQueue, ^{});

因此,通过使用这两个技巧,我能够在多个视图中仅使用一个 EAGLContext。还必须特别注意 EAGLContext 的当前状态。我得到了意想不到的结果,因为在第一个视图中我有:

glVertexPointer(2, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
glEnableClientState(GL_COLOR_ARRAY);

在第二个视图中我有完全不同的绘图代码,当然我忘记使用:

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);

瞧!

谢谢 :)

Finally managed to solve the problem.

In one of the view controllers i was using:

dispatch_async(openGLESContextQueue, ^{

        [(EAGLView *)self.view setFramebuffer];

        (...opengl draw code...)

        [(EAGLView *)self.view presentFramebuffer];

    });

When using EAGLContext in a multithreading environment one must be cautious to prevent other threads from accessing it at the same time using:

@syncronized(context) { ...opengl drawing here...}

and to drain the current dispatch_queue before passing control to another ViewController (through presentViewController:), using:

dispatch_sync(openGLESContextQueue, ^{});

So, by using these two tricks i was able to just use one EAGLContext across multiple views. One must also pay extra attention to the current state of the EAGLContext. I was having unexpected results because in the first view i had:

glVertexPointer(2, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
glEnableClientState(GL_COLOR_ARRAY);

In the second view i had completely different drawing code, and i forgot to, of course, use:

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);

And voilá!

Thanks :)

忘年祭陌 2024-12-14 23:22:29

EAGLView 实际上并不是一个单独的类;它是一个类。它是一个同名的类家族,Apple 倾向于将其放入其示例文件中。因此,就具体修改提出建议相对困难。

我最初的解决方案是创建一个提供单个共享 EAGLContext 的单例类。 EAGLContext 一次只能在一个线程上使用,因此这不一定是一个完整的解决方案,但您想要执行的操作可能取决于程序的语义,并且感觉像是一个不同的主题你的实际问题。

Xcode 4.1“OpenGL ES 应用程序”模板在视图外部创建上下文,而我认为它之前是在视图内部,这使事情变得更容易一些。

EAGLView isn't actually a single class; it's a family of classes with the same name that Apple tend to throw into their example files. So it's relatively difficult to advise on specific modifications.

My initial solution was to create a singleton class that vends a single, shared EAGLContext. EAGLContexts can be used on only one thread at a time so that's not necessarily a complete solution, but exactly what you want to do will probably depend on the semantics of your program and feels like a different topic from your actual question.

The Xcode 4.1 'OpenGL ES Application' template has context creation outside of the view, whereas I think it was previously inside, which makes things a little easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文