Skip to content

Commit 09d2de4

Browse files
committed
Add special cases for bit packed structs
This commit adds special cases for AccelerationStructureInstanceKHR and VkAccelerationStructureSRTMotionInstanceNV. These types use bit-packed fields which are not representable in the current version of the zig stage 2 compiler. This might change when ziglang/zig#13009 is resolved. Fixes #56
1 parent 80a201f commit 09d2de4

File tree

5 files changed

+133
-9
lines changed

5 files changed

+133
-9
lines changed

generator/build_integration.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub const ShaderCompileStep = struct {
104104
const dir = path.dirname(shader.full_out_path).?;
105105
try cwd.makePath(dir);
106106

107-
try cmd.appendSlice(&.{shader.source_path, "-o", shader.full_out_path});
107+
try cmd.appendSlice(&.{ shader.source_path, "-o", shader.full_out_path });
108108
try self.builder.spawnChild(cmd.items);
109109
}
110110
}

generator/vulkan/build_integration.zig

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,25 @@ pub const GenerateStep = struct {
7575
const spec = try cwd.readFileAlloc(self.builder.allocator, self.spec_path, std.math.maxInt(usize));
7676

7777
var out_buffer = std.ArrayList(u8).init(self.builder.allocator);
78-
try generate(self.builder.allocator, spec, out_buffer.writer());
78+
generate(self.builder.allocator, spec, out_buffer.writer()) catch |err| switch (err) {
79+
error.InvalidXml => {
80+
std.log.err("invalid vulkan registry - invalid xml", .{});
81+
std.log.err("please check that the correct vk.xml file is passed", .{});
82+
return err;
83+
},
84+
error.InvalidRegistry => {
85+
std.log.err("invalid vulkan registry - registry is valid xml but contents are invalid", .{});
86+
std.log.err("please check that the correct vk.xml file is passed", .{});
87+
return err;
88+
},
89+
error.UnhandledBitfieldStruct => {
90+
std.log.err("unhandled struct with bit fields detected in vk.xml", .{});
91+
std.log.err("this is a bug in vulkan-zig", .{});
92+
std.log.err("please make a bug report at https://github.com/Snektron/vulkan-zig/issues/", .{});
93+
return err;
94+
},
95+
error.OutOfMemory => return error.OutOfMemory,
96+
};
7997
try out_buffer.append(0);
8098

8199
const src = out_buffer.items[0 .. out_buffer.items.len - 1 :0];

generator/vulkan/generator.zig

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,49 @@ pub const Generator = struct {
175175
/// internal datastructures - mostly via an ArenaAllocator, but sometimes a hashmap uses this allocator
176176
/// directly.
177177
pub fn generate(allocator: Allocator, spec_xml: []const u8, writer: anytype) !void {
178-
const spec = try xml.parse(allocator, spec_xml);
178+
const spec = xml.parse(allocator, spec_xml) catch |err| switch (err) {
179+
error.InvalidDocument,
180+
error.UnexpectedEof,
181+
error.UnexpectedCharacter,
182+
error.IllegalCharacter,
183+
error.InvalidEntity,
184+
error.InvalidName,
185+
error.InvalidStandaloneValue,
186+
error.NonMatchingClosingTag,
187+
error.UnclosedComment,
188+
error.UnclosedValue,
189+
=> return error.InvalidXml,
190+
error.OutOfMemory => return error.OutOfMemory,
191+
};
179192
defer spec.deinit();
180193

181-
var gen = try Generator.init(allocator, spec.root);
194+
var gen = Generator.init(allocator, spec.root) catch |err| switch (err) {
195+
error.InvalidXml,
196+
error.InvalidCharacter,
197+
error.Overflow,
198+
error.InvalidFeatureLevel,
199+
error.InvalidSyntax,
200+
error.InvalidTag,
201+
error.MissingTypeIdentifier,
202+
error.UnexpectedCharacter,
203+
error.UnexpectedEof,
204+
error.UnexpectedToken,
205+
error.InvalidRegistry,
206+
=> return error.InvalidRegistry,
207+
error.OutOfMemory => return error.OutOfMemory,
208+
};
182209
defer gen.deinit();
183210

184211
try gen.mergeEnumFields();
185212
try gen.fixupBitFlags();
186-
try gen.render(writer);
213+
gen.render(writer) catch |err| switch (err) {
214+
error.InvalidApiConstant,
215+
error.InvalidConstantExpr,
216+
error.InvalidRegistry,
217+
error.UnexpectedCharacter,
218+
error.InvalidCharacter,
219+
error.Overflow,
220+
=> return error.InvalidRegistry,
221+
else => |others| return others,
222+
};
187223
}

generator/vulkan/render.zig

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,15 +625,85 @@ fn Renderer(comptime WriterType: type) type {
625625
}
626626
}
627627

628+
fn renderSpecialContainer(self: *Self, name: []const u8) !bool {
629+
const maybe_author = self.id_renderer.getAuthorTag(name);
630+
const basename = self.id_renderer.stripAuthorTag(name);
631+
if (std.mem.eql(u8, basename, "VkAccelerationStructureInstance")) {
632+
try self.writer.print(
633+
\\extern struct {{
634+
\\ transform: TransformMatrix{s},
635+
\\ instance_custom_index_and_mask: packed struct(u32) {{
636+
\\ instance_custom_index: u24,
637+
\\ mask: u8,
638+
\\ }},
639+
\\ instance_shader_binding_table_record_offset_and_flags: packed struct(u32) {{
640+
\\ instance_shader_binding_table_record_offset: u24,
641+
\\ flags: u8, // GeometryInstanceFlagsKHR
642+
\\ }},
643+
\\ acceleration_structure_reference: u64,
644+
\\}};
645+
\\
646+
,
647+
.{maybe_author orelse ""},
648+
);
649+
return true;
650+
} else if (std.mem.eql(u8, basename, "VkAccelerationStructureSRTMotionInstance")) {
651+
try self.writer.print(
652+
\\extern struct {{
653+
\\ transform_t0: SRTData{0s},
654+
\\ transform_t1: SRTData{0s},
655+
\\ instance_custom_index_and_mask: packed struct(u32) {{
656+
\\ instance_custom_index: u24,
657+
\\ mask: u8,
658+
\\ }},
659+
\\ instance_shader_binding_table_record_offset_and_flags: packed struct(u32) {{
660+
\\ instance_shader_binding_table_record_offset: u24,
661+
\\ flags: u8, // GeometryInstanceFlagsKHR
662+
\\ }},
663+
\\ acceleration_structure_reference: u64,
664+
\\}};
665+
\\
666+
,
667+
.{maybe_author orelse ""},
668+
);
669+
return true;
670+
} else if (std.mem.eql(u8, basename, "VkAccelerationStructureMatrixMotionInstance")) {
671+
try self.writer.print(
672+
\\extern struct {{
673+
\\ transform_t0: TransformMatrix{0s},
674+
\\ transform_t1: TransformMatrix{0s},
675+
\\ instance_custom_index_and_mask: packed struct(u32) {{
676+
\\ instance_custom_index: u24,
677+
\\ mask: u8,
678+
\\ }},
679+
\\ instance_shader_binding_table_record_offset_and_flags: packed struct(u32) {{
680+
\\ instance_shader_binding_table_record_offset: u24,
681+
\\ flags: u8, // GeometryInstanceFlagsKHR
682+
\\ }},
683+
\\ acceleration_structure_reference: u64,
684+
\\}};
685+
\\
686+
,
687+
.{maybe_author orelse ""},
688+
);
689+
return true;
690+
}
691+
692+
return false;
693+
}
694+
628695
fn renderContainer(self: *Self, name: []const u8, container: reg.Container) !void {
629696
try self.writer.writeAll("pub const ");
630697
try self.renderName(name);
631698
try self.writer.writeAll(" = ");
632699

700+
if (try self.renderSpecialContainer(name)) {
701+
return;
702+
}
703+
633704
for (container.fields) |field| {
634705
if (field.bits != null) {
635-
try self.writer.writeAll("packed ");
636-
break;
706+
return error.UnhandledBitfieldStruct;
637707
}
638708
} else {
639709
try self.writer.writeAll("extern ");

generator/xml.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ fn parseElement(parser: *Parser, alloc: Allocator, comptime kind: ElementKind) !
436436
return null;
437437
};
438438
break :blk tag;
439-
}
439+
},
440440
};
441441

442442
var attributes = std.ArrayList(Attribute).init(alloc);
@@ -475,7 +475,7 @@ fn parseElement(parser: *Parser, alloc: Allocator, comptime kind: ElementKind) !
475475
_ = parser.eatWs();
476476
try parser.expect('>');
477477
}
478-
}
478+
},
479479
}
480480

481481
const element = try alloc.create(Element);

0 commit comments

Comments
 (0)