Welcome to mirror list, hosted at ThFree Co, Russian Federation.

compile_shaders_test.cpp « drape_frontend_tests « drape_frontend - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fd0a246338f27792805ce618b9f35b4da0e47c11 (plain)
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
#include "testing/testing.hpp"

#include "drape_frontend/drape_frontend_tests/shader_def_for_tests.hpp"

#include "platform/platform.hpp"

#include "coding/file_name_utils.hpp"

#include <functional>
#include <sstream>
#include <string>
#include <vector>

#include <QTemporaryFile>
#include <QtCore/QDebug>
#include <QtCore/QProcess>
#include <QtCore/QTextStream>

#if defined(OMIM_OS_MAC)

std::string const kCompilersDir = "shaders_compiler";
std::string const kMaliCompilerOpenGLES2Dir = "mali_compiler";
std::string const kMaliCompilerOpenGLES3Dir = "mali_compiler_es3";
std::string const kCompilerOpenGLES2 = "GLSLESCompiler_Series5.mac";
std::string const kCompilerMaliOpenGLES2 = kMaliCompilerOpenGLES2Dir + "/malisc";
std::string const kCompilerOpenGLES3 = "GLSLESCompiler_Series6.mac";
std::string const kCompilerMaliOpenGLES3 = kMaliCompilerOpenGLES3Dir + "/malisc";

std::string DebugPrint(QString const & s) { return s.toStdString(); }
std::string DebugPrint(dp::ApiVersion apiVersion)
{
  if (apiVersion == dp::OpenGLES2)
    return "OpenGLES2";
  else if (apiVersion == dp::OpenGLES3)
    return "OpenGLES3";
  return "Unknown";
}

namespace
{
void WriteShaderToFile(QTemporaryFile & file, std::string const & shader)
{
  QTextStream out(&file);
  out << QString::fromStdString(shader);
}

using PrepareProcessFn = std::function<void(QProcess & p)>;
using PrepareArgumentsFn = std::function<void(QStringList & args, QString const & fileName)>;
using SuccessChecker = std::function<bool(QString const & output)>;

void RunShaderTest(dp::ApiVersion apiVersion, std::string const & shaderName,
                   QString const & glslCompiler, QString const & fileName,
                   PrepareProcessFn const & procPrepare, PrepareArgumentsFn const & argsPrepare,
                   SuccessChecker const & successChecker, QTextStream & errorLog)
{
  QProcess p;
  procPrepare(p);
  p.setProcessChannelMode(QProcess::MergedChannels);
  QStringList args;
  argsPrepare(args, fileName);
  p.start(glslCompiler, args, QIODevice::ReadOnly);

  TEST(p.waitForStarted(), ("GLSL compiler not started", glslCompiler));
  TEST(p.waitForFinished(), ("GLSL compiler not finished in time", glslCompiler));

  QString result = p.readAllStandardOutput();
  if (!successChecker(result))
  {
    errorLog << "\n"
             << QString(DebugPrint(apiVersion).c_str()) << ": " << QString(shaderName.c_str())
             << QString(": SHADER COMPILE ERROR:\n");
    errorLog << result.trimmed() << "\n";
  }
}

void TestShaders(dp::ApiVersion apiVersion, std::string const & defines,
                 gpu::ShadersEnum const & shaders, QString const & glslCompiler,
                 PrepareProcessFn const & procPrepare, PrepareArgumentsFn const & argsPrepare,
                 SuccessChecker const & successChecker, QTextStream & errorLog)
{
  for (auto const & src : shaders)
  {
    QTemporaryFile srcFile;
    TEST(srcFile.open(), ("Temporary file can't be created!"));
    std::string fullSrc;
    if (src.second.find("#version") != std::string::npos)
    {
      auto pos = src.second.find('\n');
      ASSERT_NOT_EQUAL(pos, std::string::npos, ());
      fullSrc = src.second;
      fullSrc.insert(pos + 1, defines);
    }
    else
    {
      fullSrc = defines + src.second;
    }
    WriteShaderToFile(srcFile, fullSrc);
    RunShaderTest(apiVersion, src.first, glslCompiler, srcFile.fileName(), procPrepare, argsPrepare,
                  successChecker, errorLog);
  }
}

std::string GetCompilerPath(std::string const & compilerName)
{
  Platform & platform = GetPlatform();
  std::string compilerPath =
      my::JoinFoldersToPath({platform.ResourcesDir(), kCompilersDir}, compilerName);
  if (!platform.IsFileExistsByFullPath(compilerPath))
  {
    compilerPath = my::JoinFoldersToPath({platform.WritableDir(), kCompilersDir}, compilerName);
    TEST(platform.IsFileExistsByFullPath(compilerPath), ("GLSL compiler not found"));
  }
  return compilerPath;
}
}  // namespace

