aboutsummaryrefslogtreecommitdiff
path: root/libshaderc/src/shaderc_test.cc
blob: d78c7e3bc97413d700ea1cffd70899b8dfa277c0 (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
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
// 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.h"

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

#include "SPIRV/spirv.hpp"

namespace {

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

TEST(Init, MultipleCalls) {
  shaderc_compiler_t compiler1, compiler2, compiler3;
  EXPECT_NE(nullptr, compiler1 = shaderc_compiler_initialize());
  EXPECT_NE(nullptr, compiler2 = shaderc_compiler_initialize());
  EXPECT_NE(nullptr, compiler3 = shaderc_compiler_initialize());
  shaderc_compiler_release(compiler1);
  shaderc_compiler_release(compiler2);
  shaderc_compiler_release(compiler3);
}

TEST(Init, MultipleThreadsCalling) {
  shaderc_compiler_t compiler1, compiler2, compiler3;
  std::thread t1([&compiler1]() { compiler1 = shaderc_compiler_initialize(); });
  std::thread t2([&compiler2]() { compiler2 = shaderc_compiler_initialize(); });
  std::thread t3([&compiler3]() { compiler3 = shaderc_compiler_initialize(); });
  t1.join();
  t2.join();
  t3.join();
  EXPECT_NE(nullptr, compiler1);
  EXPECT_NE(nullptr, compiler2);
  EXPECT_NE(nullptr, compiler3);
  shaderc_compiler_release(compiler1);
  shaderc_compiler_release(compiler2);
  shaderc_compiler_release(compiler3);
}

TEST(Init, SPVVersion) {
  unsigned int version = 0;
  unsigned int revision = 0;
  shaderc_get_spv_version(&version, &revision);
  EXPECT_EQ(spv::Version, version);
  EXPECT_EQ(spv::Revision, revision);
}

// RAII class for shaderc_spv_module.
class Compilation {
 public:
  // Compiles shader, keeping the result.
  Compilation(const shaderc_compiler_t compiler, const std::string& shader,
              shaderc_shader_kind kind,
              const shaderc_compile_options_t options = nullptr)
      : compiled_result_(shaderc_compile_into_spv(
            compiler, shader.c_str(), shader.size(), kind, "", options)) {}

  ~Compilation() { shaderc_module_release(compiled_result_); }

  shaderc_spv_module_t result() const { return compiled_result_; }

 private:
  shaderc_spv_module_t compiled_result_;
};

struct CleanupOptions {
  void operator()(shaderc_compile_options_t options) const {
    shaderc_compile_options_release(options);
  }
};

typedef std::unique_ptr<shaderc_compile_options, CleanupOptions>
    compile_options_ptr;

// RAII class for shaderc_compiler_t
class Compiler {
 public:
  Compiler() { compiler = shaderc_compiler_initialize(); }
  ~Compiler() { shaderc_compiler_release(compiler); }
  shaderc_compiler_t get_compiler_handle() { return compiler; }

 private:
  shaderc_compiler_t compiler;
};

// Compiles a shader and returns true on success, false on failure.
bool CompilationSuccess(const shaderc_compiler_t compiler,
                        const std::string& shader, shaderc_shader_kind kind) {
  return shaderc_module_get_success(
      Compilation(compiler, shader, kind).result());
}

// Compiles a shader and returns true if the result is valid SPIR-V.
bool CompilesToValidSpv(const shaderc_compiler_t compiler,
                        const std::string& shader, shaderc_shader_kind kind,
                        const shaderc_compile_options_t options = nullptr) {
  const Compilation comp(compiler, shader, kind, options);
  auto result = comp.result();
  if (!shaderc_module_get_success(result)) return false;
  size_t length = shaderc_module_get_length(result);
  if (length < 20) return false;
  const uint32_t* bytes = static_cast<const uint32_t*>(
      static_cast<const void*>(shaderc_module_get_bytes(result)));
  return bytes[0] == spv::MagicNumber;
}

TEST(CompileString, EmptyString) {
  Compiler compiler;
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  EXPECT_FALSE(CompilationSuccess(compiler.get_compiler_handle(), "",
                                  shaderc_glsl_vertex_shader));
  EXPECT_FALSE(CompilationSuccess(compiler.get_compiler_handle(), "",
                                  shaderc_glsl_fragment_shader));
}

TEST(CompileString, GarbageString) {
  Compiler compiler;
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  EXPECT_FALSE(CompilationSuccess(compiler.get_compiler_handle(), "jfalkds",
                                  shaderc_glsl_vertex_shader));
  EXPECT_FALSE(CompilationSuccess(compiler.get_compiler_handle(), "jfalkds",
                                  shaderc_glsl_fragment_shader));
}

TEST(CompileString, ReallyLongShader) {
  Compiler compiler;
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  std::string minimal_shader = "";
  minimal_shader += "void foo(){}";
  minimal_shader.append(1024 * 1024 * 8, ' ');  // 8MB of spaces.
  minimal_shader += "void main(){}";
  EXPECT_TRUE(CompilesToValidSpv(compiler.get_compiler_handle(), minimal_shader,
                                 shaderc_glsl_vertex_shader));
  EXPECT_TRUE(CompilesToValidSpv(compiler.get_compiler_handle(), minimal_shader,
                                 shaderc_glsl_fragment_shader));
}

TEST(CompileString, MinimalShader) {
  Compiler compiler;
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  const std::string kMinimalShader = "void main(){}";
  EXPECT_TRUE(CompilesToValidSpv(compiler.get_compiler_handle(), kMinimalShader,
                                 shaderc_glsl_vertex_shader));
  EXPECT_TRUE(CompilesToValidSpv(compiler.get_compiler_handle(), kMinimalShader,
                                 shaderc_glsl_fragment_shader));
}

TEST(CompileString, WorksWithCompileOptions) {
  Compiler compiler;
  compile_options_ptr options(shaderc_compile_options_initialize());
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  const std::string kMinimalShader = "void main(){}";
  EXPECT_TRUE(CompilesToValidSpv(compiler.get_compiler_handle(), kMinimalShader,
                                 shaderc_glsl_vertex_shader, options.get()));
}

TEST(CompileStringWithOptions, CloneCompilerOptions) {
  Compiler compiler;
  compile_options_ptr options(shaderc_compile_options_initialize());
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  const std::string kMinimalShader = "void main(){}";
  EXPECT_TRUE(CompilesToValidSpv(compiler.get_compiler_handle(), kMinimalShader,
                                 shaderc_glsl_vertex_shader, options.get()));
  compile_options_ptr cloned_options(
      shaderc_compile_options_clone(options.get()));
  EXPECT_TRUE(CompilesToValidSpv(compiler.get_compiler_handle(), kMinimalShader,
                                 shaderc_glsl_vertex_shader,
                                 cloned_options.get()));
}

TEST(CompileStringWithOptions, MacroCompileOptions) {
  Compiler compiler;
  compile_options_ptr options(shaderc_compile_options_initialize());
  shaderc_compile_options_add_macro_definition(options.get(), "E", "main");
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  const std::string kMinimalExpandedShader = "void E(){}";
  const std::string kMinimalDoubleExpandedShader = "F E(){}";
  EXPECT_TRUE(CompilesToValidSpv(compiler.get_compiler_handle(),
                                 kMinimalExpandedShader,
                                 shaderc_glsl_vertex_shader, options.get()));
  compile_options_ptr cloned_options(
      shaderc_compile_options_clone(options.get()));
  // The simplest should still compile with the cloned options.
  EXPECT_TRUE(
      CompilesToValidSpv(compiler.get_compiler_handle(), kMinimalExpandedShader,
                         shaderc_glsl_vertex_shader, cloned_options.get()));
  EXPECT_FALSE(CompilesToValidSpv(
      compiler.get_compiler_handle(), kMinimalDoubleExpandedShader,
      shaderc_glsl_vertex_shader, cloned_options.get()));

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

TEST(CompileStringWithOptions, DisassemblyOption) {
  Compiler compiler;
  compile_options_ptr options(shaderc_compile_options_initialize());
  shaderc_compile_options_set_disassembly_mode(options.get());
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  const std::string kMinimalShader = "void main(){}\n";
  const Compilation comp(compiler.get_compiler_handle(), kMinimalShader,
                         shaderc_glsl_vertex_shader, options.get());
  EXPECT_TRUE(shaderc_module_get_success(comp.result()));
  // This should work with both the glslang native disassembly format and the
  // SPIR-V tools assembly format.
  EXPECT_THAT(shaderc_module_get_bytes(comp.result()),
              HasSubstr("Capability Shader"));
  EXPECT_THAT(shaderc_module_get_bytes(comp.result()),
              HasSubstr("MemoryModel"));

  compile_options_ptr cloned_options(
      shaderc_compile_options_clone(options.get()));
  const Compilation comp_clone(compiler.get_compiler_handle(), kMinimalShader,
                               shaderc_glsl_vertex_shader,
                               cloned_options.get());
  EXPECT_TRUE(shaderc_module_get_success(comp_clone.result()));
  // The mode should be carried into any clone of the original option object.
  EXPECT_THAT(shaderc_module_get_bytes(comp_clone.result()),
              HasSubstr("Capability Shader"));
  EXPECT_THAT(shaderc_module_get_bytes(comp_clone.result()),
              HasSubstr("MemoryModel"));
}

TEST(CompileStringWithOptions, PreprocessingOnlyOption) {
  Compiler compiler;
  compile_options_ptr options(shaderc_compile_options_initialize());
  shaderc_compile_options_set_preprocessing_only_mode(options.get());
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  const std::string kMinimalShader =
      "#define E main\n"
      "void E(){}\n";
  const Compilation comp(compiler.get_compiler_handle(), kMinimalShader,
                         shaderc_glsl_vertex_shader, options.get());
  EXPECT_TRUE(shaderc_module_get_success(comp.result()));
  EXPECT_THAT(shaderc_module_get_bytes(comp.result()),
              HasSubstr("void main(){ }"));

  const std::string kMinimalShaderCloneOption =
      "#define E_CLONE_OPTION main\n"
      "void E_CLONE_OPTION(){}\n";
  compile_options_ptr cloned_options(
      shaderc_compile_options_clone(options.get()));
  const Compilation comp_clone(
      compiler.get_compiler_handle(), kMinimalShaderCloneOption,
      shaderc_glsl_vertex_shader, cloned_options.get());
  EXPECT_TRUE(shaderc_module_get_success(comp_clone.result()));
  EXPECT_THAT(shaderc_module_get_bytes(comp_clone.result()),
              HasSubstr("void main(){ }"));
}

TEST(CompileStringWithOptions, IfDefCompileOption) {
  Compiler compiler;
  compile_options_ptr options(shaderc_compile_options_initialize());
  shaderc_compile_options_add_macro_definition(options.get(), "E", nullptr);
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  const std::string kMinimalExpandedShader =
      "#ifdef E\n"
      "void main(){}\n"
      "#else\n"
      "garbage string won't compile\n"
      "#endif";
  EXPECT_TRUE(CompilesToValidSpv(compiler.get_compiler_handle(),
                                 kMinimalExpandedShader,
                                 shaderc_glsl_vertex_shader, options.get()));
}

TEST(CompileStringWithOptions, TargetEnv) {
  Compiler compiler;
  compile_options_ptr options(shaderc_compile_options_initialize());
  ASSERT_NE(nullptr, compiler.get_compiler_handle());

  // Confirm that this shader compiles with shaderc_target_env_opengl_compat;
  // if targeting Vulkan, glslang will fail to compile it
  const std::string kGlslShader =
      R"(#version 100
       uniform highp sampler2D tex;
       void main() {
         gl_FragColor = texture2D(tex, vec2(0.0,0.0));
       }
  )";

  EXPECT_FALSE(CompilesToValidSpv(compiler.get_compiler_handle(), kGlslShader,
                                  shaderc_glsl_fragment_shader, options.get()));

  shaderc_compile_options_set_target_env(options.get(),
                                         shaderc_target_env_opengl_compat, 0);
  EXPECT_TRUE(CompilesToValidSpv(compiler.get_compiler_handle(), kGlslShader,
                                 shaderc_glsl_fragment_shader, options.get()));

  shaderc_compile_options_set_target_env(options.get(),
                                         shaderc_target_env_vulkan, 0);
  EXPECT_FALSE(CompilesToValidSpv(compiler.get_compiler_handle(), kGlslShader,
                                  shaderc_glsl_fragment_shader, options.get()));
}

TEST(CompileString, ShaderKindRespected) {
  Compiler compiler;
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  const std::string kVertexShader = "void main(){ gl_Position = vec4(0);}";
  EXPECT_TRUE(CompilationSuccess(compiler.get_compiler_handle(), kVertexShader,
                                 shaderc_glsl_vertex_shader));
  EXPECT_FALSE(CompilationSuccess(compiler.get_compiler_handle(), kVertexShader,
                                  shaderc_glsl_fragment_shader));
}

TEST(CompileString, ErrorsReported) {
  Compiler compiler;
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  const Compilation comp(compiler.get_compiler_handle(),
                         "int f(){return wrongname;}",
                         shaderc_glsl_vertex_shader);
  ASSERT_FALSE(shaderc_module_get_success(comp.result()));
  EXPECT_THAT(shaderc_module_get_error_message(comp.result()),
              HasSubstr("wrongname"));
}

TEST(CompileString, MultipleThreadsCalling) {
  Compiler compiler;
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  bool results[10];
  std::vector<std::thread> threads;
  for (auto& r : results) {
    threads.emplace_back([&compiler, &r]() {
      r = CompilationSuccess(compiler.get_compiler_handle(), "void main(){}",
                             shaderc_glsl_vertex_shader);
    });
  }
  for (auto& t : threads) {
    t.join();
  }
  EXPECT_THAT(results, Each(true));
}

TEST(CompileKinds, Vertex) {
  Compiler compiler;
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  const std::string kVertexShader = "void main(){ gl_Position = vec4(0);}";
  EXPECT_TRUE(CompilationSuccess(compiler.get_compiler_handle(), kVertexShader,
                                 shaderc_glsl_vertex_shader));
}

TEST(CompileKinds, Fragment) {
  Compiler compiler;
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  const std::string kFragShader = "void main(){ gl_FragColor = vec4(0);}";
  EXPECT_TRUE(CompilationSuccess(compiler.get_compiler_handle(), kFragShader,
                                 shaderc_glsl_fragment_shader));
}

TEST(CompileKinds, Compute) {
  Compiler compiler;
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  const std::string kCompShader =
      R"(#version 310 es
       void main() {}
  )";
  EXPECT_TRUE(CompilationSuccess(compiler.get_compiler_handle(), kCompShader,
                                 shaderc_glsl_compute_shader));
}

TEST(CompileKinds, Geometry) {
  Compiler compiler;
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  const std::string kGeoShader =
      R"(#version 310 es
       #extension GL_OES_geometry_shader : enable
       layout(points) in;
       layout(points, max_vertices=1) out;
       void main() {
         gl_Position = vec4(1.0);
         EmitVertex();
         EndPrimitive();
       }
  )";
  EXPECT_TRUE(CompilationSuccess(compiler.get_compiler_handle(), kGeoShader,
                                 shaderc_glsl_geometry_shader));
}

TEST(CompileKinds, TessControl) {
  Compiler compiler;
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  const std::string kTCSShader =
      R"(#version 310 es
       #extension GL_OES_tessellation_shader : enable
       layout(vertices=1) out;
       void main() {}
  )";
  EXPECT_TRUE(CompilationSuccess(compiler.get_compiler_handle(), kTCSShader,
                                 shaderc_glsl_tess_control_shader));
}

TEST(CompileKinds, TessEvaluation) {
  Compiler compiler;
  ASSERT_NE(nullptr, compiler.get_compiler_handle());
  const std::string kTESShader =
      R"(#version 310 es
       #extension GL_OES_tessellation_shader : enable
       layout(triangles, equal_spacing, ccw) in;
       void main() {
         gl_Position = vec4(gl_TessCoord, 1.0);
       }
  )";
  EXPECT_TRUE(CompilationSuccess(compiler.get_compiler_handle(), kTESShader,
                                 shaderc_glsl_tess_evaluation_shader));
}

}  // anonymous namespace