JSONStringValueSerializer takes a StringPiece instead of std::string&.
[chromium-blink-merge.git] / gpu / command_buffer / service / shader_translator_unittest.cc
blob16c80a20daba94d33996e5193f58acd04c67dad1
1 // Copyright (c) 2012 The Chromium 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.
5 #include <GLES2/gl2.h>
7 #include "gpu/command_buffer/service/shader_translator.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace gpu {
11 namespace gles2 {
13 class ShaderTranslatorTest : public testing::Test {
14 public:
15 ShaderTranslatorTest() {
18 ~ShaderTranslatorTest() override {}
20 protected:
21 void SetUp() override {
22 ShBuiltInResources resources;
23 ShInitBuiltInResources(&resources);
24 resources.MaxExpressionComplexity = 32;
25 resources.MaxCallStackDepth = 32;
27 vertex_translator_ = new ShaderTranslator();
28 fragment_translator_ = new ShaderTranslator();
30 ASSERT_TRUE(vertex_translator_->Init(
31 GL_VERTEX_SHADER, SH_GLES2_SPEC, &resources,
32 ShaderTranslatorInterface::kGlsl,
33 SH_EMULATE_BUILT_IN_FUNCTIONS));
34 ASSERT_TRUE(fragment_translator_->Init(
35 GL_FRAGMENT_SHADER, SH_GLES2_SPEC, &resources,
36 ShaderTranslatorInterface::kGlsl,
37 static_cast<ShCompileOptions>(0)));
39 void TearDown() override {
40 vertex_translator_ = NULL;
41 fragment_translator_ = NULL;
44 scoped_refptr<ShaderTranslator> vertex_translator_;
45 scoped_refptr<ShaderTranslator> fragment_translator_;
48 TEST_F(ShaderTranslatorTest, ValidVertexShader) {
49 const char* shader =
50 "void main() {\n"
51 " gl_Position = vec4(1.0);\n"
52 "}";
54 // A valid shader should be successfully translated.
55 std::string info_log, translated_source;
56 AttributeMap attrib_map;
57 UniformMap uniform_map;
58 VaryingMap varying_map;
59 NameMap name_map;
60 EXPECT_TRUE(vertex_translator_->Translate(shader,
61 &info_log,
62 &translated_source,
63 &attrib_map,
64 &uniform_map,
65 &varying_map,
66 &name_map));
67 // Info log must be NULL.
68 EXPECT_TRUE(info_log.empty());
69 // Translated shader must be valid and non-empty.
70 ASSERT_FALSE(translated_source.empty());
71 // There should be no attributes, uniforms, and only one built-in
72 // varying: gl_Position.
73 EXPECT_TRUE(attrib_map.empty());
74 EXPECT_TRUE(uniform_map.empty());
75 EXPECT_EQ(1u, varying_map.size());
76 // There should be no name mapping.
77 EXPECT_TRUE(name_map.empty());
80 TEST_F(ShaderTranslatorTest, InvalidVertexShader) {
81 const char* bad_shader = "foo-bar";
82 const char* good_shader =
83 "void main() {\n"
84 " gl_Position = vec4(1.0);\n"
85 "}";
87 // An invalid shader should fail.
88 std::string info_log, translated_source;
89 AttributeMap attrib_map;
90 UniformMap uniform_map;
91 VaryingMap varying_map;
92 NameMap name_map;
93 EXPECT_FALSE(vertex_translator_->Translate(bad_shader,
94 &info_log,
95 &translated_source,
96 &attrib_map,
97 &uniform_map,
98 &varying_map,
99 &name_map));
100 // Info log must be valid and non-empty.
101 ASSERT_FALSE(info_log.empty());
102 // Translated shader must be NULL.
103 EXPECT_TRUE(translated_source.empty());
104 // There should be no attributes, uniforms, varyings, or name mapping.
105 EXPECT_TRUE(attrib_map.empty());
106 EXPECT_TRUE(uniform_map.empty());
107 EXPECT_TRUE(varying_map.empty());
108 EXPECT_TRUE(name_map.empty());
110 // Try a good shader after bad.
111 info_log.clear();
112 EXPECT_TRUE(vertex_translator_->Translate(good_shader,
113 &info_log,
114 &translated_source,
115 &attrib_map,
116 &uniform_map,
117 &varying_map,
118 &name_map));
119 EXPECT_TRUE(info_log.empty());
120 EXPECT_FALSE(translated_source.empty());
123 TEST_F(ShaderTranslatorTest, ValidFragmentShader) {
124 const char* shader =
125 "void main() {\n"
126 " gl_FragColor = vec4(1.0);\n"
127 "}";
129 // A valid shader should be successfully translated.
130 std::string info_log, translated_source;
131 AttributeMap attrib_map;
132 UniformMap uniform_map;
133 VaryingMap varying_map;
134 NameMap name_map;
135 EXPECT_TRUE(fragment_translator_->Translate(shader,
136 &info_log,
137 &translated_source,
138 &attrib_map,
139 &uniform_map,
140 &varying_map,
141 &name_map));
142 // Info log must be NULL.
143 EXPECT_TRUE(info_log.empty());
144 // Translated shader must be valid and non-empty.
145 ASSERT_FALSE(translated_source.empty());
146 // There should be no attributes, uniforms, varyings, or name mapping.
147 EXPECT_TRUE(attrib_map.empty());
148 EXPECT_TRUE(uniform_map.empty());
149 EXPECT_TRUE(varying_map.empty());
150 EXPECT_TRUE(name_map.empty());
153 TEST_F(ShaderTranslatorTest, InvalidFragmentShader) {
154 const char* shader = "foo-bar";
156 std::string info_log, translated_source;
157 AttributeMap attrib_map;
158 UniformMap uniform_map;
159 VaryingMap varying_map;
160 NameMap name_map;
161 // An invalid shader should fail.
162 EXPECT_FALSE(fragment_translator_->Translate(shader,
163 &info_log,
164 &translated_source,
165 &attrib_map,
166 &uniform_map,
167 &varying_map,
168 &name_map));
169 // Info log must be valid and non-empty.
170 EXPECT_FALSE(info_log.empty());
171 // Translated shader must be NULL.
172 EXPECT_TRUE(translated_source.empty());
173 // There should be no attributes or uniforms.
174 EXPECT_TRUE(attrib_map.empty());
175 EXPECT_TRUE(uniform_map.empty());
176 EXPECT_TRUE(varying_map.empty());
177 EXPECT_TRUE(name_map.empty());
180 TEST_F(ShaderTranslatorTest, GetAttributes) {
181 const char* shader =
182 "attribute vec4 vPosition;\n"
183 "void main() {\n"
184 " gl_Position = vPosition;\n"
185 "}";
187 std::string info_log, translated_source;
188 AttributeMap attrib_map;
189 UniformMap uniform_map;
190 VaryingMap varying_map;
191 NameMap name_map;
192 EXPECT_TRUE(vertex_translator_->Translate(shader,
193 &info_log,
194 &translated_source,
195 &attrib_map,
196 &uniform_map,
197 &varying_map,
198 &name_map));
199 // Info log must be NULL.
200 EXPECT_TRUE(info_log.empty());
201 // Translated shader must be valid and non-empty.
202 EXPECT_FALSE(translated_source.empty());
203 // There should be no uniforms.
204 EXPECT_TRUE(uniform_map.empty());
205 // There should be one attribute with following characteristics:
206 // name:vPosition type:GL_FLOAT_VEC4 size:0.
207 EXPECT_EQ(1u, attrib_map.size());
208 AttributeMap::const_iterator iter = attrib_map.find("vPosition");
209 EXPECT_TRUE(iter != attrib_map.end());
210 EXPECT_EQ(static_cast<GLenum>(GL_FLOAT_VEC4), iter->second.type);
211 EXPECT_EQ(0u, iter->second.arraySize);
212 EXPECT_EQ("vPosition", iter->second.name);
215 TEST_F(ShaderTranslatorTest, GetUniforms) {
216 const char* shader =
217 "precision mediump float;\n"
218 "struct Foo {\n"
219 " vec4 color[1];\n"
220 "};\n"
221 "struct Bar {\n"
222 " Foo foo;\n"
223 "};\n"
224 "uniform Bar bar[2];\n"
225 "void main() {\n"
226 " gl_FragColor = bar[0].foo.color[0] + bar[1].foo.color[0];\n"
227 "}";
229 std::string info_log, translated_source;
230 AttributeMap attrib_map;
231 UniformMap uniform_map;
232 VaryingMap varying_map;
233 NameMap name_map;
234 EXPECT_TRUE(fragment_translator_->Translate(shader,
235 &info_log,
236 &translated_source,
237 &attrib_map,
238 &uniform_map,
239 &varying_map,
240 &name_map));
241 // Info log must be NULL.
242 EXPECT_TRUE(info_log.empty());
243 // Translated shader must be valid and non-empty.
244 EXPECT_FALSE(translated_source.empty());
245 // There should be no attributes.
246 EXPECT_TRUE(attrib_map.empty());
247 // There should be two uniforms with following characteristics:
248 // 1. name:bar[0].foo.color[0] type:GL_FLOAT_VEC4 size:1
249 // 2. name:bar[1].foo.color[0] type:GL_FLOAT_VEC4 size:1
250 // However, there will be only one entry "bar" in the map.
251 EXPECT_EQ(1u, uniform_map.size());
252 UniformMap::const_iterator iter = uniform_map.find("bar");
253 EXPECT_TRUE(iter != uniform_map.end());
254 // First uniform.
255 const sh::ShaderVariable* info;
256 std::string original_name;
257 EXPECT_TRUE(iter->second.findInfoByMappedName(
258 "bar[0].foo.color[0]", &info, &original_name));
259 EXPECT_EQ(static_cast<GLenum>(GL_FLOAT_VEC4), info->type);
260 EXPECT_EQ(1u, info->arraySize);
261 EXPECT_STREQ("color", info->name.c_str());
262 EXPECT_STREQ("bar[0].foo.color[0]", original_name.c_str());
263 // Second uniform.
264 EXPECT_TRUE(iter->second.findInfoByMappedName(
265 "bar[1].foo.color[0]", &info, &original_name));
266 EXPECT_EQ(static_cast<GLenum>(GL_FLOAT_VEC4), info->type);
267 EXPECT_EQ(1u, info->arraySize);
268 EXPECT_STREQ("color", info->name.c_str());
269 EXPECT_STREQ("bar[1].foo.color[0]", original_name.c_str());
272 #if defined(OS_MACOSX)
273 TEST_F(ShaderTranslatorTest, BuiltInFunctionEmulation) {
274 // This test might become invalid in the future when ANGLE Translator is no
275 // longer emulate dot(float, float) in Mac, or the emulated function name is
276 // no longer webgl_dot_emu.
277 const char* shader =
278 "void main() {\n"
279 " gl_Position = vec4(dot(1.0, 1.0), 1.0, 1.0, 1.0);\n"
280 "}";
282 std::string info_log, translated_source;
283 AttributeMap attrib_map;
284 UniformMap uniform_map;
285 VaryingMap varying_map;
286 NameMap name_map;
287 EXPECT_TRUE(vertex_translator_->Translate(shader,
288 &info_log,
289 &translated_source,
290 &attrib_map,
291 &uniform_map,
292 &varying_map,
293 &name_map));
294 // Info log must be NULL.
295 EXPECT_TRUE(info_log.empty());
296 // Translated shader must be valid and non-empty.
297 ASSERT_FALSE(translated_source.empty());
298 EXPECT_TRUE(strstr(translated_source.c_str(),
299 "webgl_dot_emu") != NULL);
301 #endif
303 TEST_F(ShaderTranslatorTest, OptionsString) {
304 scoped_refptr<ShaderTranslator> translator_1 = new ShaderTranslator();
305 scoped_refptr<ShaderTranslator> translator_2 = new ShaderTranslator();
306 scoped_refptr<ShaderTranslator> translator_3 = new ShaderTranslator();
308 ShBuiltInResources resources;
309 ShInitBuiltInResources(&resources);
311 ASSERT_TRUE(translator_1->Init(
312 GL_VERTEX_SHADER, SH_GLES2_SPEC, &resources,
313 ShaderTranslatorInterface::kGlsl,
314 SH_EMULATE_BUILT_IN_FUNCTIONS));
315 ASSERT_TRUE(translator_2->Init(
316 GL_FRAGMENT_SHADER, SH_GLES2_SPEC, &resources,
317 ShaderTranslatorInterface::kGlsl,
318 static_cast<ShCompileOptions>(0)));
319 resources.EXT_draw_buffers = 1;
320 ASSERT_TRUE(translator_3->Init(
321 GL_VERTEX_SHADER, SH_GLES2_SPEC, &resources,
322 ShaderTranslatorInterface::kGlsl,
323 SH_EMULATE_BUILT_IN_FUNCTIONS));
325 std::string options_1(
326 translator_1->GetStringForOptionsThatWouldAffectCompilation());
327 std::string options_2(
328 translator_1->GetStringForOptionsThatWouldAffectCompilation());
329 std::string options_3(
330 translator_2->GetStringForOptionsThatWouldAffectCompilation());
331 std::string options_4(
332 translator_3->GetStringForOptionsThatWouldAffectCompilation());
334 EXPECT_EQ(options_1, options_2);
335 EXPECT_NE(options_1, options_3);
336 EXPECT_NE(options_1, options_4);
337 EXPECT_NE(options_3, options_4);
340 } // namespace gles2
341 } // namespace gpu