From 47ba585ccf45dfad9aaecf62a1b053d97a0f4cd5 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 21 Nov 2024 10:01:44 -0800 Subject: [PATCH] [Impeller] support GLES 3.0 MSAA without extension. (flutter/engine#56705) Adds multisampling support for GLES devices without GL_EXT_multisampled_render_to_texture provided they are at least GLES 3.0 to support mutlisampled render buffers. Fixes https://github.com/flutter/flutter/issues/158360 Fixes https://github.com/flutter/flutter/issues/157951 TBD: should we prefer renderbuffer 3.0 approach over multisample_render_to_texture? --- .../flutter/impeller/display_list/canvas.cc | 9 ++- .../backend/gles/capabilities_gles.cc | 6 +- .../renderer/backend/gles/proc_table_gles.h | 1 + .../renderer/backend/gles/render_pass_gles.cc | 59 +++++++++++++++++-- .../renderer/backend/gles/texture_gles.cc | 50 +++++++++++----- 5 files changed, 102 insertions(+), 23 deletions(-) diff --git a/engine/src/flutter/impeller/display_list/canvas.cc b/engine/src/flutter/impeller/display_list/canvas.cc index eae343d2fc..da15d9d84b 100644 --- a/engine/src/flutter/impeller/display_list/canvas.cc +++ b/engine/src/flutter/impeller/display_list/canvas.cc @@ -1667,10 +1667,15 @@ bool Canvas::BlitToOnscreen() { auto offscreen_target = render_passes_.back() .inline_pass_context->GetPassTarget() .GetRenderTarget(); - + // Unlike other backends the blit to restore the onscreen fails for GLES + // if the src is a multisample framebuffer, even if we specifically target + // the non-multisampled resolve of the dst. + bool is_gles_and_must_skip_blit = renderer_.GetContext()->GetBackendType() == + Context::BackendType::kOpenGLES; if (renderer_.GetContext() ->GetCapabilities() - ->SupportsTextureToTextureBlits()) { + ->SupportsTextureToTextureBlits() && + !is_gles_and_must_skip_blit) { auto blit_pass = command_buffer->CreateBlitPass(); blit_pass->AddCopy(offscreen_target.GetRenderTargetTexture(), render_target_.GetRenderTargetTexture()); diff --git a/engine/src/flutter/impeller/renderer/backend/gles/capabilities_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/capabilities_gles.cc index 2f3c488285..e6851048d8 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/capabilities_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/capabilities_gles.cc @@ -125,7 +125,11 @@ CapabilitiesGLES::CapabilitiesGLES(const ProcTableGLES& gl) { // We hard-code 4x MSAA, so let's make sure it's supported. GLint value = 0; - gl.GetIntegerv(GL_MAX_SAMPLES_EXT, &value); + gl.GetIntegerv(GL_MAX_SAMPLES, &value); + supports_offscreen_msaa_ = value >= 4; + } else if (desc->GetGlVersion().major_version >= 3 && desc->IsES()) { + GLint value = 0; + gl.GetIntegerv(GL_MAX_SAMPLES, &value); supports_offscreen_msaa_ = value >= 4; } is_es_ = desc->IsES(); diff --git a/engine/src/flutter/impeller/renderer/backend/gles/proc_table_gles.h b/engine/src/flutter/impeller/renderer/backend/gles/proc_table_gles.h index f17c42949d..22b80ff80b 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/proc_table_gles.h +++ b/engine/src/flutter/impeller/renderer/backend/gles/proc_table_gles.h @@ -242,6 +242,7 @@ void(glDepthRange)(GLdouble n, GLdouble f); PROC(FenceSync); \ PROC(DeleteSync); \ PROC(WaitSync); \ + PROC(RenderbufferStorageMultisample) \ PROC(BlitFramebuffer); #define FOR_EACH_IMPELLER_EXT_PROC(PROC) \ diff --git a/engine/src/flutter/impeller/renderer/backend/gles/render_pass_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/render_pass_gles.cc index 32228d3d87..93c7619142 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/render_pass_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/render_pass_gles.cc @@ -7,7 +7,6 @@ #include #include "GLES3/gl3.h" -#include "flutter/fml/trace_event.h" #include "fml/closure.h" #include "fml/logging.h" #include "impeller/base/validation.h" @@ -127,6 +126,7 @@ struct RenderPassData { Scalar clear_depth = 1.0; std::shared_ptr color_attachment; + std::shared_ptr resolve_attachment; std::shared_ptr depth_attachment; std::shared_ptr stencil_attachment; @@ -191,8 +191,6 @@ void RenderPassGLES::ResetGLState(const ProcTableGLES& gl) { const ReactorGLES& reactor, const std::vector& commands, const std::shared_ptr& tracer) { - TRACE_EVENT0("impeller", "RenderPassGLES::EncodeCommandsInReactor"); - const auto& gl = reactor.GetProcTable(); #ifdef IMPELLER_DEBUG tracer->MarkFrameStart(gl); @@ -490,6 +488,55 @@ void RenderPassGLES::ResetGLState(const ProcTableGLES& gl) { } } + if (pass_data.resolve_attachment && + !gl.GetCapabilities()->SupportsImplicitResolvingMSAA() && + !is_default_fbo) { + FML_DCHECK(pass_data.resolve_attachment != pass_data.color_attachment); + // Perform multisample resolve via blit. + // Create and bind a resolve FBO. + GLuint resolve_fbo; + gl.GenFramebuffers(1u, &resolve_fbo); + gl.BindFramebuffer(GL_FRAMEBUFFER, resolve_fbo); + + if (!TextureGLES::Cast(*pass_data.resolve_attachment) + .SetAsFramebufferAttachment( + GL_FRAMEBUFFER, TextureGLES::AttachmentType::kColor0)) { + return false; + } + + auto status = gl.CheckFramebufferStatus(GL_FRAMEBUFFER); + if (gl.CheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { + VALIDATION_LOG << "Could not create a complete frambuffer: " + << DebugToFramebufferError(status); + return false; + } + + // Bind MSAA renderbuffer to read framebuffer. + gl.BindFramebuffer(GL_READ_FRAMEBUFFER, fbo); + gl.BindFramebuffer(GL_DRAW_FRAMEBUFFER, resolve_fbo); + + RenderPassGLES::ResetGLState(gl); + auto size = pass_data.color_attachment->GetSize(); + + gl.BlitFramebuffer(0, // srcX0 + 0, // srcY0 + size.width, // srcX1 + size.height, // srcY1 + 0, // dstX0 + 0, // dstY0 + size.width, // dstX1 + size.height, // dstY1 + GL_COLOR_BUFFER_BIT, // mask + GL_NEAREST // filter + ); + + gl.BindFramebuffer(GL_DRAW_FRAMEBUFFER, GL_NONE); + gl.BindFramebuffer(GL_READ_FRAMEBUFFER, GL_NONE); + gl.DeleteFramebuffers(1u, &resolve_fbo); + // Rebind the original FBO so that we can discard it below. + gl.BindFramebuffer(GL_FRAMEBUFFER, fbo); + } + if (gl.DiscardFramebufferEXT.IsAvailable()) { std::vector attachments; @@ -547,6 +594,7 @@ bool RenderPassGLES::OnEncodeCommands(const Context& context) const { /// Setup color data. /// pass_data->color_attachment = color0.texture; + pass_data->resolve_attachment = color0.resolve_texture; pass_data->clear_color = color0.clear_color; pass_data->clear_color_attachment = CanClearAttachment(color0.load_action); pass_data->discard_color_attachment = @@ -556,8 +604,9 @@ bool RenderPassGLES::OnEncodeCommands(const Context& context) const { // resolved when we bind the texture to the framebuffer. We don't need to // discard the attachment when we are done. if (color0.resolve_texture) { - FML_DCHECK(context.GetCapabilities()->SupportsImplicitResolvingMSAA()); - pass_data->discard_color_attachment = false; + pass_data->discard_color_attachment = + pass_data->discard_color_attachment && + !context.GetCapabilities()->SupportsImplicitResolvingMSAA(); } //---------------------------------------------------------------------------- diff --git a/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc index 1b4b47f74b..6a911aebf0 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc @@ -45,7 +45,8 @@ static bool IsDepthStencilFormat(PixelFormat format) { } static TextureGLES::Type GetTextureTypeFromDescriptor( - const TextureDescriptor& desc) { + const TextureDescriptor& desc, + bool supports_implict_msaa) { const auto usage = static_cast(desc.usage); const auto render_target = TextureUsage::kRenderTarget; const auto is_msaa = desc.sample_count == SampleCount::kCount4; @@ -53,7 +54,9 @@ static TextureGLES::Type GetTextureTypeFromDescriptor( return is_msaa ? TextureGLES::Type::kRenderBufferMultisampled : TextureGLES::Type::kRenderBuffer; } - return is_msaa ? TextureGLES::Type::kTextureMultisampled + return is_msaa ? (supports_implict_msaa + ? TextureGLES::Type::kTextureMultisampled + : TextureGLES::Type::kRenderBufferMultisampled) : TextureGLES::Type::kTexture; } @@ -190,7 +193,11 @@ TextureGLES::TextureGLES(std::shared_ptr reactor, std::optional external_handle) : Texture(desc), reactor_(std::move(reactor)), - type_(GetTextureTypeFromDescriptor(GetTextureDescriptor())), + type_( + GetTextureTypeFromDescriptor(GetTextureDescriptor(), + reactor_->GetProcTable() + .GetCapabilities() + ->SupportsImplicitResolvingMSAA())), handle_(external_handle.has_value() ? external_handle.value() : reactor_->CreateHandle(ToHandleType(type_))), @@ -362,7 +369,7 @@ static std::optional ToRenderBufferFormat(PixelFormat format) { switch (format) { case PixelFormat::kB8G8R8A8UNormInt: case PixelFormat::kR8G8B8A8UNormInt: - return GL_RGBA4; + return GL_RGBA8; case PixelFormat::kR32G32B32A32Float: return GL_RGBA32F; case PixelFormat::kR16G16B16A16Float: @@ -445,19 +452,32 @@ void TextureGLES::InitializeContentsIfNecessary() const { { TRACE_EVENT0("impeller", "RenderBufferStorageInitialization"); if (type_ == Type::kRenderBufferMultisampled) { - gl.RenderbufferStorageMultisampleEXT( - GL_RENDERBUFFER, // target - 4, // samples - render_buffer_format.value(), // internal format - size.width, // width - size.height // height - ); + // BEWARE: these functions are not at all equivalent! the extensions + // are from EXT_multisampled_render_to_texture and cannot be used + // with regular GLES 3.0 multisampled renderbuffers/textures. + if (gl.GetCapabilities()->SupportsImplicitResolvingMSAA()) { + gl.RenderbufferStorageMultisampleEXT( + /*target=*/GL_RENDERBUFFER, // + /*samples=*/4, // + /*internal_format=*/render_buffer_format.value(), // + /*width=*/size.width, // + /*height=*/size.height // + ); + } else { + gl.RenderbufferStorageMultisample( + /*target=*/GL_RENDERBUFFER, // + /*samples=*/4, // + /*internal_format=*/render_buffer_format.value(), // + /*width=*/size.width, // + /*height=*/size.height // + ); + } } else { gl.RenderbufferStorage( - GL_RENDERBUFFER, // target - render_buffer_format.value(), // internal format - size.width, // width - size.height // height + /*target=*/GL_RENDERBUFFER, // + /*internal_format=*/render_buffer_format.value(), // + /*width=*/size.width, // + /*height=*/size.height // ); } }