perf(gargantua): 渲染按 64 像素分块并每块让出 CPU

原实现一次连续算 4 行(640 像素,单像素最坏 100 步),长时间不让出,单核下输入/合成全卡。改为 64 像素一块,每块后 process.yield_cpu();每 4 行仍 root.redraw() 保持渐进刷新节奏;事件轮询从每 4 行一次提升到每 64 像素一次。单次占用降到约 1/10,逐像素结果不变(仅调度顺序变化)。
This commit is contained in:
TermiNexus committed 2026-10-17 13:20:22 +08:00
1 parent 850c4b8adc
commit b405973e91
1 file changed
+19 -17
+19 -17
View File
@@ -19,7 +19,8 @@ define DISK_OUT = 11.0
define CAM_DIST = 22.0
define CAM_ELEV = 9.0
define FOVF = 0.55
define YIELD_ROWS = 4
define SLICE_PIXELS = 64
define FLUSH_ROWS = 4
define V3_POOL_SIZE = 8192
define V3_POOL_BYTES = V3_POOL_SIZE * 24 + 512
@@ -249,6 +250,8 @@ export def _start() -> int:
gT: double = 0.0
rendered: int = 0
rx: int = 0
ry: int = 0
evt: window.InputEvent
while window.is_active(root.win_id):
@@ -263,22 +266,21 @@ export def _start() -> int:
canvas.set_fb(fb, new_w, new_h)
if rendered == 0:
y: int = 0
while y < GH:
for x in range(GW):
pixel: uint32 = shade_pixel(x, y, gT)
fb[y * GW + x] = pixel
y += 1
if y % YIELD_ROWS == 0:
root.redraw()
process.yield_cpu()
result2: int = window.poll_event(&evt)
if result2 != 0:
if evt.type == window.EVENT_TYPE_PING:
window.flush(root.win_id)
root.redraw()
rendered = 1
gT += 50.0
# 固定像素预算切片,保证单次占用时间片有上界
budget: int = SLICE_PIXELS
while budget > 0 and ry < GH:
fb[ry * GW + rx] = shade_pixel(rx, ry, gT)
rx += 1
budget -= 1
if rx >= GW:
rx = 0
ry += 1
if ry % FLUSH_ROWS == 0:
root.redraw()
if ry >= GH:
root.redraw()
rendered = 1
gT += 50.0
process.yield_cpu()