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.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Peter Li 2026-10-31 22:10:48 -07:00
parent 076f0f7e2b
commit d6b2c9e712
4 changed files with 48 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 => {},
}; };
} }

View File

@ -0,0 +1,14 @@
// PROFILE: cs_6_0
// Arrays of resources passed to functions (whole arrays, indexed
// constantly and dynamically inside), and an element passed alone.
// ZCHECK: @dx.op.sampleLevel.f32(i32 62,
// ZCHECK: @dx.op.textureLoad.f32(i32 66,
Texture2D<float4> texs[4] : register(t0);
Texture2D<float4> more[2] : register(t4);
SamplerState samps[2] : register(s0);
RWStructuredBuffer<float4> o : register(u0);
float4 sum(Texture2D<float4> ts[4], SamplerState s, float2 uv) { float4 r = 0; for (int k = 0; k < 4; k++) r += ts[k].SampleLevel(s, uv, 0); return r; }
float4 pick(Texture2D<float4> ts[2], uint i) { return ts[i].Load(int3(0,0,0)); }
[numthreads(1,1,1)] void main(uint i : SV_DispatchThreadID) {
o[i] = sum(texs, samps[1], 0.5) + pick(more, i % 2) + pick(more, 0);
}

View File

@ -0,0 +1,15 @@
// PROFILE: cs_6_0
// FLAGS: -E main
// Arrays of resources passed to functions (whole arrays, indexed
// constantly and dynamically inside), and an element passed alone.
// ZCHECK: OpAccessChain %_ptr_UniformConstant_{{.*}} %texs
// ZCHECK: OpAccessChain %_ptr_UniformConstant_{{.*}} %more
Texture2D<float4> texs[4] : register(t0);
Texture2D<float4> more[2] : register(t4);
SamplerState samps[2] : register(s0);
RWStructuredBuffer<float4> o : register(u0);
float4 sum(Texture2D<float4> ts[4], SamplerState s, float2 uv) { float4 r = 0; for (int k = 0; k < 4; k++) r += ts[k].SampleLevel(s, uv, 0); return r; }
float4 pick(Texture2D<float4> ts[2], uint i) { return ts[i].Load(int3(0,0,0)); }
[numthreads(1,1,1)] void main(uint i : SV_DispatchThreadID) {
o[i] = sum(texs, samps[1], 0.5) + pick(more, i % 2) + pick(more, 0);
}