如何将 Unity 上下文导入 OpenGL Window
How to Get Unity Context into OpenGL Window
我想将 unity 上下文导入到 opengl 中,这样我就可以在 opengl glfw 中显示一个 unity 渲染纹理 window。我尝试使用
oldContext = glfwGetCurrentContext();
但 oldContext 的值仅为空。
我正在尝试使用低级本机统一插件和 Texture.GetNativeTexturePtr
如有任何帮助,我们将不胜感激!
OpenGL context cannot be queried like OpenGL state related objects via some glGet* API.Context is not part of OpenGL API,it is a part of the system you're running on and it exists to allow you maintaining of OpenGL state and issue command to the driver. You must access a system specific handle that points to the context via system specific API.On Windows (WinGDI)那就是
HGLRC wglGetCurrentContext();
在 linux 上查看相关 GLX API。您需要找到函数才能访问 GLXContext
我在Unity3D(framebuffer readout plugin)里做过一次。但它使用 Unity 的 OpenGL 或 DirectX 上下文仅发出 API 命令。
此外,我不确定您是否可以 'inject' 或为不拥有该上下文的 window 共享上下文。你看,当你(或 Unity)初始化显示时,它会创建上下文和相关的 GL 资源,比如默认的 FBO 和所有需要的附件,并且 FBO 被映射到一些系统资源(设备),它实际上负责呈现那些屏幕上的像素。因此,我不确定显示上下文是否可以从 Window 移动到 Window,就像上下文可以在线程之间共享一样。(但我在这一点上可能是错误的)
您可以在某个线程上创建您的插件 Window,它有自己的 GL 上下文。然后在这两者之间创建并共享一个纹理对象。请记住,GL 纹理是可共享的。如果您将 Unity 的屏幕 FBO 中的内容复制到该纹理中,那么您也可以从该纹理中将其复制到插件的屏幕 FBO 中。
顺便说一句,看看 。您可以看到供应商特定的 GL 扩展,它允许将数据从不同的上下文复制到纹理中,而无需共享上下文、共享列表设置。
关于为什么 GLFW returns 你 nullptr
。在您的示例中,您使用了 GLFW 库。
glfwGetCurrentContext()
但是如果你查看源代码,你会看到:
GLFWAPI GLFWwindow* glfwGetCurrentContext(void)
{
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
return _glfwPlatformGetTls(&_glfw.contextSlot);
}
这可能意味着它从它自己的缓存中检索指向 GLFWWindow 的指针,而不是从 system.And 中检索,如果您没有通过 GLFW 创建该上下文,您将不会得到任何有效指针。因此,请尝试直接使用您的系统相关 API 如上所述。
我想将 unity 上下文导入到 opengl 中,这样我就可以在 opengl glfw 中显示一个 unity 渲染纹理 window。我尝试使用
oldContext = glfwGetCurrentContext();
但 oldContext 的值仅为空。
我正在尝试使用低级本机统一插件和 Texture.GetNativeTexturePtr
如有任何帮助,我们将不胜感激!
OpenGL context cannot be queried like OpenGL state related objects via some glGet* API.Context is not part of OpenGL API,it is a part of the system you're running on and it exists to allow you maintaining of OpenGL state and issue command to the driver. You must access a system specific handle that points to the context via system specific API.On Windows (WinGDI)那就是
HGLRC wglGetCurrentContext();
在 linux 上查看相关 GLX API。您需要找到函数才能访问 GLXContext
我在Unity3D(framebuffer readout plugin)里做过一次。但它使用 Unity 的 OpenGL 或 DirectX 上下文仅发出 API 命令。
此外,我不确定您是否可以 'inject' 或为不拥有该上下文的 window 共享上下文。你看,当你(或 Unity)初始化显示时,它会创建上下文和相关的 GL 资源,比如默认的 FBO 和所有需要的附件,并且 FBO 被映射到一些系统资源(设备),它实际上负责呈现那些屏幕上的像素。因此,我不确定显示上下文是否可以从 Window 移动到 Window,就像上下文可以在线程之间共享一样。(但我在这一点上可能是错误的)
您可以在某个线程上创建您的插件 Window,它有自己的 GL 上下文。然后在这两者之间创建并共享一个纹理对象。请记住,GL 纹理是可共享的。如果您将 Unity 的屏幕 FBO 中的内容复制到该纹理中,那么您也可以从该纹理中将其复制到插件的屏幕 FBO 中。
顺便说一句,看看
关于为什么 GLFW returns 你 nullptr
。在您的示例中,您使用了 GLFW 库。
glfwGetCurrentContext()
但是如果你查看源代码,你会看到:
GLFWAPI GLFWwindow* glfwGetCurrentContext(void)
{
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
return _glfwPlatformGetTls(&_glfw.contextSlot);
}
这可能意味着它从它自己的缓存中检索指向 GLFWWindow 的指针,而不是从 system.And 中检索,如果您没有通过 GLFW 创建该上下文,您将不会得到任何有效指针。因此,请尝试直接使用您的系统相关 API 如上所述。