Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ See docs/process.md for more on how version tagging works.
- Building with `-sJSPI` now bumps the default minimum supported browser
versions to the first ones that support JSPI: Chrome 137, Firefox 153,
Safari 27 and Node.js 24. (#28334)
- Added support for the WebGL 2 extension `OVR_multiview2`. (#20173)

7.0.0 - 11/06/26
----------------
Expand Down
4 changes: 3 additions & 1 deletion src/lib/libhtml5_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ var LibraryHtml5WebGL = {
#if MAX_WEBGL_VERSION >= 2
if (extString == 'WEBGL_draw_instanced_base_vertex_base_instance') webgl_enable_WEBGL_draw_instanced_base_vertex_base_instance(GLctx);
if (extString == 'WEBGL_multi_draw_instanced_base_vertex_base_instance') webgl_enable_WEBGL_multi_draw_instanced_base_vertex_base_instance(GLctx);
if (extString == 'OVR_multiview2') webgl_enable_OVR_multiview2(GLctx);
#endif

if (extString == 'WEBGL_multi_draw') webgl_enable_WEBGL_multi_draw(GLctx);
Expand All @@ -360,7 +361,8 @@ var LibraryHtml5WebGL = {
'EXT_clip_control',
'WEBGL_polygon_mode',
'WEBGL_draw_instanced_base_vertex_base_instance',
'WEBGL_multi_draw_instanced_base_vertex_base_instance'].includes(extString)) {
'WEBGL_multi_draw_instanced_base_vertex_base_instance',
'OVR_multiview2'].includes(extString)) {
err('When building with -sGL_SUPPORT_SIMPLE_ENABLE_EXTENSIONS=0, function emscripten_webgl_enable_extension() cannot be used to enable extension '
+ extString + '! Use one of the functions emscripten_webgl_enable_*() to enable it!');
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/libsigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ sigs = {
emscripten_webgl_enable_EXT_clip_control__sig: 'ip',
emscripten_webgl_enable_EXT_polygon_offset_clamp__sig: 'ip',
emscripten_webgl_enable_OES_vertex_array_object__sig: 'ip',
emscripten_webgl_enable_OVR_multiview2__sig: 'ip',
emscripten_webgl_enable_WEBGL_draw_buffers__sig: 'ip',
emscripten_webgl_enable_WEBGL_draw_instanced_base_vertex_base_instance__sig: 'ip',
emscripten_webgl_enable_WEBGL_multi_draw__sig: 'ip',
Expand Down Expand Up @@ -1075,6 +1076,7 @@ sigs = {
glFramebufferTexture2D__sig: 'viiiii',
glFramebufferTexture2DOES__sig: 'viiiii',
glFramebufferTextureLayer__sig: 'viiiii',
glFramebufferTextureMultiviewOVR__sig: 'viiiiii',
glFrontFace__sig: 'vi',
glFrustum__sig: 'vdddddd',
glFrustumf__sig: 'vffffff',
Expand Down
2 changes: 2 additions & 0 deletions src/lib/libwebgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ for (let i = 0; i <= GL_POOL_TEMP_BUFFERS_SIZE; ++i) {
'EXT_disjoint_timer_query_webgl2',
'EXT_texture_norm16',
'NV_shader_noperspective_interpolation',
'OVR_multiview2',
'WEBGL_clip_cull_distance',
'WEBGL_draw_instanced_base_vertex_base_instance',
'WEBGL_multi_draw_instanced_base_vertex_base_instance',
Expand Down Expand Up @@ -1207,6 +1208,7 @@ for (let i = 0; i <= GL_POOL_TEMP_BUFFERS_SIZE; ++i) {
// Extensions that are available from WebGL >= 2 (no-op if called on a WebGL 1 context active)
webgl_enable_WEBGL_draw_instanced_base_vertex_base_instance(gl);
webgl_enable_WEBGL_multi_draw_instanced_base_vertex_base_instance(gl);
webgl_enable_OVR_multiview2(gl);

// On WebGL 2, EXT_disjoint_timer_query is replaced with an alternative
// that's based on core APIs, and exposes only the queryCounterEXT()
Expand Down
15 changes: 15 additions & 0 deletions src/lib/libwebgl2.js
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,21 @@ var LibraryWebGL2 = {

emscripten_webgl_enable_WEBGL_multi_draw_instanced_base_vertex_base_instance: (ctx) =>
webgl_enable_WEBGL_multi_draw_instanced_base_vertex_base_instance(GL.contexts[ctx].GLctx),

$webgl_enable_OVR_multiview2: (ctx) =>
// Closure is expected to be allowed to minify the '.ovrMultiview2' property, so not accessing it quoted.
!!(ctx.ovrMultiview2 = ctx.getExtension('OVR_multiview2')),

emscripten_webgl_enable_OVR_multiview2: (ctx) =>
webgl_enable_OVR_multiview2(GL.contexts[ctx].GLctx),

glFramebufferTextureMultiviewOVR: (target, attachment, texture, level, baseViewIndex, numViews) => {
#if GL_ASSERTIONS
assert(GLctx.ovrMultiview2, 'OVR_multiview2 not supported, or not enabled. Before calling glFramebufferTextureMultiviewOVR(), call emscripten_webgl_enable_OVR_multiview2() to enable this extension, and verify that it returns true to indicate support. (alternatively, build with -sGL_SUPPORT_AUTOMATIC_ENABLE_EXTENSIONS=1 to enable all GL extensions by default)');
GL.validateGLObjectID(GL.textures, texture, 'glFramebufferTextureMultiviewOVR', 'texture');
#endif
GLctx.ovrMultiview2['framebufferTextureMultiviewOVR'](target, attachment, GL.textures[texture], level, baseViewIndex, numViews);
},
};

#if MAX_WEBGL_VERSION >= 2
Expand Down
2 changes: 2 additions & 0 deletions system/include/emscripten/html5_webgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ bool emscripten_webgl_enable_EXT_clip_control(EMSCRIPTEN_WEBGL_CONTEXT_HANDLE co

bool emscripten_webgl_enable_WEBGL_polygon_mode(EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context);

bool emscripten_webgl_enable_OVR_multiview2(EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context);

typedef bool (*em_webgl_context_callback)(int eventType, const void *reserved, void *userData);
EMSCRIPTEN_RESULT emscripten_set_webglcontextlost_callback_on_thread(const char * _Nonnull target, void *userData, bool useCapture, em_webgl_context_callback callback, pthread_t targetThread);
EMSCRIPTEN_RESULT emscripten_set_webglcontextrestored_callback_on_thread(const char * _Nonnull target, void *userData, bool useCapture, em_webgl_context_callback callback, pthread_t targetThread);
Expand Down
20 changes: 20 additions & 0 deletions system/include/webgl/webgl2_ext.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "webgl2.h"
#include <emscripten/html5.h>

// 33. https://www.khronos.org/registry/webgl/extensions/EXT_disjoint_timer_query_webgl2/
#ifndef EMSCRIPTEN_GL_EXT_disjoint_timer_query_webgl2
Expand All @@ -12,6 +13,25 @@
WEBGL_APICALL void GL_APIENTRY glQueryCounterEXT(GLuint query, GLenum target);
#endif /* EMSCRIPTEN_GL_EXT_disjoint_timer_query_webgl2 */

// 36. https://www.khronos.org/registry/webgl/extensions/OVR_multiview2/
#ifndef EMSCRIPTEN_GL_OVR_multiview2
#define EMSCRIPTEN_GL_OVR_multiview2 1
// To enable: call
bool emscripten_webgl_enable_OVR_multiview2(EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context);
// or link with -sGL_SUPPORT_SIMPLE_ENABLE_EXTENSIONS=1 and
// call emscripten_webgl_enable_extension(ctx, "OVR_multiview2");
//
// Shaders must enable the extension with "#extension GL_OVR_multiview2 : require".
// WebGL does not support the older "GL_OVR_multiview" shader extension name, and
// only 2D array textures can be attached with glFramebufferTextureMultiviewOVR().
#define GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_NUM_VIEWS_OVR 0x9630
#define GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_BASE_VIEW_INDEX_OVR 0x9632
#define GL_MAX_VIEWS_OVR 0x9631
#define GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR 0x9633
WEBGL_APICALL void GL_APIENTRY emscripten_glFramebufferTextureMultiviewOVR(GLenum target, GLenum attachment, GLuint texture, GLint level, GLint baseViewIndex, GLsizei numViews);
WEBGL_APICALL void GL_APIENTRY glFramebufferTextureMultiviewOVR(GLenum target, GLenum attachment, GLuint texture, GLint level, GLint baseViewIndex, GLsizei numViews);
#endif /* EMSCRIPTEN_GL_OVR_multiview2 */

// 46. https://www.khronos.org/registry/webgl/extensions/WEBGL_draw_instanced_base_vertex_base_instance/
#ifndef EMSCRIPTEN_GL_WEBGL_draw_instanced_base_vertex_base_instance
#define EMSCRIPTEN_GL_WEBGL_draw_instanced_base_vertex_base_instance 1
Expand Down
6 changes: 5 additions & 1 deletion system/lib/gl/webgl2.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <webgl/webgl1.h>
#include <webgl/webgl2.h>
#include <webgl/webgl2_ext.h>

#include "webgl_internal.h"

Expand Down Expand Up @@ -150,6 +151,9 @@ GL_APICALL GLboolean GL_APIENTRY glIsVertexArrayOES(GLuint array) { return glIsV
GL_APICALL void GL_APIENTRY glDrawBuffersEXT(GLsizei n, const GLenum *bufs) { glDrawBuffers(n, bufs); }
GL_APICALL void GL_APIENTRY glDrawBuffersWEBGL(GLsizei n, const GLenum *bufs) { glDrawBuffers(n, bufs); }

// WebGL 2 extensions:
ASYNC_GL_FUNCTION_6(EM_FUNC_SIG_VIIIIII, void, glFramebufferTextureMultiviewOVR, GLenum, GLenum, GLuint, GLint, GLint, GLsizei);

#endif // ~__EMSCRIPTEN_PTHREADS__) && __EMSCRIPTEN_OFFSCREEN_FRAMEBUFFER__

#if GL_ENABLE_GET_PROC_ADDRESS
Expand Down Expand Up @@ -307,7 +311,7 @@ void *emscripten_webgl2_get_proc_address(const char *name) {
RETURN_FN(glDrawBuffersWEBGL);

// WebGL 2 extensions:
// (currently none)
RETURN_FN(glFramebufferTextureMultiviewOVR);

return 0;
}
Expand Down
209 changes: 209 additions & 0 deletions test/browser/webgl2_ovr_multiview2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
// Copyright 2026 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

// Tests the WebGL 2 extension OVR_multiview2, which allows rendering to
// several layers ("views") of a 2D array texture with a single draw call.
// See https://github.com/emscripten-core/emscripten/issues/20173
//
// The extension is not available in all browsers and platforms. If it is not
// supported, the test only verifies that it cannot be enabled.

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <emscripten/html5.h>

// The function glFramebufferTextureMultiviewOVR() is declared both by the
// Khronos GLES extension header, and by the Emscripten WebGL 2 extension
// header. Include both to verify that the two can be used together.
#define GL_GLEXT_PROTOTYPES
#include <GLES3/gl3.h>
#include <GLES2/gl2ext.h>
#include <webgl/webgl2_ext.h>

#define WIDTH 4
#define HEIGHT 4
#define NUM_VIEWS 2

static bool has_extension(const char *name) {
GLint num_extensions = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &num_extensions);
for (GLint i = 0; i < num_extensions; ++i) {
if (!strcmp((const char *)glGetStringi(GL_EXTENSIONS, i), name)) return true;
}
return false;
}

static GLuint compile_shader(GLenum type, const char *src) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &src, NULL);
glCompileShader(shader);
GLint compiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
char log[1024];
glGetShaderInfoLog(shader, sizeof(log), NULL, log);
printf("Shader compile failed: %s\n", log);
}
assert(compiled);
return shader;
}

static GLuint create_array_texture(GLenum internal_format) {
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D_ARRAY, texture);
glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, internal_format, WIDTH, HEIGHT, NUM_VIEWS);
return texture;
}

static void assert_attachment(GLenum attachment, GLint type, GLint num_views, GLint base_view_index) {
GLint value = -1;
glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, attachment, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value);
assert(value == type);
if (type != GL_NONE) {
glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, attachment, GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_NUM_VIEWS_OVR, &value);
assert(value == num_views);
glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, attachment, GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_BASE_VIEW_INDEX_OVR, &value);
assert(value == base_view_index);
}
assert(glGetError() == GL_NO_ERROR);
}

