From d6b2c9e71232ee1a738311f690347cb5d7e358e8 Mon Sep 17 00:00:00 2001 From: Peter Li Date: Sat, 31 Oct 2026 22:10:48 -0700 Subject: [PATCH] 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 --- src/dxil/emit.zig | 17 ++++++++++++++++- src/spirv/emit.zig | 4 +++- test/dxil/resource_array_params.hlsl | 14 ++++++++++++++ test/spirv/resource_array_params.hlsl | 15 +++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 test/dxil/resource_array_params.hlsl create mode 100644 test/spirv/resource_array_params.hlsl diff --git a/src/dxil/emit.zig b/src/dxil/emit.zig index 8e544eb..c2a7af7 100644 --- a/src/dxil/emit.zig +++ b/src/dxil/emit.zig @@ -1506,7 +1506,8 @@ const Emitter = struct { try e.rq_locals.append(e.arena, .{ .h = null, .ty = i.ty }); 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); return .{ .res_local = @intCast(e.res_locals.items.len - 1) }; } @@ -1635,6 +1636,13 @@ const Emitter = struct { return .{ .typed = tp }; } 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()] == .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) { @@ -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. fn holdsResources(e: *Emitter, ty: TypeId) bool { return switch (e.t.get(ty)) { diff --git a/src/spirv/emit.zig b/src/spirv/emit.zig index 5a328b5..f490e3e 100644 --- a/src/spirv/emit.zig +++ b/src/spirv/emit.zig @@ -2212,7 +2212,7 @@ const Emitter = struct { 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)) { + 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); return; } @@ -3055,6 +3055,8 @@ const Emitter = struct { }, .call => |c| for (c.args) |x| if (x.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 => {}, }; } diff --git a/test/dxil/resource_array_params.hlsl b/test/dxil/resource_array_params.hlsl new file mode 100644 index 0000000..52532e0 --- /dev/null +++ b/test/dxil/resource_array_params.hlsl @@ -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 texs[4] : register(t0); +Texture2D more[2] : register(t4); +SamplerState samps[2] : register(s0); +RWStructuredBuffer o : register(u0); +float4 sum(Texture2D 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 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); +} diff --git a/test/spirv/resource_array_params.hlsl b/test/spirv/resource_array_params.hlsl new file mode 100644 index 0000000..91cc5ce --- /dev/null +++ b/test/spirv/resource_array_params.hlsl @@ -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 texs[4] : register(t0); +Texture2D more[2] : register(t4); +SamplerState samps[2] : register(s0); +RWStructuredBuffer o : register(u0); +float4 sum(Texture2D 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 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); +}