~eliasnaur/gio-example

a1c6599996e368369847830226add3a241b997a8 — Elias Naur a month ago 8edd237
opengl: use Metal backend on macOS

Metal performs better on resize and display scaling changes, and
is compatible with a future macOS without OpenGL support.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
3 files changed, 19 insertions(+), 4 deletions(-)

M go.mod
M go.sum
M opengl/main.go
M go.mod => go.mod +1 -1
@@ 3,7 3,7 @@ module gioui.org/example
go 1.16

require (
	gioui.org v0.0.0-20210519204653-f8d201b7c23d
	gioui.org v0.0.0-20210612180752-0e592f8bc6b3
	gioui.org/x v0.0.0-20210419013052-6db76265c4e1
	gioui.org/x/haptic v0.0.0-20210120222453-b55819bc712b
	gioui.org/x/notify v0.0.0-20210120222453-b55819bc712b

M go.sum => go.sum +2 -2
@@ 34,8 34,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gioui.org v0.0.0-20210116085804-99bfa6a33cdf/go.mod h1:Y+uS7hHMvku1Q+ooaoq6fYD5B2LGoT8JtFgvmYmRzTw=
gioui.org v0.0.0-20210402191542-ce7f0da06ee3/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zumjgTw83q2ge/PI+yyw8=
gioui.org v0.0.0-20210519204653-f8d201b7c23d h1:G3cXJpxyR/5wTP7MeQPltmoMEi5as4SDlEMRRaYKKio=
gioui.org v0.0.0-20210519204653-f8d201b7c23d/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zumjgTw83q2ge/PI+yyw8=
gioui.org v0.0.0-20210612180752-0e592f8bc6b3 h1:hiIdcMrjArBJ0YDQafZJph9jIOnHNdtxVKXuszV17Bg=
gioui.org v0.0.0-20210612180752-0e592f8bc6b3/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zumjgTw83q2ge/PI+yyw8=
gioui.org/x v0.0.0-20210419013052-6db76265c4e1 h1:NfHFIxNzNY/s7ePtrf9nCKr9f3gYiVS40rAIdbj5Q1k=
gioui.org/x v0.0.0-20210419013052-6db76265c4e1/go.mod h1:qsAS5EBzGhn3sJ98FJdA+ae+E5/DgmxjySERjjR1Zvg=
gioui.org/x/haptic v0.0.0-20210120222453-b55819bc712b h1:3lHNKrTHXJCXhtmCVdTInlNuay3jSQ0SVi9JNQTj5yM=

M opengl/main.go => opengl/main.go +16 -1
@@ 7,6 7,8 @@ package main
// This program demonstrates the use of a custom OpenGL ES context with
// app.Window. It is similar to the GLFW example, but uses Gio's window
// implementation instead of the one in GLFW.
//
// The example runs on macOS and Windows using ANGLE.

import (
	"errors"


@@ 35,6 37,8 @@ import (

#include <EGL/egl.h>
#include <GLES2/gl2.h>
#define EGL_EGLEXT_PROTOTYPES
#include <EGL/eglext.h>

*/
import "C"


@@ 163,7 167,18 @@ func drawUI(th *material.Theme, gtx layout.Context) layout.Dimensions {
}

func createContext(view C.EGLNativeWindowType) (*eglContext, error) {
	disp := C.eglGetDisplay(C.EGL_DEFAULT_DISPLAY)
	platformExts := strings.Split(C.GoString(C.eglQueryString(C.EGL_NO_DISPLAY, C.EGL_EXTENSIONS)), " ")
	platformType := C.EGLint(C.EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE)
	if hasExtension(platformExts, "EGL_ANGLE_platform_angle_metal") {
		// The Metal backend works better than the OpenGL backend.
		platformType = C.EGL_PLATFORM_ANGLE_TYPE_METAL_ANGLE
	}
	attrs := []C.EGLint{
		C.EGL_PLATFORM_ANGLE_TYPE_ANGLE,
		platformType,
		C.EGL_NONE,
	}
	disp := C.eglGetPlatformDisplayEXT(C.EGL_PLATFORM_ANGLE_ANGLE, nil, &attrs[0])
	if disp == 0 {
		return nil, fmt.Errorf("eglGetPlatformDisplay failed: 0x%x", C.eglGetError())
	}