Emscripten:如何在运行时检测 webgl 上下文版本?

Emscripten: how do I detect webgl context version in runtime?

我在 Emscripten 中使用 GLFW3 和 GLEW 包装器,所以我不会手动调用 emscripten_webgl_create_context 也不会设置上下文的属性。上下文版本仅由 JS 代码确定,这超出了我的范围。在我的 C++ 代码中,我需要知道我们是在 WebGL1 还是 WebGL2 上下文中 运行。有没有独立于文档的方法来做到这一点?类似于:

const auto ctx = emscripten_webgl_get_current_context();
emscripten_webgl_get_context_version(ctx);// Should return 1 or 2.

在 C++ 中

const char ES_VERSION_2_0[] = "OpenGL ES 2.0";
const char ES_VERSION_3_0[] = "OpenGL ES 3.0";

const char* version = glGetString(GL_VERSION);
if (strncmp(version, ES_VERSION_2_0, sizeof(ES_VERSION_2_0)) == 0) {
  // it's WebGL1
} else if (strncmp(version, ES_VERSION_3_0, sizeof(ES_VERSION_3_0)) == 0) {
  // it's WebGL2
} else {
  // it's something else
}

WebGL 中的版本字符串具有必需的非硬件相关起始格式。参见 the spec for WebGL2

VERSION: Returns a version or release number of the form WebGL<space>2.0<optional><space><vendor-specific information></optional>.

WebGL1

VERSION: Returns a version or release number of the form WebGL<space>1.0<space><vendor-specific information>.

Emscripten 还 returns 固定字符串。查看来源

https://github.com/kripken/emscripten/blob/ec764ace634f13bab5ae932912da53fe93ee1b69/src/library_gl.js#L923