-
Notifications
You must be signed in to change notification settings - Fork 24
/
shader.go
224 lines (196 loc) · 6.24 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package glhf
import (
"fmt"
"runtime"
"github.com/faiface/mainthread"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/mathgl/mgl32"
)
// Shader is an OpenGL shader program.
type Shader struct {
program binder
vertexFmt AttrFormat
uniformFmt AttrFormat
uniformLoc []int32
}
// NewShader creates a new shader program from the specified vertex shader and fragment shader
// sources.
//
// Note that vertexShader and fragmentShader parameters must contain the source code, they're
// not filenames.
func NewShader(vertexFmt, uniformFmt AttrFormat, vertexShader, fragmentShader string) (*Shader, error) {
shader := &Shader{
program: binder{
restoreLoc: gl.CURRENT_PROGRAM,
bindFunc: func(obj uint32) {
gl.UseProgram(obj)
},
},
vertexFmt: vertexFmt,
uniformFmt: uniformFmt,
uniformLoc: make([]int32, len(uniformFmt)),
}
var vshader, fshader uint32
// vertex shader
{
vshader = gl.CreateShader(gl.VERTEX_SHADER)
src, free := gl.Strs(vertexShader)
defer free()
length := int32(len(vertexShader))
gl.ShaderSource(vshader, 1, src, &length)
gl.CompileShader(vshader)
var success int32
gl.GetShaderiv(vshader, gl.COMPILE_STATUS, &success)
if success == gl.FALSE {
var logLen int32
gl.GetShaderiv(vshader, gl.INFO_LOG_LENGTH, &logLen)
infoLog := make([]byte, logLen)
gl.GetShaderInfoLog(vshader, logLen, nil, &infoLog[0])
return nil, fmt.Errorf("error compiling vertex shader: %s", string(infoLog))
}
defer gl.DeleteShader(vshader)
}
// fragment shader
{
fshader = gl.CreateShader(gl.FRAGMENT_SHADER)
src, free := gl.Strs(fragmentShader)
defer free()
length := int32(len(fragmentShader))
gl.ShaderSource(fshader, 1, src, &length)
gl.CompileShader(fshader)
var success int32
gl.GetShaderiv(fshader, gl.COMPILE_STATUS, &success)
if success == gl.FALSE {
var logLen int32
gl.GetShaderiv(fshader, gl.INFO_LOG_LENGTH, &logLen)
infoLog := make([]byte, logLen)
gl.GetShaderInfoLog(fshader, logLen, nil, &infoLog[0])
return nil, fmt.Errorf("error compiling fragment shader: %s", string(infoLog))
}
defer gl.DeleteShader(fshader)
}
// shader program
{
shader.program.obj = gl.CreateProgram()
gl.AttachShader(shader.program.obj, vshader)
gl.AttachShader(shader.program.obj, fshader)
gl.LinkProgram(shader.program.obj)
var success int32
gl.GetProgramiv(shader.program.obj, gl.LINK_STATUS, &success)
if success == gl.FALSE {
var logLen int32
gl.GetProgramiv(shader.program.obj, gl.INFO_LOG_LENGTH, &logLen)
infoLog := make([]byte, logLen)
gl.GetProgramInfoLog(shader.program.obj, logLen, nil, &infoLog[0])
return nil, fmt.Errorf("error linking shader program: %s", string(infoLog))
}
}
// uniforms
for i, uniform := range uniformFmt {
loc := gl.GetUniformLocation(shader.program.obj, gl.Str(uniform.Name+"\x00"))
shader.uniformLoc[i] = loc
}
runtime.SetFinalizer(shader, (*Shader).delete)
return shader, nil
}
func (s *Shader) delete() {
mainthread.CallNonBlock(func() {
gl.DeleteProgram(s.program.obj)
})
}
// ID returns the OpenGL ID of this Shader.
func (s *Shader) ID() uint32 {
return s.program.obj
}
// VertexFormat returns the vertex attribute format of this Shader. Do not change it.
func (s *Shader) VertexFormat() AttrFormat {
return s.vertexFmt
}
// UniformFormat returns the uniform attribute format of this Shader. Do not change it.
func (s *Shader) UniformFormat() AttrFormat {
return s.uniformFmt
}
// SetUniformAttr sets the value of a uniform attribute of this Shader. The attribute is
// specified by the index in the Shader's uniform format.
//
// If the uniform attribute does not exist in the Shader, this method returns false.
//
// Supplied value must correspond to the type of the attribute. Correct types are these
// (right-hand is the type of the value):
// Attr{Type: Int}: int32
// Attr{Type: Float}: float32
// Attr{Type: Vec2}: mgl32.Vec2
// Attr{Type: Vec3}: mgl32.Vec3
// Attr{Type: Vec4}: mgl32.Vec4
// Attr{Type: Mat2}: mgl32.Mat2
// Attr{Type: Mat23}: mgl32.Mat2x3
// Attr{Type: Mat24}: mgl32.Mat2x4
// Attr{Type: Mat3}: mgl32.Mat3
// Attr{Type: Mat32}: mgl32.Mat3x2
// Attr{Type: Mat34}: mgl32.Mat3x4
// Attr{Type: Mat4}: mgl32.Mat4
// Attr{Type: Mat42}: mgl32.Mat4x2
// Attr{Type: Mat43}: mgl32.Mat4x3
// No other types are supported.
//
// The Shader must be bound before calling this method.
func (s *Shader) SetUniformAttr(uniform int, value interface{}) (ok bool) {
if s.uniformLoc[uniform] < 0 {
return false
}
switch s.uniformFmt[uniform].Type {
case Int:
value := value.(int32)
gl.Uniform1iv(s.uniformLoc[uniform], 1, &value)
case Float:
value := value.(float32)
gl.Uniform1fv(s.uniformLoc[uniform], 1, &value)
case Vec2:
value := value.(mgl32.Vec2)
gl.Uniform2fv(s.uniformLoc[uniform], 1, &value[0])
case Vec3:
value := value.(mgl32.Vec3)
gl.Uniform3fv(s.uniformLoc[uniform], 1, &value[0])
case Vec4:
value := value.(mgl32.Vec4)
gl.Uniform4fv(s.uniformLoc[uniform], 1, &value[0])
case Mat2:
value := value.(mgl32.Mat2)
gl.UniformMatrix2fv(s.uniformLoc[uniform], 1, false, &value[0])
case Mat23:
value := value.(mgl32.Mat2x3)
gl.UniformMatrix2x3fv(s.uniformLoc[uniform], 1, false, &value[0])
case Mat24:
value := value.(mgl32.Mat2x4)
gl.UniformMatrix2x4fv(s.uniformLoc[uniform], 1, false, &value[0])
case Mat3:
value := value.(mgl32.Mat3)
gl.UniformMatrix3fv(s.uniformLoc[uniform], 1, false, &value[0])
case Mat32:
value := value.(mgl32.Mat3x2)
gl.UniformMatrix3x2fv(s.uniformLoc[uniform], 1, false, &value[0])
case Mat34:
value := value.(mgl32.Mat3x4)
gl.UniformMatrix3x4fv(s.uniformLoc[uniform], 1, false, &value[0])
case Mat4:
value := value.(mgl32.Mat4)
gl.UniformMatrix4fv(s.uniformLoc[uniform], 1, false, &value[0])
case Mat42:
value := value.(mgl32.Mat4x2)
gl.UniformMatrix4x2fv(s.uniformLoc[uniform], 1, false, &value[0])
case Mat43:
value := value.(mgl32.Mat4x3)
gl.UniformMatrix4x3fv(s.uniformLoc[uniform], 1, false, &value[0])
default:
panic("set uniform attr: invalid attribute type")
}
return true
}
// Begin binds the Shader program. This is necessary before using the Shader.
func (s *Shader) Begin() {
s.program.bind()
}
// End unbinds the Shader program and restores the previous one.
func (s *Shader) End() {
s.program.restore()
}