Add texture status arguments and CheckAccessFullyMapped

DXIL: the ResRet's status element goes to the status argument;
CheckAccessFullyMapped (op 71) - its operand is forwarded through
locals within a block, as the validator wants the status value itself.
The tiled resources flag follows CheckAccessFullyMapped and LOD clamps.
SPIR-V: the sparse image instructions (SparseResidency) and
OpImageSparseTexelsResident.

Also: cube Gather's extra argument is a status (cubes take no offsets),
and 1-component constant offsets are scalars in SPIR-V.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Peter Li 2026-10-31 13:32:52 -07:00
parent 33f8315e12
commit 4821f76b61
6 changed files with 148 additions and 8 deletions

View File

@ -165,6 +165,13 @@ const Emitter = struct {
uses_inner_coverage: bool = false,
uses_resource_heap: bool = false,
uses_extended_command_info: bool = false,
uses_tiled_resources: bool = false,
/// The ResRet of the last texture operation (for status arguments).
last_resret: Value = undefined,
/// Status values, forwarded through locals within a block: the
/// validator wants CheckAccessFullyMapped's operand to be one.
status_vals: std.AutoHashMapUnmanaged(Value, void) = .empty,
forwarded: std.AutoHashMapUnmanaged(struct { Value, u32 }, struct { v: Value, block: u32 }) = .empty,
uses_barycentrics: bool = false,
uses_sampler_heap: bool = false,
@ -1281,6 +1288,10 @@ const Emitter = struct {
const n = try e.flatCount(ty);
const out = try e.arena.alloc(Value, n);
for (out, 0..) |*o, c| {
if (ptr.offset_dyn == null) if (e.forwarded.get(.{ ptr.base, ptr.offset_const + @as(u32, @intCast(c)) })) |fw| if (fw.block == e.cur) {
o.* = fw.v;
continue;
};
const p = try e.elementPtr(ptr, @intCast(c));
const raw = try e.add(ptr.scalar_ty, .{ .load = .{ .ptr = p, .@"align" = 4 } });
const ct = e.compOf(try e.componentType(ty, @intCast(c)));
@ -1297,6 +1308,10 @@ const Emitter = struct {
fn storePtr(e: *Emitter, ptr: Ptr, vals: []const Value, ty: TypeId) Error!void {
const void_ty = try e.voidTy();
for (vals, 0..) |v, c| {
if (ptr.offset_dyn == null) {
const key = .{ ptr.base, ptr.offset_const + @as(u32, @intCast(c)) };
if (e.status_vals.contains(v)) try e.forwarded.put(e.arena, key, .{ .v = v, .block = e.cur }) else _ = e.forwarded.remove(key);
} else e.forwarded.clearRetainingCapacity();
const p = try e.elementPtr(ptr, @intCast(c));
const ct = e.compOf(try e.componentType(ty, @intCast(c)));
const sv = if (ptr.bool_storage or (ptr.mixed and ct == .bool))
@ -1575,6 +1590,14 @@ const Emitter = struct {
}
return .{ .scalars = out };
}
if (eq(u8, name, "CheckAccessFullyMapped")) {
const i1t = try e.dm.intType(1);
const i32t = try e.dm.intType(32);
const fn_i = try e.dxOp("dx.op.checkAccessFullyMapped.i32", i1t, &.{ i32t, i32t }, .nounwind_readonly);
out[0] = try e.callOp(fn_i, i1t, &.{ try e.i32Const(71), a[0][0] });
e.uses_tiled_resources = true;
return .{ .scalars = out };
}
if (eq(u8, name, "IsHelperLane")) {
const i1t = try e.dm.intType(1);
const i32t = try e.dm.intType(32);
@ -2315,6 +2338,7 @@ const Emitter = struct {
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 }) });
e.last_resret = call;
return e.resRetComponents(call, st, via, ty);
}
@ -2328,9 +2352,19 @@ const Emitter = struct {
const c = try e.padded(coords, coords.len, 3, i32t);
const o = try e.padded(offset orelse &.{}, if (offset) |x| x.len else 0, 3, i32t);
const call = try e.callOp(fn_i, ret_ty, &.{ try e.i32Const(66), res.handle, mip, c[0], c[1], c[2], o[0], o[1], o[2] });
e.last_resret = call;
return e.resRetComponents(call, st, via, ty);
}
/// Stores the status (the ResRet's fifth element) of the last texture
/// operation into an out argument.
fn storeStatus(e: *Emitter, frame: *Frame, status_ref: Ref) Error!void {
const i32t = try e.dm.intType(32);
const v = try e.add(i32t, .{ .extractvalue = .{ .agg = e.last_resret, .index = 4 } });
try e.status_vals.put(e.arena, v, {});
try e.storePtr(try e.outPtr(frame, status_ref), &.{v}, try e.t.scalar(.uint));
}
fn textureStore(e: *Emitter, res: Resource, coords: []const Value, vals: []const Value, vty: TypeId) Error!void {
const via = try e.texelScalar(res.elem);
const st = try e.scalarTy(via);
@ -2352,11 +2386,13 @@ const Emitter = struct {
const kind = e.resourceKind(res.*);
const eq = std.mem.eql;
const i32t = try e.dm.intType(32);
const f32t = try e.dm.getType(.float);
const undef_i32 = try e.dm.constant(.{ .ty = i32t, .kind = .undef });
if (eq(u8, name, "GetDimensions")) return e.getDimensions(frame, res.*, kind, arg_refs);
if (res.kind == 10 and eq(u8, name, "Load")) {
if (arg_refs.len > 1) return e.unsupported("Load with a status argument is not supported yet", .{});
if (arg_refs.len > 1) {
const r = Lowered{ .scalars = try e.typedBufferLoad(res.*, (try e.scalars(frame, arg_refs[0]))[0], ty) };
try e.storeStatus(frame, arg_refs[1]);
return r;
}
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);
@ -2400,8 +2436,22 @@ const Emitter = struct {
}
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", .{});
if (call.offsets4) return e.unsupported("Gather with four offsets is not supported yet", .{});
if (call.status) |k| {
// The operation, then its status.
var plain = call;
plain.status = null;
const r = try e.textureCall(frame, res, sh, plain, name, arg_refs, ty);
try e.storeStatus(frame, arg_refs[k]);
return r;
}
return e.textureCall(frame, res, sh, call, name, arg_refs, ty);
}
fn textureCall(e: *Emitter, frame: *Frame, res: *Resource, sh: texture.Shape, call: texture.Call, name: []const u8, arg_refs: []const Ref, ty: TypeId) Error!Lowered {
const i32t = try e.dm.intType(32);
const f32t = try e.dm.getType(.float);
const undef_i32 = try e.dm.constant(.{ .ty = i32t, .kind = .undef });
if (call.op.implicitLod() and e.m.entry.?.stage != .pixel) return e.unsupported("'{s}' outside pixel shaders is not supported yet", .{name});
const arg = struct {
fn f(em: *Emitter, fr: *Frame, refs: []const Ref, idx: ?u8) Error!?[]const Value {
@ -2508,6 +2558,9 @@ const Emitter = struct {
const fname = try std.fmt.allocPrint(e.arena, "dx.op.{s}.{s}", .{ spec.fname, e.overloadName(st) });
const fn_i = try e.dxOp(fname, ret_ty, params.items, .nounwind_readonly);
const result = try e.callOp(fn_i, ret_ty, args.items);
e.last_resret = result;
// A LOD clamp needs tiled resources.
if (call.clamp != null) e.uses_tiled_resources = true;
return .{ .scalars = try e.resRetComponents(result, st, via, ty) };
}
@ -3476,6 +3529,7 @@ const Emitter = struct {
if (e.uses_view_id) flags |= 1 << 21;
if (e.uses_resource_heap) flags |= 1 << 30;
if (e.uses_barycentrics) flags |= 1 << 22;
if (e.uses_tiled_resources) flags |= 1 << 12;
if (e.uses_extended_command_info) flags |= 1 << 38;
if (e.uses_sampler_heap) flags |= 1 << 31;
if (e.uses_wave) flags |= 1 << 19;
@ -3491,6 +3545,7 @@ const Emitter = struct {
.{ 1 << 21, 0x10000 }, .{ 1 << 24, 0x80000 }, .{ 1 << 19, 0x4000 }, .{ 1 << 13, 0x800 },
.{ 1 << 30, 0x2000000 }, .{ 1 << 31, 0x4000000 },
.{ 1 << 22, 0x20000 }, .{ 1 << 38, 0x100000000 },
.{ 1 << 12, 0x100 },
};
var f: u64 = 0;
for (map) |m| if (flags & m[0] != 0) {

View File

@ -175,6 +175,11 @@ pub fn decode(name: []const u8, s: Shape, nargs: usize) ?Call {
c.channel = if (rest.len == 0 or eq(u8, rest, "Red")) 0 else if (eq(u8, rest, "Green")) 1 else if (eq(u8, rest, "Blue")) 2 else if (eq(u8, rest, "Alpha")) 3 else return null;
const first: u8 = if (c.compare != null) 3 else 2;
const left = n - first;
if (s.offsets() == 0) {
// Cubes take no offsets: only a status.
if (left == 1) c.status = first;
return c;
}
if (left == 1 or left == 2) c.offset = first;
if (left == 4 or left == 5) {
c.offset = first;

View File

@ -67,6 +67,15 @@ pub const Op = enum(u16) {
image_gather = 96,
image_dref_gather = 97,
image_read = 98,
image_sparse_sample_implicit_lod = 305,
image_sparse_sample_explicit_lod = 306,
image_sparse_sample_dref_implicit_lod = 307,
image_sparse_sample_dref_explicit_lod = 308,
image_sparse_fetch = 313,
image_sparse_gather = 314,
image_sparse_dref_gather = 315,
image_sparse_texels_resident = 316,
image_sparse_read = 320,
image_write = 99,
image_query_size_lod = 103,
image_query_size = 104,

View File

@ -53,6 +53,7 @@ const Capability = enum(u32) {
geometry_streams = 54,
multi_viewport = 57,
min_lod = 42,
sparse_residency = 41,
sampled1d = 43,
image1d = 44,
sampled_cube_array = 45,
@ -2326,6 +2327,8 @@ const Emitter = struct {
.constant => return e.val(r),
.construct => |xs| {
if (e.t.get(i.ty) != .vector) return null;
// 1-vectors are SPIR-V scalars.
if (e.isScalarLike(i.ty)) return e.constValue(xs[0]);
var ops: std.ArrayList(u32) = .empty;
const id = e.b.newId();
try ops.appendSlice(e.arena, &.{ try e.typeId(i.ty), id });
@ -2367,7 +2370,7 @@ const Emitter = struct {
}
const sh = texture.shape(r.kind) orelse return e.unsupported("method '{s}' is not supported yet", .{name});
const call = texture.decode(name, sh, args.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", .{});
const status: ?Ref = if (call.status) |k| args[k] else null;
if (call.offsets4) return e.unsupported("Gather with four offsets is not supported yet", .{});
if (call.op.implicitLod() and e.m.entry.?.stage != .pixel) return e.unsupported("'{s}' outside pixel shaders is not supported yet", .{name});
const s = try e.texelScalar(r.elem);
@ -2439,7 +2442,7 @@ const Emitter = struct {
try ops.append(e.arena, mask);
try ops.appendSlice(e.arena, extra.items);
}
const v = try e.opRaw(if (sh.rw) .image_read else .image_fetch, try e.vec4Of(s), ops.items);
const v = try e.imageOp(if (sh.rw) .image_read else .image_fetch, try e.vec4Of(s), ops.items, status);
return e.texelResult(v, s, ty);
}
if (call.op == .sample_position) return e.unsupported("GetSamplePosition is not supported yet", .{});
@ -2467,11 +2470,36 @@ const Emitter = struct {
.gather_cmp => .image_dref_gather,
else => unreachable,
};
if (call.op.isCmp() and call.op != .gather_cmp) return e.opRaw(op, try e.scalarType(s), ops.items);
const v = try e.opRaw(op, try e.vec4Of(s), ops.items);
if (call.op.isCmp() and call.op != .gather_cmp) return e.imageOp(op, try e.scalarType(s), ops.items, status);
const v = try e.imageOp(op, try e.vec4Of(s), ops.items, status);
return e.texelResult(v, s, ty);
}
/// An image instruction; with a status argument, its sparse variant
/// (the residency code goes to the status).
fn imageOp(e: *Emitter, op: Op, result_ty: Id, ops: []const u32, status: ?Ref) Error!Id {
const st = status orelse return e.opRaw(op, result_ty, ops);
try e.cap(.sparse_residency);
const sparse: Op = switch (op) {
.image_sample_implicit_lod => .image_sparse_sample_implicit_lod,
.image_sample_explicit_lod => .image_sparse_sample_explicit_lod,
.image_sample_dref_implicit_lod => .image_sparse_sample_dref_implicit_lod,
.image_sample_dref_explicit_lod => .image_sparse_sample_dref_explicit_lod,
.image_fetch => .image_sparse_fetch,
.image_gather => .image_sparse_gather,
.image_dref_gather => .image_sparse_dref_gather,
.image_read => .image_sparse_read,
else => return e.unsupported("this texture operation with a status argument is not supported yet", .{}),
};
const int_id = try e.scalarType(.int);
const sty = e.b.newId();
try e.b.emit(.types_globals, .type_struct, &.{ sty, int_id, result_ty });
const r = try e.opRaw(sparse, sty, ops);
const code = try e.opRaw(.composite_extract, int_id, &.{ r, 0 });
try e.b.emit(.functions, .store, &.{ e.val(st), try e.opRaw(.bitcast, try e.scalarType(.uint), &.{code}) });
return e.opRaw(.composite_extract, result_ty, &.{ r, 1 });
}
/// 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);
@ -3250,6 +3278,11 @@ const Emitter = struct {
}
return acc.?;
}
if (eqn(u8, name, "CheckAccessFullyMapped")) {
try e.cap(.sparse_residency);
const code = try e.op1(.bitcast, try e.t.scalar(.int), &.{ids[0]});
return e.op1(.image_sparse_texels_resident, ty, &.{code});
}
if (eqn(u8, name, "IsHelperLane")) return e.op1(.load, ty, &.{try e.builtinInput(23, try e.t.scalar(.bool))}); // HelperInvocation
if (eqn(u8, name, "clip")) {
// Discard when any component is negative.

16
test/dxil/status.hlsl Normal file
View File

@ -0,0 +1,16 @@
// PROFILE: ps_6_0
// Status arguments (sparse feedback) and CheckAccessFullyMapped.
// ZCHECK: checkAccessFullyMapped.i32(i32 71
Texture2D<float4> t : register(t0);
Buffer<float4> b : register(t2);
SamplerState s : register(s0);
float4 main(float2 uv : UV) : SV_Target {
uint st0, st1, st6;
float4 a = t.Sample(s, uv, int2(1, 0), 0.5, st0);
if (!CheckAccessFullyMapped(st0)) a = 0;
float4 l = t.Load(int3(1, 2, 0), int2(0, 0), st1);
bool m = CheckAccessFullyMapped(st1);
float4 bb = b.Load(3, st6);
bool n = CheckAccessFullyMapped(st6);
return a + (m ? l : 1) + (n ? bb : 2);
}

22
test/spirv/status.hlsl Normal file
View File

@ -0,0 +1,22 @@
// PROFILE: ps_6_0
// Status arguments: the sparse image instructions, residency checks.
// ZCHECK: OpImageSparseSampleImplicitLod
// ZCHECK: OpImageSparseTexelsResident
Texture2D<float4> t : register(t0);
Texture2DArray<float> ta : register(t1);
RWTexture2D<float4> rw : register(u1);
Buffer<float4> b : register(t2);
SamplerState s : register(s0);
SamplerComparisonState sc : register(s1);
float4 main(float2 uv : UV) : SV_Target {
uint st0, st1, st2, st3, st4, st5, st6;
float4 a = t.Sample(s, uv, int2(1, 0), 0.5, st0);
float4 c = t.SampleLevel(s, uv, 0, int2(0, 1), st1);
float d = ta.SampleCmpLevelZero(sc, float3(uv, 0), 0.5, int2(0, 0), st2);
float4 g = t.Gather(s, uv, int2(0, 0), st3);
float4 l = t.Load(int3(1, 2, 0), int2(0, 0), st4);
float4 r = rw.Load(int2(1, 2), st5);
float4 bb = b.Load(3, st6);
bool all = CheckAccessFullyMapped(st0) && CheckAccessFullyMapped(st1) && CheckAccessFullyMapped(st2) && CheckAccessFullyMapped(st3) && CheckAccessFullyMapped(st4) && CheckAccessFullyMapped(st5) && CheckAccessFullyMapped(st6);
return all ? a + c + d + g + l + r + bb : 0;
}