|
| 1 | +From 762270481adefff65194ef56f02768cff252efb2 Mon Sep 17 00:00:00 2001 |
| 2 | +From: =?UTF-8?q?=C3=98yvind=20Harboe?= <oyvind@ascenium.com> |
| 3 | +Date: Sun, 17 May 2026 15:02:44 +0200 |
| 4 | +Subject: [PATCH] render_gds: log VmRSS/VmPeak per phase + skip |
| 5 | + int32-overflowing exports |
| 6 | +MIME-Version: 1.0 |
| 7 | +Content-Type: text/plain; charset=UTF-8 |
| 8 | +Content-Transfer-Encoding: 8bit |
| 9 | + |
| 10 | +Headless run on sky130hd/microwatt's 562 MB merged GDS surfaces two |
| 11 | +distinct issues that the previous gltf-export path turned into an opaque |
| 12 | +"appears to choke" failure: |
| 13 | + |
| 14 | +1. No phase visibility. |
| 15 | + |
| 16 | + The action log carried no breadcrumbs about which step OOMed or |
| 17 | + returned CANCELLED inside the long bpy.ops sequence (addon-register, |
| 18 | + gdsii-import, gltf-addon-enable, gltf-export, html-write). |
| 19 | + |
| 20 | + Add a tiny `_log_phase()` helper that scrapes VmRSS/VmPeak from |
| 21 | + /proc/self/status and prints one tagged line per phase boundary, |
| 22 | + plus the imported mesh polygon/vertex totals and the .glb / .html |
| 23 | + byte sizes. Wraps `bpy.ops.import_scene.gdsii` and |
| 24 | + `bpy.ops.export_scene.gltf` in try/except so a Python exception |
| 25 | + inside the addon prints a full traceback before sys.exit, instead |
| 26 | + of being swallowed by Blender's operator dispatcher and turning |
| 27 | + into a bare `{'CANCELLED'}` return. |
| 28 | + |
| 29 | +2. Int32 overflow in foreach_get for very large meshes. |
| 30 | + |
| 31 | + With the phase logging in place, the actual failure surfaces as: |
| 32 | + |
| 33 | + render_gds.py [gdsii-imported] meshes=17 polygons=204677861 |
| 34 | + verts=303316116 |
| 35 | + Error: Array length mismatch |
| 36 | + (expected -464636871, got 609104952) |
| 37 | + RuntimeError: internal error setting the array |
| 38 | + |
| 39 | + The traceback points at gltf2_blender_gather_primitives_extract.py |
| 40 | + line 1405: |
| 41 | + |
| 42 | + self.normals = np.empty(len(self.blender_mesh.loops) * 3, |
| 43 | + dtype=np.float32) |
| 44 | + self.blender_mesh.corner_normals.foreach_get('vector', self.normals) |
| 45 | + |
| 46 | + Microwatt's mcon layer alone has ~9.3M extruded polygons × 6 faces × |
| 47 | + 4 corners ≈ 223M corner-normals = 669M float32 values, which |
| 48 | + overflows Blender 4.2's signed-int32 element count. |
| 49 | + |
| 50 | + The NORMAL attribute is gated by `self.use_normals` (`gltf_normals` |
| 51 | + export setting); when that's False the failing path is never entered. |
| 52 | + Pass `export_normals=False` along with belt-and-suspenders for the |
| 53 | + other foreach_get-backed per-corner attributes (tangents, vertex |
| 54 | + colors, attributes) so we don't trade one int32 overflow for |
| 55 | + another. model-viewer falls back to face-derived flat normals, |
| 56 | + which is the right look for a chip-layout scene anyway -- the |
| 57 | + layers are already block-like and don't benefit from smooth |
| 58 | + shading. |
| 59 | + |
| 60 | +Tested locally: applies cleanly against bazel-orfs 68b3d4e (the current |
| 61 | +pin in the consuming ORFS branch) and parses + passes black --check. |
| 62 | +A full microwatt_final_blender_html bazelisk run reproduces the |
| 63 | +overflow on top-of-tree and is needed to validate the fix end-to-end. |
| 64 | +--- |
| 65 | + tools/blendergds/render_gds.py | 112 ++++++++++++++++++++++++++++----- |
| 66 | + 1 file changed, 96 insertions(+), 16 deletions(-) |
| 67 | + |
| 68 | +diff --git a/tools/blendergds/render_gds.py b/tools/blendergds/render_gds.py |
| 69 | +index 6cf4d75..b3527e3 100644 |
| 70 | +--- a/tools/blendergds/render_gds.py |
| 71 | ++++ b/tools/blendergds/render_gds.py |
| 72 | +@@ -33,10 +33,35 @@ import importlib.util |
| 73 | + import shutil |
| 74 | + import sys |
| 75 | + import tempfile |
| 76 | ++import traceback |
| 77 | + import zipfile |
| 78 | + from pathlib import Path |
| 79 | + |
| 80 | + |
| 81 | ++def _log_phase(phase: str, extra: str = "") -> None: |
| 82 | ++ """Print a single-line phase marker with VmRSS / VmPeak. |
| 83 | ++ |
| 84 | ++ Blender's --background mode produces a lot of output; these prefixed |
| 85 | ++ lines make it easy to grep the action log for the failing phase |
| 86 | ++ (e.g. addon-register, klayout-merge inside gdsii operator, gltf-export, |
| 87 | ++ html-write) and to see exactly where RSS spikes before an OOM. |
| 88 | ++ """ |
| 89 | ++ rss = peak = "?" |
| 90 | ++ try: |
| 91 | ++ with open("/proc/self/status", "r", encoding="utf-8") as f: |
| 92 | ++ for line in f: |
| 93 | ++ if line.startswith("VmRSS:"): |
| 94 | ++ rss = line.split(":", 1)[1].strip() |
| 95 | ++ elif line.startswith("VmPeak:"): |
| 96 | ++ peak = line.split(":", 1)[1].strip() |
| 97 | ++ except OSError: |
| 98 | ++ pass |
| 99 | ++ msg = f"render_gds.py [{phase}] VmRSS={rss} VmPeak={peak}" |
| 100 | ++ if extra: |
| 101 | ++ msg += f" {extra}" |
| 102 | ++ print(msg, flush=True) |
| 103 | ++ |
| 104 | ++ |
| 105 | + def _split_argv(): |
| 106 | + """Return Blender's pass-through args (everything after `--`).""" |
| 107 | + if "--" not in sys.argv: |
| 108 | +@@ -166,6 +191,8 @@ def main(): |
| 109 | + if not wheels_dir.is_dir(): |
| 110 | + sys.exit(f"render_gds.py: wheels/ dir not found under {addon_root}") |
| 111 | + |
| 112 | ++ _log_phase("startup", extra=f"gds={gds_path} ({gds_path.stat().st_size} bytes)") |
| 113 | ++ |
| 114 | + # Stage wheels into a temp site-packages and put it on sys.path BEFORE |
| 115 | + # importing the addon (which imports numpy/gdstk/klayout/yaml at module |
| 116 | + # scope). |
| 117 | +@@ -175,6 +202,7 @@ def main(): |
| 118 | + try: |
| 119 | + _stage_wheels(wheels_dir, site) |
| 120 | + sys.path.insert(0, str(site)) |
| 121 | ++ _log_phase("wheels-staged") |
| 122 | + |
| 123 | + # bpy is provided by Blender; importing it before this point would |
| 124 | + # leak partial init in --background mode. |
| 125 | +@@ -182,6 +210,7 @@ def main(): |
| 126 | + |
| 127 | + addon = _load_addon_module(addon_root) |
| 128 | + addon.register() |
| 129 | ++ _log_phase("addon-registered") |
| 130 | + |
| 131 | + # The addon's register_properties() declares four scene props via |
| 132 | + # the legacy `bpy.types.Scene.X = StringProperty(...)` form. In |
| 133 | +@@ -192,38 +221,85 @@ def main(): |
| 134 | + # which is what we want anyway. |
| 135 | + bpy.context.scene.gdsii_pdk_selection = args.pdk |
| 136 | + |
| 137 | +- result = bpy.ops.import_scene.gdsii( |
| 138 | +- filepath=str(gds_path), |
| 139 | +- setup_scene=True, |
| 140 | +- create_collection=True, |
| 141 | +- merge_layers=True, |
| 142 | +- color_scheme="realistic", |
| 143 | +- ) |
| 144 | ++ try: |
| 145 | ++ result = bpy.ops.import_scene.gdsii( |
| 146 | ++ filepath=str(gds_path), |
| 147 | ++ setup_scene=True, |
| 148 | ++ create_collection=True, |
| 149 | ++ merge_layers=True, |
| 150 | ++ color_scheme="realistic", |
| 151 | ++ ) |
| 152 | ++ except Exception: |
| 153 | ++ _log_phase("gdsii-import-exception") |
| 154 | ++ traceback.print_exc() |
| 155 | ++ sys.exit("render_gds.py: import_scene.gdsii raised — see traceback above") |
| 156 | + if "FINISHED" not in result: |
| 157 | ++ _log_phase("gdsii-import-nonfinished", extra=f"result={result!r}") |
| 158 | + sys.exit(f"render_gds.py: import_scene.gdsii returned {result!r}") |
| 159 | + |
| 160 | ++ mesh_objs = [o for o in bpy.data.objects if o.type == "MESH"] |
| 161 | ++ total_polys = sum(len(o.data.polygons) for o in mesh_objs) |
| 162 | ++ total_verts = sum(len(o.data.vertices) for o in mesh_objs) |
| 163 | ++ _log_phase( |
| 164 | ++ "gdsii-imported", |
| 165 | ++ extra=f"meshes={len(mesh_objs)} polygons={total_polys} verts={total_verts}", |
| 166 | ++ ) |
| 167 | ++ |
| 168 | + if args.out: |
| 169 | + out_path = Path(args.out).resolve() |
| 170 | + out_path.parent.mkdir(parents=True, exist_ok=True) |
| 171 | + bpy.ops.wm.save_as_mainfile(filepath=str(out_path), copy=True) |
| 172 | ++ _log_phase("blend-saved", extra=f"{out_path.stat().st_size} bytes") |
| 173 | + |
| 174 | + if args.out_html: |
| 175 | + # io_scene_gltf2 ships with Blender as a core addon but is not |
| 176 | + # active under --factory-startup. Enable it before exporting. |
| 177 | + bpy.ops.preferences.addon_enable(module="io_scene_gltf2") |
| 178 | ++ _log_phase("gltf-addon-enabled") |
| 179 | + |
| 180 | + glb_tmp = tmp / "scene.glb" |
| 181 | +- export_result = bpy.ops.export_scene.gltf( |
| 182 | +- filepath=str(glb_tmp), |
| 183 | +- export_format="GLB", |
| 184 | +- export_apply=True, |
| 185 | +- export_yup=True, |
| 186 | +- export_lights=False, |
| 187 | +- export_cameras=False, |
| 188 | +- use_active_scene=True, |
| 189 | +- ) |
| 190 | ++ try: |
| 191 | ++ export_result = bpy.ops.export_scene.gltf( |
| 192 | ++ filepath=str(glb_tmp), |
| 193 | ++ export_format="GLB", |
| 194 | ++ export_apply=True, |
| 195 | ++ export_yup=True, |
| 196 | ++ export_lights=False, |
| 197 | ++ export_cameras=False, |
| 198 | ++ use_active_scene=True, |
| 199 | ++ # Per-corner attribute exports use foreach_get under the |
| 200 | ++ # hood and Blender 4.2's RNA bridge stores the element |
| 201 | ++ # count in a signed int32. Microwatt's mcon layer alone |
| 202 | ++ # has ~9.3M extruded polygons × 6 faces × 4 corners ≈ |
| 203 | ++ # 223M corner-normals = 669M float32 values, which |
| 204 | ++ # overflows int32 and yields: |
| 205 | ++ # |
| 206 | ++ # Error: Array length mismatch |
| 207 | ++ # (expected -464636871, got 609104952) |
| 208 | ++ # RuntimeError: internal error setting the array |
| 209 | ++ # |
| 210 | ++ # in gltf2_blender_gather_primitives_extract.__get_normals. |
| 211 | ++ # Disabling normals (and the other per-corner attributes |
| 212 | ++ # gated by foreach_get -- tangents, vertex colors) |
| 213 | ++ # short-circuits the failing path before it allocates |
| 214 | ++ # the oversized buffer. model-viewer falls back to |
| 215 | ++ # face-derived flat normals, which is the right look |
| 216 | ++ # for a chip-layout scene anyway (no smooth shading). |
| 217 | ++ export_normals=False, |
| 218 | ++ export_tangents=False, |
| 219 | ++ export_attributes=False, |
| 220 | ++ export_vertex_color="NONE", |
| 221 | ++ ) |
| 222 | ++ except Exception: |
| 223 | ++ _log_phase("gltf-export-exception") |
| 224 | ++ traceback.print_exc() |
| 225 | ++ sys.exit( |
| 226 | ++ "render_gds.py: export_scene.gltf raised — see traceback above" |
| 227 | ++ ) |
| 228 | + if "FINISHED" not in export_result: |
| 229 | ++ _log_phase("gltf-export-nonfinished", extra=f"result={export_result!r}") |
| 230 | + sys.exit(f"render_gds.py: export_scene.gltf returned {export_result!r}") |
| 231 | ++ _log_phase("gltf-exported", extra=f"glb={glb_tmp.stat().st_size} bytes") |
| 232 | + |
| 233 | + _write_html( |
| 234 | + template_path=Path(args.html_template).resolve(), |
| 235 | +@@ -232,6 +308,10 @@ def main(): |
| 236 | + title=args.title, |
| 237 | + out_path=Path(args.out_html).resolve(), |
| 238 | + ) |
| 239 | ++ _log_phase( |
| 240 | ++ "html-written", |
| 241 | ++ extra=f"out={Path(args.out_html).stat().st_size} bytes", |
| 242 | ++ ) |
| 243 | + finally: |
| 244 | + shutil.rmtree(tmp, ignore_errors=True) |
| 245 | + |
| 246 | +-- |
| 247 | +2.51.0 |
| 248 | + |
0 commit comments