From 74e8d3f0278a65fdf86a1185fec8a6016e628e88 Mon Sep 17 00:00:00 2001 From: quou Date: Sun, 19 Jan 2025 21:04:51 +1100 Subject: render UI in software --- c2.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 3 deletions(-) (limited to 'c2.cpp') diff --git a/c2.cpp b/c2.cpp index e8e6c82..3c862a6 100644 --- a/c2.cpp +++ b/c2.cpp @@ -17,7 +17,7 @@ extern "C" { #define video_arena_size (1024 * 1024 * 16) #define asset_arena_size (1024 * 1024 * 4) -#define ui_arena_size (1024 * 16) +#define ui_arena_size (1024 * 1024 * 64) #define scene_arena_size (1024) #define per_frame_memory_size (1024 * 1024) @@ -645,6 +645,8 @@ struct C2 : public App { Buffer_Id vbo, cbuf; Sampler_Id clamped_linear; Texture_Id hdr_target; + Texture_Id ui_texture; + Buffer_Id ui_buffer; Line_Renderer lr; World* world; UI* ui; @@ -683,6 +685,7 @@ struct C2 : public App { dev = Device::create(&video_arena, this); default_texture = make_default_texture(dev); make_hdr_target(); + make_ui_texture(); model_loader.init(dev, &assets); mat_loader.init(&assets, default_texture); register_asset_loader("MODL", &model_loader); @@ -705,7 +708,7 @@ struct C2 : public App { clamped_linear = create_clamped_linear(dev); world = (World*)arena_alloc(arena, sizeof *world); world->init(arena); - ui = UI::create(dev, this, &ui_arena, ui_shader->id); + ui = UI::create(this, &ui_arena); init_editor(ui, world); lr.init(dev, &assets); assert(per_frame != 0); @@ -876,7 +879,39 @@ struct C2 : public App { ctx.debug_pop(); ctx.debug_push("ui"); - ui->render(&frame_arena, dev->get_backbuffer()); + ui->render(&frame_arena); + { + int s = ui->ren.bound.w * ui->ren.bound.h * 4; + void* pixels = dev->map_buffer(ui_buffer, 0, s); + memcpy(pixels, ui->ren.pixels, s); + dev->unmap_buffer(ui_buffer); + ctx.copy(ui_texture, ui_buffer); + + pb.begin_rp(); + pb.rp_target(dev->get_backbuffer(), Clear_Mode::restore); + Render_Pass& ui_pass = pb.build_rp(); + + pb.begin(); + pb.shader(ui_shader->id); + pb.vertex_format(ui_shader->vf); + pb.blend( + Blend_Mode::add, + Blend_Factor::src_alpha, + Blend_Factor::inv_src_alpha + ); + pb.texture( + ui_shader->descriptor_binding("atlas"), + ui_texture, + clamped_linear + ); + Pipeline& ui_pip = pb.build(); + quad.render( + ctx, + ui_pip, + ui_pass, + ui_shader->binding_index("verts") + ); + } ctx.debug_pop(); if (mjp(mbtn_left)) { @@ -920,6 +955,8 @@ struct C2 : public App { assets.destroy(); dev->destroy_texture(default_texture); dev->destroy_texture(hdr_target); + dev->destroy_texture(ui_texture); + dev->destroy_buffer(ui_buffer); dev->destroy_sampler(clamped_linear); dev->destroy_buffer(vbo); dev->destroy_buffer(cbuf); @@ -930,7 +967,10 @@ struct C2 : public App { ui->layout(w, h); dev->on_resize(); dev->destroy_texture(hdr_target); + dev->destroy_texture(ui_texture); + dev->destroy_buffer(ui_buffer); make_hdr_target(); + make_ui_texture(); } void make_hdr_target() { @@ -947,6 +987,26 @@ struct C2 : public App { ); } + void make_ui_texture() { + ui_texture = dev->create_texture( + "UI texture", + texture_format_rgba8i, + Texture_Flags::sampleable | Texture_Flags::copy_dst, + w, + h, + 1, + 1, + 1, + Buffer_Id(0) + ); + ui_buffer = dev->create_buffer( + "UI Buffer", + w * h * 4, + Buffer_Flags::copy_src | + Buffer_Flags::cpu_readwrite + ); + } + void on_text_input(const char* buf) override { ui->text_input(buf); } -- cgit v1.2.3-54-g00ecf