Wire up renderer.

This commit is contained in:
Chinmay Garde
2021-05-07 15:10:00 -07:00
committed by Dan Field
parent edc53798ff
commit 6b6a8d35fd
12 changed files with 244 additions and 2 deletions

View File

@@ -7,6 +7,7 @@
#import "assets_location.h"
#include "flutter/fml/logging.h"
#import "impeller/compositor/renderer.h"
#import "impeller_renderer.h"
#import "shaders_location.h"
@@ -40,6 +41,7 @@ static const size_t kAlignedUniformsSize = (sizeof(Uniforms) & ~0xFF) + 0x100;
float _rotation;
MTKMesh* _mesh;
impeller::Renderer renderer_;
}
- (nonnull instancetype)initWithMetalKitView:(nonnull MTKView*)view {
@@ -51,6 +53,10 @@ static const size_t kAlignedUniformsSize = (sizeof(Uniforms) & ~0xFF) + 0x100;
[self _loadAssets];
}
if (!renderer_.IsValid()) {
FML_LOG(ERROR) << "Impeller Renderer is not valid.";
}
return self;
}
@@ -220,6 +226,7 @@ static const size_t kAlignedUniformsSize = (sizeof(Uniforms) & ~0xFF) + 0x100;
}
- (void)drawInMTKView:(nonnull MTKView*)view {
renderer_.Render();
/// Per frame updates here
dispatch_semaphore_wait(_inFlightSemaphore, DISPATCH_TIME_FOREVER);
@@ -294,7 +301,8 @@ static const size_t kAlignedUniformsSize = (sizeof(Uniforms) & ~0xFF) + 0x100;
}
- (void)mtkView:(nonnull MTKView*)view drawableSizeWillChange:(CGSize)size {
NSLog(@"Drawable sized did change: %@", NSStringFromSize(size));
renderer_.SurfaceSizeDidChange({size.width, size.height});
float aspect = size.width / (float)size.height;
_projectionMatrix = matrix_perspective_right_hand(65.0f * (M_PI / 180.0f),
aspect, 0.1f, 100.0f);
@@ -302,11 +310,13 @@ static const size_t kAlignedUniformsSize = (sizeof(Uniforms) & ~0xFF) + 0x100;
#pragma mark Matrix Math Utilities
// NOLINTNEXTLINE
matrix_float4x4 matrix4x4_translation(float tx, float ty, float tz) {
return (matrix_float4x4){
{{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {tx, ty, tz, 1}}};
}
// NOLINTNEXTLINE
static matrix_float4x4 matrix4x4_rotation(float radians, vector_float3 axis) {
axis = vector_normalize(axis);
float ct = cosf(radians);
@@ -321,6 +331,7 @@ static matrix_float4x4 matrix4x4_rotation(float radians, vector_float3 axis) {
{0, 0, 0, 1}}};
}
// NOLINTNEXTLINE
matrix_float4x4 matrix_perspective_right_hand(float fovyRadians,
float aspect,
float nearZ,

View File

@@ -4,7 +4,13 @@
import("//flutter/common/config.gni")
config("impeller_config") {
include_dirs = [ ".." ]
}
source_set("impeller") {
public_configs = [ ":impeller_config" ]
cflags_objc = flutter_cflags_objc_arc
cflags_objcc = flutter_cflags_objcc_arc
@@ -12,6 +18,20 @@ source_set("impeller") {
"entity",
"geometry",
"image",
"compositor",
]
compositor_sources = [
"compositor/context.h",
"compositor/context.mm",
"compositor/pipeline_library.h",
"compositor/pipeline_library.mm",
"compositor/renderer.h",
"compositor/renderer.mm",
"compositor/shader_library.h",
"compositor/shader_library.mm",
"compositor/surface.cc",
"compositor/surface.h",
]
geometry_sources = [
@@ -51,7 +71,8 @@ source_set("impeller") {
"image/image_result.h",
]
sources = geometry_sources + entity_sources + image_sources
sources =
compositor_sources + geometry_sources + entity_sources + image_sources
deps = [
"//flutter/fml",

View File

@@ -0,0 +1,31 @@
// 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.
#pragma once
#include <Metal/Metal.h>
#include "flutter/fml/macros.h"
namespace impeller {
class Context {
public:
Context();
~Context();
bool IsValid() const;
private:
id<MTLDevice> device_;
id<MTLCommandQueue> render_queue_;
id<MTLCommandQueue> transfer_queue_;
bool is_valid_ = false;
FML_DISALLOW_COPY_AND_ASSIGN(Context);
};
} // namespace impeller

View File

@@ -0,0 +1,35 @@
// 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.
#include "context.h"
#include "flutter/fml/logging.h"
namespace impeller {
Context::Context() : device_(::MTLCreateSystemDefaultDevice()) {
if (!device_) {
return;
}
render_queue_ = device_.newCommandQueue;
transfer_queue_ = device_.newCommandQueue;
if (!render_queue_ || !transfer_queue_) {
return;
}
render_queue_.label = @"Impeller Render Queue";
transfer_queue_.label = @"Impeller Transfer Queue";
is_valid_ = true;
}
Context::~Context() = default;
bool Context::IsValid() const {
return is_valid_;
}
} // namespace impeller

View File

@@ -0,0 +1,21 @@
// 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.
#pragma once
#include "flutter/fml/macros.h"
namespace impeller {
class PipelineLibrary {
public:
PipelineLibrary();
~PipelineLibrary();
private:
FML_DISALLOW_COPY_AND_ASSIGN(PipelineLibrary);
};
} // namespace impeller

View File

@@ -0,0 +1,35 @@
// 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.
#pragma once
#include "context.h"
#include "flutter/fml/macros.h"
#include "impeller/geometry/size.h"
namespace impeller {
class Renderer {
public:
Renderer();
~Renderer();
bool IsValid() const;
bool SurfaceSizeDidChange(Size size);
bool Render();
private:
Context context_;
Size size_;
bool is_valid_ = false;
bool ShouldRender() const;
FML_DISALLOW_COPY_AND_ASSIGN(Renderer);
};
} // namespace impeller

View File

@@ -0,0 +1,46 @@
// 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.
#include "renderer.h"
#include "flutter/fml/logging.h"
namespace impeller {
Renderer::Renderer() {
if (!context_.IsValid()) {
return;
}
is_valid_ = true;
}
Renderer::~Renderer() = default;
bool Renderer::IsValid() const {
return is_valid_;
}
bool Renderer::ShouldRender() const {
return IsValid() && !size_.IsZero();
}
bool Renderer::SurfaceSizeDidChange(Size size) {
if (size_ == size) {
return true;
}
size_ = size;
return true;
}
bool Renderer::Render() {
if (!ShouldRender()) {
return false;
}
return true;
}
} // namespace impeller

View File

@@ -0,0 +1,21 @@
// 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.
#pragma once
#include "flutter/fml/macros.h"
namespace impeller {
class ShaderLibrary {
public:
ShaderLibrary();
~ShaderLibrary();
private:
FML_DISALLOW_COPY_AND_ASSIGN(ShaderLibrary);
};
} // namespace impeller

View File

@@ -0,0 +1,21 @@
// 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.
#pragma once
#include "flutter/fml/macros.h"
namespace impeller {
class Surface {
public:
Surface();
~Surface();
private:
FML_DISALLOW_COPY_AND_ASSIGN(Surface);
};
} // namespace impeller