//! `cargo xtask venus-gen`: Vulkan's encoding for Venus, generated from //! Vulkan's registry and Venus's two additions (`venus/registry/`) into //! `venus/src/vk.rs` and `venus/src/encode.rs`, by venus-protocol's rules. mod emit; mod extensions; mod registry; mod xml; use registry::{Kind, Registry}; use std::path::Path; /// Reads the three registry files and narrows them to what Venus carries. pub fn load(root: &Path) -> Result<(Registry, std::collections::BTreeSet), String> { let mut reg = Registry::default(); for file in [ "vk.xml", "VK_EXT_command_serialization.xml", "VK_MESA_venus_protocol.xml", ] { let path = root.join("venus/registry").join(file); let source = std::fs::read_to_string(&path).map_err(|e| format!("{}: {e}", path.display()))?; reg.read(&xml::parse(&source)?)?; } reg.number_commands(); let selected = reg.selected(); Ok((reg, selected)) } pub fn run(root: &Path) -> Result<(), String> { let (reg, selected) = load(root)?; let mut commands = 0; let mut structs = 0; let mut unions = 0; let mut handles = 0; let mut unnumbered = Vec::new(); for name in &selected { match ®.types[name].kind { Kind::Command { .. } => { commands += 1; if !reg.command_types.contains_key(name) { unnumbered.push(name.clone()); } } Kind::Struct { union: true, .. } => unions += 1, Kind::Struct { .. } => structs += 1, Kind::Handle => handles += 1, _ => {} } } println!( "venus-gen: {commands} commands ({} command types known), {structs} structs, {unions} unions, {handles} handles", reg.command_types.len() ); if !unnumbered.is_empty() { return Err(format!("commands without a command type: {unnumbered:?}")); } let generator = emit::Gen { reg: ®, selected: &selected, }; const HEADER: &str = "// Generated by `cargo xtask venus-gen` from venus/registry: do not edit.\n"; let types = format!( "{HEADER}//! Vulkan's types as C lays them out, and its values.\n\n\ #![allow(non_camel_case_types, non_snake_case, non_upper_case_globals, dead_code, clippy::all)]\n\n\ use core::ffi::{{c_char, c_void}};\n\n{}", generator.types() ); let encoders = format!( "{HEADER}//! Venus's encoders and reply decoders for Vulkan's structs and commands.\n\n\ #![allow(non_snake_case, unused_unsafe, unused_variables, unused_mut, unsafe_op_in_unsafe_fn, \ unreachable_patterns, unused_parens, dead_code, clippy::all)]\n\n\ use super::vk::*;\nuse super::wire::{{Base, Decoder, Encoder, find_in_chain}};\n\ use core::ffi::{{c_char, c_void}};\n\n{}", generator.encoders() ); // The driver's own functions (vulkan/src/lib.rs): what Venus treats // otherwise than send-and-read. const SPECIAL: &[&str] = &[ "vkGetInstanceProcAddr", "vkGetDeviceProcAddr", "vkEnumerateInstanceExtensionProperties", "vkEnumerateInstanceLayerProperties", "vkEnumerateDeviceLayerProperties", "vkEnumerateDeviceExtensionProperties", "vkCreateInstance", "vkEnumeratePhysicalDevices", "vkEnumeratePhysicalDeviceGroups", "vkCreateDevice", "vkGetDeviceQueue", "vkGetDeviceQueue2", "vkAllocateMemory", "vkMapMemory", "vkCreateBuffer", "vkDestroyBuffer", "vkGetBufferMemoryRequirements", "vkUnmapMemory", "vkFreeMemory", "vkWaitForFences", "vkQueueWaitIdle", "vkDeviceWaitIdle", // The window system: the driver's own (vulkan/src/wsi.rs). "vkDestroySurfaceKHR", "vkGetPhysicalDeviceSurfaceSupportKHR", "vkGetPhysicalDeviceSurfaceCapabilitiesKHR", "vkGetPhysicalDeviceSurfaceFormatsKHR", "vkGetPhysicalDeviceSurfacePresentModesKHR", "vkCreateSwapchainKHR", "vkDestroySwapchainKHR", "vkGetSwapchainImagesKHR", "vkAcquireNextImageKHR", "vkQueuePresentKHR", ]; // Sent without waiting for the reply, answered VK_SUCCESS at once, as // Mesa's Venus driver sends them: the handles they make are named by // the driver, and they fail only when the host is out of memory, which // the host then reports as a lost device. A Cartopolis frame waited // 2–5 ms on each of hundreds of these. const ASYNC: &[&str] = &[ "vkCreateBufferView", "vkCreateImageView", "vkCreateSampler", "vkCreateFramebuffer", "vkCreateSemaphore", "vkCreateFence", "vkBindBufferMemory", "vkBindBufferMemory2", "vkBindImageMemory", "vkBindImageMemory2", "vkFreeDescriptorSets", "vkResetDescriptorPool", "vkResetFences", "vkBeginCommandBuffer", "vkEndCommandBuffer", "vkResetCommandBuffer", "vkResetCommandPool", ]; let driver = format!( "{HEADER}//! The driver's Vulkan functions, each sent through Venus.\n\n\ #![allow(non_snake_case, unused_unsafe, unsafe_op_in_unsafe_fn, clippy::all)]\n\n\ use crate::PFN_vkVoidFunction;\nuse pinnacle_venus::encode::*;\nuse pinnacle_venus::vk::*;\n\ use std::ffi::{{c_char, c_void}};\n\n{}", generator.driver(SPECIAL, ASYNC) ); for (file, text) in [ ("venus/src/vk.rs", types), ("venus/src/encode.rs", encoders), ("vulkan/src/commands.rs", driver), ] { let path = root.join(file); std::fs::write(&path, text).map_err(|e| format!("{}: {e}", path.display()))?; // Formatted as the rest is, so `cargo fmt --check` passes it. let formatted = std::process::Command::new("rustfmt") .args(["--edition", "2024", "--config-path"]) .arg(root.join("rustfmt.toml")) .arg(&path) .status() .map_err(|e| format!("rustfmt: {e}"))?; if !formatted.success() { return Err(format!("rustfmt failed on {}", path.display())); } println!("venus-gen: wrote {}", path.display()); } Ok(()) }