mirror of
https://github.com/redstrate/Physis.git
synced 2025-04-20 11:47:46 +00:00
Add crc method to SHPK for calculating it's value, add test for selector
This commit is contained in:
parent
58c3e15e2c
commit
cc729e38d8
2 changed files with 39 additions and 8 deletions
|
@ -29,8 +29,6 @@ hmac-sha512 = "1"
|
||||||
# used while rust doesn't have native benchmarking capability
|
# used while rust doesn't have native benchmarking capability
|
||||||
brunch = { version = "0.5.3", default-features = false }
|
brunch = { version = "0.5.3", default-features = false }
|
||||||
|
|
||||||
# used for testing our jamcrc implementation
|
|
||||||
crc = "3"
|
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["visual_data"]
|
default = ["visual_data"]
|
||||||
|
@ -67,4 +65,7 @@ half = { version = "2", optional = true }
|
||||||
bitflags = { version = "1.3", optional = true }
|
bitflags = { version = "1.3", optional = true }
|
||||||
|
|
||||||
# needed for dxt/bc decompression
|
# 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
src/shpk.rs
40
src/shpk.rs
|
@ -5,6 +5,7 @@ use std::io::{Cursor, SeekFrom};
|
||||||
|
|
||||||
use crate::ByteSpan;
|
use crate::ByteSpan;
|
||||||
use binrw::{binread, BinRead};
|
use binrw::{binread, BinRead};
|
||||||
|
use crc::{Algorithm, Crc};
|
||||||
|
|
||||||
#[binread]
|
#[binread]
|
||||||
#[br(little, import {
|
#[br(little, import {
|
||||||
|
@ -68,11 +69,12 @@ pub struct MaterialParameter {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[binread]
|
#[binread]
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
#[repr(C)]
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub struct Key {
|
pub struct Key {
|
||||||
id: u32,
|
pub id: u32,
|
||||||
default_value: u32,
|
pub default_value: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[binread]
|
#[binread]
|
||||||
|
@ -171,7 +173,7 @@ pub struct ShaderPackage {
|
||||||
#[br(count = scene_key_count)]
|
#[br(count = scene_key_count)]
|
||||||
scene_keys: Vec<Key>,
|
scene_keys: Vec<Key>,
|
||||||
#[br(count = material_key_count)]
|
#[br(count = material_key_count)]
|
||||||
material_keys: Vec<Key>,
|
pub material_keys: Vec<Key>,
|
||||||
|
|
||||||
sub_view_key1_default: u32,
|
sub_view_key1_default: u32,
|
||||||
sub_view_key2_default: u32,
|
sub_view_key2_default: u32,
|
||||||
|
@ -186,6 +188,12 @@ pub struct ShaderPackage {
|
||||||
node_aliases: Vec<NodeAlias>,
|
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 {
|
impl ShaderPackage {
|
||||||
/// Reads an existing SHPK file
|
/// Reads an existing SHPK file
|
||||||
pub fn from_existing(buffer: ByteSpan) -> Option<ShaderPackage> {
|
pub fn from_existing(buffer: ByteSpan) -> Option<ShaderPackage> {
|
||||||
|
@ -241,17 +249,22 @@ impl ShaderPackage {
|
||||||
|
|
||||||
for key in keys {
|
for key in keys {
|
||||||
selector = selector.wrapping_add(key.wrapping_mul(multiplier));
|
selector = selector.wrapping_add(key.wrapping_mul(multiplier));
|
||||||
multiplier = multiplier.wrapping_mul(31);
|
multiplier = multiplier.wrapping_mul(SELECTOR_MULTIPLER);
|
||||||
}
|
}
|
||||||
|
|
||||||
selector
|
selector
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn crc(str: &str) -> u32 {
|
||||||
|
return JAMCR.checksum(str.as_bytes());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::fs::read;
|
use std::fs::read;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use crate::repository::Category::Shader;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
@ -264,4 +277,21 @@ mod tests {
|
||||||
// Feeding it invalid data should not panic
|
// Feeding it invalid data should not panic
|
||||||
ShaderPackage::from_existing(&read(d).unwrap());
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue