Initial Commit
This commit is contained in:
20 files changed
+8665
No files matched your search
@@ -0,0 +1,150 @@
|
||||
#! Writes the files of `malunal::vulkan`, into `vulkan/`, from the Vulkan
|
||||
#! registry, `vk.xml`: the Vulkan SDK's, under `VULKAN_SDK`, unless another is
|
||||
#! named. Run it from the package's root whenever the SDK is updated:
|
||||
#!
|
||||
#! ```sh
|
||||
#! blossom run malunal.vulkan.bindings
|
||||
#! ```
|
||||
#!
|
||||
#! It binds the newest version of Vulkan's core the registry has, unless
|
||||
#! `--version` names an older one, and the extensions of `extensions`, with any
|
||||
#! `--extension` names besides. The files it writes are whole: change this, or
|
||||
#! `tools/writing.sakura`, rather than them. A file of a platform's it no longer
|
||||
#! writes, as one an extension named once made, is left as it was, to be removed.
|
||||
|
||||
import std::args;
|
||||
import std::containers;
|
||||
import std::io;
|
||||
import std::process;
|
||||
|
||||
# The extensions the library binds: surfaces and swapchains, then naming objects
|
||||
# and hearing the validation layers.
|
||||
const extensions: strview[12] = [
|
||||
"VK_KHR_surface",
|
||||
"VK_KHR_swapchain",
|
||||
"VK_KHR_display",
|
||||
"VK_KHR_display_swapchain",
|
||||
"VK_KHR_get_surface_capabilities2",
|
||||
"VK_KHR_get_display_properties2",
|
||||
"VK_EXT_headless_surface",
|
||||
"VK_KHR_win32_surface",
|
||||
"VK_KHR_xlib_surface",
|
||||
"VK_KHR_xcb_surface",
|
||||
"VK_KHR_wayland_surface",
|
||||
"VK_EXT_debug_utils",
|
||||
];
|
||||
|
||||
# The extensions whose commands the loader exports, as the core's, so they're
|
||||
# C's functions: the window system's, which `vulkan-1.dll` and `libvulkan.so.1`
|
||||
# export. Any other's are found by `vkGetInstanceProcAddr`.
|
||||
const exported: strview[11] = [
|
||||
"VK_KHR_surface",
|
||||
"VK_KHR_swapchain",
|
||||
"VK_KHR_display",
|
||||
"VK_KHR_display_swapchain",
|
||||
"VK_KHR_get_surface_capabilities2",
|
||||
"VK_KHR_get_display_properties2",
|
||||
"VK_EXT_headless_surface",
|
||||
"VK_KHR_win32_surface",
|
||||
"VK_KHR_xlib_surface",
|
||||
"VK_KHR_xcb_surface",
|
||||
"VK_KHR_wayland_surface",
|
||||
];
|
||||
|
||||
@args::command{ name = "bindings", help = "Writes the files of malunal::vulkan from the Vulkan registry, vk.xml." }
|
||||
struct asked {
|
||||
@args::option{ value = "FILE", help = "The registry read: by default, the Vulkan SDK's, under VULKAN_SDK." }
|
||||
var from: maybe<strview>;
|
||||
@args::option{ value = "FOLDER", help = "Where the files are written: by default, vulkan." }
|
||||
var into: maybe<strview>;
|
||||
@args::option{ value = "MAJOR.MINOR", help = "The newest version of Vulkan's core bound: by default, the registry's newest." }
|
||||
var version: maybe<strview>;
|
||||
@args::option{ value = "NAME", help = "An extension bound besides the library's own, as VK_KHR_present_wait. Write it once for each." }
|
||||
var extension: vector<strview>;
|
||||
}
|
||||
|
||||
def fail(why: strview): void {
|
||||
var out := error_output_locked();
|
||||
_ = out.writer().write($"bindings: {why}\n");
|
||||
system::exit(1);
|
||||
}
|
||||
|
||||
def main(args: span<strview>): void {
|
||||
val given := args::parse_or_exit::<asked>(args);
|
||||
var wanted := wanting{ extensions = vector::<strview>::new(), exported = vector::<strview>::new() };
|
||||
for &each in extensions {
|
||||
wanted.extensions.append(each);
|
||||
}
|
||||
for &each in given.extension {
|
||||
if !holds_name(&wanted.extensions, each) {
|
||||
wanted.extensions.append(each);
|
||||
}
|
||||
}
|
||||
for &each in exported {
|
||||
wanted.exported.append(each);
|
||||
}
|
||||
if given.version is some(version) {
|
||||
val dot := version.find(".") else {
|
||||
fail($"--version {version} isn't a version, as 1.3 is.");
|
||||
return;
|
||||
};
|
||||
wanted.major = number_of(version.substr(0, dot)) as u32;
|
||||
wanted.minor = number_of(version.substr(dot + 1, version.length - dot - 1)) as u32;
|
||||
}
|
||||
|
||||
var file := string::new(nothing{});
|
||||
if given.from is some(from) {
|
||||
file.append(from);
|
||||
} else {
|
||||
val sdk := process::variable("VULKAN_SDK") else {
|
||||
fail("VULKAN_SDK isn't set, so there's no registry to read: name one with --from.");
|
||||
return;
|
||||
};
|
||||
file.append(sdk);
|
||||
file.append("/share/vulkan/registry/vk.xml");
|
||||
}
|
||||
val at := path::parse(file.span) else (_) {
|
||||
fail($"{file} isn't a path.");
|
||||
return;
|
||||
};
|
||||
val text := read_file(&at) else (e) {
|
||||
fail($"{file} couldn't be read ({e}).");
|
||||
return;
|
||||
};
|
||||
|
||||
var made := vector::<made_file>::new();
|
||||
var problems := vector::<string>::new();
|
||||
if !bindings_of(text.span, &wanted, &mut made, &mut problems) {
|
||||
var out := error_output_locked();
|
||||
for &each in problems {
|
||||
_ = out.writer().write($"bindings: {each}\n");
|
||||
}
|
||||
system::exit(1);
|
||||
}
|
||||
|
||||
val folder := given.into else "vulkan";
|
||||
val into := path::parse(folder) else (_) {
|
||||
fail($"{folder} isn't a path.");
|
||||
return;
|
||||
};
|
||||
create_directories(&into) else (e) {
|
||||
fail($"{folder} couldn't be made ({e}).");
|
||||
};
|
||||
for &each in made {
|
||||
val name := $"{folder}/{each.name}";
|
||||
val written := path::parse(name.span) else (_) {
|
||||
fail($"{name} isn't a path.");
|
||||
return;
|
||||
};
|
||||
write_file(&written, each.text.span) else (e) {
|
||||
fail($"{name} couldn't be written ({e}).");
|
||||
};
|
||||
var lines: u64 = 0;
|
||||
for i in 0..each.text.length {
|
||||
if each.text.span[i] == '\n' {
|
||||
lines++;
|
||||
}
|
||||
}
|
||||
println($"Wrote {name}, of {lines} lines.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,316 @@
|
||||
#! Contains `picking`: what a version of Vulkan's core and some extensions need
|
||||
#! of a registry, each thing placed in the files every system builds, or in a
|
||||
#! platform's own, as `win32.sakura`, where only a platform's extensions need
|
||||
#! it.
|
||||
|
||||
import std::containers;
|
||||
|
||||
#! What's bound: a version of Vulkan's core, and extensions.
|
||||
struct wanting {
|
||||
var major: u32; #! The newest version of Vulkan's core bound, or 0 for the registry's newest.
|
||||
var minor: u32;
|
||||
var extensions: vector<strview>; #! The extensions bound, by name.
|
||||
var exported: vector<strview>; #! Those of them whose commands the loader exports, as `vulkan-1.dll` does a surface's.
|
||||
}
|
||||
|
||||
# A platform whose extensions may be bound, in a file of its own, and the system
|
||||
# that file is built for.
|
||||
struct platform_file {
|
||||
var platform: strview;
|
||||
var os: strview;
|
||||
}
|
||||
|
||||
# The platforms bound: those of the systems Sakura builds for.
|
||||
const platform_files: platform_file[6] = [
|
||||
platform_file{ "win32", "windows" },
|
||||
platform_file{ "xlib", "linux" },
|
||||
platform_file{ "xlib_xrandr", "linux" },
|
||||
platform_file{ "xcb", "linux" },
|
||||
platform_file{ "wayland", "linux" },
|
||||
platform_file{ "directfb", "linux" },
|
||||
];
|
||||
|
||||
# Where a platform's file is among `platform_files`, and 1 past it, as places
|
||||
# count: 0 is the files every system builds.
|
||||
def place_of_platform(platform: strview): u32 {
|
||||
for i in 0..6u {
|
||||
return i as u32 + 1 if platform_files[i].platform == platform;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
# Whether a list of names holds one.
|
||||
def holds_name(list: &vector<strview>, name: strview): bool {
|
||||
for &each in list {
|
||||
return true if each == name;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! What's bound of a registry, and where.
|
||||
struct picking {
|
||||
var major: u32; #! The newest version of the core bound.
|
||||
var minor: u32;
|
||||
var places: dict<strview, u32>; #! Each type, command, and constant bound, by name: 0 for the files every system builds, or 1 and its platform's index.
|
||||
var exported: dict<strview, bool>; #! Each command bound that the loader exports.
|
||||
var removed: dict<strview, bool>; #! What a bound set removes.
|
||||
var included: dict<strview, bool>; #! Each feature and extension bound, by name.
|
||||
var order: vector<u32>; #! Those, in turn, as the sets of `registry`: features, extensions, and then the platforms'.
|
||||
var extended: vector<u32>; #! The values bound sets give enums, in turn, as `registry`'s.
|
||||
var defined: vector<u32>; #! The constants bound sets define, in turn.
|
||||
var definers: vector<u32>; #! The set defining each of those, as `registry`'s.
|
||||
var given: dict<strview, bool>; #! Each of those, by name, so each is bound once.
|
||||
}
|
||||
|
||||
#! Creates a picking of nothing, to pick.
|
||||
def picking::new(): picking {
|
||||
return picking{
|
||||
places = dict::<strview, u32>::new(),
|
||||
exported = dict::<strview, bool>::new(),
|
||||
removed = dict::<strview, bool>::new(),
|
||||
included = dict::<strview, bool>::new(),
|
||||
given = dict::<strview, bool>::new()
|
||||
};
|
||||
}
|
||||
|
||||
#! Picks what's bound of a registry.
|
||||
#! @param this The picking, empty.
|
||||
#! @param reg The registry.
|
||||
#! @param wanted What's bound.
|
||||
#! @param problems Where why it can't be is said.
|
||||
#! @returns Whether it could be.
|
||||
def picking::pick(&mut this, reg: ®istry, wanted: &wanting, problems: &mut vector<string>): bool {
|
||||
this.major = wanted.major;
|
||||
this.minor = wanted.minor;
|
||||
if wanted.major == 0 {
|
||||
for &each in reg.sets {
|
||||
if each.feature && each.supported && (each.major > this.major || (each.major == this.major && each.minor > this.minor)) {
|
||||
this.major = each.major;
|
||||
this.minor = each.minor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Each version of the core, to the newest bound.
|
||||
var versioned := false;
|
||||
for i, &each in reg.sets {
|
||||
continue if !each.feature || !each.supported;
|
||||
continue if each.major > this.major || (each.major == this.major && each.minor > this.minor);
|
||||
this.included.set(each.name, true);
|
||||
this.order.append(i as u32);
|
||||
versioned = versioned || (each.major == this.major && each.minor == this.minor);
|
||||
}
|
||||
if !versioned {
|
||||
problems.append($"The registry has no Vulkan {this.major}.{this.minor}.");
|
||||
return false;
|
||||
}
|
||||
|
||||
# Each extension, as it can be bound.
|
||||
for &name in wanted.extensions {
|
||||
var at: u32 = 0;
|
||||
if reg.set_at.get(&name) is some(&found) {
|
||||
at = found;
|
||||
} else {
|
||||
problems.append($"The registry has no extension {name}.");
|
||||
continue;
|
||||
}
|
||||
val set := ®.sets[at as u64];
|
||||
if set.feature {
|
||||
problems.append($"{name} is a version of Vulkan, which --version binds.");
|
||||
} else if !set.supported {
|
||||
problems.append($"{name} isn't Vulkan's, or is disabled.");
|
||||
} else if set.platform.length != 0 && place_of_platform(set.platform) == 0 {
|
||||
problems.append($"{name} is of the platform {set.platform}, which isn't bound: only those of Windows and Linux are.");
|
||||
} else {
|
||||
this.included.set(name, true);
|
||||
}
|
||||
}
|
||||
for &name in wanted.extensions {
|
||||
var at: u32 = 0;
|
||||
if reg.set_at.get(&name) is some(&found) {
|
||||
at = found;
|
||||
}
|
||||
val set := ®.sets[at as u64];
|
||||
if this.included.contains(&name) && set.depends.length != 0 && !this.holds(set.depends) {
|
||||
problems.append($"{name} needs {set.depends}, which aren't all bound: bind them too, with --extension.");
|
||||
}
|
||||
}
|
||||
if problems.length != 0 {
|
||||
return false;
|
||||
}
|
||||
|
||||
# The extensions, in the registry's order: those of every system, then the
|
||||
# platforms', so what both need is placed where every system has it.
|
||||
for pass in 0..2u {
|
||||
for i, &each in reg.sets {
|
||||
continue if each.feature || !this.included.contains(&each.name);
|
||||
continue if (each.platform.length != 0) != (pass == 1);
|
||||
this.order.append(i as u32);
|
||||
}
|
||||
}
|
||||
|
||||
# What each needs, in turn.
|
||||
for k in 0..this.order.length {
|
||||
val set := ®.sets[this.order[k] as u64];
|
||||
val place := place_of_platform(set.platform);
|
||||
val exports := !set.feature && holds_name(&wanted.exported, set.name);
|
||||
for n in set.first..(set.first + set.count) {
|
||||
val item := ®.needs[n as u64];
|
||||
continue if item.depends.length != 0 && !this.holds(item.depends);
|
||||
if item.removes {
|
||||
this.removed.set(item.name, true);
|
||||
} else if item.what == need_kind::of_type {
|
||||
this.need_type(reg, item.name, place);
|
||||
} else if item.what == need_kind::of_command {
|
||||
this.need_command(reg, item.name, place, set.feature, exports);
|
||||
} else if item.what == need_kind::of_constant {
|
||||
this.need_constant(reg, item.name, place);
|
||||
} else if !this.given.contains(&item.name) {
|
||||
this.given.set(item.name, true);
|
||||
if !this.places.contains(&item.name) {
|
||||
this.places.set(item.name, place);
|
||||
}
|
||||
if item.what == need_kind::extends {
|
||||
this.extended.append(item.value);
|
||||
} else {
|
||||
this.defined.append(item.value);
|
||||
this.definers.append(this.order[k]);
|
||||
val alias := reg.values[item.value as u64].alias;
|
||||
this.need_constant(reg, alias, place);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Where a type, a command, or a constant is bound, if it is.
|
||||
#! @returns 0 for the files every system builds, 1 and a platform's index for
|
||||
#! its own, or `nothing` where it isn't bound.
|
||||
def picking::place_of(&this, name: strview): maybe<u32> {
|
||||
if this.removed.contains(&name) {
|
||||
return nothing{};
|
||||
}
|
||||
if this.places.get(&name) is some(&at) {
|
||||
return at;
|
||||
}
|
||||
return nothing{};
|
||||
}
|
||||
|
||||
#! Whether a type, a command, or a constant is bound in a place.
|
||||
def picking::is_at(&this, name: strview, place: u32): bool {
|
||||
if this.place_of(name) is some(at) {
|
||||
return at == place;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
# Binds a type, and those it's made of.
|
||||
def picking::need_type(&mut this, reg: ®istry, name: strview, place: u32): void {
|
||||
if name.length == 0 || this.places.contains(&name) {
|
||||
return;
|
||||
}
|
||||
val t := reg.type_named(name) else { return; };
|
||||
this.places.set(name, place);
|
||||
if t.alias.length != 0 {
|
||||
this.need_type(reg, t.alias, place);
|
||||
}
|
||||
if t.category == category::structure || t.category == category::joined || t.category == category::callback {
|
||||
for &d in reg.declarations_of(t.first, t.count) {
|
||||
this.need_type(reg, d.base, place);
|
||||
# An array's length, where a constant names it.
|
||||
for i in 0..(d.lengths as u64) {
|
||||
this.need_constant(reg, d.sizes[i], place);
|
||||
}
|
||||
}
|
||||
this.need_type(reg, t.returns.base, place);
|
||||
} else if t.category == category::bitmask || t.category == category::base {
|
||||
this.need_type(reg, t.requires, place);
|
||||
}
|
||||
}
|
||||
|
||||
# Binds a command, and the types it takes and gives.
|
||||
def picking::need_command(&mut this, reg: ®istry, name: strview, place: u32, by_feature: bool, exports: bool): void {
|
||||
if name.length == 0 || this.places.contains(&name) {
|
||||
return;
|
||||
}
|
||||
var at: u32 = 0;
|
||||
if reg.command_at.get(&name) is some(&found) {
|
||||
at = found;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
val c := ®.commands[at as u64];
|
||||
this.places.set(name, place);
|
||||
if (by_feature ? c.exported : exports) {
|
||||
this.exported.set(name, true);
|
||||
}
|
||||
if c.alias.length != 0 {
|
||||
this.need_command(reg, c.alias, place, false, false);
|
||||
return;
|
||||
}
|
||||
this.need_type(reg, c.returns.base, place);
|
||||
for &d in reg.declarations_of(c.first, c.count) {
|
||||
this.need_type(reg, d.base, place);
|
||||
}
|
||||
}
|
||||
|
||||
# Binds a constant of the API's, as `VK_MAX_EXTENSION_NAME_SIZE`.
|
||||
def picking::need_constant(&mut this, reg: ®istry, name: strview, place: u32): void {
|
||||
if name.length == 0 || this.places.contains(&name) || !reg.value_at.contains(&name) {
|
||||
return;
|
||||
}
|
||||
this.places.set(name, place);
|
||||
}
|
||||
|
||||
#! Whether what a set depends on is bound: names of features and extensions,
|
||||
#! `+` between those all needed, `,` between those any one of which is, in
|
||||
#! parentheses where they're mixed.
|
||||
#! @param this The picking.
|
||||
#! @param text What it depends on, as `VK_KHR_surface+(VK_VERSION_1_1,VK_KHR_device_group)`.
|
||||
#! @returns Whether it's bound.
|
||||
def picking::holds(&this, text: strview): bool {
|
||||
var at: u64 = 0;
|
||||
return this.any_of(text, &mut at);
|
||||
}
|
||||
|
||||
def picking::any_of(&this, text: strview, at: &mut u64): bool {
|
||||
var held := this.all_of(text, at);
|
||||
while at < text.length && text[at] == ',' {
|
||||
at++;
|
||||
val next := this.all_of(text, at);
|
||||
held = held || next;
|
||||
}
|
||||
return held;
|
||||
}
|
||||
|
||||
def picking::all_of(&this, text: strview, at: &mut u64): bool {
|
||||
var held := this.one_of(text, at);
|
||||
while at < text.length && text[at] == '+' {
|
||||
at++;
|
||||
val next := this.one_of(text, at);
|
||||
held = held && next;
|
||||
}
|
||||
return held;
|
||||
}
|
||||
|
||||
def picking::one_of(&this, text: strview, at: &mut u64): bool {
|
||||
if at < text.length && text[at] == '(' {
|
||||
at++;
|
||||
val held := this.any_of(text, at);
|
||||
if at < text.length && text[at] == ')' {
|
||||
at++;
|
||||
}
|
||||
return held;
|
||||
}
|
||||
val start: u64 = at;
|
||||
while at < text.length && (text[at].is_ident || text[at] == ':') {
|
||||
at++;
|
||||
}
|
||||
var name := text.substr(start, at - start);
|
||||
# A feature of an extension, as `VK_KHR_x::shaderY`, is held where the extension is.
|
||||
if name.find("::") is some(cut) {
|
||||
name = name.substr(0, cut);
|
||||
}
|
||||
return this.included.contains(&name);
|
||||
}
|
||||
@@ -0,0 +1,561 @@
|
||||
#! Contains `registry`, the Vulkan registry, `vk.xml`, read into records of
|
||||
#! what it declares: its types, the members of its structs and unions, its enums
|
||||
#! and their values, its commands and their parameters, and the features and
|
||||
#! extensions that require them. What only another API has, as Vulkan SC's, is
|
||||
#! left out as it's read.
|
||||
#!
|
||||
#! Records name what they hold by views of the document's own text, so a
|
||||
#! registry keeps the document it was read from, and stays where it was made.
|
||||
|
||||
import std::containers;
|
||||
import std::xml;
|
||||
|
||||
# The kinds of types the registry declares.
|
||||
enum category: u8 {
|
||||
other, # What isn't bound: a define, or an include.
|
||||
basic, # C's own, as `uint32_t`, or a platform's, as `HWND`: Sakura's are named by a table.
|
||||
base, # Another name for a type, as `VkBool32` for `uint32_t`, or one C declares without saying what it is.
|
||||
handle, # An object's handle.
|
||||
enumerated, # An enum, whose values are constants.
|
||||
bitmask, # Flags, of `VkFlags` or `VkFlags64`, whose bits an enum names.
|
||||
structure, # A struct.
|
||||
joined, # A union.
|
||||
callback, # A function C calls back, as `PFN_vkAllocationFunction`.
|
||||
}
|
||||
|
||||
# A declaration as C writes it: of a struct's member, a parameter, or what a
|
||||
# function returns. It names a type, and may point to it, hold an array of it,
|
||||
# or hold some bits of it.
|
||||
struct declaration {
|
||||
var name: strview; # As declared, as `pCreateInfo`: empty for what a function returns.
|
||||
var base: strview; # The type named, as `VkInstanceCreateInfo`.
|
||||
var pointers: u32; # How many `*` follow it.
|
||||
var consts: u32; # Bit 0 where the type named is const, and bit k where the k-th pointer is.
|
||||
var sizes: strview[2]; # An array's lengths, outermost first, each a number or a constant's name.
|
||||
var lengths: u32; # How many of `sizes` it has.
|
||||
var bits: u32; # A bitfield's width, or 0.
|
||||
var optional: strview; # Its `optional`: whether it may be zero, and what it points to.
|
||||
var len: strview; # Its `len`: what counts the elements a pointer points to.
|
||||
var values: strview; # A member's `values`: the one value it holds, as `sType`'s.
|
||||
var comment: strview; # What the registry says of it.
|
||||
}
|
||||
|
||||
# A type the registry declares.
|
||||
struct type_record {
|
||||
var name: strview;
|
||||
var category: category;
|
||||
var alias: strview; # The type it's another name for, where it's an alias.
|
||||
var requires: strview; # A bitmask's enum of bits; a base type's type.
|
||||
var wide: bool; # Whether a bitmask is of `VkFlags64`.
|
||||
var first: u32; # A struct's or union's first member, or a callback's first parameter, in `declarations`.
|
||||
var count: u32; # How many it has.
|
||||
var returns: declaration; # What a callback returns.
|
||||
var comment: strview;
|
||||
}
|
||||
|
||||
# A block of enum values: an enum's, or the API's constants.
|
||||
struct group_record {
|
||||
var name: strview;
|
||||
var kind: strview; # `enum`, `bitmask`, or empty for the API's constants.
|
||||
var wide: bool; # Whether its bits are of 64.
|
||||
var negative: bool; # Whether a value is below 0, as `VkResult`'s errors are.
|
||||
var first: u32; # Its first value, in `values`.
|
||||
var count: u32;
|
||||
}
|
||||
|
||||
# A value of an enum, or a constant.
|
||||
struct value_record {
|
||||
var name: strview;
|
||||
var group: strview; # The enum or block it's a value of, or empty for a constant a feature or an extension defines.
|
||||
var written: strview; # Its `value`, as written: `0x7FFFFFFF`, `(~0U)`, `1000.0F`, `"VK_KHR_surface"`.
|
||||
var bitpos: strview; # Its bit, where it's a bit.
|
||||
var alias: strview; # The value it's another name for.
|
||||
var c_type: strview; # A constant's type, as `uint32_t`.
|
||||
var offset: i64; # Its value, as an extension's number and an offset give it, where `computed`.
|
||||
var computed: bool;
|
||||
var comment: strview;
|
||||
}
|
||||
|
||||
# A command the registry declares.
|
||||
struct command_record {
|
||||
var name: strview;
|
||||
var alias: strview; # The command it's another name for.
|
||||
var returns: declaration;
|
||||
var first: u32; # Its first parameter, in `declarations`.
|
||||
var count: u32;
|
||||
var exported: bool; # Whether the loader exports it, as `export` says of Vulkan's core.
|
||||
}
|
||||
|
||||
# What a feature's or an extension's `require` or `remove` names.
|
||||
enum need_kind: u8 {
|
||||
of_type, # A type, by name.
|
||||
of_command, # A command, by name.
|
||||
of_constant, # A constant declared elsewhere, by name.
|
||||
defines, # A constant it defines, as an extension's name.
|
||||
extends, # A value it gives an enum.
|
||||
}
|
||||
|
||||
struct need {
|
||||
var what: need_kind;
|
||||
var name: strview;
|
||||
var depends: strview; # What the `require` holds only with, as `VK_VERSION_1_1`.
|
||||
var removes: bool; # Whether it's a `remove`'s.
|
||||
var value: u32; # The value it defines or gives, in `values`.
|
||||
}
|
||||
|
||||
# A feature, which is a version of Vulkan's core, or an extension.
|
||||
struct set_record {
|
||||
var name: strview;
|
||||
var feature: bool;
|
||||
var major: u32; # A feature's version.
|
||||
var minor: u32;
|
||||
var number: i64; # An extension's number, which its values' offsets are counted from.
|
||||
var supported: bool; # Whether it's Vulkan's, and not disabled.
|
||||
var platform: strview; # The platform an extension is of, as `win32`.
|
||||
var depends: strview; # What it needs bound with it.
|
||||
var first: u32; # Its first need, in `needs`.
|
||||
var count: u32;
|
||||
}
|
||||
|
||||
#! The Vulkan registry, read.
|
||||
struct registry {
|
||||
var document: xml::document;
|
||||
var types: vector<type_record>;
|
||||
var declarations: vector<declaration>;
|
||||
var blocks: vector<group_record>;
|
||||
var values: vector<value_record>;
|
||||
var commands: vector<command_record>;
|
||||
var sets: vector<set_record>;
|
||||
var needs: vector<need>;
|
||||
var type_at: dict<strview, u32>;
|
||||
var block_at: dict<strview, u32>;
|
||||
var value_at: dict<strview, u32>;
|
||||
var command_at: dict<strview, u32>;
|
||||
var set_at: dict<strview, u32>;
|
||||
var header: u64; # `VK_HEADER_VERSION`, the registry's own.
|
||||
}
|
||||
|
||||
#! Creates a registry holding nothing, to be read.
|
||||
def registry::new(): registry {
|
||||
return registry{
|
||||
type_at = dict::<strview, u32>::new(),
|
||||
block_at = dict::<strview, u32>::new(),
|
||||
value_at = dict::<strview, u32>::new(),
|
||||
command_at = dict::<strview, u32>::new(),
|
||||
set_at = dict::<strview, u32>::new()
|
||||
};
|
||||
}
|
||||
|
||||
# Whether an element is Vulkan's, as its `api` says, where it says.
|
||||
def for_vulkan(e: &xml::element): bool {
|
||||
val api := e.attribute("api") else "";
|
||||
return api.length == 0 || has_word(api, "vulkan");
|
||||
}
|
||||
|
||||
# Whether a list of words, apart by commas, holds a word.
|
||||
def has_word(list: strview, word: strview): bool {
|
||||
var start: u64 = 0;
|
||||
for i in 0..(list.length + 1) {
|
||||
if i == list.length || list[i] == ',' {
|
||||
return true if list.substr(start, i - start) == word;
|
||||
start = i + 1;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
# The text an element holds of its own, as `<name>` and `<type>` hold one name.
|
||||
def lone_text(e: &xml::element): strview {
|
||||
for child in e.children() {
|
||||
return child.text if child.kind == xml::kind::text;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
# The text of an element's first child of a name.
|
||||
def text_of_child(e: &xml::element, name: strview): strview {
|
||||
for child in e.elements() {
|
||||
return lone_text(&child) if child.name == name;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
# Splits C into names, numbers, and marks, each a view of the text.
|
||||
def split_into(text: strview, into: &mut vector<strview>): void {
|
||||
var i: u64 = 0;
|
||||
while i < text.length {
|
||||
val c: u8 = text[i];
|
||||
if c.is_space {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if c.is_ident {
|
||||
val start := i;
|
||||
while i < text.length && text[i].is_ident {
|
||||
i++;
|
||||
}
|
||||
into.append(text.substr(start, i - start));
|
||||
continue;
|
||||
}
|
||||
into.append(text.substr(i, 1));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
# The C an element holds, as words, its comments left out.
|
||||
def tokens_of(e: &xml::element, into: &mut vector<strview>): void {
|
||||
for child in e.children() {
|
||||
if child.kind == xml::kind::text {
|
||||
split_into(child.text, into);
|
||||
} else if child.as_element() is some(inner) {
|
||||
continue if inner.name == "comment";
|
||||
split_into(lone_text(&inner), into);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# A declaration, of C's words: `const char* const* ppEnabledLayerNames`,
|
||||
# `char deviceName[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE]`, `uint32_t mask:8`.
|
||||
def declaration_of(tokens: span<strview>): declaration {
|
||||
var made := declaration{};
|
||||
var i: u64 = 0;
|
||||
while i < tokens.length {
|
||||
val t: strview = tokens[i];
|
||||
if t == "const" {
|
||||
made.consts |= 1 << made.pointers;
|
||||
} else if t == "*" {
|
||||
made.pointers++;
|
||||
} else if t == "[" {
|
||||
if i + 1 < tokens.length && made.lengths < 2 {
|
||||
made.sizes[made.lengths as u64] = tokens[i + 1];
|
||||
made.lengths++;
|
||||
}
|
||||
i += 2;
|
||||
} else if t == ":" {
|
||||
if i + 1 < tokens.length {
|
||||
made.bits = number_of(tokens[i + 1]) as u32;
|
||||
}
|
||||
i++;
|
||||
} else if t == "struct" || t == "typedef" || t == "]" || t == ";" {
|
||||
} else if made.base.length == 0 {
|
||||
made.base = t;
|
||||
} else {
|
||||
made.name = t;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# A number written in decimal, or in hex after `0x`.
|
||||
def number_of(text: strview): u64 {
|
||||
var made: u64 = 0;
|
||||
if text.starts_with("0x") || text.starts_with("0X") {
|
||||
for i in 2..text.length {
|
||||
made = made * 16 + text[i].digit_value;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
for i in 0..text.length {
|
||||
break if !text[i].is_digit;
|
||||
made = made * 10 + text[i].digit_value;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Reads the registry from its text.
|
||||
#! @param this The registry, empty, which is made where it stays.
|
||||
#! @param text The text of `vk.xml`.
|
||||
#! @param problems Where why it can't be read is said.
|
||||
#! @returns Whether it was read.
|
||||
def registry::read(&mut this, text: strview, problems: &mut vector<string>): bool {
|
||||
var parsed := xml::parse(text) else (e) {
|
||||
problems.append($"The registry isn't XML: {e.describe()}");
|
||||
return false;
|
||||
};
|
||||
this.document = move(parsed);
|
||||
val root := this.document.root;
|
||||
if root.name != "registry" {
|
||||
problems.append(string::from("The XML isn't a Vulkan registry: its root isn't <registry>.", nothing{}));
|
||||
return false;
|
||||
}
|
||||
for part in root.elements() {
|
||||
if part.name == "types" {
|
||||
this.read_types(&part);
|
||||
} else if part.name == "enums" {
|
||||
this.read_block(&part);
|
||||
} else if part.name == "commands" {
|
||||
this.read_commands(&part);
|
||||
} else if part.name == "feature" {
|
||||
this.read_set(&part, true);
|
||||
} else if part.name == "extensions" {
|
||||
for each in part.elements() {
|
||||
if each.name == "extension" {
|
||||
this.read_set(&each, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
# Reads each type of `<types>`.
|
||||
def registry::read_types(&mut this, types: &xml::element): void {
|
||||
var tokens := vector::<strview>::new();
|
||||
for each in types.elements() {
|
||||
continue if each.name != "type" || !for_vulkan(&each);
|
||||
val kind_of := each.attribute("category") else "";
|
||||
var made := type_record{
|
||||
name = each.attribute("name") else text_of_child(&each, "name"),
|
||||
alias = each.attribute("alias") else "",
|
||||
comment = each.attribute("comment") else ""
|
||||
};
|
||||
if kind_of == "struct" || kind_of == "union" {
|
||||
made.category = kind_of == "struct" ? category::structure : category::joined;
|
||||
made.first = this.declarations.length as u32;
|
||||
for member in each.elements() {
|
||||
continue if member.name != "member" || !for_vulkan(&member);
|
||||
tokens.clear();
|
||||
tokens_of(&member, &mut tokens);
|
||||
var read := declaration_of(tokens.span);
|
||||
read.optional = member.attribute("optional") else "";
|
||||
read.len = member.attribute("len") else "";
|
||||
read.values = member.attribute("values") else "";
|
||||
read.comment = text_of_child(&member, "comment");
|
||||
this.declarations.append(read);
|
||||
}
|
||||
made.count = this.declarations.length as u32 - made.first;
|
||||
} else if kind_of == "handle" {
|
||||
made.category = category::handle;
|
||||
} else if kind_of == "enum" {
|
||||
made.category = category::enumerated;
|
||||
} else if kind_of == "bitmask" {
|
||||
made.category = category::bitmask;
|
||||
made.requires = each.attribute("requires") else (each.attribute("bitvalues") else "");
|
||||
made.wide = text_of_child(&each, "type") == "VkFlags64";
|
||||
} else if kind_of == "basetype" {
|
||||
made.category = category::base;
|
||||
made.requires = text_of_child(&each, "type");
|
||||
} else if kind_of == "funcpointer" {
|
||||
made.category = category::callback;
|
||||
tokens.clear();
|
||||
tokens_of(&each, &mut tokens);
|
||||
this.read_callback(tokens.span, &mut made);
|
||||
} else if kind_of.length == 0 && each.attribute("requires") is some(_) {
|
||||
made.category = category::basic;
|
||||
} else if kind_of == "define" && made.name == "VK_HEADER_VERSION" {
|
||||
tokens.clear();
|
||||
tokens_of(&each, &mut tokens);
|
||||
this.header = number_of(tokens[tokens.length - 1]);
|
||||
}
|
||||
if made.name.length != 0 && !this.type_at.contains(&made.name) {
|
||||
this.type_at.set(made.name, this.types.length as u32);
|
||||
}
|
||||
this.types.append(made);
|
||||
}
|
||||
}
|
||||
|
||||
# Reads a callback's C: `typedef VkBool32 (VKAPI_PTR *PFN_vkX)(VkFlags a, void* b);`.
|
||||
def registry::read_callback(&mut this, tokens: span<strview>, made: &mut type_record): void {
|
||||
var open: u64 = 0;
|
||||
while open < tokens.length && tokens[open] != "(" {
|
||||
open++;
|
||||
}
|
||||
made.returns = declaration_of(tokens[1:open - 1]);
|
||||
made.first = this.declarations.length as u32;
|
||||
# Past `(VKAPI_PTR *PFN_vkX)`, to the parameters' `(`.
|
||||
var at := open;
|
||||
while at < tokens.length && tokens[at] != ")" {
|
||||
at++;
|
||||
}
|
||||
at += 2;
|
||||
var start := at;
|
||||
while at < tokens.length {
|
||||
val t: strview = tokens[at];
|
||||
if t == "," || t == ")" {
|
||||
val read := declaration_of(tokens[start:at - start]);
|
||||
if read.base.length != 0 && !(read.base == "void" && read.pointers == 0) {
|
||||
this.declarations.append(read);
|
||||
}
|
||||
break if t == ")";
|
||||
start = at + 1;
|
||||
}
|
||||
at++;
|
||||
}
|
||||
made.count = this.declarations.length as u32 - made.first;
|
||||
}
|
||||
|
||||
# Reads a block of `<enums>`: an enum's values, or the API's constants.
|
||||
def registry::read_block(&mut this, block: &xml::element): void {
|
||||
var made := group_record{
|
||||
name = block.attribute("name") else "",
|
||||
kind = block.attribute("type") else "",
|
||||
wide = (block.attribute("bitwidth") else "") == "64",
|
||||
first = this.values.length as u32
|
||||
};
|
||||
if made.kind == "constants" {
|
||||
made.kind = "";
|
||||
}
|
||||
for each in block.elements() {
|
||||
continue if each.name != "enum" || !for_vulkan(&each);
|
||||
val read := value_record{
|
||||
name = each.attribute("name") else "",
|
||||
group = made.name,
|
||||
written = each.attribute("value") else "",
|
||||
bitpos = each.attribute("bitpos") else "",
|
||||
alias = each.attribute("alias") else "",
|
||||
c_type = each.attribute("type") else "",
|
||||
comment = each.attribute("comment") else ""
|
||||
};
|
||||
if read.written.starts_with("-") {
|
||||
made.negative = true;
|
||||
}
|
||||
if !this.value_at.contains(&read.name) {
|
||||
this.value_at.set(read.name, this.values.length as u32);
|
||||
}
|
||||
this.values.append(read);
|
||||
}
|
||||
made.count = this.values.length as u32 - made.first;
|
||||
this.block_at.set(made.name, this.blocks.length as u32);
|
||||
this.blocks.append(made);
|
||||
}
|
||||
|
||||
# Reads each command of `<commands>`.
|
||||
def registry::read_commands(&mut this, commands: &xml::element): void {
|
||||
var tokens := vector::<strview>::new();
|
||||
for each in commands.elements() {
|
||||
continue if each.name != "command" || !for_vulkan(&each);
|
||||
var made := command_record{
|
||||
name = each.attribute("name") else "",
|
||||
alias = each.attribute("alias") else "",
|
||||
exported = has_word(each.attribute("export") else "", "vulkan"),
|
||||
first = this.declarations.length as u32
|
||||
};
|
||||
for part in each.elements() {
|
||||
if part.name == "proto" {
|
||||
tokens.clear();
|
||||
tokens_of(&part, &mut tokens);
|
||||
made.returns = declaration_of(tokens.span);
|
||||
made.name = made.returns.name;
|
||||
made.returns.name = "";
|
||||
} else if part.name == "param" && for_vulkan(&part) {
|
||||
tokens.clear();
|
||||
tokens_of(&part, &mut tokens);
|
||||
var read := declaration_of(tokens.span);
|
||||
read.optional = part.attribute("optional") else "";
|
||||
read.len = part.attribute("len") else "";
|
||||
this.declarations.append(read);
|
||||
}
|
||||
}
|
||||
made.count = this.declarations.length as u32 - made.first;
|
||||
if !this.command_at.contains(&made.name) {
|
||||
this.command_at.set(made.name, this.commands.length as u32);
|
||||
}
|
||||
this.commands.append(made);
|
||||
}
|
||||
}
|
||||
|
||||
# Reads a feature or an extension, and what its `require`s and `remove`s name.
|
||||
def registry::read_set(&mut this, set: &xml::element, feature: bool): void {
|
||||
var made := set_record{
|
||||
name = set.attribute("name") else "",
|
||||
feature = feature,
|
||||
platform = set.attribute("platform") else "",
|
||||
depends = set.attribute("depends") else "",
|
||||
first = this.needs.length as u32
|
||||
};
|
||||
if feature {
|
||||
made.supported = has_word(set.attribute("api") else "", "vulkan");
|
||||
val number := set.attribute("number") else "";
|
||||
val dot := number.find(".") else 0;
|
||||
made.major = number_of(number.substr(0, dot)) as u32;
|
||||
made.minor = number_of(number.substr(dot + 1, number.length - dot - 1)) as u32;
|
||||
} else {
|
||||
made.supported = has_word(set.attribute("supported") else "", "vulkan");
|
||||
made.number = number_of(set.attribute("number") else "") as i64;
|
||||
}
|
||||
for part in set.elements() {
|
||||
continue if (part.name != "require" && part.name != "remove") || !for_vulkan(&part);
|
||||
val depends := part.attribute("depends") else "";
|
||||
val removes := part.name == "remove";
|
||||
for item in part.elements() {
|
||||
continue if !for_vulkan(&item);
|
||||
var named := need{
|
||||
name = item.attribute("name") else "",
|
||||
depends = depends,
|
||||
removes = removes
|
||||
};
|
||||
if item.name == "type" {
|
||||
named.what = need_kind::of_type;
|
||||
} else if item.name == "command" {
|
||||
named.what = need_kind::of_command;
|
||||
} else if item.name == "enum" {
|
||||
if !this.read_value(&item, &made, &mut named) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
this.needs.append(named);
|
||||
}
|
||||
}
|
||||
made.count = this.needs.length as u32 - made.first;
|
||||
this.set_at.set(made.name, this.sets.length as u32);
|
||||
this.sets.append(made);
|
||||
}
|
||||
|
||||
# Reads an `<enum>` a feature or an extension names: a value it gives an
|
||||
# enum, a constant it defines, or one it refers to.
|
||||
def registry::read_value(&mut this, item: &xml::element, set: &set_record, named: &mut need): bool {
|
||||
val extends := item.attribute("extends") else "";
|
||||
var read := value_record{
|
||||
name = named.name,
|
||||
group = extends,
|
||||
written = item.attribute("value") else "",
|
||||
bitpos = item.attribute("bitpos") else "",
|
||||
alias = item.attribute("alias") else "",
|
||||
c_type = item.attribute("type") else "",
|
||||
comment = item.attribute("comment") else ""
|
||||
};
|
||||
if item.attribute("offset") is some(offset) {
|
||||
var number := set.number;
|
||||
if item.attribute("extnumber") is some(given) {
|
||||
number = number_of(given) as i64;
|
||||
}
|
||||
read.offset = 1000000000 + (number - 1) * 1000 + number_of(offset) as i64;
|
||||
if (item.attribute("dir") else "") == "-" {
|
||||
read.offset = -read.offset;
|
||||
}
|
||||
read.computed = true;
|
||||
}
|
||||
val defines := read.written.length != 0 || read.bitpos.length != 0 || read.alias.length != 0 || read.computed;
|
||||
if extends.length != 0 {
|
||||
named.what = need_kind::extends;
|
||||
} else if defines {
|
||||
named.what = need_kind::defines;
|
||||
} else {
|
||||
named.what = need_kind::of_constant;
|
||||
return true;
|
||||
}
|
||||
named.value = this.values.length as u32;
|
||||
if !this.value_at.contains(&read.name) {
|
||||
this.value_at.set(read.name, this.values.length as u32);
|
||||
}
|
||||
this.values.append(read);
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Finds a type by its name.
|
||||
def registry::type_named(&this, name: strview): maybe<&type_record> {
|
||||
if this.type_at.get(&name) is some(&at) {
|
||||
return &this.types[at as u64];
|
||||
}
|
||||
return nothing{};
|
||||
}
|
||||
|
||||
#! The parameters or members a record holds, as a view.
|
||||
def registry::declarations_of(&this, first: u32, count: u32): span<declaration> {
|
||||
val start: u64 = first;
|
||||
val length: u64 = count;
|
||||
return this.declarations.span[start:length];
|
||||
}
|
||||
@@ -0,0 +1,933 @@
|
||||
#! Contains what's written of what's picked of a registry: the names Sakura
|
||||
#! gives C's, the Sakura type of each C declaration, and the files of
|
||||
#! `malunal::vulkan`, each a `made_file`.
|
||||
|
||||
import std::containers;
|
||||
|
||||
#! A file written: its name, within the library's folder, and its text.
|
||||
struct made_file {
|
||||
var name: string;
|
||||
var text: string;
|
||||
}
|
||||
|
||||
# Sakura's own words, which no name may be: a member or a parameter named so is
|
||||
# followed by `_`.
|
||||
const keywords: strview[33] = [
|
||||
"import", "module", "extern", "pub", "const", "var", "val", "def", "mut", "return", "if", "else",
|
||||
"for", "in", "while", "break", "continue", "type", "enum", "struct", "union", "trait", "as", "is",
|
||||
"not", "match", "when", "where", "true", "false", "null", "this", "self",
|
||||
];
|
||||
|
||||
# A type C or a platform names, and Sakura's of it.
|
||||
struct basic_type {
|
||||
var c: strview;
|
||||
var sakura: strview;
|
||||
}
|
||||
|
||||
# C's own types, then those of the platforms bound. A type only ever pointed
|
||||
# to, as Xlib's `Display`, is a byte.
|
||||
const basic_types: basic_type[32] = [
|
||||
basic_type{ "void", "u8" },
|
||||
basic_type{ "char", "u8" },
|
||||
basic_type{ "float", "f32" },
|
||||
basic_type{ "double", "f64" },
|
||||
basic_type{ "int8_t", "i8" },
|
||||
basic_type{ "uint8_t", "u8" },
|
||||
basic_type{ "int16_t", "i16" },
|
||||
basic_type{ "uint16_t", "u16" },
|
||||
basic_type{ "int32_t", "i32" },
|
||||
basic_type{ "uint32_t", "u32" },
|
||||
basic_type{ "int64_t", "i64" },
|
||||
basic_type{ "uint64_t", "u64" },
|
||||
basic_type{ "size_t", "u64" },
|
||||
basic_type{ "int", "i32" },
|
||||
basic_type{ "HINSTANCE", "u64" },
|
||||
basic_type{ "HWND", "u64" },
|
||||
basic_type{ "HMONITOR", "u64" },
|
||||
basic_type{ "HANDLE", "u64" },
|
||||
basic_type{ "DWORD", "u32" },
|
||||
basic_type{ "LPCWSTR", "*u16" },
|
||||
basic_type{ "SECURITY_ATTRIBUTES", "u8" },
|
||||
basic_type{ "Display", "u8" },
|
||||
basic_type{ "VisualID", "u64" },
|
||||
basic_type{ "Window", "u64" },
|
||||
basic_type{ "RROutput", "u64" },
|
||||
basic_type{ "xcb_connection_t", "u8" },
|
||||
basic_type{ "xcb_visualid_t", "u32" },
|
||||
basic_type{ "xcb_window_t", "u32" },
|
||||
basic_type{ "wl_display", "u8" },
|
||||
basic_type{ "wl_surface", "u8" },
|
||||
basic_type{ "IDirectFB", "u8" },
|
||||
basic_type{ "IDirectFBSurface", "u8" },
|
||||
];
|
||||
|
||||
# How many of `basic_types` are C's own, before the platforms'.
|
||||
const c_types: u64 = 14;
|
||||
|
||||
# The prefixes C's names give pointers and functions, longest first.
|
||||
const pointer_prefixes: strview[3] = ["pfn", "pp", "p"];
|
||||
|
||||
# A member of a struct or a union, as written.
|
||||
struct field_line {
|
||||
var name: string;
|
||||
var held: string; # Its type.
|
||||
var tail: string; # Its default, and what's said of it.
|
||||
}
|
||||
|
||||
#! What's written of a registry: the names given, and the constants written.
|
||||
struct writing {
|
||||
var names: dict<strview, u32>; # Each struct's and union's name, by its C name, in `spelled`.
|
||||
var spelled: vector<string>;
|
||||
var taken: dict<u64, u32>; # The names in `spelled`, by their hash, to find two types given one.
|
||||
var written: dict<strview, bool>; # Each constant written, so none is twice.
|
||||
}
|
||||
|
||||
#! Creates a writing of nothing yet.
|
||||
def writing::new(): writing {
|
||||
return writing{
|
||||
names = dict::<strview, u32>::new(),
|
||||
taken = dict::<u64, u32>::new(),
|
||||
written = dict::<strview, bool>::new()
|
||||
};
|
||||
}
|
||||
|
||||
#! Whether a name is one of Sakura's words.
|
||||
def is_keyword(name: strview): bool {
|
||||
for &each in keywords {
|
||||
return true if each == name;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! A C name in snake case, as Sakura names things: a capital begins a word, as
|
||||
#! does a run of capitals before one, `deviceUUID` is `device_uuid`, a number
|
||||
#! stays with the word before, `shaderFloat64` is `shader_float64`, and one
|
||||
#! before a `D` is a word, `maxImageDimension2D` is `max_image_dimension_2d`.
|
||||
def snake_of(name: strview): string {
|
||||
var made := string::new(nothing{});
|
||||
for i in 0..name.length {
|
||||
val c: u8 = name[i];
|
||||
if c == '_' {
|
||||
if made.length != 0 && !made.ends_with("_") {
|
||||
made.push('_');
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if i > 0 && made.length != 0 && !made.ends_with("_") {
|
||||
val before: u8 = name[i - 1];
|
||||
var after: u8 = 0;
|
||||
if i + 1 < name.length {
|
||||
after = name[i + 1];
|
||||
}
|
||||
var cut := false;
|
||||
if c.is_upper {
|
||||
if before.is_lower {
|
||||
cut = true;
|
||||
} else if before.is_digit {
|
||||
cut = c != 'D' || after.is_lower;
|
||||
} else if before.is_upper && after.is_lower {
|
||||
cut = true;
|
||||
}
|
||||
} else if c.is_digit && !before.is_digit {
|
||||
cut = unit_at(name, i);
|
||||
}
|
||||
if cut {
|
||||
made.push('_');
|
||||
}
|
||||
}
|
||||
made.push(c.to_lower);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# Whether a run of digits begins at a place, and a `D` follows it as a word, as
|
||||
# `2D` and `3D` do.
|
||||
def unit_at(name: strview, at: u64): bool {
|
||||
var i := at;
|
||||
while i < name.length && name[i].is_digit {
|
||||
i++;
|
||||
}
|
||||
return i < name.length && name[i] == 'D' && (i + 1 == name.length || !name[i + 1].is_lower);
|
||||
}
|
||||
|
||||
# A C name without the `p`, `pp`, or `pfn` before its first capital.
|
||||
def without_prefix(name: strview): strview {
|
||||
for &prefix in pointer_prefixes {
|
||||
if name.length > prefix.length && name.starts_with(prefix) && name[prefix.length].is_upper {
|
||||
return name.substr(prefix.length, name.length - prefix.length);
|
||||
}
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
# A type's C name without its `Vk`.
|
||||
def without_vk(name: strview): strview {
|
||||
if name.starts_with("Vk") {
|
||||
return name.substr(2, name.length - 2);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
#! A member's or a parameter's name: in snake case, without the prefix of a
|
||||
#! pointer or a function, and followed by `_` where it's a word of Sakura's.
|
||||
def member_name_of(reg: ®istry, d: &declaration): string {
|
||||
var name := d.name;
|
||||
if d.pointers > 0 {
|
||||
name = without_prefix(name);
|
||||
} else if reg.type_named(d.base) is some(t) {
|
||||
if t.category == category::callback {
|
||||
name = without_prefix(name);
|
||||
}
|
||||
}
|
||||
var made := snake_of(name);
|
||||
if is_keyword(made.span) {
|
||||
made.push('_');
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# A number in hex, as `0x7FFFFFFF`.
|
||||
def hex_of(n: u64): string {
|
||||
val digits: strview = "0123456789ABCDEF";
|
||||
var made := string::from("0x", nothing{});
|
||||
var started := false;
|
||||
for j in 0..16u {
|
||||
val shift: u64 = (15 - j) * 4;
|
||||
val digit: u64 = (n >> shift) & 0xF;
|
||||
if digit != 0 || started || j == 15 {
|
||||
made.push(digits[digit]);
|
||||
started = true;
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# Sakura's type of a constant of the API, by the C type it says.
|
||||
def constant_type_of(c_type: strview): strview {
|
||||
if c_type == "uint64_t" {
|
||||
return "u64";
|
||||
} else if c_type == "float" {
|
||||
return "f32";
|
||||
} else if c_type == "int32_t" {
|
||||
return "i32";
|
||||
}
|
||||
return "u32";
|
||||
}
|
||||
|
||||
# Sakura's type of an enum's values.
|
||||
def block_type_of(block: &group_record): strview {
|
||||
if block.kind == "bitmask" {
|
||||
return block.wide ? "u64" : "u32";
|
||||
}
|
||||
return block.negative ? "i32" : "u32";
|
||||
}
|
||||
|
||||
#! Sakura's type of a constant, by its name: an enum's values', the type an
|
||||
#! API's constant says, `strview` of a name, or what it's another name for.
|
||||
def value_type_of(reg: ®istry, name: strview): strview {
|
||||
var at: u32 = 0;
|
||||
if reg.value_at.get(&name) is some(&found) {
|
||||
at = found;
|
||||
} else {
|
||||
return "u32";
|
||||
}
|
||||
val v := ®.values[at as u64];
|
||||
if v.group.length == 0 {
|
||||
if v.alias.length != 0 {
|
||||
return value_type_of(reg, v.alias);
|
||||
}
|
||||
return v.written.starts_with("\"") ? "strview" : "u32";
|
||||
}
|
||||
if reg.block_at.get(&v.group) is some(&block) {
|
||||
val held := ®.blocks[block as u64];
|
||||
return held.kind.length == 0 ? constant_type_of(v.c_type) : block_type_of(held);
|
||||
}
|
||||
return "u32";
|
||||
}
|
||||
|
||||
#! A value as Sakura writes it, of its type.
|
||||
def value_text_of(v: &value_record, held: strview): string {
|
||||
if v.alias.length != 0 {
|
||||
return string::from(v.alias, nothing{});
|
||||
}
|
||||
if v.computed {
|
||||
return $"{v.offset}";
|
||||
}
|
||||
if v.bitpos.length != 0 {
|
||||
return hex_of((1 as u64) << number_of(v.bitpos));
|
||||
}
|
||||
val written := v.written;
|
||||
# `(~0U)` and `(~0ULL)`: all bits but those of a number.
|
||||
if written.starts_with("(~") {
|
||||
val n := number_of(written.substr(2, written.length - 2));
|
||||
return hex_of(held == "u64" ? ~n : (~n) & 0xFFFFFFFF);
|
||||
}
|
||||
if held == "f32" && (written.ends_with("F") || written.ends_with("f")) {
|
||||
return string::from(written.substr(0, written.length - 1), nothing{});
|
||||
}
|
||||
return string::from(written, nothing{});
|
||||
}
|
||||
|
||||
#! Gives each struct and union bound its name. One with an `sType` of its own is
|
||||
#! named by it, as Vulkan's names are: `VkPhysicalDeviceVulkan13Features` by
|
||||
#! `VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES`, as
|
||||
#! `physical_device_vulkan_1_3_features`. Another is its C name in snake case,
|
||||
#! without its `Vk`, and an alias that only adds its vendor's letters is its
|
||||
#! type's name and theirs, as `VkPhysicalDeviceFeatures2KHR` is
|
||||
#! `physical_device_features_2_khr`.
|
||||
def writing::name_types(&mut this, reg: ®istry, picks: &picking, problems: &mut vector<string>): void {
|
||||
for pass in 0..2u {
|
||||
for &t in reg.types {
|
||||
continue if t.category != category::structure && t.category != category::joined;
|
||||
continue if (t.alias.length != 0) != (pass == 1) || picks.place_of(t.name) is none;
|
||||
var made := string::new(nothing{});
|
||||
if t.alias.length == 0 {
|
||||
made = this.struct_name_of(reg, &t);
|
||||
} else if this.names.get(&t.alias) is some(&target) {
|
||||
val rest := t.name.substr(t.alias.length, t.name.length - t.alias.length);
|
||||
if t.name.starts_with(t.alias) && rest.length != 0 && all_capitals(rest) {
|
||||
made = string::from(this.spelled[target as u64].span, nothing{});
|
||||
made.push('_');
|
||||
made.append(rest.lower().span);
|
||||
} else {
|
||||
made = snake_of(without_vk(t.name));
|
||||
}
|
||||
} else {
|
||||
made = snake_of(without_vk(t.name));
|
||||
}
|
||||
val hash := made.hash();
|
||||
if this.taken.get(&hash) is some(&other) {
|
||||
if this.spelled[other as u64].span == made.span {
|
||||
problems.append($"{t.name} would be named {made}, as another type is.");
|
||||
}
|
||||
}
|
||||
val at := this.spelled.length as u32;
|
||||
this.taken.set(hash, at);
|
||||
this.names.set(t.name, at);
|
||||
this.spelled.append(move(made));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Whether a name is all capitals, as a vendor's are.
|
||||
def all_capitals(name: strview): bool {
|
||||
for i in 0..name.length {
|
||||
return false if !name[i].is_upper;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
# A struct's name: its `sType`'s, or its C name's.
|
||||
def writing::struct_name_of(&this, reg: ®istry, t: &type_record): string {
|
||||
for &d in reg.declarations_of(t.first, t.count) {
|
||||
if d.name == "sType" && d.values.starts_with("VK_STRUCTURE_TYPE_") && d.values.find(",") is none {
|
||||
return d.values.substr(18, d.values.length - 18).lower();
|
||||
}
|
||||
}
|
||||
return snake_of(without_vk(t.name));
|
||||
}
|
||||
|
||||
#! The name a struct or a union is given.
|
||||
def writing::name_of(&this, c: strview): strview {
|
||||
if this.names.get(&c) is some(&at) {
|
||||
return this.spelled[at as u64].span;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
#! Sakura's type of a type C names.
|
||||
def writing::base_of(&this, reg: ®istry, c: strview, problems: &mut vector<string>): string {
|
||||
for &each in basic_types {
|
||||
return string::from(each.sakura, nothing{}) if each.c == c;
|
||||
}
|
||||
val t := reg.type_named(c) else {
|
||||
problems.append($"There's no type {c}.");
|
||||
return string::from("u8", nothing{});
|
||||
};
|
||||
if t.category == category::structure || t.category == category::joined {
|
||||
if t.alias.length != 0 && !this.names.contains(&c) {
|
||||
return this.base_of(reg, t.alias, problems);
|
||||
}
|
||||
return string::from(this.name_of(c), nothing{});
|
||||
}
|
||||
if t.category == category::callback {
|
||||
return string::from(c, nothing{});
|
||||
}
|
||||
if t.category == category::handle {
|
||||
return string::from("u64", nothing{});
|
||||
}
|
||||
if t.alias.length != 0 {
|
||||
return this.base_of(reg, t.alias, problems);
|
||||
}
|
||||
if t.category == category::bitmask {
|
||||
return string::from(t.wide ? "u64" : "u32", nothing{});
|
||||
}
|
||||
if t.category == category::enumerated {
|
||||
if reg.block_at.get(&c) is some(&block) {
|
||||
return string::from(block_type_of(®.blocks[block as u64]), nothing{});
|
||||
}
|
||||
return string::from("u32", nothing{});
|
||||
}
|
||||
if t.category == category::base {
|
||||
return t.requires.length != 0 ? this.base_of(reg, t.requires, problems) : string::from("u8", nothing{});
|
||||
}
|
||||
problems.append($"{c} isn't a type that's bound.");
|
||||
return string::from("u8", nothing{});
|
||||
}
|
||||
|
||||
# Whether a parameter is a reference, as `&T` or `&mut T`: a pointer to one
|
||||
# thing that's there, not to an array, nor to nothing, nor to bytes.
|
||||
def writing::takes_reference(&this, reg: ®istry, d: &declaration, problems: &mut vector<string>): bool {
|
||||
if d.pointers == 0 || d.len.length != 0 || d.optional.starts_with("true") {
|
||||
return false;
|
||||
}
|
||||
if d.pointers == 1 {
|
||||
val base := this.base_of(reg, d.base, problems);
|
||||
return base.span != "u8";
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Sakura's type of a declaration: of a member, or of a function's parameter,
|
||||
#! whose pointer may be a reference and whose array is one.
|
||||
def writing::type_of(&mut this, reg: ®istry, d: &declaration, parameter: bool, problems: &mut vector<string>): string {
|
||||
var made := string::new(nothing{});
|
||||
if parameter && d.lengths > 0 {
|
||||
made.append((d.consts & 1) != 0 ? "&" : "&mut ");
|
||||
}
|
||||
val reference := parameter && this.takes_reference(reg, d, problems);
|
||||
var k := d.pointers;
|
||||
while k > 0 {
|
||||
val held := ((d.consts >> (k - 1)) & 1) != 0;
|
||||
if reference && k == d.pointers {
|
||||
made.append(held ? "&" : "&mut ");
|
||||
} else {
|
||||
made.append(held ? "*" : "*mut ");
|
||||
}
|
||||
k--;
|
||||
}
|
||||
made.append(this.base_of(reg, d.base, problems).span);
|
||||
# Sakura writes an array of arrays innermost first, C outermost first, so C's
|
||||
# `float matrix[3][4]`, 3 of 4, is `f32[4][3]`, indexed alike.
|
||||
var i: u64 = d.lengths;
|
||||
while i > 0 {
|
||||
i--;
|
||||
made.push('[');
|
||||
made.append(size_of_array(reg, d.sizes[i]).span);
|
||||
made.push(']');
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# An array's length, as a number, of a number or a constant's name.
|
||||
def size_of_array(reg: ®istry, size: strview): string {
|
||||
if size.length != 0 && size[0].is_digit {
|
||||
return string::from(size, nothing{});
|
||||
}
|
||||
if reg.value_at.get(&size) is some(&at) {
|
||||
return string::from(reg.values[at as u64].written, nothing{});
|
||||
}
|
||||
return string::from(size, nothing{});
|
||||
}
|
||||
|
||||
# What's said of a member: the C type it's of where Sakura's says less, as
|
||||
# `VkFormat` of a `u32`, and what the registry says of it.
|
||||
def doc_of(reg: ®istry, d: &declaration): string {
|
||||
var made := string::new(nothing{});
|
||||
var said := false;
|
||||
if reg.type_named(d.base) is some(t) {
|
||||
said = t.category == category::handle || t.category == category::enumerated
|
||||
|| t.category == category::bitmask || (t.category == category::base && t.requires.length != 0);
|
||||
if t.category == category::basic {
|
||||
said = true;
|
||||
for i in 0..c_types {
|
||||
if basic_types[i].c == d.base {
|
||||
said = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if said {
|
||||
made.append(d.base);
|
||||
}
|
||||
val about := said_of(d.comment);
|
||||
if about.length != 0 {
|
||||
made.append(said ? ": " : "");
|
||||
made.append(about);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# What the registry says of something, without the marks of a C comment or
|
||||
# its last full stop.
|
||||
def said_of(comment: strview): strview {
|
||||
var made := comment.trim();
|
||||
if made.starts_with("//") {
|
||||
made = made.substr(2, made.length - 2).trim();
|
||||
}
|
||||
while made.ends_with(".") {
|
||||
made = made.substr(0, made.length - 1);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# Whether a value is written: given by a set bound, or one of an enum bound.
|
||||
def bound_value(reg: ®istry, picks: &picking, name: strview): bool {
|
||||
if picks.given.contains(&name) {
|
||||
return true;
|
||||
}
|
||||
if reg.value_at.get(&name) is some(&at) {
|
||||
return picks.place_of(reg.values[at as u64].group) is some(_);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
# Writes a struct or a union.
|
||||
def writing::write_struct(&mut this, reg: ®istry, picks: &picking, t: &type_record, into: &mut string, problems: &mut vector<string>): void {
|
||||
var lines := vector::<field_line>::new();
|
||||
val members := reg.declarations_of(t.first, t.count);
|
||||
var i: u64 = 0;
|
||||
while i < members.length {
|
||||
val d := &members[i];
|
||||
var line := field_line{
|
||||
name = member_name_of(reg, d),
|
||||
held = this.type_of(reg, d, false, problems),
|
||||
tail = string::new(nothing{})
|
||||
};
|
||||
# A run of bitfields is one field of their type, named for them all.
|
||||
if d.bits != 0 {
|
||||
var bits: u32 = 0;
|
||||
var said := string::from("Bits, the lowest first: ", nothing{});
|
||||
while i < members.length && members[i].bits != 0 && bits < 32 {
|
||||
if bits != 0 {
|
||||
line.name.append("_and_");
|
||||
line.name.append(member_name_of(reg, &members[i]).span);
|
||||
said.append(", ");
|
||||
}
|
||||
said.append($"{members[i].name}, {members[i].bits}");
|
||||
bits += members[i].bits;
|
||||
i++;
|
||||
}
|
||||
line.tail.append($"; #! {said}.");
|
||||
lines.append(move(line));
|
||||
continue;
|
||||
}
|
||||
for &other in lines {
|
||||
if other.name.span == line.name.span {
|
||||
line.name = snake_of(d.name);
|
||||
}
|
||||
}
|
||||
if d.values.length != 0 && d.values.find(",") is none && bound_value(reg, picks, d.values) {
|
||||
line.tail.append(" = ");
|
||||
line.tail.append(d.values);
|
||||
}
|
||||
line.tail.push(';');
|
||||
val said := doc_of(reg, d);
|
||||
if said.length != 0 {
|
||||
line.tail.append(" #! ");
|
||||
line.tail.append(said.span);
|
||||
}
|
||||
lines.append(move(line));
|
||||
i++;
|
||||
}
|
||||
|
||||
into.append($"\n#! `{t.name}`");
|
||||
val about := said_of(t.comment);
|
||||
if about.length != 0 {
|
||||
into.append($": {about}");
|
||||
}
|
||||
into.append(".\n");
|
||||
into.append(t.category == category::structure ? "pub struct " : "pub union ");
|
||||
into.append(this.name_of(t.name));
|
||||
into.append(" {\n");
|
||||
var width: u64 = 0;
|
||||
for &line in lines {
|
||||
width = line.name.length > width ? line.name.length : width;
|
||||
}
|
||||
for &line in lines {
|
||||
into.append(" pub var ");
|
||||
into.append(line.name.span);
|
||||
into.push(':');
|
||||
for _ in line.name.length..(width + 1) {
|
||||
into.push(' ');
|
||||
}
|
||||
into.append(line.held.span);
|
||||
into.append(line.tail.span);
|
||||
into.push('\n');
|
||||
}
|
||||
into.append("}\n");
|
||||
}
|
||||
|
||||
# A function's type, as `cfn<def(&T, *u8): i32>`, of its parameters and what it
|
||||
# returns.
|
||||
def writing::function_type_of(&mut this, reg: ®istry, params: span<declaration>, returns: &declaration, references: bool, problems: &mut vector<string>): string {
|
||||
var made := string::from("cfn<def(", nothing{});
|
||||
for i, &d in params {
|
||||
if i != 0 {
|
||||
made.append(", ");
|
||||
}
|
||||
made.append(this.type_of(reg, &d, references, problems).span);
|
||||
}
|
||||
made.append("): ");
|
||||
made.append(this.returned_of(reg, returns, problems).span);
|
||||
made.push('>');
|
||||
return made;
|
||||
}
|
||||
|
||||
# What a function returns, as Sakura writes it.
|
||||
def writing::returned_of(&mut this, reg: ®istry, returns: &declaration, problems: &mut vector<string>): string {
|
||||
if returns.base == "void" && returns.pointers == 0 {
|
||||
return string::from("void", nothing{});
|
||||
}
|
||||
return this.type_of(reg, returns, false, problems);
|
||||
}
|
||||
|
||||
#! Writes the types of a place: its structs and unions, its aliases of them,
|
||||
#! and the functions C calls back.
|
||||
def writing::types_of(&mut this, reg: ®istry, picks: &picking, place: u32, into: &mut string, problems: &mut vector<string>): void {
|
||||
for &t in reg.types {
|
||||
continue if !picks.is_at(t.name, place);
|
||||
if t.category == category::structure || t.category == category::joined {
|
||||
if t.alias.length == 0 {
|
||||
this.write_struct(reg, picks, &t, into, problems);
|
||||
} else if this.name_of(t.name) != this.name_of(t.alias) {
|
||||
into.append($"\n#! `{t.name}`, another name for `{t.alias}`.\npub type {this.name_of(t.name)} = {this.name_of(t.alias)};\n");
|
||||
}
|
||||
} else if t.category == category::callback {
|
||||
if t.alias.length != 0 {
|
||||
into.append($"\n#! `{t.name}`, another name for `{t.alias}`.\npub type {t.name} = {t.alias};\n");
|
||||
continue;
|
||||
}
|
||||
val params := reg.declarations_of(t.first, t.count);
|
||||
into.append($"\n#! `{t.name}`, called with");
|
||||
if params.length == 0 {
|
||||
into.append(" nothing");
|
||||
}
|
||||
for i, &d in params {
|
||||
into.append(i == 0 ? " " : ", ");
|
||||
into.append($"`{d.name}`");
|
||||
}
|
||||
into.append($".\npub type {t.name} = {this.function_type_of(reg, params, &t.returns, false, problems)};\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Writes the commands of a place: each the loader exports as C's function, and
|
||||
#! each one's type, as `vkGetInstanceProcAddr` and `vkGetDeviceProcAddr` give it.
|
||||
#! @returns Whether it wrote a function of C's.
|
||||
def writing::commands_of(&mut this, reg: ®istry, picks: &picking, place: u32, into: &mut string, problems: &mut vector<string>): bool {
|
||||
var linked := false;
|
||||
for &c in reg.commands {
|
||||
continue if c.alias.length != 0 || !picks.is_at(c.name, place) || !picks.exported.contains(&c.name);
|
||||
if !linked {
|
||||
into.append("\n# The functions the loader exports.\n");
|
||||
linked = true;
|
||||
}
|
||||
into.append($"pub def {c.name}(");
|
||||
var names := vector::<string>::new();
|
||||
for i, &d in reg.declarations_of(c.first, c.count) {
|
||||
if i != 0 {
|
||||
into.append(", ");
|
||||
}
|
||||
var name := member_name_of(reg, &d);
|
||||
for &other in names {
|
||||
if other.span == name.span {
|
||||
name = snake_of(d.name);
|
||||
}
|
||||
}
|
||||
into.append($"{name}: {this.type_of(reg, &d, true, problems)}");
|
||||
names.append(move(name));
|
||||
}
|
||||
into.append($"): {this.returned_of(reg, &c.returns, problems)};\n");
|
||||
}
|
||||
|
||||
var typed := false;
|
||||
for &c in reg.commands {
|
||||
continue if !picks.is_at(c.name, place);
|
||||
if !typed {
|
||||
into.append("\n# Each command's type, as `vkGetInstanceProcAddr` and `vkGetDeviceProcAddr` give\n# it, to be cast to.\n");
|
||||
typed = true;
|
||||
}
|
||||
if c.alias.length != 0 {
|
||||
into.append($"pub type PFN_{c.name} = PFN_{c.alias};\n");
|
||||
} else {
|
||||
val params := reg.declarations_of(c.first, c.count);
|
||||
into.append($"pub type PFN_{c.name} = {this.function_type_of(reg, params, &c.returns, true, problems)};\n");
|
||||
}
|
||||
}
|
||||
return linked;
|
||||
}
|
||||
|
||||
# Writes a constant, once.
|
||||
def writing::write_value(&mut this, v: &value_record, held: strview, into: &mut string): void {
|
||||
if this.written.contains(&v.name) {
|
||||
return;
|
||||
}
|
||||
this.written.set(v.name, true);
|
||||
into.append($"pub const {v.name}: {held} = {value_text_of(v, held)};");
|
||||
if v.comment.length != 0 {
|
||||
into.append($" #! {v.comment}");
|
||||
}
|
||||
into.push('\n');
|
||||
}
|
||||
|
||||
#! Writes the constants of a place: the API's, those features and extensions
|
||||
#! define, and each enum's values, under the enum's name.
|
||||
def writing::constants_of(&mut this, reg: ®istry, picks: &picking, place: u32, into: &mut string): void {
|
||||
for &block in reg.blocks {
|
||||
continue if block.kind.length != 0;
|
||||
var heading := false;
|
||||
for i in block.first..(block.first + block.count) {
|
||||
val v := ®.values[i as u64];
|
||||
continue if !picks.is_at(v.name, place);
|
||||
if !heading {
|
||||
into.append($"\n# {block.name}\n");
|
||||
heading = true;
|
||||
}
|
||||
this.write_value(v, constant_type_of(v.c_type), into);
|
||||
}
|
||||
}
|
||||
|
||||
var definer: u32 = 0xFFFFFFFF;
|
||||
for k in 0..picks.defined.length {
|
||||
val v := ®.values[picks.defined[k] as u64];
|
||||
continue if !picks.is_at(v.name, place);
|
||||
if picks.definers[k] != definer {
|
||||
definer = picks.definers[k];
|
||||
into.append($"\n# {reg.sets[definer as u64].name}\n");
|
||||
}
|
||||
this.write_value(v, value_type_of(reg, v.name), into);
|
||||
}
|
||||
|
||||
for &block in reg.blocks {
|
||||
continue if block.kind.length == 0;
|
||||
val held := block_type_of(&block);
|
||||
var heading := false;
|
||||
if picks.is_at(block.name, place) {
|
||||
for i in block.first..(block.first + block.count) {
|
||||
val v := ®.values[i as u64];
|
||||
continue if picks.removed.contains(&v.name) || this.written.contains(&v.name);
|
||||
if !heading {
|
||||
into.append($"\n# {block.name}\n");
|
||||
heading = true;
|
||||
}
|
||||
this.write_value(v, held, into);
|
||||
}
|
||||
}
|
||||
for &at in picks.extended {
|
||||
val v := ®.values[at as u64];
|
||||
continue if v.group != block.name || !picks.is_at(v.name, place) || this.written.contains(&v.name);
|
||||
if !heading {
|
||||
into.append($"\n# {block.name}\n");
|
||||
heading = true;
|
||||
}
|
||||
this.write_value(v, held, into);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# The note every file ends its first comment with.
|
||||
def made_note(version: strview): string {
|
||||
return $"#!\n#! Written by `malunal.vulkan.bindings` from the registry of {version}: run\n#! it again, rather than editing this.\n";
|
||||
}
|
||||
|
||||
# The libraries each system's loader is.
|
||||
const loaders: strview = "extern import \"vulkan-1.dll\" when os == windows;\nextern import \"libvulkan.so.1\" when os == linux;\n";
|
||||
|
||||
# The section every system builds.
|
||||
const every_system: strview = "extern module malunal::vulkan when os == windows || os == linux;\n";
|
||||
|
||||
# The newest version of Vulkan's core the registry has, among its sets.
|
||||
def newest_feature_of(reg: ®istry): u32 {
|
||||
var newest: u32 = 0;
|
||||
for i, &each in reg.sets {
|
||||
continue if !each.feature || !each.supported;
|
||||
val held := ®.sets[newest as u64];
|
||||
if !held.feature || each.major > held.major || (each.major == held.major && each.minor > held.minor) {
|
||||
newest = i as u32;
|
||||
}
|
||||
}
|
||||
return newest;
|
||||
}
|
||||
|
||||
# That version, as `1.4`.
|
||||
def newest_of(reg: ®istry): string {
|
||||
val set := ®.sets[newest_feature_of(reg) as u64];
|
||||
return $"{set.major}.{set.minor}";
|
||||
}
|
||||
|
||||
# The file of the module's own: what it is, the versions of Vulkan, and how a
|
||||
# version is written.
|
||||
def writing::overview_of(&mut this, reg: ®istry, picks: &picking, version: strview): string {
|
||||
val newest := ®.sets[newest_feature_of(reg) as u64];
|
||||
val newest_major := newest.major;
|
||||
val newest_minor := newest.minor;
|
||||
var made := string::from("""
|
||||
#! Contains `malunal::vulkan`: Vulkan, as its registry declares it, for Sakura.
|
||||
#! It holds the functions of Vulkan's core and of the extensions bound, the
|
||||
#! structs and unions they take, laid out as C lays them out, and the numbers
|
||||
#! they're asked for and give, with the versions of Vulkan, and how a version
|
||||
#! is written.
|
||||
#!
|
||||
#! What C names keeps its name: each function, as `vkCreateInstance`; each
|
||||
#! constant, as `VK_FORMAT_R8G8B8A8_UNORM`; and each function's type, as
|
||||
#! `PFN_vkCreateInstance`. A struct or a union with a `VK_STRUCTURE_TYPE_` of
|
||||
#! its own is named by it, as Vulkan's names are, in lower case:
|
||||
#! `VkPhysicalDeviceVulkan13Features` is `physical_device_vulkan_1_3_features`.
|
||||
#! Another is named in snake case without its `Vk`: `VkExtent2D` is
|
||||
#! `extent_2d`. A member is in snake case, without the `p` or `pp` of a pointer
|
||||
#! or the `pfn` of a function, `ppEnabledLayerNames` as `enabled_layer_names`,
|
||||
#! and followed by `_` where it'd be a word of Sakura's, as `module_`. A
|
||||
#! struct's `s_type` is its own by default.
|
||||
#!
|
||||
#! Handles are `u64`s. An enum is its values' type, `u32`, or `i32` for
|
||||
#! `VkResult`, flags are `u32`, or `u64` where they're of 64 bits, and
|
||||
#! `VkBool32` is a `u32`: a member of one says which C type it is. A function's
|
||||
#! parameter that points to one thing that must be there is a reference, `&T`
|
||||
#! where it's read, and `&mut T` where it's written; one that may be null, or
|
||||
#! points to an array or to bytes, is a pointer, `*T` or `*mut T`.
|
||||
#!
|
||||
#! The functions the loader exports, those of the core and of surfaces and
|
||||
#! swapchains, are C's, and found in it. Every command has its type, as
|
||||
#! `PFN_vkCreateDebugUtilsMessengerEXT`, to call what `vkGetInstanceProcAddr`
|
||||
#! and `vkGetDeviceProcAddr` give: an extension's that the loader doesn't
|
||||
#! export are called only so.
|
||||
""", nothing{});
|
||||
made.append(made_note(version).span);
|
||||
made.append("\n");
|
||||
made.append(every_system);
|
||||
made.append($"\n#! The registry's own version, `VK_HEADER_VERSION`, as Vulkan {newest_major}.{newest_minor}.{reg.header} is.\n");
|
||||
made.append($"pub const VK_HEADER_VERSION: u32 = {reg.header};\n");
|
||||
made.append("#! The registry's whole version, as `VK_MAKE_API_VERSION` writes it.\n");
|
||||
made.append($"pub const VK_HEADER_VERSION_COMPLETE: u32 = ({newest_major} << 22) | ({newest_minor} << 12) | {reg.header};\n");
|
||||
made.append("\n# Each version of Vulkan's core bound, as `VkApplicationInfo::apiVersion` names it.\n");
|
||||
for &at in picks.order {
|
||||
val set := ®.sets[at as u64];
|
||||
continue if !set.feature;
|
||||
made.append($"pub const VK_API_VERSION_{set.major}_{set.minor}: u32 = ({set.major} << 22) | ({set.minor} << 12);\n");
|
||||
}
|
||||
made.append("""
|
||||
|
||||
#! No handle at all.
|
||||
pub const VK_NULL_HANDLE: u64 = 0;
|
||||
|
||||
#! A version, as Vulkan writes it: its variant, 0 for Vulkan's, in 3 bits, then
|
||||
#! its major version in 7, its minor version in 10, and its patch in 12.
|
||||
pub def VK_MAKE_API_VERSION(variant: u32, major: u32, minor: u32, patch: u32): u32 {
|
||||
return (variant << 29) | (major << 22) | (minor << 12) | patch;
|
||||
}
|
||||
|
||||
#! A version's variant.
|
||||
pub def VK_API_VERSION_VARIANT(version: u32): u32 {
|
||||
return version >> 29;
|
||||
}
|
||||
|
||||
#! A version's major version.
|
||||
pub def VK_API_VERSION_MAJOR(version: u32): u32 {
|
||||
return (version >> 22) & 0x7F;
|
||||
}
|
||||
|
||||
#! A version's minor version.
|
||||
pub def VK_API_VERSION_MINOR(version: u32): u32 {
|
||||
return (version >> 12) & 0x3FF;
|
||||
}
|
||||
|
||||
#! A version's patch.
|
||||
pub def VK_API_VERSION_PATCH(version: u32): u32 {
|
||||
return version & 0xFFF;
|
||||
}
|
||||
""");
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Reads a registry, and writes the files of what's wanted of it.
|
||||
#! @param text The registry's text, of `vk.xml`.
|
||||
#! @param wanted What's bound.
|
||||
#! @param into Where the files go, each named within the library's folder.
|
||||
#! @param problems Where why they couldn't be written is said.
|
||||
#! @returns Whether they were.
|
||||
def bindings_of(text: strview, wanted: &wanting, into: &mut vector<made_file>, problems: &mut vector<string>): bool {
|
||||
var reg := registry::new();
|
||||
if !reg.read(text, problems) {
|
||||
return false;
|
||||
}
|
||||
var picks := picking::new();
|
||||
if !picks.pick(®, wanted, problems) {
|
||||
return false;
|
||||
}
|
||||
var w := writing::new();
|
||||
w.name_types(®, &picks, problems);
|
||||
var version := $"Vulkan {newest_of(®)}.{reg.header}";
|
||||
if picks.major != reg.sets[newest_feature_of(®) as u64].major || picks.minor != reg.sets[newest_feature_of(®) as u64].minor {
|
||||
version.append($", to Vulkan {picks.major}.{picks.minor}");
|
||||
}
|
||||
|
||||
into.append(made_file{ name = string::from("vulkan.sakura", nothing{}), text = w.overview_of(®, &picks, version.span) });
|
||||
|
||||
var constants := string::from("""
|
||||
#! Contains the constants of `malunal::vulkan`: the API's, those its features
|
||||
#! and extensions define, and each enum's values, under the enum's name.
|
||||
""", nothing{});
|
||||
constants.append(made_note(version.span).span);
|
||||
constants.append("\n");
|
||||
constants.append(every_system);
|
||||
w.constants_of(®, &picks, 0, &mut constants);
|
||||
into.append(made_file{ name = string::from("constants.sakura", nothing{}), text = move(constants) });
|
||||
|
||||
var types := string::from("""
|
||||
#! Contains the types of `malunal::vulkan`: its structs and unions, laid out as
|
||||
#! C lays them out, each with the C name it has in the registry, and the
|
||||
#! functions C calls back.
|
||||
""", nothing{});
|
||||
types.append(made_note(version.span).span);
|
||||
types.append("\n");
|
||||
types.append(every_system);
|
||||
w.types_of(®, &picks, 0, &mut types, problems);
|
||||
into.append(made_file{ name = string::from("types.sakura", nothing{}), text = move(types) });
|
||||
|
||||
var commands := string::from("""
|
||||
#! Contains the commands of `malunal::vulkan`: the functions the loader exports,
|
||||
#! as C's, and each command's type.
|
||||
""", nothing{});
|
||||
commands.append(made_note(version.span).span);
|
||||
commands.append("\n");
|
||||
commands.append(loaders);
|
||||
commands.append("\n");
|
||||
commands.append(every_system);
|
||||
w.commands_of(®, &picks, 0, &mut commands, problems);
|
||||
into.append(made_file{ name = string::from("commands.sakura", nothing{}), text = move(commands) });
|
||||
|
||||
# Each platform's file, of what only its extensions need.
|
||||
for i in 0..6u {
|
||||
val place := i as u32 + 1;
|
||||
val platform := platform_files[i];
|
||||
var named := string::new(nothing{});
|
||||
for &at in picks.order {
|
||||
val set := ®.sets[at as u64];
|
||||
if set.platform == platform.platform {
|
||||
named.append(named.length == 0 ? "`" : ", `");
|
||||
named.append(set.name);
|
||||
named.append("`");
|
||||
}
|
||||
}
|
||||
continue if named.length == 0;
|
||||
var body := string::new(nothing{});
|
||||
w.constants_of(®, &picks, place, &mut body);
|
||||
w.types_of(®, &picks, place, &mut body, problems);
|
||||
var calls := string::new(nothing{});
|
||||
val linked := w.commands_of(®, &picks, place, &mut calls, problems);
|
||||
body.append(calls.span);
|
||||
|
||||
val system := platform.os == "windows" ? "Windows" : "Linux";
|
||||
var made := string::from($"#! Contains the part of `malunal::vulkan` only {system} has, of {platform.platform}'s\n#! extensions: {named}.\n", nothing{});
|
||||
made.append(made_note(version.span).span);
|
||||
made.append("\n");
|
||||
if linked {
|
||||
made.append(platform.os == "windows" ? "extern import \"vulkan-1.dll\" when os == windows;\n\n" : "extern import \"libvulkan.so.1\" when os == linux;\n\n");
|
||||
}
|
||||
made.append($"extern module malunal::vulkan when os == {platform.os};\n");
|
||||
made.append(body.span);
|
||||
into.append(made_file{ name = $"{platform.platform}.sakura", text = move(made) });
|
||||
}
|
||||
return problems.length == 0;
|
||||
}
|
||||
Reference in new issue
Block a user