From 126c3a3510a37e21c7404745144b5872b72aafdd Mon Sep 17 00:00:00 2001
From: Ferenc Szontágh <szf@fsociety.hu>
Date: Mon, 05 Feb 2024 18:52:50 +0000
Subject: [PATCH] deleted external prebuild stuffs, reload model if vae changed - otherwise the vae model not loading

---
 /dev/null             |    0 
 .gitignore            |    3 
 ui/MainWindowUI.h     |    1 
 .vscode/settings.json |  116 ----------------------
 vcpkg.json            |    2 
 ui/MainWindowUI.cpp   |   18 +--
 CMakeLists.txt        |  109 +++++++++++----------
 7 files changed, 72 insertions(+), 177 deletions(-)

diff --git a/.gitignore b/.gitignore
index 734495b..b10ee5e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,4 +20,5 @@
 *.out
 *.app
 vcpkg_installed
-build/
\ No newline at end of file
+build/
+external/stable-diffusion.cpp/**
\ No newline at end of file
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 8ababfd..bb03d39 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -1,116 +1,6 @@
 {
-    "files.associations": {
-        "algorithm": "cpp",
-        "array": "cpp",
-        "atomic": "cpp",
-        "bit": "cpp",
-        "bitset": "cpp",
-        "cctype": "cpp",
-        "charconv": "cpp",
-        "chrono": "cpp",
-        "clocale": "cpp",
-        "cmath": "cpp",
-        "compare": "cpp",
-        "concepts": "cpp",
-        "coroutine": "cpp",
-        "cstddef": "cpp",
-        "cstdint": "cpp",
-        "cstdio": "cpp",
-        "cstdlib": "cpp",
-        "cstring": "cpp",
-        "ctime": "cpp",
-        "cwchar": "cpp",
-        "deque": "cpp",
-        "exception": "cpp",
-        "filesystem": "cpp",
-        "format": "cpp",
-        "forward_list": "cpp",
-        "fstream": "cpp",
-        "functional": "cpp",
-        "hash_map": "cpp",
-        "hash_set": "cpp",
-        "initializer_list": "cpp",
-        "iomanip": "cpp",
-        "ios": "cpp",
-        "iosfwd": "cpp",
-        "iostream": "cpp",
-        "istream": "cpp",
-        "iterator": "cpp",
-        "limits": "cpp",
-        "list": "cpp",
-        "locale": "cpp",
-        "map": "cpp",
-        "memory": "cpp",
-        "new": "cpp",
-        "numeric": "cpp",
-        "optional": "cpp",
-        "ostream": "cpp",
-        "queue": "cpp",
-        "ranges": "cpp",
-        "ratio": "cpp",
-        "source_location": "cpp",
-        "span": "cpp",
-        "sstream": "cpp",
-        "stack": "cpp",
-        "stdexcept": "cpp",
-        "stop_token": "cpp",
-        "streambuf": "cpp",
-        "string": "cpp",
-        "system_error": "cpp",
-        "thread": "cpp",
-        "tuple": "cpp",
-        "type_traits": "cpp",
-        "typeinfo": "cpp",
-        "unordered_map": "cpp",
-        "unordered_set": "cpp",
-        "utility": "cpp",
-        "variant": "cpp",
-        "vector": "cpp",
-        "xfacet": "cpp",
-        "xhash": "cpp",
-        "xiosbase": "cpp",
-        "xlocale": "cpp",
-        "xlocbuf": "cpp",
-        "xlocinfo": "cpp",
-        "xlocmes": "cpp",
-        "xlocmon": "cpp",
-        "xlocnum": "cpp",
-        "xloctime": "cpp",
-        "xmemory": "cpp",
-        "xstring": "cpp",
-        "xtr1common": "cpp",
-        "xtree": "cpp",
-        "xutility": "cpp",
-        "__nullptr": "cpp",
-        "cstdarg": "cpp",
-        "cwctype": "cpp",
-        "mutex": "cpp",
-        "__bit_reference": "cpp",
-        "__hash_table": "cpp",
-        "__split_buffer": "cpp",
-        "__tree": "cpp",
-        "string_view": "cpp",
-        "__string": "cpp",
-        "set": "cpp",
-        "any": "cpp",
-        "cinttypes": "cpp",
-        "codecvt": "cpp",
-        "complex": "cpp",
-        "random": "cpp",
-        "regex": "cpp",
-        "typeindex": "cpp",
-        "valarray": "cpp"
+    "cmake.configureSettings": {
+        "VCPKG_ROOT": "Z:/vcpkg"
     },
-    "debug.terminal.clearBeforeReusing": true,
-    "C_Cpp.loggingLevel": "Information",
-    "remote.WSL.debug": true,
-    "cmake.configureOnOpen": false,
-    "cmake.debugConfig": {
-        "stopAtEntry": false,
-        "console": "newExternalWindow",
-        "logging": {
-            "trace": true
-        }
-    },
-    "breadcrumbs.symbolSortOrder": "name"
+    "cmake.configureOnOpen": true
 }
\ No newline at end of file
diff --git a/.vscode/tasks.json b/.vscode/tasks.json
deleted file mode 100644
index 6532339..0000000
--- a/.vscode/tasks.json
+++ /dev/null
@@ -1,28 +0,0 @@
-{
-    "tasks": [
-        {
-            "type": "cppbuild",
-            "label": "C/C++: cl.exe build active file",
-            "command": "cl.exe",
-            "args": [
-                "/Zi",
-                "/EHsc",
-                "/nologo",
-                "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
-                "${file}"
-            ],
-            "options": {
-                "cwd": "${fileDirname}"
-            },
-            "problemMatcher": [
-                "$msCompile"
-            ],
-            "group": {
-                "kind": "build",
-                "isDefault": true
-            },
-            "detail": "Task generated by Debugger."
-        }
-    ],
-    "version": "2.0.0"
-}
\ No newline at end of file
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4eee7c6..d194a7c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,7 +1,8 @@
 cmake_minimum_required(VERSION 3.10)
 
+set(PROJECT_NAME "sd.ui")
 
-project(sd.ui)
+project(${PROJECT_NAME})
 
 set (CMAKE_CXX_STANDARD 20)
 
@@ -10,71 +11,76 @@
 option(SD_CUBLAS                     "sd: cuda backend" ON)
 option(SD_HIPBLAS                    "sd: rocm backend" OFF)
 option(SD_METAL                      "sd: metal backend" OFF)
-set(SD_CUBLAS ON)
-
-
-    #enable_language(CUDA)
-message("CUDA ON")
-
-
-
 
 IF(MSVC)
     SET(OPTIONS WIN32)
 ENDIF(MSVC)
 
+if(NOT CMAKE_BUILD_TYPE)
+set(CMAKE_BUILD_TYPE Debug)
+endif()
+
+add_executable(${PROJECT_NAME} ${OPTIONS} main.cpp ui/MainWindow.cpp ui/MainWindowSettings.cpp ui/MainWindowUi.cpp ui/MainWindowImageViewer.cpp ui/QueueManager.cpp)
 
 
-add_executable(sd.ui ${OPTIONS} main.cpp ui/MainWindow.cpp ui/MainWindowSettings.cpp ui/MainWindowUi.cpp ui/MainWindowImageViewer.cpp ui/QueueManager.cpp)
-
-
-include_directories(${CMAKE_SOURCE_DIR}/external/stable-diffusion.cpp/include)
-link_directories(${CMAKE_SOURCE_DIR}/external/stable-diffusion.cpp/lib)
+set(STABLE_DIFFUSION_DIR "${CMAKE_SOURCE_DIR}/external/stable-diffusion.cpp")
+include_directories(${STABLE_DIFFUSION_DIR}/include)
+link_directories(${STABLE_DIFFUSION_DIR}/CUDA/${CMAKE_BUILD_TYPE})
 
 
 IF(MSVC)
 
     
-        set(CUDA_PATH "$ENV{CUDA_PATH}")
+     set(CUDA_PATH "$ENV{CUDA_PATH}")
+
+    #set(STABLE_DIFFUSION_LIB ${CMAKE_SOURCE_DIR}/external/stable-diffusion.cpp/${CMAKE_BUILD_TYPE}/stable-diffusion.lib)
+    #set(GGML_LIB ${CMAKE_SOURCE_DIR}/external/ggml/${CMAKE_BUILD_TYPE}/lib/ggml.lib)
+    #set(STABLE_DIFFUSION_DLL ${CMAKE_SOURCE_DIR}/external/stable-diffusion.cpp/${CMAKE_BUILD_TYPE}/stable-diffusion.dll)
+    #set(GGML_DLL ${CMAKE_SOURCE_DIR}/external/ggml/${CMAKE_BUILD_TYPE}/ggml.dll)
+    
+    #add_library(SDLIB SHARED IMPORTED)
+    #set_property(TARGET SDLIB PROPERTY IMPORTED_LOCATION "${STABLE_DIFFUSION_DLL}")
+    #set_property(TARGET SDLIB PROPERTY IMPORTED_IMPLIB "${STABLE_DIFFUSION_LIB}")
+
+    #add_library(GGMLLIB SHARED IMPORTED)
+    #set_property(TARGET GGMLLIB PROPERTY IMPORTED_LOCATION "${GGML_DLL}")
+    #set_property(TARGET GGMLLIB PROPERTY IMPORTED_IMPLIB "${GGML_LIB}")
+        
+    add_library(stable-diffusion SHARED IMPORTED)
+    add_library(ggml SHARED IMPORTED)
+    
+    set_target_properties(stable-diffusion PROPERTIES
+    IMPORTED_CONFIGURATIONS        "DEBUG;RELEASE"
+    IMPORTED_LOCATION_DEBUG        ${STABLE_DIFFUSION_DIR}/CUDA/Debug/stable-diffusion.dll
+    IMPORTED_IMPLIB_DEBUG          ${STABLE_DIFFUSION_DIR}/CUDA/Debug/stable-diffusion.lib
+    IMPORTED_LOCATION_RELEASE      ${STABLE_DIFFUSION_DIR}/CUDA/Release/stable-diffusion.dll
+    IMPORTED_IMPLIB_RELEASE        ${STABLE_DIFFUSION_DIR}/CUDA/Release/stable-diffusion.lib
+    INTERFACE_INCLUDE_DIRECTORIES  ${STABLE_DIFFUSION_DIR}/include
+    )
+
+    set_target_properties(ggml PROPERTIES
+    IMPORTED_CONFIGURATIONS        "DEBUG;RELEASE"
+    IMPORTED_LOCATION_DEBUG        ${STABLE_DIFFUSION_DIR}/CUDA/Debug/ggml.dll
+    IMPORTED_IMPLIB_DEBUG          ${STABLE_DIFFUSION_DIR}/CUDA/Debug/ggml.lib
+    IMPORTED_LOCATION_RELEASE      ${STABLE_DIFFUSION_DIR}/CUDA/Release/ggml.dll
+    IMPORTED_IMPLIB_RELEASE        ${STABLE_DIFFUSION_DIR}/CUDA/Release/ggml.lib
+    )
     
 
-    if(NOT CMAKE_BUILD_TYPE)
-        set(CMAKE_BUILD_TYPE Debug)
-    endif()
-
-    set(STABLE_DIFFUSION_LIB ${CMAKE_SOURCE_DIR}/external/stable-diffusion.cpp/${CMAKE_BUILD_TYPE}/lib/stable-diffusion.lib)
-    set(GGML_LIB ${CMAKE_SOURCE_DIR}/external/ggml/${CMAKE_BUILD_TYPE}/lib/ggml.lib)
-    set(STABLE_DIFFUSION_DLL ${CMAKE_SOURCE_DIR}/external/stable-diffusion.cpp/${CMAKE_BUILD_TYPE}/stable-diffusion.dll)
-    set(GGML_DLL ${CMAKE_SOURCE_DIR}/external/ggml/${CMAKE_BUILD_TYPE}/ggml.dll)
-    
-    add_library(SDLIB SHARED IMPORTED)
-    set_property(TARGET SDLIB PROPERTY IMPORTED_LOCATION "${STABLE_DIFFUSION_DLL}")
-    set_property(TARGET SDLIB PROPERTY IMPORTED_IMPLIB "${STABLE_DIFFUSION_LIB}")
-
-    add_library(GGMLLIB SHARED IMPORTED)
-    set_property(TARGET GGMLLIB PROPERTY IMPORTED_LOCATION "${GGML_DLL}")
-    set_property(TARGET GGMLLIB PROPERTY IMPORTED_IMPLIB "${GGML_LIB}")
-    
-
-    message("STABLE_DIFFUSION_LIB=" ${STABLE_DIFFUSION_LIB})
-    message("GGML_LIB=" ${GGML_LIB})
-    message("CUDA_PATH=" ${CUDA_PATH})
-    
-
-    add_custom_command(TARGET sd.ui POST_BUILD        # Adds a post-build event to MyTest
+    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD        # Adds a post-build event to MyTest
     COMMAND ${CMAKE_COMMAND} -E copy_if_different  # which executes "cmake - E copy_if_different..."
-        "${STABLE_DIFFUSION_DLL}"      # <--this is in-file
-        $<TARGET_FILE_DIR:sd.ui>)                 # <--this is out-file path0
-
-    add_custom_command(TARGET sd.ui POST_BUILD        # Adds a post-build event to MyTest
+        "${STABLE_DIFFUSION_DIR}/CUDA/${CMAKE_BUILD_TYPE}/stable-diffusion.dll"      # <--this is in-file
+        $<TARGET_FILE_DIR:${PROJECT_NAME}>)                 # <--this is out-file path0
+    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD        # Adds a post-build event to MyTest
     COMMAND ${CMAKE_COMMAND} -E copy_if_different  # which executes "cmake - E copy_if_different..."
-        "${GGML_DLL}"      # <--this is in-file
-        $<TARGET_FILE_DIR:sd.ui>)                 # <--this is out-file path0
+        "${STABLE_DIFFUSION_DIR}/CUDA/${CMAKE_BUILD_TYPE}/ggml.dll"      # <--this is in-file
+        $<TARGET_FILE_DIR:${PROJECT_NAME}>)                 # <--this is out-file path0
 
+        set(OpenCV_DIR "${VCPKG_INSTALLED_DIR}/x64-windows/share/opencv4")
 
 ENDIF(MSVC)
 
-set(OpenCV_DIR "${VCPKG_INSTALLED_DIR}/x64-windows/share/opencv4")
+#set(OpenCV_DIR "${VCPKG_INSTALLED_DIR}/x64-windows/share/opencv4")
 
 find_package(OpenCV REQUIRED)
 
@@ -95,16 +101,15 @@
 find_package(nlohmann_json CONFIG REQUIRED)
 find_package(CUDAToolkit REQUIRED)
 
-set(CUDA_LIB_PATH "C:/CUDA/lib/x64")
-message("CUDA_LIB_PATH=" ${CUDA_LIB_PATH})
+#set(CUDA_LIB_PATH "C:/CUDA/lib/x64")
 
 
-target_compile_definitions(sd.ui PRIVATE ${wxWidgets_DEFINITIONS} "$<$<CONFIG:DEBUG>:${wxWidgets_DEFINITIONS_DEBUG}>")
-target_include_directories(sd.ui PRIVATE ${wxWidgets_INCLUDE_DIRS})
+target_compile_definitions(${PROJECT_NAME} PRIVATE ${wxWidgets_DEFINITIONS} "$<$<CONFIG:DEBUG>:${wxWidgets_DEFINITIONS_DEBUG}>")
+target_include_directories(${PROJECT_NAME} PRIVATE ${wxWidgets_INCLUDE_DIRS})
 
-target_link_libraries(sd.ui ${CUDA_LIBRARIES})
+target_link_libraries(${PROJECT_NAME} ${CUDA_LIBRARIES})
 
-target_link_libraries(sd.ui PRIVATE restclient-cpp ${OpenCV_LIBS} nlohmann_json::nlohmann_json fmt::fmt wx::core wx::base wx::xrc wx::adv wx::richtext wx::aui SDLIB GGMLLIB CUDA::cudart CUDA::cublas CUDA::cublasLt CUDA::cuda_driver)
+target_link_libraries(${PROJECT_NAME} PRIVATE stable-diffusion ggml restclient-cpp opencv_ml opencv_dnn opencv_core opencv_flann nlohmann_json::nlohmann_json fmt::fmt wx::core wx::base wx::xrc wx::adv wx::richtext wx::aui CUDA::cudart CUDA::cublas CUDA::cublasLt CUDA::cuda_driver)
 
 include_directories(ui)
 
diff --git a/CMakePresets.json b/CMakePresets.json
deleted file mode 100644
index 7aa3150..0000000
--- a/CMakePresets.json
+++ /dev/null
@@ -1,12 +0,0 @@
-{
-    "version": 3,
-    "configurePresets": [
-        {
-            "name": "default",
-            "binaryDir": "${sourceDir}/build",
-            "cacheVariables": {
-                "CMAKE_TOOLCHAIN_FILE": "Z:/vcpkg/scripts/buildsystems/vcpkg.cmake"
-            }
-        }
-    ]
-}
\ No newline at end of file
diff --git a/CMakeUserPresets.json b/CMakeUserPresets.json
deleted file mode 100644
index 35c3e17..0000000
--- a/CMakeUserPresets.json
+++ /dev/null
@@ -1,12 +0,0 @@
-{
-    "version": 3,
-    "configurePresets": [
-        {
-            "name": "default",
-            "inherits": "vcpkg",
-            "environment": {
-                "VCPKG_ROOT": "Z:/vcpkg"
-            }
-        }
-    ]
-}
\ No newline at end of file
diff --git a/external/ggml/Debug/ggml.pdb b/external/ggml/Debug/ggml.pdb
deleted file mode 100644
index be168ac..0000000
--- a/external/ggml/Debug/ggml.pdb
+++ /dev/null
Binary files differ
diff --git a/external/ggml/Debug/lib/ggml.exp b/external/ggml/Debug/lib/ggml.exp
deleted file mode 100644
index bf5237e..0000000
--- a/external/ggml/Debug/lib/ggml.exp
+++ /dev/null
Binary files differ
diff --git a/external/ggml/Debug/lib/ggml.pdb b/external/ggml/Debug/lib/ggml.pdb
deleted file mode 100644
index 74f310a..0000000
--- a/external/ggml/Debug/lib/ggml.pdb
+++ /dev/null
Binary files differ
diff --git a/external/ggml/Release/lib/ggml.exp b/external/ggml/Release/lib/ggml.exp
deleted file mode 100644
index 6f67260..0000000
--- a/external/ggml/Release/lib/ggml.exp
+++ /dev/null
Binary files differ
diff --git a/external/stable-diffusion.cpp/Debug/lib/stable-diffusion.pdb b/external/stable-diffusion.cpp/Debug/lib/stable-diffusion.pdb
deleted file mode 100644
index b0810af..0000000
--- a/external/stable-diffusion.cpp/Debug/lib/stable-diffusion.pdb
+++ /dev/null
Binary files differ
diff --git a/external/stable-diffusion.cpp/Debug/stable-diffusion.pdb b/external/stable-diffusion.cpp/Debug/stable-diffusion.pdb
deleted file mode 100644
index 464d276..0000000
--- a/external/stable-diffusion.cpp/Debug/stable-diffusion.pdb
+++ /dev/null
Binary files differ
diff --git a/external/stable-diffusion.cpp/Release/lib/stable-diffusion.exp b/external/stable-diffusion.cpp/Release/lib/stable-diffusion.exp
deleted file mode 100644
index b62ccec..0000000
--- a/external/stable-diffusion.cpp/Release/lib/stable-diffusion.exp
+++ /dev/null
Binary files differ
diff --git a/ui/MainWindowUI.cpp b/ui/MainWindowUI.cpp
index 46ec1c6..abffbab 100644
--- a/ui/MainWindowUI.cpp
+++ b/ui/MainWindowUI.cpp
@@ -449,14 +449,10 @@
 
 MainWindowUI::~MainWindowUI()
 {
-    // this->Hide();
-    /* for (int i = 0; i < this->threads.size(); i++)
-       {
-           if (this->threads.at(i).joinable())
-           {
-               this->threads.at(i).join();
-           }
-       }*/
+    if (this->modelLoaded)
+    {
+        free_sd_ctx(this->sd_ctx);
+    }
     for (auto &t : this->threads)
     {
         t->join();
@@ -488,10 +484,11 @@
     {
         this->sd_ctx = this->LoadModelv2(eventHandler, myItem);
         this->currentModel = myItem.params.model_path;
+        this->currentVaeModel = myItem.params.vae_path;
     }
     else
     {
-        if (myItem.params.model_path != this->currentModel)
+        if (myItem.params.model_path != this->currentModel || this->currentVaeModel != myItem.params.vae_path)
         {
             free_sd_ctx(this->sd_ctx);
             this->sd_ctx = this->LoadModelv2(eventHandler, myItem);
@@ -995,7 +992,7 @@
         myItem.params.controlnet_path.c_str(),
         myItem.params.lora_model_dir.c_str(),
         myItem.params.embeddings_path.c_str(),
-        true, false, false,
+        false, myItem.params.vae_tiling, false,
         myItem.params.n_threads,
         myItem.params.wtype,
         myItem.params.rng_type,
@@ -1017,6 +1014,7 @@
         wxQueueEvent(eventHandler, c);
         this->modelLoaded = true;
         this->currentModel = myItem.params.model_path;
+        this->currentVaeModel = myItem.params.vae_path;
     }
     return sd_ctx_;
 }
diff --git a/ui/MainWindowUI.h b/ui/MainWindowUI.h
index 26ec976..a8657e1 100644
--- a/ui/MainWindowUI.h
+++ b/ui/MainWindowUI.h
@@ -78,6 +78,7 @@
 	QM::QueueManager *qmanager;
 	bool modelLoaded = false;
 	std::string currentModel;
+	std::string currentVaeModel;
 	sd_ctx_t *sd_ctx;
 	std::streambuf *buffer;
 	std::vector<std::thread*> threads;
diff --git a/vcpkg.json b/vcpkg.json
index 9ddbef9..01eda36 100644
--- a/vcpkg.json
+++ b/vcpkg.json
@@ -4,7 +4,7 @@
     "wxwidgets",
     "cuda",
     "nlohmann-json",
-    "opencv4",
+    "opencv",
     "restclient-cpp"
   ]
 }
\ No newline at end of file

--
Gitblit v1.9.3