Skip to content

Commit

Permalink
rewrite: Initial codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
Friz64 committed Mar 13, 2024
1 parent 8eb9094 commit 6b0735d
Show file tree
Hide file tree
Showing 267 changed files with 2,412 additions and 18 deletions.
1 change: 1 addition & 0 deletions analysis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
[dependencies]
roxmltree = "0.19"
tracing = "0.1"
indexmap = "2"
4 changes: 2 additions & 2 deletions analysis/src/cdecl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct UnsupportedCTok<'a>(&'a str);
impl<'a> CTok<'a> {
pub const SUPPORTED_KEYWORDS: &'static [&'static str] = &["const", "struct", "typedef", "void"];

pub fn lex_into(
pub(crate) fn lex_into(
s: &'a str,
out: &mut impl Extend<CTok<'a>>,
) -> Result<(), UnsupportedCTok<'a>> {
Expand Down Expand Up @@ -143,7 +143,7 @@ pub enum CDeclParseErrorKind<'a> {

impl<'a> CDecl<'a> {
// HACK(eddyb) this split is literally just to simplify error tracking.
pub fn parse<'b>(
pub(crate) fn parse<'b>(
mode: CDeclMode,
tokens: &'b [CTok<'a>],
) -> Result<CDecl<'a>, CDeclParseError<'a, 'b>> {
Expand Down
22 changes: 22 additions & 0 deletions analysis/src/items/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use self::structures::Structure;
use crate::LibraryId;
use indexmap::IndexMap;