UNIT_TEST(CompileShaders_Test)
{
  struct CompilerData
  {
    dp::ApiVersion m_apiVersion;
    std::string m_compilerPath;
  };
  std::vector<CompilerData> const compilers = {
      {dp::ApiVersion::OpenGLES2, GetCompilerPath(kCompilerOpenGLES2)},
      {dp::ApiVersion::OpenGLES3, GetCompilerPath(kCompilerOpenGLES3)},
  };

  auto successChecker = [](QString const & output) { return output.indexOf("Success") != -1; };

  for (auto const & compiler : compilers)
  {
    QString errorLog;
    QTextStream ss(&errorLog);

    QString compilerPath = QString::fromStdString(compiler.m_compilerPath);
    QString shaderType = "-v";
    auto argsPrepareFn = [&shaderType](QStringList & args, QString const & fileName) {
      args << fileName << fileName + ".bin" << shaderType;
    };

    string defines = "";
    TestShaders(compiler.m_apiVersion, defines, gpu::GetVertexShaders(compiler.m_apiVersion),
                compilerPath, [](QProcess const &) {}, argsPrepareFn, successChecker, ss);
    shaderType = "-f";
    TestShaders(compiler.m_apiVersion, defines, gpu::GetFragmentShaders(compiler.m_apiVersion),
                compilerPath, [](QProcess const &) {}, argsPrepareFn, successChecker, ss);

    TEST_EQUAL(errorLog.isEmpty(), true, ("PVR without defines :", errorLog));

    defines = "#define ENABLE_VTF\n";
    errorLog.clear();
    shaderType = "-v";
    TestShaders(compiler.m_apiVersion, defines, gpu::GetVertexShaders(compiler.m_apiVersion),
                compilerPath, [](QProcess const &) {}, argsPrepareFn, successChecker, ss);
    shaderType = "-f";
    TestShaders(compiler.m_apiVersion, defines, gpu::GetFragmentShaders(compiler.m_apiVersion),
                compilerPath, [](QProcess const &) {}, argsPrepareFn, successChecker, ss);

    TEST_EQUAL(errorLog.isEmpty(), true, ("PVR with defines : ", defines, "\n", errorLog));

    defines = "#define SAMSUNG_GOOGLE_NEXUS\n";
    errorLog.clear();
    shaderType = "-v";
    TestShaders(compiler.m_apiVersion, defines, gpu::GetVertexShaders(compiler.m_apiVersion),
                compilerPath, [](QProcess const &) {}, argsPrepareFn, successChecker, ss);
    shaderType = "-f";
    TestShaders(compiler.m_apiVersion, defines, gpu::GetFragmentShaders(compiler.m_apiVersion),
                compilerPath, [](QProcess const &) {}, argsPrepareFn, successChecker, ss);

    TEST_EQUAL(errorLog.isEmpty(), true, ("PVR with defines : ", defines, "\n", errorLog));
  }
}

