From 35726def3ef473d665b72109f0b52279568a84dd Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 9 Apr 2025 18:30:51 +0200 Subject: [PATCH] Add CPU matching option and improve default behavior handling --- schemsearch-cli/src/main.rs | 8 ++++++++ schemsearch-common/src/lib.rs | 16 ++++++++++++++++ schemsearch-lib/Cargo.toml | 3 +-- schemsearch-lib/src/lib.rs | 30 +++--------------------------- schemsearch-lib/src/search.rs | 4 ++-- schemsearch-ocl-matcher/src/lib.rs | 4 ++-- 6 files changed, 32 insertions(+), 33 deletions(-) diff --git a/schemsearch-cli/src/main.rs b/schemsearch-cli/src/main.rs index 21e4eef..ef84a02 100755 --- a/schemsearch-cli/src/main.rs +++ b/schemsearch-cli/src/main.rs @@ -168,6 +168,13 @@ fn main() { .default_value("50") .value_parser(|s: &str| s.parse::().map_err(|e| e.to_string())), ) + .arg( + Arg::new("cpu") + .help("Force CPU Matching, skip OpenCL checks") + .short('c') + .long("force-cpu") + .action(ArgAction::SetTrue), + ) .about("Searches for a pattern in a schematic") .bin_name("schemsearch"); @@ -214,6 +221,7 @@ fn main() { ignore_entities: matches.get_flag("ignore-entities"), threshold: *matches.get_one::("threshold").expect("Couldn't get threshold"), invalid_nbt: matches.get_flag("invalid-nbt"), + use_cpu: matches.get_flag("use-cpu"), }; let pattern = match matches.get_one::("pattern") { diff --git a/schemsearch-common/src/lib.rs b/schemsearch-common/src/lib.rs index 11d5b82..8c0b1e8 100644 --- a/schemsearch-common/src/lib.rs +++ b/schemsearch-common/src/lib.rs @@ -9,6 +9,22 @@ pub struct SearchBehavior { pub ignore_entities: bool, pub threshold: f32, pub invalid_nbt: bool, + pub use_cpu: bool, +} + +impl Default for SearchBehavior { + fn default() -> Self { + SearchBehavior { + ignore_block_data: false, + ignore_block_entities: false, + ignore_air: false, + air_as_any: false, + ignore_entities: false, + threshold: 0.9, + invalid_nbt: false, + use_cpu: false, + } + } } #[derive(Debug, Clone, Copy, Default, Deserialize, Serialize)] diff --git a/schemsearch-lib/Cargo.toml b/schemsearch-lib/Cargo.toml index 2eb56cf..114bce1 100755 --- a/schemsearch-lib/Cargo.toml +++ b/schemsearch-lib/Cargo.toml @@ -7,10 +7,9 @@ license = "AGPL-3.0-or-later" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -serde = { version = "1.0.160", features = ["derive"] } schemsearch-files = { path = "../schemsearch-files" } schemsearch-common = { path = "../schemsearch-common" } schemsearch-ocl-matcher = { path = "../schemsearch-ocl-matcher" } named-binary-tag = "0.6" libmath = "0.2.1" -lazy_static = "1.4.0" +lazy_static = "1.4.0" \ No newline at end of file diff --git a/schemsearch-lib/src/lib.rs b/schemsearch-lib/src/lib.rs index 5003922..2e33cbd 100755 --- a/schemsearch-lib/src/lib.rs +++ b/schemsearch-lib/src/lib.rs @@ -84,15 +84,7 @@ mod tests { let schematic = SpongeSchematic::load(&PathBuf::from("../tests/simple.schem")).unwrap(); let endstone = SpongeSchematic::load(&PathBuf::from("../tests/endstone.schem")).unwrap(); - let _ = search(schematic, &endstone, SearchBehavior { - ignore_block_data: true, - ignore_block_entities: true, - ignore_entities: true, - ignore_air: false, - air_as_any: false, - threshold: 0.9, - invalid_nbt: false - }); + let _ = search(schematic, &endstone, SearchBehavior::default()); } #[test] @@ -100,15 +92,7 @@ mod tests { let schematic = SpongeSchematic::load(&PathBuf::from("../tests/Random.schem")).unwrap(); let pattern = SpongeSchematic::load(&PathBuf::from("../tests/Pattern.schem")).unwrap(); - let matches = search(schematic, &pattern, SearchBehavior { - ignore_block_data: true, - ignore_block_entities: true, - ignore_entities: true, - ignore_air: false, - air_as_any: false, - threshold: 0.9, - invalid_nbt: false - }); + let matches = search(schematic, &pattern, SearchBehavior::default()); assert_eq!(matches.len(), 1); assert_eq!(matches[0].x, 1); @@ -122,15 +106,7 @@ mod tests { let schematic = SpongeSchematic::load(&PathBuf::from("../tests/warships/GreyFly-by-Bosslar.schem")).unwrap(); let pattern = SpongeSchematic::load(&PathBuf::from("../tests/gray_castle_complex.schem")).unwrap(); - let matches = search(schematic, &pattern, SearchBehavior { - ignore_block_data: false, - ignore_block_entities: false, - ignore_entities: false, - ignore_air: false, - air_as_any: false, - threshold: 0.9, - invalid_nbt: false - }); + let matches = search(schematic, &pattern, SearchBehavior::default()); assert_eq!(matches.len(), 1); } diff --git a/schemsearch-lib/src/search.rs b/schemsearch-lib/src/search.rs index dca7391..f8b0838 100755 --- a/schemsearch-lib/src/search.rs +++ b/schemsearch-lib/src/search.rs @@ -40,8 +40,8 @@ pub fn search( let schem_width = schem.width as usize; let schem_height = schem.height as usize; let schem_length = schem.length as usize; - - if ocl_available() { + + if !search_behavior.use_cpu && ocl_available() { return ocl_search(schem_data.as_slice(), [schem_width, schem_height, schem_length], pattern_schem.block_data.as_slice(), [pattern_width, pattern_height, pattern_length], *air_id, search_behavior).unwrap() } diff --git a/schemsearch-ocl-matcher/src/lib.rs b/schemsearch-ocl-matcher/src/lib.rs index 967c858..d52a502 100644 --- a/schemsearch-ocl-matcher/src/lib.rs +++ b/schemsearch-ocl-matcher/src/lib.rs @@ -1,5 +1,5 @@ use std::sync::OnceLock; -use ocl::{Buffer, MemFlags, ProQue, Platform}; +use ocl::{Buffer, MemFlags, ProQue, core}; use ocl::SpatialDims::Three; use schemsearch_common::{Match, SearchBehavior}; use math::round::ceil; @@ -9,7 +9,7 @@ const KERNEL: &str = include_str!("kernel.cl"); static PRO_QUEU_CELL: OnceLock = OnceLock::new(); pub fn ocl_available() -> bool { - !Platform::list().is_empty() + core::default_platform().is_ok() } pub fn ocl_search(