Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Impeller Scene] Add offline mesh importer #37981

Merged
merged 5 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,13 @@ FILE: ../../../flutter/impeller/scene/camera.cc
FILE: ../../../flutter/impeller/scene/camera.h
FILE: ../../../flutter/impeller/scene/geometry.cc
FILE: ../../../flutter/impeller/scene/geometry.h
FILE: ../../../flutter/impeller/scene/importer/importer.h
FILE: ../../../flutter/impeller/scene/importer/importer_gltf.cc
FILE: ../../../flutter/impeller/scene/importer/importer_main.cc
FILE: ../../../flutter/impeller/scene/importer/mesh.fbs
FILE: ../../../flutter/impeller/scene/importer/switches.cc
FILE: ../../../flutter/impeller/scene/importer/switches.h
FILE: ../../../flutter/impeller/scene/importer/types.h
FILE: ../../../flutter/impeller/scene/material.cc
FILE: ../../../flutter/impeller/scene/material.h
FILE: ../../../flutter/impeller/scene/scene.cc
Expand Down
21 changes: 19 additions & 2 deletions impeller/compiler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@
import("//flutter/impeller/tools/impeller.gni")
import("//flutter/shell/version/version.gni")

impeller_component("utilities") {
# Current versions of libcxx have deprecated some of the UTF-16 string
# conversion APIs.
defines = [ "_LIBCPP_DISABLE_DEPRECATION_WARNINGS" ]

sources = [
"utilities.cc",
"utilities.h",
]

public_deps = [
"../base",
"../geometry",
"../runtime_stage",
"//flutter/fml",
]
}

impeller_component("compiler_lib") {
include_dirs = [ "//third_party/vulkan-deps/spirv-cross/src/" ]

Expand Down Expand Up @@ -34,11 +52,10 @@ impeller_component("compiler_lib") {
"switches.h",
"types.cc",
"types.h",
"utilities.cc",
"utilities.h",
]

public_deps = [
":utilities",
"../base",
"../geometry",
"../runtime_stage",
Expand Down
6 changes: 6 additions & 0 deletions impeller/fixtures/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ impeller_shaders("shader_fixtures") {
}
}

scene_importer("geometry_fixtures") {
geometry = [ "flutter_logo.glb" ]
type = "gltf"
}

impellerc("runtime_stages") {
shaders = [
"ink_sparkle.frag",
Expand Down Expand Up @@ -87,6 +92,7 @@ group("fixtures") {

public_deps = [
":file_fixtures",
":geometry_fixtures",
":shader_fixtures",
]
}
Binary file added impeller/fixtures/flutter_logo.glb
Binary file not shown.
93 changes: 93 additions & 0 deletions impeller/scene/importer/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Copyright 2013 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//flutter/impeller/tools/impeller.gni")
import("//flutter/shell/version/version.gni")
import("//third_party/flatbuffers/flatbuffers.gni")

config("runtime_stage_config") {
configs = [ "//flutter/impeller:impeller_public_config" ]
include_dirs = [ "$root_gen_dir/flutter" ]
}

flatbuffers("importer_flatbuffers") {
flatbuffers = [ "mesh.fbs" ]
public_configs = [ ":runtime_stage_config" ]
public_deps = [ "//third_party/flatbuffers" ]
}

impeller_component("importer_lib") {
# Current versions of libcxx have deprecated some of the UTF-16 string
# conversion APIs.
defines = [ "_LIBCPP_DISABLE_DEPRECATION_WARNINGS" ]

sources = [
"importer.h",
"importer_gltf.cc",
"switches.cc",
"switches.h",
"types.h",
]

public_deps = [
":importer_flatbuffers",
"../../base",
"../../compiler:utilities",
"../../geometry",
"//flutter/fml",

# All third_party deps must be reflected below in the scene_importer_license
# target.
# TODO(bdero): Fix tinygltf compilation warnings.
#"//third_party/tinygltf",
]
}

generated_file("scene_importer_license") {
source_path = rebase_path(".", "//flutter")
git_url = "https://github.com/flutter/engine/tree/$engine_version"
outputs = [ "$target_gen_dir/LICENSE.scene_importer.md" ]
contents = [
"# scene_importer",
"",
"This tool is used by the Flutter SDK to import 3D geometry.",
"",
"Source code for this tool: [flutter/engine/$source_path]($git_url/$source_path).",
"",
"## Licenses",
"",
"### scene_importer",
"",
read_file("//flutter/sky/packages/sky_engine/LICENSE", "string"),
"",

# These licenses are ignored by the main license checker, since they are not
# shipped to end-application binaries and only shipped as part of developer
# tooling in scene_importer. Add them here.
"## Additional open source licenses",
"",
"### tinygltf",
"",
read_file("//third_party/tinygltf/LICENSE", "string"),
]
}

group("importer") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: On naming, perhaps just scenec like impellerc. importer is rather generic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, scenec seems good. My mind kept getting stuck on geometryc which I didn't like very much.

deps = [
":scene_importer",
":scene_importer_license",
]
}

impeller_component("scene_importer") {
target_type = "executable"

sources = [ "importer_main.cc" ]

deps = [ ":importer_lib" ]

metadata = {
entitlement_file_path = [ "scene_importer" ]
}
}
19 changes: 19 additions & 0 deletions impeller/scene/importer/importer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <array>
#include <memory>

#include "flutter/fml/mapping.h"
#include "impeller/scene/importer/mesh_flatbuffers.h"

namespace impeller {
namespace scene {
namespace importer {

bool ParseGLTF(const fml::Mapping& source_mapping, fb::MeshT& out_mesh);

}
} // namespace scene
} // namespace impeller
24 changes: 24 additions & 0 deletions impeller/scene/importer/importer_gltf.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <array>
#include <memory>

#include "impeller/scene/importer/importer.h"

#include "flutter/fml/mapping.h"

namespace impeller {
namespace scene {
namespace importer {

bool ParseGLTF(const fml::Mapping& source_mapping, fb::MeshT& out_mesh) {
// TODO(bdero): Parse source_mapping and populare out_mesh with just the first
// mesh in the GLTF.
return true;
}

} // namespace importer
} // namespace scene
} // namespace impeller
108 changes: 108 additions & 0 deletions impeller/scene/importer/importer_main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <filesystem>
#include <memory>

#include "flutter/fml/backtrace.h"
#include "flutter/fml/command_line.h"
#include "flutter/fml/file.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/mapping.h"
#include "impeller/base/strings.h"
#include "impeller/compiler/utilities.h"
#include "impeller/scene/importer/importer.h"
#include "impeller/scene/importer/switches.h"
#include "impeller/scene/importer/types.h"

#include "third_party/flatbuffers/include/flatbuffers/flatbuffer_builder.h"

namespace impeller {
namespace scene {
namespace importer {

// Sets the file access mode of the file at path 'p' to 0644.
static bool SetPermissiveAccess(const std::filesystem::path& p) {
auto permissions =
std::filesystem::perms::owner_read | std::filesystem::perms::owner_write |
std::filesystem::perms::group_read | std::filesystem::perms::others_read;
std::error_code error;
std::filesystem::permissions(p, permissions, error);
if (error) {
std::cerr << "Failed to set access on file '" << p
<< "': " << error.message() << std::endl;
return false;
}
return true;
}

bool Main(const fml::CommandLine& command_line) {
fml::InstallCrashHandler();
if (command_line.HasOption("help")) {
Switches::PrintHelp(std::cout);
return true;
}

Switches switches(command_line);
if (!switches.AreValid(std::cerr)) {
std::cerr << "Invalid flags specified." << std::endl;
Switches::PrintHelp(std::cerr);
return false;
}

auto source_file_mapping =
fml::FileMapping::CreateReadOnly(switches.source_file_name);
if (!source_file_mapping) {
std::cerr << "Could not open input file." << std::endl;
return false;
}

fb::MeshT mesh;
bool success = false;
switch (switches.input_type) {
case SourceType::kGLTF:
success = ParseGLTF(*source_file_mapping, mesh);
break;
case SourceType::kUnknown:
std::cerr << "Unknown input type." << std::endl;
return false;
}
if (!success) {
std::cerr << "Failed to parse input file." << std::endl;
return false;
}

flatbuffers::FlatBufferBuilder builder;
builder.Finish(fb::Mesh::Pack(builder, &mesh));

auto output_file_name = std::filesystem::absolute(
std::filesystem::current_path() / switches.output_file_name);
fml::NonOwnedMapping mapping(builder.GetCurrentBufferPointer(),
builder.GetSize());
if (!fml::WriteAtomically(*switches.working_directory,
compiler::Utf8FromPath(output_file_name).c_str(),
mapping)) {
std::cerr << "Could not write file to " << switches.output_file_name
<< std::endl;
return false;
}

// Tools that consume the geometry data expect the access mode to be 0644.
if (!SetPermissiveAccess(output_file_name)) {
return false;
}

return true;
}

} // namespace importer
} // namespace scene
} // namespace impeller

int main(int argc, char const* argv[]) {
return impeller::scene::importer::Main(
fml::CommandLineFromPlatformOrArgcArgv(argc, argv))
? EXIT_SUCCESS
: EXIT_FAILURE;
}
37 changes: 37 additions & 0 deletions impeller/scene/importer/mesh.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

namespace impeller.fb;

struct Vec2 {
x: float;
y: float;
}

struct Vec3 {
x: float;
y: float;
z: float;
}

struct Color {
r: float;
g: float;
b: float;
}

struct Vertex {
position: Vec3;
normal: Vec3;
tangent: Vec3;
texture_coords: Vec2;
}

table Mesh {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll probably have to sketch this out some more but this is a start!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, one triangle mesh to fake it till we make it. Materials, fuller scene description, bones, weights, and animation will all be necessary soon in my estimation. :)

vertices: [Vertex];
indices: [uint16];
}

root_type Mesh;
file_identifier "IPME";
Loading