Commit e836240 for stable-diffusion.cpp

commit e836240949b25363f209daccf137f6327ebe454f
Author: stduhpf <stephduh@live.fr>
Date:   Fri Dec 4 16:46:55 2026 +0100

    fix(cli): register preview callback in DGL mode (#2503)

diff --git a/examples/cli/README.md b/examples/cli/README.md
index 2881c6b..70d0c71 100644
--- a/examples/cli/README.md
+++ b/examples/cli/README.md
@@ -49,6 +49,17 @@ before execution at debug level (`--log-level debug`).

 Run a [DGL program](../../docs/dgl.md) with `sd-cli -M dgl --script program.dgl`.
 DGL mode accepts the same logging options and sends logs to stderr.
+
+DGL previews are off by default. Use `--preview on` (or `true`, `yes`, or
+`1`) to enable them, and `--preview off` (or `none`, `false`, `no`, or `0`)
+to disable them. Use `--preview-path`, `--preview-interval`, and
+`--preview-noisy` to configure the output.
+
+The script selects the preview method. Sampling uses projection by default,
+and `preview(decoder = ...)` uses the supplied decoder. Passing `proj`, `tae`,
+or `vae` to `--preview` enables previews and prints a warning that the script
+selects the method.
+
 See [Automatic DGL model downloads](#automatic-dgl-model-downloads) to fetch
 missing model files.

diff --git a/examples/cli/dgl.cpp b/examples/cli/dgl.cpp
index 7995802..276e8d4 100644
--- a/examples/cli/dgl.cpp
+++ b/examples/cli/dgl.cpp
@@ -241,7 +241,7 @@ public:
     }
 };

