peih's picture
backup native-eiffel
5dd27d0 verified
Raw History Blame Contribute Delete
6.58 kB
"""Offline checks of the browser-free capture through the real GameCaptureWorker.
1. Lockstep against the browser renderer: identical per-frame keyboard and
pointer input, game state compared on every frame.
2. Chunk contract: frame counts, condition frames as CUDA uint8, game-window
frames on the host, HUD events carrying their prompts, probe().
3. Pacing: chunks paced to EDEN_CHUNK_PERIOD_MS with the late burst, wall time
per chunk and burst length; a tap pressed and released while waiting must
still reach the game.
"""
import json
import os
import sys
import time
from pathlib import Path
import numpy as np
import torch
EDEN = Path('/root/autodl-tmp/edenlive')
sys.path.insert(0, str(EDEN))
os.environ['EDEN_NATIVE_CAPTURE'] = '1'
from eden.apps.game2v import game_renderer, session
from eden.apps.game2v.game_renderer import GameCaptureWorker, GameRenderer
from eden.apps.game2v.game_timeline import GameTimeline
from capture_compat import install_capture_context
import native_capture
install_capture_context(game_renderer)
native_capture.install(game_renderer, session)
_launch = GameRenderer._gpu_launch_options
GameRenderer._gpu_launch_options = lambda r: {**_launch(r), 'executable_path': os.environ['GBDATA_CHROMIUM']}
SLUG = 'eiffel-assault'
report = {}
def script(i):
"""Per-frame input: start, walk, turn with the mouse, fire, aim."""
events = []
if i == 3:
events += [{'action': 'enter', 'phase': 'down'}]
if i == 4:
events += [{'action': 'enter', 'phase': 'up'}]
if i in (30, 90):
events.append({'action': 'w', 'phase': 'down'})
if i in (70, 130):
events.append({'action': 'w', 'phase': 'up'})
if 40 <= i < 160 and i % 6 == 0:
events.append({'action': 'pointer', 'phase': 'move', 'x': 0.5 + 0.1 * np.sin(i / 9), 'y': 0.5, 'button': None, 'pressed': None})
if i in (60, 100):
events.append({'action': 'pointer', 'phase': 'button', 'x': 0.5, 'y': 0.5, 'button': 0, 'pressed': True})
if i in (63, 103):
events.append({'action': 'pointer', 'phase': 'button', 'x': 0.5, 'y': 0.5, 'button': 0, 'pressed': False})
if i == 120:
events.append({'action': 'k', 'phase': 'down'})
if i == 140:
events.append({'action': 'k', 'phase': 'up'})
return events
def run(worker, chunks, duration=0.0, feed=None):
tl = GameTimeline(SLUG)
frame = 0
out = []
for c in range(chunks):
chunk = tl.begin_chunk(9 if c == 0 else 12, 24)
def drain():
nonlocal frame
events = script(frame) if feed is None else feed()
frame += 1
return events
t0 = time.perf_counter()
packet = worker.capture(chunk, duration_s=duration, drain_events=drain)
out.append((packet, time.perf_counter() - t0))
return out
# 1. Lockstep: browser renderer vs native renderer, same input per frame.
native_capture_off = dict(os.environ)
os.environ['EDEN_NATIVE_CAPTURE'] = '0'
browser = GameCaptureWorker(SLUG, width=960, height=544)
browser_chunks = run(browser, 15)
browser.close()
os.environ['EDEN_NATIVE_CAPTURE'] = '1'
native = GameCaptureWorker(SLUG, width=960, height=544)
native_chunks = run(native, 15)
assert type(native._renderer).__name__ == 'NativeSceneRenderer', type(native._renderer)
diffs, compared = [], 0
keys = ('x', 'z', 'y', 'yaw', 'pitch')
for (bp, _), (np_, _) in zip(browser_chunks, native_chunks):
for bf, nf in zip(bp.frames, np_.frames):
a, b = bf.ui_state or {}, nf.ui_state or {}
pa, pb = a.get('player') or {}, b.get('player') or {}
d = max([abs(pa.get(k, 0) - pb.get(k, 0)) for k in keys] +
[float(a.get(k) != b.get(k)) for k in ('state', 'shots', 'ads', 'health', 'ammo', 'roundId')])
diffs.append(d)
compared += 1
last_b, last_n = browser_chunks[-1][0].frames[-1].ui_state, native_chunks[-1][0].frames[-1].ui_state
report['lockstep'] = dict(frames=compared, max_state_diff=max(diffs),
browser={k: last_b.get(k) for k in ('state', 'shots', 'ads')} | {'yaw': last_b['player']['yaw']},
native={k: last_n.get(k) for k in ('state', 'shots', 'ads')} | {'yaw': last_n['player']['yaw']})
# 2. Chunk contract.
packet = native_chunks[-1][0]
conditions = [f.condition_frame for f in packet.frames if f.condition_frame is not None]
events = [e for f in packet.frames for e in (f.ui_state or {}).get('events', [])]
report['contract'] = dict(
frames=len(packet.frames), conditions=len(conditions),
condition=[str(conditions[0].device), str(conditions[0].dtype), list(conditions[0].shape)],
ui_frame=[type(packet.frames[0].ui_frame).__name__, str(packet.frames[0].ui_frame.dtype), list(packet.frames[0].ui_frame.shape)],
condition_equals_ui=bool(np.array_equal(conditions[0].cpu().numpy(), packet.frames[0].ui_frame)),
model_input=list(torch.stack(conditions).permute(0, 3, 1, 2).shape),
event_prompts=sorted({(e['type'], e['prompt'] is not None) for e in events})[:6],
probe_state=sorted((native.probe(drain_events=lambda: []).get('state') or {}).keys())[:5])
native.close()
# 3. Pacing with the late burst, and a tap made while the chunk waits. A fresh
# worker: a timeline only moves forward within one renderer.
native = GameCaptureWorker(SLUG, width=960, height=544)
pending = [{'action': 'enter', 'phase': 'down'}, {'action': 'enter', 'phase': 'up'}]
def feed():
out = list(pending)
pending.clear()
return out
period = native_capture.chunk_period_s(12)
timings, ads_seen = [], False
tl = GameTimeline(SLUG)
ends = []
for c in range(24):
chunk = tl.begin_chunk(12, 24)
if c == 8:
# Press and release K well before the burst; both arrive in one batch.
pending += [{'action': 'k', 'phase': 'down'}, {'action': 'k', 'phase': 'up'}]
t0 = time.perf_counter()
out = native.capture(chunk, duration_s=period, drain_events=feed)
ends.append(time.perf_counter())
timings.append(dict(wall=(time.perf_counter() - t0) * 1e3, burst=native._renderer.last_burst_s * 1e3))
if c == 8:
ads_seen = any((f.ui_state or {}).get('ads') for f in out.frames)
native.close()
steady = timings[3:]
gaps = np.diff(ends[3:]) * 1e3
report['pacing'] = dict(period_ms=round(period * 1e3, 1),
chunk_end_interval_ms=dict(mean=round(float(gaps.mean()), 1), min=round(float(gaps.min()), 1), max=round(float(gaps.max()), 1)),
chunk_wall_ms=[round(t['wall'], 1) for t in steady],
burst_ms=[round(t['burst'], 1) for t in steady],
tap_reached_game=ads_seen)
print(json.dumps(report, indent=1, default=str))