diff --git a/engine/src/flutter/lib/gpu/context.cc b/engine/src/flutter/lib/gpu/context.cc index c0c3345ce8..7204ac443c 100644 --- a/engine/src/flutter/lib/gpu/context.cc +++ b/engine/src/flutter/lib/gpu/context.cc @@ -10,11 +10,18 @@ #include "flutter/lib/ui/ui_dart_state.h" #include "fml/make_copyable.h" #include "impeller/core/platform.h" +#include "impeller/renderer/context.h" #include "tonic/converter/dart_converter.h" namespace flutter { namespace gpu { +bool SupportsNormalOffscreenMSAA(const impeller::Context& context) { + auto& capabilities = context.GetCapabilities(); + return capabilities->SupportsOffscreenMSAA() && + !capabilities->SupportsImplicitResolvingMSAA(); +} + IMPLEMENT_WRAPPERTYPEINFO(flutter_gpu, Context); std::shared_ptr Context::default_context_; @@ -111,3 +118,8 @@ extern int InternalFlutterGpu_Context_GetMinimumUniformByteAlignment( flutter::gpu::Context* wrapper) { return impeller::DefaultUniformAlignment(); } + +extern bool InternalFlutterGpu_Context_GetSupportsOffscreenMSAA( + flutter::gpu::Context* wrapper) { + return flutter::gpu::SupportsNormalOffscreenMSAA(*wrapper->GetContext()); +} diff --git a/engine/src/flutter/lib/gpu/context.h b/engine/src/flutter/lib/gpu/context.h index b482ae4799..4dbabcb5e4 100644 --- a/engine/src/flutter/lib/gpu/context.h +++ b/engine/src/flutter/lib/gpu/context.h @@ -13,6 +13,8 @@ namespace flutter { namespace gpu { +bool SupportsNormalOffscreenMSAA(const impeller::Context& context); + class Context : public RefCountedDartWrappable { DEFINE_WRAPPERTYPEINFO(); FML_FRIEND_MAKE_REF_COUNTED(Context); @@ -74,6 +76,10 @@ FLUTTER_GPU_EXPORT extern int InternalFlutterGpu_Context_GetMinimumUniformByteAlignment( flutter::gpu::Context* wrapper); +FLUTTER_GPU_EXPORT +extern bool InternalFlutterGpu_Context_GetSupportsOffscreenMSAA( + flutter::gpu::Context* wrapper); + } // extern "C" #endif // FLUTTER_LIB_GPU_CONTEXT_H_ diff --git a/engine/src/flutter/lib/gpu/lib/src/command_buffer.dart b/engine/src/flutter/lib/gpu/lib/src/command_buffer.dart index 09cae10b8f..20c0137ce6 100644 --- a/engine/src/flutter/lib/gpu/lib/src/command_buffer.dart +++ b/engine/src/flutter/lib/gpu/lib/src/command_buffer.dart @@ -9,13 +9,15 @@ part of flutter_gpu; typedef CompletionCallback = void Function(bool success); base class CommandBuffer extends NativeFieldWrapperClass1 { + final GpuContext _gpuContext; + /// Creates a new CommandBuffer. - CommandBuffer._(GpuContext gpuContext) { - _initialize(gpuContext); + CommandBuffer._(this._gpuContext) { + _initialize(_gpuContext); } RenderPass createRenderPass(RenderTarget renderTarget) { - return RenderPass._(this, renderTarget); + return RenderPass._(_gpuContext, this, renderTarget); } void submit({CompletionCallback? completionCallback}) { diff --git a/engine/src/flutter/lib/gpu/lib/src/context.dart b/engine/src/flutter/lib/gpu/lib/src/context.dart index 217b387314..a0a0513468 100644 --- a/engine/src/flutter/lib/gpu/lib/src/context.dart +++ b/engine/src/flutter/lib/gpu/lib/src/context.dart @@ -46,6 +46,18 @@ base class GpuContext extends NativeFieldWrapperClass1 { return _getMinimumUniformByteAlignment(); } + /// Whether the backend supports multisample anti-aliasing for offscreen + /// color and stencil attachments. A subset of OpenGLES-only devices do not + /// support this functionality. + /// + /// Any texture created via [createTexture] is an offscreen texture. + /// There is currently no way to render directly against the "onscreen" + /// texture that the framework renders to, so all Flutter GPU textures are + /// "offscreen". + bool get doesSupportOffscreenMSAA { + return _getSupportsOffscreenMSAA(); + } + /// Allocates a new region of GPU-resident memory. /// /// The [storageMode] must be either [StorageMode.hostVisible] or @@ -134,6 +146,10 @@ base class GpuContext extends NativeFieldWrapperClass1 { @Native)>( symbol: 'InternalFlutterGpu_Context_GetMinimumUniformByteAlignment') external int _getMinimumUniformByteAlignment(); + + @Native)>( + symbol: 'InternalFlutterGpu_Context_GetSupportsOffscreenMSAA') + external bool _getSupportsOffscreenMSAA(); } /// The default graphics context. diff --git a/engine/src/flutter/lib/gpu/lib/src/render_pass.dart b/engine/src/flutter/lib/gpu/lib/src/render_pass.dart index 95d7b5f317..02e373c328 100644 --- a/engine/src/flutter/lib/gpu/lib/src/render_pass.dart +++ b/engine/src/flutter/lib/gpu/lib/src/render_pass.dart @@ -21,6 +21,37 @@ base class ColorAttachment { Texture texture; Texture? resolveTexture; + + void _validate() { + if (resolveTexture != null) { + if (resolveTexture!.format != texture.format) { + throw Exception( + "ColorAttachment MSAA resolve texture must have the same format as the texture"); + } + if (resolveTexture!.width != texture.width || + resolveTexture!.height != texture.height) { + throw Exception( + "ColorAttachment MSAA resolve texture must have the same dimensions as the texture"); + } + if (resolveTexture!.sampleCount != 1) { + throw Exception( + "ColorAttachment MSAA resolve texture must have a sample count of 1"); + } + if (texture.sampleCount <= 1) { + throw Exception( + "ColorAttachment must have a sample count greater than 1 when a MSAA resolve texture is set"); + } + if (storeAction != StoreAction.multisampleResolve && + storeAction != StoreAction.storeAndMultisampleResolve) { + throw Exception( + "ColorAttachment StoreAction must be multisampleResolve or storeAndMultisampleResolve when a resolve texture is set"); + } + if (resolveTexture!.storageMode == StorageMode.deviceTransient) { + throw Exception( + "ColorAttachment MSAA resolve texture must not have a storage mode of deviceTransient"); + } + } + } } base class DepthStencilAttachment { @@ -43,6 +74,19 @@ base class DepthStencilAttachment { int stencilClearValue; Texture texture; + + void _validate() { + if (texture.storageMode == StorageMode.deviceTransient) { + if (depthLoadAction == LoadAction.load) { + throw Exception( + "DepthStencilAttachment depthLoadAction must not be load when the texture has a storage mode of deviceTransient"); + } + if (stencilLoadAction == LoadAction.load) { + throw Exception( + "DepthStencilAttachment stencilLoadAction must not be load when the texture has a storage mode of deviceTransient"); + } + } + } } base class StencilConfig { @@ -117,17 +161,33 @@ base class RenderTarget { colorAttachments: [colorAttachment], depthStencilAttachment: depthStencilAttachment); + _validate() { + for (final color in colorAttachments) { + color._validate(); + } + if (depthStencilAttachment != null) { + depthStencilAttachment!._validate(); + } + } + final List colorAttachments; final DepthStencilAttachment? depthStencilAttachment; } base class RenderPass extends NativeFieldWrapperClass1 { /// Creates a new RenderPass. - RenderPass._(CommandBuffer commandBuffer, RenderTarget renderTarget) { + RenderPass._(GpuContext gpuContext, CommandBuffer commandBuffer, + RenderTarget renderTarget) { + assert(() { + renderTarget._validate(); + return true; + }()); + _initialize(); String? error; for (final (index, color) in renderTarget.colorAttachments.indexed) { error = _setColorAttachment( + gpuContext, index, color.loadAction.index, color.storeAction.index, @@ -287,6 +347,7 @@ base class RenderPass extends NativeFieldWrapperClass1 { @Native< Handle Function( + Pointer, Pointer, Int, Int, @@ -298,6 +359,7 @@ base class RenderPass extends NativeFieldWrapperClass1 { Pointer, Handle)>(symbol: 'InternalFlutterGpu_RenderPass_SetColorAttachment') external String? _setColorAttachment( + GpuContext context, int colorAttachmentIndex, int loadAction, int storeAction, @@ -432,7 +494,7 @@ base class RenderPass extends NativeFieldWrapperClass1 { symbol: 'InternalFlutterGpu_RenderPass_SetPrimitiveType') external void _setPrimitiveType(int primitiveType); - @Native, Int)>( + @Native, Int)>( symbol: 'InternalFlutterGpu_RenderPass_SetWindingOrder') external void _setWindingOrder(int windingOrder); diff --git a/engine/src/flutter/lib/gpu/render_pass.cc b/engine/src/flutter/lib/gpu/render_pass.cc index 70d68d0e78..fda1dbbd23 100644 --- a/engine/src/flutter/lib/gpu/render_pass.cc +++ b/engine/src/flutter/lib/gpu/render_pass.cc @@ -20,6 +20,7 @@ #include "impeller/renderer/pipeline.h" #include "impeller/renderer/pipeline_descriptor.h" #include "impeller/renderer/pipeline_library.h" +#include "lib/gpu/context.h" #include "lib/ui/ui_dart_state.h" #include "tonic/converter/dart_converter.h" @@ -222,6 +223,7 @@ void InternalFlutterGpu_RenderPass_Initialize(Dart_Handle wrapper) { Dart_Handle InternalFlutterGpu_RenderPass_SetColorAttachment( flutter::gpu::RenderPass* wrapper, + flutter::gpu::Context* context, int color_attachment_index, int load_action, int store_action, @@ -242,6 +244,14 @@ Dart_Handle InternalFlutterGpu_RenderPass_SetColorAttachment( tonic::DartConverter::FromDart( resolve_texture_wrapper); desc.resolve_texture = resolve_texture->GetTexture(); + + // If the backend doesn't support normal MSAA, gracefully fallback to + // rendering without MSAA. + if (!flutter::gpu::SupportsNormalOffscreenMSAA(*context->GetContext())) { + desc.texture = desc.resolve_texture; + desc.resolve_texture = nullptr; + desc.store_action = impeller::StoreAction::kStore; + } } wrapper->GetRenderTarget().SetColorAttachment(desc, color_attachment_index); return Dart_Null(); diff --git a/engine/src/flutter/lib/gpu/render_pass.h b/engine/src/flutter/lib/gpu/render_pass.h index da9840d626..865e375ac7 100644 --- a/engine/src/flutter/lib/gpu/render_pass.h +++ b/engine/src/flutter/lib/gpu/render_pass.h @@ -118,6 +118,7 @@ extern void InternalFlutterGpu_RenderPass_Initialize(Dart_Handle wrapper); FLUTTER_GPU_EXPORT extern Dart_Handle InternalFlutterGpu_RenderPass_SetColorAttachment( flutter::gpu::RenderPass* wrapper, + flutter::gpu::Context* context, int color_attachment_index, int load_action, int store_action, diff --git a/engine/src/flutter/testing/dart/gpu_test.dart b/engine/src/flutter/testing/dart/gpu_test.dart index 508e7e08d9..47c0c7d1ea 100644 --- a/engine/src/flutter/testing/dart/gpu_test.dart +++ b/engine/src/flutter/testing/dart/gpu_test.dart @@ -60,6 +60,7 @@ RenderPassState createSimpleRenderPass({Vector4? clearColor}) { final gpu.Texture? depthStencilTexture = gpu.gpuContext.createTexture( gpu.StorageMode.deviceTransient, 100, 100, format: gpu.gpuContext.defaultDepthStencilFormat); + assert(depthStencilTexture != null); final gpu.CommandBuffer commandBuffer = gpu.gpuContext.createCommandBuffer(); @@ -74,6 +75,66 @@ RenderPassState createSimpleRenderPass({Vector4? clearColor}) { return RenderPassState(renderTexture, commandBuffer, renderPass); } +RenderPassState createSimpleRenderPassWithMSAA() { + // Create transient MSAA attachments, which will live entirely in tile memory + // for most GPUs. + + final gpu.Texture? renderTexture = gpu.gpuContext.createTexture( + gpu.StorageMode.deviceTransient, 100, 100, + format: gpu.gpuContext.defaultColorFormat, sampleCount: 4); + assert(renderTexture != null); + + final gpu.Texture? depthStencilTexture = gpu.gpuContext.createTexture( + gpu.StorageMode.deviceTransient, 100, 100, + format: gpu.gpuContext.defaultDepthStencilFormat, sampleCount: 4); + assert(depthStencilTexture != null); + + // Create the single-sample resolve texture that live in DRAM and will be + // drawn to the screen. + + final gpu.Texture? resolveTexture = gpu.gpuContext.createTexture( + gpu.StorageMode.devicePrivate, 100, 100, + format: gpu.gpuContext.defaultColorFormat); + assert(resolveTexture != null); + + final gpu.CommandBuffer commandBuffer = gpu.gpuContext.createCommandBuffer(); + + final gpu.RenderTarget renderTarget = gpu.RenderTarget.singleColor( + gpu.ColorAttachment( + texture: renderTexture!, + resolveTexture: resolveTexture, + storeAction: gpu.StoreAction.multisampleResolve), + depthStencilAttachment: + gpu.DepthStencilAttachment(texture: depthStencilTexture!)); + + final gpu.RenderPass renderPass = + commandBuffer.createRenderPass(renderTarget); + + return RenderPassState(resolveTexture!, commandBuffer, renderPass); +} + +void drawTriangle(RenderPassState state, Vector4 color) { + final gpu.RenderPipeline pipeline = createUnlitRenderPipeline(); + + state.renderPass.bindPipeline(pipeline); + + final gpu.HostBuffer transients = gpu.gpuContext.createHostBuffer(); + final gpu.BufferView vertices = transients.emplace(float32([ + -0.5, 0.5, // + 0.0, -0.5, // + 0.5, 0.5, // + ])); + final gpu.BufferView vertInfoData = + transients.emplace(unlitUBO(Matrix4.identity(), color)); + state.renderPass.bindVertexBuffer(vertices, 3); + + final gpu.UniformSlot vertInfo = + pipeline.vertexShader.getUniformSlot('VertInfo'); + state.renderPass.bindUniform(vertInfo, vertInfoData); + + state.renderPass.draw(); +} + void main() async { final ImageComparer comparer = await ImageComparer.create(); @@ -337,34 +398,7 @@ void main() async { // Renders a green triangle pointing downwards. test('Can render triangle', () async { final state = createSimpleRenderPass(); - - final gpu.RenderPipeline pipeline = createUnlitRenderPipeline(); - state.renderPass.bindPipeline(pipeline); - - // Configure blending with defaults (just to test the bindings). - state.renderPass.setColorBlendEnable(true); - state.renderPass.setColorBlendEquation(gpu.ColorBlendEquation()); - - final gpu.HostBuffer transients = gpu.gpuContext.createHostBuffer(); - final gpu.BufferView vertices = transients.emplace(float32([ - -0.5, 0.5, // - 0.0, -0.5, // - 0.5, 0.5, // - ])); - final gpu.BufferView vertInfoData = transients.emplace(float32([ - 1, 0, 0, 0, // mvp - 0, 1, 0, 0, // mvp - 0, 0, 1, 0, // mvp - 0, 0, 0, 1, // mvp - 0, 1, 0, 1, // color - ])); - state.renderPass.bindVertexBuffer(vertices, 3); - - final gpu.UniformSlot vertInfo = - pipeline.vertexShader.getUniformSlot('VertInfo'); - state.renderPass.bindUniform(vertInfo, vertInfoData); - state.renderPass.draw(); - + drawTriangle(state, Colors.lime); state.commandBuffer.submit(); final ui.Image image = state.renderTexture.asImage(); @@ -408,9 +442,36 @@ void main() async { state.commandBuffer.submit(); final ui.Image image = state.renderTexture.asImage(); - await comparer.addGoldenImage(image, 'flutter_gpu_test_triangle_polygon_mode.png'); + await comparer.addGoldenImage( + image, 'flutter_gpu_test_triangle_polygon_mode.png'); }, skip: !impellerEnabled); + // Renders a green triangle pointing downwards, with 4xMSAA. + test('Can render triangle with MSAA', () async { + final state = createSimpleRenderPassWithMSAA(); + drawTriangle(state, Colors.lime); + state.commandBuffer.submit(); + + final ui.Image image = state.renderTexture.asImage(); + await comparer.addGoldenImage(image, 'flutter_gpu_test_triangle_msaa.png'); + }, skip: !(impellerEnabled && gpu.gpuContext.doesSupportOffscreenMSAA)); + + test( + 'Rendering with MSAA throws exception when offscreen MSAA is not supported', + () async { + try { + final state = createSimpleRenderPassWithMSAA(); + drawTriangle(state, Colors.lime); + state.commandBuffer.submit(); + fail('Exception not thrown when offscreen MSAA is not supported.'); + } catch (e) { + expect( + e.toString(), + contains( + 'The backend does not support multisample anti-aliasing for offscreen color and stencil attachments')); + } + }, skip: !(impellerEnabled && !gpu.gpuContext.doesSupportOffscreenMSAA)); + // Renders a hollow green triangle pointing downwards. test('Can render hollowed out triangle using stencil ops', () async { final state = createSimpleRenderPass(); @@ -548,13 +609,20 @@ void main() async { final gpu.HostBuffer transients = gpu.gpuContext.createHostBuffer(); final gpu.BufferView vertices = transients.emplace(float32([ - 1.0, 0.0, - 0.5, 0.8, - -0.5, 0.8, - -1.0, 0.0, - -0.5, -0.8, - 0.5, -0.8, - 1.0, 0.0 + 1.0, + 0.0, + 0.5, + 0.8, + -0.5, + 0.8, + -1.0, + 0.0, + -0.5, + -0.8, + 0.5, + -0.8, + 1.0, + 0.0 ])); final gpu.BufferView vertInfoData = transients.emplace(float32([ 1, 0, 0, 0, // mvp @@ -573,6 +641,7 @@ void main() async { state.commandBuffer.submit(); final ui.Image image = state.renderTexture.asImage(); - await comparer.addGoldenImage(image, 'flutter_gpu_test_hexgon_line_strip.png'); + await comparer.addGoldenImage( + image, 'flutter_gpu_test_hexgon_line_strip.png'); }, skip: !impellerEnabled); }