5 #include "gtest/gtest.h"
8 #include "flutter/testing/testing.h"
11 #include "impeller/runtime_stage/runtime_stage_types_flatbuffers.h"
12 #include "impeller/shader_bundle/shader_bundle_flatbuffers.h"
19 "\"UnlitFragment\": {\"type\": \"fragment\", \"file\": "
20 "\"shaders/flutter_gpu_unlit.frag\"}";
22 "\"UnlitVertex\": {\"type\": \"vertex\", \"file\": "
23 "\"shaders/flutter_gpu_unlit.vert\"}";
25 TEST(ShaderBundleTest, ParseShaderBundleConfigFailsForInvalidJSON) {
26 std::string bundle =
"";
27 std::stringstream error;
29 ASSERT_FALSE(result.has_value());
30 ASSERT_STREQ(error.str().c_str(),
31 "The shader bundle is not a valid JSON object.\n");
34 TEST(ShaderBundleTest, ParseShaderBundleConfigFailsWhenEntryNotObject) {
35 std::string bundle =
"{\"UnlitVertex\": []}";
36 std::stringstream error;
38 ASSERT_FALSE(result.has_value());
41 "Invalid shader entry \"UnlitVertex\": Entry is not a JSON object.\n");
44 TEST(ShaderBundleTest, ParseShaderBundleConfigFailsWhenMissingFile) {
45 std::string bundle =
"{\"UnlitVertex\": {\"type\": \"vertex\"}}";
46 std::stringstream error;
48 ASSERT_FALSE(result.has_value());
49 ASSERT_STREQ(error.str().c_str(),
50 "Invalid shader entry \"UnlitVertex\": Missing required "
54 TEST(ShaderBundleTest, ParseShaderBundleConfigFailsWhenMissingType) {
56 "{\"UnlitVertex\": {\"file\": \"shaders/flutter_gpu_unlit.vert\"}}";
57 std::stringstream error;
59 ASSERT_FALSE(result.has_value());
60 ASSERT_STREQ(error.str().c_str(),
61 "Invalid shader entry \"UnlitVertex\": Missing required "
65 TEST(ShaderBundleTest, ParseShaderBundleConfigFailsForInvalidType) {
67 "{\"UnlitVertex\": {\"type\": \"invalid\", \"file\": "
68 "\"shaders/flutter_gpu_unlit.vert\"}}";
69 std::stringstream error;
71 ASSERT_FALSE(result.has_value());
72 ASSERT_STREQ(error.str().c_str(),
73 "Invalid shader entry \"UnlitVertex\": Shader type "
74 "\"invalid\" is unknown.\n");
77 TEST(ShaderBundleTest, ParseShaderBundleConfigFailsForInvalidLanguage) {
79 "{\"UnlitVertex\": {\"type\": \"vertex\", \"language\": \"invalid\", "
80 "\"file\": \"shaders/flutter_gpu_unlit.vert\"}}";
81 std::stringstream error;
83 ASSERT_FALSE(result.has_value());
84 ASSERT_STREQ(error.str().c_str(),
85 "Invalid shader entry \"UnlitVertex\": Unknown language type "
89 TEST(ShaderBundleTest, ParseShaderBundleConfigReturnsExpectedConfig) {
92 std::stringstream error;
94 ASSERT_TRUE(result.has_value());
95 ASSERT_STREQ(error.str().c_str(),
"");
98 auto maybe_vertex = result->find(
"UnlitVertex");
99 auto maybe_fragment = result->find(
"UnlitFragment");
100 ASSERT_TRUE(maybe_vertex != result->end());
101 ASSERT_TRUE(maybe_fragment != result->end());
102 auto vertex = maybe_vertex->second;
103 auto fragment = maybe_fragment->second;
108 EXPECT_STREQ(vertex.entry_point.c_str(),
"main");
109 EXPECT_STREQ(vertex.source_file_name.c_str(),
110 "shaders/flutter_gpu_unlit.vert");
114 EXPECT_STREQ(fragment.entry_point.c_str(),
"main");
115 EXPECT_STREQ(fragment.source_file_name.c_str(),
116 "shaders/flutter_gpu_unlit.frag");
119 template <
typename T>
120 const T*
FindByName(
const std::vector<std::unique_ptr<T>>& collection,
121 const std::string& name) {
122 const auto maybe = std::find_if(
123 collection.begin(), collection.end(),
124 [&name](
const std::unique_ptr<T>& value) { return value->name == name; });
125 if (maybe == collection.end()) {
131 TEST(ShaderBundleTest, GenerateShaderBundleFlatbufferProducesCorrectResult) {
132 std::string fixtures_path = flutter::testing::GetFixturesPath();
134 "{\"UnlitFragment\": {\"type\": \"fragment\", \"file\": \"" +
136 "/flutter_gpu_unlit.frag\"}, \"UnlitVertex\": {\"type\": "
137 "\"vertex\", \"file\": \"" +
138 fixtures_path +
"/flutter_gpu_unlit.vert\"}}";
144 std::optional<fb::ShaderBundleT> bundle =
146 ASSERT_TRUE(bundle.has_value());
149 const auto& shaders = bundle->shaders;
150 const auto* vertex =
FindByName(shaders,
"UnlitVertex");
151 const auto* fragment =
FindByName(shaders,
"UnlitFragment");
152 ASSERT_NE(vertex,
nullptr);
153 ASSERT_NE(fragment,
nullptr);
159 EXPECT_TRUE(vertex->shader->metal);
160 EXPECT_STREQ(vertex->shader->metal->entrypoint.c_str(),
161 "flutter_gpu_unlit_vertex_main");
162 EXPECT_EQ(vertex->shader->metal->stage, fb::Stage::kVertex);
165 ASSERT_EQ(vertex->shader->metal->inputs.size(), 1u);
166 const auto& v_in_position = vertex->shader->metal->inputs[0];
167 EXPECT_STREQ(v_in_position->name.c_str(),
"position");
168 EXPECT_EQ(v_in_position->location, 0u);
169 EXPECT_EQ(v_in_position->set, 0u);
170 EXPECT_EQ(v_in_position->binding, 0u);
172 EXPECT_EQ(v_in_position->bit_width, 32u);
173 EXPECT_EQ(v_in_position->vec_size, 2u);
174 EXPECT_EQ(v_in_position->columns, 1u);
175 EXPECT_EQ(v_in_position->offset, 0u);
178 ASSERT_EQ(vertex->shader->metal->uniforms.size(), 2u);
179 const auto* v_mvp =
FindByName(vertex->shader->metal->uniforms,
"mvp");
180 ASSERT_NE(v_mvp,
nullptr);
181 EXPECT_EQ(v_mvp->location, 0u);
183 EXPECT_EQ(v_mvp->bit_width, 32u);
184 EXPECT_EQ(v_mvp->rows, 4u);
185 EXPECT_EQ(v_mvp->columns, 4u);
186 EXPECT_EQ(v_mvp->array_elements, 0u);
187 const auto* v_color =
FindByName(vertex->shader->metal->uniforms,
"color");
188 ASSERT_NE(v_color,
nullptr);
189 EXPECT_EQ(v_color->location, 1u);
191 EXPECT_EQ(v_color->bit_width, 32u);
192 EXPECT_EQ(v_color->rows, 4u);
193 EXPECT_EQ(v_color->columns, 1u);
194 EXPECT_EQ(v_color->array_elements, 0u);
200 EXPECT_TRUE(fragment->shader->metal);
201 EXPECT_STREQ(fragment->shader->metal->entrypoint.c_str(),
202 "flutter_gpu_unlit_fragment_main");
203 EXPECT_EQ(fragment->shader->metal->stage, fb::Stage::kFragment);
206 ASSERT_EQ(fragment->shader->metal->inputs.size(), 0u);
209 ASSERT_EQ(fragment->shader->metal->inputs.size(), 0u);