Coverage Report

Created: 2026-10-17 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/shaderc/libshaderc_util/include/libshaderc_util/compiler.h
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 LIBSHADERC_UTIL_INC_COMPILER_H
16
#define LIBSHADERC_UTIL_INC_COMPILER_H
17
18
#include <array>
19
#include <cassert>
20
#include <functional>
21
#include <mutex>
22
#include <ostream>
23
#include <string>
24
#include <unordered_map>
25
#include <utility>
26
27
#include "counting_includer.h"
28
#include "file_finder.h"
29
#include "glslang/Public/ShaderLang.h"
30
#include "mutex.h"
31
#include "resources.h"
32
#include "string_piece.h"
33
34
// Fix a typo in glslang/Public/ShaderLang.h
35
#define EShTargetClientVersion EshTargetClientVersion
36
37
namespace shaderc_util {
38
39
// To break recursive including. This header is already included in
40
// spirv_tools_wrapper.h, so cannot include spirv_tools_wrapper.h here.
41
enum class PassId;
42
43
// Initializes glslang on creation, and destroys it on completion.
44
// Used to tie gslang process operations to object lifetimes.
45
// Additionally initialization/finalization of glslang is not thread safe, so
46
// synchronizes these operations.
47
class GlslangInitializer {
48
 public:
49
  GlslangInitializer();
50
  ~GlslangInitializer();
51
52
 private:
53
  static unsigned int initialize_count_;
54
55
  // Using a bare pointer here to avoid any global class construction at the
56
  // beginning of the execution.
57
  static std::mutex* glslang_mutex_;
58
};
59
60
// Maps macro names to their definitions.  Stores string_pieces, so the
61
// underlying strings must outlive it.
62
using MacroDictionary = std::unordered_map<std::string, std::string>;
63
64
// Holds all of the state required to compile source GLSL into SPIR-V.
65
class Compiler {
66
 public:
67
  // Source language
68
  enum class SourceLanguage {
69
    GLSL,  // The default
70
    HLSL,
71
  };
72
73
  // Target environment.
74
  enum class TargetEnv {
75
    Vulkan,        // Default to Vulkan 1.0
76
    OpenGL,        // Default to OpenGL 4.5
77
    OpenGLCompat,  // Support removed. Generates error if used.
78
  };
79
80
  // Target environment versions.  These numbers match those used by Glslang.
81
  enum class TargetEnvVersion : uint32_t {
82
    Default = 0,  // Default for the corresponding target environment
83
    // For Vulkan, use numbering scheme from vulkan.h
84
    Vulkan_1_0 = ((1 << 22)),              // Vulkan 1.0
85
    Vulkan_1_1 = ((1 << 22) | (1 << 12)),  // Vulkan 1.1
86
    Vulkan_1_2 = ((1 << 22) | (2 << 12)),  // Vulkan 1.2
87
    Vulkan_1_3 = ((1 << 22) | (3 << 12)),  // Vulkan 1.3
88
    Vulkan_1_4 = ((1 << 22) | (4 << 12)),  // Vulkan 1.4
89
    // For OpenGL, use the numbering from #version in shaders.
90
    OpenGL_4_5 = 450,
91
  };
92
93
  // SPIR-V version.
94
  enum class SpirvVersion : uint32_t {
95
    v1_0 = 0x010000u,
96
    v1_1 = 0x010100u,
97
    v1_2 = 0x010200u,
98
    v1_3 = 0x010300u,
99
    v1_4 = 0x010400u,
100
    v1_5 = 0x010500u,
101
    v1_6 = 0x010600u,
102
  };
103
104
  enum class OutputType {
105
    SpirvBinary,  // A binary module, as defined by the SPIR-V specification.
106
    SpirvAssemblyText,  // Assembly syntax defined by the SPIRV-Tools project.
107
    PreprocessedText,   // Preprocessed source code.
108
  };
109
110
  // Supported optimization levels.
111
  enum class OptimizationLevel {
112
    Zero,         // No optimization.
113
    Size,         // Optimization towards reducing code size.
114
    Performance,  // Optimization towards better performance.
115
  };
116
117
  // Resource limits.  These map to the "max*" fields in
118
  // glslang::TBuiltInResource.
119
  enum class Limit {
120
#define RESOURCE(NAME, FIELD, CNAME) NAME,
121
#include "resources.inc"
122
#undef RESOURCE
123
  };
124
125
  // Types of uniform variables.
126
  enum class UniformKind {
127
    // Image, and image buffer.
128
    Image = 0,
129
    // Pure sampler.
130
    Sampler = 1,
131
    // Sampled texture in GLSL.
132
    // Shader Resource View, for HLSL.  (Read-only image or storage buffer.)
133
    Texture = 2,
134
    // Uniform Buffer Object, or UBO, in GLSL.
135
    // Also a Cbuffer in HLSL.
136
    Buffer = 3,
137
    // Shader Storage Buffer Object, or SSBO
138
    StorageBuffer = 4,
139
    // Uniform Access View, in HLSL.  (Writable storage image or storage
140
    // buffer.)
141
    UnorderedAccessView = 5,
142
  };
143
  enum { kNumUniformKinds = int(UniformKind::UnorderedAccessView) + 1 };
144
145
  // Shader pipeline stage.
146
  // TODO(dneto): Replaces interface uses of EShLanguage with this enum.
147
  enum class Stage {
148
    Vertex,
149
    TessEval,
150
    TessControl,
151
    Geometry,
152
    Fragment,
153
    Compute,
154
    RayGenNV,
155
    IntersectNV,
156
    AnyHitNV,
157
    ClosestHitNV,
158
    MissNV,
159
    CallableNV,
160
    TaskNV,
161
    MeshNV,
162
    StageEnd,
163
  };
164
  enum { kNumStages = int(Stage::StageEnd) };
165
166
  // Returns a std::array of all the Stage values.
167
0
  const std::array<Stage, kNumStages>& stages() const {
168
0
    static std::array<Stage, kNumStages> values{{
169
0
        Stage::Vertex,
170
0
        Stage::TessEval,
171
0
        Stage::TessControl,
172
0
        Stage::Geometry,
173
0
        Stage::Fragment,
174
0
        Stage::Compute,
175
0
        Stage::RayGenNV,
176
0
        Stage::IntersectNV,
177
0
        Stage::AnyHitNV,
178
0
        Stage::ClosestHitNV,
179
0
        Stage::MissNV,
180
0
        Stage::CallableNV,
181
0
        Stage::TaskNV,
182
0
        Stage::MeshNV,
183
0
    }};
184
0
    return values;
185
0
  }
186
187
  // Creates an default compiler instance targeting at Vulkan environment. Uses
188
  // version 110 and no profile specification as the default for GLSL.
189
  Compiler()
190
      // The default version for glsl is 110, or 100 if you are using an es
191
      // profile. But we want to default to a non-es profile.
192
5.12k
      : default_version_(110),
193
5.12k
        default_profile_(ENoProfile),
194
5.12k
        force_version_profile_(false),
195
5.12k
        warnings_as_errors_(false),
196
5.12k
        suppress_warnings_(false),
197
5.12k
        generate_debug_info_(false),
198
5.12k
        generate_nonsemantic_debug_info_(false),
199
5.12k
        generate_nonsemantic_debug_source_(false),
200
5.12k
        enabled_opt_passes_(),
201
5.12k
        target_env_(TargetEnv::Vulkan),
202
5.12k
        target_env_version_(TargetEnvVersion::Default),
203
5.12k
        target_spirv_version_(SpirvVersion::v1_0),
204
5.12k
        target_spirv_version_is_forced_(false),
205
5.12k
        source_language_(SourceLanguage::GLSL),
206
5.12k
        limits_(kDefaultTBuiltInResource),
207
5.12k
        auto_bind_uniforms_(false),
208
5.12k
        auto_combined_image_sampler_(false),
209
        auto_binding_base_(),
210
5.12k
        auto_map_locations_(false),
211
5.12k
        preserve_bindings_(false),
212
5.12k
        hlsl_iomap_(false),
213
5.12k
        hlsl_offsets_(false),
214
5.12k
        hlsl_legalization_enabled_(true),
215
5.12k
        hlsl_functionality1_enabled_(false),
216
5.12k
        hlsl_16bit_types_enabled_(false),
217
5.12k
        invert_y_enabled_(false),
218
5.12k
        nan_clamp_(false),
219
5.12k
        hlsl_explicit_bindings_() {}
220
221
  // Requests that the compiler place debug information into the object code,
222
  // such as identifier names and line numbers.
223
  void SetGenerateDebugInfo();
224
225
  // Requests NonSemantic.Shader.DebugInfo.100 (function/call-site info).
226
  void SetGenerateNonSemanticDebugInfo();
227
228
  // Requests embedded source in the NonSemantic debug info; implies info.
229
  void SetGenerateNonSemanticDebugSource();
230
231
  // Sets the optimization level to the given level. Only the last one takes
232
  // effect if multiple calls of this method exist.
233
  void SetOptimizationLevel(OptimizationLevel level);
234
235
  // Enables or disables HLSL legalization passes.
236
  void EnableHlslLegalization(bool hlsl_legalization_enabled);
237
238
  // Enables or disables extension SPV_GOOGLE_hlsl_functionality1
239
  void EnableHlslFunctionality1(bool enable);
240
241
  // Enables or disables HLSL 16-bit types.
242
  void EnableHlsl16BitTypes(bool enable);
243
244
  // Enables or disables relaxed Vulkan rules.
245
  //
246
  // This allows most OpenGL shaders to compile under Vulkan semantics.
247
  void SetVulkanRulesRelaxed(bool enable);
248
249
  // Enables or disables invert position.Y output in vertex shader.
250
  void EnableInvertY(bool enable);
251
252
  // Sets whether the compiler generates code for max and min builtins which,
253
  // if given a NaN operand, will return the other operand.  Also, the clamp
254
  // builtin will favour the non-NaN operands, as if clamp were implemented
255
  // as a composition of max and min.
256
  void SetNanClamp(bool enable);
257
258
  // When a warning is encountered it treat it as an error.
259
  void SetWarningsAsErrors();
260
261
  // Any warning message generated is suppressed before it is output.
262
  void SetSuppressWarnings();
263
264
  // Adds an implicit macro definition obeyed by subsequent CompileShader()
265
  // calls. The macro and definition should be passed in with their char*
266
  // pointer and their lengths. They can be modified or deleted after this
267
  // function has returned.
268
  void AddMacroDefinition(const char* macro, size_t macro_length,
269
                          const char* definition, size_t definition_length);
270
271
  // Sets the target environment, including version.  The version value should
272
  // be 0 or one of the values from TargetEnvVersion.  The default value maps
273
  // to Vulkan 1.0 if the target environment is Vulkan, and it maps to OpenGL
274
  // 4.5 if the target environment is OpenGL.
275
  void SetTargetEnv(TargetEnv env,
276
                    TargetEnvVersion version = TargetEnvVersion::Default);
277
278
  // Sets the target version of SPIR-V.  The module will use this version
279
  // of SPIR-V.  Defaults to the highest version of SPIR-V required to be
280
  // supported by the target environment.  E.g. default to SPIR-V 1.0 for
281
  // Vulkan 1.0, and SPIR-V 1.3 for Vulkan 1.1.
282
  void SetTargetSpirv(SpirvVersion version);
283
284
  // Sets the souce language.
285
  void SetSourceLanguage(SourceLanguage lang);
286
287
  // Forces (without any verification) the default version and profile for
288
  // subsequent CompileShader() calls.
289
  void SetForcedVersionProfile(int version, EProfile profile);
290
291
  // Sets a resource limit.
292
  void SetLimit(Limit limit, int value);
293
294
  // Returns the current limit.
295
  int GetLimit(Limit limit) const;
296
297
  // Set whether the compiler automatically assigns bindings to
298
  // uniform variables that don't have explicit bindings.
299
1.38k
  void SetAutoBindUniforms(bool auto_bind) { auto_bind_uniforms_ = auto_bind; }
300
301
  // Sets whether the compiler should automatically remove sampler variables
302
  // and convert image variables to combined image-sampler variables.
303
0
  void SetAutoCombinedImageSampler(bool auto_combine) {
304
0
    auto_combined_image_sampler_ = auto_combine;
305
0
  }
306
307
  // Sets the lowest binding number used when automatically assigning bindings
308
  // for uniform resources of the given type, for all shader stages.  The
309
  // default base is zero.
310
0
  void SetAutoBindingBase(UniformKind kind, uint32_t base) {
311
0
    for (auto stage : stages()) {
312
0
      SetAutoBindingBaseForStage(stage, kind, base);
313
0
    }
314
0
  }
315
316
  // Sets the lowest binding number used when automatically assigning bindings
317
  // for uniform resources of the given type for a specific shader stage.  The
318
  // default base is zero.
319
  void SetAutoBindingBaseForStage(Stage stage, UniformKind kind,
320
0
                                  uint32_t base) {
321
0
    auto_binding_base_[static_cast<int>(stage)][static_cast<int>(kind)] = base;
322
0
  }
323
324
  // Sets whether the compiler should preserve all bindings, even when those
325
  // bindings are not used.
326
1.38k
  void SetPreserveBindings(bool preserve_bindings) {
327
1.38k
    preserve_bindings_ = preserve_bindings;
328
1.38k
  }
329
330
0
  void SetMaxIdBound(uint32_t max_id_bound) { max_id_bound_ = max_id_bound; }
331
332
  // Sets whether the compiler automatically assigns locations to
333
  // uniform variables that don't have explicit locations.
334
1.38k
  void SetAutoMapLocations(bool auto_map) { auto_map_locations_ = auto_map; }
335
336
  // Use HLSL IO mapping rules for bindings.  Default is false.
337
0
  void SetHlslIoMapping(bool hlsl_iomap) { hlsl_iomap_ = hlsl_iomap; }
338
339
  // Use HLSL rules for offsets in "transparent" memory.  These allow for
340
  // tighter packing of some combinations of types than standard GLSL packings.
341
1.38k
  void SetHlslOffsets(bool hlsl_offsets) { hlsl_offsets_ = hlsl_offsets; }
342
343
  // Sets an explicit set and binding for the given HLSL register.
344
  void SetHlslRegisterSetAndBinding(const std::string& reg,
345
                                    const std::string& set,
346
0
                                    const std::string& binding) {
347
0
    for (auto stage : stages()) {
348
0
      SetHlslRegisterSetAndBindingForStage(stage, reg, set, binding);
349
0
    }
350
0
  }
351
352
  // Sets an explicit set and binding for the given HLSL register in the given
353
  // shader stage.  For example,
354
  //    SetHlslRegisterSetAndBinding(Stage::Fragment, "t1", "4", "5")
355
  // means register "t1" in a fragment shader should map to binding 5 in set 4.
356
  // (Glslang wants this data as strings, not ints or enums.)  The string data
357
  // is copied.
358
  void SetHlslRegisterSetAndBindingForStage(Stage stage, const std::string& reg,
359
                                            const std::string& set,
360
0
                                            const std::string& binding) {
361
0
    hlsl_explicit_bindings_[static_cast<int>(stage)].push_back(reg);
362
0
    hlsl_explicit_bindings_[static_cast<int>(stage)].push_back(set);
363
0
    hlsl_explicit_bindings_[static_cast<int>(stage)].push_back(binding);
364
0
  }
365
366
  // Compiles the shader source in the input_source_string parameter.
367
  //
368
  // If the forced_shader stage parameter is not EShLangCount then
369
  // the shader is assumed to be of the given stage.
370
  //
371
  // For HLSL compilation, entry_point_name is the null-terminated string for
372
  // the entry point.  For GLSL compilation, entry_point_name is ignored, and
373
  // compilation assumes the entry point is named "main".
374
  //
375
  // The stage_callback function will be called if a shader_stage has
376
  // not been forced and the stage can not be determined
377
  // from the shader text. Any #include directives are parsed with the given
378
  // includer.
379
  //
380
  // The initializer parameter must be a valid GlslangInitializer object.
381
  // Acquire will be called on the initializer and the result will be
382
  // destroyed before the function ends.
383
  //
384
  // The output_type parameter determines what kind of output should be
385
  // produced.
386
  //
387
  // Any error messages are written as if the file name were error_tag.
388
  // Any errors are written to the error_stream parameter.
389
  // total_warnings and total_errors are incremented once for every
390
  // warning or error encountered respectively.
391
  //
392
  // Returns a tuple consisting of three fields. 1) a boolean which is true when
393
  // the compilation succeeded, and false otherwise; 2) a vector of 32-bit words
394
  // which contains the compilation output data, either compiled SPIR-V binary
395
  // code, or the text string generated in preprocessing-only or disassembly
396
  // mode; 3) the size of the output data in bytes. When the output is SPIR-V
397
  // binary code, the size is the number of bytes of valid data in the vector.
398
  // If the output is a text string, the size equals the length of that string.
399
  std::tuple<bool, std::vector<uint32_t>, size_t> Compile(
400
      const string_piece& input_source_string, EShLanguage forced_shader_stage,
401
      const std::string& error_tag, const char* entry_point_name,
402
      const std::function<EShLanguage(std::ostream* error_stream,
403
                                      const string_piece& error_tag)>&
404
          stage_callback,
405
      CountingIncluder& includer, OutputType output_type,
406
      std::ostream* error_stream, size_t* total_warnings,
407
      size_t* total_errors) const;
408
409
0
  static EShMessages GetDefaultRules() {
410
0
    return static_cast<EShMessages>(EShMsgSpvRules | EShMsgVulkanRules |
411
0
                                    EShMsgCascadingErrors);
412
0
  }
413
414
 protected:
415
  // Preprocesses a shader whose filename is filename and content is
416
  // shader_source. If preprocessing is successful, returns true, the
417
  // preprocessed shader, and any warning message as a tuple. Otherwise,
418
  // returns false, an empty string, and error messages as a tuple.
419
  //
420
  // The error_tag parameter is the name to use for outputting errors.
421
  // The shader_source parameter is the input shader's source text.
422
  // The shader_preamble parameter is a context-specific preamble internally
423
  // prepended to shader_text without affecting the validity of its #version
424
  // position.
425
  //
426
  // Any #include directives are processed with the given includer.
427
  //
428
  // If force_version_profile_ is set, the shader's version/profile is forced
429
  // to be default_version_/default_profile_ regardless of the #version
430
  // directive in the source code.
431
  std::tuple<bool, std::string, std::string> PreprocessShader(
432
      const std::string& error_tag, const string_piece& shader_source,
433
      const string_piece& shader_preamble, CountingIncluder& includer) const;
434
435
  // Cleans up the preamble in a given preprocessed shader.
436
  //
437
  // The error_tag parameter is the name to be given for the main file.
438
  // The pound_extension parameter is the #extension directive we prepended to
439
  // the original shader source code via preamble.
440
  // The num_include_directives parameter is the number of #include directives
441
  // appearing in the original shader source code.
442
  // The is_for_next_line means whether the #line sets the line number for the
443
  // next line.
444
  //
445
  // If no #include directive is used in the shader source code, we can safely
446
  // delete the #extension directive we injected via preamble. Otherwise, we
447
  // need to adjust it if there exists a #version directive in the original
448
  // shader source code.
449
  std::string CleanupPreamble(const string_piece& preprocessed_shader,
450
                              const string_piece& error_tag,
451
                              const string_piece& pound_extension,
452
                              int num_include_directives,
453
                              bool is_for_next_line) const;
454
455
  // Determines version and profile from command line, or the source code.
456
  // Returns the decoded version and profile pair on success. Otherwise,
457
  // returns (0, ENoProfile).
458
  std::pair<int, EProfile> DeduceVersionProfile(
459
      const std::string& preprocessed_shader) const;
460
461
  // Determines the shader stage from pragmas embedded in the source text if
462
  // possible. In the returned pair, the glslang EShLanguage is the shader
463
  // stage deduced. If no #pragma directives for shader stage exist, it's
464
  // EShLangCount.  If errors occur, the second element in the pair is the
465
  // error message.  Otherwise, it's an empty string.
466
  std::pair<EShLanguage, std::string> GetShaderStageFromSourceCode(
467
      string_piece filename, const std::string& preprocessed_shader) const;
468
469
  // Determines version and profile from command line, or the source code.
470
  // Returns the decoded version and profile pair on success. Otherwise,
471
  // returns (0, ENoProfile).
472
  std::pair<int, EProfile> DeduceVersionProfile(
473
      const std::string& preprocessed_shader);
474
475
  // Gets version and profile specification from the given preprocessedshader.
476
  // Returns the decoded version and profile pair on success. Otherwise,
477
  // returns (0, ENoProfile).
478
  std::pair<int, EProfile> GetVersionProfileFromSourceCode(
479
      const std::string& preprocessed_shader) const;
480
481
  // Neutralizes any queued kStripDebugInfo pass so requested debug info survives optimization.
482
  void RemoveStripDebugInfoPass();
483
484
  // Version to use when force_version_profile_ is true.
485
  int default_version_;
486
  // Profile to use when force_version_profile_ is true.
487
  EProfile default_profile_;
488
  // When true, use the default version and profile from eponymous data members.
489
  bool force_version_profile_;
490
491
  // Macro definitions that must be available to reference in the shader source.
492
  MacroDictionary predefined_macros_;
493
494
  // When true, treat warnings as errors.
495
  bool warnings_as_errors_;
496
  // Supress warnings when true.
497
  bool suppress_warnings_;
498
499
  // When true, compilation will generate debug info with the binary SPIR-V
500
  // output.
501
  bool generate_debug_info_;
502
503
  // When true, emit NonSemantic.Shader.DebugInfo.100 (glslang -gV).
504
  bool generate_nonsemantic_debug_info_;
505
506
  // When true, also embed source in the NonSemantic debug info (glslang -gVS).
507
  // Only takes effect when generate_nonsemantic_debug_info_ is also true.
508
  bool generate_nonsemantic_debug_source_;
509
510
  // Optimization passes to be applied.
511
  std::vector<PassId> enabled_opt_passes_;
512
513
  // The target environment to compile with. This controls the glslang
514
  // EshMessages bitmask, which determines which dialect of GLSL and which
515
  // SPIR-V codegen semantics are used. This impacts the warning & error
516
  // messages as well as the set of available builtins, as per the
517
  // implementation of glslang.
518
  TargetEnv target_env_;
519
520
  // The version number of the target environment.  The numbering scheme is
521
  // particular to each target environment.  If this is 0, then use a default
522
  // for that particular target environment. See libshaders/shaderc/shaderc.h
523
  // for those defaults.
524
  TargetEnvVersion target_env_version_;
525
526
  // The SPIR-V version to be used for the generated module.  Defaults to 1.0.
527
  SpirvVersion target_spirv_version_;
528
  // True if the user explicitly set the target SPIR-V version.
529
  bool target_spirv_version_is_forced_;
530
531
  // The source language.  Defaults to GLSL.
532
  SourceLanguage source_language_;
533
534
  // The resource limits to be used.
535
  TBuiltInResource limits_;
536
537
  // True if the compiler should automatically bind uniforms that don't
538
  // have explicit bindings.
539
  bool auto_bind_uniforms_;
540
541
  // True if the compiler should automatically remove sampler variables
542
  // and convert image variables to combined image-sampler variables.
543
  bool auto_combined_image_sampler_;
544
545
  // The base binding number per uniform type, per stage, used when
546
  // automatically binding uniforms that don't hzve explicit bindings in the
547
  // shader source. The default is zero.
548
  uint32_t auto_binding_base_[kNumStages][kNumUniformKinds];
549
550
  // True if the compiler should automatically map uniforms that don't
551
  // have explicit locations.
552
  bool auto_map_locations_;
553
554
  // True if the compiler should preserve all bindings, even when unused.
555
  bool preserve_bindings_;
556
557
  uint32_t max_id_bound_ = 0x3FFFFF;
558
559
  // True if the compiler should use HLSL IO mapping rules when compiling HLSL.
560
  bool hlsl_iomap_;
561
562
  // True if the compiler should determine block member offsets using HLSL
563
  // packing rules instead of standard GLSL rules.
564
  bool hlsl_offsets_;
565
566
  // True if the compiler should perform legalization optimization passes if
567
  // source language is HLSL.
568
  bool hlsl_legalization_enabled_;
569
570
  // True if the compiler should support extension
571
  // SPV_GOOGLE_hlsl_functionality1.
572
  bool hlsl_functionality1_enabled_;
573
574
  // True if the compiler should support 16-bit HLSL types.
575
  bool hlsl_16bit_types_enabled_;
576
577
  // True if the compiler should relax Vulkan rules to allow OGL shaders to
578
  // compile.
579
  bool vulkan_rules_relaxed_ = false;
580
581
  // True if the compiler should invert position.Y output in vertex shader.
582
  bool invert_y_enabled_;
583
584
  // True if the compiler generates code for max and min builtins which,
585
  // if given a NaN operand, will return the other operand.  Also, the clamp
586
  // builtin will favour the non-NaN operands, as if clamp were implemented
587
  // as a composition of max and min.
588
  bool nan_clamp_;
589
590
  // A sequence of triples, each triple representing a specific HLSL register
591
  // name, and the set and binding numbers it should be mapped to, but in
592
  // the form of strings.  This is how Glslang wants to consume the data.
593
  std::vector<std::string> hlsl_explicit_bindings_[kNumStages];
594
};
595
596
// Converts a string to a vector of uint32_t by copying the content of a given
597
// string to the vector and returns it. Appends '\0' at the end if extra bytes
598
// are required to complete the last element.
599
std::vector<uint32_t> ConvertStringToVector(const std::string& str);
600
601
// Converts a valid Glslang shader stage value to a Compiler::Stage value.
602
0
inline Compiler::Stage ConvertToStage(EShLanguage stage) {
603
0
  switch (stage) {
604
0
    case EShLangVertex:
605
0
      return Compiler::Stage::Vertex;
606
0
    case EShLangTessControl:
607
0
      return Compiler::Stage::TessEval;
608
0
    case EShLangTessEvaluation:
609
0
      return Compiler::Stage::TessControl;
610
0
    case EShLangGeometry:
611
0
      return Compiler::Stage::Geometry;
612
0
    case EShLangFragment:
613
0
      return Compiler::Stage::Fragment;
614
0
    case EShLangCompute:
615
0
      return Compiler::Stage::Compute;
616
0
    case EShLangRayGenNV:
617
0
      return Compiler::Stage::RayGenNV;
618
0
    case EShLangIntersectNV:
619
0
      return Compiler::Stage::IntersectNV;
620
0
    case EShLangAnyHitNV:
621
0
      return Compiler::Stage::AnyHitNV;
622
0
    case EShLangClosestHitNV:
623
0
      return Compiler::Stage::ClosestHitNV;
624
0
    case EShLangMissNV:
625
0
      return Compiler::Stage::MissNV;
626
0
    case EShLangCallableNV:
627
0
      return Compiler::Stage::CallableNV;
628
0
    case EShLangTaskNV:
629
0
      return Compiler::Stage::TaskNV;
630
0
    case EShLangMeshNV:
631
0
      return Compiler::Stage::MeshNV;
632
0
    default:
633
0
      break;
634
0
  }
635
0
  assert(false && "Invalid case");
636
0
  return Compiler::Stage::Compute;
637
0
}
638
639
// A GlslangClientInfo captures target client version and desired SPIR-V
640
// version.
641
struct GlslangClientInfo {
642
19.6k
  GlslangClientInfo() {}
643
  GlslangClientInfo(const std::string& e, glslang::EShClient c,
644
                    glslang::EShTargetClientVersion cv,
645
                    glslang::EShTargetLanguage l,
646
                    glslang::EShTargetLanguageVersion lv)
647
      : error(e),
648
        client(c),
649
        client_version(cv),
650
        target_language(l),
651
0
        target_language_version(lv) {}
652
653
  std::string error;  // Empty if ok, otherwise contains the error message.
654
  glslang::EShClient client = glslang::EShClientNone;
655
  glslang::EShTargetClientVersion client_version;
656
  glslang::EShTargetLanguage target_language = glslang::EShTargetSpv;
657
  glslang::EShTargetLanguageVersion target_language_version =
658
      glslang::EShTargetSpv_1_0;
659
};
660
661
// Returns the mappings to Glslang client, client version, and SPIR-V version.
662
// Also indicates whether the input values were valid.
663
GlslangClientInfo GetGlslangClientInfo(
664
    const std::string& error_tag,  // Indicates source location, for errors.
665
    shaderc_util::Compiler::TargetEnv env,
666
    shaderc_util::Compiler::TargetEnvVersion env_version,
667
    shaderc_util::Compiler::SpirvVersion spv_version,
668
    bool spv_version_is_forced);
669
670
}  // namespace shaderc_util
671
#endif  // LIBSHADERC_UTIL_INC_COMPILER_H