feat(lang): support assembly sources and shader bundles
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 9s
VoidBuildSystem CI / sanitizers-gcc16 (push) Failing after 8s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 12s
VoidBuildSystem CI / fuzz-clang (push) Failing after 5s
VoidBuildSystem CI / wine-windows-gcc16 (push) Failing after 49s

This commit is contained in:
faumaray committed 2026-12-05 14:14:25 +03:00
1 parent ce041ace07
commit 0c1bf7e5b2
11 files changed
+519 -21

No files matched your search

+267 -12
View File
@@ -115,6 +115,11 @@ bool is_c_source_extension(const std::filesystem::path &p) {
return p.extension().string() == ".c";
}
bool is_asm_source_extension(const std::filesystem::path &p) {
const auto ext = p.extension().string();
return ext == ".s" || ext == ".S"; // `.S` is preprocessed by the compiler driver
}
bool is_cpp_source_extension(const std::filesystem::path &p) {
const auto ext = p.extension().string();
return ext == ".cpp" || ext == ".cc" || ext == ".cxx" || ext == ".c" ||
@@ -333,6 +338,35 @@ ProjectDescription::from_json(std::string_view json_text) {
t.nondeterministic = get_bool(*to, "nondeterministic", false);
t.labels = get_str_array(*to, "labels");
t.resources = get_str_array(*to, "resources");
if (const auto *shv = find_key(*to, "shaders")) {
if (const auto *sharr = std::get_if<JsonVal::Array>(&shv->value)) {
for (const auto &item : *sharr) {
const auto *sho = std::get_if<JsonVal::Object>(&item.value);
if (sho == nullptr) {
continue;
}
ShaderSource shader;
shader.path = get_str(*sho, "path");
if (shader.path.empty()) {
continue;
}
const auto language = get_str(*sho, "language");
shader.language = language == "glsl" ? ShaderLanguage::Glsl
: language == "hlsl" ? ShaderLanguage::Hlsl
: language == "slang" ? ShaderLanguage::Slang
: ShaderLanguage::Auto;
const auto stage = get_str(*sho, "stage");
shader.stage = stage == "vertex" ? ShaderStage::Vertex
: stage == "fragment" ? ShaderStage::Fragment
: stage == "compute" ? ShaderStage::Compute
: stage == "geometry" ? ShaderStage::Geometry
: stage == "tess_control" ? ShaderStage::TessControl
: stage == "tess_eval" ? ShaderStage::TessEval
: ShaderStage::Auto;
t.shaders.push_back(std::move(shader));
}
}
}
t.fixture_setup = get_str_array(*to, "fixture_setup");
t.fixture_required = get_str_array(*to, "fixture_required");
t.fixture_cleanup = get_str_array(*to, "fixture_cleanup");
@@ -773,6 +807,223 @@ lower_project_graph(const ProjectDescription &description,
}
}
// 2a. Shader bundle lowering: one compile action per declared shader source.
if (t.kind == TargetKind::CustomAction && !t.shaders.empty()) {
if (!t.command_argv.empty()) {
return std::unexpected{make_error(
std::format("shader bundle '{}' cannot also declare command()", t.name))};
}
const auto stage_name = [](ShaderStage stage) -> std::string {
switch (stage) {
case ShaderStage::Vertex:
return "vertex";
case ShaderStage::Fragment:
return "fragment";
case ShaderStage::Compute:
return "compute";
case ShaderStage::Geometry:
return "geometry";
case ShaderStage::TessControl:
return "tess_control";
case ShaderStage::TessEval:
return "tess_eval";
case ShaderStage::Auto:
return {};
}
return {};
};
const auto glslang_stage = [](ShaderStage stage) -> std::string {
switch (stage) {
case ShaderStage::Vertex:
return "vert";
case ShaderStage::Fragment:
return "frag";
case ShaderStage::Compute:
return "comp";
case ShaderStage::Geometry:
return "geom";
case ShaderStage::TessControl:
return "tesc";
case ShaderStage::TessEval:
return "tese";
default:
return {};
}
};
const auto slang_stage = [](ShaderStage stage) -> std::string {
switch (stage) {
case ShaderStage::Vertex:
return "vertex";
case ShaderStage::Fragment:
return "fragment";
case ShaderStage::Compute:
return "compute";
case ShaderStage::Geometry:
return "geometry";
case ShaderStage::TessControl:
return "hull";
case ShaderStage::TessEval:
return "domain";
default:
return {};
}
};
const auto hlsl_profile = [](ShaderStage stage) -> std::string {
switch (stage) {
case ShaderStage::Vertex:
return "vs_6_0";
case ShaderStage::Fragment:
return "ps_6_0";
case ShaderStage::Compute:
return "cs_6_0";
case ShaderStage::Geometry:
return "gs_6_0";
case ShaderStage::TessControl:
return "hs_6_0";
case ShaderStage::TessEval:
return "ds_6_0";
default:
return {};
}
};
const auto shader_out_root =
graph.profile_build_dir / "shaders" / sanitize_id_component(t.name);
for (const auto &shader : t.shaders) {
const auto abs_src = (canonical_root / shader.path).lexically_normal();
if (!std::filesystem::is_regular_file(abs_src)) {
return std::unexpected{make_error(std::format(
"shader bundle '{}': source '{}' does not exist", t.name, shader.path))};
}
const std::string ext = abs_src.extension().string();
ShaderLanguage language = shader.language;
if (language == ShaderLanguage::Auto) {
language = ext == ".hlsl" || ext == ".fx" ? ShaderLanguage::Hlsl
: ext == ".slang" ? ShaderLanguage::Slang
: ShaderLanguage::Glsl;
}
ShaderStage stage = shader.stage;
if (stage == ShaderStage::Auto) {
stage = ext == ".vert" || ext == ".vs" ? ShaderStage::Vertex
: ext == ".frag" || ext == ".fs" ? ShaderStage::Fragment
: ext == ".comp" || ext == ".cs" ? ShaderStage::Compute
: ext == ".geom" || ext == ".gs" ? ShaderStage::Geometry
: ext == ".tesc" || ext == ".hs" ? ShaderStage::TessControl
: ext == ".tese" || ext == ".ds" ? ShaderStage::TessEval
: ShaderStage::Auto;
}
// Slang can infer the stage from its [shader("...")] attribute.
const bool slang_infers_stage =
stage == ShaderStage::Auto && language == ShaderLanguage::Slang;
if (stage == ShaderStage::Auto && !slang_infers_stage) {
return std::unexpected{make_error(std::format(
"shader bundle '{}': cannot infer the stage of '{}' (use .vert/.frag/"
".comp or pass an explicit stage)",
t.name, shader.path))};
}
const std::string src_hash = Util::sha256_hex(abs_src.string()).substr(0, 12);
const auto out_path =
shader_out_root /
(abs_src.stem().string() + "_" + src_hash +
(stage == ShaderStage::Auto ? std::string{} : "." + stage_name(stage)) +
".spv");
std::vector<std::string> argv;
const auto &tools = graph.toolchain;
if (language == ShaderLanguage::Slang && !tools.slangc.empty()) {
argv = {tools.slangc.string(),
abs_src.string(),
"-target",
"spirv",
"-entry",
"main"};
if (!slang_infers_stage) {
argv.push_back("-stage");
argv.push_back(slang_stage(stage));
}
argv.push_back("-o");
argv.push_back(out_path.string());
} else if (language == ShaderLanguage::Hlsl && !tools.dxc.empty()) {
argv = {tools.dxc.string(),
abs_src.string(),
"-T",
hlsl_profile(stage),
"-E",
"main",
"-spirv",
"-Fo",
out_path.string()};
} else if (!tools.glslang_validator.empty() &&
(language == ShaderLanguage::Glsl || language == ShaderLanguage::Hlsl)) {
argv = {tools.glslang_validator.string(),
"-V",
"-S",
glslang_stage(stage),
"-o",
out_path.string()};
if (language == ShaderLanguage::Hlsl) {
argv.push_back("-D");
argv.push_back("-e");
argv.push_back("main");
}
argv.push_back(abs_src.string());
} else if (!tools.slangc.empty()) {
argv = {tools.slangc.string(),
abs_src.string(),
"-target",
"spirv",
"-entry",
"main",
"-stage",
slang_stage(stage),
"-o",
out_path.string()};
} else {
return std::unexpected{make_error(
std::format("shader bundle '{}': no shader compiler found for '{}' "
"(install glslangValidator, slangc or dxc)",
t.name, shader.path))};
}
ActionNode act;
act.id = "shader:" + sanitize_id_component(t.name) + ":" + src_hash + ":" +
stage_name(stage);
act.kind = ActionKind::Custom;
act.target_name = t.name;
act.executable = argv.front();
act.arguments.assign(argv.begin() + 1, argv.end());
act.working_directory = canonical_root;
if (!t.nondeterministic && !description.profile.nondeterministic) {
act.environment.emplace(
"SOURCE_DATE_EPOCH",
std::to_string(description.profile.reproducibility_epoch));
act.environment.emplace("TZ", "UTC");
act.environment.emplace("LC_ALL", "C");
}
act.dependencies = target_prereq_actions;
act.declared_inputs = {abs_src};
act.declared_outputs = {out_path};
act.logical_source_file = abs_src;
act.physical_source_file = abs_src;
act.logical_output_file = out_path;
act.physical_output_file = out_path;
act.nondeterministic = t.nondeterministic || description.profile.nondeterministic;
act.cacheable = !act.nondeterministic;
if (auto reg = register_output(out_path, act.id); !reg) {
return std::unexpected{reg.error()};
}
act.action_key = compute_action_key(act, graph);
tc.producer_action_ids.push_back(act.id);
tc.exported_prerequisite_actions.push_back(act.id);
summary.action_ids.push_back(act.id);
summary.primary_outputs.push_back(out_path);
graph.action_index_by_id[act.id] = graph.actions.size();
graph.actions.push_back(std::move(act));
}
computed[t.name] = std::move(tc);
graph.targets.push_back(std::move(summary));
continue;
}
// 2. CustomAction lowering.
if (t.kind == TargetKind::CustomAction) {
if (t.command_argv.empty()) {
@@ -1101,7 +1352,7 @@ lower_project_graph(const ProjectDescription &description,
return std::unexpected{found.error()};
}
for (const auto &f : *found) {
if (is_cpp_source_extension(f)) {
if (is_cpp_source_extension(f) || is_asm_source_extension(f)) {
add_source(f);
}
}
@@ -1320,6 +1571,7 @@ lower_project_graph(const ProjectDescription &description,
std::filesystem::path physical_src;
std::vector<std::string> tu_opts;
bool is_c{false};
bool is_asm{false};
bool is_explicit_module{false};
bool has_per_file_override{false};
};
@@ -1335,6 +1587,7 @@ lower_project_graph(const ProjectDescription &description,
cu.is_explicit_module =
explicit_module_sources.contains(src_path) || is_module_source_extension(src_path);
cu.is_c = !cu.is_explicit_module && is_c_source_extension(src_path);
cu.is_asm = !cu.is_explicit_module && is_asm_source_extension(src_path);
for (const auto &pfo : t.per_file_options) {
if (pfo.source_path == src_path.generic_string() ||
pfo.source_path == cu.logical_src.generic_string()) {
@@ -1354,7 +1607,7 @@ lower_project_graph(const ProjectDescription &description,
const auto norm_phys = cu.physical_src.lexically_normal();
all_phys.push_back(norm_phys);
raw_by_phys[norm_phys] = &cu;
if (cu.is_c || cu.is_explicit_module || cu.has_per_file_override) {
if (cu.is_c || cu.is_asm || cu.is_explicit_module || cu.has_per_file_override) {
excluded_phys.push_back(norm_phys);
}
}
@@ -1384,8 +1637,8 @@ lower_project_graph(const ProjectDescription &description,
std::vector<std::string> compile_action_ids;
const bool has_module_sources = std::ranges::any_of(
compile_units, [](const CompileUnitSpec &u) { return u.is_explicit_module; });
const bool has_cpp_sources =
std::ranges::any_of(compile_units, [](const CompileUnitSpec &u) { return !u.is_c; });
const bool has_cpp_sources = std::ranges::any_of(
compile_units, [](const CompileUnitSpec &u) { return !u.is_c && !u.is_asm; });
const bool run_module_pipeline =
has_module_sources || (has_cpp_sources && !external_module_bmis.empty());
@@ -1417,16 +1670,18 @@ lower_project_graph(const ProjectDescription &description,
req.defines = effective_defines;
req.compile_options = cu.tu_opts;
req.is_c_source = cu.is_c;
req.is_asm_source = cu.is_asm;
req.c_standard = cu.is_c ? eff_c_std : CStandard::Default;
req.cxx_standard =
cu.is_c ? CxxStandard::Default
: ((!header_unit_bmi_paths.empty() && eff_std == CxxStandard::Default)
? CxxStandard::Cxx20
: eff_std);
cu.is_c || cu.is_asm
? CxxStandard::Default
: ((!header_unit_bmi_paths.empty() && eff_std == CxxStandard::Default)
? CxxStandard::Cxx20
: eff_std);
req.lto = eff_lto;
req.sanitizers = eff_sans;
req.position_independent = pic;
req.precompiled_header = cu.is_c ? std::nullopt : pch_include_prefix;
req.precompiled_header = (cu.is_c || cu.is_asm) ? std::nullopt : pch_include_prefix;
req.prefix_maps = prefix_maps;
req.reproducibility_epoch = description.profile.reproducibility_epoch;
req.nondeterministic = t.nondeterministic || description.profile.nondeterministic;
@@ -1458,7 +1713,7 @@ lower_project_graph(const ProjectDescription &description,
act.response_file = lowered->response_file;
act.response_file_content = std::move(lowered->response_file_content);
act.declared_inputs = {cu.physical_src};
if (!cu.is_c) {
if (!cu.is_c && !cu.is_asm) {
for (const auto &hb : header_unit_bmi_paths) {
act.declared_inputs.push_back(hb);
}
@@ -1484,7 +1739,7 @@ lower_project_graph(const ProjectDescription &description,
act.position_independent = pic;
act.precompiled_header = req.precompiled_header;
act.dependencies = target_prereq_actions;
if (!cu.is_c) {
if (!cu.is_c && !cu.is_asm) {
if (pch_action_id) {
add_unique_str(act.dependencies, *pch_action_id);
}
@@ -1522,7 +1777,7 @@ lower_project_graph(const ProjectDescription &description,
scanned.reserve(compile_units.size());
std::map<std::filesystem::path, const CompileUnitSpec *> cu_by_phys;
for (const auto &cu : compile_units) {
if (cu.is_c) {
if (cu.is_c || cu.is_asm) {
if (auto r = emit_regular_compile_action(cu); !r) {
return std::unexpected{r.error()};
}