forked from faiface/pixel
/
glshader.go
267 lines (243 loc) · 5.91 KB
/
glshader.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package pixelgl
import (
"github.com/faiface/glhf"
"github.com/faiface/mainthread"
"github.com/go-gl/mathgl/mgl32"
"github.com/pkg/errors"
)
// glShader is a type to assist with managing a canvas's underlying
// shader configuration. This allows for customization of shaders on
// a per canvas basis.
type glShader struct {
s *glhf.Shader
vf, uf glhf.AttrFormat
vs, fs string
uniforms []gsUniformAttr
uniformDefaults struct {
transform mgl32.Mat3
colormask mgl32.Vec4
bounds mgl32.Vec4
texbounds mgl32.Vec4
}
}
type gsUniformAttr struct {
Name string
Type glhf.AttrType
value interface{}
ispointer bool
}
// reinitialize GLShader data and recompile the underlying gl shader object
func (gs *glShader) update() {
gs.uf = nil
for _, u := range gs.uniforms {
gs.uf = append(gs.uf, glhf.Attr{
Name: u.Name,
Type: u.Type,
})
}
var shader *glhf.Shader
mainthread.Call(func() {
var err error
shader, err = glhf.NewShader(
gs.vf,
gs.uf,
gs.vs,
gs.fs,
)
if err != nil {
panic(errors.Wrap(err, "failed to create Canvas, there's a bug in the shader"))
}
})
gs.s = shader
}
// gets the uniform index from GLShader
func (gs *glShader) getUniform(Name string) int {
for i, u := range gs.uniforms {
if u.Name == Name {
return i
}
}
return -1
}
// SetUniform appends a custom uniform name and value to the shader.
// if the uniform already exists, it will simply be overwritten.
//
// example:
//
// utime := float32(time.Since(starttime)).Seconds())
// mycanvas.shader.AddUniform("u_time", &utime)
func (gs *glShader) setUniform(name string, value interface{}) {
t, p := getAttrType(value)
if loc := gs.getUniform(name); loc > -1 {
gs.uniforms[loc].Name = name
gs.uniforms[loc].Type = t
gs.uniforms[loc].ispointer = p
gs.uniforms[loc].value = value
return
}
gs.uniforms = append(gs.uniforms, gsUniformAttr{
Name: name,
Type: t,
ispointer: p,
value: value,
})
}
// Sets up a base shader with everything needed for a Pixel
// canvas to render correctly. The defaults can be overridden
// by simply using the SetUniform function.
func baseShader(c *Canvas) {
gs := &glShader{
vf: defaultCanvasVertexFormat,
vs: baseCanvasVertexShader,
fs: baseCanvasFragmentShader,
}
gs.setUniform("uTransform", &gs.uniformDefaults.transform)
gs.setUniform("uColorMask", &gs.uniformDefaults.colormask)
gs.setUniform("uBounds", &gs.uniformDefaults.bounds)
gs.setUniform("uTexBounds", &gs.uniformDefaults.texbounds)
c.shader = gs
}
// Value returns the attribute's concrete value. If the stored value
// is a pointer, we return the dereferenced value.
func (gu *gsUniformAttr) Value() interface{} {
if !gu.ispointer {
return gu.value
}
switch gu.Type {
case glhf.Vec2:
return *gu.value.(*mgl32.Vec2)
case glhf.Vec3:
return *gu.value.(*mgl32.Vec3)
case glhf.Vec4:
return *gu.value.(*mgl32.Vec4)
case glhf.Mat2:
return *gu.value.(*mgl32.Mat2)
case glhf.Mat23:
return *gu.value.(*mgl32.Mat2x3)
case glhf.Mat24:
return *gu.value.(*mgl32.Mat2x4)
case glhf.Mat3:
return *gu.value.(*mgl32.Mat3)
case glhf.Mat32:
return *gu.value.(*mgl32.Mat3x2)
case glhf.Mat34:
return *gu.value.(*mgl32.Mat3x4)
case glhf.Mat4:
return *gu.value.(*mgl32.Mat4)
case glhf.Mat42:
return *gu.value.(*mgl32.Mat4x2)
case glhf.Mat43:
return *gu.value.(*mgl32.Mat4x3)
case glhf.Int:
return *gu.value.(*int32)
case glhf.Float:
return *gu.value.(*float32)
default:
panic("invalid attrtype")
}
}
// Returns the type identifier for any (supported) attribute variable type
// and whether or not it is a pointer of that type.
func getAttrType(v interface{}) (glhf.AttrType, bool) {
switch v.(type) {
case int32:
return glhf.Int, false
case float32:
return glhf.Float, false
case mgl32.Vec2:
return glhf.Vec2, false
case mgl32.Vec3:
return glhf.Vec3, false
case mgl32.Vec4:
return glhf.Vec4, false
case mgl32.Mat2:
return glhf.Mat2, false
case mgl32.Mat2x3:
return glhf.Mat23, false
case mgl32.Mat2x4:
return glhf.Mat24, false
case mgl32.Mat3:
return glhf.Mat3, false
case mgl32.Mat3x2:
return glhf.Mat32, false
case mgl32.Mat3x4:
return glhf.Mat34, false
case mgl32.Mat4:
return glhf.Mat4, false
case mgl32.Mat4x2:
return glhf.Mat42, false
case mgl32.Mat4x3:
return glhf.Mat43, false
case *mgl32.Vec2:
return glhf.Vec2, true
case *mgl32.Vec3:
return glhf.Vec3, true
case *mgl32.Vec4:
return glhf.Vec4, true
case *mgl32.Mat2:
return glhf.Mat2, true
case *mgl32.Mat2x3:
return glhf.Mat23, true
case *mgl32.Mat2x4:
return glhf.Mat24, true
case *mgl32.Mat3:
return glhf.Mat3, true
case *mgl32.Mat3x2:
return glhf.Mat32, true
case *mgl32.Mat3x4:
return glhf.Mat34, true
case *mgl32.Mat4:
return glhf.Mat4, true
case *mgl32.Mat4x2:
return glhf.Mat42, true
case *mgl32.Mat4x3:
return glhf.Mat43, true
case *int32:
return glhf.Int, true
case *float32:
return glhf.Float, true
default:
panic("invalid AttrType")
}
}
var baseCanvasVertexShader = `
#version 330 core
in vec2 aPosition;
in vec4 aColor;
in vec2 aTexCoords;
in float aIntensity;
out vec4 vColor;
out vec2 vTexCoords;
out float vIntensity;
uniform mat3 uTransform;
uniform vec4 uBounds;
void main() {
vec2 transPos = (uTransform * vec3(aPosition, 1.0)).xy;
vec2 normPos = (transPos - uBounds.xy) / uBounds.zw * 2 - vec2(1, 1);
gl_Position = vec4(normPos, 0.0, 1.0);
vColor = aColor;
vTexCoords = aTexCoords;
vIntensity = aIntensity;
}
`
var baseCanvasFragmentShader = `
#version 330 core
in vec4 vColor;
in vec2 vTexCoords;
in float vIntensity;
out vec4 fragColor;
uniform vec4 uColorMask;
uniform vec4 uTexBounds;
uniform sampler2D uTexture;
void main() {
if (vIntensity == 0) {
fragColor = uColorMask * vColor;
} else {
fragColor = vec4(0, 0, 0, 0);
fragColor += (1 - vIntensity) * vColor;
vec2 t = (vTexCoords - uTexBounds.xy) / uTexBounds.zw;
fragColor += vIntensity * vColor * texture(uTexture, t);
fragColor *= uColorMask;
}
}
`