Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/shaderc
Path: blob/main/glslc/src/file_compiler.h
1560 views
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 GLSLC_FILE_COMPILER_H
16
#define GLSLC_FILE_COMPILER_H
17
18
#include <string>
19
20
#include "libshaderc_util/file_finder.h"
21
#include "libshaderc_util/string_piece.h"
22
#include "shaderc/shaderc.hpp"
23
24
#include "dependency_info.h"
25
26
namespace glslc {
27
28
// Describes an input file to be compiled.
29
struct InputFileSpec {
30
std::string name;
31
shaderc_shader_kind stage;
32
shaderc_source_language language;
33
std::string entry_point_name;
34
};
35
36
// Context for managing compilation of source GLSL files into destination
37
// SPIR-V files or preprocessed output.
38
class FileCompiler {
39
public:
40
enum class SpirvBinaryEmissionFormat {
41
Unspecified, // No binary output format specified, this is the only valid
42
// option when the compilation output is not in SPIR-V binary
43
// code form.
44
Binary, // Emits SPIR-V binary code directly.
45
Numbers, // Emits SPIR-V binary code as a list of hex numbers.
46
CInitList, // Emits SPIR-V binary code as a C-style initializer list
47
// of hex numbers.
48
WGSL, // Emits SPIR-V module converted to WGSL source text.
49
// Requires a build with Tint support.
50
};
51
52
FileCompiler()
53
: output_type_(OutputType::SpirvBinary),
54
binary_emission_format_(SpirvBinaryEmissionFormat::Unspecified),
55
needs_linking_(true),
56
total_warnings_(0),
57
total_errors_(0) {}
58
59
// Compiles a shader received as specified by input_file, returning true
60
// on success and false otherwise. If force_shader_stage is not
61
// shaderc_glsl_infer_source or any default shader stage then the given
62
// shader_stage will be used, otherwise it will be determined from the source
63
// or the file type.
64
//
65
// Places the compilation output into a new file whose name is derived from
66
// input_file according to the rules from glslc/README.asciidoc.
67
//
68
// If version/profile has been forced, the shader's version/profile is set to
69
// that value regardless of the #version directive in the source code.
70
//
71
// Any errors/warnings found in the shader source will be output to std::cerr
72
// and increment the counts reported by OutputMessages().
73
bool CompileShaderFile(const InputFileSpec& input_file);
74
75
// Adds a directory to be searched when processing #include directives.
76
//
77
// Best practice: if you add an empty string before any other path, that will
78
// correctly resolve both absolute paths and paths relative to the current
79
// working directory.
80
void AddIncludeDirectory(const std::string& path);
81
82
// Sets the output filename. A name of "-" indicates standard output.
83
void SetOutputFileName(const shaderc_util::string_piece& file) {
84
output_file_name_ = file;
85
}
86
87
// Sets the format for SPIR-V binary compilation output.
88
void SetSpirvBinaryOutputFormat(SpirvBinaryEmissionFormat format) {
89
binary_emission_format_ = format;
90
}
91
92
// Returns false if any options are incompatible. The num_files parameter
93
// represents the number of files that will be compiled.
94
bool ValidateOptions(size_t num_files);
95
96
// Outputs to std::cerr the number of warnings and errors if there are any.
97
void OutputMessages();
98
99
// Sets the flag to indicate individual compilation mode. In this mode, all
100
// files are compiled individually and written to separate output files
101
// instead of linked together. This method also disables linking and sets the
102
// output file extension to ".spv". Disassembly mode and preprocessing only
103
// mode override this mode and flags.
104
void SetIndividualCompilationFlag();
105
106
// Sets the flag to indicate disassembly mode. In this mode, the compiler
107
// emits disassembled textual output, instead of outputting object files.
108
// This method also sets the output file extension to ".spvasm" and disables
109
// linking. This mode overrides individual compilation mode, and preprocessing
110
// only mode overrides this mode.
111
void SetDisassemblyFlag();
112
113
// Sets the flag to indicate preprocessing only mode. In this mode, instead of
114
// outputting object files, the compiler emits the preprocessed source files.
115
// This method disables linking and sets the output file to stdout. This mode
116
// overrides disassembly mode and individual compilation mode.
117
void SetPreprocessingOnlyFlag();
118
119
// Gets the reference of the compiler options which reflects the command-line
120
// arguments.
121
shaderc::CompileOptions& options() { return options_; }
122
123
// Gets a pointer which points to the dependency info dumping hander. Creates
124
// such a handler if such one does not exist.
125
DependencyInfoDumpingHandler* GetDependencyDumpingHandler() {
126
if (!dependency_info_dumping_handler_) {
127
dependency_info_dumping_handler_.reset(
128
new DependencyInfoDumpingHandler());
129
}
130
return dependency_info_dumping_handler_.get();
131
}
132
133
private:
134
enum class OutputType {
135
SpirvBinary, // A binary module, as defined by the SPIR-V specification.
136
SpirvAssemblyText, // Assembly syntax defined by the SPIRV-Tools project.
137
PreprocessedText, // Preprocessed source code.
138
};
139
140
// Emits the compilation output from the given result to the given output
141
// file and returns true if the result represents a successful compilation
142
// step. Otherwise returns false, possibly emits messages to the standard
143
// error stream, and does not produce an output file. Accumulates error
144
// and warning counts for use by the OutputMessages() method.
145
template <typename CompilationResultType>
146
bool EmitCompiledResult(
147
const CompilationResultType& result, const std::string& input_file_name,
148
const std::string& output_file_name,
149
shaderc_util::string_piece error_file_name,
150
const std::unordered_set<std::string>& used_source_files);
151
152
// Returns the final file name to be used for the output file.
153
//
154
// If an output file name is specified by the SetOutputFileName(), use that
155
// argument as the final output file name.
156
//
157
// If the user did not specify an output filename:
158
// If linking is not required, and the input filename has a
159
// standard stage extension (e.g. .vert) then returns the input filename
160
// without directory names but with the result extenstion (e.g. .spv or
161
// .spvasm) appended.
162
//
163
// If linking is not required, and the input file name does not have a
164
// standard stage extension, then also returns the directory-stripped input
165
// filename, but replaces its extension with the result extension. (If the
166
// resolved input filename does not have an extension, then appends the
167
// result extension.)
168
//
169
// If linking is required and output filename is not specified, returns
170
// "a.spv".
171
std::string GetOutputFileName(std::string input_filename);
172
173
// Returns the candidate output file name deduced from input file name and
174
// user specified output file name. It is computed as follows:
175
//
176
// If the user did specify an output filename and the compiler is not in
177
// preprocessing-only mode, then returns that file name.
178
//
179
// If the user did not specify an output filename:
180
// If the input filename has a standard stage extension (e.g. .vert) then
181
// returns the input filename without directory names but with the result
182
// extenstion (e.g. .spv or .spvasm) appended.
183
//
184
// If the input file name does not have a standard stage extension, then also
185
// returns the directory-stripped input filename, but replaces its extension
186
// with the result extension. (If the resolved input filename does not have
187
// an extension, then appends the result extension.)
188
//
189
// When a resolved extension is not available because the compiler is in
190
// preprocessing-only mode or the compilation requires linking, use .spv as
191
// the extension.
192
std::string GetCandidateOutputFileName(std::string input_filename);
193
194
// Returns true if the compiler's output is preprocessed text.
195
bool PreprocessingOnly() {
196
return output_type_ == OutputType::PreprocessedText;
197
}
198
199
// Performs actual SPIR-V compilation on the contents of input files.
200
shaderc::Compiler compiler_;
201
202
// Reflects the command-line arguments and goes into
203
// compiler_.CompileGlslToSpv().
204
shaderc::CompileOptions options_;
205
206
// What kind of output will be produced?
207
OutputType output_type_;
208
209
// The Flag to indicate to which format the output SPIR-V binary code should
210
// be emitted.
211
SpirvBinaryEmissionFormat binary_emission_format_;
212
213
// A FileFinder used to substitute #include directives in the source code.
214
shaderc_util::FileFinder include_file_finder_;
215
216
// Indicates whether linking is needed to generate the final output.
217
bool needs_linking_;
218
219
// The ownership of dependency dumping handler.
220
std::unique_ptr<DependencyInfoDumpingHandler>
221
dependency_info_dumping_handler_ = nullptr;
222
223
// Reflects the type of file being generated.
224
std::string file_extension_;
225
// Name of the file where the compilation output will go.
226
shaderc_util::string_piece output_file_name_;
227
228
// Counts warnings encountered in all compilations via this object.
229
size_t total_warnings_;
230
// Counts errors encountered in all compilations via this object.
231
size_t total_errors_;
232
};
233
} // namespace glslc
234
#endif // GLSLC_FILE_COMPILER_H
235
236