8#if defined(EVE_HAS_SHADERC)
9#include <shaderc/shaderc.h>
23constexpr std::uint32_t kSpirvMagic = 0x07230203u;
25std::runtime_error compileFailure(
const std::string &
message) {
return std::runtime_error(
message); }
28std::vector<std::uint32_t> spirvWords(
const void *data, std::size_t
size) {
29 if (!data ||
size < 4 || (
size % 4) != 0)
30 throw compileFailure(
"compiled SPIR-V has an invalid size");
31 const auto *words =
static_cast<const std::uint32_t *
>(data);
32 if (words[0] != kSpirvMagic)
throw compileFailure(
"compiled SPIR-V has a bad magic number");
33 return std::vector<std::uint32_t>(words, words +
size / 4);
46#if !defined(EVE_HAS_SHADERC)
49const char *stageArgument(
GlslStage stage) {
59std::vector<std::uint32_t> readCompiledFile(
const std::string &
path,
bool remove) {
62 if (fopen_s(&
file,
path.c_str(),
"rb") != 0)
file =
nullptr;
67 if (remove) std::remove(
path.c_str());
68 throw compileFailure(
"failed to open compiled SPIR-V");
70 std::fseek(
file, 0, SEEK_END);
72 std::fseek(
file, 0, SEEK_SET);
73 std::vector<std::uint8_t>
bytes(
static_cast<std::size_t
>(
size > 0 ?
size : 0));
74 const bool shortRead =
75 size > 0 && std::fread(
bytes.data(), 1,
static_cast<std::size_t
>(
size),
file) !=
static_cast<std::size_t
>(
size);
77 if (remove) std::remove(
path.c_str());
78 if (shortRead)
throw compileFailure(
"failed to read compiled SPIR-V");
85std::string findGlslc() {
86 if (
const char *sdk = std::getenv(
"VULKAN_SDK"); sdk && *sdk) {
87 for (
const char *sub : {
"\\bin\\glslc.exe",
"\\Bin\\glslc.exe"}) {
88 std::string
path = std::string(sdk) +
sub;
89 if (GetFileAttributesA(
path.c_str()) != INVALID_FILE_ATTRIBUTES)
return path;
93 WIN32_FIND_DATAA entry{};
94 HANDLE find = FindFirstFileA(
"C:\\VulkanSDK\\*", &entry);
95 if (find != INVALID_HANDLE_VALUE) {
97 if (entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
98 std::string
path = std::string(
"C:\\VulkanSDK\\") + entry.cFileName +
"\\Bin\\glslc.exe";
99 if (GetFileAttributesA(
path.c_str()) != INVALID_FILE_ATTRIBUTES) {
104 }
while (FindNextFileA(find, &entry));
107 FILE *pipe = _popen(
"where glslc 2>nul",
"r");
118 const int status = _pclose(pipe);
131std::vector<std::uint32_t> compileWithExternalGlslc(
const std::string &
source,
GlslStage stage,
132 const std::string &debugName) {
133 const std::string glslc = findGlslc();
135 throw compileFailure(
136 "no GLSL compiler available: this build has no in-process shaderc and no "
137 "glslc.exe was found (install the Vulkan SDK or set VULKAN_SDK)");
139 char tempDir[MAX_PATH];
140 if (GetTempPathA(MAX_PATH, tempDir) == 0)
throw compileFailure(
"GetTempPath failed");
141 char inputPath[MAX_PATH];
142 if (GetTempFileNameA(tempDir,
"eve", 0, inputPath) == 0)
throw compileFailure(
"GetTempFileName failed");
143 const std::string outputPath = std::string(inputPath) +
".spv";
144 const std::string errorPath = outputPath +
".err";
145 struct TemporaryFiles {
147 const std::string &
output;
148 const std::string &
error;
150 DeleteFileA(
error.c_str());
151 DeleteFileA(
output.c_str());
155 } temporary{inputPath, outputPath, errorPath};
158 FILE *
file =
nullptr;
159 if (fopen_s(&
file, inputPath,
"wb") != 0 || !
file)
throw compileFailure(
"failed to write temp GLSL");
163 const bool flushed = fflush(
file) == 0;
164 const bool closed = fclose(
file) == 0;
165 if (written !=
source.size() || !flushed || !closed)
166 throw compileFailure(
"failed to write the complete temp GLSL");
169 std::string command =
"\"" + glslc +
"\" -fshader-stage=" + stageArgument(stage) +
" \"" + inputPath +
170 "\" -o \"" + outputPath +
"\"";
171 STARTUPINFOEXA startup{};
172 startup.StartupInfo.cb =
sizeof(startup);
173 PROCESS_INFORMATION process{};
174 SECURITY_ATTRIBUTES security{};
175 security.nLength =
sizeof(security);
176 security.bInheritHandle = TRUE;
178 CreateFileA(errorPath.c_str(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_DELETE, &security, CREATE_ALWAYS,
179 FILE_ATTRIBUTE_NORMAL,
nullptr);
181 CreateFileA(
"NUL", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, &security, OPEN_EXISTING,
182 FILE_ATTRIBUTE_NORMAL,
nullptr);
183 if (diagnostics == INVALID_HANDLE_VALUE ||
input == INVALID_HANDLE_VALUE) {
184 if (diagnostics != INVALID_HANDLE_VALUE) CloseHandle(diagnostics);
185 if (
input != INVALID_HANDLE_VALUE) CloseHandle(
input);
186 throw compileFailure(
"failed to create compiler standard handles");
188 struct StandardHandles {
194 } handles{diagnostics,
input};
197 SIZE_T attributeBytes = 0;
198 InitializeProcThreadAttributeList(
nullptr, 1, 0, &attributeBytes);
199 std::vector<std::uint8_t>
attributes(attributeBytes);
200 startup.lpAttributeList =
reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST
>(
attributes.data());
201 if (!InitializeProcThreadAttributeList(startup.lpAttributeList, 1, 0, &attributeBytes))
202 throw compileFailure(
"failed to initialize process attributes");
203 struct AttributeList {
204 LPPROC_THREAD_ATTRIBUTE_LIST
value;
205 ~AttributeList() { DeleteProcThreadAttributeList(
value); }
206 } attributeList{startup.lpAttributeList};
207 HANDLE inherited[] = {diagnostics,
input};
208 if (!UpdateProcThreadAttribute(startup.lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, inherited,
209 sizeof(inherited),
nullptr,
nullptr))
210 throw compileFailure(
"failed to restrict compiler handle inheritance");
211 startup.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
212 startup.StartupInfo.hStdInput =
input;
213 startup.StartupInfo.hStdOutput = diagnostics;
214 startup.StartupInfo.hStdError = diagnostics;
215 if (!CreateProcessA(glslc.c_str(), command.data(),
nullptr,
nullptr, TRUE,
216 CREATE_NO_WINDOW | EXTENDED_STARTUPINFO_PRESENT,
nullptr,
nullptr, &startup.StartupInfo,
218 throw compileFailure(
"failed to launch glslc (error " + std::to_string(GetLastError()) +
")");
220 WaitForSingleObject(process.hProcess, INFINITE);
222 GetExitCodeProcess(process.hProcess, &exitCode);
223 CloseHandle(process.hProcess);
224 CloseHandle(process.hThread);
228 CreateFileA(errorPath.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
229 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr);
230 if (reader != INVALID_HANDLE_VALUE) {
237 throw compileFailure(
"glslc failed for " + debugName +
" (exit " + std::to_string(exitCode) +
240 return readCompiledFile(outputPath,
false);
246std::vector<std::uint32_t> compileWithExternalGlslc(
const std::string &
source,
GlslStage stage,
247 const std::string &debugName) {
248 char inputPath[] =
"/tmp/eve_shader_XXXXXX";
250 if (
descriptor < 0)
throw compileFailure(
"mkstemp failed");
251 const std::string outputPath = std::string(inputPath) +
".spv";
255 if (written < 0 ||
static_cast<std::size_t
>(written) !=
source.size()) {
257 throw compileFailure(
"failed to write temp GLSL");
261 const std::string command = std::string(
"exec glslc -fshader-stage=") + stageArgument(stage) +
" \"" + inputPath +
262 "\" -o \"" + outputPath +
"\" 2>&1";
263 FILE *pipe = popen(command.c_str(),
"r");
266 throw compileFailure(
"glslc not available (popen failed)");
271 const int status = pclose(pipe);
272 const int waitError = errno;
275 unlink(outputPath.c_str());
278 outcome =
"wait failed: " + std::string(std::strerror(waitError));
279 else if (WIFSIGNALED(
status))
280 outcome =
"signal " + std::to_string(WTERMSIG(
status));
281 else if (WIFEXITED(
status))
282 outcome =
"exit " + std::to_string(WEXITSTATUS(
status));
284 outcome =
"wait status " + std::to_string(
status);
285 throw compileFailure(
"glslc failed for " + debugName +
" (" + outcome +
"):\n" + errors);
287 return readCompiledFile(outputPath,
true);
291bool externalGlslcAvailable() {
292 FILE *pipe = popen(
"command -v glslc >/dev/null 2>&1",
"r");
293 if (!pipe)
return false;
294 return pclose(pipe) == 0;
301#if defined(EVE_HAS_SHADERC)
302std::vector<std::uint32_t> compileInProcess(
const std::string &
source,
GlslStage stage,
const std::string &debugName) {
303 struct CompilerContext {
304 shaderc_compiler_t
compiler = shaderc_compiler_initialize();
305 shaderc_compile_options_t
options =
nullptr;
307 if (!
compiler)
throw compileFailure(
"shaderc_compiler_initialize failed");
308 options = shaderc_compile_options_initialize();
311 throw compileFailure(
"shaderc_compile_options_initialize failed");
313 shaderc_compile_options_set_target_env(
options, shaderc_target_env_vulkan, shaderc_env_version_vulkan_1_0);
314 shaderc_compile_options_set_target_spirv(
options, shaderc_spirv_version_1_0);
317 shaderc_compile_options_release(
options);
323 thread_local CompilerContext
context;
324 shaderc_shader_kind
kind = shaderc_glsl_vertex_shader;
330 shaderc_compilation_result_t
result = shaderc_compile_into_spv(
332 if (!
result)
throw compileFailure(
"shaderc returned no compilation result");
333 struct CompilationResult {
334 shaderc_compilation_result_t
value;
335 ~CompilationResult() { shaderc_result_release(
value); }
337 if (shaderc_result_get_compilation_status(
result) != shaderc_compilation_status_success)
338 throw compileFailure(
"shaderc failed:\n" + std::string(shaderc_result_get_error_message(
result)));
339 const std::size_t
length = shaderc_result_get_length(
result);
340 return spirvWords(shaderc_result_get_bytes(
result),
length);
347#if defined(EVE_HAS_SHADERC)
351 static const bool available = !findGlslc().empty();
354 static const bool available = externalGlslcAvailable();
360 const std::string &debugName) {
361 if (
source.empty())
throw compileFailure(std::string(
"empty ") + stageName(stage) +
" GLSL source");
362#if defined(EVE_HAS_SHADERC)
363 return compileInProcess(
source, stage, debugName);
365 return compileWithExternalGlslc(
source, stage, debugName);
wgpu::PopErrorScopeStatus status
std::function< void(b3JointId, const Numbers &)> write
onnx_detail::CompilerQueue compiler
std::unique_ptr< gpgpu::GpuBuffer > buffer
std::map< std::string, double > attributes
const SquirrelValueOptions & options
const VegetationPresetContext & context
ssq::Table ownedResult(HSQUIRRELVM vm, Result< T > result)
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
bool glslRuntimeCompilationAvailable()
Whether this build can compile GLSL text to SPIR-V at runtime.
std::vector< std::uint32_t > compileGlslToSpirv(const std::string &source, GlslStage stage, const std::string &debugName)
Compile GLSL text to SPIR-V words.
GlslStage
GLSL stage accepted by compileGlslToSpirv.