go-gl / gl

Go bindings for OpenGL (generated via glow)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

v3.1/gles2: installing GLES2 shows "fatal error: KHR/khrplatform.h: No such file or directory"

zzador opened this issue · comments

Tried to download & install GLES2 with go get -u github.com/go-gl/gl/v3.1/gles2. Git the following error: fatal error: KHR/khrplatform.h: No such file or directory.

Im using GO 1.14.2 with MinGW GCC 8.1.0.
All correctly installed.

The file "khrplatform.h" does simply not exist within the include directories of GCC.

commented

meto
OS:MacOS 10.15.7
GO:1.14.7

go/src/github.com/go-gl/gl/v3.1/gles2/package.go:38:11: fatal error: 'KHR/khrplatform.h' file not found
 #include <KHR/khrplatform.h>
          ^~~~~~~~~~~~~~~~~~~
1 error generated.

Bump! I am having the same exact error.
I know macOS by default does not support GLES2 but using google/ANGLE this should have faux support. I will try to compile ANGLE for macOS and report back.

Having some difficulties trying to get ANGLE to work.
go-gl/glow#51 (comment)

I am able to get this works with Google/ANGLE. What I did is to set the CFlags and LDFlags like this:

CGO_CFLAGS=-I/Users/zhao/workspace/angle/include
CGO_LDFLAGS=-L/Users/zhao/workspace/angle/out/Release

In /Users/zhao/workspace/angle/out/Release it contains these two libs: libEGL.dylib and libGLESv2.dylib

Here is the PR to allow building go-gl/gl with tags on macOS: #134

Similar to windows, also need to set the flags above and link to libEGL.dll, libGLESv2.dll and d3dcompiler_47.dll

Here is the related PR for windows: #133

Note: this is for the Intel Mac. I failed to build ANGLE with M1 Mac.

@zwang wow thanks! I will try it out when I get a chance and write back with my results.

@zwang I managed to build it after several hours of messing with go mod and KHR deps.
EDIT: I ended up copying the entire KHR directory to the path pointed to by CGO_CFLAGS
cp -r /usr/local/gopath/src/github.com/go-gl/glfw/v3.1/glfw/glfw/deps/KHR /Users/username/workspace/angle/include/

But i get the following error. Please help 😅
EDIT: glfw/glfw#1169

test20-vbo: go build
test20-vbo: go run .
2021/04/25 16:27:12 PlatformError: EGL: Failed to create window surface: A NativeWindowType argument does not refer to a valid native window
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x40c0022]

goroutine 1 [running, locked to thread]:
github.com/pb/example/vendor/github.com/go-gl/glfw/v3.3/glfw.(*Window).MakeContextCurrent.func1(0x0)
	/usr/local/gopath/src/github.com/pb/example/vendor/github.com/go-gl/glfw/v3.3/glfw/context.go:17 +0x22
github.com/pb/example/vendor/github.com/go-gl/glfw/v3.3/glfw.(*Window).MakeContextCurrent(0x0)
	/usr/local/gopath/src/github.com/pb/example/vendor/github.com/go-gl/glfw/v3.3/glfw/context.go:17 +0x2b
main.main()
	/usr/local/gopath/src/github.com/pb/example/gles20-cube/test20-vbo/triangle.go:52 +0x1a0
exit status 2

EDIT: If i remove the following hint/line from my code glfw.WindowHint(glfw.ClientAPI, glfw.OpenGLESAPI), then i get the following error.

GLFW: An invalid error was not accepted by the caller: FormatUnavailable: EGL: Failed to find a suitable EGLConfig
GLFW: Please report this bug in the Go package immediately.
panic: FormatUnavailable: EGL: Failed to find a suitable EGLConfig

goroutine 1 [running, locked to thread]:
github.com/pb/example/vendor/github.com/go-gl/glfw/v3.3/glfw.acceptError(0xc000040e98, 0x2, 0x2, 0x0, 0x0)
	/usr/local/gopath/src/github.com/pb/example/vendor/github.com/go-gl/glfw/v3.3/glfw/error.go:178 +0x201
github.com/pb/example/vendor/github.com/go-gl/glfw/v3.3/glfw.CreateWindow(0x258, 0x190, 0x4150ef0, 0xb, 0x0, 0x0, 0x0, 0x0, 0x0)
	/usr/local/gopath/src/github.com/pb/example/vendor/github.com/go-gl/glfw/v3.3/glfw/window.go:350 +0x1aa
