Reason about conventions when dealing with NDC.

This commit is contained in:
Chinmay Garde
2021-06-23 16:21:43 -07:00
committed by Dan Field
parent 012eb34307
commit b5ebf6805a
10 changed files with 48 additions and 91 deletions

View File

@@ -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<Size> GetColorAttachmentSize(size_t index) const;
RenderPassDescriptor& SetColorAttachment(ColorRenderPassAttachment attachment,
size_t index);

View File

@@ -24,6 +24,17 @@ bool RenderPassDescriptor::HasColorAttachment(size_t index) const {
return false;
}
std::optional<Size> 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) {

View File

@@ -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;

View File

@@ -49,7 +49,7 @@ bool Renderer::Render(const Surface& surface) {
return false;
}
if (!OnRender(*render_pass)) {
if (!OnRender(surface, *render_pass)) {
return false;
}

View File

@@ -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<bool(void)> present_callback_;
Size size_;
bool is_valid_ = false;

View File

@@ -11,14 +11,21 @@ namespace impeller {
Surface::Surface(RenderPassDescriptor desc,
std::function<bool(void)> 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_;

View File

@@ -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);
};

View File

@@ -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;

View File

@@ -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], //

View File

@@ -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,