// Reads back a pixel from the given layer of the texture. Reading from a
// multiview framebuffer is not allowed, so do this via a regular framebuffer
// that has the layer as its attachment.
static void assert_layer_color(GLuint texture, int layer, int r, int g, int b) {
GLuint fbo;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0, layer);
assert(glCheckFramebufferStatus(GL_READ_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
unsigned char pixel[4] = { 0x12, 0x34, 0x56, 0x78 };
glReadPixels(WIDTH / 2, HEIGHT / 2, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
printf("Layer %d: %d,%d,%d,%d\n", layer, pixel[0], pixel[1], pixel[2], pixel[3]);
assert(pixel[0] == r && pixel[1] == g && pixel[2] == b && pixel[3] == 255);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glDeleteFramebuffers(1, &fbo);
assert(glGetError() == GL_NO_ERROR);
}

int main() {
EmscriptenWebGLContextAttributes attr;
emscripten_webgl_init_context_attributes(&attr);
attr.majorVersion = 2;
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attr);
assert(ctx > 0);
emscripten_webgl_make_context_current(ctx);

bool supported = has_extension("GL_OVR_multiview2");
printf("OVR_multiview2 supported: %d\n", supported);

#if defined(TEST_EXPLICIT_ENABLE)
// Enable the extension with its dedicated function.
assert(emscripten_webgl_enable_OVR_multiview2(ctx) == supported);
#elif defined(TEST_SIMPLE_ENABLE)
// Enable the extension with the generic extension enable function.
assert(emscripten_webgl_enable_extension(ctx, "OVR_multiview2") == supported);
#else
// By default the extension is enabled automatically at context creation
// time, if the browser supports it (-sGL_SUPPORT_AUTOMATIC_ENABLE_EXTENSIONS).
#endif

if (!supported) {
printf("OVR_multiview2 is not supported, skipping test.\n");
return 0;
}

GLint max_views = 0;
glGetIntegerv(GL_MAX_VIEWS_OVR, &max_views);
printf("GL_MAX_VIEWS_OVR: %d\n", max_views);
assert(max_views >= NUM_VIEWS);
assert(glGetError() == GL_NO_ERROR);

GLuint color = create_array_texture(GL_RGBA8);
GLuint depth = create_array_texture(GL_DEPTH_COMPONENT24);

GLuint fbo;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
glFramebufferTextureMultiviewOVR(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, color, 0, 0, NUM_VIEWS);
assert(glGetError() == GL_NO_ERROR);

// Attach the depth buffer via a function pointer to also test
// that the function can be looked up dynamically.
void (*framebufferTextureMultiview)(GLenum, GLenum, GLuint, GLint, GLint, GLsizei) =
(void (*)(GLenum, GLenum, GLuint, GLint, GLint, GLsizei))emscripten_webgl_get_proc_address("glFramebufferTextureMultiviewOVR");
assert(framebufferTextureMultiview);
framebufferTextureMultiview(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depth, 0, 0, NUM_VIEWS);
assert(glGetError() == GL_NO_ERROR);

assert(glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
assert_attachment(GL_COLOR_ATTACHMENT0, GL_TEXTURE, NUM_VIEWS, 0);
assert_attachment(GL_DEPTH_ATTACHMENT, GL_TEXTURE, NUM_VIEWS, 0);

// Attaching the null texture detaches the attachment.
glFramebufferTextureMultiviewOVR(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, 0, 0, 0, NUM_VIEWS);
assert_attachment(GL_DEPTH_ATTACHMENT, GL_NONE, 0, 0);
glFramebufferTextureMultiviewOVR(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depth, 0, 0, NUM_VIEWS);
assert_attachment(GL_DEPTH_ATTACHMENT, GL_TEXTURE, NUM_VIEWS, 0);

// Invalid parameters are reported as GL errors.
glFramebufferTextureMultiviewOVR(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, color, 0, 0, max_views + 1);
assert(glGetError() == GL_INVALID_VALUE);
assert_attachment(GL_COLOR_ATTACHMENT0, GL_TEXTURE, NUM_VIEWS, 0);
assert(glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);

// Render a fullscreen triangle to both views with a single draw call. The
// output color depends on the view that is being rendered to.
GLuint vs = compile_shader(GL_VERTEX_SHADER,
"#version 300 es\n"
"#extension GL_OVR_multiview2 : require\n"
"layout(num_views = 2) in;\n"
"flat out vec4 color;\n"
"void main() {\n"
" color = (gl_ViewID_OVR == 0u) ? vec4(1.0, 0.0, 0.0, 1.0) : vec4(0.0, 1.0, 0.0, 1.0);\n"
" gl_Position = vec4(float((gl_VertexID << 1) & 2) * 2.0 - 1.0, float(gl_VertexID & 2) * 2.0 - 1.0, 0.0, 1.0);\n"
"}\n");

GLuint fs = compile_shader(GL_FRAGMENT_SHADER,
"#version 300 es\n"
"precision mediump float;\n"
"flat in vec4 color;\n"
"out vec4 outColor;\n"
"void main() { outColor = color; }\n");

GLuint program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
GLint linked = 0;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
assert(linked);
glUseProgram(program);

GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

glViewport(0, 0, WIDTH, HEIGHT);
glClearColor(0.f, 0.f, 1.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
assert(glGetError() == GL_NO_ERROR);

// Clearing a multiview framebuffer clears all of the views.
assert_layer_color(color, 0, 0, 0, 255);
assert_layer_color(color, 1, 0, 0, 255);

glDrawArrays(GL_TRIANGLES, 0, 3);
assert(glGetError() == GL_NO_ERROR);

assert_layer_color(color, 0, 255, 0, 0);
assert_layer_color(color, 1, 0, 255, 0);

printf("Test passed!\n");
return 0;
}
4 changes: 4 additions & 0 deletions test/browser/webgl2_simple_enable_extensions.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ int main()
if (hasext(exts, "WEBGL_polygon_mode"))
assert(emscripten_webgl_enable_WEBGL_polygon_mode(context));

if (hasext(exts, "OVR_multiview2"))
assert(emscripten_webgl_enable_OVR_multiview2(context));

#if WEBGL_SIMPLE_ENABLE_EXTENSION
assert(hasext(exts, "ANGLE_instanced_arrays") == emscripten_webgl_enable_extension(context, "ANGLE_instanced_arrays"));
assert(hasext(exts, "OES_vertex_array_object") == emscripten_webgl_enable_extension(context, "OES_vertex_array_object"));
assert(hasext(exts, "WEBGL_draw_buffers") == emscripten_webgl_enable_extension(context, "WEBGL_draw_buffers"));
assert(hasext(exts, "WEBGL_draw_instanced_base_vertex_base_instance") == emscripten_webgl_enable_extension(context, "WEBGL_draw_instanced_base_vertex_base_instance"));
assert(hasext(exts, "WEBGL_multi_draw") == emscripten_webgl_enable_extension(context, "WEBGL_multi_draw"));
assert(hasext(exts, "WEBGL_multi_draw_instanced_base_vertex_base_instance") == emscripten_webgl_enable_extension(context, "WEBGL_multi_draw_instanced_base_vertex_base_instance"));
assert(hasext(exts, "OVR_multiview2") == emscripten_webgl_enable_extension(context, "OVR_multiview2"));
#endif

return 0;
Expand Down
Loading
Loading