renderer-gl

Garnet engine OpenGL renderer

Garnet OpenGL

require "garnet-renderer-gl" exposes the single Garnet::OpenGL::Renderer. It consumes generic graphics descriptions without loading ECS, GLFW, STB or GLEW, and performs no require-time graphics initialization.

require "garnet-renderer-gl"
require "garnet-platform-glfw"

Garnet::Platform::GLFWHost.open(800, 600, "Game") do |host|
  renderer = Garnet::OpenGL::Renderer.new
  begin
    # Build generic texture/mesh/material draws, then:
    output = renderer.render(draws, Garnet::Graphics::RenderTarget.new(256, 256))
    native = output.to_gpu(renderer)
    renderer.present(screen_draws, *host.framebuffer_size)
    host.swap_buffers
    pixels = renderer.readback(output) # optional; render itself stays on GPU
  ensure
    renderer.close
  end
end

The snippet sketches application-owned draw lists; see the executable example for a complete program. Native execution requires a current OpenGL 3.3 core context, kept on the renderer's owner until teardown. The renderer owns GL state while in use. Arbitrarily interleaving native calls is not supported.

Preparation and ownership

Texture#to_gpu(renderer) delegates to Renderer#prepare and returns a retained NativeTexture. A descriptor cache hit does not open/decode content. A miss opens its source, decodes and validates metadata, uploads, closes the reader and releases temporary CPU data. Client-memory OpenGL upload calls consume the passed data before returning; no upload pointer escapes.

The descriptor cache weakly references generic resources and strongly retains native wrappers. Native wrappers do not point back to the descriptor. Native-source textures return their wrapper directly without another cache entry. Cache keys are process-local object identities guarded by weak referent identity checks against reuse, not persisted asset IDs. Scalar stream caches remain independent for positions, UVs and indices.

render creates a distinct GPU allocation/result version. renderer.texture(native) wraps a retained native texture as a generic source; readback(texture) explicitly obtains a CPU Image. OpenGL output's vertical orientation is handled during sampling/readback. Same-renderer native sources can be reused; other renderer/device identities fail with Graphics::IncompatibleDevice until an import/transfer path is implemented.

collect drops expired descriptor associations and drains deletions queued by the GL shard. evict drops cached preparations, so reloadable generic descriptors prepare again next time. Retained native wrappers and GPU-output descriptors survive eviction. Both maintenance operations wait for GPU completion before native deletion. Invoke maintenance even for static scenes when resources have been dropped. present also sweeps automatically every auto_collect_interval (two seconds by default; nil disables it), so removing content from a scene is enough to release its GPU copies once the GC has reclaimed the source. Collection timing follows GC; it is not a deterministic VRAM budget guarantee.

close waits, releases renderer-owned buffers/program state and invalidates retained outputs; it is idempotent. Native textures are typed GL::Texture objects held by their wrappers, with finalization handled by the GL shard. Keep the matching GL::Context current and close the renderer before destroying the native context. The GLFW host scopes the dispatcher automatically; other hosts should wrap their current-context lifetime in GL::Context.open. No GL calls run directly in finalizers.

Asynchronous fences, cross-device transfers, hard residency budgets and dedicated render-thread orchestration remain future work. Default framebuffer sRGB conversion is enabled where supported; HDR/display color management is not implemented. Direct GL symbols have been tested on macOS; Windows needs a function-loading path before claiming support.

Scene integration

require "garnet-scene" loads backend-neutral ECS components and Garnet::Scene. It extracts mixed drawings with accumulated placement, independently sized sprites, mesh/vector primitives, group opacity, masks and viewports. See scene and vector.

The garnet-renderer-gl/systems/render producer renders these descriptions and presents the resulting texture through its GLFW host. The migrated card example supplies a centered view explicitly. No global GLFW initialization or hidden renderer creation occurs on require.

Verification and examples

From the workspace:

scripts/check-graphics unit
scripts/check-graphics scene
make -C LibSTBImage/src/lib_stb_image/ext
scripts/check-graphics decoder
scripts/check-graphics gpu
scripts/check-graphics build
scripts/check-graphics card

Executables are ${TMPDIR:-/tmp}/garnet-graphics-example and garnet-card-example. Set GARNET_EXAMPLE_FRAMES=120 for an automatically closing run. GPU tests check lazy acquisition/closure, cache reuse, native retention through eviction, incompatible devices, explicit readback, output versions, alpha/color/order and deformation. Scene tests run without creating a window.

Native API and profiles

The standalone gl.cr package exports GL and LibGL. Garnet rendering policy lives here; window/context setup lives in separate hosts. Compile with -Dgles for GLES 3 support; desktop requires GL 3.3. scripts/check-gles exercises the same generic primitives using the separate EGL pbuffer host. Desktop GLES validation does not establish Android/iOS host support.

Gradient materials use local geometry coordinates and native fragment evaluation on this backend. Linear and centered radial gradients support pad/repeat/reflect spread and up to 16 stops. See the shared paint contract for interpolation and limits.

Image-pattern materials support per-axis repeat/reflect/pad/transparent addressing and local paint transforms. Native shaders honor atlas rotation/trim and filter premultiplied texels without atlas-neighbor bleed. Captured GPU textures are valid pattern sources; see the image-pattern contract.

The shared composition pipeline also supports two-circle gradients, Gaussian blur, color-matrix effect chains and all sixteen isolated blend modes. See the graphics effect contract for ordering, color space and limits. These are native GPU passes; optional Clipper2 geometry arrives as ordinary meshes.

Use renderer.dynamic_texture(width, height) for writable RGBA8 textures, and renderer.update_texture(texture, bytes) to reuse their GPU storage. Rows are top-down. Supply x:, y:, width:, height:, and optional stride: for partial updates. Metal and Vulkan expose the same calls. Upload the texels before they are sampled.

For a texture or offscreen render, renderer.readback(texture) returns an image. To capture what the OpenGL host is about to display, call renderer.capture_frame(*host.framebuffer_size) after present and before swap_buffers. Use image.bytes for a top-down copy of the pixels.

Repository

renderer-gl

Owner
Statistic
  • 0
  • 0
  • 0
  • 6
  • 9
  • about 8 hours ago
  • November 30, 2026
License

MIT License

Links
Synced at

Wed, 02 Dec 2026 04:40:12 GMT

Languages