Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/shaderc
Path: blob/main/examples/online-compile/main.cc
1560 views
1
// Copyright 2016 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
// The program demonstrates basic shader compilation using the Shaderc C++ API.
16
// For clarity, each method is deliberately self-contained.
17
//
18
// Techniques demonstrated:
19
// - Preprocessing GLSL source text
20
// - Compiling a shader to SPIR-V assembly text
21
// - Compliing a shader to a SPIR-V binary module
22
// - Performing optimization with compilation
23
// - Setting basic options: setting a preprocessor symbol.
24
// - Checking compilation status and extracting an error message.
25
26
#include <cstring>
27
#include <iostream>
28
#include <string>
29
#include <vector>
30
31
#include <shaderc/shaderc.hpp>
32
33
// Returns GLSL shader source text after preprocessing.
34
std::string preprocess_shader(const std::string& source_name,
35
shaderc_shader_kind kind,
36
const std::string& source) {
37
shaderc::Compiler compiler;
38
shaderc::CompileOptions options;
39
40
// Like -DMY_DEFINE=1
41
options.AddMacroDefinition("MY_DEFINE", "1");
42
43
shaderc::PreprocessedSourceCompilationResult result =
44
compiler.PreprocessGlsl(source, kind, source_name.c_str(), options);
45
46
if (result.GetCompilationStatus() != shaderc_compilation_status_success) {
47
std::cerr << result.GetErrorMessage();
48
return "";
49
}
50
51
return {result.cbegin(), result.cend()};
52
}
53
54
// Compiles a shader to SPIR-V assembly. Returns the assembly text
55
// as a string.
56
std::string compile_file_to_assembly(const std::string& source_name,
57
shaderc_shader_kind kind,
58
const std::string& source,
59
bool optimize = false) {
60
shaderc::Compiler compiler;
61
shaderc::CompileOptions options;
62
63
// Like -DMY_DEFINE=1
64
options.AddMacroDefinition("MY_DEFINE", "1");
65
if (optimize) options.SetOptimizationLevel(shaderc_optimization_level_size);
66
67
shaderc::AssemblyCompilationResult result = compiler.CompileGlslToSpvAssembly(
68
source, kind, source_name.c_str(), options);
69
70
if (result.GetCompilationStatus() != shaderc_compilation_status_success) {
71
std::cerr << result.GetErrorMessage();
72
return "";
73
}
74
75
return {result.cbegin(), result.cend()};
76
}
77
78
// Compiles a shader to a SPIR-V binary. Returns the binary as
79
// a vector of 32-bit words.
80
std::vector<uint32_t> compile_file(const std::string& source_name,
81
shaderc_shader_kind kind,
82
const std::string& source,
83
bool optimize = false) {
84
shaderc::Compiler compiler;
85
shaderc::CompileOptions options;
86
87
// Like -DMY_DEFINE=1
88
options.AddMacroDefinition("MY_DEFINE", "1");
89
if (optimize) options.SetOptimizationLevel(shaderc_optimization_level_size);
90
91
shaderc::SpvCompilationResult module =
92
compiler.CompileGlslToSpv(source, kind, source_name.c_str(), options);
93
94
if (module.GetCompilationStatus() != shaderc_compilation_status_success) {
95
std::cerr << module.GetErrorMessage();
96
return std::vector<uint32_t>();
97
}
98
99
return {module.cbegin(), module.cend()};
100
}
101
102
int main() {
103
const char kShaderSource[] =
104
"#version 310 es\n"
105
"void main() { int x = MY_DEFINE; }\n";
106
107
{ // Preprocessing
108
auto preprocessed = preprocess_shader(
109
"shader_src", shaderc_glsl_vertex_shader, kShaderSource);
110
std::cout << "Compiled a vertex shader resulting in preprocessed text:"
111
<< std::endl
112
<< preprocessed << std::endl;
113
}
114
115
{ // Compiling
116
auto assembly = compile_file_to_assembly(
117
"shader_src", shaderc_glsl_vertex_shader, kShaderSource);
118
std::cout << "SPIR-V assembly:" << std::endl << assembly << std::endl;
119
120
auto spirv =
121
compile_file("shader_src", shaderc_glsl_vertex_shader, kShaderSource);
122
std::cout << "Compiled to a binary module with " << spirv.size()
123
<< " words." << std::endl;
124
}
125
126
{ // Compiling with optimizing
127
auto assembly =
128
compile_file_to_assembly("shader_src", shaderc_glsl_vertex_shader,
129
kShaderSource, /* optimize = */ true);
130
std::cout << "Optimized SPIR-V assembly:" << std::endl
131
<< assembly << std::endl;
132
133
auto spirv = compile_file("shader_src", shaderc_glsl_vertex_shader,
134
kShaderSource, /* optimize = */ true);
135
std::cout << "Compiled to an optimized binary module with " << spirv.size()
136
<< " words." << std::endl;
137
}
138
139
{ // Error case
140
const char kBadShaderSource[] =
141
"#version 310 es\nint main() { int main_should_be_void; }\n";
142
143
std::cout << std::endl << "Compiling a bad shader:" << std::endl;
144
compile_file("bad_src", shaderc_glsl_vertex_shader, kBadShaderSource);
145
}
146
147
{ // Compile using the C API.
148
std::cout << "\n\nCompiling with the C API" << std::endl;
149
150
// The first example has a compilation problem. The second does not.
151
const char source[2][80] = {"void main() {}", "#version 450\nvoid main() {}"};
152
153
shaderc_compiler_t compiler = shaderc_compiler_initialize();
154
for (int i = 0; i < 2; ++i) {
155
std::cout << " Source is:\n---\n" << source[i] << "\n---\n";
156
shaderc_compilation_result_t result = shaderc_compile_into_spv(
157
compiler, source[i], std::strlen(source[i]), shaderc_glsl_vertex_shader,
158
"main.vert", "main", nullptr);
159
auto status = shaderc_result_get_compilation_status(result);
160
std::cout << " Result code " << int(status) << std::endl;
161
if (status != shaderc_compilation_status_success) {
162
std::cout << "error: " << shaderc_result_get_error_message(result)
163
<< std::endl;
164
}
165
shaderc_result_release(result);
166
}
167
shaderc_compiler_release(compiler);
168
}
169
170
return 0;
171
}
172
173