Add a DXC drop-in: dxc and a dxcompiler library with DXC's COM API

zig build dropin installs the command line as bin/dxc and builds
dxcompiler (libdxcompiler.so / .dylib, dxcompiler.dll) exporting
DxcCreateInstance and DxcCreateInstance2. It implements IDxcCompiler3,
IDxcVersionInfo and IDxcUtils over compile, with DXC's result shapes,
status codes and include-path spellings, so programs that load DXC's
library (SDL_shadercross, for one) run on zdxc unchanged.

compile.Request gains compat_notes, off for the drop-in: its clients
read any diagnostic text as a warning.
This commit is contained in:
Peter Li 2026-11-01 17:59:17 -08:00
parent 31f3293380
commit c6d58c0017
4 changed files with 1189 additions and 1 deletions

View File

@ -75,6 +75,30 @@ pub fn build(b: *std.Build) void {
lib.installHeader(b.path("include/zdxc.h"), "zdxc.h"); lib.installHeader(b.path("include/zdxc.h"), "zdxc.h");
b.installArtifact(lib); b.installArtifact(lib);
// The DXC drop-in: a `dxc` command line and a dxcompiler library with
// DXC's COM entry points (src/dxcapi.zig), for programs that load
// libdxcompiler / dxcompiler.dll themselves.
const dropin_step = b.step("dropin", "Build the DXC drop-in: bin/dxc and the dxcompiler library");
const dxc_name = if (target.result.os.tag == .windows) "dxc.exe" else "dxc";
dropin_step.dependOn(&b.addInstallArtifact(exe, .{ .dest_sub_path = dxc_name }).step);
const dxcompiler = b.addLibrary(.{
.name = "dxcompiler",
.linkage = .dynamic,
.root_module = b.addModule("dxcapi", .{
.root_source_file = b.path("src/dxcapi.zig"),
.target = target,
.optimize = optimize,
// It is loaded into C programs, so it names the libc it uses
// (for TLS, among others) instead of leaving it implied.
.link_libc = target.result.os.tag != .windows,
.imports = &.{.{ .name = "zdxc", .module = mod }},
}),
});
dropin_step.dependOn(&b.addInstallArtifact(dxcompiler, .{}).step);
// (Not installed by default, so a dependent finds it by this name
// rather than with artifact().)
b.addNamedLazyPath("dxcompiler", dxcompiler.getEmittedBin());
const run_step = b.step("run", "Run zdxc"); const run_step = b.step("run", "Run zdxc");
const run_cmd = b.addRunArtifact(exe); const run_cmd = b.addRunArtifact(exe);
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);
@ -86,4 +110,6 @@ pub fn build(b: *std.Build) void {
test_step.dependOn(&b.addRunArtifact(mod_tests).step); test_step.dependOn(&b.addRunArtifact(mod_tests).step);
const exe_tests = b.addTest(.{ .root_module = exe.root_module }); const exe_tests = b.addTest(.{ .root_module = exe.root_module });
test_step.dependOn(&b.addRunArtifact(exe_tests).step); test_step.dependOn(&b.addRunArtifact(exe_tests).step);
const dxcapi_tests = b.addTest(.{ .root_module = dxcompiler.root_module });
test_step.dependOn(&b.addRunArtifact(dxcapi_tests).step);
} }

56
docs/dropin.md Normal file
View File

@ -0,0 +1,56 @@
# The DXC drop-in
Programs that run DXC usually do it in one of two ways: they call the `dxc`
executable, or they load `dxcompiler` (`dxcompiler.dll`,
`libdxcompiler.so`, `libdxcompiler.dylib`) and call `DxcCreateInstance`.
SDL_shadercross, for one, does the second. The `dropin` step builds both on
top of zdxc, so these programs can use zdxc without being changed:
```sh
zig build dropin # zig-out/bin/dxc, zig-out/lib/libdxcompiler.{so,dylib}
zig build dropin -Dtarget=x86_64-windows-gnu -Doptimize=ReleaseFast
# zig-out/bin/dxc.exe, dxcompiler.dll, lib/dxcompiler.lib
```
Put them where the program looks for DXC, in place of DXC's files. There is
no `dxil.dll`: zdxc signs DXIL itself.
- `dxc` is the zdxc command line under DXC's name.
- `dxcompiler` exports `DxcCreateInstance` and `DxcCreateInstance2`, with
`dxcapi.h`'s ABI. On Windows it imports only `kernel32` and `ntdll`. On
other platforms it links the C library and nothing else.
## What the library implements
| Class | Interfaces |
|---|---|
| `CLSID_DxcCompiler` | `IDxcCompiler3` (`Compile`), `IDxcVersionInfo` (reports 1.11) |
| `CLSID_DxcUtils` (also reached as `CLSID_DxcLibrary`) | `IDxcUtils`: blob creation, `LoadFile`, `CreateDefaultIncludeHandler`, `GetBlobAsUtf8` / `GetBlobAsWide`, `GetDxilContainerPart` |
`Compile` takes the same arguments as the command line and follows DXC:
- **Status.** A compile error is a successful call whose status is
`E_FAIL`. A bad option has status `E_INVALIDARG`.
- **Outputs.** The result holds the same outputs DXC's does: the object,
the errors (UTF-8, empty when there are none), the shader hash for DXIL,
the root signature, `-P` text, and the names given with `-Fo`, `-Fi` and
`-Frs`.
- **Includes.** Includes go through the `IDxcIncludeHandler` you pass,
with paths spelled the way DXC spells them (`./inc/common.hlsli`). With no
handler, includes fail, as they do in DXC.
- **Accepted-but-ignored options.** Options that zdxc accepts but does not
act on yet produce no note here. Many clients treat any diagnostic text
as a warning.
Classes and methods that are not implemented return `REGDB_E_CLASSNOTREG`
or `E_NOTIMPL`. These include:
- the legacy `IDxcCompiler` and `IDxcCompiler2` interfaces;
- `Disassemble`;
- `CreateReflection`, `BuildArguments` and `GetPDBContents`;
- the validator, linker, container and PDB classes.
The result has no `DXC_OUT_REFLECTION` output (the reflection container
that `CreateReflection` reads).
Language support is zdxc's own: HLSL 2021, with `-HV 2021` as the only
version.

View File

@ -31,6 +31,10 @@ pub const Request = struct {
/// Fail without -Fo, as the command line does (a library caller takes /// Fail without -Fo, as the command line does (a library caller takes
/// the object from the result instead). /// the object from the result instead).
require_output: bool = false, require_output: bool = false,
/// Note options that are accepted but have no effect yet. (The DXC
/// drop-in leaves them out: its clients read any diagnostic text as a
/// warning.)
compat_notes: bool = true,
}; };
pub const Status = enum(u8) { pub const Status = enum(u8) {
@ -345,7 +349,7 @@ fn parseArgs(ctx: *Ctx, arena: std.mem.Allocator, args: []const []const u8) !?Op
// Like dxc, the last input wins (dxc reads getLastArgValue(OPT_INPUT)). // Like dxc, the last input wins (dxc reads getLastArgValue(OPT_INPUT)).
if (parsed.inputs.len > 0) opts.input = parsed.inputs[parsed.inputs.len - 1]; if (parsed.inputs.len > 0) opts.input = parsed.inputs[parsed.inputs.len - 1];
if (opts.ignored.items.len > 0 and !opts.no_warnings) { if (opts.ignored.items.len > 0 and !opts.no_warnings and ctx.req.compat_notes) {
try ctx.stderr.writeAll("zdxc: note: accepted for compatibility, no effect yet:"); try ctx.stderr.writeAll("zdxc: note: accepted for compatibility, no effect yet:");
for (opts.ignored.items) |sp| try ctx.stderr.print(" {s}", .{sp}); for (opts.ignored.items) |sp| try ctx.stderr.print(" {s}", .{sp});
try ctx.stderr.writeByte('\n'); try ctx.stderr.writeByte('\n');

1102
src/dxcapi.zig Normal file

File diff suppressed because it is too large Load Diff