5 #include "gtest/gtest.h"
8 #include "flutter/testing/testing.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::shaderbundle::ShaderBundleT> bundle =
146 ASSERT_TRUE(bundle.has_value());
149 const auto& shaders = bundle->shaders;
151 const auto* vertex =
FindByName(shaders,
"UnlitVertex");
152 const auto* fragment =
FindByName(shaders,
"UnlitFragment");
153 ASSERT_NE(vertex,
nullptr);
154 ASSERT_NE(fragment,
nullptr);
160 EXPECT_STREQ(vertex->metal_desktop->entrypoint.c_str(),
161 "flutter_gpu_unlit_vertex_main");
162 EXPECT_EQ(vertex->metal_desktop->stage,
163 fb::shaderbundle::ShaderStage::kVertex);
166 ASSERT_EQ(vertex->metal_desktop->inputs.size(), 1u);
167 const auto& v_in_position = vertex->metal_desktop->inputs[0];
168 EXPECT_STREQ(v_in_position->name.c_str(),
"position");
169 EXPECT_EQ(v_in_position->location, 0u);
170 EXPECT_EQ(v_in_position->set, 0u);
171 EXPECT_EQ(v_in_position->binding, 0u);
173 EXPECT_EQ(v_in_position->bit_width, 32u);
174 EXPECT_EQ(v_in_position->vec_size, 2u);
175 EXPECT_EQ(v_in_position->columns, 1u);
176 EXPECT_EQ(v_in_position->offset, 0u);
179 ASSERT_EQ(vertex->metal_desktop->uniform_structs.size(), 1u);
180 const auto* vert_info =
181 FindByName(vertex->metal_desktop->uniform_structs,
"VertInfo");
182 ASSERT_NE(vert_info,
nullptr);
183 EXPECT_EQ(vert_info->ext_res_0, 0u);
184 EXPECT_EQ(vert_info->set, 0u);
185 EXPECT_EQ(vert_info->binding, 0u);
186 ASSERT_EQ(vert_info->fields.size(), 2u);
187 const auto& mvp = vert_info->fields[0];
188 EXPECT_STREQ(mvp->name.c_str(),
"mvp");
190 EXPECT_EQ(mvp->offset_in_bytes, 0u);
191 EXPECT_EQ(mvp->element_size_in_bytes, 64u);
192 EXPECT_EQ(mvp->total_size_in_bytes, 64u);
193 EXPECT_EQ(mvp->array_elements, 0u);
194 EXPECT_EQ(mvp->vec_size, 4u);
195 EXPECT_EQ(mvp->columns, 4u);
196 const auto& color = vert_info->fields[1];
197 EXPECT_STREQ(color->name.c_str(),
"color");
199 EXPECT_EQ(color->offset_in_bytes, 64u);
200 EXPECT_EQ(color->element_size_in_bytes, 16u);
201 EXPECT_EQ(color->total_size_in_bytes, 16u);
202 EXPECT_EQ(color->array_elements, 0u);
203 EXPECT_EQ(color->vec_size, 4u);
204 EXPECT_EQ(color->columns, 1u);
210 EXPECT_STREQ(fragment->metal_desktop->entrypoint.c_str(),
211 "flutter_gpu_unlit_fragment_main");
212 EXPECT_EQ(fragment->metal_desktop->stage,
213 fb::shaderbundle::ShaderStage::kFragment);
216 ASSERT_EQ(fragment->metal_desktop->inputs.size(), 0u);
219 ASSERT_EQ(fragment->metal_desktop->inputs.size(), 0u);
223 GenerateShaderBundleFlatbufferReportsSourceFilesAsDependencies) {
224 std::string fixtures_path = flutter::testing::GetFixturesPath();
225 const std::string fragment_path = fixtures_path +
"/flutter_gpu_unlit.frag";
226 const std::string vertex_path = fixtures_path +
"/flutter_gpu_unlit.vert";
228 "{\"UnlitFragment\": {\"type\": \"fragment\", \"file\":\"" +
230 "\"}, \"UnlitVertex\": {\"type\": \"vertex\", \"file\": \"" +
231 vertex_path +
"\"}}";
237 std::set<std::string> dependencies;
238 std::optional<fb::shaderbundle::ShaderBundleT> bundle =
240 ASSERT_TRUE(bundle.has_value());
247 EXPECT_NE(dependencies.find(fragment_path), dependencies.end());
248 EXPECT_NE(dependencies.find(vertex_path), dependencies.end());
252 GenerateShaderBundleFlatbufferIgnoresNullDependencyCollector) {
255 std::string fixtures_path = flutter::testing::GetFixturesPath();
257 "{\"UnlitFragment\": {\"type\": \"fragment\", \"file\": \"" +
259 "/flutter_gpu_unlit.frag\"}, \"UnlitVertex\": {\"type\": \"vertex\", "
261 fixtures_path +
"/flutter_gpu_unlit.vert\"}}";
267 std::optional<fb::shaderbundle::ShaderBundleT> bundle =
270 ASSERT_TRUE(bundle.has_value());
273 TEST(ShaderBundleTest, DeriveShaderFloatTypeFromDimensions) {
309 TEST(ShaderBundleTest, TargetPlatformDefinesMatchEachBackend) {
311 (std::vector<std::string_view>{
"IMPELLER_TARGET_METAL",
312 "IMPELLER_TARGET_METAL_IOS"}));
314 (std::vector<std::string_view>{
"IMPELLER_TARGET_METAL",
315 "IMPELLER_TARGET_METAL_DESKTOP"}));
317 (std::vector<std::string_view>{
"IMPELLER_TARGET_OPENGLES"}));
320 (std::vector<std::string_view>{
"IMPELLER_TARGET_OPENGL"}));
322 (std::vector<std::string_view>{
"IMPELLER_TARGET_VULKAN"}));
333 TEST(ShaderBundleTest, InjectsTargetDefinesDuringCompilation) {
338 std::string fixtures_path = flutter::testing::GetFixturesPath();
339 std::string config =
"{\"Probe\": {\"type\": \"fragment\", \"file\": \"" +
340 fixtures_path +
"/check_gles_definition.frag\"}}";
346 std::optional<fb::shaderbundle::ShaderBundleT> bundle =
348 EXPECT_FALSE(bundle.has_value());
const std::string kUnlitFragmentBundleConfig
const std::string kUnlitVertexBundleConfig
const T * FindByName(const std::vector< std::unique_ptr< T >> &collection, const std::string &name)
TEST(CompilerTest, Defines)
std::vector< std::string_view > GetShaderBundleTargetPlatformDefines(TargetPlatform platform)
The platform-discriminating preprocessor defines injected when compiling a bundled shader for platfor...
std::optional< fb::shaderbundle::ShaderBundleT > GenerateShaderBundleFlatbuffer(const std::string &bundle_config_json, const SourceOptions &options, std::set< std::string > *out_dependencies)
Parses the JSON shader bundle configuration and invokes the compiler multiple times to produce a shad...
std::optional< ShaderBundleConfig > ParseShaderBundleConfig(const std::string &bundle_config_json, std::ostream &error_stream)
Parse a shader bundle configuration from a given JSON string.
constexpr std::optional< ShaderFloatType > DeriveShaderFloatType(ShaderType type, size_t vec_size, size_t columns)
Derive the ShaderFloatType from the base ShaderType and the (vec_size, columns) dimensions reported b...
SourceLanguage source_language
TargetPlatform target_platform