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

Commit 84ba803

Browse files
authored
[Impeller Scene] Make Geometry/Materials own command binding behavior (#38437)
1 parent 23a56ee commit 84ba803

File tree

12 files changed

+222
-91
lines changed

12 files changed

+222
-91
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,14 +1626,15 @@ ORIGIN: ../../../flutter/impeller/scene/mesh.cc + ../../../flutter/LICENSE
16261626
ORIGIN: ../../../flutter/impeller/scene/mesh.h + ../../../flutter/LICENSE
16271627
ORIGIN: ../../../flutter/impeller/scene/node.cc + ../../../flutter/LICENSE
16281628
ORIGIN: ../../../flutter/impeller/scene/node.h + ../../../flutter/LICENSE
1629+
ORIGIN: ../../../flutter/impeller/scene/pipeline_key.h + ../../../flutter/LICENSE
16291630
ORIGIN: ../../../flutter/impeller/scene/scene.cc + ../../../flutter/LICENSE
16301631
ORIGIN: ../../../flutter/impeller/scene/scene.h + ../../../flutter/LICENSE
16311632
ORIGIN: ../../../flutter/impeller/scene/scene_context.cc + ../../../flutter/LICENSE
16321633
ORIGIN: ../../../flutter/impeller/scene/scene_context.h + ../../../flutter/LICENSE
16331634
ORIGIN: ../../../flutter/impeller/scene/scene_encoder.cc + ../../../flutter/LICENSE
16341635
ORIGIN: ../../../flutter/impeller/scene/scene_encoder.h + ../../../flutter/LICENSE
1635-
ORIGIN: ../../../flutter/impeller/scene/shaders/geometry.vert + ../../../flutter/LICENSE
16361636
ORIGIN: ../../../flutter/impeller/scene/shaders/unlit.frag + ../../../flutter/LICENSE
1637+
ORIGIN: ../../../flutter/impeller/scene/shaders/unskinned.vert + ../../../flutter/LICENSE
16371638
ORIGIN: ../../../flutter/impeller/tessellator/c/tessellator.cc + ../../../flutter/LICENSE
16381639
ORIGIN: ../../../flutter/impeller/tessellator/c/tessellator.h + ../../../flutter/LICENSE
16391640
ORIGIN: ../../../flutter/impeller/tessellator/dart/lib/tessellator.dart + ../../../flutter/LICENSE
@@ -4075,14 +4076,15 @@ FILE: ../../../flutter/impeller/scene/mesh.cc
40754076
FILE: ../../../flutter/impeller/scene/mesh.h
40764077
FILE: ../../../flutter/impeller/scene/node.cc
40774078
FILE: ../../../flutter/impeller/scene/node.h
4079+
FILE: ../../../flutter/impeller/scene/pipeline_key.h
40784080
FILE: ../../../flutter/impeller/scene/scene.cc
40794081
FILE: ../../../flutter/impeller/scene/scene.h
40804082
FILE: ../../../flutter/impeller/scene/scene_context.cc
40814083
FILE: ../../../flutter/impeller/scene/scene_context.h
40824084
FILE: ../../../flutter/impeller/scene/scene_encoder.cc
40834085
FILE: ../../../flutter/impeller/scene/scene_encoder.h
4084-
FILE: ../../../flutter/impeller/scene/shaders/geometry.vert
40854086
FILE: ../../../flutter/impeller/scene/shaders/unlit.frag
4087+
FILE: ../../../flutter/impeller/scene/shaders/unskinned.vert
40864088
FILE: ../../../flutter/impeller/tessellator/c/tessellator.cc
40874089
FILE: ../../../flutter/impeller/tessellator/c/tessellator.h
40884090
FILE: ../../../flutter/impeller/tessellator/dart/lib/tessellator.dart

impeller/scene/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ impeller_component("scene") {
1616
"mesh.h",
1717
"node.cc",
1818
"node.h",
19+
"pipeline_key.h",
1920
"scene.cc",
2021
"scene.h",
2122
"scene_context.cc",

impeller/scene/geometry.cc

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
#include "impeller/renderer/vertex_buffer.h"
1616
#include "impeller/renderer/vertex_buffer_builder.h"
1717
#include "impeller/scene/importer/scene_flatbuffers.h"
18-
#include "impeller/scene/shaders/geometry.vert.h"
19-
#include "third_party/flatbuffers/include/flatbuffers/vector.h"
18+
#include "impeller/scene/shaders/unskinned.vert.h"
2019

2120
namespace impeller {
2221
namespace scene {
@@ -111,8 +110,14 @@ void CuboidGeometry::SetSize(Vector3 size) {
111110
size_ = size;
112111
}
113112

113+
// |Geometry|
114+
GeometryType CuboidGeometry::GetGeometryType() const {
115+
return GeometryType::kUnskinned;
116+
}
117+
118+
// |Geometry|
114119
VertexBuffer CuboidGeometry::GetVertexBuffer(Allocator& allocator) const {
115-
VertexBufferBuilder<GeometryVertexShader::PerVertexData, uint16_t> builder;
120+
VertexBufferBuilder<UnskinnedVertexShader::PerVertexData, uint16_t> builder;
116121
// Layout: position, normal, tangent, uv
117122
builder.AddVertices({
118123
// Front.
@@ -132,6 +137,19 @@ VertexBuffer CuboidGeometry::GetVertexBuffer(Allocator& allocator) const {
132137
return builder.CreateVertexBuffer(allocator);
133138
}
134139

140+
// |Geometry|
141+
void CuboidGeometry::BindToCommand(const SceneContext& scene_context,
142+
HostBuffer& buffer,
143+
const Matrix& transform,
144+
Command& command) const {
145+
command.BindVertices(
146+
GetVertexBuffer(*scene_context.GetContext()->GetResourceAllocator()));
147+
148+
UnskinnedVertexShader::VertInfo info;
149+
info.mvp = transform;
150+
UnskinnedVertexShader::BindVertInfo(command, buffer.EmplaceUniform(info));
151+
}
152+
135153
//------------------------------------------------------------------------------
136154
/// VertexBufferGeometry
137155
///
@@ -144,9 +162,28 @@ void VertexBufferGeometry::SetVertexBuffer(VertexBuffer vertex_buffer) {
144162
vertex_buffer_ = std::move(vertex_buffer);
145163
}
146164

165+
// |Geometry|
166+
GeometryType VertexBufferGeometry::GetGeometryType() const {
167+
return GeometryType::kUnskinned;
168+
}
169+
170+
// |Geometry|
147171
VertexBuffer VertexBufferGeometry::GetVertexBuffer(Allocator& allocator) const {
148172
return vertex_buffer_;
149173
}
150174

175+
// |Geometry|
176+
void VertexBufferGeometry::BindToCommand(const SceneContext& scene_context,
177+
HostBuffer& buffer,
178+
const Matrix& transform,
179+
Command& command) const {
180+
command.BindVertices(
181+
GetVertexBuffer(*scene_context.GetContext()->GetResourceAllocator()));
182+
183+
UnskinnedVertexShader::VertInfo info;
184+
info.mvp = transform;
185+
UnskinnedVertexShader::BindVertInfo(command, buffer.EmplaceUniform(info));
186+
}
187+
151188
} // namespace scene
152189
} // namespace impeller

impeller/scene/geometry.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
#include <memory>
88

99
#include "flutter/fml/macros.h"
10+
#include "impeller/geometry/matrix.h"
1011
#include "impeller/geometry/vector.h"
1112
#include "impeller/renderer/allocator.h"
13+
#include "impeller/renderer/command.h"
1214
#include "impeller/renderer/device_buffer.h"
15+
#include "impeller/renderer/host_buffer.h"
1316
#include "impeller/renderer/vertex_buffer.h"
1417
#include "impeller/scene/importer/scene_flatbuffers.h"
18+
#include "impeller/scene/pipeline_key.h"
19+
#include "impeller/scene/scene_context.h"
1520

1621
namespace impeller {
1722
namespace scene {
@@ -32,7 +37,14 @@ class Geometry {
3237
const fb::MeshPrimitive& mesh,
3338
Allocator& allocator);
3439

40+
virtual GeometryType GetGeometryType() const = 0;
41+
3542
virtual VertexBuffer GetVertexBuffer(Allocator& allocator) const = 0;
43+
44+
virtual void BindToCommand(const SceneContext& scene_context,
45+
HostBuffer& buffer,
46+
const Matrix& transform,
47+
Command& command) const = 0;
3648
};
3749

3850
class CuboidGeometry final : public Geometry {
@@ -43,9 +55,18 @@ class CuboidGeometry final : public Geometry {
4355

4456
void SetSize(Vector3 size);
4557

58+
// |Geometry|
59+
GeometryType GetGeometryType() const override;
60+
4661
// |Geometry|
4762
VertexBuffer GetVertexBuffer(Allocator& allocator) const override;
4863

64+
// |Geometry|
65+
void BindToCommand(const SceneContext& scene_context,
66+
HostBuffer& buffer,
67+
const Matrix& transform,
68+
Command& command) const override;
69+
4970
private:
5071
Vector3 size_;
5172

@@ -60,9 +81,18 @@ class VertexBufferGeometry final : public Geometry {
6081

6182
void SetVertexBuffer(VertexBuffer vertex_buffer);
6283

84+
// |Geometry|
85+
GeometryType GetGeometryType() const override;
86+
6387
// |Geometry|
6488
VertexBuffer GetVertexBuffer(Allocator& allocator) const override;
6589

90+
// |Geometry|
91+
void BindToCommand(const SceneContext& scene_context,
92+
HostBuffer& buffer,
93+
const Matrix& transform,
94+
Command& command) const override;
95+
6696
private:
6797
VertexBuffer vertex_buffer_;
6898

impeller/scene/material.cc

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "impeller/renderer/formats.h"
77
#include "impeller/renderer/sampler_descriptor.h"
88
#include "impeller/renderer/sampler_library.h"
9+
#include "impeller/scene/pipeline_key.h"
910
#include "impeller/scene/scene_context.h"
1011
#include "impeller/scene/shaders/unlit.frag.h"
1112

@@ -64,30 +65,27 @@ void UnlitMaterial::SetVertexColorWeight(Scalar weight) {
6465
}
6566

6667
// |Material|
67-
std::shared_ptr<Pipeline<PipelineDescriptor>> UnlitMaterial::GetPipeline(
68-
const SceneContext& scene_context,
69-
const RenderPass& pass) const {
70-
return scene_context.GetUnlitPipeline(GetContextOptions(pass));
68+
MaterialType UnlitMaterial::GetMaterialType() const {
69+
return MaterialType::kUnlit;
7170
}
7271

7372
// |Material|
7473
void UnlitMaterial::BindToCommand(const SceneContext& scene_context,
7574
HostBuffer& buffer,
7675
Command& command) const {
7776
// Uniform buffer.
78-
UnlitPipeline::FragmentShader::FragInfo info;
77+
UnlitFragmentShader::FragInfo info;
7978
info.color = color_;
8079
info.vertex_color_weight = vertex_color_weight_;
81-
UnlitPipeline::FragmentShader::BindFragInfo(command,
82-
buffer.EmplaceUniform(info));
80+
UnlitFragmentShader::BindFragInfo(command, buffer.EmplaceUniform(info));
8381

8482
// Textures.
8583
SamplerDescriptor sampler_descriptor;
8684
sampler_descriptor.label = "Trilinear";
8785
sampler_descriptor.min_filter = MinMagFilter::kLinear;
8886
sampler_descriptor.mag_filter = MinMagFilter::kLinear;
8987
sampler_descriptor.mip_filter = MipFilter::kLinear;
90-
UnlitPipeline::FragmentShader::BindBaseColorTexture(
88+
UnlitFragmentShader::BindBaseColorTexture(
9189
command,
9290
color_texture_ ? color_texture_ : scene_context.GetPlaceholderTexture(),
9391
scene_context.GetContext()->GetSamplerLibrary()->GetSampler(
@@ -134,10 +132,9 @@ void StandardMaterial::SetEnvironmentMap(
134132
}
135133

136134
// |Material|
137-
std::shared_ptr<Pipeline<PipelineDescriptor>> StandardMaterial::GetPipeline(
138-
const SceneContext& scene_context,
139-
const RenderPass& pass) const {
140-
return nullptr;
135+
MaterialType StandardMaterial::GetMaterialType() const {
136+
// TODO(bdero): Replace this once a PBR shader has landed.
137+
return MaterialType::kUnlit;
141138
}
142139

143140
// |Material|

impeller/scene/material.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "impeller/renderer/formats.h"
1111
#include "impeller/renderer/render_pass.h"
1212
#include "impeller/renderer/texture.h"
13+
#include "impeller/scene/pipeline_key.h"
1314

1415
namespace impeller {
1516
namespace scene {
@@ -47,16 +48,15 @@ class Material {
4748

4849
void SetTranslucent(bool is_translucent);
4950

50-
virtual std::shared_ptr<Pipeline<PipelineDescriptor>> GetPipeline(
51-
const SceneContext& scene_context,
52-
const RenderPass& pass) const = 0;
51+
SceneContextOptions GetContextOptions(const RenderPass& pass) const;
52+
53+
virtual MaterialType GetMaterialType() const = 0;
54+
5355
virtual void BindToCommand(const SceneContext& scene_context,
5456
HostBuffer& buffer,
5557
Command& command) const = 0;
5658

5759
protected:
58-
SceneContextOptions GetContextOptions(const RenderPass& pass) const;
59-
6060
BlendConfig blend_config_;
6161
StencilConfig stencil_config_;
6262
bool is_translucent_ = false;
@@ -73,9 +73,7 @@ class UnlitMaterial final : public Material {
7373
void SetVertexColorWeight(Scalar weight);
7474

7575
// |Material|
76-
std::shared_ptr<Pipeline<PipelineDescriptor>> GetPipeline(
77-
const SceneContext& scene_context,
78-
const RenderPass& pass) const override;
76+
MaterialType GetMaterialType() const override;
7977

8078
// |Material|
8179
void BindToCommand(const SceneContext& scene_context,
@@ -104,9 +102,7 @@ class StandardMaterial final : public Material {
104102
void SetEnvironmentMap(std::shared_ptr<Texture> environment_map);
105103

106104
// |Material|
107-
std::shared_ptr<Pipeline<PipelineDescriptor>> GetPipeline(
108-
const SceneContext& scene_context,
109-
const RenderPass& pass) const override;
105+
MaterialType GetMaterialType() const override;
110106

111107
// |Material|
112108
void BindToCommand(const SceneContext& scene_context,

impeller/scene/pipeline_key.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 "flutter/fml/hash_combine.h"
8+
9+
namespace impeller {
10+
namespace scene {
11+
12+
enum class GeometryType {
13+
kUnskinned = 0,
14+
kLastType = kUnskinned,
15+
};
16+
enum class MaterialType {
17+
kUnlit = 0,
18+
kLastType = kUnlit,
19+
};
20+
21+
struct PipelineKey {
22+
GeometryType geometry_type = GeometryType::kUnskinned;
23+
MaterialType material_type = MaterialType::kUnlit;
24+
25+
struct Hash {
26+
constexpr std::size_t operator()(const PipelineKey& o) const {
27+
return fml::HashCombine(o.geometry_type, o.material_type);
28+
}
29+
};
30+
31+
struct Equal {
32+
constexpr bool operator()(const PipelineKey& lhs,
33+
const PipelineKey& rhs) const {
34+
return lhs.geometry_type == rhs.geometry_type &&
35+
lhs.material_type == rhs.material_type;
36+
}
37+
};
38+
};
39+
40+
} // namespace scene
41+
} // namespace impeller

impeller/scene/scene_context.cc

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
#include "impeller/scene/scene_context.h"
66
#include "impeller/renderer/formats.h"
7+
#include "impeller/scene/material.h"
8+
#include "impeller/scene/shaders/unlit.frag.h"
9+
#include "impeller/scene/shaders/unskinned.vert.h"
710

811
namespace impeller {
912
namespace scene {
@@ -26,25 +29,15 @@ void SceneContextOptions::ApplyToPipelineDescriptor(
2629
desc.SetPrimitiveType(primitive_type);
2730
}
2831

29-
template <typename PipelineT>
30-
static std::unique_ptr<PipelineT> CreateDefaultPipeline(
31-
const Context& context) {
32-
auto desc = PipelineT::Builder::MakeDefaultPipelineDescriptor(context);
33-
if (!desc.has_value()) {
34-
return nullptr;
35-
}
36-
// Apply default ContentContextOptions to the descriptor.
37-
SceneContextOptions{}.ApplyToPipelineDescriptor(*desc);
38-
return std::make_unique<PipelineT>(context, desc);
39-
}
40-
4132
SceneContext::SceneContext(std::shared_ptr<Context> context)
4233
: context_(std::move(context)) {
4334
if (!context_ || !context_->IsValid()) {
4435
return;
4536
}
4637

47-
unlit_pipeline_[{}] = CreateDefaultPipeline<UnlitPipeline>(*context_);
38+
pipelines_[{PipelineKey{GeometryType::kUnskinned, MaterialType::kUnlit}}] =
39+
MakePipelineVariants<UnskinnedVertexShader, UnlitFragmentShader>(
40+
*context_);
4841

4942
{
5043
impeller::TextureDescriptor texture_descriptor;
@@ -72,6 +65,18 @@ SceneContext::SceneContext(std::shared_ptr<Context> context)
7265

7366
SceneContext::~SceneContext() = default;
7467

68+
std::shared_ptr<Pipeline<PipelineDescriptor>> SceneContext::GetPipeline(
69+
PipelineKey key,
70+
SceneContextOptions opts) const {
71+
if (!IsValid()) {
72+
return nullptr;
73+
}
74+
if (auto found = pipelines_.find(key); found != pipelines_.end()) {
75+
return found->second->GetPipeline(opts);
76+
}
77+
return nullptr;
78+
}
79+
7580
bool SceneContext::IsValid() const {
7681
return is_valid_;
7782
}

0 commit comments

Comments
 (0)