Add vk::SampledTexture

vk::SampledTexture1D ... SampledTextureCUBEArray as resource kinds of
their own (element type defaulting to float4, checked like textures'),
with their VkSampledTexture*Methods: SPIR-V combined image sampler
variables, sampled directly; Load, GetDimensions and [] go through
OpImage. .mips / .sample views as for textures.
This commit is contained in:
Peter Li 2026-10-31 13:45:50 -07:00
parent e21f2d95e7
commit 9c6968061f
5 changed files with 99 additions and 10 deletions

View File

@ -14,6 +14,9 @@ pub const Shape = struct {
array: bool = false, array: bool = false,
ms: bool = false, ms: bool = false,
rw: bool = false, rw: bool = false,
/// A combined image sampler (vk::SampledTexture*): sampling methods
/// take no sampler argument.
combined: bool = false,
/// Coordinate components, without the array index. /// Coordinate components, without the array index.
pub fn coords(s: Shape) u8 { pub fn coords(s: Shape) u8 {
@ -61,6 +64,15 @@ pub fn shape(kind: Kind) ?Shape {
.rw_texture2dms => .{ .dim = .d2, .ms = true, .rw = true }, .rw_texture2dms => .{ .dim = .d2, .ms = true, .rw = true },
.rw_texture2dms_array => .{ .dim = .d2, .array = true, .ms = true, .rw = true }, .rw_texture2dms_array => .{ .dim = .d2, .array = true, .ms = true, .rw = true },
.rw_texture3d => .{ .dim = .d3, .rw = true }, .rw_texture3d => .{ .dim = .d3, .rw = true },
.sampled_texture1d => .{ .dim = .d1, .combined = true },
.sampled_texture1d_array => .{ .dim = .d1, .array = true, .combined = true },
.sampled_texture2d => .{ .dim = .d2, .combined = true },
.sampled_texture2d_array => .{ .dim = .d2, .array = true, .combined = true },
.sampled_texture2dms => .{ .dim = .d2, .ms = true, .combined = true },
.sampled_texture2dms_array => .{ .dim = .d2, .array = true, .ms = true, .combined = true },
.sampled_texture3d => .{ .dim = .d3, .combined = true },
.sampled_texture_cube => .{ .dim = .cube, .combined = true },
.sampled_texture_cube_array => .{ .dim = .cube, .array = true, .combined = true },
else => null, else => null,
}; };
} }
@ -136,6 +148,23 @@ const sampled = [_]Sampled{
/// Decodes `name(args...)` on a resource of shape `s`; null for methods /// Decodes `name(args...)` on a resource of shape `s`; null for methods
/// that are not texture operations (GetDimensions is handled apart). /// that are not texture operations (GetDimensions is handled apart).
pub fn decode(name: []const u8, s: Shape, nargs: usize) ?Call { pub fn decode(name: []const u8, s: Shape, nargs: usize) ?Call {
if (s.combined) {
// As with a sampler argument, every index one lower.
var plain = s;
plain.combined = false;
var c = decodePlain(name, plain, nargs + 1) orelse return null;
if (c.sampler == null) return decodePlain(name, plain, nargs);
c.sampler = null;
c.coord -= 1;
inline for (.{ "compare", "bias", "lod", "ddx", "ddy", "offset", "clamp", "status", "sample_index" }) |f| {
if (@field(c, f)) |*k| k.* -= 1;
}
return c;
}
return decodePlain(name, s, nargs);
}
fn decodePlain(name: []const u8, s: Shape, nargs: usize) ?Call {
const eq = std.mem.eql; const eq = std.mem.eql;
const n: u8 = @intCast(nargs); const n: u8 = @intCast(nargs);
for (sampled) |sm| if (eq(u8, name, sm.name)) { for (sampled) |sm| if (eq(u8, name, sm.name)) {

View File

@ -1047,6 +1047,19 @@ pub const Sema = struct {
return TypeTable.error_type; return TypeTable.error_type;
}, },
}; };
// vk::SampledTexture<T> (T defaults to float4).
for (types.ResourceKind.vk_names) |vn| if (std.mem.eql(u8, vn[0], n)) {
var elem = try s.types.vector(.float, 4);
if (args.len > 0) elem = try s.typeArg(args[0]) orelse elem;
if (args.len > 0 and elem != TypeTable.error_type) switch (s.types.get(elem)) {
.scalar, .vector => {},
else => {
s.err(templateArgLoc(args[0]), "elements of typed buffers and textures must be scalars or vectors", .{});
return TypeTable.error_type;
},
};
return s.types.intern(.{ .resource = .{ .kind = vn[1], .elem = elem, .name = n } });
};
// Any other builtin object (RayQuery, node records, vk:: types...). // Any other builtin object (RayQuery, node records, vk:: types...).
if (isOtherBuiltinObject(n)) { if (isOtherBuiltinObject(n)) {
var elem: TypeId = TypeTable.void_type; var elem: TypeId = TypeTable.void_type;
@ -2813,8 +2826,8 @@ pub const Sema = struct {
return err_typed; return err_typed;
} }
const view: ?@FieldType(@FieldType(types.Type, "texture_view"), "kind") = switch (r.kind) { const view: ?@FieldType(@FieldType(types.Type, "texture_view"), "kind") = switch (r.kind) {
.texture1d, .texture1d_array, .texture2d, .texture2d_array, .texture3d => if (eql(name, "mips")) .mips else null, .texture1d, .texture1d_array, .texture2d, .texture2d_array, .texture3d, .sampled_texture1d, .sampled_texture1d_array, .sampled_texture2d, .sampled_texture2d_array, .sampled_texture3d => if (eql(name, "mips")) .mips else null,
.texture2dms, .texture2dms_array, .rw_texture2dms, .rw_texture2dms_array => if (eql(name, "sample")) .sample else null, .texture2dms, .texture2dms_array, .rw_texture2dms, .rw_texture2dms_array, .sampled_texture2dms, .sampled_texture2dms_array => if (eql(name, "sample")) .sample else null,
else => null, else => null,
}; };
if (view) |k| return rvalue(try s.types.intern(.{ .texture_view = .{ .texture = b.ty, .kind = k } })); if (view) |k| return rvalue(try s.types.intern(.{ .texture_view = .{ .texture = b.ty, .kind = k } }));
@ -3532,6 +3545,15 @@ fn methodNamespace(t: types.Type) ?[]const u8 {
.rw_texture3d => "RWTexture3DMethods", .rw_texture3d => "RWTexture3DMethods",
.feedback_texture2d => "FeedbackTexture2DMethods", .feedback_texture2d => "FeedbackTexture2DMethods",
.feedback_texture2d_array => "FeedbackTexture2DArrayMethods", .feedback_texture2d_array => "FeedbackTexture2DArrayMethods",
.sampled_texture1d => "VkSampledTexture1DMethods",
.sampled_texture1d_array => "VkSampledTexture1DArrayMethods",
.sampled_texture2d => "VkSampledTexture2DMethods",
.sampled_texture2d_array => "VkSampledTexture2DArrayMethods",
.sampled_texture2dms => "VkSampledTexture2DMSMethods",
.sampled_texture2dms_array => "VkSampledTexture2DMSArrayMethods",
.sampled_texture3d => "VkSampledTexture3DMethods",
.sampled_texture_cube => "VkSampledTextureCUBEMethods",
.sampled_texture_cube_array => "VkSampledTextureCUBEArrayMethods",
.constant_buffer, .texture_buffer, .raytracing_acceleration_structure => null, .constant_buffer, .texture_buffer, .raytracing_acceleration_structure => null,
.other => null, .other => null,
}, },

View File

@ -261,6 +261,16 @@ pub const ResourceKind = enum(u8) {
raytracing_acceleration_structure, raytracing_acceleration_structure,
feedback_texture2d, feedback_texture2d,
feedback_texture2d_array, feedback_texture2d_array,
/// vk::SampledTexture*: combined image samplers (SPIR-V only).
sampled_texture1d,
sampled_texture1d_array,
sampled_texture2d,
sampled_texture2d_array,
sampled_texture2dms,
sampled_texture2dms_array,
sampled_texture3d,
sampled_texture_cube,
sampled_texture_cube_array,
/// Any other builtin object (RayQuery, node records, vk:: types...): /// Any other builtin object (RayQuery, node records, vk:: types...):
/// accepted, with methods resolved through the intrinsic table. /// accepted, with methods resolved through the intrinsic table.
other, other,
@ -275,9 +285,9 @@ pub const ResourceKind = enum(u8) {
/// Dimensions of the integer index used by operator[] (0: no indexing). /// Dimensions of the integer index used by operator[] (0: no indexing).
pub fn indexDims(k: ResourceKind) u8 { pub fn indexDims(k: ResourceKind) u8 {
return switch (k) { return switch (k) {
.buffer, .rw_buffer, .structured_buffer, .rw_structured_buffer, .texture1d, .rw_texture1d => 1, .buffer, .rw_buffer, .structured_buffer, .rw_structured_buffer, .texture1d, .rw_texture1d, .sampled_texture1d => 1,
.texture1d_array, .rw_texture1d_array, .texture2d, .rw_texture2d, .texture2dms, .rw_texture2dms => 2, .texture1d_array, .rw_texture1d_array, .texture2d, .rw_texture2d, .texture2dms, .rw_texture2dms, .sampled_texture1d_array, .sampled_texture2d, .sampled_texture2dms => 2,
.texture2d_array, .rw_texture2d_array, .texture2dms_array, .rw_texture2dms_array, .texture3d, .rw_texture3d => 3, .texture2d_array, .rw_texture2d_array, .texture2dms_array, .rw_texture2dms_array, .texture3d, .rw_texture3d, .sampled_texture2d_array, .sampled_texture2dms_array, .sampled_texture3d => 3,
else => 0, else => 0,
}; };
} }
@ -306,6 +316,15 @@ pub const ResourceKind = enum(u8) {
.{ "RasterizerOrderedTexture3D", .rw_texture3d }, .{ "RasterizerOrderedTexture3D", .rw_texture3d },
}; };
/// vk::SampledTexture* by name.
pub const vk_names = [_]struct { []const u8, ResourceKind }{
.{ "SampledTexture1D", .sampled_texture1d }, .{ "SampledTexture1DArray", .sampled_texture1d_array },
.{ "SampledTexture2D", .sampled_texture2d }, .{ "SampledTexture2DArray", .sampled_texture2d_array },
.{ "SampledTexture2DMS", .sampled_texture2dms }, .{ "SampledTexture2DMSArray", .sampled_texture2dms_array },
.{ "SampledTexture3D", .sampled_texture3d }, .{ "SampledTextureCUBE", .sampled_texture_cube },
.{ "SampledTextureCUBEArray", .sampled_texture_cube_array },
};
pub fn fromName(n: []const u8) ?ResourceKind { pub fn fromName(n: []const u8) ?ResourceKind {
for (names) |m| if (std.mem.eql(u8, m[0], n)) return m[1]; for (names) |m| if (std.mem.eql(u8, m[0], n)) return m[1];
return null; return null;

View File

@ -67,6 +67,7 @@ pub const Op = enum(u16) {
image_gather = 96, image_gather = 96,
image_dref_gather = 97, image_dref_gather = 97,
image_read = 98, image_read = 98,
image = 100,
image_sparse_sample_implicit_lod = 305, image_sparse_sample_implicit_lod = 305,
image_sparse_sample_explicit_lod = 306, image_sparse_sample_explicit_lod = 306,
image_sparse_sample_dref_implicit_lod = 307, image_sparse_sample_dref_implicit_lod = 307,

View File

@ -2131,7 +2131,12 @@ const Emitter = struct {
.structured_buffer, .rw_structured_buffer, .append_structured_buffer, .consume_structured_buffer, .byte_address_buffer, .rw_byte_address_buffer => return .{ .sc = e.bufferSc(), .ty = try e.bufferBlockType(ty) }, .structured_buffer, .rw_structured_buffer, .append_structured_buffer, .consume_structured_buffer, .byte_address_buffer, .rw_byte_address_buffer => return .{ .sc = e.bufferSc(), .ty = try e.bufferBlockType(ty) },
.constant_buffer => if (e.t.get(r.elem) == .@"struct") return .{ .sc = .uniform, .ty = try e.constantBufferBlock(r.elem) }, .constant_buffer => if (e.t.get(r.elem) == .@"struct") return .{ .sc = .uniform, .ty = try e.constantBufferBlock(r.elem) },
.texture_buffer => if (e.t.get(r.elem) == .@"struct") return .{ .sc = e.bufferSc(), .ty = try e.textureBufferBlock(r.elem) }, .texture_buffer => if (e.t.get(r.elem) == .@"struct") return .{ .sc = e.bufferSc(), .ty = try e.textureBufferBlock(r.elem) },
else => if (texture.shape(r.kind)) |sh| return .{ .sc = .uniform_constant, .ty = try e.imageType(sh, r.elem) }, else => if (texture.shape(r.kind)) |sh| {
var plain = sh;
plain.combined = false;
const img = try e.imageType(plain, r.elem);
return .{ .sc = .uniform_constant, .ty = if (sh.combined) try e.sampledImageType(img) else img };
},
}, },
.array => |arr| { .array => |arr| {
const inner = try e.resourcePointee(arr.elem); const inner = try e.resourcePointee(arr.elem);
@ -2291,6 +2296,12 @@ const Emitter = struct {
} }
fn loadImage(e: *Emitter, r: Ref) Error!Id { fn loadImage(e: *Emitter, r: Ref) Error!Id {
if (texture.shape(e.resourceInfo(r).kind)) |sh| if (sh.combined) {
// A combined image sampler: its image.
const si = try e.opRaw(.load, try e.sampledImageType(try e.imageTypeOf(r)), &.{e.val(r)});
if (e.nonuniform_ids.contains(e.val(r))) try e.markNonUniform(si, e.f.inst(r).ty);
return e.opRaw(.image, try e.imageTypeOf(r), &.{si});
};
const id = try e.opRaw(.load, try e.imageTypeOf(r), &.{e.val(r)}); const id = try e.opRaw(.load, try e.imageTypeOf(r), &.{e.val(r)});
if (e.nonuniform_ids.contains(e.val(r))) try e.markNonUniform(id, e.f.inst(r).ty); if (e.nonuniform_ids.contains(e.val(r))) try e.markNonUniform(id, e.f.inst(r).ty);
return id; return id;
@ -2488,10 +2499,17 @@ const Emitter = struct {
return e.texelResult(v, s, ty); return e.texelResult(v, s, ty);
} }
if (call.op == .sample_position) return e.unsupported("GetSamplePosition is not supported yet", .{}); if (call.op == .sample_position) return e.unsupported("GetSamplePosition is not supported yet", .{});
const si = if (sh.combined) blk: {
const v = try e.opRaw(.load, try e.sampledImageType(try e.imageTypeOf(res)), &.{e.val(res)});
if (e.nonuniform_ids.contains(e.val(res))) try e.markNonUniform(v, e.f.inst(res).ty);
break :blk v;
} else blk: {
const smp = try e.opRaw(.load, try e.samplerType(), &.{e.val(args[call.sampler.?])}); const smp = try e.opRaw(.load, try e.samplerType(), &.{e.val(args[call.sampler.?])});
if (e.nonuniform_ids.contains(e.val(args[call.sampler.?]))) try e.markNonUniform(smp, e.f.inst(args[call.sampler.?]).ty); if (e.nonuniform_ids.contains(e.val(args[call.sampler.?]))) try e.markNonUniform(smp, e.f.inst(args[call.sampler.?]).ty);
const si = try e.opRaw(.sampled_image, try e.sampledImageType(try e.imageTypeOf(res)), &.{ img, smp }); const v = try e.opRaw(.sampled_image, try e.sampledImageType(try e.imageTypeOf(res)), &.{ img, smp });
if (e.nonuniform_ids.contains(img) or e.nonuniform_ids.contains(smp)) try e.markNonUniform(si, e.f.inst(res).ty); if (e.nonuniform_ids.contains(img) or e.nonuniform_ids.contains(smp)) try e.markNonUniform(v, e.f.inst(res).ty);
break :blk v;
};
if (call.op == .calc_lod or call.op == .calc_lod_unclamped) { if (call.op == .calc_lod or call.op == .calc_lod_unclamped) {
try e.cap(.image_query); try e.cap(.image_query);
const q = try e.opRaw(.image_query_lod, try e.typeId(try e.t.vector(.float, 2)), &.{ si, coord }); const q = try e.opRaw(.image_query_lod, try e.typeId(try e.t.vector(.float, 2)), &.{ si, coord });