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

Commit 4b38736

Browse files
authored
[Impeller Scene] Import materials, load embedded textures (#38577)
1 parent b9bf51d commit 4b38736

18 files changed

+463
-129
lines changed

impeller/aiks/aiks_unittests.cc

+1-13
Original file line numberDiff line numberDiff line change
@@ -1708,25 +1708,13 @@ TEST_P(AiksTest, SaveLayerFiltersScaleWithTransform) {
17081708
TEST_P(AiksTest, SceneColorSource) {
17091709
// Load up the scene.
17101710
auto mapping =
1711-
flutter::testing::OpenFixtureAsMapping("flutter_logo.glb.ipscene");
1711+
flutter::testing::OpenFixtureAsMapping("flutter_logo_baked.glb.ipscene");
17121712
ASSERT_NE(mapping, nullptr);
17131713

17141714
std::shared_ptr<scene::Node> gltf_scene = scene::Node::MakeFromFlatbuffer(
17151715
*mapping, *GetContext()->GetResourceAllocator());
17161716
ASSERT_NE(gltf_scene, nullptr);
17171717

1718-
// Assign a material (temporary stopgap).
1719-
std::shared_ptr<scene::UnlitMaterial> material = scene::Material::MakeUnlit();
1720-
auto color_baked = CreateTextureForFixture("flutter_logo_baked.png");
1721-
ASSERT_NE(color_baked, nullptr);
1722-
material->SetColorTexture(std::move(color_baked));
1723-
material->SetVertexColorWeight(0);
1724-
1725-
ASSERT_EQ(gltf_scene->GetChildren().size(), 1u);
1726-
ASSERT_EQ(gltf_scene->GetChildren()[0]->GetMesh().GetPrimitives().size(), 1u);
1727-
gltf_scene->GetChildren()[0]->GetMesh().GetPrimitives()[0].material =
1728-
std::move(material);
1729-
17301718
auto callback = [&](AiksContext& renderer, RenderTarget& render_target) {
17311719
Paint paint;
17321720

impeller/fixtures/BUILD.gn

+2-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impeller_shaders("shader_fixtures") {
3535

3636
scenec("scene_fixtures") {
3737
geometry = [
38-
"flutter_logo.glb",
38+
"flutter_logo_baked.glb",
3939
"two_triangles.glb",
4040
]
4141
type = "gltf"
@@ -61,8 +61,7 @@ test_fixtures("file_fixtures") {
6161
"blue_noise.png",
6262
"boston.jpg",
6363
"embarcadero.jpg",
64-
"flutter_logo.glb",
65-
"flutter_logo_baked.png",
64+
"flutter_logo_baked.glb",
6665
"kalimba.jpg",
6766
"multiple_stages.hlsl",
6867
"resources_limit.vert",

impeller/fixtures/flutter_logo.glb

-18.2 KB
Binary file not shown.

impeller/playground/playground.cc

+38-24
Original file line numberDiff line numberDiff line change
@@ -329,69 +329,83 @@ bool Playground::OpenPlaygroundHere(SinglePassCallback pass_callback) {
329329
});
330330
}
331331

332-
std::optional<DecompressedImage> Playground::LoadFixtureImageRGBA(
333-
const char* fixture_name) const {
334-
if (!renderer_ || fixture_name == nullptr) {
335-
return std::nullopt;
336-
}
337-
338-
auto compressed_image =
339-
CompressedImage::Create(OpenAssetAsMapping(fixture_name));
332+
std::shared_ptr<CompressedImage> Playground::LoadFixtureImageCompressed(
333+
std::shared_ptr<fml::Mapping> mapping) const {
334+
auto compressed_image = CompressedImage::Create(std::move(mapping));
340335
if (!compressed_image) {
341336
VALIDATION_LOG << "Could not create compressed image.";
337+
return nullptr;
338+
}
339+
340+
return compressed_image;
341+
}
342+
343+
std::optional<DecompressedImage> Playground::DecodeImageRGBA(
344+
const std::shared_ptr<CompressedImage>& compressed) const {
345+
if (compressed == nullptr) {
342346
return std::nullopt;
343347
}
344348
// The decoded image is immediately converted into RGBA as that format is
345349
// known to be supported everywhere. For image sources that don't need 32
346350
// bit pixel strides, this is overkill. Since this is a test fixture we
347351
// aren't necessarily trying to eke out memory savings here and instead
348352
// favor simplicity.
349-
auto image = compressed_image->Decode().ConvertToRGBA();
353+
auto image = compressed->Decode().ConvertToRGBA();
350354
if (!image.IsValid()) {
351-
VALIDATION_LOG << "Could not find fixture named " << fixture_name;
355+
VALIDATION_LOG << "Could not decode image.";
352356
return std::nullopt;
353357
}
354358

355359
return image;
356360
}
357361

358362
std::shared_ptr<Texture> Playground::CreateTextureForFixture(
359-
const char* fixture_name,
363+
DecompressedImage& decompressed_image,
360364
bool enable_mipmapping) const {
361-
auto image = LoadFixtureImageRGBA(fixture_name);
362-
if (!image.has_value()) {
363-
return nullptr;
364-
}
365-
366365
auto texture_descriptor = TextureDescriptor{};
367366
texture_descriptor.storage_mode = StorageMode::kHostVisible;
368367
texture_descriptor.format = PixelFormat::kR8G8B8A8UNormInt;
369-
texture_descriptor.size = image->GetSize();
368+
texture_descriptor.size = decompressed_image.GetSize();
370369
texture_descriptor.mip_count =
371-
enable_mipmapping ? image->GetSize().MipCount() : 1u;
370+
enable_mipmapping ? decompressed_image.GetSize().MipCount() : 1u;
372371

373372
auto texture = renderer_->GetContext()->GetResourceAllocator()->CreateTexture(
374373
texture_descriptor);
375374
if (!texture) {
376-
VALIDATION_LOG << "Could not allocate texture for fixture " << fixture_name;
375+
VALIDATION_LOG << "Could not allocate texture for fixture.";
377376
return nullptr;
378377
}
379-
texture->SetLabel(fixture_name);
380378

381-
auto uploaded = texture->SetContents(image->GetAllocation());
379+
auto uploaded = texture->SetContents(decompressed_image.GetAllocation());
382380
if (!uploaded) {
383-
VALIDATION_LOG << "Could not upload texture to device memory for fixture "
384-
<< fixture_name;
381+
VALIDATION_LOG << "Could not upload texture to device memory for fixture.";
385382
return nullptr;
386383
}
387384
return texture;
388385
}
389386

387+
std::shared_ptr<Texture> Playground::CreateTextureForFixture(
388+
std::shared_ptr<fml::Mapping> mapping,
389+
bool enable_mipmapping) const {
390+
auto image = DecodeImageRGBA(LoadFixtureImageCompressed(std::move(mapping)));
391+
if (!image.has_value()) {
392+
return nullptr;
393+
}
394+
return CreateTextureForFixture(image.value());
395+
}
396+
397+
std::shared_ptr<Texture> Playground::CreateTextureForFixture(
398+
const char* fixture_name,
399+
bool enable_mipmapping) const {
400+
return CreateTextureForFixture(OpenAssetAsMapping(fixture_name));
401+
}
402+
390403
std::shared_ptr<Texture> Playground::CreateTextureCubeForFixture(
391404
std::array<const char*, 6> fixture_names) const {
392405
std::array<DecompressedImage, 6> images;
393406
for (size_t i = 0; i < fixture_names.size(); i++) {
394-
auto image = LoadFixtureImageRGBA(fixture_names[i]);
407+
auto image = DecodeImageRGBA(
408+
LoadFixtureImageCompressed(OpenAssetAsMapping(fixture_names[i])));
395409
if (!image.has_value()) {
396410
return nullptr;
397411
}

impeller/playground/playground.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "flutter/fml/time/time_delta.h"
1212

1313
#include "impeller/geometry/point.h"
14+
#include "impeller/image/compressed_image.h"
15+
#include "impeller/image/decompressed_image.h"
1416
#include "impeller/renderer/renderer.h"
1517
#include "impeller/renderer/texture.h"
1618
#include "impeller/runtime_stage/runtime_stage.h"
@@ -61,8 +63,19 @@ class Playground {
6163

6264
bool OpenPlaygroundHere(SinglePassCallback pass_callback);
6365

64-
std::optional<DecompressedImage> LoadFixtureImageRGBA(
65-
const char* fixture_name) const;
66+
std::shared_ptr<CompressedImage> LoadFixtureImageCompressed(
67+
std::shared_ptr<fml::Mapping> mapping) const;
68+
69+
std::optional<DecompressedImage> DecodeImageRGBA(
70+
const std::shared_ptr<CompressedImage>& compressed) const;
71+
72+
std::shared_ptr<Texture> CreateTextureForFixture(
73+
DecompressedImage& decompressed_image,
74+
bool enable_mipmapping = false) const;
75+
76+
std::shared_ptr<Texture> CreateTextureForFixture(
77+
std::shared_ptr<fml::Mapping> mapping,
78+
bool enable_mipmapping = false) const;
6679

6780
std::shared_ptr<Texture> CreateTextureForFixture(
6881
const char* fixture_name,

impeller/scene/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ impeller_component("scene") {
2727

2828
public_deps = [
2929
"../renderer",
30+
"importer:conversions",
3031
"importer:importer_flatbuffers",
3132
"shaders",
3233
]

impeller/scene/importer/BUILD.gn

+15-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,26 @@ flatbuffers("importer_flatbuffers") {
1717
public_deps = [ "//third_party/flatbuffers" ]
1818
}
1919

20+
impeller_component("conversions") {
21+
sources = [
22+
"conversions.cc",
23+
"conversions.h",
24+
]
25+
26+
public_deps = [
27+
":importer_flatbuffers",
28+
"../../base",
29+
"../../geometry",
30+
"//flutter/fml",
31+
]
32+
}
33+
2034
impeller_component("importer_lib") {
2135
# Current versions of libcxx have deprecated some of the UTF-16 string
2236
# conversion APIs.
2337
defines = [ "_LIBCPP_DISABLE_DEPRECATION_WARNINGS" ]
2438

2539
sources = [
26-
"conversions.cc",
27-
"conversions.h",
2840
"importer.h",
2941
"importer_gltf.cc",
3042
"switches.cc",
@@ -35,6 +47,7 @@ impeller_component("importer_lib") {
3547
]
3648

3749
public_deps = [
50+
":conversions",
3851
":importer_flatbuffers",
3952
"../../base",
4053
"../../compiler:utilities",

impeller/scene/importer/conversions.cc

+8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ fb::Color ToFBColor(const Color c) {
7575
return fb::Color(c.red, c.green, c.blue, c.alpha);
7676
}
7777

78+
std::unique_ptr<fb::Color> ToFBColor(const std::vector<double>& c) {
79+
auto* color = new fb::Color(c.size() > 0 ? c[0] : 1, //
80+
c.size() > 1 ? c[1] : 1, //
81+
c.size() > 2 ? c[2] : 1, //
82+
c.size() > 3 ? c[3] : 1);
83+
return std::unique_ptr<fb::Color>(color);
84+
}
85+
7886
} // namespace importer
7987
} // namespace scene
8088
} // namespace impeller

impeller/scene/importer/conversions.h

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <cstddef>
88
#include <map>
9+
#include <vector>
910

1011
#include "impeller/geometry/matrix.h"
1112
#include "impeller/scene/importer/scene_flatbuffers.h"
@@ -44,6 +45,8 @@ fb::Vec4 ToFBVec4(const Vector4 v);
4445

4546
fb::Color ToFBColor(const Color c);
4647

48+
std::unique_ptr<fb::Color> ToFBColor(const std::vector<double>& c);
49+
4750
} // namespace importer
4851
} // namespace scene
4952
} // namespace impeller

0 commit comments

Comments
 (0)