-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstate.go
More file actions
235 lines (191 loc) · 5.36 KB
/
state.go
File metadata and controls
235 lines (191 loc) · 5.36 KB
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
package glitch
// TODO - maybe push this up into internal/gl?
// TODO - this might lock us up into a single window? That doesn't seem like too bad of a requirement though
import (
"github.com/unitoftime/glitch/internal/gl"
"github.com/unitoftime/glitch/internal/mainthread"
)
type stateTracker struct {
// FBO
fbo gl.Framebuffer
fboBounds Rect
fboBinder func()
// Depth Test
// depthTest bool
// enableDepthFunc func()
// depthFunc gl.Enum
// depthFuncBinder func()
depthMode DepthMode
depthModeBinder func()
// Texture
texture *Texture
textureBinder func()
// BlendFunc
// blendSrc, blendDst gl.Enum
blendMode BlendMode
blendModeBinder func()
// Cull Mode
// cullModeEnable bool
cullMode CullMode
cullModeBinder func()
// Vert Buffer
vertBuf *VertexBuffer
vertBufDrawer func()
clearColor RGBA
clearMode gl.Enum
clearFunc func()
}
var state *stateTracker
func init() {
state = &stateTracker{}
state.depthModeBinder = func() {
if state.depthMode == DepthModeNone {
gl.Disable(gl.DEPTH_TEST)
} else {
gl.Enable(gl.DEPTH_TEST)
data := depthModeLut[state.depthMode]
gl.DepthFunc(data.mode)
}
}
state.blendModeBinder = func() {
if state.blendMode == BlendModeNone {
gl.Disable(gl.BLEND)
return
}
gl.Enable(gl.BLEND) // TODO: This only needs to run if it was disabled previously
data := blendModeLut[state.blendMode]
gl.BlendFunc(data.src, data.dst)
}
state.cullModeBinder = func() {
if state.cullMode == CullModeNone {
gl.Disable(gl.CULL_FACE)
} else {
gl.Enable(gl.CULL_FACE)
data := cullModeLut[state.cullMode]
gl.CullFace(data.face)
gl.FrontFace(data.dir)
}
}
// state.enableDepthFunc = func() {
// if state.depthTest {
// gl.Enable(gl.DEPTH_TEST)
// } else {
// gl.Disable(gl.DEPTH_TEST)
// }
// }
// state.depthFuncBinder = func() {
// gl.DepthFunc(state.depthFunc)
// }
state.fboBinder = func() {
// TODO - Note: I set the viewport when I bind the framebuffer. Is this okay?
gl.Viewport(0, 0, int(state.fboBounds.W()), int(state.fboBounds.H()))
gl.BindFramebuffer(gl.FRAMEBUFFER, state.fbo)
}
state.textureBinder = func() {
gl.ActiveTexture(gl.TEXTURE0)
// gl.ActiveTexture(gl.TEXTURE0 + position); // TODO - include position
if state.texture == nil {
gl.BindTexture(gl.TEXTURE_2D, gl.NoTexture)
} else {
gl.BindTexture(gl.TEXTURE_2D, state.texture.texture)
}
// gl.BindTexture(gl.TEXTURE_2D, state.texture.texture)
}
// state.blendFuncBinder = func() {
// gl.BlendFunc(state.blendSrc, state.blendDst)
// }
// state.cullModeBinder = func() {
// if state.cullModeEnable {
// gl.Enable(gl.CULL_FACE)
// gl.CullFace(state.cullMode.face)
// gl.FrontFace(state.cullMode.dir)
// } else {
// gl.Disable(gl.CULL_FACE)
// }
// }
state.vertBufDrawer = func() {
state.vertBuf.mainthreadDraw()
}
state.clearFunc = func() {
gl.ClearColor(float32(state.clearColor.R), float32(state.clearColor.G), float32(state.clearColor.B), float32(state.clearColor.A))
// gl.Clear(gl.COLOR_BUFFER_BIT) // Make configurable?
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
}
}
// TODO - maybe allow for more than 15 if the platform supports it? TODO - max texture units
func (s *stateTracker) bindTexture(texture *Texture) {
// TODO: Check if changed
s.texture = texture
mainthread.Call(state.textureBinder)
}
func (s *stateTracker) bindFramebuffer(fbo gl.Framebuffer, bounds Rect) {
if s.fbo.Equal(fbo) && s.fboBounds == bounds {
return
}
state.fbo = fbo
state.fboBounds = bounds
mainthread.Call(s.fboBinder)
}
func (s *stateTracker) setDepthMode(depth DepthMode) {
if s.depthMode == depth {
return // Skip: State already matches
}
s.depthMode = depth
mainthread.Call(s.depthModeBinder)
}
func (s *stateTracker) setBlendMode(blend BlendMode) {
if s.blendMode == blend {
return // Skip: State already matches
}
s.blendMode = blend
mainthread.Call(s.blendModeBinder)
}
func (s *stateTracker) setCullMode(cull CullMode) {
if s.cullMode == cull {
return // Skip: State already matches
}
s.cullMode = cull
mainthread.Call(s.cullModeBinder)
}
// func (s *stateTracker) enableDepthTest(enable bool) {
// if s.depthTest == enable {
// return // Skip if state already matches
// }
// s.depthTest = enable
// mainthread.Call(s.enableDepthFunc)
// }
// func (s *stateTracker) setDepthFunc(depthFunc gl.Enum) {
// if s.depthFunc == depthFunc {
// return // Skip if already enabled and depth functions match
// }
// s.depthFunc = depthFunc
// mainthread.Call(s.depthFuncBinder)
// }
// func (s *stateTracker) setBlendFunc(src, dst gl.Enum) {
// s.blendSrc = src
// s.blendDst = dst
// mainthread.Call(s.blendFuncBinder)
// }
func (s *stateTracker) drawVertBuffer(vb *VertexBuffer) {
s.vertBuf = vb
mainthread.Call(s.vertBufDrawer)
}
func (s *stateTracker) clearTarget(color RGBA) {
s.clearColor = color
mainthread.Call(s.clearFunc)
}
// func (s *stateTracker) disableCullMode() {
// if s.cullModeEnable == false {
// return // Skip if state already matches
// }
// s.cullModeEnable = false
// mainthread.Call(s.cullModeBinder)
// }
// func (s *stateTracker) enableCullMode(cullMode CullMode) {
// if s.cullModeEnable == true && s.cullMode == cullMode {
// return // Skip if state already matches
// }
// s.cullModeEnable = true
// s.cullMode = cullMode
// mainthread.Call(s.cullModeBinder)
// }