Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 25a2ac8

Browse files
authored
[Impeller Scene] Wire up pipelines (#37961)
1 parent 3956f6d commit 25a2ac8

File tree

14 files changed

+252
-19
lines changed

14 files changed

+252
-19
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,11 +1598,15 @@ FILE: ../../../flutter/impeller/scene/material.cc
15981598
FILE: ../../../flutter/impeller/scene/material.h
15991599
FILE: ../../../flutter/impeller/scene/scene.cc
16001600
FILE: ../../../flutter/impeller/scene/scene.h
1601+
FILE: ../../../flutter/impeller/scene/scene_context.cc
1602+
FILE: ../../../flutter/impeller/scene/scene_context.h
16011603
FILE: ../../../flutter/impeller/scene/scene_encoder.cc
16021604
FILE: ../../../flutter/impeller/scene/scene_encoder.h
16031605
FILE: ../../../flutter/impeller/scene/scene_entity.cc
16041606
FILE: ../../../flutter/impeller/scene/scene_entity.h
16051607
FILE: ../../../flutter/impeller/scene/scene_unittests.cc
1608+
FILE: ../../../flutter/impeller/scene/shaders/geometry.vert
1609+
FILE: ../../../flutter/impeller/scene/shaders/unlit.frag
16061610
FILE: ../../../flutter/impeller/scene/static_mesh_entity.cc
16071611
FILE: ../../../flutter/impeller/scene/static_mesh_entity.h
16081612
FILE: ../../../flutter/impeller/tessellator/c/tessellator.cc

impeller/playground/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ impeller_component("playground") {
4343
"../entity:modern_entity_shaders",
4444
"../fixtures:shader_fixtures",
4545
"../renderer",
46+
"../scene/shaders",
4647
"imgui:imgui_impeller_backend",
4748
"//flutter/fml",
4849
"//third_party/glfw",

impeller/playground/backend/gles/playground_impl_gles.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "impeller/playground/imgui/gles/imgui_shaders_gles.h"
1414
#include "impeller/renderer/backend/gles/context_gles.h"
1515
#include "impeller/renderer/backend/gles/surface_gles.h"
16+
#include "impeller/scene/shaders/gles/scene_shaders_gles.h"
1617

1718
namespace impeller {
1819

@@ -94,6 +95,8 @@ ShaderLibraryMappingsForPlayground() {
9495
impeller_fixtures_shaders_gles_length),
9596
std::make_shared<fml::NonOwnedMapping>(
9697
impeller_imgui_shaders_gles_data, impeller_imgui_shaders_gles_length),
98+
std::make_shared<fml::NonOwnedMapping>(
99+
impeller_scene_shaders_gles_data, impeller_scene_shaders_gles_length),
97100
};
98101
}
99102

impeller/playground/backend/metal/playground_impl_mtl.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "impeller/renderer/backend/metal/formats_mtl.h"
2323
#include "impeller/renderer/backend/metal/surface_mtl.h"
2424
#include "impeller/renderer/backend/metal/texture_mtl.h"
25+
#include "impeller/scene/shaders/mtl/scene_shaders.h"
2526

2627
namespace impeller {
2728

@@ -40,6 +41,8 @@
4041
impeller_fixtures_shaders_length),
4142
std::make_shared<fml::NonOwnedMapping>(impeller_imgui_shaders_data,
4243
impeller_imgui_shaders_length),
44+
std::make_shared<fml::NonOwnedMapping>(impeller_scene_shaders_data,
45+
impeller_scene_shaders_length),
4346

4447
};
4548
}

impeller/playground/backend/vulkan/playground_impl_vk.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "impeller/renderer/backend/vulkan/formats_vk.h"
2020
#include "impeller/renderer/backend/vulkan/surface_vk.h"
2121
#include "impeller/renderer/backend/vulkan/texture_vk.h"
22+
#include "impeller/scene/shaders/vk/scene_shaders_vk.h"
2223

2324
namespace impeller {
2425

@@ -34,6 +35,8 @@ ShaderLibraryMappingsForPlayground() {
3435
impeller_fixtures_shaders_vk_length),
3536
std::make_shared<fml::NonOwnedMapping>(impeller_imgui_shaders_vk_data,
3637
impeller_imgui_shaders_vk_length),
38+
std::make_shared<fml::NonOwnedMapping>(impeller_scene_shaders_vk_data,
39+
impeller_scene_shaders_vk_length),
3740

3841
};
3942
}

impeller/scene/BUILD.gn

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ impeller_component("scene") {
1414
"material.h",
1515
"scene.cc",
1616
"scene.h",
17+
"scene_context.cc",
18+
"scene_context.h",
1719
"scene_encoder.cc",
1820
"scene_encoder.h",
1921
"scene_entity.cc",
@@ -22,7 +24,10 @@ impeller_component("scene") {
2224
"static_mesh_entity.h",
2325
]
2426

25-
public_deps = [ "../renderer" ]
27+
public_deps = [
28+
"../renderer",
29+
"shaders",
30+
]
2631

2732
deps = [ "//flutter/fml" ]
2833
}

impeller/scene/scene.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99

