|
| 1 | +From 7e2809623a8bce8f11a0fcf247a3125a72cf3fbe 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 phases, skip int32-overflowing exports, drop |
| 5 | + noise layers |
| 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 three |
| 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 the bpy.ops calls in try/except so a Python |
| 24 | + exception inside the addon prints a full traceback before |
| 25 | + sys.exit, instead of being swallowed into a bare `{'CANCELLED'}`. |
| 26 | + |
| 27 | +2. Int32 overflow in foreach_get for very large per-corner attributes. |
| 28 | + |
| 29 | + With the phase logging in place, the first iteration surfaced: |
| 30 | + |
| 31 | + Error: Array length mismatch (expected -464636871, got 609104952) |
| 32 | + RuntimeError: internal error setting the array |
| 33 | + File ".../io_scene_gltf2/blender/exp/gltf2_blender_gather_primitives_extract.py", |
| 34 | + line 1405, in __get_normals |
| 35 | + self.blender_mesh.corner_normals.foreach_get('vector', self.normals) |
| 36 | + |
| 37 | + Blender 4.2's RNA bridge stores the foreach_get element count in a |
| 38 | + signed int32; microwatt's mcon layer alone has ~9.3M extruded |
| 39 | + polygons × 6 faces × 4 corners ≈ 223M corner-normals = 669M float32 |
| 40 | + values, which overflows. |
| 41 | + |
| 42 | + The NORMAL attribute is gated by `self.use_normals` (`gltf_normals` |
| 43 | + export setting); when False the failing path is never entered. |
| 44 | + Pass `export_normals=False` plus belt-and-suspenders for the other |
| 45 | + foreach_get-backed per-corner attributes (tangents, vertex colors, |
| 46 | + custom attributes) so we don't trade one overflow for another. |
| 47 | + model-viewer falls back to face-derived flat normals, which is the |
| 48 | + right look for a chip-layout scene -- the extruded layers are |
| 49 | + already block-like and don't benefit from smooth shading. |
| 50 | + |
| 51 | +3. GLB 4 GB single-file ceiling for full-stack microwatt. |
| 52 | + |
| 53 | + With normals disabled, the second iteration trips: |
| 54 | + |
| 55 | + File ".../io_scene_gltf2/io/exp/gltf2_io_export.py", line 105, |
| 56 | + in save_gltf |
| 57 | + file.write(struct.pack("I", length)) |
| 58 | + struct.error: 'I' format requires 0 <= number <= 4294967295 |
| 59 | + |
| 60 | + i.e. the GLB binary container stores its total length as uint32 and |
| 61 | + microwatt's raw vertex+index payload is ~9-10 GB (mcon alone is |
| 62 | + ~2.9 GB raw, licon ~2.6 GB). Even if the GLB write succeeded, the |
| 63 | + base64-embedded data URL in the HTML would exceed the ~2 GB |
| 64 | + per-tab memory cap that mainstream browsers enforce. |
| 65 | + |
| 66 | + Drop the densest interconnect/contact layers (licon, li1, mcon, |
| 67 | + met1 and all vias) when total polygons cross 30M. The visually |
| 68 | + distinctive layers (substrate nwell, transistor regions |
| 69 | + diff+poly, upper metals met2-5 including the IP macros that route |
| 70 | + there) stay. Estimated post-drop budget for microwatt: |
| 71 | + ~3.5M input polys × 6 ≈ 21M faces, ~760 MB GLB, ~1 GB base64 HTML |
| 72 | + -- comfortably browser-loadable. |
| 73 | + |
| 74 | + Layer names are sky130-specific (the BlenderGDS addon names each |
| 75 | + per-layer object `L<layer-name>` via its configs/sky130.yaml |
| 76 | + stack). Other PDKs would need a different drop set if they hit |
| 77 | + the same ceiling; leaving that for when a non-sky130 design |
| 78 | + actually trips it. |
| 79 | + |
| 80 | +Threshold 30M is empirical: gcd-class designs (<1M polygons) stay |
| 81 | +below it and get the full unfiltered stack. |
| 82 | +--- |
| 83 | + tools/blendergds/render_gds.py | 155 +++++++++++++++++++++++++++++---- |
| 84 | + 1 file changed, 139 insertions(+), 16 deletions(-) |
| 85 | + |
| 86 | +diff --git a/tools/blendergds/render_gds.py b/tools/blendergds/render_gds.py |
| 87 | +index 6cf4d75..aabd2f8 100644 |
| 88 | +--- a/tools/blendergds/render_gds.py |
| 89 | ++++ b/tools/blendergds/render_gds.py |
| 90 | +@@ -33,10 +33,35 @@ import importlib.util |
| 91 | + import shutil |
| 92 | + import sys |
| 93 | + import tempfile |
| 94 | ++import traceback |
| 95 | + import zipfile |
| 96 | + from pathlib import Path |
| 97 | + |
| 98 | + |
| 99 | ++def _log_phase(phase: str, extra: str = "") -> None: |
| 100 | ++ """Print a single-line phase marker with VmRSS / VmPeak. |
| 101 | ++ |
| 102 | ++ Blender's --background mode produces a lot of output; these prefixed |
| 103 | ++ lines make it easy to grep the action log for the failing phase |
| 104 | ++ (e.g. addon-register, klayout-merge inside gdsii operator, gltf-export, |
| 105 | ++ html-write) and to see exactly where RSS spikes before an OOM. |
| 106 | ++ """ |
| 107 | ++ rss = peak = "?" |
| 108 | ++ try: |
| 109 | ++ with open("/proc/self/status", "r", encoding="utf-8") as f: |
| 110 | ++ for line in f: |
| 111 | ++ if line.startswith("VmRSS:"): |
| 112 | ++ rss = line.split(":", 1)[1].strip() |
| 113 | ++ elif line.startswith("VmPeak:"): |
| 114 | ++ peak = line.split(":", 1)[1].strip() |
| 115 | ++ except OSError: |
| 116 | ++ pass |
| 117 | ++ msg = f"render_gds.py [{phase}] VmRSS={rss} VmPeak={peak}" |
| 118 | ++ if extra: |
| 119 | ++ msg += f" {extra}" |
| 120 | ++ print(msg, flush=True) |
| 121 | ++ |
| 122 | ++ |
| 123 | + def _split_argv(): |
| 124 | + """Return Blender's pass-through args (everything after `--`).""" |
| 125 | + if "--" not in sys.argv: |
| 126 | +@@ -166,6 +191,8 @@ def main(): |
| 127 | + if not wheels_dir.is_dir(): |
| 128 | + sys.exit(f"render_gds.py: wheels/ dir not found under {addon_root}") |
| 129 | + |
| 130 | ++ _log_phase("startup", extra=f"gds={gds_path} ({gds_path.stat().st_size} bytes)") |
| 131 | ++ |
| 132 | + # Stage wheels into a temp site-packages and put it on sys.path BEFORE |
| 133 | + # importing the addon (which imports numpy/gdstk/klayout/yaml at module |
| 134 | + # scope). |
| 135 | +@@ -175,6 +202,7 @@ def main(): |
| 136 | + try: |
| 137 | + _stage_wheels(wheels_dir, site) |
| 138 | + sys.path.insert(0, str(site)) |
| 139 | ++ _log_phase("wheels-staged") |
| 140 | + |
| 141 | + # bpy is provided by Blender; importing it before this point would |
| 142 | + # leak partial init in --background mode. |
| 143 | +@@ -182,6 +210,7 @@ def main(): |
| 144 | + |
| 145 | + addon = _load_addon_module(addon_root) |
| 146 | + addon.register() |
| 147 | ++ _log_phase("addon-registered") |
| 148 | + |
| 149 | + # The addon's register_properties() declares four scene props via |
| 150 | + # the legacy `bpy.types.Scene.X = StringProperty(...)` form. In |
| 151 | +@@ -192,38 +221,128 @@ def main(): |
| 152 | + # which is what we want anyway. |
| 153 | + bpy.context.scene.gdsii_pdk_selection = args.pdk |
| 154 | + |
| 155 | +- result = bpy.ops.import_scene.gdsii( |
| 156 | +- filepath=str(gds_path), |
| 157 | +- setup_scene=True, |
| 158 | +- create_collection=True, |
| 159 | +- merge_layers=True, |
| 160 | +- color_scheme="realistic", |
| 161 | +- ) |
| 162 | ++ try: |
| 163 | ++ result = bpy.ops.import_scene.gdsii( |
| 164 | ++ filepath=str(gds_path), |
| 165 | ++ setup_scene=True, |
| 166 | ++ create_collection=True, |
| 167 | ++ merge_layers=True, |
| 168 | ++ color_scheme="realistic", |
| 169 | ++ ) |
| 170 | ++ except Exception: |
| 171 | ++ _log_phase("gdsii-import-exception") |
| 172 | ++ traceback.print_exc() |
| 173 | ++ sys.exit("render_gds.py: import_scene.gdsii raised — see traceback above") |
| 174 | + if "FINISHED" not in result: |
| 175 | ++ _log_phase("gdsii-import-nonfinished", extra=f"result={result!r}") |
| 176 | + sys.exit(f"render_gds.py: import_scene.gdsii returned {result!r}") |
| 177 | + |
| 178 | ++ mesh_objs = [o for o in bpy.data.objects if o.type == "MESH"] |
| 179 | ++ total_polys = sum(len(o.data.polygons) for o in mesh_objs) |
| 180 | ++ total_verts = sum(len(o.data.vertices) for o in mesh_objs) |
| 181 | ++ _log_phase( |
| 182 | ++ "gdsii-imported", |
| 183 | ++ extra=f"meshes={len(mesh_objs)} polygons={total_polys} verts={total_verts}", |
| 184 | ++ ) |
| 185 | ++ |
| 186 | + if args.out: |
| 187 | + out_path = Path(args.out).resolve() |
| 188 | + out_path.parent.mkdir(parents=True, exist_ok=True) |
| 189 | + bpy.ops.wm.save_as_mainfile(filepath=str(out_path), copy=True) |
| 190 | ++ _log_phase("blend-saved", extra=f"{out_path.stat().st_size} bytes") |
| 191 | + |
| 192 | + if args.out_html: |
| 193 | ++ # The GLB binary container stores its total length as a uint32 |
| 194 | ++ # (struct.pack("I", length)), and the base64-embedded data URL |
| 195 | ++ # in the HTML must stay under a browser tab's ~2 GB memory cap. |
| 196 | ++ # Microwatt's full layer stack hits both ceilings: 26M input |
| 197 | ++ # polygons × ~6 extruded faces ≈ 200M faces ≈ 10 GB of raw |
| 198 | ++ # vertex+index data, vs. a ~1 GB working budget for an HTML |
| 199 | ++ # viewer. Drop the densest interconnect/contact layers when |
| 200 | ++ # the scene is microwatt-scale; the visually distinctive |
| 201 | ++ # layers (substrate, transistor regions, upper metals + their |
| 202 | ++ # macros) stay. Threshold is empirical — gcd-class designs |
| 203 | ++ # (<1M polygons) stay below it and get the full stack. |
| 204 | ++ if total_polys > 30_000_000: |
| 205 | ++ # sky130-specific layer names; the BlenderGDS addon names |
| 206 | ++ # each per-layer object "L<layer-name>" via the addon's |
| 207 | ++ # configs/sky130.yaml stack. Other PDKs would need a |
| 208 | ++ # different drop set -- leave that for when a non-sky130 |
| 209 | ++ # design hits the same ceiling. |
| 210 | ++ drop_names = { |
| 211 | ++ "Llicon", |
| 212 | ++ "Lli1", |
| 213 | ++ "Lmcon", |
| 214 | ++ "Lmet1", |
| 215 | ++ "Lvia", |
| 216 | ++ "Lvia2", |
| 217 | ++ "Lvia3", |
| 218 | ++ "Lvia4", |
| 219 | ++ } |
| 220 | ++ dropped = [] |
| 221 | ++ for name in sorted(drop_names): |
| 222 | ++ obj = bpy.data.objects.get(name) |
| 223 | ++ if obj is not None and obj.type == "MESH": |
| 224 | ++ dropped.append((name, len(obj.data.polygons))) |
| 225 | ++ bpy.data.objects.remove(obj, do_unlink=True) |
| 226 | ++ kept_objs = [o for o in bpy.data.objects if o.type == "MESH"] |
| 227 | ++ kept_polys = sum(len(o.data.polygons) for o in kept_objs) |
| 228 | ++ _log_phase( |
| 229 | ++ "large-layers-dropped", |
| 230 | ++ extra=( |
| 231 | ++ f"dropped={dropped} " |
| 232 | ++ f"kept_meshes={len(kept_objs)} kept_polys={kept_polys}" |
| 233 | ++ ), |
| 234 | ++ ) |
| 235 | ++ |
| 236 | + # io_scene_gltf2 ships with Blender as a core addon but is not |
| 237 | + # active under --factory-startup. Enable it before exporting. |
| 238 | + bpy.ops.preferences.addon_enable(module="io_scene_gltf2") |
| 239 | ++ _log_phase("gltf-addon-enabled") |
| 240 | + |
| 241 | + glb_tmp = tmp / "scene.glb" |
| 242 | +- export_result = bpy.ops.export_scene.gltf( |
| 243 | +- filepath=str(glb_tmp), |
| 244 | +- export_format="GLB", |
| 245 | +- export_apply=True, |
| 246 | +- export_yup=True, |
| 247 | +- export_lights=False, |
| 248 | +- export_cameras=False, |
| 249 | +- use_active_scene=True, |
| 250 | +- ) |
| 251 | ++ try: |
| 252 | ++ export_result = bpy.ops.export_scene.gltf( |
| 253 | ++ filepath=str(glb_tmp), |
| 254 | ++ export_format="GLB", |
| 255 | ++ export_apply=True, |
| 256 | ++ export_yup=True, |
| 257 | ++ export_lights=False, |
| 258 | ++ export_cameras=False, |
| 259 | ++ use_active_scene=True, |
| 260 | ++ # Per-corner attribute exports use foreach_get under the |
| 261 | ++ # hood and Blender 4.2's RNA bridge stores the element |
| 262 | ++ # count in a signed int32. Microwatt's mcon layer alone |
| 263 | ++ # has ~9.3M extruded polygons × 6 faces × 4 corners ≈ |
| 264 | ++ # 223M corner-normals = 669M float32 values, which |
| 265 | ++ # overflows int32 and yields: |
| 266 | ++ # |
| 267 | ++ # Error: Array length mismatch |
| 268 | ++ # (expected -464636871, got 609104952) |
| 269 | ++ # RuntimeError: internal error setting the array |
| 270 | ++ # |
| 271 | ++ # in gltf2_blender_gather_primitives_extract.__get_normals. |
| 272 | ++ # Disabling normals (and the other per-corner attributes |
| 273 | ++ # gated by foreach_get -- tangents, vertex colors) |
| 274 | ++ # short-circuits the failing path before it allocates |
| 275 | ++ # the oversized buffer. model-viewer falls back to |
| 276 | ++ # face-derived flat normals, which is the right look |
| 277 | ++ # for a chip-layout scene anyway (no smooth shading). |
| 278 | ++ export_normals=False, |
| 279 | ++ export_tangents=False, |
| 280 | ++ export_attributes=False, |
| 281 | ++ export_vertex_color="NONE", |
| 282 | ++ ) |
| 283 | ++ except Exception: |
| 284 | ++ _log_phase("gltf-export-exception") |
| 285 | ++ traceback.print_exc() |
| 286 | ++ sys.exit( |
| 287 | ++ "render_gds.py: export_scene.gltf raised — see traceback above" |
| 288 | ++ ) |
| 289 | + if "FINISHED" not in export_result: |
| 290 | ++ _log_phase("gltf-export-nonfinished", extra=f"result={export_result!r}") |
| 291 | + sys.exit(f"render_gds.py: export_scene.gltf returned {export_result!r}") |
| 292 | ++ _log_phase("gltf-exported", extra=f"glb={glb_tmp.stat().st_size} bytes") |
| 293 | + |
| 294 | + _write_html( |
| 295 | + template_path=Path(args.html_template).resolve(), |
| 296 | +@@ -232,6 +351,10 @@ def main(): |
| 297 | + title=args.title, |
| 298 | + out_path=Path(args.out_html).resolve(), |
| 299 | + ) |
| 300 | ++ _log_phase( |
| 301 | ++ "html-written", |
| 302 | ++ extra=f"out={Path(args.out_html).stat().st_size} bytes", |
| 303 | ++ ) |
| 304 | + finally: |
| 305 | + shutil.rmtree(tmp, ignore_errors=True) |
| 306 | + |
| 307 | +-- |
| 308 | +2.51.0 |
| 309 | + |
0 commit comments