1 // Copyright 2019 The Amber Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or parseried.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "gtest/gtest.h"
16 #include "src/amberscript/parser.h"
17
18 namespace amber {
19 namespace amberscript {
20
21 using AmberScriptParserTest = testing::Test;
22
TEST_F(AmberScriptParserTest,PipelineShaderOptimization)23 TEST_F(AmberScriptParserTest, PipelineShaderOptimization) {
24 std::string in = R"(
25 SHADER vertex my_shader PASSTHROUGH
26 SHADER fragment my_fragment GLSL
27 # GLSL Shader
28 END
29 SHADER geometry my_geom GLSL
30 # Geom shader
31 END
32 PIPELINE graphics my_pipeline
33 ATTACH my_shader
34 SHADER_OPTIMIZATION my_shader
35 opt1
36 opt_second
37 END
38
39 ATTACH my_fragment
40 SHADER_OPTIMIZATION my_fragment
41 another_optimization
42 third
43 END
44
45 ATTACH my_geom
46 SHADER_OPTIMIZATION my_geom
47 END
48 END
49 )";
50
51 Parser parser;
52 Result r = parser.Parse(in);
53 ASSERT_TRUE(r.IsSuccess()) << r.Error();
54
55 auto script = parser.GetScript();
56 const auto& pipelines = script->GetPipelines();
57 ASSERT_EQ(1U, pipelines.size());
58
59 const auto* pipeline = pipelines[0].get();
60 const auto& shaders = pipeline->GetShaders();
61 ASSERT_EQ(3U, shaders.size());
62
63 ASSERT_TRUE(shaders[0].GetShader() != nullptr);
64 EXPECT_EQ(kShaderTypeVertex, shaders[0].GetShader()->GetType());
65 std::vector<std::string> my_shader_opts = {"opt1", "opt_second"};
66 EXPECT_EQ(my_shader_opts, shaders[0].GetShaderOptimizations());
67
68 ASSERT_TRUE(shaders[1].GetShader() != nullptr);
69 EXPECT_EQ(kShaderTypeFragment, shaders[1].GetShader()->GetType());
70 std::vector<std::string> my_fragment_opts = {"another_optimization", "third"};
71 EXPECT_EQ(my_fragment_opts, shaders[1].GetShaderOptimizations());
72
73 ASSERT_TRUE(shaders[2].GetShader() != nullptr);
74 EXPECT_EQ(kShaderTypeGeometry, shaders[2].GetShader()->GetType());
75 std::vector<std::string> my_geom_opts = {};
76 EXPECT_EQ(my_geom_opts, shaders[2].GetShaderOptimizations());
77 }
78
TEST_F(AmberScriptParserTest,PipelineShaderOptmizationInvalidShader)79 TEST_F(AmberScriptParserTest, PipelineShaderOptmizationInvalidShader) {
80 std::string in = R"(
81 PIPELINE graphics my_pipeline
82 SHADER_OPTIMIZATION invalid_shader
83 opt1
84 opt_second
85 END)";
86
87 Parser parser;
88 Result r = parser.Parse(in);
89 ASSERT_FALSE(r.IsSuccess());
90 EXPECT_EQ("3: unknown shader in SHADER_OPTIMIZATION command", r.Error());
91 }
92
TEST_F(AmberScriptParserTest,PipelineShaderOptmizationMissingShader)93 TEST_F(AmberScriptParserTest, PipelineShaderOptmizationMissingShader) {
94 std::string in = R"(
95 PIPELINE graphics my_pipeline
96 SHADER_OPTIMIZATION
97 opt1
98 opt_second
99 END)";
100
101 Parser parser;
102 Result r = parser.Parse(in);
103 ASSERT_FALSE(r.IsSuccess());
104 EXPECT_EQ("4: missing shader name in SHADER_OPTIMIZATION command", r.Error());
105 }
106
TEST_F(AmberScriptParserTest,PipelineShaderOptmizationnUnAttachedShader)107 TEST_F(AmberScriptParserTest, PipelineShaderOptmizationnUnAttachedShader) {
108 std::string in = R"(
109 SHADER vertex my_vertex PASSTHROUGH
110 PIPELINE graphics my_pipeline
111 SHADER_OPTIMIZATION my_vertex
112 opt1
113 opt_second
114 END
115 END)";
116
117 Parser parser;
118 Result r = parser.Parse(in);
119 ASSERT_FALSE(r.IsSuccess());
120 EXPECT_EQ("7: unknown shader specified for optimizations: my_vertex",
121 r.Error());
122 }
123
TEST_F(AmberScriptParserTest,PipelineShaderOptimizationMissingEnd)124 TEST_F(AmberScriptParserTest, PipelineShaderOptimizationMissingEnd) {
125 std::string in = R"(
126 SHADER vertex my_shader PASSTHROUGH
127 PIPELINE graphics my_pipeline
128 ATTACH my_shader
129 SHADER_OPTIMIZATION my_shader
130 opt1
131 opt_second)";
132
133 Parser parser;
134 Result r = parser.Parse(in);
135 ASSERT_FALSE(r.IsSuccess());
136 EXPECT_EQ("7: SHADER_OPTIMIZATION missing END command", r.Error());
137 }
138
TEST_F(AmberScriptParserTest,PipelineShaderOptimizationExtraParams)139 TEST_F(AmberScriptParserTest, PipelineShaderOptimizationExtraParams) {
140 std::string in = R"(
141 SHADER vertex my_shader PASSTHROUGH
142 PIPELINE graphics my_pipeline
143 ATTACH my_shader
144 SHADER_OPTIMIZATION my_shader EXTRA
145 opt1
146 opt_second
147 END
148 END)";
149
150 Parser parser;
151 Result r = parser.Parse(in);
152 ASSERT_FALSE(r.IsSuccess());
153 EXPECT_EQ("5: extra parameters after SHADER_OPTIMIZATION command: EXTRA",
154 r.Error());
155 }
156
TEST_F(AmberScriptParserTest,PipelineShaderOptimizationNonStringParam)157 TEST_F(AmberScriptParserTest, PipelineShaderOptimizationNonStringParam) {
158 std::string in = R"(
159 SHADER vertex my_shader PASSTHROUGH
160 PIPELINE graphics my_pipeline
161 ATTACH my_shader
162 SHADER_OPTIMIZATION my_shader
163 123
164 opt
165 END
166 END)";
167
168 Parser parser;
169 Result r = parser.Parse(in);
170 ASSERT_FALSE(r.IsSuccess());
171 EXPECT_EQ("6: SHADER_OPTIMIZATION options must be identifiers", r.Error());
172 }
173
174 } // namespace amberscript
175 } // namespace amber
176