From 9d829cc02e654b7024bb08010b0d582d7018bc91 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Sat, 12 Jul 2025 16:03:47 +0200 Subject: [PATCH] Implement draw transform --- armorpaint/shaders/layer_invert.kong | 2 +- armorpaint/shaders/layer_view.kong | 2 +- base/shaders/draw/draw_image.kong | 3 +- base/shaders/draw/draw_rect.kong | 3 +- base/shaders/draw/draw_text.kong | 3 +- base/shaders/draw/draw_tris.kong | 3 +- base/shaders/layer_copy.kong | 2 +- base/shaders/layer_copy_bgra.kong | 2 +- base/sources/iron_draw.c | 49 ++++++++++++++-------------- 9 files changed, 36 insertions(+), 33 deletions(-) diff --git a/armorpaint/shaders/layer_invert.kong b/armorpaint/shaders/layer_invert.kong index f3d1d163..7db6b5de 100644 --- a/armorpaint/shaders/layer_invert.kong +++ b/armorpaint/shaders/layer_invert.kong @@ -1,7 +1,7 @@ #[set(everything)] const constants: { - P: float4x4; + W: float3x3; pos: float4; // xywh tex: float4; // xywh col: float4; diff --git a/armorpaint/shaders/layer_view.kong b/armorpaint/shaders/layer_view.kong index a91160b5..b34cc2ec 100644 --- a/armorpaint/shaders/layer_view.kong +++ b/armorpaint/shaders/layer_view.kong @@ -1,7 +1,7 @@ #[set(everything)] const constants: { - P: float4x4; + W: float3x3; pos: float4; // xywh tex: float4; // xywh col: float4; diff --git a/base/shaders/draw/draw_image.kong b/base/shaders/draw/draw_image.kong index ad0e628b..045c2a6a 100644 --- a/base/shaders/draw/draw_image.kong +++ b/base/shaders/draw/draw_image.kong @@ -1,7 +1,7 @@ #[set(everything)] const constants: { - P: float4x4; + W: float3x3; pos: float4; // xywh tex: float4; // xywh col: float4; @@ -30,6 +30,7 @@ fun draw_image_vert(input: vert_in): vert_out { var ctex: float4 = constants.tex; output.pos = float4(input.pos, 0.0, 1.0); + output.pos.xyz = constants.W * output.pos.xyz; output.pos.xy = output.pos.xy * cpos.zw + cpos.xy; output.pos.xy = output.pos.xy * 2.0 - 1.0; output.pos.y = -output.pos.y; diff --git a/base/shaders/draw/draw_rect.kong b/base/shaders/draw/draw_rect.kong index b0b9de90..d0175d57 100644 --- a/base/shaders/draw/draw_rect.kong +++ b/base/shaders/draw/draw_rect.kong @@ -1,7 +1,7 @@ #[set(everything)] const constants: { - P: float4x4; + W: float3x3; pos: float4; // xywh col: float4; }; @@ -21,6 +21,7 @@ fun draw_rect_vert(input: vert_in): vert_out { var cpos: float4 = constants.pos; output.pos = float4(input.pos, 0.0, 1.0); + // output.pos.xyz = constants.W * output.pos.xyz; output.pos.xy = output.pos.xy * cpos.zw + cpos.xy; output.pos.xy = output.pos.xy * 2.0 - 1.0; output.pos.y = -output.pos.y; diff --git a/base/shaders/draw/draw_text.kong b/base/shaders/draw/draw_text.kong index f556168d..14d4b5d5 100644 --- a/base/shaders/draw/draw_text.kong +++ b/base/shaders/draw/draw_text.kong @@ -1,7 +1,7 @@ #[set(everything)] const constants: { - P: float4x4; + W: float3x3; pos: float4; // xywh tex: float4; // xywh col: float4; @@ -30,6 +30,7 @@ fun draw_text_vert(input: vert_in): vert_out { var ctex: float4 = constants.tex; output.pos = float4(input.pos, 0.0, 1.0); + // output.pos.xyz = constants.W * output.pos.xyz; output.pos.xy = output.pos.xy * cpos.zw + cpos.xy; output.pos.xy = output.pos.xy * 2.0 - 1.0; output.pos.y = -output.pos.y; diff --git a/base/shaders/draw/draw_tris.kong b/base/shaders/draw/draw_tris.kong index dd1c7304..82024d73 100644 --- a/base/shaders/draw/draw_tris.kong +++ b/base/shaders/draw/draw_tris.kong @@ -1,7 +1,7 @@ #[set(everything)] const constants: { - P: float4x4; + W: float3x3; pos0: float2; pos1: float2; pos2: float2; @@ -28,6 +28,7 @@ fun draw_tris_vert(input: vert_in): vert_out { else if (vertex_id() == 2) { output.pos = float4(constants.pos2, 0.0, 1.0); } + // output.pos.xyz = constants.W * output.pos.xyz; output.pos.xy = output.pos.xy * 2.0 - 1.0; output.pos.y = -output.pos.y; output.col = constants.col; diff --git a/base/shaders/layer_copy.kong b/base/shaders/layer_copy.kong index 2488c0cf..70a88a73 100644 --- a/base/shaders/layer_copy.kong +++ b/base/shaders/layer_copy.kong @@ -1,7 +1,7 @@ #[set(everything)] const constants: { - P: float4x4; + W: float3x3; pos: float4; // xywh tex: float4; // xywh col: float4; diff --git a/base/shaders/layer_copy_bgra.kong b/base/shaders/layer_copy_bgra.kong index 1bcdcca4..c2ede951 100644 --- a/base/shaders/layer_copy_bgra.kong +++ b/base/shaders/layer_copy_bgra.kong @@ -1,7 +1,7 @@ #[set(everything)] const constants: { - P: float4x4; + W: float3x3; pos: float4; // xywh tex: float4; // xywh col: float4; diff --git a/base/sources/iron_draw.c b/base/sources/iron_draw.c index 2901ec00..d42abc04 100644 --- a/base/sources/iron_draw.c +++ b/base/sources/iron_draw.c @@ -16,7 +16,6 @@ draw_font_t *draw_font = NULL; int draw_font_size; gpu_texture_t *_draw_current = NULL; -static iron_matrix4x4_t draw_projection_matrix; static iron_matrix3x3_t draw_transform; static bool draw_bilinear_filter = true; static uint32_t draw_color = 0; @@ -32,7 +31,7 @@ static gpu_shader_t image_vert_shader; static gpu_shader_t image_frag_shader; static gpu_pipeline_t image_pipeline; static int image_tex_unit; -static int image_p_loc; +static int image_w_loc; static int image_pos_loc; static int image_tex_loc; static int image_col_loc; @@ -40,14 +39,14 @@ static int image_col_loc; static gpu_shader_t rect_vert_shader; static gpu_shader_t rect_frag_shader; static gpu_pipeline_t rect_pipeline; -static int rect_p_loc; +static int rect_w_loc; static int rect_pos_loc; static int rect_col_loc; static gpu_shader_t tris_vert_shader; static gpu_shader_t tris_frag_shader; static gpu_pipeline_t tris_pipeline; -static int tris_p_loc; +static int tris_w_loc; static int tris_pos0_loc; static int tris_pos1_loc; static int tris_pos2_loc; @@ -58,7 +57,7 @@ static gpu_shader_t text_frag_shader; static gpu_pipeline_t text_pipeline; static gpu_pipeline_t text_pipeline_rt; static int text_tex_unit; -static int text_p_loc; +static int text_w_loc; static int text_pos_loc; static int text_tex_loc; static int text_col_loc; @@ -161,10 +160,10 @@ void draw_init(buffer_t *image_vert, buffer_t *image_frag, buffer_t *rect_vert, draw_pipeline_init(&image_pipeline, &image_vert_shader, &image_frag_shader); gpu_pipeline_compile(&image_pipeline); image_tex_unit = 0; - image_p_loc = 0; - image_pos_loc = 64; - image_tex_loc = 80; - image_col_loc = 96; + image_w_loc = 0; + image_pos_loc = 48; + image_tex_loc = 64; + image_col_loc = 80; } // Rect painter @@ -174,9 +173,9 @@ void draw_init(buffer_t *image_vert, buffer_t *image_frag, buffer_t *rect_vert, draw_pipeline_init(&rect_pipeline, &rect_vert_shader, &rect_frag_shader); rect_pipeline.blend_source = GPU_BLEND_SOURCE_ALPHA; gpu_pipeline_compile(&rect_pipeline); - rect_p_loc = 0; - rect_pos_loc = 64; - rect_col_loc = 80; + rect_w_loc = 0; + rect_pos_loc = 48; + rect_col_loc = 64; } // Tris painter @@ -186,11 +185,11 @@ void draw_init(buffer_t *image_vert, buffer_t *image_frag, buffer_t *rect_vert, draw_pipeline_init(&tris_pipeline, &tris_vert_shader, &tris_frag_shader); tris_pipeline.blend_source = GPU_BLEND_SOURCE_ALPHA; gpu_pipeline_compile(&tris_pipeline); - tris_p_loc = 0; - tris_pos0_loc = 64; - tris_pos1_loc = 72; - tris_pos2_loc = 80; - tris_col_loc = 96; // 88 with 16 align + tris_w_loc = 0; + tris_pos0_loc = 48; + tris_pos1_loc = 56; + tris_pos2_loc = 64; + tris_col_loc = 80; // 16 align } // Text painter @@ -201,10 +200,10 @@ void draw_init(buffer_t *image_vert, buffer_t *image_frag, buffer_t *rect_vert, text_pipeline.blend_source = GPU_BLEND_SOURCE_ALPHA; gpu_pipeline_compile(&text_pipeline); text_tex_unit = 0; - text_p_loc = 0; - text_pos_loc = 64; - text_tex_loc = 80; - text_col_loc = 96; + text_w_loc = 0; + text_pos_loc = 48; + text_tex_loc = 64; + text_col_loc = 80; draw_pipeline_init(&text_pipeline_rt, &text_vert_shader, &text_frag_shader); text_pipeline_rt.blend_source = GPU_BLEND_SOURCE_ALPHA; @@ -232,7 +231,7 @@ void draw_scaled_sub_image(gpu_texture_t *tex, float sx, float sy, float sw, flo gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : &image_pipeline); gpu_set_vertex_buffer(&rect_vertex_buffer); gpu_set_index_buffer(&rect_index_buffer); - gpu_set_matrix4(image_p_loc, draw_projection_matrix); + gpu_set_matrix3(image_w_loc, draw_transform); gpu_set_float4(image_pos_loc, dx / vw(), dy / vh(), dw / vw(), dh / vh()); gpu_set_float4(image_tex_loc, sx / tex->width, sy / tex->height, sw / tex->width, sh / tex->height); gpu_set_float4(image_col_loc, _draw_color_r(draw_color) / 255.0, _draw_color_g(draw_color) / 255.0, _draw_color_b(draw_color) / 255.0, _draw_color_a(draw_color) / 255.0); @@ -256,7 +255,7 @@ void draw_filled_triangle(float x0, float y0, float x1, float y1, float x2, floa gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : &tris_pipeline); gpu_set_vertex_buffer(&tris_vertex_buffer); gpu_set_index_buffer(&tris_index_buffer); - gpu_set_matrix4(tris_p_loc, draw_projection_matrix); + gpu_set_matrix3(tris_w_loc, draw_transform); gpu_set_float2(tris_pos0_loc, x0 / vw(), y0 / vh()); gpu_set_float2(tris_pos1_loc, x1 / vw(), y1 / vh()); gpu_set_float2(tris_pos2_loc, x2 / vw(), y2 / vh()); @@ -268,7 +267,7 @@ void draw_filled_rect(float x, float y, float width, float height) { gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : &rect_pipeline); gpu_set_vertex_buffer(&rect_vertex_buffer); gpu_set_index_buffer(&rect_index_buffer); - gpu_set_matrix4(rect_p_loc, draw_projection_matrix); + gpu_set_matrix3(rect_w_loc, draw_transform); gpu_set_float4(rect_pos_loc, x / vw(), y / vh(), width / vw(), height / vh()); gpu_set_float4(rect_col_loc, _draw_color_r(draw_color) / 255.0, _draw_color_g(draw_color) / 255.0, _draw_color_b(draw_color) / 255.0, _draw_color_a(draw_color) / 255.0); gpu_draw(); @@ -530,7 +529,7 @@ void draw_string(const char *text, float x, float y) { xpos += q.xadvance; gpu_set_float4(text_pos_loc, q.x0 / vw(), q.y0 / vh(), (q.x1 - q.x0) / vw(), (q.y1 - q.y0) / vh()); gpu_set_float4(text_tex_loc, q.s0, q.t0, q.s1 - q.s0, q.t1 - q.t0); - gpu_set_matrix4(text_p_loc, draw_projection_matrix); + gpu_set_matrix3(text_w_loc, draw_transform); gpu_set_float4(text_col_loc, _draw_color_r(draw_color) / 255.0, _draw_color_g(draw_color) / 255.0, _draw_color_b(draw_color) / 255.0, _draw_color_a(draw_color) / 255.0); gpu_draw(); }