7#include "gtest/gtest.h"
19 std::shared_ptr<const fml::Mapping> fixture =
52 GTEST_SKIP() <<
"Not supported with SkSL";
54 ASSERT_TRUE(CanCompileAndReflect(
"sample.vert"));
62 GTEST_SKIP() <<
"Not supported with SkSL";
64 ASSERT_TRUE(CanCompileAndReflect(
70 GTEST_SKIP() <<
"Not supported with SkSL";
72 ASSERT_TRUE(CanCompileAndReflect(
"multiple_stages.hlsl",
75 ASSERT_TRUE(CanCompileAndReflect(
"multiple_stages.hlsl",
83 <<
"Only enabled on Metal backends till ES 3.2 support is added.";
85 ASSERT_TRUE(CanCompileAndReflect(
"sample.comp"));
91 GTEST_SKIP() <<
"Not supported with SkSL";
100 GTEST_SKIP() <<
"Not supported with SkSL";
103 ASSERT_FALSE(CanCompileAndReflect(
"struct_def_bug.vert"));
114 auto get_binding = [&](
const char* fixture) -> uint32_t {
115 auto json_fd = GetReflectionJson(fixture);
116 nlohmann::json shader_json = nlohmann::json::parse(json_fd->GetMapping());
117 return shader_json[
"buffers"][0][
"binding"].get<uint32_t>();
120 auto vert_uniform_binding = get_binding(
"sample.vert");
121 auto frag_uniform_binding = get_binding(
"sample.frag");
123 ASSERT_GT(frag_uniform_binding, vert_uniform_binding);
134 static UniformInfo fromJson(
const nlohmann::json& json) {
136 .uniform_name = json[
"name"].get<std::string>(),
137 .
location = json[
"location"].get<uint32_t>(),
138 .type_name = json[
"type"][
"type_name"].get<std::string>(),
139 .
columns = json[
"type"][
"columns"].get<uint32_t>(),
140 .vec_size = json[
"type"][
"vec_size"].get<uint32_t>(),
144 static UniformInfo
Sampler(
const std::string&
name, uint32_t location) {
146 .uniform_name =
name,
148 .type_name =
"ShaderType::kSampledImage",
153 static UniformInfo Float(
const std::string&
name, uint32_t location) {
154 return FloatInfo(
name, location, 1u, 1u);
156 static UniformInfo Vec2(
const std::string&
name, uint32_t location) {
157 return FloatInfo(
name, location, 1u, 2u);
159 static UniformInfo Vec3(
const std::string&
name, uint32_t location) {
160 return FloatInfo(
name, location, 1u, 3u);
162 static UniformInfo Vec4(
const std::string&
name, uint32_t location) {
163 return FloatInfo(
name, location, 1u, 4u);
165 static UniformInfo Mat4(
const std::string&
name, uint32_t location) {
166 return FloatInfo(
name, location, 4u, 4u);
169 constexpr bool operator==(
const UniformInfo& other)
const {
170 return (uniform_name == other.uniform_name &&
171 location == other.location &&
172 type_name == other.type_name &&
173 columns == other.columns &&
174 vec_size == other.vec_size);
178 static UniformInfo FloatInfo(
const std::string&
name,
183 .uniform_name =
name,
185 .type_name =
"ShaderType::kFloat",
192inline std::ostream&
operator<<(std::ostream& out,
const UniformInfo& info) {
193 out <<
"UniformInfo {" << std::endl
194 <<
" uniform_name: " << info.uniform_name << std::endl
195 <<
" location: " << info.location << std::endl
196 <<
" type_name: " << info.type_name << std::endl
197 <<
" columns: " << info.columns << std::endl
198 <<
" vec_size: " << info.vec_size << std::endl
205 ASSERT_TRUE(CanCompileAndReflect(
"sample_with_uniforms.frag",
209 auto json_fd = GetReflectionJson(
"sample_with_uniforms.frag");
210 ASSERT_TRUE(json_fd);
211 nlohmann::json shader_json = nlohmann::json::parse(json_fd->GetMapping());
212 auto sampler_list = shader_json[
"sampled_images"];
213 auto float_list = shader_json[
"uniforms"];
214 ASSERT_EQ(sampler_list.size(), 2u);
215 ASSERT_EQ(float_list.size(), 6u);
219 std::array expected_infos = {
220 UniformInfo::Sampler(
"uFirstSampler", 1u),
221 UniformInfo::Sampler(
"uSampler", 7u),
224 ASSERT_EQ(sampler_list.size(), expected_infos.size());
225 for (
size_t i = 0;
i < expected_infos.size();
i++) {
226 EXPECT_EQ(UniformInfo::fromJson(sampler_list[
i]), expected_infos[
i])
233 std::array expected_infos = {
234 UniformInfo::Float(
"uFirstFloat", 0u),
235 UniformInfo::Float(
"uFloat", 2u),
236 UniformInfo::Vec2(
"uVec2", 3u),
237 UniformInfo::Vec3(
"uVec3", 4u),
238 UniformInfo::Vec4(
"uVec4", 5u),
239 UniformInfo::Mat4(
"uMat4", 6u),
242 ASSERT_EQ(float_list.size(), expected_infos.size());
243 for (
size_t i = 0;
i < expected_infos.size();
i++) {
244 EXPECT_EQ(UniformInfo::fromJson(float_list[
i]), expected_infos[
i])
251 ASSERT_TRUE(CanCompileAndReflect(
"sample_with_positioned_uniforms.frag",
255 auto json_fd = GetReflectionJson(
"sample_with_positioned_uniforms.frag");
256 ASSERT_TRUE(json_fd);
257 nlohmann::json shader_json = nlohmann::json::parse(json_fd->GetMapping());
258 auto sampler_list = shader_json[
"sampled_images"];
259 auto float_list = shader_json[
"uniforms"];
260 ASSERT_EQ(sampler_list.size(), 3u);
261 ASSERT_EQ(float_list.size(), 7u);
265 std::array expected_infos = {
266 UniformInfo::Sampler(
"uSamplerNotPositioned1", 1u),
267 UniformInfo::Sampler(
"uSampler", 0u),
268 UniformInfo::Sampler(
"uSamplerNotPositioned2", 3u),
271 ASSERT_EQ(sampler_list.size(), expected_infos.size());
272 for (
size_t i = 0;
i < expected_infos.size();
i++) {
273 EXPECT_EQ(UniformInfo::fromJson(sampler_list[
i]), expected_infos[
i])
280 std::array expected_infos = {
281 UniformInfo::Float(
"uFloatNotPositioned1", 0u),
282 UniformInfo::Float(
"uFloat", 6u),
283 UniformInfo::Vec2(
"uVec2", 5u),
284 UniformInfo::Vec3(
"uVec3", 3u),
285 UniformInfo::Vec4(
"uVec4", 2u),
286 UniformInfo::Mat4(
"uMat4", 1u),
287 UniformInfo::Float(
"uFloatNotPositioned2", 2u),
290 ASSERT_EQ(float_list.size(), expected_infos.size());
291 for (
size_t i = 0;
i < expected_infos.size();
i++) {
292 EXPECT_EQ(UniformInfo::fromJson(float_list[
i]), expected_infos[
i])
300 GTEST_SKIP() <<
"Not supported with SkSL";
302 ASSERT_TRUE(CanCompileAndReflect(
"sample_with_binding.vert",
306 struct binding_and_set {
311 auto get_binding = [&](
const char* fixture) -> binding_and_set {
312 auto json_fd = GetReflectionJson(fixture);
313 nlohmann::json shader_json = nlohmann::json::parse(json_fd->GetMapping());
314 uint32_t binding = shader_json[
"buffers"][0][
"binding"].get<uint32_t>();
315 uint32_t set = shader_json[
"buffers"][0][
"set"].get<uint32_t>();
316 return {binding, set};
319 auto vert_uniform_binding = get_binding(
"sample_with_binding.vert");
320 auto frag_uniform_binding = get_binding(
"sample.frag");
322 ASSERT_EQ(frag_uniform_binding.set, 0u);
323 ASSERT_EQ(vert_uniform_binding.set, 3u);
324 ASSERT_EQ(vert_uniform_binding.binding, 17u);
331 auto shader = GetShaderFile(
"texture_lookup.frag", GetParam());
332 std::string_view shader_mapping(
333 reinterpret_cast<const char*
>(shader->GetMapping()), shader->GetSize());
335 constexpr std::string_view expected =
336 "textureA.eval(textureA_size * ( vec2(1.0) + flutter_FragCoord.xy));";
338 EXPECT_NE(shader_mapping.find(expected), std::string::npos);
342 ASSERT_TRUE(CanCompileAndReflect(
"struct_internal.frag",
346#define INSTANTIATE_TARGET_PLATFORM_TEST_SUITE_P(suite_name) \
347 INSTANTIATE_TEST_SUITE_P( \
348 suite_name, CompilerTest, \
349 ::testing::Values(TargetPlatform::kOpenGLES, \
350 TargetPlatform::kOpenGLDesktop, \
351 TargetPlatform::kMetalDesktop, \
352 TargetPlatform::kMetalIOS, TargetPlatform::kSkSL), \
353 [](const ::testing::TestParamInfo<CompilerTest::ParamType>& info) { \
354 return TargetPlatformToString(info.param); \
359#define INSTANTIATE_RUNTIME_TARGET_PLATFORM_TEST_SUITE_P(suite_name) \
360 INSTANTIATE_TEST_SUITE_P( \
361 suite_name, CompilerTestRuntime, \
362 ::testing::Values(TargetPlatform::kRuntimeStageMetal), \
363 [](const ::testing::TestParamInfo<CompilerTest::ParamType>& info) { \
364 return TargetPlatformToString(info.param); \
369#define INSTANTIATE_SKSL_TARGET_PLATFORM_TEST_SUITE_P(suite_name) \
370 INSTANTIATE_TEST_SUITE_P( \
371 suite_name, CompilerTestSkSL, ::testing::Values(TargetPlatform::kSkSL), \
372 [](const ::testing::TestParamInfo<CompilerTest::ParamType>& info) { \
373 return TargetPlatformToString(info.param); \
std::shared_ptr< fml::Mapping > GetSPIRVAssembly() const
#define INSTANTIATE_SKSL_TARGET_PLATFORM_TEST_SUITE_P(suite_name)
#define INSTANTIATE_RUNTIME_TARGET_PLATFORM_TEST_SUITE_P(suite_name)
#define INSTANTIATE_TARGET_PLATFORM_TEST_SUITE_P(suite_name)
bool operator==(const FlutterPoint &a, const FlutterPoint &b)
std::ostream & operator<<(std::ostream &out, const FlutterPoint &point)
std::unique_ptr< fml::Mapping > OpenFixtureAsMapping(const std::string &fixture_name)
Opens a fixture of the given file name and returns a mapping to its contents.
std::function< ProfileSample(void)> Sampler
Sampler is run during SamplingProfiler::SampleRepeatedly. Each platform should implement its version ...
TEST_P(CompilerTest, CanCompile)
TEST(CompilerTest, Defines)
SourceType SourceTypeFromFileName(const std::filesystem::path &file_name)
bool TargetPlatformIsMetal(TargetPlatform platform)
bool TargetPlatformIsVulkan(TargetPlatform platform)
TargetPlatform target_platform
SourceLanguage source_language
std::string entry_point_name
TargetPlatform target_platform