From ddcd290be3d464e1b5f3d7ffd7a811388ef9e76e Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Sat, 3 Dec 2022 01:04:09 -0800 Subject: [PATCH 1/2] [Impeller Scene] Wire up camera --- impeller/scene/camera.cc | 7 +++---- impeller/scene/camera.h | 4 ++-- impeller/scene/scene.cc | 4 ++-- impeller/scene/scene_encoder.cc | 7 +++++-- impeller/scene/scene_encoder.h | 2 ++ impeller/scene/scene_entity.cc | 8 ++++---- impeller/scene/scene_entity.h | 6 +++--- impeller/scene/scene_unittests.cc | 11 ++++++----- impeller/scene/static_mesh_entity.cc | 3 +-- impeller/scene/static_mesh_entity.h | 2 +- 10 files changed, 29 insertions(+), 25 deletions(-) diff --git a/impeller/scene/camera.cc b/impeller/scene/camera.cc index b10673bd4899c..b4736177588f8 100644 --- a/impeller/scene/camera.cc +++ b/impeller/scene/camera.cc @@ -7,7 +7,7 @@ namespace impeller { namespace scene { -Camera Camera::MakePerspective(Scalar fov_y, Vector3 position) { +Camera Camera::MakePerspective(Radians fov_y, Vector3 position) { Camera camera; camera.fov_y_ = fov_y; camera.position_ = position; @@ -26,9 +26,8 @@ Matrix Camera::GetTransform(ISize target_size) const { return transform_.value(); } - transform_ = - Matrix::MakePerspective(Radians(fov_y_), target_size, z_near_, z_far_) * - Matrix::MakeLookAt(position_, target_, up_).Invert(); + transform_ = Matrix::MakePerspective(fov_y_, target_size, z_near_, z_far_) * + Matrix::MakeLookAt(position_, target_, up_); return transform_.value(); } diff --git a/impeller/scene/camera.h b/impeller/scene/camera.h index 0eb02376ef684..a8b2eb7b541d0 100644 --- a/impeller/scene/camera.h +++ b/impeller/scene/camera.h @@ -13,14 +13,14 @@ namespace scene { class Camera { public: - static Camera MakePerspective(Scalar fov_y, Vector3 position); + static Camera MakePerspective(Radians fov_y, Vector3 position); Camera LookAt(Vector3 target, Vector3 up = Vector3(0, -1, 0)) const; Matrix GetTransform(ISize target_size) const; private: - Scalar fov_y_ = 60; + Radians fov_y_ = Degrees(60); Vector3 position_ = Vector3(); Vector3 target_ = Vector3(0, 0, -1); Vector3 up_ = Vector3(0, -1, 0); diff --git a/impeller/scene/scene.cc b/impeller/scene/scene.cc index ee4fa8cf8f785..3dce509e1b824 100644 --- a/impeller/scene/scene.cc +++ b/impeller/scene/scene.cc @@ -26,7 +26,7 @@ bool Scene::Render(const RenderTarget& render_target, const Camera& camera) const { // Collect the render commands from the scene. SceneEncoder encoder; - if (!root_.Render(encoder, camera)) { + if (!root_.Render(encoder)) { FML_LOG(ERROR) << "Failed to render frame."; return false; } @@ -34,7 +34,7 @@ bool Scene::Render(const RenderTarget& render_target, // Encode the commands. std::shared_ptr command_buffer = - encoder.BuildSceneCommandBuffer(*scene_context_, render_target); + encoder.BuildSceneCommandBuffer(*scene_context_, camera, render_target); // TODO(bdero): Do post processing. diff --git a/impeller/scene/scene_encoder.cc b/impeller/scene/scene_encoder.cc index fbf7e4923d1e6..e4b4deec4ceb7 100644 --- a/impeller/scene/scene_encoder.cc +++ b/impeller/scene/scene_encoder.cc @@ -22,6 +22,7 @@ void SceneEncoder::Add(const SceneCommand& command) { } static void EncodeCommand(const SceneContext& scene_context, + const Camera& camera, RenderPass& render_pass, const SceneCommand& scene_command) { auto& host_buffer = render_pass.GetTransientsBuffer(); @@ -39,7 +40,8 @@ static void EncodeCommand(const SceneContext& scene_context, scene_command.material->BindToCommand(scene_context, host_buffer, cmd); GeometryVertexShader::VertInfo info; - info.mvp = scene_command.transform; + info.mvp = camera.GetTransform(render_pass.GetRenderTargetSize()) * + scene_command.transform; GeometryVertexShader::BindVertInfo(cmd, host_buffer.EmplaceUniform(info)); render_pass.AddCommand(std::move(cmd)); @@ -47,6 +49,7 @@ static void EncodeCommand(const SceneContext& scene_context, std::shared_ptr SceneEncoder::BuildSceneCommandBuffer( const SceneContext& scene_context, + const Camera& camera, const RenderTarget& render_target) const { auto command_buffer = scene_context.GetContext()->CreateCommandBuffer(); if (!command_buffer) { @@ -61,7 +64,7 @@ std::shared_ptr SceneEncoder::BuildSceneCommandBuffer( } for (auto& command : commands_) { - EncodeCommand(scene_context, *render_pass, command); + EncodeCommand(scene_context, camera, *render_pass, command); } if (!render_pass->EncodeCommands()) { diff --git a/impeller/scene/scene_encoder.h b/impeller/scene/scene_encoder.h index e09be574e0bd9..208e4f51a0208 100644 --- a/impeller/scene/scene_encoder.h +++ b/impeller/scene/scene_encoder.h @@ -10,6 +10,7 @@ #include "flutter/fml/macros.h" #include "impeller/renderer/command_buffer.h" +#include "impeller/scene/camera.h" #include "impeller/scene/geometry.h" #include "impeller/scene/material.h" @@ -34,6 +35,7 @@ class SceneEncoder { std::shared_ptr BuildSceneCommandBuffer( const SceneContext& scene_context, + const Camera& camera, const RenderTarget& render_target) const; std::vector commands_; diff --git a/impeller/scene/scene_entity.cc b/impeller/scene/scene_entity.cc index 4183849a9e865..52d7fcdd55a8c 100644 --- a/impeller/scene/scene_entity.cc +++ b/impeller/scene/scene_entity.cc @@ -56,17 +56,17 @@ bool SceneEntity::Add(const std::shared_ptr& child) { return true; } -bool SceneEntity::Render(SceneEncoder& encoder, const Camera& camera) const { - OnRender(encoder, camera); +bool SceneEntity::Render(SceneEncoder& encoder) const { + OnRender(encoder); for (auto& child : children_) { - if (!child->Render(encoder, camera)) { + if (!child->Render(encoder)) { return false; } } return true; } -bool SceneEntity::OnRender(SceneEncoder& encoder, const Camera& camera) const { +bool SceneEntity::OnRender(SceneEncoder& encoder) const { return true; } diff --git a/impeller/scene/scene_entity.h b/impeller/scene/scene_entity.h index 694d0cd3616fe..11b370b7d083b 100644 --- a/impeller/scene/scene_entity.h +++ b/impeller/scene/scene_entity.h @@ -34,13 +34,13 @@ class SceneEntity { bool Add(const std::shared_ptr& child); - bool Render(SceneEncoder& encoder, const Camera& camera) const; + bool Render(SceneEncoder& encoder) const; protected: - Matrix local_transform_; + Matrix local_transform_ = Matrix(); private: - virtual bool OnRender(SceneEncoder& encoder, const Camera& camera) const; + virtual bool OnRender(SceneEncoder& encoder) const; SceneEntity* parent_ = nullptr; std::vector> children_; diff --git a/impeller/scene/scene_unittests.cc b/impeller/scene/scene_unittests.cc index 9f2281cd1245e..8aa7d02641ddb 100644 --- a/impeller/scene/scene_unittests.cc +++ b/impeller/scene/scene_unittests.cc @@ -37,20 +37,21 @@ TEST_P(SceneTest, CuboidUnlit) { material->SetColor(Color::Red()); mesh->SetMaterial(std::move(material)); - Vector3 size(1, 2, 3); + Vector3 size(1, 1, 0); mesh->SetGeometry(Geometry::MakeCuboid(size)); - mesh->SetLocalTransform(Matrix::MakeTranslation(size / 2)); + mesh->SetLocalTransform(Matrix::MakeTranslation(-size / 2)); scene.Add(mesh); } + // Face towards the +Z direction (+X right, +Y up). auto camera = Camera::MakePerspective( - /* fov */ kPiOver4, - /* position */ {50, -30, 50}) + /* fov */ Radians(kPiOver4), + /* position */ {2, 2, -5}) .LookAt( /* target */ Vector3(), - /* up */ {0, -1, 0}); + /* up */ {0, 1, 0}); scene.Render(render_target, camera); return true; diff --git a/impeller/scene/static_mesh_entity.cc b/impeller/scene/static_mesh_entity.cc index e234e956eac43..53ca7fac1b9e6 100644 --- a/impeller/scene/static_mesh_entity.cc +++ b/impeller/scene/static_mesh_entity.cc @@ -24,8 +24,7 @@ void StaticMeshEntity::SetMaterial(std::shared_ptr material) { } // |SceneEntity| -bool StaticMeshEntity::OnRender(SceneEncoder& encoder, - const Camera& camera) const { +bool StaticMeshEntity::OnRender(SceneEncoder& encoder) const { SceneCommand command = { .label = "Static Mesh", .transform = GetGlobalTransform(), diff --git a/impeller/scene/static_mesh_entity.h b/impeller/scene/static_mesh_entity.h index 7a1df4543d973..77d3de8b79f6f 100644 --- a/impeller/scene/static_mesh_entity.h +++ b/impeller/scene/static_mesh_entity.h @@ -25,7 +25,7 @@ class StaticMeshEntity final : public SceneEntity { private: // |SceneEntity| - bool OnRender(SceneEncoder& encoder, const Camera& camera) const override; + bool OnRender(SceneEncoder& encoder) const override; std::shared_ptr material_; std::shared_ptr geometry_; From 8ef3329709e67e1907f85e4b68242a8cd9dabcb2 Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Sat, 3 Dec 2022 14:23:11 -0800 Subject: [PATCH 2/2] Resolve view transform once --- impeller/scene/scene_encoder.cc | 9 +++++---- impeller/scene/scene_entity.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/impeller/scene/scene_encoder.cc b/impeller/scene/scene_encoder.cc index e4b4deec4ceb7..c4a460bca7f4e 100644 --- a/impeller/scene/scene_encoder.cc +++ b/impeller/scene/scene_encoder.cc @@ -22,7 +22,7 @@ void SceneEncoder::Add(const SceneCommand& command) { } static void EncodeCommand(const SceneContext& scene_context, - const Camera& camera, + const Matrix& view_transform, RenderPass& render_pass, const SceneCommand& scene_command) { auto& host_buffer = render_pass.GetTransientsBuffer(); @@ -40,8 +40,7 @@ static void EncodeCommand(const SceneContext& scene_context, scene_command.material->BindToCommand(scene_context, host_buffer, cmd); GeometryVertexShader::VertInfo info; - info.mvp = camera.GetTransform(render_pass.GetRenderTargetSize()) * - scene_command.transform; + info.mvp = view_transform * scene_command.transform; GeometryVertexShader::BindVertInfo(cmd, host_buffer.EmplaceUniform(info)); render_pass.AddCommand(std::move(cmd)); @@ -64,7 +63,9 @@ std::shared_ptr SceneEncoder::BuildSceneCommandBuffer( } for (auto& command : commands_) { - EncodeCommand(scene_context, camera, *render_pass, command); + Matrix view_transform = + camera.GetTransform(render_pass->GetRenderTargetSize()); + EncodeCommand(scene_context, view_transform, *render_pass, command); } if (!render_pass->EncodeCommands()) { diff --git a/impeller/scene/scene_entity.h b/impeller/scene/scene_entity.h index 11b370b7d083b..9c774c69772cd 100644 --- a/impeller/scene/scene_entity.h +++ b/impeller/scene/scene_entity.h @@ -37,7 +37,7 @@ class SceneEntity { bool Render(SceneEncoder& encoder) const; protected: - Matrix local_transform_ = Matrix(); + Matrix local_transform_; private: virtual bool OnRender(SceneEncoder& encoder) const;