forked from go-gl-legacy/gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.go
207 lines (158 loc) · 6.12 KB
/
program.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2012 The go-gl 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
// #include "gl.h"
// GLuint workaroundGlGetUniformBlockIndex(GLuint program, const GLchar* uniformBlockName) {
// return glGetUniformBlockIndex(program, uniformBlockName);
// }
import "C"
import "unsafe"
// Program
type Program Object
func CreateProgram() Program { return Program(C.glCreateProgram()) }
func (program Program) Delete() { C.glDeleteProgram(C.GLuint(program)) }
func (program Program) AttachShader(shader Shader) {
C.glAttachShader(C.GLuint(program), C.GLuint(shader))
}
func (program Program) GetAttachedShaders() []Object {
var len C.GLint
C.glGetProgramiv(C.GLuint(program), C.GLenum(ACTIVE_UNIFORM_MAX_LENGTH), &len)
objects := make([]Object, len)
C.glGetAttachedShaders(C.GLuint(program), C.GLsizei(len), nil, *((**C.GLuint)(unsafe.Pointer(&objects))))
return objects
}
func (program Program) DetachShader(shader Shader) {
C.glDetachShader(C.GLuint(program), C.GLuint(shader))
}
func (program Program) TransformFeedbackVaryings(names []string, buffer_mode GLenum) {
if len(names) == 0 {
C.glTransformFeedbackVaryings(C.GLuint(program), 0, (**C.GLchar)(nil), C.GLenum(buffer_mode))
} else {
gl_names := make([]*C.GLchar, len(names))
for i := range names {
gl_names[i] = glString(names[i])
}
C.glTransformFeedbackVaryings(C.GLuint(program), C.GLsizei(len(gl_names)), &gl_names[0], C.GLenum(buffer_mode))
for _, s := range gl_names {
freeString(s)
}
}
}
func (program Program) Link() { C.glLinkProgram(C.GLuint(program)) }
func (program Program) Validate() { C.glValidateProgram(C.GLuint(program)) }
func (program Program) Use() { C.glUseProgram(C.GLuint(program)) }
func (program Program) Unuse() { C.glUseProgram(C.GLuint(0)) }
// Deprecated, please use program.Unuse()
func ProgramUnuse() { C.glUseProgram(C.GLuint(0)) }
func (program Program) GetInfoLog() string {
var length C.GLint
C.glGetProgramiv(C.GLuint(program), C.GLenum(INFO_LOG_LENGTH), &length)
// length is buffer size including null character
if length > 1 {
log := C.malloc(C.size_t(length))
defer C.free(log)
C.glGetProgramInfoLog(C.GLuint(program), C.GLsizei(length), nil, (*C.GLchar)(log))
return C.GoString((*C.char)(log))
}
return ""
}
func (program Program) Get(param GLenum) int {
var rv C.GLint
C.glGetProgramiv(C.GLuint(program), C.GLenum(param), &rv)
return int(rv)
}
// glGetActiveUniform(GLuint program, GLuint index, GLsizei bufSize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
func (program Program) GetActiveUniform(index int) (
Size int, Type GLenum, Name string) {
// Maximum length of active uniform name in program
bufSize := program.Get(ACTIVE_UNIFORM_MAX_LENGTH)
nameBuf := C.malloc(C.size_t(bufSize))
defer C.free(nameBuf)
var size C.GLint
C.glGetActiveUniform(
C.GLuint(program),
C.GLuint(index),
C.GLsizei(bufSize),
nil, // length == len(Name)
&size,
(*C.GLenum)(&Type),
(*C.GLchar)(nameBuf))
Name = C.GoString((*C.char)(nameBuf))
Size = int(size)
return
}
func (program Program) GetUniformiv(location UniformLocation, values []int32) {
if len(values) == 0 {
panic("Invalid values length")
}
// FIXME(jimt): This should really yield only one return value instead of using a slice.
// http://www.opengl.org/sdk/docs/man/xhtml/glGetUniform.xml
C.glGetUniformiv(C.GLuint(program), C.GLint(location), (*C.GLint)(&(values[0])))
}
func (program Program) GetUniformfv(location UniformLocation, values []float32) {
if len(values) == 0 {
panic("Invalid values length")
}
// FIXME(jimt): This should really yield only one return value instead of using a slice.
// http://www.opengl.org/sdk/docs/man/xhtml/glGetUniform.xml
C.glGetUniformfv(C.GLuint(program), C.GLint(location), (*C.GLfloat)(&(values[0])))
}
func (program Program) GetUniformLocation(name string) UniformLocation {
cname := glString(name)
defer freeString(cname)
return UniformLocation(C.glGetUniformLocation(C.GLuint(program), cname))
}
func (program Program) GetUniformBlockIndex(name string) UniformBlockIndex {
cname := glString(name)
defer freeString(cname)
// Workaround bug in GLEW < 1.8 where glGetUniformBlockIndex expects
// a string of char instead of a string of GLchar. We could ask everybody
// to bump their version of GLEW, or we could add a bit of C code that
// will silently cast GLchar into char.
//return UniformBlockIndex(C.glGetUniformBlockIndex(C.GLuint(program), cname))
return UniformBlockIndex(C.workaroundGlGetUniformBlockIndex(C.GLuint(program), cname))
}
func (program Program) UniformBlockBinding(index UniformBlockIndex, binding uint) {
C.glUniformBlockBinding(C.GLuint(program), C.GLuint(index), C.GLuint(binding))
}
// glGetActiveAttrib(GLuint program, GLuint index, GLsizei bufSize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
func (program Program) GetActiveAttrib(index int) (
Size int, Type GLenum, Name string) {
// Maximum length of active uniform name in program
bufSize := program.Get(ACTIVE_ATTRIBUTE_MAX_LENGTH)
nameBuf := C.malloc(C.size_t(bufSize))
defer C.free(nameBuf)
var size C.GLint
C.glGetActiveAttrib(
C.GLuint(program),
C.GLuint(index),
C.GLsizei(bufSize),
nil, // length == len(Name)
&size,
(*C.GLenum)(&Type),
(*C.GLchar)(nameBuf))
Name = C.GoString((*C.char)(nameBuf))
Size = int(size)
return
}
func (program Program) GetAttribLocation(name string) AttribLocation {
cname := glString(name)
defer freeString(cname)
return AttribLocation(C.glGetAttribLocation(C.GLuint(program), cname))
}
func (program Program) BindAttribLocation(index AttribLocation, name string) {
cname := glString(name)
defer freeString(cname)
C.glBindAttribLocation(C.GLuint(program), C.GLuint(index), cname)
}
func (program Program) BindFragDataLocation(colorNumber int, name string) {
cname := glString(name)
defer freeString(cname)
C.glBindFragDataLocation(C.GLuint(program), C.GLuint(colorNumber), cname)
}
func (program Program) GetFragDataLocation(name string) int {
cname := glString(name)
defer freeString(cname)
return int(C.glGetFragDataLocation(C.GLuint(program), cname))
}