mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Wire up shader libraries.
This commit is contained in:
parent
73f01575b6
commit
631cc5cf0e
@ -41,7 +41,7 @@ static const size_t kAlignedUniformsSize = (sizeof(Uniforms) & ~0xFF) + 0x100;
|
||||
float _rotation;
|
||||
|
||||
MTKMesh* _mesh;
|
||||
impeller::Renderer renderer_;
|
||||
std::unique_ptr<impeller::Renderer> renderer_;
|
||||
}
|
||||
|
||||
- (nonnull instancetype)initWithMetalKitView:(nonnull MTKView*)view {
|
||||
@ -53,7 +53,9 @@ static const size_t kAlignedUniformsSize = (sizeof(Uniforms) & ~0xFF) + 0x100;
|
||||
[self _loadAssets];
|
||||
}
|
||||
|
||||
if (!renderer_.IsValid()) {
|
||||
renderer_ = std::make_unique<impeller::Renderer>(
|
||||
impeller::ImpellerShadersDirectory());
|
||||
if (!renderer_->IsValid()) {
|
||||
FML_LOG(ERROR) << "Impeller Renderer is not valid.";
|
||||
}
|
||||
|
||||
@ -226,7 +228,7 @@ static const size_t kAlignedUniformsSize = (sizeof(Uniforms) & ~0xFF) + 0x100;
|
||||
}
|
||||
|
||||
- (void)drawInMTKView:(nonnull MTKView*)view {
|
||||
renderer_.Render();
|
||||
renderer_->Render();
|
||||
/// Per frame updates here
|
||||
dispatch_semaphore_wait(_inFlightSemaphore, DISPATCH_TIME_FOREVER);
|
||||
|
||||
@ -301,7 +303,7 @@ static const size_t kAlignedUniformsSize = (sizeof(Uniforms) & ~0xFF) + 0x100;
|
||||
}
|
||||
|
||||
- (void)mtkView:(nonnull MTKView*)view drawableSizeWillChange:(CGSize)size {
|
||||
renderer_.SurfaceSizeDidChange({size.width, size.height});
|
||||
renderer_->SurfaceSizeDidChange({size.width, size.height});
|
||||
|
||||
float aspect = size.width / (float)size.height;
|
||||
_projectionMatrix = matrix_perspective_right_hand(65.0f * (M_PI / 180.0f),
|
||||
|
||||
@ -9,16 +9,16 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
std::optional<std::string> ImpellerShadersLocation(std::string library_name) {
|
||||
auto executable_directory = fml::paths::GetExecutableDirectoryPath();
|
||||
|
||||
if (!executable_directory.first) {
|
||||
FML_LOG(ERROR) << "Shaders directory could not be found.";
|
||||
return std::nullopt;
|
||||
std::string ImpellerShadersDirectory() {
|
||||
auto path_result = fml::paths::GetExecutableDirectoryPath();
|
||||
if (!path_result.first) {
|
||||
return {};
|
||||
}
|
||||
return fml::paths::JoinPaths({path_result.second, "shaders"});
|
||||
}
|
||||
|
||||
auto path = fml::paths::JoinPaths(
|
||||
{executable_directory.second, "shaders", library_name});
|
||||
std::optional<std::string> ImpellerShadersLocation(std::string library_name) {
|
||||
auto path = fml::paths::JoinPaths({ImpellerShadersDirectory(), library_name});
|
||||
|
||||
if (!fml::IsFile(path)) {
|
||||
FML_LOG(ERROR) << "The shader library does not exist: " << path;
|
||||
|
||||
@ -7,6 +7,8 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
std::string ImpellerShadersDirectory();
|
||||
|
||||
std::optional<std::string> ImpellerShadersLocation(std::string library_name);
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@ -23,6 +23,10 @@ source_set("impeller") {
|
||||
cflags_objc = flutter_cflags_objc_arc
|
||||
cflags_objcc = flutter_cflags_objcc_arc
|
||||
|
||||
if (is_clang) {
|
||||
cflags_cc = [ "-Wno-unused-private-field" ]
|
||||
}
|
||||
|
||||
include_dirs = [
|
||||
"entity",
|
||||
"geometry",
|
||||
@ -45,6 +49,8 @@ source_set("impeller") {
|
||||
"compositor/shader_library.mm",
|
||||
"compositor/surface.h",
|
||||
"compositor/surface.mm",
|
||||
"compositor/vertex_descriptor.h",
|
||||
"compositor/vertex_descriptor.mm",
|
||||
]
|
||||
|
||||
geometry_sources = [
|
||||
|
||||
@ -6,13 +6,17 @@
|
||||
|
||||
#include <Metal/Metal.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
class ShaderLibrary;
|
||||
|
||||
class Context {
|
||||
public:
|
||||
Context();
|
||||
Context(std::string shaders_directory);
|
||||
|
||||
~Context();
|
||||
|
||||
@ -22,10 +26,13 @@ class Context {
|
||||
|
||||
id<MTLCommandQueue> GetTransferQueue() const;
|
||||
|
||||
std::shared_ptr<ShaderLibrary> GetShaderLibrary() const;
|
||||
|
||||
private:
|
||||
id<MTLDevice> device_;
|
||||
id<MTLCommandQueue> render_queue_;
|
||||
id<MTLCommandQueue> transfer_queue_;
|
||||
id<MTLDevice> device_ = nullptr;
|
||||
id<MTLCommandQueue> render_queue_ = nullptr;
|
||||
id<MTLCommandQueue> transfer_queue_ = nullptr;
|
||||
std::shared_ptr<ShaderLibrary> shader_library_;
|
||||
bool is_valid_ = false;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(Context);
|
||||
|
||||
@ -5,14 +5,19 @@
|
||||
#include "context.h"
|
||||
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "flutter/fml/paths.h"
|
||||
#include "impeller/compositor/shader_library.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
Context::Context() : device_(::MTLCreateSystemDefaultDevice()) {
|
||||
Context::Context(std::string shaders_directory)
|
||||
: device_(::MTLCreateSystemDefaultDevice()) {
|
||||
// Setup device.
|
||||
if (!device_) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Setup command queues.
|
||||
render_queue_ = device_.newCommandQueue;
|
||||
transfer_queue_ = device_.newCommandQueue;
|
||||
|
||||
@ -23,6 +28,25 @@ Context::Context() : device_(::MTLCreateSystemDefaultDevice()) {
|
||||
render_queue_.label = @"Impeller Render Queue";
|
||||
transfer_queue_.label = @"Impeller Transfer Queue";
|
||||
|
||||
// Setup the shader library.
|
||||
{
|
||||
NSError* shader_library_error = nil;
|
||||
auto shader_library_path =
|
||||
fml::paths::JoinPaths({shaders_directory, "impeller.metallib"});
|
||||
id<MTLLibrary> library =
|
||||
[device_ newLibraryWithFile:@(shader_library_path.c_str())
|
||||
error:&shader_library_error];
|
||||
if (!library) {
|
||||
FML_LOG(ERROR) << "Could not create shader library: "
|
||||
<< shader_library_error.localizedDescription.UTF8String;
|
||||
return;
|
||||
}
|
||||
|
||||
// std::make_shared disallowed because of private friend ctor.
|
||||
shader_library_ =
|
||||
std::shared_ptr<ShaderLibrary>(new ShaderLibrary(library));
|
||||
}
|
||||
|
||||
is_valid_ = true;
|
||||
}
|
||||
|
||||
|
||||
@ -4,8 +4,12 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "impeller/compositor/pipeline_descriptor.h"
|
||||
#include "impeller/compositor/shader_library.h"
|
||||
#include "impeller/compositor/vertex_descriptor.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
@ -15,7 +19,17 @@ class PipelineBuilder {
|
||||
|
||||
~PipelineBuilder();
|
||||
|
||||
PipelineDescriptor Build() const;
|
||||
PipelineBuilder& SetLabel();
|
||||
|
||||
PipelineBuilder& SetSampleCountCount(size_t samples);
|
||||
|
||||
PipelineBuilder& SetVertexFunction(std::shared_ptr<ShaderFunction> function);
|
||||
|
||||
PipelineBuilder& SetFragmentFunction(
|
||||
std::shared_ptr<ShaderFunction> function);
|
||||
|
||||
PipelineBuilder& SetVertexDescriptor(
|
||||
std::shared_ptr<VertexDescriptor> vertex_descriptor);
|
||||
|
||||
private:
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(PipelineBuilder);
|
||||
|
||||
@ -15,7 +15,7 @@ namespace impeller {
|
||||
|
||||
class Renderer {
|
||||
public:
|
||||
Renderer();
|
||||
Renderer(std::string shaders_directory);
|
||||
|
||||
~Renderer();
|
||||
|
||||
|
||||
@ -8,8 +8,8 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
Renderer::Renderer()
|
||||
: context_(std::make_shared<Context>()),
|
||||
Renderer::Renderer(std::string shaders_directory)
|
||||
: context_(std::make_shared<Context>(std::move(shaders_directory))),
|
||||
surface_(std::make_unique<Surface>(context_)) {
|
||||
if (!context_->IsValid()) {
|
||||
return;
|
||||
|
||||
@ -4,17 +4,53 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Metal/Metal.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
class ShaderLibrary {
|
||||
public:
|
||||
ShaderLibrary();
|
||||
class Context;
|
||||
|
||||
~ShaderLibrary();
|
||||
enum class ShaderStage {
|
||||
kVertex,
|
||||
kFragment,
|
||||
};
|
||||
|
||||
class ShaderFunction {
|
||||
public:
|
||||
~ShaderFunction();
|
||||
|
||||
ShaderStage GetStage() const;
|
||||
|
||||
private:
|
||||
friend class ShaderLibrary;
|
||||
|
||||
id<MTLFunction> function_ = nullptr;
|
||||
ShaderStage stage_;
|
||||
|
||||
ShaderFunction(id<MTLFunction> function, ShaderStage stage);
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(ShaderFunction);
|
||||
};
|
||||
|
||||
class ShaderLibrary {
|
||||
public:
|
||||
~ShaderLibrary();
|
||||
|
||||
std::shared_ptr<ShaderFunction> GetFunction(const std::string& name,
|
||||
ShaderStage stage);
|
||||
|
||||
private:
|
||||
friend class Context;
|
||||
|
||||
id<MTLLibrary> library_ = nullptr;
|
||||
|
||||
ShaderLibrary(id<MTLLibrary> library);
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(ShaderLibrary);
|
||||
};
|
||||
|
||||
|
||||
@ -0,0 +1,32 @@
|
||||
// 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 "impeller/compositor/shader_library.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
ShaderFunction::ShaderFunction(id<MTLFunction> function, ShaderStage stage)
|
||||
: function_(function), stage_(stage) {}
|
||||
|
||||
ShaderFunction::~ShaderFunction() = default;
|
||||
|
||||
ShaderStage ShaderFunction::GetStage() const {
|
||||
return stage_;
|
||||
}
|
||||
|
||||
ShaderLibrary::ShaderLibrary(id<MTLLibrary> library) : library_(library) {}
|
||||
|
||||
ShaderLibrary::~ShaderLibrary() = default;
|
||||
|
||||
std::shared_ptr<ShaderFunction> ShaderLibrary::GetFunction(
|
||||
const std::string& name,
|
||||
ShaderStage stage) {
|
||||
auto function = [library_ newFunctionWithName:@(name.c_str())];
|
||||
if (!function) {
|
||||
return nullptr;
|
||||
}
|
||||
return std::shared_ptr<ShaderFunction>(new ShaderFunction(function, stage));
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
@ -25,7 +25,7 @@ class Surface {
|
||||
|
||||
private:
|
||||
std::shared_ptr<Context> context_;
|
||||
dispatch_semaphore_t frames_in_flight_sema_;
|
||||
dispatch_semaphore_t frames_in_flight_sema_ = nullptr;
|
||||
bool is_valid_ = false;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(Surface);
|
||||
|
||||
@ -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 VertexDescriptor {
|
||||
public:
|
||||
VertexDescriptor();
|
||||
|
||||
~VertexDescriptor();
|
||||
|
||||
private:
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(VertexDescriptor);
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
@ -0,0 +1,13 @@
|
||||
// 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 "impeller/compositor/vertex_descriptor.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
VertexDescriptor::VertexDescriptor() = default;
|
||||
|
||||
VertexDescriptor::~VertexDescriptor() = default;
|
||||
|
||||
} // namespace impeller
|
||||
Loading…
x
Reference in New Issue
Block a user