gl.cr
OpenGL and OpenGL ES for Crystal
This shard combines the former CrystGL and LibGL packages: require "gl" exposes GL wrappers and LibGL C bindings; require "lib_gl" imports only the bindings. It does not create a context or depend on Garnet.
Use GL::Buffer, GL::Texture, GL::Shader, GL::Program and GL::VertexArray with an application-owned current context. Context creation belongs to a separate host such as GLFW or EGL. Native commands remain available through LibGL; the existing named wrappers cover a subset of the API.
Compile with -Dgles to link GLESv2 instead of desktop GL. require "gl/profile" provides GL::Profile.current for a current context. Shared bindings do not imply every command exists on both profiles: callers must respect the selected API version and extensions. Garnet's current renderer requires desktop GL 3.3 or GLES 3.0.
From the workspace, scripts/check-render-api gl checks buffer upload/readback against a real desktop context; scripts/check-gles exercises Garnet through an EGL GLES context. GLES has been tested locally on desktop; Android/iOS application hosts remain future work.
Ownership
Owned core resource wrappers schedule native deletion when collected. Each resource captures its GL::Context at creation. Finalizers link a preallocated deletion record into that context's queue; they never call GL. Generated calls drain queued deletions while the context is current, and context.collect_garbage can drain them explicitly without forcing a GC cycle. delete uses the same idempotent path immediately.
The host must set GL::Context.current to the matching dispatcher whenever it switches the native context. Call context.close while that native context is current, immediately before destroying it. Closing drains pending deletions, clears binding references and invalidates surviving wrappers; the host's subsequent native-context destruction reclaims remaining context resources. A dispatcher does not create, switch or destroy a native context itself. Shared native contexts require their own host lifetime policy.
Typed vertex-array attribute setup retains its buffers; typed framebuffer attachments retain their textures/renderbuffers; program attachment retains shaders. Generated direct-state attachment calls retain their typed dependencies too. These references are conservative: replaced attachments can remain retained until the container is deleted or collected. Context binding references keep bound objects reachable until changed or unbound. Ordinary GL driver rules handle work already submitted to the GPU.
Native-handle wrap(handle, context) borrows by default. For a newly owned handle use owned: true exactly once. Core generated resource types such as Framebuffer, Renderbuffer, Sampler and Query also provide owned constructors. Legacy/vendor handle types without a mapped destructor remain explicit interoperability types. Raw IDs, pointer arrays and direct LibGL calls bypass wrapper relationships and must obey native ownership rules. The deletion queue is synchronized; concurrent use of a GL context still requires host synchronization.
The optimized GL tests check actual GC cleanup, vertex-array and framebuffer dependencies, and idempotent deletion. Optimization avoids conservative stack roots artificially extending test object lifetimes.
API reference
Run crystal docs src/gl.cr --output=/tmp/gl-reference from this shard, or scripts/check-api-docs /tmp/garnet-reference gl.cr from the workspace. The gl entry point selects the active generated registry. A scan of every source file also reaches legacy declarations and can report conflicting signatures.
Generated methods and enums mirror the native GL and GLES APIs. For command semantics and per-profile availability, use the Khronos OpenGL reference pages.
Regenerating bindings
From this shard directory:
crystal run tools/generate_api.cr
The Crystal generator reads the vendored Khronos tools/registry/gl.xml, applies the receiver annotations in tools/receivers.json, and emits native declarations, enum groups, receiver methods and tools/coverage.json. tools/native_names.json preserves existing Crystal names for native declarations. Generation requires no Python or network access.
gl.cr
- 0
- 0
- 0
- 2
- 1
- 1 day ago
- November 30, 2026
MIT License
Wed, 02 Dec 2026 02:09:10 GMT