AI media harness for planning, generating, and editing locally
TypeScript
Isolate native handles and shared services
Bitti2b56781e7f22
diff --git a/src-tauri/src/agent/discovery.rs b/src-tauri/src/agent/discovery.rsindex 505aae5..732294d 100644--- a/src-tauri/src/agent/discovery.rs+++ b/src-tauri/src/agent/discovery.rs@@ -18,7 +18,8 @@ pub(super) struct CachedProbe {statuses: Vec<ProviderStatus>,}-pub(super) static PROVIDER_PROBE_CACHE: Mutex<Option<CachedProbe>> = Mutex::new(None);+#[derive(Clone, Default)]+pub(crate) struct ProviderDiscovery { cache: std::sync::Arc<Mutex<Option<CachedProbe>>> }pub(super) const PROVIDER_PROBE_TTL: Duration = Duration::from_secs(120);/// What the answer depends on: the settings for the agent CLIs themselves.@@ -66,9 +67,9 @@ wherestatuses}-pub fn provider_statuses(settings: &BTreeMap<String, ProviderSetting>) -> Vec<ProviderStatus> {+pub fn provider_statuses(discovery: &ProviderDiscovery, settings: &BTreeMap<String, ProviderSetting>) -> Vec<ProviderStatus> {let mut statuses = cached_or_probe(- &PROVIDER_PROBE_CACHE,+ &discovery.cache,provider_probe_key(settings),PROVIDER_PROBE_TTL,|| probe_provider_statuses(settings),diff --git a/src-tauri/src/agent/mod.rs b/src-tauri/src/agent/mod.rsindex 4b5f8dc..96b6d80 100644--- a/src-tauri/src/agent/mod.rs+++ b/src-tauri/src/agent/mod.rs@@ -11,7 +11,7 @@ mod retry;mod providers;pub(crate) use types::*;pub(crate) use runtime::AgentRuntime;-pub(crate) use discovery::provider_statuses;+pub(crate) use discovery::{provider_statuses, ProviderDiscovery};pub(crate) use providers::compatible::compatible_models;pub(crate) use policy::validate_agent_scene_conventions;#[cfg(test)]diff --git a/src-tauri/src/agent/runtime.rs b/src-tauri/src/agent/runtime.rsindex 8a76657..328841f 100644--- a/src-tauri/src/agent/runtime.rs+++ b/src-tauri/src/agent/runtime.rs@@ -6,7 +6,7 @@ use crate::project::{*, validation::validate_and_normalize_config};use crate::project::commands::execute_checked;use crate::project::validation::issue::ValidationIssue;use std::{path::Path, time::Duration};-use super::{providers::*, discovery::*, policy::*, protocol::*, retry::*};+use super::{discovery::*, policy::*, protocol::*, retry::*};pub(super) const DEFAULT_TIMEOUT_SECONDS: u64 = 300;#[derive(Clone, Default)]pub struct AgentRuntime {diff --git a/src-tauri/src/agent/tests.rs b/src-tauri/src/agent/tests.rsindex 6a98028..6a0d366 100644--- a/src-tauri/src/agent/tests.rs+++ b/src-tauri/src/agent/tests.rs@@ -3,9 +3,9 @@ use crate::project::storage::PROJECT_FILE_NAME;use crate::generation::models::h3::validate_agent_shot_settings;use std::{sync::atomic::Ordering, path::PathBuf};use super::*;-use super::{providers::*, providers::options::*, providers::output::*, providers::compatible::*, process::*, discovery::*, protocol::*, policy::*, prompt::*, retry::*};+use super::{providers::*, providers::output::*, providers::compatible::*, process::*, discovery::*, protocol::*, prompt::*, retry::*};use crate::project::*;-use std::{collections::BTreeMap, path::Path, time::Duration, sync::{Mutex,Arc,atomic::AtomicBool}, ffi::OsString};+use std::{collections::BTreeMap, path::Path, time::Duration, sync::{Mutex,Arc,atomic::AtomicBool}};use serde_json::Value;use std::fs;@@ -631,7 +631,7 @@ fn engine_paths_do_not_invalidate_the_agent_probe() {#[test]fn installed_provider_probes_end_in_ready_or_auth_required() {- for status in provider_statuses(&BTreeMap::new()) {+ for status in provider_statuses(&ProviderDiscovery::default(), &BTreeMap::new()) {if status.executable.is_some() {assert!(matches!(status.state, "ready" | "authRequired"),@@ -785,3 +785,4 @@ fn http_turn_cancels_while_waiting_for_a_response() {assert!(result.unwrap_err().contains("cancelled"));assert!(started.elapsed() < Duration::from_secs(5));}+diff --git a/src-tauri/src/app.rs b/src-tauri/src/app.rsindex 05ffce9..1b5069c 100644--- a/src-tauri/src/app.rs+++ b/src-tauri/src/app.rs@@ -42,6 +42,8 @@ pub fn run() {}Ok(())})+ .manage(crate::media::access::MediaAccess::default())+ .manage(agent::ProviderDiscovery::default()).manage(agent::AgentRuntime::default()).manage(slopfab::SlopfabRuntime::default()).manage(weights::WeightDownloads::default())diff --git a/src-tauri/src/commands/generation.rs b/src-tauri/src/commands/generation.rsindex 3097355..8ac7813 100644--- a/src-tauri/src/commands/generation.rs+++ b/src-tauri/src/commands/generation.rs@@ -1,11 +1,11 @@use crate::{diagnostics, slopfab};use crate::project::{*, validation::validate_and_normalize_config};use crate::media::artifacts::*;-use tauri::AppHandle;+use tauri::{AppHandle, Emitter};#[tauri::command]-pub(crate) fn create_reference_video(duration_seconds: f64, config: ProjectConfig) -> Result<String, String> {+pub(crate) fn create_reference_video(state: tauri::State<'_, slopfab::SlopfabRuntime>, duration_seconds: f64, config: ProjectConfig) -> Result<String, String> {let config = validate_and_normalize_config(config)?;- slopfab::create_reference_video(duration_seconds, &config.provider_settings)+ state.references.create_reference_video(duration_seconds, &config.provider_settings)}pub(crate) fn reference_video_header<'a>(request: &'a tauri::ipc::Request<'_>, name: &str) -> Result<&'a str, String> {@@ -14,31 +14,32 @@ pub(crate) fn reference_video_header<'a>(request: &'a tauri::ipc::Request<'_>, n}#[tauri::command]-pub(crate) fn append_reference_video(request: tauri::ipc::Request<'_>) -> Result<(), String> {+pub(crate) fn append_reference_video(state: tauri::State<'_, slopfab::SlopfabRuntime>, request: tauri::ipc::Request<'_>) -> Result<(), String> {let tauri::ipc::InvokeBody::Raw(bytes) = request.body() else { return Err("Reference frames require a binary body.".into()); };let id = reference_video_header(&request, "x-reference-id")?;let width = reference_video_header(&request, "x-reference-width")?.parse().map_err(|_| "Invalid frame width.")?;let height = reference_video_header(&request, "x-reference-height")?.parse().map_err(|_| "Invalid frame height.")?;let timestamp = reference_video_header(&request, "x-reference-time")?.parse().map_err(|_| "Invalid frame timestamp.")?;- slopfab::append_reference_video(id, bytes, width, height, timestamp)+ state.references.append_reference_video(id, bytes, width, height, timestamp)}#[tauri::command]-pub(crate) fn set_reference_video_audio(request: tauri::ipc::Request<'_>) -> Result<(), String> {+pub(crate) fn set_reference_video_audio(state: tauri::State<'_, slopfab::SlopfabRuntime>, request: tauri::ipc::Request<'_>) -> Result<(), String> {let tauri::ipc::InvokeBody::Raw(bytes) = request.body() else { return Err("Reference audio requires a binary body.".into()); };let id = reference_video_header(&request, "x-reference-id")?;let channels = reference_video_header(&request, "x-reference-channels")?.parse().map_err(|_| "Invalid audio channels.")?;let rate = reference_video_header(&request, "x-reference-rate")?.parse().map_err(|_| "Invalid sample rate.")?;- slopfab::set_reference_video_audio(id, bytes, channels, rate)+ state.references.set_reference_video_audio(id, bytes, channels, rate)}#[tauri::command]-pub(crate) fn release_reference_videos(ids: Vec<String>) -> Result<(), String> {- slopfab::release_reference_videos(&ids)+pub(crate) fn release_reference_videos(state: tauri::State<'_, slopfab::SlopfabRuntime>, ids: Vec<String>) -> Result<(), String> {+ state.references.release_reference_videos(&ids)}#[tauri::command]pub(crate) fn resolve_slopfab_plan(+ state: tauri::State<'_, slopfab::SlopfabRuntime>,mut request: slopfab::GenerationRequest,config: ProjectConfig,folder_path: Option<String>,@@ -56,7 +57,7 @@ pub(crate) fn resolve_slopfab_plan(context.clone(),);let result = validate_and_normalize_config(config)- .and_then(|config| slopfab::resolve_plan(&request, &config.provider_settings));+ .and_then(|config| slopfab::resolve_plan(&request, &config.provider_settings, &state.references));match &result {Ok(plan) => diagnostics::info("slopfab",@@ -85,7 +86,12 @@ pub(crate) fn enqueue_slopfab_generation(request.save_latents_path = Some(generated_latent_destination(&folder_path, &request.job_id)?);let job_id = request.job_id.clone();let result = validate_and_normalize_config(config)- .and_then(|config| state.enqueue(app, request, &config.provider_settings));+ .and_then(|config| state.enqueue(std::sync::Arc::new(move |event| {+ match event {+ slopfab::NativeEvent::Job(value) => { let _ = app.emit("slopfab-job", value); }+ slopfab::NativeEvent::Progress(value) => { let _ = app.emit("slopfab-progress", value); }+ }+ }), request, &config.provider_settings));if let Err(error) = &result {diagnostics::error("slopfab",diff --git a/src-tauri/src/commands/media.rs b/src-tauri/src/commands/media.rsindex 40414c4..a3e215e 100644--- a/src-tauri/src/commands/media.rs+++ b/src-tauri/src/commands/media.rs@@ -1,3 +1,4 @@+use crate::media::formats::*;use crate::project::paths::*;use crate::media::{access::*, import::*};use std::{fs, path::PathBuf};@@ -9,7 +10,7 @@ pub(crate) fn choose_initial_reference_images(app: AppHandle) -> Result<Vec<Pend.dialog().file().set_title("Add references to the project brief")- .add_filter("Images", &["png", "jpg", "jpeg", "webp"])+ .add_filter("Images", &image_extensions()).blocking_pick_files().unwrap_or_default();selected@@ -43,7 +44,7 @@ pub(crate) fn choose_reference_image(.dialog().file().set_title("Add an image reference")- .add_filter("Images", &["png", "jpg", "jpeg", "webp"])+ .add_filter("Images", &image_extensions()).blocking_pick_file();let Some(selected) = selected else {return Ok(None);@@ -69,7 +70,7 @@ pub(crate) fn choose_reference_images(.dialog().file().set_title("Add reference images")- .add_filter("Images", &["png", "jpg", "jpeg", "webp"])+ .add_filter("Images", &image_extensions()).blocking_pick_files().unwrap_or_default();if selected.is_empty() {@@ -155,17 +156,17 @@ pub(crate) fn read_project_file(/// What it deliberately does NOT do is trust the caller's string: an argument/// the project does not name is refused whatever it points at.#[tauri::command]-pub(crate) fn read_external_media_file(+pub(crate) fn read_external_media_file(access: tauri::State<'_, MediaAccess>,folder_path: String,source_path: String,) -> Result<tauri::ipc::Response, String> {- external_media_bytes(&folder_path, &source_path).map(tauri::ipc::Response::new)+ external_media_bytes(&access, &folder_path, &source_path).map(tauri::ipc::Response::new)}/// Adds the chosen files to the project: images are copied in, video and audio/// are recorded where they already are. An empty list means the user cancelled.#[tauri::command]-pub(crate) fn import_media_files(+pub(crate) fn import_media_files(access: tauri::State<'_, MediaAccess>,app: AppHandle,folder_path: String,) -> Result<Vec<ImportedMediaFile>, String> {@@ -176,7 +177,7 @@ pub(crate) fn import_media_files(.dialog().file().set_title("Add media to this project")- .add_filter("Media", MEDIA_EXTENSIONS)+ .add_filter("Media", &media_extensions()).blocking_pick_files();let Some(selected) = selected else {return Ok(Vec::new());@@ -187,28 +188,28 @@ pub(crate) fn import_media_files(let source = path.into_path().map_err(|error| format!("Could not access the selected file: {error}"))?;- import_media_file(&source, &project_folder)+ import_media_file(&access, &source, &project_folder)}).collect()}#[tauri::command]-pub(crate) fn choose_reference_video(app: AppHandle, folder_path: String) -> Result<Option<String>, String> {+pub(crate) fn choose_reference_video(access: tauri::State<'_, MediaAccess>, app: AppHandle, folder_path: String) -> Result<Option<String>, String> {let root = project_root(&folder_path)?;let picked = app.dialog().file().set_title("Add a video reference")- .add_filter("MP4 video", &["mp4", "m4v", "mov"]).blocking_pick_file();+ .add_filter("MP4 video", &video_extensions()).blocking_pick_file();let Some(picked) = picked else { return Ok(None); };let path = picked.into_path().map_err(|error| format!("Could not access selected video: {error}"))?;if !path.is_file() { return Err("The selected video does not exist.".into()); }- picked_external_source_path(&path, &root).map(Some)+ picked_external_source_path(&access, &path, &root).map(Some)}#[tauri::command]-pub(crate) fn choose_reference_files(app: AppHandle, folder_path: String) -> Result<Vec<ImportedReference>, String> {+pub(crate) fn choose_reference_files(access: tauri::State<'_, MediaAccess>, app: AppHandle, folder_path: String) -> Result<Vec<ImportedReference>, String> {let root = project_root(&folder_path)?;let selected = app.dialog().file().set_title("Add reference files")- .add_filter("Images, videos and refmods", &["png", "jpg", "jpeg", "webp", "mp4", "m4v", "mov", "safetensors"])+ .add_filter("Images, videos and refmods", &reference_extensions()).blocking_pick_files().unwrap_or_default();let paths = selected.into_iter().map(|path| path.into_path().map_err(|error| format!("Could not access selected file: {error}"))).collect::<Result<Vec<_>, _>>()?;- import_reference_attachments(&root, &paths)+ import_reference_attachments(&access, &root, &paths)}diff --git a/src-tauri/src/commands/runtime.rs b/src-tauri/src/commands/runtime.rsindex fd5aecc..ff83a7b 100644--- a/src-tauri/src/commands/runtime.rs+++ b/src-tauri/src/commands/runtime.rs@@ -73,10 +73,12 @@ pub(crate) fn choose_engine_path(/// every project shares the answer.#[tauri::command]pub(crate) async fn runtime_status(+ discovery: tauri::State<'_, agent::ProviderDiscovery>,settings: BTreeMap<String, ProviderSetting>,) -> Result<RuntimeStatus, String> {+ let discovery = discovery.inner().clone();let result = tauri::async_runtime::spawn_blocking(move || RuntimeStatus {- providers: agent::provider_statuses(&settings),+ providers: agent::provider_statuses(&discovery, &settings),slopfab: slopfab::status(&settings),}).awaitdiff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rsindex 3409e65..05eee8c 100644--- a/src-tauri/src/lib.rs+++ b/src-tauri/src/lib.rs@@ -21,7 +21,6 @@ mod window;pub use app::run;pub use slopfab::{default_dll_path, generate_reference_icon_batch, ReferenceIconBatchConfig, ReferenceIconSpec};-use project::*;#[cfg(test)]mod tests;diff --git a/src-tauri/src/media/access.rs b/src-tauri/src/media/access.rsindex c3085fc..c1f27f6 100644--- a/src-tauri/src/media/access.rs+++ b/src-tauri/src/media/access.rs@@ -16,9 +16,8 @@ use super::import::media_kind_and_mime;/// and read YOUR footage: nothing in the read path could tell which project a/// pick belonged to. Entries are never written to disk and never survive a/// restart; the project file does that.-pub(crate) static PICKED_EXTERNAL_FILES: std::sync::LazyLock<- std::sync::Mutex<BTreeMap<PathBuf, BTreeSet<PathBuf>>>,-> = std::sync::LazyLock::new(Default::default);+#[derive(Default)]+pub(crate) struct MediaAccess { picked: std::sync::Mutex<BTreeMap<PathBuf, BTreeSet<PathBuf>>> }/// The identity a project folder is remembered by. Canonical, so one folder/// spelled two ways is one key. `None` when the folder cannot be resolved, in@@ -28,24 +27,24 @@ pub(crate) fn session_key(project_folder: &Path) -> Option<PathBuf> {project_folder.canonicalize().ok()}-pub(crate) fn remember_picked_file(project_folder: &Path, path: &Path) {+pub(crate) fn remember_picked_file(access: &MediaAccess, project_folder: &Path, path: &Path) {let (Some(key), Ok(canonical)) = (session_key(project_folder), path.canonicalize()) else {return;};// A poisoned lock only means another thread panicked mid-insert; the// fallback is the project file, so there is nothing to escalate here.- if let Ok(mut picked) = PICKED_EXTERNAL_FILES.lock() {+ if let Ok(mut picked) = access.picked.lock() {picked.entry(key).or_default().insert(canonical);}}/// Was this file picked in this run *for this project*? A pick made while a/// different project was open is not an answer here.-pub(crate) fn was_picked_for_project(project_folder: &Path, canonical: &Path) -> bool {+pub(crate) fn was_picked_for_project(access: &MediaAccess, project_folder: &Path, canonical: &Path) -> bool {let Some(key) = session_key(project_folder) else {return false;};- PICKED_EXTERNAL_FILES+ access.picked.lock().map(|picked| {picked@@ -71,9 +70,9 @@ pub(crate) fn external_source_path(source: &Path) -> Result<String, String> {/// project — so its preview works before the project has been saved. Only the/// import paths call this, and they only run on what came back from a native/// picker.-pub(crate) fn picked_external_source_path(source: &Path, project_folder: &Path) -> Result<String, String> {+pub(crate) fn picked_external_source_path(access: &MediaAccess, source: &Path, project_folder: &Path) -> Result<String, String> {let path = external_source_path(source)?;- remember_picked_file(project_folder, source);+ remember_picked_file(access, project_folder, source);Ok(path)}/// Every absolute path this project has recorded — the media and references@@ -93,7 +92,7 @@ pub(crate) fn recorded_external_paths(config: &ProjectConfig) -> Vec<String> {).collect()}-pub(crate) fn external_media_bytes(folder_path: &str, source_path: &str) -> Result<Vec<u8>, String> {+pub(crate) fn external_media_bytes(access: &MediaAccess, folder_path: &str, source_path: &str) -> Result<Vec<u8>, String> {let root = project_root(folder_path)?;let project = read_project(&root)?;let requested = PathBuf::from(&source_path)@@ -107,7 +106,7 @@ pub(crate) fn external_media_bytes(folder_path: &str, source_path: &str) -> Resu.map(|canonical| canonical == requested).unwrap_or(false)});- if !recorded && !was_picked_for_project(&root, &requested) {+ if !recorded && !was_picked_for_project(access, &root, &requested) {return Err(format!("{source_path} is not one of the files this project points at."));diff --git a/src-tauri/src/media/formats.rs b/src-tauri/src/media/formats.rsnew file mode 100644index 0000000..e12bc11--- /dev/null+++ b/src-tauri/src/media/formats.rs@@ -0,0 +1,30 @@+//! Media format classification and explicit per-use policies.+pub(crate) struct Format { pub extension: &'static str, pub kind: &'static str, pub mime: &'static str, pub reference: bool }+pub(crate) const FORMATS: &[Format] = &[+ Format { extension: "mp4", kind: "video", mime: "video/mp4", reference: true },+ Format { extension: "m4v", kind: "video", mime: "video/mp4", reference: true },+ Format { extension: "mov", kind: "video", mime: "video/quicktime", reference: true },+ Format { extension: "webm", kind: "video", mime: "video/webm", reference: false },+ Format { extension: "mkv", kind: "video", mime: "video/x-matroska", reference: false },+ Format { extension: "mp3", kind: "audio", mime: "audio/mpeg", reference: false },+ Format { extension: "m4a", kind: "audio", mime: "audio/mp4", reference: false },+ Format { extension: "aac", kind: "audio", mime: "audio/mp4", reference: false },+ Format { extension: "wav", kind: "audio", mime: "audio/wav", reference: false },+ Format { extension: "flac", kind: "audio", mime: "audio/flac", reference: false },+ Format { extension: "ogg", kind: "audio", mime: "audio/ogg", reference: false },+ Format { extension: "oga", kind: "audio", mime: "audio/ogg", reference: false },+ Format { extension: "png", kind: "image", mime: "image/png", reference: true },+ Format { extension: "jpg", kind: "image", mime: "image/jpeg", reference: true },+ Format { extension: "jpeg", kind: "image", mime: "image/jpeg", reference: true },+ Format { extension: "webp", kind: "image", mime: "image/webp", reference: true },+ Format { extension: "gif", kind: "image", mime: "image/gif", reference: false },+ Format { extension: "safetensors", kind: "refmod", mime: "application/octet-stream", reference: true },+];+pub(crate) fn lookup(extension: &str) -> Option<&'static Format> { FORMATS.iter().find(|format| format.extension == extension) }+pub(crate) fn media_kind_and_mime(extension: &str) -> Option<(&'static str, &'static str)> {+ lookup(extension).filter(|format| format.kind != "refmod").map(|format| (format.kind, format.mime))+}+pub(crate) fn media_extensions() -> Vec<&'static str> { FORMATS.iter().filter(|f| f.kind != "refmod").map(|f| f.extension).collect() }+pub(crate) fn reference_extensions() -> Vec<&'static str> { FORMATS.iter().filter(|f| f.reference).map(|f| f.extension).collect() }+pub(crate) fn image_extensions() -> Vec<&'static str> { FORMATS.iter().filter(|f| f.reference && f.kind == "image").map(|f| f.extension).collect() }+pub(crate) fn video_extensions() -> Vec<&'static str> { FORMATS.iter().filter(|f| f.reference && f.kind == "video").map(|f| f.extension).collect() }diff --git a/src-tauri/src/media/import.rs b/src-tauri/src/media/import.rsindex cbb40f3..0c5cfa7 100644--- a/src-tauri/src/media/import.rs+++ b/src-tauri/src/media/import.rs@@ -1,7 +1,8 @@+pub(crate) use super::formats::media_kind_and_mime;use crate::project::paths::*;use std::{fs, path::{Path, PathBuf}};use serde::Serialize;-use super::access::picked_external_source_path;+use super::access::{MediaAccess, picked_external_source_path};#[derive(Debug, Serialize)]#[serde(rename_all = "camelCase")]pub(crate) struct ImportedReferenceImage {@@ -45,12 +46,8 @@ pub(crate) fn validate_reference_image(source: &Path) -> Result<(String, &'stati.and_then(|value| value.to_str()).map(str::to_ascii_lowercase).ok_or_else(|| "The selected image needs a file extension.".to_string())?;- let mime_type = match extension.as_str() {- "png" => "image/png",- "jpg" | "jpeg" => "image/jpeg",- "webp" => "image/webp",- _ => return Err("Choose a PNG, JPEG, or WebP image.".into()),- };+ let mime_type = super::formats::lookup(&extension).filter(|format| format.kind == "image" && format.reference)+ .map(|format| format.mime).ok_or("Choose a PNG, JPEG, or WebP image.")?;Ok((extension, mime_type))}@@ -59,9 +56,7 @@ pub(crate) fn copy_reference_image(project_folder: &Path,) -> Result<ImportedReferenceImage, String> {let (extension, mime_type) = validate_reference_image(source)?;- let references_folder = project_folder.join("references");- fs::create_dir_all(&references_folder)- .map_err(|error| format!("Could not prepare references folder: {error}"))?;+ let references_folder = ProjectRoot::from_directory(project_folder)?.directory("references")?;let display_name = source.file_stem().and_then(|value| value.to_str())@@ -96,7 +91,7 @@ pub(crate) struct ImportedReference {/// Adds one file to a project as a reference, following the same rule the media/// panel does: pictures are small and get copied so the folder stays portable;/// video and audio are not copied at any size.-pub(crate) fn import_reference_file(+pub(crate) fn import_reference_file(access: &MediaAccess,source: &Path,project_folder: &Path,) -> Result<ImportedReference, String> {@@ -126,44 +121,18 @@ pub(crate) fn import_reference_file(.unwrap_or("Reference").to_string(),relative_path: None,- source_path: Some(picked_external_source_path(source, project_folder)?),+ source_path: Some(picked_external_source_path(access, source, project_folder)?),}),_ => Err("Choose an image, a video, or a sound file.".into()),}}-/// Extensions Slopus accepts, and what each one is. Anything not listed is-/// refused rather than imported as an unknown blob.-pub(crate) fn media_kind_and_mime(extension: &str) -> Option<(&'static str, &'static str)> {- Some(match extension {- "mp4" | "m4v" => ("video", "video/mp4"),- "mov" => ("video", "video/quicktime"),- "webm" => ("video", "video/webm"),- "mkv" => ("video", "video/x-matroska"),- "mp3" => ("audio", "audio/mpeg"),- "m4a" | "aac" => ("audio", "audio/mp4"),- "wav" => ("audio", "audio/wav"),- "flac" => ("audio", "audio/flac"),- "ogg" | "oga" => ("audio", "audio/ogg"),- "png" => ("image", "image/png"),- "jpg" | "jpeg" => ("image", "image/jpeg"),- "webp" => ("image", "image/webp"),- "gif" => ("image", "image/gif"),- _ => return None,- })-}--pub(crate) const MEDIA_EXTENSIONS: &[&str] = &[- "mp4", "m4v", "mov", "webm", "mkv", "mp3", "m4a", "aac", "wav", "flac", "ogg", "oga", "png",- "jpg", "jpeg", "webp", "gif",-];-/// Adds one file to the project. Video and audio are NEVER copied: a folder of/// rushes is tens of gigabytes, and duplicating it to make the project folder/// "portable" costs the user that much disk to gain a copy they did not ask/// for. The project records where the file already lives instead. Images are/// small and stay copied, so a project's own artwork travels with it.-pub(crate) fn import_media_file(source: &Path, project_folder: &Path) -> Result<ImportedMediaFile, String> {+pub(crate) fn import_media_file(access: &MediaAccess, source: &Path, project_folder: &Path) -> Result<ImportedMediaFile, String> {if !source.is_file() {return Err(format!("{} does not exist.", source.to_string_lossy()));}@@ -183,13 +152,11 @@ pub(crate) fn import_media_file(source: &Path, project_folder: &Path) -> Result<.unwrap_or("Imported media").to_string(),relative_path: None,- source_path: Some(picked_external_source_path(source, project_folder)?),+ source_path: Some(picked_external_source_path(access, source, project_folder)?),mime_type: mime_type.into(),});}- let media_folder = project_folder.join("media");- fs::create_dir_all(&media_folder)- .map_err(|error| format!("Could not prepare media folder: {error}"))?;+ let media_folder = ProjectRoot::from_directory(project_folder)?.directory("media")?;let display_name = source.file_stem().and_then(|value| value.to_str())@@ -215,15 +182,12 @@ pub(crate) fn import_media_file(source: &Path, project_folder: &Path) -> Result<})}pub(crate) fn reference_attachment_kind(path: &Path) -> Result<&'static str, String> {- match path.extension().and_then(|ext| ext.to_str()).unwrap_or("").to_ascii_lowercase().as_str() {- "png" | "jpg" | "jpeg" | "webp" => Ok("image"),- "mp4" | "m4v" | "mov" => Ok("video"),- "safetensors" => Ok("refmod"),- _ => Err("Choose an image, MP4/MOV video, or refmod safetensors file.".into()),- }+ let extension = path.extension().and_then(|ext| ext.to_str()).unwrap_or("").to_ascii_lowercase();+ super::formats::lookup(&extension).filter(|format| format.reference).map(|format| format.kind)+ .ok_or_else(|| "Choose an image, MP4/MOV video, or refmod safetensors file.".into())}-pub(crate) fn import_reference_attachments(root: &Path, paths: &[PathBuf]) -> Result<Vec<ImportedReference>, String> {+pub(crate) fn import_reference_attachments(access: &MediaAccess, root: &Path, paths: &[PathBuf]) -> Result<Vec<ImportedReference>, String> {let kinds = paths.iter().map(|path| {if !path.is_file() { return Err("The selected reference file does not exist.".into()); }reference_attachment_kind(path)@@ -232,12 +196,13 @@ pub(crate) fn import_reference_attachments(root: &Path, paths: &[PathBuf]) -> Rereturn Err("Add one video per reference. Use another reference for additional videos.".into());}paths.iter().zip(kinds).map(|(path, kind)| {- if kind == "image" { return import_reference_file(path, root); }+ if kind == "image" { return import_reference_file(access, path, root); }Ok(ImportedReference {kind,name: path.file_name().and_then(|name| name.to_str()).unwrap_or("Reference").into(),relative_path: None,- source_path: Some(picked_external_source_path(path, root)?),+ source_path: Some(picked_external_source_path(access, path, root)?),})}).collect()}+diff --git a/src-tauri/src/media/mod.rs b/src-tauri/src/media/mod.rsindex 48dbdc0..0e6da2b 100644--- a/src-tauri/src/media/mod.rs+++ b/src-tauri/src/media/mod.rs@@ -1,3 +1,5 @@pub(crate) mod import;pub(crate) mod access;pub(crate) mod artifacts;++pub(crate) mod formats;diff --git a/src-tauri/src/project/commands/batch.rs b/src-tauri/src/project/commands/batch.rsindex 4f7c4a3..9bc939d 100644--- a/src-tauri/src/project/commands/batch.rs+++ b/src-tauri/src/project/commands/batch.rs@@ -3,6 +3,7 @@ use crate::project::{ProjectConfig, GenerationJob};use super::{types::*, effects::Effects};/** Apply a complete batch to a clone. Callers validate agent conventions after* this returns; neither a command error nor a validator error mutates input. */+#[cfg(test)]pub fn execute_commands(current: &ProjectConfig,commands: &[ProjectCommand],@@ -72,4 +73,3 @@ pub(super) fn ensure_any<const N: usize>(fields: [bool; N], error: &str) -> Resu.then_some(()).ok_or_else(|| error.into())}-diff --git a/src-tauri/src/project/commands/tests.rs b/src-tauri/src/project/commands/tests.rsindex 9633b53..eeb0a74 100644--- a/src-tauri/src/project/commands/tests.rs+++ b/src-tauri/src/project/commands/tests.rs@@ -1,4 +1,5 @@use super::*;+use super::batch::execute_commands;#[test]fn scene_creation_uses_injected_model_defaults() {@@ -16,7 +17,6 @@ fn scene_creation_uses_injected_model_defaults() {use crate::project::validation::validate_and_normalize_config;use super::clips::find_clip_location;use crate::project::*;-use std::collections::BTreeMap;fn fixture() -> ProjectConfig {serde_json::from_str(include_str!("../../../../fixtures/project-v1-complete.json")).unwrap()diff --git a/src-tauri/src/project/lifecycle.rs b/src-tauri/src/project/lifecycle.rsindex 864c5d8..a1db531 100644--- a/src-tauri/src/project/lifecycle.rs+++ b/src-tauri/src/project/lifecycle.rs@@ -111,7 +111,7 @@ pub(crate) fn create_project_at_with_references(.map_err(|error| format!("Could not create {child} folder: {error}"))?;}for (index, source) in reference_paths.iter().enumerate() {- let imported = import_reference_file(source, &project_folder)?;+ let imported = import_reference_file(&crate::media::access::MediaAccess::default(), source, &project_folder)?;let mut reference_id = format!("ref-initial-{}", index + 1);let mut suffix = 2;while configdiff --git a/src-tauri/src/project/paths.rs b/src-tauri/src/project/paths.rsindex 08f2c78..7dada25 100644--- a/src-tauri/src/project/paths.rs+++ b/src-tauri/src/project/paths.rs@@ -7,6 +7,12 @@ use super::storage::project_file_in;pub(crate) struct ProjectRoot(PathBuf);impl ProjectRoot {+ /// Used while creating a project, before its config file exists.+ pub(crate) fn from_directory(folder: &Path) -> Result<Self, String> {+ let root = folder.canonicalize().map_err(|error| format!("Could not resolve project folder: {error}"))?;+ if !root.is_dir() { return Err("The selected project folder does not exist.".into()); }+ Ok(Self(root))+ }pub(crate) fn open(folder: &str) -> Result<Self, String> {project_root(folder).map(Self)}diff --git a/src-tauri/src/project/storage.rs b/src-tauri/src/project/storage.rsindex 0dbf41c..b490f04 100644--- a/src-tauri/src/project/storage.rs+++ b/src-tauri/src/project/storage.rs@@ -1,13 +1,12 @@use super::{ProjectConfig, ProjectRecord, AgentConversation, validation::validate_and_normalize_config, paths::display_path};use crate::storage::atomic::atomic_replace;-use std::{fs, io, path::{Path, PathBuf}, sync::atomic::{AtomicU64, Ordering}};+use std::{fs, io, path::{Path, PathBuf}};pub(crate) const PROJECT_FILE_NAME: &str = "slopus.json";/// What the file was called before, newest first. Folders written by earlier/// builds still open; the next save writes `slopus.json` and leaves the old/// file alone, so a project stays readable by the build that made it.pub(crate) const LEGACY_PROJECT_FILE_NAMES: [&str; 3] =["polstudio.json", "pols.json", "polstudio.project.json"];-pub(crate) static TEMP_FILE_COUNTER: AtomicU64 = AtomicU64::new(0);/// The settings file to READ from a project folder: the current name if it is/// there, otherwise the first legacy name that is. Returns the current name/// when the folder holds none of them, so the caller reports a missing@@ -63,28 +62,6 @@ wherelet json = serde_json::to_string_pretty(&config).map_err(|error| format!("Could not serialize project: {error}"))?;let destination = folder.join(PROJECT_FILE_NAME);- let sequence = TEMP_FILE_COUNTER.fetch_add(1, Ordering::Relaxed);- let temporary = folder.join(format!(- ".{PROJECT_FILE_NAME}.{}.{}.tmp",- std::process::id(),- sequence- ));- let write_result = (|| -> io::Result<()> {- use io::Write;- let mut file = fs::OpenOptions::new()- .write(true)- .create_new(true)- .open(&temporary)?;- file.write_all(format!("{json}\n").as_bytes())?;- file.sync_all()- })();- if let Err(error) = write_result {- let _ = fs::remove_file(&temporary);- return Err(format!("Could not write project config: {error}"));- }- if let Err(error) = replacer(&temporary, &destination) {- let _ = fs::remove_file(&temporary);- return Err(format!("Could not finalize project config: {error}"));- }- Ok(())+ crate::storage::atomic::write_with_replacer(&destination, format!("{json}\n").as_bytes(), replacer)+ .map_err(|error| format!("Could not write project config: {error}"))}diff --git a/src-tauri/src/project/validation/scenes.rs b/src-tauri/src/project/validation/scenes.rsindex a8c7cb6..4fc5d33 100644--- a/src-tauri/src/project/validation/scenes.rs+++ b/src-tauri/src/project/validation/scenes.rs@@ -4,6 +4,8 @@ use std::collections::BTreeSet;use super::values::*;pub(super) fn validate(config: &mut ProjectConfig) -> Result<(), String> {for job in &mut config.generation_jobs {+ let max_seconds = crate::generation::models::for_scene(job.provider_id.as_deref())+ .unwrap_or_else(crate::generation::models::default_model).capabilities.max_scene_seconds;/* Normalise the scene BEFORE anything is judged, so both layers judgethe same thing. An empty `shots` array collapses to no key at all —the same three-step tidy `normalize_shot_tags` performs, for the same@@ -30,9 +32,9 @@ pub(super) fn validate(config: &mut ProjectConfig) -> Result<(), String> {shot.id, job.id));}- if !shot.start_seconds.is_finite() || !(0.0..=15.0).contains(&shot.start_seconds) {+ if !shot.start_seconds.is_finite() || !(0.0..=max_seconds).contains(&shot.start_seconds) {return Err(format!(- "Shot '{}' in scene '{}' starts at {} seconds, outside the 0-15 second scene.",+ "Shot '{}' in scene '{}' starts at {} seconds, outside the 0-{max_seconds} second scene.",shot.id, job.id, shot.start_seconds));}@@ -44,9 +46,9 @@ pub(super) fn validate(config: &mut ProjectConfig) -> Result<(), String> {job.shots = None;}if let Some(seconds) = job.duration_seconds {- if !seconds.is_finite() || !(0.0..=15.0).contains(&seconds) {+ if !seconds.is_finite() || !(0.0..=max_seconds).contains(&seconds) {return Err(format!(- "Scene '{}' is {seconds} seconds long, outside the 0-15 second range.",+ "Scene '{}' is {seconds} seconds long, outside the 0-{max_seconds} second range.",job.id));}diff --git a/src-tauri/src/rendered.rs b/src-tauri/src/rendered.rsindex c2eb52e..1fac346 100644--- a/src-tauri/src/rendered.rs+++ b/src-tauri/src/rendered.rs@@ -197,7 +197,7 @@ pub fn rgba_from_planar(pub fn from_source(job_id: &str,source: Box<dyn FrameSource>,- audio: &[f32],+ audio: Vec<f32>,frame_count: u32,width: u32,height: u32,@@ -239,7 +239,7 @@ pub fn from_source(frame_count,fps,source,- audio: audio.to_vec(),+ audio,audio_channels,audio_sample_rate,})@@ -278,7 +278,7 @@ fn from_output(from_source(job_id,Box::new(OwnedRgba(rgba)),- audio,+ audio.to_vec(),frame_count,width,height,diff --git a/src-tauri/src/slopfab.rs b/src-tauri/src/slopfab.rsdeleted file mode 100644index 6623125..0000000--- a/src-tauri/src/slopfab.rs+++ /dev/null@@ -1,2301 +0,0 @@-//! Safe host-side boundary for the dynamically loaded slopfab C API.-//!-//! All ABI declarations and `unsafe` calls live in `ffi`; the rest of the-//! application only handles owned Rust status, plan, progress, and metadata.-use crate::{diagnostics, rendered, ProviderOption, ProviderSetting};-use getrandom::fill as fill_random;-use serde::{Deserialize, Serialize};-use std::{- collections::{BTreeMap, HashMap},- path::{Path, PathBuf},- sync::{- atomic::{AtomicBool, Ordering},- mpsc, Arc, Mutex,- },- thread,-};-use tauri::{AppHandle, Emitter};--const DLL_FILE_NAME: &str = "slopfab.dll";-// https://huggingface.co/Viggle/Viggle-Animate/raw/main/assets/fixed_prompt.txt-const ANIMATE_PROMPT: &str = include_str!("../assets/viggle-animate-fixed-prompt.txt");-// SlopFab rounds up to 17*k + 5 frames but rejects Animate plans over 360.-const ANIMATE_MAX_ALIGNED_FRAMES: i32 = (360 - 5) / 17 * 17 + 5;-/// Tauri stages the repository's runtime resources in development and release-/// builds. Installers and portable folders use the same relative layout.-pub fn default_dll_path() -> PathBuf {- let executable = std::env::current_exe().unwrap_or_else(|_| PathBuf::from("Slopus.exe"));- let directory = executable.parent().unwrap_or_else(|| Path::new("."));- // Cargo test executables live one level below the staged resources.- #[cfg(test)]- let directory = if directory.file_name().is_some_and(|name| name == "deps") {- directory.parent().unwrap_or(directory)- } else { directory };- directory.join(DLL_FILE_NAME)-}-const EXPECTED_CAPI_MAJOR: u32 = 1;-const NOT_READY: i32 = -7;-const CANCELLED: i32 = -8;--#[derive(Debug, Clone, Copy, PartialEq, Eq)]-enum ComputePlatform {- Cuda13,- Cuda12,- Vulkan,-}--impl ComputePlatform {- fn label(self) -> &'static str {- match self {- Self::Cuda13 => "CUDA 13",- Self::Cuda12 => "CUDA 12",- Self::Vulkan => "Vulkan",- }- }-- fn backend(self) -> i32 {- match self {- Self::Cuda13 | Self::Cuda12 => 0,- Self::Vulkan => 1,- }- }-}--fn platform_from_cuda_probe(result: Result<i32, String>) -> ComputePlatform {- match result {- Ok(13) => ComputePlatform::Cuda13,- Ok(12) => ComputePlatform::Cuda12,- _ => ComputePlatform::Vulkan,- }-}--fn detect_platform(api: &ffi::Api) -> ComputePlatform {- // A toolkit can be installed on an AMD/Intel machine. Check for a usable- // NVIDIA device before treating the cuBLAS installation as CUDA support.- if !ffi::has_cuda_device() {- return ComputePlatform::Vulkan;- }- // Explicit "auto" makes Slopus's order deterministic even if the host- // process carries SLOPFAB_CUDA_VERSION. If this DLL instance was already- // initialized, the setter is expected to refuse the change and the loaded- // major below remains the authority.- let _ = api.set_cuda_version("auto");- platform_from_cuda_probe(api.cuda_loaded_major())-}--#[derive(Debug, Clone, Serialize)]-#[serde(rename_all = "camelCase")]-pub struct SlopfabStatus {- pub state: &'static str,- pub dll_path: String,- pub version: Option<String>,- pub platform: Option<&'static str>,- pub cuda_available: bool,- pub cuda_device_names: Vec<String>,- pub detail: String,- pub models: Vec<ModelStatus>,-}--#[derive(Debug, Clone, Serialize)]-#[serde(rename_all = "camelCase")]-pub struct ModelStatus {- pub id: &'static str,- pub configured: bool,- pub available: bool,- pub path: Option<String>,-}--#[derive(Debug, Clone, Serialize)]-#[serde(rename_all = "camelCase")]-pub struct GpuDevice {- pub name: String,- pub memory_bytes: u64,-}--pub fn gpu_devices() -> Vec<GpuDevice> { ffi::gpu_devices() }--#[derive(Debug, Clone, Default, Deserialize)]-#[serde(rename_all = "camelCase")]-pub struct GenerationRequest {- pub job_id: String,- pub prompt: String,- pub frames: i32,- #[serde(default)]- pub still_image: bool,- pub steps: i32,- /// -1 asks the host to draw a fresh seed for this generation.- pub seed: i64,- pub canvas_width: i32,- pub canvas_height: i32,- #[serde(default)]- pub reference_paths: Vec<String>,- #[serde(default)]- pub reference_video_ids: Vec<String>,- #[serde(default)]- pub refmods: Vec<RefmodInput>,- #[serde(default)]- pub continuation_relative_path: Option<String>,- // Only the project commands resolve paths; IPC cannot supply save targets.- #[serde(skip)]- pub continuation_path: Option<PathBuf>,- #[serde(skip)]- pub save_latents_path: Option<PathBuf>,-}--#[derive(Debug, Clone, Deserialize, Serialize)]-pub struct RefmodInput {- pub path: String,- pub strength: f32,- pub copies: u32,-}--impl GenerationRequest {- pub fn reference_count(&self) -> usize {- self.reference_paths.len() + self.reference_video_ids.len()- }-}--// A video owns its DLL and input buffers. Commands serialize mutations; the-// C API retains immutable snapshots when a plan/generation attaches a video.-static REFERENCE_VIDEOS: std::sync::LazyLock<Mutex<HashMap<String, ffi::ReferenceVideoHandle>>> =- std::sync::LazyLock::new(Default::default);-static NEXT_REFERENCE_VIDEO: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);--pub fn create_reference_video(duration: f64, settings: &BTreeMap<String, ProviderSetting>) -> Result<String, String> {- let configuration = Configuration::from_settings(settings);- let api = ffi::Api::load(&configuration.dll_path)?;- api.version()?;- let video = ffi::ReferenceVideoHandle::new(api, duration, configuration.dll_path)?;- let mut videos = REFERENCE_VIDEOS.lock().map_err(|_| "Reference video lock failed.")?;- if videos.len() >= 3 { return Err("At most three video references can be prepared at once.".into()); }- let id = NEXT_REFERENCE_VIDEO.fetch_add(1, Ordering::Relaxed).to_string();- videos.insert(id.clone(), video);- Ok(id)-}--pub fn append_reference_video(id: &str, bytes: &[u8], width: i32, height: i32, timestamp: f64) -> Result<(), String> {- REFERENCE_VIDEOS.lock().map_err(|_| "Reference video lock failed.")?- .get_mut(id).ok_or("The prepared reference video no longer exists.")?- .append(bytes, width, height, timestamp)-}--pub fn set_reference_video_audio(id: &str, bytes: &[u8], channels: i32, sample_rate: i32) -> Result<(), String> {- if bytes.len() % 4 != 0 { return Err("Reference audio must contain complete float32 samples.".into()); }- let samples: Vec<f32> = bytes.chunks_exact(4).map(|chunk| f32::from_le_bytes(chunk.try_into().unwrap())).collect();- REFERENCE_VIDEOS.lock().map_err(|_| "Reference video lock failed.")?- .get_mut(id).ok_or("The prepared reference video no longer exists.")?- .set_audio(&samples, channels, sample_rate)-}--pub fn release_reference_videos(ids: &[String]) -> Result<(), String> {- let mut videos = REFERENCE_VIDEOS.lock().map_err(|_| "Reference video lock failed.")?;- for id in ids { videos.remove(id); }- Ok(())-}--#[derive(Debug, Clone, Serialize)]-#[serde(rename_all = "camelCase")]-pub struct ResolvedPlan {- pub canvas_width: i32,- pub canvas_height: i32,- pub aligned_frames: i32,- pub duration_seconds: f64,- pub model_evaluations: i32,- pub sequence_rows_without_text: i32,- pub latent_frames: i32,- pub latent_width: i32,- pub latent_height: i32,- pub description: String,- pub boundary: &'static str,-}--#[derive(Debug, Clone, Serialize)]-#[serde(rename_all = "camelCase")]-struct ProgressEvent {- job_id: String,- stage: &'static str,- step: i32,- total_steps: i32,- planned_steps: i32,- elapsed_seconds: f64,- frames: i32,- canvas_width: i32,- canvas_height: i32,- reference_count: usize,- timing_profile: String,-}--#[derive(Debug, Clone, Serialize)]-#[serde(rename_all = "camelCase")]-struct JobEvent {- job_id: String,- state: &'static str,- detail: String,- output: Option<OutputMetadata>,-}--/// What came back, and what the webview needs in order to encode it: the-/// shape of the pictures now waiting in [`crate::rendered`], and how much sound-/// came with them.-#[derive(Debug, Clone, Serialize)]-#[serde(rename_all = "camelCase")]-struct OutputMetadata {- frames: i32,- width: i32,- height: i32,- fps: f64,- audio_channels: i32,- audio_sample_rate: i32,- /// Interleaved samples across every channel, so the webview can tell an- /// empty soundtrack from one it has not read yet.- audio_samples: usize,- reference_count: usize,- timing_profile: String,- seconds_conditioning: f64,- seconds_denoise: f64,- seconds_video_decode: f64,- seconds_audio_decode: f64,- seconds_total: f64,- steps_computed: i32,- steps_skipped: i32,- duration_seconds: f64,- boundary: &'static str,-}--#[derive(Clone)]-pub struct SlopfabRuntime {- sender: mpsc::Sender<QueueItem>,- cancellations: Arc<Mutex<HashMap<String, Arc<AtomicBool>>>>,-}--impl Default for SlopfabRuntime {- fn default() -> Self {- let (sender, receiver) = mpsc::channel::<QueueItem>();- let cancellations = Arc::new(Mutex::new(HashMap::new()));- let worker_flags = cancellations.clone();- thread::Builder::new().name("slopfab-serial-queue".into()).spawn(move || {- while let Ok(item) = receiver.recv() {- let started = std::time::Instant::now();- diagnostics::info("slopfab", "generation.started", "Native generation worker started.", serde_json::json!({- "jobId": item.request.job_id,- "frames": item.request.frames,- "steps": item.request.steps,- "canvasWidth": item.request.canvas_width,- "canvasHeight": item.request.canvas_height,- "referenceCount": item.request.reference_count(),- "randomSeed": item.request.seed == -1,- }));- let result = run_generation(&item);- let (state, detail, output) = match result {- Ok((metadata, pictures)) => {- /* The pictures wait in memory for the webview, which- encodes them and asks Rust to write the file. What- had to be dropped to make room is said out loud —- an abandoned render is a scene that has to be run- again, and the user is owed that sentence. */- let evicted = rendered::keep(pictures);- let detail = match evicted {- Some(job) if job != item.request.job_id => format!(- "Frames and audio are ready to be encoded. The frames of {job} were dropped to make room and that scene would have to be rendered again."- ),- _ => "Frames and audio are ready to be encoded.".to_string(),- };- ("framesReady", detail, Some(metadata))- }- Err(error) if item.cancel.load(Ordering::Acquire) => ("cancelled", error, None),- Err(error) => ("failed", error, None),- };- let context = serde_json::json!({- "jobId": item.request.job_id,- "state": state,- "elapsedMs": started.elapsed().as_millis().min(u64::MAX as u128) as u64,- "output": output.as_ref().map(|value| serde_json::json!({- "frames": value.frames, "width": value.width, "height": value.height,- "fps": value.fps, "audioChannels": value.audio_channels,- "audioSamples": value.audio_samples,- "secondsConditioning": value.seconds_conditioning,- "secondsDenoise": value.seconds_denoise,- "secondsVideoDecode": value.seconds_video_decode,- "secondsAudioDecode": value.seconds_audio_decode,- "secondsTotal": value.seconds_total,- "stepsComputed": value.steps_computed,- "stepsSkipped": value.steps_skipped,- })),- });- if state == "failed" {- diagnostics::error("slopfab", "generation.finished", &detail, context);- } else if state == "cancelled" {- diagnostics::warn("slopfab", "generation.finished", &detail, context);- } else {- diagnostics::info("slopfab", "generation.finished", &detail, context);- }- let _ = item.app.emit("slopfab-job", JobEvent { job_id: item.request.job_id.clone(), state, detail, output });- let queue_empty = if let Ok(mut flags) = worker_flags.lock() {- flags.remove(&item.request.job_id);- flags.is_empty()- } else {- false- };- // Reuse is scoped to one contiguous batch. Keeping it while a- // queued scene exists avoids repeating conditioning and- // reference preparation; clearing at the drain boundary gives- // the otherwise multi-gigabyte cache a deterministic lifetime.- if queue_empty {- if let Err(error) = clear_reused_models(&item.configuration) {- diagnostics::warn("slopfab", "models.clear_failed", &error, serde_json::json!({}));- }- }- }- }).expect("could not start slopfab queue worker");- Self {- sender,- cancellations,- }- }-}--fn clear_reused_models(configuration: &Configuration) -> Result<(), String> {- let api = ffi::Api::load(&configuration.dll_path)?;- api.version()?;- api.clear_reused_models()-}--impl SlopfabRuntime {- pub fn enqueue(- &self,- app: AppHandle,- mut request: GenerationRequest,- settings: &BTreeMap<String, ProviderSetting>,- ) -> Result<(), String> {- let configuration = Configuration::from_settings(settings);- request.steps = configuration.generation_steps(request.steps)?;- diagnostics::info(- "slopfab",- "generation.enqueue_requested",- "Generation was submitted to the native queue.",- serde_json::json!({- "jobId": request.job_id,- "frames": request.frames,- "steps": request.steps,- "canvasWidth": request.canvas_width,- "canvasHeight": request.canvas_height,- "referenceCount": request.reference_count(),- "inputChars": request.prompt.chars().count(),- }),- );- if request.job_id.trim().is_empty() {- return Err("Generation job id cannot be empty.".into());- }- configuration.validate_inputs(&request)?;- validate_generation_controls(&request)?;- let cancel = Arc::new(AtomicBool::new(false));- let mut flags = self- .cancellations- .lock()- .map_err(|_| "Generation queue lock failed.")?;- if flags.contains_key(&request.job_id) {- return Err("This generation job is already queued.".into());- }- flags.insert(request.job_id.clone(), cancel.clone());- drop(flags);- let queued_id = request.job_id.clone();- if self- .sender- .send(QueueItem {- app: app.clone(),- request,- configuration,- cancel,- })- .is_err()- {- self.cancellations- .lock()- .map_err(|_| "Generation queue lock failed.")?- .remove(&queued_id);- return Err("Generation queue is unavailable.".into());- }- let _ = app.emit(- "slopfab-job",- JobEvent {- job_id: queued_id.clone(),- state: "queued",- detail: "Queued behind any active slopfab generation.".into(),- output: None,- },- );- diagnostics::info(- "slopfab",- "generation.queued",- "Generation entered the native queue.",- serde_json::json!({ "jobId": queued_id }),- );- Ok(())- }-- pub fn cancel(&self, job_id: &str) -> bool {- let accepted = self- .cancellations- .lock()- .ok()- .and_then(|flags| flags.get(job_id).cloned())- .is_some_and(|flag| {- flag.store(true, Ordering::Release);- true- });- diagnostics::info(- "slopfab",- "generation.cancel_requested",- if accepted {- "Cancellation was accepted."- } else {- "No queued generation matched the cancellation."- },- serde_json::json!({- "jobId": job_id, "accepted": accepted,- }),- );- accepted- }-}--struct QueueItem {- app: AppHandle,- request: GenerationRequest,- configuration: Configuration,- cancel: Arc<AtomicBool>,-}--#[derive(Debug, Clone, Deserialize)]-struct LoraAdapter {- path: String,- strength: f32,-}--#[derive(Clone)]-struct Configuration {- dll_path: PathBuf,- vulkan: bool,- attention: &'static str,- animate: bool,- prompt_embedding: Option<PathBuf>,- models: [(i32, &'static str, Option<PathBuf>); 5],- loras: Result<Vec<LoraAdapter>, String>,- step_override: Result<Option<i32>, String>,-}--impl Configuration {- fn from_settings(settings: &BTreeMap<String, ProviderSetting>) -> Self {- let slopfab = settings.get("slopfab");- let string_option = |name: &str| {- slopfab.and_then(|setting| match setting.options.get(name) {- Some(ProviderOption::String(value)) => Some(value.as_str()),- _ => None,- })- };- let option = |name: &str| {- slopfab.and_then(|setting| match setting.options.get(name) {- Some(ProviderOption::String(value)) if !value.trim().is_empty() => {- Some(PathBuf::from(value))- }- _ => None,- })- };- Self {- // `dllPath` is no longer written by the app and has no settings UI.- // It is still READ so a project or a test that carries one keeps- // working; with none, use the DLL beside the executable.- dll_path: option("dllPath").unwrap_or_else(default_dll_path),- vulkan: string_option("inferenceBackend") == Some("vulkan"),- animate: string_option("generationMode") == Some("animate"),- prompt_embedding: option("promptEmbedding"),- attention: match string_option("attention") {- Some("exact") => "exact",- Some("flash2") => "flash2",- _ => "sage2",- },- models: [- (0, "transformer", option("transformer")),- (1, "textEncoder", option("textEncoder")),- (2, "tokenizer", option("tokenizer")),- (3, "videoVae", option("videoVae")),- (4, "audioVae", option("audioVae")),- ],- loras: match slopfab.and_then(|setting| setting.options.get("loras")) {- None => Ok(Vec::new()),- Some(ProviderOption::String(value)) => serde_json::from_str::<Vec<LoraAdapter>>(value)- .map_err(|error| format!("Invalid LoRA settings: {error}")),- _ => Err("LoRA settings must be a JSON array of paths and strengths.".into()),- },- step_override: match slopfab.and_then(|setting| setting.options.get("stepOverride")) {- None => Ok(None),- Some(ProviderOption::Number(value)) if value.is_finite() && value.fract() == 0.0 && *value >= 2.0 && *value <= i32::MAX as f64 => Ok(Some(*value as i32)),- _ => Err("LoRA step override must be a whole number from 2 to 2147483647.".into()),- },- }- }-- fn validate_inputs(&self, request: &GenerationRequest) -> Result<(), String> {- if self.animate {- if request.still_image {- return Err("Animate requires video generation.".into());- }- if request.reference_video_ids.len() != 1 {- return Err("Animate requires exactly one reference video.".into());- }- if request.reference_paths.len() != 1 {- return Err("Animate requires exactly one repainted frame of the driving scene.".into());- }- if request.continuation_path.is_some() || !request.refmods.is_empty() {- return Err("Animate does not support scene continuation or refmods.".into());- }- } else if request.prompt.trim().is_empty() {- return Err("Generation prompt cannot be empty.".into());- }- Ok(())- }-- fn generation_steps(&self, fallback: i32) -> Result<i32, String> {- self.step_override.clone().map(|steps| steps.unwrap_or(fallback))- }-- fn platform(&self, detected: ComputePlatform) -> ComputePlatform {- if self.vulkan {- ComputePlatform::Vulkan- } else {- detected- }- }-- fn timing_profile(&self, version: &str, platform: ComputePlatform) -> String {- let models = self- .models- .iter()- .filter_map(|(_, id, path)| {- path.as_ref().map(|path| {- let file = path- .file_name()- .and_then(|value| value.to_str())- .unwrap_or("unknown");- format!("{id}={file}")- })- })- .collect::<Vec<_>>()- .join(";");- let adapters = self.loras.as_ref().map(|loras| loras.iter()- .map(|lora| format!("{}@{}", lora.path, lora.strength)).collect::<Vec<_>>().join(";")).unwrap_or_default();- format!("slopfab={version}|platform={}|attention={}|{models}|loras={adapters}|steps={:?}", platform.label(), self.attention, self.step_override)- }-}--pub fn status(settings: &BTreeMap<String, ProviderSetting>) -> SlopfabStatus {- let configuration = Configuration::from_settings(settings);- let mut models = configuration- .models- .iter()- .filter(|(id, _, _)| !configuration.animate || (*id != 1 && *id != 2))- .map(|(_, id, path)| ModelStatus {- id,- configured: path.is_some(),- available: path.as_deref().is_some_and(Path::is_file),- path: path- .as_ref()- .map(|value| value.to_string_lossy().into_owned()),- })- .collect::<Vec<_>>();- if configuration.animate {- models.push(ModelStatus {- id: "promptEmbedding", configured: configuration.prompt_embedding.is_some(),- available: configuration.prompt_embedding.as_deref().is_some_and(Path::is_file),- path: configuration.prompt_embedding.as_ref().map(|path| path.to_string_lossy().into_owned()),- });- }- let dll_path = configuration.dll_path.to_string_lossy().into_owned();- match ffi::Api::load(&configuration.dll_path) {- Ok(api) => match api.version() {- Ok(version) => {- let detected = detect_platform(&api);- let platform = configuration.platform(detected);- let missing = models- .iter()- .filter(|model| !model.available && model.id != "tokenizer")- .count();- SlopfabStatus {- state: if missing == 0 {- "ready"- } else {- "modelsMissing"- },- dll_path,- version: Some(version),- platform: Some(platform.label()),- cuda_available: detected != ComputePlatform::Vulkan,- cuda_device_names: ffi::cuda_device_names(),- detail: if missing == 0 {- "Runtime and required model paths are available.".into()- } else {- format!("Runtime loaded; {missing} required model paths are missing. Editing remains available.")- },- models,- }- }- Err(error) => SlopfabStatus {- state: "incompatible",- dll_path,- version: None,- platform: None,- cuda_available: false,- cuda_device_names: Vec::new(),- detail: error,- models,- },- },- Err(error) => SlopfabStatus {- state: "runtimeMissing",- dll_path,- version: None,- platform: None,- cuda_available: false,- cuda_device_names: Vec::new(),- detail: format!("slopfab is unavailable: {error}. Editing remains available."),- models,- },- }-}--pub fn resolve_plan(- request: &GenerationRequest,- settings: &BTreeMap<String, ProviderSetting>,-) -> Result<ResolvedPlan, String> {- validate_generation_controls(request)?;- let configuration = Configuration::from_settings(settings);- let api = ffi::Api::load(&configuration.dll_path)?;- api.version()?;- let platform = configuration.platform(detect_platform(&api));- let handle = RequestHandle::new(&api)?;- configure_request(&api, handle.0, request, &configuration, platform, false)?;- let plan = api.resolve(handle.0)?;- let description = api.describe(handle.0)?;- let (_, frames) = scene_frame_window(request, plan.aligned_frames)?;- Ok(ResolvedPlan {- canvas_width: plan.canvas_width,- canvas_height: plan.canvas_height,- aligned_frames: frames,- duration_seconds: plan.duration_seconds * frames as f64 / plan.aligned_frames as f64,- model_evaluations: plan.num_model_evaluations,- sequence_rows_without_text: plan.sequence_rows_without_text,- latent_frames: plan.latent_frames,- latent_width: plan.latent_width,- latent_height: plan.latent_height,- description,- boundary: "Plan only. No weights were opened and no media file was created.",- })-}--fn validate_generation_controls(request: &GenerationRequest) -> Result<(), String> {- if request.reference_paths.len() > 9 || request.reference_video_ids.len() > 3 || request.reference_count() > 12 {- return Err("Use at most nine images and three videos per generation.".into());- }- if request.still_image && !request.reference_video_ids.is_empty() {- return Err("Video references cannot be used for still-image generation.".into());- }- if request.steps < 2 {- return Err("Generation step count must be at least 2.".into());- }- if request.seed < -1 {- return Err("Generation seed must be -1 or greater.".into());- }- if request.canvas_width <= 0 || request.canvas_height <= 0 {- return Err("Generation canvas dimensions must be positive.".into());- }- Ok(())-}--fn generation_seed(value: i64) -> Result<u64, String> {- if value == -1 {- let mut bytes = [0_u8; 8];- fill_random(&mut bytes)- .map_err(|error| format!("Could not create a random generation seed: {error}"))?;- Ok(u64::from_ne_bytes(bytes))- } else {- u64::try_from(value).map_err(|_| "Generation seed must be -1 or greater.".to_string())- }-}--fn configure_request(- api: &ffi::Api,- handle: *mut ffi::Request,- request: &GenerationRequest,- configuration: &Configuration,- platform: ComputePlatform,- include_models: bool,-) -> Result<(), String> {- configuration.validate_inputs(request)?;- if configuration.animate {- let videos = REFERENCE_VIDEOS.lock().map_err(|_| "Reference video lock failed.")?;- let video = videos.get(&request.reference_video_ids[0])- .ok_or("The prepared reference video no longer exists. Please retry generation.")?;- // The setter selects Euler and four boundaries; explicit step settings follow.- api.set_animate(handle, video.has_audio())?;- let source = configuration.prompt_embedding.as_deref()- .ok_or("Animate conditioning is missing. Download Animate in Settings and select its conditioning additional safetensor.")?;- let embedding = crate::conditioning::prepare(source)?;- api.set_prompt_embedding(handle, &embedding)?;- }- api.set_prompt(handle, if configuration.animate { ANIMATE_PROMPT } else { &request.prompt })?;- // Keep lengths within the advertised 15-second range from rounding past- // Animate's limit. Leave out-of-range requests to the DLL's validation.- let frames = if configuration.animate && request.frames <= 360 {- request.frames.min(ANIMATE_MAX_ALIGNED_FRAMES)- } else { request.frames };- api.set_frames(handle, frames)?;- if request.still_image {- api.set_still_image(handle)?;- }- api.set_steps(handle, configuration.generation_steps(request.steps)?)?;- api.set_seed(handle, generation_seed(request.seed)?)?;- api.set_resolution(handle, request.canvas_width, request.canvas_height)?;- api.set_inference_backend(handle, platform.backend())?;- api.set_attention(handle, configuration.attention)?;- api.set_verbose(handle, false)?;- if let Some(path) = &request.continuation_path {- api.set_continuation_file(handle, path, 22)?;- }- if include_models {- if let Some(path) = &request.save_latents_path {- api.set_save_latents(handle, path)?;- }- }- for lora in configuration.loras.as_ref().map_err(Clone::clone)? {- if lora.path.trim().is_empty() || !lora.strength.is_finite() {- return Err("LoRA paths must be nonempty and strengths finite.".into());- }- if lora.strength == 0.0 { continue; }- if include_models && !Path::new(&lora.path).is_file() {- return Err(format!("LoRA file is missing: {}", lora.path));- }- api.add_lora(handle, Path::new(&lora.path), lora.strength)?;- }- if include_models {- api.set_reuse_models(handle, true)?;- for (id, _, path) in &configuration.models {- if configuration.animate && (*id == 1 || *id == 2) { continue; }- if request.still_image && *id == 4 {- continue;- }- if let Some(path) = path {- api.set_model(handle, *id, path)?;- }- }- }- for path in &request.reference_paths {- api.add_reference(handle, Path::new(path))?;- }- if !request.reference_video_ids.is_empty() {- let videos = REFERENCE_VIDEOS.lock().map_err(|_| "Reference video lock failed.")?;- for id in &request.reference_video_ids {- videos.get(id).ok_or("The prepared reference video no longer exists. Please retry generation.")?- .attach(handle, &configuration.dll_path)?;- }- }- for refmod in &request.refmods {- if refmod.path.trim().is_empty() || !refmod.strength.is_finite() || !(0.0..=1.0).contains(&refmod.strength)- || !(1..=10).contains(&refmod.copies) {- return Err("Refmods need a path, strength 0 to 1 and copies 1 to 10.".into());- }- if refmod.strength == 0.0 { continue; }- api.add_refmod(handle, Path::new(&refmod.path), refmod.strength, refmod.copies as i32)?;- }- Ok(())-}--/// Continuation output is cumulative. Expose only the new scene to the editor,-/// while the DLL saves the full archive for the next continuation.-fn scene_frame_window(request: &GenerationRequest, total: i32) -> Result<(i32, i32), String> {- if total <= 0 { return Err("Slopfab returned no video frames.".into()); }- if request.continuation_path.is_none() { return Ok((0, total)); }- if request.frames <= 0 { return Err("Continuation needs a positive frame count.".into()); }- let frames = request.frames.checked_add(16).ok_or("Continuation frame count overflow.")? / 17 * 17;- let offset = total.checked_sub(frames).filter(|offset| *offset >= 22)- .ok_or("Slopfab continuation output is missing its source frames.".to_string())?;- Ok((offset, frames))-}--fn scene_audio_offset(frame_offset: i32, fps: f64, channels: i32, sample_rate: i32, total: usize) -> Result<usize, String> {- if total == 0 || frame_offset == 0 { return Ok(0); }- if !fps.is_finite() || fps <= 0.0 || channels <= 0 || sample_rate <= 0 {- return Err("Slopfab returned invalid continuation audio timing.".into());- }- let samples = (frame_offset as f64 * sample_rate as f64 / fps).round();- let offset = (samples as usize).checked_mul(channels as usize)- .filter(|offset| *offset <= total).ok_or("Slopfab continuation audio is shorter than its source scene.")?;- Ok(offset)-}--/// Runs one generation and retains its terminal handle for frame streaming.-/// Audio is copied once; video stays owned by slopfab until encoding completes.-struct FinishedGeneration {- api: ffi::Api,- generation: *mut ffi::Generation,- frame_offset: u32,-}--// Created by the serial worker and moved into a mutex-protected render store-// only after reaching a terminal state. Finished output is read-only.-unsafe impl Send for FinishedGeneration {}--impl rendered::FrameSource for FinishedGeneration {- fn frame_rgba(&self, index: u32, width: u32, height: u32) -> Result<Vec<u8>, String> {- let index = index.checked_add(self.frame_offset).ok_or("Continuation frame index overflow.")?;- self.api.frame_rgba8(self.generation, index, width, height)- }-}--impl Drop for FinishedGeneration {- fn drop(&mut self) {- self.api.destroy_generation(self.generation);- }-}--fn run_generation(item: &QueueItem) -> Result<(OutputMetadata, rendered::RenderedVideo), String> {- if item.cancel.load(Ordering::Acquire) {- return Err("Generation was cancelled before it started.".into());- }- let api = ffi::Api::load(&item.configuration.dll_path)?;- let version = api.version()?;- let platform = item.configuration.platform(detect_platform(&api));- let timing_profile = item.configuration.timing_profile(&version, platform);- diagnostics::debug(- "slopfab",- "generation.runtime_ready",- "slopfab runtime loaded.",- serde_json::json!({- "jobId": item.request.job_id, "version": version, "platform": platform.label(),- }),- );- let request = RequestHandle::new(&api)?;- configure_request(- &api,- request.0,- &item.request,- &item.configuration,- platform,- true,- )?;- api.resolve(request.0)?;- diagnostics::debug(- "slopfab",- "generation.request_resolved",- "slopfab resolved the generation request.",- serde_json::json!({ "jobId": item.request.job_id }),- );- let context = Box::new(CallbackContext {- app: item.app.clone(),- job_id: item.request.job_id.clone(),- frames: item.request.frames,- canvas_width: item.request.canvas_width,- canvas_height: item.request.canvas_height,- reference_count: item.request.reference_count(),- timing_profile: timing_profile.clone(),- planned_steps: (item.request.steps - 1).max(0),- });- let context_ptr = Box::into_raw(context);- let generation = match api.start(request.0, Some(progress_callback), context_ptr.cast()) {- Ok(value) => value,- Err(error) => {- unsafe {- drop(Box::from_raw(context_ptr));- }- return Err(error);- }- };- diagnostics::debug(- "slopfab",- "generation.api_started",- "slopfab accepted the generation request.",- serde_json::json!({ "jobId": item.request.job_id }),- );- loop {- if item.cancel.load(Ordering::Acquire) {- api.cancel(generation);- }- let code = api.wait(generation, 100);- if code == NOT_READY {- continue;- }- if code == CANCELLED {- api.destroy_generation(generation);- unsafe {- drop(Box::from_raw(context_ptr));- }- return Err("Generation cancelled at the next slopfab checkpoint.".into());- }- if code != 0 {- let error = api.generation_error(generation);- api.destroy_generation(generation);- unsafe {- drop(Box::from_raw(context_ptr));- }- return Err(if error.is_empty() {- format!("slopfab generation failed with status {code}.")- } else {- error- });- }- break;- }- let output = match api.output(generation) {- Ok(output) => output,- Err(error) => {- api.destroy_generation(generation);- unsafe { drop(Box::from_raw(context_ptr)) };- return Err(error);- }- };- unsafe { drop(Box::from_raw(context_ptr)) };- drop(request);- let mut source = Box::new(FinishedGeneration { api, generation, frame_offset: 0 });- let (frame_offset, frames) = scene_frame_window(&item.request, output.frames)?;- source.frame_offset = frame_offset as u32;- if output.audio.is_null() && output.audio_float_count != 0 {- return Err("Slopfab returned a null audio buffer with a non-zero size.".into());- }- // Audio is small enough to own directly. Video remains in the generation- // and is converted through frame_rgba8 only as WebCodecs asks for it.- let audio_offset = scene_audio_offset(frame_offset, output.fps, output.audio_channels, output.audio_sample_rate, output.audio_float_count)?;- let audio = if output.audio.is_null() {- &[][..]- } else {- &(unsafe { std::slice::from_raw_parts(output.audio, output.audio_float_count) })[audio_offset..]- };- let metadata = OutputMetadata {- frames, width: output.width, height: output.height, fps: output.fps,- audio_channels: output.audio_channels, audio_sample_rate: output.audio_sample_rate,- audio_samples: audio.len(),- reference_count: item.request.reference_count(),- timing_profile,- seconds_conditioning: output.seconds_conditioning,- seconds_denoise: output.seconds_denoise,- seconds_video_decode: output.seconds_video_decode,- seconds_audio_decode: output.seconds_audio_decode,- seconds_total: output.seconds_total,- steps_computed: output.steps_computed,- steps_skipped: output.steps_skipped,- duration_seconds: if output.fps > 0.0 {- frames as f64 / output.fps- } else {- 0.0- },- boundary: "Latents are saved in the project. Decoded buffers remain owned by slopfab; the webview encodes the new scene's frames on demand.",- };- let pictures = rendered::from_source(- &item.request.job_id,- source,- audio,- frames as u32,- output.width.max(0) as u32,- output.height.max(0) as u32,- output.channels.max(0) as u32,- output.video_float_count,- output.fps,- output.audio_channels.max(0) as u32,- output.audio_sample_rate.max(0) as u32,- )?;- Ok((metadata, pictures))-}--struct RequestHandle<'a>(*mut ffi::Request, &'a ffi::Api);-impl<'a> RequestHandle<'a> {- fn new(api: &'a ffi::Api) -> Result<Self, String> {- api.create_request().map(|value| Self(value, api))- }-}-impl Drop for RequestHandle<'_> {- fn drop(&mut self) {- self.1.destroy_request(self.0);- }-}--struct GenerationHandle<'a>(*mut ffi::Generation, &'a ffi::Api);-impl Drop for GenerationHandle<'_> {- fn drop(&mut self) {- self.1.destroy_generation(self.0);- }-}--#[derive(Debug, Deserialize)]-#[serde(rename_all = "camelCase")]-pub struct ReferenceIconSpec {- pub id: String,- pub prompt: String,- pub seed: u64,- pub destination: PathBuf,-}--#[derive(Debug)]-pub struct ReferenceIconBatchConfig {- pub dll_path: PathBuf,- pub transformer: PathBuf,- pub text_encoder: PathBuf,- pub video_vae: PathBuf,- pub backend: String,-}--fn write_reference_icon(- api: &ffi::Api,- configuration: &Configuration,- platform: ComputePlatform,- spec: &ReferenceIconSpec,-) -> Result<(), String> {- let request = GenerationRequest {- job_id: spec.id.clone(),- prompt: spec.prompt.clone(),- frames: 1,- still_image: true,- steps: crate::reference_icons::STEPS,- seed: i64::try_from(spec.seed)- .map_err(|_| format!("Icon '{}' seed is too large.", spec.id))?,- canvas_width: crate::reference_icons::RENDER_SIZE as i32,- canvas_height: crate::reference_icons::RENDER_SIZE as i32,- reference_paths: Vec::new(),- refmods: Vec::new(),- reference_video_ids: Vec::new(),- ..Default::default()- };- let handle = RequestHandle::new(api)?;- configure_request(api, handle.0, &request, configuration, platform, true)?;- api.resolve(handle.0)?;- let generation = GenerationHandle(api.start(handle.0, None, std::ptr::null_mut())?, api);- let status = api.wait(generation.0, -1);- if status != 0 {- let detail = api.generation_error(generation.0);- return Err(if detail.is_empty() {- format!("Icon '{}' failed with slopfab status {status}.", spec.id)- } else {- format!("Icon '{}' failed: {detail}", spec.id)- });- }- let output = api.output(generation.0)?;- let render_size = crate::reference_icons::RENDER_SIZE;- if output.width != render_size as i32 || output.height != render_size as i32 || output.frames != 1 {- return Err(format!(- "Icon '{}' returned {}x{} with {} frames.",- spec.id, output.width, output.height, output.frames- ));- }- let rgba = api.frame_rgba8(generation.0, 0, render_size, render_size)?;- let jpeg = crate::reference_icons::encode_jpeg(&rgba)?;- if let Some(parent) = spec.destination.parent() {- std::fs::create_dir_all(parent).map_err(|error| {- format!(- "Could not create icon folder '{}': {error}",- parent.display()- )- })?;- }- std::fs::write(&spec.destination, jpeg).map_err(|error| {- format!(- "Could not create icon '{}': {error}",- spec.destination.display()- )- })-}--pub fn generate_reference_icon_batch(- specs: &[ReferenceIconSpec],- batch: &ReferenceIconBatchConfig,-) -> Result<(), String> {- let backend = match batch.backend.as_str() {- "cuda" => ComputePlatform::Cuda13,- "vulkan" => ComputePlatform::Vulkan,- value => return Err(format!("Unsupported icon generation backend '{value}'.")),- };- let configuration = Configuration {- dll_path: batch.dll_path.clone(),- loras: Ok(Vec::new()),- step_override: Ok(None),- vulkan: backend == ComputePlatform::Vulkan,- attention: "sage2",- animate: false,- prompt_embedding: None,- models: [- (0, "transformer", Some(batch.transformer.clone())),- (1, "textEncoder", Some(batch.text_encoder.clone())),- (2, "tokenizer", None),- (3, "videoVae", Some(batch.video_vae.clone())),- // Icons keep one picture and need no decoded soundtrack.- (4, "audioVae", None),- ],- };- let api = ffi::Api::load(&configuration.dll_path)?;- api.version()?;- let platform = if backend == ComputePlatform::Cuda13 {- detect_platform(&api)- } else {- backend- };- let result = specs.iter().enumerate().try_for_each(|(index, spec)| {- let started = std::time::Instant::now();- write_reference_icon(&api, &configuration, platform, spec)?;- println!(- "[{}/{}] {}: generated in {:.1}s",- index + 1,- specs.len(),- spec.id,- started.elapsed().as_secs_f64()- );- Ok(())- });- let cleared = api.clear_reused_models();- result.and(cleared)-}--struct CallbackContext {- app: AppHandle,- job_id: String,- frames: i32,- canvas_width: i32,- canvas_height: i32,- reference_count: usize,- timing_profile: String,- planned_steps: i32,-}-unsafe extern "C" fn progress_callback(- progress: *const ffi::Progress,- userdata: *mut std::ffi::c_void,-) {- let _ = std::panic::catch_unwind(|| {- if progress.is_null() || userdata.is_null() {- return;- }- let progress = unsafe { &*progress };- let context = unsafe { &*(userdata.cast::<CallbackContext>()) };- diagnostics::debug(- "slopfab",- "generation.progress",- "slopfab reported progress.",- serde_json::json!({- "jobId": context.job_id,- "stage": stage_name(progress.stage),- "step": progress.step,- "totalSteps": progress.total_steps,- "plannedSteps": context.planned_steps,- "elapsedSeconds": progress.elapsed_seconds,- }),- );- let _ = context.app.emit(- "slopfab-progress",- ProgressEvent {- job_id: context.job_id.clone(),- stage: stage_name(progress.stage),- step: progress.step,- total_steps: progress.total_steps,- planned_steps: context.planned_steps,- elapsed_seconds: progress.elapsed_seconds,- frames: context.frames,- canvas_width: context.canvas_width,- canvas_height: context.canvas_height,- reference_count: context.reference_count,- timing_profile: context.timing_profile.clone(),- },- );- });-}--fn stage_name(stage: i32) -> &'static str {- match stage {- 0 => "starting",- 1 => "references",- 2 => "conditioning",- 3 => "transformerLoad",- 4 => "denoising",- 5 => "videoDecode",- 6 => "audioDecode",- 7 => "delivering",- 8 => "finished",- _ => "unknown",- }-}--mod ffi {- use libloading::Library;- use std::{- ffi::{c_char, c_void, CStr, CString},- path::Path,- ptr,- };-- pub enum Request {}- enum ReferenceVideo {}- type VideoCreate = unsafe extern "C" fn(f64, *mut *mut ReferenceVideo) -> i32;- type VideoDestroy = unsafe extern "C" fn(*mut ReferenceVideo);- type VideoAppend = unsafe extern "C" fn(*mut ReferenceVideo, *const u8, usize, i32, i32, usize, f64) -> i32;- type VideoAudio = unsafe extern "C" fn(*mut ReferenceVideo, *const f32, usize, i32, i32, f64) -> i32;- type VideoAttach = unsafe extern "C" fn(*mut Request, *const ReferenceVideo) -> i32;-- pub struct ReferenceVideoHandle {- api: Api,- handle: *mut ReferenceVideo,- destroy: VideoDestroy,- append_rgba: VideoAppend,- audio: VideoAudio,- attach_video: VideoAttach,- dll_path: std::path::PathBuf,- frames: usize,- has_audio: bool,- }-- // The registry mutex serializes access. The DLL handle and all copied input- // buffers outlive every call, and attached snapshots own their references.- unsafe impl Send for ReferenceVideoHandle {}-- impl ReferenceVideoHandle {- pub fn new(api: Api, duration: f64, dll_path: std::path::PathBuf) -> Result<Self, String> {- unsafe {- let missing = |error| format!("This slopfab.dll does not support video references. Update the runtime: {error}");- let create = *api._library.get::<VideoCreate>(b"slopfab_reference_video_create\0").map_err(missing)?;- let destroy = *api._library.get::<VideoDestroy>(b"slopfab_reference_video_destroy\0").map_err(missing)?;- let append_rgba = *api._library.get::<VideoAppend>(b"slopfab_reference_video_append_rgba8\0").map_err(missing)?;- let audio = *api._library.get::<VideoAudio>(b"slopfab_reference_video_set_audio_f32\0").map_err(missing)?;- let attach_video = *api._library.get::<VideoAttach>(b"slopfab_request_add_reference_video\0").map_err(missing)?;- let mut handle = std::ptr::null_mut();- api.error(create(duration, &mut handle))?;- if handle.is_null() { return Err("slopfab returned an empty reference video handle.".into()); }- Ok(Self { api, handle, destroy, append_rgba, audio, attach_video, dll_path, frames: 0, has_audio: false })- }- }- pub fn append(&mut self, bytes: &[u8], width: i32, height: i32, timestamp: f64) -> Result<(), String> {- if width <= 0 || height <= 0 || width > 1024 || height > 1024 || self.frames >= 361 {- return Err("Reference video preparation exceeds the frame or dimension limit.".into());- }- self.api.error(unsafe { (self.append_rgba)(self.handle, bytes.as_ptr(), bytes.len(), width, height, width as usize * 4, timestamp) })?;- self.frames += 1;- Ok(())- }- pub fn set_audio(&mut self, samples: &[f32], channels: i32, sample_rate: i32) -> Result<(), String> {- self.api.error(unsafe { (self.audio)(self.handle, samples.as_ptr(), samples.len(), channels, sample_rate, 0.0) })?;- self.has_audio = !samples.is_empty();- Ok(())- }- pub fn has_audio(&self) -> bool { self.has_audio }- pub fn attach(&self, request: *mut Request, dll_path: &Path) -> Result<(), String> {- if dll_path != self.dll_path { return Err("Reference video and generation must use the same slopfab runtime.".into()); }- self.api.error(unsafe { (self.attach_video)(request, self.handle) })- }- }- impl Drop for ReferenceVideoHandle {- fn drop(&mut self) { unsafe { (self.destroy)(self.handle) }; }- }-- pub fn has_cuda_device() -> bool {- // CUDA's driver API uses the system calling convention on Windows.- // Keep the library alive until both calls have returned.- unsafe {- #[cfg(windows)]- let driver = libloading::os::windows::Library::load_with_flags(- "nvcuda.dll", 0x00000800, // LOAD_LIBRARY_SEARCH_SYSTEM32- ).map(Library::from);- #[cfg(not(windows))]- let driver = Library::new("libcuda.so.1");- let Ok(library) = driver else { return false };- let Ok(init) = library.get::<unsafe extern "system" fn(u32) -> i32>(b"cuInit\0") else { return false };- let Ok(count) = library.get::<unsafe extern "system" fn(*mut i32) -> i32>(b"cuDeviceGetCount\0") else { return false };- let mut devices = 0;- init(0) == 0 && count(&mut devices) == 0 && devices > 0- }- }- pub fn cuda_device_names() -> Vec<String> {- gpu_devices().into_iter().map(|device| device.name).collect()- }-- pub fn gpu_devices() -> Vec<super::GpuDevice> {- // The display driver's API is available without the CUDA toolkit.- // Keep the library and fixed-size name buffer alive for every call.- unsafe {- #[cfg(windows)]- let driver = libloading::os::windows::Library::load_with_flags(- "nvcuda.dll", 0x00000800, // LOAD_LIBRARY_SEARCH_SYSTEM32- ).map(Library::from);- #[cfg(not(windows))]- let driver = Library::new("libcuda.so.1");- let Ok(library) = driver else { return Vec::new() };- let Ok(init) = library.get::<unsafe extern "system" fn(u32) -> i32>(b"cuInit\0") else { return Vec::new() };- let Ok(count) = library.get::<unsafe extern "system" fn(*mut i32) -> i32>(b"cuDeviceGetCount\0") else { return Vec::new() };- let Ok(get) = library.get::<unsafe extern "system" fn(*mut i32, i32) -> i32>(b"cuDeviceGet\0") else { return Vec::new() };- let Ok(name) = library.get::<unsafe extern "system" fn(*mut c_char, i32, i32) -> i32>(b"cuDeviceGetName\0") else { return Vec::new() };- let memory = library.get::<unsafe extern "system" fn(*mut usize, i32) -> i32>(b"cuDeviceTotalMem_v2\0").ok();- let mut count_value = 0;- if init(0) != 0 || count(&mut count_value) != 0 { return Vec::new() }- (0..count_value).filter_map(|ordinal| {- let mut device = 0;- let mut buffer = [0u8; 256];- if get(&mut device, ordinal) != 0 || name(buffer.as_mut_ptr().cast(), buffer.len() as i32, device) != 0 {- return None;- }- let end = buffer.iter().position(|byte| *byte == 0).unwrap_or(buffer.len());- let mut memory_bytes = 0usize;- if let Some(memory) = &memory { if memory(&mut memory_bytes, device) != 0 { memory_bytes = 0; } }- Some(super::GpuDevice { name: String::from_utf8_lossy(&buffer[..end]).into_owned(), memory_bytes: memory_bytes as u64 })- }).collect()- }- }-- pub enum Generation {}- #[repr(C)]- #[derive(Clone, Copy)]- pub struct Plan {- pub canvas_width: i32,- pub canvas_height: i32,- pub aligned_frames: i32,- pub duration_seconds: f64,- pub num_model_evaluations: i32,- pub sequence_rows_without_text: i32,- pub latent_frames: i32,- pub latent_height: i32,- pub latent_width: i32,- pub num_video_rows: i32,- pub num_audio_rows: i32,- pub num_audio_latents: i32,- }- #[repr(C)]- pub struct Progress {- pub stage: i32,- pub step: i32,- pub total_steps: i32,- pub elapsed_seconds: f64,- }- #[repr(C)]- #[derive(Clone, Copy)]- pub struct Output {- pub video: *const f32,- pub video_float_count: usize,- pub channels: i32,- pub frames: i32,- pub width: i32,- pub height: i32,- pub audio: *const f32,- pub audio_float_count: usize,- pub audio_channels: i32,- pub audio_sample_rate: i32,- pub audio_frames: i64,- pub fps: f64,- pub seconds_conditioning: f64,- pub seconds_denoise: f64,- pub seconds_video_decode: f64,- pub seconds_audio_decode: f64,- pub seconds_total: f64,- pub steps_computed: i32,- pub steps_skipped: i32,- }- pub type ProgressFn = Option<unsafe extern "C" fn(*const Progress, *mut c_void)>;-- pub struct Api {- _library: Library,- capi_version: unsafe extern "C" fn() -> u32,- version_string: unsafe extern "C" fn() -> *const c_char,- last_error: unsafe extern "C" fn() -> *const c_char,- free_string: unsafe extern "C" fn(*mut c_char),- cuda_set_version: unsafe extern "C" fn(*const c_char) -> i32,- cuda_loaded_major: unsafe extern "C" fn(*mut i32) -> i32,- request_create: unsafe extern "C" fn() -> *mut Request,- request_destroy: unsafe extern "C" fn(*mut Request),- set_prompt: unsafe extern "C" fn(*mut Request, *const c_char) -> i32,- set_animate: Option<unsafe extern "C" fn(*mut Request, i32, i32) -> i32>,- set_prompt_embedding: Option<unsafe extern "C" fn(*mut Request, *const c_char) -> i32>,- set_resolution: unsafe extern "C" fn(*mut Request, i32, i32) -> i32,- set_frames: unsafe extern "C" fn(*mut Request, i32) -> i32,- set_still_image: Option<unsafe extern "C" fn(*mut Request, i32) -> i32>,- set_save_latents: Option<unsafe extern "C" fn(*mut Request, *const c_char) -> i32>,- set_continuation_file: Option<unsafe extern "C" fn(*mut Request, *const c_char, i32) -> i32>,- set_steps: unsafe extern "C" fn(*mut Request, i32) -> i32,- set_seed: unsafe extern "C" fn(*mut Request, u64) -> i32,- set_model: unsafe extern "C" fn(*mut Request, i32, *const c_char) -> i32,- add_lora: Option<unsafe extern "C" fn(*mut Request, *const c_char, f32) -> i32>,- add_refmod: Option<unsafe extern "C" fn(*mut Request, *const c_char, f32, i32) -> i32>,- add_reference: unsafe extern "C" fn(*mut Request, *const c_char) -> i32,- set_attention: unsafe extern "C" fn(*mut Request, *const c_char) -> i32,- set_inference_backend: unsafe extern "C" fn(*mut Request, i32) -> i32,- set_verbose: unsafe extern "C" fn(*mut Request, i32) -> i32,- set_reuse_models: unsafe extern "C" fn(*mut Request, i32) -> i32,- reused_models_clear: unsafe extern "C" fn() -> i32,- resolve_plan: unsafe extern "C" fn(*const Request, *mut Plan) -> i32,- describe_plan: unsafe extern "C" fn(*const Request, *mut *mut c_char) -> i32,- generation_start: unsafe extern "C" fn(- *const Request,- ProgressFn,- *mut c_void,- *mut *mut Generation,- ) -> i32,- generation_cancel: unsafe extern "C" fn(*mut Generation),- generation_wait: unsafe extern "C" fn(*mut Generation, i32) -> i32,- generation_error: unsafe extern "C" fn(*const Generation) -> *const c_char,- generation_output: unsafe extern "C" fn(*const Generation, *mut Output) -> i32,- generation_frame_rgba8: unsafe extern "C" fn(*const Generation, i32, *mut u8, usize) -> i32,- generation_destroy: unsafe extern "C" fn(*mut Generation),- }-- impl Api {- pub fn load(path: &Path) -> Result<Self, String> {- // SAFETY: symbols are copied function pointers with signatures from capi.h.- // The Library is retained in Api, so no pointer outlives the loaded module.- unsafe {- let library = load_library(path)- .map_err(|error| format!("could not load {}: {error}", path.display()))?;- macro_rules! symbol {- ($name:literal, $ty:ty) => {- *library- .get::<$ty>(concat!($name, "\0").as_bytes())- .map_err(|error| format!("missing {}: {error}", $name))?- };- }- Ok(Self {- capi_version: symbol!("slopfab_capi_version", unsafe extern "C" fn() -> u32),- version_string: symbol!(- "slopfab_capi_version_string",- unsafe extern "C" fn() -> *const c_char- ),- last_error: symbol!(- "slopfab_last_error",- unsafe extern "C" fn() -> *const c_char- ),- free_string: symbol!("slopfab_free_string", unsafe extern "C" fn(*mut c_char)),- cuda_set_version: symbol!(- "slopfab_cuda_set_version",- unsafe extern "C" fn(*const c_char) -> i32- ),- cuda_loaded_major: symbol!(- "slopfab_cuda_loaded_major",- unsafe extern "C" fn(*mut i32) -> i32- ),- request_create: symbol!(- "slopfab_request_create",- unsafe extern "C" fn() -> *mut Request- ),- request_destroy: symbol!(- "slopfab_request_destroy",- unsafe extern "C" fn(*mut Request)- ),- set_prompt: symbol!(- "slopfab_request_set_prompt",- unsafe extern "C" fn(*mut Request, *const c_char) -> i32- ),- set_animate: library- .get::<unsafe extern "C" fn(*mut Request, i32, i32) -> i32>(b"slopfab_request_set_animate\0")- .ok().map(|symbol| *symbol),- set_prompt_embedding: library- .get::<unsafe extern "C" fn(*mut Request, *const c_char) -> i32>(b"slopfab_request_set_prompt_embedding_path\0")- .ok().map(|symbol| *symbol),- set_resolution: symbol!(- "slopfab_request_set_resolution",- unsafe extern "C" fn(*mut Request, i32, i32) -> i32- ),- set_frames: symbol!(- "slopfab_request_set_frames",- unsafe extern "C" fn(*mut Request, i32) -> i32- ),- set_still_image: library- .get::<unsafe extern "C" fn(*mut Request, i32) -> i32>(b"slopfab_request_set_still_image\0")- .ok().map(|symbol| *symbol),- set_steps: symbol!(- "slopfab_request_set_steps",- unsafe extern "C" fn(*mut Request, i32) -> i32- ),- set_seed: symbol!(- "slopfab_request_set_seed",- unsafe extern "C" fn(*mut Request, u64) -> i32- ),- set_model: symbol!(- "slopfab_request_set_model_path",- unsafe extern "C" fn(*mut Request, i32, *const c_char) -> i32- ),- add_reference: symbol!(- "slopfab_request_add_reference_image",- unsafe extern "C" fn(*mut Request, *const c_char) -> i32- ),- add_lora: library- .get::<unsafe extern "C" fn(*mut Request, *const c_char, f32) -> i32>(b"slopfab_request_add_lora\0")- .ok().map(|symbol| *symbol),- set_save_latents: library- .get::<unsafe extern "C" fn(*mut Request, *const c_char) -> i32>(b"slopfab_request_set_save_latents\0")- .ok().map(|symbol| *symbol),- set_continuation_file: library- .get::<unsafe extern "C" fn(*mut Request, *const c_char, i32) -> i32>(b"slopfab_request_set_continuation_file\0")- .ok().map(|symbol| *symbol),- add_refmod: library- .get::<unsafe extern "C" fn(*mut Request, *const c_char, f32, i32) -> i32>(b"slopfab_request_add_refmod\0")- .ok().map(|symbol| *symbol),- set_attention: symbol!(- "slopfab_request_set_attention",- unsafe extern "C" fn(*mut Request, *const c_char) -> i32- ),- set_inference_backend: symbol!(- "slopfab_request_set_inference_backend",- unsafe extern "C" fn(*mut Request, i32) -> i32- ),- set_verbose: symbol!(- "slopfab_request_set_verbose",- unsafe extern "C" fn(*mut Request, i32) -> i32- ),- set_reuse_models: symbol!(- "slopfab_request_set_reuse_models",- unsafe extern "C" fn(*mut Request, i32) -> i32- ),- reused_models_clear: symbol!(- "slopfab_reused_models_clear",- unsafe extern "C" fn() -> i32- ),- resolve_plan: symbol!(- "slopfab_resolve_plan",- unsafe extern "C" fn(*const Request, *mut Plan) -> i32- ),- describe_plan: symbol!(- "slopfab_describe_plan",- unsafe extern "C" fn(*const Request, *mut *mut c_char) -> i32- ),- generation_start: symbol!(- "slopfab_generation_start",- unsafe extern "C" fn(- *const Request,- ProgressFn,- *mut c_void,- *mut *mut Generation,- ) -> i32- ),- generation_cancel: symbol!(- "slopfab_generation_cancel",- unsafe extern "C" fn(*mut Generation)- ),- generation_wait: symbol!(- "slopfab_generation_wait",- unsafe extern "C" fn(*mut Generation, i32) -> i32- ),- generation_error: symbol!(- "slopfab_generation_error",- unsafe extern "C" fn(*const Generation) -> *const c_char- ),- generation_output: symbol!(- "slopfab_generation_output",- unsafe extern "C" fn(*const Generation, *mut Output) -> i32- ),- generation_frame_rgba8: symbol!(- "slopfab_generation_frame_rgba8",- unsafe extern "C" fn(*const Generation, i32, *mut u8, usize) -> i32- ),- generation_destroy: symbol!(- "slopfab_generation_destroy",- unsafe extern "C" fn(*mut Generation)- ),- _library: library,- })- }- }- pub fn version(&self) -> Result<String, String> {- unsafe {- let packed = (self.capi_version)();- let major = packed >> 24;- if major != super::EXPECTED_CAPI_MAJOR {- return Err(format!(- "Incompatible slopfab C API major {major}; expected {}.",- super::EXPECTED_CAPI_MAJOR- ));- }- Ok(c_string((self.version_string)()))- }- }- fn error(&self, code: i32) -> Result<(), String> {- if code == 0 {- Ok(())- } else {- let message = unsafe { c_string((self.last_error)()) };- Err(if message.is_empty() {- format!("slopfab returned status {code}.")- } else {- message- })- }- }- pub fn create_request(&self) -> Result<*mut Request, String> {- let value = unsafe { (self.request_create)() };- if value.is_null() {- Err(unsafe { c_string((self.last_error)()) })- } else {- Ok(value)- }- }- pub fn destroy_request(&self, request: *mut Request) {- unsafe { (self.request_destroy)(request) }- }- pub fn set_cuda_version(&self, value: &str) -> Result<(), String> {- let value = CString::new(value)- .map_err(|_| "CUDA version contains a null byte.".to_string())?;- self.error(unsafe { (self.cuda_set_version)(value.as_ptr()) })- }- pub fn cuda_loaded_major(&self) -> Result<i32, String> {- let mut major = 0;- self.error(unsafe { (self.cuda_loaded_major)(&mut major) })?;- Ok(major)- }- pub fn set_prompt(&self, r: *mut Request, v: &str) -> Result<(), String> {- let v = CString::new(v).map_err(|_| "Prompt contains a null byte.".to_string())?;- self.error(unsafe { (self.set_prompt)(r, v.as_ptr()) })- }- #[cfg(test)]- pub fn disable_animate_for_test(&mut self) { self.set_animate = None; }- pub fn set_animate(&self, r: *mut Request, preserve_audio: bool) -> Result<(), String> {- let set = self.set_animate.ok_or("Animate requires slopfab.dll API 1.10 or later. Update the runtime.")?;- self.error(unsafe { set(r, 1, preserve_audio as i32) })- }- pub fn set_prompt_embedding(&self, r: *mut Request, path: &Path) -> Result<(), String> {- let set = self.set_prompt_embedding.ok_or("This slopfab.dll does not support frozen conditioning. Update the runtime.")?;- let path = path_cstring(path)?;- self.error(unsafe { set(r, path.as_ptr()) })- }- pub fn set_resolution(&self, r: *mut Request, w: i32, h: i32) -> Result<(), String> {- self.error(unsafe { (self.set_resolution)(r, w, h) })- }- pub fn set_frames(&self, r: *mut Request, v: i32) -> Result<(), String> {- self.error(unsafe { (self.set_frames)(r, v) })- }- pub fn set_still_image(&self, r: *mut Request) -> Result<(), String> {- let set = self.set_still_image.ok_or("This slopfab.dll does not support still-image generation. Update the runtime to generate reference icons.")?;- self.error(unsafe { set(r, 1) })- }- pub fn set_save_latents(&self, r: *mut Request, path: &Path) -> Result<(), String> {- let set = self.set_save_latents.ok_or("Saving generation latents requires slopfab.dll API 1.9 or later. Update the runtime.")?;- let path = path_cstring(path)?;- self.error(unsafe { set(r, path.as_ptr()) })- }- pub fn set_continuation_file(&self, r: *mut Request, path: &Path, overlap: i32) -> Result<(), String> {- let set = self.set_continuation_file.ok_or("Scene continuation requires slopfab.dll API 1.9 or later. Update the runtime.")?;- let path = path_cstring(path)?;- self.error(unsafe { set(r, path.as_ptr(), overlap) })- }- pub fn set_steps(&self, r: *mut Request, v: i32) -> Result<(), String> {- self.error(unsafe { (self.set_steps)(r, v) })- }- pub fn set_seed(&self, r: *mut Request, v: u64) -> Result<(), String> {- self.error(unsafe { (self.set_seed)(r, v) })- }- pub fn set_verbose(&self, r: *mut Request, v: bool) -> Result<(), String> {- self.error(unsafe { (self.set_verbose)(r, v as i32) })- }- pub fn set_reuse_models(&self, r: *mut Request, v: bool) -> Result<(), String> {- self.error(unsafe { (self.set_reuse_models)(r, v as i32) })- }- pub fn clear_reused_models(&self) -> Result<(), String> {- self.error(unsafe { (self.reused_models_clear)() })- }- pub fn set_attention(&self, r: *mut Request, v: &str) -> Result<(), String> {- let v =- CString::new(v).map_err(|_| "Attention mode contains a null byte.".to_string())?;- self.error(unsafe { (self.set_attention)(r, v.as_ptr()) })- }- pub fn set_inference_backend(&self, r: *mut Request, backend: i32) -> Result<(), String> {- self.error(unsafe { (self.set_inference_backend)(r, backend) })- }- pub fn set_model(&self, r: *mut Request, id: i32, path: &Path) -> Result<(), String> {- let v = path_cstring(path)?;- self.error(unsafe { (self.set_model)(r, id, v.as_ptr()) })- }- pub fn add_reference(&self, r: *mut Request, path: &Path) -> Result<(), String> {- let v = path_cstring(path)?;- self.error(unsafe { (self.add_reference)(r, v.as_ptr()) })- }- pub fn add_lora(&self, r: *mut Request, path: &Path, strength: f32) -> Result<(), String> {- let add = self.add_lora.ok_or("This slopfab.dll does not support LoRAs. Update the DLL or disable the selected LoRAs.")?;- let path = path_cstring(path)?;- self.error(unsafe { add(r, path.as_ptr(), strength) })- }- pub fn add_refmod(&self, r: *mut Request, path: &Path, strength: f32, copies: i32) -> Result<(), String> {- let add = self.add_refmod.ok_or("This slopfab.dll does not support refmods. Install a build with the refmod API (1.8 or later).")?;- let path = path_cstring(path)?;- self.error(unsafe { add(r, path.as_ptr(), strength, copies) })- }- pub fn resolve(&self, request: *mut Request) -> Result<Plan, String> {- let mut value = Plan {- canvas_width: 0,- canvas_height: 0,- aligned_frames: 0,- duration_seconds: 0.0,- num_model_evaluations: 0,- sequence_rows_without_text: 0,- latent_frames: 0,- latent_height: 0,- latent_width: 0,- num_video_rows: 0,- num_audio_rows: 0,- num_audio_latents: 0,- };- self.error(unsafe { (self.resolve_plan)(request, &mut value) })?;- Ok(value)- }- pub fn describe(&self, request: *mut Request) -> Result<String, String> {- let mut value = ptr::null_mut();- self.error(unsafe { (self.describe_plan)(request, &mut value) })?;- let text = unsafe { c_string(value) };- unsafe { (self.free_string)(value) };- Ok(text)- }- pub fn start(- &self,- request: *mut Request,- callback: ProgressFn,- userdata: *mut c_void,- ) -> Result<*mut Generation, String> {- let mut value = ptr::null_mut();- self.error(unsafe {- (self.generation_start)(request, callback, userdata, &mut value)- })?;- Ok(value)- }- pub fn cancel(&self, generation: *mut Generation) {- unsafe { (self.generation_cancel)(generation) }- }- pub fn wait(&self, generation: *mut Generation, timeout: i32) -> i32 {- unsafe { (self.generation_wait)(generation, timeout) }- }- pub fn generation_error(&self, generation: *mut Generation) -> String {- unsafe { c_string((self.generation_error)(generation)) }- }- pub fn output(&self, generation: *mut Generation) -> Result<Output, String> {- let mut value = Output {- video: ptr::null(),- video_float_count: 0,- channels: 0,- frames: 0,- width: 0,- height: 0,- audio: ptr::null(),- audio_float_count: 0,- audio_channels: 0,- audio_sample_rate: 0,- audio_frames: 0,- fps: 0.0,- seconds_conditioning: 0.0,- seconds_denoise: 0.0,- seconds_video_decode: 0.0,- seconds_audio_decode: 0.0,- seconds_total: 0.0,- steps_computed: 0,- steps_skipped: 0,- };- self.error(unsafe { (self.generation_output)(generation, &mut value) })?;- Ok(value)- }- pub fn frame_rgba8(- &self,- generation: *mut Generation,- index: u32,- width: u32,- height: u32,- ) -> Result<Vec<u8>, String> {- let index = i32::try_from(index)- .map_err(|_| "Frame index does not fit the slopfab API.".to_string())?;- let bytes = (width as usize)- .checked_mul(height as usize)- .and_then(|value| value.checked_mul(4))- .ok_or_else(|| "Frame dimensions do not fit in memory.".to_string())?;- let mut rgba = vec![0_u8; bytes];- self.error(unsafe {- (self.generation_frame_rgba8)(generation, index, rgba.as_mut_ptr(), rgba.len())- })?;- Ok(rgba)- }- pub fn destroy_generation(&self, generation: *mut Generation) {- unsafe { (self.generation_destroy)(generation) }- }- }- fn path_cstring(path: &Path) -> Result<CString, String> {- CString::new(path.to_string_lossy().as_bytes())- .map_err(|_| "A slopfab path contains a null byte.".into())- }-- #[cfg(windows)]- unsafe fn load_library(path: &Path) -> Result<Library, libloading::Error> {- // LOAD_WITH_ALTERED_SEARCH_PATH makes Windows resolve the runtime's- // dynamic dependencies relative to this absolute path instead of the host exe.- const LOAD_WITH_ALTERED_SEARCH_PATH: u32 = 0x0000_0008;- unsafe {- libloading::os::windows::Library::load_with_flags(path, LOAD_WITH_ALTERED_SEARCH_PATH)- .map(Into::into)- }- }-- #[cfg(not(windows))]- unsafe fn load_library(path: &Path) -> Result<Library, libloading::Error> {- unsafe { Library::new(path) }- }- unsafe fn c_string(value: *const c_char) -> String {- if value.is_null() {- String::new()- } else {- unsafe { CStr::from_ptr(value) }- .to_string_lossy()- .into_owned()- }- }-}--#[cfg(test)]-mod tests {- use super::*;-- #[test]- fn animate_requires_video_and_prompt_mode_requires_text() {- let mut configuration = Configuration::from_settings(&BTreeMap::new());- let mut request = GenerationRequest::default();- assert!(configuration.validate_inputs(&request).unwrap_err().contains("prompt"));- configuration.animate = true;- assert!(configuration.validate_inputs(&request).unwrap_err().contains("reference video"));- request.reference_paths.push("image.png".into());- assert!(configuration.validate_inputs(&request).is_err());- request.reference_video_ids.push("video".into());- configuration.validate_inputs(&request).unwrap();- request.still_image = true;- assert!(configuration.validate_inputs(&request).unwrap_err().contains("video generation"));- }-- #[test]- fn animate_recipe_and_audio_selection_reach_bundled_dll() {- let root = tempfile::tempdir().unwrap();- let embedding = root.path().join("conditioning.safetensors");- crate::conditioning::fixture(&embedding);- let settings = BTreeMap::from([("slopfab".into(), ProviderSetting {- enabled: true, model: None,- options: BTreeMap::from([- ("generationMode".into(), ProviderOption::String("animate".into())),- ("promptEmbedding".into(), ProviderOption::String(embedding.to_string_lossy().into_owned())),- ("stepOverride".into(), ProviderOption::Number(4.0)),- ]),- })]);- let configuration = Configuration::from_settings(&settings);- let api = ffi::Api::load(&configuration.dll_path).unwrap();- let id = create_reference_video(2.0, &settings).unwrap();- let result = (|| -> Result<(), String> {- append_reference_video(&id, &vec![127; 64 * 64 * 4], 64, 64, 0.0)?;- let request = GenerationRequest {- job_id: "animate-test".into(), frames: 48, steps: 20, seed: 1,- prompt: "This scene prompt must be ignored by Animate.".into(),- canvas_width: 736, canvas_height: 416, reference_video_ids: vec![id.clone()],- reference_paths: vec!["repainted.png".into()],- ..Default::default()- };- for preserve_audio in [false, true] {- if preserve_audio {- set_reference_video_audio(&id, &vec![0; 32_000 * 2 * 4], 1, 32_000)?;- }- for platform in [ComputePlatform::Cuda13, ComputePlatform::Vulkan] {- for include_models in [false, true] {- let handle = RequestHandle::new(&api)?;- configure_request(&api, handle.0, &request, &configuration, platform, include_models)?;- assert_eq!(api.resolve(handle.0)?.num_model_evaluations, 3);- let description = api.describe(handle.0)?;- assert!(description.contains("fixed 362-token embedding; video then repainted image"));- assert!(description.contains("reference short edge 416"));- assert!(description.contains("video 1.000000 .. 0.600000 (shift 3.0)"));- assert!(description.contains(if preserve_audio { "pinned driving soundtrack" } else { "generated; driving reference soundtrack omitted" }));- assert!(description.contains(&format!("prompt {} characters", ANIMATE_PROMPT.len())));- }- }- }- Ok(())- })();- release_reference_videos(&[id]).unwrap();- result.unwrap();- }-- #[test]- fn animate_duration_limit_resolves_with_bundled_dll() {- let root = tempfile::tempdir().unwrap();- let embedding = root.path().join("conditioning.safetensors");- crate::conditioning::fixture(&embedding);- let settings = BTreeMap::new();- let mut configuration = Configuration::from_settings(&settings);- configuration.animate = true;- configuration.prompt_embedding = Some(embedding);- let api = ffi::Api::load(&configuration.dll_path).unwrap();- let id = create_reference_video(15.0, &settings).unwrap();- let result = (|| -> Result<(), String> {- append_reference_video(&id, &vec![127; 64 * 64 * 4], 64, 64, 0.0)?;- let mut request = GenerationRequest {- prompt: "A dancer.".into(), steps: 4, seed: 1,- canvas_width: 736, canvas_height: 416, reference_video_ids: vec![id.clone()],- reference_paths: vec!["repainted.png".into()],- ..Default::default()- };- for platform in [ComputePlatform::Cuda13, ComputePlatform::Vulkan] {- for include_models in [false, true] {- for (frames, expected) in [(48, 56), (336, 345), (345, 345), (346, 345), (348, 345), (359, 345), (360, 345)] {- request.frames = frames;- let handle = RequestHandle::new(&api)?;- configure_request(&api, handle.0, &request, &configuration, platform, include_models)?;- let plan = api.resolve(handle.0)?;- assert_eq!(plan.aligned_frames, expected, "requested {frames} frames");- assert_eq!(plan.duration_seconds, f64::from(expected) / 24.0);- assert_eq!(scene_frame_window(&request, plan.aligned_frames)?, (0, expected));- }- }- }- request.frames = 361;- let handle = RequestHandle::new(&api)?;- configure_request(&api, handle.0, &request, &configuration, ComputePlatform::Cuda13, false)?;- assert!(api.resolve(handle.0).err().unwrap().contains("at most 15 seconds"));-- // Other generators keep their existing upward alignment at 15 s.- configuration.animate = false;- request.frames = 360;- let handle = RequestHandle::new(&api)?;- configure_request(&api, handle.0, &request, &configuration, ComputePlatform::Cuda13, false)?;- assert_eq!(api.resolve(handle.0)?.aligned_frames, 362);- Ok(())- })();- release_reference_videos(&[id]).unwrap();- result.unwrap();- }-- #[test]- fn animate_reports_missing_runtime_api() {- let mut api = ffi::Api::load(&default_dll_path()).unwrap();- api.disable_animate_for_test();- let handle = RequestHandle::new(&api).unwrap();- assert!(api.set_animate(handle.0, true).unwrap_err().contains("1.10"));- }-- #[test]- fn continuation_uses_22_frames_and_plans_only_the_new_scene() {- // An independent 39-frame, 64x32 archive exercises the real DLL without- // loading model weights. Its sampling window should be 22 + 34 frames.- let root = tempfile::tempdir().unwrap();- let path = root.path().join("previous.safetensors");- let video_bytes = 24 * 96 * 4;- let audio_bytes = 130 * 32 * 4;- let mut header = serde_json::json!({- "__metadata__": { "slopfab_latents": "h3-av-v1", "width": "64", "height": "32", "frames": "39", "fps": "24", "sampled": "1" },- "video_rows": { "dtype": "F32", "shape": [24, 96], "data_offsets": [0, video_bytes] },- "audio_rows": { "dtype": "F32", "shape": [130, 32], "data_offsets": [video_bytes, video_bytes + audio_bytes] },- }).to_string();- while header.len() % 8 != 0 { header.push(' '); }- let mut bytes = (header.len() as u64).to_le_bytes().to_vec();- bytes.extend_from_slice(header.as_bytes());- bytes.resize(bytes.len() + video_bytes + audio_bytes, 0);- std::fs::write(&path, bytes).unwrap();- let mut request = GenerationRequest {- job_id: "continued".into(), prompt: "The camera keeps moving.".into(),- frames: 18, steps: 4, seed: 1, canvas_width: 64, canvas_height: 32,- continuation_path: Some(path.clone()), save_latents_path: Some(root.path().join("next.safetensors")),- ..Default::default()- };- let settings = BTreeMap::new();- let configuration = Configuration::from_settings(&settings);- let api = ffi::Api::load(&configuration.dll_path).unwrap();- for platform in [ComputePlatform::Cuda13, ComputePlatform::Vulkan] {- let handle = RequestHandle::new(&api).unwrap();- configure_request(&api, handle.0, &request, &configuration, platform, true).unwrap();- let raw = api.resolve(handle.0).unwrap();- assert_eq!(raw.aligned_frames, 73);- assert_eq!(raw.latent_frames, 17); // 56-frame window, including 22 overlap.- assert_eq!(scene_frame_window(&request, raw.aligned_frames).unwrap(), (39, 34));- }- let plan = resolve_plan(&request, &settings).unwrap();- assert_eq!(plan.aligned_frames, 34);- assert!((plan.duration_seconds - 34.0 / 24.0).abs() < 1e-6);- assert!(!request.save_latents_path.as_ref().unwrap().exists()); // Planning never writes.- request.canvas_width = 128;- assert!(resolve_plan(&request, &settings).is_err());- request.canvas_width = 64;- std::fs::write(&path, b"invalid archive").unwrap();- assert!(resolve_plan(&request, &settings).is_err());- }-- #[test]- fn continuation_video_and_audio_skip_the_cumulative_source() {- let mut request = GenerationRequest { frames: 119, continuation_path: Some("source".into()), ..Default::default() };- assert_eq!(scene_frame_window(&request, 243).unwrap(), (124, 119));- // A further continuation skips the full joined source, not just its last scene.- assert_eq!(scene_frame_window(&request, 362).unwrap(), (243, 119));- assert_eq!(scene_audio_offset(124, 24.0, 2, 48_000, 972_000).unwrap(), 496_000);- assert_eq!(scene_audio_offset(243, 24.0, 2, 44_100, 2_000_000).unwrap(), 893_026);- assert_eq!(scene_audio_offset(124, 24.0, 0, 0, 0).unwrap(), 0);- assert!(scene_audio_offset(124, 24.0, 2, 48_000, 100).is_err());- assert!(scene_frame_window(&request, 120).is_err());- request.continuation_path = None;- assert_eq!(scene_frame_window(&request, 124).unwrap(), (0, 124));- }-- #[test]- fn refmods_attach_for_plans_and_icons_or_report_an_older_dll() {- let configuration = Configuration::from_settings(&BTreeMap::new());- let api = ffi::Api::load(&configuration.dll_path).unwrap();- let root = tempfile::tempdir().unwrap();- let file = root.path().join("person.safetensors");- let header = serde_json::json!({- "__metadata__": { "refmod_meta": serde_json::json!({ "kind": "image", "name": "Person", "_format_version": 4 }).to_string() },- "latent": { "dtype": "F32", "shape": [1, 24, 1, 2, 2], "data_offsets": [0, 384] }- }).to_string();- let mut bytes = (header.len() as u64).to_le_bytes().to_vec();- bytes.extend_from_slice(header.as_bytes());- for _ in 0..96 { bytes.extend_from_slice(&0.25_f32.to_le_bytes()); }- std::fs::write(&file, bytes).unwrap();- let mut request = GenerationRequest {- job_id: "refmod-test".into(), prompt: "A portrait.".into(), still_image: true, frames: 1,- steps: 20, seed: 1, canvas_width: 768, canvas_height: 768,- reference_paths: Vec::new(), reference_video_ids: Vec::new(),- refmods: vec![RefmodInput { path: file.to_string_lossy().into_owned(), strength: 0.7, copies: 2 }],- ..Default::default()- };- let probe = RequestHandle::new(&api).unwrap();- let added = api.add_refmod(probe.0, &file, 0.7, 2);- if let Err(error) = &added {- assert!(error.contains("does not support refmods"), "{error}");- eprintln!("Refmod export not yet available in the installed DLL; checked compatibility error.");- assert!(configure_request(&api, probe.0, &request, &configuration, ComputePlatform::Vulkan, false).unwrap_err().contains("does not support refmods"));- } else {- for platform in [ComputePlatform::Cuda13, ComputePlatform::Vulkan] {- for still_image in [false, true] {- request.still_image = still_image;- request.frames = if still_image { 1 } else { 120 };- for include_models in [false, true] {- let handle = RequestHandle::new(&api).unwrap();- configure_request(&api, handle.0, &request, &configuration, platform, include_models).unwrap();- let plan = api.resolve(handle.0).unwrap();- assert!(plan.aligned_frames >= request.frames);- }- }- }- }- request.refmods[0].strength = 0.0;- std::fs::remove_file(&file).unwrap();- let handle = RequestHandle::new(&api).unwrap();- configure_request(&api, handle.0, &request, &configuration, ComputePlatform::Vulkan, true).unwrap();- request.refmods[0].copies = 11;- assert!(configure_request(&api, handle.0, &request, &configuration, ComputePlatform::Vulkan, false).is_err());- }-- #[test]- fn ordered_loras_and_step_override_reach_cuda_and_vulkan_requests() {- let root = tempfile::tempdir().unwrap();- let paths = [root.path().join("style.safetensors"), root.path().join("TaoMate.safetensors")];- for path in &paths { std::fs::write(path, b"planning does not load weights").unwrap(); }- let settings = BTreeMap::from([("slopfab".into(), ProviderSetting {- enabled: true, model: None,- options: BTreeMap::from([- ("loras".into(), ProviderOption::String(serde_json::json!([- { "path": paths[0], "strength": -0.5 },- { "path": paths[1], "strength": 1.0 },- ]).to_string())),- ("stepOverride".into(), ProviderOption::Number(8.0)),- ]),- })]);- let configuration = Configuration::from_settings(&settings);- let api = ffi::Api::load(&configuration.dll_path).unwrap();- let request = GenerationRequest {- job_id: "lora-test".into(), prompt: "A quiet harbour.".into(),- frames: 120, still_image: false, steps: 20, seed: 1,- canvas_width: 736, canvas_height: 416,- reference_paths: Vec::new(), reference_video_ids: Vec::new(),- refmods: Vec::new(),- ..Default::default()- };- for platform in [ComputePlatform::Cuda13, ComputePlatform::Vulkan] {- for include_models in [false, true] {- let handle = RequestHandle::new(&api).unwrap();- configure_request(&api, handle.0, &request, &configuration, platform, include_models).unwrap();- assert_eq!(api.resolve(handle.0).unwrap().num_model_evaluations, 7);- let description = api.describe(handle.0).unwrap();- assert!(!description.contains("taomate-3step"));- assert!(description.find("style.safetensors").unwrap() < description.find("TaoMate.safetensors").unwrap());- assert!(description.contains("strength -0.5"));- }- }- let invalid = BTreeMap::from([("slopfab".into(), ProviderSetting {- enabled: true, model: None,- options: BTreeMap::from([("loras".into(), ProviderOption::String("invalid JSON".into()))]),- })]);- assert!(Configuration::from_settings(&invalid).loras.is_err());- std::fs::remove_file(&paths[0]).unwrap();- let handle = RequestHandle::new(&api).unwrap();- assert!(configure_request(&api, handle.0, &request, &configuration, ComputePlatform::Vulkan, true).unwrap_err().contains("LoRA file is missing"));- }-- #[test]- fn vulkan_video_references_prepare_and_attach_for_planning_and_generation() {- let settings = BTreeMap::from([("slopfab".into(), ProviderSetting {- enabled: true,- model: None,- options: BTreeMap::from([("inferenceBackend".into(), ProviderOption::String("vulkan".into()))]),- })]);- let id = create_reference_video(2.0, &settings).unwrap();- let result = (|| -> Result<(), String> {- append_reference_video(&id, &vec![127; 64 * 64 * 4], 64, 64, 0.0)?;- set_reference_video_audio(&id, &vec![0; 32_000 * 2 * 4], 1, 32_000)?;- let request = GenerationRequest {- job_id: "vulkan-reference-test".into(), prompt: "A scene using <Video 1>.".into(),- frames: 120, still_image: false, steps: 12, seed: 1,- canvas_width: 736, canvas_height: 416,- reference_paths: Vec::new(), reference_video_ids: vec![id.clone()],- refmods: Vec::new(),- ..Default::default()- };- let configuration = Configuration::from_settings(&settings);- let api = ffi::Api::load(&configuration.dll_path)?;- for include_models in [false, true] {- let handle = RequestHandle::new(&api)?;- configure_request(&api, handle.0, &request, &configuration, ComputePlatform::Vulkan, include_models)?;- assert!(api.resolve(handle.0)?.aligned_frames > 0);- }- Ok(())- })();- release_reference_videos(&[id]).unwrap();- result.unwrap();- }-- #[test]- fn video_reference_c_api_copies_inputs_and_retains_attached_snapshot() {- let path = default_dll_path();- let api = ffi::Api::load(&path).unwrap();- let request = RequestHandle::new(&api).unwrap();- api.set_prompt(request.0, "A scene using <Video 1>.").unwrap();- api.set_frames(request.0, 120).unwrap();- api.set_resolution(request.0, 736, 416).unwrap();- let mut video = ffi::ReferenceVideoHandle::new(ffi::Api::load(&path).unwrap(), 2.0, path.clone()).unwrap();- let pixels = vec![127_u8; 64 * 64 * 4];- video.append(&pixels, 64, 64, 0.0).unwrap();- assert!(video.append(&pixels, 64, 64, 0.0).is_err());- assert!(video.append(&pixels[..10], 64, 64, 0.5).is_err());- video.append(&pixels, 64, 64, 1.0).unwrap();- video.set_audio(&vec![0.0; 32_000 * 2], 1, 32_000).unwrap();- video.attach(request.0, &path).unwrap();- drop(video);- drop(pixels);- // Plan resolution reads the retained video/audio snapshot without a- // neural model or a CUDA allocation, even after the host releases it.- let plan = api.resolve(request.0).unwrap();- assert!(plan.aligned_frames > 0);- }-- #[test]- fn absent_dll_is_an_explicit_disabled_state() {- let mut settings = BTreeMap::new();- settings.insert(- "slopfab".into(),- ProviderSetting {- enabled: true,- model: None,- options: BTreeMap::from([(- "dllPath".into(),- ProviderOption::String("Z:/definitely-absent/slopfab.dll".into()),- )]),- },- );- let status = status(&settings);- assert_eq!(status.state, "runtimeMissing");- }- #[test]- fn compatible_major_version_decodes_from_packed_value() {- let packed = (1_u32 << 24) | (37 << 12) | 5;- assert_eq!(packed >> 24, EXPECTED_CAPI_MAJOR);- }- #[test]- fn platform_selection_prefers_supported_cuda_majors_and_falls_back_to_vulkan() {- assert_eq!(platform_from_cuda_probe(Ok(13)), ComputePlatform::Cuda13);- assert_eq!(platform_from_cuda_probe(Ok(12)), ComputePlatform::Cuda12);- assert_eq!(platform_from_cuda_probe(Ok(11)), ComputePlatform::Vulkan);- assert_eq!(- platform_from_cuda_probe(Err("no CUDA toolkit".into())),- ComputePlatform::Vulkan- );- assert_eq!(ComputePlatform::Cuda13.backend(), 0);- assert_eq!(ComputePlatform::Vulkan.backend(), 1);- }-- #[test]- fn backend_preference_cannot_enable_cuda_without_cuda_hardware() {- let mut config = Configuration::from_settings(&BTreeMap::new());- assert_eq!(config.platform(ComputePlatform::Cuda13), ComputePlatform::Cuda13);- assert_eq!(config.platform(ComputePlatform::Cuda12), ComputePlatform::Cuda12);- assert_eq!(config.platform(ComputePlatform::Vulkan), ComputePlatform::Vulkan);- config.vulkan = true;- for detected in [ComputePlatform::Cuda13, ComputePlatform::Cuda12, ComputePlatform::Vulkan] {- assert_eq!(config.platform(detected), ComputePlatform::Vulkan);- }- }-- #[test]- fn attention_defaults_to_sage_and_is_independent_of_backend() {- assert_eq!(Configuration::from_settings(&BTreeMap::new()).attention, "sage2");- for backend in ["cuda", "vulkan"] {- for attention in ["exact", "flash2", "sage2", "invalid"] {- let settings = serde_json::from_value(serde_json::json!({- "slopfab": { "enabled": true, "model": null,- "options": { "inferenceBackend": backend, "attention": attention } }- })).unwrap();- let config = Configuration::from_settings(&settings);- let expected = if attention == "invalid" { "sage2" } else { attention };- assert_eq!(config.attention, expected);- assert_eq!(config.vulkan, backend == "vulkan");- assert!(config.timing_profile("1.4.0", ComputePlatform::Vulkan).contains(&format!("attention={expected}|")));- }- }- }-- #[test]- #[cfg(windows)]- fn bundled_dll_reports_a_compatible_api() {- assert!(default_dll_path().is_file(), "The build must stage slopfab.dll beside the executable.");- let runtime = status(&BTreeMap::new());- assert_ne!(runtime.state, "runtimeMissing", "{}", runtime.detail);- assert_ne!(runtime.state, "incompatible", "{}", runtime.detail);- assert!(runtime- .version- .as_deref()- .is_some_and(|value| value.starts_with("1.")));- assert!(matches!(- runtime.platform,- Some("CUDA 13" | "CUDA 12" | "Vulkan")- ));- }- #[test]- fn stage_ids_are_normalized_without_assuming_an_enum() {- assert_eq!(stage_name(4), "denoising");- assert_eq!(stage_name(99), "unknown");- }-- #[test]- fn installed_dll_resolves_icons_as_one_still_frame_when_present() {- if !default_dll_path().is_file() { return; }- let request: GenerationRequest = serde_json::from_value(serde_json::json!({- "jobId": "icon-plan", "prompt": "A red toy car", "frames": 1, "stillImage": true,- "steps": 20, "seed": 1, "canvasWidth": 768, "canvasHeight": 768- })).unwrap();- let plan = resolve_plan(&request, &BTreeMap::new()).unwrap();- assert_eq!(plan.aligned_frames, 1);- assert_eq!(plan.latent_frames, 1);- assert_eq!((plan.canvas_width, plan.canvas_height), (768, 768));- }-- #[test]- fn bundled_dll_accepts_all_attention_modes_on_both_backends() {- if !default_dll_path().is_file() { return; }- let api = ffi::Api::load(&default_dll_path()).unwrap();- let request: GenerationRequest = serde_json::from_value(serde_json::json!({- "jobId": "attention-plan", "prompt": "A red toy car", "frames": 1, "stillImage": true,- "steps": 20, "seed": 1, "canvasWidth": 768, "canvasHeight": 768- })).unwrap();- let mut config = Configuration::from_settings(&BTreeMap::new());- for platform in [ComputePlatform::Cuda13, ComputePlatform::Vulkan] {- for attention in ["exact", "flash2", "sage2"] {- config.attention = attention;- let handle = RequestHandle::new(&api).unwrap();- configure_request(&api, handle.0, &request, &config, platform, false).unwrap();- assert_eq!(api.resolve(handle.0).unwrap().aligned_frames, 1);- }- }- }- #[test]- fn generation_seed_accepts_random_and_non_negative_values() {- assert!(generation_seed(-1).is_ok());- assert_eq!(generation_seed(0).unwrap(), 0);- assert_eq!(generation_seed(482_091).unwrap(), 482_091);- assert!(generation_seed(-2).is_err());- }-}diff --git a/src-tauri/src/slopfab/config.rs b/src-tauri/src/slopfab/config.rsnew file mode 100644index 0000000..5ba1420--- /dev/null+++ b/src-tauri/src/slopfab/config.rs@@ -0,0 +1,123 @@+use super::*;++#[derive(Debug, Clone, Deserialize)]+pub(super) struct LoraAdapter {+ pub(super) path: String,+ pub(super) strength: f32,+}++#[derive(Clone)]+pub(super) struct Configuration {+ pub(super) dll_path: PathBuf,+ pub(super) vulkan: bool,+ pub(super) attention: &'static str,+ pub(super) animate: bool,+ pub(super) prompt_embedding: Option<PathBuf>,+ pub(super) models: [(i32, &'static str, Option<PathBuf>); 5],+ pub(super) loras: Result<Vec<LoraAdapter>, String>,+ pub(super) step_override: Result<Option<i32>, String>,+}++impl Configuration {+ pub(super) fn from_settings(settings: &BTreeMap<String, ProviderSetting>) -> Self {+ let slopfab = settings.get("slopfab");+ let string_option = |name: &str| {+ slopfab.and_then(|setting| match setting.options.get(name) {+ Some(ProviderOption::String(value)) => Some(value.as_str()),+ _ => None,+ })+ };+ let option = |name: &str| {+ slopfab.and_then(|setting| match setting.options.get(name) {+ Some(ProviderOption::String(value)) if !value.trim().is_empty() => {+ Some(PathBuf::from(value))+ }+ _ => None,+ })+ };+ Self {+ // `dllPath` is no longer written by the app and has no settings UI.+ // It is still READ so a project or a test that carries one keeps+ // working; with none, use the DLL beside the executable.+ dll_path: option("dllPath").unwrap_or_else(default_dll_path),+ vulkan: string_option("inferenceBackend") == Some("vulkan"),+ animate: string_option("generationMode") == Some("animate"),+ prompt_embedding: option("promptEmbedding"),+ attention: match string_option("attention") {+ Some("exact") => "exact",+ Some("flash2") => "flash2",+ _ => "sage2",+ },+ models: [+ (0, "transformer", option("transformer")),+ (1, "textEncoder", option("textEncoder")),+ (2, "tokenizer", option("tokenizer")),+ (3, "videoVae", option("videoVae")),+ (4, "audioVae", option("audioVae")),+ ],+ loras: match slopfab.and_then(|setting| setting.options.get("loras")) {+ None => Ok(Vec::new()),+ Some(ProviderOption::String(value)) => serde_json::from_str::<Vec<LoraAdapter>>(value)+ .map_err(|error| format!("Invalid LoRA settings: {error}")),+ _ => Err("LoRA settings must be a JSON array of paths and strengths.".into()),+ },+ step_override: match slopfab.and_then(|setting| setting.options.get("stepOverride")) {+ None => Ok(None),+ Some(ProviderOption::Number(value)) if value.is_finite() && value.fract() == 0.0 && *value >= 2.0 && *value <= i32::MAX as f64 => Ok(Some(*value as i32)),+ _ => Err("LoRA step override must be a whole number from 2 to 2147483647.".into()),+ },+ }+ }++ pub(super) fn validate_inputs(&self, request: &GenerationRequest) -> Result<(), String> {+ if self.animate {+ if request.still_image {+ return Err("Animate requires video generation.".into());+ }+ if request.reference_video_ids.len() != 1 {+ return Err("Animate requires exactly one reference video.".into());+ }+ if request.reference_paths.len() != 1 {+ return Err("Animate requires exactly one repainted frame of the driving scene.".into());+ }+ if request.continuation_path.is_some() || !request.refmods.is_empty() {+ return Err("Animate does not support scene continuation or refmods.".into());+ }+ } else if request.prompt.trim().is_empty() {+ return Err("Generation prompt cannot be empty.".into());+ }+ Ok(())+ }++ pub(super) fn generation_steps(&self, fallback: i32) -> Result<i32, String> {+ self.step_override.clone().map(|steps| steps.unwrap_or(fallback))+ }++ pub(super) fn platform(&self, detected: ComputePlatform) -> ComputePlatform {+ if self.vulkan {+ ComputePlatform::Vulkan+ } else {+ detected+ }+ }++ pub(super) fn timing_profile(&self, version: &str, platform: ComputePlatform) -> String {+ let models = self+ .models+ .iter()+ .filter_map(|(_, id, path)| {+ path.as_ref().map(|path| {+ let file = path+ .file_name()+ .and_then(|value| value.to_str())+ .unwrap_or("unknown");+ format!("{id}={file}")+ })+ })+ .collect::<Vec<_>>()+ .join(";");+ let adapters = self.loras.as_ref().map(|loras| loras.iter()+ .map(|lora| format!("{}@{}", lora.path, lora.strength)).collect::<Vec<_>>().join(";")).unwrap_or_default();+ format!("slopfab={version}|platform={}|attention={}|{models}|loras={adapters}|steps={:?}", platform.label(), self.attention, self.step_override)+ }+}diff --git a/src-tauri/src/slopfab/events.rs b/src-tauri/src/slopfab/events.rsnew file mode 100644index 0000000..22e27e3--- /dev/null+++ b/src-tauri/src/slopfab/events.rs@@ -0,0 +1,63 @@+use super::*;++pub type EventSink = Arc<dyn Fn(NativeEvent) + Send + Sync>;+pub enum NativeEvent { Job(JobEvent), Progress(ProgressEvent) }++pub(super) struct CallbackContext {+ pub(super) events: EventSink,+ pub(super) job_id: String,+ pub(super) frames: i32,+ pub(super) canvas_width: i32,+ pub(super) canvas_height: i32,+ pub(super) reference_count: usize,+ pub(super) timing_profile: String,+ pub(super) planned_steps: i32,+}++pub(super) fn progress_sink(context: CallbackContext) -> ffi::ProgressSink {+ Box::new(move |progress| {+ diagnostics::debug(+ "slopfab",+ "generation.progress",+ "slopfab reported progress.",+ serde_json::json!({+ "jobId": context.job_id,+ "stage": stage_name(progress.stage),+ "step": progress.step,+ "totalSteps": progress.total_steps,+ "plannedSteps": context.planned_steps,+ "elapsedSeconds": progress.elapsed_seconds,+ }),+ );+ (context.events)(NativeEvent::Progress(+ ProgressEvent {+ job_id: context.job_id.clone(),+ stage: stage_name(progress.stage),+ step: progress.step,+ total_steps: progress.total_steps,+ planned_steps: context.planned_steps,+ elapsed_seconds: progress.elapsed_seconds,+ frames: context.frames,+ canvas_width: context.canvas_width,+ canvas_height: context.canvas_height,+ reference_count: context.reference_count,+ timing_profile: context.timing_profile.clone(),+ },+ ));++ })+}+pub(super) fn stage_name(stage: i32) -> &'static str {+ match stage {+ 0 => "starting",+ 1 => "references",+ 2 => "conditioning",+ 3 => "transformerLoad",+ 4 => "denoising",+ 5 => "videoDecode",+ 6 => "audioDecode",+ 7 => "delivering",+ 8 => "finished",+ _ => "unknown",+ }+}diff --git a/src-tauri/src/slopfab/ffi/handles.rs b/src-tauri/src/slopfab/ffi/handles.rsnew file mode 100644index 0000000..fc64c2c--- /dev/null+++ b/src-tauri/src/slopfab/ffi/handles.rs@@ -0,0 +1,124 @@+use super::{raw, Api, RequestHandle};+use std::{ffi::c_void, ptr::NonNull};++pub type ProgressSink = Box<dyn Fn(&super::Progress) + Send + Sync>;++struct Callback(ProgressSink);+unsafe extern "C" fn progress_callback(progress: *const raw::Progress, userdata: *mut c_void) {+ // Native callbacks may arrive on worker threads. Never unwind through C.+ let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ if !progress.is_null() && !userdata.is_null() {+ let callback = unsafe { &*userdata.cast::<Callback>() };+ (callback.0)(unsafe { &*progress });+ }+ }));+}++/// Owns both the loaded library and callback until native workers have stopped.+pub struct GenerationHandle {+ api: Api,+ pointer: NonNull<raw::Generation>,+ _callback: Option<Box<Callback>>,+ terminal: bool,+ succeeded: bool,+}++impl GenerationHandle {+ pub(super) fn start(request: &RequestHandle, sink: Option<ProgressSink>) -> Result<Self, String> {+ let mut callback = sink.map(|sink| Box::new(Callback(sink)));+ let userdata = callback.as_mut().map_or(std::ptr::null_mut(), |value| {+ (&mut **value as *mut Callback).cast()+ });+ let pointer = request.api.inner.start(+ request.pointer.as_ptr(),+ callback.as_ref().map(|_| progress_callback as unsafe extern "C" fn(_, _)),+ userdata,+ )?;+ let pointer = NonNull::new(pointer).ok_or("Slopfab returned an empty generation handle.")?;+ Ok(Self { api: request.api.clone(), pointer, _callback: callback, terminal: false, succeeded: false })+ }++ pub fn cancel(&self) { self.api.inner.cancel(self.pointer.as_ptr()); }++ /// A false result means the wait timed out; all other statuses are terminal.+ pub fn wait(&mut self, timeout: i32) -> Result<bool, String> {+ let code = self.api.inner.wait(self.pointer.as_ptr(), timeout);+ if code == super::super::NOT_READY { return Ok(false); }+ self.terminal = true;+ self.succeeded = code == 0;+ if code == 0 { return Ok(true); }+ if code == super::super::CANCELLED {+ return Err("Generation cancelled at the next slopfab checkpoint.".into());+ }+ let detail = self.api.inner.generation_error(self.pointer.as_ptr());+ Err(if detail.is_empty() { format!("slopfab generation failed with status {code}.") } else { detail })+ }++ pub fn finish(self) -> Result<FinishedGeneration, String> {+ if !self.succeeded { return Err("Generation has not completed successfully.".into()); }+ Ok(FinishedGeneration(self))+ }+}++impl Drop for GenerationHandle {+ fn drop(&mut self) {+ if !self.terminal {+ self.cancel();+ // Joining precedes destruction of callback memory and the library.+ while self.api.inner.wait(self.pointer.as_ptr(), -1) == super::super::NOT_READY {}+ }+ self.api.inner.destroy_generation(self.pointer.as_ptr());+ }+}++pub struct FinishedGeneration(GenerationHandle);+// Only successful, joined generations cross threads. The render store serializes+// access and the native API exposes immutable output after wait completes.+unsafe impl Send for FinishedGeneration {}++/// Pointer-free snapshot; audio is copied once, video stays in the native handle.+pub struct Output {+ pub video_float_count: usize,+ pub channels: i32,+ pub frames: i32,+ pub width: i32,+ pub height: i32,+ pub audio: Vec<f32>,+ pub audio_channels: i32,+ pub audio_sample_rate: i32,+ pub fps: f64,+ pub seconds_conditioning: f64,+ pub seconds_denoise: f64,+ pub seconds_video_decode: f64,+ pub seconds_audio_decode: f64,+ pub seconds_total: f64,+ pub steps_computed: i32,+ pub steps_skipped: i32,+}++impl FinishedGeneration {+ pub fn output(&self) -> Result<Output, String> {+ let value = self.0.api.inner.output(self.0.pointer.as_ptr())?;+ if value.audio_float_count > isize::MAX as usize / std::mem::size_of::<f32>()+ || (value.audio.is_null() && value.audio_float_count != 0) {+ return Err("Slopfab returned an invalid audio buffer.".into());+ }+ let audio = if value.audio_float_count == 0 { Vec::new() } else {+ // The successful generation owns this buffer for the whole borrow.+ unsafe { std::slice::from_raw_parts(value.audio, value.audio_float_count) }.to_vec()+ };+ Ok(Output {+ video_float_count: value.video_float_count, channels: value.channels,+ frames: value.frames, width: value.width, height: value.height, audio,+ audio_channels: value.audio_channels, audio_sample_rate: value.audio_sample_rate,+ fps: value.fps, seconds_conditioning: value.seconds_conditioning,+ seconds_denoise: value.seconds_denoise, seconds_video_decode: value.seconds_video_decode,+ seconds_audio_decode: value.seconds_audio_decode, seconds_total: value.seconds_total,+ steps_computed: value.steps_computed, steps_skipped: value.steps_skipped,+ })+ }++ pub fn frame_rgba8(&self, index: u32, width: u32, height: u32) -> Result<Vec<u8>, String> {+ self.0.api.inner.frame_rgba8(self.0.pointer.as_ptr(), index, width, height)+ }+}diff --git a/src-tauri/src/slopfab/ffi/mod.rs b/src-tauri/src/slopfab/ffi/mod.rsnew file mode 100644index 0000000..6680a99--- /dev/null+++ b/src-tauri/src/slopfab/ffi/mod.rs@@ -0,0 +1,71 @@+mod raw;+mod handles;+pub use handles::{GenerationHandle, FinishedGeneration, ProgressSink};+pub use raw::{Plan, Progress, has_cuda_device, gpu_devices, cuda_device_names};+use std::{path::{Path,PathBuf}, sync::Arc, ptr::NonNull};++#[derive(Clone)]+pub struct Api { inner: Arc<raw::Api>, path: PathBuf }+impl Api {+ pub fn load(path: &Path) -> Result<Self,String> {+ let inner=raw::Api::load(path)?;+ inner.version()?;+ Ok(Self {inner:Arc::new(inner),path:path.to_owned()})+ }+ pub fn version(&self)->Result<String,String>{self.inner.version()}+ pub fn set_cuda_version(&self,v:&str)->Result<(),String>{self.inner.set_cuda_version(v)}+ pub fn cuda_loaded_major(&self)->Result<i32,String>{self.inner.cuda_loaded_major()}+ pub fn clear_reused_models(&self)->Result<(),String>{self.inner.clear_reused_models()}+ #[cfg(test)] pub fn disable_animate_for_test(&mut self) { Arc::get_mut(&mut self.inner).expect("configure API before creating handles").disable_animate_for_test(); }+ fn check(&self,r:&RequestHandle)->Result<(),String>{+ if Arc::ptr_eq(&self.inner,&r.api.inner){Ok(())}else{Err("Request belongs to a different runtime instance.".into())}+ }+pub fn set_prompt(&self, r: &RequestHandle, v: &str) -> Result<(), String> { self.check(r)?; self.inner.set_prompt(r.pointer.as_ptr(), v) }+pub fn set_animate(&self, r: &RequestHandle, preserve_audio: bool) -> Result<(), String> { self.check(r)?; self.inner.set_animate(r.pointer.as_ptr(), preserve_audio) }+pub fn set_prompt_embedding(&self, r: &RequestHandle, path: &Path) -> Result<(), String> { self.check(r)?; self.inner.set_prompt_embedding(r.pointer.as_ptr(), path) }+pub fn set_resolution(&self, r: &RequestHandle, w: i32, h: i32) -> Result<(), String> { self.check(r)?; self.inner.set_resolution(r.pointer.as_ptr(), w, h) }+pub fn set_frames(&self, r: &RequestHandle, v: i32) -> Result<(), String> { self.check(r)?; self.inner.set_frames(r.pointer.as_ptr(), v) }+pub fn set_still_image(&self, r: &RequestHandle) -> Result<(), String> { self.check(r)?; self.inner.set_still_image(r.pointer.as_ptr()) }+pub fn set_save_latents(&self, r: &RequestHandle, path: &Path) -> Result<(), String> { self.check(r)?; self.inner.set_save_latents(r.pointer.as_ptr(), path) }+pub fn set_continuation_file(&self, r: &RequestHandle, path: &Path, overlap: i32) -> Result<(), String> { self.check(r)?; self.inner.set_continuation_file(r.pointer.as_ptr(), path, overlap) }+pub fn set_steps(&self, r: &RequestHandle, v: i32) -> Result<(), String> { self.check(r)?; self.inner.set_steps(r.pointer.as_ptr(), v) }+pub fn set_seed(&self, r: &RequestHandle, v: u64) -> Result<(), String> { self.check(r)?; self.inner.set_seed(r.pointer.as_ptr(), v) }+pub fn set_verbose(&self, r: &RequestHandle, v: bool) -> Result<(), String> { self.check(r)?; self.inner.set_verbose(r.pointer.as_ptr(), v) }+pub fn set_reuse_models(&self, r: &RequestHandle, v: bool) -> Result<(), String> { self.check(r)?; self.inner.set_reuse_models(r.pointer.as_ptr(), v) }+pub fn set_attention(&self, r: &RequestHandle, v: &str) -> Result<(), String> { self.check(r)?; self.inner.set_attention(r.pointer.as_ptr(), v) }+pub fn set_inference_backend(&self, r: &RequestHandle, backend: i32) -> Result<(), String> { self.check(r)?; self.inner.set_inference_backend(r.pointer.as_ptr(), backend) }+pub fn set_model(&self, r: &RequestHandle, id: i32, path: &Path) -> Result<(), String> { self.check(r)?; self.inner.set_model(r.pointer.as_ptr(), id, path) }+pub fn add_reference(&self, r: &RequestHandle, path: &Path) -> Result<(), String> { self.check(r)?; self.inner.add_reference(r.pointer.as_ptr(), path) }+pub fn add_lora(&self, r: &RequestHandle, path: &Path, strength: f32) -> Result<(), String> { self.check(r)?; self.inner.add_lora(r.pointer.as_ptr(), path, strength) }+pub fn add_refmod(&self, r: &RequestHandle, path: &Path, strength: f32, copies: i32) -> Result<(), String> { self.check(r)?; self.inner.add_refmod(r.pointer.as_ptr(), path, strength, copies) }+pub fn resolve(&self, request: &RequestHandle) -> Result<Plan, String> { self.check(request)?; self.inner.resolve(request.pointer.as_ptr()) }+pub fn describe(&self, request: &RequestHandle) -> Result<String, String> { self.check(request)?; self.inner.describe(request.pointer.as_ptr()) }++}++/// Owns its library; native requests cannot outlive the DLL or cross instances.+pub struct RequestHandle { api: Api, pointer: NonNull<raw::Request> }+impl RequestHandle {+ pub fn new(api:&Api)->Result<Self,String>{+ let pointer=NonNull::new(api.inner.create_request()?).ok_or("Empty native request.")?;+ Ok(Self {api:api.clone(),pointer})+ }+ pub fn start(&self,callback:Option<ProgressSink>)->Result<GenerationHandle,String>{GenerationHandle::start(self,callback)}+}+impl Drop for RequestHandle {fn drop(&mut self){self.api.inner.destroy_request(self.pointer.as_ptr());}}++pub struct ReferenceVideoHandle(raw::ReferenceVideoHandle);+impl ReferenceVideoHandle {+ pub fn new(api:Api,duration:f64,path:PathBuf)->Result<Self,String>{+ if api.path != path {return Err("Reference video runtime mismatch.".into());}+ raw::ReferenceVideoHandle::new(api.inner,duration,path).map(Self)+ }+ pub fn append(&mut self,bytes:&[u8],w:i32,h:i32,time:f64)->Result<(),String>{self.0.append(bytes,w,h,time)}+ pub fn set_audio(&mut self,samples:&[f32],channels:i32,rate:i32)->Result<(),String>{self.0.set_audio(samples,channels,rate)}+ pub fn has_audio(&self)->bool{self.0.has_audio()}+ pub fn attach(&self,request:&RequestHandle,path:&Path)->Result<(),String>{+ if request.api.path != path {return Err("Reference video runtime mismatch.".into());}+ self.0.attach(request.pointer.as_ptr(),path)+ }+}+diff --git a/src-tauri/src/slopfab/ffi/raw.rs b/src-tauri/src/slopfab/ffi/raw.rsnew file mode 100644index 0000000..1ac1afd--- /dev/null+++ b/src-tauri/src/slopfab/ffi/raw.rs@@ -0,0 +1,617 @@+use libloading::Library;+use std::{+ ffi::{c_char, c_void, CStr, CString},+ path::Path,+ ptr,+};++pub enum Request {}+enum ReferenceVideo {}+type VideoCreate = unsafe extern "C" fn(f64, *mut *mut ReferenceVideo) -> i32;+type VideoDestroy = unsafe extern "C" fn(*mut ReferenceVideo);+type VideoAppend = unsafe extern "C" fn(*mut ReferenceVideo, *const u8, usize, i32, i32, usize, f64) -> i32;+type VideoAudio = unsafe extern "C" fn(*mut ReferenceVideo, *const f32, usize, i32, i32, f64) -> i32;+type VideoAttach = unsafe extern "C" fn(*mut Request, *const ReferenceVideo) -> i32;++pub struct ReferenceVideoHandle {+ api: std::sync::Arc<Api>,+ handle: *mut ReferenceVideo,+ destroy: VideoDestroy,+ append_rgba: VideoAppend,+ audio: VideoAudio,+ attach_video: VideoAttach,+ dll_path: std::path::PathBuf,+ frames: usize,+ has_audio: bool,+}++// The registry mutex serializes access. The DLL handle and all copied input+// buffers outlive every call, and attached snapshots own their references.+unsafe impl Send for ReferenceVideoHandle {}++impl ReferenceVideoHandle {+ pub fn new(api: std::sync::Arc<Api>, duration: f64, dll_path: std::path::PathBuf) -> Result<Self, String> {+ unsafe {+ let missing = |error| format!("This slopfab.dll does not support video references. Update the runtime: {error}");+ let create = *api._library.get::<VideoCreate>(b"slopfab_reference_video_create\0").map_err(missing)?;+ let destroy = *api._library.get::<VideoDestroy>(b"slopfab_reference_video_destroy\0").map_err(missing)?;+ let append_rgba = *api._library.get::<VideoAppend>(b"slopfab_reference_video_append_rgba8\0").map_err(missing)?;+ let audio = *api._library.get::<VideoAudio>(b"slopfab_reference_video_set_audio_f32\0").map_err(missing)?;+ let attach_video = *api._library.get::<VideoAttach>(b"slopfab_request_add_reference_video\0").map_err(missing)?;+ let mut handle = std::ptr::null_mut();+ api.error(create(duration, &mut handle))?;+ if handle.is_null() { return Err("slopfab returned an empty reference video handle.".into()); }+ Ok(Self { api, handle, destroy, append_rgba, audio, attach_video, dll_path, frames: 0, has_audio: false })+ }+ }+ pub fn append(&mut self, bytes: &[u8], width: i32, height: i32, timestamp: f64) -> Result<(), String> {+ if width <= 0 || height <= 0 || width > 1024 || height > 1024 || self.frames >= 361 {+ return Err("Reference video preparation exceeds the frame or dimension limit.".into());+ }+ self.api.error(unsafe { (self.append_rgba)(self.handle, bytes.as_ptr(), bytes.len(), width, height, width as usize * 4, timestamp) })?;+ self.frames += 1;+ Ok(())+ }+ pub fn set_audio(&mut self, samples: &[f32], channels: i32, sample_rate: i32) -> Result<(), String> {+ self.api.error(unsafe { (self.audio)(self.handle, samples.as_ptr(), samples.len(), channels, sample_rate, 0.0) })?;+ self.has_audio = !samples.is_empty();+ Ok(())+ }+ pub fn has_audio(&self) -> bool { self.has_audio }+ pub fn attach(&self, request: *mut Request, dll_path: &Path) -> Result<(), String> {+ if dll_path != self.dll_path { return Err("Reference video and generation must use the same slopfab runtime.".into()); }+ self.api.error(unsafe { (self.attach_video)(request, self.handle) })+ }+}+impl Drop for ReferenceVideoHandle {+ fn drop(&mut self) { unsafe { (self.destroy)(self.handle) }; }+}++pub fn has_cuda_device() -> bool {+ // CUDA's driver API uses the system calling convention on Windows.+ // Keep the library alive until both calls have returned.+ unsafe {+ #[cfg(windows)]+ let driver = libloading::os::windows::Library::load_with_flags(+ "nvcuda.dll", 0x00000800, // LOAD_LIBRARY_SEARCH_SYSTEM32+ ).map(Library::from);+ #[cfg(not(windows))]+ let driver = Library::new("libcuda.so.1");+ let Ok(library) = driver else { return false };+ let Ok(init) = library.get::<unsafe extern "system" fn(u32) -> i32>(b"cuInit\0") else { return false };+ let Ok(count) = library.get::<unsafe extern "system" fn(*mut i32) -> i32>(b"cuDeviceGetCount\0") else { return false };+ let mut devices = 0;+ init(0) == 0 && count(&mut devices) == 0 && devices > 0+ }+}+pub fn cuda_device_names() -> Vec<String> {+ gpu_devices().into_iter().map(|device| device.name).collect()+}++pub fn gpu_devices() -> Vec<super::super::GpuDevice> {+ // The display driver's API is available without the CUDA toolkit.+ // Keep the library and fixed-size name buffer alive for every call.+ unsafe {+ #[cfg(windows)]+ let driver = libloading::os::windows::Library::load_with_flags(+ "nvcuda.dll", 0x00000800, // LOAD_LIBRARY_SEARCH_SYSTEM32+ ).map(Library::from);+ #[cfg(not(windows))]+ let driver = Library::new("libcuda.so.1");+ let Ok(library) = driver else { return Vec::new() };+ let Ok(init) = library.get::<unsafe extern "system" fn(u32) -> i32>(b"cuInit\0") else { return Vec::new() };+ let Ok(count) = library.get::<unsafe extern "system" fn(*mut i32) -> i32>(b"cuDeviceGetCount\0") else { return Vec::new() };+ let Ok(get) = library.get::<unsafe extern "system" fn(*mut i32, i32) -> i32>(b"cuDeviceGet\0") else { return Vec::new() };+ let Ok(name) = library.get::<unsafe extern "system" fn(*mut c_char, i32, i32) -> i32>(b"cuDeviceGetName\0") else { return Vec::new() };+ let memory = library.get::<unsafe extern "system" fn(*mut usize, i32) -> i32>(b"cuDeviceTotalMem_v2\0").ok();+ let mut count_value = 0;+ if init(0) != 0 || count(&mut count_value) != 0 { return Vec::new() }+ (0..count_value).filter_map(|ordinal| {+ let mut device = 0;+ let mut buffer = [0u8; 256];+ if get(&mut device, ordinal) != 0 || name(buffer.as_mut_ptr().cast(), buffer.len() as i32, device) != 0 {+ return None;+ }+ let end = buffer.iter().position(|byte| *byte == 0).unwrap_or(buffer.len());+ let mut memory_bytes = 0usize;+ if let Some(memory) = &memory { if memory(&mut memory_bytes, device) != 0 { memory_bytes = 0; } }+ Some(super::super::GpuDevice { name: String::from_utf8_lossy(&buffer[..end]).into_owned(), memory_bytes: memory_bytes as u64 })+ }).collect()+ }+}++pub enum Generation {}+#[repr(C)]+#[derive(Clone, Copy)]+pub struct Plan {+ pub canvas_width: i32,+ pub canvas_height: i32,+ pub aligned_frames: i32,+ pub duration_seconds: f64,+ pub num_model_evaluations: i32,+ pub sequence_rows_without_text: i32,+ pub latent_frames: i32,+ pub latent_height: i32,+ pub latent_width: i32,+ pub num_video_rows: i32,+ pub num_audio_rows: i32,+ pub num_audio_latents: i32,+}+#[repr(C)]+pub struct Progress {+ pub stage: i32,+ pub step: i32,+ pub total_steps: i32,+ pub elapsed_seconds: f64,+}+#[repr(C)]+#[derive(Clone, Copy)]+pub struct Output {+ pub video: *const f32,+ pub video_float_count: usize,+ pub channels: i32,+ pub frames: i32,+ pub width: i32,+ pub height: i32,+ pub audio: *const f32,+ pub audio_float_count: usize,+ pub audio_channels: i32,+ pub audio_sample_rate: i32,+ pub audio_frames: i64,+ pub fps: f64,+ pub seconds_conditioning: f64,+ pub seconds_denoise: f64,+ pub seconds_video_decode: f64,+ pub seconds_audio_decode: f64,+ pub seconds_total: f64,+ pub steps_computed: i32,+ pub steps_skipped: i32,+}+pub type ProgressFn = Option<unsafe extern "C" fn(*const Progress, *mut c_void)>;++pub struct Api {+ _library: Library,+ capi_version: unsafe extern "C" fn() -> u32,+ version_string: unsafe extern "C" fn() -> *const c_char,+ last_error: unsafe extern "C" fn() -> *const c_char,+ free_string: unsafe extern "C" fn(*mut c_char),+ cuda_set_version: unsafe extern "C" fn(*const c_char) -> i32,+ cuda_loaded_major: unsafe extern "C" fn(*mut i32) -> i32,+ request_create: unsafe extern "C" fn() -> *mut Request,+ request_destroy: unsafe extern "C" fn(*mut Request),+ set_prompt: unsafe extern "C" fn(*mut Request, *const c_char) -> i32,+ set_animate: Option<unsafe extern "C" fn(*mut Request, i32, i32) -> i32>,+ set_prompt_embedding: Option<unsafe extern "C" fn(*mut Request, *const c_char) -> i32>,+ set_resolution: unsafe extern "C" fn(*mut Request, i32, i32) -> i32,+ set_frames: unsafe extern "C" fn(*mut Request, i32) -> i32,+ set_still_image: Option<unsafe extern "C" fn(*mut Request, i32) -> i32>,+ set_save_latents: Option<unsafe extern "C" fn(*mut Request, *const c_char) -> i32>,+ set_continuation_file: Option<unsafe extern "C" fn(*mut Request, *const c_char, i32) -> i32>,+ set_steps: unsafe extern "C" fn(*mut Request, i32) -> i32,+ set_seed: unsafe extern "C" fn(*mut Request, u64) -> i32,+ set_model: unsafe extern "C" fn(*mut Request, i32, *const c_char) -> i32,+ add_lora: Option<unsafe extern "C" fn(*mut Request, *const c_char, f32) -> i32>,+ add_refmod: Option<unsafe extern "C" fn(*mut Request, *const c_char, f32, i32) -> i32>,+ add_reference: unsafe extern "C" fn(*mut Request, *const c_char) -> i32,+ set_attention: unsafe extern "C" fn(*mut Request, *const c_char) -> i32,+ set_inference_backend: unsafe extern "C" fn(*mut Request, i32) -> i32,+ set_verbose: unsafe extern "C" fn(*mut Request, i32) -> i32,+ set_reuse_models: unsafe extern "C" fn(*mut Request, i32) -> i32,+ reused_models_clear: unsafe extern "C" fn() -> i32,+ resolve_plan: unsafe extern "C" fn(*const Request, *mut Plan) -> i32,+ describe_plan: unsafe extern "C" fn(*const Request, *mut *mut c_char) -> i32,+ generation_start: unsafe extern "C" fn(+ *const Request,+ ProgressFn,+ *mut c_void,+ *mut *mut Generation,+ ) -> i32,+ generation_cancel: unsafe extern "C" fn(*mut Generation),+ generation_wait: unsafe extern "C" fn(*mut Generation, i32) -> i32,+ generation_error: unsafe extern "C" fn(*const Generation) -> *const c_char,+ generation_output: unsafe extern "C" fn(*const Generation, *mut Output) -> i32,+ generation_frame_rgba8: unsafe extern "C" fn(*const Generation, i32, *mut u8, usize) -> i32,+ generation_destroy: unsafe extern "C" fn(*mut Generation),+}++impl Api {+ pub fn load(path: &Path) -> Result<Self, String> {+ // SAFETY: symbols are copied function pointers with signatures from capi.h.+ // The Library is retained in Api, so no pointer outlives the loaded module.+ unsafe {+ let library = load_library(path)+ .map_err(|error| format!("could not load {}: {error}", path.display()))?;+ macro_rules! symbol {+ ($name:literal, $ty:ty) => {+ *library+ .get::<$ty>(concat!($name, "\0").as_bytes())+ .map_err(|error| format!("missing {}: {error}", $name))?+ };+ }+ Ok(Self {+ capi_version: symbol!("slopfab_capi_version", unsafe extern "C" fn() -> u32),+ version_string: symbol!(+ "slopfab_capi_version_string",+ unsafe extern "C" fn() -> *const c_char+ ),+ last_error: symbol!(+ "slopfab_last_error",+ unsafe extern "C" fn() -> *const c_char+ ),+ free_string: symbol!("slopfab_free_string", unsafe extern "C" fn(*mut c_char)),+ cuda_set_version: symbol!(+ "slopfab_cuda_set_version",+ unsafe extern "C" fn(*const c_char) -> i32+ ),+ cuda_loaded_major: symbol!(+ "slopfab_cuda_loaded_major",+ unsafe extern "C" fn(*mut i32) -> i32+ ),+ request_create: symbol!(+ "slopfab_request_create",+ unsafe extern "C" fn() -> *mut Request+ ),+ request_destroy: symbol!(+ "slopfab_request_destroy",+ unsafe extern "C" fn(*mut Request)+ ),+ set_prompt: symbol!(+ "slopfab_request_set_prompt",+ unsafe extern "C" fn(*mut Request, *const c_char) -> i32+ ),+ set_animate: library+ .get::<unsafe extern "C" fn(*mut Request, i32, i32) -> i32>(b"slopfab_request_set_animate\0")+ .ok().map(|symbol| *symbol),+ set_prompt_embedding: library+ .get::<unsafe extern "C" fn(*mut Request, *const c_char) -> i32>(b"slopfab_request_set_prompt_embedding_path\0")+ .ok().map(|symbol| *symbol),+ set_resolution: symbol!(+ "slopfab_request_set_resolution",+ unsafe extern "C" fn(*mut Request, i32, i32) -> i32+ ),+ set_frames: symbol!(+ "slopfab_request_set_frames",+ unsafe extern "C" fn(*mut Request, i32) -> i32+ ),+ set_still_image: library+ .get::<unsafe extern "C" fn(*mut Request, i32) -> i32>(b"slopfab_request_set_still_image\0")+ .ok().map(|symbol| *symbol),+ set_steps: symbol!(+ "slopfab_request_set_steps",+ unsafe extern "C" fn(*mut Request, i32) -> i32+ ),+ set_seed: symbol!(+ "slopfab_request_set_seed",+ unsafe extern "C" fn(*mut Request, u64) -> i32+ ),+ set_model: symbol!(+ "slopfab_request_set_model_path",+ unsafe extern "C" fn(*mut Request, i32, *const c_char) -> i32+ ),+ add_reference: symbol!(+ "slopfab_request_add_reference_image",+ unsafe extern "C" fn(*mut Request, *const c_char) -> i32+ ),+ add_lora: library+ .get::<unsafe extern "C" fn(*mut Request, *const c_char, f32) -> i32>(b"slopfab_request_add_lora\0")+ .ok().map(|symbol| *symbol),+ set_save_latents: library+ .get::<unsafe extern "C" fn(*mut Request, *const c_char) -> i32>(b"slopfab_request_set_save_latents\0")+ .ok().map(|symbol| *symbol),+ set_continuation_file: library+ .get::<unsafe extern "C" fn(*mut Request, *const c_char, i32) -> i32>(b"slopfab_request_set_continuation_file\0")+ .ok().map(|symbol| *symbol),+ add_refmod: library+ .get::<unsafe extern "C" fn(*mut Request, *const c_char, f32, i32) -> i32>(b"slopfab_request_add_refmod\0")+ .ok().map(|symbol| *symbol),+ set_attention: symbol!(+ "slopfab_request_set_attention",+ unsafe extern "C" fn(*mut Request, *const c_char) -> i32+ ),+ set_inference_backend: symbol!(+ "slopfab_request_set_inference_backend",+ unsafe extern "C" fn(*mut Request, i32) -> i32+ ),+ set_verbose: symbol!(+ "slopfab_request_set_verbose",+ unsafe extern "C" fn(*mut Request, i32) -> i32+ ),+ set_reuse_models: symbol!(+ "slopfab_request_set_reuse_models",+ unsafe extern "C" fn(*mut Request, i32) -> i32+ ),+ reused_models_clear: symbol!(+ "slopfab_reused_models_clear",+ unsafe extern "C" fn() -> i32+ ),+ resolve_plan: symbol!(+ "slopfab_resolve_plan",+ unsafe extern "C" fn(*const Request, *mut Plan) -> i32+ ),+ describe_plan: symbol!(+ "slopfab_describe_plan",+ unsafe extern "C" fn(*const Request, *mut *mut c_char) -> i32+ ),+ generation_start: symbol!(+ "slopfab_generation_start",+ unsafe extern "C" fn(+ *const Request,+ ProgressFn,+ *mut c_void,+ *mut *mut Generation,+ ) -> i32+ ),+ generation_cancel: symbol!(+ "slopfab_generation_cancel",+ unsafe extern "C" fn(*mut Generation)+ ),+ generation_wait: symbol!(+ "slopfab_generation_wait",+ unsafe extern "C" fn(*mut Generation, i32) -> i32+ ),+ generation_error: symbol!(+ "slopfab_generation_error",+ unsafe extern "C" fn(*const Generation) -> *const c_char+ ),+ generation_output: symbol!(+ "slopfab_generation_output",+ unsafe extern "C" fn(*const Generation, *mut Output) -> i32+ ),+ generation_frame_rgba8: symbol!(+ "slopfab_generation_frame_rgba8",+ unsafe extern "C" fn(*const Generation, i32, *mut u8, usize) -> i32+ ),+ generation_destroy: symbol!(+ "slopfab_generation_destroy",+ unsafe extern "C" fn(*mut Generation)+ ),+ _library: library,+ })+ }+ }+ pub fn version(&self) -> Result<String, String> {+ unsafe {+ let packed = (self.capi_version)();+ let major = packed >> 24;+ if major != super::super::EXPECTED_CAPI_MAJOR {+ return Err(format!(+ "Incompatible slopfab C API major {major}; expected {}.",+ super::super::EXPECTED_CAPI_MAJOR+ ));+ }+ Ok(c_string((self.version_string)()))+ }+ }+ fn error(&self, code: i32) -> Result<(), String> {+ if code == 0 {+ Ok(())+ } else {+ let message = unsafe { c_string((self.last_error)()) };+ Err(if message.is_empty() {+ format!("slopfab returned status {code}.")+ } else {+ message+ })+ }+ }+ pub fn create_request(&self) -> Result<*mut Request, String> {+ let value = unsafe { (self.request_create)() };+ if value.is_null() {+ Err(unsafe { c_string((self.last_error)()) })+ } else {+ Ok(value)+ }+ }+ pub fn destroy_request(&self, request: *mut Request) {+ unsafe { (self.request_destroy)(request) }+ }+ pub fn set_cuda_version(&self, value: &str) -> Result<(), String> {+ let value = CString::new(value)+ .map_err(|_| "CUDA version contains a null byte.".to_string())?;+ self.error(unsafe { (self.cuda_set_version)(value.as_ptr()) })+ }+ pub fn cuda_loaded_major(&self) -> Result<i32, String> {+ let mut major = 0;+ self.error(unsafe { (self.cuda_loaded_major)(&mut major) })?;+ Ok(major)+ }+ pub fn set_prompt(&self, r: *mut Request, v: &str) -> Result<(), String> {+ let v = CString::new(v).map_err(|_| "Prompt contains a null byte.".to_string())?;+ self.error(unsafe { (self.set_prompt)(r, v.as_ptr()) })+ }+ #[cfg(test)]+ pub fn disable_animate_for_test(&mut self) { self.set_animate = None; }+ pub fn set_animate(&self, r: *mut Request, preserve_audio: bool) -> Result<(), String> {+ let set = self.set_animate.ok_or("Animate requires slopfab.dll API 1.10 or later. Update the runtime.")?;+ self.error(unsafe { set(r, 1, preserve_audio as i32) })+ }+ pub fn set_prompt_embedding(&self, r: *mut Request, path: &Path) -> Result<(), String> {+ let set = self.set_prompt_embedding.ok_or("This slopfab.dll does not support frozen conditioning. Update the runtime.")?;+ let path = path_cstring(path)?;+ self.error(unsafe { set(r, path.as_ptr()) })+ }+ pub fn set_resolution(&self, r: *mut Request, w: i32, h: i32) -> Result<(), String> {+ self.error(unsafe { (self.set_resolution)(r, w, h) })+ }+ pub fn set_frames(&self, r: *mut Request, v: i32) -> Result<(), String> {+ self.error(unsafe { (self.set_frames)(r, v) })+ }+ pub fn set_still_image(&self, r: *mut Request) -> Result<(), String> {+ let set = self.set_still_image.ok_or("This slopfab.dll does not support still-image generation. Update the runtime to generate reference icons.")?;+ self.error(unsafe { set(r, 1) })+ }+ pub fn set_save_latents(&self, r: *mut Request, path: &Path) -> Result<(), String> {+ let set = self.set_save_latents.ok_or("Saving generation latents requires slopfab.dll API 1.9 or later. Update the runtime.")?;+ let path = path_cstring(path)?;+ self.error(unsafe { set(r, path.as_ptr()) })+ }+ pub fn set_continuation_file(&self, r: *mut Request, path: &Path, overlap: i32) -> Result<(), String> {+ let set = self.set_continuation_file.ok_or("Scene continuation requires slopfab.dll API 1.9 or later. Update the runtime.")?;+ let path = path_cstring(path)?;+ self.error(unsafe { set(r, path.as_ptr(), overlap) })+ }+ pub fn set_steps(&self, r: *mut Request, v: i32) -> Result<(), String> {+ self.error(unsafe { (self.set_steps)(r, v) })+ }+ pub fn set_seed(&self, r: *mut Request, v: u64) -> Result<(), String> {+ self.error(unsafe { (self.set_seed)(r, v) })+ }+ pub fn set_verbose(&self, r: *mut Request, v: bool) -> Result<(), String> {+ self.error(unsafe { (self.set_verbose)(r, v as i32) })+ }+ pub fn set_reuse_models(&self, r: *mut Request, v: bool) -> Result<(), String> {+ self.error(unsafe { (self.set_reuse_models)(r, v as i32) })+ }+ pub fn clear_reused_models(&self) -> Result<(), String> {+ self.error(unsafe { (self.reused_models_clear)() })+ }+ pub fn set_attention(&self, r: *mut Request, v: &str) -> Result<(), String> {+ let v =+ CString::new(v).map_err(|_| "Attention mode contains a null byte.".to_string())?;+ self.error(unsafe { (self.set_attention)(r, v.as_ptr()) })+ }+ pub fn set_inference_backend(&self, r: *mut Request, backend: i32) -> Result<(), String> {+ self.error(unsafe { (self.set_inference_backend)(r, backend) })+ }+ pub fn set_model(&self, r: *mut Request, id: i32, path: &Path) -> Result<(), String> {+ let v = path_cstring(path)?;+ self.error(unsafe { (self.set_model)(r, id, v.as_ptr()) })+ }+ pub fn add_reference(&self, r: *mut Request, path: &Path) -> Result<(), String> {+ let v = path_cstring(path)?;+ self.error(unsafe { (self.add_reference)(r, v.as_ptr()) })+ }+ pub fn add_lora(&self, r: *mut Request, path: &Path, strength: f32) -> Result<(), String> {+ let add = self.add_lora.ok_or("This slopfab.dll does not support LoRAs. Update the DLL or disable the selected LoRAs.")?;+ let path = path_cstring(path)?;+ self.error(unsafe { add(r, path.as_ptr(), strength) })+ }+ pub fn add_refmod(&self, r: *mut Request, path: &Path, strength: f32, copies: i32) -> Result<(), String> {+ let add = self.add_refmod.ok_or("This slopfab.dll does not support refmods. Install a build with the refmod API (1.8 or later).")?;+ let path = path_cstring(path)?;+ self.error(unsafe { add(r, path.as_ptr(), strength, copies) })+ }+ pub fn resolve(&self, request: *mut Request) -> Result<Plan, String> {+ let mut value = Plan {+ canvas_width: 0,+ canvas_height: 0,+ aligned_frames: 0,+ duration_seconds: 0.0,+ num_model_evaluations: 0,+ sequence_rows_without_text: 0,+ latent_frames: 0,+ latent_height: 0,+ latent_width: 0,+ num_video_rows: 0,+ num_audio_rows: 0,+ num_audio_latents: 0,+ };+ self.error(unsafe { (self.resolve_plan)(request, &mut value) })?;+ Ok(value)+ }+ pub fn describe(&self, request: *mut Request) -> Result<String, String> {+ let mut value = ptr::null_mut();+ self.error(unsafe { (self.describe_plan)(request, &mut value) })?;+ let text = unsafe { c_string(value) };+ unsafe { (self.free_string)(value) };+ Ok(text)+ }+ pub fn start(+ &self,+ request: *mut Request,+ callback: ProgressFn,+ userdata: *mut c_void,+ ) -> Result<*mut Generation, String> {+ let mut value = ptr::null_mut();+ self.error(unsafe {+ (self.generation_start)(request, callback, userdata, &mut value)+ })?;+ Ok(value)+ }+ pub fn cancel(&self, generation: *mut Generation) {+ unsafe { (self.generation_cancel)(generation) }+ }+ pub fn wait(&self, generation: *mut Generation, timeout: i32) -> i32 {+ unsafe { (self.generation_wait)(generation, timeout) }+ }+ pub fn generation_error(&self, generation: *mut Generation) -> String {+ unsafe { c_string((self.generation_error)(generation)) }+ }+ pub fn output(&self, generation: *mut Generation) -> Result<Output, String> {+ let mut value = Output {+ video: ptr::null(),+ video_float_count: 0,+ channels: 0,+ frames: 0,+ width: 0,+ height: 0,+ audio: ptr::null(),+ audio_float_count: 0,+ audio_channels: 0,+ audio_sample_rate: 0,+ audio_frames: 0,+ fps: 0.0,+ seconds_conditioning: 0.0,+ seconds_denoise: 0.0,+ seconds_video_decode: 0.0,+ seconds_audio_decode: 0.0,+ seconds_total: 0.0,+ steps_computed: 0,+ steps_skipped: 0,+ };+ self.error(unsafe { (self.generation_output)(generation, &mut value) })?;+ Ok(value)+ }+ pub fn frame_rgba8(+ &self,+ generation: *mut Generation,+ index: u32,+ width: u32,+ height: u32,+ ) -> Result<Vec<u8>, String> {+ let index = i32::try_from(index)+ .map_err(|_| "Frame index does not fit the slopfab API.".to_string())?;+ let bytes = (width as usize)+ .checked_mul(height as usize)+ .and_then(|value| value.checked_mul(4))+ .ok_or_else(|| "Frame dimensions do not fit in memory.".to_string())?;+ let mut rgba = vec![0_u8; bytes];+ self.error(unsafe {+ (self.generation_frame_rgba8)(generation, index, rgba.as_mut_ptr(), rgba.len())+ })?;+ Ok(rgba)+ }+ pub fn destroy_generation(&self, generation: *mut Generation) {+ unsafe { (self.generation_destroy)(generation) }+ }+}+fn path_cstring(path: &Path) -> Result<CString, String> {+ CString::new(path.to_string_lossy().as_bytes())+ .map_err(|_| "A slopfab path contains a null byte.".into())+}++#[cfg(windows)]+unsafe fn load_library(path: &Path) -> Result<Library, libloading::Error> {+ // LOAD_WITH_ALTERED_SEARCH_PATH makes Windows resolve the runtime's+ // dynamic dependencies relative to this absolute path instead of the host exe.+ const LOAD_WITH_ALTERED_SEARCH_PATH: u32 = 0x0000_0008;+ unsafe {+ libloading::os::windows::Library::load_with_flags(path, LOAD_WITH_ALTERED_SEARCH_PATH)+ .map(Into::into)+ }+}++#[cfg(not(windows))]+unsafe fn load_library(path: &Path) -> Result<Library, libloading::Error> {+ unsafe { Library::new(path) }+}+unsafe fn c_string(value: *const c_char) -> String {+ if value.is_null() {+ String::new()+ } else {+ unsafe { CStr::from_ptr(value) }+ .to_string_lossy()+ .into_owned()+ }+}+diff --git a/src-tauri/src/slopfab/generation.rs b/src-tauri/src/slopfab/generation.rsnew file mode 100644index 0000000..00ae4c0--- /dev/null+++ b/src-tauri/src/slopfab/generation.rs@@ -0,0 +1,107 @@+use super::*;++struct FrameSource { generation: ffi::FinishedGeneration, frame_offset: u32 }+impl rendered::FrameSource for FrameSource {+ fn frame_rgba(&self,index:u32,width:u32,height:u32)->Result<Vec<u8>,String>{+ let index=index.checked_add(self.frame_offset).ok_or("Continuation frame index overflow.")?;+ self.generation.frame_rgba8(index,width,height)+ }+}++pub(super) fn run_generation(item: &QueueItem) -> Result<(OutputMetadata, rendered::RenderedVideo), String> {+ if item.cancel.load(Ordering::Acquire) {+ return Err("Generation was cancelled before it started.".into());+ }+ let api = ffi::Api::load(&item.configuration.dll_path)?;+ let version = api.version()?;+ let platform = item.configuration.platform(detect_platform(&api));+ let timing_profile = item.configuration.timing_profile(&version, platform);+ diagnostics::debug(+ "slopfab",+ "generation.runtime_ready",+ "slopfab runtime loaded.",+ serde_json::json!({+ "jobId": item.request.job_id, "version": version, "platform": platform.label(),+ }),+ );+ let request = RequestHandle::new(&api)?;+ configure_request(+ &api,+ &request,+ &item.request,+ &item.configuration,+ platform,+ RequestPurpose::Generate,+ &item.references,+ )?;+ api.resolve(&request)?;+ diagnostics::debug(+ "slopfab",+ "generation.request_resolved",+ "slopfab resolved the generation request.",+ serde_json::json!({ "jobId": item.request.job_id }),+ );+ let context = CallbackContext {+ events: item.events.clone(),+ job_id: item.request.job_id.clone(),+ frames: item.request.frames,+ canvas_width: item.request.canvas_width,+ canvas_height: item.request.canvas_height,+ reference_count: item.request.reference_count(),+ timing_profile: timing_profile.clone(),+ planned_steps: (item.request.steps - 1).max(0),+ };+ let mut generation = request.start(Some(progress_sink(context)))?;+ diagnostics::debug(+ "slopfab",+ "generation.api_started",+ "slopfab accepted the generation request.",+ serde_json::json!({ "jobId": item.request.job_id }),+ );+ loop {+ if item.cancel.load(Ordering::Acquire) { generation.cancel(); }+ if generation.wait(100)? { break; }+ }+ let generation = generation.finish()?;+ let output = generation.output()?;+ drop(request);+ let (frame_offset, frames) = scene_frame_window(&item.request, output.frames)?;+ let source = Box::new(FrameSource { generation, frame_offset: frame_offset as u32 });+ let audio_offset = scene_audio_offset(frame_offset, output.fps, output.audio_channels, output.audio_sample_rate, output.audio.len())?;+ let mut audio = output.audio;+ audio.drain(..audio_offset);+ let metadata = OutputMetadata {+ frames, width: output.width, height: output.height, fps: output.fps,+ audio_channels: output.audio_channels, audio_sample_rate: output.audio_sample_rate,+ audio_samples: audio.len(),+ reference_count: item.request.reference_count(),+ timing_profile,+ seconds_conditioning: output.seconds_conditioning,+ seconds_denoise: output.seconds_denoise,+ seconds_video_decode: output.seconds_video_decode,+ seconds_audio_decode: output.seconds_audio_decode,+ seconds_total: output.seconds_total,+ steps_computed: output.steps_computed,+ steps_skipped: output.steps_skipped,+ duration_seconds: if output.fps > 0.0 {+ frames as f64 / output.fps+ } else {+ 0.0+ },+ boundary: "Latents are saved in the project. Decoded buffers remain owned by slopfab; the webview encodes the new scene's frames on demand.",+ };+ let pictures = rendered::from_source(+ &item.request.job_id,+ source,+ audio,+ frames as u32,+ output.width.max(0) as u32,+ output.height.max(0) as u32,+ output.channels.max(0) as u32,+ output.video_float_count,+ output.fps,+ output.audio_channels.max(0) as u32,+ output.audio_sample_rate.max(0) as u32,+ )?;+ Ok((metadata, pictures))+}diff --git a/src-tauri/src/slopfab/h3.rs b/src-tauri/src/slopfab/h3.rsnew file mode 100644index 0000000..6fe85fd--- /dev/null+++ b/src-tauri/src/slopfab/h3.rs@@ -0,0 +1,90 @@+//! H3 and Animate request recipes for the SlopFab backend.+use super::*;+pub(super) fn configure_request(+ api: &ffi::Api,+ handle: &RequestHandle,+ request: &GenerationRequest,+ configuration: &Configuration,+ platform: ComputePlatform,+ purpose: RequestPurpose,+ references: &ReferenceVideos,+) -> Result<(), String> {+ configuration.validate_inputs(request)?;+ if configuration.animate {+ let videos = references.videos.lock().map_err(|_| "Reference video lock failed.")?;+ let video = videos.get(&request.reference_video_ids[0])+ .ok_or("The prepared reference video no longer exists. Please retry generation.")?;+ // The setter selects Euler and four boundaries; explicit step settings follow.+ api.set_animate(handle, video.has_audio())?;+ let source = configuration.prompt_embedding.as_deref()+ .ok_or("Animate conditioning is missing. Download Animate in Settings and select its conditioning additional safetensor.")?;+ let embedding = crate::conditioning::prepare(source)?;+ api.set_prompt_embedding(handle, &embedding)?;+ }+ api.set_prompt(handle, if configuration.animate { ANIMATE_PROMPT } else { &request.prompt })?;+ // Keep lengths within the advertised 15-second range from rounding past+ // Animate's limit. Leave out-of-range requests to the DLL's validation.+ let frames = if configuration.animate && request.frames <= 360 {+ request.frames.min(ANIMATE_MAX_ALIGNED_FRAMES)+ } else { request.frames };+ api.set_frames(handle, frames)?;+ if request.still_image {+ api.set_still_image(handle)?;+ }+ api.set_steps(handle, configuration.generation_steps(request.steps)?)?;+ api.set_seed(handle, generation_seed(request.seed)?)?;+ api.set_resolution(handle, request.canvas_width, request.canvas_height)?;+ api.set_inference_backend(handle, platform.backend())?;+ api.set_attention(handle, configuration.attention)?;+ api.set_verbose(handle, false)?;+ if let Some(path) = &request.continuation_path {+ api.set_continuation_file(handle, path, crate::generation::models::h3::MODEL.capabilities.continuation_overlap)?;+ }+ if purpose == RequestPurpose::Generate {+ if let Some(path) = &request.save_latents_path {+ api.set_save_latents(handle, path)?;+ }+ }+ for lora in configuration.loras.as_ref().map_err(Clone::clone)? {+ if lora.path.trim().is_empty() || !lora.strength.is_finite() {+ return Err("LoRA paths must be nonempty and strengths finite.".into());+ }+ if lora.strength == 0.0 { continue; }+ if purpose == RequestPurpose::Generate && !Path::new(&lora.path).is_file() {+ return Err(format!("LoRA file is missing: {}", lora.path));+ }+ api.add_lora(handle, Path::new(&lora.path), lora.strength)?;+ }+ if purpose == RequestPurpose::Generate {+ api.set_reuse_models(handle, true)?;+ for (id, _, path) in &configuration.models {+ if configuration.animate && (*id == 1 || *id == 2) { continue; }+ if request.still_image && *id == 4 {+ continue;+ }+ if let Some(path) = path {+ api.set_model(handle, *id, path)?;+ }+ }+ }+ for path in &request.reference_paths {+ api.add_reference(handle, Path::new(path))?;+ }+ if !request.reference_video_ids.is_empty() {+ let videos = references.videos.lock().map_err(|_| "Reference video lock failed.")?;+ for id in &request.reference_video_ids {+ videos.get(id).ok_or("The prepared reference video no longer exists. Please retry generation.")?+ .attach(handle, &configuration.dll_path)?;+ }+ }+ for refmod in &request.refmods {+ if refmod.path.trim().is_empty() || !refmod.strength.is_finite() || !(0.0..=1.0).contains(&refmod.strength)+ || !(1..=10).contains(&refmod.copies) {+ return Err("Refmods need a path, strength 0 to 1 and copies 1 to 10.".into());+ }+ if refmod.strength == 0.0 { continue; }+ api.add_refmod(handle, Path::new(&refmod.path), refmod.strength, refmod.copies as i32)?;+ }+ Ok(())+}+diff --git a/src-tauri/src/slopfab/icons.rs b/src-tauri/src/slopfab/icons.rsnew file mode 100644index 0000000..809fb44--- /dev/null+++ b/src-tauri/src/slopfab/icons.rs@@ -0,0 +1,121 @@+use super::*;++#[derive(Debug, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct ReferenceIconSpec {+ pub id: String,+ pub prompt: String,+ pub seed: u64,+ pub destination: PathBuf,+}++#[derive(Debug)]+pub struct ReferenceIconBatchConfig {+ pub dll_path: PathBuf,+ pub transformer: PathBuf,+ pub text_encoder: PathBuf,+ pub video_vae: PathBuf,+ pub backend: String,+}++pub(super) fn write_reference_icon(+ api: &ffi::Api,+ configuration: &Configuration,+ platform: ComputePlatform,+ spec: &ReferenceIconSpec,+) -> Result<(), String> {+ let request = GenerationRequest {+ job_id: spec.id.clone(),+ prompt: spec.prompt.clone(),+ frames: 1,+ still_image: true,+ steps: crate::reference_icons::STEPS,+ seed: i64::try_from(spec.seed)+ .map_err(|_| format!("Icon '{}' seed is too large.", spec.id))?,+ canvas_width: crate::reference_icons::RENDER_SIZE as i32,+ canvas_height: crate::reference_icons::RENDER_SIZE as i32,+ reference_paths: Vec::new(),+ refmods: Vec::new(),+ reference_video_ids: Vec::new(),+ ..Default::default()+ };+ let handle = RequestHandle::new(api)?;+ configure_request(api, &handle, &request, configuration, platform, RequestPurpose::Generate, &ReferenceVideos::default())?;+ api.resolve(&handle)?;+ let mut generation = handle.start(None)?;+ while !generation.wait(-1).map_err(|error| format!("Icon '{}': {error}", spec.id))? {}+ let generation = generation.finish()?;+ let output = generation.output()?;+ let render_size = crate::reference_icons::RENDER_SIZE;+ if output.width != render_size as i32 || output.height != render_size as i32 || output.frames != 1 {+ return Err(format!(+ "Icon '{}' returned {}x{} with {} frames.",+ spec.id, output.width, output.height, output.frames+ ));+ }+ let rgba = generation.frame_rgba8(0, render_size, render_size)?;+ let jpeg = crate::reference_icons::encode_jpeg(&rgba)?;+ if let Some(parent) = spec.destination.parent() {+ std::fs::create_dir_all(parent).map_err(|error| {+ format!(+ "Could not create icon folder '{}': {error}",+ parent.display()+ )+ })?;+ }+ std::fs::write(&spec.destination, jpeg).map_err(|error| {+ format!(+ "Could not create icon '{}': {error}",+ spec.destination.display()+ )+ })+}++pub fn generate_reference_icon_batch(+ specs: &[ReferenceIconSpec],+ batch: &ReferenceIconBatchConfig,+) -> Result<(), String> {+ let backend = match batch.backend.as_str() {+ "cuda" => ComputePlatform::Cuda13,+ "vulkan" => ComputePlatform::Vulkan,+ value => return Err(format!("Unsupported icon generation backend '{value}'.")),+ };+ let configuration = Configuration {+ dll_path: batch.dll_path.clone(),+ loras: Ok(Vec::new()),+ step_override: Ok(None),+ vulkan: backend == ComputePlatform::Vulkan,+ attention: "sage2",+ animate: false,+ prompt_embedding: None,+ models: [+ (0, "transformer", Some(batch.transformer.clone())),+ (1, "textEncoder", Some(batch.text_encoder.clone())),+ (2, "tokenizer", None),+ (3, "videoVae", Some(batch.video_vae.clone())),+ // Icons keep one picture and need no decoded soundtrack.+ (4, "audioVae", None),+ ],+ };+ let api = ffi::Api::load(&configuration.dll_path)?;+ api.version()?;+ let platform = if backend == ComputePlatform::Cuda13 {+ detect_platform(&api)+ } else {+ backend+ };+ let result = specs.iter().enumerate().try_for_each(|(index, spec)| {+ let started = std::time::Instant::now();+ write_reference_icon(&api, &configuration, platform, spec)?;+ println!(+ "[{}/{}] {}: generated in {:.1}s",+ index + 1,+ specs.len(),+ spec.id,+ started.elapsed().as_secs_f64()+ );+ Ok(())+ });+ let cleared = api.clear_reused_models();+ result.and(cleared)+}diff --git a/src-tauri/src/slopfab/mod.rs b/src-tauri/src/slopfab/mod.rsnew file mode 100644index 0000000..1594628--- /dev/null+++ b/src-tauri/src/slopfab/mod.rs@@ -0,0 +1,55 @@+//! Native runtime adapter. Raw ABI and resource ownership are confined to ffi.+use crate::{diagnostics, rendered};+use crate::project::{ProviderOption, ProviderSetting};+use getrandom::fill as fill_random;+use serde::{Deserialize, Serialize};+use std::{collections::{BTreeMap, HashMap},path::{Path, PathBuf},sync::{atomic::{AtomicBool, Ordering},mpsc,Arc,Mutex},thread};+mod types;+mod platform;+mod config;+mod status;+mod runtime;+mod references;+mod planning;+mod h3;+use h3::configure_request;+mod generation;+mod icons;+mod events;+mod ffi;+pub use types::*;+pub use runtime::SlopfabRuntime;+pub use references::*;+pub use planning::resolve_plan;+pub use status::status;+pub use icons::{generate_reference_icon_batch, ReferenceIconBatchConfig, ReferenceIconSpec};+pub use events::{EventSink, NativeEvent};+use platform::*;+use config::*;+use planning::*;+use runtime::QueueItem;+use generation::run_generation;+use events::*;+use ffi::RequestHandle;+const DLL_FILE_NAME: &str = "slopfab.dll";+// https://huggingface.co/Viggle/Viggle-Animate/raw/main/assets/fixed_prompt.txt+const ANIMATE_PROMPT: &str = include_str!("../../assets/viggle-animate-fixed-prompt.txt");+// SlopFab rounds up to 17*k + 5 frames but rejects Animate plans over 360.+const ANIMATE_MAX_ALIGNED_FRAMES: i32 = (360 - 5) / 17 * 17 + 5;+/// Tauri stages the repository's runtime resources in development and release+/// builds. Installers and portable folders use the same relative layout.+pub fn default_dll_path() -> PathBuf {+ let executable = std::env::current_exe().unwrap_or_else(|_| PathBuf::from("Slopus.exe"));+ let directory = executable.parent().unwrap_or_else(|| Path::new("."));+ // Cargo test executables live one level below the staged resources.+ #[cfg(test)]+ let directory = if directory.file_name().is_some_and(|name| name == "deps") {+ directory.parent().unwrap_or(directory)+ } else { directory };+ directory.join(DLL_FILE_NAME)+}+const EXPECTED_CAPI_MAJOR: u32 = 1;+const NOT_READY: i32 = -7;+const CANCELLED: i32 = -8;++#[cfg(test)] mod tests;diff --git a/src-tauri/src/slopfab/planning.rs b/src-tauri/src/slopfab/planning.rsnew file mode 100644index 0000000..9ac1613--- /dev/null+++ b/src-tauri/src/slopfab/planning.rs@@ -0,0 +1,88 @@+#[derive(Clone, Copy, PartialEq, Eq)]+pub(super) enum RequestPurpose { Plan, Generate }+use super::*;++pub fn resolve_plan(+ request: &GenerationRequest,+ settings: &BTreeMap<String, ProviderSetting>,+ references: &ReferenceVideos,+) -> Result<ResolvedPlan, String> {+ validate_generation_controls(request)?;+ let configuration = Configuration::from_settings(settings);+ let api = ffi::Api::load(&configuration.dll_path)?;+ api.version()?;+ let platform = configuration.platform(detect_platform(&api));+ let handle = RequestHandle::new(&api)?;+ configure_request(&api, &handle, request, &configuration, platform, RequestPurpose::Plan, references)?;+ let plan = api.resolve(&handle)?;+ let description = api.describe(&handle)?;+ let (_, frames) = scene_frame_window(request, plan.aligned_frames)?;+ Ok(ResolvedPlan {+ canvas_width: plan.canvas_width,+ canvas_height: plan.canvas_height,+ aligned_frames: frames,+ duration_seconds: plan.duration_seconds * frames as f64 / plan.aligned_frames as f64,+ model_evaluations: plan.num_model_evaluations,+ sequence_rows_without_text: plan.sequence_rows_without_text,+ latent_frames: plan.latent_frames,+ latent_width: plan.latent_width,+ latent_height: plan.latent_height,+ description,+ boundary: "Plan only. No weights were opened and no media file was created.",+ })+}++pub(super) fn validate_generation_controls(request: &GenerationRequest) -> Result<(), String> {+ let limits = crate::generation::models::h3::MODEL.capabilities;+ if request.reference_paths.len() > limits.max_image_references || request.reference_video_ids.len() > limits.max_video_references || request.reference_count() > limits.max_references {+ return Err("Use at most nine images and three videos per generation.".into());+ }+ if request.still_image && !request.reference_video_ids.is_empty() {+ return Err("Video references cannot be used for still-image generation.".into());+ }+ if request.steps < limits.min_steps {+ return Err("Generation step count must be at least 2.".into());+ }+ if request.seed < -1 {+ return Err("Generation seed must be -1 or greater.".into());+ }+ if request.canvas_width <= 0 || request.canvas_height <= 0 {+ return Err("Generation canvas dimensions must be positive.".into());+ }+ Ok(())+}++pub(super) fn generation_seed(value: i64) -> Result<u64, String> {+ if value == -1 {+ let mut bytes = [0_u8; 8];+ fill_random(&mut bytes)+ .map_err(|error| format!("Could not create a random generation seed: {error}"))?;+ Ok(u64::from_ne_bytes(bytes))+ } else {+ u64::try_from(value).map_err(|_| "Generation seed must be -1 or greater.".to_string())+ }+}++/// Continuation output is cumulative. Expose only the new scene to the editor,+/// while the DLL saves the full archive for the next continuation.+pub(super) fn scene_frame_window(request: &GenerationRequest, total: i32) -> Result<(i32, i32), String> {+ if total <= 0 { return Err("Slopfab returned no video frames.".into()); }+ if request.continuation_path.is_none() { return Ok((0, total)); }+ if request.frames <= 0 { return Err("Continuation needs a positive frame count.".into()); }+ let limits = crate::generation::models::h3::MODEL.capabilities;+ let frames = request.frames.checked_add(limits.frame_stride - 1).ok_or("Continuation frame count overflow.")? / limits.frame_stride * limits.frame_stride;+ let offset = total.checked_sub(frames).filter(|offset| *offset >= limits.continuation_overlap)+ .ok_or("Slopfab continuation output is missing its source frames.".to_string())?;+ Ok((offset, frames))+}++pub(super) fn scene_audio_offset(frame_offset: i32, fps: f64, channels: i32, sample_rate: i32, total: usize) -> Result<usize, String> {+ if total == 0 || frame_offset == 0 { return Ok(0); }+ if !fps.is_finite() || fps <= 0.0 || channels <= 0 || sample_rate <= 0 {+ return Err("Slopfab returned invalid continuation audio timing.".into());+ }+ let samples = (frame_offset as f64 * sample_rate as f64 / fps).round();+ let offset = (samples as usize).checked_mul(channels as usize)+ .filter(|offset| *offset <= total).ok_or("Slopfab continuation audio is shorter than its source scene.")?;+ Ok(offset)+}diff --git a/src-tauri/src/slopfab/platform.rs b/src-tauri/src/slopfab/platform.rsnew file mode 100644index 0000000..8bcc430--- /dev/null+++ b/src-tauri/src/slopfab/platform.rs@@ -0,0 +1,47 @@+use super::*;++#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub(super) enum ComputePlatform {+ Cuda13,+ Cuda12,+ Vulkan,+}++impl ComputePlatform {+ pub(super) fn label(self) -> &'static str {+ match self {+ Self::Cuda13 => "CUDA 13",+ Self::Cuda12 => "CUDA 12",+ Self::Vulkan => "Vulkan",+ }+ }++ pub(super) fn backend(self) -> i32 {+ match self {+ Self::Cuda13 | Self::Cuda12 => 0,+ Self::Vulkan => 1,+ }+ }+}++pub(super) fn platform_from_cuda_probe(result: Result<i32, String>) -> ComputePlatform {+ match result {+ Ok(13) => ComputePlatform::Cuda13,+ Ok(12) => ComputePlatform::Cuda12,+ _ => ComputePlatform::Vulkan,+ }+}++pub(super) fn detect_platform(api: &ffi::Api) -> ComputePlatform {+ // A toolkit can be installed on an AMD/Intel machine. Check for a usable+ // NVIDIA device before treating the cuBLAS installation as CUDA support.+ if !ffi::has_cuda_device() {+ return ComputePlatform::Vulkan;+ }+ // Explicit "auto" makes Slopus's order deterministic even if the host+ // process carries SLOPFAB_CUDA_VERSION. If this DLL instance was already+ // initialized, the setter is expected to refuse the change and the loaded+ // major below remains the authority.+ let _ = api.set_cuda_version("auto");+ platform_from_cuda_probe(api.cuda_loaded_major())+}diff --git a/src-tauri/src/slopfab/references.rs b/src-tauri/src/slopfab/references.rsnew file mode 100644index 0000000..c2a6da5--- /dev/null+++ b/src-tauri/src/slopfab/references.rs@@ -0,0 +1,44 @@+use super::*;++// A video owns its DLL and input buffers. Commands serialize mutations; the+// C API retains immutable snapshots when a plan/generation attaches a video.+#[derive(Clone, Default)]+pub struct ReferenceVideos {+ pub(super) videos: Arc<Mutex<HashMap<String, ffi::ReferenceVideoHandle>>>,+ next: Arc<std::sync::atomic::AtomicU64>,+}++impl ReferenceVideos {+pub fn create_reference_video(&self, duration: f64, settings: &BTreeMap<String, ProviderSetting>) -> Result<String, String> {+ let configuration = Configuration::from_settings(settings);+ let api = ffi::Api::load(&configuration.dll_path)?;+ api.version()?;+ let video = ffi::ReferenceVideoHandle::new(api, duration, configuration.dll_path)?;+ let mut videos = self.videos.lock().map_err(|_| "Reference video lock failed.")?;+ if videos.len() >= 3 { return Err("At most three video references can be prepared at once.".into()); }+ let id = self.next.fetch_add(1, Ordering::Relaxed).to_string();+ videos.insert(id.clone(), video);+ Ok(id)+}++pub fn append_reference_video(&self, id: &str, bytes: &[u8], width: i32, height: i32, timestamp: f64) -> Result<(), String> {+ self.videos.lock().map_err(|_| "Reference video lock failed.")?+ .get_mut(id).ok_or("The prepared reference video no longer exists.")?+ .append(bytes, width, height, timestamp)+}++pub fn set_reference_video_audio(&self, id: &str, bytes: &[u8], channels: i32, sample_rate: i32) -> Result<(), String> {+ if bytes.len() % 4 != 0 { return Err("Reference audio must contain complete float32 samples.".into()); }+ let samples: Vec<f32> = bytes.chunks_exact(4).map(|chunk| f32::from_le_bytes(chunk.try_into().unwrap())).collect();+ self.videos.lock().map_err(|_| "Reference video lock failed.")?+ .get_mut(id).ok_or("The prepared reference video no longer exists.")?+ .set_audio(&samples, channels, sample_rate)+}++pub fn release_reference_videos(&self, ids: &[String]) -> Result<(), String> {+ let mut videos = self.videos.lock().map_err(|_| "Reference video lock failed.")?;+ for id in ids { videos.remove(id); }+ Ok(())++}+}diff --git a/src-tauri/src/slopfab/runtime.rs b/src-tauri/src/slopfab/runtime.rsnew file mode 100644index 0000000..1c1397f--- /dev/null+++ b/src-tauri/src/slopfab/runtime.rs@@ -0,0 +1,209 @@+use super::*;++#[derive(Clone)]+pub struct SlopfabRuntime {+ pub(super) sender: mpsc::Sender<QueueItem>,+ pub references: ReferenceVideos,+ pub(super) cancellations: Arc<Mutex<HashMap<String, Arc<AtomicBool>>>>,+}++impl Default for SlopfabRuntime {+ fn default() -> Self {+ let (sender, receiver) = mpsc::channel::<QueueItem>();+ let cancellations = Arc::new(Mutex::new(HashMap::new()));+ let worker_flags = cancellations.clone();+ thread::Builder::new().name("slopfab-serial-queue".into()).spawn(move || {+ while let Ok(item) = receiver.recv() {+ let started = std::time::Instant::now();+ diagnostics::info("slopfab", "generation.started", "Native generation worker started.", serde_json::json!({+ "jobId": item.request.job_id,+ "frames": item.request.frames,+ "steps": item.request.steps,+ "canvasWidth": item.request.canvas_width,+ "canvasHeight": item.request.canvas_height,+ "referenceCount": item.request.reference_count(),+ "randomSeed": item.request.seed == -1,+ }));+ let result = run_generation(&item);+ let (state, detail, output) = match result {+ Ok((metadata, pictures)) => {+ /* The pictures wait in memory for the webview, which+ encodes them and asks Rust to write the file. What+ had to be dropped to make room is said out loud —+ an abandoned render is a scene that has to be run+ again, and the user is owed that sentence. */+ let evicted = rendered::keep(pictures);+ let detail = match evicted {+ Some(job) if job != item.request.job_id => format!(+ "Frames and audio are ready to be encoded. The frames of {job} were dropped to make room and that scene would have to be rendered again."+ ),+ _ => "Frames and audio are ready to be encoded.".to_string(),+ };+ ("framesReady", detail, Some(metadata))+ }+ Err(error) if item.cancel.load(Ordering::Acquire) => ("cancelled", error, None),+ Err(error) => ("failed", error, None),+ };+ let context = serde_json::json!({+ "jobId": item.request.job_id,+ "state": state,+ "elapsedMs": started.elapsed().as_millis().min(u64::MAX as u128) as u64,+ "output": output.as_ref().map(|value| serde_json::json!({+ "frames": value.frames, "width": value.width, "height": value.height,+ "fps": value.fps, "audioChannels": value.audio_channels,+ "audioSamples": value.audio_samples,+ "secondsConditioning": value.seconds_conditioning,+ "secondsDenoise": value.seconds_denoise,+ "secondsVideoDecode": value.seconds_video_decode,+ "secondsAudioDecode": value.seconds_audio_decode,+ "secondsTotal": value.seconds_total,+ "stepsComputed": value.steps_computed,+ "stepsSkipped": value.steps_skipped,+ })),+ });+ if state == "failed" {+ diagnostics::error("slopfab", "generation.finished", &detail, context);+ } else if state == "cancelled" {+ diagnostics::warn("slopfab", "generation.finished", &detail, context);+ } else {+ diagnostics::info("slopfab", "generation.finished", &detail, context);+ }+ (item.events)(NativeEvent::Job(JobEvent { job_id: item.request.job_id.clone(), state, detail, output }));+ let queue_empty = if let Ok(mut flags) = worker_flags.lock() {+ flags.remove(&item.request.job_id);+ flags.is_empty()+ } else {+ false+ };+ // Reuse is scoped to one contiguous batch. Keeping it while a+ // queued scene exists avoids repeating conditioning and+ // reference preparation; clearing at the drain boundary gives+ // the otherwise multi-gigabyte cache a deterministic lifetime.+ if queue_empty {+ if let Err(error) = clear_reused_models(&item.configuration) {+ diagnostics::warn("slopfab", "models.clear_failed", &error, serde_json::json!({}));+ }+ }+ }+ }).expect("could not start slopfab queue worker");+ Self {+ sender,+ references: ReferenceVideos::default(),+ cancellations,+ }+ }+}++pub(super) fn clear_reused_models(configuration: &Configuration) -> Result<(), String> {+ let api = ffi::Api::load(&configuration.dll_path)?;+ api.version()?;+ api.clear_reused_models()+}++impl SlopfabRuntime {+ pub fn enqueue(+ &self,+ events: EventSink,+ mut request: GenerationRequest,+ settings: &BTreeMap<String, ProviderSetting>,+ ) -> Result<(), String> {+ let configuration = Configuration::from_settings(settings);+ request.steps = configuration.generation_steps(request.steps)?;+ diagnostics::info(+ "slopfab",+ "generation.enqueue_requested",+ "Generation was submitted to the native queue.",+ serde_json::json!({+ "jobId": request.job_id,+ "frames": request.frames,+ "steps": request.steps,+ "canvasWidth": request.canvas_width,+ "canvasHeight": request.canvas_height,+ "referenceCount": request.reference_count(),+ "inputChars": request.prompt.chars().count(),+ }),+ );+ if request.job_id.trim().is_empty() {+ return Err("Generation job id cannot be empty.".into());+ }+ configuration.validate_inputs(&request)?;+ validate_generation_controls(&request)?;+ let cancel = Arc::new(AtomicBool::new(false));+ let mut flags = self+ .cancellations+ .lock()+ .map_err(|_| "Generation queue lock failed.")?;+ if flags.contains_key(&request.job_id) {+ return Err("This generation job is already queued.".into());+ }+ flags.insert(request.job_id.clone(), cancel.clone());+ drop(flags);+ let queued_id = request.job_id.clone();+ events(NativeEvent::Job(+ JobEvent {+ job_id: queued_id.clone(),+ state: "queued",+ detail: "Queued behind any active slopfab generation.".into(),+ output: None,+ },+ ));+ if self+ .sender+ .send(QueueItem {+ events: events.clone(),+ references: self.references.clone(),+ request,+ configuration,+ cancel,+ })+ .is_err()+ {+ self.cancellations+ .lock()+ .map_err(|_| "Generation queue lock failed.")?+ .remove(&queued_id);+ return Err("Generation queue is unavailable.".into());+ }+ diagnostics::info(+ "slopfab",+ "generation.queued",+ "Generation entered the native queue.",+ serde_json::json!({ "jobId": queued_id }),+ );+ Ok(())+ }++ pub fn cancel(&self, job_id: &str) -> bool {+ let accepted = self+ .cancellations+ .lock()+ .ok()+ .and_then(|flags| flags.get(job_id).cloned())+ .is_some_and(|flag| {+ flag.store(true, Ordering::Release);+ true+ });+ diagnostics::info(+ "slopfab",+ "generation.cancel_requested",+ if accepted {+ "Cancellation was accepted."+ } else {+ "No queued generation matched the cancellation."+ },+ serde_json::json!({+ "jobId": job_id, "accepted": accepted,+ }),+ );+ accepted+ }+}++pub(super) struct QueueItem {+ pub(super) references: ReferenceVideos,+ pub(super) events: EventSink,+ pub(super) request: GenerationRequest,+ pub(super) configuration: Configuration,+ pub(super) cancel: Arc<AtomicBool>,+}+diff --git a/src-tauri/src/slopfab/status.rs b/src-tauri/src/slopfab/status.rsnew file mode 100644index 0000000..fdac017--- /dev/null+++ b/src-tauri/src/slopfab/status.rs@@ -0,0 +1,76 @@+use super::*;++pub fn status(settings: &BTreeMap<String, ProviderSetting>) -> SlopfabStatus {+ let configuration = Configuration::from_settings(settings);+ let mut models = configuration+ .models+ .iter()+ .filter(|(id, _, _)| !configuration.animate || (*id != 1 && *id != 2))+ .map(|(_, id, path)| ModelStatus {+ id,+ configured: path.is_some(),+ available: path.as_deref().is_some_and(Path::is_file),+ path: path+ .as_ref()+ .map(|value| value.to_string_lossy().into_owned()),+ })+ .collect::<Vec<_>>();+ if configuration.animate {+ models.push(ModelStatus {+ id: "promptEmbedding", configured: configuration.prompt_embedding.is_some(),+ available: configuration.prompt_embedding.as_deref().is_some_and(Path::is_file),+ path: configuration.prompt_embedding.as_ref().map(|path| path.to_string_lossy().into_owned()),+ });+ }+ let dll_path = configuration.dll_path.to_string_lossy().into_owned();+ match ffi::Api::load(&configuration.dll_path) {+ Ok(api) => match api.version() {+ Ok(version) => {+ let detected = detect_platform(&api);+ let platform = configuration.platform(detected);+ let missing = models+ .iter()+ .filter(|model| !model.available && model.id != "tokenizer")+ .count();+ SlopfabStatus {+ state: if missing == 0 {+ "ready"+ } else {+ "modelsMissing"+ },+ dll_path,+ version: Some(version),+ platform: Some(platform.label()),+ cuda_available: detected != ComputePlatform::Vulkan,+ cuda_device_names: ffi::cuda_device_names(),+ detail: if missing == 0 {+ "Runtime and required model paths are available.".into()+ } else {+ format!("Runtime loaded; {missing} required model paths are missing. Editing remains available.")+ },+ models,+ }+ }+ Err(error) => SlopfabStatus {+ state: "incompatible",+ dll_path,+ version: None,+ platform: None,+ cuda_available: false,+ cuda_device_names: Vec::new(),+ detail: error,+ models,+ },+ },+ Err(error) => SlopfabStatus {+ state: "runtimeMissing",+ dll_path,+ version: None,+ platform: None,+ cuda_available: false,+ cuda_device_names: Vec::new(),+ detail: format!("slopfab is unavailable: {error}. Editing remains available."),+ models,+ },+ }+}diff --git a/src-tauri/src/slopfab/tests.rs b/src-tauri/src/slopfab/tests.rsnew file mode 100644index 0000000..391e498--- /dev/null+++ b/src-tauri/src/slopfab/tests.rs@@ -0,0 +1,468 @@+use super::*;++#[test]+fn animate_requires_video_and_prompt_mode_requires_text() {+ let mut configuration = Configuration::from_settings(&BTreeMap::new());+ let mut request = GenerationRequest::default();+ assert!(configuration.validate_inputs(&request).unwrap_err().contains("prompt"));+ configuration.animate = true;+ assert!(configuration.validate_inputs(&request).unwrap_err().contains("reference video"));+ request.reference_paths.push("image.png".into());+ assert!(configuration.validate_inputs(&request).is_err());+ request.reference_video_ids.push("video".into());+ configuration.validate_inputs(&request).unwrap();+ request.still_image = true;+ assert!(configuration.validate_inputs(&request).unwrap_err().contains("video generation"));+}++#[test]+fn animate_recipe_and_audio_selection_reach_bundled_dll() {+ let references = ReferenceVideos::default();+ let root = tempfile::tempdir().unwrap();+ let embedding = root.path().join("conditioning.safetensors");+ crate::conditioning::fixture(&embedding);+ let settings = BTreeMap::from([("slopfab".into(), ProviderSetting {+ enabled: true, model: None,+ options: BTreeMap::from([+ ("generationMode".into(), ProviderOption::String("animate".into())),+ ("promptEmbedding".into(), ProviderOption::String(embedding.to_string_lossy().into_owned())),+ ("stepOverride".into(), ProviderOption::Number(4.0)),+ ]),+ })]);+ let configuration = Configuration::from_settings(&settings);+ let api = ffi::Api::load(&configuration.dll_path).unwrap();+ let id = references.create_reference_video(2.0, &settings).unwrap();+ let result = (|| -> Result<(), String> {+ references.append_reference_video(&id, &vec![127; 64 * 64 * 4], 64, 64, 0.0)?;+ let request = GenerationRequest {+ job_id: "animate-test".into(), frames: 48, steps: 20, seed: 1,+ prompt: "This scene prompt must be ignored by Animate.".into(),+ canvas_width: 736, canvas_height: 416, reference_video_ids: vec![id.clone()],+ reference_paths: vec!["repainted.png".into()],+ ..Default::default()+ };+ for preserve_audio in [false, true] {+ if preserve_audio {+ references.set_reference_video_audio(&id, &vec![0; 32_000 * 2 * 4], 1, 32_000)?;+ }+ for platform in [ComputePlatform::Cuda13, ComputePlatform::Vulkan] {+ for purpose in [RequestPurpose::Plan, RequestPurpose::Generate] {+ let handle = RequestHandle::new(&api)?;+ configure_request(&api, &handle, &request, &configuration, platform, purpose, &references)?;+ assert_eq!(api.resolve(&handle)?.num_model_evaluations, 3);+ let description = api.describe(&handle)?;+ assert!(description.contains("fixed 362-token embedding; video then repainted image"));+ assert!(description.contains("reference short edge 416"));+ assert!(description.contains("video 1.000000 .. 0.600000 (shift 3.0)"));+ assert!(description.contains(if preserve_audio { "pinned driving soundtrack" } else { "generated; driving reference soundtrack omitted" }));+ assert!(description.contains(&format!("prompt {} characters", ANIMATE_PROMPT.len())));+ }+ }+ }+ Ok(())+ })();+ references.release_reference_videos(&[id]).unwrap();+ result.unwrap();+}++#[test]+fn animate_duration_limit_resolves_with_bundled_dll() {+ let references = ReferenceVideos::default();+ let root = tempfile::tempdir().unwrap();+ let embedding = root.path().join("conditioning.safetensors");+ crate::conditioning::fixture(&embedding);+ let settings = BTreeMap::new();+ let mut configuration = Configuration::from_settings(&settings);+ configuration.animate = true;+ configuration.prompt_embedding = Some(embedding);+ let api = ffi::Api::load(&configuration.dll_path).unwrap();+ let id = references.create_reference_video(15.0, &settings).unwrap();+ let result = (|| -> Result<(), String> {+ references.append_reference_video(&id, &vec![127; 64 * 64 * 4], 64, 64, 0.0)?;+ let mut request = GenerationRequest {+ prompt: "A dancer.".into(), steps: 4, seed: 1,+ canvas_width: 736, canvas_height: 416, reference_video_ids: vec![id.clone()],+ reference_paths: vec!["repainted.png".into()],+ ..Default::default()+ };+ for platform in [ComputePlatform::Cuda13, ComputePlatform::Vulkan] {+ for purpose in [RequestPurpose::Plan, RequestPurpose::Generate] {+ for (frames, expected) in [(48, 56), (336, 345), (345, 345), (346, 345), (348, 345), (359, 345), (360, 345)] {+ request.frames = frames;+ let handle = RequestHandle::new(&api)?;+ configure_request(&api, &handle, &request, &configuration, platform, purpose, &references)?;+ let plan = api.resolve(&handle)?;+ assert_eq!(plan.aligned_frames, expected, "requested {frames} frames");+ assert_eq!(plan.duration_seconds, f64::from(expected) / 24.0);+ assert_eq!(scene_frame_window(&request, plan.aligned_frames)?, (0, expected));+ }+ }+ }+ request.frames = 361;+ let handle = RequestHandle::new(&api)?;+ configure_request(&api, &handle, &request, &configuration, ComputePlatform::Cuda13, RequestPurpose::Plan, &references)?;+ assert!(api.resolve(&handle).err().unwrap().contains("at most 15 seconds"));++ // Other generators keep their existing upward alignment at 15 s.+ configuration.animate = false;+ request.frames = 360;+ let handle = RequestHandle::new(&api)?;+ configure_request(&api, &handle, &request, &configuration, ComputePlatform::Cuda13, RequestPurpose::Plan, &references)?;+ assert_eq!(api.resolve(&handle)?.aligned_frames, 362);+ Ok(())+ })();+ references.release_reference_videos(&[id]).unwrap();+ result.unwrap();+}++#[test]+fn animate_reports_missing_runtime_api() {+ let mut api = ffi::Api::load(&default_dll_path()).unwrap();+ api.disable_animate_for_test();+ let handle = RequestHandle::new(&api).unwrap();+ assert!(api.set_animate(&handle, true).unwrap_err().contains("1.10"));+}++#[test]+fn continuation_uses_22_frames_and_plans_only_the_new_scene() {+ let references = ReferenceVideos::default();+ // An independent 39-frame, 64x32 archive exercises the real DLL without+ // loading model weights. Its sampling window should be 22 + 34 frames.+ let root = tempfile::tempdir().unwrap();+ let path = root.path().join("previous.safetensors");+ let video_bytes = 24 * 96 * 4;+ let audio_bytes = 130 * 32 * 4;+ let mut header = serde_json::json!({+ "__metadata__": { "slopfab_latents": "h3-av-v1", "width": "64", "height": "32", "frames": "39", "fps": "24", "sampled": "1" },+ "video_rows": { "dtype": "F32", "shape": [24, 96], "data_offsets": [0, video_bytes] },+ "audio_rows": { "dtype": "F32", "shape": [130, 32], "data_offsets": [video_bytes, video_bytes + audio_bytes] },+ }).to_string();+ while header.len() % 8 != 0 { header.push(' '); }+ let mut bytes = (header.len() as u64).to_le_bytes().to_vec();+ bytes.extend_from_slice(header.as_bytes());+ bytes.resize(bytes.len() + video_bytes + audio_bytes, 0);+ std::fs::write(&path, bytes).unwrap();+ let mut request = GenerationRequest {+ job_id: "continued".into(), prompt: "The camera keeps moving.".into(),+ frames: 18, steps: 4, seed: 1, canvas_width: 64, canvas_height: 32,+ continuation_path: Some(path.clone()), save_latents_path: Some(root.path().join("next.safetensors")),+ ..Default::default()+ };+ let settings = BTreeMap::new();+ let configuration = Configuration::from_settings(&settings);+ let api = ffi::Api::load(&configuration.dll_path).unwrap();+ for platform in [ComputePlatform::Cuda13, ComputePlatform::Vulkan] {+ let handle = RequestHandle::new(&api).unwrap();+ configure_request(&api, &handle, &request, &configuration, platform, RequestPurpose::Generate, &references).unwrap();+ let raw = api.resolve(&handle).unwrap();+ assert_eq!(raw.aligned_frames, 73);+ assert_eq!(raw.latent_frames, 17); // 56-frame window, including 22 overlap.+ assert_eq!(scene_frame_window(&request, raw.aligned_frames).unwrap(), (39, 34));+ }+ let plan = resolve_plan(&request, &settings, &references).unwrap();+ assert_eq!(plan.aligned_frames, 34);+ assert!((plan.duration_seconds - 34.0 / 24.0).abs() < 1e-6);+ assert!(!request.save_latents_path.as_ref().unwrap().exists()); // Planning never writes.+ request.canvas_width = 128;+ assert!(resolve_plan(&request, &settings, &references).is_err());+ request.canvas_width = 64;+ std::fs::write(&path, b"invalid archive").unwrap();+ assert!(resolve_plan(&request, &settings, &references).is_err());+}++#[test]+fn continuation_video_and_audio_skip_the_cumulative_source() {+ let mut request = GenerationRequest { frames: 119, continuation_path: Some("source".into()), ..Default::default() };+ assert_eq!(scene_frame_window(&request, 243).unwrap(), (124, 119));+ // A further continuation skips the full joined source, not just its last scene.+ assert_eq!(scene_frame_window(&request, 362).unwrap(), (243, 119));+ assert_eq!(scene_audio_offset(124, 24.0, 2, 48_000, 972_000).unwrap(), 496_000);+ assert_eq!(scene_audio_offset(243, 24.0, 2, 44_100, 2_000_000).unwrap(), 893_026);+ assert_eq!(scene_audio_offset(124, 24.0, 0, 0, 0).unwrap(), 0);+ assert!(scene_audio_offset(124, 24.0, 2, 48_000, 100).is_err());+ assert!(scene_frame_window(&request, 120).is_err());+ request.continuation_path = None;+ assert_eq!(scene_frame_window(&request, 124).unwrap(), (0, 124));+}++#[test]+fn refmods_attach_for_plans_and_icons_or_report_an_older_dll() {+ let references = ReferenceVideos::default();+ let configuration = Configuration::from_settings(&BTreeMap::new());+ let api = ffi::Api::load(&configuration.dll_path).unwrap();+ let root = tempfile::tempdir().unwrap();+ let file = root.path().join("person.safetensors");+ let header = serde_json::json!({+ "__metadata__": { "refmod_meta": serde_json::json!({ "kind": "image", "name": "Person", "_format_version": 4 }).to_string() },+ "latent": { "dtype": "F32", "shape": [1, 24, 1, 2, 2], "data_offsets": [0, 384] }+ }).to_string();+ let mut bytes = (header.len() as u64).to_le_bytes().to_vec();+ bytes.extend_from_slice(header.as_bytes());+ for _ in 0..96 { bytes.extend_from_slice(&0.25_f32.to_le_bytes()); }+ std::fs::write(&file, bytes).unwrap();+ let mut request = GenerationRequest {+ job_id: "refmod-test".into(), prompt: "A portrait.".into(), still_image: true, frames: 1,+ steps: 20, seed: 1, canvas_width: 768, canvas_height: 768,+ reference_paths: Vec::new(), reference_video_ids: Vec::new(),+ refmods: vec![RefmodInput { path: file.to_string_lossy().into_owned(), strength: 0.7, copies: 2 }],+ ..Default::default()+ };+ let probe = RequestHandle::new(&api).unwrap();+ let added = api.add_refmod(&probe, &file, 0.7, 2);+ if let Err(error) = &added {+ assert!(error.contains("does not support refmods"), "{error}");+ eprintln!("Refmod export not yet available in the installed DLL; checked compatibility error.");+ assert!(configure_request(&api, &probe, &request, &configuration, ComputePlatform::Vulkan, RequestPurpose::Plan, &references).unwrap_err().contains("does not support refmods"));+ } else {+ for platform in [ComputePlatform::Cuda13, ComputePlatform::Vulkan] {+ for still_image in [false, true] {+ request.still_image = still_image;+ request.frames = if still_image { 1 } else { 120 };+ for purpose in [RequestPurpose::Plan, RequestPurpose::Generate] {+ let handle = RequestHandle::new(&api).unwrap();+ configure_request(&api, &handle, &request, &configuration, platform, purpose, &references).unwrap();+ let plan = api.resolve(&handle).unwrap();+ assert!(plan.aligned_frames >= request.frames);+ }+ }+ }+ }+ request.refmods[0].strength = 0.0;+ std::fs::remove_file(&file).unwrap();+ let handle = RequestHandle::new(&api).unwrap();+ configure_request(&api, &handle, &request, &configuration, ComputePlatform::Vulkan, RequestPurpose::Generate, &references).unwrap();+ request.refmods[0].copies = 11;+ assert!(configure_request(&api, &handle, &request, &configuration, ComputePlatform::Vulkan, RequestPurpose::Plan, &references).is_err());+}++#[test]+fn ordered_loras_and_step_override_reach_cuda_and_vulkan_requests() {+ let references = ReferenceVideos::default();+ let root = tempfile::tempdir().unwrap();+ let paths = [root.path().join("style.safetensors"), root.path().join("TaoMate.safetensors")];+ for path in &paths { std::fs::write(path, b"planning does not load weights").unwrap(); }+ let settings = BTreeMap::from([("slopfab".into(), ProviderSetting {+ enabled: true, model: None,+ options: BTreeMap::from([+ ("loras".into(), ProviderOption::String(serde_json::json!([+ { "path": paths[0], "strength": -0.5 },+ { "path": paths[1], "strength": 1.0 },+ ]).to_string())),+ ("stepOverride".into(), ProviderOption::Number(8.0)),+ ]),+ })]);+ let configuration = Configuration::from_settings(&settings);+ let api = ffi::Api::load(&configuration.dll_path).unwrap();+ let request = GenerationRequest {+ job_id: "lora-test".into(), prompt: "A quiet harbour.".into(),+ frames: 120, still_image: false, steps: 20, seed: 1,+ canvas_width: 736, canvas_height: 416,+ reference_paths: Vec::new(), reference_video_ids: Vec::new(),+ refmods: Vec::new(),+ ..Default::default()+ };+ for platform in [ComputePlatform::Cuda13, ComputePlatform::Vulkan] {+ for purpose in [RequestPurpose::Plan, RequestPurpose::Generate] {+ let handle = RequestHandle::new(&api).unwrap();+ configure_request(&api, &handle, &request, &configuration, platform, purpose, &references).unwrap();+ assert_eq!(api.resolve(&handle).unwrap().num_model_evaluations, 7);+ let description = api.describe(&handle).unwrap();+ assert!(!description.contains("taomate-3step"));+ assert!(description.find("style.safetensors").unwrap() < description.find("TaoMate.safetensors").unwrap());+ assert!(description.contains("strength -0.5"));+ }+ }+ let invalid = BTreeMap::from([("slopfab".into(), ProviderSetting {+ enabled: true, model: None,+ options: BTreeMap::from([("loras".into(), ProviderOption::String("invalid JSON".into()))]),+ })]);+ assert!(Configuration::from_settings(&invalid).loras.is_err());+ std::fs::remove_file(&paths[0]).unwrap();+ let handle = RequestHandle::new(&api).unwrap();+ assert!(configure_request(&api, &handle, &request, &configuration, ComputePlatform::Vulkan, RequestPurpose::Generate, &references).unwrap_err().contains("LoRA file is missing"));+}++#[test]+fn vulkan_video_references_prepare_and_attach_for_planning_and_generation() {+ let references = ReferenceVideos::default();+ let settings = BTreeMap::from([("slopfab".into(), ProviderSetting {+ enabled: true,+ model: None,+ options: BTreeMap::from([("inferenceBackend".into(), ProviderOption::String("vulkan".into()))]),+ })]);+ let id = references.create_reference_video(2.0, &settings).unwrap();+ let result = (|| -> Result<(), String> {+ references.append_reference_video(&id, &vec![127; 64 * 64 * 4], 64, 64, 0.0)?;+ references.set_reference_video_audio(&id, &vec![0; 32_000 * 2 * 4], 1, 32_000)?;+ let request = GenerationRequest {+ job_id: "vulkan-reference-test".into(), prompt: "A scene using <Video 1>.".into(),+ frames: 120, still_image: false, steps: 12, seed: 1,+ canvas_width: 736, canvas_height: 416,+ reference_paths: Vec::new(), reference_video_ids: vec![id.clone()],+ refmods: Vec::new(),+ ..Default::default()+ };+ let configuration = Configuration::from_settings(&settings);+ let api = ffi::Api::load(&configuration.dll_path)?;+ for purpose in [RequestPurpose::Plan, RequestPurpose::Generate] {+ let handle = RequestHandle::new(&api)?;+ configure_request(&api, &handle, &request, &configuration, ComputePlatform::Vulkan, purpose, &references)?;+ assert!(api.resolve(&handle)?.aligned_frames > 0);+ }+ Ok(())+ })();+ references.release_reference_videos(&[id]).unwrap();+ result.unwrap();+}++#[test]+fn video_reference_c_api_copies_inputs_and_retains_attached_snapshot() {+ let path = default_dll_path();+ let api = ffi::Api::load(&path).unwrap();+ let request = RequestHandle::new(&api).unwrap();+ api.set_prompt(&request, "A scene using <Video 1>.").unwrap();+ api.set_frames(&request, 120).unwrap();+ api.set_resolution(&request, 736, 416).unwrap();+ let mut video = ffi::ReferenceVideoHandle::new(ffi::Api::load(&path).unwrap(), 2.0, path.clone()).unwrap();+ let pixels = vec![127_u8; 64 * 64 * 4];+ video.append(&pixels, 64, 64, 0.0).unwrap();+ assert!(video.append(&pixels, 64, 64, 0.0).is_err());+ assert!(video.append(&pixels[..10], 64, 64, 0.5).is_err());+ video.append(&pixels, 64, 64, 1.0).unwrap();+ video.set_audio(&vec![0.0; 32_000 * 2], 1, 32_000).unwrap();+ video.attach(&request, &path).unwrap();+ drop(video);+ drop(pixels);+ // Plan resolution reads the retained video/audio snapshot without a+ // neural model or a CUDA allocation, even after the host releases it.+ let plan = api.resolve(&request).unwrap();+ assert!(plan.aligned_frames > 0);+}++#[test]+fn absent_dll_is_an_explicit_disabled_state() {+ let mut settings = BTreeMap::new();+ settings.insert(+ "slopfab".into(),+ ProviderSetting {+ enabled: true,+ model: None,+ options: BTreeMap::from([(+ "dllPath".into(),+ ProviderOption::String("Z:/definitely-absent/slopfab.dll".into()),+ )]),+ },+ );+ let status = status(&settings);+ assert_eq!(status.state, "runtimeMissing");+}+#[test]+fn compatible_major_version_decodes_from_packed_value() {+ let packed = (1_u32 << 24) | (37 << 12) | 5;+ assert_eq!(packed >> 24, EXPECTED_CAPI_MAJOR);+}+#[test]+fn platform_selection_prefers_supported_cuda_majors_and_falls_back_to_vulkan() {+ assert_eq!(platform_from_cuda_probe(Ok(13)), ComputePlatform::Cuda13);+ assert_eq!(platform_from_cuda_probe(Ok(12)), ComputePlatform::Cuda12);+ assert_eq!(platform_from_cuda_probe(Ok(11)), ComputePlatform::Vulkan);+ assert_eq!(+ platform_from_cuda_probe(Err("no CUDA toolkit".into())),+ ComputePlatform::Vulkan+ );+ assert_eq!(ComputePlatform::Cuda13.backend(), 0);+ assert_eq!(ComputePlatform::Vulkan.backend(), 1);+}++#[test]+fn backend_preference_cannot_enable_cuda_without_cuda_hardware() {+ let mut config = Configuration::from_settings(&BTreeMap::new());+ assert_eq!(config.platform(ComputePlatform::Cuda13), ComputePlatform::Cuda13);+ assert_eq!(config.platform(ComputePlatform::Cuda12), ComputePlatform::Cuda12);+ assert_eq!(config.platform(ComputePlatform::Vulkan), ComputePlatform::Vulkan);+ config.vulkan = true;+ for detected in [ComputePlatform::Cuda13, ComputePlatform::Cuda12, ComputePlatform::Vulkan] {+ assert_eq!(config.platform(detected), ComputePlatform::Vulkan);+ }+}++#[test]+fn attention_defaults_to_sage_and_is_independent_of_backend() {+ assert_eq!(Configuration::from_settings(&BTreeMap::new()).attention, "sage2");+ for backend in ["cuda", "vulkan"] {+ for attention in ["exact", "flash2", "sage2", "invalid"] {+ let settings = serde_json::from_value(serde_json::json!({+ "slopfab": { "enabled": true, "model": null,+ "options": { "inferenceBackend": backend, "attention": attention } }+ })).unwrap();+ let config = Configuration::from_settings(&settings);+ let expected = if attention == "invalid" { "sage2" } else { attention };+ assert_eq!(config.attention, expected);+ assert_eq!(config.vulkan, backend == "vulkan");+ assert!(config.timing_profile("1.4.0", ComputePlatform::Vulkan).contains(&format!("attention={expected}|")));+ }+ }+}++#[test]+#[cfg(windows)]+fn bundled_dll_reports_a_compatible_api() {+ assert!(default_dll_path().is_file(), "The build must stage slopfab.dll beside the executable.");+ let runtime = status(&BTreeMap::new());+ assert_ne!(runtime.state, "runtimeMissing", "{}", runtime.detail);+ assert_ne!(runtime.state, "incompatible", "{}", runtime.detail);+ assert!(runtime+ .version+ .as_deref()+ .is_some_and(|value| value.starts_with("1.")));+ assert!(matches!(+ runtime.platform,+ Some("CUDA 13" | "CUDA 12" | "Vulkan")+ ));+}+#[test]+fn stage_ids_are_normalized_without_assuming_an_enum() {+ assert_eq!(stage_name(4), "denoising");+ assert_eq!(stage_name(99), "unknown");+}++#[test]+fn installed_dll_resolves_icons_as_one_still_frame_when_present() {+ let references = ReferenceVideos::default();+ if !default_dll_path().is_file() { return; }+ let request: GenerationRequest = serde_json::from_value(serde_json::json!({+ "jobId": "icon-plan", "prompt": "A red toy car", "frames": 1, "stillImage": true,+ "steps": 20, "seed": 1, "canvasWidth": 768, "canvasHeight": 768+ })).unwrap();+ let plan = resolve_plan(&request, &BTreeMap::new(), &references).unwrap();+ assert_eq!(plan.aligned_frames, 1);+ assert_eq!(plan.latent_frames, 1);+ assert_eq!((plan.canvas_width, plan.canvas_height), (768, 768));+}++#[test]+fn bundled_dll_accepts_all_attention_modes_on_both_backends() {+ let references = ReferenceVideos::default();+ if !default_dll_path().is_file() { return; }+ let api = ffi::Api::load(&default_dll_path()).unwrap();+ let request: GenerationRequest = serde_json::from_value(serde_json::json!({+ "jobId": "attention-plan", "prompt": "A red toy car", "frames": 1, "stillImage": true,+ "steps": 20, "seed": 1, "canvasWidth": 768, "canvasHeight": 768+ })).unwrap();+ let mut config = Configuration::from_settings(&BTreeMap::new());+ for platform in [ComputePlatform::Cuda13, ComputePlatform::Vulkan] {+ for attention in ["exact", "flash2", "sage2"] {+ config.attention = attention;+ let handle = RequestHandle::new(&api).unwrap();+ configure_request(&api, &handle, &request, &config, platform, RequestPurpose::Plan, &references).unwrap();+ assert_eq!(api.resolve(&handle).unwrap().aligned_frames, 1);+ }+ }+}+#[test]+fn generation_seed_accepts_random_and_non_negative_values() {+ assert!(generation_seed(-1).is_ok());+ assert_eq!(generation_seed(0).unwrap(), 0);+ assert_eq!(generation_seed(482_091).unwrap(), 482_091);+ assert!(generation_seed(-2).is_err());+}diff --git a/src-tauri/src/slopfab/types.rs b/src-tauri/src/slopfab/types.rsnew file mode 100644index 0000000..87ead92--- /dev/null+++ b/src-tauri/src/slopfab/types.rs@@ -0,0 +1,142 @@+use super::*;++#[derive(Debug, Clone, Serialize)]+#[serde(rename_all = "camelCase")]+pub struct SlopfabStatus {+ pub state: &'static str,+ pub dll_path: String,+ pub version: Option<String>,+ pub platform: Option<&'static str>,+ pub cuda_available: bool,+ pub cuda_device_names: Vec<String>,+ pub detail: String,+ pub models: Vec<ModelStatus>,+}++#[derive(Debug, Clone, Serialize)]+#[serde(rename_all = "camelCase")]+pub struct ModelStatus {+ pub id: &'static str,+ pub configured: bool,+ pub available: bool,+ pub path: Option<String>,+}++#[derive(Debug, Clone, Serialize)]+#[serde(rename_all = "camelCase")]+pub struct GpuDevice {+ pub name: String,+ pub memory_bytes: u64,+}++pub fn gpu_devices() -> Vec<GpuDevice> { ffi::gpu_devices() }++#[derive(Debug, Clone, Default, Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct GenerationRequest {+ pub job_id: String,+ pub prompt: String,+ pub frames: i32,+ #[serde(default)]+ pub still_image: bool,+ pub steps: i32,+ /// -1 asks the host to draw a fresh seed for this generation.+ pub seed: i64,+ pub canvas_width: i32,+ pub canvas_height: i32,+ #[serde(default)]+ pub reference_paths: Vec<String>,+ #[serde(default)]+ pub reference_video_ids: Vec<String>,+ #[serde(default)]+ pub refmods: Vec<RefmodInput>,+ #[serde(default)]+ pub continuation_relative_path: Option<String>,+ // Only the project commands resolve paths; IPC cannot supply save targets.+ #[serde(skip)]+ pub continuation_path: Option<PathBuf>,+ #[serde(skip)]+ pub save_latents_path: Option<PathBuf>,+}++#[derive(Debug, Clone, Deserialize, Serialize)]+pub struct RefmodInput {+ pub path: String,+ pub strength: f32,+ pub copies: u32,+}++impl GenerationRequest {+ pub fn reference_count(&self) -> usize {+ self.reference_paths.len() + self.reference_video_ids.len()+ }+}+#[derive(Debug, Clone, Serialize)]+#[serde(rename_all = "camelCase")]+pub struct ResolvedPlan {+ pub canvas_width: i32,+ pub canvas_height: i32,+ pub aligned_frames: i32,+ pub duration_seconds: f64,+ pub model_evaluations: i32,+ pub sequence_rows_without_text: i32,+ pub latent_frames: i32,+ pub latent_width: i32,+ pub latent_height: i32,+ pub description: String,+ pub boundary: &'static str,+}++#[derive(Debug, Clone, Serialize)]+#[serde(rename_all = "camelCase")]+pub struct ProgressEvent {+ pub(super) job_id: String,+ pub(super) stage: &'static str,+ pub(super) step: i32,+ pub(super) total_steps: i32,+ pub(super) planned_steps: i32,+ pub(super) elapsed_seconds: f64,+ pub(super) frames: i32,+ pub(super) canvas_width: i32,+ pub(super) canvas_height: i32,+ pub(super) reference_count: usize,+ pub(super) timing_profile: String,+}++#[derive(Debug, Clone, Serialize)]+#[serde(rename_all = "camelCase")]+pub struct JobEvent {+ pub(super) job_id: String,+ pub(super) state: &'static str,+ pub(super) detail: String,+ pub(super) output: Option<OutputMetadata>,+}++/// What came back, and what the webview needs in order to encode it: the+/// shape of the pictures now waiting in [`crate::rendered`], and how much sound+/// came with them.+#[derive(Debug, Clone, Serialize)]+#[serde(rename_all = "camelCase")]+pub(super) struct OutputMetadata {+ pub(super) frames: i32,+ pub(super) width: i32,+ pub(super) height: i32,+ pub(super) fps: f64,+ pub(super) audio_channels: i32,+ pub(super) audio_sample_rate: i32,+ /// Interleaved samples across every channel, so the webview can tell an+ /// empty soundtrack from one it has not read yet.+ pub(super) audio_samples: usize,+ pub(super) reference_count: usize,+ pub(super) timing_profile: String,+ pub(super) seconds_conditioning: f64,+ pub(super) seconds_denoise: f64,+ pub(super) seconds_video_decode: f64,+ pub(super) seconds_audio_decode: f64,+ pub(super) seconds_total: f64,+ pub(super) steps_computed: i32,+ pub(super) steps_skipped: i32,+ pub(super) duration_seconds: f64,+ pub(super) boundary: &'static str,+}+diff --git a/src-tauri/src/storage/atomic.rs b/src-tauri/src/storage/atomic.rsindex a8eee26..e6ca465 100644--- a/src-tauri/src/storage/atomic.rs+++ b/src-tauri/src/storage/atomic.rs@@ -47,26 +47,36 @@ pub(crate) fn atomic_replace(temporary: &Path, destination: &Path) -> io::Result}}++static NEXT_TEMPORARY: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);++struct TemporaryFile(PathBuf);+impl Drop for TemporaryFile {+ fn drop(&mut self) { let _ = fs::remove_file(&self.0); }+}++pub(crate) fn write_with_replacer<F>(destination: &Path, bytes: &[u8], replacer: F) -> io::Result<()>+where F: FnOnce(&Path, &Path) -> io::Result<()> {+ use io::Write;+ use std::sync::atomic::Ordering;+ let parent = destination.parent().ok_or_else(|| io::Error::other("Destination has no parent"))?;+ let name = destination.file_name().ok_or_else(|| io::Error::other("Destination has no filename"))?;+ let (temporary, mut file) = loop {+ let sequence = NEXT_TEMPORARY.fetch_add(1, Ordering::Relaxed);+ let path = parent.join(format!(".{}.{}.{}.tmp", name.to_string_lossy(), std::process::id(), sequence));+ match fs::OpenOptions::new().write(true).create_new(true).open(&path) {+ Ok(file) => break (TemporaryFile(path), file),+ Err(error) if error.kind() == io::ErrorKind::AlreadyExists => continue,+ Err(error) => return Err(error),+ }+ };+ file.write_all(bytes)?;+ file.sync_all()?;+ drop(file);+ replacer(&temporary.0, destination)+}+pub(crate) fn write_atomically(destination: &Path, bytes: &[u8]) -> Result<(), String> {- let mut temporary = destination.as_os_str().to_os_string();- temporary.push(".part");- let temporary = PathBuf::from(temporary);- use std::io::Write;- let written = (|| {- let mut file = fs::File::create(&temporary)?;- file.write_all(bytes)?;- file.sync_all()- })();- if let Err(error) = written {- let _ = fs::remove_file(&temporary);- return Err(format!("Could not write {}: {error}", temporary.to_string_lossy()));- }- if let Err(error) = atomic_replace(&temporary, destination) {- let _ = fs::remove_file(&temporary);- return Err(format!(- "Could not save {}: {error}",- destination.to_string_lossy()- ));- }- Ok(())-}\ No newline at end of file+ write_with_replacer(destination, bytes, atomic_replace)+ .map_err(|error| format!("Could not save {}: {error}", destination.display()))+}diff --git a/src-tauri/src/tests/group_1.rs b/src-tauri/src/tests/group_1.rsindex 8b7edf6..99cf463 100644--- a/src-tauri/src/tests/group_1.rs+++ b/src-tauri/src/tests/group_1.rs@@ -247,6 +247,7 @@ fn the_windows_verbatim_prefix_never_reaches_the_user() {#[test]fn video_and_audio_are_recorded_where_they_are_and_images_are_copied() {+ let access = MediaAccess::default();let root = tempfile::tempdir().unwrap();let project = root.path().join("project");fs::create_dir(&project).unwrap();@@ -264,7 +265,7 @@ fn video_and_audio_are_recorded_where_they_are_and_images_are_copied() {// A rush is not duplicated to make a folder "portable": the project// records where it already is.for name in ["news broadcast.mp4", "room tone.wav"] {- let imported = import_media_file(&outside.join(name), &project).unwrap();+ let imported = import_media_file(&access, &outside.join(name), &project).unwrap();assert_eq!(imported.relative_path, None, "{name} was copied");let source = imported.source_path.expect("external media needs a path");assert!(@@ -279,7 +280,7 @@ fn video_and_audio_are_recorded_where_they_are_and_images_are_copied() {assert!(Path::new(&source).is_file());}// A picture is small, and a project's own artwork should travel with it.- let image = import_media_file(&outside.join("title card.png"), &project).unwrap();+ let image = import_media_file(&access, &outside.join("title card.png"), &project).unwrap();assert_eq!(image.relative_path.as_deref(), Some("media/title card.png"));assert_eq!(image.source_path, None);assert!(project.join("media/title card.png").is_file());@@ -295,6 +296,7 @@ fn video_and_audio_are_recorded_where_they_are_and_images_are_copied() {#[test]fn a_video_reference_is_pointed_at_while_an_image_reference_is_copied() {+ let access = MediaAccess::default();let root = tempfile::tempdir().unwrap();let project = root.path().join("project");fs::create_dir(&project).unwrap();@@ -303,7 +305,7 @@ fn a_video_reference_is_pointed_at_while_an_image_reference_is_copied() {let image = root.path().join("harbor facade.png");fs::write(&image, b"\x89PNG\r\n\x1a\n").unwrap();- let referenced = import_reference_file(&video, &project).unwrap();+ let referenced = import_reference_file(&access, &video, &project).unwrap();assert_eq!(referenced.kind, "video");assert_eq!(referenced.relative_path, None);assert!(referenced.source_path.is_some());@@ -315,7 +317,7 @@ fn a_video_reference_is_pointed_at_while_an_image_reference_is_copied() {"a video reference was copied into the project");- let copied = import_reference_file(&image, &project).unwrap();+ let copied = import_reference_file(&access, &image, &project).unwrap();assert_eq!(copied.kind, "image");assert_eq!(copied.relative_path.as_deref(),@@ -327,6 +329,7 @@ fn a_video_reference_is_pointed_at_while_an_image_reference_is_copied() {#[test]fn external_media_is_readable_only_because_the_project_names_it() {+ let access = MediaAccess::default();let root = tempfile::tempdir().unwrap();let outside = root.path().join("Rushes");fs::create_dir(&outside).unwrap();@@ -357,23 +360,24 @@ fn external_media_is_readable_only_because_the_project_names_it() {// The file the user picked, which the project now names.assert_eq!(- external_media_bytes(folder, &recorded).unwrap(),+ external_media_bytes(&access, folder, &recorded).unwrap(),b"rush bytes");// A media file next to it that the project does NOT name.- let error = external_media_bytes(folder, &unlisted.to_string_lossy()).unwrap_err();+ let error = external_media_bytes(&access, folder, &unlisted.to_string_lossy()).unwrap_err();assert!(error.contains("not one of the files this project points at"));// And the shape this command must never take: reading anything the// caller asks for. A path the project does not carry is refused// whatever it points at.- assert!(external_media_bytes(folder, &secret.to_string_lossy()).is_err());- assert!(external_media_bytes(folder, "/etc/passwd").is_err());+ assert!(external_media_bytes(&access, folder, &secret.to_string_lossy()).is_err());+ assert!(external_media_bytes(&access, folder, "/etc/passwd").is_err());}#[test]fn a_clip_imported_a_moment_ago_previews_before_the_project_is_saved() {+ let access = MediaAccess::default();// Saving is a deliberate act, so the project file on disk does not// mention a clip the user just imported. Refusing to read it until they// press Save would mean every fresh import shows a broken preview.@@ -384,14 +388,14 @@ fn a_clip_imported_a_moment_ago_previews_before_the_project_is_saved() {let clip = root.path().join("just picked.mp4");fs::write(&clip, b"fresh rush").unwrap();- let imported = import_media_file(&clip, &project).unwrap();+ let imported = import_media_file(&access, &clip, &project).unwrap();let source = imported.source_path.unwrap();assert!(recorded_external_paths(&read_project(&project).unwrap().config).is_empty(),"the saved project must not know about the import yet");assert_eq!(- external_media_bytes(&project.to_string_lossy(), &source).unwrap(),+ external_media_bytes(&access, &project.to_string_lossy(), &source).unwrap(),b"fresh rush");@@ -400,7 +404,7 @@ fn a_clip_imported_a_moment_ago_previews_before_the_project_is_saved() {let sibling = root.path().join("never picked.mp4");fs::write(&sibling, b"other rush").unwrap();assert!(- external_media_bytes(&project.to_string_lossy(), &sibling.to_string_lossy()).is_err()+ external_media_bytes(&access, &project.to_string_lossy(), &sibling.to_string_lossy()).is_err());}diff --git a/src-tauri/src/tests/group_2.rs b/src-tauri/src/tests/group_2.rsindex b0923aa..5424f18 100644--- a/src-tauri/src/tests/group_2.rs+++ b/src-tauri/src/tests/group_2.rs@@ -8,6 +8,7 @@use super::*;#[test]fn a_project_the_user_only_opened_reads_any_media_file_it_names() {+ let access = MediaAccess::default();let root = tempfile::tempdir().unwrap();let pictures = root.path().join("Pictures");fs::create_dir(&pictures).unwrap();@@ -47,7 +48,7 @@ fn a_project_the_user_only_opened_reads_any_media_file_it_names() {// The rule, stated plainly: the project names it, so it is served.let named = config.assets[0].source_path.clone().unwrap();assert_eq!(- external_media_bytes(&folder, &named).unwrap(),+ external_media_bytes(&access, &folder, &named).unwrap(),b"PRIVATE PHOTO BYTES","this is the documented perimeter, not a wish about it");@@ -55,11 +56,11 @@ fn a_project_the_user_only_opened_reads_any_media_file_it_names() {// And the two things that DO bound it. Media extensions only, checked// on the canonical path — a project file cannot name credentials.let crafted = config.assets[1].source_path.clone().unwrap();- assert!(external_media_bytes(&folder, &crafted)+ assert!(external_media_bytes(&access, &folder, &crafted).unwrap_err().contains("not a media file"));// And only what the project names: not the whole folder around it.- assert!(external_media_bytes(&folder, &unnamed.to_string_lossy())+ assert!(external_media_bytes(&access, &folder, &unnamed.to_string_lossy()).unwrap_err().contains("not one of the files this project points at"));}@@ -71,6 +72,7 @@ fn a_project_the_user_only_opened_reads_any_media_file_it_names() {// already in memory.#[test]fn one_project_s_picks_are_not_another_project_s_to_read() {+ let access = MediaAccess::default();let root = tempfile::tempdir().unwrap();let rushes = root.path().join("Rushes");fs::create_dir(&rushes).unwrap();@@ -82,16 +84,16 @@ fn one_project_s_picks_are_not_another_project_s_to_read() {// Imported into MY project, and not saved yet: only the session list// knows about it.- let imported = import_media_file(&footage, &mine).unwrap();+ let imported = import_media_file(&access, &footage, &mine).unwrap();let source = imported.source_path.unwrap();assert!(recorded_external_paths(&read_project(&mine).unwrap().config).is_empty());assert_eq!(- external_media_bytes(&mine.to_string_lossy(), &source).unwrap(),+ external_media_bytes(&access, &mine.to_string_lossy(), &source).unwrap(),b"A's footage","the project the clip was imported into must still preview it");- let error = external_media_bytes(&theirs.to_string_lossy(), &source).unwrap_err();+ let error = external_media_bytes(&access, &theirs.to_string_lossy(), &source).unwrap_err();assert!(error.contains("not one of the files this project points at"),"another project read a file picked for this one: {error}"@@ -103,6 +105,7 @@ fn one_project_s_picks_are_not_another_project_s_to_read() {// pair was the hole: every check passed while pointing at `.ssh`.#[test]fn reading_a_project_file_requires_the_folder_to_hold_a_project() {+ let access = MediaAccess::default();let root = tempfile::tempdir().unwrap();let ssh = root.path().join(".ssh");fs::create_dir(&ssh).unwrap();@@ -120,7 +123,7 @@ fn reading_a_project_file_requires_the_folder_to_hold_a_project() {"unexpected error: {error}");// Same shape for the external read, which takes the folder too.- assert!(external_media_bytes(+ assert!(external_media_bytes(&access,&ssh.to_string_lossy(),&ssh.join("id_rsa").to_string_lossy())@@ -138,6 +141,7 @@ fn reading_a_project_file_requires_the_folder_to_hold_a_project() {#[test]fn a_project_file_only_yields_media_a_hand_edit_cannot_widen() {+ let access = MediaAccess::default();// Belt and braces for the direction the allow-list alone cannot cover:// a HAND-EDITED project file naming something that is not media. The// path is recorded, so the allow-list passes — the extension check is@@ -162,7 +166,7 @@ fn a_project_file_only_yields_media_a_hand_edit_cannot_widen() {});let created = create_project_in(root.path(), &config).unwrap();let recorded = created.config.assets[0].source_path.clone().unwrap();- let error = external_media_bytes(&created.folder_path, &recorded).unwrap_err();+ let error = external_media_bytes(&access, &created.folder_path, &recorded).unwrap_err();assert!(error.contains("not a media file"),"unexpected error: {error}"@@ -332,18 +336,19 @@ fn refmod_attachments_round_trip_and_validate_ranges_and_paths() {#[test]fn unified_reference_import_copies_images_and_keeps_video_and_refmod_sources() {+ let access = MediaAccess::default();let root = tempfile::tempdir().unwrap();let project = root.path().join("project");fs::create_dir_all(&project).unwrap();let paths = [root.path().join("photo.png"), root.path().join("motion.mp4"), root.path().join("person.safetensors")];for path in &paths { fs::write(path, b"source").unwrap(); }- let imported = import_reference_attachments(&project, &paths).unwrap();+ let imported = import_reference_attachments(&access, &project, &paths).unwrap();assert_eq!(imported.iter().map(|file| file.kind).collect::<Vec<_>>(), ["image", "video", "refmod"]);assert!(project.join(imported[0].relative_path.as_ref().unwrap()).is_file());assert!(imported[2].relative_path.is_none());assert_eq!(imported[2].source_path.as_deref(), Some(external_source_path(&paths[2]).unwrap().as_str()));assert!(paths.iter().all(|path| path.is_file()));- assert!(import_reference_attachments(&project, &[paths[1].clone(), paths[1].clone()]).unwrap_err().contains("one video"));+ assert!(import_reference_attachments(&access, &project, &[paths[1].clone(), paths[1].clone()]).unwrap_err().contains("one video"));assert!(reference_attachment_kind(Path::new("script.exe")).is_err());}