From f3a1f81b6ca1baa66f1f9607195b6bdea087ced6 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Fri, 16 Jul 2021 12:08:44 -0700 Subject: [PATCH] Add test to check rendering to multiple primitives. --- engine/src/flutter/impeller/geometry/matrix.h | 7 +- .../primitives/primitives_unittests.mm | 85 ++++++++++++++++++- 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/engine/src/flutter/impeller/geometry/matrix.h b/engine/src/flutter/impeller/geometry/matrix.h index e90cdb594e..7be4168497 100644 --- a/engine/src/flutter/impeller/geometry/matrix.h +++ b/engine/src/flutter/impeller/geometry/matrix.h @@ -269,9 +269,12 @@ struct Matrix { Matrix operator+(const Matrix& m) const; - static constexpr Matrix MakeOrthographic(Scalar width, Scalar height) { + template + static constexpr Matrix MakeOrthographic(TSize size) { // Per assumptions about NDC documented above. - const auto scale = MakeScale({1.0f / width, -1.0f / height, 1.0}); + const auto scale = + MakeScale({1.0f / static_cast(size.width), + -1.0f / static_cast(size.height), 1.0}); const auto translate = MakeTranslation({-1.0, 1.0, 0.5}); return translate * scale; } diff --git a/engine/src/flutter/impeller/primitives/primitives_unittests.mm b/engine/src/flutter/impeller/primitives/primitives_unittests.mm index 8748a95d32..3c2d7eafa6 100644 --- a/engine/src/flutter/impeller/primitives/primitives_unittests.mm +++ b/engine/src/flutter/impeller/primitives/primitives_unittests.mm @@ -60,8 +60,7 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) { cmd.BindVertices(vertex_buffer); BoxVertexShader::UniformBuffer uniforms; - uniforms.mvp = Matrix::MakeOrthographic(surface.GetSize().width, - surface.GetSize().height); + uniforms.mvp = Matrix::MakeOrthographic(surface.GetSize()); BoxVertexShader::BindUniformBuffer( cmd, pass.GetTransientsBuffer().EmplaceUniform(uniforms)); @@ -82,7 +81,87 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) { } return true; }; - OpenPlaygroundHere(callback); + // OpenPlaygroundHere(callback); +} + +TEST_F(PrimitivesTest, CanRenderMultiplePrimitives) { + auto context = GetContext(); + ASSERT_TRUE(context); + using BoxPipelineBuilder = + PipelineBuilder; + auto desc = BoxPipelineBuilder::MakeDefaultPipelineDescriptor(*context); + auto box_pipeline = + context->GetPipelineLibrary()->GetRenderPipeline(std::move(desc)).get(); + ASSERT_TRUE(box_pipeline); + + // Vertex buffer. + VertexBufferBuilder vertex_builder; + vertex_builder.SetLabel("Box"); + vertex_builder.AddVertices({ + {{100, 100, 0.0}, {0.0, 0.0}}, // 1 + {{800, 100, 0.0}, {1.0, 0.0}}, // 2 + {{800, 800, 0.0}, {1.0, 1.0}}, // 3 + {{100, 100, 0.0}, {0.0, 0.0}}, // 1 + {{800, 800, 0.0}, {1.0, 1.0}}, // 3 + {{100, 800, 0.0}, {0.0, 1.0}}, // 4 + }); + auto vertex_buffer = + vertex_builder.CreateVertexBuffer(*context->GetPermanentsAllocator()); + ASSERT_TRUE(vertex_buffer); + + auto bridge = CreateTextureForFixture("bay_bridge.jpg"); + auto boston = CreateTextureForFixture("boston.jpg"); + ASSERT_TRUE(bridge && boston); + auto sampler = context->GetSamplerLibrary()->GetSampler({}); + ASSERT_TRUE(sampler); + + Renderer::RenderCallback callback = [&](const Surface& surface, + RenderPass& pass) { + Command cmd; + cmd.label = "Box"; + cmd.pipeline = box_pipeline; + + cmd.BindVertices(vertex_buffer); + + BoxFragmentShader::FrameInfo frame_info; + frame_info.current_time = fml::TimePoint::Now().ToEpochDelta().ToSecondsF(); + frame_info.cursor_position = GetCursorPosition(); + frame_info.window_size.x = GetWindowSize().width; + frame_info.window_size.y = GetWindowSize().height; + + BoxFragmentShader::BindFrameInfo( + cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info)); + BoxFragmentShader::BindContents1(cmd, boston, sampler); + BoxFragmentShader::BindContents2(cmd, bridge, sampler); + + cmd.primitive_type = PrimitiveType::kTriangle; + + { + BoxVertexShader::UniformBuffer uniforms; + uniforms.mvp = Matrix::MakeOrthographic(surface.GetSize()); + BoxVertexShader::BindUniformBuffer( + cmd, pass.GetTransientsBuffer().EmplaceUniform(uniforms)); + + if (!pass.RecordCommand(cmd)) { + return false; + } + } + + { + BoxVertexShader::UniformBuffer uniforms; + uniforms.mvp = Matrix::MakeOrthographic(surface.GetSize()) * + Matrix::MakeTranslation({100.0, 100.0, 0.0}); + BoxVertexShader::BindUniformBuffer( + cmd, pass.GetTransientsBuffer().EmplaceUniform(uniforms)); + + if (!pass.RecordCommand(cmd)) { + return false; + } + } + + return true; + }; + // OpenPlaygroundHere(callback); } } // namespace testing