main.main()
	/usr/local/gopath/src/github.com/pb/example/gles20-cube/test20-vbo/triangle.go:48 +0x156
exit status 2

It seems that on OSX, cgo is igorning the CGO_LDFLAGS.

Setting the env variable export DYLD_LIBRARY_PATH=/Users/username/workspace/angle/out/Release does not help. Btw, for anyone reading this, DYLD_LIBRARY_PATH is the mac equivalent of LD_LIBRARY_PATH in linux.

I also ran go build -a with -a flag to ensure a proper full rebuild was taking place with cgo.
Just in case, I also used go clean -cache -testcache . to clear any caches, but still same problem.

The mac equivalent of linux's ldd is otool -L. I used this to inspect the built go binary of my project. I noticed that it was picking up a slightly older version of some libraries. So I went ahead and deleted all the libs under /Users/username/workspace/angle/out/Release and tried a rebuild, hoping that it would fail, saying the libs are not found. But to my surprise, it builds successfully.

This is why I believe cgo is ignoring the CGO_LDFLAGS and finding some older version of these libs somewhere in my system.

EDIT: The question still remains, does it matter that an older version is found? I think it does. One of the libs in my system has reference to Metal, the other does not. See below for the 2 outputs.

otool -L test20-vbo 
test20-vbo:
	/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 22.0.0)
	/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
	/System/Library/Frameworks/CoreVideo.framework/Versions/A/CoreVideo (compatibility version 1.2.0, current version 1.5.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
	/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1404.47.0)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1259.22.0)
	/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 600.0.0)
	/System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 728.14.0)
	/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1259.32.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)

This has reference to metal.

otool -L "/Applications/Google Chrome.app/Contents/Frameworks/Google Chrome Framework.framework/Versions/89.0.4389.128/Libraries/libGLESv2.dylib"
/Applications/Google Chrome.app/Contents/Frameworks/Google Chrome Framework.framework/Versions/89.0.4389.128/Libraries/libGLESv2.dylib:
	./libGLESv2.dylib (compatibility version 0.0.0, current version 0.0.0)
	/System/Library/Frameworks/Metal.framework/Versions/A/Metal (compatibility version 1.0.0, current version 244.32.6)
	/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1770.106.0)
	/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1770.106.0)
	/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 1463.0.0)
	/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/IOSurface.framework/Versions/A/IOSurface (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/QuartzCore.framework/Versions/A/QuartzCore (compatibility version 1.2.0, current version 1.11.0)
	/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 23.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.0.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)

Some more update.
If you run build with -x flag, you get more verbose output of the cgo process.
go build -a -x

I noticed that -framework OpenGL was being added to the CGO_LDFLAGS ... i suspect this to be the problem, maybe a conflict with this package?

cd /usr/local/gopath/src/github.com/go-gl/gl/v3.1/gles2
  1 CGO_LDFLAGS='"-L/Users/username/workspace/angle/out/Release" "-framework" "OpenGL" "-framework" "OpenGL"' /usr/local/go/pkg/tool/darwin_amd64/cgo -o
  2 /usr/local/go/pkg/tool/darwin_amd64/buildid -w $WORK/b042/_pkg_.a # internal
  3 cp $WORK/b042/_pkg_.a /Users/username/Library/Caches/go-build/87/876ebdd267ab8b9acdbab20da2958b742c2517b376fa8181c2de01eae0cc7df2-d # internal
  4 mkdir -p $WORK/b035/
  5 cd /usr/local/gopath/src/github.com/pb/example/vendor/github.com/go-gl/glfw/v3.3/glfw
  6 CGO_LDFLAGS='"-L/Users/username/workspace/angle/out/Release" "-framework" "Cocoa" "-framework" "OpenGL" "-framework" "IOKit" "-framework" "CoreVideo
  7 cd $WORK/b032
  8 TERM='dumb' clang -I /usr/local/gopath/src/github.com/go-gl/gl/v3.1/gles2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-len
  9 TERM='dumb' clang -I /usr/local/gopath/src/github.com/go-gl/gl/v3.1/gles2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-len
 10 TERM='dumb' clang -I /usr/local/gopath/src/github.com/go-gl/gl/v3.1/gles2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-len
 11 TERM='dumb' clang -I /usr/local/gopath/src/github.com/go-gl/gl/v3.1/gles2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-len
 12 TERM='dumb' clang -I /usr/local/gopath/src/github.com/go-gl/gl/v3.1/gles2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-len
 13 TERM='dumb' clang -I /usr/local/gopath/src/github.com/go-gl/gl/v3.1/gles2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-len
 14 cd /usr/local/gopath/src/github.com/go-gl/gl/v3.1/gles2
 15 TERM='dumb' clang -I . -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=$WORK/b032=/tmp/go-build -
 16 TERM='dumb' /usr/local/go/pkg/tool/darwin_amd64/cgo -dynpackage gles2 -dynimport $WORK/b032/_cgo_.o -dynout $WORK/b032/_cgo_import.go

