Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions fixtures/integer_compressed_floats.usda
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#usda 1.0

# Regression fixture for USDC's `i` (integer-compressed float array) encoding.
# Generated with OpenUSD `usdcat -o integer_compressed_floats.usdc`.
def Scope "IntegerCompressedFloats"
{
float[] weights = [
1.0, 0.0, 1.0, 0.0,
0.0, 1.0, 0.0, 1.0,
1.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 1.0,
]
}
Binary file added fixtures/integer_compressed_floats.usdc
Binary file not shown.
18 changes: 18 additions & 0 deletions src/usdc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,24 @@ mod tests {
Ok(())
}

#[test]
fn test_read_integer_compressed_float_array() -> Result<()> {
// OpenUSD serialises an all-integral float array with the `i` code:
// its LZ4 payload still needs Usd_IntegerCompression decoding.
let data = read_file("fixtures/integer_compressed_floats.usdc")?;
let weights = data
.get_field(&sdf::path("/IntegerCompressedFloats.weights")?, "default")?
.into_owned()
.try_as_float_vec()
.unwrap();

assert_eq!(
weights,
vec![1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0,]
);
Ok(())
}

#[test]
fn test_read_halfs() -> Result<()> {
let data = read_file("fixtures/floats.usdc")?;
Expand Down
32 changes: 30 additions & 2 deletions src/usdc/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,9 +662,13 @@ impl<R: io::Read + io::Seek> CrateFile<R> {
let code = self.reader.read_pod::<u8>()?;

match code {
// Compressed integers
// Compressed integers. Pixar's `_ReadCompressedInts` runs the
// LZ4 block through `Usd_IntegerCompression` decoding after
// decompression, so the payload must be integer-decoded
// (`read_encoded_ints`), not reinterpreted as raw `i32`s
// straight out of LZ4.
b'i' => {
let ints: Vec<i32> = self.read_compressed(count)?;
let ints: Vec<i32> = self.read_encoded_ints(count)?;
ints.into_iter().map(|i| cast(i).unwrap()).collect()
}
// Lookup table and indexes
Expand Down Expand Up @@ -1546,6 +1550,30 @@ mod tests {
use super::*;
use std::fs;

#[test]
fn integer_compressed_float_fixture_uses_i_encoding() -> Result<()> {
let mut file = CrateFile::open(fs::File::open("fixtures/integer_compressed_floats.usdc")?)?;
let value = file
.fields
.iter()
.find_map(|field| {
let ty = field.value_rep.ty().ok()?;
(file.tokens[field.token_index] == "default"
&& ty == Type::Float
&& field.value_rep.is_array()
&& field.value_rep.is_compressed())
.then_some(field.value_rep)
})
.expect("fixture must contain one compressed float default value");

file.set_position(value.payload())?;
let (count, compressed) = file.unpack_array_len(value, ArrayKind::Floats)?;
assert_eq!(count, 16);
assert!(compressed);
assert_eq!(file.reader.read_pod::<u8>()?, b'i');
Ok(())
}

#[test]
fn test_read_crate_struct() {
let path = "./vendor/usd-wg-assets/full_assets/ElephantWithMonochord/SoC-ElephantWithMonochord.usdc";
Expand Down