## Renders a scene for a number of frames and saves the last one as a PNG. ## Runs inside the tidewall-capture container via tools/capture.sh, never on the host ## display. Prints one JSON line with the image statistics and exits: ## 0 saved and not blank, 1 blank or failed, 2 bad arguments. ## ## --scene=res://...tscn --out=res://captures/x.png [--frames=N] [--allow-blank] extends SceneTree var _scene: String = "" var _out: String = "" var _frames: int = 30 var _allow_blank: bool = false func _initialize() -> void: for arg: String in OS.get_cmdline_user_args(): if arg.begins_with("--scene="): _scene = arg.trim_prefix("--scene=") elif arg.begins_with("--out="): _out = arg.trim_prefix("--out=") elif arg.begins_with("--frames=") and arg.trim_prefix("--frames=").is_valid_int(): _frames = maxi(arg.trim_prefix("--frames=").to_int(), 1) elif arg == "--allow-blank": _allow_blank = true else: printerr("capture: unknown argument '%s'" % arg) quit(2) return if _scene.is_empty() or _out.is_empty(): printerr("capture: --scene and --out are required") quit(2) return if DisplayServer.get_name() == "headless": printerr("capture: no display; run this through tools/capture.sh") quit(2) return _capture.call_deferred() func _capture() -> void: var packed: PackedScene = load(_scene) as PackedScene if packed == null: printerr("capture: can't load scene '%s'" % _scene) quit(1) return root.add_child(packed.instantiate()) for _index: int in _frames: await process_frame await RenderingServer.frame_post_draw var image: Image = root.get_texture().get_image() var stats: Dictionary = ImageStats.measure(image) var blank: bool = ImageStats.is_blank(stats) var error: Error = image.save_png(_out) stats["scene"] = _scene stats["size"] = [image.get_width(), image.get_height()] stats["renderer"] = RenderingServer.get_current_rendering_method() stats["adapter"] = RenderingServer.get_video_adapter_name() stats["blank"] = blank stats["saved"] = error == OK stats["render"] = render_counts() print("CAPTURE " + JSON.stringify(stats)) quit(0 if error == OK and (not blank or _allow_blank) else 1) ## H1: what the last frame cost to draw. "draw_calls", "primitives" and "objects" cover ## every pass (shadows included); "visible_*" only the main camera pass. Memory is in ## bytes. Counts, not timings: software rendering says nothing about real frame rates. func render_counts() -> Dictionary: var counts: Dictionary = { "draw_calls": int(Performance.get_monitor(Performance.RENDER_TOTAL_DRAW_CALLS_IN_FRAME)), "primitives": int(Performance.get_monitor(Performance.RENDER_TOTAL_PRIMITIVES_IN_FRAME)), "objects": int(Performance.get_monitor(Performance.RENDER_TOTAL_OBJECTS_IN_FRAME)), "visible_draw_calls": root.get_render_info(Viewport.RENDER_INFO_TYPE_VISIBLE, Viewport.RENDER_INFO_DRAW_CALLS_IN_FRAME), "visible_primitives": root.get_render_info(Viewport.RENDER_INFO_TYPE_VISIBLE, Viewport.RENDER_INFO_PRIMITIVES_IN_FRAME), "shadow_draw_calls": root.get_render_info(Viewport.RENDER_INFO_TYPE_SHADOW, Viewport.RENDER_INFO_DRAW_CALLS_IN_FRAME), "video_mem_bytes": int(Performance.get_monitor(Performance.RENDER_VIDEO_MEM_USED)), "texture_mem_bytes": int(Performance.get_monitor(Performance.RENDER_TEXTURE_MEM_USED)), "buffer_mem_bytes": int(Performance.get_monitor(Performance.RENDER_BUFFER_MEM_USED)), "static_mem_bytes": OS.get_static_memory_usage(), "nodes": int(Performance.get_monitor(Performance.OBJECT_NODE_COUNT)), } return counts