From 09c6cb9e3be2655b842e13dd68879cb10cf52acf Mon Sep 17 00:00:00 2001 From: Fox Caminiti Date: Mon, 21 Nov 2022 13:56:37 -0500 Subject: things --- stable_diffusion.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 72 insertions(+), 8 deletions(-) (limited to 'stable_diffusion.cpp') diff --git a/stable_diffusion.cpp b/stable_diffusion.cpp index 2eb230b..dc12bbc 100644 --- a/stable_diffusion.cpp +++ b/stable_diffusion.cpp @@ -38,9 +38,19 @@ SD_JSONToSource(project_data *File, project_state *State, memory *Memory, void * void *RawData = stbi_load_from_memory((stbi_uc *)PNGData, PNGSize, &x, &y, &a, 4); Assert(x == Width && y == Height); Memory_PopScratch(Memory, MaxSize); + int32 Highest = 0; + { + int h = 0, c = 0, i = 0; + while (Block_Loop(Memory, F_Sources, File->Source_Count, &h, &c, &i)) { + block_source *Source = (block_source *)Memory_Block_AddressAtIndex(Memory, F_Sources, i); + if (Source->Type == source_type_principal_temp && Source->RelativeTimestamp > Highest) + Highest = Source->RelativeTimestamp; + } + } int SrcIdx = Source_Generate_Blank(File, State, Memory, Width, Height, 4); block_source *Source = (block_source *)Memory_Block_AddressAtIndex(Memory, F_Sources, SrcIdx); - // Source->Type = source_type_principal_temp; + Source->Type = source_type_principal_temp; + Source->RelativeTimestamp = Highest + 1; void *BitmapAddress = Memory_Block_AddressAtIndex(Memory, F_PrincipalBitmaps, Source->Bitmap_Index, 0); Memory_Copy((uint8 *)BitmapAddress, (uint8 *)RawData, MaxSize); stbi_image_free(RawData); @@ -74,6 +84,8 @@ SD_ParseProgress(project_state *State, char *JSONInfo) // Assert(0); } +static char *pre = "data:image/png;base64,"; + static void JSON_AppendParam_String(char *String, uint64 *i, char *P1, char *P2) { @@ -83,17 +95,22 @@ JSON_AppendParam_String(char *String, uint64 *i, char *P1, char *P2) while(P1[a] != '\0') { String[c++] = P1[a++]; } + String[c++] = '"'; String[c++] = ':'; String[c++] = ' '; + String[c++] = '['; String[c++] = '"'; a = 0; + while(pre[a] != '\0') { + String[c++] = pre[a++]; + } + a = 0; while(P2[a] != '\0') { String[c++] = P2[a++]; - if (a > 64) - break; } c--; String[c++] = '"'; + String[c++] = ']'; String[c++] = ','; String[c++] = '\n'; String[c++] = '\0'; @@ -114,8 +131,9 @@ SD_AssembleJSON(sd_state *SD, char *JSONPayload, void *Base64Bitmap = NULL) JSONPayload[1] = '\n'; JSONPayload[2] = '\0'; uint64 i = 2; - if (SD->Mode) + if (SD->Mode) { JSON_AppendParam_String(JSONPayload, &i, "init_images", (char *)Base64Bitmap); + } for (int i = 0; i < 6; i++) { if (Type[i] == 0) { sprintf(JSONPayload, "%s\"%s\": \"%s\",\n", JSONPayload, Test[i], (char *)Test2[i]); @@ -131,6 +149,7 @@ SD_AssembleJSON(sd_state *SD, char *JSONPayload, void *Base64Bitmap = NULL) sprintf(JSONPayload, "%s\"%s\": %.2f,\n", JSONPayload, "denoising_strength", SD->DenoisingStrength); sprintf(JSONPayload, "%s%s\n", JSONPayload, "\"sampler_index\": \"DDIM\""); sprintf(JSONPayload, "%s}\n", JSONPayload); + printf("%s\n", JSONPayload); // sprintf(CurlCommand, "curl -X POST -H 'Content-Type: application/json' -i '%s/sdapi/v1/txt2img' --data '%s'", SD->ServerAddress, JSONPayload); // printf("%s\n", CurlCommand); }; @@ -143,6 +162,43 @@ struct curl_state curl_data CurlData; }; +static void +Curl_Main() +{ + if (State->CurlActive == -1) { + Curl_GET_Init(&MainHandle, State->Dump1, State->JSONPayload, State->SD.ServerAddress, State->SD.Mode); + Curl_Prog_Init(&ProgHandle, State->Dump2); + State->CurlActive = 1; + } else { + if (Curl_Check(&MainHandle) == 1) { + SD_JSONToSource(File, State, &Memory, State->Dump1, State->SD.Height, State->SD.Width); + Curl_StopAll(State, &ProgHandle, &MainHandle); + } + uint64 Time = ImGui::GetTime(); + if (Time - State->SDTimer > 0.3f) { + int Test = Curl_Check(&ProgHandle); + if (Test == 1) { + SD_ParseProgress(State, (char *)State->Dump2); + curl_multi_remove_handle(ProgHandle.curlm, ProgHandle.curl); + curl_easy_reset(ProgHandle.curl); + ProgHandle.CurlData.size = 0; + curl_easy_setopt(ProgHandle.curl, CURLOPT_URL, "http://127.0.0.1:7860/sdapi/v1/progress"); + curl_easy_setopt(ProgHandle.curl, CURLOPT_WRITEFUNCTION, dumbcurlcallback); + curl_easy_setopt(ProgHandle.curl, CURLOPT_WRITEDATA, (void *)&ProgHandle.CurlData); + curl_multi_add_handle(ProgHandle.curlm, ProgHandle.curl); + } else if (Test == -1) { + PostMsg(State, "Active stable-diffusion-webui instance not found at URL."); + Curl_StopAll(State, &ProgHandle, &MainHandle); + } else if (Test == -2) { + PostMsg(State, "CURL error; see command line."); + Curl_StopAll(State, &ProgHandle, &MainHandle); + } + State->SDTimer = Time; + } + Inc++; + } +} + static void Curl_Free(curl_state *Handle) { @@ -151,7 +207,16 @@ Curl_Free(curl_state *Handle) curl_multi_cleanup(Handle->curlm); } -static bool32 +static void +Curl_StopAll(project_state *State, curl_state *ProgHandle, curl_state *MainHandle) +{ + Curl_Free(ProgHandle); + curl_slist_free_all(ProgHandle->list); + Curl_Free(MainHandle); + State->CurlActive = 0; +} + +static int Curl_Check(curl_state *Handle) { int IsActive; @@ -167,11 +232,10 @@ Curl_Check(curl_state *Handle) if (!msg->data.result) { return 1; } else if (msg->data.result == CURLE_COULDNT_CONNECT) { - // printf("Active stable-diffusion-webui instance not found at URL.\n"); return -1; } else { - // printf("curl error: %s!\n", curl_easy_strerror(msg->data.result)); - return -1; + printf("curl error: %s!\n", curl_easy_strerror(msg->data.result)); + return -2; } } } -- cgit v1.2.3