| 1 |
// Copyright 2014 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
//go:build darwin && !ios |
| 6 |
|
| 7 |
package glutil |
| 8 |
|
| 9 |
// TODO(crawshaw): Only used in glutil tests for now (cgo is not support in _test.go files). |
| 10 |
// TODO(crawshaw): Export some kind of Context. Work out what we can offer, where. Maybe just for tests. |
| 11 |
// TODO(crawshaw): Support android and windows. |
| 12 |
|
| 13 |
/* |
| 14 |
#cgo CFLAGS: -DGL_SILENCE_DEPRECATION |
| 15 |
#cgo LDFLAGS: -framework OpenGL |
| 16 |
#import <OpenGL/OpenGL.h> |
| 17 |
#import <OpenGL/gl3.h> |
| 18 |
|
| 19 |
CGLError CGCreate(CGLContextObj* ctx) { |
| 20 |
CGLError err; |
| 21 |
CGLPixelFormatAttribute attributes[] = { |
| 22 |
kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core, |
| 23 |
kCGLPFAColorSize, (CGLPixelFormatAttribute)24, |
| 24 |
kCGLPFAAlphaSize, (CGLPixelFormatAttribute)8, |
| 25 |
kCGLPFADepthSize, (CGLPixelFormatAttribute)16, |
| 26 |
kCGLPFAAccelerated, |
| 27 |
kCGLPFADoubleBuffer, |
| 28 |
(CGLPixelFormatAttribute) 0 |
| 29 |
}; |
| 30 |
CGLPixelFormatObj pix; |
| 31 |
GLint num; |
| 32 |
|
| 33 |
if ((err = CGLChoosePixelFormat(attributes, &pix, &num)) != kCGLNoError) { |
| 34 |
return err; |
| 35 |
} |
| 36 |
if ((err = CGLCreateContext(pix, 0, ctx)) != kCGLNoError) { |
| 37 |
return err; |
| 38 |
} |
| 39 |
if ((err = CGLDestroyPixelFormat(pix)) != kCGLNoError) { |
| 40 |
return err; |
| 41 |
} |
| 42 |
if ((err = CGLSetCurrentContext(*ctx)) != kCGLNoError) { |
| 43 |
return err; |
| 44 |
} |
| 45 |
if ((err = CGLLockContext(*ctx)) != kCGLNoError) { |
| 46 |
return err; |
| 47 |
} |
| 48 |
return kCGLNoError; |
| 49 |
} |
| 50 |
*/ |
| 51 |
import "C" |
| 52 |
|
| 53 |
import ( |
| 54 |
"fmt" |
| 55 |
"runtime" |
| 56 |
) |
| 57 |
|
| 58 |
// contextGL holds a copy of the OpenGL Context from thread-local storage. |
| 59 |
// |
| 60 |
// Do not move a contextGL between goroutines or OS threads. |
| 61 |
type contextGL struct { |
| 62 |
ctx C.CGLContextObj |
| 63 |
} |
| 64 |
|
| 65 |
// createContext creates an OpenGL context, binds it as the current context |
| 66 |
// stored in thread-local storage, and locks the current goroutine to an os |
| 67 |
// thread. |
| 68 |
func createContext() (*contextGL, error) { |
| 69 |
// The OpenGL active context is stored in TLS. |
| 70 |
runtime.LockOSThread() |
| 71 |
|
| 72 |
c := new(contextGL) |
| 73 |
if cglErr := C.CGCreate(&c.ctx); cglErr != C.kCGLNoError { |
| 74 |
return nil, fmt.Errorf("CGL: %v", C.GoString(C.CGLErrorString(cglErr))) |
| 75 |
} |
| 76 |
|
| 77 |
// Using attribute arrays in OpenGL 3.3 requires the use of a VBA. |
| 78 |
// But VBAs don't exist in ES 2. So we bind a default one. |
| 79 |
var id C.GLuint |
| 80 |
C.glGenVertexArrays(1, &id) |
| 81 |
C.glBindVertexArray(id) |
| 82 |
|
| 83 |
return c, nil |
| 84 |
} |
| 85 |
|
| 86 |
// destroy destroys an OpenGL context and unlocks the current goroutine from |
| 87 |
// its os thread. |
| 88 |
func (c *contextGL) destroy() { |
| 89 |
C.CGLUnlockContext(c.ctx) |
| 90 |
C.CGLSetCurrentContext(nil) |
| 91 |
C.CGLDestroyContext(c.ctx) |
| 92 |
c.ctx = nil |
| 93 |
runtime.UnlockOSThread() |
| 94 |
} |