Files
vulkan/tools/registry.sakura
T
2026-11-11 12:59:00 -06:00

562 lines
20 KiB
Plaintext

#! 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];
}