OpenGL context version not respected on gtk4-rs

Hello,
I’m very new to the gtk4 rust bindings, so I may be doing something wrong.

I have this simple setup of a GLArea inside a top-level window. I use the gl crate for GL calls.

use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, GLArea, Inhibit, Widget, Window};
use gtk::gdk::GLContext;
use std::ffi::{CStr, c_char};

const APP_ID: &str = "my.app";

fn main() {
    // Initialize GL function pointers
    #[cfg(target_os = "macos")]
    let library = unsafe { libloading::os::unix::Library::new("libepoxy.0.dylib") }.unwrap();
    #[cfg(all(unix, not(target_os = "macos")))]
    let library = unsafe { libloading::os::unix::Library::new("libepoxy.so.0") }.unwrap();
    #[cfg(windows)]
    let library = libloading::os::windows::Library::open_already_loaded("epoxy-0.dll").unwrap();

    epoxy::load_with(|name| {
        unsafe { library.get::<_>(name.as_bytes()) }
        .map(|symbol| *symbol)
            .unwrap_or(std::ptr::null())
    });
    gl::load_with(|s| epoxy::get_proc_addr(s));

    let app = Application::builder().application_id(APP_ID).build();
    app.connect_activate(build_ui);
    app.run();
}

fn build_ui(app: &Application) {
    let window = ApplicationWindow::builder()
        .application(app)
        .title("Gull")
        .build();

    let gl_canvas = GLArea::builder()
        .auto_render(false)
        .build();
    gl_canvas.set_required_version(4, 2);
    gl_canvas.connect_render(render);

    window.set_child(Some(&gl_canvas));
    window.show();
}

fn render(_canvas: &GLArea,  context: &GLContext) -> Inhibit {
    unsafe {
        gl::ClearColor(1.0, 0.0, 0.0, 1.0);
        gl::Clear(gl::COLOR_BUFFER_BIT);
    }
    
    let (major, minor) = context.version();
    println!("OpenGL version: {}.{}", major, minor);

    Inhibit(true)
}

I get a window filled with a red background as expected, but though I’m asking for GL 4.2, I see 2 different behaviors on 2 different machines:

  • On an Intel laptop, running wayland, I get GL 4.6 (indeed supported as reported by glxinfo)
  • On an NVidia desktop running X11 and proprietary drivers, I get 3.2 (4.6 is also supported)

Is it a normal operation of the GLArea widget or am I doing something wrong ? Maybe a peculiarity of the rust bindings ?

Ok some clarification after more tests:

  • getting a higher GL version than requested is actually normal
  • this is not a Rust issue, same behavior happens in pure C

The remaining question is: why is the best GL version obtained on nvidia proprietary drivers only 3.2 ?

I tried against the current development version, and it behaves as expected (as opposed to fedora’s gtk 4.6). Now the requested version is satisfied (and not even bumped up to the maximum supported on the system). This works on the Rust bindings as well.This works on the Rust bindings as well.

I’m not sure what released or yet to be released version of Gtk fixes it (4.8 or later).

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.