-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader.go
executable file
·51 lines (39 loc) · 1.23 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
package gfx
import (
"fmt"
"github.com/go-gl/gl/v2.1/gl"
)
// Shader wraps OpenGL shaders.
type Shader struct {
id uint32
}
// ErrCompileShader indicates that a shader failed to compile.
const ErrCompileShader constErr = "failed to compile shader"
// ErrCreateShader indicates that a shader couldn't be created.
const ErrCreateShader constErr = "failed to create shader"
// NewShader attempts to compile the given shader source code as a shader
// of type shaderType (e.g. gl.FRAGMENT_SHADER).
func NewShader(source string, shaderType uint32) (Shader, error) {
shader := gl.CreateShader(shaderType)
if shader == 0 {
return Shader{}, ErrCreateShader
}
csources, free := gl.Strs(source + "\x00")
gl.ShaderSource(shader, 1, csources, nil)
free()
gl.CompileShader(shader)
var status int32
gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status)
if status == gl.FALSE {
var logLength int32
gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength)
log := string(make([]byte, logLength+1))
gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log))
return Shader{}, fmt.Errorf("%w: %v", ErrCompileShader, log)
}
return Shader{shader}, nil
}
// Destroy frees external resources.
func (sh Shader) Destroy() {
gl.DeleteShader(sh.id)
}