Add box shader sample.

This commit is contained in:
Chinmay Garde 2021-05-21 10:51:50 -07:00 committed by Dan Field
parent 8da39c0afb
commit 5d6dd79bf0
10 changed files with 81 additions and 25 deletions

View File

@ -17,6 +17,7 @@ namespace impeller {
namespace shader {
struct {{camel_case(shader_name)}}{{camel_case(shader_stage)}}Info {
static constexpr std::string_view kLabel = "{{camel_case(shader_name)}}";
static constexpr std::string_view kEntrypointName = "{{entrypoint}}";
static constexpr ShaderStage kShaderStage = {{to_shader_stage(shader_stage)}};

View File

@ -64,4 +64,8 @@ id<MTLCommandQueue> Context::GetTransferQueue() const {
return transfer_queue_;
}
std::shared_ptr<ShaderLibrary> Context::GetShaderLibrary() const {
return shader_library_;
}
} // namespace impeller

View File

@ -4,7 +4,10 @@
#pragma once
#include <functional>
#include <memory>
#include <type_traits>
#include <unordered_map>
#include "flutter/fml/macros.h"
#include "impeller/compositor/pipeline_descriptor.h"
@ -19,19 +22,21 @@ class PipelineBuilder {
~PipelineBuilder();
PipelineBuilder& SetLabel();
PipelineBuilder& SetLabel(const std::string_view& label);
PipelineBuilder& SetSampleCountCount(size_t samples);
PipelineBuilder& SetSampleCount(size_t samples);
PipelineBuilder& SetVertexFunction(std::shared_ptr<ShaderFunction> function);
PipelineBuilder& SetFragmentFunction(
std::shared_ptr<ShaderFunction> function);
PipelineBuilder& AddStageEntrypoint(std::shared_ptr<ShaderFunction> function);
PipelineBuilder& SetVertexDescriptor(
std::shared_ptr<VertexDescriptor> vertex_descriptor);
private:
std::string label_;
size_t sample_count_ = 1;
std::unordered_map<ShaderStage, std::shared_ptr<ShaderFunction>> entrypoints_;
std::shared_ptr<VertexDescriptor> vertex_descriptor_;
FML_DISALLOW_COPY_AND_ASSIGN(PipelineBuilder);
};

View File

@ -10,4 +10,35 @@ PipelineBuilder::PipelineBuilder() = default;
PipelineBuilder::~PipelineBuilder() = default;
PipelineBuilder& PipelineBuilder::SetLabel(const std::string_view& label) {
label_ = {label.data(), label.size()};
return *this;
}
PipelineBuilder& PipelineBuilder::SetSampleCount(size_t samples) {
sample_count_ = samples;
return *this;
}
PipelineBuilder& PipelineBuilder::AddStageEntrypoint(
std::shared_ptr<ShaderFunction> function) {
if (!function) {
return *this;
}
if (function->GetStage() == ShaderStage::kUnknown) {
return *this;
}
entrypoints_[function->GetStage()] = std::move(function);
return *this;
}
PipelineBuilder& PipelineBuilder::SetVertexDescriptor(
std::shared_ptr<VertexDescriptor> vertex_descriptor) {
vertex_descriptor_ = std::move(vertex_descriptor);
return *this;
}
} // namespace impeller

View File

@ -8,6 +8,7 @@
#include <memory>
#include <string>
#include <string_view>
#include "flutter/fml/macros.h"
#include "impeller/shader_glue/shader_types.h"
@ -37,7 +38,7 @@ class ShaderLibrary {
public:
~ShaderLibrary();
std::shared_ptr<ShaderFunction> GetFunction(const std::string& name,
std::shared_ptr<ShaderFunction> GetFunction(const std::string_view& name,
ShaderStage stage);
private:

View File

@ -20,9 +20,9 @@ ShaderLibrary::ShaderLibrary(id<MTLLibrary> library) : library_(library) {}
ShaderLibrary::~ShaderLibrary() = default;
std::shared_ptr<ShaderFunction> ShaderLibrary::GetFunction(
const std::string& name,
const std::string_view& name,
ShaderStage stage) {
auto function = [library_ newFunctionWithName:@(name.c_str())];
auto function = [library_ newFunctionWithName:@(name.data())];
if (!function) {
return nullptr;
}

View File

@ -14,8 +14,8 @@ impeller_shaders("impeller_shaders") {
impeller_component("primitives") {
sources = [
"box.cc",
"box.h",
"box.mm",
]
public_deps = [ "../compositor" ]

View File

@ -1,14 +0,0 @@
// 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/primitives/box.h"
#include "box.frag.h"
#include "box.vert.h"
namespace impeller {
void RenderBox() {}
} // namespace impeller

View File

@ -2,8 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "impeller/compositor/context.h"
namespace impeller {
void RenderBox();
void RenderBox(std::shared_ptr<Context> context);
} // namespace impeller

View File

@ -0,0 +1,26 @@
// 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/primitives/box.h"
#include "box.frag.h"
#include "box.vert.h"
#include "impeller/compositor/pipeline_builder.h"
namespace impeller {
void RenderBox(std::shared_ptr<Context> context) {
PipelineBuilder builder;
auto fragment_function = context->GetShaderLibrary()->GetFunction(
shader::BoxFragmentInfo::kEntrypointName, ShaderStage::kFragment);
auto vertex_function = context->GetShaderLibrary()->GetFunction(
shader::BoxVertexInfo::kEntrypointName, ShaderStage::kVertex);
builder.AddStageEntrypoint(vertex_function);
builder.AddStageEntrypoint(fragment_function);
builder.SetLabel(shader::BoxVertexInfo::kLabel);
}
} // namespace impeller