From 29d8d8cc8a85ec1e91c7a83dd1c77a6b57278458 Mon Sep 17 00:00:00 2001 From: Andreew Gregory Date: Fri, 20 Feb 2026 02:43:47 +0300 Subject: [PATCH] Added drawing of funny silly rectangles on screen --- Makefile | 1 + src/l2/alice/engine.h | 8 +- src/l2/drawer_2d.h | 106 +++++++++++++++++++++++++ src/l2/lucy/glyph_render.h | 2 - src/l3/r4/r4.c | 8 ++ src/l_adele/drawer_2d/plain/plain.frag | 9 +++ src/l_adele/drawer_2d/plain/plain.vert | 24 ++++++ src/l_adele/lucy/lucy.vert | 1 - 8 files changed, 155 insertions(+), 4 deletions(-) create mode 100644 src/l2/drawer_2d.h create mode 100644 src/l_adele/drawer_2d/plain/plain.frag create mode 100644 src/l_adele/drawer_2d/plain/plain.vert diff --git a/Makefile b/Makefile index 58e297b..cbc3d05 100644 --- a/Makefile +++ b/Makefile @@ -95,6 +95,7 @@ gen/l_adele/dorothy.txt: $(ASSETS_src_l_adele) $(call compile_shader,alice/0gen,0gen) $(call compile_shader,alice/0sh,0sh) $(call compile_shader,alice/1,1) + $(call compile_shader,drawer_2d/plain,plain) touch gen/l_adele/dorothy.txt out/l2/t0: src/l2/tests/data_structures/t0.c $(HEADERS_gen_l1_5) diff --git a/src/l2/alice/engine.h b/src/l2/alice/engine.h index 617eaf8..e347a5c 100644 --- a/src/l2/alice/engine.h +++ b/src/l2/alice/engine.h @@ -4,6 +4,7 @@ #include "../../l1/marie/geom_alg_utils.h" #include "../margaret/vulkan_utils.h" #include "transfer_in_mainloop.h" +#include "../drawer_2d.h" #include "../lucy/glyph_render.h" typedef struct { @@ -743,6 +744,7 @@ struct Alice { MargaretImg IT1_image; MargaretImg zbuffer_image; + Plain2dShapeRenderer plain_shape_renderer; FT_Library ft_library; LucyGlyphCache lucy_cache; LucyRenderer lucy_renderer; @@ -938,6 +940,7 @@ void alice_reset_and_record_command_buffer_1( sizeof(Pipeline1PushRangeVertex), sizeof(Pipeline1PushRangeFragment), ®_and_params_fragment); vkCmdDraw(alice->rendering_command_buf_1, 3, 1, 0, 0); + Plain2dShapeRenderer_frame_rec_drawing(&alice->plain_shape_renderer, alice->rendering_command_buf_1, image_extent); LucyRenderer_another_frame_rec_drawing(&alice->lucy_renderer, alice->rendering_command_buf_1, image_extent); vkCmdEndRenderPass(alice->rendering_command_buf_1); @@ -1077,7 +1080,8 @@ void alice_frame_drawing(Alice* alice) { alice->callbacks.on_another_frame(alice->guest, (float)(alice->wl.cur_frame_time - alice->wl.last_frame_time) / 1000); AliceScene__another_frame(alice); - // LucyGlyphCache_another_frame(&alice->lucy_cache); lucy cache has no business here + // LucyGlyphCache_another_frame(&alice->lucy_cache); lucy cache has no business here... Well, maybe later + Plain2dShapeRenderer_another_frame(&alice->plain_shape_renderer); LucyRenderer_another_frame(&alice->lucy_renderer); margaret_end_command_buffer(alice->transfer_command_buf); margaret_end_command_buffer(alice->compute_command_buf); @@ -1613,6 +1617,8 @@ Alice* Alice_new(){ .linear_sampler = alice->linear_sampler, .nearest_sampler = alice->nearest_sampler }; + alice->plain_shape_renderer = Plain2dShapeRenderer_new(engine_reference, root_dir, alice->render_pass_1, 0); + FT_Error ft_init_err = FT_Init_FreeType(&alice->ft_library); if (ft_init_err) abortf("Can't init free type library\n"); diff --git a/src/l2/drawer_2d.h b/src/l2/drawer_2d.h new file mode 100644 index 0000000..4de0f60 --- /dev/null +++ b/src/l2/drawer_2d.h @@ -0,0 +1,106 @@ +#pragma once + +#include "margaret/vulkan_utils.h" +#include "../../gen/l1/geom.h" + +typedef struct { + vec2 pos; + vec4 color; +} Plain2dShapeVertex; + +typedef struct { + MargaretEngineReference ve; + VkPipelineLayout pipeline_layout; + VkPipeline pipeline; + + U64 vertex_count; + MargaretSubbuf staging_vbo; + MargaretSubbuf vbo; + bool need_to_transfer; +} Plain2dShapeRenderer; + +Plain2dShapeRenderer Plain2dShapeRenderer_new(MargaretEngineReference ve, SpanU8 root_dir, + VkRenderPass render_pass, U32 renderpass_subpass) { + VkPipelineLayout pipeline_layout; + vkCreatePipelineLayout(ve.device, &(VkPipelineLayoutCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .pushConstantRangeCount = 1, + .pPushConstantRanges = (VkPushConstantRange[]){{ + .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, .offset = 0, .size = sizeof(vec2) + }}, + }, NULL, &pipeline_layout); + + VkPipeline pipeline = margaret_create_triangle_pipeline_one_attachment(ve.device, + render_pass, renderpass_subpass, (MargaretMostImportantPipelineOptions){ + .pipeline_layout = pipeline_layout, + .vertex_shader_code = read_file_by_path(VecU8_fmt("%s/gen/l_adele/drawer_2d/plain/vert.spv", root_dir)), + .fragment_shader_code = read_file_by_path(VecU8_fmt("%s/gen/l_adele/drawer_2d/plain/frag.spv", root_dir)), + .vertexBindingDescriptionCount = 1, + .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]){ + { .binding = 0, .stride = sizeof(Plain2dShapeVertex), .inputRate = VK_VERTEX_INPUT_RATE_VERTEX } }, + .vertexAttributeDescriptionCount = 2, + .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]){ + {.location = 0, .binding = 0, + .format = VK_FORMAT_R32G32_SFLOAT, .offset = offsetof(Plain2dShapeVertex, pos)}, + {.location = 1, .binding = 0, + .format = VK_FORMAT_R32G32B32A32_SFLOAT, .offset = offsetof(Plain2dShapeVertex, color)}, + }, + .depthTestEnable = false, .depthWriteEnable = false, .blendEnable = true, + }); + + return (Plain2dShapeRenderer){ + .ve = ve, .pipeline_layout = pipeline_layout, .pipeline = pipeline, + .vertex_count = 0, + .staging_vbo = MargaretBufAllocator_alloc(ve.staging_buffers, 67), + .vbo = MargaretBufAllocator_alloc(ve.dev_local_buffers, 67) + }; +} + +void Plain2dShapeRenderer_clear(Plain2dShapeRenderer* self) { + self->vertex_count = 0; +} + +/* Sadly, we don't use rbo right now. Might fix it when I get enough rizz */ +void Plain2dShapeRenderer_add_triangle(Plain2dShapeRenderer* self, vec4 color, vec2 A, vec2 B, vec2 C) { + U64 needed_vbo_length = (self->vertex_count + 3) * sizeof(Plain2dShapeVertex); + if (needed_vbo_length > self->staging_vbo.len) { + MargaretBufAllocator_expand_or_move_old_host_visible( + self->ve.staging_buffers, &self->staging_vbo, needed_vbo_length); + } + Plain2dShapeVertex* vbo_data = (Plain2dShapeVertex*)MargaretSubbuf_get_mapped(&self->staging_vbo); + vbo_data[self->vertex_count++] = (Plain2dShapeVertex){.pos = A, .color = color}; + vbo_data[self->vertex_count++] = (Plain2dShapeVertex){.pos = B, .color = color}; + vbo_data[self->vertex_count++] = (Plain2dShapeVertex){.pos = C, .color = color}; + self->need_to_transfer = true; +} + +void Plain2dShapeRenderer_another_frame(Plain2dShapeRenderer* self){ + U64 needed_vbo_length = self->vertex_count * sizeof(Plain2dShapeVertex); + if (self->vbo.len < needed_vbo_length) { + MargaretBufAllocator_expand_or_free_old(self->ve.dev_local_buffers, &self->vbo, needed_vbo_length); + } + if ((self->need_to_transfer) && self->vertex_count > 0) { + self->need_to_transfer = false; + margaret_rec_cmd_copy_buffer_one_to_one_part(self->ve.transfer_cmd_buffer, + &self->staging_vbo, &self->vbo, 0, needed_vbo_length); + } +} + +void Plain2dShapeRenderer_frame_rec_drawing(Plain2dShapeRenderer* self, + VkCommandBuffer drawing_cmd_buf, VkExtent2D image_extent + ){ + vkCmdBindPipeline(drawing_cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, self->pipeline); + vkCmdBindVertexBuffers(drawing_cmd_buf, 0, 1, + (VkBuffer[]){MargaretSubbuf_get_buffer( &self->vbo )}, (U64[]){ self->vbo.start }); + vkCmdPushConstants(drawing_cmd_buf, self->pipeline_layout, VK_SHADER_STAGE_VERTEX_BIT, + 0, sizeof(vec2), &(vec2){ + .x /* aka width */ = (float)image_extent.width, .y /* aka height */ = (float)image_extent.height }); + vkCmdDraw(drawing_cmd_buf, self->vertex_count, 1, 0, 0); +} + +void Plain2dShapeRenderer_add_rectangle(Plain2dShapeRenderer* self, vec4 color, vec2 start, float width, float height) { + Plain2dShapeRenderer_add_triangle(self, color, + (vec2){start.x + width, start.y}, start, (vec2){start.x, start.y + height}); + Plain2dShapeRenderer_add_triangle(self, color, + (vec2){start.x + width, start.y}, (vec2){start.x, start.y + height}, (vec2){start.x + width, start.y + height}); +} \ No newline at end of file diff --git a/src/l2/lucy/glyph_render.h b/src/l2/lucy/glyph_render.h index a54f0f4..0de639b 100644 --- a/src/l2/lucy/glyph_render.h +++ b/src/l2/lucy/glyph_render.h @@ -103,8 +103,6 @@ void LucyRenderer_draw_char_glyph(LucyRenderer* self, vec4 color, ivec2 pos, Luc U64 needed_vbo_length = (self->glyphs_count + 1) * sizeof(LucyRenderInstance); if (self->staging_vbo.len < needed_vbo_length) { - //printf("LucyRenderer Staging Buffer: Gotta replace %lu with %lu\n", - // self->staging_vbo.len, needed_vbo_length); MargaretBufAllocator_expand_or_move_old_host_visible( self->ve.staging_buffers, &self->staging_vbo, needed_vbo_length); } diff --git a/src/l3/r4/r4.c b/src/l3/r4/r4.c index f81f336..9d32094 100644 --- a/src/l3/r4/r4.c +++ b/src/l3/r4/r4.c @@ -328,6 +328,14 @@ void run_app(){ LucyRenderer_add_simple_label(&st.alice->lucy_renderer, st.font_face_of_size_40, (vec4){0, 0, 0, 1}, 0, cstr("..."), (ivec2){10, 10}); + // Just a little test of plain shape drawing + for (int i = 0; i < 10; i++) { + for (int j = 0; j < 5; j++) { + Plain2dShapeRenderer_add_rectangle(&alice->plain_shape_renderer, + vec3_and_one(sample_rainbow_color()), (vec2){20 + 60 * (float)i, 60 + 30 * (float)j }, 50, 20); + } + } + VecU8 ROA_mesh_path = vcstr("./gen/l2/models/log_10_2_6.AliceGenericMesh"); st.ROA_topology = alice_expect_read_generic_mesh_from_file(ROA_mesh_path); diff --git a/src/l_adele/drawer_2d/plain/plain.frag b/src/l_adele/drawer_2d/plain/plain.frag new file mode 100644 index 0000000..b3c0eee --- /dev/null +++ b/src/l_adele/drawer_2d/plain/plain.frag @@ -0,0 +1,9 @@ +#version 450 + +layout(location = 0) in vec4 color; + +layout(location = 0) out vec4 fin_color; + +void main(){ + fin_color = color; +} \ No newline at end of file diff --git a/src/l_adele/drawer_2d/plain/plain.vert b/src/l_adele/drawer_2d/plain/plain.vert new file mode 100644 index 0000000..6bf52c0 --- /dev/null +++ b/src/l_adele/drawer_2d/plain/plain.vert @@ -0,0 +1,24 @@ +#version 450 + +layout(location = 0) in vec2 pos; +layout(location = 1) in vec4 color; + +layout(push_constant, std430) uniform pc { + float width; + float height; +}; + +layout (location = 0) out vec4 vsout_color; + +float lint(float A1, float B1, float A2, float B2, float x){ + return A2 + (B2 - A2) * (x - A1) / (B1 - A1); +} + +float deng(float B1, float x){ + return lint(0, B1, -1, 1, x); +} + +void main(){ + vsout_color = color; + gl_Position = vec4(deng(width, pos.x), deng(height, pos.y), 0, 1); +} diff --git a/src/l_adele/lucy/lucy.vert b/src/l_adele/lucy/lucy.vert index 788fc91..b02e6a3 100644 --- a/src/l_adele/lucy/lucy.vert +++ b/src/l_adele/lucy/lucy.vert @@ -38,5 +38,4 @@ void main(){ vsout_tex_ind = tex_ind; vec2 pos = all_v_pos[gl_VertexIndex % 6]; gl_Position = vec4(deng(width, pos.x), deng(height, pos.y), 0, 1); - }