From fed0ed46e993fe9d479ebf9b5605b9be36082aba Mon Sep 17 00:00:00 2001 From: vmobilis <75476228+vmobilis@users.noreply.github.com> Date: Sat, 1 Mar 2025 01:09:01 +0300 Subject: [PATCH 1/2] Move mask creation closer to img2img Or else it can be filled with zeroes instead of 0xFF, with -O3 optimization. --- examples/cli/main.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/examples/cli/main.cpp b/examples/cli/main.cpp index cf8f5b13..698ddaac 100644 --- a/examples/cli/main.cpp +++ b/examples/cli/main.cpp @@ -931,18 +931,6 @@ int main(int argc, const char* argv[]) { } } - if (params.mask_path != "") { - int c = 0; - mask_image_buffer = stbi_load(params.mask_path.c_str(), ¶ms.width, ¶ms.height, &c, 1); - } else { - std::vector arr(params.width * params.height, 255); - mask_image_buffer = arr.data(); - } - sd_image_t mask_image = {(uint32_t)params.width, - (uint32_t)params.height, - 1, - mask_image_buffer}; - sd_image_t* results; if (params.mode == TXT2IMG) { results = txt2img(sd_ctx, @@ -1011,6 +999,19 @@ int main(int argc, const char* argv[]) { free_sd_ctx(sd_ctx); return 0; } else { + if (params.mask_path != "") { + int c = 0; + mask_image_buffer = stbi_load(params.mask_path.c_str(), ¶ms.width, ¶ms.height, &c, 1); + } else { + std::vector arr(params.width * params.height, 255); + for (uint8_t dummy_arr : arr) if (dummy_arr) break; // dummy cycle to avoid -O3 optimization + mask_image_buffer = arr.data(); + } + sd_image_t mask_image = {(uint32_t)params.width, + (uint32_t)params.height, + 1, + mask_image_buffer}; + LOG_DEBUG("img2img: created mask with 0x%x fill", *mask_image.data); results = img2img(sd_ctx, input_image, mask_image, From bbe73b5052dc4be8ead893521a3a33894813929c Mon Sep 17 00:00:00 2001 From: vmobilis <75476228+vmobilis@users.noreply.github.com> Date: Sat, 1 Mar 2025 11:20:45 +0300 Subject: [PATCH 2/2] Use LOG_DEBUG() to reference arr[] --- examples/cli/main.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/cli/main.cpp b/examples/cli/main.cpp index 698ddaac..970f0aef 100644 --- a/examples/cli/main.cpp +++ b/examples/cli/main.cpp @@ -22,6 +22,8 @@ #define STB_IMAGE_RESIZE_STATIC #include "stb_image_resize.h" +#include "util.h" + const char* rng_type_to_str[] = { "std_default", "cuda", @@ -825,7 +827,6 @@ int main(int argc, const char* argv[]) { bool vae_decode_only = true; uint8_t* input_image_buffer = NULL; uint8_t* control_image_buffer = NULL; - uint8_t* mask_image_buffer = NULL; if (params.mode == IMG2IMG || params.mode == IMG2VID) { vae_decode_only = false; @@ -999,15 +1000,18 @@ int main(int argc, const char* argv[]) { free_sd_ctx(sd_ctx); return 0; } else { + uint8_t* mask_image_buffer = NULL; if (params.mask_path != "") { int c = 0; mask_image_buffer = stbi_load(params.mask_path.c_str(), ¶ms.width, ¶ms.height, &c, 1); } else { std::vector arr(params.width * params.height, 255); - for (uint8_t dummy_arr : arr) if (dummy_arr) break; // dummy cycle to avoid -O3 optimization + // do not remove this LOG_DEBUG line, or -O3 may destroy arr[] + // before assigning to mask_image_buffer + LOG_DEBUG("img2img: created array with 0x%x fill", arr[0]); mask_image_buffer = arr.data(); } - sd_image_t mask_image = {(uint32_t)params.width, + const sd_image_t mask_image = {(uint32_t)params.width, (uint32_t)params.height, 1, mask_image_buffer};