zig fmt **.zig

This commit is contained in:
Robin Voetter
2021-06-13 15:15:42 +02:00
parent 4b4ef38c93
commit 419e541a16
12 changed files with 313 additions and 342 deletions

View File

@@ -3,9 +3,7 @@ const vk = @import("vulkan");
const c = @import("c.zig");
const Allocator = std.mem.Allocator;
const required_device_extensions = [_][]const u8{
vk.extension_info.khr_swapchain.name
};
const required_device_extensions = [_][]const u8{vk.extension_info.khr_swapchain.name};
const BaseDispatch = struct {
vkCreateInstance: vk.PfnCreateInstance,
@@ -207,7 +205,7 @@ fn initializeCandidate(vki: InstanceDispatch, candidate: DeviceCandidate) !vk.De
.queue_family_index = candidate.queues.present_family,
.queue_count = 1,
.p_queue_priorities = &priority,
}
},
};
const queue_count: u32 = if (candidate.queues.graphics_family == candidate.queues.present_family)
@@ -281,19 +279,14 @@ fn checkSuitable(
return DeviceCandidate{
.pdev = pdev,
.props = props,
.queues = allocation
.queues = allocation,
};
}
return null;
}
fn allocateQueues(
vki: InstanceDispatch,
pdev: vk.PhysicalDevice,
allocator: *Allocator,
surface: vk.SurfaceKHR
) !?QueueAllocation {
fn allocateQueues(vki: InstanceDispatch, pdev: vk.PhysicalDevice, allocator: *Allocator, surface: vk.SurfaceKHR) !?QueueAllocation {
var family_count: u32 = undefined;
vki.getPhysicalDeviceQueueFamilyProperties(pdev, &family_count, null);
@@ -307,7 +300,7 @@ fn allocateQueues(
for (families) |properties, i| {
const family = @intCast(u32, i);
if (graphics_family == null and properties.queue_flags.contains(.{.graphics_bit = true})) {
if (graphics_family == null and properties.queue_flags.graphics_bit) {
graphics_family = family;
}
@@ -319,7 +312,7 @@ fn allocateQueues(
if (graphics_family != null and present_family != null) {
return QueueAllocation{
.graphics_family = graphics_family.?,
.present_family = present_family.?
.present_family = present_family.?,
};
}

View File

@@ -52,7 +52,7 @@ pub fn main() !void {
@intCast(c_int, extent.height),
app_name,
null,
null
null,
) orelse return error.WindowInitFailed;
defer c.glfwDestroyWindow(window);
@@ -114,7 +114,7 @@ pub fn main() !void {
swapchain.extent,
render_pass,
pipeline,
framebuffers
framebuffers,
);
defer destroyCommandBuffers(&gc, pool, allocator, cmdbufs);
@@ -146,13 +146,12 @@ pub fn main() !void {
swapchain.extent,
render_pass,
pipeline,
framebuffers
framebuffers,
);
}
c.glfwSwapBuffers(window);
c.glfwPollEvents();
}
try swapchain.waitForAllFences();
@@ -297,12 +296,7 @@ fn destroyCommandBuffers(gc: *const GraphicsContext, pool: vk.CommandPool, alloc
allocator.free(cmdbufs);
}
fn createFramebuffers(
gc: *const GraphicsContext,
allocator: *Allocator,
render_pass: vk.RenderPass,
swapchain: Swapchain
) ![]vk.Framebuffer {
fn createFramebuffers(gc: *const GraphicsContext, allocator: *Allocator, render_pass: vk.RenderPass, swapchain: Swapchain) ![]vk.Framebuffer {
const framebuffers = try allocator.alloc(vk.Framebuffer, swapchain.swap_images.len);
errdefer allocator.free(framebuffers);
@@ -506,7 +500,8 @@ fn createPipeline(
_ = try gc.vkd.createGraphicsPipelines(
gc.dev,
.null_handle,
1, @ptrCast([*]const vk.GraphicsPipelineCreateInfo, &gpci),
1,
@ptrCast([*]const vk.GraphicsPipelineCreateInfo, &gpci),
null,
@ptrCast([*]vk.Pipeline, &pipeline),
);

View File

@@ -143,15 +143,12 @@ pub const CTokenizer = struct {
']' => kind = .rbracket,
'(' => kind = .lparen,
')' => kind = .rparen,
else => return error.UnexpectedCharacter
else => return error.UnexpectedCharacter,
}
const start = self.offset;
_ = self.consumeNoEof();
return Token{
.kind = kind,
.text = self.source[start .. self.offset]
};
return Token{ .kind = kind, .text = self.source[start..self.offset] };
}
};
@@ -365,7 +362,7 @@ fn parseDeclaration(allocator: *Allocator, xctok: *XmlCTokenizer) ParseError!Dec
.array = .{
.size = array_size,
.child = child,
}
},
};
// update the inner_type pointer so it points to the proper
@@ -403,8 +400,8 @@ fn parseFnPtrSuffix(allocator: *Allocator, xctok: *XmlCTokenizer, return_type: T
.return_type = return_type_heap,
.success_codes = &[_][]const u8{},
.error_codes = &[_][]const u8{},
}
}
},
},
};
const first_param = try parseDeclaration(allocator, xctok);
@@ -500,10 +497,10 @@ fn parseArrayDeclarator(xctok: *XmlCTokenizer) !?ArraySize {
.int = std.fmt.parseInt(usize, size_tok.text, 10) catch |err| switch (err) {
error.Overflow => return error.Overflow,
error.InvalidCharacter => unreachable,
}
},
},
.enum_name => .{ .alias = size_tok.text },
else => return error.InvalidSyntax
else => return error.InvalidSyntax,
};
_ = try xctok.expect(.rbracket);
@@ -551,13 +548,9 @@ fn testTokenizer(tokenizer: anytype, expected_tokens: []const Token) !void {
}
test "CTokenizer" {
var ctok = CTokenizer {
.source = \\typedef ([const)]** VKAPI_PTR 123,;aaaa
};
var ctok = CTokenizer{ .source = "typedef ([const)]** VKAPI_PTR 123,;aaaa" };
try testTokenizer(
&ctok,
&[_]Token{
try testTokenizer(&ctok, &[_]Token{
.{ .kind = .kw_typedef, .text = "typedef" },
.{ .kind = .lparen, .text = "(" },
.{ .kind = .lbracket, .text = "[" },
@@ -571,13 +564,11 @@ test "CTokenizer" {
.{ .kind = .comma, .text = "," },
.{ .kind = .semicolon, .text = ";" },
.{ .kind = .id, .text = "aaaa" },
}
);
});
}
test "XmlCTokenizer" {
const document = try xml.parse(
testing.allocator,
const document = try xml.parse(testing.allocator,
\\<root>// comment <name>commented name</name> <type>commented type</type> trailing
\\ typedef void (VKAPI_PTR *<name>PFN_vkVoidFunction</name>)(void);
\\</root>
@@ -586,9 +577,7 @@ test "XmlCTokenizer" {
var xctok = XmlCTokenizer.init(document.root);
try testTokenizer(
&xctok,
&[_]Token{
try testTokenizer(&xctok, &[_]Token{
.{ .kind = .kw_typedef, .text = "typedef" },
.{ .kind = .id, .text = "void" },
.{ .kind = .lparen, .text = "(" },
@@ -600,13 +589,11 @@ test "XmlCTokenizer" {
.{ .kind = .id, .text = "void" },
.{ .kind = .rparen, .text = ")" },
.{ .kind = .semicolon, .text = ";" },
}
);
});
}
test "parseTypedef" {
const document = try xml.parse(
testing.allocator,
const document = try xml.parse(testing.allocator,
\\<root> // comment <name>commented name</name> trailing
\\ typedef const struct <type>Python</type>* pythons[4];
\\ // more comments

View File

@@ -118,10 +118,13 @@ fn parseBitmaskType(ty: *xml.Element) !registry.Declaration {
return registry.Declaration{
.name = ty.getCharData("name") orelse return error.InvalidRegistry,
.decl_type = .{.bitmask = .{
.bits_enum = ty.getAttribute("requires") orelse ty.getAttribute("bitvalues"), // Who knows why these are different fields
.decl_type = .{
.bitmask = .{
// Who knows why these are different fields
.bits_enum = ty.getAttribute("requires") orelse ty.getAttribute("bitvalues"),
.bitwidth = bitwidth,
}},
},
},
};
}
}
@@ -132,7 +135,9 @@ fn parseHandleType(ty: *xml.Element) !registry.Declaration {
const alias = ty.getAttribute("alias") orelse return error.InvalidRegistry;
return registry.Declaration{
.name = name,
.decl_type = .{.alias = .{.name = alias, .target = .other_type}},
.decl_type = .{
.alias = .{ .name = alias, .target = .other_type },
},
};
} else {
const name = ty.getCharData("name") orelse return error.InvalidRegistry;
@@ -148,7 +153,7 @@ fn parseHandleType(ty: *xml.Element) !registry.Declaration {
.handle = .{
.parent = ty.getAttribute("parent"),
.is_dispatchable = dispatchable,
}
},
},
};
}
@@ -175,7 +180,9 @@ fn parseContainer(allocator: *Allocator, ty: *xml.Element, is_union: bool) !regi
if (ty.getAttribute("alias")) |alias| {
return registry.Declaration{
.name = name,
.decl_type = .{.alias = .{.name = alias, .target = .other_type}},
.decl_type = .{
.alias = .{ .name = alias, .target = .other_type },
},
};
}
@@ -211,8 +218,8 @@ fn parseContainer(allocator: *Allocator, ty: *xml.Element, is_union: bool) !regi
.stype = maybe_stype,
.fields = members,
.is_union = is_union,
}
}
},
},
};
}
@@ -295,7 +302,9 @@ fn parseEnumAlias(allocator: *Allocator, elem: *xml.Element) !?registry.Declarat
const name = elem.getAttribute("name") orelse return error.InvalidRegistry;
return registry.Declaration{
.name = name,
.decl_type = .{.alias = .{.name = alias, .target = .other_type}},
.decl_type = .{
.alias = .{ .name = alias, .target = .other_type },
},
};
}
@@ -419,7 +428,9 @@ fn parseCommand(allocator: *Allocator, elem: *xml.Element) !registry.Declaration
const name = elem.getAttribute("name") orelse return error.InvalidRegistry;
return registry.Declaration{
.name = name,
.decl_type = .{.alias = .{.name = alias, .target = .other_command}}
.decl_type = .{
.alias = .{ .name = alias, .target = .other_command },
},
};
}
@@ -471,8 +482,8 @@ fn parseCommand(allocator: *Allocator, elem: *xml.Element) !registry.Declaration
.return_type = return_type,
.success_codes = success_codes,
.error_codes = error_codes,
}
}
},
},
};
}
@@ -546,9 +557,7 @@ fn parseDefines(types: *xml.Element, out: []registry.ApiConstant) !usize {
var xctok = cparse.XmlCTokenizer.init(ty);
out[i] = .{
.name = name,
.value = .{
.version = cparse.parseVersion(&xctok) catch continue
},
.value = .{ .version = cparse.parseVersion(&xctok) catch continue },
};
}
i += 1;
@@ -609,7 +618,7 @@ fn parseFeature(allocator: *Allocator, feature: *xml.Element) !registry.Feature
return registry.Feature{
.name = name,
.level = feature_level,
.requires = allocator.shrink(requires, i)
.requires = allocator.shrink(requires, i),
};
}
@@ -642,7 +651,10 @@ fn parseEnumExtension(elem: *xml.Element, parent_extnumber: ?u31) !?registry.Req
return registry.Require.EnumExtension{
.extends = extends,
.extnumber = actual_extnumber,
.field = .{.name = name, .value = .{.int = value}},
.field = .{
.name = name,
.value = .{ .int = value },
},
};
}
@@ -771,7 +783,6 @@ fn parseExtension(allocator: *Allocator, extension: *xml.Element) !registry.Exte
const promotedto = extension.getAttribute("promotedto") orelse break :blk .none;
if (mem.startsWith(u8, promotedto, "VK_VERSION_")) {
const feature_level = try splitFeatureLevel(promotedto["VK_VERSION_".len..], "_");
break :blk .{ .feature = feature_level };
}
@@ -816,7 +827,7 @@ fn parseExtension(allocator: *Allocator, extension: *xml.Element) !registry.Exte
.promoted_to = promoted_to,
.platform = platform,
.required_feature_level = requires_core,
.requires = allocator.shrink(requires, i)
.requires = allocator.shrink(requires, i),
};
}

