intel/perf: Move perf query register programming to static tables.
[mesa.git] / .gitlab-ci / tracie / dump_trace_images.py
index 66a99f7efe2620abffceb0f0805a490933b778d5..74efb1f32c7618b2afb57f013a100ba4c3553381 100644 (file)
@@ -1,6 +1,7 @@
 #!/usr/bin/python3
 
 # Copyright (c) 2019 Collabora Ltd
+# Copyright © 2019-2020 Valve Corporation.
 #
 # Permission is hereby granted, free of charge, to any person obtaining a
 # copy of this software and associated documentation files (the "Software"),
@@ -35,8 +36,8 @@ def log(severity, msg, end='\n'):
 def log_result(msg):
     print(msg, flush=True)
 
-def run_logged_command(cmd, log_path):
-    ret = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+def run_logged_command(cmd, env, log_path):
+    ret = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env)
     logoutput = ("[dump_trace_images] Running: %s\n" % " ".join(cmd)).encode() + \
                 ret.stdout
     log_path.parent.mkdir(parents=True, exist_ok=True)
@@ -47,8 +48,8 @@ def run_logged_command(cmd, log_path):
             logoutput.decode(errors='replace') +
             "[dump_traces_images] Process failed with error code: %d" % ret.returncode)
 
-def get_last_apitrace_frame_call(trace_path):
-    cmd = ["apitrace", "dump", "--calls=frame", str(trace_path)]
+def get_last_apitrace_frame_call(cmd_wrapper, trace_path):
+    cmd = cmd_wrapper + ["apitrace", "dump", "--calls=frame", str(trace_path)]
     ret = subprocess.run(cmd, stdout=subprocess.PIPE)
     for l in reversed(ret.stdout.decode(errors='replace').splitlines()):
         s = l.split(None, 1)
@@ -56,16 +57,31 @@ def get_last_apitrace_frame_call(trace_path):
             return int(s[0])
     return -1
 
-def dump_with_apitrace(trace_path, calls, device_name):
+def get_last_gfxreconstruct_frame_call(trace_path):
+    # FIXME: It would be great to have another way to get the amount of
+    # traces which wouldn't imply replaying the whole trace:
+    # https://github.com/LunarG/gfxreconstruct/issues/329
+    cmd = ["gfxrecon-replay", str(trace_path)]
+    ret = subprocess.run(cmd, stdout=subprocess.PIPE)
+    for l in reversed(ret.stdout.decode(errors='replace').splitlines()):
+        s = l.split(", ", 2)
+        if len(s) >= 3:
+            c = s[2].split(None, 1)
+            if len(c) >= 1 and c[0].isnumeric():
+                return int(c[0])
+    return -1
+
+def dump_with_apitrace(retrace_cmd, trace_path, calls, device_name):
     outputdir = str(trace_path.parent / "test" / device_name)
     os.makedirs(outputdir, exist_ok=True)
     outputprefix = str(Path(outputdir) / trace_path.name) + "-"
     if len(calls) == 0:
-        calls = [str(get_last_apitrace_frame_call(trace_path))]
-    cmd = ["apitrace", "dump-images", "--calls=" + ','.join(calls),
-           "-o", outputprefix, str(trace_path)]
+        calls = [str(get_last_apitrace_frame_call(retrace_cmd[:-1], trace_path))]
+    cmd = retrace_cmd + ["--headless",
+                         "--snapshot=" + ','.join(calls),
+                         "--snapshot-prefix=" + outputprefix, str(trace_path)]
     log_path = Path(outputdir) / (trace_path.name + ".log")
-    run_logged_command(cmd, log_path)
+    run_logged_command(cmd, None, log_path)
 
 def dump_with_renderdoc(trace_path, calls, device_name):
     outputdir = str(trace_path.parent / "test" / device_name)
@@ -73,7 +89,33 @@ def dump_with_renderdoc(trace_path, calls, device_name):
     cmd = [str(script_path / "renderdoc_dump_images.py"), str(trace_path), outputdir]
     cmd.extend(calls)
     log_path = Path(outputdir) / (trace_path.name + ".log")
-    run_logged_command(cmd, log_path)
+    run_logged_command(cmd, None, log_path)
+
+def dump_with_gfxreconstruct(trace_path, calls, device_name):
+    from PIL import Image
+    outputdir_path = trace_path.parent / "test" / device_name
+    outputdir_path.mkdir(parents=True, exist_ok=True)
+    outputprefix = str(outputdir_path / trace_path.name) + "-"
+    if len(calls) == 0:
+        # FIXME: The VK_LAYER_LUNARG_screenshot numbers the calls from
+        # 0 to (total-num-calls - 1) while gfxreconstruct does it from
+        # 1 to total-num-calls:
+        # https://github.com/LunarG/gfxreconstruct/issues/284
+        calls = [str(get_last_gfxreconstruct_frame_call(trace_path) - 1)]
+    cmd = ["gfxrecon-replay", str(trace_path)]
+    log_path = outputdir_path / (trace_path.name + ".log")
+    env = os.environ.copy()
+    env["VK_INSTANCE_LAYERS"] = "VK_LAYER_LUNARG_screenshot"
+    env["VK_SCREENSHOT_FRAMES"] = ",".join(calls)
+    env["VK_SCREENSHOT_DIR"] = str(outputdir_path)
+    run_logged_command(cmd, env, log_path)
+    for c in calls:
+        ppm = str(outputdir_path / c) + ".ppm"
+        outputfile = outputprefix + c + ".png"
+        with log_path.open(mode='w') as log:
+            log.write("Writing: %s to %s" % (ppm, outputfile))
+        Image.open(ppm).save(outputfile)
+        os.remove(ppm)
 
 def dump_with_testtrace(trace_path, calls, device_name):
     from PIL import Image
@@ -96,9 +138,13 @@ def dump_from_trace(trace_path, calls, device_name):
     trace_type = trace_type_from_filename(trace_path.name)
     try:
         if trace_type == TraceType.APITRACE:
-            dump_with_apitrace(trace_path, calls, device_name)
+            dump_with_apitrace(["eglretrace"], trace_path, calls, device_name)
+        elif trace_type == TraceType.APITRACE_DXGI:
+            dump_with_apitrace(["wine", "d3dretrace"], trace_path, calls, device_name)
         elif trace_type == TraceType.RENDERDOC:
             dump_with_renderdoc(trace_path, calls, device_name)
+        elif trace_type == TraceType.GFXRECONSTRUCT:
+            dump_with_gfxreconstruct(trace_path, calls, device_name)
         elif trace_type == TraceType.TESTTRACE:
             dump_with_testtrace(trace_path, calls, device_name)
         else: