Skip to content

Commit

Permalink
internal/graphicsdriver/opengl: bug fix: use GLX when possible for ES
Browse files Browse the repository at this point in the history
Use GLX when possible. EGL with an X window might not work well on
Wayland unfortunately.

Closes #3152
  • Loading branch information
hajimehoshi committed Nov 6, 2024
1 parent 332c682 commit 1e583e6
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions internal/graphicsdriver/opengl/graphics_glfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,63 @@
package opengl

import (
"bufio"
"bytes"
"fmt"
"os/exec"
"runtime"
"strings"

"github.com/hajimehoshi/ebiten/v2/internal/glfw"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl"
"github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk"
)

func isGLXExtensionForGL2Available() bool {
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
return false
}

var buf bytes.Buffer
cmd := exec.Command("glxinfo")
cmd.Stdout = &buf
if err := cmd.Run(); err != nil {
return false
}

const (
indent = " "
ext = "GLX_EXT_create_context_es2_profile"
)

var listingExtensions bool
s := bufio.NewScanner(&buf)
for s.Scan() {
if !listingExtensions {
if s.Text() == "GLX extensions:" {
listingExtensions = true
}
continue
}

if !strings.HasPrefix(s.Text(), indent) {
listingExtensions = false
break
}

line := s.Text()
for len(line) > 0 {
head, tail, _ := strings.Cut(line, ",")
if strings.TrimSpace(head) == ext {
return true
}
line = tail
}
}
return false
}

type graphicsPlatform struct {
window *glfw.Window
}
Expand Down Expand Up @@ -60,8 +108,12 @@ func setGLFWClientAPI(isES bool) error {
if err := glfw.WindowHint(glfw.ContextVersionMinor, 0); err != nil {
return err
}
if err := glfw.WindowHint(glfw.ContextCreationAPI, glfw.EGLContextAPI); err != nil {
return err
// Use GLX if the extension allows, or use EGL otherwise.
// Prefer GLX since EGL might not work well on Wayland (#3152).
if !isGLXExtensionForGL2Available() {
if err := glfw.WindowHint(glfw.ContextCreationAPI, glfw.EGLContextAPI); err != nil {
return err
}
}
return nil
}
Expand Down

0 comments on commit 1e583e6

Please sign in to comment.