View File

@@ -70,15 +70,12 @@ pub const Container = struct {
};
pub const Enum = struct {
pub const Value = union(enum) {
bitpos: u6, // 1 << bitpos
pub const Value = union(enum) { bitpos: u6, // 1 << bitpos
bit_vector: i32, // Combined flags & some vendor IDs
int: i32,
alias: struct {
int: i32, alias: struct {
name: []const u8,
is_compat_alias: bool,
}
};
} };
pub const Field = struct {
name: []const u8,
@@ -118,7 +115,7 @@ pub const Pointer = struct {
one,
many, // The length is given by some complex expression, possibly involving another field
other_field: []const u8, // The length is given by some other field or parameter
zero_terminated
zero_terminated,
};
is_const: bool,

View File

@@ -539,7 +539,7 @@ fn Renderer(comptime WriterType: type) type {
try self.writeIdentifierFmt("{s}Flags{s}{s}", .{
trimVkNamespace(bitflag_name.base_name),
@as([]const u8, if (bitflag_name.revision) |revision| revision else ""),
@as([]const u8, if (bitflag_name.tag) |tag| tag else "")
@as([]const u8, if (bitflag_name.tag) |tag| tag else ""),
});
return;
} else if (mem.startsWith(u8, name, "vk")) {
@@ -579,7 +579,7 @@ fn Renderer(comptime WriterType: type) type {
try self.writeIdentifierFmt("{s}Flags{s}{s}", .{
trimVkNamespace(bitflag_name.base_name),
@as([]const u8, if (bitflag_name.revision) |revision| revision else ""),
@as([]const u8, if (bitflag_name.tag) |tag| tag else "")
@as([]const u8, if (bitflag_name.tag) |tag| tag else ""),
});
try self.writer.writeAll(".IntType");
break :blk;
@@ -828,15 +828,13 @@ fn Renderer(comptime WriterType: type) type {
\\ = packed struct {{
\\_reserved_bits: {s} = 0,
\\pub usingnamespace FlagsMixin(
, .{ flags_type }
);
, .{flags_type});
try self.renderName(name);
try self.writer.print(
\\, {s});
\\}};
\\
, .{ flags_type }
);
, .{flags_type});
}
}
@@ -943,8 +941,7 @@ fn Renderer(comptime WriterType: type) type {
\\pub fn {s}(comptime Self: type) type {{
\\ return struct {{
\\
, .{name}
);
, .{name});
try self.renderWrapperLoader(dispatch_type);
@@ -986,8 +983,7 @@ fn Renderer(comptime WriterType: type) type {
\\ return self;
\\}}
\\
, .{params, loader_first_param}
);
, .{ params, loader_first_param });
}
fn derefName(name: []const u8) []const u8 {
@@ -1016,7 +1012,8 @@ fn Renderer(comptime WriterType: type) type {
.bitflags, // Special stuff handled in renderWrapperCall
.buffer_len,
.mut_buffer_len,
.other => {
.other,
=> {
try self.writeIdentifierWithCase(.snake, param.name);
try self.writer.writeAll(": ");
try self.renderTypeInfo(param.param_type);
@@ -1064,10 +1061,7 @@ fn Renderer(comptime WriterType: type) type {
try self.writeIdentifierWithCase(.snake, param.name);
try self.writer.writeAll(".toInt()");
},
.in_out_pointer,
.buffer_len,
.mut_buffer_len,
.other => {
.in_out_pointer, .buffer_len, .mut_buffer_len, .other => {
try self.writeIdentifierWithCase(.snake, param.name);
},
}
@@ -1131,16 +1125,13 @@ fn Renderer(comptime WriterType: type) type {
try self.writer.writeAll(": ");
try self.renderTypeInfo(ret.return_value_type);
try self.writer.writeAll(", ");
}
try self.writer.writeAll("};\n");
}
fn renderWrapper(self: *Self, name: []const u8, command: reg.Command) !void {
const returns_vk_result = command.return_type.* == .name
and mem.eql(u8, command.return_type.name, "VkResult");
const returns_void = command.return_type.* == .name
and mem.eql(u8, command.return_type.name, "void");
const returns_vk_result = command.return_type.* == .name and mem.eql(u8, command.return_type.name, "VkResult");
const returns_void = command.return_type.* == .name and mem.eql(u8, command.return_type.name, "void");
const returns = try self.extractReturns(command);

View File

@@ -7,13 +7,13 @@ const ArrayList = std.ArrayList;
pub const Attribute = struct {
name: []const u8,
value: []const u8
value: []const u8,
};
pub const Content = union(enum) {
CharData: []const u8,
Comment: []const u8,
Element: *Element
Element: *Element,
};
pub const Element = struct {
@@ -50,7 +50,7 @@ pub const Element = struct {
return switch (child.children.items[0]) {
.CharData => |char_data| char_data,
else => null
else => null,
};
}
@@ -74,7 +74,7 @@ pub const Element = struct {
pub fn findChildrenByTag(self: *Element, tag: []const u8) FindChildrenByTagIterator {
return .{
.inner = self.elements(),
.tag = tag
.tag = tag,
};
}
@@ -129,7 +129,7 @@ pub const Element = struct {
pub const XmlDecl = struct {
version: []const u8,
encoding: ?[]const u8,
standalone: ?bool
standalone: ?bool,
};
pub const Document = struct {
@@ -154,7 +154,7 @@ const ParseContext = struct {
.source = source,
.offset = 0,
.line = 0,
.column = 0
.column = 0,
};
}
@@ -232,7 +232,7 @@ const ParseContext = struct {
ws = true;
_ = self.consumeNoEof();
},
else => break
else => break,
}
}
@@ -311,7 +311,7 @@ pub const ParseError = error {
InvalidStandaloneValue,
NonMatchingClosingTag,
InvalidDocument,
OutOfMemory
OutOfMemory,
};
pub fn parse(backing_allocator: *Allocator, source: []const u8) !Document {
@@ -323,7 +323,7 @@ fn parseDocument(ctx: *ParseContext, backing_allocator: *Allocator) !Document {
var doc = Document{
.arena = ArenaAllocator.init(backing_allocator),
.xml_decl = null,
.root = undefined
.root = undefined,
};
errdefer doc.deinit();
@@ -376,7 +376,7 @@ fn parseNameNoDupe(ctx: *ParseContext) ![]const u8 {
switch (ch) {
' ', '\t', '\n', '\r' => break,
'&', '"', '\'', '<', '>', '?', '=', '/' => break,
else => _ = ctx.consumeNoEof()
else => _ = ctx.consumeNoEof(),
}
}
@@ -392,7 +392,7 @@ fn tryParseCharData(ctx: *ParseContext, alloc: *Allocator) !?[]const u8 {
while (ctx.peek()) |ch| {
switch (ch) {
'<' => break,
else => _ = ctx.consumeNoEof()
else => _ = ctx.consumeNoEof(),
}
}
@@ -605,17 +605,14 @@ fn tryParseComment(ctx: *ParseContext, alloc: *Allocator) !?[]const u8 {
}
fn unescapeEntity(text: []const u8) !u8 {
const EntitySubstition = struct {
text: []const u8,
replacement: u8
};
const EntitySubstition = struct { text: []const u8, replacement: u8 };
const entities = [_]EntitySubstition{
.{ .text = "&lt;", .replacement = '<' },
.{ .text = "&gt;", .replacement = '>' },
.{ .text = "&amp;", .replacement = '&' },
.{ .text = "&apos;", .replacement = '\'' },
.{.text = "&quot;", .replacement = '"'}
.{ .text = "&quot;", .replacement = '"' },
};
for (entities) |entity| {