Skip to main content

vc_runtime/
sdxl.rs

1use crate::{
2    renderer::{ImageRenderer, RenderParams},
3    BackendKind, RuntimeBackend, RuntimeInput, RuntimeResult, RuntimeStatus,
4    RUNTIME_SCHEMA_VERSION,
5};
6use anyhow::Result;
7use std::path::Path;
8
9pub struct SdxlBackend {
10    renderer: Box<dyn ImageRenderer>,
11    width: u32,
12    height: u32,
13    steps: u32,
14    cfg: f64,
15    sampler: String,
16    model: String,
17    negative_prompt: String,
18}
19
20impl SdxlBackend {
21    /// Create a new SDXL backend with the given renderer.
22    pub fn new(renderer: Box<dyn ImageRenderer>) -> Self {
23        Self {
24            renderer,
25            width: 1024,
26            height: 1024,
27            steps: 30,
28            cfg: 7.0,
29            sampler: "DPM++ 2M Karras".to_string(),
30            model: "sdxl-base-1.0".to_string(),
31            negative_prompt: String::new(),
32        }
33    }
34
35    pub fn with_negative_prompt(mut self, negative: impl Into<String>) -> Self {
36        self.negative_prompt = negative.into();
37        self
38    }
39
40    pub fn with_width(mut self, width: u32) -> Self {
41        self.width = width;
42        self
43    }
44
45    pub fn with_height(mut self, height: u32) -> Self {
46        self.height = height;
47        self
48    }
49
50    pub fn with_steps(mut self, steps: u32) -> Self {
51        self.steps = steps;
52        self
53    }
54
55    pub fn with_cfg(mut self, cfg: f64) -> Self {
56        self.cfg = cfg;
57        self
58    }
59
60    pub fn with_sampler(mut self, sampler: impl Into<String>) -> Self {
61        self.sampler = sampler.into();
62        self
63    }
64
65    pub fn with_model(mut self, model: impl Into<String>) -> Self {
66        self.model = model.into();
67        self
68    }
69}
70
71impl RuntimeBackend for SdxlBackend {
72    fn execute(&self, input: &RuntimeInput, output_dir: &Path) -> Result<RuntimeResult> {
73        let compiler_output = &input.vc_output;
74        let metadata = &compiler_output.metadata;
75        let prompt = &compiler_output.prompt;
76
77        let prompt_text = prompt.character.descriptors.join(", ");
78
79        let seed_bytes = metadata.prompt_hash.as_bytes();
80        let seed = u64::from_be_bytes(seed_bytes[0..8].try_into().unwrap_or([0u8; 8]));
81
82        let params = RenderParams {
83            width: self.width,
84            height: self.height,
85            steps: self.steps,
86            cfg: self.cfg,
87            backend_options: serde_json::Value::Object(serde_json::Map::new()),
88        };
89
90        let render_output =
91            self.renderer.render(&prompt_text, &self.negative_prompt, seed, output_dir, &params)?;
92
93        Ok(RuntimeResult {
94            runtime_schema_version: RUNTIME_SCHEMA_VERSION,
95            status: RuntimeStatus::Success,
96            backend: BackendKind::Sdxl,
97            compiler_schema_version: compiler_output.schema_version,
98            seed,
99            image_path: Some(render_output.image_path),
100            image_hash: Some(render_output.image_hash.to_hex()),
101            input_prompt_hash: metadata.prompt_hash.to_hex(),
102            model: Some(self.model.clone()),
103            width: self.width,
104            height: self.height,
105            steps: self.steps,
106            cfg: self.cfg,
107            sampler: self.sampler.clone(),
108            started_at: None,
109            finished_at: None,
110            duration_ms: Some(render_output.duration_ms),
111        })
112    }
113}