1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
  441
  442
  443
  444
  445
  446
  447
  448
  449
  450
  451
  452
  453
  454
  455
  456
  457
  458
  459
  460
  461
  462
  463
  464
  465
  466
  467
  468
  469
  470
  471
  472
  473
  474
  475
  476
  477
  478
  479
  480
  481
  482
  483
  484
  485
  486
  487
  488
  489
  490
  491
  492
  493
  494
  495
  496
  497
  498
  499
  500
  501
  502
  503
  504
  505
  506
  507
  508
  509
  510
  511
  512
  513
  514
  515
  516
  517
  518
  519
  520
  521
  522
  523
  524
  525
  526
  527
  528
  529
  530
  531
  532
  533
  534
  535
  536
  537
  538
  539
  540
  541
  542
  543
  544
  545
  546
  547
  548
  549
  550
  551
  552
  553
  554
  555
  556
  557
  558
  559
  560

gpu / command_buffer / service / shader_translator_unittest.cc [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gpu/command_buffer/service/shader_translator.h"

#include "base/containers/contains.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gl/gl_bindings.h"

namespace gpu {
namespace gles2 {

class ShaderTranslatorTest : public testing::Test {
 public:
  ShaderTranslatorTest() = default;
  ~ShaderTranslatorTest() override = default;

 protected:
  void SetUp() override {
#if BUILDFLAG(IS_WIN) && defined(ARCH_CPU_ARM64)
    GTEST_SKIP() << "Angle doesn't support OpenGL on Windows";
#else
    ShBuiltInResources resources;
    sh::InitBuiltInResources(&resources);
    resources.MaxExpressionComplexity = 32;
    resources.MaxCallStackDepth = 32;

    vertex_translator_ = new ShaderTranslator();
    fragment_translator_ = new ShaderTranslator();

    ASSERT_TRUE(vertex_translator_->Init(GL_VERTEX_SHADER, SH_GLES2_SPEC,
                                         &resources, SH_ESSL_OUTPUT, {},
                                         false));
    ASSERT_TRUE(fragment_translator_->Init(GL_FRAGMENT_SHADER, SH_GLES2_SPEC,
                                           &resources, SH_ESSL_OUTPUT, {},
                                           false));
#endif  //  BUILDFLAG(IS_WIN) && defined(ARCH_CPU_ARM64)
  }
  void TearDown() override {
    vertex_translator_ = nullptr;
    fragment_translator_ = nullptr;
  }

  scoped_refptr<ShaderTranslator> vertex_translator_;
  scoped_refptr<ShaderTranslator> fragment_translator_;
};

class ES3ShaderTranslatorTest : public testing::Test {
 public:
  ES3ShaderTranslatorTest() = default;
  ~ES3ShaderTranslatorTest() override = default;

 protected:
  void SetUp() override {
#if BUILDFLAG(IS_WIN) && defined(ARCH_CPU_ARM64)
    GTEST_SKIP() << "Angle doesn't support OpenGL on Windows";
#else
    ShBuiltInResources resources;
    sh::InitBuiltInResources(&resources);
    resources.MaxExpressionComplexity = 32;
    resources.MaxCallStackDepth = 32;

    vertex_translator_ = new ShaderTranslator();
    fragment_translator_ = new ShaderTranslator();

    ASSERT_TRUE(vertex_translator_->Init(GL_VERTEX_SHADER, SH_GLES3_SPEC,
                                         &resources, SH_ESSL_OUTPUT, {},
                                         false));
    ASSERT_TRUE(fragment_translator_->Init(GL_FRAGMENT_SHADER, SH_GLES3_SPEC,
                                           &resources, SH_ESSL_OUTPUT, {},
                                           false));
#endif  //  BUILDFLAG(IS_WIN) && defined(ARCH_CPU_ARM64)
  }
  void TearDown() override {
    vertex_translator_ = nullptr;
    fragment_translator_ = nullptr;
  }

  scoped_refptr<ShaderTranslator> vertex_translator_;
  scoped_refptr<ShaderTranslator> fragment_translator_;
};

TEST_F(ShaderTranslatorTest, ValidVertexShader) {
  const char* shader =
      "void main() {\n"
      "  gl_Position = vec4(1.0);\n"
      "}";

  // A valid shader should be successfully translated.
  std::string info_log, translated_source;
  int shader_version;
  AttributeMap attrib_map;
  UniformMap uniform_map;
  VaryingMap varying_map;
  InterfaceBlockMap interface_block_map;
  OutputVariableList output_variable_list;
  EXPECT_TRUE(vertex_translator_->Translate(
      shader, &info_log, &translated_source, &shader_version, &attrib_map,
      &uniform_map, &varying_map, &interface_block_map, &output_variable_list));

  // Info log must be NULL.
  EXPECT_TRUE(info_log.empty());
  // Translated shader must be valid and non-empty.
  ASSERT_FALSE(translated_source.empty());
  // There should be no attributes, uniforms, and only one built-in
  // varying: gl_Position.
  EXPECT_TRUE(attrib_map.empty());
  EXPECT_TRUE(uniform_map.empty());
  EXPECT_TRUE(interface_block_map.empty());
  EXPECT_EQ(1u, varying_map.size());
  EXPECT_TRUE(output_variable_list.empty());
}

TEST_F(ShaderTranslatorTest, InvalidVertexShader) {
  const char* bad_shader = "foo-bar";
  const char* good_shader =
      "void main() {\n"
      "  gl_Position = vec4(1.0);\n"
      "}";

  // An invalid shader should fail.
  std::string info_log, translated_source;
  int shader_version;
  AttributeMap attrib_map;
  UniformMap uniform_map;
  VaryingMap varying_map;
  InterfaceBlockMap interface_block_map;
  OutputVariableList output_variable_list;
  EXPECT_FALSE(vertex_translator_->Translate(
      bad_shader, &info_log, &translated_source, &shader_version, &attrib_map,
      &uniform_map, &varying_map, &interface_block_map, &output_variable_list));
  // Info log must be valid and non-empty.
  ASSERT_FALSE(info_log.empty());
  // Translated shader must be NULL.
  EXPECT_TRUE(translated_source.empty());
  // There should be no attributes, uniforms, varyings, interface block or
  // name mapping.
  EXPECT_TRUE(attrib_map.empty());
  EXPECT_TRUE(uniform_map.empty());
  EXPECT_TRUE(varying_map.empty());
  EXPECT_TRUE(interface_block_map.empty());
  EXPECT_TRUE(output_variable_list.empty());

  // Try a good shader after bad.
  info_log.clear();
  EXPECT_TRUE(vertex_translator_->Translate(
      good_shader, &info_log, &translated_source, &shader_version, &attrib_map,
      &uniform_map, &varying_map, &interface_block_map, &output_variable_list));
  EXPECT_TRUE(info_log.empty());
  EXPECT_FALSE(translated_source.empty());
  EXPECT_TRUE(interface_block_map.empty());
}

TEST_F(ShaderTranslatorTest, ValidFragmentShader) {
  const char* shader =
      "void main() {\n"
      "  gl_FragColor = vec4(1.0);\n"
      "}";

  // A valid shader should be successfully translated.
  std::string info_log, translated_source;
  int shader_version;
  AttributeMap attrib_map;
  UniformMap uniform_map;
  VaryingMap varying_map;
  InterfaceBlockMap interface_block_map;
  OutputVariableList output_variable_list;
  EXPECT_TRUE(fragment_translator_->Translate(
      shader, &info_log, &translated_source, &shader_version, &attrib_map,
      &uniform_map, &varying_map, &interface_block_map, &output_variable_list));
  // Info log must be NULL.
  EXPECT_TRUE(info_log.empty());
  // Translated shader must be valid and non-empty.
  ASSERT_FALSE(translated_source.empty());
  // There should be no attributes, uniforms, varyings, interface block or
  // name mapping.
  EXPECT_TRUE(attrib_map.empty());
  EXPECT_TRUE(uniform_map.empty());
  EXPECT_TRUE(varying_map.empty());
  EXPECT_TRUE(interface_block_map.empty());
  // gl_FragColor.
  EXPECT_EQ(1u, output_variable_list.size());
}

TEST_F(ShaderTranslatorTest, InvalidFragmentShader) {
  const char* shader = "foo-bar";

  std::string info_log, translated_source;
  int shader_version;
  AttributeMap attrib_map;
  UniformMap uniform_map;
  VaryingMap varying_map;
  InterfaceBlockMap interface_block_map;
  OutputVariableList output_variable_list;
  // An invalid shader should fail.
  EXPECT_FALSE(fragment_translator_->Translate(
      shader, &info_log, &translated_source, &shader_version, &attrib_map,
      &uniform_map, &varying_map, &interface_block_map, &output_variable_list));
  // Info log must be valid and non-empty.
  EXPECT_FALSE(info_log.empty());
  // Translated shader must be NULL.
  EXPECT_TRUE(translated_source.empty());
  // There should be no attributes, uniforms, varyings, interface block or
  // name mapping.
  EXPECT_TRUE(attrib_map.empty());
  EXPECT_TRUE(uniform_map.empty());
  EXPECT_TRUE(varying_map.empty());
  EXPECT_TRUE(output_variable_list.empty());
}

TEST_F(ShaderTranslatorTest, GetAttributes) {
  const char* shader =
      "attribute vec4 vPosition;\n"
      "void main() {\n"
      "  gl_Position = vPosition;\n"
      "}";

  std::string info_log, translated_source;
  int shader_version;
  AttributeMap attrib_map;
  UniformMap uniform_map;
  VaryingMap varying_map;
  InterfaceBlockMap interface_block_map;
  OutputVariableList output_variable_list;
  EXPECT_TRUE(vertex_translator_->Translate(
      shader, &info_log, &translated_source, &shader_version, &attrib_map,
      &uniform_map, &varying_map, &interface_block_map, &output_variable_list));
  // Info log must be NULL.
  EXPECT_TRUE(info_log.empty());
  // Translated shader must be valid and non-empty.
  EXPECT_FALSE(translated_source.empty());
  // There should be no uniforms.
  EXPECT_TRUE(uniform_map.empty());
  // There should be no interface blocks.
  EXPECT_TRUE(interface_block_map.empty());
  // There should be one attribute with following characteristics:
  // name:vPosition type:GL_FLOAT_VEC4 size:0.
  EXPECT_EQ(1u, attrib_map.size());
  // The shader translator adds a "_u" prefix to user-defined names.
  AttributeMap::const_iterator iter = attrib_map.find("_uvPosition");
  EXPECT_TRUE(iter != attrib_map.end());
  EXPECT_EQ(static_cast<GLenum>(GL_FLOAT_VEC4), iter->second.type);
  EXPECT_EQ(0u, iter->second.getOutermostArraySize());
  EXPECT_EQ("vPosition", iter->second.name);
}

TEST_F(ShaderTranslatorTest, GetUniforms) {
  const char* shader =
      "precision mediump float;\n"
      "struct Foo {\n"
      "  vec4 color[1];\n"
      "};\n"
      "struct Bar {\n"
      "  Foo foo;\n"
      "};\n"
      "uniform Bar bar[2];\n"
      "void main() {\n"
      "  gl_FragColor = bar[0].foo.color[0] + bar[1].foo.color[0];\n"
      "}";

  std::string info_log, translated_source;
  int shader_version;
  AttributeMap attrib_map;
  UniformMap uniform_map;
  VaryingMap varying_map;
  InterfaceBlockMap interface_block_map;
  OutputVariableList output_variable_list;
  EXPECT_TRUE(fragment_translator_->Translate(
      shader, &info_log, &translated_source, &shader_version, &attrib_map,
      &uniform_map, &varying_map, &interface_block_map, &output_variable_list));
  // Info log must be NULL.
  EXPECT_TRUE(info_log.empty());
  // Translated shader must be valid and non-empty.
  EXPECT_FALSE(translated_source.empty());
  // There should be no attributes.
  EXPECT_TRUE(attrib_map.empty());
  // There should be no interface blocks.
  EXPECT_TRUE(interface_block_map.empty());
  // There should be two uniforms with following characteristics:
  // 1. name:bar[0].foo.color[0] type:GL_FLOAT_VEC4 size:1
  // 2. name:bar[1].foo.color[0] type:GL_FLOAT_VEC4 size:1
  // However, there will be only one entry "bar" in the map.
  EXPECT_EQ(1u, uniform_map.size());
  // The shader translator adds a "_u" prefix to user-defined names.
  UniformMap::const_iterator iter = uniform_map.find("_ubar");
  EXPECT_TRUE(iter != uniform_map.end());
  // First uniform.
  const sh::ShaderVariable* info;
  std::string original_name;
  EXPECT_TRUE(iter->second.findInfoByMappedName("_ubar[0]._ufoo._ucolor[0]",
                                                &info, &original_name));
  EXPECT_EQ(static_cast<GLenum>(GL_FLOAT_VEC4), info->type);
  EXPECT_EQ(1u, info->getOutermostArraySize());
  EXPECT_STREQ("color", info->name.c_str());
  EXPECT_STREQ("bar[0].foo.color[0]", original_name.c_str());
  // Second uniform.
  EXPECT_TRUE(iter->second.findInfoByMappedName("_ubar[1]._ufoo._ucolor[0]",
                                                &info, &original_name));
  EXPECT_EQ(static_cast<GLenum>(GL_FLOAT_VEC4), info->type);
  EXPECT_EQ(1u, info->getOutermostArraySize());
  EXPECT_STREQ("color", info->name.c_str());
  EXPECT_STREQ("bar[1].foo.color[0]", original_name.c_str());
  EXPECT_EQ(1u, output_variable_list.size());
  ASSERT_TRUE(output_variable_list.size() > 0);
  EXPECT_EQ(output_variable_list[0].mappedName, "gl_FragColor");
}


TEST_F(ES3ShaderTranslatorTest, InvalidInterfaceBlocks) {
  const char* shader =
      "#version 300 es\n"
      "precision mediump float;\n"
      "layout(location=0) out vec4 oColor;\n"
      "uniform Color {\n"
      "  float red;\n"
      "  float green;\n"
      "  float blue;\n"
      "};\n"
      "uniform Color2 {\n"
      "  float R;\n"
      "  float green;\n"
      "  float B;\n"
      "};\n"
      "void main() {\n"
      "  oColor = vec4(red * R, green * green, blue * B, 1.0);\n"
      "}";

  std::string info_log, translated_source;
  int shader_version;
  AttributeMap attrib_map;
  UniformMap uniform_map;
  VaryingMap varying_map;
  InterfaceBlockMap interface_block_map;
  OutputVariableList output_variable_list;
  EXPECT_FALSE(fragment_translator_->Translate(
      shader, &info_log, &translated_source, &shader_version, &attrib_map,
      &uniform_map, &varying_map, &interface_block_map, &output_variable_list));
  // Info log must be valid and non-empty.
  ASSERT_FALSE(info_log.empty());
  // Translated shader must be NULL.
  EXPECT_TRUE(translated_source.empty());
  // There should be no attributes, uniforms, varyings, interface block or
  // name mapping.
  EXPECT_TRUE(attrib_map.empty());
  EXPECT_TRUE(uniform_map.empty());
  EXPECT_TRUE(varying_map.empty());
  EXPECT_TRUE(interface_block_map.empty());
}

TEST_F(ES3ShaderTranslatorTest, GetInterfaceBlocks) {
  const char* shader =
      "#version 300 es\n"
      "precision mediump float;\n"
      "layout(location=0) out vec4 oColor;\n"
      "uniform Color {\n"
      "  float red;\n"
      "  float green;\n"
      "  float blue;\n"
      "};\n"
      "void main() {\n"
      "  oColor = vec4(red, green, blue, 1.0);\n"
      "}";

  std::string info_log, translated_source;
  int shader_version;
  AttributeMap attrib_map;
  UniformMap uniform_map;
  VaryingMap varying_map;
  InterfaceBlockMap interface_block_map;
  OutputVariableList output_variable_list;
  EXPECT_TRUE(fragment_translator_->Translate(
      shader, &info_log, &translated_source, &shader_version, &attrib_map,
      &uniform_map, &varying_map, &interface_block_map, &output_variable_list));
  // Info log must be NULL.
  EXPECT_TRUE(info_log.empty());
  // Translated shader must be valid and non-empty.
  EXPECT_FALSE(translated_source.empty());
  // There should be no attributes.
  EXPECT_TRUE(attrib_map.empty());
  // There should be one block in interface_block_map
  EXPECT_EQ(1u, interface_block_map.size());
  InterfaceBlockMap::const_iterator iter;
  for (iter = interface_block_map.begin();
       iter != interface_block_map.end(); ++iter) {
    if (iter->second.name == "Color")
      break;
  }
  EXPECT_TRUE(iter != interface_block_map.end());
}

TEST_F(ShaderTranslatorTest, OptionsString) {
  scoped_refptr<ShaderTranslator> translator_1 = new ShaderTranslator();
  scoped_refptr<ShaderTranslator> translator_2 = new ShaderTranslator();
  scoped_refptr<ShaderTranslator> translator_3 = new ShaderTranslator();

  ShBuiltInResources resources;
  sh::InitBuiltInResources(&resources);

  ShCompileOptions with_init_output_variables{};
  with_init_output_variables.initOutputVariables = true;

  ASSERT_TRUE(translator_1->Init(GL_VERTEX_SHADER, SH_GLES2_SPEC, &resources,
                                 SH_GLSL_150_CORE_OUTPUT, {}, false));
  ASSERT_TRUE(translator_2->Init(GL_FRAGMENT_SHADER, SH_GLES2_SPEC, &resources,
                                 SH_GLSL_150_CORE_OUTPUT,
                                 with_init_output_variables, false));
  resources.EXT_draw_buffers = 1;
  ASSERT_TRUE(translator_3->Init(GL_VERTEX_SHADER, SH_GLES2_SPEC, &resources,
                                 SH_GLSL_150_CORE_OUTPUT, {}, false));

  std::string options_1(
      translator_1->GetStringForOptionsThatWouldAffectCompilation()->data);
  std::string options_2(
      translator_1->GetStringForOptionsThatWouldAffectCompilation()->data);
  std::string options_3(
      translator_2->GetStringForOptionsThatWouldAffectCompilation()->data);
  std::string options_4(
      translator_3->GetStringForOptionsThatWouldAffectCompilation()->data);

  EXPECT_EQ(options_1, options_2);
  EXPECT_NE(options_1, options_3);
  EXPECT_NE(options_1, options_4);
  EXPECT_NE(options_3, options_4);
}

class ShaderTranslatorOutputVersionTest
    : public testing::TestWithParam<testing::tuple<const char*, const char*>> {
 public:
#if BUILDFLAG(IS_WIN) && defined(ARCH_CPU_ARM64)
  void SetUp() override {
    GTEST_SKIP() << "Angle doesn't support OpenGL on Windows";
  }
#endif
};

// crbug.com/540543
// https://bugs.chromium.org/p/angleproject/issues/detail?id=1276
// https://bugs.chromium.org/p/angleproject/issues/detail?id=1277
TEST_F(ShaderTranslatorOutputVersionTest, DISABLED_CompatibilityOutput) {
  ShBuiltInResources resources;
  sh::InitBuiltInResources(&resources);

  ShCompileOptions compile_options{};
  compile_options.objectCode = true;

  ShShaderOutput shader_output_language = SH_GLSL_COMPATIBILITY_OUTPUT;
  scoped_refptr<ShaderTranslator> vertex_translator = new ShaderTranslator();
  ASSERT_TRUE(vertex_translator->Init(GL_VERTEX_SHADER, SH_GLES2_SPEC,
                                      &resources, shader_output_language,
                                      compile_options,
                                      false));
  scoped_refptr<ShaderTranslator> fragment_translator = new ShaderTranslator();
  ASSERT_TRUE(fragment_translator->Init(GL_FRAGMENT_SHADER, SH_GLES2_SPEC,
                                        &resources, shader_output_language,
                                        compile_options,
                                        false));

  std::string translated_source;
  int shader_version;
  {
    const char* kShader =
        "attribute vec4 vPosition;\n"
        "void main() {\n"
        "}";

    EXPECT_TRUE(vertex_translator->Translate(
        kShader, nullptr, &translated_source, &shader_version, nullptr, nullptr,
        nullptr, nullptr, nullptr));
    EXPECT_TRUE(translated_source.find("#version") == std::string::npos);
    if (!base::Contains(translated_source, "gl_Position =")) {
      ADD_FAILURE() << "Did not find gl_Position initialization.";
      LOG(ERROR) << "Generated output:\n" << translated_source;
    }
  }
  {
    const char* kShader =
        "#pragma STDGL invariant(all)\n"
        "precision mediump float;\n"
        "varying vec4 v_varying;\n"
        "void main() {\n"
        "    gl_FragColor = v_varying;\n"
        "}\n";

    EXPECT_TRUE(fragment_translator->Translate(
        kShader, nullptr, &translated_source, &shader_version, nullptr, nullptr,
        nullptr, nullptr, nullptr));
    EXPECT_TRUE(base::Contains(translated_source, "#version 120"));
    if (base::Contains(translated_source, "#pragma STDGL invariant(all)")) {
      ADD_FAILURE() << "Found forbidden pragma.";
      LOG(ERROR) << "Generated output:\n" << translated_source;
    }
  }
}

TEST_P(ShaderTranslatorOutputVersionTest, HasCorrectOutputGLSLVersion) {
  // Test that translating to a shader targeting certain OpenGL context version
  // (version string in test param tuple index 0) produces a GLSL shader that
  // contains correct version string for that context (version directive
  // in test param tuple index 1).

  const char* kShader =
      "attribute vec4 vPosition;\n"
      "void main() {\n"
      "  gl_Position = vPosition;\n"
      "}";

  scoped_refptr<ShaderTranslator> translator = new ShaderTranslator();
  ShBuiltInResources resources;
  sh::InitBuiltInResources(&resources);

  ShCompileOptions compile_options{};
  compile_options.objectCode = true;

  ASSERT_TRUE(translator->Init(GL_VERTEX_SHADER, SH_GLES2_SPEC, &resources,
                               SH_ESSL_OUTPUT, compile_options, false));

  std::string translated_source;
  int shader_version;
  EXPECT_TRUE(translator->Translate(kShader, nullptr, &translated_source,
                                    &shader_version, nullptr, nullptr, nullptr,
                                    nullptr, nullptr));

  std::string expected_version_directive = testing::get<1>(GetParam());
  if (expected_version_directive.empty()) {
    EXPECT_TRUE(!base::Contains(translated_source, "#version"))
        << "Translation was:\n"
        << translated_source;
  } else {
    EXPECT_TRUE(base::Contains(translated_source, expected_version_directive))
        << "Translation was:\n"
        << translated_source;
  }
}

// For some compilers, using make_tuple("a", "bb") would end up
// instantiating make_tuple<char[1], char[2]>. This does not work.
namespace {
testing::tuple<const char*, const char*> make_gl_glsl_tuple(
    const char* gl_version,
    const char* glsl_version_directive) {
  return testing::make_tuple(gl_version, glsl_version_directive);
}
}

// Test data for the above test. Check that for the OpenGL ES output
// contexts, the shader is such that GLSL 1.0 is used. The translator
// selects GLSL 1.0 by not output any version at the moment, though we
// do not know if that would be correct for the future OpenGL ES specs.
INSTANTIATE_TEST_SUITE_P(
    OpenGLESContexts,
    ShaderTranslatorOutputVersionTest,
    testing::Values(make_gl_glsl_tuple("opengl es 2.0", ""),
                    make_gl_glsl_tuple("opengl es 3.0", ""),
                    make_gl_glsl_tuple("opengl es 3.1", ""),
                    make_gl_glsl_tuple("opengl es 3.2", "")));

}  // namespace gles2
}  // namespace gpu