This commit is contained in:
parent
988c1821af
commit
df9436c51d
3 changed files with 67 additions and 31 deletions
|
|
@ -417,6 +417,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
|
|||
|
||||
std::string files;
|
||||
std::unordered_set<std::string> libSet;
|
||||
std::unordered_set<std::string> publicFlagSet;
|
||||
std::mutex fileMutex;
|
||||
std::vector<std::thread> depThreads;
|
||||
depThreads.reserve(config.dependencies.size());
|
||||
|
|
@ -477,6 +478,7 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
|
|||
const BuildResult& result = resultFuture.get();
|
||||
fileMutex.lock();
|
||||
for (const std::string& lib : result.libs) libSet.insert(lib);
|
||||
for (const std::string& f : result.publicCompileFlags) publicFlagSet.insert(f);
|
||||
fileMutex.unlock();
|
||||
if (!result.result.empty()) {
|
||||
bool expected = false;
|
||||
|
|
@ -577,10 +579,19 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
|
|||
for(const std::string& flag : config.linkFlags) {
|
||||
buildResult.libs.insert(flag);
|
||||
}
|
||||
// Public compile flags propagated from sub-deps. Add them to this build's
|
||||
// command so config sees the headers its deps expose, and re-publish them
|
||||
// so config's own consumers see them transitively.
|
||||
for (const std::string& flag : publicFlagSet) command += " " + flag;
|
||||
buildResult.publicCompileFlags = std::move(publicFlagSet);
|
||||
fs::file_time_type externalFloor = fs::file_time_type::min();
|
||||
for(const ExternalBuildResult& ext : externalResults) {
|
||||
for(const std::string& flag : ext.compileFlags) {
|
||||
command += " " + flag;
|
||||
// Headers a dep links via ExternalDependency are part of its
|
||||
// public surface (its modules can include them in declarations
|
||||
// visible to consumers), so propagate the -I to consumers.
|
||||
buildResult.publicCompileFlags.insert(flag);
|
||||
}
|
||||
for(const std::string& flag : ext.linkFlags) {
|
||||
buildResult.libs.insert(flag);
|
||||
|
|
@ -1047,6 +1058,11 @@ int Crafter::Run(int argc, char** argv) {
|
|||
// through cmd /c, so the relative-prefixed path makes cmd error
|
||||
// with "'.' is not recognized as an internal or external command".
|
||||
artifact = fs::absolute(artifact);
|
||||
// Run from the artifact's own directory so relative file opens
|
||||
// (shaders, assets copied alongside the exe via cfg.files) resolve
|
||||
// against the bin dir rather than the user's cwd. We exit the
|
||||
// process immediately after, so no cwd restore needed.
|
||||
fs::current_path(dir);
|
||||
return std::system(artifact.string().c_str()) == 0 ? 0 : 1;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -59,65 +59,80 @@ namespace Crafter {
|
|||
std::string Shader::Compile(const fs::path& outputDir) const {
|
||||
EShLanguage glslangType = ToEShLanguage(type);
|
||||
glslang::InitializeProcess();
|
||||
|
||||
// Every error path returns a non-empty string; the caller treats
|
||||
// empty as success (see Crafter.Build-Clang.cpp BuildOnce shader
|
||||
// worker). Prefixing with the source path is what tells the user
|
||||
// which shader actually failed when several compile in parallel.
|
||||
auto fail = [&](std::string_view stage, std::string log) {
|
||||
glslang::FinalizeProcess();
|
||||
std::string out = path.string();
|
||||
out += ": ";
|
||||
out += stage;
|
||||
if (!log.empty()) {
|
||||
out += '\n';
|
||||
out += log;
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
EShMessages messages = static_cast<EShMessages>(EShMsgDefault | EShMsgVulkanRules | EShMsgSpvRules);
|
||||
std::ifstream fileStream(path, std::ios::in | std::ios::binary);
|
||||
if (!fileStream) {
|
||||
throw std::ios_base::failure("Failed to open file: " + path.string());
|
||||
return fail("failed to open shader source", {});
|
||||
}
|
||||
std::ostringstream contents;
|
||||
contents << fileStream.rdbuf();
|
||||
std::string src = contents.str();
|
||||
|
||||
const char *file_name_list[1] = {""};
|
||||
const char *shader_source = reinterpret_cast<const char *>(src.data());
|
||||
std::string pathStr = path.string();
|
||||
const char* file_name_list[1] = { pathStr.c_str() };
|
||||
const char* shader_source = src.data();
|
||||
const int shader_source_len = static_cast<int>(src.size());
|
||||
|
||||
glslang::TShader shader(glslangType);
|
||||
shader.setStringsWithLengthsAndNames(&shader_source, nullptr, file_name_list, 1);
|
||||
shader.setStringsWithLengthsAndNames(&shader_source, &shader_source_len, file_name_list, 1);
|
||||
shader.setEntryPoint(entrypoint.c_str());
|
||||
shader.setSourceEntryPoint(entrypoint.c_str());
|
||||
shader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_4);
|
||||
DirStackFileIncluder includeDir;
|
||||
includeDir.pushExternalLocalDirectory(path.parent_path().generic_string());
|
||||
std::string info_log;
|
||||
if (!shader.parse(GetDefaultResources(), 100, false, messages, includeDir))
|
||||
{
|
||||
info_log = std::string(shader.getInfoLog()) + std::string(shader.getInfoDebugLog());
|
||||
|
||||
if (!shader.parse(GetDefaultResources(), 100, false, messages, includeDir)) {
|
||||
return fail("GLSL parse failed", std::string(shader.getInfoLog()) + shader.getInfoDebugLog());
|
||||
}
|
||||
// Add shader to new program object.
|
||||
|
||||
glslang::TProgram program;
|
||||
program.addShader(&shader);
|
||||
|
||||
// Link program.
|
||||
if (!program.link(messages))
|
||||
{
|
||||
info_log = std::string(program.getInfoLog()) + std::string(program.getInfoDebugLog());
|
||||
}
|
||||
|
||||
// Save any info log that was generated.
|
||||
if (shader.getInfoLog())
|
||||
{
|
||||
info_log += std::string(shader.getInfoLog()) + std::string(shader.getInfoDebugLog());
|
||||
}
|
||||
|
||||
if (program.getInfoLog())
|
||||
{
|
||||
info_log += std::string(program.getInfoLog()) + std::string(program.getInfoDebugLog());
|
||||
if (!program.link(messages)) {
|
||||
return fail("GLSL link failed", std::string(program.getInfoLog()) + program.getInfoDebugLog());
|
||||
}
|
||||
|
||||
glslang::TIntermediate* intermediate = program.getIntermediate(glslangType);
|
||||
if (!intermediate)
|
||||
{
|
||||
info_log += "Failed to get shared intermediate code.";
|
||||
if (!intermediate) {
|
||||
// Defensive: parse+link succeeded above, so this should be
|
||||
// unreachable. If glslang ever changes that contract we'd
|
||||
// rather surface a clear error than dereference null (the
|
||||
// pre-fix bug that masqueraded as a silent SIGSEGV).
|
||||
return fail("glslang produced no intermediate code", {});
|
||||
}
|
||||
|
||||
spv::SpvBuildLogger logger;
|
||||
std::vector<std::uint32_t> spirv;
|
||||
glslang::GlslangToSpv(*intermediate, spirv, &logger);
|
||||
std::string errorLog = logger.getAllMessages();
|
||||
glslang::FinalizeProcess();
|
||||
std::string spvLog = logger.getAllMessages();
|
||||
|
||||
fs::path filename = path.filename().replace_extension("spv");
|
||||
std::ofstream file(outputDir/filename, std::ios::binary);
|
||||
if (!file) {
|
||||
return fail("failed to open SPIR-V output", (outputDir/filename).string());
|
||||
}
|
||||
file.write(reinterpret_cast<const char*>(spirv.data()), spirv.size() * sizeof(std::uint32_t));
|
||||
return errorLog;
|
||||
|
||||
if (!spvLog.empty()) {
|
||||
return fail("SPIR-V codegen reported issues", std::move(spvLog));
|
||||
}
|
||||
glslang::FinalizeProcess();
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue