Groupshared memory, atomics, counters and byte-address buffers
DXIL: groupshared variables are address-space-3 module globals (the bitcode writer gains global variables, atomicrmw and cmpxchg), with the PSV0 groupshared byte count. Interlocked* on groupshared memory, structured and byte-address buffers, typed buffers and texels (atomicBinOp / atomicCompareExchange). ByteAddressBuffer Load/Store (2..4) and atomic methods; IncrementCounter / DecrementCounter, Append and Consume buffers with the counter flag in metadata, PSV0 and SM 6.6 handle properties. Also fixes void intrinsics (barriers) in DXIL. SPIR-V: the same atomics (OpImageTexelPointer for texels, Workgroup or Device scope, Int64Atomics), byte-address buffer methods, and dxc's separate counter buffers for counters and Append/Consume. Typed-buffer elements reached through assignments read and write as images.
This commit is contained in:
parent
7a74e55fc7
commit
52266c6fc6
|
|
@ -45,6 +45,8 @@ const Lowered = union(enum) {
|
|||
/// A local holding a resource (index into Emitter.res_locals): an
|
||||
/// alias of the one resource stored into it.
|
||||
res_local: u32,
|
||||
/// An element of a typed buffer or texture (atomics' destination).
|
||||
texel: struct { res: u32, coords: []const Value },
|
||||
/// An array of resources (or a sub-array), with the flattened index so
|
||||
/// far.
|
||||
res_array: struct { res: u32, index: ?Value = null, ty: TypeId },
|
||||
|
|
@ -68,6 +70,8 @@ const Ptr = struct {
|
|||
bool_storage: bool = false,
|
||||
/// Mixed 32-bit component types share i32 storage (floats bitcast).
|
||||
mixed: bool = false,
|
||||
/// 3 for groupshared memory.
|
||||
addr_space: u32 = 0,
|
||||
};
|
||||
|
||||
const Resource = struct {
|
||||
|
|
@ -80,6 +84,8 @@ const Resource = struct {
|
|||
elem: TypeId,
|
||||
/// SamplerComparisonState.
|
||||
comparison: bool = false,
|
||||
/// Structured UAV with a hidden counter (IncrementCounter, Append...).
|
||||
has_counter: bool = false,
|
||||
/// Registers in the range (arrays of resources); maxInt for unsized.
|
||||
count: u32 = 1,
|
||||
/// An array element's handle made at its use: a copy of the array's
|
||||
|
|
@ -127,6 +133,7 @@ const Emitter = struct {
|
|||
/// 16-bit min-precision values cross the signature.
|
||||
uses_min_precision: bool = false,
|
||||
uses_view_id: bool = false,
|
||||
groupshared_bytes: u32 = 0,
|
||||
/// A typed UAV load of more than one component.
|
||||
uses_typed_uav_load: bool = false,
|
||||
uses_inner_coverage: bool = false,
|
||||
|
|
@ -329,7 +336,7 @@ const Emitter = struct {
|
|||
else => return e.unsupported("global '{s}' is not supported yet", .{g.name}),
|
||||
};
|
||||
const kind: u32 = switch (r.kind) {
|
||||
.structured_buffer, .rw_structured_buffer => 12,
|
||||
.structured_buffer, .rw_structured_buffer, .append_structured_buffer, .consume_structured_buffer => 12,
|
||||
.byte_address_buffer, .rw_byte_address_buffer => 11,
|
||||
.buffer, .rw_buffer => 10,
|
||||
.texture1d, .rw_texture1d => 1,
|
||||
|
|
@ -343,14 +350,34 @@ const Emitter = struct {
|
|||
.texture_cube_array => 9,
|
||||
else => return e.unsupported("resource type '{s}' is not supported yet", .{r.name}),
|
||||
};
|
||||
const class: Resource.Class = if (r.kind.isWritable()) .uav else .srv;
|
||||
const class: Resource.Class = if (r.kind.isWritable() or r.kind == .consume_structured_buffer) .uav else .srv;
|
||||
if (g.binding.explicit and g.binding.class != @as(u8, if (class == .uav) 'u' else 't')) {
|
||||
return e.unsupported("binding '{s}' to the wrong register class", .{g.name});
|
||||
}
|
||||
try e.resources.append(e.arena, .{ .global = @intCast(gi), .class = class, .id = counts[@backingInt(class)], .kind = kind, .elem = r.elem, .count = count });
|
||||
try e.resources.append(e.arena, .{ .global = @intCast(gi), .class = class, .id = counts[@backingInt(class)], .kind = kind, .elem = r.elem, .count = count, .has_counter = r.kind == .append_structured_buffer or r.kind == .consume_structured_buffer });
|
||||
counts[@backingInt(class)] += 1;
|
||||
try e.resource_of_global.put(e.arena, @intCast(gi), @intCast(e.resources.items.len - 1));
|
||||
}
|
||||
// Counters must be known before handles are made (SM 6.6 encodes
|
||||
// them in the handle's properties).
|
||||
for (e.m.functions.items) |f| for (f.insts.items) |inst| {
|
||||
const mt = switch (inst.op) {
|
||||
.method => |mt| mt,
|
||||
else => continue,
|
||||
};
|
||||
const eq = std.mem.eql;
|
||||
if (!(eq(u8, mt.name, "IncrementCounter") or eq(u8, mt.name, "DecrementCounter"))) continue;
|
||||
var r = mt.res;
|
||||
const g: ?u32 = while (true) switch (f.inst(r).op) {
|
||||
.load => |p| r = p,
|
||||
.access => |a| r = a.base,
|
||||
.global => |gi| break gi,
|
||||
else => break null,
|
||||
};
|
||||
if (g) |gi| if (e.resource_of_global.get(gi)) |ri| {
|
||||
e.resources.items[ri].has_counter = true;
|
||||
};
|
||||
};
|
||||
// Unbound resources take the next free register of their class.
|
||||
var used: std.AutoHashMapUnmanaged(struct { u8, u32, u32 }, void) = .empty;
|
||||
// (Unsized ranges are taken to hold 64 registers here.)
|
||||
|
|
@ -441,7 +468,7 @@ const Emitter = struct {
|
|||
fn resourceProperties(e: *Emitter, res: Resource) Error![2]i64 {
|
||||
var basic: i64 = res.kind;
|
||||
if (res.class == .uav) basic |= 0x1000;
|
||||
if (res.comparison) basic |= 0x8000;
|
||||
if (res.comparison or res.has_counter) basic |= 0x8000;
|
||||
const extra: i64 = switch (res.kind) {
|
||||
13 => try e.cbufferSize(res),
|
||||
12 => try e.stride(res.elem),
|
||||
|
|
@ -547,6 +574,15 @@ const Emitter = struct {
|
|||
x.ptr = R.v(map, x.ptr);
|
||||
x.indices = try R.vs(a, map, x.indices);
|
||||
},
|
||||
.atomicrmw => |*x| {
|
||||
x.ptr = R.v(map, x.ptr);
|
||||
x.val = R.v(map, x.val);
|
||||
},
|
||||
.cmpxchg => |*x| {
|
||||
x.ptr = R.v(map, x.ptr);
|
||||
x.cmp = R.v(map, x.cmp);
|
||||
x.new = R.v(map, x.new);
|
||||
},
|
||||
.@"unreachable" => {},
|
||||
}
|
||||
return out;
|
||||
|
|
@ -654,6 +690,19 @@ const Emitter = struct {
|
|||
};
|
||||
}
|
||||
|
||||
/// The flattened storage shape for `ty` (base not yet set).
|
||||
fn storageFor(e: *Emitter, ty: TypeId) Error!Ptr {
|
||||
const n = try e.flatCount(ty);
|
||||
if (try e.mixedStorage(ty)) {
|
||||
const st = try e.dm.intType(32);
|
||||
return .{ .base = undefined, .array_ty = try e.dm.getType(.{ .array = .{ .len = n, .elem = st } }), .scalar_ty = st, .mixed = true };
|
||||
}
|
||||
const c = try e.uniformScalar(ty);
|
||||
if (c.size() != 4) return e.unsupported("groupshared values of type '{s}' are not supported yet", .{try e.t.format(e.arena, ty)});
|
||||
const st = try e.scalarTy(if (c == .bool) Scalar.int else c);
|
||||
return .{ .base = undefined, .array_ty = try e.dm.getType(.{ .array = .{ .len = n, .elem = st } }), .scalar_ty = st, .bool_storage = c == .bool };
|
||||
}
|
||||
|
||||
/// Flattened storage for a value of type `ty`, allocated on entry.
|
||||
fn alloca(e: *Emitter, ty: TypeId) Error!Ptr {
|
||||
const n = try e.flatCount(ty);
|
||||
|
|
@ -689,6 +738,19 @@ const Emitter = struct {
|
|||
if (!gop.found_existing) gop.value_ptr.* = try e.alloca(e.m.globals.items[g].ty);
|
||||
return .{ .ptr = gop.value_ptr.* };
|
||||
}
|
||||
if (e.m.globals.items[g].space == .groupshared) {
|
||||
// Address-space-3 module variables, flattened like locals.
|
||||
const gop = try e.private_globals.getOrPut(e.arena, g);
|
||||
if (!gop.found_existing) {
|
||||
var p = try e.storageFor(e.m.globals.items[g].ty);
|
||||
const gv = try e.dm.addGlobal(e.m.globals.items[g].name, p.array_ty, 3);
|
||||
p.base = gv;
|
||||
p.addr_space = 3;
|
||||
gop.value_ptr.* = p;
|
||||
e.groupshared_bytes += @intCast(e.dm.types.items[p.array_ty].array.len * 4);
|
||||
}
|
||||
return .{ .ptr = gop.value_ptr.* };
|
||||
}
|
||||
const ri = e.resource_of_global.get(g) orelse return e.unsupported("global '{s}' is not supported yet", .{e.m.globals.items[g].name});
|
||||
if (e.resources.items[ri].class == .cbuffer) return .{ .cbuf = .{ .res = ri } };
|
||||
if (e.resources.items[ri].count != 1) return .{ .res_array = .{ .res = ri, .ty = e.m.globals.items[g].ty } };
|
||||
|
|
@ -705,6 +767,12 @@ const Emitter = struct {
|
|||
.handle => |h| return .{ .handle = h },
|
||||
.res_local => |k| return e.res_locals.items[k] orelse return e.unsupported("a resource local read before it is assigned", .{}),
|
||||
.res_array => |ra| return .{ .res_array = ra },
|
||||
.texel => |tx| {
|
||||
const res = e.resources.items[tx.res];
|
||||
const mip = if (res.class == .uav) try e.dm.constant(.{ .ty = try e.dm.intType(32), .kind = .undef }) else try e.i32Const(0);
|
||||
if (res.kind == 10) return .{ .scalars = try e.typedBufferLoad(res, tx.coords[0], i.ty) };
|
||||
return .{ .scalars = try e.textureLoad(res, mip, tx.coords, null, i.ty) };
|
||||
},
|
||||
.cbuf => |c| return e.cbufferLoad(c.res, c.offset, c.dyn_regs, c.row_major, i.ty),
|
||||
.buf => |b| return .{ .scalars = try e.structuredLoad(e.resources.items[b.res], b.index, b.offset, b.dyn, b.row_major, i.ty) },
|
||||
.ptr => |ptr| return e.loadPtr(ptr, i.ty),
|
||||
|
|
@ -738,7 +806,9 @@ const Emitter = struct {
|
|||
if (frame.vals[a.base.index()] == .cbuf) return e.cbufferAccess(frame, frame.vals[a.base.index()].cbuf, f.inst(a.base).ty, a.indices);
|
||||
if (frame.vals[a.base.index()] == .handle) {
|
||||
const ri = frame.vals[a.base.index()].handle;
|
||||
if (e.resources.items[ri].kind != 12) return e.unsupported("member access into this resource is not supported yet", .{});
|
||||
const rk = e.resources.items[ri].kind;
|
||||
if (rk >= 1 and rk <= 10) return .{ .texel = .{ .res = ri, .coords = try e.scalars(frame, a.indices[0]) } };
|
||||
if (rk != 12) return e.unsupported("member access into this resource is not supported yet", .{});
|
||||
const start: @FieldType(Lowered, "buf") = .{ .res = ri, .index = (try e.scalars(frame, a.indices[0]))[0] };
|
||||
return e.bufferAccess(frame, start, e.t.get(f.inst(a.base).ty).resource.elem, a.indices[1..]);
|
||||
}
|
||||
|
|
@ -1001,7 +1071,7 @@ const Emitter = struct {
|
|||
fn elementPtr(e: *Emitter, ptr: Ptr, c: u32) Error!Value {
|
||||
var idx = try e.i32Const(ptr.offset_const + c);
|
||||
if (ptr.offset_dyn) |d| idx = try e.add(try e.dm.intType(32), .{ .binop = .{ .op = .add, .lhs = d, .rhs = idx } });
|
||||
const pt = try e.dm.ptrType(ptr.scalar_ty);
|
||||
const pt = try e.dm.getType(.{ .pointer = .{ .elem = ptr.scalar_ty, .addr_space = ptr.addr_space } });
|
||||
return e.add(pt, .{ .gep = .{ .elem = ptr.array_ty, .ptr = ptr.base, .indices = try e.arena.dupe(Value, &.{ try e.i32Const(0), idx }) } });
|
||||
}
|
||||
|
||||
|
|
@ -1088,6 +1158,7 @@ const Emitter = struct {
|
|||
fn intrinsic(e: *Emitter, frame: *Frame, name: []const u8, ty: TypeId, args: []const Ref) Error!Lowered {
|
||||
const eq = std.mem.eql;
|
||||
if (try e.outIntrinsic(frame, name, ty, args)) |r| return r;
|
||||
for (args) |x| if (frame.vals[x.index()] != .scalars) return e.unsupported("intrinsic '{s}' is not supported yet", .{name});
|
||||
var a: [4][]const Value = undefined;
|
||||
for (args, 0..) |x, k| a[k] = try e.scalars(frame, x);
|
||||
const ac = if (args.len > 0) e.compOf(frame.f.inst(args[0]).ty) else e.compOf(ty);
|
||||
|
|
@ -1096,7 +1167,7 @@ const Emitter = struct {
|
|||
const at = try e.scalarTy(ac);
|
||||
const fl = ac.isFloat();
|
||||
const n = if (args.len > 0) a[0].len else 1;
|
||||
const out = try e.arena.alloc(Value, try e.flatCount(ty));
|
||||
const out = try e.arena.alloc(Value, if (ty == TypeTable.void_type) 0 else try e.flatCount(ty));
|
||||
const Unary = struct { name: []const u8, op: i64 };
|
||||
const float_unary = [_]Unary{
|
||||
.{ .name = "sqrt", .op = 24 }, .{ .name = "rsqrt", .op = 25 }, .{ .name = "sin", .op = 13 }, .{ .name = "cos", .op = 12 },
|
||||
|
|
@ -1355,6 +1426,7 @@ const Emitter = struct {
|
|||
_ = ty;
|
||||
return .{ .scalars = a2 };
|
||||
}
|
||||
if (std.mem.startsWith(u8, name, "Interlocked")) return try e.atomic(frame, name, args);
|
||||
if (eq(u8, name, "asuint") and args.len == 3) {
|
||||
// SplitDouble.
|
||||
const i32t = try e.dm.intType(32);
|
||||
|
|
@ -1377,6 +1449,59 @@ const Emitter = struct {
|
|||
return null;
|
||||
}
|
||||
|
||||
/// Interlocked*(dest, value(s) [, out original]) on groupshared memory,
|
||||
/// buffer elements and texels.
|
||||
fn atomic(e: *Emitter, frame: *Frame, name: []const u8, args: []const Ref) Error!Lowered {
|
||||
const eq = std.mem.eql;
|
||||
const op_name = name["Interlocked".len..];
|
||||
const dest_ty = frame.f.inst(args[0]).ty;
|
||||
const dc = e.compOf(dest_ty);
|
||||
if (dc.size() != 4 or dc.isFloat()) return e.unsupported("'{s}' on '{s}' is not supported yet", .{ name, try e.t.format(e.arena, dest_ty) });
|
||||
const i32t = try e.dm.intType(32);
|
||||
const is_cmp = std.mem.startsWith(u8, op_name, "Compare");
|
||||
const has_orig = (is_cmp and eq(u8, op_name, "CompareExchange")) or (!is_cmp and args.len == 3);
|
||||
const val = (try e.scalars(frame, args[if (is_cmp) 2 else 1]))[0];
|
||||
const cmp = if (is_cmp) (try e.scalars(frame, args[1]))[0] else null;
|
||||
const orig: Value = switch (frame.vals[args[0].index()]) {
|
||||
.ptr => |p| blk: {
|
||||
if (p.addr_space != 3) return e.unsupported("'{s}' on this variable is not supported yet", .{name});
|
||||
const ptr = try e.elementPtr(p, 0);
|
||||
if (cmp) |cv| {
|
||||
const pair = try e.dm.getType(.{ .@"struct" = .{ .name = "", .elems = &.{ i32t, try e.dm.intType(1) } } });
|
||||
const r = try e.add(pair, .{ .cmpxchg = .{ .ptr = ptr, .cmp = cv, .new = val } });
|
||||
break :blk try e.add(i32t, .{ .extractvalue = .{ .agg = r, .index = 0 } });
|
||||
}
|
||||
const rmw: module.RmwOp = if (eq(u8, op_name, "Add")) .add else if (eq(u8, op_name, "And")) .@"and" else if (eq(u8, op_name, "Or")) .@"or" else if (eq(u8, op_name, "Xor")) .xor else if (eq(u8, op_name, "Min")) (if (dc.isUnsigned()) .umin else .min) else if (eq(u8, op_name, "Max")) (if (dc.isUnsigned()) .umax else .max) else if (eq(u8, op_name, "Exchange")) .xchg else return e.unsupported("'{s}' is not supported yet", .{name});
|
||||
break :blk try e.add(i32t, .{ .atomicrmw = .{ .op = rmw, .ptr = ptr, .val = val } });
|
||||
},
|
||||
.buf => |b| blk: {
|
||||
const res = e.resources.items[b.res];
|
||||
var off = try e.i32Const(b.offset);
|
||||
if (b.dyn) |d| off = try e.add(i32t, .{ .binop = .{ .op = .add, .lhs = d, .rhs = off } });
|
||||
break :blk try e.resourceAtomic(res, &.{ b.index, off }, op_name, dc, val, cmp);
|
||||
},
|
||||
.texel => |tx| try e.resourceAtomic(e.resources.items[tx.res], tx.coords, op_name, dc, val, cmp),
|
||||
else => return e.unsupported("'{s}' on this destination is not supported yet", .{name}),
|
||||
};
|
||||
if (has_orig) try e.storePtr(try e.outPtr(frame, args[args.len - 1]), &.{orig}, frame.f.inst(args[args.len - 1]).ty);
|
||||
return .none;
|
||||
}
|
||||
|
||||
/// dx.op.atomicBinOp / atomicCompareExchange at `coords` (padded with
|
||||
/// undef to three).
|
||||
fn resourceAtomic(e: *Emitter, res: Resource, coords: []const Value, op_name: []const u8, dc: Scalar, val: Value, cmp: ?Value) Error!Value {
|
||||
const eq = std.mem.eql;
|
||||
const i32t = try e.dm.intType(32);
|
||||
const c = try e.padded(coords, coords.len, 3, i32t);
|
||||
if (cmp) |cv| {
|
||||
const fn_i = try e.dxOp("dx.op.atomicCompareExchange.i32", i32t, &.{ i32t, e.handle_ty, i32t, i32t, i32t, i32t, i32t }, .nounwind);
|
||||
return e.callOp(fn_i, i32t, &.{ try e.i32Const(79), res.handle, c[0], c[1], c[2], cv, val });
|
||||
}
|
||||
const op: i64 = if (eq(u8, op_name, "Add")) 0 else if (eq(u8, op_name, "And")) 1 else if (eq(u8, op_name, "Or")) 2 else if (eq(u8, op_name, "Xor")) 3 else if (eq(u8, op_name, "Min")) (if (dc.isUnsigned()) 6 else 4) else if (eq(u8, op_name, "Max")) (if (dc.isUnsigned()) 7 else 5) else if (eq(u8, op_name, "Exchange")) 8 else return e.unsupported("'Interlocked{s}' is not supported yet", .{op_name});
|
||||
const fn_i = try e.dxOp("dx.op.atomicBinOp.i32", i32t, &.{ i32t, e.handle_ty, i32t, i32t, i32t, i32t, i32t }, .nounwind);
|
||||
return e.callOp(fn_i, i32t, &.{ try e.i32Const(78), res.handle, try e.i32Const(op), c[0], c[1], c[2], val });
|
||||
}
|
||||
|
||||
fn fbin(e: *Emitter, op: module.BinOp, x: Value, y: Value, ty: DType) Error!Value {
|
||||
return e.add(ty, .{ .binop = .{ .op = op, .lhs = x, .rhs = y } });
|
||||
}
|
||||
|
|
@ -1739,6 +1864,17 @@ const Emitter = struct {
|
|||
return out;
|
||||
}
|
||||
|
||||
fn typedBufferLoad(e: *Emitter, res: Resource, idx: Value, ty: TypeId) Error![]const Value {
|
||||
const via = try e.texelScalar(res.elem);
|
||||
const st = try e.scalarTy(via);
|
||||
const ret_ty = try e.resRetType(st);
|
||||
const i32t = try e.dm.intType(32);
|
||||
if (res.class == .uav and try e.flatCount(res.elem) > 1) e.uses_typed_uav_load = true;
|
||||
const fn_i = try e.dxOp(try std.fmt.allocPrint(e.arena, "dx.op.bufferLoad.{s}", .{e.overloadName(st)}), ret_ty, &.{ i32t, e.handle_ty, i32t, i32t }, .nounwind_readonly);
|
||||
const call = try e.callOp(fn_i, ret_ty, &.{ try e.i32Const(68), res.handle, idx, try e.dm.constant(.{ .ty = i32t, .kind = .undef }) });
|
||||
return e.resRetComponents(call, st, via, ty);
|
||||
}
|
||||
|
||||
fn textureLoad(e: *Emitter, res: Resource, mip: Value, coords: []const Value, offset: ?[]const Value, ty: TypeId) Error![]const Value {
|
||||
const via = try e.texelScalar(res.elem);
|
||||
const st = try e.scalarTy(via);
|
||||
|
|
@ -1781,6 +1917,44 @@ const Emitter = struct {
|
|||
return e.resourceLoad(frame, res_ref, arg_refs[0], ty);
|
||||
}
|
||||
if (res.kind == 12 and eq(u8, name, "Load")) return e.resourceLoad(frame, res_ref, arg_refs[0], ty);
|
||||
if (res.kind == 11) {
|
||||
// Byte-address buffers: Load/Load2..4, Store/Store2..4, atomics.
|
||||
if (std.mem.startsWith(u8, name, "Load") and arg_refs.len == 1) return e.resourceLoad(frame, res_ref, arg_refs[0], ty);
|
||||
if (std.mem.startsWith(u8, name, "Store") and arg_refs.len == 2) {
|
||||
try e.resourceStore(frame, res_ref, arg_refs[0], arg_refs[1]);
|
||||
return .none;
|
||||
}
|
||||
if (std.mem.startsWith(u8, name, "Interlocked")) {
|
||||
const op_name = name["Interlocked".len..];
|
||||
const is_cmp = std.mem.startsWith(u8, op_name, "Compare");
|
||||
const off = (try e.scalars(frame, arg_refs[0]))[0];
|
||||
const val = (try e.scalars(frame, arg_refs[if (is_cmp) 2 else 1]))[0];
|
||||
const cmp = if (is_cmp) (try e.scalars(frame, arg_refs[1]))[0] else null;
|
||||
const vty = frame.f.inst(arg_refs[if (is_cmp) 2 else 1]).ty;
|
||||
if (e.compOf(vty).size() != 4) return e.unsupported("64-bit '{s}' is not supported yet", .{name});
|
||||
const orig = try e.resourceAtomic(res.*, &.{off}, op_name, e.compOf(vty), val, cmp);
|
||||
const has_orig = if (is_cmp) eq(u8, op_name, "CompareExchange") else arg_refs.len == 3;
|
||||
if (has_orig) try e.storePtr(try e.outPtr(frame, arg_refs[arg_refs.len - 1]), &.{orig}, frame.f.inst(arg_refs[arg_refs.len - 1]).ty);
|
||||
return .none;
|
||||
}
|
||||
}
|
||||
if (res.kind == 12 and (eq(u8, name, "IncrementCounter") or eq(u8, name, "DecrementCounter") or eq(u8, name, "Append") or eq(u8, name, "Consume"))) {
|
||||
for (e.resources.items) |*r2| if (r2.global == res.global) {
|
||||
r2.has_counter = true;
|
||||
};
|
||||
const i8t = try e.dm.intType(8);
|
||||
const inc = eq(u8, name, "IncrementCounter") or eq(u8, name, "Append");
|
||||
const fn_i = try e.dxOp("dx.op.bufferUpdateCounter", i32t, &.{ i32t, e.handle_ty, i8t }, .nounwind);
|
||||
const n = try e.callOp(fn_i, i32t, &.{ try e.i32Const(70), res.handle, try e.dm.constant(.{ .ty = i8t, .kind = .{ .int = if (inc) 1 else -1 } }) });
|
||||
if (eq(u8, name, "Append")) {
|
||||
try e.structuredStore(res.*, n, 0, null, false, frame.f.inst(arg_refs[0]).ty, try e.scalars(frame, arg_refs[0]));
|
||||
return .none;
|
||||
}
|
||||
if (eq(u8, name, "Consume")) return .{ .scalars = try e.structuredLoad(res.*, n, 0, null, false, ty) };
|
||||
// (BufferUpdateCounter returns the old value for increments and
|
||||
// the new one for decrements, matching HLSL.)
|
||||
return e.list(&.{n});
|
||||
}
|
||||
const sh = texture.shape(kind) orelse return e.unsupported("method '{s}' is not supported yet", .{name});
|
||||
const call = texture.decode(name, sh, arg_refs.len) orelse return e.unsupported("method '{s}' is not supported yet", .{name});
|
||||
if (call.status != null) return e.unsupported("texture methods with a status argument are not supported yet", .{});
|
||||
|
|
@ -2531,7 +2705,7 @@ const Emitter = struct {
|
|||
var fields: std.ArrayList(?module.MdId) = .empty;
|
||||
try fields.appendSlice(e.arena, &.{ try mdI32(dm, r.id), try dm.mdValue(try dm.ptrType(class_ty), undef_ptr), try dm.mdString(""), try mdI32(dm, b.space), try mdI32(dm, b.index), try mdI32(dm, range), try mdI32(dm, r.kind) });
|
||||
if (r.class == .uav) {
|
||||
try fields.appendSlice(e.arena, &.{ try mdBool(dm, i1t, false), try mdBool(dm, i1t, false), try mdBool(dm, i1t, false) });
|
||||
try fields.appendSlice(e.arena, &.{ try mdBool(dm, i1t, false), try mdBool(dm, i1t, r.has_counter), try mdBool(dm, i1t, false) });
|
||||
} else try fields.append(e.arena, try mdI32(dm, 0)); // sample count
|
||||
const extra: ?module.MdId = switch (r.kind) {
|
||||
12 => try dm.mdNode(&.{ try mdI32(dm, 1), try mdI32(dm, try e.stride(r.elem)) }),
|
||||
|
|
@ -2618,7 +2792,7 @@ const Emitter = struct {
|
|||
else => 3,
|
||||
},
|
||||
.uav => switch (r.kind) {
|
||||
12 => 8,
|
||||
12 => if (r.has_counter) 9 else 8,
|
||||
11 => 7,
|
||||
else => 6,
|
||||
},
|
||||
|
|
@ -2657,6 +2831,7 @@ const Emitter = struct {
|
|||
.resources = binds.items,
|
||||
.inputs = e.sig_in,
|
||||
.outputs = e.sig_out,
|
||||
.groupshared_bytes = if (entry.stage == .compute) e.groupshared_bytes else 0,
|
||||
});
|
||||
defer gpa.free(psv);
|
||||
const isg1 = try signature.programSignature(gpa, e.sig_in, true);
|
||||
|
|
|
|||
|
|
@ -58,6 +58,8 @@ pub const Linkage = enum(u32) { external = 0, internal = 3 };
|
|||
pub const FnAttrs = enum { none, nounwind, nounwind_readnone, nounwind_readonly };
|
||||
|
||||
pub const Value = union(enum) {
|
||||
/// Index into Module.globals (a pointer to the variable).
|
||||
global: u32,
|
||||
/// Index into Module.functions.
|
||||
function: u32,
|
||||
constant: ConstId,
|
||||
|
|
@ -67,6 +69,12 @@ pub const Value = union(enum) {
|
|||
};
|
||||
|
||||
pub const BinOp = enum(u32) { add = 0, sub = 1, mul = 2, udiv = 3, sdiv = 4, urem = 5, srem = 6, shl = 7, lshr = 8, ashr = 9, @"and" = 10, @"or" = 11, xor = 12 };
|
||||
pub const RmwOp = enum(u32) { xchg = 0, add = 1, sub = 2, @"and" = 3, nand = 4, @"or" = 5, xor = 6, max = 7, min = 8, umax = 9, umin = 10 };
|
||||
|
||||
/// A module-level variable (groupshared memory: address space 3),
|
||||
/// declared without an initializer as dxc does.
|
||||
pub const GlobalVar = struct { name: []const u8, ty: TypeId, addr_space: u32, ptr_ty: TypeId };
|
||||
|
||||
pub const CastOp = enum(u32) { trunc = 0, zext = 1, sext = 2, fptoui = 3, fptosi = 4, uitofp = 5, sitofp = 6, fptrunc = 7, fpext = 8, ptrtoint = 9, inttoptr = 10, bitcast = 11 };
|
||||
|
||||
pub const Inst = struct {
|
||||
|
|
@ -88,6 +96,10 @@ pub const Inst = struct {
|
|||
store: struct { ptr: Value, val: Value, @"align": u32 },
|
||||
/// `ty` is the result pointer type; `elem` the source element type.
|
||||
gep: struct { elem: TypeId, ptr: Value, indices: []const Value },
|
||||
/// Sequentially consistent read-modify-write; yields the old value.
|
||||
atomicrmw: struct { op: RmwOp, ptr: Value, val: Value },
|
||||
/// Yields `{ old, success }` (`ty` is that literal struct).
|
||||
cmpxchg: struct { ptr: Value, cmp: Value, new: Value },
|
||||
@"unreachable",
|
||||
},
|
||||
};
|
||||
|
|
@ -117,6 +129,7 @@ pub const Module = struct {
|
|||
types: std.ArrayList(Type) = .empty,
|
||||
constants: std.ArrayList(Constant) = .empty,
|
||||
functions: std.ArrayList(Function) = .empty,
|
||||
globals: std.ArrayList(GlobalVar) = .empty,
|
||||
metadata: std.ArrayList(Metadata) = .empty,
|
||||
named_metadata: std.ArrayList(struct { name: []const u8, nodes: []const MdId }) = .empty,
|
||||
|
||||
|
|
@ -171,6 +184,12 @@ pub const Module = struct {
|
|||
unreachable;
|
||||
}
|
||||
|
||||
pub fn addGlobal(m: *Module, name: []const u8, ty: TypeId, addr_space: u32) !Value {
|
||||
const ptr_ty = try m.getType(.{ .pointer = .{ .elem = ty, .addr_space = addr_space } });
|
||||
try m.globals.append(m.allocator(), .{ .name = try m.allocator().dupe(u8, name), .ty = ty, .addr_space = addr_space, .ptr_ty = ptr_ty });
|
||||
return .{ .global = @intCast(m.globals.items.len - 1) };
|
||||
}
|
||||
|
||||
pub fn constant(m: *Module, c: Constant) !Value {
|
||||
for (m.constants.items, 0..) |existing, i| {
|
||||
if (existing.ty == c.ty and std.meta.eql(existing.kind, c.kind)) return .{ .constant = @intCast(i) };
|
||||
|
|
@ -268,15 +287,17 @@ const Encoder = struct {
|
|||
}
|
||||
|
||||
fn moduleValueCount(e: *Encoder) u32 {
|
||||
return @intCast(e.m.functions.items.len + e.m.constants.items.len);
|
||||
return @intCast(e.m.globals.items.len + e.m.functions.items.len + e.m.constants.items.len);
|
||||
}
|
||||
|
||||
/// Absolute value ID. Module values: functions, then constants.
|
||||
/// Absolute value ID. Module values: globals, functions, constants.
|
||||
fn valueId(e: *Encoder, f: ?*const Function, v: Value) u32 {
|
||||
const nglobals: u32 = @intCast(e.m.globals.items.len);
|
||||
const nfuncs: u32 = @intCast(e.m.functions.items.len);
|
||||
return switch (v) {
|
||||
.function => |i| i,
|
||||
.constant => |c| nfuncs + c,
|
||||
.global => |i| i,
|
||||
.function => |i| nglobals + i,
|
||||
.constant => |c| nglobals + nfuncs + c,
|
||||
.arg => |a| e.moduleValueCount() + a,
|
||||
.inst => |i| blk: {
|
||||
const func = f.?;
|
||||
|
|
@ -292,6 +313,7 @@ const Encoder = struct {
|
|||
|
||||
fn typeOf(e: *Encoder, f: ?*const Function, v: Value) TypeId {
|
||||
return switch (v) {
|
||||
.global => |i| e.m.globals.items[i].ptr_ty,
|
||||
.function => |i| e.m.ptrTypeConst(e.m.functions.items[i].ty),
|
||||
.constant => |c| e.m.constants.items[c].ty,
|
||||
.arg => |a| e.m.types.items[f.?.ty].function.params[a],
|
||||
|
|
@ -327,6 +349,12 @@ const Encoder = struct {
|
|||
try e.writeTypes();
|
||||
try w.recordString(2, &.{}, e.m.triple);
|
||||
try w.recordString(3, &.{}, e.m.datalayout);
|
||||
for (e.m.globals.items) |g| {
|
||||
// GLOBALVAR: [type, addrspace << 2 | explicit type, init (none),
|
||||
// linkage (external), align 4, section]
|
||||
try e.ops.appendSlice(e.gpa(), &.{ g.ty, (@as(u64, g.addr_space) << 2) | 2, 0, 0, 3, 0 });
|
||||
try e.rec(7);
|
||||
}
|
||||
for (e.m.functions.items) |f| {
|
||||
// FUNCTION: [type, cc, isproto, linkage, paramattr, align, section,
|
||||
// visibility, gc, unnamed_addr, prologue, dllstorage,
|
||||
|
|
@ -493,7 +521,9 @@ const Encoder = struct {
|
|||
}
|
||||
}.lt);
|
||||
try w.enterBlock(block_value_symtab, 4);
|
||||
for (order) |i| try w.recordString(1, &.{i}, funcs[i].name); // VST_ENTRY
|
||||
const nglobals: u32 = @intCast(e.m.globals.items.len);
|
||||
for (e.m.globals.items, 0..) |g, i| try w.recordString(1, &.{i}, g.name);
|
||||
for (order) |i| try w.recordString(1, &.{nglobals + i}, funcs[i].name); // VST_ENTRY
|
||||
try w.endBlock();
|
||||
}
|
||||
|
||||
|
|
@ -587,6 +617,22 @@ const Encoder = struct {
|
|||
try e.rec(43);
|
||||
},
|
||||
.@"unreachable" => try e.rec(15),
|
||||
.atomicrmw => |a| {
|
||||
// ATOMICRMW: [ptr, val, op, volatile, ordering, scope]
|
||||
try e.pushValueAndType(f, inst_id, a.ptr);
|
||||
try e.pushValue(f, inst_id, a.val);
|
||||
try e.ops.appendSlice(e.gpa(), &.{ @backingInt(a.op), 0, 6, 1 }); // seq_cst, cross-thread
|
||||
try e.rec(38);
|
||||
},
|
||||
.cmpxchg => |c| {
|
||||
// CMPXCHG: [ptr, cmp, new, volatile, success ordering,
|
||||
// scope, failure ordering, weak]
|
||||
try e.pushValueAndType(f, inst_id, c.ptr);
|
||||
try e.pushValueAndType(f, inst_id, c.cmp);
|
||||
try e.pushValue(f, inst_id, c.new);
|
||||
try e.ops.appendSlice(e.gpa(), &.{ 0, 6, 1, 6, 0 });
|
||||
try e.rec(46);
|
||||
},
|
||||
.phi => |p| {
|
||||
try e.push(inst.ty);
|
||||
for (p.incoming) |in| {
|
||||
|
|
|
|||
|
|
@ -145,6 +145,7 @@ pub const PsvInfo = struct {
|
|||
resources: []const ResourceBind = &.{},
|
||||
inputs: []const signature.Element = &.{},
|
||||
outputs: []const signature.Element = &.{},
|
||||
groupshared_bytes: u32 = 0,
|
||||
};
|
||||
|
||||
pub fn psvCompute(gpa: Allocator, entry: []const u8, num_threads: [3]u32, resources: []const ResourceBind) ![]u8 {
|
||||
|
|
@ -211,7 +212,7 @@ pub fn psv(gpa: Allocator, info: PsvInfo) ![]u8 {
|
|||
// PSVRuntimeInfo3: EntryFunctionName
|
||||
try w(&buf, gpa, entry_off);
|
||||
// PSVRuntimeInfo4
|
||||
try w(&buf, gpa, 0); // NumBytesGroupSharedMemory
|
||||
try w(&buf, gpa, info.groupshared_bytes); // NumBytesGroupSharedMemory
|
||||
try w(&buf, gpa, 0); // Flags
|
||||
|
||||
try w(&buf, gpa, @intCast(info.resources.len));
|
||||
|
|
|
|||
|
|
@ -920,6 +920,12 @@ const Lowerer = struct {
|
|||
return l.b.add(.{ .op = .{ .access = .{ .base = base, .indices = try l.arena.dupe(Ref, &.{idx}) } }, .ty = e.ty, .ptr = l.b.f.inst(base).ptr });
|
||||
},
|
||||
.index => |x| {
|
||||
if (t.get(x.base.ty) == .resource) {
|
||||
// An element of a buffer or texture (atomics' `ref`).
|
||||
const base = try l.rvalue(x.base);
|
||||
const idx = try l.indexValue(x.index, t.get(x.base.ty).resource.kind.indexDims());
|
||||
return l.b.add(.{ .op = .{ .access = .{ .base = base, .indices = try l.arena.dupe(Ref, &.{idx}) } }, .ty = e.ty, .ptr = .resource });
|
||||
}
|
||||
const base = try l.lvalue(x.base);
|
||||
const idx = try l.converted(x.index, try t.scalar(.uint));
|
||||
return l.b.add(.{ .op = .{ .access = .{ .base = base, .indices = try l.arena.dupe(Ref, &.{idx}) } }, .ty = e.ty, .ptr = l.b.f.inst(base).ptr });
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@ pub const Op = enum(u16) {
|
|||
variable = 59,
|
||||
load = 61,
|
||||
store = 62,
|
||||
image_texel_pointer = 60,
|
||||
access_chain = 65,
|
||||
decorate = 71,
|
||||
member_decorate = 72,
|
||||
|
|
@ -72,6 +73,17 @@ pub const Op = enum(u16) {
|
|||
image_query_lod = 105,
|
||||
image_query_levels = 106,
|
||||
image_query_samples = 107,
|
||||
atomic_exchange = 229,
|
||||
atomic_compare_exchange = 230,
|
||||
atomic_i_add = 234,
|
||||
atomic_i_sub = 235,
|
||||
atomic_s_min = 236,
|
||||
atomic_u_min = 237,
|
||||
atomic_s_max = 238,
|
||||
atomic_u_max = 239,
|
||||
atomic_and = 240,
|
||||
atomic_or = 241,
|
||||
atomic_xor = 242,
|
||||
convert_f_to_u = 109,
|
||||
convert_f_to_s = 110,
|
||||
convert_s_to_f = 111,
|
||||
|
|
|
|||
|
|
@ -23,13 +23,14 @@ const Ref = ir.Ref;
|
|||
pub const Error = Allocator.Error || error{Unsupported};
|
||||
|
||||
/// SPIR-V storage classes.
|
||||
const StorageClass = enum(u32) { uniform_constant = 0, input = 1, uniform = 2, output = 3, workgroup = 4, private = 6, function = 7, storage_buffer = 12 };
|
||||
const StorageClass = enum(u32) { uniform_constant = 0, input = 1, uniform = 2, output = 3, workgroup = 4, private = 6, function = 7, image = 11, storage_buffer = 12 };
|
||||
|
||||
const Capability = enum(u32) {
|
||||
shader = 1,
|
||||
float16 = 9,
|
||||
float64 = 10,
|
||||
int64 = 11,
|
||||
int64_atomics = 12,
|
||||
int16 = 22,
|
||||
image_gather_extended = 25,
|
||||
storage_image_multisample = 27,
|
||||
|
|
@ -93,6 +94,10 @@ const Emitter = struct {
|
|||
image_types: std.AutoHashMapUnmanaged([6]u32, Id) = .empty,
|
||||
sampled_image_types: std.AutoHashMapUnmanaged(Id, Id) = .empty,
|
||||
sampler_type: Id = 0,
|
||||
counter_vars: []Id = &.{},
|
||||
/// Per instruction (current function): texel places (`tex[i]`).
|
||||
texel_places: std.AutoHashMapUnmanaged(u32, struct { res: Ref, coord: Ref, comp: Ref = .none }) = .empty,
|
||||
counter_type: Id = 0,
|
||||
resource_arrays: std.AutoHashMapUnmanaged([2]u32, Id) = .empty,
|
||||
extensions: std.ArrayList([]const u8) = .empty,
|
||||
/// Values selected with NonUniformResourceIndex (and resources reached
|
||||
|
|
@ -540,6 +545,70 @@ const Emitter = struct {
|
|||
else => return e.unsupported("global '{s}' is not supported yet", .{g.name}),
|
||||
}
|
||||
}
|
||||
|
||||
// Hidden counters (as dxc): a `{ int }` buffer per structured buffer
|
||||
// that uses one, on the next free binding of its set.
|
||||
e.counter_vars = try e.arena.alloc(Id, m.globals.items.len);
|
||||
@memset(e.counter_vars, 0);
|
||||
for (m.globals.items, 0..) |g, gi| {
|
||||
if (!e.needsCounter(@intCast(gi))) continue;
|
||||
if (e.counter_type == 0) {
|
||||
e.counter_type = e.b.newId();
|
||||
try e.b.emit(.types_globals, .type_struct, &.{ e.counter_type, try e.scalarType(.int) });
|
||||
try e.b.emit(.annotations, .member_decorate, &.{ e.counter_type, 0, 35, 0 }); // Offset 0
|
||||
try e.b.emit(.annotations, .decorate, &.{ e.counter_type, 3 }); // BufferBlock
|
||||
try e.b.emitWithString(.debug_names, .name, &.{e.counter_type}, "type.ACSBuffer.counter", &.{});
|
||||
}
|
||||
const var_id = e.b.newId();
|
||||
try e.b.emit(.types_globals, .variable, &.{ try e.pointerType(.uniform, e.counter_type), var_id, @backingInt(StorageClass.uniform) });
|
||||
while (used.contains(.{ g.binding.space, next_free })) next_free += 1;
|
||||
try used.put(e.arena, .{ g.binding.space, next_free }, {});
|
||||
try e.b.emit(.annotations, .decorate, &.{ var_id, 34, g.binding.space }); // DescriptorSet
|
||||
try e.b.emit(.annotations, .decorate, &.{ var_id, 33, next_free }); // Binding
|
||||
try e.b.emitWithString(.debug_names, .name, &.{var_id}, try std.fmt.allocPrint(e.arena, "counter.var.{s}", .{g.name}), &.{});
|
||||
e.counter_vars[gi] = var_id;
|
||||
}
|
||||
}
|
||||
|
||||
/// Append/Consume buffers, and structured buffers whose counter is
|
||||
/// incremented or decremented somewhere.
|
||||
fn needsCounter(e: *Emitter, gi: u32) bool {
|
||||
const r = switch (e.t.get(e.m.globals.items[gi].ty)) {
|
||||
.resource => |r| r,
|
||||
else => return false,
|
||||
};
|
||||
if (r.kind == .append_structured_buffer or r.kind == .consume_structured_buffer) return true;
|
||||
if (r.kind != .rw_structured_buffer) return false;
|
||||
for (e.m.functions.items) |f| for (f.insts.items) |inst| switch (inst.op) {
|
||||
.method => |mt| {
|
||||
if (!std.mem.eql(u8, mt.name, "IncrementCounter") and !std.mem.eql(u8, mt.name, "DecrementCounter")) continue;
|
||||
var x = mt.res;
|
||||
while (true) switch (f.inst(x).op) {
|
||||
.load => |p| x = p,
|
||||
.global => |g| {
|
||||
if (g == gi) return true;
|
||||
break;
|
||||
},
|
||||
else => break,
|
||||
};
|
||||
},
|
||||
else => {},
|
||||
};
|
||||
return false;
|
||||
}
|
||||
|
||||
/// The counter update for IncrementCounter / DecrementCounter / Append /
|
||||
/// Consume: returns the index (or counter value) as uint.
|
||||
fn counterOp(e: *Emitter, res: Ref, increment: bool, post: bool) Error!Id {
|
||||
const g = e.globalOf(res) orelse return e.unsupported("counters of resources not tied to a global are not supported yet", .{});
|
||||
const cv = e.counter_vars[g];
|
||||
if (cv == 0) return e.unsupported("this buffer has no counter", .{});
|
||||
const int_ty = try e.t.scalar(.int);
|
||||
const ptr = try e.opRaw(.access_chain, try e.pointerType(.uniform, try e.scalarType(.int)), &.{ cv, try e.constUint(0) });
|
||||
const old = try e.op1(if (increment) .atomic_i_add else .atomic_i_sub, int_ty, &.{ ptr, try e.constUint(1), try e.constUint(0), try e.constInt(1) });
|
||||
// Decrements report the new value.
|
||||
const v = if (post) try e.op1(.i_sub, int_ty, &.{ old, try e.constInt(1) }) else old;
|
||||
return e.op1(.bitcast, try e.t.scalar(.uint), &.{v});
|
||||
}
|
||||
|
||||
/// `float`, `v4float`: dxc's type names inside buffer type names.
|
||||
|
|
@ -666,6 +735,7 @@ const Emitter = struct {
|
|||
|
||||
fn emitFunction(e: *Emitter, f: *const ir.Function, fn_id: Id, bindings: []const Id) Error!void {
|
||||
e.f = f;
|
||||
e.texel_places = .empty;
|
||||
e.vals = try e.arena.alloc(Id, f.insts.items.len);
|
||||
@memset(e.vals, 0);
|
||||
e.ptr_layouts = try e.arena.alloc(?PtrLayout, f.insts.items.len);
|
||||
|
|
@ -861,6 +931,16 @@ const Emitter = struct {
|
|||
};
|
||||
},
|
||||
.load => |p| {
|
||||
if (e.texel_places.get(p.index())) |tp| {
|
||||
if (tp.comp == .none) {
|
||||
e.vals[ri] = try e.imageIndexLoad(tp.res, e.val(tp.coord), i.ty);
|
||||
} else {
|
||||
const elem = e.resourceInfo(tp.res).elem;
|
||||
const whole = try e.imageIndexLoad(tp.res, e.val(tp.coord), elem);
|
||||
e.vals[ri] = if (e.isScalarLike(elem)) whole else try e.op1(.vector_extract_dynamic, i.ty, &.{ whole, e.val(tp.comp) });
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Resources stay as their variables (accessed by chains).
|
||||
if (i.ptr == .none and (e.t.get(i.ty) == .resource or e.t.get(i.ty) == .sampler)) {
|
||||
e.vals[ri] = e.val(p);
|
||||
|
|
@ -874,6 +954,10 @@ const Emitter = struct {
|
|||
e.vals[ri] = try e.op1(.load, i.ty, &.{e.val(p)});
|
||||
},
|
||||
.store => |s| {
|
||||
if (e.texel_places.get(s.ptr.index())) |tp| {
|
||||
if (tp.comp != .none) return e.unsupported("storing one component of a texel is not supported yet", .{});
|
||||
return e.imageWrite(tp.res, e.val(tp.coord), e.val(s.value));
|
||||
}
|
||||
const target = f.inst(s.ptr);
|
||||
if (target.op == .local_var and isResourceType(e.t, target.ty)) {
|
||||
const old = e.vals[s.ptr.index()];
|
||||
|
|
@ -890,6 +974,18 @@ const Emitter = struct {
|
|||
},
|
||||
.access => |a| {
|
||||
if (e.t.get(f.inst(a.base).ty) == .array and isResourceType(e.t, f.inst(a.base).ty)) return e.resourceArrayAccess(ri, a, i.ty);
|
||||
if (e.texel_places.get(a.base.index())) |tp| {
|
||||
// A component of a texel.
|
||||
if (tp.comp != .none or a.indices.len != 1) return e.unsupported("this texel access is not supported yet", .{});
|
||||
try e.texel_places.put(e.arena, ri, .{ .res = tp.res, .coord = tp.coord, .comp = a.indices[0] });
|
||||
return;
|
||||
}
|
||||
if (e.isImage(a.base)) {
|
||||
// A texel: read and written as image accesses, or the
|
||||
// pointer of an atomic (made on use).
|
||||
try e.texel_places.put(e.arena, ri, .{ .res = a.base, .coord = a.indices[0] });
|
||||
return;
|
||||
}
|
||||
// Buffers reached through locals or parameters are still
|
||||
// Uniform std430 storage.
|
||||
const into_buffer = e.t.get(f.inst(a.base).ty) == .resource;
|
||||
|
|
@ -1190,7 +1286,7 @@ const Emitter = struct {
|
|||
};
|
||||
const elem_ty = switch (r.kind) {
|
||||
.byte_address_buffer, .rw_byte_address_buffer => try e.t.scalar(.uint),
|
||||
.structured_buffer, .rw_structured_buffer => r.elem,
|
||||
.structured_buffer, .rw_structured_buffer, .append_structured_buffer, .consume_structured_buffer => r.elem,
|
||||
else => return null,
|
||||
};
|
||||
if (e.isTrueMatrix(elem_ty)) return e.unsupported("buffers of matrices are not supported yet", .{});
|
||||
|
|
@ -1211,7 +1307,7 @@ const Emitter = struct {
|
|||
const st = e.b.newId();
|
||||
try e.b.emit(.types_globals, .type_struct, &.{ st, rt });
|
||||
try e.b.emit(.annotations, .member_decorate, &.{ st, 0, 35, 0 }); // Offset 0
|
||||
if (!r.kind.isWritable()) try e.b.emit(.annotations, .member_decorate, &.{ st, 0, 24 }); // NonWritable
|
||||
if (!r.kind.isWritable() and r.kind != .consume_structured_buffer) try e.b.emit(.annotations, .member_decorate, &.{ st, 0, 24 }); // NonWritable
|
||||
try e.b.emit(.annotations, .decorate, &.{ st, 3 }); // BufferBlock
|
||||
try e.b.emitWithString(.debug_names, .name, &.{st}, try std.fmt.allocPrint(e.arena, "type.{s}.{s}", .{ r.name, try e.typeLabel(elem_ty) }), &.{});
|
||||
try e.buffer_blocks.put(e.arena, ty, st);
|
||||
|
|
@ -1233,7 +1329,7 @@ const Emitter = struct {
|
|||
switch (e.t.get(ty)) {
|
||||
.sampler => return .{ .sc = .uniform_constant, .ty = try e.samplerType() },
|
||||
.resource => |r| switch (r.kind) {
|
||||
.structured_buffer, .rw_structured_buffer, .byte_address_buffer, .rw_byte_address_buffer => return .{ .sc = .uniform, .ty = try e.bufferBlockType(ty) },
|
||||
.structured_buffer, .rw_structured_buffer, .append_structured_buffer, .consume_structured_buffer, .byte_address_buffer, .rw_byte_address_buffer => return .{ .sc = .uniform, .ty = try e.bufferBlockType(ty) },
|
||||
else => if (texture.shape(r.kind)) |sh| return .{ .sc = .uniform_constant, .ty = try e.imageType(sh, r.elem) },
|
||||
},
|
||||
.array => |arr| {
|
||||
|
|
@ -1287,7 +1383,7 @@ const Emitter = struct {
|
|||
const c: Capability = switch (e.t.get(et)) {
|
||||
.sampler => .sampled_image_array_non_uniform_indexing,
|
||||
.resource => |r| switch (r.kind) {
|
||||
.structured_buffer, .rw_structured_buffer, .byte_address_buffer, .rw_byte_address_buffer => .storage_buffer_array_non_uniform_indexing,
|
||||
.structured_buffer, .rw_structured_buffer, .append_structured_buffer, .consume_structured_buffer, .byte_address_buffer, .rw_byte_address_buffer => .storage_buffer_array_non_uniform_indexing,
|
||||
.buffer => .uniform_texel_buffer_array_non_uniform_indexing,
|
||||
.rw_buffer => .storage_texel_buffer_array_non_uniform_indexing,
|
||||
else => if (r.kind.isWritable()) .storage_image_array_non_uniform_indexing else .sampled_image_array_non_uniform_indexing,
|
||||
|
|
@ -1474,6 +1570,21 @@ const Emitter = struct {
|
|||
try e.getDimensions(res, r, args);
|
||||
return 0;
|
||||
}
|
||||
if (r.kind == .byte_address_buffer or r.kind == .rw_byte_address_buffer) return e.byteAddressMethod(name, res, args, ty);
|
||||
if (eq(u8, name, "IncrementCounter")) return e.counterOp(res, true, false);
|
||||
if (eq(u8, name, "DecrementCounter")) return e.counterOp(res, false, true);
|
||||
if (eq(u8, name, "Append") or eq(u8, name, "Consume")) {
|
||||
const idx = try e.counterOp(res, eq(u8, name, "Append"), eq(u8, name, "Consume"));
|
||||
const elem = (try e.bufferElemType(e.f.inst(res).ty)).?;
|
||||
const ptr = try e.opRaw(.access_chain, try e.pointerType(.uniform, elem), &.{ e.val(res), try e.constUint(0), idx });
|
||||
const ety = r.elem;
|
||||
if (eq(u8, name, "Append")) {
|
||||
try e.b.emit(.functions, .store, &.{ ptr, try e.toLayout(e.val(args[0]), e.f.inst(args[0]).ty, .std430, false) });
|
||||
_ = ety;
|
||||
return 0;
|
||||
}
|
||||
return e.fromLayout(try e.opRaw(.load, elem, &.{ptr}), ty, .std430, false);
|
||||
}
|
||||
if (eq(u8, name, "Load") and (r.kind == .structured_buffer or r.kind == .rw_structured_buffer)) {
|
||||
const ptr = try e.bufferElement(res, args[0]);
|
||||
const raw = try e.opRaw(.load, try e.layoutTypeId(ty, .std430, false), &.{ptr});
|
||||
|
|
@ -1586,6 +1697,42 @@ const Emitter = struct {
|
|||
return e.texelResult(v, s, ty);
|
||||
}
|
||||
|
||||
/// Load/Store(2..4) and atomics on byte-address buffers (uint words).
|
||||
fn byteAddressMethod(e: *Emitter, name: []const u8, res: Ref, args: []const Ref, ty: TypeId) Error!Id {
|
||||
const uint_ty = try e.t.scalar(.uint);
|
||||
const word = struct {
|
||||
fn f(em: *Emitter, base: Id, off: Id, k: u32) Error!Id {
|
||||
const ut = try em.t.scalar(.uint);
|
||||
var idx = try em.op1(.shift_right_logical, ut, &.{ off, try em.constUint(2) });
|
||||
if (k > 0) idx = try em.op1(.i_add, ut, &.{ idx, try em.constUint(k) });
|
||||
return em.opRaw(.access_chain, try em.pointerType(.uniform, try em.scalarType(.uint)), &.{ base, try em.constInt(0), idx });
|
||||
}
|
||||
}.f;
|
||||
const off = try e.convert(e.val(args[0]), e.f.inst(args[0]).ty, uint_ty);
|
||||
if (std.mem.startsWith(u8, name, "Load") and args.len == 1) {
|
||||
const n = e.t.componentCount(ty) orelse return e.unsupported("templated Load is not supported yet", .{});
|
||||
if (e.t.component(ty).? != .uint) return e.unsupported("templated Load is not supported yet", .{});
|
||||
const parts = try e.arena.alloc(Id, n);
|
||||
for (parts, 0..) |*p, k| p.* = try e.op1(.load, uint_ty, &.{try word(e, e.val(res), off, @intCast(k))});
|
||||
return if (n == 1) parts[0] else e.op1(.composite_construct, ty, parts);
|
||||
}
|
||||
if (std.mem.startsWith(u8, name, "Store") and args.len == 2) {
|
||||
const vty = e.f.inst(args[1]).ty;
|
||||
const n = e.t.componentCount(vty) orelse return e.unsupported("templated Store is not supported yet", .{});
|
||||
if (e.t.component(vty).? != .uint) return e.unsupported("templated Store is not supported yet", .{});
|
||||
for (0..n) |k| {
|
||||
const v = if (n == 1) e.val(args[1]) else try e.op1(.composite_extract, uint_ty, &.{ e.val(args[1]), @intCast(k) });
|
||||
try e.b.emit(.functions, .store, &.{ try word(e, e.val(res), off, @intCast(k)), v });
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (std.mem.startsWith(u8, name, "Interlocked")) {
|
||||
if (e.t.component(e.f.inst(args[1]).ty).?.size() != 4) return e.unsupported("64-bit '{s}' is not supported yet", .{name});
|
||||
return e.atomicOp(name["Interlocked".len..], try word(e, e.val(res), off, 0), uint_ty, 1, args[1..]);
|
||||
}
|
||||
return e.unsupported("method '{s}' is not supported yet", .{name});
|
||||
}
|
||||
|
||||
fn getDimensions(e: *Emitter, res: Ref, r: @FieldType(types.Type, "resource"), args: []const Ref) Error!void {
|
||||
const uint_ty = try e.t.scalar(.uint);
|
||||
const store = struct {
|
||||
|
|
@ -1596,7 +1743,7 @@ const Emitter = struct {
|
|||
}
|
||||
}.f;
|
||||
switch (r.kind) {
|
||||
.structured_buffer, .rw_structured_buffer, .byte_address_buffer, .rw_byte_address_buffer => {
|
||||
.structured_buffer, .rw_structured_buffer, .append_structured_buffer, .consume_structured_buffer, .byte_address_buffer, .rw_byte_address_buffer => {
|
||||
const len = try e.op1(.array_length, uint_ty, &.{ e.val(res), 0 });
|
||||
if (r.kind == .byte_address_buffer or r.kind == .rw_byte_address_buffer) {
|
||||
try store(e, args[0], try e.op1(.i_mul, uint_ty, &.{ len, try e.constUint(4) }));
|
||||
|
|
@ -1794,6 +1941,16 @@ const Emitter = struct {
|
|||
try e.b.emit(.functions, .store, &.{ e.val(args[1]), try e.convert(bv, xty, e.f.inst(args[1]).ty) });
|
||||
return av;
|
||||
}
|
||||
if (std.mem.startsWith(u8, name, "Interlocked")) {
|
||||
const dest = e.f.inst(args[0]);
|
||||
const scope: u32 = if (dest.ptr == .groupshared) 2 else 1; // Workgroup / Device
|
||||
var ptr = e.val(args[0]);
|
||||
if (e.texel_places.get(args[0].index())) |tp| {
|
||||
const s_ = try e.texelScalar(e.resourceInfo(tp.res).elem);
|
||||
ptr = try e.opRaw(.image_texel_pointer, try e.pointerType(.image, try e.scalarType(s_)), &.{ e.val(tp.res), e.val(tp.coord), try e.constUint(0) });
|
||||
}
|
||||
return try e.atomicOp(name["Interlocked".len..], ptr, dest.ty, scope, args[1..]);
|
||||
}
|
||||
if (eq(u8, name, "asuint") and args.len == 3) {
|
||||
const n = e.t.componentCount(xty).?;
|
||||
if (n != 1) return e.unsupported("asuint of double vectors is not supported yet", .{});
|
||||
|
|
@ -1806,6 +1963,38 @@ const Emitter = struct {
|
|||
return null;
|
||||
}
|
||||
|
||||
/// An atomic on `ptr` (to a `dty` scalar); `rest` holds the value(s)
|
||||
/// and, last, the optional out original.
|
||||
fn atomicOp(e: *Emitter, op_name: []const u8, ptr: Id, dty: TypeId, scope: u32, rest: []const Ref) Error!Id {
|
||||
const eq = std.mem.eql;
|
||||
const dc = e.t.component(dty) orelse return e.unsupported("atomics on this destination are not supported yet", .{});
|
||||
if (dc.isFloat()) return e.unsupported("float atomics are not supported yet", .{});
|
||||
if (dc.size() == 8) try e.cap(.int64_atomics);
|
||||
const is_cmp = std.mem.startsWith(u8, op_name, "Compare");
|
||||
const asDest = struct {
|
||||
fn f(em: *Emitter, r: Ref, t: TypeId) Error!Id {
|
||||
return em.convert(em.val(r), em.f.inst(r).ty, t);
|
||||
}
|
||||
}.f;
|
||||
const scope_id = try e.constUint(scope);
|
||||
const sem = try e.constUint(0);
|
||||
var orig: Id = undefined;
|
||||
var n_in: usize = 1;
|
||||
if (is_cmp) {
|
||||
n_in = 2;
|
||||
orig = try e.op1(.atomic_compare_exchange, dty, &.{ ptr, scope_id, sem, sem, try asDest(e, rest[1], dty), try asDest(e, rest[0], dty) });
|
||||
} else {
|
||||
const op: Op = if (eq(u8, op_name, "Add")) .atomic_i_add else if (eq(u8, op_name, "And")) .atomic_and else if (eq(u8, op_name, "Or")) .atomic_or else if (eq(u8, op_name, "Xor")) .atomic_xor else if (eq(u8, op_name, "Min")) (if (dc.isUnsigned()) .atomic_u_min else .atomic_s_min) else if (eq(u8, op_name, "Max")) (if (dc.isUnsigned()) .atomic_u_max else .atomic_s_max) else if (eq(u8, op_name, "Exchange")) .atomic_exchange else return e.unsupported("'Interlocked{s}' is not supported yet", .{op_name});
|
||||
orig = try e.op1(op, dty, &.{ ptr, scope_id, sem, try asDest(e, rest[0], dty) });
|
||||
}
|
||||
const has_orig = if (is_cmp) eq(u8, op_name, "CompareExchange") else rest.len > n_in;
|
||||
if (has_orig) {
|
||||
const out = rest[rest.len - 1];
|
||||
try e.b.emit(.functions, .store, &.{ e.val(out), try e.convert(orig, dty, e.f.inst(out).ty) });
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
fn intrinsic(e: *Emitter, name: []const u8, ty: TypeId, args: []const Ref) Error!Id {
|
||||
if (try e.outIntrinsic(name, ty, args)) |id| return id;
|
||||
if (std.mem.eql(u8, name, "NonUniformResourceIndex")) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue