aboutsummaryrefslogtreecommitdiff
path: root/libshaderc/src/shaderc_cpp_test.cc
blob: eafbd06967b2660e26480f7c86ab1f4610026161 (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
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
// Copyright 2015 The Shaderc Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "shaderc.hpp"

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <memory>
#include <thread>

#include "SPIRV/spirv.hpp"

namespace {

using testing::Each;
using testing::HasSubstr;

const char kMinimalShader[] = "void main(){}";
const std::string kMinimalShaderWithMacro =
    "#define E main\n"
    "void E(){}\n";

TEST(CppInterface, MultipleCalls) {
  shaderc::Compiler compiler1, compiler2, compiler3;
  EXPECT_TRUE(compiler1.IsValid());
  EXPECT_TRUE(compiler2.IsValid());
  EXPECT_TRUE(compiler3.IsValid());
}

TEST(CppInterface, MultipleThreadsInitializing) {
  std::unique_ptr<shaderc::Compiler> compiler1;
  std::unique_ptr<shaderc::Compiler> compiler2;
  std::unique_ptr<shaderc::Compiler> compiler3;
  std::thread t1([&compiler1]() {
    compiler1 = std::unique_ptr<shaderc::Compiler>(new shaderc::Compiler());
  });
  std::thread t2([&compiler2]() {
    compiler2 = std::unique_ptr<shaderc::Compiler>(new shaderc::Compiler());
  });
  std::thread t3([&compiler3]() {
    compiler3 = std::unique_ptr<shaderc::Compiler>(new shaderc::Compiler());
  });
  t1.join();
  t2.join();
  t3.join();
  EXPECT_TRUE(compiler1->IsValid());
  EXPECT_TRUE(compiler2->IsValid());
  EXPECT_TRUE(compiler3->IsValid());
}

// Compiles a shader and returns true on success, false on failure.
bool CompilationSuccess(const shaderc::Compiler& compiler,
                        const std::string& shader, shaderc_shader_kind kind) {
  return compiler.CompileGlslToSpv(shader.c_str(), shader.length(), kind)
      .GetSuccess();
}

// Compiles a shader and returns true if the result is valid SPIR-V.
bool CompilesToValidSpv(const shaderc::Compiler& compiler,
                        const std::string& shader, shaderc_shader_kind kind) {
  shaderc::SpvModule result = compiler.CompileGlslToSpv(shader, kind);
  if (!result.GetSuccess()) return false;
  size_t length = result.GetLength();
  if (length < 20) return false;
  const uint32_t* bytes =
      static_cast<const uint32_t*>(static_cast<const void*>(result.GetData()));
  return bytes[0] == spv::MagicNumber;
}

// Compiles a shader with the given options and returns true if the result is
// valid SPIR-V.
bool CompilesToValidSpv(const shaderc::Compiler& compiler,
                        const std::string& shader, shaderc_shader_kind kind,
                        const shaderc::CompileOptions& options) {
  shaderc::SpvModule result = compiler.CompileGlslToSpv(shader, kind, options);
  if (!result.GetSuccess()) return false;
  size_t length = result.GetLength();
  if (length < 20) return false;
  const uint32_t* bytes =
      static_cast<const uint32_t*>(static_cast<const void*>(result.GetData()));
  return bytes[0] == spv::MagicNumber;
}

TEST(CppInterface, CompilerMoves) {
  shaderc::Compiler compiler;
  ASSERT_TRUE(compiler.IsValid());
  shaderc::Compiler compiler2(std::move(compiler));
  ASSERT_FALSE(compiler.IsValid());
  ASSERT_TRUE(compiler2.IsValid());
}

TEST(CppInterface, EmptyString) {
  shaderc::Compiler compiler;
  ASSERT_TRUE(compiler.IsValid());
  EXPECT_FALSE(CompilationSuccess(compiler, "", shaderc_glsl_vertex_shader));
  EXPECT_FALSE(CompilationSuccess(compiler, "", shaderc_glsl_fragment_shader));
}

TEST(CppInterface, ModuleMoves) {
  shaderc::Compiler compiler;
  ASSERT_TRUE(compiler.IsValid());
  shaderc::SpvModule result =
      compiler.CompileGlslToSpv(kMinimalShader, shaderc_glsl_vertex_shader);
  EXPECT_TRUE(result.GetSuccess());
  shaderc::SpvModule result2(std::move(result));
  EXPECT_FALSE(result.GetSuccess());
  EXPECT_TRUE(result2.GetSuccess());
}

TEST(CppInterface, GarbageString) {
  shaderc::Compiler compiler;
  ASSERT_TRUE(compiler.IsValid());
  EXPECT_FALSE(
      CompilationSuccess(compiler, "jfalkds", shaderc_glsl_vertex_shader));
  EXPECT_FALSE(
      CompilationSuccess(compiler, "jfalkds", shaderc_glsl_fragment_shader));
}

TEST(CppInterface, MinimalShader) {
  shaderc::Compiler compiler;
  ASSERT_TRUE(compiler.IsValid());
  EXPECT_TRUE(
      CompilesToValidSpv(compiler, kMinimalShader, shaderc_glsl_vertex_shader));
  EXPECT_TRUE(CompilesToValidSpv(compiler, kMinimalShader,
                                 shaderc_glsl_fragment_shader));
}

TEST(CppInterface, BasicOptions) {
  shaderc::Compiler compiler;
  shaderc::CompileOptions options;
  ASSERT_TRUE(compiler.IsValid());
  EXPECT_TRUE(CompilesToValidSpv(compiler, kMinimalShader,
                                 shaderc_glsl_vertex_shader, options));
  EXPECT_TRUE(CompilesToValidSpv(compiler, kMinimalShader,
                                 shaderc_glsl_fragment_shader, options));
}

TEST(CppInterface, CopiedOptions) {
  shaderc::Compiler compiler;
  shaderc::CompileOptions options;
  ASSERT_TRUE(compiler.IsValid());
  EXPECT_TRUE(CompilesToValidSpv(compiler, kMinimalShader,
                                 shaderc_glsl_vertex_shader, options));
  shaderc::CompileOptions copied_options(options);
  EXPECT_TRUE(CompilesToValidSpv(compiler, kMinimalShader,
                                 shaderc_glsl_fragment_shader, copied_options));
}

TEST(CppInterface, MovedOptions) {
  shaderc::Compiler compiler;
  shaderc::CompileOptions options;
  ASSERT_TRUE(compiler.IsValid());
  EXPECT_TRUE(CompilesToValidSpv(compiler, kMinimalShader,
                                 shaderc_glsl_vertex_shader, options));
  shaderc::CompileOptions copied_options(std::move(options));
  EXPECT_TRUE(CompilesToValidSpv(compiler, kMinimalShader,
                                 shaderc_glsl_fragment_shader, copied_options));
}

TEST(CppInterface, StdAndCString) {
  shaderc::Compiler compiler;
  ASSERT_TRUE(compiler.IsValid());
  shaderc::SpvModule result1 = compiler.CompileGlslToSpv(
      kMinimalShader, strlen(kMinimalShader), shaderc_glsl_fragment_shader);
  shaderc::SpvModule result2 = compiler.CompileGlslToSpv(
      std::string(kMinimalShader), shaderc_glsl_fragment_shader);
  EXPECT_TRUE(result1.GetSuccess());
  EXPECT_TRUE(result2.GetSuccess());
  EXPECT_EQ(result1.GetLength(), result2.GetLength());
  EXPECT_EQ(std::vector<char>(result1.GetData(),
                              result1.GetData() + result1.GetLength()),
            std::vector<char>(result2.GetData(),
                              result2.GetData() + result2.GetLength()));
}

TEST(CppInterface, ErrorsReported) {
  shaderc::Compiler compiler;
  ASSERT_TRUE(compiler.IsValid());
  shaderc::SpvModule result = compiler.CompileGlslToSpv(
      "int f(){return wrongname;}", shaderc_glsl_vertex_shader);
  ASSERT_FALSE(result.GetSuccess());
  EXPECT_THAT(result.GetErrorMessage(), HasSubstr("wrongname"));
}

TEST(CppInterface, MultipleThreadsCalling) {
  shaderc::Compiler compiler;
  ASSERT_TRUE(compiler.IsValid());
  bool results[10];
  std::vector<std::thread> threads;
  for (auto& r : results) {
    threads.emplace_back([&compiler, &r]() {
      r = CompilationSuccess(compiler, kMinimalShader,
                             shaderc_glsl_vertex_shader);
    });
  }
  for (auto& t : threads) {
    t.join();
  }
  EXPECT_THAT(results, Each(true));
}

TEST(CppInterface, AccessorsOnNullModule) {
  shaderc::SpvModule result(nullptr);
  EXPECT_FALSE(result.GetSuccess());
  EXPECT_EQ(std::string(), result.GetErrorMessage());
  EXPECT_EQ(std::string(), result.GetData());
  EXPECT_EQ(0u, result.GetLength());
}

TEST(CppInterface, MacroCompileOptions) {
  shaderc::Compiler compiler;
  shaderc::CompileOptions options;
  options.AddMacroDefinition("E", "main");
  const std::string kMinimalExpandedShader = "void E(){}";
  const std::string kMinimalDoubleExpandedShader = "F E(){}";
  EXPECT_TRUE(compiler.CompileGlslToSpv(kMinimalExpandedShader,
                                        shaderc_glsl_vertex_shader, options)
                  .GetSuccess());

  shaderc::CompileOptions cloned_options(options);
  // The simplest should still compile with the cloned options.
  EXPECT_TRUE(compiler.CompileGlslToSpv(kMinimalExpandedShader,
                                        shaderc_glsl_vertex_shader,
                                        cloned_options)
                  .GetSuccess());

  EXPECT_FALSE(compiler.CompileGlslToSpv(kMinimalDoubleExpandedShader,
                                         shaderc_glsl_vertex_shader,
                                         cloned_options)
                   .GetSuccess());

  cloned_options.AddMacroDefinition("F", "void");
  // This should still not work with the original options.
  EXPECT_FALSE(compiler.CompileGlslToSpv(kMinimalDoubleExpandedShader,
                                         shaderc_glsl_vertex_shader, options)
                   .GetSuccess());
  // This should work with the cloned options that have the additional
  // parameter.
  EXPECT_TRUE(compiler.CompileGlslToSpv(kMinimalDoubleExpandedShader,
                                        shaderc_glsl_vertex_shader,
                                        cloned_options)
                  .GetSuccess());
}

TEST(CppInterface, DisassemblyOption) {
  shaderc::Compiler compiler;
  shaderc::CompileOptions options;
  options.SetDisassemblyMode();
  shaderc::SpvModule result = compiler.CompileGlslToSpv(
      kMinimalShader, shaderc_glsl_vertex_shader, options);
  EXPECT_TRUE(result.GetSuccess());
  // This should work with both the glslang native disassembly format and the
  // SPIR-V Tools assembly format.
  EXPECT_THAT(result.GetData(), HasSubstr("Capability Shader"));
  EXPECT_THAT(result.GetData(), HasSubstr("MemoryModel"));

  shaderc::CompileOptions cloned_options(options);
  shaderc::SpvModule result_from_cloned_options = compiler.CompileGlslToSpv(
      kMinimalShader, shaderc_glsl_vertex_shader, cloned_options);
  EXPECT_TRUE(result_from_cloned_options.GetSuccess());
  // The mode should be carried into any clone of the original option object.
  EXPECT_THAT(result_from_cloned_options.GetData(),
              HasSubstr("Capability Shader"));
  EXPECT_THAT(result_from_cloned_options.GetData(), HasSubstr("MemoryModel"));
}

TEST(CppInterface, PreprocessingOnlyOption) {
  shaderc::Compiler compiler;
  shaderc::CompileOptions options;
  options.SetPreprocessingOnlyMode();
  shaderc::SpvModule result = compiler.CompileGlslToSpv(
      kMinimalShaderWithMacro, shaderc_glsl_vertex_shader, options);
  EXPECT_TRUE(result.GetSuccess());
  EXPECT_THAT(result.GetData(), HasSubstr("void main(){ }"));

  const std::string kMinimalShaderCloneOption =
      "#define E_CLONE_OPTION main\n"
      "void E_CLONE_OPTION(){}\n";
  shaderc::CompileOptions cloned_options(options);
  shaderc::SpvModule result_from_cloned_options = compiler.CompileGlslToSpv(
      kMinimalShaderCloneOption, shaderc_glsl_vertex_shader, cloned_options);
  EXPECT_TRUE(result_from_cloned_options.GetSuccess());
  EXPECT_THAT(result_from_cloned_options.GetData(),
              HasSubstr("void main(){ }"));
}

TEST(CppInterface, PreprocessingOnlyModeFirstOverridesDisassemblyMode) {
  shaderc::Compiler compiler;
  // Sets preprocessing only mode first, then sets disassembly mode.
  // Preprocessing only mode should override disassembly mode.
  shaderc::CompileOptions options_preprocessing_mode_first;
  options_preprocessing_mode_first.SetPreprocessingOnlyMode();
  options_preprocessing_mode_first.SetDisassemblyMode();
  shaderc::SpvModule result_preprocessing_mode_first =
      compiler.CompileGlslToSpv(kMinimalShaderWithMacro,
                                shaderc_glsl_vertex_shader,
                                options_preprocessing_mode_first);
  EXPECT_TRUE(result_preprocessing_mode_first.GetSuccess());
  EXPECT_THAT(result_preprocessing_mode_first.GetData(),
              HasSubstr("void main(){ }"));
}

TEST(CppInterface, PreprocessingOnlyModeSecondOverridesDisassemblyMode) {
  shaderc::Compiler compiler;
  // Sets disassembly mode first, then preprocessing only mode.
  // Preprocessing only mode should still override disassembly mode.
  shaderc::CompileOptions options_disassembly_mode_first;
  options_disassembly_mode_first.SetDisassemblyMode();
  options_disassembly_mode_first.SetPreprocessingOnlyMode();
  shaderc::SpvModule result_disassembly_mode_first = compiler.CompileGlslToSpv(
      kMinimalShaderWithMacro, shaderc_glsl_vertex_shader,
      options_disassembly_mode_first);
  EXPECT_TRUE(result_disassembly_mode_first.GetSuccess());
  EXPECT_THAT(result_disassembly_mode_first.GetData(),
              HasSubstr("void main(){ }"));
}

TEST(CppInterface, TargetEnvCompileOptions) {
  shaderc::Compiler compiler;
  shaderc::CompileOptions options;

  // Test shader compilation which requires opengl compatibility environment
  options.SetTargetEnvironment(shaderc_target_env_opengl_compat, 0);
  const std::string kGlslShader =
    R"(#version 100
       uniform highp sampler2D tex;
       void main() {
         gl_FragColor = texture2D(tex, vec2(0.0,0.0));
       }
  )";

  EXPECT_TRUE(compiler.CompileGlslToSpv(kGlslShader,
                                        shaderc_glsl_fragment_shader,
                                        options)
                  .GetSuccess());
}

}  // anonymous namespace