Skip to content

Fix BigInt/mjtSize WASM bindings and material transparency in demo viewer (#3496) - #3501

Open
Adityakk9031 wants to merge 2 commits into
google-deepmind:mainfrom
Adityakk9031:fix-wasm-bigint-geom-rendering
Open

Fix BigInt/mjtSize WASM bindings and material transparency in demo viewer (#3496)#3501
Adityakk9031 wants to merge 2 commits into
google-deepmind:mainfrom
Adityakk9031:fix-wasm-bigint-geom-rendering

Conversation

@Adityakk9031

@Adityakk9031 Adityakk9031 commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Fixes #3496

Root Cause

MjModel.tex_adr is mjtSize* (int64_t*). Emscripten's typed_memory_view over a 64-bit integer pointer surfaces as a BigInt64Array in JavaScript. Any arithmetic on its elements (e.g. tex_adr[i] * 3 to compute byte offsets) throws:

  • Chrome: Cannot mix BigInt and other types, use explicit conversions
  • Firefox: can't convert BigInt to number

This is the root cause of the viewer error [mujoco stage: geom[0] texture] can't convert BigInt to number: the texture staging code reads tex_adr, hits BigInt, and the material setup aborts — leaving the geom with the wrong color.

Scalar mjtSize fields already avoid this by casting to int in the code generator. This PR extends the same policy to pointer (array) fields.

Changes

1. wasm/codegen/generators/structs.py — Binding generator (root cause fix)

When generating an accessor for a dynamically-sized pointer field whose inner type is mjtSize, emit an Int32Array copy instead of a BigInt64Array typed_memory_view. The WASM heap is 32-bit addressed so values always fit an int.

2. wasm/codegen/generated/bindings.h — Regenerated output

tex_adr() now returns an Int32Array copy, consistent with tex_height, tex_width, and tex_nchannel.

3. wasm/tests/bindings_test.ts — Test coverage

Added assertions that:

  • tex_adr is an Int32Array (not BigInt64Array)
  • Arithmetic on tex_adr[0] does not throw

4. wasm/demo_app/app.ts — Defensive consumer-side fixes

  • Explicit Number() coercion on all mjvGeom fields (type, dataid, size, rgba, mat, pos) and geoms.size() / mjData.time
  • Removed duplicate geoms.get(i) call (memory leak)
  • Fixed transparent condition: rgba[3] < 1.0 instead of rgba[3] !== 0
  • Material color/transparency now updates dynamically every frame

Before / After

// Before
model.tex_adr          // BigInt64Array [ 0n, 786432n ]
model.tex_adr[1] * 3   // throws: can't convert BigInt to number

// After
model.tex_adr          // Int32Array [ 0, 786432 ]
model.tex_adr[1] * 3   // 2359296

@Adityakk9031 Adityakk9031 changed the title Fix BigInt conversion and material transparency in WASM demo viewer (#3496) Fix BigInt/mjtSize WASM bindings and material transparency in demo viewer (#3496) Aug 22, 2026
@Adityakk9031

Copy link
Copy Markdown
Contributor Author

@yuvaltassa have a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[mujoco stage: geom[0] texture] can't convert BigInt to number

1 participant