-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathshader.go
79 lines (61 loc) · 2.13 KB
/
shader.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
// 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"
//
// // Workaround for https://github.com/go-gl/gl/issues/104
// void gogl_glGetShaderSource(GLuint shader, GLsizei bufsize, GLsizei* len, GLchar* source) {
// glGetShaderSource(shader, bufsize, len, source);
// }
//
import "C"
import "unsafe"
// Shader
type Shader Object
func CreateShader(type_ GLenum) Shader { return Shader(C.glCreateShader(C.GLenum(type_))) }
func (shader Shader) Delete() { C.glDeleteShader(C.GLuint(shader)) }
func (shader Shader) GetInfoLog() string {
var length C.GLint
C.glGetShaderiv(C.GLuint(shader), 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.glGetShaderInfoLog(C.GLuint(shader), C.GLsizei(length), nil, (*C.GLchar)(log))
return C.GoString((*C.char)(log))
}
return ""
}
func (shader Shader) GetSource() string {
var length C.GLint
C.glGetShaderiv(C.GLuint(shader), C.GLenum(SHADER_SOURCE_LENGTH), &length)
log := C.malloc(C.size_t(length + 1))
C.gogl_glGetShaderSource(C.GLuint(shader), C.GLsizei(length), nil, (*C.GLchar)(log))
defer C.free(log)
if length > 1 {
log := C.malloc(C.size_t(length + 1))
defer C.free(log)
C.gogl_glGetShaderSource(C.GLuint(shader), C.GLsizei(length), nil, (*C.GLchar)(log))
return C.GoString((*C.char)(log))
}
return ""
}
func (shader Shader) Source(source ...string) {
count := C.GLsizei(len(source))
cstrings := make([]*C.GLchar, count)
length := make([]C.GLint, count)
for i, s := range source {
csource := glString(s)
cstrings[i] = csource
length[i] = C.GLint(len(s))
defer freeString(csource)
}
C.glShaderSource(C.GLuint(shader), count, (**_Ctype_GLchar)(unsafe.Pointer(&cstrings[0])), (*_Ctype_GLint)(unsafe.Pointer(&length[0])))
}
func (shader Shader) Compile() { C.glCompileShader(C.GLuint(shader)) }
func (shader Shader) Get(param GLenum) int {
var rv C.GLint
C.glGetShaderiv(C.GLuint(shader), C.GLenum(param), &rv)
return int(rv)
}