[Impeller] Matrix perspective projection (flutter/engine#34014)

This commit is contained in:
Brandon DeRosier
2022-06-14 00:18:04 -07:00
committed by GitHub
parent 77c686c958
commit a695ebb030
6 changed files with 208 additions and 0 deletions

View File

@@ -10,6 +10,8 @@ impeller_shaders("shader_fixtures") {
shaders = [
"box_fade.frag",
"box_fade.vert",
"colors.vert",
"colors.frag",
"impeller.frag",
"impeller.vert",
"instanced_draw.frag",

View File

@@ -0,0 +1,11 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
in vec4 v_color;
out vec4 frag_color;
void main() {
frag_color = v_color;
}

View File

@@ -0,0 +1,18 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
uniform UniformBuffer {
mat4 mvp;
}
uniform_buffer;
in vec3 position;
in vec4 color;
out vec4 v_color;
void main() {
gl_Position = uniform_buffer.mvp * vec4(position, 1.0);
v_color = color;
}

View File

@@ -253,6 +253,54 @@ TEST(GeometryTest, MatrixGetMaxBasisLength) {
}
}
TEST(GeometryTest, MatrixMakeOrthographic) {
{
auto m = Matrix::MakeOrthographic(Size(100, 200));
auto expect = Matrix{
0.02, 0, 0, 0, //
0, -0.01, 0, 0, //
0, 0, 1, 0, //
-1, 1, 0.5, 1, //
};
ASSERT_MATRIX_NEAR(m, expect);
}
{
auto m = Matrix::MakeOrthographic(Size(400, 100));
auto expect = Matrix{
0.005, 0, 0, 0, //
0, -0.02, 0, 0, //
0, 0, 1, 0, //
-1, 1, 0.5, 1, //
};
ASSERT_MATRIX_NEAR(m, expect);
}
}
TEST(GeometryTest, MatrixMakePerspective) {
{
auto m = Matrix::MakePerspective(Degrees(60), Size(100, 200), 1, 10);
auto expect = Matrix{
3.4641, 0, 0, 0, //
0, 1.73205, 0, 0, //
0, 0, -1.22222, -1, //
0, 0, -2.22222, 0, //
};
ASSERT_MATRIX_NEAR(m, expect);
}
{
auto m = Matrix::MakePerspective(Radians(1), 2, 10, 20);
auto expect = Matrix{
0.915244, 0, 0, 0, //
0, 1.83049, 0, 0, //
0, 0, -3, -1, //
0, 0, -40, 0, //
};
ASSERT_MATRIX_NEAR(m, expect);
}
}
TEST(GeometryTest, QuaternionLerp) {
auto q1 = Quaternion{{0.0, 0.0, 1.0}, 0.0};
auto q2 = Quaternion{{0.0, 0.0, 1.0}, M_PI_4};

View File

@@ -329,6 +329,33 @@ struct Matrix {
const auto translate = MakeTranslation({-1.0, 1.0, 0.5});
return translate * scale;
}
static constexpr Matrix MakePerspective(Radians fov_y,
Scalar aspect_ratio,
Scalar z_near,
Scalar z_far) {
Scalar height = std::tan(fov_y.radians * 0.5);
Scalar width = height * aspect_ratio;
Scalar z_diff = z_near - z_far;
// clang-format off
return {
1.0f / width, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f / height, 0.0f, 0.0f,
0.0f, 0.0f, (z_near + z_far) / z_diff, -1.0f,
0.0f, 0.0f, (2.0f * z_near * z_far) / z_diff, 0.0f
};
// clang-format on
}
template <class T>
static constexpr Matrix MakePerspective(Radians fov_y,
TSize<T> size,
Scalar z_near,
Scalar z_far) {
return MakePerspective(fov_y, static_cast<Scalar>(size.width) / size.height,
z_near, z_far);
}
};
static_assert(sizeof(struct Matrix) == sizeof(Scalar) * 16,

View File

@@ -6,6 +6,8 @@
#include "flutter/testing/testing.h"
#include "impeller/fixtures/box_fade.frag.h"
#include "impeller/fixtures/box_fade.vert.h"
#include "impeller/fixtures/colors.frag.h"
#include "impeller/fixtures/colors.vert.h"
#include "impeller/fixtures/impeller.frag.h"
#include "impeller/fixtures/impeller.vert.h"
#include "impeller/fixtures/instanced_draw.frag.h"
@@ -28,6 +30,7 @@
#include "impeller/renderer/surface.h"
#include "impeller/renderer/vertex_buffer_builder.h"
#include "impeller/tessellator/tessellator.h"
#include "third_party/imgui/imgui.h"
namespace impeller {
namespace testing {
@@ -101,6 +104,105 @@ TEST_P(RendererTest, CanCreateBoxPrimitive) {
OpenPlaygroundHere(callback);
}
TEST_P(RendererTest, CanRenderPerspectiveCube) {
using VS = ColorsVertexShader;
using FS = ColorsFragmentShader;
auto context = GetContext();
ASSERT_TRUE(context);
auto desc = PipelineBuilder<VS, FS>::MakeDefaultPipelineDescriptor(*context);
ASSERT_TRUE(desc.has_value());
desc->SetSampleCount(SampleCount::kCount4);
auto pipeline =
context->GetPipelineLibrary()->GetRenderPipeline(std::move(desc)).get();
ASSERT_TRUE(pipeline);
struct Cube {
VS::PerVertexData vertices[8] = {
// -Z
{{-1, -1, -1}, Color::Red()},
{{1, -1, -1}, Color::Yellow()},
{{1, 1, -1}, Color::Green()},
{{-1, 1, -1}, Color::Blue()},
// +Z
{{-1, -1, 1}, Color::Green()},
{{1, -1, 1}, Color::Blue()},
{{1, 1, 1}, Color::Red()},
{{-1, 1, 1}, Color::Yellow()},
};
uint16_t indices[36] = {
1, 5, 2, 2, 5, 6, // +X
4, 0, 7, 7, 0, 3, // -X
4, 5, 0, 0, 5, 1, // +Y
3, 2, 7, 7, 2, 6, // -Y
5, 4, 6, 6, 4, 7, // +Z
0, 1, 3, 3, 1, 2, // -Z
};
} cube;
VertexBuffer vertex_buffer;
{
auto device_buffer =
context->GetPermanentsAllocator()->CreateBufferWithCopy(
reinterpret_cast<uint8_t*>(&cube), sizeof(cube));
vertex_buffer.vertex_buffer = {
.buffer = device_buffer,
.range = Range(offsetof(Cube, vertices), sizeof(Cube::vertices))};
vertex_buffer.index_buffer = {
.buffer = device_buffer,
.range = Range(offsetof(Cube, indices), sizeof(Cube::indices))};
vertex_buffer.index_count = 36;
vertex_buffer.index_type = IndexType::k16bit;
}
auto sampler = context->GetSamplerLibrary()->GetSampler({});
ASSERT_TRUE(sampler);
Vector3 euler_angles;
bool first_frame = true;
SinglePassCallback callback = [&](RenderPass& pass) {
if (first_frame) {
first_frame = false;
ImGui::SetNextWindowSize({400, 80});
ImGui::SetNextWindowPos({20, 20});
}
static Degrees fov_y(60);
static Scalar distance = 10;
ImGui::Begin("Controls");
ImGui::SliderFloat("Field of view", &fov_y.degrees, 0, 180);
ImGui::SliderFloat("Camera distance", &distance, 0, 30);
ImGui::End();
Command cmd;
cmd.label = "Perspective Cube";
cmd.pipeline = pipeline;
cmd.BindVertices(vertex_buffer);
VS::UniformBuffer uniforms;
Scalar time = fml::TimePoint::Now().ToEpochDelta().ToSecondsF();
euler_angles = Vector3(0.19 * time, 0.7 * time, 0.43 * time);
uniforms.mvp =
Matrix::MakePerspective(fov_y, pass.GetRenderTargetSize(), 0, 10) *
Matrix::MakeTranslation({0, 0, -distance}) *
Matrix::MakeRotationX(Radians(euler_angles.x)) *
Matrix::MakeRotationY(Radians(euler_angles.y)) *
Matrix::MakeRotationZ(Radians(euler_angles.z));
VS::BindUniformBuffer(cmd,
pass.GetTransientsBuffer().EmplaceUniform(uniforms));
cmd.primitive_type = PrimitiveType::kTriangle;
cmd.cull_mode = CullMode::kBackFace;
if (!pass.AddCommand(std::move(cmd))) {
return false;
}
return true;
};
OpenPlaygroundHere(callback);
}
TEST_P(RendererTest, CanRenderMultiplePrimitives) {
using VS = BoxFadeVertexShader;
using FS = BoxFadeFragmentShader;