forked from g3n/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uniform.go
62 lines (51 loc) · 1.52 KB
/
uniform.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
// Copyright 2016 The G3N 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 gls
import (
"fmt"
)
// Uniform represents an OpenGL uniform.
type Uniform struct {
name string // base name
nameIdx string // cached indexed name
handle uint32 // program handle
location int32 // last cached location
lastIndex int32 // last index
}
// Init initializes this uniform location cache and sets its name.
func (u *Uniform) Init(name string) {
u.name = name
u.handle = 0 // invalid program handle
u.location = -1 // invalid location
u.lastIndex = -1 // invalid index
}
// Name returns the uniform name.
func (u *Uniform) Name() string {
return u.name
}
// Location returns the location of this uniform for the current shader program.
// The returned location can be -1 if not found.
func (u *Uniform) Location(gs *GLS) int32 {
handle := gs.prog.Handle()
if handle != u.handle {
u.location = gs.prog.GetUniformLocation(u.name)
u.handle = handle
}
return u.location
}
// LocationIdx returns the location of this indexed uniform for the current shader program.
// The returned location can be -1 if not found.
func (u *Uniform) LocationIdx(gs *GLS, idx int32) int32 {
if idx != u.lastIndex {
u.nameIdx = fmt.Sprintf("%s[%d]", u.name, idx)
u.lastIndex = idx
u.handle = 0
}
handle := gs.prog.Handle()
if handle != u.handle {
u.location = gs.prog.GetUniformLocation(u.nameIdx)
u.handle = handle
}
return u.location
}