Files
vulkan/tools/writing.sakura
T
2026-11-14 15:38:59 -06:00

933 lines
33 KiB
Plaintext

#! 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: &registry, 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: &registry, name: strview): strview {
var at: u32 = 0;
if reg.value_at.get(&name) is some(&found) {
at = found;
} else {
return "u32";
}
val v := &reg.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 := &reg.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: &registry, 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: &registry, 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: &registry, 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(&reg.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: &registry, 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: &registry, 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: &registry, 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: &registry, 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: &registry, 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: &registry, 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: &registry, 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: &registry, 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: &registry, 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: &registry, 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: &registry, 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 := &reg.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 := &reg.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 := &reg.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 := &reg.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: &registry): u32 {
var newest: u32 = 0;
for i, &each in reg.sets {
continue if !each.feature || !each.supported;
val held := &reg.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: &registry): string {
val set := &reg.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: &registry, picks: &picking, version: strview): string {
val newest := &reg.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 := &reg.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(&reg, wanted, problems) {
return false;
}
var w := writing::new();
w.name_types(&reg, &picks, problems);
var version := $"Vulkan {newest_of(&reg)}.{reg.header}";
if picks.major != reg.sets[newest_feature_of(&reg) as u64].major || picks.minor != reg.sets[newest_feature_of(&reg) 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(&reg, &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(&reg, &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(&reg, &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(&reg, &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 := &reg.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(&reg, &picks, place, &mut body);
w.types_of(&reg, &picks, place, &mut body, problems);
var calls := string::new(nothing{});
val linked := w.commands_of(&reg, &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;
}