-int DGLModeParams::run(const SDContextParams& options, bool embed_metadata) const {
+int DGLModeParams::run(const SDContextParams& options, bool embed_metadata, const DGLPreviewOptions& preview) const {
     try {
         if (script_path.empty()) {
             const auto* catalog = sd_dgl_builtin_catalog();
@@ -295,6 +295,11 @@ int DGLModeParams::run(const SDContextParams& options, bool embed_metadata) cons
         check_dgl(raw_context != nullptr);
         std::unique_ptr<sd_dgl_ctx_t, decltype(&sd_dgl_ctx_free)> context(raw_context, sd_dgl_ctx_free);
         check_dgl(options.configure_dgl(context.get()));
+        if (preview.callback) {
+            // Decoder-based mode so the script's preview(decoder = ...) argument, not the host,
+            // selects between projection and decoding; tae and vae behave identically with a decoder.
+            check_dgl(sd_dgl_ctx_set_preview_callback(context.get(), preview.callback, "tae", preview.interval, preview.denoised, preview.noisy, preview.data) == SD_STATUS_OK);
+        }
         check_dgl(sd_dgl_ctx_set(context.get(), "embed_metadata", embed_metadata ? "on" : "off"));
         if (!max_ram.empty())
             check_dgl(sd_dgl_ctx_set(context.get(), "max_ram", max_ram.c_str()));
diff --git a/examples/cli/dgl.h b/examples/cli/dgl.h
index ece25ea..db0503b 100644
--- a/examples/cli/dgl.h
+++ b/examples/cli/dgl.h
@@ -9,6 +9,8 @@

 #include "common/dgl_model_sources.h"

+#include "stable-diffusion.h"
+
 struct ArgOptions;
 struct SDContextParams;

@@ -16,6 +18,14 @@ using DGLScriptArgs = std::map<std::string, std::string, std::less<>>;

 void apply_dgl_args(std::string& source, const DGLScriptArgs& args);

+struct DGLPreviewOptions {
+    sd_dgl_preview_cb_t callback = nullptr;
+    int interval                 = 0;
+    bool denoised                = false;
+    bool noisy                   = false;
+    void* data                   = nullptr;
+};
+
 struct DGLModeParams {
     std::string script_path;
     std::string write_builtins_path;
@@ -31,7 +41,7 @@ struct DGLModeParams {
     ArgOptions get_options();
     bool has_options() const;
     bool validate() const;
-    int run(const SDContextParams& options, bool embed_metadata) const;
+    int run(const SDContextParams& options, bool embed_metadata, const DGLPreviewOptions& preview) const;
 };

 #endif  // __EXAMPLES_CLI_DGL_H__
diff --git a/examples/cli/main.cpp b/examples/cli/main.cpp
index 2e0be18..2facf91 100644
--- a/examples/cli/main.cpp
+++ b/examples/cli/main.cpp
@@ -7,6 +7,7 @@
 #include <filesystem>
 #include <functional>
 #include <iostream>
+#include <iterator>
 #include <map>
 #include <random>
 #include <regex>
@@ -158,10 +159,26 @@ struct SDCliParams {
         };

         auto on_preview_arg = [&](int argc, const char** argv, int index) {
-            if (++index >= argc) {
+            if (++index >= argc || argv[index][0] == '-') {
+                LOG_ERROR("--preview requires a value");
                 return -1;
             }
-            preview_method = argv[index];
+            std::string value      = argv[index];
+            const auto preview_end = std::end(previews_str);
+            if (std::find(std::begin(previews_str), preview_end, value) == preview_end) {
+                std::transform(value.begin(), value.end(), value.begin(), [](unsigned char c) {
+                    return static_cast<char>(std::tolower(c));
+                });
+                if (value == "on" || value == "true" || value == "yes" || value == "1") {
+                    value = "on";
+                } else if (value == "off" || value == "false" || value == "no" || value == "0") {
+                    value = "off";
+                } else {
+                    LOG_ERROR("invalid value '%s' for --preview", argv[index]);
+                    return -1;
+                }
+            }
+            preview_method = value;
             return 1;
         };

@@ -186,7 +203,7 @@ struct SDCliParams {
              on_mode_arg},
             {"",
              "--preview",
-             std::string("preview method. must be one of the following [") + previews_str[0] + ", " + previews_str[1] + ", " + previews_str[2] + ", " + previews_str[3] + "] (default is " + previews_str[0] + ")",
+             std::string("preview method, one of [") + previews_str[0] + ", " + previews_str[1] + ", " + previews_str[2] + ", " + previews_str[3] + "] (default " + previews_str[0] + "). In DGL mode, use on to enable or off to disable.",
              on_preview_arg},
             {"-h",
              "--help",
@@ -367,9 +384,11 @@ void step_callback(int step, int frame_count, const sd_tensor_t* const* tensors,
             LOG_ERROR("save preview image to '%s' failed", path.u8string().c_str());
         }
     } else {
-        const int fps  = cli_params->preview_method == "proj" ? cli_params->preview_fps / 4 : cli_params->preview_fps;
-        auto path      = fs::u8path(cli_params->preview_path.empty() ? "preview.avi" : cli_params->preview_path);
-        auto extension = path.extension().u8string();
+        // DGL preview methods are selected in the script, so the --preview proj slowdown does not apply.
+        const bool proj_slowdown = cli_params->mode != DGL && cli_params->preview_method == "proj";
+        const int fps            = proj_slowdown ? cli_params->preview_fps / 4 : cli_params->preview_fps;
+        auto path                = fs::u8path(cli_params->preview_path.empty() ? "preview.avi" : cli_params->preview_path);
+        auto extension           = path.extension().u8string();
         std::transform(extension.begin(), extension.end(), extension.begin(), [](unsigned char c) { return char(std::tolower(c)); });
         if (extension != ".avi" && extension != ".webm" && extension != ".webp" && extension != ".mp4")
             path.replace_extension(".avi");
@@ -379,6 +398,11 @@ void step_callback(int step, int frame_count, const sd_tensor_t* const* tensors,
     }
 }

+void dgl_step_callback(const sd_preview_info_t* info, int step, int frame_count, const sd_tensor_t* const* frames, bool is_noisy, void* data) {
+    (void)info;
+    step_callback(step, frame_count, frames, is_noisy, data);
+}
+
 static fs::path get_video_audio_sidecar_path(const SDCliParams& cli_params) {
     fs::path out_path     = fs::u8path(cli_params.output_path);
     fs::path base_path    = out_path;
@@ -551,7 +575,18 @@ int main(int argc, const char* argv[]) {
     parse_args(argc, argv, cli_params, ctx_params, gen_params);

     if (cli_params.mode == DGL) {
-        return cli_params.dgl.run(ctx_params, gen_params.embed_image_metadata);
+        DGLPreviewOptions preview;
+        if (cli_params.preview_method != previews_str[0] && cli_params.preview_method != "off") {
+            if (cli_params.preview_method != "on") {
+                LOG_WARN("DGL previews use the method selected by the script. Use --preview on to enable previews without this warning.");
+            }
+            preview.callback = dgl_step_callback;
+            preview.interval = cli_params.preview_interval;
+            preview.denoised = !cli_params.preview_noisy;
+            preview.noisy    = cli_params.preview_noisy;
+            preview.data     = &cli_params;
+        }
+        return cli_params.dgl.run(ctx_params, gen_params.embed_image_metadata, preview);
     }

     if (cli_params.mode == METADATA) {