Skip to content

Commit

Permalink
Add special cases for bit packed structs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Snektron committed Oct 6, 2022
1 parent 80a201f commit 09d2de4
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 9 deletions.
2 changes: 1 addition & 1 deletion generator/build_integration.zig
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub const ShaderCompileStep = struct {
const dir = path.dirname(shader.full_out_path).?;
try cwd.makePath(dir);

try cmd.appendSlice(&.{shader.source_path, "-o", shader.full_out_path});
try cmd.appendSlice(&.{ shader.source_path, "-o", shader.full_out_path });
try self.builder.spawnChild(cmd.items);
}
}
Expand Down
20 changes: 19 additions & 1 deletion generator/vulkan/build_integration.zig
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,25 @@ pub const GenerateStep = struct {
const spec = try cwd.readFileAlloc(self.builder.allocator, self.spec_path, std.math.maxInt(usize));

var out_buffer = std.ArrayList(u8).init(self.builder.allocator);
try generate(self.builder.allocator, spec, out_buffer.writer());
generate(self.builder.allocator, spec, out_buffer.writer()) catch |err| switch (err) {
error.InvalidXml => {
std.log.err("invalid vulkan registry - invalid xml", .{});
std.log.err("please check that the correct vk.xml file is passed", .{});
return err;
},
error.InvalidRegistry => {
std.log.err("invalid vulkan registry - registry is valid xml but contents are invalid", .{});
std.log.err("please check that the correct vk.xml file is passed", .{});
return err;
},
error.UnhandledBitfieldStruct => {
std.log.err("unhandled struct with bit fields detected in vk.xml", .{});
std.log.err("this is a bug in vulkan-zig", .{});
std.log.err("please make a bug report at https://github.com/Snektron/vulkan-zig/issues/", .{});
return err;
},
error.OutOfMemory => return error.OutOfMemory,
};
try out_buffer.append(0);

const src = out_buffer.items[0 .. out_buffer.items.len - 1 :0];
Expand Down
42 changes: 39 additions & 3 deletions generator/vulkan/generator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,49 @@ pub const Generator = struct {
/// internal datastructures - mostly via an ArenaAllocator, but sometimes a hashmap uses this allocator
/// directly.
pub fn generate(allocator: Allocator, spec_xml: []const u8, writer: anytype) !void {
const spec = try xml.parse(allocator, spec_xml);
const spec = xml.parse(allocator, spec_xml) catch |err| switch (err) {
error.InvalidDocument,
error.UnexpectedEof,
error.UnexpectedCharacter,
error.IllegalCharacter,
error.InvalidEntity,
error.InvalidName,
error.InvalidStandaloneValue,
error.NonMatchingClosingTag,
error.UnclosedComment,
error.UnclosedValue,
=> return error.InvalidXml,
error.OutOfMemory => return error.OutOfMemory,
};
defer spec.deinit();

var gen = try Generator.init(allocator, spec.root);
var gen = Generator.init(allocator, spec.root) catch |err| switch (err) {
error.InvalidXml,
error.InvalidCharacter,
error.Overflow,
error.InvalidFeatureLevel,
error.InvalidSyntax,
error.InvalidTag,
error.MissingTypeIdentifier,
error.UnexpectedCharacter,
error.UnexpectedEof,
error.UnexpectedToken,
error.InvalidRegistry,
=> return error.InvalidRegistry,
error.OutOfMemory => return error.OutOfMemory,
};
defer gen.deinit();

try gen.mergeEnumFields();
try gen.fixupBitFlags();
try gen.render(writer);
gen.render(writer) catch |err| switch (err) {
error.InvalidApiConstant,
error.InvalidConstantExpr,
error.InvalidRegistry,
error.UnexpectedCharacter,
error.InvalidCharacter,
error.Overflow,
=> return error.InvalidRegistry,
else => |others| return others,
};
}
74 changes: 72 additions & 2 deletions generator/vulkan/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -625,15 +625,85 @@ fn Renderer(comptime WriterType: type) type {
}
}

fn renderSpecialContainer(self: *Self, name: []const u8) !bool {
const maybe_author = self.id_renderer.getAuthorTag(name);
const basename = self.id_renderer.stripAuthorTag(name);
if (std.mem.eql(u8, basename, "VkAccelerationStructureInstance")) {
try self.writer.print(
\\extern struct {{
\\ transform: TransformMatrix{s},
\\ instance_custom_index_and_mask: packed struct(u32) {{
\\ instance_custom_index: u24,
\\ mask: u8,
\\ }},
\\ instance_shader_binding_table_record_offset_and_flags: packed struct(u32) {{
\\ instance_shader_binding_table_record_offset: u24,
\\ flags: u8, // GeometryInstanceFlagsKHR
\\ }},
\\ acceleration_structure_reference: u64,
\\}};
\\
,
.{maybe_author orelse ""},
);
return true;
} else if (std.mem.eql(u8, basename, "VkAccelerationStructureSRTMotionInstance")) {
try self.writer.print(
\\extern struct {{
\\ transform_t0: SRTData{0s},
\\ transform_t1: SRTData{0s},
\\ instance_custom_index_and_mask: packed struct(u32) {{
\\ instance_custom_index: u24,
\\ mask: u8,
\\ }},
\\ instance_shader_binding_table_record_offset_and_flags: packed struct(u32) {{
\\ instance_shader_binding_table_record_offset: u24,
\\ flags: u8, // GeometryInstanceFlagsKHR
\\ }},
\\ acceleration_structure_reference: u64,
\\}};
\\
,
.{maybe_author orelse ""},
);
return true;
} else if (std.mem.eql(u8, basename, "VkAccelerationStructureMatrixMotionInstance")) {
try self.writer.print(
\\extern struct {{
\\ transform_t0: TransformMatrix{0s},
\\ transform_t1: TransformMatrix{0s},
\\ instance_custom_index_and_mask: packed struct(u32) {{
\\ instance_custom_index: u24,
\\ mask: u8,
\\ }},
\\ instance_shader_binding_table_record_offset_and_flags: packed struct(u32) {{
\\ instance_shader_binding_table_record_offset: u24,
\\ flags: u8, // GeometryInstanceFlagsKHR
\\ }},
\\ acceleration_structure_reference: u64,
\\}};
\\
,
.{maybe_author orelse ""},
);
return true;
}

return false;
}

fn renderContainer(self: *Self, name: []const u8, container: reg.Container) !void {
try self.writer.writeAll("pub const ");
try self.renderName(name);
try self.writer.writeAll(" = ");

if (try self.renderSpecialContainer(name)) {
return;
}

for (container.fields) |field| {
if (field.bits != null) {
try self.writer.writeAll("packed ");
break;
return error.UnhandledBitfieldStruct;
}
} else {
try self.writer.writeAll("extern ");
Expand Down
4 changes: 2 additions & 2 deletions generator/xml.zig
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ fn parseElement(parser: *Parser, alloc: Allocator, comptime kind: ElementKind) !
return null;
};
break :blk tag;
}
},
};

var attributes = std.ArrayList(Attribute).init(alloc);
Expand Down Expand Up @@ -475,7 +475,7 @@ fn parseElement(parser: *Parser, alloc: Allocator, comptime kind: ElementKind) !
_ = parser.eatWs();
try parser.expect('>');
}
}
},
}

const element = try alloc.create(Element);
Expand Down

0 comments on commit 09d2de4

Please sign in to comment.