diff --git a/engine/src/flutter/impeller/compositor/render_pass.h b/engine/src/flutter/impeller/compositor/render_pass.h index 877746110e..ef31b70865 100644 --- a/engine/src/flutter/impeller/compositor/render_pass.h +++ b/engine/src/flutter/impeller/compositor/render_pass.h @@ -15,6 +15,7 @@ #include "impeller/compositor/host_buffer.h" #include "impeller/compositor/texture.h" #include "impeller/geometry/color.h" +#include "impeller/geometry/size.h" namespace impeller { @@ -48,6 +49,8 @@ class RenderPassDescriptor { bool HasColorAttachment(size_t index) const; + std::optional GetColorAttachmentSize(size_t index) const; + RenderPassDescriptor& SetColorAttachment(ColorRenderPassAttachment attachment, size_t index); diff --git a/engine/src/flutter/impeller/compositor/render_pass.mm b/engine/src/flutter/impeller/compositor/render_pass.mm index b9ef2678f9..7257863d47 100644 --- a/engine/src/flutter/impeller/compositor/render_pass.mm +++ b/engine/src/flutter/impeller/compositor/render_pass.mm @@ -24,6 +24,17 @@ bool RenderPassDescriptor::HasColorAttachment(size_t index) const { return false; } +std::optional RenderPassDescriptor::GetColorAttachmentSize( + size_t index) const { + auto found = colors_.find(index); + + if (found == colors_.end()) { + return std::nullopt; + } + + return found->second.texture->GetSize(); +} + RenderPassDescriptor& RenderPassDescriptor::SetColorAttachment( ColorRenderPassAttachment attachment, size_t index) { diff --git a/engine/src/flutter/impeller/compositor/renderer.h b/engine/src/flutter/impeller/compositor/renderer.h index 8a03bdc1a9..85b1f9e813 100644 --- a/engine/src/flutter/impeller/compositor/renderer.h +++ b/engine/src/flutter/impeller/compositor/renderer.h @@ -32,7 +32,7 @@ class Renderer { virtual bool OnIsValid() const = 0; - virtual bool OnRender(RenderPass& pass) = 0; + virtual bool OnRender(const Surface& surface, RenderPass& pass) = 0; private: dispatch_semaphore_t frames_in_flight_sema_ = nullptr; diff --git a/engine/src/flutter/impeller/compositor/renderer.mm b/engine/src/flutter/impeller/compositor/renderer.mm index 20c0c4b42b..b5dfcf8290 100644 --- a/engine/src/flutter/impeller/compositor/renderer.mm +++ b/engine/src/flutter/impeller/compositor/renderer.mm @@ -49,7 +49,7 @@ bool Renderer::Render(const Surface& surface) { return false; } - if (!OnRender(*render_pass)) { + if (!OnRender(surface, *render_pass)) { return false; } diff --git a/engine/src/flutter/impeller/compositor/surface.h b/engine/src/flutter/impeller/compositor/surface.h index 0d91604be1..7a92188e80 100644 --- a/engine/src/flutter/impeller/compositor/surface.h +++ b/engine/src/flutter/impeller/compositor/surface.h @@ -22,6 +22,8 @@ class Surface { ~Surface(); + const Size& GetSize() const; + bool Present() const; bool IsValid() const; @@ -31,6 +33,7 @@ class Surface { private: RenderPassDescriptor desc_; std::function present_callback_; + Size size_; bool is_valid_ = false; diff --git a/engine/src/flutter/impeller/compositor/surface.mm b/engine/src/flutter/impeller/compositor/surface.mm index dea5e3327a..40f57d9e00 100644 --- a/engine/src/flutter/impeller/compositor/surface.mm +++ b/engine/src/flutter/impeller/compositor/surface.mm @@ -11,14 +11,21 @@ namespace impeller { Surface::Surface(RenderPassDescriptor desc, std::function present_callback) : desc_(std::move(desc)), present_callback_(present_callback) { - if (!desc_.HasColorAttachment(0)) { + if (auto size = desc_.GetColorAttachmentSize(0u); size.has_value()) { + size_ = size.value(); + } else { return; } + is_valid_ = true; } Surface::~Surface() = default; +const Size& Surface::GetSize() const { + return size_; +} + bool Surface::Present() const { auto callback = present_callback_; diff --git a/engine/src/flutter/impeller/entity/entity_renderer.h b/engine/src/flutter/impeller/entity/entity_renderer.h index ac1b7566db..0d00e6ffe9 100644 --- a/engine/src/flutter/impeller/entity/entity_renderer.h +++ b/engine/src/flutter/impeller/entity/entity_renderer.h @@ -26,7 +26,7 @@ class EntityRenderer final : public Renderer { bool OnIsValid() const override; // |Renderer| - bool OnRender(RenderPass& pass) override; + bool OnRender(const Surface& surface, RenderPass& pass) override; FML_DISALLOW_COPY_AND_ASSIGN(EntityRenderer); }; diff --git a/engine/src/flutter/impeller/entity/entity_renderer.mm b/engine/src/flutter/impeller/entity/entity_renderer.mm index bd23b9fa3d..663dd1670a 100644 --- a/engine/src/flutter/impeller/entity/entity_renderer.mm +++ b/engine/src/flutter/impeller/entity/entity_renderer.mm @@ -4,7 +4,9 @@ #include "impeller/entity/entity_renderer.h" +#include "flutter/fml/time/time_point.h" #include "impeller/compositor/command.h" +#include "impeller/compositor/surface.h" #include "impeller/compositor/vertex_buffer_builder.h" #include "impeller/primitives/box.frag.h" #include "impeller/primitives/box.vert.h" @@ -36,18 +38,20 @@ bool EntityRenderer::OnIsValid() const { return is_valid_; } -bool EntityRenderer::OnRender(RenderPass& pass) { +bool EntityRenderer::OnRender(const Surface& surface, RenderPass& pass) { pass.SetLabel("EntityRenderer Render Pass"); shader::BoxVertexInfo::UniformBuffer uniforms; uniforms.color = Color::Blue(); - uniforms.mvp = - Matrix::MakeOrthographic({800, 600}).Scale({200.0, 200.0, 1.0}); + + uniforms.mvp = Matrix::MakeOrthographic(surface.GetSize()); + VertexBufferBuilder vertex_builder; + vertex_builder.AddVertices({ - {0, 0.5, 0.0}, // - {0.5, -0.5, 0.0}, // - {-0.5, -0.5, 0.0}, // + {0, 0, 0.0}, // + {800, 0.0, 0.0}, // + {0.0, 600, 0.0}, // }); Command cmd; diff --git a/engine/src/flutter/impeller/geometry/matrix.cc b/engine/src/flutter/impeller/geometry/matrix.cc index fd24147d84..662987053c 100644 --- a/engine/src/flutter/impeller/geometry/matrix.cc +++ b/engine/src/flutter/impeller/geometry/matrix.cc @@ -85,56 +85,6 @@ Matrix::Matrix(const Decomposition& d) : Matrix() { } } -Matrix Matrix::MakeOrthographic(Scalar left, - Scalar right, - Scalar bottom, - Scalar top, - Scalar nearZ, - Scalar farZ) { - Scalar ral = right + left; - Scalar rsl = right - left; - Scalar tab = top + bottom; - Scalar tsb = top - bottom; - // Scalar fan = farZ + nearZ; - Scalar fsn = farZ - nearZ; - - // clang-format off - return Matrix(2.0 / rsl, 0.0, 0.0, 0.0, - 0.0, 2.0 / tsb, 0.0, 0.0, - 0.0, 0.0, -1.0 / fsn, 0.0, - -ral / rsl, -tab / tsb, -nearZ / fsn, 1.0); - // clang-format on -} - -Matrix Matrix::MakeOrthographic(const Size& size) { - return Matrix::MakeOrthographic(0, size.width, size.height, 0, 0.0, 1.0); -} - -Matrix Matrix::MakePerspective(Scalar fov, - Scalar aspect, - Scalar nearZ, - Scalar farZ) { - Scalar cotan = 1.0 / tan(fov / 2.0); - - return Matrix(cotan / aspect, 0.0, 0.0, 0.0, // - 0.0, cotan, 0.0, 0.0, // - 0.0, 0.0, (farZ + nearZ) / (nearZ - farZ), -1.0, // - 0.0, 0.0, (2.0 * farZ * nearZ) / (nearZ - farZ), 0.0 // - ); -} - -Matrix Matrix::MakeLookAt(const Vector3& eye, - const Vector3& center, - const Vector3& up) { - auto n = (eye - center).Normalize(); - auto u = (up.Cross(n)).Normalize(); - auto v = n.Cross(u); - return {u.x, v.x, n.x, 0.0, // - u.y, v.y, n.y, 0.0, // - u.z, v.z, n.z, 0.0, // - (-u).Dot(eye), (-v).Dot(eye), (-n).Dot(eye), 1.0}; -} - Matrix Matrix::operator+(const Matrix& o) const { return Matrix( m[0] + o.m[0], m[1] + o.m[1], m[2] + o.m[2], m[3] + o.m[3], // diff --git a/engine/src/flutter/impeller/geometry/matrix.h b/engine/src/flutter/impeller/geometry/matrix.h index 62dadf76b9..c2c5310acb 100644 --- a/engine/src/flutter/impeller/geometry/matrix.h +++ b/engine/src/flutter/impeller/geometry/matrix.h @@ -20,14 +20,14 @@ namespace impeller { /// @brief A 4x4 matrix using column-major storage. /// /// Utility methods that need to make assumptions about normalized -/// device coordinates use the following convention: +/// device coordinates must use the following convention: /// * Left-handed coordinate system. Positive rotation is /// clockwise about axis of rotation. /// * Lower left corner is -1.0, -1.0. /// * Upper left corner is 1.0, 1.0. /// * Visible z-space is from 0.0 to 1.0. -/// * NOTE: This is not the same as OpenGL conventions! -/// * Center is at (0.0, 0.0, 0.5). +/// * This is NOT the same as OpenGL! Be careful. +/// * NDC origin is at (0.0, 0.0, 0.5). struct Matrix { union { Scalar m[16]; @@ -167,34 +167,6 @@ struct Matrix { // clang-format on } - static Matrix MakeOrthographic(Scalar left, - Scalar right, - Scalar bottom, - Scalar top, - Scalar nearZ, - Scalar farZ); - - static Matrix MakeOrthographic(const Size& size); - - /** - * Specify a viewing frustum in the worlds coordinate system. - * - * @param fov angle of the field of view (in radians). - * @param aspect aspect ratio. - * @param nearZ near clipping plane. - * @param farZ far clipping plane. - * - * @return the perspective projection matrix. - */ - static Matrix MakePerspective(Scalar fov, - Scalar aspect, - Scalar nearZ, - Scalar farZ); - - static Matrix MakeLookAt(const Vector3& eye, - const Vector3& center, - const Vector3& up); - constexpr Matrix Translate(const Vector3& t) const { // clang-format off return Matrix(m[0], m[1], m[2], m[3], @@ -296,6 +268,13 @@ struct Matrix { Matrix operator*(const Matrix& m) const { return Multiply(m); } Matrix operator+(const Matrix& m) const; + + static constexpr Matrix MakeOrthographic(const Size& size) { + // Per assumptions about NDC documented above. + const auto scale = MakeScale({1.0f / size.width, -1.0f / size.height, 1.0}); + const auto translate = MakeTranslation({-1.0, 1.0, 0.5}); + return translate * scale; + } }; static_assert(sizeof(struct Matrix) == sizeof(Scalar) * 16,