Skip to content

Commit

Permalink
Add MSAA support for WebXR
Browse files Browse the repository at this point in the history
  • Loading branch information
dsnopek committed Dec 4, 2023
1 parent caddce1 commit 9f1dd94
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 37 deletions.
8 changes: 7 additions & 1 deletion drivers/gles3/storage/config.cpp
Expand Up @@ -99,8 +99,14 @@ Config::Config() {
msaa_supported = extensions.has("GL_EXT_framebuffer_multisample");
#endif
#ifndef IOS_ENABLED
#ifdef WEB_ENABLED
msaa_multiview_supported = extensions.has("OCULUS_multiview");
rt_msaa_multiview_supported = msaa_multiview_supported;
#else
msaa_multiview_supported = extensions.has("GL_EXT_multiview_texture_multisample");
multiview_supported = extensions.has("GL_OVR_multiview2") || extensions.has("GL_OVR_multiview");
#endif

multiview_supported = extensions.has("OCULUS_multiview") || extensions.has("GL_OVR_multiview2") || extensions.has("GL_OVR_multiview");
#endif

#ifdef ANDROID_ENABLED
Expand Down
72 changes: 50 additions & 22 deletions drivers/gles3/storage/render_scene_buffers_gles3.cpp
Expand Up @@ -51,10 +51,42 @@ RenderSceneBuffersGLES3::~RenderSceneBuffersGLES3() {
free_render_buffer_data();
}

void RenderSceneBuffersGLES3::_rt_attach_textures(GLuint p_color, GLuint p_depth, GLsizei p_samples, uint32_t p_view_count) {
if (p_view_count > 1) {
if (p_samples > 1) {
#if defined(ANDROID_ENABLED) || defined(WEB_ENABLED)
glFramebufferTextureMultisampleMultiviewOVR(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, p_color, 0, p_samples, 0, p_view_count);
glFramebufferTextureMultisampleMultiviewOVR(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, p_depth, 0, p_samples, 0, p_view_count);
#else
ERR_PRINT_ONCE("Multiview MSAA isn't supported on this platform.");
#endif
} else {
#ifndef IOS_ENABLED
glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, p_color, 0, 0, p_view_count);
glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, p_depth, 0, 0, p_view_count);
#else
ERR_PRINT_ONCE("Multiview isn't supported on this platform.");
#endif
}
} else {
if (p_samples > 1) {
#ifdef ANDROID_ENABLED
glFramebufferTexture2DMultisampleEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_color, 0, p_samples);
glFramebufferTexture2DMultisampleEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, p_depth, 0, p_samples);
#else
ERR_PRINT_ONCE("MSAA via EXT_multisampled_render_to_texture isn't supported on this platform.");
#endif
} else {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_color, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, p_depth, 0);
}
}
}