UNIT_TEST(MALI_CompileShaders_Test)
{
  using ReleaseVersion = std::pair<QString, QString>;
  using Releases = std::vector<ReleaseVersion>;

  struct DriverSet
  {
    QString m_driverName;
    Releases m_releases;
  };

  struct CompilerData
  {
    dp::ApiVersion m_apiVersion;
    std::string m_compilerPath;
    std::string m_compilerAdditionalPath;
    std::vector<DriverSet> m_driverSets;
  };

  std::vector<CompilerData> const compilers = {
      {dp::ApiVersion::OpenGLES2,
       GetCompilerPath(kCompilerMaliOpenGLES2),
       GetCompilerPath(kMaliCompilerOpenGLES2Dir),
       {{"Mali-400_r4p0-00rel1",
         {std::make_pair("Mali-200", "r0p1"), std::make_pair("Mali-200", "r0p2"),
          std::make_pair("Mali-200", "r0p3"), std::make_pair("Mali-200", "r0p4"),
          std::make_pair("Mali-200", "r0p5"), std::make_pair("Mali-200", "r0p6"),
          std::make_pair("Mali-400", "r0p0"), std::make_pair("Mali-400", "r0p1"),
          std::make_pair("Mali-400", "r1p0"), std::make_pair("Mali-400", "r1p1"),
          std::make_pair("Mali-300", "r0p0"), std::make_pair("Mali-450", "r0p0")}},
        {"Mali-T600_r4p0-00rel0",
         {std::make_pair("Mali-T600", "r0p0"), std::make_pair("Mali-T600", "r0p0_15dev0"),
          std::make_pair("Mali-T600", "r0p1"), std::make_pair("Mali-T620", "r0p1"),
          std::make_pair("Mali-T620", "r1p0"), std::make_pair("Mali-T670", "r1p0")}},
        {"Mali-T600_r4p1-00rel0",
         {std::make_pair("Mali-T600", "r0p0"), std::make_pair("Mali-T600", "r0p0_15dev0"),
          std::make_pair("Mali-T600", "r0p1"), std::make_pair("Mali-T620", "r0p1"),
          std::make_pair("Mali-T620", "r1p0"), std::make_pair("Mali-T620", "r1p1"),
          std::make_pair("Mali-T720", "r0p0"), std::make_pair("Mali-T720", "r1p0"),
          std::make_pair("Mali-T760", "r0p0"), std::make_pair("Mali-T760", "r0p1"),
          std::make_pair("Mali-T760", "r0p1_50rel0"), std::make_pair("Mali-T760", "r0p2"),
          std::make_pair("Mali-T760", "r0p3"), std::make_pair("Mali-T760", "r1p0")}}}},
      {dp::ApiVersion::OpenGLES3,
       GetCompilerPath(kCompilerMaliOpenGLES3),
       GetCompilerPath(kMaliCompilerOpenGLES3Dir),
       {{"Mali-T600_r5p0-00rel0",
         {std::make_pair("Mali-T600", "r0p0"), std::make_pair("Mali-T600", "r0p0_15dev0"),
          std::make_pair("Mali-T600", "r0p1"), std::make_pair("Mali-T620", "r0p1"),
          std::make_pair("Mali-T620", "r1p0"), std::make_pair("Mali-T620", "r1p1"),
          std::make_pair("Mali-T760", "r0p0"), std::make_pair("Mali-T760", "r0p1"),
          std::make_pair("Mali-T760", "r0p1_50rel0"), std::make_pair("Mali-T760", "r0p2"),
          std::make_pair("Mali-T760", "r0p3"), std::make_pair("Mali-T760", "r1p0")}},
        {"Mali-T600_r5p1-00rel0",
         {std::make_pair("Mali-T600", "r0p0"), std::make_pair("Mali-T600", "r0p0_15dev0"),
          std::make_pair("Mali-T600", "r0p1"), std::make_pair("Mali-T620", "r0p1"),
          std::make_pair("Mali-T620", "r1p0"), std::make_pair("Mali-T620", "r1p1"),
          std::make_pair("Mali-T760", "r0p0"), std::make_pair("Mali-T760", "r0p1"),
          std::make_pair("Mali-T760", "r0p1_50rel0"), std::make_pair("Mali-T760", "r0p2"),
          std::make_pair("Mali-T760", "r0p3"), std::make_pair("Mali-T760", "r1p0")}},
        {"Mali-T600_r6p0-00rel0",
         {std::make_pair("Mali-T600", "r0p0"), std::make_pair("Mali-T600", "r0p0_15dev0"),
          std::make_pair("Mali-T600", "r0p1"), std::make_pair("Mali-T620", "r0p1"),
          std::make_pair("Mali-T620", "r1p0"), std::make_pair("Mali-T620", "r1p1"),
          std::make_pair("Mali-T760", "r0p0"), std::make_pair("Mali-T760", "r0p1"),
          std::make_pair("Mali-T760", "r0p1_50rel0"), std::make_pair("Mali-T760", "r0p2"),
          std::make_pair("Mali-T760", "r0p3"), std::make_pair("Mali-T760", "r1p0")}}}},
  };

  auto successChecker = [](QString const & output) {
    return output.indexOf("Compilation succeeded.") != -1;
  };

  for (auto const & compiler : compilers)
  {
    for (auto const & set : compiler.m_driverSets)
    {
      for (auto const & version : set.m_releases)
      {
        QString errorLog;
        QTextStream ss(&errorLog);

        QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
        env.insert("MALICM_LOCATION", QString::fromStdString(compiler.m_compilerAdditionalPath));
        auto procPrepare = [&env](QProcess & p) { p.setProcessEnvironment(env); };
        QString shaderType = "-v";
        auto argForming = [&](QStringList & args, QString const & fileName) {
          args << shaderType << "-V"
               << "-r" << version.second << "-c" << version.first << "-d" << set.m_driverName
               << fileName;
        };
        std::string defines = "";
        QString const compilerPath = QString::fromStdString(compiler.m_compilerPath);
        TestShaders(compiler.m_apiVersion, defines, gpu::GetVertexShaders(compiler.m_apiVersion),
                    compilerPath, procPrepare, argForming, successChecker, ss);
        shaderType = "-f";
        TestShaders(compiler.m_apiVersion, defines, gpu::GetFragmentShaders(compiler.m_apiVersion),
                    compilerPath, procPrepare, argForming, successChecker, ss);
        TEST(errorLog.isEmpty(),
             (shaderType, version.second, version.first, set.m_driverName, defines, errorLog));

        // MALI GPUs do not support ENABLE_VTF. Do not test it here.
        // SAMSUNG_GOOGLE_NEXUS doesn't use Mali GPU. Do not test it here.
      }
    }
  }
}
#endif