gl: factor cgo types out of gl.go

This allows a subsequent CL to introduce windows support by directly
calling an ANGLE dll.

For golang/go#9306

Change-Id: I7dbe8f2b77b9e2c744f0d848f716ee4448916fe7
Reviewed-on: https://go-review.googlesource.com/17674
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
David Crawshaw 2015-12-09 12:52:53 -05:00
parent 563f2bdd3a
commit 2b7eb78ade
9 changed files with 1232 additions and 1042 deletions

188
gl/fn.go Normal file
View File

@ -0,0 +1,188 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gl
import "unsafe"
type call struct {
args fnargs
parg unsafe.Pointer
blocking bool
}
type fnargs struct {
fn glfn
a0 uintptr
a1 uintptr
a2 uintptr
a3 uintptr
a4 uintptr
a5 uintptr
a6 uintptr
a7 uintptr
}
type glfn int
const (
glfnUNDEFINED glfn = iota
glfnActiveTexture
glfnAttachShader
glfnBindAttribLocation
glfnBindBuffer
glfnBindFramebuffer
glfnBindRenderbuffer
glfnBindTexture
glfnBlendColor
glfnBlendEquation
glfnBlendEquationSeparate
glfnBlendFunc
glfnBlendFuncSeparate
glfnBufferData
glfnBufferSubData
glfnCheckFramebufferStatus
glfnClear
glfnClearColor
glfnClearDepthf
glfnClearStencil
glfnColorMask
glfnCompileShader
glfnCompressedTexImage2D
glfnCompressedTexSubImage2D
glfnCopyTexImage2D
glfnCopyTexSubImage2D
glfnCreateProgram
glfnCreateShader
glfnCullFace
glfnDeleteBuffer
glfnDeleteFramebuffer
glfnDeleteProgram
glfnDeleteRenderbuffer
glfnDeleteShader
glfnDeleteTexture
glfnDepthFunc
glfnDepthRangef
glfnDepthMask
glfnDetachShader
glfnDisable
glfnDisableVertexAttribArray
glfnDrawArrays
glfnDrawElements
glfnEnable
glfnEnableVertexAttribArray
glfnFinish
glfnFlush
glfnFramebufferRenderbuffer
glfnFramebufferTexture2D
glfnFrontFace
glfnGenBuffer
glfnGenFramebuffer
glfnGenRenderbuffer
glfnGenTexture
glfnGenerateMipmap
glfnGetActiveAttrib
glfnGetActiveUniform
glfnGetAttachedShaders
glfnGetAttribLocation
glfnGetBooleanv
glfnGetBufferParameteri
glfnGetError
glfnGetFloatv
glfnGetFramebufferAttachmentParameteriv
glfnGetIntegerv
glfnGetProgramInfoLog
glfnGetProgramiv
glfnGetRenderbufferParameteriv
glfnGetShaderInfoLog
glfnGetShaderPrecisionFormat
glfnGetShaderSource
glfnGetShaderiv
glfnGetString
glfnGetTexParameterfv
glfnGetTexParameteriv
glfnGetUniformLocation
glfnGetUniformfv
glfnGetUniformiv
glfnGetVertexAttribfv
glfnGetVertexAttribiv
glfnHint
glfnIsBuffer
glfnIsEnabled
glfnIsFramebuffer
glfnIsProgram
glfnIsRenderbuffer
glfnIsShader
glfnIsTexture
glfnLineWidth
glfnLinkProgram
glfnPixelStorei
glfnPolygonOffset
glfnReadPixels
glfnReleaseShaderCompiler
glfnRenderbufferStorage
glfnSampleCoverage
glfnScissor
glfnShaderSource
glfnStencilFunc
glfnStencilFuncSeparate
glfnStencilMask
glfnStencilMaskSeparate
glfnStencilOp
glfnStencilOpSeparate
glfnTexImage2D
glfnTexParameterf
glfnTexParameterfv
glfnTexParameteri
glfnTexParameteriv
glfnTexSubImage2D
glfnUniform1f
glfnUniform1fv
glfnUniform1i
glfnUniform1iv
glfnUniform2f
glfnUniform2fv
glfnUniform2i
glfnUniform2iv
glfnUniform3f
glfnUniform3fv
glfnUniform3i
glfnUniform3iv
glfnUniform4f
glfnUniform4fv
glfnUniform4i
glfnUniform4iv
glfnUniformMatrix2fv
glfnUniformMatrix3fv
glfnUniformMatrix4fv
glfnUseProgram
glfnValidateProgram
glfnVertexAttrib1f
glfnVertexAttrib1fv
glfnVertexAttrib2f
glfnVertexAttrib2fv
glfnVertexAttrib3f
glfnVertexAttrib3fv
glfnVertexAttrib4f
glfnVertexAttrib4fv
glfnVertexAttribPointer
glfnViewport
)
func goString(buf []byte) string {
for i, b := range buf {
if b == 0 {
return string(buf[:i])
}
}
panic("buf is not NUL-terminated")
}
func glBoolean(b bool) uintptr {
if b {
return TRUE
}
return FALSE
}

