Repaint only what changed in a retained frame
A frame whose paint differed from the last one only in a small window, such as a practice playing over a page, was painted whole. EndFrameDamaged compares the frame's paint with the last frame's command by command and reports the area the differing commands cover. A raylib host keeps its previous frame's pixels outside that area with BeginRetainedDamage and repaints only the commands touching it with PaintFlushArea. Presenting a retained frame now clears the screen first, sparing tiling GPUs a load.
This commit is contained in:
1 parent
42301bba5b
commit
89810768a5
5 files changed
+363
No files matched your search
@@ -142,6 +142,9 @@ HostRaylibNormal :: (x: float32, y: float32, z: float32) #foreign raylib "rlNorm
|
||||
HostRaylibScale :: (x: float32, y: float32, z: float32) #foreign raylib "rlScalef";
|
||||
HostRaylibPushMatrix :: () #foreign raylib "rlPushMatrix";
|
||||
HostRaylibPopMatrix :: () #foreign raylib "rlPopMatrix";
|
||||
HostRaylibLoadIdentity :: () #foreign raylib "rlLoadIdentity";
|
||||
HostRaylibDisableBlend :: () #foreign raylib "rlDisableColorBlend";
|
||||
HostRaylibEnableBlend :: () #foreign raylib "rlEnableColorBlend";
|
||||
HostRaylibMultMatrix :: (matrix: *float32) #foreign raylib "rlMultMatrixf";
|
||||
HostRaylibParseFloat :: (value: *u8) -> float64 #foreign c "atof";
|
||||
HostRaylibTakeScreenshot :: (path: *u8) #foreign raylib "TakeScreenshot";
|
||||
@@ -2088,6 +2091,9 @@ BeginRetainedFrame :: (width: s32, height: s32) -> bool {
|
||||
}
|
||||
|
||||
RaylibPresentTexture :: (texture: Texture2D) {
|
||||
// The texture covers the screen. Clearing first spares a tiling GPU
|
||||
// from loading the old screen contents before drawing over them.
|
||||
HostRaylibClearBackground(Color.{0, 0, 0, 255})
|
||||
source := Rectangle.{0.0, 0.0, cast(float32)texture.width,
|
||||
-cast(float32)texture.height}
|
||||
destination := Rectangle.{0.0, 0.0,
|
||||
@@ -2125,6 +2131,63 @@ EndRetainedFrameReusePrevious :: () -> bool {
|
||||
return true
|
||||
}
|
||||
|
||||
retained_damage_clip: RasterClipState;
|
||||
retained_damage_active: bool;
|
||||
|
||||
// Copies part of the previous complete frame, in screen pixels, to the same
|
||||
// place in the frame being rendered. Render targets are stored bottom up.
|
||||
RaylibKeepPrevious :: (texture: Texture2D, x: float32, y: float32, width: float32, height: float32) {
|
||||
if width <= 0.0 || height <= 0.0 { return }
|
||||
source := Rectangle.{x, cast(float32)texture.height - y - height, width, -height}
|
||||
HostRaylibDrawTexture(texture, source, Rectangle.{x, y, width, height}, Vector2.{}, 0.0,
|
||||
Color.{255, 255, 255, 255})
|
||||
}
|
||||
|
||||
// Repaints only `area`, in layout units, of the frame being rendered: the
|
||||
// rest is copied unchanged from the previous complete frame, and paint is
|
||||
// clipped to the area until EndRetainedDamage. False when there is no
|
||||
// previous frame to keep; the host then repaints everything.
|
||||
#program_export
|
||||
BeginRetainedDamage :: (area: Rectangle) -> bool {
|
||||
if !retained_frame_active || retained_damage_active || retained_frame_previous_index < 0 ||
|
||||
retained_frame_previous_index == retained_frame_render_index { return false }
|
||||
previous := retained_frame_targets[retained_frame_previous_index].texture
|
||||
width := cast(float32)retained_frame_width
|
||||
height := cast(float32)retained_frame_height
|
||||
screen := ClipScreenBounds(area)
|
||||
left := screen.x
|
||||
top := screen.y
|
||||
right := screen.x + screen.width
|
||||
bottom := screen.y + screen.height
|
||||
if left < 0.0 { left = 0.0 }
|
||||
if top < 0.0 { top = 0.0 }
|
||||
if right > width { right = width }
|
||||
if bottom > height { bottom = height }
|
||||
if right < left { right = left }
|
||||
if bottom < top { bottom = top }
|
||||
HostRaylibFlushBatch()
|
||||
HostRaylibDisableBlend()
|
||||
HostRaylibPushMatrix()
|
||||
HostRaylibLoadIdentity()
|
||||
RaylibKeepPrevious(previous, 0.0, 0.0, width, top)
|
||||
RaylibKeepPrevious(previous, 0.0, bottom, width, height - bottom)
|
||||
RaylibKeepPrevious(previous, 0.0, top, left, bottom - top)
|
||||
RaylibKeepPrevious(previous, right, top, width - right, bottom - top)
|
||||
HostRaylibPopMatrix()
|
||||
HostRaylibFlushBatch()
|
||||
HostRaylibEnableBlend()
|
||||
retained_damage_clip = BeginRasterClip(area)
|
||||
retained_damage_active = true
|
||||
return true
|
||||
}
|
||||
|
||||
#program_export
|
||||
EndRetainedDamage :: () {
|
||||
if !retained_damage_active { return }
|
||||
EndRasterClip(retained_damage_clip)
|
||||
retained_damage_active = false
|
||||
}
|
||||
|
||||
#program_export
|
||||
RetainedPreviousFrameTexture :: () -> Texture2D {
|
||||
if retained_frame_previous_index < 0 { return Texture2D.{} }
|
||||
|
||||
@@ -1335,6 +1335,128 @@ PaintPlanUnchanged :: (session: Session) -> bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// The area a builtin command may cover, in layout units and widened for
|
||||
// anti-aliased edges, or false when only repainting everything is safe.
|
||||
// Text is bounded without measuring: no glyph is wider than its font size
|
||||
// per byte, nor a line taller than twice the font size.
|
||||
PaintCommandArea :: (command: PaintCommand, area: *Rectangle) -> bool {
|
||||
margin: float32 = 2.0 + command.line_width
|
||||
kind := command.kind
|
||||
if kind == PaintKindClippedText || kind == PaintKindImage ||
|
||||
(kind == PaintKindText && (command.italic || command.emphasis)) {
|
||||
// These draw only inside their clip.
|
||||
area.* = Rectangle.{command.clip.x - margin, command.clip.y - margin,
|
||||
command.clip.width + margin * 2.0, command.clip.height + margin * 2.0}
|
||||
return command.clip.width > 0.0 && command.clip.height > 0.0
|
||||
}
|
||||
if kind == PaintKindText {
|
||||
lines: s32 = 1
|
||||
for byte: command.value { if byte == #char "\n" { lines += 1 } }
|
||||
font := cast(float32)command.font
|
||||
area.* = Rectangle.{cast(float32)command.x - font, cast(float32)command.y - font,
|
||||
cast(float32)command.value.count * font + font * 2.0, cast(float32)lines * font * 2.0 + font * 2.0}
|
||||
return true
|
||||
}
|
||||
if kind < PaintKindLine || kind > PaintKindShapeTriangle { return false }
|
||||
bounds := command.bounds
|
||||
if bounds.width < 0.0 { bounds.x += bounds.width; bounds.width = -bounds.width }
|
||||
if bounds.height < 0.0 { bounds.y += bounds.height; bounds.height = -bounds.height }
|
||||
area.* = Rectangle.{bounds.x - margin, bounds.y - margin,
|
||||
bounds.width + margin * 2.0, bounds.height + margin * 2.0}
|
||||
if command.clip_set {
|
||||
area.* = RectangleIntersection(area.*, Rectangle.{command.clip.x - margin, command.clip.y - margin,
|
||||
command.clip.width + margin * 2.0, command.clip.height + margin * 2.0})
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
PaintAreaJoin :: (area: *Rectangle, joined: *bool, next: Rectangle) {
|
||||
if next.width <= 0.0 || next.height <= 0.0 { return }
|
||||
if !joined.* {
|
||||
area.* = next
|
||||
joined.* = true
|
||||
return
|
||||
}
|
||||
left := ifx next.x < area.x then next.x else area.x
|
||||
top := ifx next.y < area.y then next.y else area.y
|
||||
right := ifx next.x + next.width > area.x + area.width then next.x + next.width else area.x + area.width
|
||||
bottom := ifx next.y + next.height > area.y + area.height then next.y + next.height else area.y + area.height
|
||||
area.* = Rectangle.{left, top, right - left, bottom - top}
|
||||
}
|
||||
|
||||
PaintFrameDamage :: struct {
|
||||
unchanged: bool
|
||||
// Only `area` differs from the previous frame's paint.
|
||||
partial: bool
|
||||
area: Rectangle
|
||||
}
|
||||
|
||||
// Compares this frame's paint with the previous frame's, command by command,
|
||||
// and joins the areas of the commands that differ, as they were and as they
|
||||
// are now. A command painted by its own emitter makes the whole frame differ.
|
||||
PaintDamage :: (session: Session) -> PaintFrameDamage {
|
||||
result: PaintFrameDamage
|
||||
if !SessionValid(session) { return result }
|
||||
slot: s32 = SessionIndex(session)
|
||||
if !paint_snapshots_valid[slot] { return result }
|
||||
count := pending_paint[slot].items.count
|
||||
previous := paint_snapshots[slot].count
|
||||
total := ifx count > previous then count else previous
|
||||
joined: bool
|
||||
area: Rectangle
|
||||
for index: cast(s64)0..total - 1 {
|
||||
if index < count && !pending_paint[slot].items[index].builtin_emitter { return result }
|
||||
if index < previous && !paint_snapshots[slot][index].builtin_emitter { return result }
|
||||
current: PaintCommand
|
||||
if index < count { current = PendingPaintAt(session, cast(s32)index) }
|
||||
if index < count && index < previous &&
|
||||
pending_paint[slot].items[index].has_emitter == paint_snapshots[slot][index].has_emitter &&
|
||||
PaintCommandEqual(slot, current, paint_snapshots[slot][index]) { continue }
|
||||
box: Rectangle
|
||||
if index < count {
|
||||
if !PaintCommandArea(current, *box) { return result }
|
||||
PaintAreaJoin(*area, *joined, box)
|
||||
}
|
||||
if index < previous {
|
||||
kept := paint_snapshots[slot][index]
|
||||
before := kept.data
|
||||
before.value = SnapshotTextRead(slot, true, kept.value_start, kept.value_count)
|
||||
if before.value.count != kept.value_count || !PaintCommandArea(before, *box) { return result }
|
||||
PaintAreaJoin(*area, *joined, box)
|
||||
}
|
||||
if !joined { return result }
|
||||
}
|
||||
result.unchanged = !joined
|
||||
result.partial = joined
|
||||
result.area = area
|
||||
return result
|
||||
}
|
||||
|
||||
// Emits only the commands that may touch `area`, for a host that kept the
|
||||
// previous frame's pixels everywhere else and clips this paint to the area.
|
||||
#program_export
|
||||
PaintFlushArea :: (session: Session, area: Rectangle) {
|
||||
if PaintOverflow(session) || !PaintOrder(session) { PaintClear(session); return }
|
||||
index: s32 = 0
|
||||
while cast(s64)index < pending_paint[SessionIndex(session)].items.count {
|
||||
slot := SessionIndex(session)
|
||||
queued := *pending_paint[slot].items[index]
|
||||
if queued.has_emitter {
|
||||
emit: PaintEmitter = queued.emit
|
||||
data := PendingPaintAt(session, index)
|
||||
box: Rectangle
|
||||
touched := !queued.builtin_emitter || !PaintCommandArea(data, *box)
|
||||
if !touched {
|
||||
overlap := RectangleIntersection(box, area)
|
||||
touched = overlap.width > 0.0 && overlap.height > 0.0
|
||||
}
|
||||
if touched { emit(data) }
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
PaintClear(session)
|
||||
}
|
||||
|
||||
#program_export
|
||||
FramePaintUnchanged :: (session: Session) -> bool {
|
||||
if !SessionValid(session) { return false }
|
||||
|
||||
@@ -103,6 +103,9 @@ FrameEndResult :: struct {
|
||||
status: FrameStatus
|
||||
paint_unchanged: bool
|
||||
paint_count: s32
|
||||
// EndFrameDamaged: only `damage` changed and its paint waits for the host.
|
||||
damage_partial: bool
|
||||
damage: Rectangle
|
||||
}
|
||||
|
||||
#program_export
|
||||
@@ -170,6 +173,40 @@ EndFrameRetained :: (session: Session, reuse: bool,
|
||||
return result
|
||||
}
|
||||
|
||||
// Ends a frame for a host that keeps its previous frame's pixels and can
|
||||
// repaint part of them. An unchanged frame emits nothing, as with
|
||||
// EndFrameRetained. When only some paint differs, damage_partial is set,
|
||||
// damage is the area it covers in layout units, and nothing is emitted yet:
|
||||
// the host keeps its previous pixels outside that area and calls
|
||||
// PaintFlushArea, or PaintFlush to repaint everything. Any other frame is
|
||||
// emitted whole.
|
||||
#program_export
|
||||
EndFrameDamaged :: (session: Session) -> FrameEndResult {
|
||||
if SessionValid(session) && frame_retained[SessionIndex(session)] { return EndFrameRetained(session, true) }
|
||||
result: FrameEndResult
|
||||
result.status = CommitFrame(session)
|
||||
if result.status != cast(FrameStatus)FrameOk {
|
||||
PaintSnapshotInvalidate(session)
|
||||
return result
|
||||
}
|
||||
slot: s32 = SessionIndex(session)
|
||||
result.paint_count = PendingPaintCount(session)
|
||||
damage := PaintDamage(session)
|
||||
stored: bool = damage.unchanged
|
||||
if !damage.unchanged { stored = PaintSnapshotStore(session) }
|
||||
result.paint_unchanged = damage.unchanged && stored
|
||||
frame_paint_unchanged[slot] = result.paint_unchanged
|
||||
if result.paint_unchanged {
|
||||
PaintClear(session)
|
||||
} else if damage.partial && stored {
|
||||
result.damage_partial = true
|
||||
result.damage = damage.area
|
||||
} else {
|
||||
PaintFlush(session)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
#program_export
|
||||
CloseSession :: (session: Session) -> bool {
|
||||
if !SessionValid(session) { return false }
|
||||
|
||||
Executable
+74
@@ -0,0 +1,74 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
unset DISPLAY WAYLAND_DISPLAY XAUTHORITY DBUS_SESSION_BUS_ADDRESS SESSION_MANAGER
|
||||
export YUE_DESKTOP_RECOVERY=0
|
||||
|
||||
repo=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
. "$repo/tests/toolchain.sh"
|
||||
include=${ZIRAN_INCLUDE:-"$ziran_root/include"}
|
||||
std="${include%/include}/std"
|
||||
work=$(mktemp -d)
|
||||
trap 'rm -rf "$work"' EXIT HUP INT TERM
|
||||
source=$repo/tests/ziran_frame_damage_test.zi
|
||||
|
||||
set -- \
|
||||
--bind raster:RasterLine=ziran_frame_retained_host:RasterLine \
|
||||
--bind raster_text:RasterText=ziran_frame_retained_host:RasterText \
|
||||
--bind raster_text:RasterTextClipped=ziran_frame_retained_host:RasterTextClipped \
|
||||
--bind paint_queue:RasterImage=ziran_frame_retained_host:RasterImage \
|
||||
--bind paint_queue:RasterImageMesh=ziran_frame_retained_host:RasterImageMesh
|
||||
|
||||
"$ziran" ir --root "$repo/tests" --module-path "$repo/src/ui" \
|
||||
--module-path "$std" -o "$work/ir" "$source"
|
||||
"$ziran" bundle --root "$repo/tests" --module-path "$repo/src/ui" \
|
||||
--module-path "$std" "$@" \
|
||||
--entry ziran_frame_damage_test:main -o "$work/source.zib" "$source"
|
||||
"$ziran" bundle --root "$work/ir" --module-path "$work/ir" "$@" \
|
||||
--entry ziran_frame_damage_test:main -o "$work/saved.zib" \
|
||||
"$work/ir/ziran_frame_damage_test.zir"
|
||||
cmp "$work/source.zib" "$work/saved.zib"
|
||||
test "$("$ziran" run "$work/source.zib")" = 0
|
||||
test "$("$ziran" run "$work/saved.zib")" = 0
|
||||
|
||||
for input in source saved; do
|
||||
if test "$input" = source; then
|
||||
module=$source
|
||||
module_root=$repo/tests
|
||||
module_dir=$repo/src/ui
|
||||
else
|
||||
module=$work/ir/ziran_frame_damage_test.zir
|
||||
module_root=$work/ir
|
||||
module_dir=$work/ir
|
||||
fi
|
||||
for target in c cpp rust py; do
|
||||
output=$work/$target-$input
|
||||
executable=--exe
|
||||
if test "$target" = cpp; then executable=; fi
|
||||
build_target() {
|
||||
"$ziran" build --target="$target" \
|
||||
--entry ziran_frame_damage_test:main $executable "$@" \
|
||||
--root "$module_root" --module-path "$module_dir" \
|
||||
--module-path "$std" -o "$output" "$module"
|
||||
}
|
||||
case "$target" in
|
||||
c|cpp) build_target;;
|
||||
*) build_target "$@";;
|
||||
esac
|
||||
if test "$target" = cpp; then
|
||||
"${CXX:-c++}" -std=c++17 -I"$include" "$output"/*.cpp \
|
||||
-o "$output/ziran_frame_damage_test"
|
||||
fi
|
||||
case "$target" in
|
||||
rust) CARGO_TARGET_DIR="$work/rust-$input" cargo run --offline --quiet \
|
||||
--manifest-path "$output/Cargo.toml";;
|
||||
py) python3 "$output/__main__.py";;
|
||||
*) "$output/ziran_frame_damage_test";;
|
||||
esac
|
||||
done
|
||||
output=$work/go-$input
|
||||
"$ziran" build --target=go --pkg main --exe \
|
||||
--entry ziran_frame_damage_test:main "$@" \
|
||||
--root "$module_root" --module-path "$module_dir" \
|
||||
--module-path "$std" -o "$output" "$module"
|
||||
env -u DISPLAY -u WAYLAND_DISPLAY GO111MODULE=off go run "$output"/*.go
|
||||
done
|
||||
@@ -0,0 +1,67 @@
|
||||
#import "drawing_props"
|
||||
#import "geometry"
|
||||
#import "paint_queue"
|
||||
#import "session"
|
||||
#import "tree"
|
||||
#import "tree_draw"
|
||||
#import "ziran_frame_retained_host"
|
||||
using FrameStatus;
|
||||
|
||||
custom_calls: s32;
|
||||
|
||||
Custom :: (command: PaintCommand) {
|
||||
unused command
|
||||
custom_calls += 1
|
||||
}
|
||||
|
||||
// Two still lines, one that moves and a label, as a page with a playing
|
||||
// practice window over it.
|
||||
Frame :: (session: Session, moving: float32, label: string, custom: bool = false) -> FrameEndResult {
|
||||
unused BeginFrame(session, cast(u64)23, Rectangle.{0.0, 0.0, 400.0, 300.0})
|
||||
PaintLine(session, -1, Rectangle.{10.0, 10.0, 100.0, 1.0}, Color.{0, 0, 0, 255})
|
||||
PaintLine(session, -1, Rectangle.{10.0, 200.0, 100.0, 1.0}, Color.{0, 0, 0, 255})
|
||||
PaintLine(session, -1, Rectangle.{moving, 100.0, 20.0, 1.0}, Color.{0, 0, 0, 255})
|
||||
PaintLabel(session, -1, label, 300, 50, 12, Color.{0, 0, 0, 255})
|
||||
if custom {
|
||||
command: QueuedPaint
|
||||
command.emit = Custom
|
||||
command.has_emitter = true
|
||||
SubmitPaint(session, command)
|
||||
}
|
||||
return EndFrameDamaged(session)
|
||||
}
|
||||
|
||||
#program_export
|
||||
main :: () -> s32 {
|
||||
session := SessionOpen()
|
||||
if !SessionValid(session) { return 1 }
|
||||
// The first frame has nothing to keep and is painted whole.
|
||||
result := Frame(session, 50.0, "a")
|
||||
if result.status != cast(FrameStatus)FrameOk || result.damage_partial || line_draws != 3 || text_draws != 1 { return 2 }
|
||||
// An identical frame paints nothing.
|
||||
result = Frame(session, 50.0, "a")
|
||||
if !result.paint_unchanged || result.damage_partial || line_draws != 3 || text_draws != 1 { return 3 }
|
||||
// A moved line damages where it was and where it is, and nothing else.
|
||||
result = Frame(session, 60.0, "a")
|
||||
damage := result.damage
|
||||
if !result.damage_partial || result.paint_unchanged || line_draws != 3 ||
|
||||
damage.x > 50.0 || damage.x + damage.width < 80.0 || damage.y > 100.0 || damage.y + damage.height < 101.0 ||
|
||||
damage.y < 90.0 || damage.y + damage.height > 110.0 || damage.x < 40.0 || damage.x + damage.width > 90.0 { return 4 }
|
||||
PaintFlushArea(session, damage)
|
||||
if line_draws != 4 || text_draws != 1 || failed { return 5 }
|
||||
// A changed label repaints only around itself.
|
||||
result = Frame(session, 60.0, "bb")
|
||||
if !result.damage_partial || result.damage.x > 300.0 || result.damage.y > 50.0 { return 6 }
|
||||
PaintFlushArea(session, result.damage)
|
||||
if line_draws != 4 || text_draws != 2 { return 7 }
|
||||
// A host that cannot keep its pixels repaints everything instead.
|
||||
result = Frame(session, 70.0, "bb")
|
||||
if !result.damage_partial { return 8 }
|
||||
PaintFlush(session)
|
||||
if line_draws != 7 || text_draws != 3 { return 9 }
|
||||
// Paint with its own emitter may draw anywhere: the frame is painted whole.
|
||||
result = Frame(session, 70.0, "bb", true)
|
||||
if result.damage_partial || result.paint_unchanged || line_draws != 10 || text_draws != 4 || custom_calls != 1 { return 10 }
|
||||
unused CloseSession(session)
|
||||
return 0
|
||||
}
|
||||
Reference in new issue
Block a user