1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
//! This module models a SPIR-V Type. These are distinct from Zig types, with some types
//! which are not representable by Zig directly.
const std = @import("std");
const assert = std.debug.assert;
const Signedness = std.builtin.Signedness;
const Allocator = std.mem.Allocator;
const spec = @import("spec.zig");
pub const Type = extern union {
tag_if_small_enough: Tag,
ptr_otherwise: *Payload,
/// A reference to another SPIR-V type.
pub const Ref = enum(u32) { _ };
pub fn initTag(comptime small_tag: Tag) Type {
comptime assert(@enumToInt(small_tag) < Tag.no_payload_count);
return .{ .tag_if_small_enough = small_tag };
}
pub fn initPayload(pl: *Payload) Type {
assert(@enumToInt(pl.tag) >= Tag.no_payload_count);
return .{ .ptr_otherwise = pl };
}
pub fn int(arena: Allocator, signedness: Signedness, bits: u16) !Type {
const bits_and_signedness = switch (signedness) {
.signed => -@as(i32, bits),
.unsigned => @as(i32, bits),
};
return switch (bits_and_signedness) {
8 => initTag(.u8),
16 => initTag(.u16),
32 => initTag(.u32),
64 => initTag(.u64),
-8 => initTag(.i8),
-16 => initTag(.i16),
-32 => initTag(.i32),
-64 => initTag(.i64),
else => {
const int_payload = try arena.create(Payload.Int);
int_payload.* = .{
.width = bits,
.signedness = signedness,
};
return initPayload(&int_payload.base);
},
};
}
pub fn float(bits: u16) Type {
return switch (bits) {
16 => initTag(.f16),
32 => initTag(.f32),
64 => initTag(.f64),
else => unreachable, // Enable more types if required.
};
}
pub fn tag(self: Type) Tag {
if (@enumToInt(self.tag_if_small_enough) < Tag.no_payload_count) {
return self.tag_if_small_enough;
} else {
return self.ptr_otherwise.tag;
}
}
pub fn castTag(self: Type, comptime t: Tag) ?*t.Type() {
if (@enumToInt(self.tag_if_small_enough) < Tag.no_payload_count)
return null;
if (self.ptr_otherwise.tag == t)
return self.payload(t);
return null;
}
/// Access the payload of a type directly.
pub fn payload(self: Type, comptime t: Tag) *t.Type() {
assert(self.tag() == t);
return @fieldParentPtr(t.Type(), "base", self.ptr_otherwise);
}
/// Perform a shallow equality test, comparing two types while assuming that any child types
/// are equal only if their references are equal.
pub fn eqlShallow(a: Type, b: Type) bool {
if (a.tag_if_small_enough == b.tag_if_small_enough)
return true;
const tag_a = a.tag();
const tag_b = b.tag();
if (tag_a != tag_b)
return false;
inline for (@typeInfo(Tag).Enum.fields) |field| {
const t = @field(Tag, field.name);
if (t == tag_a) {
return eqlPayloads(t, a, b);
}
}
unreachable;
}
/// Compare the payload of two compatible tags, given that we already know the tag of both types.
fn eqlPayloads(comptime t: Tag, a: Type, b: Type) bool {
switch (t) {
.void,
.bool,
.sampler,
.event,
.device_event,
.reserve_id,
.queue,
.pipe_storage,
.named_barrier,
.u8,
.u16,
.u32,
.u64,
.i8,
.i16,
.i32,
.i64,
.f16,
.f32,
.f64,
=> return true,
.int,
.vector,
.matrix,
.sampled_image,
.array,
.runtime_array,
.@"opaque",
.pointer,
.pipe,
.image,
=> return std.meta.eql(a.payload(t).*, b.payload(t).*),
.@"struct" => {
const struct_a = a.payload(.@"struct");
const struct_b = b.payload(.@"struct");
if (struct_a.members.len != struct_b.members.len)
return false;
for (struct_a.members, 0..) |mem_a, i| {
if (!std.meta.eql(mem_a, struct_b.members[i]))
return false;
}
return true;
},
.function => {
const fn_a = a.payload(.function);
const fn_b = b.payload(.function);
if (fn_a.return_type != fn_b.return_type)
return false;
return std.mem.eql(Ref, fn_a.parameters, fn_b.parameters);
},
}
}
/// Perform a shallow hash, which hashes the reference value of child types instead of recursing.
pub fn hashShallow(self: Type) u64 {
var hasher = std.hash.Wyhash.init(0);
const t = self.tag();
std.hash.autoHash(&hasher, t);
inline for (@typeInfo(Tag).Enum.fields) |field| {
if (@field(Tag, field.name) == t) {
switch (@field(Tag, field.name)) {
.void,
.bool,
.sampler,
.event,
.device_event,
.reserve_id,
.queue,
.pipe_storage,
.named_barrier,
.u8,
.u16,
.u32,
.u64,
.i8,
.i16,
.i32,
.i64,
.f16,
.f32,
.f64,
=> {},
else => self.hashPayload(@field(Tag, field.name), &hasher),
}
}
}
return hasher.final();
}
/// Perform a shallow hash, given that we know the tag of the field ahead of time.
fn hashPayload(self: Type, comptime t: Tag, hasher: *std.hash.Wyhash) void {
const fields = @typeInfo(t.Type()).Struct.fields;
const pl = self.payload(t);
comptime assert(std.mem.eql(u8, fields[0].name, "base"));
inline for (fields[1..]) |field| { // Skip the 'base' field.
std.hash.autoHashStrat(hasher, @field(pl, field.name), .DeepRecursive);
}
}
/// Hash context that hashes and compares types in a shallow fashion, useful for type caches.
pub const ShallowHashContext32 = struct {
pub fn hash(self: @This(), t: Type) u32 {
_ = self;
return @truncate(u32, t.hashShallow());
}
pub fn eql(self: @This(), a: Type, b: Type, b_index: usize) bool {
_ = self;
_ = b_index;
return a.eqlShallow(b);
}
};
/// Return the reference to any child type. Asserts the type is one of:
/// - Vectors
/// - Matrices
/// - Images
/// - SampledImages,
/// - Arrays
/// - RuntimeArrays
/// - Pointers
pub fn childType(self: Type) Ref {
return switch (self.tag()) {
.vector => self.payload(.vector).component_type,
.matrix => self.payload(.matrix).column_type,
.image => self.payload(.image).sampled_type,
.sampled_image => self.payload(.sampled_image).image_type,
.array => self.payload(.array).element_type,
.runtime_array => self.payload(.runtime_array).element_type,
.pointer => self.payload(.pointer).child_type,
else => unreachable,
};
}
pub fn isInt(self: Type) bool {
return switch (self.tag()) {
.u8,
.u16,
.u32,
.u64,
.i8,
.i16,
.i32,
.i64,
.int,
=> true,
else => false,
};
}
pub fn isFloat(self: Type) bool {
return switch (self.tag()) {
.f16, .f32, .f64 => true,
else => false,
};
}
/// Returns the number of bits that make up an int or float type.
/// Asserts type is either int or float.
pub fn intFloatBits(self: Type) u16 {
return switch (self.tag()) {
.u8, .i8 => 8,
.u16, .i16, .f16 => 16,
.u32, .i32, .f32 => 32,
.u64, .i64, .f64 => 64,
.int => self.payload(.int).width,
else => unreachable,
};
}
/// Returns the signedness of an integer type.
/// Asserts that the type is an int.
pub fn intSignedness(self: Type) Signedness {
return switch (self.tag()) {
.u8, .u16, .u32, .u64 => .unsigned,
.i8, .i16, .i32, .i64 => .signed,
.int => self.payload(.int).signedness,
else => unreachable,
};
}
pub const Tag = enum(usize) {
void,
bool,
sampler,
event,
device_event,
reserve_id,
queue,
pipe_storage,
named_barrier,
u8,
u16,
u32,
u64,
i8,
i16,
i32,
i64,
f16,
f32,
f64,
// After this, the tag requires a payload.
int,
vector,
matrix,
image,
sampled_image,
array,
runtime_array,
@"struct",
@"opaque",
pointer,
function,
pipe,
pub const last_no_payload_tag = Tag.f64;
pub const no_payload_count = @enumToInt(last_no_payload_tag) + 1;
pub fn Type(comptime t: Tag) type {
return switch (t) {
.void,
.bool,
.sampler,
.event,
.device_event,
.reserve_id,
.queue,
.pipe_storage,
.named_barrier,
.u8,
.u16,
.u32,
.u64,
.i8,
.i16,
.i32,
.i64,
.f16,
.f32,
.f64,
=> @compileError("Type Tag " ++ @tagName(t) ++ " has no payload"),
.int => Payload.Int,
.vector => Payload.Vector,
.matrix => Payload.Matrix,
.image => Payload.Image,
.sampled_image => Payload.SampledImage,
.array => Payload.Array,
.runtime_array => Payload.RuntimeArray,
.@"struct" => Payload.Struct,
.@"opaque" => Payload.Opaque,
.pointer => Payload.Pointer,
.function => Payload.Function,
.pipe => Payload.Pipe,
};
}
};
pub const Payload = struct {
tag: Tag,
pub const Int = struct {
base: Payload = .{ .tag = .int },
width: u16,
signedness: Signedness,
};
pub const Vector = struct {
base: Payload = .{ .tag = .vector },
component_type: Ref,
component_count: u32,
};
pub const Matrix = struct {
base: Payload = .{ .tag = .matrix },
column_type: Ref,
column_count: u32,
};
pub const Image = struct {
base: Payload = .{ .tag = .image },
sampled_type: Ref,
dim: spec.Dim,
depth: enum(u2) {
no = 0,
yes = 1,
maybe = 2,
},
arrayed: bool,
multisampled: bool,
sampled: enum(u2) {
known_at_runtime = 0,
with_sampler = 1,
without_sampler = 2,
},
format: spec.ImageFormat,
access_qualifier: ?spec.AccessQualifier,
};
pub const SampledImage = struct {
base: Payload = .{ .tag = .sampled_image },
image_type: Ref,
};
pub const Array = struct {
base: Payload = .{ .tag = .array },
element_type: Ref,
/// Note: Must be emitted as constant, not as literal!
length: u32,
/// Type has the 'ArrayStride' decoration.
/// If zero, no stride is present.
array_stride: u32 = 0,
};
pub const RuntimeArray = struct {
base: Payload = .{ .tag = .runtime_array },
element_type: Ref,
/// Type has the 'ArrayStride' decoration.
/// If zero, no stride is present.
array_stride: u32,
};
pub const Struct = struct {
base: Payload = .{ .tag = .@"struct" },
// TODO: name
members: []Member,
decorations: StructDecorations = .{},
/// Extra information for decorations, packed for efficiency. Fields are stored sequentially by
/// order of the `members` slice and `MemberDecorations` struct.
member_decoration_extra: []u32 = &.{},
pub const Member = struct {
ty: Ref,
offset: u32,
// TODO: name
decorations: MemberDecorations = .{},
};
pub const StructDecorations = packed struct {
/// Type has the 'Block' decoration.
block: bool = false,
/// Type has the 'BufferBlock' decoration.
buffer_block: bool = false,
/// Type has the 'GLSLShared' decoration.
glsl_shared: bool = false,
/// Type has the 'GLSLPacked' decoration.
glsl_packed: bool = false,
/// Type has the 'CPacked' decoration.
c_packed: bool = false,
};
pub const MemberDecorations = packed struct {
/// Matrix layout for (arrays of) matrices. If this field is not .none,
/// then there is also an extra field containing the matrix stride corresponding
/// to the 'MatrixStride' decoration.
matrix_layout: enum(u2) {
/// Member has the 'RowMajor' decoration. The member type
/// must be a matrix or an array of matrices.
row_major,
/// Member has the 'ColMajor' decoration. The member type
/// must be a matrix or an array of matrices.
col_major,
/// Member is not a matrix or array of matrices.
none,
} = .none,
// Regular decorations, these do not imply extra fields.
/// Member has the 'NoPerspective' decoration.
no_perspective: bool = false,
/// Member has the 'Flat' decoration.
flat: bool = false,
/// Member has the 'Patch' decoration.
patch: bool = false,
/// Member has the 'Centroid' decoration.
centroid: bool = false,
/// Member has the 'Sample' decoration.
sample: bool = false,
/// Member has the 'Invariant' decoration.
/// Note: requires parent struct to have 'Block'.
invariant: bool = false,
/// Member has the 'Volatile' decoration.
@"volatile": bool = false,
/// Member has the 'Coherent' decoration.
coherent: bool = false,
/// Member has the 'NonWritable' decoration.
non_writable: bool = false,
/// Member has the 'NonReadable' decoration.
non_readable: bool = false,
// The following decorations all imply extra field(s).
/// Member has the 'BuiltIn' decoration.
/// This decoration has an extra field of type `spec.BuiltIn`.
/// Note: If any member of a struct has the BuiltIn decoration, all members must have one.
/// Note: Each builtin may only be reachable once for a particular entry point.
/// Note: The member type may be constrained by a particular built-in, defined in the client API specification.
builtin: bool = false,
/// Member has the 'Stream' decoration.
/// This member has an extra field of type `u32`.
stream: bool = false,
/// Member has the 'Location' decoration.
/// This member has an extra field of type `u32`.
location: bool = false,
/// Member has the 'Component' decoration.
/// This member has an extra field of type `u32`.
component: bool = false,
/// Member has the 'XfbBuffer' decoration.
/// This member has an extra field of type `u32`.
xfb_buffer: bool = false,
/// Member has the 'XfbStride' decoration.
/// This member has an extra field of type `u32`.
xfb_stride: bool = false,
/// Member has the 'UserSemantic' decoration.
/// This member has an extra field of type `[]u8`, which is encoded
/// by an `u32` containing the number of chars exactly, and then the string padded to
/// a multiple of 4 bytes with zeroes.
user_semantic: bool = false,
};
};
pub const Opaque = struct {
base: Payload = .{ .tag = .@"opaque" },
name: []u8,
};
pub const Pointer = struct {
base: Payload = .{ .tag = .pointer },
storage_class: spec.StorageClass,
child_type: Ref,
/// Type has the 'ArrayStride' decoration.
/// This is valid for pointers to elements of an array.
/// If zero, no stride is present.
array_stride: u32,
/// Type has the 'Alignment' decoration.
alignment: ?u32,
/// Type has the 'MaxByteOffset' decoration.
max_byte_offset: ?u32,
};
pub const Function = struct {
base: Payload = .{ .tag = .function },
return_type: Ref,
parameters: []Ref,
};
pub const Pipe = struct {
base: Payload = .{ .tag = .pipe },
qualifier: spec.AccessQualifier,
};
};
};
|