webgl.pyi 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """Pyodide-only WebGL bridge for imgui_bundle.
  2. Available only in Pyodide builds (compile-time gated by
  3. ``IMGUI_BUNDLE_WITH_WEBGL``). On desktop, importing this submodule raises
  4. ``ImportError``; gate with ``imgui_bundle.has_submodule("webgl")``.
  5. Typical usage:
  6. import imgui_bundle
  7. if imgui_bundle.has_submodule("webgl"):
  8. from imgui_bundle import webgl
  9. from js import document
  10. gl = document.getElementById("canvas").getContext("webgl2")
  11. tex = gl.createTexture()
  12. # ... populate the texture via WebGL ...
  13. tex_id = webgl.register_texture(tex)
  14. # ... in the GUI loop ...
  15. imgui.image(imgui.ImTextureRef(tex_id), ImVec2(256, 256))
  16. # ... at shutdown ...
  17. webgl.unregister_texture(tex_id)
  18. gl.deleteTexture(tex)
  19. See ``_plans/pyodide_webgl_bridge__spec.md`` for the design rationale.
  20. """
  21. def register_texture(jstexture: object) -> int:
  22. """Register a JS WebGLTexture with imgui_bundle's renderer.
  23. Returns an ``ImTextureID`` (int) usable with ``imgui.image()`` via
  24. ``imgui.ImTextureRef``. The integer is stable for the lifetime of the
  25. registration; the underlying JS texture is referenced (not copied) by
  26. imgui_bundle's ``GL.textures`` table.
  27. The caller still owns the JS texture and is responsible for eventually
  28. calling ``gl.deleteTexture()`` on it. Call ``unregister_texture(tex_id)``
  29. first.
  30. """
  31. ...
  32. def unregister_texture(tex_id: int) -> None:
  33. """Release the slot in imgui_bundle's ``GL.textures`` for ``tex_id``.
  34. Does NOT call ``gl.deleteTexture()`` on the underlying JS texture.
  35. After this call, the integer ID is invalid and must not be passed to
  36. ``imgui.image()``.
  37. """
  38. ...