Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-11 12:59:00 -06:00
commit 2ca5636fc3
20 files changed
+8665

No files matched your search

+327
View File
@@ -0,0 +1,327 @@
#! Contains the tests of what `malunal.vulkan.bindings` writes, of a small
#! registry of their own, written as `vk.xml` is: the names it gives, the types
#! of members and parameters, constants' values, the functions the loader
#! exports and the types of the rest, a platform's file, the versions and
#! extensions bound, and what it refuses.
import std::containers;
import std::testing;
# A registry of a few of each: Vulkan SC's parts, which are left out, among
# them.
const small_registry: strview = """
<registry>
<types>
<type requires="vk_platform" name="void"/>
<type requires="vk_platform" name="char"/>
<type requires="vk_platform" name="float"/>
<type requires="vk_platform" name="uint32_t"/>
<type requires="vk_platform" name="uint64_t"/>
<type requires="windows.h" name="HWND"/>
<type api="vulkan" category="define">// Version of this file
#define <name>VK_HEADER_VERSION</name> 7</type>
<type api="vulkansc" category="define">// Version of this file
#define <name>VK_HEADER_VERSION</name> 3</type>
<type category="basetype">typedef <type>uint32_t</type> <name>VkFlags</name>;</type>
<type category="basetype">typedef <type>uint64_t</type> <name>VkFlags64</name>;</type>
<type category="basetype">typedef <type>uint32_t</type> <name>VkBool32</name>;</type>
<type category="handle"><type>VK_DEFINE_HANDLE</type>(<name>VkInstance</name>)</type>
<type category="handle"><type>VK_DEFINE_NON_DISPATCHABLE_HANDLE</type>(<name>VkThing</name>)</type>
<type name="VkStructureType" category="enum"/>
<type name="VkResult" category="enum"/>
<type name="VkStageFlagBits2" category="enum"/>
<type category="bitmask" bitvalues="VkStageFlagBits2">typedef <type>VkFlags64</type> <name>VkStageFlags2</name>;</type>
<type category="funcpointer">typedef void (VKAPI_PTR *<name>PFN_vkVoidFunction</name>)(void);</type>
<type category="funcpointer">typedef VkBool32 (VKAPI_PTR *<name>PFN_vkHeardCallback</name>)(
<type>uint32_t</type> severity,
const <type>char</type>* pMessage,
<type>void</type>* pUserData);</type>
<type category="struct" name="VkThingCreateInfo">
<member values="VK_STRUCTURE_TYPE_THING_CREATE_INFO"><type>VkStructureType</type> <name>sType</name></member>
<member optional="true">const <type>void</type>* <name>pNext</name></member>
<member><type>uint32_t</type> <name>nameCount</name></member>
<member len="nameCount">const <type>char</type>* const* <name>ppNames</name></member>
<member><type>char</type> <name>label</name>[<enum>VK_MAX_LABEL_SIZE</enum>]</member>
<member><type>uint64_t</type> <name>module</name><comment>Which module</comment></member>
<member><type>VkStageFlags2</type> <name>stages</name></member>
<member><type>float</type> <name>matrix</name>[3][4]</member>
<member><type>PFN_vkHeardCallback</type> <name>pfnHeard</name></member>
<member api="vulkansc"><type>uint32_t</type> <name>scOnly</name></member>
</type>
<type category="struct" name="VkExtent2D">
<member><type>uint32_t</type> <name>width</name></member>
<member><type>uint32_t</type> <name>height</name></member>
</type>
<type category="struct" name="VkPhysicalDeviceVulkan13Features">
<member values="VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES"><type>VkStructureType</type> <name>sType</name></member>
<member optional="true"><type>void</type>* <name>pNext</name></member>
<member><type>VkBool32</type> <name>shaderFloat64</name></member>
<member><type>uint32_t</type> <name>deviceUUID</name></member>
<member><type>uint32_t</type> <name>maxImageDimension2D</name></member>
</type>
<type category="struct" name="VkPackedKHR">
<member><type>uint32_t</type> <name>index</name>:24</member>
<member><type>uint32_t</type> <name>mask</name>:8</member>
</type>
<type category="struct" name="VkWin32ThingCreateInfoKHR">
<member values="VK_STRUCTURE_TYPE_WIN32_THING_CREATE_INFO_KHR"><type>VkStructureType</type> <name>sType</name></member>
<member optional="true">const <type>void</type>* <name>pNext</name></member>
<member><type>HWND</type> <name>hwnd</name></member>
</type>
<type category="struct" name="VkThingCreateInfoKHR" alias="VkThingCreateInfo"/>
</types>
<enums name="API Constants">
<enum type="uint32_t" value="64" name="VK_MAX_LABEL_SIZE"/>
<enum type="uint32_t" value="(~0U)" name="VK_REMAINING"/>
<enum type="uint64_t" value="(~0ULL)" name="VK_WHOLE"/>
<enum type="float" value="1000.0F" name="VK_CLAMP"/>
</enums>
<enums name="VkStructureType" type="enum">
<enum value="0" name="VK_STRUCTURE_TYPE_THING_CREATE_INFO"/>
</enums>
<enums name="VkResult" type="enum">
<enum value="0" name="VK_SUCCESS"/>
<enum value="-1" name="VK_ERROR_OUT_OF_HOST_MEMORY"/>
</enums>
<enums name="VkStageFlagBits2" type="bitmask" bitwidth="64">
<enum bitpos="0" name="VK_STAGE_2_FIRST_BIT"/>
<enum bitpos="40" name="VK_STAGE_2_FAR_BIT"/>
</enums>
<commands>
<command export="vulkan,vulkansc">
<proto><type>VkResult</type> <name>vkCreateThing</name></proto>
<param><type>VkInstance</type> <name>instance</name></param>
<param>const <type>VkThingCreateInfo</type>* <name>pCreateInfo</name></param>
<param optional="true">const <type>void</type>* <name>pAllocator</name></param>
<param><type>VkThing</type>* <name>pThing</name></param>
</command>
<command export="vulkan">
<proto><type>void</type> <name>vkListThings</name></proto>
<param><type>VkInstance</type> <name>instance</name></param>
<param optional="false,true"><type>uint32_t</type>* <name>pCount</name></param>
<param optional="true" len="pCount"><type>VkExtent2D</type>* <name>pThings</name></param>
<param>const <type>float</type> <name>blend</name>[4]</param>
</command>
<command export="vulkan">
<proto><type>PFN_vkVoidFunction</type> <name>vkGetInstanceProcAddr</name></proto>
<param><type>VkInstance</type> <name>instance</name></param>
<param len="null-terminated">const <type>char</type>* <name>pName</name></param>
</command>
<command>
<proto><type>void</type> <name>vkHearEXT</name></proto>
<param><type>VkInstance</type> <name>instance</name></param>
<param><type>VkPhysicalDeviceVulkan13Features</type>* <name>pFeatures</name></param>
</command>
<command>
<proto><type>VkResult</type> <name>vkCreateWin32ThingKHR</name></proto>
<param><type>VkInstance</type> <name>instance</name></param>
<param>const <type>VkWin32ThingCreateInfoKHR</type>* <name>pCreateInfo</name></param>
</command>
<command name="vkCreateThingKHR" alias="vkCreateThing"/>
</commands>
<feature api="vulkan,vulkansc" name="VK_VERSION_1_0" number="1.0">
<require>
<type name="VkThingCreateInfo"/>
<type name="VkResult"/>
<enum name="VK_REMAINING"/>
<enum name="VK_WHOLE"/>
<enum name="VK_CLAMP"/>
<command name="vkCreateThing"/>
<command name="vkGetInstanceProcAddr"/>
</require>
</feature>
<feature api="vulkan" name="VK_VERSION_1_1" number="1.1">
<require>
<type name="VkPhysicalDeviceVulkan13Features"/>
<type name="VkPackedKHR"/>
<enum offset="0" extnumber="12" extends="VkStructureType" name="VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES"/>
<command name="vkListThings"/>
</require>
</feature>
<feature api="vulkansc" name="VKSC_VERSION_1_0" number="1.0">
<require>
<command name="vkSafetyOnly"/>
</require>
</feature>
<extensions>
<extension name="VK_EXT_heard" number="3" supported="vulkan">
<require>
<enum value="2" name="VK_EXT_HEARD_SPEC_VERSION"/>
<enum value="&quot;VK_EXT_heard&quot;" name="VK_EXT_HEARD_EXTENSION_NAME"/>
<enum offset="1" extends="VkResult" dir="-" name="VK_ERROR_NOT_HEARD_EXT"/>
<command name="vkHearEXT"/>
</require>
<require depends="VK_VERSION_1_1">
<type name="VkThingCreateInfoKHR"/>
<command name="vkCreateThingKHR"/>
</require>
</extension>
<extension name="VK_KHR_win32_thing" number="10" supported="vulkan" platform="win32" depends="VK_EXT_heard">
<require>
<enum offset="0" extends="VkStructureType" name="VK_STRUCTURE_TYPE_WIN32_THING_CREATE_INFO_KHR"/>
<type name="VkWin32ThingCreateInfoKHR"/>
<command name="vkCreateWin32ThingKHR"/>
</require>
</extension>
<extension name="VK_EXT_disabled" number="4" supported="disabled"/>
<extension name="VK_MVK_moved" number="5" supported="vulkan" platform="macos"/>
</extensions>
</registry>
""";
# What's wanted of the small registry: its newest version, and the extensions
# named, the platform's exported.
def wanting_of(major: u32, minor: u32, names: span<strview>): wanting {
var made := wanting{ major = major, minor = minor, extensions = vector::<strview>::new(), exported = vector::<strview>::new() };
for &each in names {
made.extensions.append(each);
}
made.exported.append("VK_KHR_win32_thing");
return made;
}
# The text of a file written, or nothing where none of the name was.
def text_of(files: &vector<made_file>, name: strview): strview {
for &each in files {
return each.text.span if each.name.span == name;
}
return "";
}
# Whether a file holds a line.
def holds_line(text: strview, line: strview): bool {
return text.find(line) is some(_);
}
@test def names_types_and_members_as_sakura_does(t: &mut tester): void {
val names: strview[2] = ["VK_EXT_heard", "VK_KHR_win32_thing"];
val wanted := wanting_of(0, 0, names[:]);
var files := vector::<made_file>::new();
var problems := vector::<string>::new();
t.expects(bindings_of(small_registry, &wanted, &mut files, &mut problems) && problems.length == 0);
val types := text_of(&files, "types.sakura");
t.expects(holds_line(types, "pub struct thing_create_info {\n"));
t.expects(holds_line(types, " pub var s_type: u32 = VK_STRUCTURE_TYPE_THING_CREATE_INFO; #! VkStructureType\n"));
t.expects(holds_line(types, " pub var next: *u8;\n"));
t.expects(holds_line(types, " pub var name_count: u32;\n"));
t.expects(holds_line(types, " pub var names: **u8;\n"));
t.expects(holds_line(types, " pub var label: u8[64];\n"));
t.expects(holds_line(types, " pub var module_: u64; #! Which module\n"));
t.expects(holds_line(types, " pub var stages: u64; #! VkStageFlags2\n"));
# C's 3 arrays of 4, as Sakura writes them: innermost first.
t.expects(holds_line(types, " pub var matrix: f32[4][3];\n"));
t.expects(holds_line(types, " pub var heard: PFN_vkHeardCallback;\n"));
t.expects(!holds_line(types, "sc_only"));
# By its `sType`, and in snake case.
t.expects(holds_line(types, "pub struct physical_device_vulkan_1_3_features {\n"));
t.expects(holds_line(types, " pub var next: *mut u8;\n"));
t.expects(holds_line(types, " pub var shader_float64: u32; #! VkBool32\n"));
t.expects(holds_line(types, " pub var device_uuid: u32;\n"));
t.expects(holds_line(types, " pub var max_image_dimension_2d: u32;\n"));
t.expects(holds_line(types, "pub struct extent_2d {\n"));
# Bitfields as one word, an alias by its vendor's letters, and a callback.
t.expects(holds_line(types, "pub struct packed_khr {\n pub var index_and_mask: u32; #! Bits, the lowest first: index, 24, mask, 8.\n"));
t.expects(holds_line(types, "pub type thing_create_info_khr = thing_create_info;\n"));
t.expects(holds_line(types, "pub type PFN_vkHeardCallback = cfn<def(u32, *u8, *mut u8): u32>;\n"));
}
@test def gives_constants_their_values(t: &mut tester): void {
val names: strview[1] = ["VK_EXT_heard"];
val wanted := wanting_of(0, 0, names[:]);
var files := vector::<made_file>::new();
var problems := vector::<string>::new();
t.expects(bindings_of(small_registry, &wanted, &mut files, &mut problems));
val overview := text_of(&files, "vulkan.sakura");
t.expects(holds_line(overview, "pub const VK_HEADER_VERSION: u32 = 7;\n"));
t.expects(holds_line(overview, "pub const VK_API_VERSION_1_1: u32 = (1 << 22) | (1 << 12);\n"));
val constants := text_of(&files, "constants.sakura");
t.expects(holds_line(constants, "pub const VK_REMAINING: u32 = 0xFFFFFFFF;\n"));
t.expects(holds_line(constants, "pub const VK_WHOLE: u64 = 0xFFFFFFFFFFFFFFFF;\n"));
t.expects(holds_line(constants, "pub const VK_CLAMP: f32 = 1000.0;\n"));
t.expects(holds_line(constants, "pub const VK_MAX_LABEL_SIZE: u32 = 64;\n"));
t.expects(holds_line(constants, "pub const VK_ERROR_OUT_OF_HOST_MEMORY: i32 = -1;\n"));
t.expects(holds_line(constants, "pub const VK_ERROR_NOT_HEARD_EXT: i32 = -1000002001;\n"));
t.expects(holds_line(constants, "pub const VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES: u32 = 1000011000;\n"));
t.expects(holds_line(constants, "pub const VK_STAGE_2_FAR_BIT: u64 = 0x10000000000;\n"));
t.expects(holds_line(constants, "# VK_EXT_heard\npub const VK_EXT_HEARD_SPEC_VERSION: u32 = 2;\npub const VK_EXT_HEARD_EXTENSION_NAME: strview = \"VK_EXT_heard\";\n"));
}
@test def declares_what_the_loader_exports_and_types_the_rest(t: &mut tester): void {
val names: strview[1] = ["VK_EXT_heard"];
val wanted := wanting_of(0, 0, names[:]);
var files := vector::<made_file>::new();
var problems := vector::<string>::new();
t.expects(bindings_of(small_registry, &wanted, &mut files, &mut problems));
val commands := text_of(&files, "commands.sakura");
t.expects(holds_line(commands, "extern import \"vulkan-1.dll\" when os == windows;\n"));
t.expects(holds_line(commands, "pub def vkCreateThing(instance: u64, create_info: &thing_create_info, allocator: *u8, thing: &mut u64): i32;\n"));
t.expects(holds_line(commands, "pub def vkListThings(instance: u64, count: &mut u32, things: *mut extent_2d, blend: &f32[4]): void;\n"));
t.expects(holds_line(commands, "pub def vkGetInstanceProcAddr(instance: u64, name: *u8): PFN_vkVoidFunction;\n"));
t.expects(!holds_line(commands, "pub def vkHearEXT"));
t.expects(holds_line(commands, "pub type PFN_vkHearEXT = cfn<def(u64, &mut physical_device_vulkan_1_3_features): void>;\n"));
t.expects(holds_line(commands, "pub type PFN_vkCreateThingKHR = PFN_vkCreateThing;\n"));
t.expects(!holds_line(commands, "vkSafetyOnly"));
}
@test def writes_a_platforms_file_for_its_system(t: &mut tester): void {
val names: strview[2] = ["VK_EXT_heard", "VK_KHR_win32_thing"];
val wanted := wanting_of(0, 0, names[:]);
var files := vector::<made_file>::new();
var problems := vector::<string>::new();
t.expects(bindings_of(small_registry, &wanted, &mut files, &mut problems));
val win32 := text_of(&files, "win32.sakura");
t.expects(holds_line(win32, "extern import \"vulkan-1.dll\" when os == windows;\n\nextern module malunal::vulkan when os == windows;\n"));
t.expects(holds_line(win32, "pub const VK_STRUCTURE_TYPE_WIN32_THING_CREATE_INFO_KHR: u32 = 1000009000;\n"));
t.expects(holds_line(win32, "pub struct win32_thing_create_info_khr {\n"));
t.expects(holds_line(win32, " pub var hwnd: u64; #! HWND\n"));
t.expects(holds_line(win32, "pub def vkCreateWin32ThingKHR(instance: u64, create_info: &win32_thing_create_info_khr): i32;\n"));
t.expects(!holds_line(text_of(&files, "types.sakura"), "win32_thing"));
t.expects(text_of(&files, "xlib.sakura").length == 0);
}
@test def binds_an_older_version_without_whats_newer(t: &mut tester): void {
val names: strview[1] = ["VK_EXT_heard"];
val wanted := wanting_of(1, 0, names[:]);
var files := vector::<made_file>::new();
var problems := vector::<string>::new();
t.expects(bindings_of(small_registry, &wanted, &mut files, &mut problems));
t.expects(!holds_line(text_of(&files, "vulkan.sakura"), "VK_API_VERSION_1_1"));
t.expects(holds_line(text_of(&files, "vulkan.sakura"), "Vulkan 1.1.7, to Vulkan 1.0"));
t.expects(!holds_line(text_of(&files, "commands.sakura"), "vkListThings"));
# What the extension needs only with 1.1.
t.expects(!holds_line(text_of(&files, "commands.sakura"), "PFN_vkCreateThingKHR"));
t.expects(!holds_line(text_of(&files, "types.sakura"), "thing_create_info_khr"));
}
@test def refuses_what_cant_be_bound(t: &mut tester): void {
val cases: strview[4] = ["VK_EXT_nothing", "VK_EXT_disabled", "VK_MVK_moved", "VK_KHR_win32_thing"];
val said: strview[4] = [
"The registry has no extension VK_EXT_nothing.",
"VK_EXT_disabled isn't Vulkan's, or is disabled.",
"VK_MVK_moved is of the platform macos",
"VK_KHR_win32_thing needs VK_EXT_heard",
];
for i in 0..4u {
val wanted := wanting_of(0, 0, cases[i:1]);
var files := vector::<made_file>::new();
var problems := vector::<string>::new();
t.expects(!bindings_of(small_registry, &wanted, &mut files, &mut problems) && problems.length == 1);
t.expects(problems.length == 1 && problems[0].span.starts_with(said[i]));
}
val heard: strview[1] = ["VK_EXT_heard"];
val too_new := wanting_of(2, 0, heard[:]);
var files := vector::<made_file>::new();
var problems := vector::<string>::new();
t.expects(!bindings_of(small_registry, &too_new, &mut files, &mut problems));
t.expects(problems.length == 1 && problems[0].span == "The registry has no Vulkan 2.0.");
}
@test def writes_c_names_in_snake_case(t: &mut tester): void {
t.expects(snake_of("sType").span == "s_type" && snake_of("deviceUUID").span == "device_uuid");
t.expects(snake_of("shaderFloat64").span == "shader_float64" && snake_of("maxImageDimension2D").span == "max_image_dimension_2d");
t.expects(snake_of("Extent2D").span == "extent_2d" && snake_of("textureCompressionASTC_HDR").span == "texture_compression_astc_hdr");
t.expects(snake_of("storageBuffer16BitAccess").span == "storage_buffer16_bit_access" && snake_of("image2DViewOf3D").span == "image_2d_view_of_3d");
t.expects(snake_of("driverID").span == "driver_id" && snake_of("SurfaceFormatKHR").span == "surface_format_khr");
t.expects(without_prefix("ppEnabledLayerNames") == "EnabledLayerNames" && without_prefix("pfnCallback") == "Callback");
t.expects(without_prefix("plane") == "plane" && without_prefix("pNext") == "Next");
}