Resources in structs (DXIL), global structs of resources (both)
- DXIL: structs and arrays holding resources as values (data plus the resources, statically known) and as locals (data in memory, a resource slot per member): copies, nested structs, arrays, init lists, function arguments and returns, and functions returning resources. - Global structs with resources: each resource is its own global named by its path (g.tex), bound like other unbound resources; data members stay in $Globals with resources taking no space in the layout. - SPIR-V: struct types drop resource members (member indices remapped); a global struct of textures and samplers gets a binding per resource and turns on per-element array bindings, as dxc does; dxc's errors for structs with buffers or mixed members. - LinAlg vectors of one element stay native vectors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
dc90b606a3
commit
99e3591481
|
|
@ -46,6 +46,13 @@ const Lowered = union(enum) {
|
||||||
none,
|
none,
|
||||||
/// Flattened scalar components.
|
/// Flattened scalar components.
|
||||||
scalars: []const Value,
|
scalars: []const Value,
|
||||||
|
/// A struct or array value holding resources: its data components
|
||||||
|
/// (flattened, resources counting none) and its resources, in order.
|
||||||
|
agg: struct { vals: []const Value, res: []const Lowered },
|
||||||
|
/// A local holding resources: its data storage (.ptr or .typed; null
|
||||||
|
/// when it has no data) and one resource slot (Emitter.res_locals)
|
||||||
|
/// per resource, each an alias of what is stored into it.
|
||||||
|
agg_ptr: struct { data: ?*const Lowered, slots: []const u32, ty: TypeId },
|
||||||
ptr: Ptr,
|
ptr: Ptr,
|
||||||
/// A resource handle (index into Emitter.resources).
|
/// A resource handle (index into Emitter.resources).
|
||||||
handle: u32,
|
handle: u32,
|
||||||
|
|
@ -136,6 +143,8 @@ const Frame = struct {
|
||||||
/// For inlined calls: where `ret` goes and the incoming return values.
|
/// For inlined calls: where `ret` goes and the incoming return values.
|
||||||
cont: ?u32 = null,
|
cont: ?u32 = null,
|
||||||
rets: std.ArrayList(struct { block: u32, vals: []const Value }) = .empty,
|
rets: std.ArrayList(struct { block: u32, vals: []const Value }) = .empty,
|
||||||
|
/// The resources a function returns (every return the same ones).
|
||||||
|
ret_res: ?[]const Lowered = null,
|
||||||
};
|
};
|
||||||
|
|
||||||
const PInst = struct { inst: module.Inst, id: u32 };
|
const PInst = struct { inst: module.Inst, id: u32 };
|
||||||
|
|
@ -187,6 +196,8 @@ const Emitter = struct {
|
||||||
/// SM 6.10 matrix types used (`!dx.targetTypes`), in first-use order.
|
/// SM 6.10 matrix types used (`!dx.targetTypes`), in first-use order.
|
||||||
linalg_types: std.ArrayList(struct { ty: TypeId, dty: DType }) = .empty,
|
linalg_types: std.ArrayList(struct { ty: TypeId, dty: DType }) = .empty,
|
||||||
uses_linalg: bool = false,
|
uses_linalg: bool = false,
|
||||||
|
/// The resources the last inlined call returned.
|
||||||
|
last_ret_res: ?[]const Lowered = null,
|
||||||
/// RayQuery locals: the query handle (allocated in the prologue on
|
/// RayQuery locals: the query handle (allocated in the prologue on
|
||||||
/// first use, or a copy of another query) and the query type.
|
/// first use, or a copy of another query) and the query type.
|
||||||
rq_locals: std.ArrayList(struct { h: ?Value, ty: TypeId }) = .empty,
|
rq_locals: std.ArrayList(struct { h: ?Value, ty: TypeId }) = .empty,
|
||||||
|
|
@ -304,8 +315,10 @@ const Emitter = struct {
|
||||||
/// Number of flattened scalar components of `ty`.
|
/// Number of flattened scalar components of `ty`.
|
||||||
fn flatCount(e: *Emitter, ty: TypeId) Error!u32 {
|
fn flatCount(e: *Emitter, ty: TypeId) Error!u32 {
|
||||||
return switch (e.t.get(ty)) {
|
return switch (e.t.get(ty)) {
|
||||||
// (A matrix handle is one opaque component.)
|
// (A matrix handle is one opaque component; resources are not
|
||||||
|
// data.)
|
||||||
.scalar, .@"enum", .linalg => 1,
|
.scalar, .@"enum", .linalg => 1,
|
||||||
|
.resource, .sampler => 0,
|
||||||
.vector => |v| v.len,
|
.vector => |v| v.len,
|
||||||
.matrix => |m| @as(u32, m.rows) * m.cols,
|
.matrix => |m| @as(u32, m.rows) * m.cols,
|
||||||
.array => |a| a.len * try e.flatCount(a.elem),
|
.array => |a| a.len * try e.flatCount(a.elem),
|
||||||
|
|
@ -326,6 +339,7 @@ const Emitter = struct {
|
||||||
.@"struct" => |s| blk: {
|
.@"struct" => |s| blk: {
|
||||||
var first: ?Scalar = null;
|
var first: ?Scalar = null;
|
||||||
for (s.fields.items) |f| {
|
for (s.fields.items) |f| {
|
||||||
|
if (try e.flatCount(f.type) == 0) continue;
|
||||||
const c = try e.uniformScalar(f.type);
|
const c = try e.uniformScalar(f.type);
|
||||||
if (first != null and first.? != c) return e.unsupported("structs with mixed component types are not supported yet", .{});
|
if (first != null and first.? != c) return e.unsupported("structs with mixed component types are not supported yet", .{});
|
||||||
first = c;
|
first = c;
|
||||||
|
|
@ -1270,6 +1284,7 @@ const Emitter = struct {
|
||||||
e.cur = try e.blockFor(&frame, @intCast(bi));
|
e.cur = try e.blockFor(&frame, @intCast(bi));
|
||||||
try e.emitBlock(&frame, @intCast(bi));
|
try e.emitBlock(&frame, @intCast(bi));
|
||||||
}
|
}
|
||||||
|
e.last_ret_res = frame.ret_res;
|
||||||
if (frame.cont) |cont| {
|
if (frame.cont) |cont| {
|
||||||
e.cur = cont;
|
e.cur = cont;
|
||||||
if (f.ret == TypeTable.void_type or frame.rets.items.len == 0) return &.{};
|
if (f.ret == TypeTable.void_type or frame.rets.items.len == 0) return &.{};
|
||||||
|
|
@ -1316,7 +1331,18 @@ const Emitter = struct {
|
||||||
switch (blk.term) {
|
switch (blk.term) {
|
||||||
.ret => |v| {
|
.ret => |v| {
|
||||||
if (frame.cont) |cont| {
|
if (frame.cont) |cont| {
|
||||||
if (v != .none) try frame.rets.append(e.arena, .{ .block = e.cur, .vals = try e.scalars(frame, v) });
|
if (v != .none) {
|
||||||
|
const rty = frame.f.inst(v).ty;
|
||||||
|
if ((isResourceType(e.t, rty) and !isRayQuery(e.t, rty)) or e.holdsResources(rty)) {
|
||||||
|
const parts = try e.aggParts(frame.vals[v.index()], rty);
|
||||||
|
if (frame.ret_res) |old| {
|
||||||
|
if (old.len != parts.res.len) return e.unsupported("returning different resources from one function is not supported yet", .{});
|
||||||
|
for (old, parts.res) |a, b| if (!std.meta.eql(a, b)) return e.unsupported("returning different resources from one function is not supported yet", .{});
|
||||||
|
}
|
||||||
|
frame.ret_res = parts.res;
|
||||||
|
try frame.rets.append(e.arena, .{ .block = e.cur, .vals = parts.vals });
|
||||||
|
} else try frame.rets.append(e.arena, .{ .block = e.cur, .vals = try e.scalars(frame, v) });
|
||||||
|
}
|
||||||
_ = try e.add(void_ty, .{ .br = .{ .then = cont } });
|
_ = try e.add(void_ty, .{ .br = .{ .then = cont } });
|
||||||
} else _ = try e.add(void_ty, .{ .ret = null });
|
} else _ = try e.add(void_ty, .{ .ret = null });
|
||||||
},
|
},
|
||||||
|
|
@ -1484,6 +1510,7 @@ const Emitter = struct {
|
||||||
try e.res_locals.append(e.arena, null);
|
try e.res_locals.append(e.arena, null);
|
||||||
return .{ .res_local = @intCast(e.res_locals.items.len - 1) };
|
return .{ .res_local = @intCast(e.res_locals.items.len - 1) };
|
||||||
}
|
}
|
||||||
|
if (e.holdsResources(i.ty)) return e.aggLocal(i.ty);
|
||||||
if (try e.mixedSizes(i.ty) or e.containsLinAlg(i.ty)) {
|
if (try e.mixedSizes(i.ty) or e.containsLinAlg(i.ty)) {
|
||||||
// Components of several sizes (or matrix handles): a
|
// Components of several sizes (or matrix handles): a
|
||||||
// struct-typed alloca.
|
// struct-typed alloca.
|
||||||
|
|
@ -1495,6 +1522,16 @@ const Emitter = struct {
|
||||||
},
|
},
|
||||||
.load => |p| switch (frame.vals[p.index()]) {
|
.load => |p| switch (frame.vals[p.index()]) {
|
||||||
.handle => |h| return .{ .handle = h },
|
.handle => |h| return .{ .handle = h },
|
||||||
|
.agg_ptr => |ap| {
|
||||||
|
const vals: []const Value = if (ap.data) |d| switch (d.*) {
|
||||||
|
.ptr => |ptr| (try e.loadPtr(ptr, ap.ty)).scalars,
|
||||||
|
.typed => |tp| (try e.typedLoad(tp, ap.ty)).scalars,
|
||||||
|
else => unreachable,
|
||||||
|
} else &.{};
|
||||||
|
const res = try e.arena.alloc(Lowered, ap.slots.len);
|
||||||
|
for (res, ap.slots) |*o, sl| o.* = e.res_locals.items[sl] orelse return e.unsupported("a resource in a local read before it is assigned", .{});
|
||||||
|
return .{ .agg = .{ .vals = vals, .res = res } };
|
||||||
|
},
|
||||||
.rq_local => |k| return .{ .ray_query = try e.rqHandle(k) },
|
.rq_local => |k| return .{ .ray_query = try e.rqHandle(k) },
|
||||||
.res_local => |k| return e.res_locals.items[k] orelse return e.unsupported("a resource local read before it is assigned", .{}),
|
.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 },
|
.res_array => |ra| return .{ .res_array = ra },
|
||||||
|
|
@ -1553,6 +1590,10 @@ const Emitter = struct {
|
||||||
slot.* = h;
|
slot.* = h;
|
||||||
return .none;
|
return .none;
|
||||||
}
|
}
|
||||||
|
if (frame.vals[s.ptr.index()] == .agg_ptr) {
|
||||||
|
try e.aggStore(frame.vals[s.ptr.index()].agg_ptr, frame.vals[s.value.index()]);
|
||||||
|
return .none;
|
||||||
|
}
|
||||||
if (frame.vals[s.ptr.index()] == .mesh) {
|
if (frame.vals[s.ptr.index()] == .mesh) {
|
||||||
try e.meshStore(frame.vals[s.ptr.index()].mesh, try e.scalars(frame, s.value), f.inst(s.value).ty);
|
try e.meshStore(frame.vals[s.ptr.index()].mesh, try e.scalars(frame, s.value), f.inst(s.value).ty);
|
||||||
return .none;
|
return .none;
|
||||||
|
|
@ -1593,6 +1634,7 @@ const Emitter = struct {
|
||||||
tp.path = path.items;
|
tp.path = path.items;
|
||||||
return .{ .typed = tp };
|
return .{ .typed = tp };
|
||||||
}
|
}
|
||||||
|
if (frame.vals[a.base.index()] == .agg_ptr) return e.aggAccess(frame, frame.vals[a.base.index()].agg_ptr, a.indices);
|
||||||
if (frame.vals[a.base.index()] == .res_array) return e.arrayElement(frame, frame.vals[a.base.index()].res_array, a.indices);
|
if (frame.vals[a.base.index()] == .res_array) return e.arrayElement(frame, frame.vals[a.base.index()].res_array, a.indices);
|
||||||
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()] == .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) {
|
if (frame.vals[a.base.index()] == .handle) {
|
||||||
|
|
@ -1706,12 +1748,24 @@ const Emitter = struct {
|
||||||
return .{ .scalars = out };
|
return .{ .scalars = out };
|
||||||
},
|
},
|
||||||
.extract => |x| {
|
.extract => |x| {
|
||||||
|
if (frame.vals[x.agg.index()] == .agg) return e.aggExtract(frame.vals[x.agg.index()].agg, f.inst(x.agg).ty, x.index);
|
||||||
const v = try e.scalars(frame, x.agg);
|
const v = try e.scalars(frame, x.agg);
|
||||||
const at = f.inst(x.agg).ty;
|
const at = f.inst(x.agg).ty;
|
||||||
const range = try e.componentRange(at, x.index);
|
const range = try e.componentRange(at, x.index);
|
||||||
return e.list(v[range[0]..range[1]]);
|
return e.list(v[range[0]..range[1]]);
|
||||||
},
|
},
|
||||||
.insert => |x| {
|
.insert => |x| {
|
||||||
|
if (e.holdsResources(i.ty)) {
|
||||||
|
const whole = try e.aggParts(frame.vals[x.agg.index()], i.ty);
|
||||||
|
const part = try e.aggParts(frame.vals[x.value.index()], f.inst(x.value).ty);
|
||||||
|
const vals = try e.arena.dupe(Value, whole.vals);
|
||||||
|
const res = try e.arena.dupe(Lowered, whole.res);
|
||||||
|
const dr = try e.componentRange(i.ty, x.index);
|
||||||
|
const rr = try e.resRange(i.ty, x.index);
|
||||||
|
@memcpy(vals[dr[0]..dr[1]], part.vals);
|
||||||
|
@memcpy(res[rr[0]..rr[1]], part.res);
|
||||||
|
return .{ .agg = .{ .vals = vals, .res = res } };
|
||||||
|
}
|
||||||
const v = try e.scalars(frame, x.agg);
|
const v = try e.scalars(frame, x.agg);
|
||||||
const out = try e.arena.dupe(Value, v);
|
const out = try e.arena.dupe(Value, v);
|
||||||
const range = try e.componentRange(i.ty, x.index);
|
const range = try e.componentRange(i.ty, x.index);
|
||||||
|
|
@ -1720,6 +1774,16 @@ const Emitter = struct {
|
||||||
return .{ .scalars = out };
|
return .{ .scalars = out };
|
||||||
},
|
},
|
||||||
.construct => |xs| {
|
.construct => |xs| {
|
||||||
|
if (e.holdsResources(i.ty)) {
|
||||||
|
var vals: std.ArrayList(Value) = .empty;
|
||||||
|
var res: std.ArrayList(Lowered) = .empty;
|
||||||
|
for (xs) |x| {
|
||||||
|
const part = try e.aggParts(frame.vals[x.index()], f.inst(x).ty);
|
||||||
|
try vals.appendSlice(e.arena, part.vals);
|
||||||
|
try res.appendSlice(e.arena, part.res);
|
||||||
|
}
|
||||||
|
return .{ .agg = .{ .vals = vals.items, .res = res.items } };
|
||||||
|
}
|
||||||
var out: std.ArrayList(Value) = .empty;
|
var out: std.ArrayList(Value) = .empty;
|
||||||
for (xs) |x| try out.appendSlice(e.arena, try e.scalars(frame, x));
|
for (xs) |x| try out.appendSlice(e.arena, try e.scalars(frame, x));
|
||||||
return .{ .scalars = out.items };
|
return .{ .scalars = out.items };
|
||||||
|
|
@ -1743,6 +1807,12 @@ const Emitter = struct {
|
||||||
const args = try e.arena.alloc(Lowered, c.args.len);
|
const args = try e.arena.alloc(Lowered, c.args.len);
|
||||||
for (c.args, args) |a, *o| o.* = frame.vals[a.index()];
|
for (c.args, args) |a, *o| o.* = frame.vals[a.index()];
|
||||||
const res = try e.inlineCall(c.func, args);
|
const res = try e.inlineCall(c.func, args);
|
||||||
|
// Functions returning resources (or structs of them).
|
||||||
|
if (isResourceType(e.t, i.ty) and !isRayQuery(e.t, i.ty)) {
|
||||||
|
const rr = e.last_ret_res orelse return e.unsupported("a function returning a resource with no return value", .{});
|
||||||
|
return rr[0];
|
||||||
|
}
|
||||||
|
if (e.holdsResources(i.ty)) return .{ .agg = .{ .vals = res, .res = e.last_ret_res orelse return e.unsupported("a function returning resources with no return value", .{}) } };
|
||||||
return if (res.len == 0) .none else .{ .scalars = res };
|
return if (res.len == 0) .none else .{ .scalars = res };
|
||||||
},
|
},
|
||||||
.intrinsic => |c| return e.intrinsic(frame, c.name, i.ty, c.args),
|
.intrinsic => |c| return e.intrinsic(frame, c.name, i.ty, c.args),
|
||||||
|
|
@ -1775,6 +1845,168 @@ const Emitter = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Components [start, end) of member / element / component `index`.
|
/// Components [start, end) of member / element / component `index`.
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
// Structs and arrays holding resources
|
||||||
|
|
||||||
|
/// Resources in a value of `ty`, in flattened order.
|
||||||
|
fn resCount(e: *Emitter, ty: TypeId) u32 {
|
||||||
|
return switch (e.t.get(ty)) {
|
||||||
|
.resource, .sampler => if (isRayQuery(e.t, ty)) 0 else 1,
|
||||||
|
.array => |a| a.len * e.resCount(a.elem),
|
||||||
|
.@"struct" => |st| blk: {
|
||||||
|
var n: u32 = 0;
|
||||||
|
for (st.fields.items) |f| n += e.resCount(f.type);
|
||||||
|
break :blk n;
|
||||||
|
},
|
||||||
|
else => 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A struct or array with resources in it.
|
||||||
|
fn holdsResources(e: *Emitter, ty: TypeId) bool {
|
||||||
|
return switch (e.t.get(ty)) {
|
||||||
|
.array, .@"struct" => e.resCount(ty) > 0,
|
||||||
|
else => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Where element / member `index`'s resources are among a value's.
|
||||||
|
fn resRange(e: *Emitter, ty: TypeId, index: u32) Error![2]u32 {
|
||||||
|
switch (e.t.get(ty)) {
|
||||||
|
.array => |a| {
|
||||||
|
const n = e.resCount(a.elem);
|
||||||
|
return .{ index * n, (index + 1) * n };
|
||||||
|
},
|
||||||
|
.@"struct" => |st| {
|
||||||
|
var start: u32 = 0;
|
||||||
|
for (st.fields.items[0..index]) |fld| start += e.resCount(fld.type);
|
||||||
|
return .{ start, start + e.resCount(st.fields.items[index].type) };
|
||||||
|
},
|
||||||
|
else => return .{ 0, 0 },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The data and resources of a value of `ty`.
|
||||||
|
fn aggParts(e: *Emitter, v: Lowered, ty: TypeId) Error!@FieldType(Lowered, "agg") {
|
||||||
|
return switch (v) {
|
||||||
|
.agg => |a| a,
|
||||||
|
.scalars => |vals| if (e.resCount(ty) == 0) .{ .vals = vals, .res = &.{} } else e.unsupported("this value with resources is not supported yet", .{}),
|
||||||
|
.handle, .res_array => .{ .vals = &.{}, .res = try e.arena.dupe(Lowered, &.{v}) },
|
||||||
|
.none => .{ .vals = &.{}, .res = &.{} },
|
||||||
|
else => e.unsupported("this value with resources is not supported yet", .{}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn aggLocal(e: *Emitter, ty: TypeId) Error!Lowered {
|
||||||
|
const slots = try e.arena.alloc(u32, e.resCount(ty));
|
||||||
|
for (slots) |*sl| {
|
||||||
|
sl.* = @intCast(e.res_locals.items.len);
|
||||||
|
try e.res_locals.append(e.arena, null);
|
||||||
|
}
|
||||||
|
var data: ?*const Lowered = null;
|
||||||
|
if (try e.flatCount(ty) > 0) {
|
||||||
|
const d = try e.arena.create(Lowered);
|
||||||
|
if (try e.mixedSizes(ty)) {
|
||||||
|
const dty = try e.memoryType(ty);
|
||||||
|
const base = try e.addPrologue(try e.dm.ptrType(dty), .{ .alloca = .{ .elem = dty, .size = try e.i32Const(1) } });
|
||||||
|
d.* = .{ .typed = .{ .base = base, .dty = dty, .addr_space = 0, .ty = ty } };
|
||||||
|
} else d.* = .{ .ptr = try e.alloca(ty) };
|
||||||
|
data = d;
|
||||||
|
}
|
||||||
|
return .{ .agg_ptr = .{ .data = data, .slots = slots, .ty = ty } };
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A member or element of a local holding resources.
|
||||||
|
fn aggAccess(e: *Emitter, frame: *Frame, start: @FieldType(Lowered, "agg_ptr"), indices: []const Ref) Error!Lowered {
|
||||||
|
var cur = start.ty;
|
||||||
|
var slot: u32 = 0;
|
||||||
|
var data: ?Lowered = if (start.data) |d| d.* else null;
|
||||||
|
var path: std.ArrayList(Value) = .empty;
|
||||||
|
if (data) |d| if (d == .typed) try path.appendSlice(e.arena, d.typed.path);
|
||||||
|
for (indices) |x| {
|
||||||
|
const xi = frame.f.inst(x);
|
||||||
|
const konst: ?u32 = if (xi.op == .constant) @intCast(xi.op.constant.int) else null;
|
||||||
|
var data_off: u32 = 0;
|
||||||
|
var data_dyn: ?Value = null;
|
||||||
|
switch (e.t.get(cur)) {
|
||||||
|
.@"struct" => |st| {
|
||||||
|
const k = konst.?;
|
||||||
|
for (st.fields.items[0..k]) |fld| {
|
||||||
|
slot += e.resCount(fld.type);
|
||||||
|
data_off += try e.flatCount(fld.type);
|
||||||
|
}
|
||||||
|
cur = st.fields.items[k].type;
|
||||||
|
},
|
||||||
|
.array => |arr| {
|
||||||
|
if (e.resCount(arr.elem) > 0) {
|
||||||
|
const k = konst orelse return e.unsupported("dynamically indexing an array holding resources in a local is not supported yet", .{});
|
||||||
|
slot += k * e.resCount(arr.elem);
|
||||||
|
data_off = k * try e.flatCount(arr.elem);
|
||||||
|
} else if (konst) |k| {
|
||||||
|
data_off = k * try e.flatCount(arr.elem);
|
||||||
|
} else {
|
||||||
|
const idx = (try e.scalars(frame, x))[0];
|
||||||
|
const n = try e.flatCount(arr.elem);
|
||||||
|
data_dyn = if (n == 1) idx else try e.add(try e.dm.intType(32), .{ .binop = .{ .op = .mul, .lhs = idx, .rhs = try e.i32Const(n) } });
|
||||||
|
}
|
||||||
|
cur = arr.elem;
|
||||||
|
},
|
||||||
|
else => return e.unsupported("indexing this type is not supported yet", .{}),
|
||||||
|
}
|
||||||
|
if (data) |*d| switch (d.*) {
|
||||||
|
.ptr => |*ptr| {
|
||||||
|
ptr.offset_const += data_off;
|
||||||
|
if (data_dyn) |dd| ptr.offset_dyn = if (ptr.offset_dyn) |old| try e.add(try e.dm.intType(32), .{ .binop = .{ .op = .add, .lhs = old, .rhs = dd } }) else dd;
|
||||||
|
},
|
||||||
|
.typed => try path.append(e.arena, (try e.scalars(frame, x))[0]),
|
||||||
|
else => {},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (isResourceType(e.t, cur)) return .{ .res_local = start.slots[slot] };
|
||||||
|
if (data) |*d| if (d.* == .typed) {
|
||||||
|
d.typed.path = path.items;
|
||||||
|
d.typed.ty = cur;
|
||||||
|
};
|
||||||
|
if (e.holdsResources(cur)) {
|
||||||
|
var sub: ?*const Lowered = null;
|
||||||
|
if (data != null and try e.flatCount(cur) > 0) {
|
||||||
|
const p = try e.arena.create(Lowered);
|
||||||
|
p.* = data.?;
|
||||||
|
sub = p;
|
||||||
|
}
|
||||||
|
return .{ .agg_ptr = .{ .data = sub, .slots = start.slots[slot..][0..e.resCount(cur)], .ty = cur } };
|
||||||
|
}
|
||||||
|
return data orelse e.unsupported("this member of a local holding resources", .{});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn aggStore(e: *Emitter, ap: @FieldType(Lowered, "agg_ptr"), v: Lowered) Error!void {
|
||||||
|
const parts = try e.aggParts(v, ap.ty);
|
||||||
|
if (ap.data) |d| switch (d.*) {
|
||||||
|
.ptr => |ptr| try e.storePtr(ptr, parts.vals, ap.ty),
|
||||||
|
.typed => |tp| try e.typedStore(tp, parts.vals, ap.ty),
|
||||||
|
else => unreachable,
|
||||||
|
};
|
||||||
|
if (parts.res.len != ap.slots.len) return e.unsupported("this assignment of resources is not supported yet", .{});
|
||||||
|
for (ap.slots, parts.res) |sl, r| {
|
||||||
|
const slot = &e.res_locals.items[sl];
|
||||||
|
if (slot.*) |old| if (!std.meta.eql(old, r)) return e.unsupported("assigning different resources to one variable is not supported yet", .{});
|
||||||
|
slot.* = r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn aggExtract(e: *Emitter, a: @FieldType(Lowered, "agg"), ty: TypeId, index: u32) Error!Lowered {
|
||||||
|
const field: TypeId = switch (e.t.get(ty)) {
|
||||||
|
.array => |arr| arr.elem,
|
||||||
|
.@"struct" => |st| st.fields.items[index].type,
|
||||||
|
else => return e.unsupported("extracting from this type is not supported yet", .{}),
|
||||||
|
};
|
||||||
|
const dr = try e.componentRange(ty, index);
|
||||||
|
const rr = try e.resRange(ty, index);
|
||||||
|
if (isResourceType(e.t, field)) return a.res[rr[0]];
|
||||||
|
if (e.holdsResources(field)) return .{ .agg = .{ .vals = a.vals[dr[0]..dr[1]], .res = a.res[rr[0]..rr[1]] } };
|
||||||
|
return e.list(a.vals[dr[0]..dr[1]]);
|
||||||
|
}
|
||||||
|
|
||||||
fn componentRange(e: *Emitter, ty: TypeId, index: u32) Error![2]u32 {
|
fn componentRange(e: *Emitter, ty: TypeId, index: u32) Error![2]u32 {
|
||||||
switch (e.t.get(ty)) {
|
switch (e.t.get(ty)) {
|
||||||
.vector, .scalar => return .{ index, index + 1 },
|
.vector, .scalar => return .{ index, index + 1 },
|
||||||
|
|
@ -4080,7 +4312,7 @@ const Emitter = struct {
|
||||||
fn linAlgSuffix(e: *Emitter, ty: TypeId) Error![]const u8 {
|
fn linAlgSuffix(e: *Emitter, ty: TypeId) Error![]const u8 {
|
||||||
return switch (e.t.get(ty)) {
|
return switch (e.t.get(ty)) {
|
||||||
.linalg => |la| std.fmt.allocPrint(e.arena, "mC{d}M{d}N{d}U{d}S{d}", .{ la.comp, la.m, la.n, la.use, la.scope }),
|
.linalg => |la| std.fmt.allocPrint(e.arena, "mC{d}M{d}N{d}U{d}S{d}", .{ la.comp, la.m, la.n, la.use, la.scope }),
|
||||||
.vector => |v| if (v.len == 1) e.overloadName(try e.scalarTy(e.compOf(ty))) else std.fmt.allocPrint(e.arena, "v{d}{s}", .{ v.len, e.overloadName(try e.scalarTy(e.compOf(ty))) }),
|
.vector => |v| std.fmt.allocPrint(e.arena, "v{d}{s}", .{ v.len, e.overloadName(try e.scalarTy(e.compOf(ty))) }),
|
||||||
else => e.overloadName(try e.scalarTy(e.compOf(ty))),
|
else => e.overloadName(try e.scalarTy(e.compOf(ty))),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -4091,7 +4323,12 @@ const Emitter = struct {
|
||||||
const ty = frame.f.inst(r).ty;
|
const ty = frame.f.inst(r).ty;
|
||||||
const vals = try e.scalars(frame, r);
|
const vals = try e.scalars(frame, r);
|
||||||
if (e.t.get(ty) == .linalg) return .{ vals[0], try e.matrixType(ty) };
|
if (e.t.get(ty) == .linalg) return .{ vals[0], try e.matrixType(ty) };
|
||||||
if (e.t.get(ty) == .vector and vals.len > 1) return .{ try e.libJoin(vals, ty), try e.libValueType(ty) };
|
if (e.t.get(ty) == .vector) {
|
||||||
|
const vt = try e.linAlgVectorType(ty);
|
||||||
|
var v = try e.dm.constant(.{ .ty = vt, .kind = .undef });
|
||||||
|
for (vals, 0..) |c, k| v = try e.add(vt, .{ .insertelement = .{ .vec = v, .val = c, .index = try e.dm.constInt(64, @intCast(k)) } });
|
||||||
|
return .{ v, vt };
|
||||||
|
}
|
||||||
return .{ vals[0], try e.valueType(vals[0]) };
|
return .{ vals[0], try e.valueType(vals[0]) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -4099,7 +4336,7 @@ const Emitter = struct {
|
||||||
/// argument's storage.
|
/// argument's storage.
|
||||||
fn linAlgStore(e: *Emitter, frame: *Frame, dst: Ref, v: Value) Error!void {
|
fn linAlgStore(e: *Emitter, frame: *Frame, dst: Ref, v: Value) Error!void {
|
||||||
const ty = frame.f.inst(dst).ty;
|
const ty = frame.f.inst(dst).ty;
|
||||||
const vals = if (e.t.get(ty) == .vector and e.t.get(ty).vector.len > 1) try e.libSplit(v, ty) else try e.arena.dupe(Value, &.{v});
|
const vals = if (e.t.get(ty) == .vector) try e.linAlgSplit(v, ty) else try e.arena.dupe(Value, &.{v});
|
||||||
switch (frame.vals[dst.index()]) {
|
switch (frame.vals[dst.index()]) {
|
||||||
.typed => |tp| try e.typedStore(tp, vals, ty),
|
.typed => |tp| try e.typedStore(tp, vals, ty),
|
||||||
.ptr => |p| try e.storePtr(p, vals, ty),
|
.ptr => |p| try e.storePtr(p, vals, ty),
|
||||||
|
|
@ -4174,10 +4411,10 @@ const Emitter = struct {
|
||||||
if (out_first) {
|
if (out_first) {
|
||||||
const dst_ty = frame.f.inst(args[0]).ty;
|
const dst_ty = frame.f.inst(args[0]).ty;
|
||||||
try suffix.print(e.arena, ".{s}", .{try e.linAlgSuffix(dst_ty)});
|
try suffix.print(e.arena, ".{s}", .{try e.linAlgSuffix(dst_ty)});
|
||||||
break :blk if (e.t.get(dst_ty) == .linalg) try e.matrixType(dst_ty) else if (e.t.get(dst_ty) == .vector and e.t.get(dst_ty).vector.len > 1) try e.libValueType(dst_ty) else try e.scalarTy(e.compOf(dst_ty));
|
break :blk if (e.t.get(dst_ty) == .linalg) try e.matrixType(dst_ty) else if (e.t.get(dst_ty) == .vector) try e.linAlgVectorType(dst_ty) else try e.scalarTy(e.compOf(dst_ty));
|
||||||
}
|
}
|
||||||
if (ty == TypeTable.void_type) break :blk try e.voidTy();
|
if (ty == TypeTable.void_type) break :blk try e.voidTy();
|
||||||
break :blk if (e.t.get(ty) == .vector) try e.libValueType(ty) else try e.scalarTy(e.compOf(ty));
|
break :blk if (e.t.get(ty) == .vector) try e.linAlgVectorType(ty) else try e.scalarTy(e.compOf(ty));
|
||||||
};
|
};
|
||||||
for (args[@intFromBool(out_first)..], @intFromBool(out_first)..) |a, k| {
|
for (args[@intFromBool(out_first)..], @intFromBool(out_first)..) |a, k| {
|
||||||
const aty = frame.f.inst(a).ty;
|
const aty = frame.f.inst(a).ty;
|
||||||
|
|
@ -4198,7 +4435,7 @@ const Emitter = struct {
|
||||||
const v = try e.linAlgValue(frame, a);
|
const v = try e.linAlgValue(frame, a);
|
||||||
try vals.append(e.arena, v[0]);
|
try vals.append(e.arena, v[0]);
|
||||||
try params.append(e.arena, v[1]);
|
try params.append(e.arena, v[1]);
|
||||||
const overloaded = e.t.get(aty) == .linalg or (e.t.get(aty) == .vector and e.t.get(aty).vector.len > 1) or
|
const overloaded = e.t.get(aty) == .linalg or e.t.get(aty) == .vector or
|
||||||
// The filled / set value.
|
// The filled / set value.
|
||||||
(op.code == 323 and k == 2) or (op.code == 330 and k == 3);
|
(op.code == 323 and k == 2) or (op.code == 330 and k == 3);
|
||||||
if (overloaded) try suffix.print(e.arena, ".{s}", .{try e.linAlgSuffix(aty)});
|
if (overloaded) try suffix.print(e.arena, ".{s}", .{try e.linAlgSuffix(aty)});
|
||||||
|
|
@ -4216,10 +4453,24 @@ const Emitter = struct {
|
||||||
return .none;
|
return .none;
|
||||||
}
|
}
|
||||||
if (ty == TypeTable.void_type) return .none;
|
if (ty == TypeTable.void_type) return .none;
|
||||||
if (e.t.get(ty) == .vector) return .{ .scalars = try e.libSplit(r, ty) };
|
if (e.t.get(ty) == .vector) return .{ .scalars = try e.linAlgSplit(r, ty) };
|
||||||
return .{ .scalars = try e.arena.dupe(Value, &.{r}) };
|
return .{ .scalars = try e.arena.dupe(Value, &.{r}) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// LinAlg vectors are native vectors, even of one element.
|
||||||
|
fn linAlgVectorType(e: *Emitter, ty: TypeId) Error!DType {
|
||||||
|
const v = e.t.get(ty).vector;
|
||||||
|
return e.dm.getType(.{ .vector = .{ .len = v.len, .elem = try e.scalarTy(e.compOf(ty)) } });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn linAlgSplit(e: *Emitter, v: Value, ty: TypeId) Error![]const Value {
|
||||||
|
const n = e.t.get(ty).vector.len;
|
||||||
|
const et = try e.scalarTy(e.compOf(ty));
|
||||||
|
const out = try e.arena.alloc(Value, n);
|
||||||
|
for (out, 0..) |*o, k| o.* = try e.add(et, .{ .extractelement = .{ .vec = v, .index = try e.i32Const(@intCast(k)) } });
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
/// What the module does with matrices (PSV0), when it has any.
|
/// What the module does with matrices (PSV0), when it has any.
|
||||||
fn linAlgTables(e: *Emitter) Error!?linalg_info.Tables {
|
fn linAlgTables(e: *Emitter) Error!?linalg_info.Tables {
|
||||||
if (!e.uses_linalg) return null;
|
if (!e.uses_linalg) return null;
|
||||||
|
|
@ -4247,6 +4498,8 @@ const Emitter = struct {
|
||||||
fn memoryType(e: *Emitter, ty: TypeId) Error!DType {
|
fn memoryType(e: *Emitter, ty: TypeId) Error!DType {
|
||||||
return switch (e.t.get(ty)) {
|
return switch (e.t.get(ty)) {
|
||||||
.linalg => e.matrixType(ty),
|
.linalg => e.matrixType(ty),
|
||||||
|
// (A resource member's place in a struct-typed local.)
|
||||||
|
.resource, .sampler => e.dm.intType(32),
|
||||||
.scalar, .@"enum" => e.scalarTy(memScalar(e.compOf(ty))),
|
.scalar, .@"enum" => e.scalarTy(memScalar(e.compOf(ty))),
|
||||||
.vector => |v| e.dm.getType(.{ .array = .{ .len = v.len, .elem = try e.scalarTy(memScalar(e.compOf(ty))) } }),
|
.vector => |v| e.dm.getType(.{ .array = .{ .len = v.len, .elem = try e.scalarTy(memScalar(e.compOf(ty))) } }),
|
||||||
.matrix => |m| e.dm.getType(.{ .array = .{ .len = m.rows, .elem = try e.dm.getType(.{ .array = .{ .len = m.cols, .elem = try e.scalarTy(memScalar(e.compOf(ty))) } }) } }),
|
.matrix => |m| e.dm.getType(.{ .array = .{ .len = m.rows, .elem = try e.dm.getType(.{ .array = .{ .len = m.cols, .elem = try e.scalarTy(memScalar(e.compOf(ty))) } }) } }),
|
||||||
|
|
@ -4269,6 +4522,7 @@ const Emitter = struct {
|
||||||
fn memLayout(e: *Emitter, ty: TypeId) Error![2]u32 {
|
fn memLayout(e: *Emitter, ty: TypeId) Error![2]u32 {
|
||||||
switch (e.t.get(ty)) {
|
switch (e.t.get(ty)) {
|
||||||
.linalg => return .{ 8, 8 },
|
.linalg => return .{ 8, 8 },
|
||||||
|
.resource, .sampler => return .{ 4, 4 },
|
||||||
.scalar, .@"enum", .vector, .matrix => {
|
.scalar, .@"enum", .vector, .matrix => {
|
||||||
const sz = memScalar(e.compOf(ty)).size();
|
const sz = memScalar(e.compOf(ty)).size();
|
||||||
return .{ sz * (e.t.componentCount(ty) orelse 1), sz };
|
return .{ sz * (e.t.componentCount(ty) orelse 1), sz };
|
||||||
|
|
@ -4303,6 +4557,7 @@ const Emitter = struct {
|
||||||
}.f;
|
}.f;
|
||||||
switch (e.t.get(ty)) {
|
switch (e.t.get(ty)) {
|
||||||
.scalar, .@"enum", .linalg => try out.append(e.arena, prefix),
|
.scalar, .@"enum", .linalg => try out.append(e.arena, prefix),
|
||||||
|
.resource, .sampler => {},
|
||||||
.vector => |v| for (0..v.len) |k| try out.append(e.arena, try sub(e, prefix, @intCast(k))),
|
.vector => |v| for (0..v.len) |k| try out.append(e.arena, try sub(e, prefix, @intCast(k))),
|
||||||
.matrix => |m| for (0..m.rows) |r| {
|
.matrix => |m| for (0..m.rows) |r| {
|
||||||
const rp = try sub(e, prefix, @intCast(r));
|
const rp = try sub(e, prefix, @intCast(r));
|
||||||
|
|
|
||||||
|
|
@ -515,6 +515,9 @@ pub const Module = struct {
|
||||||
/// Descriptor declarations in declaration order.
|
/// Descriptor declarations in declaration order.
|
||||||
binding_decls: std.ArrayList(BindingDecl) = .empty,
|
binding_decls: std.ArrayList(BindingDecl) = .empty,
|
||||||
vk: VkOptions = .{},
|
vk: VkOptions = .{},
|
||||||
|
/// A global struct holds resources (dxc's SPIR-V then numbers array
|
||||||
|
/// elements' bindings separately, as with -fspv-flatten-resource-arrays).
|
||||||
|
resource_structs: bool = false,
|
||||||
/// -enable-16bit-types: half and 16-bit integers are native types.
|
/// -enable-16bit-types: half and 16-bit integers are native types.
|
||||||
native_16bit: bool = false,
|
native_16bit: bool = false,
|
||||||
/// Root signatures: version 1.0 or 1.1 (-force-rootsig-ver), and
|
/// Root signatures: version 1.0 or 1.1 (-force-rootsig-ver), and
|
||||||
|
|
|
||||||
|
|
@ -160,7 +160,9 @@ fn structAlign(t: *const TypeTable, info: *const types.StructInfo, rules: Rules)
|
||||||
.dx_legacy => return 16,
|
.dx_legacy => return 16,
|
||||||
else => {
|
else => {
|
||||||
var al: u32 = if (rules == .vk_relaxed or rules == .gl_std140) 16 else 1;
|
var al: u32 = if (rules == .vk_relaxed or rules == .gl_std140) 16 else 1;
|
||||||
for (info.fields.items) |f| al = @max(al, try alignOf(t, f.type, f.row_major, rules));
|
for (info.fields.items) |f| if (!isOpaque(t, f.type)) {
|
||||||
|
al = @max(al, try alignOf(t, f.type, f.row_major, rules));
|
||||||
|
};
|
||||||
return al;
|
return al;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -200,6 +202,11 @@ fn layoutFields(t: *const TypeTable, info: *const types.StructInfo, rules: Rules
|
||||||
if (packed_end) |pe| off = @max(off, pe);
|
if (packed_end) |pe| off = @max(off, pe);
|
||||||
var end: u32 = off;
|
var end: u32 = off;
|
||||||
for (info.fields.items[first..], out[first..]) |f, *o| {
|
for (info.fields.items[first..], out[first..]) |f, *o| {
|
||||||
|
// (Resources in structs take no space in buffers.)
|
||||||
|
if (isOpaque(t, f.type)) {
|
||||||
|
o.* = off;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (f.pack_offset) |po| {
|
if (f.pack_offset) |po| {
|
||||||
o.* = po;
|
o.* = po;
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -250,9 +257,19 @@ pub fn componentOffset(a: Allocator, t: *const TypeTable, ty: TypeId, row_major:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Resources (and arrays of them): no data.
|
||||||
|
pub fn isOpaque(t: *const TypeTable, ty: TypeId) bool {
|
||||||
|
return switch (t.get(ty)) {
|
||||||
|
.resource, .sampler => true,
|
||||||
|
.array => |a| isOpaque(t, a.elem),
|
||||||
|
else => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub fn flatCount(t: *const TypeTable, ty: TypeId) Error!u32 {
|
pub fn flatCount(t: *const TypeTable, ty: TypeId) Error!u32 {
|
||||||
return switch (t.get(ty)) {
|
return switch (t.get(ty)) {
|
||||||
.scalar, .@"enum" => 1,
|
.scalar, .@"enum" => 1,
|
||||||
|
.resource, .sampler => 0,
|
||||||
.vector => |v| v.len,
|
.vector => |v| v.len,
|
||||||
.matrix => |m| @as(u32, m.rows) * m.cols,
|
.matrix => |m| @as(u32, m.rows) * m.cols,
|
||||||
.array => |a| a.len * try flatCount(t, a.elem),
|
.array => |a| a.len * try flatCount(t, a.elem),
|
||||||
|
|
|
||||||
180
src/ir/lower.zig
180
src/ir/lower.zig
|
|
@ -7,6 +7,7 @@ const std = @import("std");
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const ast = @import("../front/ast.zig");
|
const ast = @import("../front/ast.zig");
|
||||||
const ir = @import("ir.zig");
|
const ir = @import("ir.zig");
|
||||||
|
const layout = @import("layout.zig");
|
||||||
const semamod = @import("../sema/sema.zig");
|
const semamod = @import("../sema/sema.zig");
|
||||||
const types = @import("../sema/types.zig");
|
const types = @import("../sema/types.zig");
|
||||||
const Bitfield = types.Bitfield;
|
const Bitfield = types.Bitfield;
|
||||||
|
|
@ -91,6 +92,9 @@ const Lowerer = struct {
|
||||||
funcs: std.AutoHashMapUnmanaged(*const semamod.Function, u32) = .empty,
|
funcs: std.AutoHashMapUnmanaged(*const semamod.Function, u32) = .empty,
|
||||||
pending: std.ArrayList(*const semamod.Function) = .empty,
|
pending: std.ArrayList(*const semamod.Function) = .empty,
|
||||||
globals: std.AutoHashMapUnmanaged(*const Variable, u32) = .empty,
|
globals: std.AutoHashMapUnmanaged(*const Variable, u32) = .empty,
|
||||||
|
/// Resources inside global structs (`g.tex`): a global each, by
|
||||||
|
/// variable and member path.
|
||||||
|
leaf_globals: std.StringHashMapUnmanaged(u32) = .empty,
|
||||||
/// cbuffer globals by declaration (0: `$Globals`), and where each
|
/// cbuffer globals by declaration (0: `$Globals`), and where each
|
||||||
/// member variable lives.
|
/// member variable lives.
|
||||||
cbuffers: std.AutoHashMapUnmanaged(usize, u32) = .empty,
|
cbuffers: std.AutoHashMapUnmanaged(usize, u32) = .empty,
|
||||||
|
|
@ -230,6 +234,32 @@ const Lowerer = struct {
|
||||||
var globals_done = false;
|
var globals_done = false;
|
||||||
for (l.s.global_order.items) |gd| switch (gd) {
|
for (l.s.global_order.items) |gd| switch (gd) {
|
||||||
.variable => |v| {
|
.variable => |v| {
|
||||||
|
// A struct of resources: a binding per resource (used or
|
||||||
|
// not), the first taking the struct's register.
|
||||||
|
if (v.storage == .global_uniform and holdsResources(&l.s.types, v.type)) {
|
||||||
|
l.m.resource_structs = true;
|
||||||
|
if (l.s.opts.spirv) {
|
||||||
|
if (structHoldsBuffer(&l.s.types, v.type)) {
|
||||||
|
l.unsupported(v.loc, "global structures containing buffers are not supported", .{});
|
||||||
|
return error.Unsupported;
|
||||||
|
}
|
||||||
|
if (layout.flatCount(&l.s.types, v.type) catch 0 > 0) {
|
||||||
|
l.unsupported(v.loc, "global structures containing both resources and non-resources are not supported", .{});
|
||||||
|
return error.Unsupported;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var leaves: std.ArrayList(LeafPath) = .empty;
|
||||||
|
try l.resourceLeaves(v.type, &.{}, &leaves);
|
||||||
|
for (leaves.items, 0..) |leaf, k| {
|
||||||
|
const name = try l.leafName(v, leaf.path);
|
||||||
|
var bd: ir.BindingDecl = .{ .global = l.leaf_globals.get(name) };
|
||||||
|
if (k == 0) if (v.decl) |d| {
|
||||||
|
bd = try l.bindingDecl(d.semantics, d.attrs, bd.global);
|
||||||
|
};
|
||||||
|
try l.m.binding_decls.append(l.arena, bd);
|
||||||
|
}
|
||||||
|
if (layout.flatCount(&l.s.types, v.type) catch 0 == 0) continue;
|
||||||
|
}
|
||||||
if (!isObject(&l.s.types, v.type)) {
|
if (!isObject(&l.s.types, v.type)) {
|
||||||
if (v.storage != .global_uniform or globals_done or l.isPushConstantStruct(v) or l.isSpecConstant(v)) continue;
|
if (v.storage != .global_uniform or globals_done or l.isPushConstantStruct(v) or l.isSpecConstant(v)) continue;
|
||||||
globals_done = true;
|
globals_done = true;
|
||||||
|
|
@ -854,7 +884,8 @@ const Lowerer = struct {
|
||||||
const t = l.types();
|
const t = l.types();
|
||||||
const ty = l.b.f.inst(v).ty;
|
const ty = l.b.f.inst(v).ty;
|
||||||
switch (t.get(ty)) {
|
switch (t.get(ty)) {
|
||||||
.scalar, .@"enum" => try out.append(l.arena, v),
|
// (Resources in structs are leaves of their own.)
|
||||||
|
.scalar, .@"enum", .resource, .sampler => try out.append(l.arena, v),
|
||||||
.vector, .matrix => for (0..t.componentCount(ty).?) |k| try out.append(l.arena, try l.component(v, @intCast(k))),
|
.vector, .matrix => for (0..t.componentCount(ty).?) |k| try out.append(l.arena, try l.component(v, @intCast(k))),
|
||||||
.array => |a| for (0..a.len) |i| try l.flatten(try l.b.add(.{ .op = .{ .extract = .{ .agg = v, .index = @intCast(i) } }, .ty = a.elem }), out),
|
.array => |a| for (0..a.len) |i| try l.flatten(try l.b.add(.{ .op = .{ .extract = .{ .agg = v, .index = @intCast(i) } }, .ty = a.elem }), out),
|
||||||
.@"struct" => |st| for (st.fields.items, 0..) |f, i| {
|
.@"struct" => |st| for (st.fields.items, 0..) |f, i| {
|
||||||
|
|
@ -874,6 +905,10 @@ const Lowerer = struct {
|
||||||
fn fromFlat(l: *Lowerer, ty: TypeId, comps: []const Ref, next: *usize) Error!Ref {
|
fn fromFlat(l: *Lowerer, ty: TypeId, comps: []const Ref, next: *usize) Error!Ref {
|
||||||
const t = l.types();
|
const t = l.types();
|
||||||
switch (t.get(ty)) {
|
switch (t.get(ty)) {
|
||||||
|
.resource, .sampler => {
|
||||||
|
defer next.* += 1;
|
||||||
|
return comps[next.*];
|
||||||
|
},
|
||||||
.scalar, .@"enum" => {
|
.scalar, .@"enum" => {
|
||||||
defer next.* += 1;
|
defer next.* += 1;
|
||||||
return l.convert(comps[next.*], ty);
|
return l.convert(comps[next.*], ty);
|
||||||
|
|
@ -940,6 +975,7 @@ const Lowerer = struct {
|
||||||
},
|
},
|
||||||
.variable => |v| {
|
.variable => |v| {
|
||||||
if (l.value_vars.get(v)) |val| return val;
|
if (l.value_vars.get(v)) |val| return val;
|
||||||
|
if (v.storage == .global_uniform and holdsResources(&l.s.types, v.type)) return l.uniformValue(v, &.{}, v.type);
|
||||||
if (l.isSpecConstant(v)) return l.specConstant(v);
|
if (l.isSpecConstant(v)) return l.specConstant(v);
|
||||||
// Integer `static const` scalars are constants.
|
// Integer `static const` scalars are constants.
|
||||||
if (v.storage == .constant and v.init != null and isIntConstantType(&l.s.types, v.type)) {
|
if (v.storage == .constant and v.init != null and isIntConstantType(&l.s.types, v.type)) {
|
||||||
|
|
@ -1180,7 +1216,7 @@ const Lowerer = struct {
|
||||||
try flattenList(l.arena, e, &flat);
|
try flattenList(l.arena, e, &flat);
|
||||||
var comps: std.ArrayList(Ref) = .empty;
|
var comps: std.ArrayList(Ref) = .empty;
|
||||||
for (flat.items) |x| try l.flatten(try l.rvalue(x), &comps);
|
for (flat.items) |x| try l.flatten(try l.rvalue(x), &comps);
|
||||||
const need = l.types().componentCount(ty) orelse return l.fail(e, "this initializer list");
|
const need = flatLength(l.types(), ty) orelse return l.fail(e, "this initializer list");
|
||||||
if (comps.items.len < need) return l.fail(e, "this initializer list");
|
if (comps.items.len < need) return l.fail(e, "this initializer list");
|
||||||
var next: usize = 0;
|
var next: usize = 0;
|
||||||
return l.fromFlat(ty, comps.items, &next);
|
return l.fromFlat(ty, comps.items, &next);
|
||||||
|
|
@ -1210,6 +1246,7 @@ const Lowerer = struct {
|
||||||
return l.b.add(.{ .op = .{ .construct = parts }, .ty = try l.concrete(e.ty) });
|
return l.b.add(.{ .op = .{ .construct = parts }, .ty = try l.concrete(e.ty) });
|
||||||
},
|
},
|
||||||
.@"struct" => |info| {
|
.@"struct" => |info| {
|
||||||
|
if (l.uniformPath(e)) |up| return l.uniformValue(up.v, up.path, e.ty);
|
||||||
const r = l.s.resolution(e) orelse return l.fail(e, "this member");
|
const r = l.s.resolution(e) orelse return l.fail(e, "this member");
|
||||||
// A static member through an object.
|
// A static member through an object.
|
||||||
if (r == .variable) {
|
if (r == .variable) {
|
||||||
|
|
@ -1527,6 +1564,91 @@ const Lowerer = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Pointer to the storage `e` designates.
|
/// Pointer to the storage `e` designates.
|
||||||
|
/// `g.a.b`, rooted at a global uniform struct holding resources: the
|
||||||
|
/// variable and the member indices.
|
||||||
|
fn uniformPath(l: *Lowerer, e: *ast.Expr) ?struct { v: *const Variable, path: []const u32 } {
|
||||||
|
switch (e.kind) {
|
||||||
|
.name => {
|
||||||
|
const r = l.s.resolution(e) orelse return null;
|
||||||
|
if (r != .variable) return null;
|
||||||
|
const v = r.variable;
|
||||||
|
if (v.storage != .global_uniform or !holdsResources(&l.s.types, v.type)) return null;
|
||||||
|
return .{ .v = v, .path = &.{} };
|
||||||
|
},
|
||||||
|
.member => |m| {
|
||||||
|
if (l.s.types.get(m.base.ty) != .@"struct") return null;
|
||||||
|
const base = l.uniformPath(m.base) orelse return null;
|
||||||
|
const r = l.s.resolution(e) orelse return null;
|
||||||
|
if (r != .field) return null;
|
||||||
|
const path = std.mem.concat(l.arena, u32, &.{ base.path, &.{r.field} }) catch return null;
|
||||||
|
return .{ .v = base.v, .path = path };
|
||||||
|
},
|
||||||
|
else => return null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const LeafPath = struct { path: []const u32, ty: TypeId };
|
||||||
|
|
||||||
|
/// The resources of a struct type, in member order.
|
||||||
|
fn resourceLeaves(l: *Lowerer, ty: TypeId, prefix: []const u32, out: *std.ArrayList(LeafPath)) Error!void {
|
||||||
|
const t = l.types();
|
||||||
|
if (isResourceLike(t, ty)) return out.append(l.arena, .{ .path = prefix, .ty = ty });
|
||||||
|
if (t.get(ty) != .@"struct") return;
|
||||||
|
for (t.get(ty).@"struct".fields.items, 0..) |f, k| {
|
||||||
|
if (!isResourceLike(t, f.type) and !holdsResources(t, f.type)) continue;
|
||||||
|
try l.resourceLeaves(f.type, try std.mem.concat(l.arena, u32, &.{ prefix, &.{@intCast(k)} }), out);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `g.inner.tex`.
|
||||||
|
fn leafName(l: *Lowerer, v: *const Variable, path: []const u32) Error![]const u8 {
|
||||||
|
var name: std.ArrayList(u8) = .empty;
|
||||||
|
try name.appendSlice(l.arena, v.name);
|
||||||
|
var cur = v.type;
|
||||||
|
for (path) |k| {
|
||||||
|
const f = l.s.types.get(cur).@"struct".fields.items[k];
|
||||||
|
try name.print(l.arena, ".{s}", .{f.name});
|
||||||
|
cur = f.type;
|
||||||
|
}
|
||||||
|
return name.items;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The global of a resource inside a global struct, named by its path
|
||||||
|
/// (`g.tex`) and bound like any other unbound resource.
|
||||||
|
fn leafGlobal(l: *Lowerer, v: *const Variable, path: []const u32, ty: TypeId) Error!Ref {
|
||||||
|
const name = try l.leafName(v, path);
|
||||||
|
const gop = try l.leaf_globals.getOrPut(l.arena, name);
|
||||||
|
if (!gop.found_existing) {
|
||||||
|
try l.m.globals.append(l.arena, .{ .name = name, .ty = ty, .space = .resource });
|
||||||
|
gop.value_ptr.* = @intCast(l.m.globals.items.len - 1);
|
||||||
|
}
|
||||||
|
return l.b.add(.{ .op = .{ .global = gop.value_ptr.* }, .ty = ty, .ptr = .resource });
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The value at `path` in a global struct holding resources: its
|
||||||
|
/// resources from their globals, its data from $Globals.
|
||||||
|
fn uniformValue(l: *Lowerer, v: *const Variable, path: []const u32, ty: TypeId) Error!Ref {
|
||||||
|
const t = l.types();
|
||||||
|
if (isResourceLike(t, ty)) return l.b.load(try l.leafGlobal(v, path, ty));
|
||||||
|
if (holdsResources(t, ty)) {
|
||||||
|
if (t.get(ty) != .@"struct") {
|
||||||
|
l.unsupported(v.loc, "arrays of structs holding resources in global '{s}' are not supported yet", .{v.name});
|
||||||
|
return error.Unsupported;
|
||||||
|
}
|
||||||
|
const st = t.get(ty).@"struct";
|
||||||
|
const parts = try l.arena.alloc(Ref, st.fields.items.len);
|
||||||
|
for (parts, st.fields.items, 0..) |*p, f, k| p.* = try l.uniformValue(v, try std.mem.concat(l.arena, u32, &.{ path, &.{@intCast(k)} }), f.type);
|
||||||
|
return l.b.add(.{ .op = .{ .construct = parts }, .ty = ty });
|
||||||
|
}
|
||||||
|
const cm = try l.cbufferMember(v);
|
||||||
|
const glob = l.m.globals.items[cm.global];
|
||||||
|
const base = try l.b.add(.{ .op = .{ .global = cm.global }, .ty = glob.ty, .ptr = .cbuffer });
|
||||||
|
const idx = try l.arena.alloc(Ref, path.len + 1);
|
||||||
|
idx[0] = try l.b.constInt(try t.scalar(.uint), cm.field);
|
||||||
|
for (path, idx[1..]) |k, *x| x.* = try l.b.constInt(try t.scalar(.uint), k);
|
||||||
|
return l.b.load(try l.b.add(.{ .op = .{ .access = .{ .base = base, .indices = idx } }, .ty = ty, .ptr = .cbuffer }));
|
||||||
|
}
|
||||||
|
|
||||||
/// Storage of a variable that is not a local (locals are in `vars`).
|
/// Storage of a variable that is not a local (locals are in `vars`).
|
||||||
fn variableLvalue(l: *Lowerer, e: *ast.Expr, v: *const Variable) Error!Ref {
|
fn variableLvalue(l: *Lowerer, e: *ast.Expr, v: *const Variable) Error!Ref {
|
||||||
const t = l.types();
|
const t = l.types();
|
||||||
|
|
@ -1563,6 +1685,8 @@ const Lowerer = struct {
|
||||||
|
|
||||||
fn lvalue(l: *Lowerer, e: *ast.Expr) Error!Ref {
|
fn lvalue(l: *Lowerer, e: *ast.Expr) Error!Ref {
|
||||||
const t = l.types();
|
const t = l.types();
|
||||||
|
// A resource inside a global struct: its own global.
|
||||||
|
if (e.kind == .member) if (l.uniformPath(e)) |up| if (isResourceLike(t, e.ty)) return l.leafGlobal(up.v, up.path, e.ty);
|
||||||
switch (e.kind) {
|
switch (e.kind) {
|
||||||
.this => {
|
.this => {
|
||||||
if (l.this_ptr == .none) return l.fail(e, "this expression");
|
if (l.this_ptr == .none) return l.fail(e, "this expression");
|
||||||
|
|
@ -2334,6 +2458,58 @@ pub fn saturatingInt(f: f64) i64 {
|
||||||
|
|
||||||
/// Non-static member functions take their object as a hidden first
|
/// Non-static member functions take their object as a hidden first
|
||||||
/// parameter.
|
/// parameter.
|
||||||
|
/// (dxc's SPIR-V supports textures and samplers in global structs, not
|
||||||
|
/// buffers.)
|
||||||
|
fn structHoldsBuffer(t: *const TypeTable, ty: TypeId) bool {
|
||||||
|
return switch (t.get(ty)) {
|
||||||
|
.resource => |r| switch (r.kind) {
|
||||||
|
.buffer, .rw_buffer, .byte_address_buffer, .rw_byte_address_buffer, .structured_buffer, .rw_structured_buffer, .append_structured_buffer, .consume_structured_buffer, .constant_buffer, .texture_buffer => true,
|
||||||
|
else => false,
|
||||||
|
},
|
||||||
|
.array => |a| structHoldsBuffer(t, a.elem),
|
||||||
|
.@"struct" => |st| for (st.fields.items) |f| {
|
||||||
|
if (structHoldsBuffer(t, f.type)) break true;
|
||||||
|
} else false,
|
||||||
|
else => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn isResourceLike(t: *const TypeTable, ty: TypeId) bool {
|
||||||
|
return switch (t.get(ty)) {
|
||||||
|
.resource, .sampler => true,
|
||||||
|
else => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A struct or array with a resource in it.
|
||||||
|
fn holdsResources(t: *const TypeTable, ty: TypeId) bool {
|
||||||
|
return switch (t.get(ty)) {
|
||||||
|
.array => |a| isResourceLike(t, a.elem) or holdsResources(t, a.elem),
|
||||||
|
.@"struct" => |st| for (st.fields.items) |f| {
|
||||||
|
if (isResourceLike(t, f.type) or holdsResources(t, f.type)) break true;
|
||||||
|
} else false,
|
||||||
|
else => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Leaves of an initializer list for `ty` (components, and resources).
|
||||||
|
fn flatLength(t: *const TypeTable, ty: TypeId) ?u32 {
|
||||||
|
return switch (t.get(ty)) {
|
||||||
|
.resource, .sampler => 1,
|
||||||
|
.array => |a| if (flatLength(t, a.elem)) |n| n * a.len else null,
|
||||||
|
.@"struct" => |st| blk: {
|
||||||
|
var n: u32 = 0;
|
||||||
|
for (st.fields.items, 0..) |f, i| {
|
||||||
|
if (f.bits) {
|
||||||
|
n += @intCast(st.bitfieldsIn(@intCast(i)).len);
|
||||||
|
} else n += flatLength(t, f.type) orelse break :blk null;
|
||||||
|
}
|
||||||
|
break :blk n;
|
||||||
|
},
|
||||||
|
else => t.componentCount(ty),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// Integer (and enum) `static const` values are folded to constants.
|
/// Integer (and enum) `static const` values are folded to constants.
|
||||||
fn isIntConstantType(t: *const TypeTable, ty: TypeId) bool {
|
fn isIntConstantType(t: *const TypeTable, ty: TypeId) bool {
|
||||||
return switch (t.get(ty)) {
|
return switch (t.get(ty)) {
|
||||||
|
|
|
||||||
|
|
@ -378,15 +378,15 @@ const Emitter = struct {
|
||||||
break :blk id;
|
break :blk id;
|
||||||
},
|
},
|
||||||
.@"struct" => |st| blk: {
|
.@"struct" => |st| blk: {
|
||||||
const members = try e.arena.alloc(u32, st.fields.items.len);
|
var members: std.ArrayList(u32) = .empty;
|
||||||
for (st.fields.items, members) |fld, *mid| mid.* = try e.typeId(fld.type);
|
for (st.fields.items) |fld| if (!layout.isOpaque(e.t, fld.type)) try members.append(e.arena, try e.typeId(fld.type));
|
||||||
const id = e.b.newId();
|
const id = e.b.newId();
|
||||||
var ops: std.ArrayList(u32) = .empty;
|
var ops: std.ArrayList(u32) = .empty;
|
||||||
try ops.append(e.arena, id);
|
try ops.append(e.arena, id);
|
||||||
try ops.appendSlice(e.arena, members);
|
try ops.appendSlice(e.arena, members.items);
|
||||||
try e.b.emit(.types_globals, .type_struct, ops.items);
|
try e.b.emit(.types_globals, .type_struct, ops.items);
|
||||||
try e.b.emitWithString(.debug_names, .name, &.{id}, try std.fmt.allocPrint(e.arena, "struct.{s}", .{st.name}), &.{});
|
try e.b.emitWithString(.debug_names, .name, &.{id}, try std.fmt.allocPrint(e.arena, "struct.{s}", .{st.name}), &.{});
|
||||||
for (st.fields.items, 0..) |fld, k| try e.b.emitWithString(.debug_names, .member_name, &.{ id, @intCast(k) }, fld.name, &.{});
|
for (st.fields.items, 0..) |fld, k| if (!layout.isOpaque(e.t, fld.type)) try e.b.emitWithString(.debug_names, .member_name, &.{ id, dataIndex(e.t, st, @intCast(k)) }, fld.name, &.{});
|
||||||
try e.memberDecorations(id, st, false);
|
try e.memberDecorations(id, st, false);
|
||||||
break :blk id;
|
break :blk id;
|
||||||
},
|
},
|
||||||
|
|
@ -506,7 +506,9 @@ const Emitter = struct {
|
||||||
{
|
{
|
||||||
var ops: std.ArrayList(u32) = .empty;
|
var ops: std.ArrayList(u32) = .empty;
|
||||||
try ops.append(e.arena, id);
|
try ops.append(e.arena, id);
|
||||||
for (st.fields.items) |fld| try ops.append(e.arena, try e.layoutTypeId(fld.type, rules, fld.row_major));
|
// (Resources in structs are not members of the SPIR-V
|
||||||
|
// struct: they are their own variables.)
|
||||||
|
for (st.fields.items) |fld| if (!layout.isOpaque(e.t, fld.type)) try ops.append(e.arena, try e.layoutTypeId(fld.type, rules, fld.row_major));
|
||||||
try e.b.emit(.types_globals, .type_struct, ops.items);
|
try e.b.emit(.types_globals, .type_struct, ops.items);
|
||||||
const offs = layout.fieldOffsets(e.arena, e.t, st, rules) catch return e.unsupported("buffer member type '{s}'", .{st.name});
|
const offs = layout.fieldOffsets(e.arena, e.t, st, rules) catch return e.unsupported("buffer member type '{s}'", .{st.name});
|
||||||
// An explicit [[vk::offset]] still has to respect the rules.
|
// An explicit [[vk::offset]] still has to respect the rules.
|
||||||
|
|
@ -519,7 +521,8 @@ const Emitter = struct {
|
||||||
if (o % need != 0) return e.unsupported("[[vk::offset({d})]] on '{s}' is not aligned to {d} bytes for this buffer layout", .{ o, fld.name, need });
|
if (o % need != 0) return e.unsupported("[[vk::offset({d})]] on '{s}' is not aligned to {d} bytes for this buffer layout", .{ o, fld.name, need });
|
||||||
};
|
};
|
||||||
for (st.fields.items, offs, 0..) |fld, o, k| {
|
for (st.fields.items, offs, 0..) |fld, o, k| {
|
||||||
const mk: u32 = @intCast(k);
|
if (layout.isOpaque(e.t, fld.type)) continue;
|
||||||
|
const mk: u32 = dataIndex(e.t, st, @intCast(k));
|
||||||
try e.b.emit(.annotations, .member_decorate, &.{ id, mk, 35, o }); // Offset
|
try e.b.emit(.annotations, .member_decorate, &.{ id, mk, 35, o }); // Offset
|
||||||
// Float matrices (possibly in arrays): HLSL rows are
|
// Float matrices (possibly in arrays): HLSL rows are
|
||||||
// SPIR-V columns, so column_major is RowMajor.
|
// SPIR-V columns, so column_major is RowMajor.
|
||||||
|
|
@ -540,7 +543,9 @@ const Emitter = struct {
|
||||||
/// Members' inline SPIR-V decorations (not Offset in buffer layouts,
|
/// Members' inline SPIR-V decorations (not Offset in buffer layouts,
|
||||||
/// which place members themselves).
|
/// which place members themselves).
|
||||||
fn memberDecorations(e: *Emitter, id: Id, st: *const types.StructInfo, laid_out: bool) Error!void {
|
fn memberDecorations(e: *Emitter, id: Id, st: *const types.StructInfo, laid_out: bool) Error!void {
|
||||||
for (st.fields.items, 0..) |fld, k| for (fld.decorations) |d| {
|
for (st.fields.items, 0..) |fld, kk| for (fld.decorations) |d| {
|
||||||
|
if (layout.isOpaque(e.t, fld.type)) continue;
|
||||||
|
const k = dataIndex(e.t, st, @intCast(kk));
|
||||||
if (laid_out and d.decoration == 35) continue;
|
if (laid_out and d.decoration == 35) continue;
|
||||||
try e.requireNeed(op_caps.need(&op_caps.decorations, d.decoration));
|
try e.requireNeed(op_caps.need(&op_caps.decorations, d.decoration));
|
||||||
if (d.strings.len > 0) {
|
if (d.strings.len > 0) {
|
||||||
|
|
@ -1134,7 +1139,7 @@ const Emitter = struct {
|
||||||
}
|
}
|
||||||
switch (g.space) {
|
switch (g.space) {
|
||||||
.resource => {
|
.resource => {
|
||||||
if (e.vk.flatten_arrays and e.t.get(g.ty) == .array and e.constantlyIndexed(@intCast(gi))) {
|
if ((e.vk.flatten_arrays or e.m.resource_structs) and e.t.get(g.ty) == .array and e.constantlyIndexed(@intCast(gi))) {
|
||||||
// One variable per element, on consecutive bindings.
|
// One variable per element, on consecutive bindings.
|
||||||
var et = g.ty;
|
var et = g.ty;
|
||||||
var n: u32 = 1;
|
var n: u32 = 1;
|
||||||
|
|
@ -1354,7 +1359,7 @@ const Emitter = struct {
|
||||||
record(e, v, .{ .set = set, .binding = b });
|
record(e, v, .{ .set = set, .binding = b });
|
||||||
} else if (v.bd.vk_binding) |b| {
|
} else if (v.bd.vk_binding) |b| {
|
||||||
const set = v.bd.vk_set orelse default_set;
|
const set = v.bd.vk_set orelse default_set;
|
||||||
try e.useBindings(set, b, v.n(e.vk.flatten_arrays));
|
try e.useBindings(set, b, v.n((e.vk.flatten_arrays or e.m.resource_structs)));
|
||||||
record(e, v, .{ .set = set, .binding = b });
|
record(e, v, .{ .set = set, .binding = b });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1364,7 +1369,7 @@ const Emitter = struct {
|
||||||
const set = if (v.bd.reg.space_given) v.bd.reg.space else default_set;
|
const set = if (v.bd.reg.space_given) v.bd.reg.space else default_set;
|
||||||
const class: u8 = if (v.bd.reg.class == 's' and v.bd.combined) 't' else v.bd.reg.class;
|
const class: u8 = if (v.bd.reg.class == 's' and v.bd.combined) 't' else v.bd.reg.class;
|
||||||
const b = v.bd.reg.index + e.vk.shift(class, set);
|
const b = v.bd.reg.index + e.vk.shift(class, set);
|
||||||
try e.useBindings(set, b, v.n(e.vk.flatten_arrays));
|
try e.useBindings(set, b, v.n((e.vk.flatten_arrays or e.m.resource_structs)));
|
||||||
record(e, v, .{ .set = set, .binding = b });
|
record(e, v, .{ .set = set, .binding = b });
|
||||||
}
|
}
|
||||||
// The rest, in order.
|
// The rest, in order.
|
||||||
|
|
@ -1381,7 +1386,7 @@ const Emitter = struct {
|
||||||
}
|
}
|
||||||
const set = if (v.bd.space_only and v.bd.reg.space_given) v.bd.reg.space else default_set;
|
const set = if (v.bd.space_only and v.bd.reg.space_given) v.bd.reg.space else default_set;
|
||||||
const shift = if (e.vk.auto_shift) e.vk.shift(e.implicitClass(v.bd), set) else 0;
|
const shift = if (e.vk.auto_shift) e.vk.shift(e.implicitClass(v.bd), set) else 0;
|
||||||
record(e, v, .{ .set = set, .binding = try e.nextBindings(set, shift, v.n(e.vk.flatten_arrays)) });
|
record(e, v, .{ .set = set, .binding = try e.nextBindings(set, shift, v.n((e.vk.flatten_arrays or e.m.resource_structs))) });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2325,8 +2330,16 @@ const Emitter = struct {
|
||||||
.@"struct" => |st| blk: {
|
.@"struct" => |st| blk: {
|
||||||
const k = f.inst(x);
|
const k = f.inst(x);
|
||||||
if (k.op != .constant) break :blk cur;
|
if (k.op != .constant) break :blk cur;
|
||||||
const fld = st.fields.items[@intCast(k.op.constant.int)];
|
const fi: u32 = @intCast(k.op.constant.int);
|
||||||
|
const fld = st.fields.items[fi];
|
||||||
if (pl) |*p| p.row_major = fld.row_major;
|
if (pl) |*p| p.row_major = fld.row_major;
|
||||||
|
// (Resource members are not in the SPIR-V
|
||||||
|
// struct: later members move up.)
|
||||||
|
if (dataIndex(e.t, st, fi) != fi) {
|
||||||
|
try ops.append(e.arena, try e.constUint(dataIndex(e.t, st, fi)));
|
||||||
|
cur = fld.type;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
break :blk fld.type;
|
break :blk fld.type;
|
||||||
},
|
},
|
||||||
else => cur,
|
else => cur,
|
||||||
|
|
@ -4693,6 +4706,15 @@ fn isRayQuery(t: *TypeTable, ty: TypeId) bool {
|
||||||
|
|
||||||
/// Resources and samplers (not RayQuery objects, which are function
|
/// Resources and samplers (not RayQuery objects, which are function
|
||||||
/// storage passed by pointer).
|
/// storage passed by pointer).
|
||||||
|
/// Member `k`'s index in the SPIR-V struct (resource members dropped).
|
||||||
|
fn dataIndex(t: *const TypeTable, st: *const types.StructInfo, k: u32) u32 {
|
||||||
|
var n: u32 = 0;
|
||||||
|
for (st.fields.items[0..k]) |f| {
|
||||||
|
if (!layout.isOpaque(t, f.type)) n += 1;
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
fn isResourceType(t: *TypeTable, ty: TypeId) bool {
|
fn isResourceType(t: *TypeTable, ty: TypeId) bool {
|
||||||
return switch (t.get(ty)) {
|
return switch (t.get(ty)) {
|
||||||
.resource => !isRayQuery(t, ty),
|
.resource => !isRayQuery(t, ty),
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
// PROFILE: cs_6_0
|
||||||
|
// Resources in structs: locals, copies, nested structs, arrays, init
|
||||||
|
// lists, functions taking and returning them, and a global struct whose
|
||||||
|
// resources become their own bindings (data members stay in $Globals).
|
||||||
|
// ZCHECK: @dx.op.sampleLevel.f32(i32 62,
|
||||||
|
// ZCHECK: @dx.op.textureLoad.f32(i32 66,
|
||||||
|
// ZCHECK: texture f32 2d T2 t0 1
|
||||||
|
struct Material { Texture2D<float4> albedo; SamplerState samp; float scale; };
|
||||||
|
struct Pair { Material a; Material b; };
|
||||||
|
Texture2D<float4> texA : register(t3);
|
||||||
|
Texture2D<float4> texB : register(t4);
|
||||||
|
SamplerState sampA : register(s1);
|
||||||
|
struct Globals { float a; Texture2D<float4> t; float b; RWBuffer<float> out_buf; };
|
||||||
|
Globals g;
|
||||||
|
RWStructuredBuffer<float4> o : register(u0);
|
||||||
|
float4 shade(Material m, float2 uv) { return m.albedo.SampleLevel(m.samp, uv, 0) * m.scale; }
|
||||||
|
Material make(Texture2D<float4> t, float k) { Material m; m.albedo = t; m.samp = sampA; m.scale = k; return m; }
|
||||||
|
Texture2D<float4> pick() { return texB; }
|
||||||
|
[numthreads(1,1,1)] void main(uint i : SV_DispatchThreadID) {
|
||||||
|
Material m; m.albedo = texA; m.samp = sampA; m.scale = 2;
|
||||||
|
Material m2 = m; m2.scale = 3;
|
||||||
|
Pair p; p.a = m; p.b = make(texB, 4);
|
||||||
|
Material arr[2] = { m, m2 };
|
||||||
|
o[i] = shade(m, float2(0.5, 0.5)) + shade(m2, 0.25) + shade(p.b, 0.1) + shade(arr[1], 0.2) + pick().Load(int3(0,0,0)) + g.t.Load(int3(1,1,0)) * g.a + g.b;
|
||||||
|
g.out_buf[i] = 1;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
// PROFILE: ps_6_6
|
||||||
|
// FLAGS: -E main
|
||||||
|
// A global struct of resources: one variable per resource, numbered as
|
||||||
|
// dxc does (array elements then take a binding each).
|
||||||
|
// ZCHECK: OpName %TheStruct_Texture "TheStruct.Texture"
|
||||||
|
// ZCHECK: OpDecorate %TheStruct_Texture Binding 10
|
||||||
|
// ZCHECK: OpDecorate %TheStruct_Sampler Binding 11
|
||||||
|
Texture2D<float4> Textures[10];
|
||||||
|
struct StructOfResources {
|
||||||
|
Texture2D Texture;
|
||||||
|
SamplerState Sampler;
|
||||||
|
};
|
||||||
|
StructOfResources TheStruct;
|
||||||
|
float4 main() : SV_Target {
|
||||||
|
return Textures[0].Sample(TheStruct.Sampler, float2(0, 0)) + TheStruct.Texture.Sample(TheStruct.Sampler, float2(0, 0));
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue