| Flutter Impeller |

compiler_unittests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "flutter/testing/testing.h"
6 #include "impeller/base/validation.h"
7 #include "impeller/compiler/compiler.h"
8 #include "impeller/compiler/compiler_test.h"
9 #include "impeller/compiler/source_options.h"
10 #include "impeller/compiler/types.h"
11
12 namespace impeller {
13 namespace compiler {
14 namespace testing {
15
16 TEST(CompilerTest, ShaderKindMatchingIsSuccessful) {
17 ASSERT_EQ(SourceTypeFromFileName("hello.vert"), SourceType::kVertexShader);
18 ASSERT_EQ(SourceTypeFromFileName("hello.frag"), SourceType::kFragmentShader);
19 ASSERT_EQ(SourceTypeFromFileName("hello.tesc"),
20 SourceType::kTessellationControlShader);
21 ASSERT_EQ(SourceTypeFromFileName("hello.tese"),
22 SourceType::kTessellationEvaluationShader);
23 ASSERT_EQ(SourceTypeFromFileName("hello.comp"), SourceType::kComputeShader);
24 ASSERT_EQ(SourceTypeFromFileName("hello.msl"), SourceType::kUnknown);
25 ASSERT_EQ(SourceTypeFromFileName("hello.glsl"), SourceType::kUnknown);
26 }
27
28 TEST_P(CompilerTest, CanCompile) {
29 ASSERT_TRUE(CanCompileAndReflect("sample.vert"));
30 ASSERT_TRUE(CanCompileAndReflect("sample.vert", SourceType::kVertexShader));
31 ASSERT_TRUE(CanCompileAndReflect("sample.vert", SourceType::kVertexShader,
32 SourceLanguage::kGLSL));
33 }
34
35 TEST_P(CompilerTest, CanCompileHLSL) {
36 ASSERT_TRUE(CanCompileAndReflect(
37 "simple.vert.hlsl", SourceType::kVertexShader, SourceLanguage::kHLSL));
38 }
39
40 TEST_P(CompilerTest, CanCompileHLSLWithMultipleStages) {
41 ASSERT_TRUE(CanCompileAndReflect("multiple_stages.hlsl",
42 SourceType::kVertexShader,
43 SourceLanguage::kHLSL, "VertexShader"));
44 ASSERT_TRUE(CanCompileAndReflect("multiple_stages.hlsl",
45 SourceType::kFragmentShader,
46 SourceLanguage::kHLSL, "FragmentShader"));
47 }
48
49 TEST_P(CompilerTest, CanCompileTessellationControlShader) {
50 ASSERT_TRUE(CanCompileAndReflect("sample.tesc"));
51 ASSERT_TRUE(CanCompileAndReflect("sample.tesc",
52 SourceType::kTessellationControlShader));
53 }
54
55 TEST_P(CompilerTest, CanCompileTessellationEvaluationShader) {
56 ASSERT_TRUE(CanCompileAndReflect("sample.tese"));
57 ASSERT_TRUE(CanCompileAndReflect("sample.tese",
58 SourceType::kTessellationEvaluationShader));
59 }
60
61 TEST_P(CompilerTest, CanCompileComputeShader) {
62 if (!TargetPlatformIsMetal(GetParam())) {
63 GTEST_SKIP_("Only enabled on Metal backends till ES 3.2 support is added.");
64 }
65 ASSERT_TRUE(CanCompileAndReflect("sample.comp"));
66 ASSERT_TRUE(CanCompileAndReflect("sample.comp", SourceType::kComputeShader));
67 }
68
69 TEST_P(CompilerTest, MustFailDueToExceedingResourcesLimit) {
70 ScopedValidationDisable disable_validation;
71 ASSERT_FALSE(
72 CanCompileAndReflect("resources_limit.vert", SourceType::kVertexShader));
73 }
74
75 TEST_P(CompilerTest, MustFailDueToMultipleLocationPerStructMember) {
76 ScopedValidationDisable disable_validation;
77 ASSERT_FALSE(CanCompileAndReflect("struct_def_bug.vert"));
78 }
79
80 TEST_P(CompilerTest, BindingBaseForFragShader) {
81 if (!TargetPlatformIsVulkan(GetParam())) {
82 GTEST_SKIP();
83 }
84
85 ASSERT_TRUE(CanCompileAndReflect("sample.vert", SourceType::kVertexShader));
86 ASSERT_TRUE(CanCompileAndReflect("sample.frag", SourceType::kFragmentShader));
87
88 auto get_binding = [&](const char* fixture) -> uint32_t {
89 auto json_fd = GetReflectionJson(fixture);
90 nlohmann::json shader_json = nlohmann::json::parse(json_fd->GetMapping());
91 return shader_json["buffers"][0]["binding"].get<uint32_t>();
92 };
93
94 auto vert_uniform_binding = get_binding("sample.vert");
95 auto frag_uniform_binding = get_binding("sample.frag");
96
97 ASSERT_GT(frag_uniform_binding, vert_uniform_binding);
98 }
99
100 TEST_P(CompilerTest, UniformsHaveBindingAndSet) {
101 ASSERT_TRUE(CanCompileAndReflect("sample_with_binding.vert",
102 SourceType::kVertexShader));
103 ASSERT_TRUE(CanCompileAndReflect("sample.frag", SourceType::kFragmentShader));
104
105 struct binding_and_set {
106 uint32_t binding;
107 uint32_t set;
108 };
109
110 auto get_binding = [&](const char* fixture) -> binding_and_set {
111 auto json_fd = GetReflectionJson(fixture);
112 nlohmann::json shader_json = nlohmann::json::parse(json_fd->GetMapping());
113 uint32_t binding = shader_json["buffers"][0]["binding"].get<uint32_t>();
114 uint32_t set = shader_json["buffers"][0]["set"].get<uint32_t>();
115 return {binding, set};
116 };
117
118 auto vert_uniform_binding = get_binding("sample_with_binding.vert");
119 auto frag_uniform_binding = get_binding("sample.frag");
120
121 ASSERT_EQ(frag_uniform_binding.set, 0u);
122 ASSERT_EQ(vert_uniform_binding.set, 3u);
123 ASSERT_EQ(vert_uniform_binding.binding, 17u);
124 }
125
126 #define INSTANTIATE_TARGET_PLATFORM_TEST_SUITE_P(suite_name) \
127 INSTANTIATE_TEST_SUITE_P( \
128 suite_name, CompilerTest, \
129 ::testing::Values( \
130 TargetPlatform::kOpenGLES, TargetPlatform::kOpenGLDesktop, \
131 TargetPlatform::kMetalDesktop, TargetPlatform::kMetalIOS), \
132 [](const ::testing::TestParamInfo<CompilerTest::ParamType>& info) { \
133 return TargetPlatformToString(info.param); \
134 });
135
136 INSTANTIATE_TARGET_PLATFORM_TEST_SUITE_P(CompilerSuite);
137
138 } // namespace testing
139 } // namespace compiler
140 } // namespace impeller
impeller::compiler::testing::TEST
TEST(CompilerTest, ShaderKindMatchingIsSuccessful)
Definition: compiler_unittests.cc:16
impeller::compiler::testing::INSTANTIATE_TARGET_PLATFORM_TEST_SUITE_P
INSTANTIATE_TARGET_PLATFORM_TEST_SUITE_P(CompilerSuite)
impeller::compiler::SourceType::kUnknown
@ kUnknown
impeller::compiler::SourceType::kTessellationControlShader
@ kTessellationControlShader
validation.h
impeller::compiler::SourceLanguage::kGLSL
@ kGLSL
impeller::compiler::SourceLanguage::kHLSL
@ kHLSL
impeller::compiler::SourceType::kFragmentShader
@ kFragmentShader
source_options.h
impeller::compiler::SourceType::kComputeShader
@ kComputeShader
compiler.h
impeller::compiler::TargetPlatformIsMetal
bool TargetPlatformIsMetal(TargetPlatform platform)
Definition: types.cc:281
impeller::compiler::TargetPlatformIsVulkan
bool TargetPlatformIsVulkan(TargetPlatform platform)
Definition: types.cc:299
compiler_test.h
impeller::compiler::SourceTypeFromFileName
SourceType SourceTypeFromFileName(const std::string &file_name)
Definition: types.cc:29
impeller::compiler::testing::TEST_P
TEST_P(CompilerTest, CanCompile)
Definition: compiler_unittests.cc:28
impeller::compiler::testing::CompilerTest
Definition: compiler_test.h:18
impeller::ScopedValidationDisable
Definition: validation.h:37
impeller::compiler::SourceType::kVertexShader
@ kVertexShader
impeller
Definition: aiks_context.cc:10
impeller::compiler::SourceType::kTessellationEvaluationShader
@ kTessellationEvaluationShader
types.h