glad Loader-Generator

repository·glad2·Indexed 26 days ago

https://github.com/dav1dde/glad

A multi-language loader-generator for Vulkan, OpenGL, GLES, EGL, GLX, and WGL based on official specifications. It allows developers to generate source code and load function pointers for graphics APIs across various programming languages, including C/C++ and Rust, via a CLI tool or the glad2 web service.

Tokens
1.5K
Snippets
7
Records
12
Agent score
87%

What's inside glad

  1. Generate loader files using the glad2 web service

    glad2

    To generate the specific loader files required for your project (Vulkan, GL, GLES, EGL, GLX, or WGL) in multiple languages, use the official glad2 web service. This is the recommended way to obtain the generated source code tailored to your specific API requirements.

    https://glad.sh
  2. Explore glad language examples

    glad2

    The repository contains various implementation examples for different APIs and windowing libraries. Key examples include:

    C/C++

    • GL with GLFW (standard and on-demand loading)
    • GL with SDL2 and SDL3 (including callbacks)
    • Vulkan with GLFW
    • Platform-specific loaders: GLX, WGL, and EGL X11

    Rust

    • GL with GLFW (standard and multiple windows/contexts)
  3. Specify APIs and Profiles

    glad2

    When using the --api flag, you can specify exact versions and profiles using the following syntax:

    • name:profile=version (e.g., gl:core=3.3)
    • name:profile/spec=version (e.g., gles1/gl=2)

    If no version is provided, the latest version is used. A profile is only mandatory if the specific API requires one.

  4. Initialize glad with the glfw crate

    glad2

    When using the glfw crate in Rust, you can initialize glad by calling gl::load. You must provide a closure that maps OpenGL function pointers to the addresses provided by glfw.get_proc_address_raw.

    gl::load(|e| glfw.get_proc_address_raw(e) as *const std::os::raw::c_void);
  5. Initialize OpenGL with gladLoadGL and GLFW

    glad2

    When using GLFW, you must include glad/gl.h before any other graphics headers (like GLFW/glfw3.h). To initialize the OpenGL context, call gladLoadGL passing the glfwGetProcAddress function. If the returned version is 0, initialization failed. You can retrieve the loaded version using the GLAD_VERSION_MAJOR and GLAD_VERSION_MINOR macros.

    #include <glad/gl.h>
    // GLFW (include after glad)
    #include <GLFW/glfw3.h>
    
    
    int main() {
        // -- snip --
    
        GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", NULL, NULL);
        glfwMakeContextCurrent(window);
    
        int version = gladLoadGL(glfwGetProcAddress);
        if (version == 0) {
            printf("Failed to initialize OpenGL context\n");
            return -1;
        }
    
        // Successfully loaded OpenGL
        printf("Loaded OpenGL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
    
        // -- snip --
    }
  6. Initialize glad with glfw in Rust

    glad2

    To use glad with the glfw crate in Rust, you need to initialize glad by providing a loader function that maps OpenGL function pointers to the addresses provided by glfw.

    Use the gl::load method with a closure that calls glfw.get_proc_address_raw(e) for each entry e.

    gl::load(|e| glfw.get_proc_address_raw(e) as *const std::os::raw::c_void);
  7. Reference: Global Configuration Options

    glad2

    These global flags apply to all generator subcommands. They define the core parameters for the Khronos-XML specification generation.

    --out-path <path> (Required)
        Output directory for the generated files
    
    --api <api_list> (Required)
        Comma separated list of APIs in `name:profile=version` pairs 
        optionally including a specification `name:profile/spec=version`. 
        No version means latest, a profile is only required if the API requires a profile. 
        E.g. `gl:core=3.3,gles1/gl=2,gles2`
    
    --extensions <extensions>
        Path to a file containing a list of extensions or 
        a comma separated list of extensions. If missing, 
        all possible extensions are included.
    
    --merge
        Merge multiple APIs of the same specification into one file.
    
    --quiet
        Disable logging.
    
    --reproducible
        Makes the build reproducible by not fetching the latest 
        specification from Khronos.
  8. Configure the glad generator via CLI

    glad2
    The glad CLI tool uses a set of global configuration options to control the generation process. These options must be provided alongside a language-specific subcommand (e.g., c for C, py for Python, etc., depending on available generators).