From 4ed3e174326c3b243685c08ded3079ad0a044050 Mon Sep 17 00:00:00 2001 From: Katajisto Date: Tue, 7 Oct 2025 22:25:38 +0300 Subject: [PATCH] move trile editor to new rendering task model as well --- src/editor/trile_editor.jai | 85 ++------------------------------- src/rendering/backend.jai | 13 +++++ src/rendering/backend_sokol.jai | 56 +++++++++++++++++++++- src/rendering/helpers.jai | 22 +++++++-- src/rendering/tasks.jai | 17 +++++++ 5 files changed, 107 insertions(+), 86 deletions(-) diff --git a/src/editor/trile_editor.jai b/src/editor/trile_editor.jai index 29bd1f3..39e18e3 100644 --- a/src/editor/trile_editor.jai +++ b/src/editor/trile_editor.jai @@ -254,87 +254,10 @@ draw_material_tab :: (theme: *GR.Overall_Theme, area: GR.Rect) { draw_trile_editor :: () { - - draw_trile(); -} - -draw_trile :: () { - return; // @ToDo: Implement this using the new drawing commands. - if is_in_reflection_pass then return; // We don't want to double update the buffer. - - if editor_current_trile == null then return; - cam := get_trile_editor_camera(); - - mvp := create_viewproj(*cam); - vs_params : Vs_Params; - vs_params.mvp = mvp.floats; - vs_params.camera = cam.position.component; - - world_conf : Trixel_World_Config; - wc : *World_Config = *(World_Config.{}); - world_config_to_shader_type(wc, *world_conf); - - trixels : [4096]Position_Color; - - min_distance : float = 999.0; - - - ray := get_mouse_ray(*cam); - - hovered_trixel_x = -1; - hovered_trixel_y = -1; - hovered_trixel_z = -1; - - for x: 0..15 { - for y: 0..15 { - for z: 0..15 { - if editor_current_trile.trixels[x][y][z].empty then continue; - hit := does_ray_hit_cube(ray, .{ .{x * TRIXEL_SIZE, y * TRIXEL_SIZE, z * TRIXEL_SIZE}, .{TRIXEL_SIZE, TRIXEL_SIZE, TRIXEL_SIZE}}); - if hit.hit && hit.distance < min_distance { - hovered_trixel_x = x; - hovered_trixel_y = y; - hovered_trixel_z = z; - min_distance = hit.distance; - } - } - } - } - - trixel_count : s32 = 0; - - for x: 0..15 { - for y: 0..15 { - for z: 0..15 { - if editor_current_trile.trixels[x][y][z].empty then continue; - - trixels[trixel_count].pos.x = x * (1.0 / 16.0) + TRIXEL_SIZE_HALF; - trixels[trixel_count].pos.y = y * (1.0 / 16.0) + TRIXEL_SIZE_HALF; - trixels[trixel_count].pos.z = z * (1.0 / 16.0) + TRIXEL_SIZE_HALF; - trixels[trixel_count].pos.w = 1.0; - trixel_color := editor_current_trile.trixels[x][y][z].material.color; - - if hovered_trixel_x == x && - hovered_trixel_y == y && - hovered_trixel_z == z { - trixel_color = .{1.0, 0.0, 0.0}; - } - - trixels[trixel_count].col = .{trixel_color.x, trixel_color.y, trixel_color.z, material_encode_to_float(editor_current_trile.trixels[x][y][z].material)}; - trixel_count += 1; - } - } - } - - sg_update_buffer(gPipelines.trixel.bind.vertex_buffers[2], *(sg_range.{ - ptr = trixels.data, - size = size_of(type_of(trixels)), - })); - - sg_apply_pipeline(gPipelines.trixel.pipeline); - sg_apply_bindings(*gPipelines.trixel.bind); - sg_apply_uniforms(UB_vs_params, *(sg_range.{ ptr = *vs_params, size = size_of(type_of(vs_params)) })); - sg_apply_uniforms(UB_trixel_world_config, *(sg_range.{ptr = *world_conf, size = size_of(type_of(world_conf))})); - sg_draw(0, 36, trixel_count); + create_set_cam_rendering_task(get_trile_editor_camera()); + w := New(World,, temp); + create_sky_rendering_task(*w.conf); + create_trixel_rendering_task(editor_current_trile); } trile_editor_shortcuts :: () { diff --git a/src/rendering/backend.jai b/src/rendering/backend.jai index d0041d0..d602498 100644 --- a/src/rendering/backend.jai +++ b/src/rendering/backend.jai @@ -18,6 +18,8 @@ Render_Command_Type :: enum { GENERATE_GROUND; ADD_TRILE_POSITIONS; DRAW_TRILE_POSITIONS; + UPDATE_TRIXELS; + DRAW_TRIXELS; } Render_Command :: struct { @@ -44,6 +46,17 @@ Render_Command_Draw_Trile_Positions :: struct { conf : *World_Config; } +Render_Command_Update_Trixels :: struct { + #as using c : Render_Command; + c.type = .UPDATE_TRIXELS; + trile : *Trile; +} + +Render_Command_Draw_Trixels :: struct { + #as using c : Render_Command; + c.type = .DRAW_TRIXELS; +} + Render_Command_Draw_Ground :: struct { #as using c : Render_Command; c.type = .DRAW_GROUND; diff --git a/src/rendering/backend_sokol.jai b/src/rendering/backend_sokol.jai index c740226..f045b63 100644 --- a/src/rendering/backend_sokol.jai +++ b/src/rendering/backend_sokol.jai @@ -1,5 +1,7 @@ camera: Camera; +trixel_count : s32 = 0; + trile_offsets : [..]s32; current_trile_offset_index : s32 = 0; @@ -23,10 +25,62 @@ backend_handle_command :: (cmd: *Render_Command) { case .DRAW_GROUND; ground_command := cast(*Render_Command_Draw_Ground)cmd; backend_draw_ground(ground_command.worldConfig); - + case .UPDATE_TRIXELS; + trixel_update_command := cast(*Render_Command_Update_Trixels)cmd; + backend_update_trixels(trixel_update_command.trile); + case .DRAW_TRIXELS; + backend_draw_trixels(); } } +backend_update_trixels :: (trile: *Trile) { + if trile == null then return; + + trixels : [4096]Position_Color; + trixel_count = 0; + + for x: 0..15 { + for y: 0..15 { + for z: 0..15 { + if trile.trixels[x][y][z].empty then continue; + + trixels[trixel_count].pos.x = x * (1.0 / 16.0) + TRIXEL_SIZE_HALF; + trixels[trixel_count].pos.y = y * (1.0 / 16.0) + TRIXEL_SIZE_HALF; + trixels[trixel_count].pos.z = z * (1.0 / 16.0) + TRIXEL_SIZE_HALF; + trixels[trixel_count].pos.w = 1.0; + trixel_color := trile.trixels[x][y][z].material.color; + + trixels[trixel_count].col = .{trixel_color.x, trixel_color.y, trixel_color.z, material_encode_to_float(trile.trixels[x][y][z].material)}; + trixel_count += 1; + } + } + } + + sg_update_buffer(gPipelines.trixel.bind.vertex_buffers[2], *(sg_range.{ + ptr = trixels.data, + size = size_of(type_of(trixels)), + })); +} + +backend_draw_trixels :: () { + if trixel_count == 0 then return; + + mvp := create_viewproj(*camera); + vs_params : Vs_Params; + vs_params.mvp = mvp.floats; + vs_params.camera = camera.position.component; + + world_conf : Trixel_World_Config; + wc : *World_Config = *(World_Config.{}); + world_config_to_shader_type(wc, *world_conf); + + sg_apply_pipeline(gPipelines.trixel.pipeline); + sg_apply_bindings(*gPipelines.trixel.bind); + sg_apply_uniforms(UB_vs_params, *(sg_range.{ ptr = *vs_params, size = size_of(type_of(vs_params)) })); + sg_apply_uniforms(UB_trixel_world_config, *(sg_range.{ptr = *world_conf, size = size_of(type_of(world_conf))})); + sg_draw(0, 36, trixel_count); +} + backend_add_trile_positions :: (positions : []Vector4) { offset := sg_append_buffer(gPipelines.trile.bind.vertex_buffers[3], *(sg_range.{ ptr = positions.data, diff --git a/src/rendering/helpers.jai b/src/rendering/helpers.jai index 6a40759..226b17c 100644 --- a/src/rendering/helpers.jai +++ b/src/rendering/helpers.jai @@ -1,8 +1,6 @@ create_world_rendering_tasks :: (world: *World) { - skytask := Rendering_Task_Sky.{type = .SKY, worldConfig = *world.conf}; - add_rendering_task(skytask); - groundtask := Rendering_Task_Ground.{type = .GROUND, world = world}; - add_rendering_task(groundtask); + create_sky_rendering_task(*world.conf); + create_ground_rendering_task(world); for world.positions { triletask := Rendering_Task_Trile.{}; triletask.trile = it.trileName; @@ -12,6 +10,22 @@ create_world_rendering_tasks :: (world: *World) { } } +create_sky_rendering_task :: (conf: *World_Config) { + skytask := Rendering_Task_Sky.{type = .SKY, worldConfig = conf}; + add_rendering_task(skytask); +} + +create_trixel_rendering_task :: (trile: *Trile) { + trixeltask := Rendering_Task_Trixels.{type = .TRIXELS, trile = trile}; + add_rendering_task(trixeltask); + +} + +create_ground_rendering_task :: (world: *World) { + groundtask := Rendering_Task_Ground.{type = .GROUND, world = world}; + add_rendering_task(groundtask); +} + create_set_cam_rendering_task :: (cam: Camera) { camtask := Rendering_Task_Set_Camera.{type = .SET_CAMERA, camera = cam}; add_rendering_task(camtask); diff --git a/src/rendering/tasks.jai b/src/rendering/tasks.jai index 8420bf3..57c1550 100644 --- a/src/rendering/tasks.jai +++ b/src/rendering/tasks.jai @@ -6,6 +6,7 @@ Rendering_Task_Type :: enum { SKY; SET_CAMERA; TRILE; // not implemented yet + TRIXELS; SPRITE; // not implemented yet PARTICLES; // not implemented yet }; @@ -39,6 +40,12 @@ Rendering_Task_Trile :: struct { worldConf : *World_Config; } +Rendering_Task_Trixels :: struct { + #as using t : Rendering_Task; + t.type = .TRIXELS; + trile : *Trile; +} + Rendering_Task_Set_Camera :: struct { #as using t : Rendering_Task; camera : Camera; @@ -59,6 +66,16 @@ rendering_tasklist : [..]*Rendering_Task; tasks_to_commands :: () { for rendering_tasklist { if it.type == { + case .TRIXELS; + trixelsTask := (cast(*Rendering_Task_Trixels)it); + updateCommand := New(Render_Command_Update_Trixels,, temp); + updateCommand.trile = trixelsTask.trile; + array_add(*render_command_buckets.setup, updateCommand); + drawCommand := New(Render_Command_Draw_Trixels,, temp); + array_add(*render_command_buckets.main, drawCommand); + // Currently no need to draw trixels anywhere but the editor so no + // need for drawing them in the planar reflection pass... + // array_add(*render_command_buckets.reflection, drawCommand); case .TRILE; trileTask := (cast(*Rendering_Task_Trile)it); addPositionsCmd := New(Render_Command_Add_Trile_Positions,, temp);