use std::collections::HashMap;
use super::ints::{encodable_u64, encode_u32, encode_u64, write_u32_array};
use super::lz4;
use super::parse::{Specifier, UsdLayer, UsdPrim, UsdProperty, UsdVariantSet};
use super::value::UsdValue;
const VERSION: (u8, u8, u8) = (0, 8, 0);
const BOOTSTRAP: usize = 88;
mod spec {
pub const ATTRIBUTE: u32 = 1;
pub const PRIM: u32 = 6;
pub const PSEUDO_ROOT: u32 = 7;
pub const RELATIONSHIP: u32 = 8;
pub const VARIANT: u32 = 10;
pub const VARIANT_SET: u32 = 11;
}
mod kind {
pub const BOOL: u8 = 1;
pub const UCHAR: u8 = 2;
pub const INT: u8 = 3;
pub const UINT: u8 = 4;
pub const INT64: u8 = 5;
pub const UINT64: u8 = 6;
pub const HALF: u8 = 7;
pub const FLOAT: u8 = 8;
pub const DOUBLE: u8 = 9;
pub const STRING: u8 = 10;
pub const TOKEN: u8 = 11;
pub const ASSET_PATH: u8 = 12;
pub const MATRIX2D: u8 = 13;
pub const MATRIX3D: u8 = 14;
pub const MATRIX4D: u8 = 15;
pub const QUATD: u8 = 16;
pub const QUATF: u8 = 17;
pub const QUATH: u8 = 18;
pub const VEC2D: u8 = 19;
pub const VEC2F: u8 = 20;
pub const VEC2H: u8 = 21;
pub const VEC2I: u8 = 22;
pub const VEC3D: u8 = 23;
pub const VEC3F: u8 = 24;
pub const VEC3H: u8 = 25;
pub const VEC3I: u8 = 26;
pub const VEC4D: u8 = 27;
pub const VEC4F: u8 = 28;
pub const VEC4H: u8 = 29;
pub const VEC4I: u8 = 30;
pub const DICTIONARY: u8 = 31;
pub const TOKEN_LIST_OP: u8 = 32;
pub const VALUE_BLOCK: u8 = 51;
pub const TIME_CODE: u8 = 56;
pub const PATH_EXPRESSION: u8 = 57;
pub const STRING_LIST_OP: u8 = 33;
pub const PATH_LIST_OP: u8 = 34;
pub const REFERENCE_LIST_OP: u8 = 35;
pub const TOKEN_VECTOR: u8 = 41;
pub const SPECIFIER: u8 = 42;
pub const VARIABILITY: u8 = 44;
pub const VARIANT_SELECTION_MAP: u8 = 45;
pub const TIME_SAMPLES: u8 = 46;
pub const LAYER_OFFSET_VECTOR: u8 = 49;
pub const STRING_VECTOR: u8 = 50;
pub const PAYLOAD_LIST_OP: u8 = 55;
}
const IS_ARRAY: u64 = 1 << 63;
const IS_INLINED: u64 = 1 << 62;
fn kind_of(type_name: &str) -> Option<(u8, bool)> {
let (base, array) = match type_name.strip_suffix("[]") {
Some(base) => (base, true),
None => (type_name, false),
};
let kind = match base {
"bool" => kind::BOOL,
"uchar" => kind::UCHAR,
"int" => kind::INT,
"uint" => kind::UINT,
"int64" => kind::INT64,
"uint64" => kind::UINT64,
"half" => kind::HALF,
"float" => kind::FLOAT,
"double" => kind::DOUBLE,
"timecode" => kind::TIME_CODE,
"string" => kind::STRING,
"token" => kind::TOKEN,
"asset" => kind::ASSET_PATH,
"pathExpression" => kind::PATH_EXPRESSION,
"matrix2d" => kind::MATRIX2D,
"matrix3d" => kind::MATRIX3D,
"matrix4d" | "frame4d" => kind::MATRIX4D,
"quatd" => kind::QUATD,
"quatf" => kind::QUATF,
"quath" => kind::QUATH,
"double2" | "point2d" | "normal2d" | "vector2d" | "texCoord2d" => kind::VEC2D,
"float2" | "point2f" | "normal2f" | "vector2f" | "texCoord2f" => kind::VEC2F,
"half2" | "texCoord2h" => kind::VEC2H,
"int2" => kind::VEC2I,
"double3" | "point3d" | "normal3d" | "vector3d" | "color3d" | "texCoord3d" => kind::VEC3D,
"float3" | "point3f" | "normal3f" | "vector3f" | "color3f" | "texCoord3f" => kind::VEC3F,
"half3" | "point3h" | "normal3h" | "vector3h" | "color3h" | "texCoord3h" => kind::VEC3H,
"int3" => kind::VEC3I,
"double4" | "color4d" => kind::VEC4D,
"float4" | "color4f" => kind::VEC4F,
"half4" | "color4h" => kind::VEC4H,
"int4" => kind::VEC4I,
_ => return None,
};
Some((kind, array))
}
fn shape(kind: u8) -> Option<(usize, usize)> {
Some(match kind {
kind::BOOL | kind::UCHAR => (1, 1),
kind::INT | kind::UINT | kind::FLOAT => (1, 4),
kind::INT64 | kind::UINT64 | kind::DOUBLE | kind::TIME_CODE => (1, 8),
kind::HALF => (1, 2),
kind::VEC2H => (2, 2),
kind::VEC3H => (3, 2),
kind::VEC4H | kind::QUATH => (4, 2),
kind::VEC2F => (2, 4),
kind::VEC3F => (3, 4),
kind::VEC4F | kind::QUATF => (4, 4),
kind::VEC2I => (2, 4),
kind::VEC3I => (3, 4),
kind::VEC4I => (4, 4),
kind::VEC2D => (2, 8),
kind::VEC3D => (3, 8),
kind::VEC4D | kind::QUATD => (4, 8),
kind::MATRIX2D => (4, 8),
kind::MATRIX3D => (9, 8),
kind::MATRIX4D => (16, 8),
_ => return None,
})
}
fn is_float_kind(kind: u8) -> bool {
matches!(
kind,
kind::HALF
| kind::FLOAT
| kind::DOUBLE
| kind::VEC2H
| kind::VEC3H
| kind::VEC4H
| kind::QUATH
| kind::VEC2F
| kind::VEC3F
| kind::VEC4F
| kind::QUATF
| kind::VEC2D
| kind::VEC3D
| kind::VEC4D
| kind::QUATD
| kind::MATRIX2D
| kind::MATRIX3D
| kind::MATRIX4D
| kind::TIME_CODE
)
}
fn is_quat(kind: u8) -> bool {
matches!(kind, kind::QUATD | kind::QUATF | kind::QUATH)
}
fn f32_to_half(v: f32) -> u16 {
let bits = v.to_bits();
let sign = ((bits >> 16) & 0x8000) as u16;
let exponent = ((bits >> 23) & 0xFF) as i32;
let mantissa = bits & 0x7F_FFFF;
if exponent == 0xFF {
let payload = if mantissa != 0 { 0x200 } else { 0 };
return sign | 0x7C00 | payload;
}
let unbiased = exponent - 127 + 15;
if unbiased >= 0x1F {
return sign | 0x7C00; }
if unbiased <= 0 {
if unbiased < -10 {
return sign;
}
let mantissa = mantissa | 0x80_0000;
let shift = (14 - unbiased) as u32;
let kept = (mantissa >> shift) as u16;
return sign | (kept + round_up(mantissa, shift, kept) as u16);
}
let kept = ((unbiased as u16) << 10) | (mantissa >> 13) as u16;
sign | (kept + round_up(mantissa, 13, kept) as u16)
}
fn round_up(mantissa: u32, shift: u32, kept: u16) -> bool {
let dropped = mantissa & ((1u32 << shift) - 1);
let halfway = 1u32 << (shift - 1);
dropped > halfway || (dropped == halfway && kept & 1 == 1)
}
#[derive(Default)]
struct Writer {
tokens: Vec<String>,
token_index: HashMap<String, u32>,
strings: Vec<u32>,
string_index: HashMap<u32, u32>,
fields: Vec<(u32, u64)>,
field_index: HashMap<(u32, u64), u32>,
field_sets: Vec<i32>,
set_index: HashMap<Vec<i32>, u32>,
path_elements: Vec<i32>,
path_jumps: Vec<i32>,
specs: Vec<(u32, u32, u32)>,
data: Vec<u8>,
slots: HashMap<String, u32>,
empty_path_slot: u32,
needs_version: (u8, u8, u8),
interned: HashMap<Vec<u8>, u64>,
}
impl Writer {
fn token(&mut self, text: &str) -> u32 {
if let Some(found) = self.token_index.get(text) {
return *found;
}
let index = self.tokens.len() as u32;
self.tokens.push(text.to_string());
self.token_index.insert(text.to_string(), index);
index
}
fn string(&mut self, text: &str) -> u32 {
let token = self.token(text);
if let Some(found) = self.string_index.get(&token) {
return *found;
}
let index = self.strings.len() as u32;
self.strings.push(token);
self.string_index.insert(token, index);
index
}
fn field(&mut self, name: &str, rep: u64) -> u32 {
let name = self.token(name);
if let Some(found) = self.field_index.get(&(name, rep)) {
return *found;
}
let index = self.fields.len() as u32;
self.fields.push((name, rep));
self.field_index.insert((name, rep), index);
index
}
fn field_set(&mut self, mut members: Vec<i32>) -> u32 {
members.push(-1);
if let Some(found) = self.set_index.get(&members) {
return *found;
}
let index = self.field_sets.len() as u32;
self.field_sets.extend_from_slice(&members);
self.set_index.insert(members, index);
index
}
fn path_slot(&mut self, path: &str) -> u32 {
if path.is_empty() {
return self.empty_path_slot;
}
self.slots
.get(path)
.copied()
.unwrap_or(self.empty_path_slot)
}
fn needs(&mut self, major: u8, minor: u8, patch: u8) {
self.needs_version = self.needs_version.max((major, minor, patch));
}
fn intern(&mut self, bytes: Vec<u8>) -> u64 {
if let Some(found) = self.interned.get(&bytes) {
return *found;
}
while !self.data.len().is_multiple_of(8) {
self.data.push(0);
}
let at = (BOOTSTRAP + self.data.len()) as u64;
self.data.extend_from_slice(&bytes);
self.interned.insert(bytes, at);
at
}
}
#[derive(Default)]
struct Node {
name: String,
property: bool,
children: Vec<Node>,
}
impl Node {
fn child_mut(&mut self, name: &str, property: bool) -> &mut Node {
if let Some(i) = self
.children
.iter()
.position(|c| c.name == name && c.property == property)
{
return &mut self.children[i];
}
self.children.push(Node {
name: name.to_string(),
property,
children: Vec::new(),
});
self.children.last_mut().unwrap()
}
fn insert_path(&mut self, path: &str) {
let Some(rest) = path.strip_prefix('/') else {
return;
};
let (prims, property) = match rest.split_once('.') {
Some((prims, property)) => (prims, Some(property)),
None => (rest, None),
};
let mut node = self;
for part in prims.split('/').filter(|p| !p.is_empty()) {
node = node.child_mut(part, false);
}
if let Some(property) = property {
node.child_mut(property, true);
}
}
}
fn collect(node: &mut Node, prims: &[UsdPrim]) {
for prim in prims {
let child = node.child_mut(&prim.name, false);
collect_one(child, prim);
}
}
fn collect_one(node: &mut Node, prim: &UsdPrim) {
for property in &prim.properties {
node.child_mut(&property.name, true);
}
for set in &prim.variant_sets {
node.child_mut(&format!("{{{}=}}", set.name), false);
for (choice, body) in &set.variants {
let variant = node.child_mut(&format!("{{{}={}}}", set.name, choice), false);
collect_one(variant, body);
}
}
collect(node, &prim.children);
}
fn referenced_paths(prims: &[UsdPrim], out: &mut Vec<String>) {
for prim in prims {
for property in &prim.properties {
match &property.value {
UsdValue::Path(p) => out.push(p.clone()),
UsdValue::Array(items) if property.relationship => {
out.extend(items.iter().filter_map(|v| v.as_str()).map(str::to_string));
}
_ => {}
}
}
for (key, value) in &prim.metadata {
if !matches!(
split_qualifier(key).1,
"references" | "payload" | "inherits" | "specializes"
) {
continue;
}
for arc in value.references() {
if !arc.prim_path.is_empty() {
out.push(arc.prim_path.clone());
}
}
}
for set in &prim.variant_sets {
for (_, body) in &set.variants {
referenced_paths(std::slice::from_ref(body), out);
}
}
referenced_paths(&prim.children, out);
}
}
pub fn write(layer: &UsdLayer) -> Vec<u8> {
let mut w = Writer::default();
w.token("");
let mut tree = Node::default();
collect(&mut tree, &layer.prims);
let mut referenced = Vec::new();
referenced_paths(&layer.prims, &mut referenced);
for path in &referenced {
tree.insert_path(path);
}
let root_at = begin_entry(&mut w, 0);
w.slots.insert("/".to_string(), (root_at + 1) as u32);
let mut total = 1usize;
for (i, child) in tree.children.iter().enumerate() {
total += emit(&mut w, child, "", i + 1 < tree.children.len());
}
w.path_jumps[root_at] = jump_for(!tree.children.is_empty(), false, total);
write_root_spec(&mut w, layer);
for prim in &layer.prims {
write_prim_spec(&mut w, prim, "");
}
assemble(w)
}
fn emit(w: &mut Writer, node: &Node, parent: &str, has_sibling: bool) -> usize {
let token = w.token(&node.name) as i32;
let at = begin_entry(w, if node.property { -token } else { token });
let path = if node.property {
format!("{parent}.{}", node.name)
} else if node.name.starts_with('{') {
format!("{parent}{}", node.name)
} else {
format!("{parent}/{}", node.name)
};
w.slots.insert(path.clone(), (at + 1) as u32);
let mut total = 1usize;
for (i, child) in node.children.iter().enumerate() {
total += emit(w, child, &path, i + 1 < node.children.len());
}
w.path_jumps[at] = jump_for(!node.children.is_empty(), has_sibling, total);
total
}
fn begin_entry(w: &mut Writer, element: i32) -> usize {
let at = w.path_elements.len();
w.path_elements.push(element);
w.path_jumps.push(-2);
at
}
fn write_root_spec(w: &mut Writer, layer: &UsdLayer) {
let mut members = Vec::new();
for (name, rep) in arc_fields(w, &layer.metadata) {
members.push(w.field(name, rep) as i32);
}
for (key, value) in &layer.metadata {
if key.is_empty() || is_arc_name(split_qualifier(key).1) {
continue;
}
if let Some(rep) = value_rep(w, key, value, "") {
members.push(w.field(key, rep) as i32);
}
}
if !layer.prims.is_empty() {
let names: Vec<UsdValue> = layer
.prims
.iter()
.map(|p| UsdValue::Token(p.name.clone()))
.collect();
let rep = token_vector(w, &names);
members.push(w.field("primChildren", rep) as i32);
}
let set = w.field_set(members);
let at = w.slots.get("/").copied().unwrap_or(0);
w.specs.push((at, set, spec::PSEUDO_ROOT));
}
fn write_prim_spec(w: &mut Writer, prim: &UsdPrim, parent: &str) {
let path = format!("{parent}/{}", prim.name);
let mut members = Vec::new();
let specifier = match prim.specifier {
Specifier::Def => 0u64,
Specifier::Over => 1,
Specifier::Class => 2,
};
let rep = inlined(kind::SPECIFIER, specifier);
members.push(w.field("specifier", rep) as i32);
if !prim.type_name.is_empty() {
let token = w.token(&prim.type_name);
let rep = inlined(kind::TOKEN, token as u64);
members.push(w.field("typeName", rep) as i32);
}
for (name, rep) in arc_fields(w, &prim.metadata) {
members.push(w.field(name, rep) as i32);
}
for (key, value) in &prim.metadata {
if key.is_empty() || key == "specifier" || key == "typeName" {
continue;
}
if is_arc_name(split_qualifier(key).1) {
continue;
}
if let Some(rep) = value_rep(w, key, value, "") {
members.push(w.field(key, rep) as i32);
}
}
if !prim.properties.is_empty() {
let names: Vec<UsdValue> = prim
.properties
.iter()
.map(|p| UsdValue::Token(p.name.clone()))
.collect();
let rep = token_vector(w, &names);
members.push(w.field("properties", rep) as i32);
}
if !prim.children.is_empty() {
let names: Vec<UsdValue> = prim
.children
.iter()
.map(|c| UsdValue::Token(c.name.clone()))
.collect();
let rep = token_vector(w, &names);
members.push(w.field("primChildren", rep) as i32);
}
if !prim.variant_sets.is_empty() {
let names: Vec<UsdValue> = prim
.variant_sets
.iter()
.map(|s| UsdValue::Token(s.name.clone()))
.collect();
let rep = token_vector(w, &names);
members.push(w.field("variantSetChildren", rep) as i32);
}
let set = w.field_set(members);
if let Some(at) = w.slots.get(path.as_str()).copied() {
w.specs.push((at, set, spec::PRIM));
}
for property in &prim.properties {
write_property_spec(w, property, &path);
}
for variant_set in &prim.variant_sets {
write_variant_set_spec(w, variant_set, &path);
}
for child in &prim.children {
write_prim_spec(w, child, &path);
}
}
fn write_variant_set_spec(w: &mut Writer, set: &UsdVariantSet, prim_path: &str) {
let set_path = format!("{prim_path}{{{}=}}", set.name);
let names: Vec<UsdValue> = set
.variants
.iter()
.map(|(n, _)| UsdValue::Token(n.clone()))
.collect();
let rep = token_vector(w, &names);
let members = vec![w.field("variantChildren", rep) as i32];
let field_set = w.field_set(members);
if let Some(at) = w.slots.get(set_path.as_str()).copied() {
w.specs.push((at, field_set, spec::VARIANT_SET));
}
for (choice, body) in &set.variants {
let variant_path = format!("{prim_path}{{{}={choice}}}", set.name);
write_variant_spec(w, body, &variant_path);
}
}
fn write_variant_spec(w: &mut Writer, body: &UsdPrim, path: &str) {
let mut members = Vec::new();
if !body.type_name.is_empty() {
let token = w.token(&body.type_name);
let rep = inlined(kind::TOKEN, token as u64);
members.push(w.field("typeName", rep) as i32);
}
for (name, rep) in arc_fields(w, &body.metadata) {
members.push(w.field(name, rep) as i32);
}
for (key, value) in &body.metadata {
if key.is_empty() || is_arc_name(split_qualifier(key).1) {
continue;
}
if let Some(rep) = value_rep(w, key, value, "") {
members.push(w.field(key, rep) as i32);
}
}
if !body.properties.is_empty() {
let names: Vec<UsdValue> = body
.properties
.iter()
.map(|p| UsdValue::Token(p.name.clone()))
.collect();
let rep = token_vector(w, &names);
members.push(w.field("properties", rep) as i32);
}
if !body.children.is_empty() {
let names: Vec<UsdValue> = body
.children
.iter()
.map(|c| UsdValue::Token(c.name.clone()))
.collect();
let rep = token_vector(w, &names);
members.push(w.field("primChildren", rep) as i32);
}
let field_set = w.field_set(members);
if let Some(at) = w.slots.get(path).copied() {
w.specs.push((at, field_set, spec::VARIANT));
}
for property in &body.properties {
write_property_spec(w, property, path);
}
for child in &body.children {
write_prim_spec(w, child, path);
}
}
fn write_property_spec(w: &mut Writer, property: &UsdProperty, prim_path: &str) {
let path = format!("{prim_path}.{}", property.name);
let mut members = Vec::new();
if !property.type_name.is_empty() {
let token = w.token(&property.type_name);
let rep = inlined(kind::TOKEN, token as u64);
members.push(w.field("typeName", rep) as i32);
}
if property.uniform || property.relationship {
let rep = inlined(kind::VARIABILITY, 1);
members.push(w.field("variability", rep) as i32);
}
if property.relationship {
if let Some(rep) =
path_list_op_with(w, &property.value, list_op_header(&property.qualifier))
{
members.push(w.field("targetPaths", rep) as i32);
}
} else if property.value.samples().is_some() {
if let Some(rep) = time_samples(w, &property.value, &property.type_name) {
members.push(w.field("timeSamples", rep) as i32);
}
} else if matches!(property.value, UsdValue::Path(_)) {
if let Some(rep) = path_list_op(w, &property.value) {
members.push(w.field("connectionPaths", rep) as i32);
}
} else if !matches!(property.value, UsdValue::None)
&& let Some(rep) = value_rep(w, "default", &property.value, &property.type_name)
{
members.push(w.field("default", rep) as i32);
}
for (key, value) in &property.metadata {
if key.is_empty() {
continue;
}
if let Some(rep) = value_rep(w, key, value, "") {
members.push(w.field(key, rep) as i32);
}
}
let set = w.field_set(members);
if let Some(at) = w.slots.get(path.as_str()).copied() {
w.specs.push((
at,
set,
if property.relationship {
spec::RELATIONSHIP
} else {
spec::ATTRIBUTE
},
));
}
}
fn jump_for(has_child: bool, has_sibling: bool, subtree: usize) -> i32 {
match (has_child, has_sibling) {
(true, true) => subtree as i32,
(true, false) => -1,
(false, true) => 0,
(false, false) => -2,
}
}
fn inlined(kind: u8, payload: u64) -> u64 {
IS_INLINED | ((kind as u64) << 48) | (payload & ((1 << 48) - 1))
}
fn referenced(kind: u8, at: u64, array: bool) -> u64 {
let mut rep = ((kind as u64) << 48) | (at & ((1 << 48) - 1));
if array {
rep |= IS_ARRAY;
}
rep
}
fn token_vector(w: &mut Writer, names: &[UsdValue]) -> u64 {
let indices: Vec<u32> = names
.iter()
.map(|v| w.token(v.as_str().unwrap_or_default()))
.collect();
let mut bytes = (indices.len() as u64).to_le_bytes().to_vec();
for index in indices {
bytes.extend_from_slice(&index.to_le_bytes());
}
referenced(kind::TOKEN_VECTOR, w.intern(bytes), false)
}
fn arc_fields(w: &mut Writer, metadata: &[(String, UsdValue)]) -> Vec<(&'static str, u64)> {
let mut groups: Vec<(String, Vec<(u8, UsdValue)>)> = Vec::new();
for (key, value) in metadata {
let (qualifier, bare) = split_qualifier(key);
if !is_arc_name(bare) {
continue;
}
let bit = list_op_bit(qualifier);
match groups.iter_mut().find(|(name, _)| name == bare) {
Some((_, parts)) => parts.push((bit, value.clone())),
None => groups.push((bare.to_string(), vec![(bit, value.clone())])),
}
}
let mut out = Vec::new();
for (bare, parts) in groups {
if let Some(field) = combined_field(w, &bare, &parts) {
out.extend(field);
}
}
out
}
fn split_qualifier(key: &str) -> (&str, &str) {
match key.split_once(' ') {
Some((q, rest)) if matches!(q, "prepend" | "append" | "delete" | "add" | "reorder") => {
(q, rest)
}
_ => ("", key),
}
}
fn is_arc_name(name: &str) -> bool {
matches!(
name,
"references"
| "payload"
| "inherits"
| "specializes"
| "variants"
| "variantSets"
| "subLayers"
| "apiSchemas"
| "nameChildren"
| "properties"
)
}
fn list_op_bit(qualifier: &str) -> u8 {
match qualifier {
"add" => 2,
"delete" => 3,
"reorder" => 4,
"prepend" => 5,
"append" => 6,
_ => 1,
}
}
fn combined_field(
w: &mut Writer,
bare: &str,
parts: &[(u8, UsdValue)],
) -> Option<Vec<(&'static str, u64)>> {
let single = || parts.first().map(|(_, v)| v.clone());
match bare {
"references" => Some(vec![(
"references",
sublists(w, kind::REFERENCE_LIST_OP, parts),
)]),
"payload" => Some(vec![("payload", sublists(w, kind::PAYLOAD_LIST_OP, parts))]),
"inherits" => Some(vec![(
"inheritPaths",
sublists(w, kind::PATH_LIST_OP, parts),
)]),
"specializes" => Some(vec![(
"specializes",
sublists(w, kind::PATH_LIST_OP, parts),
)]),
"apiSchemas" => Some(vec![(
"apiSchemas",
sublists(w, kind::TOKEN_LIST_OP, parts),
)]),
"variantSets" => Some(vec![(
"variantSetNames",
sublists(w, kind::STRING_LIST_OP, parts),
)]),
"variants" => {
let value = single()?;
Some(vec![("variantSelection", variant_selection(w, &value)?)])
}
"nameChildren" => {
let value = single()?;
Some(vec![("primOrder", token_vector(w, &as_items(&value)))])
}
"properties" => {
let value = single()?;
Some(vec![("propertyOrder", token_vector(w, &as_items(&value)))])
}
"subLayers" => {
let value = single()?;
let (layers, _) = string_vector(w, &value);
let offsets: Vec<(f64, f64)> = value
.references()
.into_iter()
.map(|arc| (arc.offset, arc.scale))
.collect();
Some(vec![
("subLayers", layers),
("subLayerOffsets", layer_offsets(w, &offsets)),
])
}
_ => None,
}
}
fn sublists(w: &mut Writer, kind: u8, parts: &[(u8, UsdValue)]) -> u64 {
let mut header = 0u8;
for (bit, _) in parts {
header |= 1 << bit;
if *bit == 1 {
header |= 1;
}
}
let mut bytes = vec![header];
for bit in 1..7u8 {
let Some((_, value)) = parts.iter().find(|(b, _)| *b == bit) else {
continue;
};
match kind {
kind::REFERENCE_LIST_OP | kind::PAYLOAD_LIST_OP => {
let arcs = value.references();
bytes.extend_from_slice(&(arcs.len() as u64).to_le_bytes());
for arc in &arcs {
let asset = w.string(&arc.asset);
let path = w.path_slot(&arc.prim_path);
bytes.extend_from_slice(&asset.to_le_bytes());
bytes.extend_from_slice(&path.to_le_bytes());
bytes.extend_from_slice(&arc.offset.to_le_bytes());
bytes.extend_from_slice(&arc.scale.to_le_bytes());
bytes.extend_from_slice(&0u64.to_le_bytes());
}
}
kind::PATH_LIST_OP => {
let arcs = value.references();
bytes.extend_from_slice(&(arcs.len() as u64).to_le_bytes());
for arc in &arcs {
bytes.extend_from_slice(&w.path_slot(&arc.prim_path).to_le_bytes());
}
}
kind::STRING_LIST_OP => {
let names = as_items(value);
bytes.extend_from_slice(&(names.len() as u64).to_le_bytes());
for name in &names {
let index = w.string(name.as_str().unwrap_or_default());
bytes.extend_from_slice(&index.to_le_bytes());
}
}
_ => {
let names = value.flat_tokens();
bytes.extend_from_slice(&(names.len() as u64).to_le_bytes());
for name in names {
let index = w.token(name);
bytes.extend_from_slice(&index.to_le_bytes());
}
}
}
}
referenced(kind, w.intern(bytes), false)
}
fn layer_offsets(w: &mut Writer, offsets: &[(f64, f64)]) -> u64 {
let mut bytes = (offsets.len() as u64).to_le_bytes().to_vec();
for (offset, scale) in offsets {
bytes.extend_from_slice(&offset.to_le_bytes());
bytes.extend_from_slice(&scale.to_le_bytes());
}
referenced(kind::LAYER_OFFSET_VECTOR, w.intern(bytes), false)
}
fn dictionary(w: &mut Writer, entries: &[(String, UsdValue)]) -> u64 {
let reps: Vec<(u32, u64)> = entries
.iter()
.map(|(key, value)| {
let rep = match value {
UsdValue::Dict(inner) => Some(dictionary(w, inner)),
other => value_rep_in(w, key, other, "", true),
};
(w.string(key), rep)
})
.filter_map(|(key, rep)| rep.map(|rep| (key, rep)))
.collect();
let mut bytes = (reps.len() as u64).to_le_bytes().to_vec();
for (key, rep) in reps {
bytes.extend_from_slice(&key.to_le_bytes());
bytes.extend_from_slice(&8u64.to_le_bytes());
bytes.extend_from_slice(&rep.to_le_bytes());
}
referenced(kind::DICTIONARY, w.intern(bytes), false)
}
fn as_items(value: &UsdValue) -> Vec<UsdValue> {
match value {
UsdValue::Array(items) => items.clone(),
other => vec![other.clone()],
}
}
fn variant_selection(w: &mut Writer, value: &UsdValue) -> Option<u64> {
let UsdValue::Dict(entries) = value else {
return None;
};
let mut bytes = (entries.len() as u64).to_le_bytes().to_vec();
for (set, choice) in entries {
bytes.extend_from_slice(&w.string(set).to_le_bytes());
bytes.extend_from_slice(&w.string(choice.as_str().unwrap_or_default()).to_le_bytes());
}
Some(referenced(
kind::VARIANT_SELECTION_MAP,
w.intern(bytes),
false,
))
}
fn string_vector(w: &mut Writer, value: &UsdValue) -> (u64, usize) {
let names: Vec<String> = match value {
UsdValue::Array(items) => items.iter().filter_map(name_of).collect(),
other => name_of(other).into_iter().collect(),
};
let mut bytes = (names.len() as u64).to_le_bytes().to_vec();
for name in &names {
bytes.extend_from_slice(&w.string(name).to_le_bytes());
}
let count = names.len();
(
referenced(kind::STRING_VECTOR, w.intern(bytes), false),
count,
)
}
fn name_of(value: &UsdValue) -> Option<String> {
match value {
UsdValue::Reference(arc) => Some(arc.asset.clone()),
other => other.as_str().map(str::to_string),
}
}
fn list_op_header(qualifier: &str) -> u8 {
match qualifier {
"add" => 1 << 2,
"delete" => 1 << 3,
"reorder" => 1 << 4,
"prepend" => 1 << 5,
"append" => 1 << 6,
_ => 0b11,
}
}
fn path_list_op(w: &mut Writer, value: &UsdValue) -> Option<u64> {
path_list_op_with(w, value, 0b11)
}
fn path_list_op_with(w: &mut Writer, value: &UsdValue, header: u8) -> Option<u64> {
let targets: Vec<&str> = match value {
UsdValue::Path(p) => vec![p.as_str()],
UsdValue::Array(items) => items.iter().filter_map(|v| v.as_str()).collect(),
_ => return None,
};
if targets.is_empty() {
return None;
}
let indices: Vec<u32> = targets
.iter()
.filter_map(|p| w.slots.get(*p).copied())
.collect();
if indices.is_empty() {
return None;
}
let mut bytes = vec![header];
bytes.extend_from_slice(&(indices.len() as u64).to_le_bytes());
for index in indices {
bytes.extend_from_slice(&index.to_le_bytes());
}
Some(referenced(kind::PATH_LIST_OP, w.intern(bytes), false))
}
fn time_samples(w: &mut Writer, value: &UsdValue, type_name: &str) -> Option<u64> {
let samples = value.samples()?;
let times: Vec<f64> = samples.iter().map(|(t, _)| *t).collect();
let reps: Vec<u64> = samples
.iter()
.map(|(_, v)| value_rep(w, "default", v, type_name).unwrap_or(0))
.collect();
let mut time_bytes = (times.len() as u64).to_le_bytes().to_vec();
for time in × {
time_bytes.extend_from_slice(&time.to_le_bytes());
}
let times_rep = referenced(48, w.intern(time_bytes), false);
let section = 8 + times.len() * 8 + 8;
let mut bytes = (section as u64).to_le_bytes().to_vec();
bytes.extend_from_slice(&(times.len() as u64).to_le_bytes());
for time in × {
bytes.extend_from_slice(&time.to_le_bytes());
}
bytes.extend_from_slice(×_rep.to_le_bytes());
bytes.extend_from_slice(&8u64.to_le_bytes());
bytes.extend_from_slice(&(reps.len() as u64).to_le_bytes());
for rep in reps {
bytes.extend_from_slice(&rep.to_le_bytes());
}
Some(referenced(kind::TIME_SAMPLES, w.intern(bytes), false))
}
fn value_rep(w: &mut Writer, key: &str, value: &UsdValue, type_name: &str) -> Option<u64> {
value_rep_in(w, key, value, type_name, false)
}
fn value_rep_in(
w: &mut Writer,
key: &str,
value: &UsdValue,
type_name: &str,
in_dictionary: bool,
) -> Option<u64> {
match key {
"primChildren" | "properties" => {
if let UsdValue::Array(items) = value {
return Some(token_vector(w, items));
}
}
_ => {}
}
if let UsdValue::Dict(entries) = value {
return Some(dictionary(w, entries));
}
if matches!(value, UsdValue::Block) {
return Some(inlined(kind::VALUE_BLOCK, 0));
}
let (kind, array) = match kind_of(type_name) {
Some(found) => found,
None if in_dictionary => infer(value)?,
None => match known_field_type(key).and_then(kind_of) {
Some(found) => found,
None => infer(value)?,
},
};
if array {
return Some(write_array(w, kind, value));
}
if let Some(text) = value.as_str() {
return Some(match kind {
kind::STRING => {
let index = w.string(text);
inlined(kind::STRING, index as u64)
}
kind::PATH_EXPRESSION => {
let index = w.string(text);
w.needs(0, 10, 0);
let at = w.intern(index.to_le_bytes().to_vec());
at | ((kind::PATH_EXPRESSION as u64) << 48)
}
kind::ASSET_PATH => {
let token = w.token(text);
inlined(kind::ASSET_PATH, token as u64)
}
_ => {
let token = w.token(text);
inlined(kind::TOKEN, token as u64)
}
});
}
if let UsdValue::Path(_) = value {
return path_list_op(w, value);
}
if let Some(payload) = inline_payload(kind, value) {
return Some(inlined(kind, payload));
}
let (lanes, width) = shape(kind)?;
let mut bytes = Vec::new();
write_numbers(&mut bytes, kind, value, lanes, width);
Some(referenced(kind, w.intern(bytes), false))
}
fn inline_payload(kind: u8, value: &UsdValue) -> Option<u64> {
Some(match kind {
kind::BOOL => match value {
UsdValue::Bool(v) => *v as u64,
other => (other.as_f64()? != 0.0) as u64,
},
kind::UCHAR => as_i128(value)? as u8 as u64,
kind::INT | kind::UINT => as_i128(value)? as i64 as i32 as u32 as u64,
kind::INT64 | kind::UINT64 => {
let v = as_i128(value)?;
i32::try_from(v).ok()? as u32 as u64
}
kind::HALF => f32_to_half(value.as_f64()? as f32) as u64,
kind::FLOAT => (value.as_f64()? as f32).to_bits() as u64,
kind::DOUBLE => {
let v = value.as_f64()?;
let narrow = v as f32;
if narrow as f64 != v {
return None;
}
narrow.to_bits() as u64
}
_ => return None,
})
}
fn as_i128(value: &UsdValue) -> Option<i128> {
match value {
UsdValue::Int(v) => Some(*v),
UsdValue::Bool(v) => Some(*v as i128),
other => other.as_f64().map(|v| v as i128),
}
}
fn known_field_type(key: &str) -> Option<&'static str> {
Some(match key {
"defaultPrim" | "upAxis" | "kind" | "interpolation" | "visibility" => "token",
"metersPerUnit" | "timeCodesPerSecond" | "framesPerSecond" | "startTimeCode"
| "endTimeCode" => "double",
"documentation" | "comment" => "string",
"elementSize" => "int",
"active" | "hidden" | "instanceable" => "bool",
_ => return None,
})
}
fn infer(value: &UsdValue) -> Option<(u8, bool)> {
Some(match value {
UsdValue::Bool(_) => (kind::BOOL, false),
UsdValue::Int(_) => (kind::INT, false),
UsdValue::Float(_) => (kind::DOUBLE, false),
UsdValue::String(_) => (kind::STRING, false),
UsdValue::Token(_) => (kind::TOKEN, false),
UsdValue::Asset(_) => (kind::ASSET_PATH, false),
UsdValue::Path(_) => (kind::PATH_LIST_OP, false),
UsdValue::Tuple(items) => match items.len() {
2 => (kind::VEC2D, false),
3 => (kind::VEC3D, false),
4 => (kind::VEC4D, false),
9 => (kind::MATRIX3D, false),
16 => (kind::MATRIX4D, false),
_ => return None,
},
UsdValue::Array(items) => match items.first() {
None => (kind::TOKEN_VECTOR, false),
Some(UsdValue::Asset(_)) => (kind::ASSET_PATH, true),
Some(UsdValue::String(_)) => (kind::STRING, true),
Some(UsdValue::Token(_)) => (kind::TOKEN_VECTOR, false),
Some(UsdValue::Int(_)) => (kind::INT, true),
Some(UsdValue::Bool(_)) => (kind::BOOL, true),
Some(UsdValue::Float(_)) => (kind::DOUBLE, true),
Some(UsdValue::Tuple(lanes)) => (
match lanes.len() {
2 => kind::VEC2D,
3 => kind::VEC3D,
4 => kind::VEC4D,
9 => kind::MATRIX3D,
16 => kind::MATRIX4D,
_ => return None,
},
true,
),
_ => return None,
},
_ => return None,
})
}
fn write_array(w: &mut Writer, kind: u8, value: &UsdValue) -> u64 {
let empty = Vec::new();
let items = match value {
UsdValue::Array(items) => items,
UsdValue::None => &empty,
other => std::slice::from_ref(other),
};
if matches!(kind, kind::TOKEN | kind::STRING | kind::ASSET_PATH) {
let indices: Vec<u32> = items
.iter()
.map(|v| {
let text = v.as_str().unwrap_or_default();
if matches!(kind, kind::STRING | kind::ASSET_PATH) {
w.string(text)
} else {
w.token(text)
}
})
.collect();
let mut bytes = (indices.len() as u64).to_le_bytes().to_vec();
for index in indices {
bytes.extend_from_slice(&index.to_le_bytes());
}
return referenced(kind, w.intern(bytes), true);
}
let Some((lanes, width)) = shape(kind) else {
return referenced(kind, w.intern(0u64.to_le_bytes().to_vec()), true);
};
let mut bytes = (items.len() as u64).to_le_bytes().to_vec();
for item in items {
write_numbers(&mut bytes, kind, item, lanes, width);
}
referenced(kind, w.intern(bytes), true)
}
fn flat_f64(value: &UsdValue) -> Vec<f64> {
match value {
UsdValue::Tuple(items) | UsdValue::Array(items) => {
items.iter().flat_map(flat_f64).collect()
}
other => other.as_f64().into_iter().collect(),
}
}
fn write_numbers(out: &mut Vec<u8>, kind: u8, value: &UsdValue, lanes: usize, width: usize) {
if is_float_kind(kind) {
let mut components = flat_f64(value);
if is_quat(kind) && components.len() == 4 {
components.rotate_left(1);
}
components.resize(lanes, 0.0);
for value in components {
match width {
2 => out.extend_from_slice(&f32_to_half(value as f32).to_le_bytes()),
4 => out.extend_from_slice(&(value as f32).to_le_bytes()),
_ => out.extend_from_slice(&value.to_le_bytes()),
}
}
return;
}
let mut components = flat_i128(value);
components.resize(lanes, 0);
for value in components {
match width {
1 => out.push(value as u8),
2 => out.extend_from_slice(&(value as i16).to_le_bytes()),
4 => out.extend_from_slice(&(value as i32).to_le_bytes()),
_ => out.extend_from_slice(&(value as i64).to_le_bytes()),
}
}
}
fn flat_i128(value: &UsdValue) -> Vec<i128> {
match value {
UsdValue::Tuple(items) | UsdValue::Array(items) => {
items.iter().flat_map(flat_i128).collect()
}
other => as_i128(other).into_iter().collect(),
}
}
fn assemble(w: Writer) -> Vec<u8> {
let Writer {
tokens,
strings,
fields,
field_sets,
path_elements,
path_jumps,
specs,
data,
needs_version,
..
} = w;
let version = VERSION.max(needs_version);
let mut out = vec![0u8; BOOTSTRAP];
out[..8].copy_from_slice(b"PXR-USDC");
out[8] = version.0;
out[9] = version.1;
out[10] = version.2;
out.extend_from_slice(&data);
let mut sections: Vec<(&str, u64, u64)> = Vec::new();
let mut section = |out: &mut Vec<u8>, name: &'static str, body: Vec<u8>| {
while !out.len().is_multiple_of(8) {
out.push(0);
}
let at = out.len() as u64;
out.extend_from_slice(&body);
sections.push((name, at, body.len() as u64));
};
let mut text = Vec::new();
for token in &tokens {
text.extend_from_slice(token.as_bytes());
text.push(0);
}
let packed = lz4::compress(&text);
let mut body = Vec::new();
body.extend_from_slice(&(tokens.len() as u64).to_le_bytes());
body.extend_from_slice(&(text.len() as u64).to_le_bytes());
body.extend_from_slice(&(packed.len() as u64).to_le_bytes());
body.extend_from_slice(&packed);
section(&mut out, "TOKENS", body);
let mut body = (strings.len() as u64).to_le_bytes().to_vec();
for index in &strings {
body.extend_from_slice(&index.to_le_bytes());
}
section(&mut out, "STRINGS", body);
let mut body = (fields.len() as u64).to_le_bytes().to_vec();
write_u32_array(
&mut body,
&fields
.iter()
.map(|(name, _)| *name as i32)
.collect::<Vec<_>>(),
);
let mut reps = Vec::with_capacity(fields.len() * 8);
for (_, rep) in &fields {
reps.extend_from_slice(&rep.to_le_bytes());
}
let packed = lz4::compress(&reps);
body.extend_from_slice(&(packed.len() as u64).to_le_bytes());
body.extend_from_slice(&packed);
section(&mut out, "FIELDS", body);
let mut body = (field_sets.len() as u64).to_le_bytes().to_vec();
write_u32_array(&mut body, &field_sets);
section(&mut out, "FIELDSETS", body);
let indexes: Vec<i32> = (1..=path_elements.len() as i32).collect();
let mut body = (path_elements.len() as u64 + 1).to_le_bytes().to_vec();
body.extend_from_slice(&(path_elements.len() as u64).to_le_bytes());
write_u32_array(&mut body, &indexes);
write_u32_array(&mut body, &path_elements);
write_u32_array(&mut body, &path_jumps);
section(&mut out, "PATHS", body);
let mut body = (specs.len() as u64).to_le_bytes().to_vec();
write_u32_array(
&mut body,
&specs.iter().map(|(p, _, _)| *p as i32).collect::<Vec<_>>(),
);
write_u32_array(
&mut body,
&specs.iter().map(|(_, s, _)| *s as i32).collect::<Vec<_>>(),
);
write_u32_array(
&mut body,
&specs.iter().map(|(_, _, t)| *t as i32).collect::<Vec<_>>(),
);
section(&mut out, "SPECS", body);
while !out.len().is_multiple_of(8) {
out.push(0);
}
let toc_at = out.len() as u64;
out.extend_from_slice(&(sections.len() as u64).to_le_bytes());
for (name, at, size) in §ions {
let mut padded = [0u8; 16];
padded[..name.len()].copy_from_slice(name.as_bytes());
out.extend_from_slice(&padded);
out.extend_from_slice(&at.to_le_bytes());
out.extend_from_slice(&size.to_le_bytes());
}
out[16..24].copy_from_slice(&toc_at.to_le_bytes());
let _ = (encode_u32, encode_u64, encodable_u64);
out
}
#[cfg(test)]
mod half_tests {
use super::super::crate_read;
use super::*;
#[test]
fn half_rounds_to_nearest() {
for (value, expected) in [
(0.333f32, 0.333008f32),
(0.1, 0.099976),
(-0.333, -0.333008),
(1.0 / 3.0, 0.333252),
] {
let back = crate_read::half_to_f32_for_test(f32_to_half(value));
assert!(
(back - expected).abs() < 1e-6,
"{value} became {back}, expected {expected}"
);
}
}
#[test]
fn exact_values_are_unchanged() {
for value in [0.0f32, 1.0, -1.0, 2.0, 0.5, -0.5, 65504.0, -65504.0] {
let back = crate_read::half_to_f32_for_test(f32_to_half(value));
assert_eq!(back, value, "{value} did not survive");
}
}
#[test]
fn the_error_is_never_worse_than_half_precision() {
let mut worst = 0.0f32;
for i in -2000..2000 {
let value = i as f32 * 0.01;
let back = crate_read::half_to_f32_for_test(f32_to_half(value));
let tolerance = value.abs() * 0.00049 + 1e-7;
let error = (back - value).abs();
assert!(error <= tolerance, "{value} became {back}");
worst = worst.max(error / (value.abs() + 1e-7));
}
assert!(worst < 0.000489, "worst relative error {worst}");
assert!(
worst > 0.0001,
"suspiciously good — is the sweep hitting anything?"
);
}
#[test]
fn a_carry_out_of_the_mantissa_is_not_lost() {
let back = crate_read::half_to_f32_for_test(f32_to_half(0.99999));
assert_eq!(back, 1.0);
assert!(crate_read::half_to_f32_for_test(f32_to_half(1e30)).is_infinite());
}
}
#[cfg(test)]
mod tests {
use super::super::value::UsdValue;
use super::super::{crate_read, parse::parse, write::layer_to_usda};
use super::*;
fn round_trip(source: &str) -> UsdLayer {
let original = parse(source).expect("the source parses");
let bytes = write(&original);
let back = crate_read::read(&bytes).expect("what we wrote, we can read");
compare_prims(&original.prims, &back.prims, "");
for (key, value) in &original.metadata {
let mine = back
.meta(key)
.unwrap_or_else(|| panic!("layer metadata {key} was lost"));
compare_values(value, mine, key);
}
let again = crate_read::read(&write(&back)).expect("stable");
assert_eq!(
layer_to_usda(&again),
layer_to_usda(&back),
"a second round trip changed the layer"
);
back
}
fn compare_prims(want: &[UsdPrim], got: &[UsdPrim], at: &str) {
assert_eq!(
want.iter().map(|p| &p.name).collect::<Vec<_>>(),
got.iter().map(|p| &p.name).collect::<Vec<_>>(),
"children of {at:?}"
);
for (want, got) in want.iter().zip(got) {
let path = format!("{at}/{}", want.name);
assert_eq!(want.type_name, got.type_name, "type of {path}");
assert_eq!(want.specifier, got.specifier, "specifier of {path}");
assert_eq!(
want.properties.iter().map(|p| &p.name).collect::<Vec<_>>(),
got.properties.iter().map(|p| &p.name).collect::<Vec<_>>(),
"properties of {path}"
);
assert_eq!(
want.variant_sets
.iter()
.map(|s| &s.name)
.collect::<Vec<_>>(),
got.variant_sets.iter().map(|s| &s.name).collect::<Vec<_>>(),
"variant sets of {path}"
);
for (want, got) in want.properties.iter().zip(&got.properties) {
let where_ = format!("{path}.{}", want.name);
assert_eq!(want.type_name, got.type_name, "type of {where_}");
assert_eq!(want.uniform, got.uniform, "uniform of {where_}");
assert_eq!(want.relationship, got.relationship, "rel of {where_}");
compare_values_of_type(&want.value, &got.value, &where_, &want.type_name);
}
compare_prims(&want.children, &got.children, &path);
}
}
fn compare_values(want: &UsdValue, got: &UsdValue, at: &str) {
compare_values_of_type(want, got, at, "")
}
fn compare_values_of_type(want: &UsdValue, got: &UsdValue, at: &str, type_name: &str) {
match (want.samples(), got.samples()) {
(Some(want), Some(got)) => {
assert_eq!(want.len(), got.len(), "sample count of {at}");
for (a, b) in want.iter().zip(got) {
assert_eq!(a.0, b.0, "sample time of {at}");
compare_values_of_type(&a.1, &b.1, at, type_name);
}
return;
}
(None, None) => {}
_ => panic!("{at} changed whether it is animated"),
}
if let Some(text) = want.as_str() {
assert_eq!(got.as_str(), Some(text), "{at}");
return;
}
let (want, got) = (want.flat_f32(), got.flat_f32());
assert_eq!(want.len(), got.len(), "number count of {at}");
let base = type_name.strip_suffix("[]").unwrap_or(type_name);
let relative = if base.starts_with("half") || base.ends_with('h') {
5e-4
} else {
1e-6
};
for (a, b) in want.iter().zip(&got) {
if a == b || (a.is_nan() && b.is_nan()) {
continue;
}
assert!(
(a - b).abs() <= a.abs() * relative + 1e-6,
"{at}: {a} became {b} (tolerance {relative})"
);
}
}
#[test]
fn a_triangle_survives() {
round_trip(
r#"#usda 1.0
(
defaultPrim = "Root"
metersPerUnit = 0.01
upAxis = "Y"
)
def Xform "Root"
{
def Mesh "Tri"
{
int[] faceVertexCounts = [3]
int[] faceVertexIndices = [0, 1, 2]
point3f[] points = [(0, 0, 0), (1, 0, 0), (0, 1, 0)]
uniform token subdivisionScheme = "none"
}
}
"#,
);
}
#[test]
fn the_documents_this_crate_already_reads_survive() {
round_trip(include_str!("testdata/rich.usda"));
round_trip(include_str!("testdata/anim.usda"));
round_trip(include_str!("testdata/wide.usda"));
round_trip(include_str!("testdata/roundtrip.usda"));
}
#[test]
fn arcs_round_trip() {
let layer = round_trip(include_str!("testdata/comp/arcs.usda"));
let a = layer.prim_at("/A").expect("A");
let refs = a.meta("references").expect("references").references();
assert_eq!(refs.len(), 3);
assert_eq!(
(refs[0].asset.as_str(), refs[0].prim_path.as_str()),
("asset.usda", "/Chair")
);
assert_eq!(
(refs[1].asset.as_str(), refs[1].prim_path.as_str()),
("other.usda", "")
);
assert_eq!(
(refs[2].asset.as_str(), refs[2].prim_path.as_str()),
("", "/A/Local")
);
let payload = a.meta("payload").expect("payload").references();
assert_eq!(
(payload[0].asset.as_str(), payload[0].prim_path.as_str()),
("heavy.usda", "/Deep")
);
assert_eq!(
a.meta("specializes").unwrap().references()[0].prim_path,
"/A/Base"
);
let b = layer
.prim_at("/B")
.unwrap()
.meta("references")
.unwrap()
.references();
assert_eq!((b[0].offset, b[0].scale), (10.0, 2.0), "the layer offset");
}
#[test]
fn variants_round_trip() {
let layer = round_trip(include_str!("testdata/comp/asset.usda"));
let chair = layer.prim_at("/Chair").expect("Chair");
let set = chair
.variant_sets
.iter()
.find(|s| s.name == "look")
.expect("the look set");
let mut names: Vec<&str> = set.variants.iter().map(|(n, _)| n.as_str()).collect();
names.sort();
assert_eq!(names, vec!["oak", "steel"]);
assert_eq!(
set.get("steel")
.unwrap()
.value("primvars:displayColor")
.unwrap()
.flat_f32(),
vec![0.7, 0.7, 0.75]
);
assert_eq!(
chair.meta("inherits").unwrap().references()[0].prim_path,
"/Furniture"
);
}
#[test]
fn a_pipeline_still_composes_after_being_written_as_crates() {
use super::super::compose::{compose, ComposeOptions, MemoryResolver};
let mut binary = MemoryResolver::new();
for (name, text) in [
("asset.usda", include_str!("testdata/comp/asset.usda")),
("base.usda", include_str!("testdata/comp/base.usda")),
] {
binary.insert(name, write(&parse(text).unwrap()));
}
let shot = write(&parse(include_str!("testdata/comp/shot.usda")).unwrap());
let root = crate_read::read(&shot).unwrap();
let composed = compose(&root, "shot.usda", &binary, &ComposeOptions::default()).unwrap();
let chair = composed
.prim_at("/Room/Chair1")
.expect("the chair composed");
assert_eq!(chair.type_name, "Xform");
assert_eq!(
chair.value("xformOp:translate").unwrap().flat_f32(),
vec![9.0, 1.0, 0.0]
);
assert_eq!(chair.value("purpose").unwrap().as_str(), Some("render"));
assert_eq!(
chair.value("primvars:displayColor").unwrap().flat_f32(),
vec![0.7, 0.7, 0.75],
"the shot's variant selection still wins"
);
assert!(composed.prim_at("/Room/Chair1/Seat").is_some());
}
#[test]
fn the_awkward_cases_survive() {
let layer = round_trip(include_str!("testdata/stress.usda"));
let types = layer.prim_at("/Root/Types").expect("Types");
let h = types.value("h").unwrap().flat_f32()[0];
assert!((h - 0.3330078).abs() < 1e-7, "0.333 became {h}");
assert_eq!(
types.value("huger").unwrap(),
&UsdValue::Int(18446744073709551615)
);
assert_eq!(
types.value("huge").unwrap(),
&UsdValue::Int(-9223372036854775808)
);
assert_eq!(types.value("neg").unwrap(), &UsdValue::Int(-2147483648));
assert_eq!(
types.value("d").unwrap().as_f64(),
Some(1.7976931348623157e308)
);
assert_eq!(types.value("big").unwrap(), &UsdValue::Int(4294967295));
let empty = layer.prim_at("/Root/Empty").unwrap();
assert_eq!(empty.value("points").unwrap().flat_f32(), Vec::<f32>::new());
assert!(empty.property("faceVertexCounts").is_some());
let multi = layer.prim_at("/Root/Multi").unwrap();
let mut sets: Vec<&str> = multi.variant_sets.iter().map(|s| s.name.as_str()).collect();
sets.sort();
assert_eq!(sets, vec!["lod", "look"]);
let many = types.value("many").unwrap().flat_tokens();
assert_eq!(many.len(), 3, "{many:?}");
let keyed = layer.prim_at("/Root/Keyed").unwrap();
let samples = keyed.value("xformOp:translate").unwrap().samples().unwrap();
assert_eq!(samples[0].0, -12.0);
assert_eq!(samples[2].0, 0.5);
}
#[test]
fn the_awkward_cases_read_the_same_from_openusds_own_crate() {
let theirs = crate_read::read(include_bytes!("testdata/stress.usdc")).expect("reads");
let ours = parse(include_str!("testdata/stress.usda")).unwrap();
compare_prims(&ours.prims, &theirs.prims, "");
}
#[test]
fn a_large_mesh_survives() {
const N: usize = 40_000;
let points: Vec<UsdValue> = (0..N)
.map(|i| {
let f = i as f64;
UsdValue::Tuple(vec![
UsdValue::Float(f * 0.001),
UsdValue::Float((f * 0.7).sin()),
UsdValue::Float(-f * 0.002),
])
})
.collect();
let indices: Vec<UsdValue> = (0..N)
.map(|i| UsdValue::Int(((i * 7919) % N) as i128))
.collect();
let mut prim = UsdPrim {
specifier: Specifier::Def,
type_name: "Mesh".into(),
name: "Big".into(),
metadata: Vec::new(),
properties: Vec::new(),
children: Vec::new(),
variant_sets: Vec::new(),
};
for (name, type_name, value) in [
("points", "point3f[]", UsdValue::Array(points.clone())),
(
"faceVertexIndices",
"int[]",
UsdValue::Array(indices.clone()),
),
] {
prim.properties.push(UsdProperty {
qualifier: String::new(),
name: name.into(),
type_name: type_name.into(),
uniform: false,
relationship: false,
value,
metadata: Vec::new(),
});
}
let layer = UsdLayer {
metadata: Vec::new(),
prims: vec![prim],
};
let bytes = write(&layer);
let back = crate_read::read(&bytes).expect("a large crate reads");
let big = back.prim_at("/Big").expect("Big");
let got = big.value("points").unwrap().flat_f32();
assert_eq!(got.len(), N * 3);
for (i, expected) in points.iter().enumerate() {
let expected = expected.flat_f32();
for lane in 0..3 {
let (a, b) = (expected[lane], got[i * 3 + lane]);
assert!((a - b).abs() < 1e-6, "point {i} lane {lane}: {a} vs {b}");
}
}
let got = big.value("faceVertexIndices").unwrap().flat_u32();
let want: Vec<u32> = (0..N).map(|i| ((i * 7919) % N) as u32).collect();
assert_eq!(got, want);
}
#[test]
fn api_schemas_survive_as_a_list_operation() {
let layer = round_trip(include_str!("testdata/apple.usda"));
let root = layer.prim_at("/Root").expect("Root");
assert_eq!(
root.meta("apiSchemas").map(UsdValue::flat_tokens),
Some(vec!["Preliminary_AnchoringAPI"]),
"metadata: {:?}",
root.metadata
);
assert!(
root.meta_exact("prepend apiSchemas").is_some(),
"the `prepend` was dropped: {:?}",
root.metadata
);
assert_eq!(
layer
.prim_at("/Root/Body")
.unwrap()
.meta("apiSchemas")
.map(UsdValue::flat_tokens),
Some(vec!["Preliminary_PhysicsColliderAPI"])
);
}
#[test]
fn apples_ar_schemas_survive() {
let layer = round_trip(include_str!("testdata/apple.usda"));
assert_eq!(
layer
.prim_at("/Root")
.unwrap()
.value("preliminary:anchoring:type")
.unwrap()
.as_str(),
Some("plane")
);
assert_eq!(
layer.prim_at("/Root/Gravity").unwrap().type_name,
"Preliminary_PhysicsGravitationalForce"
);
assert_eq!(
layer
.prim_at("/Root/Gravity")
.unwrap()
.value("physics:gravitationalForce:acceleration")
.unwrap()
.flat_f32(),
vec![0.0, -9.8, 0.0]
);
let tap = layer.prim_at("/Root/Tap").expect("the behaviour");
assert_eq!(
tap.value("preliminary:behavior:triggers").unwrap().as_str(),
Some("/Root/Tap/Trigger")
);
assert_eq!(
layer
.prim_at("/Root/Tap/Trigger")
.unwrap()
.value("info:id")
.unwrap()
.as_str(),
Some("TapGesture")
);
assert_eq!(
layer
.prim_at("/Root/Label")
.unwrap()
.value("content")
.unwrap()
.as_str(),
Some("hello")
);
}
#[test]
fn apples_ar_schemas_read_from_openusds_own_crate() {
let theirs = crate_read::read(include_bytes!("testdata/apple.usdc")).expect("reads");
assert_eq!(
theirs
.prim_at("/Root")
.unwrap()
.meta("apiSchemas")
.map(UsdValue::flat_tokens),
Some(vec!["Preliminary_AnchoringAPI"]),
"apiSchemas was lost reading OpenUSD's own file"
);
assert_eq!(
theirs
.prim_at("/Root/Label")
.unwrap()
.value("height")
.unwrap()
.flat_f32(),
vec![0.1]
);
}
#[test]
fn dictionaries_survive() {
let layer = round_trip(include_str!("testdata/clips/stage.usda"));
let UsdValue::Dict(outer) = layer
.prim_at("/Shot")
.unwrap()
.meta("clips")
.expect("clips")
else {
panic!("expected a dictionary");
};
assert_eq!(outer.len(), 1);
assert_eq!(outer[0].0, "default");
let UsdValue::Dict(inner) = &outer[0].1 else {
panic!("nested dictionary");
};
let field = |name: &str| inner.iter().find(|(k, _)| k == name).map(|(_, v)| v);
assert_eq!(field("primPath").and_then(|v| v.as_str()), Some("/Cache"));
assert_eq!(
field("active").map(UsdValue::flat_f32),
Some(vec![0.0, 0.0, 2.0, 1.0])
);
assert_eq!(
field("assetPaths").map(|v| v.flat_tokens()),
Some(vec!["clip_0.usda", "clip_1.usda"])
);
assert_eq!(
field("manifestAssetPath").and_then(|v| v.as_str()),
Some("manifest.usda")
);
}
#[test]
fn a_relationships_qualifier_survives() {
let layer = round_trip(include_str!("testdata/instancer.usda"));
let prototypes = layer
.prim_at("/World/Scatter")
.unwrap()
.property("prototypes")
.expect("the relationship");
assert!(prototypes.relationship);
assert_eq!(prototypes.qualifier, "prepend");
assert_eq!(prototypes.value.flat_tokens().len(), 2);
}
#[test]
fn the_whole_grammar_survives() {
let layer = round_trip(include_str!("testdata/grammar.usda"));
assert!(layer
.meta("comment")
.unwrap()
.as_str()
.unwrap()
.contains("doc comment"));
assert!(layer
.meta("documentation")
.unwrap()
.as_str()
.unwrap()
.contains("triple-quoted"));
let sub = layer.meta("subLayers").unwrap().references();
assert_eq!((sub[0].offset, sub[0].scale), (5.0, 2.0));
let root = layer.prim_at("/Root").expect("Root");
assert_eq!(
root.meta_exact("add references").unwrap().references()[0].prim_path,
"/Thing"
);
assert!(
root.meta_exact("delete references").is_some(),
"{:?}",
root.metadata
);
assert_eq!(
root.meta_exact("reorder nameChildren")
.unwrap()
.flat_tokens(),
vec!["B", "A"]
);
let attr = root.property("customAttr").expect("customAttr");
assert!(attr.metadata.iter().any(|(k, _)| k == "custom"));
let UsdValue::Dict(custom_data) = root.meta("customData").unwrap() else {
panic!("a dictionary");
};
let nested = custom_data.iter().find(|(k, _)| k == "nested").unwrap();
let UsdValue::Dict(inner) = &nested.1 else {
panic!("nested")
};
assert!(
matches!(inner[0].1, UsdValue::Token(_)),
"a `token` declaration makes a token, not a string: {:?}",
inner[0].1
);
for (name, qualifier) in [
("listRel", "prepend"),
("listRel2", "append"),
("listRel3", "delete"),
("listRel4", "reorder"),
] {
assert_eq!(
root.property(name)
.unwrap_or_else(|| panic!("{name}"))
.qualifier,
qualifier
);
}
let set = root
.variant_sets
.iter()
.find(|s| s.name == "style")
.expect("style");
assert_eq!(
set.get("plain")
.unwrap()
.value("inVariant")
.unwrap()
.flat_f32(),
vec![1.0]
);
assert_eq!(set.get("plain").unwrap().children[0].name, "OnlyInPlain");
}
#[test]
fn relocations_survive_a_crate_round_trip() {
let layer = round_trip(include_str!("testdata/gaps/reloc2.usda"));
let UsdValue::Dict(entries) = layer.meta("relocates").expect("relocates") else {
panic!("expected a map");
};
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].0, "/Holder/OldName");
assert_eq!(entries[0].1.as_str(), Some("/Holder/NewName"));
}
#[test]
fn a_membership_expression_survives_and_lifts_the_version() {
let source = include_str!("testdata/gaps/pexpr.usda");
let layer = round_trip(source);
let prim = layer.prim_at("/World").expect("World");
assert_eq!(
prim.value("collection:a:membershipExpression")
.and_then(|v| v.as_str().map(str::to_owned)),
Some("/Alpha//*".to_string())
);
assert_eq!(
prim.value("collection:b:membershipExpression")
.and_then(|v| v.as_str().map(str::to_owned)),
Some("/Beta//*".to_string())
);
let bytes = super::write(&parse(source).unwrap());
assert_eq!((bytes[8], bytes[9], bytes[10]), (0, 10, 0));
let plain = super::write(&parse("#usda 1.0\n\ndef Scope \"A\" {}\n").unwrap());
assert_eq!((plain[8], plain[9], plain[10]), (0, 8, 0));
}
#[test]
fn a_time_code_is_not_inlined() {
let layer = round_trip(include_str!("testdata/block/strong.usda"));
assert_eq!(
layer
.prim_at("/Root")
.unwrap()
.value("when")
.unwrap()
.flat_f32(),
vec![24.0]
);
}
#[test]
fn a_relationship_keeps_its_target() {
let layer = round_trip(
r#"#usda 1.0
def Mesh "M"
{
rel material:binding = </Mat>
}
def Material "Mat"
{
}
"#,
);
let binding = layer
.prim_at("/M")
.unwrap()
.property("material:binding")
.unwrap();
assert!(binding.relationship);
assert_eq!(binding.value.as_str(), Some("/Mat"));
}
#[test]
fn a_target_with_no_prim_behind_it_is_still_written() {
let layer = round_trip(
r#"#usda 1.0
def Mesh "M"
{
rel material:binding = </Nowhere/At/All>
}
"#,
);
assert_eq!(
layer
.prim_at("/M")
.unwrap()
.value("material:binding")
.unwrap()
.as_str(),
Some("/Nowhere/At/All")
);
}
#[test]
fn animation_survives() {
let layer = round_trip(include_str!("testdata/anim.usda"));
let samples = layer
.prim_at("/Spin")
.unwrap()
.value("xformOp:translate")
.unwrap()
.samples()
.expect("still animated");
assert_eq!(samples.len(), 3);
assert_eq!(samples[2].0, 48.0);
assert_eq!(samples[2].1.flat_f32(), vec![10.0, 10.0, 0.0]);
}
#[test]
fn an_empty_layer_is_a_valid_file() {
let bytes = write(&UsdLayer::default());
let info = super::super::usdc::info(&bytes).expect("a readable bootstrap");
assert_eq!(info.version, (0, 8, 0));
assert_eq!(info.sections.len(), 6);
assert!(crate_read::read(&bytes).unwrap().prims.is_empty());
}
#[test]
fn repeated_structure_is_interned() {
let mut source = String::from("#usda 1.0\n");
for i in 0..200 {
source.push_str(&format!(
"def Mesh \"M{i}\"\n{{\n int[] faceVertexCounts = [3]\n \
uniform token subdivisionScheme = \"none\"\n}}\n"
));
}
let layer = parse(&source).unwrap();
let bytes = write(&layer);
assert!(
bytes.len() < 6000,
"200 near-identical prims took {} bytes",
bytes.len()
);
assert_eq!(crate_read::read(&bytes).unwrap().prims.len(), 200);
}
}
#[cfg(test)]
mod external {
use super::super::parse::parse;
use super::*;
#[test]
#[ignore = "writes files for external validation"]
fn write_crates_for_usdcat() {
let dir = std::env::var("USD_OUT").unwrap_or_else(|_| "/tmp".into());
if let Ok(list) = std::env::var("USD_CONVERT") {
for path in list.split(',').filter(|p| !p.is_empty()) {
let text = std::fs::read_to_string(path).expect("readable");
let layer = parse(&text).expect("parses");
let out = path.replace(".usda", ".usdc");
std::fs::write(&out, write(&layer)).expect("writable");
println!("wrote {out}");
}
return;
}
for (name, source) in [
("arcs", include_str!("testdata/comp/arcs.usda")),
("variants", include_str!("testdata/comp/asset.usda")),
("shot", include_str!("testdata/comp/shot.usda")),
("base", include_str!("testdata/comp/base.usda")),
("rich", include_str!("testdata/rich.usda")),
("anim", include_str!("testdata/anim.usda")),
("wide", include_str!("testdata/wide.usda")),
("roundtrip", include_str!("testdata/roundtrip.usda")),
] {
let layer = parse(source).expect("parses");
let bytes = write(&layer);
let path = format!("{dir}/ours_{name}.usdc");
std::fs::write(&path, &bytes).unwrap();
println!("wrote {path} ({} bytes)", bytes.len());
}
}
}