为什么 OpenGL 给对象提供句柄而不是指针? [英] Why does OpenGL give handles to objects instead of pointers?

查看:68
本文介绍了为什么 OpenGL 给对象提供句柄而不是指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OpenGL 的传统是让用户使用 unsigned int 句柄来操作 OpenGL 对象.为什么不直接给出一个指针呢?与指针相比,唯一 ID 有哪些优势?

The OpenGL tradition is to let the user manipulate OpenGL objects using an unsigned int handle. Why not just give a pointer instead? What are the advantages of unique IDs over pointers?

推荐答案

TL;DR:OpenGL ID 不会双射映射到内存位置.一个 OpenGL ID 可以同时引用多个内存位置.此外,OpenGL 也被设计用于分布式渲染架构(如 X11),并且给定一个间接上下文,在不同机器上运行的程序可能使用相同的 OpenGL 上下文.

OpenGL 被设计为一种与架构和显示系统无关的 API.当 OpenGL 最初开发时,这是根据客户端-服务器显示架构(如 X11)发生的.如果您查看 OpenGL 规范,即使是现代 OpenGL-4,它也指的是客户端和服务器.

OpenGL has been designed as an architecture and display system agnostic API. When OpenGL was first developed this happened in light of client-server display architectures (like X11). If you look into the OpenGL specification, even of modern OpenGL-4 it refers to clients and servers.

然而,在客户端/服务器架构中,指针毫无意义.一方面,客户端无法访问服务器的地址空间,除非跳一些圈.而且即使设置了共享内存映射,客户端和服务器的对象地址也不相同.除此之外,在像 X11 这样的体系结构上,单个间接 OpenGL 上下文可以被多个客户端使用,甚至可以在不同的机器上运行.指针根本不起作用.

However in a client/server architectures pointers make no sense. For one the address space of the server is not accessible to the clients without jumping some hoops. And even if you set up a shared memory mapping, the addresses of objects are not the same for client and server. Add to this that on architectures like X11 a single indirect OpenGL context can be used by multiple clients, that may even run on different machines. Pointers simply don't work for that.

最后但并非最不重要的是,OpenGL 对象模型是高度抽象的,而 OpenGL 绘图模型是异步的假设我执行以下操作:

Last but not least the OpenGL object model is highly abstract and the OpenGL drawing model is asynchonous Say I do the following:

id = glGenTextures(1)
glBindTexture(id)

glTexStorage(…)

glTexSubImage(image_a)
draw_something()

glTexSubImage(image_b)
draw_someting_b()

当到达这个小片段的末尾时,实际上可能什么都没有绘制,因为还没有达到同步点(glFinish、glReadPixels、缓冲区交换).请注意对 glTexSubImage 的两次调用,它们发生在同一 ID 上.当像素最终被放入帧缓冲区时,有两个不同的图像来自单个纹理 ID,因为 OpenGL 向您保证,事物看起来就像是同步绘制的一样.因此,在绘制批次结束时,单个对象 ID 可能指代在内存中具有不同位置的不同数据集的整个集合.

When the end of this little snippet has reached, actually nothing at all may have been drawn yet, because no synchronization point has been reached (glFinish, glReadPixels, a buffer swap). Note the two calls to glTexSubImage, which happen on the same id. When the pixels are finally put to the framebuffer, there two different images to be sourced from a single texture ID, because OpenGL guarantees you, that things will appear as if things were drawn synchronously. So at the end of a drawing batch a single object ID may refer to a whole collection of different data sets with different locations in memory.

这篇关于为什么 OpenGL 给对象提供句柄而不是指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