glXGetProcAddress returns 对于以 "gl" 开头的任何 procName 非空

glXGetProcAddress returns non-null for any procName starting with "gl"

当我发现以下代码及其许多变体产生非空内存地址时,我感到非常惊讶。我尝试过的变体包括:

程序是用 -lGL(需要时 -lglfw)编译的,没有警告也没有错误。

获得0输出,NULL指针的唯一方法是询问名称不以gl开头的函数的地址,例如manny.

我对这种行为感到非常惊讶,因为 glottismanny 应该同样不存在,我本以为它们都会产生一个 NULL 指针。

资源

这是 glXGetProcAddress 文档的摘录。

Notes

A NULL pointer is returned if function requested is not suported in the implementation being queried.

GLU functions are not queryable due to the fact that the library may not be loaded at the time of the query.

Full document

More info on this topic

嗯,从正确性的角度来看,您观察到的行为是合规的。您不能断定 glXGetProcAddress(或其他)的非 NULL return 值暗示该函数存在或可以使用。您必须始终查询扩展字符串。尝试获取扩展字符串未公布的函数的函数指针(或其上下文的核心 GL 版本隐含的存在)在概念上将是未定义的行为。

您确实引用了 reference page on glXGetProcAddress。不幸的是,这些参考页是出了名的不精确、不完整,有时甚至完全 错误 。在这种情况下,至少措辞是不幸的。

我通常建议使用官方规范来查找此类详细信息。在这种情况下,GLX 1.4 specification 将是相关文件。第 3.3.12 节 "Obtaining Extension Function Pointers" 关于 glXGetProcAddress 的说明(强调我的):

A return value of NULL indicates that the specified function does not exist for the implementation.

A non-NULL return value for glXGetProcAddress does not guarantee that an extension function is actually supported at runtime. The client must also query glGetString(GL_EXTENSIONS) or glXQueryExtensionsString to determine if an extension is supported by a particular context. [...]

glXGetProcAddress may be queried for all of the following functions:

  • All GL and GLX extension functions supported by the implementation (whether those extensions are supported by the current context or not).
  • All core (non-extension) functions in GL and GLX from version 1.0 up to and including the versions of those specifications supported by the implementation, as determined by glGetString(GL_VERSION) and glXQueryVersion queries.

看起来 Mesa3D 实现实际上能够为每个以 gl 开头的查询函数动态生成一些存根。

查看 /src/mapi/glapi/glapi_getproc.c 的当前版本揭示函数 _glapi_get_proc_address() 这样做:

/**
 * Return pointer to the named function.  If the function name isn't found
 * in the name of static functions, try generating a new API entrypoint on
 * the fly with assembly language.
 */
_glapi_proc
_glapi_get_proc_address(const char *funcName)
{
   _glapi_proc func;
   struct _glapi_function * entry;

   init_glapi_relocs_once();

#ifdef MANGLE
   /* skip the prefix on the name */
   if (funcName[1] != 'g' || funcName[2] != 'l')
      return NULL;
#else
   if (funcName[0] != 'g' || funcName[1] != 'l')
      return NULL;
#endif

   /* search extension functions first */
   func = get_extension_proc_address(funcName);
   if (func)
      return func;

   /* search static functions */
   func = get_static_proc_address(funcName);
   if (func)
      return func;

   /* generate entrypoint, dispatch offset must be filled in by the driver */
   entry = add_function_name(funcName);
   if (entry == NULL)
      return NULL;

   return entry->dispatch_stub;
}

所以它实际上检查了 gl 前缀,如果函数不知道,它会动态地为它创建一个存根。稍后,当加载硬件后端驱动程序时,它可能会注册 gl 函数,并且存根代码会将调用转发给驱动程序(如果它为其提供了实现)。