Arrays of resources as function parameters

- DXIL: a local array of resources (a parameter's copy) aliases the
  resource array stored into it; elements index that array.
- SPIR-V: a whole resource array read to pass on stays its variable, so
  the callee is specialized on it; arrays read whole are not flattened
  (their bindings are still numbered per element), which also lets
  global structs holding resource arrays be passed to functions.
This commit is contained in:
Peter Li 2026-10-31 22:10:48 -07:00
parent e830120128
commit 56d0da0b92
2 changed files with 19 additions and 2 deletions

View File

@ -1506,7 +1506,8 @@ const Emitter = struct {
try e.rq_locals.append(e.arena, .{ .h = null, .ty = i.ty }); try e.rq_locals.append(e.arena, .{ .h = null, .ty = i.ty });
return .{ .rq_local = @intCast(e.rq_locals.items.len - 1) }; return .{ .rq_local = @intCast(e.rq_locals.items.len - 1) };
} }
if (isResourceType(e.t, i.ty)) { // (An array of resources is one alias too: of an array.)
if (isResourceType(e.t, i.ty) or e.isResourceArray(i.ty)) {
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) };
} }
@ -1635,6 +1636,13 @@ const Emitter = struct {
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()] == .agg_ptr) return e.aggAccess(frame, frame.vals[a.base.index()].agg_ptr, a.indices);
// An element of a local array of resources: of the array
// it aliases.
if (frame.vals[a.base.index()] == .res_local) {
const held = e.res_locals.items[frame.vals[a.base.index()].res_local] orelse return e.unsupported("a resource local read before it is assigned", .{});
if (held != .res_array) return e.unsupported("indexing this resource is not supported yet", .{});
return e.arrayElement(frame, held.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()] == .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) {
@ -1862,6 +1870,13 @@ const Emitter = struct {
}; };
} }
fn isResourceArray(e: *Emitter, ty: TypeId) bool {
var t = ty;
if (e.t.get(t) != .array) return false;
while (e.t.get(t) == .array) t = e.t.get(t).array.elem;
return isResourceType(e.t, t) and !isRayQuery(e.t, t);
}
/// A struct or array with resources in it. /// A struct or array with resources in it.
fn holdsResources(e: *Emitter, ty: TypeId) bool { fn holdsResources(e: *Emitter, ty: TypeId) bool {
return switch (e.t.get(ty)) { return switch (e.t.get(ty)) {

View File

@ -2212,7 +2212,7 @@ const Emitter = struct {
return; return;
} }
// Resources stay as their variables (accessed by chains). // 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)) { if (i.ptr == .none and (e.t.get(i.ty) == .resource or e.t.get(i.ty) == .sampler or (e.t.get(i.ty) == .array and isResourceType(e.t, i.ty)))) {
e.vals[ri] = e.val(p); e.vals[ri] = e.val(p);
return; return;
} }
@ -3055,6 +3055,8 @@ const Emitter = struct {
}, },
.call => |c| for (c.args) |x| if (x.index() == k) return false, .call => |c| for (c.args) |x| if (x.index() == k) return false,
.store => |st| if (st.value.index() == k) return false, .store => |st| if (st.value.index() == k) return false,
// (Read whole: passed on to a function.)
.load => |p| if (p.index() == k) return false,
else => {}, else => {},
}; };
} }