Add remaining intrinsics and system values

Intrinsics on both backends: firstbitlow / firstbithigh, f32tof16 /
f16tof32, and / or / select, isnan / isinf / isfinite / isnormal, fmod,
integer dot, any / all on matrices, ddx_fine / ddy_fine, clip (SPIR-V),
IsHelperLane, and the device / group / all memory barriers.

SPIR-V system values: ClipDistance / CullDistance (packed into one
array per direction by semantic index, as dxc), ViewIndex, BaseVertex /
BaseInstance, ViewportIndex / Layer outside geometry shaders, shading
rate, SampleMask, FullyCoveredEXT, FragStencilRefEXT, barycentrics,
conservative depth; DXIL flags for barycentrics and extended command
info.

Intrinsic overloads: all '<>' types of an intrinsic share their
dimensions (scalars splat to the widest), as dxc - f16tof32(uint2) is
a float2. Typed resources of bool are U32.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Peter Li 2026-10-31 13:25:13 -07:00
parent e77b648314
commit 33f8315e12
12 changed files with 451 additions and 43 deletions

View File

@ -164,6 +164,8 @@ const Emitter = struct {
uses_typed_uav_load: bool = false,
uses_inner_coverage: bool = false,
uses_resource_heap: bool = false,
uses_extended_command_info: bool = false,
uses_barycentrics: bool = false,
uses_sampler_heap: bool = false,
sig_in: []signature.Element = &.{},
@ -593,7 +595,7 @@ const Emitter = struct {
.int16 => 2,
.uint16 => 3,
.float16 => 8,
.bool => 1,
.bool => 5, // stored as U32
.int64 => 6,
.uint64 => 7,
.double => 10,
@ -1573,6 +1575,60 @@ const Emitter = struct {
}
return .{ .scalars = out };
}
if (eq(u8, name, "IsHelperLane")) {
const i1t = try e.dm.intType(1);
const i32t = try e.dm.intType(32);
const fn_i = try e.dxOp("dx.op.isHelperLane.i1", i1t, &.{i32t}, .nounwind_readonly);
out[0] = try e.callOp(fn_i, i1t, &.{try e.i32Const(221)});
return .{ .scalars = out };
}
if (eq(u8, name, "firstbitlow") or eq(u8, name, "firstbithigh")) {
const i32t = try e.dm.intType(32);
const bits: i64 = @as(i64, ac.size()) * 8;
for (out, 0..) |*o, k| {
const op: i64 = if (eq(u8, name, "firstbitlow")) 32 else if (ac.isUnsigned()) 33 else 34;
const fn_i = try e.dxOp(try std.fmt.allocPrint(e.arena, "dx.op.unaryBits.{s}", .{e.overloadName(at)}), i32t, &.{ i32t, at }, .nounwind_readnone);
const r = try e.callOp(fn_i, i32t, &.{ try e.i32Const(op), a[0][k] });
if (op == 32) {
o.* = r;
continue;
}
// FirstbitHi counts from the top bit; HLSL from bit 0.
const flipped = try e.add(i32t, .{ .binop = .{ .op = .sub, .lhs = try e.i32Const(bits - 1), .rhs = r } });
const none = try e.add(try e.dm.intType(1), .{ .cmp = .{ .pred = 32, .lhs = r, .rhs = try e.i32Const(-1) } });
o.* = try e.select(none, try e.i32Const(-1), flipped, i32t);
}
return .{ .scalars = out };
}
if (eq(u8, name, "f32tof16") or eq(u8, name, "f16tof32")) {
const i32t = try e.dm.intType(32);
const ft = try e.dm.getType(.float);
const to16 = eq(u8, name, "f32tof16");
const fn_i = if (to16)
try e.dxOp("dx.op.legacyF32ToF16", i32t, &.{ i32t, ft }, .nounwind_readnone)
else
try e.dxOp("dx.op.legacyF16ToF32", ft, &.{ i32t, i32t }, .nounwind_readnone);
for (out, 0..) |*o, k| o.* = try e.callOp(fn_i, if (to16) i32t else ft, &.{ try e.i32Const(if (to16) 130 else 131), a[0][k] });
return .{ .scalars = out };
}
if (eq(u8, name, "and") or eq(u8, name, "or")) {
const i1t = try e.dm.intType(1);
for (out, 0..) |*o, k| {
const x = try e.convertScalar(a[0][if (a[0].len == 1) 0 else k], e.compOf(frame.f.inst(args[0]).ty), .bool);
const y = try e.convertScalar(a[1][if (a[1].len == 1) 0 else k], e.compOf(frame.f.inst(args[1]).ty), .bool);
o.* = try e.add(i1t, .{ .binop = .{ .op = if (eq(u8, name, "and")) .@"and" else .@"or", .lhs = x, .rhs = y } });
}
return .{ .scalars = out };
}
if (eq(u8, name, "select") and args.len == 3) {
for (out, 0..) |*o, k| {
const c = try e.convertScalar(a[0][if (a[0].len == 1) 0 else k], ac, .bool);
const tv = a[1][if (a[1].len == 1) 0 else k];
const fv = a[2][if (a[2].len == 1) 0 else k];
o.* = try e.select(c, tv, fv, try e.valueType(tv));
}
return .{ .scalars = out };
}
if (eq(u8, name, "any") or eq(u8, name, "all")) {
const i1t = try e.dm.intType(1);
var acc: ?Value = null;
@ -1583,10 +1639,17 @@ const Emitter = struct {
out[0] = acc.?;
return .{ .scalars = out };
}
if (eq(u8, name, "GroupMemoryBarrierWithGroupSync") or eq(u8, name, "GroupMemoryBarrier") or eq(u8, name, "AllMemoryBarrierWithGroupSync") or eq(u8, name, "DeviceMemoryBarrierWithGroupSync")) {
const barriers = [_]struct { []const u8, i64 }{
.{ "GroupMemoryBarrierWithGroupSync", 9 }, .{ "GroupMemoryBarrier", 8 },
.{ "AllMemoryBarrierWithGroupSync", 11 }, .{ "AllMemoryBarrier", 10 },
.{ "DeviceMemoryBarrierWithGroupSync", 3 }, .{ "DeviceMemoryBarrier", 2 },
};
const barrier: ?i64 = for (barriers) |b| {
if (eq(u8, name, b[0])) break b[1];
} else null;
if (barrier) |flags| {
// Barrier flags: SyncThreadGroup 1, AccessUAVGlobal 2,
// AccessGroupShared 8 (as dxc).
const flags: i64 = if (eq(u8, name, "GroupMemoryBarrierWithGroupSync")) 9 else if (eq(u8, name, "GroupMemoryBarrier")) 8 else if (eq(u8, name, "AllMemoryBarrierWithGroupSync")) 11 else 3;
const i32t = try e.dm.intType(32);
const fn_i = try e.dxOp("dx.op.barrier", try e.voidTy(), &.{ i32t, i32t }, .nounwind);
_ = try e.callOp(fn_i, try e.voidTy(), &.{ try e.i32Const(80), try e.i32Const(flags) });
@ -1915,8 +1978,8 @@ const Emitter = struct {
const real = try e.select(ge, frc, try e.fbin(.sub, neg_zero, frc, at), at);
o.* = try e.fbin(.mul, real, y, at);
}
} else if (eq(u8, name, "isnan") or eq(u8, name, "isinf") or eq(u8, name, "isfinite")) {
const op: i64 = if (eq(u8, name, "isnan")) 8 else if (eq(u8, name, "isinf")) 9 else 10;
} else if (eq(u8, name, "isnan") or eq(u8, name, "isinf") or eq(u8, name, "isfinite") or eq(u8, name, "isnormal")) {
const op: i64 = if (eq(u8, name, "isnan")) 8 else if (eq(u8, name, "isinf")) 9 else if (eq(u8, name, "isfinite")) 10 else 11;
const i1t = try e.dm.intType(1);
const i32t = try e.dm.intType(32);
const fn_i = try e.dxOp(try std.fmt.allocPrint(e.arena, "dx.op.isSpecialFloat.{s}", .{e.overloadName(at)}), i1t, &.{ i32t, at }, .nounwind_readnone);
@ -2859,6 +2922,7 @@ const Emitter = struct {
const shape = try e.sigShape(el.ty, el.row_major);
var comp = try e.sigCompType(el.ty);
if (kind == .is_front_face and comp == .i1) comp = .u32;
if (kind == .barycentrics) e.uses_barycentrics = true;
if (comp == .f16 or comp == .i16 or comp == .u16) e.uses_min_precision = true;
const eff_kind: signature.SemanticKind = if (interp == .arb) .arbitrary else kind;
map[k] = @intCast(out.items.len);
@ -3016,6 +3080,7 @@ const Emitter = struct {
}
if (kind == .view_id) e.uses_view_id = true;
if (kind == .inner_coverage) e.uses_inner_coverage = true;
if (kind == .start_vertex_location or kind == .start_instance_location) e.uses_extended_command_info = true;
if (kind == .domain_location) {
const ft = try e.dm.getType(.float);
const fn_i = try e.dxOp("dx.op.domainLocation.f32", ft, &.{ i32t, try e.dm.intType(8) }, .nounwind_readnone);
@ -3410,6 +3475,8 @@ const Emitter = struct {
};
if (e.uses_view_id) flags |= 1 << 21;
if (e.uses_resource_heap) flags |= 1 << 30;
if (e.uses_barycentrics) flags |= 1 << 22;
if (e.uses_extended_command_info) flags |= 1 << 38;
if (e.uses_sampler_heap) flags |= 1 << 31;
if (e.uses_wave) flags |= 1 << 19;
if (e.uses_inner_coverage) flags |= 1 << 10;
@ -3423,6 +3490,7 @@ const Emitter = struct {
.{ 1 << 11, 0x200 }, .{ 1 << 10, 0x400 }, .{ 1 << 9, 0x2000 }, .{ 1 << 20, 0x8000 },
.{ 1 << 21, 0x10000 }, .{ 1 << 24, 0x80000 }, .{ 1 << 19, 0x4000 }, .{ 1 << 13, 0x800 },
.{ 1 << 30, 0x2000000 }, .{ 1 << 31, 0x4000000 },
.{ 1 << 22, 0x20000 }, .{ 1 << 38, 0x100000000 },
};
var f: u64 = 0;
for (map) |m| if (flags & m[0] != 0) {

View File

@ -122,6 +122,10 @@ const Bindings = struct {
names: [8][]const u8 = undefined,
values: [8]u8 = undefined,
n: usize = 0,
/// The widest shape of the arguments declared `<>`: all such types of
/// an intrinsic share their dimensions (dxc's r / c), scalars
/// splatting to the widest.
any_shape: ?TypeTable.Shape = null,
fn get(b: *const Bindings, name: []const u8) ?u8 {
for (b.names[0..b.n], b.values[0..b.n]) |k, v| if (std.mem.eql(u8, k, name)) return v;
@ -184,6 +188,11 @@ fn matchOne(sema: anytype, it: *const table.Intrinsic, object: ?TypeId, args: []
}
var bindings: Bindings = .{};
for (args[0..it.params.len], 1..) |a, i| {
if (!specs[i].any_layout) continue;
const sh = t.shape(a.ty) orelse continue;
if (bindings.any_shape == null or sh.count() > bindings.any_shape.?.count()) bindings.any_shape = sh;
}
var cost: u32 = 0;
const out_params = try sema.arena.alloc(bool, args.len);
@memset(out_params, false);
@ -273,10 +282,9 @@ fn layoutType(sema: anytype, ls: *const table.Spec, comp: Scalar, group: usize,
// widest one).
var shape: ?TypeTable.Shape = null;
if (group > 0 and group - 1 < args.len) shape = t.shape(args[group - 1].ty);
if (shape == null) {
// Look for any argument in this layout group.
return try t.scalar(comp);
}
// A scalar (or no argument of its own) takes the shared shape.
if (shape == null or shape.?.count() == 1) shape = bindings.any_shape orelse shape;
if (shape == null) return try t.scalar(comp);
return try t.withShape(comp, shape.?);
}
if (ls.dims.len == 0) return try t.scalar(comp);

View File

@ -153,6 +153,8 @@ pub const Op = enum(u16) {
dpdx = 207,
dpdy = 208,
fwidth = 209,
dpdx_fine = 210,
dpdy_fine = 211,
control_barrier = 224,
memory_barrier = 225,
phi = 245,

View File

@ -40,6 +40,16 @@ const Capability = enum(u32) {
image_gather_extended = 25,
storage_image_multisample = 27,
sample_rate_shading = 35,
derivative_control = 51,
clip_distance = 32,
cull_distance = 33,
draw_parameters = 4427,
multi_view = 4439,
fragment_shading_rate_khr = 4422,
stencil_export_ext = 5013,
shader_viewport_index_layer_ext = 5254,
fragment_fully_covered_ext = 5265,
fragment_barycentric_khr = 5284,
geometry_streams = 54,
multi_viewport = 57,
min_lod = 42,
@ -172,9 +182,11 @@ const Emitter = struct {
const Interface = struct {
var_id: Id,
ty: TypeId,
/// TessLevelOuter / TessLevelInner: float[4] / float[2] builtins
/// holding a shorter array (or a scalar), accessed element-wise.
tess_level: bool = false,
/// An array builtin holding the value's components from `base` on
/// (TessLevelOuter / TessLevelInner, ClipDistance / CullDistance
/// shared by several elements, SampleMask), accessed element-wise.
elem_array: bool = false,
base: u32 = 0,
};
const LayoutKey = struct { TypeId, layout.Rules, bool };
const MeshPlace = struct { kind: ir.MeshOutput, index: Ref = .none, offset: u32 = 0, ty: TypeId };
@ -642,9 +654,14 @@ const Emitter = struct {
},
.pixel => {
try e.b.emit(.execution_modes, .execution_mode, &.{ wrapper, 7 }); // OriginUpperLeft
for (entry.outputs.items) |o| if (std.ascii.eqlIgnoreCase(o.semantic, "SV_Depth")) {
try e.b.emit(.execution_modes, .execution_mode, &.{ wrapper, 12 }); // DepthReplacing
};
for (entry.outputs.items) |o| {
const eqi = std.ascii.eqlIgnoreCase;
if (eqi(o.semantic, "SV_Depth") or eqi(o.semantic, "SV_DepthLessEqual") or eqi(o.semantic, "SV_DepthGreaterEqual")) {
try e.b.emit(.execution_modes, .execution_mode, &.{ wrapper, 12 }); // DepthReplacing
}
if (eqi(o.semantic, "SV_DepthLessEqual")) try e.b.emit(.execution_modes, .execution_mode, &.{ wrapper, 15 }); // DepthLess
if (eqi(o.semantic, "SV_DepthGreaterEqual")) try e.b.emit(.execution_modes, .execution_mode, &.{ wrapper, 14 }); // DepthGreater
}
},
else => {},
}
@ -874,6 +891,49 @@ const Emitter = struct {
}
if (stage == .domain and input and eq(n, "SV_DomainLocation")) return .{ .id = 13, .ty = try t.vector(.float, 3) }; // TessCoord
if (stage == .hull and input and eq(n, "SV_OutputControlPointID")) return .{ .id = 8, .ty = uint1 }; // InvocationId
if (input and eq(n, "SV_ViewID")) {
try e.cap(.multi_view);
try e.extension("SPV_KHR_multiview");
return .{ .id = 4440, .ty = uint1 }; // ViewIndex
}
if (stage == .vertex and input and (eq(n, "SV_StartVertexLocation") or eq(n, "SV_StartInstanceLocation"))) {
try e.cap(.draw_parameters);
try e.extension("SPV_KHR_shader_draw_parameters");
return .{ .id = if (eq(n, "SV_StartVertexLocation")) 4424 else 4425, .ty = uint1 }; // BaseVertex / BaseInstance
}
if (!input and (stage == .vertex or stage == .domain) and (eq(n, "SV_ViewportArrayIndex") or eq(n, "SV_RenderTargetArrayIndex"))) {
try e.cap(.shader_viewport_index_layer_ext);
try e.extension("SPV_EXT_shader_viewport_index_layer");
return .{ .id = if (eq(n, "SV_ViewportArrayIndex")) 10 else 9, .ty = uint1 }; // ViewportIndex / Layer
}
if (eq(n, "SV_ShadingRate") and ((input and stage == .pixel) or (!input and (stage == .vertex or stage == .geometry or stage == .mesh)))) {
try e.cap(.fragment_shading_rate_khr);
try e.extension("SPV_KHR_fragment_shading_rate");
return .{ .id = if (input) 4444 else 4432, .ty = uint1 }; // ShadingRateKHR / PrimitiveShadingRateKHR
}
if (stage == .pixel) {
if (eq(n, "SV_Coverage")) return .{ .id = 20, .ty = try t.array(uint1, 1) }; // SampleMask
if (input and eq(n, "SV_InnerCoverage")) {
try e.cap(.fragment_fully_covered_ext);
try e.extension("SPV_EXT_fragment_fully_covered");
return .{ .id = 5264, .ty = try t.scalar(.bool) }; // FullyCoveredEXT
}
if (!input and eq(n, "SV_StencilRef")) {
try e.cap(.stencil_export_ext);
try e.extension("SPV_EXT_shader_stencil_export");
return .{ .id = 5014, .ty = uint1 }; // FragStencilRefEXT
}
if (!input and (eq(n, "SV_DepthLessEqual") or eq(n, "SV_DepthGreaterEqual"))) return .{ .id = 22, .ty = try t.scalar(.float) }; // FragDepth
if (input and eq(n, "SV_Barycentrics")) {
try e.cap(.fragment_barycentric_khr);
try e.extension("SPV_KHR_fragment_shader_barycentric");
return .{ .id = if (el.interp.noperspective) 5287 else 5286, .ty = try t.vector(.float, 3) }; // BaryCoord{NoPersp}KHR
}
}
if (!input and stage == .geometry) {
if (eq(n, "SV_PrimitiveID")) return .{ .id = 7, .ty = uint1 }; // PrimitiveId
if (eq(n, "SV_IsFrontFace")) return null; // a located variable
}
if (eq(n, "SV_Target") or eq(n, "SV_VertexID") or eq(n, "SV_InstanceID") or eq(n, "SV_Position")) return null;
if (el.system_value) return e.unsupported("system value '{s}' is not supported yet", .{n});
return null;
@ -892,7 +952,47 @@ const Emitter = struct {
for (lists, ifaces, 0..) |els, out, li| {
const patch = li == 2;
const input = if (patch) entry.stage == .domain else li == 0;
for (els, out) |el, *iface| {
// ClipDistance / CullDistance: one float array per direction,
// the elements packed in semantic index order (as dxc).
const ClipSlot = struct { kind: u1, base: u32 };
const slots = try e.arena.alloc(?ClipSlot, els.len);
@memset(slots, null);
var clip_vars: [2]Id = .{ 0, 0 };
var totals: [2]u32 = .{ 0, 0 };
for (0..2) |kind| {
var order: std.ArrayList(usize) = .empty;
// (Vertex shader inputs are ordinary located variables.)
if (input and entry.stage == .vertex) break;
for (els, 0..) |el, k| {
const want = if (kind == 0) "SV_ClipDistance" else "SV_CullDistance";
if (std.ascii.eqlIgnoreCase(el.semantic, want)) try order.append(e.arena, k);
}
if (order.items.len == 0) continue;
std.sort.insertion(usize, order.items, els, struct {
fn lt(xs: []const ir.SigElement, a: usize, b: usize) bool {
return xs[a].semantic_index < xs[b].semantic_index;
}
}.lt);
for (order.items) |k| {
if (els[k].per_vertex) return e.unsupported("per-vertex {s} inputs are not supported yet", .{els[k].semantic});
slots[k] = .{ .kind = @intCast(kind), .base = totals[kind] };
totals[kind] += e.t.componentCount(els[k].ty) orelse return e.unsupported("{s} of this type", .{els[k].semantic});
}
try e.cap(if (kind == 0) .clip_distance else .cull_distance);
const arr = try e.t.array(try e.t.scalar(.float), totals[kind]);
const sc: StorageClass = if (input) .input else .output;
const v = e.b.newId();
try e.b.emit(.types_globals, .variable, &.{ try e.pointerType(sc, try e.typeId(arr)), v, @backingInt(sc) });
try e.b.emit(.annotations, .decorate, &.{ v, 11, if (kind == 0) 3 else 4 }); // BuiltIn ClipDistance / CullDistance
try e.b.emitWithString(.debug_names, .name, &.{v}, if (kind == 0) "gl_ClipDistance" else "gl_CullDistance", &.{});
try e.interface_ids.append(e.arena, v);
clip_vars[kind] = v;
}
for (els, out, 0..) |el, *iface, ek| {
if (slots[ek]) |sl| {
iface.* = .{ .var_id = clip_vars[sl.kind], .ty = try e.t.array(try e.t.scalar(.float), totals[sl.kind]), .elem_array = true, .base = sl.base };
continue;
}
const bi = try e.builtinFor(el, input, entry.stage);
// bool is not allowed in located interface variables.
var ty = if (bi) |x| x.ty else if (e.t.component(el.ty) == .bool) try e.t.withComponent(el.ty, .uint) else el.ty;
@ -907,8 +1007,8 @@ const Emitter = struct {
const sc: StorageClass = if (input) .input else .output;
if (bi != null and input and !el.per_vertex) {
// One variable per input builtin, shared by its uses.
iface.* = .{ .var_id = try e.builtinInput(bi.?.id, ty), .ty = vty, .tess_level = bi.?.id == 11 or bi.?.id == 12 };
if (iface.tess_level) try e.b.emit(.annotations, .decorate, &.{ iface.var_id, 15 }); // Patch
iface.* = .{ .var_id = try e.builtinInput(bi.?.id, ty), .ty = vty, .elem_array = e.t.get(ty) == .array };
if (bi.?.id == 11 or bi.?.id == 12) try e.b.emit(.annotations, .decorate, &.{ iface.var_id, 15 }); // Patch
continue;
}
const ptr = try e.pointerType(sc, try e.typeId(ty));
@ -948,7 +1048,7 @@ const Emitter = struct {
}
const name = try std.fmt.allocPrint(e.arena, "{s}.var.{s}{d}", .{ if (input) "in" else "out", el.semantic, el.semantic_index });
try e.b.emitWithString(.debug_names, .name, &.{var_id}, name, &.{});
iface.* = .{ .var_id = var_id, .ty = vty, .tess_level = bi != null and (bi.?.id == 11 or bi.?.id == 12) };
iface.* = .{ .var_id = var_id, .ty = vty, .elem_array = bi != null and e.t.get(bi.?.ty) == .array };
try e.interface_ids.append(e.arena, var_id);
}
}
@ -1665,11 +1765,7 @@ const Emitter = struct {
try e.b.emit(.functions, .store, &.{ ptr, v });
},
.method => |x| e.vals[ri] = try e.method(x.name, x.res, x.args, i.ty),
.input => |k| {
const iface = e.inputs[k];
const loaded = try e.op1(.load, iface.ty, &.{iface.var_id});
e.vals[ri] = try e.reshape(loaded, iface.ty, i.ty);
},
.input => |k| e.vals[ri] = try e.loadInterface(e.inputs[k], .input, i.ty),
.input_vertex => |x| {
const iface = e.inputs[x.element];
const ptr = try e.opRaw(.access_chain, try e.pointerType(.input, try e.typeId(iface.ty)), &.{ iface.var_id, try e.constUint(x.vertex) });
@ -1722,6 +1818,7 @@ const Emitter = struct {
},
.output => |o| {
const iface = e.outputs[o.element];
if (iface.elem_array) return e.storeInterface(iface, f.inst(o.value).ty, e.val(o.value));
const v = try e.reshape(e.val(o.value), f.inst(o.value).ty, iface.ty);
if (e.m.entry.?.outputs.items[o.element].per_vertex) {
// Hull output control points: this invocation's element.
@ -1881,30 +1978,52 @@ const Emitter = struct {
/// Reads an interface variable as a value of type `ty`.
fn loadInterface(e: *Emitter, iface: Interface, sc: StorageClass, ty: TypeId) Error!Id {
if (!iface.tess_level) return e.reshape(try e.op1(.load, iface.ty, &.{iface.var_id}), iface.ty, ty);
const ft = try e.t.scalar(.float);
const n: u32 = if (e.t.get(ty) == .array) e.t.get(ty).array.len else 1;
if (!iface.elem_array) return e.reshape(try e.op1(.load, iface.ty, &.{iface.var_id}), iface.ty, ty);
const et = try e.t.scalar(e.t.component(e.t.get(iface.ty).array.elem).?);
const n = e.t.componentCount(ty).?;
const parts = try e.arena.alloc(Id, n);
for (parts, 0..) |*p, k| {
const ptr = try e.opRaw(.access_chain, try e.pointerType(sc, try e.typeId(ft)), &.{ iface.var_id, try e.constUint(@intCast(k)) });
p.* = try e.op1(.load, ft, &.{ptr});
const ptr = try e.opRaw(.access_chain, try e.pointerType(sc, try e.typeId(et)), &.{ iface.var_id, try e.constUint(iface.base + @as(u32, @intCast(k))) });
p.* = try e.convert(try e.op1(.load, et, &.{ptr}), et, (try e.rawLeafScalar(ty, @intCast(k))));
}
if (e.t.get(ty) != .array) return parts[0];
return e.op1(.composite_construct, ty, parts);
return e.composeFlat(ty, parts);
}
/// The scalar type of flattened component `k` of `ty`.
fn rawLeafScalar(e: *Emitter, ty: TypeId, k: u32) Error!TypeId {
var cur = ty;
var kk = k;
while (true) switch (e.t.get(cur)) {
.scalar, .@"enum" => return cur,
.vector, .matrix => return e.t.scalar(e.t.component(cur).?),
.array => |a| {
kk %= e.t.componentCount(a.elem).?;
cur = a.elem;
},
.@"struct" => |st| for (st.fields.items) |fld| {
const n = e.t.componentCount(fld.type).?;
if (kk < n) {
cur = fld.type;
break;
}
kk -= n;
},
else => return e.unsupported("values of type '{s}' are not supported here yet", .{try e.t.format(e.arena, ty)}),
};
}
/// Writes value `v` of type `ty` to an output interface variable.
fn storeInterface(e: *Emitter, iface: Interface, ty: TypeId, v: Id) Error!void {
if (!iface.tess_level) {
if (!iface.elem_array) {
try e.b.emit(.functions, .store, &.{ iface.var_id, try e.reshape(v, ty, iface.ty) });
return;
}
const ft = try e.t.scalar(.float);
const n: u32 = if (e.t.get(ty) == .array) e.t.get(ty).array.len else 1;
const et = try e.t.scalar(e.t.component(e.t.get(iface.ty).array.elem).?);
const n = e.t.componentCount(ty).?;
for (0..n) |k| {
const ptr = try e.opRaw(.access_chain, try e.pointerType(.output, try e.typeId(ft)), &.{ iface.var_id, try e.constUint(@intCast(k)) });
const x = if (e.t.get(ty) == .array) try e.op1(.composite_extract, ft, &.{ v, @intCast(k) }) else v;
try e.b.emit(.functions, .store, &.{ ptr, x });
const ptr = try e.opRaw(.access_chain, try e.pointerType(.output, try e.typeId(et)), &.{ iface.var_id, try e.constUint(iface.base + @as(u32, @intCast(k))) });
const x = try e.flatComponent(v, ty, @intCast(k));
try e.b.emit(.functions, .store, &.{ ptr, try e.convert(x[0], x[1], et) });
}
}
@ -2904,7 +3023,7 @@ const Emitter = struct {
}
// Component-wise intrinsics on matrices apply row by row (GLSL
// instructions take scalars and vectors).
const whole = [_][]const u8{ "mul", "transpose", "determinant", "any", "all", "length", "dot" };
const whole = [_][]const u8{ "mul", "transpose", "determinant", "any", "all", "length", "dot", "clip" };
const component_wise = for (whole) |w| {
if (std.mem.eql(u8, w, name)) break false;
} else true;
@ -3002,11 +3121,19 @@ const Emitter = struct {
const one = try e.splatConst(ty, .{ .float = 1 });
return e.op1(.ext_inst, ty, &.{ e.glslSet(), 43, ids[0], zero, one });
}
if (std.mem.eql(u8, name, "dot") and fl) return e.op1(.dot, ty, ids);
if (std.mem.eql(u8, name, "dot") and fl) {
// 1-vectors are SPIR-V scalars.
if (e.isScalarLike(tys[0]) or e.isScalarLike(tys[1])) return e.op1(.f_mul, ty, &.{ try e.convert(ids[0], tys[0], ty), try e.convert(ids[1], tys[1], ty) });
return e.op1(.dot, ty, ids);
}
const deriv = for ([_][]const u8{ "ddx", "ddy", "ddx_coarse", "ddy_coarse", "ddx_fine", "ddy_fine", "fwidth" }) |d| {
if (eqn(u8, name, d)) break true;
} else false;
if (deriv and e.m.entry.?.stage != .pixel) return e.unsupported("derivatives outside pixel shaders are not supported yet", .{});
if (eqn(u8, name, "ddx_fine") or eqn(u8, name, "ddy_fine")) {
try e.cap(.derivative_control);
return e.op1(if (eqn(u8, name, "ddx_fine")) .dpdx_fine else .dpdy_fine, ty, ids);
}
if (std.mem.eql(u8, name, "ddx") or std.mem.eql(u8, name, "ddx_coarse")) return e.op1(.dpdx, ty, ids);
if (std.mem.eql(u8, name, "ddy") or std.mem.eql(u8, name, "ddy_coarse")) return e.op1(.dpdy, ty, ids);
if (std.mem.eql(u8, name, "fwidth")) return e.op1(.fwidth, ty, ids);
@ -3019,10 +3146,136 @@ const Emitter = struct {
const one = try e.splatConst(ty, .{ .float = 1 });
return e.op1(.f_div, ty, &.{ one, ids[0] });
}
if (std.mem.eql(u8, name, "GroupMemoryBarrierWithGroupSync")) {
// Workgroup execution and memory scope, AcquireRelease |
// WorkgroupMemory semantics.
try e.b.emit(.functions, .control_barrier, &.{ try e.constUint(2), try e.constUint(2), try e.constUint(0x108) });
// Barriers: (scope, semantics) as dxc: AcquireRelease with
// workgroup memory (Group), uniform and image memory (Device) or
// all three (All).
const barriers = [_]struct { []const u8, u32, u32, bool }{
.{ "GroupMemoryBarrierWithGroupSync", 2, 0x108, true }, .{ "GroupMemoryBarrier", 2, 0x108, false },
.{ "DeviceMemoryBarrierWithGroupSync", 1, 0x848, true }, .{ "DeviceMemoryBarrier", 1, 0x848, false },
.{ "AllMemoryBarrierWithGroupSync", 1, 0x948, true }, .{ "AllMemoryBarrier", 1, 0x948, false },
};
for (barriers) |b| if (eqn(u8, name, b[0])) {
if (b[3]) {
try e.b.emit(.functions, .control_barrier, &.{ try e.constUint(2), try e.constUint(b[1]), try e.constUint(b[2]) });
} else try e.b.emit(.functions, .memory_barrier, &.{ try e.constUint(b[1]), try e.constUint(b[2]) });
return 0;
};
const bool_ty = try e.t.withComponent(ty, .bool);
if (eqn(u8, name, "any") or eqn(u8, name, "all")) {
const is_any = eqn(u8, name, "any");
const at = tys[0];
if (e.isTrueMatrix(at)) {
// Row by row, combined.
const m = e.t.get(at).matrix;
const rt = try e.t.vector(m.elem, m.cols);
var acc: ?Id = null;
for (0..m.rows) |r| {
const v = try e.intrinsicOn(name, ty, &.{try e.row(ids[0], rt, @intCast(r))}, &.{rt});
acc = if (acc) |a| try e.op1(if (is_any) .logical_or else .logical_and, ty, &.{ a, v }) else v;
}
return acc.?;
}
const b = if (e.compOf(at) == .bool) ids[0] else try e.convert(ids[0], at, try e.t.withComponent(at, .bool));
if (e.isScalarLike(at)) return b;
return e.op1(if (is_any) .any else .all, ty, &.{b});
}
if (eqn(u8, name, "and") or eqn(u8, name, "or")) {
const x = try e.convert(ids[0], tys[0], bool_ty);
const y = try e.convert(ids[1], tys[1], bool_ty);
return e.op1(if (eqn(u8, name, "and")) .logical_and else .logical_or, ty, &.{ x, y });
}
if (eqn(u8, name, "select") and ids.len == 3) {
var c = try e.convert(ids[0], tys[0], try e.t.withComponent(tys[0], .bool));
// A scalar condition over vectors: splatted (before SPIR-V 1.4).
if (e.isScalarLike(tys[0]) and !e.isScalarLike(ty)) {
const n = e.t.componentCount(ty).?;
const copies = try e.arena.alloc(Id, n);
@memset(copies, c);
c = try e.op1(.composite_construct, try e.t.withComponent(ty, .bool), copies);
}
return e.op1(.select, ty, &.{ c, try e.convert(ids[1], tys[1], ty), try e.convert(ids[2], tys[2], ty) });
}
if (eqn(u8, name, "isnan")) return e.op1(.is_nan, ty, ids);
if (eqn(u8, name, "isinf")) return e.op1(.is_inf, ty, ids);
if (eqn(u8, name, "isfinite") or eqn(u8, name, "isnormal")) {
const nan = try e.op1(.is_nan, ty, ids);
const inf = try e.op1(.is_inf, ty, ids);
var r = try e.op1(.logical_not, ty, &.{try e.op1(.logical_or, ty, &.{ nan, inf })});
if (eqn(u8, name, "isnormal")) {
// Not zero or subnormal either.
const at = tys[0];
const min_normal: f64 = switch (e.compOf(at)) {
.float16 => 6.103515625e-05,
.double => 2.2250738585072014e-308,
else => 1.1754943508222875e-38,
};
const abs = try e.op1(.ext_inst, at, &.{ e.glslSet(), 4, ids[0] });
const big = try e.op1(.f_ord_greater_than_equal, ty, &.{ abs, try e.splatConst(at, .{ .float = min_normal }) });
r = try e.op1(.logical_and, ty, &.{ r, big });
}
return r;
}
if (eqn(u8, name, "fmod")) return e.op1(.f_rem, ty, ids);
if (eqn(u8, name, "firstbitlow")) return e.op1(.ext_inst, ty, &.{ e.glslSet(), 73, ids[0] }); // FindILsb
if (eqn(u8, name, "firstbithigh")) return e.op1(.ext_inst, ty, &.{ e.glslSet(), if (e.compOf(tys[0]).isUnsigned()) 75 else 74, ids[0] }); // FindUMsb / FindSMsb
if (eqn(u8, name, "f32tof16") or eqn(u8, name, "f16tof32")) {
// PackHalf2x16 / UnpackHalf2x16, a component at a time.
const to16 = eqn(u8, name, "f32tof16");
const n = e.t.componentCount(ty).?;
const parts = try e.arena.alloc(Id, n);
const ft = try e.t.scalar(.float);
const ut = try e.t.scalar(.uint);
for (parts, 0..) |*p, k| {
const x = if (n == 1) ids[0] else try e.op1(.composite_extract, if (to16) ft else ut, &.{ ids[0], @intCast(k) });
if (to16) {
const pair = try e.op1(.composite_construct, try e.t.vector(.float, 2), &.{ x, try e.constant(ft, .{ .float = 0 }) });
p.* = try e.op1(.ext_inst, ut, &.{ e.glslSet(), 58, pair });
} else {
const pair = try e.op1(.ext_inst, try e.t.vector(.float, 2), &.{ e.glslSet(), 62, x });
p.* = try e.op1(.composite_extract, ft, &.{ pair, 0 });
}
}
return if (n == 1) parts[0] else e.op1(.composite_construct, ty, parts);
}
if (eqn(u8, name, "dot") and !fl) {
// Integer dot product: the sum of the products.
const n = e.t.componentCount(tys[0]).?;
const st = try e.t.scalar(arg_c);
var acc: ?Id = null;
for (0..n) |k| {
const x = if (n == 1) ids[0] else try e.op1(.composite_extract, st, &.{ ids[0], @intCast(k) });
const y = if (n == 1) ids[1] else try e.op1(.composite_extract, st, &.{ ids[1], @intCast(k) });
const p = try e.op1(.i_mul, st, &.{ x, y });
acc = if (acc) |s0| try e.op1(.i_add, st, &.{ s0, p }) else p;
}
return acc.?;
}
if (eqn(u8, name, "IsHelperLane")) return e.op1(.load, ty, &.{try e.builtinInput(23, try e.t.scalar(.bool))}); // HelperInvocation
if (eqn(u8, name, "clip")) {
// Discard when any component is negative.
const at = tys[0];
const bt = try e.t.scalar(.bool);
const c = if (e.isTrueMatrix(at)) blk: {
const m = e.t.get(at).matrix;
const rt = try e.t.vector(m.elem, m.cols);
var acc: ?Id = null;
for (0..m.rows) |r| {
const neg = try e.op1(.f_ord_less_than, try e.t.withComponent(rt, .bool), &.{ try e.row(ids[0], rt, @intCast(r)), try e.splatConst(rt, .{ .float = 0 }) });
const v = try e.op1(.any, bt, &.{neg});
acc = if (acc) |a| try e.op1(.logical_or, bt, &.{ a, v }) else v;
}
break :blk acc.?;
} else blk: {
const neg = try e.op1(.f_ord_less_than, try e.t.withComponent(at, .bool), &.{ ids[0], try e.splatConst(at, .{ .float = 0 }) });
break :blk if (e.isScalarLike(at)) neg else try e.op1(.any, bt, &.{neg});
};
const kill_l = e.b.newId();
const merge_l = e.b.newId();
try e.b.emit(.functions, .selection_merge, &.{ merge_l, 0 });
try e.b.emit(.functions, .branch_conditional, &.{ c, kill_l, merge_l });
try e.b.emit(.functions, .label, &.{kill_l});
try e.b.emit(.functions, .kill, &.{});
try e.b.emit(.functions, .label, &.{merge_l});
return 0;
}
return e.unsupported("intrinsic '{s}' is not supported yet", .{name});

10
test/dxil/barriers.hlsl Normal file
View File

@ -0,0 +1,10 @@
// PROFILE: cs_6_0
// Memory barriers (device, group, all; with and without group sync).
// ZCHECK: call void @dx.op.barrier(i32 80, i32 10)
// ZCHECK: call void @dx.op.barrier(i32 80, i32 2)
RWStructuredBuffer<uint4> o : register(u0);
[numthreads(1,1,1)] void main(uint i : SV_GroupIndex) {
AllMemoryBarrier(); AllMemoryBarrierWithGroupSync(); DeviceMemoryBarrier(); DeviceMemoryBarrierWithGroupSync(); GroupMemoryBarrier();
o[0] = uint4(firstbitlow(i), firstbithigh(i), firstbithigh((int)i), f32tof16(asfloat(i)));
o[1].x = asuint(f16tof32(i));
}

View File

@ -0,0 +1,18 @@
// PROFILE: ps_6_6
// Intrinsics: firstbit*, f32tof16/f16tof32, and/or/select, isnan & co.,
// fmod, integer dot, any/all, fine derivatives, clip, IsHelperLane.
// ZCHECK: unaryBits.i32(i32 34
// ZCHECK: legacyF32ToF16
// ZCHECK: isHelperLane
RWStructuredBuffer<uint4> o : register(u0);
float4 main(float4 p : SV_Position, uint i : U) : SV_Target {
DeviceMemoryBarrier();
o[0] = uint4(firstbitlow(i), firstbithigh(i), firstbithigh((int)i), f32tof16(p.x));
int3 a = int3(1, 2, i), b = int3(3, i, 5);
bool3 m = and(a > 1, b < 4);
bool3 n = or(m, isnan(p.xyz));
float3 s = select(n, p.xyz, float3(1, 2, 3));
float r = fmod(p.x, 3) + f16tof32(i) + dot(a, b) + any(isinf(p)) + all(isfinite(p)) + isnormal(p.w) + ddx_fine(p.x) + ddy_fine(p.y);
clip(p.xy - 1);
return float4(s, r) + IsHelperLane();
}

6
test/dxil/sv_pixel.hlsl Normal file
View File

@ -0,0 +1,6 @@
// PROFILE: ps_6_8
// Pixel system values: clip distances in, barycentrics, coverage in and
// out, ViewID, shading rate, stencil ref.
struct I { float4 pos : SV_Position; float2 c0 : SV_ClipDistance0; float c1 : SV_ClipDistance1; noperspective float3 b2 : SV_Barycentrics1; };
struct O { float4 c : SV_Target; uint cov : SV_Coverage; uint st : SV_StencilRef; };
O main(I i, float3 b : SV_Barycentrics, uint cov : SV_Coverage, uint v : SV_ViewID, uint sr : SV_ShadingRate) { O o; o.c = float4(b + i.b2, i.c0.y + i.c1 + v + sr); o.cov = cov; o.st = 1; return o; }

5
test/dxil/sv_vertex.hlsl Normal file
View File

@ -0,0 +1,5 @@
// PROFILE: vs_6_8
// Vertex system values: clip/cull distances, ViewID, start vertex and
// instance, viewport / render target index, shading rate.
struct O { float4 pos : SV_Position; float2 c0 : SV_ClipDistance0; float c1 : SV_ClipDistance1; float cu : SV_CullDistance; uint vp : SV_ViewportArrayIndex; uint rt : SV_RenderTargetArrayIndex; uint sr : SV_ShadingRate; };
O main(uint v : SV_ViewID, uint sv : SV_StartVertexLocation, uint si : SV_StartInstanceLocation) { O o = (O)0; o.pos = v + sv + si; o.c0 = 1; o.c1 = 2; return o; }

8
test/spirv/barriers.hlsl Normal file
View File

@ -0,0 +1,8 @@
// PROFILE: cs_6_0
// Memory barriers (device, group, all; with and without group sync).
RWStructuredBuffer<uint4> o : register(u0);
[numthreads(1,1,1)] void main(uint i : SV_GroupIndex) {
AllMemoryBarrier(); AllMemoryBarrierWithGroupSync(); DeviceMemoryBarrier(); DeviceMemoryBarrierWithGroupSync(); GroupMemoryBarrier();
o[0] = uint4(firstbitlow(i), firstbithigh(i), firstbithigh((int)i), f32tof16(asfloat(i)));
o[1].x = asuint(f16tof32(i));
}

View File

@ -0,0 +1,15 @@
// PROFILE: ps_6_6
// Intrinsics: firstbit*, f32tof16/f16tof32, and/or/select, isnan & co.,
// fmod, integer dot, any/all, fine derivatives, clip, IsHelperLane.
RWStructuredBuffer<uint4> o : register(u0);
float4 main(float4 p : SV_Position, uint i : U) : SV_Target {
DeviceMemoryBarrier();
o[0] = uint4(firstbitlow(i), firstbithigh(i), firstbithigh((int)i), f32tof16(p.x));
int3 a = int3(1, 2, i), b = int3(3, i, 5);
bool3 m = and(a > 1, b < 4);
bool3 n = or(m, isnan(p.xyz));
float3 s = select(n, p.xyz, float3(1, 2, 3));
float r = fmod(p.x, 3) + f16tof32(i) + dot(a, b) + any(isinf(p)) + all(isfinite(p)) + isnormal(p.w) + ddx_fine(p.x) + ddy_fine(p.y);
clip(p.xy - 1);
return float4(s, r) + IsHelperLane();
}

8
test/spirv/sv_pixel.hlsl Normal file
View File

@ -0,0 +1,8 @@
// PROFILE: ps_6_8
// Pixel system values: clip distances in, barycentrics, coverage in and
// out, ViewID, shading rate, stencil ref.
// ZCHECK: BuiltIn BaryCoordNoPerspKHR
// ZCHECK: BuiltIn SampleMask
struct I { float4 pos : SV_Position; float2 c0 : SV_ClipDistance0; float c1 : SV_ClipDistance1; noperspective float3 b2 : SV_Barycentrics1; };
struct O { float4 c : SV_Target; uint cov : SV_Coverage; uint st : SV_StencilRef; };
O main(I i, float3 b : SV_Barycentrics, uint cov : SV_Coverage, uint v : SV_ViewID, uint sr : SV_ShadingRate) { O o; o.c = float4(b + i.b2, i.c0.y + i.c1 + v + sr); o.cov = cov; o.st = 1; return o; }

View File

@ -0,0 +1,7 @@
// PROFILE: vs_6_8
// Vertex system values: clip/cull distances, ViewID, start vertex and
// instance, viewport / render target index, shading rate.
// ZCHECK: OpVariable %_ptr_Output__arr_float_uint_3 Output
// ZCHECK: BuiltIn ViewIndex
struct O { float4 pos : SV_Position; float2 c0 : SV_ClipDistance0; float c1 : SV_ClipDistance1; float cu : SV_CullDistance; uint vp : SV_ViewportArrayIndex; uint rt : SV_RenderTargetArrayIndex; uint sr : SV_ShadingRate; };
O main(uint v : SV_ViewID, uint sv : SV_StartVertexLocation, uint si : SV_StartInstanceLocation) { O o = (O)0; o.pos = v + sv + si; o.c0 = 1; o.c1 = 2; return o; }