pub mod structures;

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct Origin {
pub library_id: LibraryId,
pub required_by: RequiredBy,
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum RequiredBy {
Feature { major: u32, minor: u32 },
Extension { name: &'static str },
}

#[derive(Default, Debug)]
pub struct Items {
pub structures: IndexMap<&'static str, Structure>,
}
17 changes: 17 additions & 0 deletions analysis/src/items/structures.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use super::Origin;
use crate::xml::{self, UnwrapBorrowed};

#[derive(Debug)]
pub struct Structure {
pub origin: Origin,
pub name: &'static str,
}

impl Structure {
pub fn new(origin: Origin, xml: &xml::Structure) -> Structure {
Structure {
origin,
name: xml.name.unwrap_borrowed(),
}
}
}
127 changes: 114 additions & 13 deletions analysis/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,139 @@
mod xml;
pub mod cdecl;
pub mod items;
pub mod xml;

use std::{fs, path::Path};
use crate::items::Origin;
use items::{structures::Structure, Items, RequiredBy};
use std::{
collections::HashMap,
fmt::{self, Display},
fs,
path::{Path, PathBuf},
};
use tracing::{debug, error_span};
use xml::UnwrapBorrowed;

#[derive(Debug)]
pub struct Analysis {
pub vk: Library,
pub video: Library,
vk: Library,
video: Library,
items: Items,
}

impl Analysis {
pub fn new(vulkan_headers_path: impl AsRef<Path>) -> Analysis {
let vk = Library::new(LibraryId::Vk, &vulkan_headers_path);
let video = Library::new(LibraryId::Video, &vulkan_headers_path);

let mut items = Items::default();
vk.collect_into(&mut items);
video.collect_into(&mut items);

Analysis { vk, video, items }
}

pub fn vk_xml(&self) -> &xml::Registry {
&self.vk.xml
}

pub fn video_xml(&self) -> &xml::Registry {
&self.video.xml
}

pub fn items(&self) -> &Items {
&self.items
}
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum LibraryId {
Vk,
Video,
}

impl LibraryId {
fn xml_path(&self, vulkan_headers_path: impl AsRef<Path>) -> PathBuf {
let vulkan_headers_path = vulkan_headers_path.as_ref();
Analysis {
vk: Library::new(vulkan_headers_path.join("registry/vk.xml")),
video: Library::new(vulkan_headers_path.join("registry/video.xml")),
match self {
LibraryId::Vk => vulkan_headers_path.join("registry/vk.xml"),
LibraryId::Video => vulkan_headers_path.join("registry/video.xml"),
}
}
}

impl Display for LibraryId {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(match self {
LibraryId::Vk => "vk",
LibraryId::Video => "video",
})
}
}

#[derive(Debug)]
pub struct Library {
_xml: xml::Registry,
id: LibraryId,
xml: xml::Registry,
}

impl Library {
fn new(xml_path: impl AsRef<Path>) -> Library {
let xml = error_span!("xml", path = %xml_path.as_ref().display()).in_scope(|| {
fn new(id: LibraryId, vulkan_headers_path: impl AsRef<Path>) -> Library {
let xml = error_span!("xml", library_id = %id).in_scope(|| {
let path = id.xml_path(vulkan_headers_path);
// We leak the input string here for convenience, to avoid explicit lifetimes.
let xml_input = Box::leak(fs::read_to_string(xml_path).unwrap().into_boxed_str());
let input = Box::leak(fs::read_to_string(path).unwrap().into_boxed_str());
debug!("parsing xml");
xml::Registry::parse(xml_input, "vulkan")
xml::Registry::parse(input, "vulkan")
});

Library { _xml: xml }
Library { id, xml }
}

pub fn xml(&self) -> &xml::Registry {
&self.xml
}

fn collect_into(&self, items: &mut Items) {
let mut types_required_by = HashMap::new();

for feature in &self.xml.features {
let required_by = RequiredBy::Feature {
major: feature.major,
minor: feature.minor,
};

for require in &feature.requires {
for require_type in &require.types {
types_required_by.insert(require_type.name.unwrap_borrowed(), required_by);
}
}
}

for extension in &self.xml.extensions {
let required_by = RequiredBy::Extension {
name: extension.name.unwrap_borrowed(),
};

for require in &extension.requires {
for require_type in &require.types {
types_required_by.insert(require_type.name.unwrap_borrowed(), required_by);
}
}
}

for structure in &self.xml.structs {
let name = structure.name.unwrap_borrowed();
let Some(&required_by) = types_required_by.get(name) else {
continue;
};

let origin = Origin {
library_id: self.id,
required_by,
};

let structure = Structure::new(origin, structure);
assert!(items.structures.insert(name, structure).is_none());
}
}
}
21 changes: 20 additions & 1 deletion analysis/src/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,26 @@ type Node<'a> = roxmltree::Node<'a, 'static>;
///
/// A `&'static str` is not used directly because sometimes string allocation is
/// needed, for example when replacing `&quot;` with normal quotes.
type XmlStr = Cow<'static, str>;
pub type XmlStr = Cow<'static, str>;

pub trait UnwrapBorrowed<'a, B>
where
B: ToOwned + ?Sized,
{
fn unwrap_borrowed(&self) -> &'a B;
}

impl<'a, B> UnwrapBorrowed<'a, B> for Cow<'a, B>
where
B: ToOwned + ?Sized,
{
fn unwrap_borrowed(&self) -> &'a B {
match self {
Cow::Borrowed(b) => b,
Cow::Owned(_) => panic!("Called `unwrap_borrowed` on `Owned` value"),
}
}
}

/// Retrieves the value of the `node`'s attribute named `name`.
fn attribute(node: Node, name: &str) -> Option<XmlStr> {
Expand Down
3 changes: 3 additions & 0 deletions ash-rewrite/src/generated/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub mod video;
pub mod vk;
9 changes: 9 additions & 0 deletions ash-rewrite/src/generated/video/extension/codec_h264std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct StdVideoH264SpsVuiFlags;
pub struct StdVideoH264HrdParameters;
pub struct StdVideoH264SequenceParameterSetVui;
pub struct StdVideoH264SpsFlags;
pub struct StdVideoH264ScalingLists;
pub struct StdVideoH264SequenceParameterSet;
pub struct StdVideoH264PpsFlags;
pub struct StdVideoH264PictureParameterSet;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct StdVideoDecodeH264PictureInfoFlags;
pub struct StdVideoDecodeH264PictureInfo;
pub struct StdVideoDecodeH264ReferenceInfoFlags;
pub struct StdVideoDecodeH264ReferenceInfo;
13 changes: 13 additions & 0 deletions ash-rewrite/src/generated/video/extension/codec_h264std_encode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct StdVideoEncodeH264WeightTableFlags;
pub struct StdVideoEncodeH264WeightTable;
pub struct StdVideoEncodeH264SliceHeaderFlags;
pub struct StdVideoEncodeH264PictureInfoFlags;
pub struct StdVideoEncodeH264ReferenceInfoFlags;
pub struct StdVideoEncodeH264ReferenceListsInfoFlags;
pub struct StdVideoEncodeH264RefListModEntry;
pub struct StdVideoEncodeH264RefPicMarkingEntry;
pub struct StdVideoEncodeH264ReferenceListsInfo;
pub struct StdVideoEncodeH264PictureInfo;
pub struct StdVideoEncodeH264ReferenceInfo;
pub struct StdVideoEncodeH264SliceHeader;
20 changes: 20 additions & 0 deletions ash-rewrite/src/generated/video/extension/codec_h265std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct StdVideoH265ProfileTierLevelFlags;
pub struct StdVideoH265ProfileTierLevel;
pub struct StdVideoH265DecPicBufMgr;
pub struct StdVideoH265SubLayerHrdParameters;
pub struct StdVideoH265HrdFlags;
pub struct StdVideoH265HrdParameters;
pub struct StdVideoH265VpsFlags;
pub struct StdVideoH265VideoParameterSet;
pub struct StdVideoH265ScalingLists;
pub struct StdVideoH265ShortTermRefPicSetFlags;
pub struct StdVideoH265ShortTermRefPicSet;
pub struct StdVideoH265LongTermRefPicsSps;
pub struct StdVideoH265SpsVuiFlags;
pub struct StdVideoH265SequenceParameterSetVui;
pub struct StdVideoH265PredictorPaletteEntries;
pub struct StdVideoH265SpsFlags;
pub struct StdVideoH265SequenceParameterSet;
pub struct StdVideoH265PpsFlags;
pub struct StdVideoH265PictureParameterSet;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct StdVideoDecodeH265PictureInfoFlags;
pub struct StdVideoDecodeH265PictureInfo;
pub struct StdVideoDecodeH265ReferenceInfoFlags;
pub struct StdVideoDecodeH265ReferenceInfo;
12 changes: 12 additions & 0 deletions ash-rewrite/src/generated/video/extension/codec_h265std_encode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct StdVideoEncodeH265WeightTableFlags;
pub struct StdVideoEncodeH265WeightTable;
pub struct StdVideoEncodeH265LongTermRefPics;
pub struct StdVideoEncodeH265SliceSegmentHeaderFlags;
pub struct StdVideoEncodeH265SliceSegmentHeader;
pub struct StdVideoEncodeH265ReferenceListsInfoFlags;
pub struct StdVideoEncodeH265ReferenceListsInfo;
pub struct StdVideoEncodeH265PictureInfoFlags;
pub struct StdVideoEncodeH265PictureInfo;
pub struct StdVideoEncodeH265ReferenceInfoFlags;
pub struct StdVideoEncodeH265ReferenceInfo;
19 changes: 19 additions & 0 deletions ash-rewrite/src/generated/video/extension/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub mod codec_h264std;
pub mod codec_h264std_decode;
pub mod codec_h264std_encode;
pub mod codec_h265std;
pub mod codec_h265std_decode;
pub mod codec_h265std_encode;
#[doc(no_inline)]
pub use codec_h264std::*;
#[doc(no_inline)]
pub use codec_h264std_decode::*;
#[doc(no_inline)]
pub use codec_h264std_encode::*;
#[doc(no_inline)]
pub use codec_h265std::*;
#[doc(no_inline)]
pub use codec_h265std_decode::*;
#[doc(no_inline)]
pub use codec_h265std_encode::*;
4 changes: 4 additions & 0 deletions ash-rewrite/src/generated/video/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub mod extension;
#[doc(no_inline)]
pub use extension::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct VkPhysicalDeviceCoherentMemoryFeaturesAMD;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct VkDisplayNativeHdrSurfaceCapabilitiesAMD;
pub struct VkSwapchainDisplayNativeHdrCreateInfoAMD;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct VkDeviceMemoryOverallocationCreateInfoAMD;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct VkPipelineCompilerControlCreateInfoAMD;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct VkPipelineRasterizationStateRasterizationOrderAMD;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct VkPhysicalDeviceShaderCorePropertiesAMD;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct VkPhysicalDeviceShaderCoreProperties2AMD;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct VkPhysicalDeviceShaderEarlyAndLateFragmentTestsFeaturesAMD;
3 changes: 3 additions & 0 deletions ash-rewrite/src/generated/vk/extension/amd_shader_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct VkShaderResourceUsageAMD;
pub struct VkShaderStatisticsInfoAMD;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct VkTextureLODGatherFormatPropertiesAMD;
8 changes: 8 additions & 0 deletions ash-rewrite/src/generated/vk/extension/amdx_shader_enqueue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct VkPhysicalDeviceShaderEnqueuePropertiesAMDX;
pub struct VkPhysicalDeviceShaderEnqueueFeaturesAMDX;
pub struct VkExecutionGraphPipelineCreateInfoAMDX;
pub struct VkPipelineShaderStageNodeCreateInfoAMDX;
pub struct VkExecutionGraphPipelineScratchSizeAMDX;
pub struct VkDispatchGraphInfoAMDX;
pub struct VkDispatchGraphCountInfoAMDX;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct VkPhysicalDeviceExternalFormatResolveFeaturesANDROID;
pub struct VkPhysicalDeviceExternalFormatResolvePropertiesANDROID;
pub struct VkAndroidHardwareBufferFormatResolvePropertiesANDROID;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// DO NOT EDIT: @generated by ash generator-rewrite 2.0.0
pub struct VkImportAndroidHardwareBufferInfoANDROID;
pub struct VkAndroidHardwareBufferUsageANDROID;
pub struct VkAndroidHardwareBufferPropertiesANDROID;
pub struct VkMemoryGetAndroidHardwareBufferInfoANDROID;
pub struct VkAndroidHardwareBufferFormatPropertiesANDROID;
pub struct VkExternalFormatANDROID;
pub struct VkAndroidHardwareBufferFormatProperties2ANDROID;
Loading

0 comments on commit 6b0735d

Please sign in to comment.