#!/usr/bin/env bash # Renders the real client offscreen and saves a PNG, without touching the host display, # GPU or audio. Godot runs in a throwaway Docker container with a private Xvfb and Mesa # software rendering. # # tools/capture.sh [--frames=N] [--allow-blank] # # is a res:// path to a .tscn, or the name of a scenario in # test/capture/ (for example `smoke` means res://test/capture/smoke.tscn). # # Environment: # CAPTURE_SIZE=1280x720 frame size # CAPTURE_RENDERER=forward_plus Vulkan via Lavapipe (default, the shipping renderer), # compatibility for OpenGL via llvmpipe, or no_vulkan: # asks for Vulkan with no Vulkan driver present, and # expects the game to fall back to OpenGL (H2) # # Exits 0 when the PNG was written and isn't blank (pixel variance and colour count), # 1 when the render failed or came out blank, 2 on bad usage. Open the image and look # at it anyway: these checks only catch the obvious failures. set -euo pipefail repo="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" image="tidewall-capture" context="$repo/tools/capture" usage() { sed -n '2,20p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//' >&2 exit 2 } [[ $# -ge 2 ]] || usage target="$1" out="$2" shift 2 extra=() for arg in "$@"; do case "$arg" in --frames=* | --allow-blank) extra+=("$arg") ;; *) echo "capture.sh: unknown option '$arg'" >&2; usage ;; esac done if [[ "$target" == res://* ]]; then scene="$target" else [[ "$target" =~ ^[a-z0-9_]+$ ]] || { echo "capture.sh: bad scenario name '$target'" >&2; exit 2; } scene="res://test/capture/$target.tscn" fi [[ -f "$repo/${scene#res://}" ]] || { echo "capture.sh: no such scene: $scene" >&2; exit 2; } size="${CAPTURE_SIZE:-1280x720}" [[ "$size" =~ ^[0-9]+x[0-9]+$ ]] || { echo "capture.sh: bad CAPTURE_SIZE '$size'" >&2; exit 2; } env_args=() case "${CAPTURE_RENDERER:-forward_plus}" in compatibility) render=(--rendering-method gl_compatibility --rendering-driver opengl3); wanted="gl_compatibility" ;; forward_plus) render=(--rendering-method forward_plus --rendering-driver vulkan); wanted="forward_plus" ;; # The project's own renderer (Forward+ on Vulkan), with no Vulkan driver to find. no_vulkan) render=(); env_args=(-e VK_ICD_FILENAMES=/nonexistent/none.json -e VK_DRIVER_FILES=/nonexistent/none.json); wanted="gl_compatibility" ;; *) echo "capture.sh: CAPTURE_RENDERER must be compatibility, forward_plus or no_vulkan" >&2; exit 2 ;; esac # Rebuild the image only when its recipe changes. recipe_hash="$(cat "$context/Dockerfile" "$context/entrypoint.sh" | sha256sum | cut -c1-16)" current="$(docker image inspect "$image" --format '{{ index .Config.Labels "tidewall.recipe" }}' 2>/dev/null || true)" if [[ "$current" != "$recipe_hash" ]]; then echo "capture.sh: building $image" >&2 docker build --quiet --label "tidewall.recipe=$recipe_hash" -t "$image" "$context" >/dev/null fi # Refresh the global class cache headless on the host, so class_name lookups resolve. "$repo/tools/godot.sh" --import --quit >/dev/null 2>&1 || true # The container can only write inside the repo, so render to a scratch file there. mkdir -p "$repo/captures" # Keep Godot from importing captures as project resources. touch "$repo/captures/.gdignore" scratch="captures/.capture-$$-$RANDOM.png" name="tidewall-capture-$$-$RANDOM" cleanup() { docker rm -f "$name" >/dev/null 2>&1 || true rm -f "$repo/$scratch" } trap cleanup EXIT set +e output="$(timeout --signal=KILL 300 nice -n 15 docker run --rm --name "$name" \ --network none \ --user "$(id -u):$(id -g)" \ --memory 3g --cpus 4 \ -e "CAPTURE_SCREEN=${size}x24" \ "${env_args[@]}" \ -v "$repo:/repo" \ "$image" \ ${render[@]+"${render[@]}"} --resolution "$size" \ --script res://tools/capture.gd -- --scene="$scene" --out="res://$scratch" "${extra[@]}" 2>&1)" code=$? set -e report="$(grep -m1 '^CAPTURE ' <<<"$output" | cut -c9- || true)" if [[ -z "$report" ]]; then echo "capture.sh: render failed (exit $code)" >&2 tail -n 30 <<<"$output" >&2 exit 1 fi if [[ -f "$repo/$scratch" ]]; then mkdir -p "$(dirname "$out")" mv "$repo/$scratch" "$out" fi echo "$report" # Godot falls back to OpenGL when Vulkan fails; unless that was the point, it is not the # requested capture. if ! grep -q "\"renderer\":\"$wanted\"" <<<"$report"; then echo "capture.sh: expected the $wanted renderer, but Godot used another" >&2 grep -iE 'vulkan|error' <<<"$output" | tail -n 10 >&2 || true exit 1 fi if [[ "$code" -ne 0 ]]; then echo "capture.sh: capture of $scene failed or came out blank (exit $code)" >&2 grep -iE 'error|fail' <<<"$output" | grep -v '^CAPTURE ' | tail -n 15 >&2 || true exit 1 fi echo "capture.sh: wrote $out" >&2