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

Commit 972e8d8

Browse files
authored
[Impeller Scene] Add offline mesh importer (#37981)
1 parent 4199b10 commit 972e8d8

File tree

13 files changed

+514
-2
lines changed

13 files changed

+514
-2
lines changed

ci/licenses_golden/licenses_flutter

+7
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,13 @@ FILE: ../../../flutter/impeller/scene/camera.cc
16481648
FILE: ../../../flutter/impeller/scene/camera.h
16491649
FILE: ../../../flutter/impeller/scene/geometry.cc
16501650
FILE: ../../../flutter/impeller/scene/geometry.h
1651+
FILE: ../../../flutter/impeller/scene/importer/importer.h
1652+
FILE: ../../../flutter/impeller/scene/importer/importer_gltf.cc
1653+
FILE: ../../../flutter/impeller/scene/importer/importer_main.cc
1654+
FILE: ../../../flutter/impeller/scene/importer/mesh.fbs
1655+
FILE: ../../../flutter/impeller/scene/importer/switches.cc
1656+
FILE: ../../../flutter/impeller/scene/importer/switches.h
1657+
FILE: ../../../flutter/impeller/scene/importer/types.h
16511658
FILE: ../../../flutter/impeller/scene/material.cc
16521659
FILE: ../../../flutter/impeller/scene/material.h
16531660
FILE: ../../../flutter/impeller/scene/scene.cc

impeller/compiler/BUILD.gn

+19-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@
55
import("//flutter/impeller/tools/impeller.gni")
66
import("//flutter/shell/version/version.gni")
77

8+
impeller_component("utilities") {
9+
# Current versions of libcxx have deprecated some of the UTF-16 string
10+
# conversion APIs.
11+
defines = [ "_LIBCPP_DISABLE_DEPRECATION_WARNINGS" ]
12+
13+
sources = [
14+
"utilities.cc",
15+
"utilities.h",
16+
]
17+
18+
public_deps = [
19+
"../base",
20+
"../geometry",
21+
"../runtime_stage",
22+
"//flutter/fml",
23+
]
24+
}
25+
826
impeller_component("compiler_lib") {
927
include_dirs = [ "//third_party/vulkan-deps/spirv-cross/src/" ]
1028

@@ -34,11 +52,10 @@ impeller_component("compiler_lib") {
3452
"switches.h",
3553
"types.cc",
3654
"types.h",
37-
"utilities.cc",
38-
"utilities.h",
3955
]
4056

4157
public_deps = [
58+
":utilities",
4259
"../base",
4360
"../geometry",
4461
"../runtime_stage",

impeller/fixtures/BUILD.gn

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ impeller_shaders("shader_fixtures") {
3333
}
3434
}
3535

36+
scene_importer("geometry_fixtures") {
37+
geometry = [ "flutter_logo.glb" ]
38+
type = "gltf"
39+
}
40+
3641
impellerc("runtime_stages") {
3742
shaders = [
3843
"ink_sparkle.frag",
@@ -88,6 +93,7 @@ group("fixtures") {
8893

8994
public_deps = [
9095
":file_fixtures",
96+
":geometry_fixtures",
9197
":shader_fixtures",
9298
]
9399
}

impeller/fixtures/flutter_logo.glb

18.2 KB
Binary file not shown.

impeller/scene/importer/BUILD.gn

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
import("//flutter/shell/version/version.gni")
7+
import("//third_party/flatbuffers/flatbuffers.gni")
8+
9+
config("runtime_stage_config") {
10+
configs = [ "//flutter/impeller:impeller_public_config" ]
11+
include_dirs = [ "$root_gen_dir/flutter" ]
12+
}
13+
14+
flatbuffers("importer_flatbuffers") {
15+
flatbuffers = [ "mesh.fbs" ]
16+
public_configs = [ ":runtime_stage_config" ]
17+
public_deps = [ "//third_party/flatbuffers" ]
18+
}
19+
20+
impeller_component("importer_lib") {
21+
# Current versions of libcxx have deprecated some of the UTF-16 string
22+
# conversion APIs.
23+
defines = [ "_LIBCPP_DISABLE_DEPRECATION_WARNINGS" ]
24+
25+
sources = [
26+
"importer.h",
27+
"importer_gltf.cc",
28+
"switches.cc",
29+
"switches.h",
30+
"types.h",
31+
]
32+
33+
public_deps = [
34+
":importer_flatbuffers",
35+
"../../base",
36+
"../../compiler:utilities",
37+
"../../geometry",
38+
"//flutter/fml",
39+
40+
# All third_party deps must be reflected below in the scene_importer_license
41+
# target.
42+
# TODO(bdero): Fix tinygltf compilation warnings.
43+
#"//third_party/tinygltf",
44+
]
45+
}
46+
47+
generated_file("scene_importer_license") {
48+
source_path = rebase_path(".", "//flutter")
49+
git_url = "https://github.com/flutter/engine/tree/$engine_version"
50+
outputs = [ "$target_gen_dir/LICENSE.scene_importer.md" ]
51+
contents = [
52+
"# scene_importer",
53+
"",
54+
"This tool is used by the Flutter SDK to import 3D geometry.",
55+
"",
56+
"Source code for this tool: [flutter/engine/$source_path]($git_url/$source_path).",
57+
"",
58+
"## Licenses",
59+
"",
60+
"### scene_importer",
61+
"",
62+
read_file("//flutter/sky/packages/sky_engine/LICENSE", "string"),
63+
"",
64+
65+
# These licenses are ignored by the main license checker, since they are not
66+
# shipped to end-application binaries and only shipped as part of developer
67+
# tooling in scene_importer. Add them here.
68+
"## Additional open source licenses",
69+
"",
70+
"### tinygltf",
71+
"",
72+
read_file("//third_party/tinygltf/LICENSE", "string"),
73+
]
74+
}
75+
76+
group("importer") {
77+
deps = [
78+
":scene_importer",
79+
":scene_importer_license",
80+
]
81+
}
82+
83+
impeller_component("scene_importer") {
84+
target_type = "executable"
85+
86+
sources = [ "importer_main.cc" ]
87+
88+
deps = [ ":importer_lib" ]
89+
90+
metadata = {
91+
entitlement_file_path = [ "scene_importer" ]
92+
}
93+
}

impeller/scene/importer/importer.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 <array>
6+
#include <memory>
7+
8+
#include "flutter/fml/mapping.h"
9+
#include "impeller/scene/importer/mesh_flatbuffers.h"
10+
11+
namespace impeller {
12+
namespace scene {
13+
namespace importer {
14+
15+
bool ParseGLTF(const fml::Mapping& source_mapping, fb::MeshT& out_mesh);
16+
17+
}
18+
} // namespace scene
19+
} // namespace impeller
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 <array>
6+
#include <memory>
7+
8+
#include "impeller/scene/importer/importer.h"
9+
10+
#include "flutter/fml/mapping.h"
11+
12+
namespace impeller {
13+
namespace scene {
14+
namespace importer {
15+
16+
bool ParseGLTF(const fml::Mapping& source_mapping, fb::MeshT& out_mesh) {
17+
// TODO(bdero): Parse source_mapping and populare out_mesh with just the first
18+
// mesh in the GLTF.
19+
return true;
20+
}
21+
22+
} // namespace importer
23+
} // namespace scene
24+
} // namespace impeller
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 <filesystem>
6+
#include <memory>
7+
8+
#include "flutter/fml/backtrace.h"
9+
#include "flutter/fml/command_line.h"
10+
#include "flutter/fml/file.h"
11+
#include "flutter/fml/macros.h"
12+
#include "flutter/fml/mapping.h"
13+
#include "impeller/base/strings.h"
14+
#include "impeller/compiler/utilities.h"
15+
#include "impeller/scene/importer/importer.h"
16+
#include "impeller/scene/importer/switches.h"
17+
#include "impeller/scene/importer/types.h"
18+
19+
#include "third_party/flatbuffers/include/flatbuffers/flatbuffer_builder.h"
20+
21+
namespace impeller {
22+
namespace scene {
23+
namespace importer {
24+
25+
// Sets the file access mode of the file at path 'p' to 0644.
26+
static bool SetPermissiveAccess(const std::filesystem::path& p) {
27+
auto permissions =
28+
std::filesystem::perms::owner_read | std::filesystem::perms::owner_write |
29+
std::filesystem::perms::group_read | std::filesystem::perms::others_read;
30+
std::error_code error;
31+
std::filesystem::permissions(p, permissions, error);
32+
if (error) {
33+
std::cerr << "Failed to set access on file '" << p
34+
<< "': " << error.message() << std::endl;
35+
return false;
36+
}
37+
return true;
38+
}
39+
40+
bool Main(const fml::CommandLine& command_line) {
41+
fml::InstallCrashHandler();
42+
if (command_line.HasOption("help")) {
43+
Switches::PrintHelp(std::cout);
44+
return true;
45+
}
46+
47+
Switches switches(command_line);
48+
if (!switches.AreValid(std::cerr)) {
49+
std::cerr << "Invalid flags specified." << std::endl;
50+
Switches::PrintHelp(std::cerr);
51+
return false;
52+
}
53+
54+
auto source_file_mapping =
55+
fml::FileMapping::CreateReadOnly(switches.source_file_name);
56+
if (!source_file_mapping) {
57+
std::cerr << "Could not open input file." << std::endl;
58+
return false;
59+
}
60+
61+
fb::MeshT mesh;
62+
bool success = false;
63+
switch (switches.input_type) {
64+
case SourceType::kGLTF:
65+
success = ParseGLTF(*source_file_mapping, mesh);
66+
break;
67+
case SourceType::kUnknown:
68+
std::cerr << "Unknown input type." << std::endl;
69+
return false;
70+
}
71+
if (!success) {
72+
std::cerr << "Failed to parse input file." << std::endl;
73+
return false;
74+
}
75+
76+
flatbuffers::FlatBufferBuilder builder;
77+
builder.Finish(fb::Mesh::Pack(builder, &mesh));
78+
79+
auto output_file_name = std::filesystem::absolute(
80+
std::filesystem::current_path() / switches.output_file_name);
81+
fml::NonOwnedMapping mapping(builder.GetCurrentBufferPointer(),
82+
builder.GetSize());
83+
if (!fml::WriteAtomically(*switches.working_directory,
84+
compiler::Utf8FromPath(output_file_name).c_str(),
85+
mapping)) {
86+
std::cerr << "Could not write file to " << switches.output_file_name
87+
<< std::endl;
88+
return false;
89+
}
90+
91+
// Tools that consume the geometry data expect the access mode to be 0644.
92+
if (!SetPermissiveAccess(output_file_name)) {
93+
return false;
94+
}
95+
96+
return true;
97+
}
98+
99+
} // namespace importer
100+
} // namespace scene
101+
} // namespace impeller
102+
103+
int main(int argc, char const* argv[]) {
104+
return impeller::scene::importer::Main(
105+
fml::CommandLineFromPlatformOrArgcArgv(argc, argv))
106+
? EXIT_SUCCESS
107+
: EXIT_FAILURE;
108+
}

impeller/scene/importer/mesh.fbs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
namespace impeller.fb;
6+
7+
struct Vec2 {
8+
x: float;
9+
y: float;
10+
}
11+
12+
struct Vec3 {
13+
x: float;
14+
y: float;
15+
z: float;
16+
}
17+
18+
struct Color {
19+
r: float;
20+
g: float;
21+
b: float;
22+
}
23+
24+
struct Vertex {
25+
position: Vec3;
26+
normal: Vec3;
27+
tangent: Vec3;
28+
texture_coords: Vec2;
29+
}
30+
31+
table Mesh {
32+
vertices: [Vertex];
33+
indices: [uint16];
34+
}
35+
36+
root_type Mesh;
37+
file_identifier "IPME";

0 commit comments

Comments
 (0)