forked from mirror/vulkan-zig
Compare commits
10 Commits
vkbool32-e
...
instructiv
| Author | SHA1 | Date | |
|---|---|---|---|
| c93e5a0fe2 | |||
| d1902bddd6 | |||
|
|
8961518db2 | ||
|
|
7acf3a1163 | ||
|
|
4b7b9a8b94 | ||
|
|
4066c2c526 | ||
|
|
c9c4dae703 | ||
|
|
ecf97034c4 | ||
|
|
3c7d4021e9 | ||
|
|
f879074293 |
@@ -2,7 +2,7 @@
|
||||
.name = .vulkan,
|
||||
.fingerprint = 0xbe155a03c72db6af,
|
||||
.version = "0.0.0",
|
||||
.minimum_zig_version = "0.15.0-dev.1518+749f10af4",
|
||||
.minimum_zig_version = "0.15.1",
|
||||
.paths = .{
|
||||
"build.zig",
|
||||
"LICENSE",
|
||||
|
||||
@@ -35,6 +35,7 @@ pub const GraphicsContext = struct {
|
||||
vkb: BaseWrapper,
|
||||
|
||||
instance: Instance,
|
||||
debug_messenger: vk.DebugUtilsMessengerEXT,
|
||||
surface: vk.SurfaceKHR,
|
||||
pdev: vk.PhysicalDevice,
|
||||
props: vk.PhysicalDeviceProperties,
|
||||
@@ -51,10 +52,11 @@ pub const GraphicsContext = struct {
|
||||
|
||||
var extension_names: std.ArrayList([*:0]const u8) = .empty;
|
||||
defer extension_names.deinit(allocator);
|
||||
// these extensions are to support vulkan in mac os
|
||||
try extension_names.append(allocator, vk.extensions.ext_debug_utils.name);
|
||||
// the following extensions are to support vulkan in mac os
|
||||
// see https://github.com/glfw/glfw/issues/2335
|
||||
try extension_names.append(allocator, "VK_KHR_portability_enumeration");
|
||||
try extension_names.append(allocator, "VK_KHR_get_physical_device_properties2");
|
||||
try extension_names.append(allocator, vk.extensions.khr_portability_enumeration.name);
|
||||
try extension_names.append(allocator, vk.extensions.khr_get_physical_device_properties_2.name);
|
||||
|
||||
var glfw_exts_count: u32 = 0;
|
||||
const glfw_exts = c.glfwGetRequiredInstanceExtensions(&glfw_exts_count);
|
||||
@@ -81,6 +83,22 @@ pub const GraphicsContext = struct {
|
||||
self.instance = Instance.init(instance, vki);
|
||||
errdefer self.instance.destroyInstance(null);
|
||||
|
||||
self.debug_messenger = try self.instance.createDebugUtilsMessengerEXT(&.{
|
||||
.message_severity = .{
|
||||
//.verbose_bit_ext = true,
|
||||
//.info_bit_ext = true,
|
||||
.warning_bit_ext = true,
|
||||
.error_bit_ext = true,
|
||||
},
|
||||
.message_type = .{
|
||||
.general_bit_ext = true,
|
||||
.validation_bit_ext = true,
|
||||
.performance_bit_ext = true,
|
||||
},
|
||||
.pfn_user_callback = &debugUtilsMessengerCallback,
|
||||
.p_user_data = null,
|
||||
}, null);
|
||||
|
||||
self.surface = try createSurface(self.instance, window);
|
||||
errdefer self.instance.destroySurfaceKHR(self.surface, null);
|
||||
|
||||
@@ -107,6 +125,7 @@ pub const GraphicsContext = struct {
|
||||
pub fn deinit(self: GraphicsContext) void {
|
||||
self.dev.destroyDevice(null);
|
||||
self.instance.destroySurfaceKHR(self.surface, null);
|
||||
self.instance.destroyDebugUtilsMessengerEXT(self.debug_messenger, null);
|
||||
self.instance.destroyInstance(null);
|
||||
|
||||
// Don't forget to free the tables to prevent a memory leak.
|
||||
@@ -196,6 +215,17 @@ const QueueAllocation = struct {
|
||||
present_family: u32,
|
||||
};
|
||||
|
||||
fn debugUtilsMessengerCallback(severity: vk.DebugUtilsMessageSeverityFlagsEXT, msg_type: vk.DebugUtilsMessageTypeFlagsEXT, callback_data: ?*const vk.DebugUtilsMessengerCallbackDataEXT, _: ?*anyopaque) callconv(.c) vk.Bool32 {
|
||||
const severity_str = if (severity.verbose_bit_ext) "verbose" else if (severity.info_bit_ext) "info" else if (severity.warning_bit_ext) "warning" else if (severity.error_bit_ext) "error" else "unknown";
|
||||
|
||||
const type_str = if (msg_type.general_bit_ext) "general" else if (msg_type.validation_bit_ext) "validation" else if (msg_type.performance_bit_ext) "performance" else if (msg_type.device_address_binding_bit_ext) "device addr" else "unknown";
|
||||
|
||||
const message: [*c]const u8 = if (callback_data) |cb_data| cb_data.p_message else "NO MESSAGE!";
|
||||
std.debug.print("[{s}][{s}]. Message:\n {s}\n", .{ severity_str, type_str, message });
|
||||
|
||||
return .false;
|
||||
}
|
||||
|
||||
fn pickPhysicalDevice(
|
||||
instance: Instance,
|
||||
allocator: Allocator,
|
||||
|
||||
@@ -52,7 +52,7 @@ pub fn isZigPrimitiveType(name: []const u8) bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
pub fn writeIdentifier(w: *std.io.Writer, id: []const u8) !void {
|
||||
pub fn writeIdentifier(w: *std.Io.Writer, id: []const u8) !void {
|
||||
try w.print("{f}", .{std.zig.fmtId(id)});
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ pub const SegmentIterator = struct {
|
||||
|
||||
pub const IdRenderer = struct {
|
||||
tags: []const []const u8,
|
||||
text_cache: std.io.Writer.Allocating,
|
||||
text_cache: std.Io.Writer.Allocating,
|
||||
|
||||
pub fn init(allocator: Allocator, tags: []const []const u8) IdRenderer {
|
||||
return .{
|
||||
|
||||
10
src/main.zig
10
src/main.zig
@@ -22,6 +22,7 @@ fn reportParseErrors(tree: std.zig.Ast) !void {
|
||||
}
|
||||
try w.writeAll("^\n");
|
||||
}
|
||||
try w.flush();
|
||||
}
|
||||
|
||||
fn oomPanic() noreturn {
|
||||
@@ -70,6 +71,9 @@ pub fn main() !void {
|
||||
) catch |err| {
|
||||
std.process.fatal("failed to write to stdout: {s}", .{@errorName(err)});
|
||||
};
|
||||
w.interface.flush() catch |err| {
|
||||
std.process.fatal("failed to flush stdout: {s}", .{@errorName(err)});
|
||||
};
|
||||
return;
|
||||
} else if (std.mem.eql(u8, arg, "-a") or std.mem.eql(u8, arg, "--api")) {
|
||||
const api_str = args.next() orelse {
|
||||
@@ -102,18 +106,18 @@ pub fn main() !void {
|
||||
};
|
||||
|
||||
const cwd = std.fs.cwd();
|
||||
const xml_src = cwd.readFileAlloc(allocator, xml_path, std.math.maxInt(usize)) catch |err| {
|
||||
const xml_src = cwd.readFileAlloc(xml_path, allocator, .unlimited) catch |err| {
|
||||
std.process.fatal("failed to open input file '{s}' ({s})", .{ xml_path, @errorName(err) });
|
||||
};
|
||||
|
||||
const maybe_video_xml_src = if (maybe_video_xml_path) |video_xml_path|
|
||||
cwd.readFileAlloc(allocator, video_xml_path, std.math.maxInt(usize)) catch |err| {
|
||||
cwd.readFileAlloc(video_xml_path, allocator, .unlimited) catch |err| {
|
||||
std.process.fatal("failed to open input file '{s}' ({s})", .{ video_xml_path, @errorName(err) });
|
||||
}
|
||||
else
|
||||
null;
|
||||
|
||||
var aw: std.io.Writer.Allocating = .init(allocator);
|
||||
var aw: std.Io.Writer.Allocating = .init(allocator);
|
||||
generator.generate(allocator, api, xml_src, maybe_video_xml_src, &aw.writer) catch |err| {
|
||||
if (debug) {
|
||||
return err;
|
||||
|
||||
@@ -335,6 +335,194 @@ pub fn trimVkNamespace(id: []const u8) []const u8 {
|
||||
return id;
|
||||
}
|
||||
|
||||
const Renderer2 = struct {
|
||||
const Self = @This();
|
||||
|
||||
writer: *std.Io.Writer,
|
||||
allocator: Allocator,
|
||||
registry: *const reg.Registry,
|
||||
id_renderer: *IdRenderer,
|
||||
// decls_by_name: std.StringArrayHashMap(reg.DeclarationType),
|
||||
// structure_types: std.StringHashMap(void),
|
||||
have_video: bool,
|
||||
|
||||
fn init(
|
||||
writer: *std.Io.Writer,
|
||||
allocator: Allocator,
|
||||
registry: *const reg.Registry,
|
||||
id_renderer: *IdRenderer,
|
||||
have_video: bool,
|
||||
) !Self {
|
||||
return Self{
|
||||
.writer = writer,
|
||||
.allocator = allocator,
|
||||
.registry = registry,
|
||||
.id_renderer = id_renderer,
|
||||
// .decls_by_name = decls_by_name,
|
||||
// .structure_types = structure_types,
|
||||
.have_video = have_video,
|
||||
};
|
||||
}
|
||||
|
||||
fn deinit(self: *Self) void {
|
||||
_ = self; // autofix
|
||||
// self.decls_by_name.deinit();
|
||||
}
|
||||
|
||||
pub const Error = error{
|
||||
UnhandledBitfieldStruct,
|
||||
InvalidApiConstant,
|
||||
InvalidConstantExpr,
|
||||
InvalidRegistry,
|
||||
UnexpectedCharacter,
|
||||
InvalidCharacter,
|
||||
Overflow,
|
||||
OutOfMemory,
|
||||
WriteFailed,
|
||||
};
|
||||
|
||||
fn render(self: *Self) Error!void {
|
||||
try self.renderDecls();
|
||||
}
|
||||
|
||||
fn text(self: *Self, str: []const u8) Error!void {
|
||||
try self.writer.writeAll(str);
|
||||
}
|
||||
|
||||
fn print(self: *Self, comptime fmt: []const u8, args: anytype) Error!void {
|
||||
try self.writer.print(fmt, args);
|
||||
}
|
||||
|
||||
fn idWithCase(self: *Self, case: CaseStyle, id: []const u8) Error!void {
|
||||
try self.id_renderer.renderWithCase(self.writer, case, id);
|
||||
}
|
||||
|
||||
const _reserved_names = std.StaticStringMap([]const u8).initComptime(.{
|
||||
.{ "void", "void" },
|
||||
.{ "char", "u8" },
|
||||
.{ "float", "f32" },
|
||||
.{ "double", "f64" },
|
||||
.{ "uint8_t", "u8" },
|
||||
.{ "int8_t", "i8" },
|
||||
.{ "int16_t", "i16" },
|
||||
.{ "uint16_t", "u16" },
|
||||
.{ "uint32_t", "u32" },
|
||||
.{ "uint64_t", "u64" },
|
||||
.{ "int32_t", "i32" },
|
||||
.{ "int64_t", "i64" },
|
||||
.{ "size_t", "usize" },
|
||||
.{ "int", "c_int" },
|
||||
});
|
||||
|
||||
fn renderDecls(self: *Self) Error!void {
|
||||
var arena = std.heap.ArenaAllocator.init(self.allocator);
|
||||
defer arena.deinit();
|
||||
|
||||
for (self.registry.decls) |decl| {
|
||||
defer _ = arena.reset(.retain_capacity);
|
||||
|
||||
switch (decl.decl_type) {
|
||||
.alias => |alias| {
|
||||
switch (alias.target) {
|
||||
.other_command => try self.print("pub const Pfn{s} = Pfn{s};\n", .{
|
||||
trimVkNamespace(decl.name),
|
||||
trimVkNamespace(alias.name),
|
||||
}),
|
||||
.other_type => try self.print("pub const {s} = {s};\n", .{
|
||||
trimVkNamespace(decl.name),
|
||||
trimVkNamespace(alias.name),
|
||||
}),
|
||||
}
|
||||
},
|
||||
.handle => |handle| {
|
||||
const backing_int = if (handle.is_dispatchable) "usize" else "u32";
|
||||
try self.print("pub const {s} = enum({s}) {{ null_handle = 0, _ }}; // parent {?s}\n", .{
|
||||
trimVkNamespace(decl.name),
|
||||
backing_int,
|
||||
handle.parent,
|
||||
});
|
||||
},
|
||||
.typedef => |typ| {
|
||||
try self.text("pub const ");
|
||||
if (typ == .command_ptr) {
|
||||
try self.text("Pfn");
|
||||
}
|
||||
try self.text(trimVkNamespace(decl.name));
|
||||
try self.text(" = ");
|
||||
try self.typeInfo(typ);
|
||||
try self.text(";\n");
|
||||
},
|
||||
.foreign => |frn| {
|
||||
if (_reserved_names.has(decl.name)) continue;
|
||||
try self.print("pub const {s} = opaque {{}};", .{trimVkNamespace(decl.name)});
|
||||
if (frn.depends.len > 0) {
|
||||
try self.print(" // requires \"{s}\"", .{frn.depends});
|
||||
}
|
||||
try self.text("\n");
|
||||
},
|
||||
.command => |cmd| {
|
||||
try self.text("pub const Pfn");
|
||||
try self.idWithCase(.title, trimVkNamespace(decl.name));
|
||||
try self.text(" = *const ");
|
||||
try self.commandType(cmd);
|
||||
try self.text(";\n");
|
||||
},
|
||||
else => try self.print("pub const {s} = undefined; // {s};\n", .{
|
||||
trimVkNamespace(decl.name),
|
||||
@tagName(decl.decl_type),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn commandType(self: *Self, cmd: reg.Command) Error!void {
|
||||
try self.text("fn(");
|
||||
for (cmd.params, 0..) |param, idx| {
|
||||
if (idx > 0) try self.text(", ");
|
||||
try self.idWithCase(.snake, param.name);
|
||||
try self.text(": ");
|
||||
try self.typeInfo(param.param_type);
|
||||
}
|
||||
try self.text(") void");
|
||||
}
|
||||
|
||||
fn typeInfo(self: *Self, typ: reg.TypeInfo) Error!void {
|
||||
switch (typ) {
|
||||
.name => |name| {
|
||||
if (_reserved_names.get(name)) |real| {
|
||||
try self.text(real);
|
||||
} else {
|
||||
try self.text(trimVkNamespace(name));
|
||||
}
|
||||
},
|
||||
.command_ptr => |cmd| {
|
||||
try self.text("?*const ");
|
||||
try self.commandType(cmd);
|
||||
},
|
||||
.pointer => |ptr| {
|
||||
if (ptr.is_optional) try self.text("?");
|
||||
switch (ptr.size) {
|
||||
.one => try self.text("*"),
|
||||
.many, .other_field => try self.text("[*]"),
|
||||
.zero_terminated => try self.text("[*:0]"),
|
||||
}
|
||||
if (ptr.is_const) try self.text("const ");
|
||||
try self.typeInfo(ptr.child.*);
|
||||
},
|
||||
.array => |arr| {
|
||||
if (arr.is_optional) try self.text("?");
|
||||
try self.text("[");
|
||||
switch (arr.size) {
|
||||
.int => |size| try self.print("{}", .{size}),
|
||||
.alias => |_| @panic("Not implemented."),
|
||||
}
|
||||
try self.text("]");
|
||||
try self.typeInfo(arr.child.*);
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const Renderer = struct {
|
||||
const Self = @This();
|
||||
const RenderTypeInfoError = std.Io.Writer.Error || std.fmt.ParseIntError || error{ OutOfMemory, InvalidRegistry };
|
||||
@@ -2141,7 +2329,9 @@ pub fn render(
|
||||
id_renderer: *IdRenderer,
|
||||
have_video: bool,
|
||||
) !void {
|
||||
var renderer = try Renderer.init(writer, allocator, registry, id_renderer, have_video);
|
||||
// var renderer = try Renderer.init(writer, allocator, registry, id_renderer, have_video);
|
||||
var renderer = try Renderer2.init(writer, allocator, registry, id_renderer, have_video);
|
||||
defer renderer.deinit();
|
||||
try renderer.render();
|
||||
try writer.flush();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user