Getting close!
So after updating glfw package to latest to incorporate @zwang fixes for OpenGL ES (totally forgot to do that). I realized I was also being stupid for not using the tags flag accordingly. So this time, I ran it correctly.

go build -a -x -tags=gles2

And the output from otool was much better.

test20-vbo: otool -L test20-vbo 
test20-vbo:
	./libGLESv2.dylib (compatibility version 0.0.0, current version 0.0.0)
	/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 22.0.0)
	/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
	/System/Library/Frameworks/CoreVideo.framework/Versions/A/CoreVideo (compatibility version 1.2.0, current version 1.5.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
	/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1404.47.0)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1259.22.0)
	/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 600.0.0)
	/System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 728.14.0)
	/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1259.32.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)

Notice the first line,
./libGLESv2.dylib (compatibility version 0.0.0, current version 0.0.0) (this is new)
but, we still have the following line,
/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
and it is not showing Metal like Google Chrome's libs do.

FINALLY! Found the solution that works.
First of all, the damn go mod is a pain in the ass, causing all sorts of problems.

  1. I turned it off using export GO111MODULE="off"
  2. I ensured both "github.com/go-gl/gl/v3.1/gles2" and "github.com/go-gl/glfw/v3.3/glfw" were 100% updated to latest versions. (this is where go mod was messing up)
  3. then build using the correct flags go build -a -tags=gles2,egl (i missed egl last time)
  4. ensure dylib and cgo flags are correctly setup (see @zwang response)
  5. in desperation i put the dylib's in several places, including the local working directory and to directories where CGO_LDFLAGS and CGO_CFLAGS is pointing to.

The results from otool didn't change much, but it worked.

otool -L test20-vbo 
test20-vbo:
	./libGLESv2.dylib (compatibility version 0.0.0, current version 0.0.0)
	./libEGL.dylib (compatibility version 0.0.0, current version 0.0.0)
	/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 22.0.0)
	/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
	/System/Library/Frameworks/CoreVideo.framework/Versions/A/CoreVideo (compatibility version 1.2.0, current version 1.5.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
	/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1404.47.0)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1259.22.0)
	/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 600.0.0)
	/System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 728.14.0)
	/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1259.32.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)

This is the output from my program once it worked.

OpenGL version OpenGL ES 1.1.0 (ANGLE 2.1.14675 git hash: f25a0b5b404b)

and after I changed my code to use OpenGL ES 2.0, the output is

OpenGL version OpenGL ES 3.0.0 (ANGLE 2.1.14675 git hash: f25a0b5b404b)

not sure why it says 3.0.0 even thought I only want v2.0 ... but at least it works.
Thanks @zwang for getting me going on the right track.

This issue can be marked as closed! @zzador @dmitshur or whoever the moderator is 👍

I still have a similar issue on macOS (Apple Silicon), what could be a mistake from my side?

$ go build
# github.com/go-gl/gl/v2.1/gl
../../vendor/github.com/go-gl/gl/v2.1/gl/package.go:40:11: fatal error: 'KHR/khrplatform.h' file not found
 #include <KHR/khrplatform.h>
          ^~~~~~~~~~~~~~~~~~~
1 error generated.

Fall back to an old version solves the issue:

diff --git a/go.mod b/go.mod
index 62291a2..510b6d6 100644
--- a/go.mod
+++ b/go.mod
 go 1.17
 
 require (
-       github.com/go-gl/gl v0.0.0-20210501111010-69f74958bac0
+       github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7