Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 45 additions & 21 deletions src/direct_compute_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <map>
#include <memory>
#include <mutex>
#include <thread>
#include <d3d12.h>
#include <wrl/client.h>

Expand Down Expand Up @@ -297,31 +298,54 @@ GuestShaderStage reviewed_graphics_stage(std::string_view raw, GpuStage kind, co
return match_graphics_stage(raw, blob, exact, kind);
}

size_t index_graphics_interfaces(const fs::path &source, const fs::path &cache) {
size_t index_graphics_interfaces(const fs::path &source, const fs::path &cache, unsigned jobs) {
const auto dir = cache / "interfaces" / "library";
std::vector<fs::path> files;
for (const auto &entry : fs::recursive_directory_iterator(source))
if (entry.is_regular_file() && entry.path().extension() == L".cdxil") files.push_back(entry.path());
std::sort(files.begin(), files.end());
// Workers read and publish containers; keys are merged below in file order.
struct Indexed { Bytes id; std::vector<Bytes> keys; std::exception_ptr error; };
std::vector<Indexed> results(files.size());
std::atomic<size_t> next{};
auto work = [&] {
for (size_t i = next++; i < files.size(); i = next++) {
auto &out = results[i];
try {
const auto blob = read_shader_container_bytes(files[i]);
const auto stages = read_guest_shader_container(blob);
out.id = shader_digest(blob);
for (const auto &stage : stages) {
const bool mesh = stage.stage == GpuStage::ms;
if (!mesh && stage.stage != GpuStage::vs && stage.stage != GpuStage::ps) continue;
out.keys.push_back((mesh ? "M" : "F") + shader_digest(stage.code));
if (!stage.exports.empty()) out.keys.push_back("F" + shader_digest(stage.full_precision_code()));
if (embedded_graphics_id(stage.code))
out.keys.push_back((mesh ? "N" : "I") + shader_digest(std::string_view(stage.code).substr(0,24)));
}
if (!out.keys.empty()) publish(dir / (out.id + ".stage"), out.id, blob);
} catch (...) {
out.error = std::current_exception();
}
}
};
const auto workers = (std::min)(static_cast<size_t>((std::max)(jobs, 1u)), files.size());
if (workers <= 1) {
work();
} else {
std::vector<std::thread> threads;
for (size_t w = 0; w < workers; ++w) threads.emplace_back(work);
for (auto &thread : threads) thread.join();
}
InterfaceMap index;
size_t count = 0;
for (const auto &entry : fs::recursive_directory_iterator(source)) {
if (!entry.is_regular_file() || entry.path().extension() != L".cdxil") continue;
auto blob = read_shader_container_bytes(entry.path());
auto stages = read_guest_shader_container(blob);
const auto id = shader_digest(blob);
auto add = [&](const Bytes &key) {
auto [it, fresh] = index.emplace(key,id);
if (!fresh && it->second != id) it->second.assign(64,'-');
};
bool graphics = false;
for (const auto &stage : stages) {
const bool mesh = stage.stage == GpuStage::ms;
if (!mesh && stage.stage != GpuStage::vs && stage.stage != GpuStage::ps) continue;
graphics = true;
add((mesh ? "M" : "F") + shader_digest(stage.code));
if (!stage.exports.empty()) add("F" + shader_digest(stage.full_precision_code()));
if (embedded_graphics_id(stage.code))
add((mesh ? "N" : "I") + shader_digest(std::string_view(stage.code).substr(0,24)));
for (const auto &result : results) {
if (result.error) std::rethrow_exception(result.error);
if (result.keys.empty()) continue;
for (const auto &key : result.keys) {
auto [it, fresh] = index.emplace(key, result.id);
if (!fresh && it->second != result.id) it->second.assign(64,'-');
}
if (!graphics) continue;
publish(dir / (id + ".stage"), id, blob);
++count;
}
if (!count) throw std::runtime_error("no graphics .cdxil containers in interface directory");
Expand Down
4 changes: 3 additions & 1 deletion src/direct_compute_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ ComputePackCounts build_compute_frontend_pack(const std::filesystem::path &reque
unsigned entry, size_t part_limit = compute_pack_part_limit);
// Part n of the pack whose part 0 is file: compute-0.pack -> compute-0.<n>.pack.
std::filesystem::path compute_pack_part(const std::filesystem::path &file, unsigned n);
size_t index_graphics_interfaces(const std::filesystem::path &source, const std::filesystem::path &cache);
// jobs: worker threads (1 = in this thread). The catalog does not depend on it.
size_t index_graphics_interfaces(const std::filesystem::path &source, const std::filesystem::path &cache,
unsigned jobs = 1);
// A logical match identifies the source shader. Only exact matches establish
// the compiled entry ABI, instruction offsets, and resource register layout.
GuestShaderStage reviewed_graphics_stage(std::string_view raw, GpuStage kind,
Expand Down
10 changes: 10 additions & 0 deletions tests/native_graphics_interface_library_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,19 @@ def batch(folder, output, **kwargs):
other = base/"other"
publish(other, words(*prefix,0xbf800000,0xbf800000,*branch,0xf800180f,0,end),vs,root)
batch(other,base/"ambiguous",expected="ambiguous reviewed graphics interfaces")
# Workers do not change the catalog, the ambiguous key included.
def tree(folder):
root = folder / "interfaces" / "library"
return {p.relative_to(root): p.read_bytes() for p in root.rglob("*") if p.is_file()}
run("--index-graphics-interfaces", library, base / "one")
out = run("--index-graphics-interfaces", library, base / "three", "--jobs", "3")
assert "indexed_graphics_containers=3" in out, out
assert tree(base / "one") == tree(base / "three")
run("--index-graphics-interfaces", library, base / "zero", "--jobs", "0", expected="--jobs must be")
# A failed rebuild must leave the published generation usable.
(library/"broken.cdxil").write_bytes(b"bad")
assert subprocess.run([compiler,*install],capture_output=True).returncode
assert subprocess.run([compiler,*install,"--jobs","3"],capture_output=True).returncode
batch(requests,base/"retained")

# Two reflected parameter rows. The fixed PS uses P1/P2 for x
Expand Down
11 changes: 8 additions & 3 deletions tools/startup/compile_dxil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1544,9 +1544,14 @@ int wmain(int argc, wchar_t** argv) {
return 1;
}
}
if (argc == 4 && std::wstring_view(argv[1]) == L"--index-graphics-interfaces") {
if (argc > 1 && std::wstring_view(argv[1]) == L"--index-graphics-interfaces") {
if (argc != 4 && !(argc == 6 && std::wstring_view(argv[4]) == L"--jobs")) {
std::cerr << "usage: compile_dxil --index-graphics-interfaces REVIEWED_DIRECTORY CACHE_DIRECTORY [--jobs N]\n";
return 2;
}
try {
const auto stages = x2w::index_graphics_interfaces(argv[2],argv[3]);
const unsigned jobs = argc == 6 ? static_cast<unsigned>(parse_jobs_count(argv[5])) : 1u;
const auto stages = x2w::index_graphics_interfaces(argv[2],argv[3],jobs);
std::cout << "indexed_graphics_containers=" << stages << "\n";
return 0;
} catch (const std::exception &error) {
Expand Down Expand Up @@ -1575,7 +1580,7 @@ int wmain(int argc, wchar_t** argv) {
"Use --refusals REQUEST_DIRECTORY [--jobs N] [--json] [--mesh-layout FILE] for a read-only refusal summary.\n"
"Use --watch SDK_DIRECTORY CACHE_DIRECTORY REQUEST_DIRECTORY [--jobs N] [--interval MS] [--once] to translate pending requests.\n"
"Use --rekey OLD_CACHE_DIR NEW_REQUEST_DIR [--jobs N] to copy requests under the current cache revision keys.\n"
"Use --index-graphics-interfaces REVIEWED_DIRECTORY CACHE_DIRECTORY to index native graphics interfaces.\n"
"Use --index-graphics-interfaces REVIEWED_DIRECTORY CACHE_DIRECTORY [--jobs N] to index native graphics interfaces.\n"
"Use --prepare-compute-pack REQUEST_DIRECTORY CACHE_DIRECTORY ENTRY_BYTE_OFFSET for captured compute.\n";
return 2;
}
Expand Down
Loading