Skip to content

"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/

Python

fix(renderdoc): apply review fixes for RenderDoc harness

alvinshica8d7e130d09
diff --git a/renderdoc/agent-harness/.gitignore b/renderdoc/agent-harness/.gitignore
index 3eaa0d1e0..6cd3b293f 100644
--- a/renderdoc/agent-harness/.gitignore
+++ b/renderdoc/agent-harness/.gitignore
@@ -1,3 +1,6 @@
+# Bundled RenderDoc native bindings must not be committed (use system install).
+cli_anything/renderdoc/native/
+
# Python
__pycache__/
*.pyc
diff --git a/renderdoc/agent-harness/HARNESS.md b/renderdoc/agent-harness/HARNESS.md
index b2ebe5fa8..61f93a5dd 100644
--- a/renderdoc/agent-harness/HARNESS.md
+++ b/renderdoc/agent-harness/HARNESS.md
@@ -20,7 +20,7 @@ agent-harness/
│ ├── capture.py # Capture file open/close/metadata/convert
│ ├── actions.py # Draw call / action tree navigation
│ ├── textures.py # Texture listing, pixel picking, export
- │ ├── pipeline.py # Pipeline state, shader disassembly, cbuffers
+ │ ├── pipeline.py # Pipeline state, shader export, diff, cbuffers
│ ├── resources.py # Buffer/resource enumeration and reading
│ ├── mesh.py # Vertex input/output decoding
│ └── counters.py # GPU performance counters
@@ -42,7 +42,7 @@ agent-harness/
| `capture` | `info`, `thumb`, `convert` |
| `actions` | `list`, `summary`, `find`, `get` |
| `textures` | `list`, `get`, `save`, `save-outputs`, `pick` |
-| `pipeline` | `state`, `disasm`, `cbuffer` |
+| `pipeline` | `state`, `shader-export`, `cbuffer`, `diff` |
| `resources` | `list`, `buffers`, `read-buffer` |
| `mesh` | `inputs`, `outputs` |
| `counters` | `list`, `fetch` |
@@ -77,6 +77,6 @@ agent-harness/
## Dependencies
-- **Required**: `click>=8.0`, `python>=3.8`
+- **Required**: `click>=8.0`, `prompt-toolkit>=3.0`, `python>=3.10`
- **Optional** (runtime): `renderdoc` Python module (from RenderDoc installation)
- **Test**: `pytest>=7.0`
diff --git a/renderdoc/agent-harness/cli_anything/renderdoc/__init__.py b/renderdoc/agent-harness/cli_anything/renderdoc/__init__.py
index 5fc9c1aed..8a7b9222e 100644
--- a/renderdoc/agent-harness/cli_anything/renderdoc/__init__.py
+++ b/renderdoc/agent-harness/cli_anything/renderdoc/__init__.py
@@ -2,12 +2,5 @@
__version__ = "0.1.0"
-# Ensure the native renderdoc.pyd / renderdoc.dll are importable.
-# Try the system-installed renderdoc first; fall back to the bundled native/ copy.
-try:
- import renderdoc as _rd_probe # noqa: F401
- del _rd_probe
-except ImportError:
- from cli_anything.renderdoc._bootstrap import ensure_native_on_path as _ensure
- _ensure()
- del _ensure
+# The ``renderdoc`` module is loaded lazily by core/CLI code when needed.
+# Install RenderDoc and add its Python bindings directory to PYTHONPATH.
diff --git a/renderdoc/agent-harness/cli_anything/renderdoc/_bootstrap.py b/renderdoc/agent-harness/cli_anything/renderdoc/_bootstrap.py
deleted file mode 100644
index af00031ea..000000000
--- a/renderdoc/agent-harness/cli_anything/renderdoc/_bootstrap.py
+++ /dev/null
@@ -1,20 +0,0 @@
-"""
-Bootstrap renderdoc native module import.
-
-This module ensures the native renderdoc.pyd (and its dependency renderdoc.dll)
-can be found by Python. It adds the ``native/`` directory to sys.path so that
-``import renderdoc`` resolves to the compiled SWIG bindings built for
-Python 3.10.
-"""
-
-import os
-import sys
-
-_HERE = os.path.dirname(os.path.abspath(__file__))
-_NATIVE_DIR = os.path.join(_HERE, "native")
-
-
-def ensure_native_on_path():
- """Add the native/ directory to sys.path (idempotent)."""
- if _NATIVE_DIR not in sys.path:
- sys.path.insert(0, _NATIVE_DIR)
diff --git a/renderdoc/agent-harness/cli_anything/renderdoc/core/capture.py b/renderdoc/agent-harness/cli_anything/renderdoc/core/capture.py
index b87ac70ac..4ef46c43b 100644
--- a/renderdoc/agent-harness/cli_anything/renderdoc/core/capture.py
+++ b/renderdoc/agent-harness/cli_anything/renderdoc/core/capture.py
@@ -36,6 +36,31 @@ def _require_rd():
)
+# ---------------------------------------------------------------------------
+# Global RenderDoc replay API (InitialiseReplay / ShutdownReplay) — refcounted
+# ---------------------------------------------------------------------------
+_replay_refcount = 0
+
+
+def _ensure_replay_api():
+ """Initialise RenderDoc replay once; pair with ``_release_replay_api`` per handle."""
+ global _replay_refcount
+ _require_rd()
+ if _replay_refcount == 0:
+ rd.InitialiseReplay(rd.GlobalEnvironment(), [])
+ _replay_refcount += 1
+
+
+def _release_replay_api():
+ """Shut down replay when the last CaptureHandle in the process closes."""
+ global _replay_refcount
+ if not HAS_RD or _replay_refcount <= 0:
+ return
+ _replay_refcount -= 1
+ if _replay_refcount == 0:
+ rd.ShutdownReplay()
+
+
# ---------------------------------------------------------------------------
# Capture file handle wrapper
# ---------------------------------------------------------------------------
@@ -48,13 +73,18 @@ class CaptureHandle:
if not os.path.isfile(self.path):
raise FileNotFoundError(f"Capture file not found: {self.path}")
- rd.InitialiseReplay(rd.GlobalEnvironment(), [])
- self._cap = rd.OpenCaptureFile()
- result = self._cap.OpenFile(self.path, "", None)
- if result != rd.ResultCode.Succeeded:
- raise RuntimeError(f"Failed to open capture: {result}")
+ _ensure_replay_api()
+ try:
+ self._cap = rd.OpenCaptureFile()
+ result = self._cap.OpenFile(self.path, "", None)
+ if result != rd.ResultCode.Succeeded:
+ raise RuntimeError(f"Failed to open capture: {result}")
+ except Exception:
+ _release_replay_api()
+ raise
self._controller: Any = None
+ self._closed = False
# -- lazy replay init ---------------------------------------------------
def _ensure_replay(self):
@@ -131,13 +161,16 @@ class CaptureHandle:
# -- cleanup ------------------------------------------------------------
def close(self):
+ if getattr(self, "_closed", False):
+ return
+ self._closed = True
if self._controller is not None:
self._controller.Shutdown()
self._controller = None
if self._cap is not None:
self._cap.Shutdown()
self._cap = None
- rd.ShutdownReplay()
+ _release_replay_api()
def __enter__(self):
return self
diff --git a/renderdoc/agent-harness/cli_anything/renderdoc/core/pipeline.py b/renderdoc/agent-harness/cli_anything/renderdoc/core/pipeline.py
index 3aecebb2c..f48eadffc 100644
--- a/renderdoc/agent-harness/cli_anything/renderdoc/core/pipeline.py
+++ b/renderdoc/agent-harness/cli_anything/renderdoc/core/pipeline.py
@@ -736,7 +736,7 @@ def _serialize_cbuffer_var(v) -> Dict[str, Any]:
return d
-def dump_shader_reflection(refl, include_file_contents: bool = False) -> Dict[str, Any]:
+def dump_shader_reflection(refl, include_file_contents: bool = False) -> Optional[Dict[str, Any]]:
"""Serialize a ShaderReflection object to a JSON-friendly dict.
Maps all fields from the RenderDoc Python API ShaderReflection class.
diff --git a/renderdoc/agent-harness/cli_anything/renderdoc/native/renderdoc.dll b/renderdoc/agent-harness/cli_anything/renderdoc/native/renderdoc.dll
deleted file mode 100644
index 7e6959fed..000000000
Binary files a/renderdoc/agent-harness/cli_anything/renderdoc/native/renderdoc.dll and /dev/null differ
diff --git a/renderdoc/agent-harness/cli_anything/renderdoc/native/renderdoc.pyd b/renderdoc/agent-harness/cli_anything/renderdoc/native/renderdoc.pyd
deleted file mode 100644
index f461195a2..000000000
Binary files a/renderdoc/agent-harness/cli_anything/renderdoc/native/renderdoc.pyd and /dev/null differ
diff --git a/renderdoc/agent-harness/cli_anything/renderdoc/renderdoc_cli.py b/renderdoc/agent-harness/cli_anything/renderdoc/renderdoc_cli.py
index d67af3456..685722476 100644
--- a/renderdoc/agent-harness/cli_anything/renderdoc/renderdoc_cli.py
+++ b/renderdoc/agent-harness/cli_anything/renderdoc/renderdoc_cli.py
@@ -34,6 +34,16 @@ _capture_handle = None # type: ignore
_repl_mode = False
+def _close_all_captures():
+ global _capture_handle, _capture_handle_b
+ if _capture_handle is not None:
+ _capture_handle.close()
+ _capture_handle = None
+ if _capture_handle_b is not None:
+ _capture_handle_b.close()
+ _capture_handle_b = None
+
+
def _get_export_dir(ctx: click.Context, subfolder: str = "") -> str:
"""Return the default export directory for the current capture.
@@ -810,13 +820,11 @@ def pipeline_diff_cmd(ctx, event_a, event_b, capture_b, compact, output):
@cli.result_callback()
@click.pass_context
def cleanup(ctx, *args, **kwargs):
- global _capture_handle, _capture_handle_b
- if _capture_handle is not None:
- _capture_handle.close()
- _capture_handle = None
- if _capture_handle_b is not None:
- _capture_handle_b.close()
- _capture_handle_b = None
+ global _repl_mode
+ # REPL invokes cli.main() per line; keep captures open until repl() exits.
+ if _repl_mode:
+ return
+ _close_all_captures()
# ===========================================================================
@@ -853,33 +861,35 @@ def repl(ctx):
capture_path = ctx.obj.get("capture_path", "")
context = os.path.basename(capture_path) if capture_path else ""
- while True:
- try:
- line = skin.get_input(pt_session, project_name=context, modified=False)
- if not line:
- continue
- if line.lower() in ("quit", "exit", "q"):
+ try:
+ while True:
+ try:
+ line = skin.get_input(pt_session, project_name=context, modified=False)
+ if not line:
+ continue
+ if line.lower() in ("quit", "exit", "q"):
+ skin.print_goodbye()
+ break
+ if line.lower() == "help":
+ skin.help(_repl_commands)
+ continue
+
+ args = line.split()
+ try:
+ cli.main(args, standalone_mode=False)
+ except SystemExit:
+ pass
+ except click.exceptions.UsageError as e:
+ skin.warning("Usage error: %s" % e)
+ except Exception as e:
+ skin.error("%s" % e)
+
+ except (EOFError, KeyboardInterrupt):
skin.print_goodbye()
break
- if line.lower() == "help":
- skin.help(_repl_commands)
- continue
-
- args = line.split()
- try:
- cli.main(args, standalone_mode=False)
- except SystemExit:
- pass
- except click.exceptions.UsageError as e:
- skin.warning("Usage error: %s" % e)
- except Exception as e:
- skin.error("%s" % e)
-
- except (EOFError, KeyboardInterrupt):
- skin.print_goodbye()
- break
-
- _repl_mode = False
+ finally:
+ _close_all_captures()
+ _repl_mode = False
# ===========================================================================
diff --git a/renderdoc/agent-harness/cli_anything/renderdoc/tests/TEST.md b/renderdoc/agent-harness/cli_anything/renderdoc/tests/TEST.md
index 6e31a5fed..411c229e6 100644
--- a/renderdoc/agent-harness/cli_anything/renderdoc/tests/TEST.md
+++ b/renderdoc/agent-harness/cli_anything/renderdoc/tests/TEST.md
@@ -36,9 +36,10 @@ pytest cli_anything/renderdoc/tests/ -v
| core/actions.py | 9 | ✅ Pass |
| core/textures.py | 4 | ✅ Pass |
| core/resources.py | 5 | ✅ Pass |
+| core/diff.py | 12 | ✅ Pass |
| CLI help (all groups) | 8 | ✅ Pass |
| CLI subprocess | 1 | ✅ Pass |
-| **Total Unit** | **33**| **✅ All Pass** |
+| **Total Unit** | **45**| **✅ All Pass** |
| E2E (capture info) | 2 | ⏭️ Skip (no RD) |
| E2E (actions) | 4 | ⏭️ Skip (no RD) |
| E2E (textures) | 2 | ⏭️ Skip (no RD) |
@@ -87,10 +88,22 @@ cli_anything/renderdoc/tests/test_core.py::TestCLIHelp::test_resources_help PASS
cli_anything/renderdoc/tests/test_core.py::TestCLIHelp::test_mesh_help PASSED
cli_anything/renderdoc/tests/test_core.py::TestCLIHelp::test_counters_help PASSED
cli_anything/renderdoc/tests/test_core.py::TestCLISubprocess::test_cli_help_subprocess PASSED
-
-============================= 33 passed in 0.15s ==============================
+cli_anything/renderdoc/tests/test_core.py::TestDiffModule::test_identical_snapshots PASSED
+cli_anything/renderdoc/tests/test_core.py::TestDiffModule::test_different_viewport PASSED
+cli_anything/renderdoc/tests/test_core.py::TestDiffModule::test_float_tolerance PASSED
+cli_anything/renderdoc/tests/test_core.py::TestDiffModule::test_float_nan_equal PASSED
+cli_anything/renderdoc/tests/test_core.py::TestDiffModule::test_diff_lists_only_in_one_side PASSED
+cli_anything/renderdoc/tests/test_core.py::TestDiffModule::test_diff_dicts_missing_key PASSED
+cli_anything/renderdoc/tests/test_core.py::TestDiffModule::test_diff_dicts_identical PASSED
+cli_anything/renderdoc/tests/test_core.py::TestDiffModule::test_diff_dicts_none_inputs PASSED
+cli_anything/renderdoc/tests/test_core.py::TestDiffModule::test_stage_diff_shader_changed PASSED
+cli_anything/renderdoc/tests/test_core.py::TestDiffModule::test_cbuffer_variable_diff PASSED
+cli_anything/renderdoc/tests/test_core.py::TestDiffModule::test_cbuffer_variable_identical PASSED
+cli_anything/renderdoc/tests/test_core.py::TestDiffModule::test_output_table_extra_columns PASSED
+
+============================= 45 passed in 0.15s ==============================
cli_anything/renderdoc/tests/test_full_e2e.py - 14 skipped (no RenderDoc)
-============================= 47 total, 33 passed, 14 skipped ================
+============================= 59 total, 45 passed, 14 skipped ================
```
diff --git a/renderdoc/agent-harness/cli_anything/renderdoc/tests/test_core.py b/renderdoc/agent-harness/cli_anything/renderdoc/tests/test_core.py
index cb3b4eaec..a034f7c88 100644
--- a/renderdoc/agent-harness/cli_anything/renderdoc/tests/test_core.py
+++ b/renderdoc/agent-harness/cli_anything/renderdoc/tests/test_core.py
@@ -8,21 +8,13 @@ Run with: pytest test_core.py -v
from __future__ import annotations
import json
-import os
-import sys
import struct
+import sys
+from pathlib import Path
from unittest.mock import MagicMock, patch, PropertyMock
import pytest
-# ---------------------------------------------------------------------------
-# Ensure the package is importable
-# ---------------------------------------------------------------------------
-HARNESS_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
-if HARNESS_ROOT not in sys.path:
- sys.path.insert(0, HARNESS_ROOT)
-
-
# ===========================================================================
# Test utils/output.py
# ===========================================================================
@@ -498,14 +490,19 @@ class TestCLIHelp:
# ===========================================================================
class TestCLISubprocess:
- """Test CLI via subprocess (requires pip install -e .)"""
+ """Test CLI via subprocess from agent-harness root (namespace on cwd)."""
def test_cli_help_subprocess(self):
import subprocess
+
+ harness_root = Path(__file__).resolve().parents[3]
try:
result = subprocess.run(
[sys.executable, "-m", "cli_anything.renderdoc.renderdoc_cli", "--help"],
- capture_output=True, text=True, timeout=10,
+ capture_output=True,
+ text=True,
+ timeout=10,
+ cwd=str(harness_root),
)
assert result.returncode == 0
assert "RenderDoc CLI" in result.stdout
diff --git a/renderdoc/agent-harness/cli_anything/renderdoc/tests/test_full_e2e.py b/renderdoc/agent-harness/cli_anything/renderdoc/tests/test_full_e2e.py
index 8589a5546..133c65979 100644
--- a/renderdoc/agent-harness/cli_anything/renderdoc/tests/test_full_e2e.py
+++ b/renderdoc/agent-harness/cli_anything/renderdoc/tests/test_full_e2e.py
@@ -17,6 +17,7 @@ import os
import subprocess
import sys
import tempfile
+from pathlib import Path
import pytest
@@ -24,9 +25,7 @@ import pytest
# Configuration
# ---------------------------------------------------------------------------
-HARNESS_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
-if HARNESS_ROOT not in sys.path:
- sys.path.insert(0, HARNESS_ROOT)
+HARNESS_ROOT = str(Path(__file__).resolve().parents[3])
TEST_CAPTURE = os.environ.get("RENDERDOC_TEST_CAPTURE", "")
HAS_CAPTURE = os.path.isfile(TEST_CAPTURE) if TEST_CAPTURE else False
diff --git a/renderdoc/agent-harness/setup.py b/renderdoc/agent-harness/setup.py
index bc095a54a..57d6e4222 100644
--- a/renderdoc/agent-harness/setup.py
+++ b/renderdoc/agent-harness/setup.py
@@ -1,18 +1,24 @@
"""Setup for cli-anything-renderdoc package."""
+from pathlib import Path
+
from setuptools import setup, find_namespace_packages
+_README = Path(__file__).parent / "cli_anything" / "renderdoc" / "README.md"
+_long_desc = _README.read_text(encoding="utf-8") if _README.is_file() else ""
+
setup(
name="cli-anything-renderdoc",
version="0.1.0",
description="CLI harness for RenderDoc graphics debugger",
- long_description=open("cli_anything/renderdoc/README.md").read(),
+ long_description=_long_desc,
long_description_content_type="text/markdown",
author="cli-anything",
packages=find_namespace_packages(include=["cli_anything.*"]),
python_requires=">=3.10",
install_requires=[
"click>=8.0",
+ "prompt-toolkit>=3.0",
],
extras_require={
"test": ["pytest>=7.0"],
@@ -23,7 +29,7 @@ setup(
],
},
package_data={
- "cli_anything.renderdoc": ["skills/*.md", "README.md", "native/*.pyd", "native/*.dll"],
+ "cli_anything.renderdoc": ["skills/*.md", "README.md"],
},
classifiers=[
"Programming Language :: Python :: 3",