Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Full bundle->flatbuffer test
  • Loading branch information
bdero committed Dec 12, 2023
commit ce28f95adb403a97968fd909f9dfd9d97727e78d
39 changes: 28 additions & 11 deletions impeller/compiler/shader_bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ namespace impeller {
namespace compiler {

std::optional<ShaderBundleConfig> ParseShaderBundleConfig(
const std::string& json_config,
const std::string& bundle_config_json,
std::ostream& error_stream) {
auto json = nlohmann::json::parse(json_config, nullptr, false);
auto json = nlohmann::json::parse(bundle_config_json, nullptr, false);
if (json.is_discarded() || !json.is_object()) {
error_stream << "The shader bundle is not a valid JSON object."
<< std::endl;
Expand Down Expand Up @@ -141,17 +141,17 @@ static std::unique_ptr<fb::ShaderT> GenerateShaderFB(
return result;
}

/// Parses the given JSON shader bundle configuration and invokes the compiler
/// multiple times to produce a shader bundle file.
bool GenerateShaderBundle(Switches& switches, SourceOptions& options) {
std::optional<fb::ShaderBundleT> GenerateShaderBundleFlatbuffer(
const std::string& bundle_config_json,
SourceOptions& options) {
// --------------------------------------------------------------------------
/// 1. Parse the bundle configuration.
///

std::optional<ShaderBundleConfig> bundle_config =
ParseShaderBundleConfig(switches.shader_bundle, std::cerr);
ParseShaderBundleConfig(bundle_config_json, std::cerr);
if (!bundle_config) {
return false;
return std::nullopt;
}

// --------------------------------------------------------------------------
Expand All @@ -164,18 +164,35 @@ bool GenerateShaderBundle(Switches& switches, SourceOptions& options) {
std::unique_ptr<fb::ShaderT> shader =
GenerateShaderFB(options, shader_name, shader_config);
if (!shader) {
return false;
return std::nullopt;
}
shader_bundle.shaders.push_back(std::move(shader));
}

return shader_bundle;
}

bool GenerateShaderBundle(Switches& switches, SourceOptions& options) {
// --------------------------------------------------------------------------
/// 1. Parse the shader bundle and generate the flatbuffer result.
///

auto shader_bundle =
GenerateShaderBundleFlatbuffer(switches.shader_bundle, options);
if (!shader_bundle.has_value()) {
// Specific error messages are already handled by
// GenerateShaderBundleFlatbuffer.
return false;
}

// --------------------------------------------------------------------------
/// 3. Serialize the shader bundle and write to disk.
/// 2. Serialize the shader bundle and write to disk.
///

auto builder = std::make_shared<flatbuffers::FlatBufferBuilder>();
builder->Finish(fb::ShaderBundle::Pack(*builder.get(), &shader_bundle),
fb::ShaderBundleIdentifier());
builder->Finish(
fb::ShaderBundle::Pack(*builder.get(), &shader_bundle.value()),
fb::ShaderBundleIdentifier());
auto mapping = std::make_shared<fml::NonOwnedMapping>(
builder->GetBufferPointer(), builder->GetSize(),
[builder](auto, auto) {});
Expand Down
19 changes: 18 additions & 1 deletion impeller/compiler/shader_bundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,31 @@

#include "impeller/compiler/source_options.h"
#include "impeller/compiler/switches.h"
#include "impeller/shader_bundle/shader_bundle_flatbuffers.h"

namespace impeller {
namespace compiler {

/// @brief Parse a shader bundle configuration from a given JSON string.
///
/// @note Exposed only for testing purposes. Use `GenerateShaderBundle`
/// directly.
std::optional<ShaderBundleConfig> ParseShaderBundleConfig(
const std::string& json_config,
const std::string& bundle_config_json,
std::ostream& error_stream);

/// @brief Parses the JSON shader bundle configuration and invokes the
/// compiler multiple times to produce a shader bundle flatbuffer.
///
/// @note Exposed only for testing purposes. Use `GenerateShaderBundle`
/// directly.
std::optional<fb::ShaderBundleT> GenerateShaderBundleFlatbuffer(
const std::string& bundle_config_json,
SourceOptions& options);

/// @brief Parses the JSON shader bundle configuration and invokes the
/// compiler multiple times to produce a shader bundle flatbuffer, which
/// is then output to the `sl` file.
bool GenerateShaderBundle(Switches& switches, SourceOptions& options);

} // namespace compiler
Expand Down
97 changes: 97 additions & 0 deletions impeller/compiler/shader_bundle_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gtest/gtest.h"
#include "impeller/compiler/shader_bundle.h"

#include "flutter/testing/testing.h"
#include "impeller/compiler/source_options.h"
#include "impeller/compiler/types.h"
#include "impeller/runtime_stage/runtime_stage_types_flatbuffers.h"
#include "impeller/shader_bundle/shader_bundle_flatbuffers.h"

namespace impeller {
namespace compiler {
Expand Down Expand Up @@ -112,6 +116,99 @@ TEST(ShaderBundleTest, ParseShaderBundleConfigReturnsExpectedConfig) {
"shaders/flutter_gpu_unlit.frag");
}

template <typename T>
const T* FindByName(const std::vector<std::unique_ptr<T>>& collection,
const std::string& name) {
const auto maybe = std::find_if(
collection.begin(), collection.end(),
[&name](const std::unique_ptr<T>& value) { return value->name == name; });
if (maybe == collection.end()) {
return nullptr;
}
return maybe->get();
}

TEST(ShaderBundleTest, GenerateShaderBundleFlatbufferProducesCorrectResult) {
std::string fixtures_path = flutter::testing::GetFixturesPath();
std::string config =
"{\"UnlitFragment\": {\"type\": \"fragment\", \"file\": \"" +
fixtures_path +
"/flutter_gpu_unlit.frag\"}, \"UnlitVertex\": {\"type\": "
"\"vertex\", \"file\": \"" +
fixtures_path + "/flutter_gpu_unlit.vert\"}}";

SourceOptions options;
options.target_platform = TargetPlatform::kRuntimeStageMetal;
options.source_language = SourceLanguage::kGLSL;

std::optional<fb::ShaderBundleT> bundle =
GenerateShaderBundleFlatbuffer(config, options);
ASSERT_TRUE(bundle.has_value());

// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
const auto& shaders = bundle->shaders;
const auto* vertex = FindByName(shaders, "UnlitVertex");
const auto* fragment = FindByName(shaders, "UnlitFragment");
ASSERT_NE(vertex, nullptr);
ASSERT_NE(fragment, nullptr);

// --------------------------------------------------------------------------
/// Verify vertex shader.
///

EXPECT_STREQ(vertex->shader->entrypoint.c_str(),
"flutter_gpu_unlit_vertex_main");
EXPECT_EQ(vertex->shader->stage, fb::Stage::kVertex);
EXPECT_EQ(vertex->shader->target_platform, fb::TargetPlatform::kMetal);

// Inputs.
ASSERT_EQ(vertex->shader->inputs.size(), 1u);
const auto& v_in_position = vertex->shader->inputs[0];
EXPECT_STREQ(v_in_position->name.c_str(), "position");
EXPECT_EQ(v_in_position->location, 0u);
EXPECT_EQ(v_in_position->set, 0u);
EXPECT_EQ(v_in_position->binding, 0u);
EXPECT_EQ(v_in_position->type, fb::InputDataType::kFloat);
EXPECT_EQ(v_in_position->bit_width, 32u);
EXPECT_EQ(v_in_position->vec_size, 2u);
EXPECT_EQ(v_in_position->columns, 1u);
EXPECT_EQ(v_in_position->offset, 0u);

// Uniforms.
ASSERT_EQ(vertex->shader->uniforms.size(), 2u);
const auto* v_mvp = FindByName(vertex->shader->uniforms, "mvp");
ASSERT_NE(v_mvp, nullptr);
EXPECT_EQ(v_mvp->location, 0u);
EXPECT_EQ(v_mvp->type, fb::UniformDataType::kFloat);
EXPECT_EQ(v_mvp->bit_width, 32u);
EXPECT_EQ(v_mvp->rows, 4u);
EXPECT_EQ(v_mvp->columns, 4u);
EXPECT_EQ(v_mvp->array_elements, 0u);
const auto* v_color = FindByName(vertex->shader->uniforms, "color");
ASSERT_NE(v_color, nullptr);
EXPECT_EQ(v_color->location, 1u);
EXPECT_EQ(v_color->type, fb::UniformDataType::kFloat);
EXPECT_EQ(v_color->bit_width, 32u);
EXPECT_EQ(v_color->rows, 4u);
EXPECT_EQ(v_color->columns, 1u);
EXPECT_EQ(v_color->array_elements, 0u);

// --------------------------------------------------------------------------
/// Verify fragment shader.
///

EXPECT_STREQ(fragment->shader->entrypoint.c_str(),
"flutter_gpu_unlit_fragment_main");
EXPECT_EQ(fragment->shader->stage, fb::Stage::kFragment);
EXPECT_EQ(fragment->shader->target_platform, fb::TargetPlatform::kMetal);

// Inputs (not recorded for fragment shaders).
ASSERT_EQ(fragment->shader->inputs.size(), 0u);

// Uniforms.
ASSERT_EQ(fragment->shader->inputs.size(), 0u);
}

} // namespace testing
} // namespace compiler
} // namespace impeller
4 changes: 4 additions & 0 deletions impeller/fixtures/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ test_fixtures("file_fixtures") {
"blue_noise.png",
"boston.jpg",
"embarcadero.jpg",
"flutter_gpu_unlit.frag",
Copy link
Member

Choose a reason for hiding this comment

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

nit: alphabetize

Copy link
Member Author

Choose a reason for hiding this comment

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

Done. Someday I'll learn my abcs :^)

"flutter_gpu_unlit.vert",
"flutter_gpu_texture.frag",
"flutter_gpu_texture.vert",
"flutter_logo_baked.glb",
"kalimba.jpg",
"multiple_stages.hlsl",
Expand Down