glow

repository·main·Indexed 23 days ago

https://github.com/grovesnl/glow

A set of cross-platform bindings for OpenGL, OpenGL ES, and WebGL (version 0.18.0) that enables writing graphics code for native platforms and the web without target-specific logic. It provides a unified interface via the HasContext trait to manage graphics objects like shaders, programs, buffers, and textures across different backends.

Tokens
12K
Snippets
13
Records
71
Agent score
81%

What's inside glow

  1. Overview of glow

    main
    glow is a set of bindings designed to run OpenGL, OpenGL ES, and WebGL anywhere. It provides a unified interface to avoid writing target-specific graphics code, allowing you to write code that works across native platforms and the web.
  2. Build and run Glow natively

    main

    To run the hello example on native platforms, use cargo run with the appropriate feature flag for your windowing backend:

    • For glutin and winit: cargo run --features=glutin_winit
    • For sdl2: cargo run --features=sdl2
    cargo run --features=glutin_winit
    # or
    cargo run --features=sdl2
  3. Build and run Glow for Web (wasm64/memory64)

    main

    Building for wasm64-unknown-unknown requires a nightly Rust toolchain and a from-source build of std because it is a tier-3 target.

    1. Install the nightly toolchain and rust-src component: rustup toolchain install nightly --component rust-src.
    2. Navigate to examples/hello.
    3. Build using the +nightly toolchain with the -Z build-std flag: cargo +nightly build --target wasm64-unknown-unknown -Z build-std=std,panic_abort.
    4. Generate web assets with wasm-bindgen and copy index.html to the generated folder.

    To view the result, open generated/index.html in a browser that supports the WebAssembly memory64 proposal (recent versions of Chrome and Firefox enable this by default).

    rustup toolchain install nightly --component rust-src
    cargo +nightly build --target wasm64-unknown-unknown -Z build-std=std,panic_abort
    mkdir -p generated
    wasm-bindgen ../../target/wasm64-unknown-unknown/debug/hello.wasm --out-dir generated --target web
    cp index.html generated
  4. Build and run Glow for Web (wasm32)

    main

    To build the hello example for the web using the wasm32-unknown-unknown target, follow these steps:

    1. Navigate to the example directory: cd examples/hello.
    2. Ensure your wasm-bindgen-cli version matches the version in Cargo.lock by installing it via cargo install wasm-bindgen-cli --version <version-from-Cargo.lock>.
    3. Build the target: cargo build --target wasm32-unknown-unknown.
    4. Generate the web assets using wasm-bindgen and move the index.html into the output directory.

    Note: The wasm-bindgen command expects the path to the .wasm file relative to your current location in the examples/hello directory.

    cd examples/hello
    cargo build --target wasm32-unknown-unknown
    mkdir -p generated
    wasm-bindgen ../../target/wasm32-unknown-unknown/debug/hello.wasm --out-dir generated --target web
    cp index.html generated
  5. Core OpenGL object types in glow

    main

    The following types are associated with a Context implementing HasContext. They represent the primary handles used in graphics programming:

    • Shader: Handle to a shader object.
    • Program: Handle to a shader program.
    • Buffer: Handle to a buffer object.
    • VertexArray: Handle to a vertex array object (VAO).
    • Texture: Handle to a texture object.
    • Sampler: Handle to a sampler object.
    • Fence: Handle to a sync object/fence.
    • Framebuffer: Handle to a framebuffer object.
    • Renderbuffer: Handle to a renderbuffer object.
    • Query: Handle to a query object.
    • UniformLocation: Handle to a uniform location within a program.
    • TransformFeedback: Handle to a transform feedback object.
  6. Understand the HasContext trait and OpenGL abstraction

    main

    The HasContext trait is the core abstraction in glow. It allows the library to provide a unified API for different graphics backends (like native OpenGL or WebGL via web-sys).

    Any type implementing HasContext acts as a provider for OpenGL functions and manages the lifecycle of various graphics objects. The trait uses associated types to define what specific types represent shaders, programs, buffers, etc., for that particular context.

    Safety Warning: All GL API usage must be valid according to the relevant GL specification. glow does not enforce these rules; the caller is responsible for ensuring that the context is valid and that calls follow the specification for the specific backend being used.

  7. Manage pixel data with PixelPackData and PixelUnpackData

    main

    When performing texture operations like get_tex_image, glow uses enums to specify how pixel data is provided or received:

    • PixelPackData<'a>: Used when sending data to the GPU (e.g., BufferOffset(u32) or Slice(Option<&'a mut [u8]>)).
    • PixelUnpackData<'a>: Used when receiving data from the GPU (e.g., BufferOffset(u32) or Slice(Option<&'a [u8]>)).
    • CompressedPixelUnpackData<'a>: Specifically for compressed data, using BufferRange(core::ops::Range<u32>) or Slice(&'a [u8]).
  8. WebGL Texture Sub-Image Operations

    main

    The tex_sub_image_2d and tex_sub_image_3d methods allow updating parts of textures. The behavior depends on the PixelUnpackData provided:

    2D Sub-Image (tex_sub_image_2d)

    • WebGL 1: Supports PixelUnpackData::Slice. Using PixelUnpackData::BufferOffset is not supported and will panic!.
    • WebGL 2: Supports both PixelUnpackData::Slice and PixelUnpackData::BufferOffset.

    3D Sub-Image (tex_sub_image_3d)

    • WebGL 1: Not supported (will panic!).
    • WebGL 2: Supports both PixelUnpackData::Slice and PixelUnpackData::BufferOffset.

    Compressed Sub-Image (compressed_tex_sub_image_2d/3d)

    • WebGL 1: Supports CompressedPixelUnpackData::Slice. Using CompressedPixelUnpackData::BufferRange is not supported and will panic!.
    • WebGL 2: Supports both CompressedPixelUnpackData::Slice and CompressedPixelUnpackData::BufferRange.
  9. Native OpenGL resource types

    main

    The native backend uses specific wrapper types to represent OpenGL objects. These types ensure type safety when interacting with the Context. Common types include:

    • NativeShader
    • NativeProgram
    • NativeBuffer
    • NativeVertexArray
    • NativeTexture
    • NativeSampler
    • NativeFence
    • NativeFramebuffer
    • NativeRenderbuffer
    • NativeQuery
    • NativeUniformLocation
    • NativeTransformFeedback
  10. WebGL2-only 3D texture operations

    main

    The following operations are WebGL2 only. Attempting to call them while using a WebGL1 context will result in a panic:

    • tex_image_3d_with_image_bitmap
    • tex_image_3d_with_html_canvas_element
    • tex_image_3d_with_html_image_element
    • tex_image_3d_with_html_video_element
    • tex_image_3d_with_video_frame
    • tex_image_3d_with_image_data
    • tex_sub_image_3d_with_image_bitmap
    • tex_sub_image_3d_with_html_canvas_element
    • tex_sub_image_3d_with_html_image_element
    • tex_sub_image_3d_with_html_video_element
    • tex_sub_image_3d_with_video_frame
    • tex_sub_image_3d_with_image_data