inpaint_node.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. declare let Krom_texsynth: any;
  2. type inpaint_node_t = {
  3. base?: logic_node_t;
  4. };
  5. let inpaint_node_image: image_t = null;
  6. let inpaint_node_mask: image_t = null;
  7. let inpaint_node_result: image_t = null;
  8. let inpaint_node_temp: image_t = null;
  9. let inpaint_node_prompt = "";
  10. let inpaint_node_strength = 0.5;
  11. let inpaint_node_auto = true;
  12. function inpaint_node_create(arg: any): inpaint_node_t {
  13. let n: inpaint_node_t = {};
  14. n.base = logic_node_create();
  15. n.base.get_as_image = inpaint_node_get_as_image;
  16. n.base.get_cached_image = inpaint_node_get_cached_image;
  17. inpaint_node_init();
  18. return n;
  19. }
  20. function inpaint_node_init() {
  21. if (inpaint_node_image == null) {
  22. inpaint_node_image = image_create_render_target(config_get_texture_res_x(), config_get_texture_res_y());
  23. }
  24. if (inpaint_node_mask == null) {
  25. inpaint_node_mask = image_create_render_target(config_get_texture_res_x(), config_get_texture_res_y(), tex_format_t.R8);
  26. app_notify_on_next_frame(function () {
  27. g4_begin(inpaint_node_mask);
  28. g4_clear(color_from_floats(1.0, 1.0, 1.0, 1.0));
  29. g4_end();
  30. });
  31. }
  32. if (inpaint_node_temp == null) {
  33. inpaint_node_temp = image_create_render_target(512, 512);
  34. }
  35. if (inpaint_node_result == null) {
  36. inpaint_node_result = image_create_render_target(config_get_texture_res_x(), config_get_texture_res_y());
  37. }
  38. }
  39. function inpaint_node_buttons(ui: zui_t, nodes: zui_nodes_t, node: zui_node_t) {
  40. inpaint_node_auto = node.buttons[0].default_value == 0 ? false : true;
  41. if (!inpaint_node_auto) {
  42. let inpaint_node_strength_handle: zui_handle_t = zui_handle(__ID__);
  43. if (inpaint_node_strength_handle.init) {
  44. inpaint_node_strength_handle.value = inpaint_node_strength;
  45. }
  46. inpaint_node_strength = zui_slider(inpaint_node_strength_handle, tr("strength"), 0, 1, true);
  47. inpaint_node_prompt = zui_text_area(zui_handle(__ID__), zui_align_t.LEFT, true, tr("prompt"), true);
  48. node.buttons[1].height = 1 + string_split(inpaint_node_prompt, "\n").length;
  49. }
  50. else {
  51. node.buttons[1].height = 0;
  52. }
  53. }
  54. function inpaint_node_get_as_image(self: inpaint_node_t, from: i32): image_t {
  55. let source: image_t = logic_node_input_get_as_image(self.base.inputs[0]);
  56. console_progress(tr("Processing") + " - " + tr("Inpaint"));
  57. krom_g4_swap_buffers();
  58. g2_begin(inpaint_node_image);
  59. g2_draw_scaled_image(source, 0, 0, config_get_texture_res_x(), config_get_texture_res_y());
  60. g2_end();
  61. return inpaint_node_auto ? inpaint_node_texsynth_inpaint(inpaint_node_image, false, inpaint_node_mask) : inpaint_node_sd_inpaint(inpaint_node_image, inpaint_node_mask);
  62. }
  63. function inpaint_node_get_cached_image(self: inpaint_node_t): image_t {
  64. app_notify_on_next_frame(function (self: inpaint_node_t) {
  65. let source: image_t = logic_node_input_get_as_image(self.base.inputs[0]);
  66. if (base_pipe_copy == null) {
  67. base_make_pipe();
  68. }
  69. if (const_data_screen_aligned_vb == null) {
  70. const_data_create_screen_aligned_data();
  71. }
  72. g4_begin(inpaint_node_image);
  73. g4_set_pipeline(base_pipe_inpaint_preview);
  74. g4_set_tex(base_tex0_inpaint_preview, source);
  75. g4_set_tex(base_texa_inpaint_preview, inpaint_node_mask);
  76. g4_set_vertex_buffer(const_data_screen_aligned_vb);
  77. g4_set_index_buffer(const_data_screen_aligned_ib);
  78. g4_draw();
  79. g4_end();
  80. }, self);
  81. return inpaint_node_image;
  82. }
  83. function inpaint_node_get_target(): image_t {
  84. return inpaint_node_mask;
  85. }
  86. function inpaint_node_texsynth_inpaint(image: image_t, tiling: bool, mask: image_t): image_t {
  87. let w = config_get_texture_res_x();
  88. let h = config_get_texture_res_y();
  89. let bytes_img = image_get_pixels(image);
  90. let bytes_mask = mask != null ? image_get_pixels(mask) : buffer_create(w * h);
  91. let bytes_out = buffer_create(w * h * 4);
  92. Krom_texsynth.inpaint(w, h, bytes_out, bytes_img, bytes_mask, tiling);
  93. inpaint_node_result = image_from_bytes(bytes_out, w, h);
  94. return inpaint_node_result;
  95. }
  96. function inpaint_node_sd_inpaint(image: image_t, mask: image_t): image_t {
  97. inpaint_node_init();
  98. let bytes_img = image_get_pixels(mask);
  99. let u8 = u8_array_create_from_buffer(bytes_img);
  100. let f32mask = f32_array_create(4 * 64 * 64);
  101. let vae_encoder_blob: buffer_t = data_get_blob("models/sd_vae_encoder.quant.onnx");
  102. // for (let x: i32 = 0; x < math_floor(image.width / 512); ++x) {
  103. // for (let y: i32 = 0; y < math_floor(image.height / 512); ++y) {
  104. let x = 0;
  105. let y = 0;
  106. for (let xx: i32 = 0; xx < 64; ++xx) {
  107. for (let yy: i32 = 0; yy < 64; ++yy) {
  108. // let step = math_floor(512 / 64);
  109. // let j = (yy * step * mask.width + xx * step) + (y * 512 * mask.width + x * 512);
  110. let step = math_floor(mask.width / 64);
  111. let j = (yy * step * mask.width + xx * step);
  112. let f = u8[j] / 255.0;
  113. let i = yy * 64 + xx;
  114. f32mask[i ] = f;
  115. f32mask[i + 64 * 64 ] = f;
  116. f32mask[i + 64 * 64 * 2] = f;
  117. f32mask[i + 64 * 64 * 3] = f;
  118. }
  119. }
  120. g2_begin(inpaint_node_temp);
  121. // g2_drawImage(image, -x * 512, -y * 512);
  122. g2_draw_scaled_image(image, 0, 0, 512, 512);
  123. g2_end();
  124. bytes_img = image_get_pixels(inpaint_node_temp);
  125. let u8a = u8_array_create_from_buffer(bytes_img);
  126. let f32a = f32_array_create(3 * 512 * 512);
  127. for (let i: i32 = 0; i < (512 * 512); ++i) {
  128. f32a[i ] = (u8a[i * 4 ] / 255.0) * 2.0 - 1.0;
  129. f32a[i + 512 * 512 ] = (u8a[i * 4 + 1] / 255.0) * 2.0 - 1.0;
  130. f32a[i + 512 * 512 * 2] = (u8a[i * 4 + 2] / 255.0) * 2.0 - 1.0;
  131. }
  132. let latents_buf: buffer_t = krom_ml_inference(vae_encoder_blob, [f32a.buffer], [[1, 3, 512, 512]], [1, 4, 64, 64], config_raw.gpu_inference);
  133. let latents: f32_array_t = f32_array_create_from_buffer(latents_buf);
  134. for (let i: i32 = 0; i < latents.length; ++i) {
  135. latents[i] = 0.18215 * latents[i];
  136. }
  137. // let latents_orig: f32_array_t = array_slice(latents, 0, latents.length);
  138. let latents_orig: f32_array_t = latents.slice(0, latents.length);
  139. let noise = f32_array_create(latents.length);
  140. for (let i: i32 = 0; i < noise.length; ++i) {
  141. noise[i] = math_cos(2.0 * 3.14 * random_node_get_float()) * math_sqrt(-2.0 * math_log(random_node_get_float()));
  142. }
  143. let num_inference_steps = 50;
  144. let init_timestep = math_floor(num_inference_steps * inpaint_node_strength);
  145. let timestep = text_to_photo_node_timesteps[num_inference_steps - init_timestep];
  146. let alphas_cumprod = text_to_photo_node_alphas_cumprod;
  147. let sqrt_alpha_prod = math_pow(alphas_cumprod[timestep], 0.5);
  148. let sqrt_one_minus_alpha_prod = math_pow(1.0 - alphas_cumprod[timestep], 0.5);
  149. for (let i: i32 = 0; i < latents.length; ++i) {
  150. latents[i] = sqrt_alpha_prod * latents[i] + sqrt_one_minus_alpha_prod * noise[i];
  151. }
  152. let start = num_inference_steps - init_timestep;
  153. inpaint_node_result = text_to_photo_node_stable_diffusion(inpaint_node_prompt, latents, start, true, f32mask, latents_orig);
  154. return inpaint_node_result;
  155. // }
  156. // }
  157. }
  158. let inpaint_node_def: zui_node_t = {
  159. id: 0,
  160. name: _tr("Inpaint"),
  161. type: "inpaint_node",
  162. x: 0,
  163. y: 0,
  164. color: 0xff4982a0,
  165. inputs: [
  166. {
  167. id: 0,
  168. node_id: 0,
  169. name: _tr("Color"),
  170. type: "RGBA",
  171. color: 0xffc7c729,
  172. default_value: f32_array_create_xyzw(1.0, 1.0, 1.0, 1.0),
  173. min: 0.0,
  174. max: 1.0,
  175. precision: 100,
  176. display: 0
  177. }
  178. ],
  179. outputs: [
  180. {
  181. id: 0,
  182. node_id: 0,
  183. name: _tr("Color"),
  184. type: "RGBA",
  185. color: 0xffc7c729,
  186. default_value: f32_array_create_xyzw(0.0, 0.0, 0.0, 1.0),
  187. min: 0.0,
  188. max: 1.0,
  189. precision: 100,
  190. display: 0
  191. }
  192. ],
  193. buttons: [
  194. {
  195. name: _tr("auto"),
  196. type: "BOOL",
  197. output: 0,
  198. default_value: f32_array_create_x(1),
  199. data: null,
  200. min: 0.0,
  201. max: 1.0,
  202. precision: 100,
  203. height: 0
  204. },
  205. {
  206. name: "inpaint_node_buttons",
  207. type: "CUSTOM",
  208. output: -1,
  209. default_value: null,
  210. data: null,
  211. min: 0.0,
  212. max: 1.0,
  213. precision: 100,
  214. height: 0
  215. }
  216. ],
  217. width: 0
  218. };