/
technique.go
226 lines (196 loc) · 4.48 KB
/
technique.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
package render
import (
"github.com/go-gl/gl/v4.1-core/gl"
)
var (
prevBlendFunc *blendFunc
prevCullFace *cullFace
prevDepthMask *depthMask
prevDepthFunc *depthFunc
prevViewport *Viewport
prevShader *Shader
prevFrameBuffer *FrameBuffer
prevEnables = make(map[uint32]bool)
)
type blendFunc struct {
sfactor uint32
dfactor uint32
}
func (b *blendFunc) Equals(other *blendFunc) bool {
return other != nil &&
b.sfactor == other.sfactor &&
b.dfactor == other.dfactor
}
type cullFace struct {
mode uint32
}
func (c *cullFace) Equals(other *cullFace) bool {
return other != nil &&
c.mode == other.mode
}
type depthMask struct {
flag bool
}
func (d *depthMask) Equals(other *depthMask) bool {
return other != nil &&
d.flag == other.flag
}
type depthFunc struct {
xfunc uint32
}
func (d *depthFunc) Equals(other *depthFunc) bool {
return other != nil &&
d.xfunc == other.xfunc
}
type clearColor struct {
r float32
g float32
b float32
a float32
}
// Technique represents a render technique.
type Technique struct {
enables []uint32
shader *Shader
viewport *Viewport
framebuffer *FrameBuffer
blendFunc *blendFunc
cullFace *cullFace
depthMask *depthMask
depthFunc *depthFunc
clearColor *clearColor
}
// NewTechnique instantiates and returns a new technique instance.
func NewTechnique() *Technique {
return &Technique{
blendFunc: &blendFunc{
sfactor: gl.ONE,
dfactor: gl.ZERO,
},
cullFace: &cullFace{
mode: gl.BACK,
},
depthMask: &depthMask{
flag: true,
},
depthFunc: &depthFunc{
xfunc: gl.LESS,
},
}
}
// Enable enables the rendering states for the technique.
func (t *Technique) Enable(enable uint32) {
t.enables = append(t.enables, enable)
}
// Shader sets the shader for the technique.
func (t *Technique) Shader(shader *Shader) {
t.shader = shader
}
// Viewport sets the viewport for the technique.
func (t *Technique) Viewport(viewport *Viewport) {
t.viewport = viewport
}
// BlendFunc sets the blend func for the technique.
func (t *Technique) BlendFunc(sfactor uint32, dfactor uint32) {
t.blendFunc = &blendFunc{
sfactor: sfactor,
dfactor: dfactor,
}
}
// CullFace sets the cull face mode for the technique.
func (t *Technique) CullFace(mode uint32) {
t.cullFace = &cullFace{
mode: mode,
}
}
// DepthMask sets the depth mask for the technique.
func (t *Technique) DepthMask(flag bool) {
t.depthMask = &depthMask{
flag: flag,
}
}
// DepthFunc sets the depth mask for the technique.
func (t *Technique) DepthFunc(xfunc uint32) {
t.depthFunc = &depthFunc{
xfunc: xfunc,
}
}
// ClearColor sets the clear color for the frame.
func (t *Technique) ClearColor(r, g, b, a float32) {
t.clearColor = &clearColor{
r: r,
g: g,
b: b,
a: a,
}
}
// Draw renders all commands using the technique.
func (t *Technique) Draw(commands []*Command) error {
t.setup()
for _, command := range commands {
err := command.Execute(t.shader)
if err != nil {
return err
}
}
return nil
}
func (t *Technique) setup() {
// bind framebuffer
if t.framebuffer == nil && prevFrameBuffer != nil {
prevFrameBuffer.Unbind()
}
if t.framebuffer != nil && t.framebuffer != prevFrameBuffer {
t.framebuffer.Bind()
prevFrameBuffer = t.framebuffer
}
// use shader
if prevShader != t.shader {
t.shader.Use()
prevShader = t.shader
}
// track previous enables to determine which are stale
staleEnables := make(map[uint32]bool)
for state := range prevEnables {
staleEnables[state] = true
}
// enable state
for _, state := range t.enables {
if !prevEnables[state] {
gl.Enable(state)
prevEnables[state] = true
}
delete(staleEnables, state)
}
// disable stale state
for state := range staleEnables {
gl.Disable(state)
delete(prevEnables, state)
}
// update state functions
if t.blendFunc != nil && !t.blendFunc.Equals(prevBlendFunc) {
gl.BlendFunc(t.blendFunc.sfactor, t.blendFunc.dfactor)
prevBlendFunc = t.blendFunc
}
if t.cullFace != nil && !t.cullFace.Equals(prevCullFace) {
gl.CullFace(t.cullFace.mode)
prevCullFace = t.cullFace
}
if t.depthMask != nil && !t.depthMask.Equals(prevDepthMask) {
gl.DepthMask(t.depthMask.flag)
prevDepthMask = t.depthMask
}
if t.depthFunc != nil && !t.depthFunc.Equals(prevDepthFunc) {
gl.DepthFunc(t.depthFunc.xfunc)
prevDepthFunc = t.depthFunc
}
// update viewport
if t.viewport != nil && !t.viewport.Equals(prevViewport) {
gl.Viewport(
t.viewport.X,
t.viewport.Y,
t.viewport.Width,
t.viewport.Height)
prevViewport = t.viewport
}
}