GLuint RenderSceneBuffersGLES3::_rt_get_cached_fbo(GLuint p_color, GLuint p_depth, GLsizei p_samples, uint32_t p_view_count) {
FBDEF new_fbo;

#ifdef ANDROID_ENABLED
#if defined(ANDROID_ENABLED) || defined(WEB_ENABLED)
// There shouldn't be more then 3 entries in this...
for (const FBDEF &cached_fbo : msaa3d.cached_fbos) {
if (cached_fbo.color == p_color && cached_fbo.depth == p_depth) {
Expand All @@ -68,13 +100,7 @@ GLuint RenderSceneBuffersGLES3::_rt_get_cached_fbo(GLuint p_color, GLuint p_dept
glGenFramebuffers(1, &new_fbo.fbo);
glBindFramebuffer(GL_FRAMEBUFFER, new_fbo.fbo);

if (p_view_count > 1) {
glFramebufferTextureMultisampleMultiviewOVR(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, p_color, 0, p_samples, 0, p_view_count);
glFramebufferTextureMultisampleMultiviewOVR(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, p_depth, 0, p_samples, 0, p_view_count);
} else {
glFramebufferTexture2DMultisampleEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, p_color, 0, p_samples);
glFramebufferTexture2DMultisampleEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, p_depth, 0, p_samples);
}
_rt_attach_textures(p_color, p_depth, p_samples, p_view_count);

GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
Expand Down Expand Up @@ -317,8 +343,6 @@ void RenderSceneBuffersGLES3::configure(const RenderSceneBuffersConfiguration *p
// hence we'll use our FBO cache here.
msaa3d.needs_resolve = false;
msaa3d.check_fbo_cache = true;
#endif
#ifdef ANDROID_ENABLED
} else if (use_internal_buffer) {
// We can combine MSAA and scaling/effects.
msaa3d.needs_resolve = false;
Expand All @@ -329,13 +353,7 @@ void RenderSceneBuffersGLES3::configure(const RenderSceneBuffersConfiguration *p
glGenFramebuffers(1, &msaa3d.fbo);
glBindFramebuffer(GL_FRAMEBUFFER, msaa3d.fbo);

if (use_multiview) {
glFramebufferTextureMultisampleMultiviewOVR(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, internal3d.color, 0, msaa3d.samples, 0, view_count);
glFramebufferTextureMultisampleMultiviewOVR(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, internal3d.depth, 0, msaa3d.samples, 0, view_count);
} else {
glFramebufferTexture2DMultisampleEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, internal3d.color, 0, msaa3d.samples);
glFramebufferTexture2DMultisampleEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, internal3d.depth, 0, msaa3d.samples);
}
_rt_attach_textures(internal3d.color, internal3d.depth, msaa3d.samples, view_count);

GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
Expand Down Expand Up @@ -515,24 +533,34 @@ void RenderSceneBuffersGLES3::free_render_buffer_data() {
}

GLuint RenderSceneBuffersGLES3::get_render_fbo() {
if (msaa3d.check_fbo_cache) {
GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
GLuint rt_fbo = 0;

if (msaa3d.check_fbo_cache) {
GLuint color = texture_storage->render_target_get_color(render_target);
GLuint depth = texture_storage->render_target_get_depth(render_target);

return _rt_get_cached_fbo(color, depth, msaa3d.samples, view_count);
rt_fbo = _rt_get_cached_fbo(color, depth, msaa3d.samples, view_count);
} else if (msaa3d.fbo != 0) {
// We have an MSAA fbo, render to our MSAA buffer
return msaa3d.fbo;
} else if (internal3d.fbo != 0) {
// We have an internal buffer, render to our internal buffer!
return internal3d.fbo;
} else {
GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
rt_fbo = texture_storage->render_target_get_fbo(render_target);
}

return texture_storage->render_target_get_fbo(render_target);
if (texture_storage->render_target_is_reattach_textures(render_target)) {
GLuint color = texture_storage->render_target_get_color(render_target);
GLuint depth = texture_storage->render_target_get_depth(render_target);

glBindFramebuffer(GL_FRAMEBUFFER, rt_fbo);
_rt_attach_textures(color, depth, msaa3d.samples, view_count);
glBindFramebuffer(GL_FRAMEBUFFER, texture_storage->system_fbo);
}

return rt_fbo;
}

#endif // GLES3_ENABLED
1 change: 1 addition & 0 deletions drivers/gles3/storage/render_scene_buffers_gles3.h
Expand Up @@ -95,6 +95,7 @@ class RenderSceneBuffersGLES3 : public RenderSceneBuffers {
void _clear_intermediate_buffers();
void _clear_back_buffers();

void _rt_attach_textures(GLuint p_color, GLuint p_depth, GLsizei p_samples, uint32_t p_view_count);
GLuint _rt_get_cached_fbo(GLuint p_color, GLuint p_depth, GLsizei p_samples, uint32_t p_view_count);

public:
Expand Down
7 changes: 7 additions & 0 deletions drivers/gles3/storage/texture_storage.cpp
Expand Up @@ -2320,6 +2320,13 @@ GLuint TextureStorage::render_target_get_depth(RID p_render_target) const {
}
}

bool TextureStorage::render_target_is_reattach_textures(RID p_render_target) const {
RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
ERR_FAIL_NULL_V(rt, false);

return rt->reattach_textures;
}

void TextureStorage::render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale) {
RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
ERR_FAIL_NULL(rt);
Expand Down
2 changes: 2 additions & 0 deletions drivers/gles3/storage/texture_storage.h
Expand Up @@ -365,6 +365,7 @@ struct RenderTarget {

bool used_in_frame = false;
RS::ViewportMSAA msaa = RS::VIEWPORT_MSAA_DISABLED;
bool reattach_textures = false;

struct RTOverridden {
bool is_overridden = false;
Expand Down Expand Up @@ -639,6 +640,7 @@ class TextureStorage : public RendererTextureStorage {
GLuint render_target_get_fbo(RID p_render_target) const;
GLuint render_target_get_color(RID p_render_target) const;
GLuint render_target_get_depth(RID p_render_target) const;
bool render_target_is_reattach_textures(RID p_render_target) const;

virtual void render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale) override;
virtual Rect2i render_target_get_sdf_rect(RID p_render_target) const override;
Expand Down
27 changes: 13 additions & 14 deletions modules/webxr/webxr_interface_js.cpp
Expand Up @@ -461,21 +461,10 @@ bool WebXRInterfaceJS::pre_draw_viewport(RID p_render_target) {
// See: https://immersive-web.github.io/layers/#xropaquetextures
//
// This is why we're doing this sort of silly check: if the color and depth
// textures are the same this frame as last frame, we need to attach them
// textures are the same this frame as last frame, we need to reattach them
// again, despite the fact that the GLuint for them hasn't changed.
if (rt->overridden.is_overridden && rt->overridden.color == color_texture && rt->overridden.depth == depth_texture) {
GLES3::Config *config = GLES3::Config::get_singleton();
bool use_multiview = rt->view_count > 1 && config->multiview_supported;

glBindFramebuffer(GL_FRAMEBUFFER, rt->fbo);
if (use_multiview) {
glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, rt->color, 0, 0, rt->view_count);
glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, rt->depth, 0, 0, rt->view_count);
} else {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rt->depth, 0);
}
glBindFramebuffer(GL_FRAMEBUFFER, texture_storage->system_fbo);
rt->reattach_textures = true;
}

return true;
Expand All @@ -484,7 +473,17 @@ bool WebXRInterfaceJS::pre_draw_viewport(RID p_render_target) {
Vector<BlitToScreen> WebXRInterfaceJS::post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) {
Vector<BlitToScreen> blit_to_screen;

// We don't need to do anything here.
GLES3::TextureStorage *texture_storage = dynamic_cast<GLES3::TextureStorage *>(RSG::texture_storage);
if (texture_storage == nullptr) {
return blit_to_screen;
}

GLES3::RenderTarget *rt = texture_storage->get_render_target(p_render_target);
if (rt == nullptr) {
return blit_to_screen;
}

rt->reattach_textures = false;

return blit_to_screen;
};
Expand Down
2 changes: 2 additions & 0 deletions platform/web/godot_webgl2.h
Expand Up @@ -44,9 +44,11 @@ extern "C" {
#endif

void godot_webgl2_glFramebufferTextureMultiviewOVR(GLenum target, GLenum attachment, GLuint texture, GLint level, GLint baseViewIndex, GLsizei numViews);
void godot_webgl2_glFramebufferTextureMultisampleMultiviewOVR(GLenum target, GLenum attachment, GLuint texture, GLint level, GLsizei samples, GLint baseViewIndex, GLsizei numViews);
void godot_webgl2_glGetBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, GLvoid *data);

#define glFramebufferTextureMultiviewOVR godot_webgl2_glFramebufferTextureMultiviewOVR
#define glFramebufferTextureMultisampleMultiviewOVR godot_webgl2_glFramebufferTextureMultisampleMultiviewOVR

#ifdef __cplusplus
}
Expand Down
16 changes: 16 additions & 0 deletions platform/web/js/libs/library_godot_webgl2.externs.js
Expand Up @@ -34,3 +34,19 @@ OVR_multiview2.prototype.FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
* @return {void}
*/
OVR_multiview2.prototype.framebufferTextureMultiviewOVR = function(target, attachment, texture, level, baseViewIndex, numViews) {};

/**
* @constructor OCULUS_multiview
*/
function OCULUS_multiview() {}

/**
* @param {number} target
* @param {number} attachment
* @param {WebGLTexture} texture
* @param {number} level
* @param {number} baseViewIndex
* @param {number} numViews
* @return {void}
*/
OCULUS_multiview.prototype.framebufferTextureMultisampleMultiviewOVR = function(target, attachment, texture, level, samples, baseViewIndex, numViews) {};
17 changes: 17 additions & 0 deletions platform/web/js/libs/library_godot_webgl2.js
Expand Up @@ -61,6 +61,23 @@ const GodotWebGL2 = {
const /** OVR_multiview2 */ ext = context.multiviewExt;
ext.framebufferTextureMultiviewOVR(target, attachment, GL.textures[texture], level, base_view_index, num_views);
},

godot_webgl2_glFramebufferTextureMultisampleMultiviewOVR__deps: ['emscripten_webgl_get_current_context'],
godot_webgl2_glFramebufferTextureMultisampleMultiviewOVR__proxy: 'sync',
godot_webgl2_glFramebufferTextureMultisampleMultiviewOVR__sig: 'viiiiiii',
godot_webgl2_glFramebufferTextureMultisampleMultiviewOVR: function (target, attachment, texture, level, samples, base_view_index, num_views) {
const context = GL.currentContext;
if (typeof context.oculusMultiviewExt === 'undefined') {
const /** OCULUS_multiview */ ext = context.GLctx.getExtension('OCULUS_multiview');
if (!ext) {
GodotRuntime.error('Trying to call glFramebufferTextureMultisampleMultiviewOVR() without the OCULUS_multiview extension');
return;
}
context.oculusMultiviewExt = ext;
}
const /** OCULUS_multiview */ ext = context.oculusMultiviewExt;
ext.framebufferTextureMultisampleMultiviewOVR(target, attachment, GL.textures[texture], level, samples, base_view_index, num_views);
},
};

autoAddDeps(GodotWebGL2, '$GodotWebGL2');
Expand Down

0 comments on commit 9f1dd94

Please sign in to comment.