Coverage Report

Created: 2025-11-11 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/shaderc/libshaderc/include/shaderc/shaderc.hpp
Line
Count
Source
1
// Copyright 2015 The Shaderc Authors. All rights reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#ifndef SHADERC_SHADERC_HPP_
16
#define SHADERC_SHADERC_HPP_
17
18
#include <memory>
19
#include <string>
20
#include <vector>
21
22
#include "shaderc.h"
23
24
namespace shaderc {
25
// A CompilationResult contains the compiler output, compilation status,
26
// and messages.
27
//
28
// The compiler output is stored as an array of elements and accessed
29
// via random access iterators provided by cbegin() and cend().  The iterators
30
// are contiguous in the sense of "Contiguous Iterators: A Refinement of
31
// Random Access Iterators", Nevin Liber, C++ Library Evolution Working
32
// Group Working Paper N3884.
33
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3884.pdf
34
//
35
// Methods begin() and end() are also provided to enable range-based for.
36
// They are synonyms to cbegin() and cend(), respectively.
37
template <typename OutputElementType>
38
class CompilationResult {
39
 public:
40
  typedef OutputElementType element_type;
41
  // The type used to describe the begin and end iterators on the
42
  // compiler output.
43
  typedef const OutputElementType* const_iterator;
44
45
  // Upon creation, the CompilationResult takes ownership of the
46
  // shaderc_compilation_result instance. During destruction of the
47
  // CompilationResult, the shaderc_compilation_result will be released.
48
  explicit CompilationResult(shaderc_compilation_result_t compilation_result)
49
20.5k
      : compilation_result_(compilation_result) {}
shaderc::CompilationResult<unsigned int>::CompilationResult(shaderc_compilation_result*)
Line
Count
Source
49
6.83k
      : compilation_result_(compilation_result) {}
shaderc::CompilationResult<char>::CompilationResult(shaderc_compilation_result*)
Line
Count
Source
49
13.6k
      : compilation_result_(compilation_result) {}
50
  CompilationResult() : compilation_result_(nullptr) {}
51
20.5k
  ~CompilationResult() { shaderc_result_release(compilation_result_); }
shaderc::CompilationResult<unsigned int>::~CompilationResult()
Line
Count
Source
51
6.83k
  ~CompilationResult() { shaderc_result_release(compilation_result_); }
shaderc::CompilationResult<char>::~CompilationResult()
Line
Count
Source
51
13.6k
  ~CompilationResult() { shaderc_result_release(compilation_result_); }
52
53
  CompilationResult(CompilationResult&& other) : compilation_result_(nullptr) {
54
    *this = std::move(other);
55
  }
56
57
  CompilationResult& operator=(CompilationResult&& other) {
58
    if (compilation_result_) {
59
      shaderc_result_release(compilation_result_);
60
    }
61
    compilation_result_ = other.compilation_result_;
62
    other.compilation_result_ = nullptr;
63
    return *this;
64
  }
65
66
  // Returns any error message found during compilation.
67
20.5k
  std::string GetErrorMessage() const {
68
20.5k
    if (!compilation_result_) {
69
0
      return "";
70
0
    }
71
20.5k
    return shaderc_result_get_error_message(compilation_result_);
72
20.5k
  }
shaderc::CompilationResult<unsigned int>::GetErrorMessage() const
Line
Count
Source
67
6.83k
  std::string GetErrorMessage() const {
68
6.83k
    if (!compilation_result_) {
69
0
      return "";
70
0
    }
71
6.83k
    return shaderc_result_get_error_message(compilation_result_);
72
6.83k
  }
shaderc::CompilationResult<char>::GetErrorMessage() const
Line
Count
Source
67
13.6k
  std::string GetErrorMessage() const {
68
13.6k
    if (!compilation_result_) {
69
0
      return "";
70
0
    }
71
13.6k
    return shaderc_result_get_error_message(compilation_result_);
72
13.6k
  }
73
74
  // Returns the compilation status, indicating whether the compilation
75
  // succeeded, or failed due to some reasons, like invalid shader stage or
76
  // compilation errors.
77
41.0k
  shaderc_compilation_status GetCompilationStatus() const {
78
41.0k
    if (!compilation_result_) {
79
0
      return shaderc_compilation_status_null_result_object;
80
0
    }
81
41.0k
    return shaderc_result_get_compilation_status(compilation_result_);
82
41.0k
  }
shaderc::CompilationResult<unsigned int>::GetCompilationStatus() const
Line
Count
Source
77
13.6k
  shaderc_compilation_status GetCompilationStatus() const {
78
13.6k
    if (!compilation_result_) {
79
0
      return shaderc_compilation_status_null_result_object;
80
0
    }
81
13.6k
    return shaderc_result_get_compilation_status(compilation_result_);
82
13.6k
  }
shaderc::CompilationResult<char>::GetCompilationStatus() const
Line
Count
Source
77
27.3k
  shaderc_compilation_status GetCompilationStatus() const {
78
27.3k
    if (!compilation_result_) {
79
0
      return shaderc_compilation_status_null_result_object;
80
0
    }
81
27.3k
    return shaderc_result_get_compilation_status(compilation_result_);
82
27.3k
  }
83
84
  // Returns a random access (contiguous) iterator pointing to the start
85
  // of the compilation output.  It is valid for the lifetime of this object.
86
  // If there is no compilation result, then returns nullptr.
87
41.0k
  const_iterator cbegin() const {
88
41.0k
    if (!compilation_result_) return nullptr;
89
41.0k
    return reinterpret_cast<const_iterator>(
90
41.0k
        shaderc_result_get_bytes(compilation_result_));
91
41.0k
  }
shaderc::CompilationResult<unsigned int>::cbegin() const
Line
Count
Source
87
13.6k
  const_iterator cbegin() const {
88
13.6k
    if (!compilation_result_) return nullptr;
89
13.6k
    return reinterpret_cast<const_iterator>(
90
13.6k
        shaderc_result_get_bytes(compilation_result_));
91
13.6k
  }
shaderc::CompilationResult<char>::cbegin() const
Line
Count
Source
87
27.3k
  const_iterator cbegin() const {
88
27.3k
    if (!compilation_result_) return nullptr;
89
27.3k
    return reinterpret_cast<const_iterator>(
90
27.3k
        shaderc_result_get_bytes(compilation_result_));
91
27.3k
  }
92
93
  // Returns a random access (contiguous) iterator pointing to the end of
94
  // the compilation output.  It is valid for the lifetime of this object.
95
  // If there is no compilation result, then returns nullptr.
96
20.5k
  const_iterator cend() const {
97
20.5k
    if (!compilation_result_) return nullptr;
98
20.5k
    return cbegin() +
99
20.5k
           shaderc_result_get_length(compilation_result_) /
100
20.5k
               sizeof(OutputElementType);
101
20.5k
  }
shaderc::CompilationResult<unsigned int>::cend() const
Line
Count
Source
96
6.83k
  const_iterator cend() const {
97
6.83k
    if (!compilation_result_) return nullptr;
98
6.83k
    return cbegin() +
99
6.83k
           shaderc_result_get_length(compilation_result_) /
100
6.83k
               sizeof(OutputElementType);
101
6.83k
  }
shaderc::CompilationResult<char>::cend() const
Line
Count
Source
96
13.6k
  const_iterator cend() const {
97
13.6k
    if (!compilation_result_) return nullptr;
98
13.6k
    return cbegin() +
99
13.6k
           shaderc_result_get_length(compilation_result_) /
100
13.6k
               sizeof(OutputElementType);
101
13.6k
  }
102
103
  // Returns the same iterator as cbegin().
104
0
  const_iterator begin() const { return cbegin(); }
Unexecuted instantiation: shaderc::CompilationResult<unsigned int>::begin() const
Unexecuted instantiation: shaderc::CompilationResult<char>::begin() const
105
  // Returns the same iterator as cend().
106
0
  const_iterator end() const { return cend(); }
Unexecuted instantiation: shaderc::CompilationResult<unsigned int>::end() const
Unexecuted instantiation: shaderc::CompilationResult<char>::end() const
107
108
  // Returns the number of warnings generated during the compilation.
109
20.5k
  size_t GetNumWarnings() const {
110
20.5k
    if (!compilation_result_) {
111
0
      return 0;
112
0
    }
113
20.5k
    return shaderc_result_get_num_warnings(compilation_result_);
114
20.5k
  }
shaderc::CompilationResult<unsigned int>::GetNumWarnings() const
Line
Count
Source
109
6.83k
  size_t GetNumWarnings() const {
110
6.83k
    if (!compilation_result_) {
111
0
      return 0;
112
0
    }
113
6.83k
    return shaderc_result_get_num_warnings(compilation_result_);
114
6.83k
  }
shaderc::CompilationResult<char>::GetNumWarnings() const
Line
Count
Source
109
13.6k
  size_t GetNumWarnings() const {
110
13.6k
    if (!compilation_result_) {
111
0
      return 0;
112
0
    }
113
13.6k
    return shaderc_result_get_num_warnings(compilation_result_);
114
13.6k
  }
115
116
  // Returns the number of errors generated during the compilation.
117
20.5k
  size_t GetNumErrors() const {
118
20.5k
    if (!compilation_result_) {
119
0
      return 0;
120
0
    }
121
20.5k
    return shaderc_result_get_num_errors(compilation_result_);
122
20.5k
  }
shaderc::CompilationResult<unsigned int>::GetNumErrors() const
Line
Count
Source
117
6.83k
  size_t GetNumErrors() const {
118
6.83k
    if (!compilation_result_) {
119
0
      return 0;
120
0
    }
121
6.83k
    return shaderc_result_get_num_errors(compilation_result_);
122
6.83k
  }
shaderc::CompilationResult<char>::GetNumErrors() const
Line
Count
Source
117
13.6k
  size_t GetNumErrors() const {
118
13.6k
    if (!compilation_result_) {
119
0
      return 0;
120
0
    }
121
13.6k
    return shaderc_result_get_num_errors(compilation_result_);
122
13.6k
  }
123
124
 private:
125
  CompilationResult(const CompilationResult& other) = delete;
126
  CompilationResult& operator=(const CompilationResult& other) = delete;
127
128
  shaderc_compilation_result_t compilation_result_;
129
};
130
131
// A compilation result for a SPIR-V binary module, which is an array
132
// of uint32_t words.
133
using SpvCompilationResult = CompilationResult<uint32_t>;
134
// A compilation result in SPIR-V assembly syntax.
135
using AssemblyCompilationResult = CompilationResult<char>;
136
// Preprocessed source text.
137
using PreprocessedSourceCompilationResult = CompilationResult<char>;
138
139
// Contains any options that can have default values for a compilation.
140
class CompileOptions {
141
 public:
142
6.83k
  CompileOptions() { options_ = shaderc_compile_options_initialize(); }
143
6.83k
  ~CompileOptions() { shaderc_compile_options_release(options_); }
144
0
  CompileOptions(const CompileOptions& other) {
145
0
    options_ = shaderc_compile_options_clone(other.options_);
146
0
  }
147
0
  CompileOptions(CompileOptions&& other) {
148
0
    options_ = other.options_;
149
0
    other.options_ = nullptr;
150
0
  }
151
152
  // Adds a predefined macro to the compilation options. It behaves the same as
153
  // shaderc_compile_options_add_macro_definition in shaderc.h.
154
  void AddMacroDefinition(const char* name, size_t name_length,
155
0
                          const char* value, size_t value_length) {
156
0
    shaderc_compile_options_add_macro_definition(options_, name, name_length,
157
0
                                                 value, value_length);
158
0
  }
159
160
  // Adds a valueless predefined macro to the compilation options.
161
0
  void AddMacroDefinition(const std::string& name) {
162
0
    AddMacroDefinition(name.c_str(), name.size(), nullptr, 0u);
163
0
  }
164
165
  // Adds a predefined macro to the compilation options.
166
0
  void AddMacroDefinition(const std::string& name, const std::string& value) {
167
0
    AddMacroDefinition(name.c_str(), name.size(), value.c_str(), value.size());
168
0
  }
169
170
  // Sets the compiler mode to generate debug information in the output.
171
0
  void SetGenerateDebugInfo() {
172
0
    shaderc_compile_options_set_generate_debug_info(options_);
173
0
  }
174
175
  // Sets the compiler optimization level to the given level. Only the last one
176
  // takes effect if multiple calls of this function exist.
177
0
  void SetOptimizationLevel(shaderc_optimization_level level) {
178
0
    shaderc_compile_options_set_optimization_level(options_, level);
179
0
  }
180
181
  // A C++ version of the libshaderc includer interface.
182
  class IncluderInterface {
183
   public:
184
    // Handles shaderc_include_resolver_fn callbacks.
185
    virtual shaderc_include_result* GetInclude(const char* requested_source,
186
                                               shaderc_include_type type,
187
                                               const char* requesting_source,
188
                                               size_t include_depth) = 0;
189
190
    // Handles shaderc_include_result_release_fn callbacks.
191
    virtual void ReleaseInclude(shaderc_include_result* data) = 0;
192
193
20.5k
    virtual ~IncluderInterface() = default;
194
  };
195
196
  // Sets the includer instance for libshaderc to call during compilation, as
197
  // described in shaderc_compile_options_set_include_callbacks().  Callbacks
198
  // are routed to this includer's methods.
199
20.5k
  void SetIncluder(std::unique_ptr<IncluderInterface>&& includer) {
200
20.5k
    includer_ = std::move(includer);
201
20.5k
    shaderc_compile_options_set_include_callbacks(
202
20.5k
        options_,
203
20.5k
        [](void* user_data, const char* requested_source, int type,
204
20.5k
           const char* requesting_source, size_t include_depth) {
205
2.25k
          auto* sub_includer = static_cast<IncluderInterface*>(user_data);
206
2.25k
          return sub_includer->GetInclude(
207
2.25k
              requested_source, static_cast<shaderc_include_type>(type),
208
2.25k
              requesting_source, include_depth);
209
2.25k
        },
210
20.5k
        [](void* user_data, shaderc_include_result* include_result) {
211
2.25k
          auto* sub_includer = static_cast<IncluderInterface*>(user_data);
212
2.25k
          return sub_includer->ReleaseInclude(include_result);
213
2.25k
        },
214
20.5k
        includer_.get());
215
20.5k
  }
216
217
  // Forces the GLSL language version and profile to a given pair. The version
218
  // number is the same as would appear in the #version annotation in the
219
  // source. Version and profile specified here overrides the #version
220
  // annotation in the source. Use profile: 'shaderc_profile_none' for GLSL
221
  // versions that do not define profiles, e.g. versions below 150.
222
0
  void SetForcedVersionProfile(int version, shaderc_profile profile) {
223
0
    shaderc_compile_options_set_forced_version_profile(options_, version,
224
0
                                                       profile);
225
0
  }
226
227
  // Sets the compiler mode to suppress warnings. Note this option overrides
228
  // warnings-as-errors mode. When both suppress-warnings and warnings-as-errors
229
  // modes are turned on, warning messages will be inhibited, and will not be
230
  // emitted as error message.
231
6.83k
  void SetSuppressWarnings() {
232
6.83k
    shaderc_compile_options_set_suppress_warnings(options_);
233
6.83k
  }
234
235
  // Sets the source language. The default is GLSL.
236
20.5k
  void SetSourceLanguage(shaderc_source_language lang) {
237
20.5k
    shaderc_compile_options_set_source_language(options_, lang);
238
20.5k
  }
239
240
  // Sets the target shader environment, affecting which warnings or errors will
241
  // be issued.  The version will be for distinguishing between different
242
  // versions of the target environment.  The version value should be either 0
243
  // or a value listed in shaderc_env_version.  The 0 value maps to Vulkan 1.0
244
  // if |target| is Vulkan, and it maps to OpenGL 4.5 if |target| is OpenGL.
245
0
  void SetTargetEnvironment(shaderc_target_env target, uint32_t version) {
246
0
    shaderc_compile_options_set_target_env(options_, target, version);
247
0
  }
248
249
  // Sets the target SPIR-V version.  The generated module will use this version
250
  // of SPIR-V.  Each target environment determines what versions of SPIR-V
251
  // it can consume.  Defaults to the highest version of SPIR-V 1.0 which is
252
  // required to be supported by the target environment.  E.g. Default to SPIR-V
253
  // 1.0 for Vulkan 1.0 and SPIR-V 1.3 for Vulkan 1.1.
254
0
  void SetTargetSpirv(shaderc_spirv_version version) {
255
0
    shaderc_compile_options_set_target_spirv(options_, version);
256
0
  }
257
258
  // Sets the compiler mode to make all warnings into errors. Note the
259
  // suppress-warnings mode overrides this option, i.e. if both
260
  // warning-as-errors and suppress-warnings modes are set on, warnings will not
261
  // be emitted as error message.
262
0
  void SetWarningsAsErrors() {
263
0
    shaderc_compile_options_set_warnings_as_errors(options_);
264
0
  }
265
266
  // Sets a resource limit.
267
0
  void SetLimit(shaderc_limit limit, int value) {
268
0
    shaderc_compile_options_set_limit(options_, limit, value);
269
0
  }
270
271
  // Sets whether the compiler should automatically assign bindings to uniforms
272
  // that aren't already explicitly bound in the shader source.
273
0
  void SetAutoBindUniforms(bool auto_bind) {
274
0
    shaderc_compile_options_set_auto_bind_uniforms(options_, auto_bind);
275
0
  }
276
277
  // Sets whether the compiler should automatically remove sampler variables
278
  // and convert image variables to combined image sampler variables.
279
0
  void SetAutoSampledTextures(bool auto_sampled) {
280
0
    shaderc_compile_options_set_auto_combined_image_sampler(options_,
281
0
                                                            auto_sampled);
282
0
  }
283
284
  // Sets whether the compiler should use HLSL IO mapping rules for bindings.
285
  // Defaults to false.
286
0
  void SetHlslIoMapping(bool hlsl_iomap) {
287
0
    shaderc_compile_options_set_hlsl_io_mapping(options_, hlsl_iomap);
288
0
  }
289
290
  // Sets whether the compiler should determine block member offsets using HLSL
291
  // packing rules instead of standard GLSL rules.  Defaults to false.  Only
292
  // affects GLSL compilation.  HLSL rules are always used when compiling HLSL.
293
0
  void SetHlslOffsets(bool hlsl_offsets) {
294
0
    shaderc_compile_options_set_hlsl_offsets(options_, hlsl_offsets);
295
0
  }
296
297
  // Sets the base binding number used for for a uniform resource type when
298
  // automatically assigning bindings.  For GLSL compilation, sets the lowest
299
  // automatically assigned number.  For HLSL compilation, the regsiter number
300
  // assigned to the resource is added to this specified base.
301
0
  void SetBindingBase(shaderc_uniform_kind kind, uint32_t base) {
302
0
    shaderc_compile_options_set_binding_base(options_, kind, base);
303
0
  }
304
305
  // Like SetBindingBase, but only takes effect when compiling a given shader
306
  // stage.  The stage is assumed to be one of vertex, fragment, tessellation
307
  // evaluation, tesselation control, geometry, or compute.
308
  void SetBindingBaseForStage(shaderc_shader_kind shader_kind,
309
0
                              shaderc_uniform_kind kind, uint32_t base) {
310
0
    shaderc_compile_options_set_binding_base_for_stage(options_, shader_kind,
311
0
                                                       kind, base);
312
0
  }
313
314
  // Sets whether the compiler should preserve all bindings, even when those
315
  // bindings are not used.
316
0
  void SetPreserveBindings(bool preserve_bindings) {
317
0
    shaderc_compile_options_set_preserve_bindings(options_, preserve_bindings);
318
0
  }
319
320
0
  void SetMaxIdBound(uint32_t max_id_bound) {
321
0
    shaderc_compile_options_set_max_id_bound(options_, max_id_bound);
322
0
  }
323
324
  // Sets whether the compiler automatically assigns locations to
325
  // uniform variables that don't have explicit locations.
326
0
  void SetAutoMapLocations(bool auto_map) {
327
0
    shaderc_compile_options_set_auto_map_locations(options_, auto_map);
328
0
  }
329
330
  // Sets a descriptor set and binding for an HLSL register in the given stage.
331
  // Copies the parameter strings.
332
  void SetHlslRegisterSetAndBindingForStage(shaderc_shader_kind shader_kind,
333
                                            const std::string& reg,
334
                                            const std::string& set,
335
0
                                            const std::string& binding) {
336
0
    shaderc_compile_options_set_hlsl_register_set_and_binding_for_stage(
337
0
        options_, shader_kind, reg.c_str(), set.c_str(), binding.c_str());
338
0
  }
339
340
  // Sets a descriptor set and binding for an HLSL register in any stage.
341
  // Copies the parameter strings.
342
  void SetHlslRegisterSetAndBinding(const std::string& reg,
343
                                    const std::string& set,
344
0
                                    const std::string& binding) {
345
0
    shaderc_compile_options_set_hlsl_register_set_and_binding(
346
0
        options_, reg.c_str(), set.c_str(), binding.c_str());
347
0
  }
348
349
  // Sets whether the compiler should enable extension
350
  // SPV_GOOGLE_hlsl_functionality1.
351
0
  void SetHlslFunctionality1(bool enable) {
352
0
    shaderc_compile_options_set_hlsl_functionality1(options_, enable);
353
0
  }
354
355
  // Sets whether 16-bit types are supported in HLSL or not.
356
0
  void SetHlsl16BitTypes(bool enable) {
357
0
    shaderc_compile_options_set_hlsl_16bit_types(options_, enable);
358
0
  }
359
360
  // Enables or disables relaxed Vulkan rules.
361
  //
362
  // This allows most OpenGL shaders to compile under Vulkan semantics.
363
0
  void SetVulkanRulesRelaxed(bool enable) {
364
0
    shaderc_compile_options_set_vulkan_rules_relaxed(options_, enable);
365
0
  }
366
367
  // Sets whether the compiler should invert position.Y output in vertex shader.
368
0
  void SetInvertY(bool enable) {
369
0
    shaderc_compile_options_set_invert_y(options_, enable);
370
0
  }
371
372
  // Sets whether the compiler should generate code for max and min which,
373
  // if given a NaN operand, will return the other operand. Similarly, the
374
  // clamp builtin will favour the non-NaN operands, as if clamp were
375
  // implemented as a composition of max and min.
376
0
  void SetNanClamp(bool enable) {
377
0
    shaderc_compile_options_set_nan_clamp(options_, enable);
378
0
  }
379
380
 private:
381
  CompileOptions& operator=(const CompileOptions& other) = delete;
382
  shaderc_compile_options_t options_;
383
  std::unique_ptr<IncluderInterface> includer_;
384
385
  friend class Compiler;
386
};
387
388
// The compilation context for compiling source to SPIR-V.
389
class Compiler {
390
 public:
391
6.83k
  Compiler() : compiler_(shaderc_compiler_initialize()) {}
392
6.83k
  ~Compiler() { shaderc_compiler_release(compiler_); }
393
394
0
  Compiler(Compiler&& other) {
395
0
    compiler_ = other.compiler_;
396
0
    other.compiler_ = nullptr;
397
0
  }
398
399
0
  bool IsValid() const { return compiler_ != nullptr; }
400
401
  // Compiles the given source GLSL and returns a SPIR-V binary module
402
  // compilation result.
403
  // The source_text parameter must be a valid pointer.
404
  // The source_text_size parameter must be the length of the source text.
405
  // The shader_kind parameter either forces the compilation to be done with a
406
  // specified shader kind, or hint the compiler how to determine the exact
407
  // shader kind. If the shader kind is set to shaderc_glslc_infer_from_source,
408
  // the compiler will try to deduce the shader kind from the source string and
409
  // a failure in this proess will generate an error. Currently only #pragma
410
  // annotation is supported. If the shader kind is set to one of the default
411
  // shader kinds, the compiler will fall back to the specified default shader
412
  // kind in case it failed to deduce the shader kind from the source string.
413
  // The input_file_name is a null-termintated string. It is used as a tag to
414
  // identify the source string in cases like emitting error messages. It
415
  // doesn't have to be a 'file name'.
416
  // The entry_point_name parameter is a null-terminated string specifying
417
  // the entry point name for HLSL compilation.  For GLSL compilation, the
418
  // entry point name is assumed to be "main".
419
  // The compilation is passed any options specified in the CompileOptions
420
  // parameter.
421
  // It is valid for the returned CompilationResult object to outlive this
422
  // compiler object.
423
  // Note when the options_ has disassembly mode or preprocessing only mode set
424
  // on, the returned CompilationResult will hold a text string, instead of a
425
  // SPIR-V binary generated with default options.
426
  SpvCompilationResult CompileGlslToSpv(const char* source_text,
427
                                        size_t source_text_size,
428
                                        shaderc_shader_kind shader_kind,
429
                                        const char* input_file_name,
430
                                        const char* entry_point_name,
431
6.83k
                                        const CompileOptions& options) const {
432
6.83k
    shaderc_compilation_result_t compilation_result = shaderc_compile_into_spv(
433
6.83k
        compiler_, source_text, source_text_size, shader_kind, input_file_name,
434
6.83k
        entry_point_name, options.options_);
435
6.83k
    return SpvCompilationResult(compilation_result);
436
6.83k
  }
437
438
  // Compiles the given source shader and returns a SPIR-V binary module
439
  // compilation result.
440
  // Like the first CompileGlslToSpv method but assumes the entry point name
441
  // is "main".
442
  SpvCompilationResult CompileGlslToSpv(const char* source_text,
443
                                        size_t source_text_size,
444
                                        shaderc_shader_kind shader_kind,
445
                                        const char* input_file_name,
446
0
                                        const CompileOptions& options) const {
447
0
    return CompileGlslToSpv(source_text, source_text_size, shader_kind,
448
0
                            input_file_name, "main", options);
449
0
  }
450
451
  // Compiles the given source GLSL and returns a SPIR-V binary module
452
  // compilation result.
453
  // Like the previous CompileGlslToSpv method but uses default options.
454
  SpvCompilationResult CompileGlslToSpv(const char* source_text,
455
                                        size_t source_text_size,
456
                                        shaderc_shader_kind shader_kind,
457
0
                                        const char* input_file_name) const {
458
0
    shaderc_compilation_result_t compilation_result =
459
0
        shaderc_compile_into_spv(compiler_, source_text, source_text_size,
460
0
                                 shader_kind, input_file_name, "main", nullptr);
461
0
    return SpvCompilationResult(compilation_result);
462
0
  }
463
464
  // Compiles the given source shader and returns a SPIR-V binary module
465
  // compilation result.
466
  // Like the first CompileGlslToSpv method but the source is provided as
467
  // a std::string, and we assume the entry point is "main".
468
  SpvCompilationResult CompileGlslToSpv(const std::string& source_text,
469
                                        shaderc_shader_kind shader_kind,
470
                                        const char* input_file_name,
471
0
                                        const CompileOptions& options) const {
472
0
    return CompileGlslToSpv(source_text.data(), source_text.size(), shader_kind,
473
0
                            input_file_name, options);
474
0
  }
475
476
  // Compiles the given source shader and returns a SPIR-V binary module
477
  // compilation result.
478
  // Like the first CompileGlslToSpv method but the source is provided as
479
  // a std::string.
480
  SpvCompilationResult CompileGlslToSpv(const std::string& source_text,
481
                                        shaderc_shader_kind shader_kind,
482
                                        const char* input_file_name,
483
                                        const char* entry_point_name,
484
0
                                        const CompileOptions& options) const {
485
0
    return CompileGlslToSpv(source_text.data(), source_text.size(), shader_kind,
486
0
                            input_file_name, entry_point_name, options);
487
0
  }
488
489
  // Compiles the given source GLSL and returns a SPIR-V binary module
490
  // compilation result.
491
  // Like the previous CompileGlslToSpv method but assumes the entry point
492
  // name is "main".
493
  SpvCompilationResult CompileGlslToSpv(const std::string& source_text,
494
                                        shaderc_shader_kind shader_kind,
495
0
                                        const char* input_file_name) const {
496
0
    return CompileGlslToSpv(source_text.data(), source_text.size(), shader_kind,
497
0
                            input_file_name);
498
0
  }
499
500
  // Assembles the given SPIR-V assembly and returns a SPIR-V binary module
501
  // compilation result.
502
  // The assembly should follow the syntax defined in the SPIRV-Tools project
503
  // (https://github.com/KhronosGroup/SPIRV-Tools/blob/master/syntax.md).
504
  // It is valid for the returned CompilationResult object to outlive this
505
  // compiler object.
506
  // The assembling will pick options suitable for assembling specified in the
507
  // CompileOptions parameter.
508
  SpvCompilationResult AssembleToSpv(const char* source_assembly,
509
                                     size_t source_assembly_size,
510
0
                                     const CompileOptions& options) const {
511
0
    return SpvCompilationResult(shaderc_assemble_into_spv(
512
0
        compiler_, source_assembly, source_assembly_size, options.options_));
513
0
  }
514
515
  // Assembles the given SPIR-V assembly and returns a SPIR-V binary module
516
  // compilation result.
517
  // Like the first AssembleToSpv method but uses the default compiler options.
518
  SpvCompilationResult AssembleToSpv(const char* source_assembly,
519
0
                                     size_t source_assembly_size) const {
520
0
    return SpvCompilationResult(shaderc_assemble_into_spv(
521
0
        compiler_, source_assembly, source_assembly_size, nullptr));
522
0
  }
523
524
  // Assembles the given SPIR-V assembly and returns a SPIR-V binary module
525
  // compilation result.
526
  // Like the first AssembleToSpv method but the source is provided as a
527
  // std::string.
528
  SpvCompilationResult AssembleToSpv(const std::string& source_assembly,
529
0
                                     const CompileOptions& options) const {
530
0
    return SpvCompilationResult(
531
0
        shaderc_assemble_into_spv(compiler_, source_assembly.data(),
532
0
                                  source_assembly.size(), options.options_));
533
0
  }
534
535
  // Assembles the given SPIR-V assembly and returns a SPIR-V binary module
536
  // compilation result.
537
  // Like the first AssembleToSpv method but the source is provided as a
538
  // std::string and also uses default compiler options.
539
0
  SpvCompilationResult AssembleToSpv(const std::string& source_assembly) const {
540
0
    return SpvCompilationResult(shaderc_assemble_into_spv(
541
0
        compiler_, source_assembly.data(), source_assembly.size(), nullptr));
542
0
  }
543
544
  // Compiles the given source GLSL and returns the SPIR-V assembly text
545
  // compilation result.
546
  // Options are similar to the first CompileToSpv method.
547
  AssemblyCompilationResult CompileGlslToSpvAssembly(
548
      const char* source_text, size_t source_text_size,
549
      shaderc_shader_kind shader_kind, const char* input_file_name,
550
0
      const char* entry_point_name, const CompileOptions& options) const {
551
0
    shaderc_compilation_result_t compilation_result =
552
0
        shaderc_compile_into_spv_assembly(
553
0
            compiler_, source_text, source_text_size, shader_kind,
554
0
            input_file_name, entry_point_name, options.options_);
555
0
    return AssemblyCompilationResult(compilation_result);
556
0
  }
557
558
  // Compiles the given source GLSL and returns the SPIR-V assembly text
559
  // compilation result.
560
  // Similare to the previous method, but assumes entry point name is "main".
561
  AssemblyCompilationResult CompileGlslToSpvAssembly(
562
      const char* source_text, size_t source_text_size,
563
      shaderc_shader_kind shader_kind, const char* input_file_name,
564
0
      const CompileOptions& options) const {
565
0
    return CompileGlslToSpvAssembly(source_text, source_text_size, shader_kind,
566
0
                                    input_file_name, "main", options);
567
0
  }
568
569
  // Compiles the given source GLSL and returns the SPIR-V assembly text
570
  // result. Like the first CompileGlslToSpvAssembly method but the source
571
  // is provided as a std::string.  Options are otherwise similar to
572
  // the first CompileToSpv method.
573
  AssemblyCompilationResult CompileGlslToSpvAssembly(
574
      const std::string& source_text, shaderc_shader_kind shader_kind,
575
      const char* input_file_name, const char* entry_point_name,
576
0
      const CompileOptions& options) const {
577
0
    return CompileGlslToSpvAssembly(source_text.data(), source_text.size(),
578
0
                                    shader_kind, input_file_name,
579
0
                                    entry_point_name, options);
580
0
  }
581
582
  // Compiles the given source GLSL and returns the SPIR-V assembly text
583
  // result. Like the previous  CompileGlslToSpvAssembly method but assumes
584
  // the entry point name is "main".
585
  AssemblyCompilationResult CompileGlslToSpvAssembly(
586
      const std::string& source_text, shaderc_shader_kind shader_kind,
587
0
      const char* input_file_name, const CompileOptions& options) const {
588
0
    return CompileGlslToSpvAssembly(source_text, shader_kind, input_file_name,
589
0
                                    "main", options);
590
0
  }
591
592
  // Preprocesses the given source GLSL and returns the preprocessed
593
  // source text as a compilation result.
594
  // Options are similar to the first CompileToSpv method.
595
  PreprocessedSourceCompilationResult PreprocessGlsl(
596
      const char* source_text, size_t source_text_size,
597
      shaderc_shader_kind shader_kind, const char* input_file_name,
598
13.6k
      const CompileOptions& options) const {
599
13.6k
    shaderc_compilation_result_t compilation_result =
600
13.6k
        shaderc_compile_into_preprocessed_text(
601
13.6k
            compiler_, source_text, source_text_size, shader_kind,
602
13.6k
            input_file_name, "main", options.options_);
603
13.6k
    return PreprocessedSourceCompilationResult(compilation_result);
604
13.6k
  }
605
606
  // Preprocesses the given source GLSL and returns text result.  Like the first
607
  // PreprocessGlsl method but the source is provided as a std::string.
608
  // Options are otherwise similar to the first CompileToSpv method.
609
  PreprocessedSourceCompilationResult PreprocessGlsl(
610
      const std::string& source_text, shaderc_shader_kind shader_kind,
611
0
      const char* input_file_name, const CompileOptions& options) const {
612
0
    return PreprocessGlsl(source_text.data(), source_text.size(), shader_kind,
613
0
                          input_file_name, options);
614
0
  }
615
616
 private:
617
  Compiler(const Compiler&) = delete;
618
  Compiler& operator=(const Compiler& other) = delete;
619
620
  shaderc_compiler_t compiler_;
621
};
622
}  // namespace shaderc
623
624
#endif  // SHADERC_SHADERC_HPP_