OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <GLES2/gl2.h> | 5 #include <GLES2/gl2.h> |
6 | 6 |
7 #include "gpu/command_buffer/service/shader_translator.h" | 7 #include "gpu/command_buffer/service/shader_translator.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/gl/gl_version_info.h" |
9 | 10 |
10 namespace gpu { | 11 namespace gpu { |
11 namespace gles2 { | 12 namespace gles2 { |
12 | 13 |
13 class ShaderTranslatorTest : public testing::Test { | 14 class ShaderTranslatorTest : public testing::Test { |
14 public: | 15 public: |
15 ShaderTranslatorTest() { | 16 ShaderTranslatorTest() { |
| 17 shader_output_language_ = |
| 18 ShaderTranslator::GetShaderOutputLanguageForContext( |
| 19 gfx::GLVersionInfo("2.0", "", "")); |
16 } | 20 } |
17 | 21 |
18 ~ShaderTranslatorTest() override {} | 22 ~ShaderTranslatorTest() override {} |
19 | 23 |
20 protected: | 24 protected: |
21 void SetUp() override { | 25 void SetUp() override { |
22 ShBuiltInResources resources; | 26 ShBuiltInResources resources; |
23 ShInitBuiltInResources(&resources); | 27 ShInitBuiltInResources(&resources); |
24 resources.MaxExpressionComplexity = 32; | 28 resources.MaxExpressionComplexity = 32; |
25 resources.MaxCallStackDepth = 32; | 29 resources.MaxCallStackDepth = 32; |
26 | 30 |
27 vertex_translator_ = new ShaderTranslator(); | 31 vertex_translator_ = new ShaderTranslator(); |
28 fragment_translator_ = new ShaderTranslator(); | 32 fragment_translator_ = new ShaderTranslator(); |
29 | 33 |
30 ASSERT_TRUE(vertex_translator_->Init( | 34 ASSERT_TRUE(vertex_translator_->Init(GL_VERTEX_SHADER, SH_GLES2_SPEC, |
31 GL_VERTEX_SHADER, SH_GLES2_SPEC, &resources, | 35 &resources, shader_output_language_, |
32 ShaderTranslatorInterface::kGlsl, | 36 SH_EMULATE_BUILT_IN_FUNCTIONS)); |
33 SH_EMULATE_BUILT_IN_FUNCTIONS)); | 37 ASSERT_TRUE(fragment_translator_->Init(GL_FRAGMENT_SHADER, SH_GLES2_SPEC, |
34 ASSERT_TRUE(fragment_translator_->Init( | 38 &resources, shader_output_language_, |
35 GL_FRAGMENT_SHADER, SH_GLES2_SPEC, &resources, | 39 static_cast<ShCompileOptions>(0))); |
36 ShaderTranslatorInterface::kGlsl, | |
37 static_cast<ShCompileOptions>(0))); | |
38 } | 40 } |
39 void TearDown() override { | 41 void TearDown() override { |
40 vertex_translator_ = NULL; | 42 vertex_translator_ = NULL; |
41 fragment_translator_ = NULL; | 43 fragment_translator_ = NULL; |
42 } | 44 } |
43 | 45 |
44 scoped_refptr<ShaderTranslator> vertex_translator_; | 46 scoped_refptr<ShaderTranslator> vertex_translator_; |
45 scoped_refptr<ShaderTranslator> fragment_translator_; | 47 scoped_refptr<ShaderTranslator> fragment_translator_; |
| 48 ShShaderOutput shader_output_language_; |
46 }; | 49 }; |
47 | 50 |
48 TEST_F(ShaderTranslatorTest, ValidVertexShader) { | 51 TEST_F(ShaderTranslatorTest, ValidVertexShader) { |
49 const char* shader = | 52 const char* shader = |
50 "void main() {\n" | 53 "void main() {\n" |
51 " gl_Position = vec4(1.0);\n" | 54 " gl_Position = vec4(1.0);\n" |
52 "}"; | 55 "}"; |
53 | 56 |
54 // A valid shader should be successfully translated. | 57 // A valid shader should be successfully translated. |
55 std::string info_log, translated_source; | 58 std::string info_log, translated_source; |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 } | 286 } |
284 | 287 |
285 TEST_F(ShaderTranslatorTest, OptionsString) { | 288 TEST_F(ShaderTranslatorTest, OptionsString) { |
286 scoped_refptr<ShaderTranslator> translator_1 = new ShaderTranslator(); | 289 scoped_refptr<ShaderTranslator> translator_1 = new ShaderTranslator(); |
287 scoped_refptr<ShaderTranslator> translator_2 = new ShaderTranslator(); | 290 scoped_refptr<ShaderTranslator> translator_2 = new ShaderTranslator(); |
288 scoped_refptr<ShaderTranslator> translator_3 = new ShaderTranslator(); | 291 scoped_refptr<ShaderTranslator> translator_3 = new ShaderTranslator(); |
289 | 292 |
290 ShBuiltInResources resources; | 293 ShBuiltInResources resources; |
291 ShInitBuiltInResources(&resources); | 294 ShInitBuiltInResources(&resources); |
292 | 295 |
293 ASSERT_TRUE(translator_1->Init( | 296 ASSERT_TRUE(translator_1->Init(GL_VERTEX_SHADER, SH_GLES2_SPEC, &resources, |
294 GL_VERTEX_SHADER, SH_GLES2_SPEC, &resources, | 297 SH_GLSL_150_CORE_OUTPUT, |
295 ShaderTranslatorInterface::kGlsl, | 298 SH_EMULATE_BUILT_IN_FUNCTIONS)); |
296 SH_EMULATE_BUILT_IN_FUNCTIONS)); | 299 ASSERT_TRUE(translator_2->Init(GL_FRAGMENT_SHADER, SH_GLES2_SPEC, &resources, |
297 ASSERT_TRUE(translator_2->Init( | 300 SH_GLSL_150_CORE_OUTPUT, |
298 GL_FRAGMENT_SHADER, SH_GLES2_SPEC, &resources, | 301 static_cast<ShCompileOptions>(0))); |
299 ShaderTranslatorInterface::kGlsl, | |
300 static_cast<ShCompileOptions>(0))); | |
301 resources.EXT_draw_buffers = 1; | 302 resources.EXT_draw_buffers = 1; |
302 ASSERT_TRUE(translator_3->Init( | 303 ASSERT_TRUE(translator_3->Init(GL_VERTEX_SHADER, SH_GLES2_SPEC, &resources, |
303 GL_VERTEX_SHADER, SH_GLES2_SPEC, &resources, | 304 SH_GLSL_150_CORE_OUTPUT, |
304 ShaderTranslatorInterface::kGlsl, | 305 SH_EMULATE_BUILT_IN_FUNCTIONS)); |
305 SH_EMULATE_BUILT_IN_FUNCTIONS)); | |
306 | 306 |
307 std::string options_1( | 307 std::string options_1( |
308 translator_1->GetStringForOptionsThatWouldAffectCompilation()); | 308 translator_1->GetStringForOptionsThatWouldAffectCompilation()); |
309 std::string options_2( | 309 std::string options_2( |
310 translator_1->GetStringForOptionsThatWouldAffectCompilation()); | 310 translator_1->GetStringForOptionsThatWouldAffectCompilation()); |
311 std::string options_3( | 311 std::string options_3( |
312 translator_2->GetStringForOptionsThatWouldAffectCompilation()); | 312 translator_2->GetStringForOptionsThatWouldAffectCompilation()); |
313 std::string options_4( | 313 std::string options_4( |
314 translator_3->GetStringForOptionsThatWouldAffectCompilation()); | 314 translator_3->GetStringForOptionsThatWouldAffectCompilation()); |
315 | 315 |
316 EXPECT_EQ(options_1, options_2); | 316 EXPECT_EQ(options_1, options_2); |
317 EXPECT_NE(options_1, options_3); | 317 EXPECT_NE(options_1, options_3); |
318 EXPECT_NE(options_1, options_4); | 318 EXPECT_NE(options_1, options_4); |
319 EXPECT_NE(options_3, options_4); | 319 EXPECT_NE(options_3, options_4); |
320 } | 320 } |
321 | 321 |
| 322 class ShaderTranslatorOutputVersionTest |
| 323 : public testing::TestWithParam<testing::tuple<const char*, const char*>> { |
| 324 }; |
| 325 |
| 326 TEST_P(ShaderTranslatorOutputVersionTest, HasCorrectOutputGLSLVersion) { |
| 327 // Test that translating to a shader targeting certain OpenGL context version |
| 328 // (version string in test param tuple index 0) produces a GLSL shader that |
| 329 // contains correct version string for that context (version directive |
| 330 // in test param tuple index 1). |
| 331 |
| 332 const char* kShader = |
| 333 "attribute vec4 vPosition;\n" |
| 334 "void main() {\n" |
| 335 " gl_Position = vPosition;\n" |
| 336 "}"; |
| 337 |
| 338 gfx::GLVersionInfo output_context_version(testing::get<0>(GetParam()), "", |
| 339 ""); |
| 340 |
| 341 scoped_refptr<ShaderTranslator> translator = new ShaderTranslator(); |
| 342 ShBuiltInResources resources; |
| 343 ShInitBuiltInResources(&resources); |
| 344 ShCompileOptions compile_options = SH_OBJECT_CODE; |
| 345 ShShaderOutput shader_output_language = |
| 346 ShaderTranslator::GetShaderOutputLanguageForContext( |
| 347 output_context_version); |
| 348 ASSERT_TRUE(translator->Init(GL_VERTEX_SHADER, SH_GLES2_SPEC, &resources, |
| 349 shader_output_language, compile_options)); |
| 350 |
| 351 std::string translated_source; |
| 352 int shader_version; |
| 353 EXPECT_TRUE(translator->Translate(kShader, nullptr, &translated_source, |
| 354 &shader_version, nullptr, nullptr, nullptr, |
| 355 nullptr)); |
| 356 |
| 357 std::string expected_version_directive = testing::get<1>(GetParam()); |
| 358 if (expected_version_directive.empty()) { |
| 359 EXPECT_TRUE(translated_source.find("#version") == std::string::npos) |
| 360 << "Translation was:\n" << translated_source; |
| 361 } else { |
| 362 EXPECT_TRUE(translated_source.find(expected_version_directive) != |
| 363 std::string::npos) |
| 364 << "Translation was:\n" << translated_source; |
| 365 } |
| 366 } |
| 367 |
| 368 // For some compilers, using make_tuple("a", "bb") would end up |
| 369 // instantiating make_tuple<char[1], char[2]>. This does not work. |
| 370 namespace { |
| 371 testing::tuple<const char*, const char*> make_gl_glsl_tuple( |
| 372 const char* gl_version, |
| 373 const char* glsl_version_directive) { |
| 374 return testing::make_tuple(gl_version, glsl_version_directive); |
| 375 } |
| 376 } |
| 377 |
| 378 // Test data for the above test. OpenGL specifications specify a |
| 379 // certain version of GLSL to be guaranteed to be supported. Test |
| 380 // that ShaderTranslator produces a GLSL shader with the exact |
| 381 // specified GLSL version for each known OpenGL version. |
| 382 INSTANTIATE_TEST_CASE_P( |
| 383 KnownOpenGLContexts, |
| 384 ShaderTranslatorOutputVersionTest, |
| 385 testing::Values(make_gl_glsl_tuple("4.5", "#version 450\n"), |
| 386 make_gl_glsl_tuple("4.4", "#version 440\n"), |
| 387 make_gl_glsl_tuple("4.3", "#version 430\n"), |
| 388 make_gl_glsl_tuple("4.2", "#version 420\n"), |
| 389 make_gl_glsl_tuple("4.1", "#version 410\n"), |
| 390 make_gl_glsl_tuple("4.0", "#version 400\n"), |
| 391 make_gl_glsl_tuple("3.3", "#version 330\n"), |
| 392 make_gl_glsl_tuple("3.2", "#version 150\n"), |
| 393 make_gl_glsl_tuple("3.1", "#version 140\n"), |
| 394 make_gl_glsl_tuple("3.0", "#version 130\n"))); |
| 395 |
| 396 // Test data for the above test. Check that early OpenGL contexts get |
| 397 // GLSL compatibility profile shader, e.g. shader has no #version |
| 398 // directive. Also check that future version 3.3+ OpenGL contexts get |
| 399 // similar shader. We do not expect that future 3.3+ specs contain |
| 400 // the "all eariler GLSL versions" clause, since 3.3 did not contain |
| 401 // it either. |
| 402 INSTANTIATE_TEST_CASE_P(OldOrUnknownOpenGLContexts, |
| 403 ShaderTranslatorOutputVersionTest, |
| 404 testing::Values(make_gl_glsl_tuple("3.4", ""), |
| 405 make_gl_glsl_tuple("2.0", ""))); |
| 406 |
| 407 // Test data for the above test. Cases for the future OpenGL versions. The |
| 408 // code assumes that the future OpenGL specs specify the clause that all |
| 409 // earlier GLSL versions are supported. We select the highest GLSL |
| 410 // version known at the time of writing. |
| 411 INSTANTIATE_TEST_CASE_P( |
| 412 BackwardsCompatibleFutureOpenGLContexts, |
| 413 ShaderTranslatorOutputVersionTest, |
| 414 testing::Values(make_gl_glsl_tuple("5.0", "#version 450\n"), |
| 415 make_gl_glsl_tuple("4.6", "#version 450\n"))); |
| 416 |
| 417 // Test data for the above test. Check that for the OpenGL ES output |
| 418 // contexts, the shader is such that GLSL 1.0 is used. The translator |
| 419 // selects GLSL 1.0 by not output any version at the moment, though we |
| 420 // do not know if that would be correct for the future OpenGL ES specs. |
| 421 INSTANTIATE_TEST_CASE_P(OpenGLESContexts, |
| 422 ShaderTranslatorOutputVersionTest, |
| 423 testing::Values(make_gl_glsl_tuple("opengl es 2.0", ""), |
| 424 make_gl_glsl_tuple("opengl es 3.0", ""), |
| 425 make_gl_glsl_tuple("opengl es 3.1", ""), |
| 426 make_gl_glsl_tuple("opengl es 3.2", |
| 427 ""))); |
| 428 |
322 } // namespace gles2 | 429 } // namespace gles2 |
323 } // namespace gpu | 430 } // namespace gpu |
324 | 431 |
OLD | NEW |