View File

@ -269,14 +269,11 @@ const preamble = `// Copyright 2014 The Go Authors. All rights reserved.
// Generated from gl.go using go generate. DO NOT EDIT.
// See doc.go for details.
// +build linux darwin
// +build linux darwin windows
// +build gldebug
package gl
// #include "work.h"
import "C"
import (
"fmt"
"log"
@ -300,7 +297,7 @@ func (ctx *context) errDrain() string {
return ""
}
func (ctx *context) enqueueDebug(c call) C.uintptr_t {
func (ctx *context) enqueueDebug(c call) uintptr {
numCalls := atomic.AddInt32(&ctx.debug, 1)
if numCalls > 1 {
panic("concurrent calls made to the same GL context")

975
gl/gl.go

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build linux darwin
// +build linux darwin windows
// +build gldebug
package gl
@ -10,8 +10,6 @@ package gl
// Alternate versions of the types defined in types.go with extra
// debugging information attached. For documentation, see types.go.
// #include "work.h"
import "C"
import "fmt"
type Enum uint32
@ -22,6 +20,7 @@ type Attrib struct {
}
type Program struct {
Init bool
Value uint32
}
@ -50,15 +49,22 @@ type Uniform struct {
name string
}
func (v Attrib) c() C.uintptr_t { return C.uintptr_t(v.Value) }
func (v Enum) c() C.uintptr_t { return C.uintptr_t(v) }
func (v Program) c() C.uintptr_t { return C.uintptr_t(v.Value) }
func (v Shader) c() C.uintptr_t { return C.uintptr_t(v.Value) }
func (v Buffer) c() C.uintptr_t { return C.uintptr_t(v.Value) }
func (v Framebuffer) c() C.uintptr_t { return C.uintptr_t(v.Value) }
func (v Renderbuffer) c() C.uintptr_t { return C.uintptr_t(v.Value) }
func (v Texture) c() C.uintptr_t { return C.uintptr_t(v.Value) }
func (v Uniform) c() C.uintptr_t { return C.uintptr_t(v.Value) }
func (v Attrib) c() uintptr { return uintptr(v.Value) }
func (v Enum) c() uintptr { return uintptr(v) }
func (v Program) c() uintptr {
if !v.Init {
ret := uintptr(0)
ret--
return ret
}
return uintptr(v.Value)
}
func (v Shader) c() uintptr { return uintptr(v.Value) }
func (v Buffer) c() uintptr { return uintptr(v.Value) }
func (v Framebuffer) c() uintptr { return uintptr(v.Value) }
func (v Renderbuffer) c() uintptr { return uintptr(v.Value) }
func (v Texture) c() uintptr { return uintptr(v.Value) }
func (v Uniform) c() uintptr { return uintptr(v.Value) }
func (v Attrib) String() string { return fmt.Sprintf("Attrib(%d:%s)", v.Value, v.name) }
func (v Program) String() string { return fmt.Sprintf("Program(%d)", v.Value) }

View File

@ -2,13 +2,11 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build linux darwin
// +build linux darwin windows
// +build !gldebug
package gl
// #include "work.h"
import "C"
import "fmt"
// Enum is equivalent to GLenum, and is normally used with one of the
@ -25,6 +23,9 @@ type Attrib struct {
// Program identifies a compiled shader program.
type Program struct {
// Init is set by CreateProgram, as some GL drivers (in particular,
// ANGLE) return true for glIsProgram(0).
Init bool
Value uint32
}
@ -58,15 +59,22 @@ type Uniform struct {
Value int32
}
func (v Attrib) c() C.uintptr_t { return C.uintptr_t(v.Value) }
func (v Enum) c() C.uintptr_t { return C.uintptr_t(v) }
func (v Program) c() C.uintptr_t { return C.uintptr_t(v.Value) }
func (v Shader) c() C.uintptr_t { return C.uintptr_t(v.Value) }
func (v Buffer) c() C.uintptr_t { return C.uintptr_t(v.Value) }
func (v Framebuffer) c() C.uintptr_t { return C.uintptr_t(v.Value) }
func (v Renderbuffer) c() C.uintptr_t { return C.uintptr_t(v.Value) }
func (v Texture) c() C.uintptr_t { return C.uintptr_t(v.Value) }
func (v Uniform) c() C.uintptr_t { return C.uintptr_t(v.Value) }
func (v Attrib) c() uintptr { return uintptr(v.Value) }
func (v Enum) c() uintptr { return uintptr(v) }
func (v Program) c() uintptr {
if !v.Init {
ret := uintptr(0)
ret--
return ret
}
return uintptr(v.Value)
}
func (v Shader) c() uintptr { return uintptr(v.Value) }
func (v Buffer) c() uintptr { return uintptr(v.Value) }
func (v Framebuffer) c() uintptr { return uintptr(v.Value) }
func (v Renderbuffer) c() uintptr { return uintptr(v.Value) }
func (v Texture) c() uintptr { return uintptr(v.Value) }
func (v Uniform) c() uintptr { return uintptr(v.Value) }
func (v Attrib) String() string { return fmt.Sprintf("Attrib(%d)", v.Value) }
func (v Program) String() string { return fmt.Sprintf("Program(%d)", v.Value) }

View File

@ -22,7 +22,6 @@ uintptr_t processFn(struct fnargs* args, char* parg) {
break;
case glfnBindAttribLocation:
glBindAttribLocation((GLint)args->a0, (GLint)args->a1, (GLchar*)args->a2);
free((void*)args->a2);
break;
case glfnBindBuffer:
glBindBuffer((GLenum)args->a0, (GLuint)args->a1);
@ -183,9 +182,9 @@ uintptr_t processFn(struct fnargs* args, char* parg) {
(GLuint)args->a1,
(GLsizei)args->a2,
NULL,
(GLint*)args->a4,
(GLenum*)args->a5,
(GLchar*)args->a6);
(GLint*)&ret,
(GLenum*)args->a3,
(GLchar*)parg);
break;
case glfnGetActiveUniform:
glGetActiveUniform(
@ -193,19 +192,18 @@ uintptr_t processFn(struct fnargs* args, char* parg) {
(GLuint)args->a1,
(GLsizei)args->a2,
NULL,
(GLint*)args->a4,
(GLenum*)args->a5,
(GLchar*)args->a6);
(GLint*)&ret,
(GLenum*)args->a3,
(GLchar*)parg);
break;
case glfnGetAttachedShaders:
glGetAttachedShaders((GLuint)args->a0, (GLsizei)args->a1, (GLsizei*)args->a2, (GLuint*)args->a3);
glGetAttachedShaders((GLuint)args->a0, (GLsizei)args->a1, (GLsizei*)&ret, (GLuint*)parg);
break;
case glfnGetAttribLocation:
ret = glGetAttribLocation((GLint)args->a0, (GLchar*)args->a1);
free((void*)args->a1);
break;
case glfnGetBooleanv:
glGetBooleanv((GLenum)args->a0, (GLboolean*)args->a1);
glGetBooleanv((GLenum)args->a0, (GLboolean*)parg);
break;
case glfnGetBufferParameteri:
glGetBufferParameteriv((GLenum)args->a0, (GLenum)args->a1, (GLint*)&ret);
@ -226,7 +224,7 @@ uintptr_t processFn(struct fnargs* args, char* parg) {
glGetProgramiv((GLint)args->a0, (GLenum)args->a1, (GLint*)&ret);
break;
case glfnGetProgramInfoLog:
glGetProgramInfoLog((GLuint)args->a0, (GLsizei)args->a1, (GLsizei*)args->a2, (GLchar*)args->a3);
glGetProgramInfoLog((GLuint)args->a0, (GLsizei)args->a1, 0, (GLchar*)parg);
break;
case glfnGetRenderbufferParameteriv:
glGetRenderbufferParameteriv((GLenum)args->a0, (GLenum)args->a1, (GLint*)&ret);
@ -235,13 +233,13 @@ uintptr_t processFn(struct fnargs* args, char* parg) {
glGetShaderiv((GLint)args->a0, (GLenum)args->a1, (GLint*)&ret);
break;
case glfnGetShaderInfoLog:
glGetShaderInfoLog((GLuint)args->a0, (GLsizei)args->a1, (GLsizei*)args->a2, (GLchar*)args->a3);
glGetShaderInfoLog((GLuint)args->a0, (GLsizei)args->a1, 0, (GLchar*)parg);
break;
case glfnGetShaderPrecisionFormat:
glGetShaderPrecisionFormat((GLenum)args->a0, (GLenum)args->a1, (GLint*)args->a2, (GLint*)args->a3);
glGetShaderPrecisionFormat((GLenum)args->a0, (GLenum)args->a1, (GLint*)parg, &((GLint*)parg)[2]);
break;
case glfnGetShaderSource:
glGetShaderSource((GLuint)args->a0, (GLsizei)args->a1, (GLsizei*)args->a2, (GLchar*)args->a3);
glGetShaderSource((GLuint)args->a0, (GLsizei)args->a1, 0, (GLchar*)parg);
break;
case glfnGetString:
ret = (uintptr_t)glGetString((GLenum)args->a0);
@ -259,8 +257,7 @@ uintptr_t processFn(struct fnargs* args, char* parg) {
glGetUniformiv((GLuint)args->a0, (GLint)args->a1, (GLint*)parg);
break;
case glfnGetUniformLocation:
ret = glGetUniformLocation((GLint)args->a0, (GLchar*)parg);
free((void*)args->a1);
ret = glGetUniformLocation((GLint)args->a0, (GLchar*)args->a1);
break;
case glfnGetVertexAttribfv:
glGetVertexAttribfv((GLuint)args->a0, (GLenum)args->a1, (GLfloat*)parg);
@ -325,8 +322,6 @@ uintptr_t processFn(struct fnargs* args, char* parg) {
#else
glShaderSource((GLuint)args->a0, (GLsizei)args->a1, (const GLchar **)args->a2, NULL);
#endif
free(*(void**)args->a2);
free((void*)args->a2);
break;
case glfnStencilFunc:
glStencilFunc((GLenum)args->a0, (GLint)args->a1, (GLuint)args->a2);

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin linux cgo
// +build darwin linux
package gl
@ -78,13 +78,7 @@ func NewContext() (Context, Worker) {
return glctx, glctx
}
type call struct {
args C.struct_fnargs
parg unsafe.Pointer
blocking bool
}
func (ctx *context) enqueue(c call) C.uintptr_t {
func (ctx *context) enqueue(c call) uintptr {
ctx.work <- c
select {
@ -93,7 +87,7 @@ func (ctx *context) enqueue(c call) C.uintptr_t {
}
if c.blocking {
return <-ctx.retvalue
return uintptr(<-ctx.retvalue)
}
return 0
}
@ -123,7 +117,7 @@ func (ctx *context) DoWork() {
// Process the queued GL functions.
for i, q := range queue {
ctx.cargs[i] = q.args
ctx.cargs[i] = *(*C.struct_fnargs)(unsafe.Pointer(&q.args))
ctx.parg[i] = (*C.char)(q.parg)
}
ret := C.process(&ctx.cargs[0], ctx.parg[0], ctx.parg[1], ctx.parg[2], C.int(len(queue)))
@ -136,9 +130,27 @@ func (ctx *context) DoWork() {
}
}
func glBoolean(b bool) C.uintptr_t {
if b {
return TRUE
func init() {
if unsafe.Sizeof(C.GLint(0)) != unsafe.Sizeof(int32(0)) {
panic("GLint is not an int32")
}
}
// cString creates C string off the Go heap.
// ret is a *char.
func cString(str string) (uintptr, func()) {
ptr := unsafe.Pointer(C.CString(str))
return uintptr(ptr), func() { C.free(ptr) }
}
// cString creates a pointer to a C string off the Go heap.
// ret is a **char.
func cStringPtr(str string) (uintptr, func()) {
s, free := cString(str)
ptr := C.malloc(C.size_t(unsafe.Sizeof((*int)(nil))))
*(*uintptr)(ptr) = s
return uintptr(ptr), func() {
free()
C.free(ptr)
}
return FALSE
}

View File

@ -15,6 +15,7 @@
#include <stdint.h>
#include <stdlib.h>
// TODO: generate this enum from fn.go.
typedef enum {
glfnUNDEFINED,
@ -160,6 +161,7 @@ typedef enum {
glfnViewport,
} glfn;
// TODO: generate this type from fn.go.
struct fnargs {
glfn fn;
@ -173,6 +175,4 @@ struct fnargs {
uintptr_t a7;
};
extern uintptr_t ret;
extern uintptr_t processFn(struct fnargs* args, char* parg);