From 3fed10bf0d6d7504481654ae1ba44b88e9493fa6 Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Tue, 29 Oct 2024 15:52:21 -0700 Subject: [PATCH] [Flutter GPU] Fix MSAA sample size and HostBuffer alignment. (flutter/engine#56218) - Align HostBuffer emplacements properly (fix Metal validation failures). - Correctly set the pipeline sample size. - Throw an error when attempting to bind `deviceTransient` textures. --- .../src/flutter/lib/gpu/lib/src/buffer.dart | 6 ++++- .../flutter/lib/gpu/lib/src/render_pass.dart | 8 ++++++ engine/src/flutter/lib/gpu/render_pass.cc | 3 +++ engine/src/flutter/testing/dart/gpu_test.dart | 26 ++++++++++++++++++- 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/lib/gpu/lib/src/buffer.dart b/engine/src/flutter/lib/gpu/lib/src/buffer.dart index f568fe912f..fd3ae464f8 100644 --- a/engine/src/flutter/lib/gpu/lib/src/buffer.dart +++ b/engine/src/flutter/lib/gpu/lib/src/buffer.dart @@ -212,7 +212,11 @@ base class HostBuffer { offsetInBytes: 0, lengthInBytes: bytes.lengthInBytes); } - int padding = _offsetCursor % _gpuContext.minimumUniformByteAlignment; + int padding = _gpuContext.minimumUniformByteAlignment - + (_offsetCursor % _gpuContext.minimumUniformByteAlignment); + // If the padding is the full alignment size, then we're already aligned. + // So reset the padding to zero. + padding %= _gpuContext.minimumUniformByteAlignment; if (_offsetCursor + padding >= blockLengthInBytes) { DeviceBuffer buffer = _allocateNewBlock(blockLengthInBytes); _buffers[_frameCursor].add(buffer); 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 4c514891b0..15389579b7 100644 --- a/engine/src/flutter/lib/gpu/lib/src/render_pass.dart +++ b/engine/src/flutter/lib/gpu/lib/src/render_pass.dart @@ -250,6 +250,14 @@ base class RenderPass extends NativeFieldWrapperClass1 { sampler = SamplerOptions(); } + assert(() { + if (texture.storageMode == StorageMode.deviceTransient) { + throw Exception( + "Textures with StorageMode.deviceTransient cannot be bound to a RenderPass"); + } + return true; + }()); + bool success = _bindTexture( slot.shader, slot.uniformName, diff --git a/engine/src/flutter/lib/gpu/render_pass.cc b/engine/src/flutter/lib/gpu/render_pass.cc index af987fd089..a9dc310ffc 100644 --- a/engine/src/flutter/lib/gpu/render_pass.cc +++ b/engine/src/flutter/lib/gpu/render_pass.cc @@ -102,6 +102,9 @@ std::shared_ptr> RenderPass::GetOrCreatePipeline() { // Infer the pipeline layout based on the shape of the RenderTarget. auto pipeline_desc = pipeline_descriptor_; + + pipeline_desc.SetSampleCount(render_target_.GetSampleCount()); + for (const auto& it : render_target_.GetColorAttachments()) { auto& color = GetColorAttachmentDescriptor(it.first); color.format = render_target_.GetRenderTargetPixelFormat(); diff --git a/engine/src/flutter/testing/dart/gpu_test.dart b/engine/src/flutter/testing/dart/gpu_test.dart index 605cf75734..c1d45183f9 100644 --- a/engine/src/flutter/testing/dart/gpu_test.dart +++ b/engine/src/flutter/testing/dart/gpu_test.dart @@ -171,7 +171,8 @@ void main() async { final gpu.BufferView view1 = hostBuffer .emplace(Int8List.fromList([0, 1, 2, 3]).buffer.asByteData()); - expect(view1.offsetInBytes, greaterThanOrEqualTo(4)); + expect(view1.offsetInBytes, + equals(gpu.gpuContext.minimumUniformByteAlignment)); expect(view1.lengthInBytes, 4); }, skip: !impellerEnabled); @@ -416,6 +417,29 @@ void main() async { } }, skip: !impellerEnabled); + test('RenderPass.bindTexture throws for deviceTransient Textures', () async { + final state = createSimpleRenderPass(); + + final gpu.RenderPipeline pipeline = createUnlitRenderPipeline(); + // Although this is a non-texture uniform slot, it'll work fine for the + // purposes of testing this error. + final gpu.UniformSlot vertInfo = + pipeline.vertexShader.getUniformSlot('VertInfo'); + + final gpu.Texture texture = gpu.gpuContext + .createTexture(gpu.StorageMode.deviceTransient, 100, 100)!; + + try { + state.renderPass.bindTexture(vertInfo, texture); + fail('Exception not thrown when binding a transient texture.'); + } catch (e) { + expect( + e.toString(), + contains( + 'Textures with StorageMode.deviceTransient cannot be bound to a RenderPass')); + } + }, skip: !impellerEnabled); + // Performs no draw calls. Just clears the render target to a solid green color. test('Can render clear color', () async { final state = createSimpleRenderPass(clearColor: Colors.lime);