Files
vulkan/tools/picking.sakura
T

316 lines
11 KiB
Plaintext
Raw Permalink Normal View History

2026-11-11 12:59:00 -06:00
#! 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: &registry, 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 := &reg.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 := &reg.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 := &reg.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 := &reg.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: &registry, 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: &registry, 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 := &reg.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: &registry, 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);
}