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