Skip to content

Commit

Permalink
lab fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
luboslenco committed Oct 21, 2024
1 parent 0a5671a commit eb9160d
Show file tree
Hide file tree
Showing 40 changed files with 154 additions and 119 deletions.
Binary file modified armorlab/assets/Scene.arm
Binary file not shown.
1 change: 1 addition & 0 deletions armorlab/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ project.add_assets("../armorpaint/assets/plugins/hello_node_brush.js", { destina
project.add_assets("../armorpaint/assets/plugins/import_svg.js", { destination: "data/plugins/{name}" });
project.add_assets("assets/meshes/*", { destination: "data/meshes/{name}", noembed: true });
project.add_assets("assets/models/*.onnx", { destination: "data/models/{name}" });
project.add_assets("assets/models/*.json", { destination: "data/models/{name}" });
project.add_assets("assets/models/LICENSE.txt", { destination: "data/models/LICENSE.txt" });
project.add_assets("assets/readme/readme.txt", { destination: "{name}" });

Expand Down
3 changes: 2 additions & 1 deletion armorlab/sources/nodes/brush_output_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ let brush_output_node_inst: brush_output_node_t = null;

function brush_output_node_create(raw: ui_node_t, args: f32_array_t): brush_output_node_t {
let n: brush_output_node_t = {};
n.base = logic_node_create();
n.base = logic_node_create(n);
n.base.get_as_image = brush_output_node_get_as_image;

if (brush_output_node_inst == null) {
{
Expand Down
11 changes: 7 additions & 4 deletions armorlab/sources/nodes/image_texture_node.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@

type image_texture_node_t = {
base?: logic_node_t;
file?: string;
color_space?: string;
raw?: ui_node_t;
};

function image_texture_node_create(raw: ui_node_t, args: f32_array_t): image_texture_node_t {
let n: image_texture_node_t = {};
n.base = logic_node_create();
n.base = logic_node_create(n);
n.base.get_as_image = image_texture_node_get_as_image;
n.base.get_cached_image = image_texture_node_get_cached_image;
n.raw = raw;
return n;
}

function image_texture_node_get_as_image(self: image_texture_node_t, from: i32): image_t {
let index: i32 = array_index_of(project_asset_names, self.file);
if (project_assets.length == 0) {
return null;
}
let index: i32 = self.raw.buttons[0].default_value[0];
let asset: asset_t = project_assets[index];
return project_get_image(asset);
}
Expand Down
12 changes: 7 additions & 5 deletions armorlab/sources/nodes/inpaint_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let inpaint_node_auto: bool = true;

function inpaint_node_create(raw: ui_node_t, args: f32_array_t): inpaint_node_t {
let n: inpaint_node_t = {};
n.base = logic_node_create();
n.base = logic_node_create(n);
n.base.get_as_image = inpaint_node_get_as_image;
n.base.get_cached_image = inpaint_node_get_cached_image;

Expand Down Expand Up @@ -48,7 +48,9 @@ function inpaint_node_init() {
}
}

function inpaint_node_buttons(ui: ui_t, nodes: ui_nodes_t, node: ui_node_t) {
function inpaint_node_button(node_id: i32) {
let node: ui_node_t = ui_get_node(ui_nodes_get_canvas(true).nodes, node_id);

inpaint_node_auto = node.buttons[0].default_value == 0 ? false : true;
if (!inpaint_node_auto) {

Expand Down Expand Up @@ -163,7 +165,7 @@ function inpaint_node_sd_inpaint(image: image_t, mask: image_t): image_t {
f32a[i + 512 * 512 * 2] = (u8a[i * 4 + 2] / 255.0) * 2.0 - 1.0;
}

let tensors: buffer_t[] = [f32a.buffer];
let tensors: buffer_t[] = [buffer_create_from_raw(f32a.buffer, f32a.length * 4)];
let input_shape: i32_array_t[] = [];
let input_shape0: i32[] = [1, 3, 512, 512];
array_push(input_shape, input_shape0);
Expand Down Expand Up @@ -246,10 +248,10 @@ let inpaint_node_def: ui_node_t = {
height: 0
},
{
name: "inpaint_node_buttons",
name: "inpaint_node_button",
type: "CUSTOM",
output: -1,
default_value: null,
default_value: f32_array_create_x(0),
data: null,
min: 0.0,
max: 1.0,
Expand Down
6 changes: 3 additions & 3 deletions armorlab/sources/nodes/photo_to_pbr_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let photo_to_pbr_node_tile_with_border_w: i32 = photo_to_pbr_node_tile_w + photo

function photo_to_pbr_node_create(raw: ui_node_t, args: f32_array_t): photo_to_pbr_node_t {
let n: photo_to_pbr_node_t = {};
n.base = logic_node_create();
n.base = logic_node_create(n);
n.base.get_as_image = photo_to_pbr_node_get_as_image;

if (photo_to_pbr_node_temp == null) {
Expand Down Expand Up @@ -78,7 +78,7 @@ function photo_to_pbr_node_get_as_image(self: photo_to_pbr_node_t, from: i32): i
}

let model_blob: buffer_t = data_get_blob("models/photo_to_" + photo_to_pbr_node_model_names[from] + ".quant.onnx");
let tensors: buffer_t[] = [f32a.buffer];
let tensors: buffer_t[] = [buffer_create_from_raw(f32a.buffer, f32a.length * 4)];
let buf: buffer_t = iron_ml_inference(model_blob, tensors, null, null, config_raw.gpu_inference);
let ar: f32_array_t = f32_array_create_from_buffer(buf);
u8a = u8_array_create(4 * photo_to_pbr_node_tile_w * photo_to_pbr_node_tile_w);
Expand Down Expand Up @@ -154,7 +154,7 @@ function photo_to_pbr_node_get_as_image(self: photo_to_pbr_node_t, from: i32): i
}
///end

let temp2: image_t = image_from_bytes(u8a.buffer, photo_to_pbr_node_tile_w, photo_to_pbr_node_tile_w);
let temp2: image_t = image_from_bytes(u8a, photo_to_pbr_node_tile_w, photo_to_pbr_node_tile_w);
g2_begin(photo_to_pbr_node_images[from]);
g2_draw_image(temp2, x * photo_to_pbr_node_tile_w, y * photo_to_pbr_node_tile_w);
g2_end();
Expand Down
5 changes: 3 additions & 2 deletions armorlab/sources/nodes/rgb_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type rgb_node_t = {
function rgb_node_create(raw: ui_node_t, args: f32_array_t): rgb_node_t {
let n: rgb_node_t = {};
n.raw = raw;
n.base = logic_node_create();
n.base = logic_node_create(n);
n.base.get_as_image = rgb_node_get_as_image;
n.base.get_cached_image = rgb_node_get_cached_image;
return n;
Expand All @@ -28,7 +28,8 @@ function rgb_node_get_as_image(self: rgb_node_t, from: i32): image_t {
f32a[1] = default_value[1];
f32a[2] = default_value[2];
f32a[3] = default_value[3];
self.image = image_from_bytes(f32a.buffer, 1, 1, tex_format_t.RGBA128);
let buf: buffer_t = buffer_create_from_raw(f32a.buffer, f32a.length * 4);
self.image = image_from_bytes(buf, 1, 1, tex_format_t.RGBA128);
return self.image;
}

Expand Down
38 changes: 27 additions & 11 deletions armorlab/sources/nodes/text_to_photo_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let text_to_photo_node_vae_decoder_blob: buffer_t;

function text_to_photo_node_create(raw: ui_node_t, args: f32_array_t): text_to_photo_node_t {
let n: text_to_photo_node_t = {};
n.base = logic_node_create();
n.base = logic_node_create(n);
n.base.get_as_image = text_to_photo_node_get_as_image;
n.base.get_cached_image = text_to_photo_node_get_cached_image;
return n;
Expand All @@ -27,7 +27,9 @@ function text_to_photo_node_get_cached_image(self: text_to_photo_node_t): image_
return text_to_photo_node_image;
}

function text_to_photo_node_buttons(ui: ui_t, nodes: ui_nodes_t, node: ui_node_t) {
function text_to_photo_node_button(node_id: i32) {
let node: ui_node_t = ui_get_node(ui_nodes_get_canvas(true).nodes, node_id);

text_to_photo_node_tiling = node.buttons[0].default_value == 0 ? false : true;
text_to_photo_node_prompt = ui_text_area(ui_handle(__ID__), ui_align_t.LEFT, true, tr("prompt"), true);
node.buttons[1].height = string_split(text_to_photo_node_prompt, "\n").length;
Expand Down Expand Up @@ -57,15 +59,25 @@ function text_to_photo_node_text_encoder(prompt: string, inpaint_latents: f32_ar
let words: string[] = string_split(
trim_end(string_replace_all(string_replace_all(string_replace_all(prompt, "\n", " "), ",", " , "), " ", " ")), " "
);

if (text_to_photo_node_vocab == null) {
let vocab_buffer: buffer_t = data_get_blob("models/vocab.json");
let vocab_json: string = sys_buffer_to_string(vocab_buffer);
text_to_photo_node_vocab = json_parse_to_map(vocab_json);
}

for (let i: i32 = 0; i < words.length; ++i) {
text_to_photo_node_text_input_ids[i + 1] = map_get(text_to_photo_node_vocab, to_lower_case(words[i]) + "</w>");
let word: string = to_lower_case(words[i]) + "</w>";
let value_string: string = map_get(text_to_photo_node_vocab, word);
let value: i32 = parse_int(value_string);
text_to_photo_node_text_input_ids[i + 1] = value;
}
for (let i: i32 = words.length; i < (text_to_photo_node_text_input_ids.length - 1); ++i) {
text_to_photo_node_text_input_ids[i + 1] = 49407; // <|endoftext|>
}

let i32a: i32_array_t = i32_array_create_from_array(text_to_photo_node_text_input_ids);
let tensors: buffer_t[] = [i32a.buffer];
let tensors: buffer_t[] = [buffer_create_from_raw(i32a.buffer, i32a.length * 4)];
let input_shape: i32_array_t[] = [];
let input_shape0: i32[] = [1, 77];
array_push(input_shape, input_shape0);
Expand All @@ -74,7 +86,7 @@ function text_to_photo_node_text_encoder(prompt: string, inpaint_latents: f32_ar
let text_embeddings: f32_array_t = f32_array_create_from_buffer(text_embeddings_buf);

i32a = i32_array_create_from_array(text_to_photo_node_uncond_input_ids);
tensors = [i32a.buffer];
tensors = [buffer_create_from_raw(i32a.buffer, i32a.length * 4)];
let uncond_embeddings_buf: buffer_t = iron_ml_inference(text_to_photo_node_text_encoder_blob, tensors, input_shape, output_shape, config_raw.gpu_inference);
let uncond_embeddings: f32_array_t = f32_array_create_from_buffer(uncond_embeddings_buf);

Expand Down Expand Up @@ -123,7 +135,11 @@ function text_to_photo_node_unet(latents: f32_array_t, text_embeddings: f32_arra

let t32: i32_array_t = i32_array_create(2);
t32[0] = timestep;
let tensors: buffer_t[] = [latent_model_input.buffer, t32.buffer, text_embeddings.buffer];
let tensors: buffer_t[] = [
buffer_create_from_raw(latent_model_input.buffer, latent_model_input.length * 4),
buffer_create_from_raw(t32.buffer, t32.length * 4),
buffer_create_from_raw(text_embeddings.buffer, text_embeddings.length * 4),
];
let input_shape: i32_array_t[] = [];
let input_shape0: i32[] = [2, 4, 64, 64];
let input_shape1: i32[] = [1];
Expand Down Expand Up @@ -233,7 +249,7 @@ function text_to_photo_node_vae_decoder(latents: f32_array_t, upscale: bool): im
latents[i] = 1.0 / 0.18215 * latents[i];
}

let tensors: buffer_t[] = [latents.buffer];
let tensors: buffer_t[] = [buffer_create_from_raw(latents.buffer, latents.length)];
let input_shape: i32_array_t[] = [];
let input_shape0: i32[] = [1, 4, 64, 64];
array_push(input_shape, input_shape0);
Expand All @@ -254,7 +270,7 @@ function text_to_photo_node_vae_decoder(latents: f32_array_t, upscale: bool): im
u8a[i * 4 + 2] = math_floor(pyimage[i + 512 * 512 * 2] * 255);
u8a[i * 4 + 3] = 255;
}
let image: image_t = image_from_bytes(u8a.buffer, 512, 512);
let image: image_t = image_from_bytes(u8a, 512, 512);

if (text_to_photo_node_tiling) {
tiling_node_prompt = text_to_photo_node_prompt;
Expand Down Expand Up @@ -312,10 +328,10 @@ let text_to_photo_node_def: ui_node_t = {
height: 0
},
{
name: "text_to_photo_node_buttons",
name: "text_to_photo_node_button",
type: "CUSTOM",
output: -1,
default_value: null,
default_value: f32_array_create_x(0),
data: null,
min: 0.0,
max: 1.0,
Expand Down Expand Up @@ -518,4 +534,4 @@ let text_to_photo_node_timesteps: i32[] = [981, 961, 961, 941, 921, 901, 881, 86
641, 621, 601, 581, 561, 541, 521, 501, 481, 461, 441, 421, 401, 381, 361, 341, 321, 301,
281, 261, 241, 221, 201, 181, 161, 141, 121, 101, 81, 61, 41, 21, 1];

let text_to_photo_node_vocab: map_t<string, i32>;
let text_to_photo_node_vocab: map_t<string, string> = null;
12 changes: 7 additions & 5 deletions armorlab/sources/nodes/tiling_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let tiling_node_auto: bool = true;

function tiling_node_create(raw: ui_node_t, args: f32_array_t): tiling_node_t {
let n: float_node_t = {};
n.base = logic_node_create();
n.base = logic_node_create(n);
n.base.get_as_image = tiling_node_get_as_image;
n.base.get_cached_image = tiling_node_get_cached_image;
tiling_node_init();
Expand All @@ -24,7 +24,9 @@ function tiling_node_init() {
}
}

function tiling_node_buttons(ui: ui_t, nodes: ui_nodes_t, node: ui_node_t) {
function tiling_node_button(node_id: i32) {
let node: ui_node_t = ui_get_node(ui_nodes_get_canvas(true).nodes, node_id);

tiling_node_auto = node.buttons[0].default_value == 0 ? false : true;
if (!tiling_node_auto) {
let tiling_node_strength_handle: ui_handle_t = ui_handle(__ID__);
Expand Down Expand Up @@ -91,7 +93,7 @@ function tiling_node_sd_tiling(image: image_t, seed: i32): image_t {
// u8a[y * 512 + x] = 0;
// }
// }
let mask: image_t = image_from_bytes(u8a.buffer, 512, 512, tex_format_t.R8);
let mask: image_t = image_from_bytes(u8a, 512, 512, tex_format_t.R8);

inpaint_node_prompt = tiling_node_prompt;
inpaint_node_strength = tiling_node_strength;
Expand Down Expand Up @@ -149,10 +151,10 @@ let tiling_node_def: ui_node_t = {
height: 0
},
{
name: "tiling_node_buttons",
name: "tiling_node_button",
type: "CUSTOM",
output: -1,
default_value: null,
default_value: f32_array_create_x(0),
data: null,
min: 0.0,
max: 1.0,
Expand Down
6 changes: 3 additions & 3 deletions armorlab/sources/nodes/upscale_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let upscale_node_esrgan_blob: buffer_t;

function upscale_node_create(raw: ui_node_t, args: f32_array_t): upscale_node_t {
let n: float_node_t = {};
n.base = logic_node_create();
n.base = logic_node_create(n);
n.base.get_as_image = upscale_node_get_as_image;
n.base.get_cached_image = upscale_node_get_cached_image;
return n;
Expand Down Expand Up @@ -64,7 +64,7 @@ function upscale_node_do_tile(source: image_t): image_t {
f32a[i + size1w * size1w * 2] = (u8a[i * 4 + 2] / 255);
}

let tensors: buffer_t[] = [f32a.buffer];
let tensors: buffer_t[] = [buffer_create_from_raw(f32a.buffer, f32a.length)];
let input_shape: i32_array_t[] = [];
let input_shape0: i32[] = [1, 3, size1w, size1h];
array_push(input_shape, input_shape0);
Expand All @@ -88,7 +88,7 @@ function upscale_node_do_tile(source: image_t): image_t {
u8a[i * 4 + 3] = 255;
}

result = image_from_bytes(u8a.buffer, size2w, size2h);
result = image_from_bytes(u8a, size2w, size2h);
return result;
}

Expand Down
12 changes: 7 additions & 5 deletions armorlab/sources/nodes/variance_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let variance_node_prompt: string = "";

function variance_node_create(raw: ui_node_t, args: f32_array_t): variance_node_t {
let n: variance_node_t = {};
n.base = logic_node_create();
n.base = logic_node_create(n);
n.base.get_as_image = variance_node_get_as_image;
n.base.get_cached_image = variance_node_get_cached_image;

Expand All @@ -26,7 +26,9 @@ function variance_node_init() {
}
}

function variance_node_buttons(ui: ui_t, nodes: ui_nodes_t, node: ui_node_t) {
function variance_node_button(node_id: i32) {
let node: ui_node_t = ui_get_node(ui_nodes_get_canvas(true).nodes, node_id);

variance_node_prompt = ui_text_area(ui_handle(__ID__), ui_align_t.LEFT, true, tr("prompt"), true);
node.buttons[0].height = string_split(variance_node_prompt, "\n").length;
}
Expand All @@ -53,7 +55,7 @@ function variance_node_get_as_image(self: variance_node_t, from: i32): image_t {
iron_g4_swap_buffers();

let vae_encoder_blob: buffer_t = data_get_blob("models/sd_vae_encoder.quant.onnx");
let tensors: buffer_t[] = [f32a.buffer];
let tensors: buffer_t[] = [buffer_create_from_raw(f32a.buffer, f32a.length)];
let input_shape: i32_array_t[] = [];
let input_shape0: i32[] = [1, 3, 512, 512];
array_push(input_shape, input_shape0);
Expand Down Expand Up @@ -135,10 +137,10 @@ let variance_node_def: ui_node_t = {
],
buttons: [
{
name: "variance_node_buttons",
name: "variance_node_button",
type: "CUSTOM",
output: -1,
default_value: null,
default_value: f32_array_create_x(0),
data: null,
min: 0.0,
max: 1.0,
Expand Down
9 changes: 9 additions & 0 deletions armorlab/sources/nodes_brush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ function nodes_brush_init() {
map_set(nodes_brush_creates, "upscale_node", upscale_node_create);
map_set(nodes_brush_creates, "variance_node", variance_node_create);

map_set(nodes_brush_creates, "float_node", float_node_create);
map_set(nodes_brush_creates, "vector_node", vector_node_create);
map_set(nodes_brush_creates, "color_node", color_node_create);

nodes_brush_input = [
image_texture_node_def,
rgb_node_def,
Expand All @@ -40,6 +44,11 @@ function nodes_brush_init() {
nodes_brush_input,
nodes_brush_model
];

map_set(ui_nodes_custom_buttons, "inpaint_node_button", inpaint_node_button);
map_set(ui_nodes_custom_buttons, "text_to_photo_node_button", text_to_photo_node_button);
map_set(ui_nodes_custom_buttons, "tiling_node_button", tiling_node_button);
map_set(ui_nodes_custom_buttons, "variance_node_button", variance_node_button);
}

function nodes_brush_create_node(node_type: string): ui_node_t {
Expand Down
4 changes: 2 additions & 2 deletions armorlab/sources/render_path_paint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ function render_path_paint_bind_layers() {
let canvas: ui_node_canvas_t = ui_nodes_get_canvas(true);
if (nodes.nodes_selected_id.length > 0) {
let node: ui_node_t = ui_get_node(canvas.nodes, nodes.nodes_selected_id[0]);
let brush_node: logic_node_t = parser_logic_get_logic_node(node);
let brush_node: logic_node_ext_t = parser_logic_get_logic_node(node);
if (brush_node != null) {
image = logic_node_get_cached_image(brush_node);
image = logic_node_get_cached_image(brush_node.base);
}
}
if (image != null) {
Expand Down
Loading

0 comments on commit eb9160d

Please sign in to comment.