1010
#include "flutter/fml/logging.h"
1111
#include "impeller/renderer/render_target.h"
12+
#include "impeller/scene/scene_context.h"
1213
#include "impeller/scene/scene_encoder.h"
1314

1415
namespace impeller {
1516
namespace scene {
1617

17-
Scene::Scene(const std::shared_ptr<Context>& context) : context_(context){};
18+
Scene::Scene(std::shared_ptr<Context> context)
19+
: scene_context_(std::make_unique<SceneContext>(std::move(context))){};
1820

1921
void Scene::Add(const std::shared_ptr<SceneEntity>& child) {
2022
root_.Add(child);
@@ -31,7 +33,8 @@ bool Scene::Render(const RenderTarget& render_target,
3133

3234
// Encode the commands.
3335
std::shared_ptr<CommandBuffer> command_buffer =
34-
encoder.BuildSceneCommandBuffer(*context_, render_target);
36+
encoder.BuildSceneCommandBuffer(*scene_context_->GetContext(),
37+
render_target);
3538

3639
// TODO(bdero): Do post processing.
3740

impeller/scene/scene.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "impeller/renderer/render_target.h"
1313
#include "impeller/scene/camera.h"
14+
#include "impeller/scene/scene_context.h"
1415
#include "impeller/scene/scene_entity.h"
1516

1617
namespace impeller {
@@ -19,13 +20,13 @@ namespace scene {
1920
class Scene {
2021
public:
2122
Scene() = delete;
22-
explicit Scene(const std::shared_ptr<Context>& context);
23+
explicit Scene(std::shared_ptr<Context> context);
2324

2425
void Add(const std::shared_ptr<SceneEntity>& child);
2526
bool Render(const RenderTarget& render_target, const Camera& camera) const;
2627

2728
private:
28-
std::shared_ptr<Context> context_;
29+
std::unique_ptr<SceneContext> scene_context_;
2930
SceneEntity root_;
3031

3132
FML_DISALLOW_COPY_AND_ASSIGN(Scene);

impeller/scene/scene_context.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "impeller/scene/scene_context.h"
6+
7+
namespace impeller {
8+
namespace scene {
9+
10+
void SceneContextOptions::ApplyToPipelineDescriptor(
11+
PipelineDescriptor& desc) const {
12+
desc.SetSampleCount(sample_count);
13+
desc.SetPrimitiveType(primitive_type);
14+
}
15+
16+
template <typename PipelineT>
17+
static std::unique_ptr<PipelineT> CreateDefaultPipeline(
18+
const Context& context) {
19+
auto desc = PipelineT::Builder::MakeDefaultPipelineDescriptor(context);
20+
if (!desc.has_value()) {
21+
return nullptr;
22+
}
23+
// Apply default ContentContextOptions to the descriptor.
24+
SceneContextOptions{}.ApplyToPipelineDescriptor(*desc);
25+
return std::make_unique<PipelineT>(context, desc);
26+
}
27+
28+
SceneContext::SceneContext(std::shared_ptr<Context> context)
29+
: context_(std::move(context)) {
30+
if (!context_ || !context_->IsValid()) {
31+
return;
32+
}
33+
34+
unlit_pipeline_[{}] = CreateDefaultPipeline<UnlitPipeline>(*context_);
35+
36+
is_valid_ = true;
37+
}
38+
39+
SceneContext::~SceneContext() = default;
40+
41+
bool SceneContext::IsValid() const {
42+
return is_valid_;
43+
}
44+
45+
std::shared_ptr<Context> SceneContext::GetContext() const {
46+
return context_;
47+
}
48+
49+
} // namespace scene
50+
} // namespace impeller

impeller/scene/scene_context.h

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#pragma once
6+
7+
#include "impeller/renderer/context.h"
8+
#include "impeller/renderer/pipeline_descriptor.h"
9+
#include "impeller/scene/shaders/geometry.vert.h"
10+
#include "impeller/scene/shaders/unlit.frag.h"
11+
12+
namespace impeller {
13+
namespace scene {
14+
15+
using UnlitPipeline =
16+
RenderPipelineT<GeometryVertexShader, UnlitFragmentShader>;
17+
18+
struct SceneContextOptions {
19+
SampleCount sample_count = SampleCount::kCount1;
20+
PrimitiveType primitive_type = PrimitiveType::kTriangle;
21+
22+
struct Hash {
23+
constexpr std::size_t operator()(const SceneContextOptions& o) const {
24+
return fml::HashCombine(o.sample_count, o.primitive_type);
25+
}
26+
};
27+
28+
struct Equal {
29+
constexpr bool operator()(const SceneContextOptions& lhs,
30+
const SceneContextOptions& rhs) const {
31+
return lhs.sample_count == rhs.sample_count &&
32+
lhs.primitive_type == rhs.primitive_type;
33+
}
34+
};
35+
36+
void ApplyToPipelineDescriptor(PipelineDescriptor& desc) const;
37+
};
38+
39+
class SceneContext {
40+
public:
41+
explicit SceneContext(std::shared_ptr<Context> context);
42+
43+
~SceneContext();
44+
45+
bool IsValid() const;
46+
47+
std::shared_ptr<Context> GetContext() const;
48+
49+
std::shared_ptr<Pipeline<PipelineDescriptor>> GetUnlitPipeline(
50+
SceneContextOptions opts) const {
51+
return GetPipeline(unlit_pipeline_, opts);
52+
}
53+
54+
private:
55+
std::shared_ptr<Context> context_;
56+
57+
template <class T>
58+
using Variants = std::unordered_map<SceneContextOptions,
59+
std::unique_ptr<T>,
60+
SceneContextOptions::Hash,
61+
SceneContextOptions::Equal>;
62+
63+
mutable Variants<UnlitPipeline> unlit_pipeline_;
64+
65+
template <class TypedPipeline>
66+
std::shared_ptr<Pipeline<PipelineDescriptor>> GetPipeline(
67+
Variants<TypedPipeline>& container,
68+
SceneContextOptions opts) const {
69+
if (!IsValid()) {
70+
return nullptr;
71+
}
72+
73+
if (auto found = container.find(opts); found != container.end()) {
74+
return found->second->WaitAndGet();
75+
}
76+
77+
auto prototype = container.find({});
78+
79+
// The prototype must always be initialized in the constructor.
80+
FML_CHECK(prototype != container.end());
81+
82+
auto variant_future = prototype->second->WaitAndGet()->CreateVariant(
83+
[&opts, variants_count = container.size()](PipelineDescriptor& desc) {
84+
opts.ApplyToPipelineDescriptor(desc);
85+
desc.SetLabel(
86+
SPrintF("%s V#%zu", desc.GetLabel().c_str(), variants_count));
87+
});
88+
auto variant = std::make_unique<TypedPipeline>(std::move(variant_future));
89+
auto variant_pipeline = variant->WaitAndGet();
90+
container[opts] = std::move(variant);
91+
return variant_pipeline;
92+
}
93+
94+
bool is_valid_ = false;
95+
96+
FML_DISALLOW_COPY_AND_ASSIGN(SceneContext);
97+
};
98+
99+
} // namespace scene
100+
} // namespace impeller

impeller/scene/scene_unittests.cc

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,26 @@ namespace testing {
2525
using SceneTest = PlaygroundTest;
2626
INSTANTIATE_PLAYGROUND_SUITE(SceneTest);
2727

28-
TEST_P(SceneTest, UnlitScene) {
29-
auto allocator = GetContext()->GetResourceAllocator();
30-
auto scene = Scene(GetContext());
28+
TEST_P(SceneTest, CuboidUnlit) {
29+
Renderer::RenderCallback callback = [&](RenderTarget& render_target) {
30+
auto allocator = GetContext()->GetResourceAllocator();
31+
auto scene = Scene(GetContext());
3132

32-
{
33-
auto mesh = SceneEntity::MakeStaticMesh();
33+
{
34+
auto mesh = SceneEntity::MakeStaticMesh();
3435

35-
auto material = Material::MakeUnlit();
36-
material->SetColor(Color::Red());
37-
mesh->SetMaterial(std::move(material));
36+
auto material = Material::MakeUnlit();
37+
material->SetColor(Color::Red());
38+
mesh->SetMaterial(std::move(material));
3839

39-
Vector3 size(1, 2, 3);
40-
mesh->SetGeometry(Geometry::MakeCuboid(size));
40+
Vector3 size(1, 2, 3);
41+
mesh->SetGeometry(Geometry::MakeCuboid(size));
4142

42-
mesh->SetLocalTransform(Matrix::MakeTranslation(size / 2));
43+
mesh->SetLocalTransform(Matrix::MakeTranslation(size / 2));
4344

44-
scene.Add(mesh);
45-
}
45+
scene.Add(mesh);
46+
}
4647

47-
Renderer::RenderCallback callback = [&](RenderTarget& render_target) {
4848
auto camera = Camera::MakePerspective(
4949
/* fov */ kPiOver4,
5050
/* position */ {50, -30, 50})

impeller/scene/shaders/BUILD.gn

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2013 The Flutter Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style license that can be
3+
# found in the LICENSE file.
4+
5+
import("//flutter/impeller/tools/impeller.gni")
6+
7+
impeller_shaders("shaders") {
8+
name = "scene"
9+
10+
shaders = [
11+
"geometry.vert",
12+
"unlit.frag",
13+
]
14+
}

impeller/scene/shaders/geometry.vert

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
uniform VertInfo {
6+
mat4 mvp;
7+
}
8+
vert_info;
9+
10+
in vec3 position;
11+
in vec3 normal;
12+
in vec3 tangent;
13+
in vec2 texture_coords;
14+
15+
out vec3 v_position;
16+
out mat3 v_tangent_space;
17+
out vec2 v_texture_coords;
18+
19+
void main() {
20+
gl_Position = vert_info.mvp * vec4(position, 1.0);
21+
v_position = gl_Position.xyz;
22+
23+
v_tangent_space =
24+
mat3(vert_info.mvp) * mat3(tangent, cross(normal, tangent), normal);
25+
v_texture_coords = texture_coords;
26+
}

0 commit comments

Comments
 (0)