| Flutter Impeller |

material.cc
Go to the documentation of this file.
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/material.h"
6 #include "impeller/base/validation.h"
7 #include "impeller/core/formats.h"
8 #include "impeller/core/sampler_descriptor.h"
9 #include "impeller/scene/importer/conversions.h"
10 #include "impeller/scene/importer/scene_flatbuffers.h"
11 #include "impeller/scene/pipeline_key.h"
12 #include "impeller/scene/scene_context.h"
13 #include "impeller/scene/shaders/unlit.frag.h"
14
15 #include <memory>
16
17 namespace impeller {
18 namespace scene {
19
20 //------------------------------------------------------------------------------
21 /// Material
22 ///
23
24 Material::~Material() = default;
25
26 std::unique_ptr<Material> Material::MakeFromFlatbuffer(
27 const fb::Material& material,
28 const std::vector<std::shared_ptr<Texture>>& textures) {
29 switch (material.type()) {
30 case fb::MaterialType::kUnlit:
31 return UnlitMaterial::MakeFromFlatbuffer(material, textures);
32 case fb::MaterialType::kPhysicallyBased:
33 return PhysicallyBasedMaterial::MakeFromFlatbuffer(material, textures);
34 }
35 }
36
37 std::unique_ptr<UnlitMaterial> Material::MakeUnlit() {
38 return std::make_unique<UnlitMaterial>();
39 }
40
41 std::unique_ptr<PhysicallyBasedMaterial> Material::MakePhysicallyBased() {
42 return std::make_unique<PhysicallyBasedMaterial>();
43 }
44
45 void Material::SetVertexColorWeight(Scalar weight) {
46 vertex_color_weight_ = weight;
47 }
48
49 void Material::SetBlendConfig(BlendConfig blend_config) {
50 blend_config_ = blend_config;
51 }
52
53 void Material::SetStencilConfig(StencilConfig stencil_config) {
54 stencil_config_ = stencil_config;
55 }
56
57 void Material::SetTranslucent(bool is_translucent) {
58 is_translucent_ = is_translucent;
59 }
60
61 SceneContextOptions Material::GetContextOptions(const RenderPass& pass) const {
62 // TODO(bdero): Pipeline blend and stencil config.
63 return {.sample_count = pass.GetRenderTarget().GetSampleCount()};
64 }
65
66 //------------------------------------------------------------------------------
67 /// UnlitMaterial
68 ///
69
70 std::unique_ptr<UnlitMaterial> UnlitMaterial::MakeFromFlatbuffer(
71 const fb::Material& material,
72 const std::vector<std::shared_ptr<Texture>>& textures) {
73 if (material.type() != fb::MaterialType::kUnlit) {
74 VALIDATION_LOG << "Cannot unpack unlit material because the ipscene "
75 "material type is not unlit.";
76 return nullptr;
77 }
78
79 auto result = Material::MakeUnlit();
80
81 if (material.base_color_factor()) {
82 result->SetColor(importer::ToColor(*material.base_color_factor()));
83 }
84
85 if (material.base_color_texture() >= 0 &&
86 material.base_color_texture() < static_cast<int32_t>(textures.size())) {
87 result->SetColorTexture(textures[material.base_color_texture()]);
88 }
89
90 return result;
91 }
92
93 UnlitMaterial::~UnlitMaterial() = default;
94
95 void UnlitMaterial::SetColor(Color color) {
96 color_ = color;
97 }
98
99 void UnlitMaterial::SetColorTexture(std::shared_ptr<Texture> color_texture) {
100 color_texture_ = std::move(color_texture);
101 }
102
103 // |Material|
104 MaterialType UnlitMaterial::GetMaterialType() const {
105 return MaterialType::kUnlit;
106 }
107
108 // |Material|
109 void UnlitMaterial::BindToCommand(const SceneContext& scene_context,
110 HostBuffer& buffer,
111 Command& command) const {
112 // Uniform buffer.
113 UnlitFragmentShader::FragInfo info;
114 info.color = color_;
115 info.vertex_color_weight = vertex_color_weight_;
116 UnlitFragmentShader::BindFragInfo(command, buffer.EmplaceUniform(info));
117
118 // Textures.
119 SamplerDescriptor sampler_descriptor;
120 sampler_descriptor.label = "Trilinear";
121 sampler_descriptor.min_filter = MinMagFilter::kLinear;
122 sampler_descriptor.mag_filter = MinMagFilter::kLinear;
123 sampler_descriptor.mip_filter = MipFilter::kLinear;
124 UnlitFragmentShader::BindBaseColorTexture(
125 command,
126 color_texture_ ? color_texture_ : scene_context.GetPlaceholderTexture(),
127 scene_context.GetContext()->GetSamplerLibrary()->GetSampler(
128 sampler_descriptor));
129 }
130
131 //------------------------------------------------------------------------------
132 /// StandardMaterial
133 ///
134
135 std::unique_ptr<PhysicallyBasedMaterial>
136 PhysicallyBasedMaterial::MakeFromFlatbuffer(
137 const fb::Material& material,
138 const std::vector<std::shared_ptr<Texture>>& textures) {
139 if (material.type() != fb::MaterialType::kPhysicallyBased) {
140 VALIDATION_LOG << "Cannot unpack unlit material because the ipscene "
141 "material type is not unlit.";
142 return nullptr;
143 }
144
145 auto result = Material::MakePhysicallyBased();
146
147 result->SetAlbedo(material.base_color_factor()
148 ? importer::ToColor(*material.base_color_factor())
149 : Color::White());
150 result->SetRoughness(material.roughness_factor());
151 result->SetMetallic(material.metallic_factor());
152
153 if (material.base_color_texture() >= 0 &&
154 material.base_color_texture() < static_cast<int32_t>(textures.size())) {
155 result->SetAlbedoTexture(textures[material.base_color_texture()]);
156 result->SetVertexColorWeight(0);
157 }
158 if (material.metallic_roughness_texture() >= 0 &&
159 material.metallic_roughness_texture() <
160 static_cast<int32_t>(textures.size())) {
161 result->SetMetallicRoughnessTexture(
162 textures[material.metallic_roughness_texture()]);
163 }
164 if (material.normal_texture() >= 0 &&
165 material.normal_texture() < static_cast<int32_t>(textures.size())) {
166 result->SetNormalTexture(textures[material.normal_texture()]);
167 }
168 if (material.occlusion_texture() >= 0 &&
169 material.occlusion_texture() < static_cast<int32_t>(textures.size())) {
170 result->SetOcclusionTexture(textures[material.occlusion_texture()]);
171 }
172
173 return result;
174 }
175
176 PhysicallyBasedMaterial::~PhysicallyBasedMaterial() = default;
177
178 void PhysicallyBasedMaterial::SetAlbedo(Color albedo) {
179 albedo_ = albedo;
180 }
181
182 void PhysicallyBasedMaterial::SetRoughness(Scalar roughness) {
183 roughness_ = roughness;
184 }
185
186 void PhysicallyBasedMaterial::SetMetallic(Scalar metallic) {
187 metallic_ = metallic;
188 }
189
190 void PhysicallyBasedMaterial::SetAlbedoTexture(
191 std::shared_ptr<Texture> albedo_texture) {
192 albedo_texture_ = std::move(albedo_texture);
193 }
194
195 void PhysicallyBasedMaterial::SetMetallicRoughnessTexture(
196 std::shared_ptr<Texture> metallic_roughness_texture) {
197 metallic_roughness_texture_ = std::move(metallic_roughness_texture);
198 }
199
200 void PhysicallyBasedMaterial::SetNormalTexture(
201 std::shared_ptr<Texture> normal_texture) {
202 normal_texture_ = std::move(normal_texture);
203 }
204
205 void PhysicallyBasedMaterial::SetOcclusionTexture(
206 std::shared_ptr<Texture> occlusion_texture) {
207 occlusion_texture_ = std::move(occlusion_texture);
208 }
209
210 void PhysicallyBasedMaterial::SetEnvironmentMap(
211 std::shared_ptr<Texture> environment_map) {
212 environment_map_ = std::move(environment_map);
213 }
214
215 // |Material|
216 MaterialType PhysicallyBasedMaterial::GetMaterialType() const {
217 // TODO(bdero): Replace this once a PBR shader has landed.
218 return MaterialType::kUnlit;
219 }
220
221 // |Material|
222 void PhysicallyBasedMaterial::BindToCommand(const SceneContext& scene_context,
223 HostBuffer& buffer,
224 Command& command) const {}
225
226 } // namespace scene
227 } // namespace impeller
impeller::Command
An object used to specify work to the GPU along with references to resources the GPU will used when d...
Definition: command.h:92
impeller::scene::PhysicallyBasedMaterial::SetEnvironmentMap
void SetEnvironmentMap(std::shared_ptr< Texture > environment_map)
Definition: material.cc:210
pipeline_key.h
impeller::RenderPass::GetRenderTarget
const RenderTarget & GetRenderTarget() const
Definition: render_pass.cc:43
impeller::scene::Material::StencilConfig
Definition: material.h:38
scene_context.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::scene::PhysicallyBasedMaterial::SetMetallic
void SetMetallic(Scalar metallic)
Definition: material.cc:186
impeller::scene::PhysicallyBasedMaterial::SetMetallicRoughnessTexture
void SetMetallicRoughnessTexture(std::shared_ptr< Texture > metallic_roughness_texture)
Definition: material.cc:195
impeller::scene::SceneContextOptions::sample_count
SampleCount sample_count
Definition: scene_context.h:19
impeller::Color
Definition: color.h:124
impeller::HostBuffer
Definition: host_buffer.h:20
impeller::scene::PhysicallyBasedMaterial::SetAlbedo
void SetAlbedo(Color albedo)
Definition: material.cc:178
impeller::scene::UnlitMaterial::~UnlitMaterial
~UnlitMaterial()
formats.h
impeller::scene::UnlitMaterial::SetColorTexture
void SetColorTexture(std::shared_ptr< Texture > color_texture)
Definition: material.cc:99
impeller::scene::Material::MakePhysicallyBased
static std::unique_ptr< PhysicallyBasedMaterial > MakePhysicallyBased()
Definition: material.cc:41
impeller::scene::SceneContext::GetPlaceholderTexture
std::shared_ptr< Texture > GetPlaceholderTexture() const
Definition: scene_context.cc:109
impeller::scene::Material::SetBlendConfig
void SetBlendConfig(BlendConfig blend_config)
Definition: material.cc:49
impeller::scene::SceneContext
Definition: scene_context.h:40
impeller::scene::Material::blend_config_
BlendConfig blend_config_
Definition: material.h:68
impeller::scene::Material::GetContextOptions
SceneContextOptions GetContextOptions(const RenderPass &pass) const
Definition: material.cc:61
validation.h
impeller::scene::UnlitMaterial::GetMaterialType
MaterialType GetMaterialType() const override
Definition: material.cc:104
conversions.h
impeller::scene::UnlitMaterial::BindToCommand
void BindToCommand(const SceneContext &scene_context, HostBuffer &buffer, Command &command) const override
Definition: material.cc:109
impeller::scene::SceneContextOptions
Definition: scene_context.h:18
impeller::SamplerDescriptor::mag_filter
MinMagFilter mag_filter
Definition: sampler_descriptor.h:17
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:15
impeller::scene::MaterialType
MaterialType
Definition: pipeline_key.h:18
impeller::SamplerDescriptor::min_filter
MinMagFilter min_filter
Definition: sampler_descriptor.h:16
impeller::scene::importer::ToColor
Color ToColor(const fb::Color &c)
Definition: conversions.cc:46
impeller::scene::Material::SetVertexColorWeight
void SetVertexColorWeight(Scalar weight)
Definition: material.cc:45
material.h
impeller::scene::UnlitMaterial::SetColor
void SetColor(Color color)
Definition: material.cc:95
impeller::MinMagFilter::kLinear
@ kLinear
impeller::scene::Material::SetStencilConfig
void SetStencilConfig(StencilConfig stencil_config)
Definition: material.cc:53
impeller::scene::PhysicallyBasedMaterial::SetNormalTexture
void SetNormalTexture(std::shared_ptr< Texture > normal_texture)
Definition: material.cc:200
impeller::scene::Material::BlendConfig
Definition: material.h:29
impeller::scene::MaterialType::kUnlit
@ kUnlit
impeller::scene::Material::is_translucent_
bool is_translucent_
Definition: material.h:70
impeller::Color::White
static constexpr Color White()
Definition: color.h:256
impeller::scene::PhysicallyBasedMaterial::BindToCommand
void BindToCommand(const SceneContext &scene_context, HostBuffer &buffer, Command &command) const override
Definition: material.cc:222
impeller::scene::UnlitMaterial::MakeFromFlatbuffer
static std::unique_ptr< UnlitMaterial > MakeFromFlatbuffer(const fb::Material &material, const std::vector< std::shared_ptr< Texture >> &textures)
Definition: material.cc:70
impeller::scene::PhysicallyBasedMaterial::SetOcclusionTexture
void SetOcclusionTexture(std::shared_ptr< Texture > occlusion_texture)
Definition: material.cc:205
impeller::scene::PhysicallyBasedMaterial::GetMaterialType
MaterialType GetMaterialType() const override
Definition: material.cc:216
impeller::scene::Material::MakeFromFlatbuffer
static std::unique_ptr< Material > MakeFromFlatbuffer(const fb::Material &material, const std::vector< std::shared_ptr< Texture >> &textures)
Definition: material.cc:26
impeller::SamplerDescriptor::mip_filter
MipFilter mip_filter
Definition: sampler_descriptor.h:18
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:29
impeller::scene::PhysicallyBasedMaterial::SetRoughness
void SetRoughness(Scalar roughness)
Definition: material.cc:182
sampler_descriptor.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:67
impeller::scene::Material::~Material
virtual ~Material()
impeller::scene::Material::MakeUnlit
static std::unique_ptr< UnlitMaterial > MakeUnlit()
Definition: material.cc:37
impeller::scene::PhysicallyBasedMaterial::SetAlbedoTexture
void SetAlbedoTexture(std::shared_ptr< Texture > albedo_texture)
Definition: material.cc:190
impeller::scene::SceneContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: scene_context.cc:105
impeller::MipFilter::kLinear
@ kLinear
impeller::HostBuffer::EmplaceUniform
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:41
impeller::SamplerDescriptor::label
std::string label
Definition: sampler_descriptor.h:24
impeller::scene::Material::vertex_color_weight_
Scalar vertex_color_weight_
Definition: material.h:67
impeller::scene::PhysicallyBasedMaterial::~PhysicallyBasedMaterial
~PhysicallyBasedMaterial()
impeller::RenderTarget::GetSampleCount
SampleCount GetSampleCount() const
Definition: render_target.cc:126
impeller::scene::PhysicallyBasedMaterial::MakeFromFlatbuffer
static std::unique_ptr< PhysicallyBasedMaterial > MakeFromFlatbuffer(const fb::Material &material, const std::vector< std::shared_ptr< Texture >> &textures)
Definition: material.cc:136
impeller::scene::Material::stencil_config_
StencilConfig stencil_config_
Definition: material.h:69
impeller
Definition: aiks_context.cc:10
impeller::scene::Material::SetTranslucent
void SetTranslucent(bool is_translucent)
Definition: material.cc:57