Skip to content

Commit

Permalink
Add crc method to SHPK for calculating it's value, add test for selector
Browse files Browse the repository at this point in the history
  • Loading branch information
redstrate committed Apr 21, 2024
1 parent 58c3e15 commit cc729e3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ hmac-sha512 = "1"
# used while rust doesn't have native benchmarking capability
brunch = { version = "0.5.3", default-features = false }

# used for testing our jamcrc implementation
crc = "3"

[features]
default = ["visual_data"]
Expand Down Expand Up @@ -67,4 +65,7 @@ half = { version = "2", optional = true }
bitflags = { version = "1.3", optional = true }

# needed for dxt/bc decompression
texture2ddecoder = { version = "0.0.5", optional = true }
texture2ddecoder = { version = "0.0.5", optional = true }

# used for testing our jamcrc implementation and currently SHPK
crc = "3"
40 changes: 35 additions & 5 deletions src/shpk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::io::{Cursor, SeekFrom};

use crate::ByteSpan;
use binrw::{binread, BinRead};
use crc::{Algorithm, Crc};

#[binread]
#[br(little, import {
Expand Down Expand Up @@ -68,11 +69,12 @@ pub struct MaterialParameter {
}

#[binread]
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
#[repr(C)]
#[allow(unused)]
pub struct Key {
id: u32,
default_value: u32,
pub id: u32,
pub default_value: u32,
}

#[binread]
Expand Down Expand Up @@ -171,7 +173,7 @@ pub struct ShaderPackage {
#[br(count = scene_key_count)]
scene_keys: Vec<Key>,
#[br(count = material_key_count)]
material_keys: Vec<Key>,
pub material_keys: Vec<Key>,

sub_view_key1_default: u32,
sub_view_key2_default: u32,
Expand All @@ -186,6 +188,12 @@ pub struct ShaderPackage {
node_aliases: Vec<NodeAlias>,
}

const SELECTOR_MULTIPLER: u32 = 31;

// TODO: replace use of crc crate here
const CRC_32_TEST: Algorithm<u32> = Algorithm { width: 32, poly: 0x04c11db7, init: 0x00000000, refin: true, refout: true, xorout: 0x00000000, check: 0x765e7680, residue: 0xc704dd7b };
const JAMCR: Crc<u32> = Crc::<u32>::new(&CRC_32_TEST);

impl ShaderPackage {
/// Reads an existing SHPK file
pub fn from_existing(buffer: ByteSpan) -> Option<ShaderPackage> {
Expand Down Expand Up @@ -241,17 +249,22 @@ impl ShaderPackage {

for key in keys {
selector = selector.wrapping_add(key.wrapping_mul(multiplier));
multiplier = multiplier.wrapping_mul(31);
multiplier = multiplier.wrapping_mul(SELECTOR_MULTIPLER);
}

selector
}

pub fn crc(str: &str) -> u32 {
return JAMCR.checksum(str.as_bytes());
}
}

#[cfg(test)]
mod tests {
use std::fs::read;
use std::path::PathBuf;
use crate::repository::Category::Shader;

use super::*;

Expand All @@ -264,4 +277,21 @@ mod tests {
// Feeding it invalid data should not panic
ShaderPackage::from_existing(&read(d).unwrap());
}

#[test]
fn test_crc() {
assert_eq!(ShaderPackage::crc("PASS_0"), 0xC5A5389C);
assert_eq!(ShaderPackage::crc("DecodeDepthBuffer"), 0x2C6C023C);
}

#[test]
fn test_selector() {
let selector = ShaderPackage::build_selector_from_all_keys(
&[],
&[ShaderPackage::crc("TransformViewSkin"), ShaderPackage::crc("GetAmbientLight_SH"), ShaderPackage::crc("GetReflectColor_Texture"), ShaderPackage::crc("GetAmbientOcclusion_None"), ShaderPackage::crc("ApplyDitherClipOff")],
&[3756477356, 1556481461, 1111668802, 428675533],
&[ShaderPackage::crc("Default"), ShaderPackage::crc("SUB_VIEW_MAIN")]);

assert_eq!(selector, 0x1075AE91);
}
}

0 comments on commit cc729e3

Please sign in to comment.