forked from nelsam/gxui
/
blitter.go
306 lines (275 loc) · 7.85 KB
/
blitter.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright 2015 The Go 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 gl
import (
"github.com/nelsam/gxui"
"github.com/nelsam/gxui/math"
"github.com/goxjs/gl"
)
const (
debugWireframePolygons = false
vsCopySrc = `
attribute vec2 aPosition;
varying vec2 vTexcoords;
uniform mat3 mPos;
uniform mat3 mUV;
void main() {
vec3 pos3 = vec3(aPosition, 1.0);
gl_Position = vec4(mPos * pos3, 1.0);
vTexcoords = (mUV * pos3).xy;
}`
fsCopySrc = `
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D source;
varying vec2 vTexcoords;
void main() {
gl_FragColor = texture2D(source, vTexcoords);
}`
vsColorSrc = `
attribute vec2 aPosition;
uniform mat3 mPos;
void main() {
vec3 pos3 = vec3(aPosition, 1.0);
gl_Position = vec4((mPos * pos3).xy, 0.0, 1.0);
}`
fsColorSrc = `
#ifdef GL_ES
precision mediump float;
#endif
uniform vec4 Color;
void main() {
gl_FragColor = Color;
gl_FragColor *= gl_FragColor.a; // PMA
}`
vsFontSrc = `
attribute vec2 aSrc;
attribute vec2 aDst;
attribute vec4 aClp;
attribute vec4 aCol;
varying vec2 vSrc;
varying vec4 vCol;
varying vec2 vClp;
uniform mat3 mSrc;
uniform mat3 mDst;
void main() {
vec2 vClipMin = (mDst * vec3(aClp.xy, 1.0)).xy;
vec2 vClipMax = (mDst * vec3(aClp.zw, 1.0)).xy;
gl_Position = vec4(mDst * vec3(aDst, 1.0), 1.0);
vSrc = (mSrc * vec3(aSrc, 1.0)).xy;
vClp = (gl_Position.xy - vClipMin) / (vClipMax - vClipMin);
vCol = aCol;
}`
fsFontSrc = `
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D source;
varying vec2 vSrc;
varying vec4 vCol;
varying vec2 vClp;
void main() {
vec2 clipping = step(vec2(0.0, 0.0), vClp) * step(vClp, vec2(1.0, 1.0));
gl_FragColor = vCol * texture2D(source, vSrc).aaaa;
gl_FragColor *= clipping.x * clipping.y;
}`
)
type glyphBatch struct {
DstRects []float32
SrcRects []float32
Colors []float32
ClipRects []float32
Indices []uint16
GlyphPage *textureContext
}
type blitter struct {
stats *contextStats
quad *shape
copyShader *shaderProgram
colorShader *shaderProgram
fontShader *shaderProgram
glyphBatch glyphBatch
}
func newBlitter(ctx *context, stats *contextStats) *blitter {
return &blitter{
stats: stats,
quad: newQuadShape(),
copyShader: newShaderProgram(ctx, vsCopySrc, fsCopySrc),
colorShader: newShaderProgram(ctx, vsColorSrc, fsColorSrc),
fontShader: newShaderProgram(ctx, vsFontSrc, fsFontSrc),
}
}
func (b *blitter) destroy(ctx *context) {
b.copyShader.destroy(ctx)
b.colorShader.destroy(ctx)
b.fontShader.destroy(ctx)
}
func (b *blitter) blit(ctx *context, tc *textureContext, srcRect, dstRect math.Rect, ds *drawState) {
b.commitGlyphs(ctx)
dstRect = dstRect.Offset(ds.OriginPixels)
sw, sh := tc.sizePixels.WH()
dw, dh := ctx.sizePixels.WH()
var mUV math.Mat3
if tc.flipY {
mUV = math.CreateMat3(
float32(srcRect.W())/float32(sw), 0, 0,
0, -float32(srcRect.H())/float32(sh), 0,
float32(srcRect.Min.X)/float32(sw),
1.0-float32(srcRect.Min.Y)/float32(sh), 1,
)
} else {
mUV = math.CreateMat3(
float32(srcRect.W())/float32(sw), 0, 0,
0, float32(srcRect.H())/float32(sh), 0,
float32(srcRect.Min.X)/float32(sw),
float32(srcRect.Min.Y)/float32(sh), 1,
)
}
mPos := math.CreateMat3(
+2.0*float32(dstRect.W())/float32(dw), 0, 0,
0, -2.0*float32(dstRect.H())/float32(dh), 0,
-1.0+2.0*float32(dstRect.Min.X)/float32(dw),
+1.0-2.0*float32(dstRect.Min.Y)/float32(dh), 1,
)
if !tc.pma {
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
}
b.quad.draw(ctx, b.copyShader, uniformBindings{
"source": tc,
"mUV": mUV,
"mPos": mPos,
})
if !tc.pma {
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
}
b.stats.drawCallCount++
}
func (b *blitter) blitGlyph(ctx *context, tc *textureContext, c gxui.Color, srcRect, dstRect math.Rect, ds *drawState) {
dstRect = dstRect.Offset(ds.OriginPixels)
if b.glyphBatch.GlyphPage != tc {
b.commitGlyphs(ctx)
b.glyphBatch.GlyphPage = tc
}
i := uint16(len(b.glyphBatch.DstRects)) / 2
clip := []float32{
float32(ds.ClipPixels.Min.X),
float32(ds.ClipPixels.Min.Y),
float32(ds.ClipPixels.Max.X),
float32(ds.ClipPixels.Max.Y),
}
b.glyphBatch.DstRects = append(b.glyphBatch.DstRects,
float32(dstRect.Min.X), float32(dstRect.Min.Y),
float32(dstRect.Max.X), float32(dstRect.Min.Y),
float32(dstRect.Min.X), float32(dstRect.Max.Y),
float32(dstRect.Max.X), float32(dstRect.Max.Y),
)
b.glyphBatch.SrcRects = append(b.glyphBatch.SrcRects,
float32(srcRect.Min.X), float32(srcRect.Min.Y),
float32(srcRect.Max.X), float32(srcRect.Min.Y),
float32(srcRect.Min.X), float32(srcRect.Max.Y),
float32(srcRect.Max.X), float32(srcRect.Max.Y),
)
b.glyphBatch.ClipRects = append(b.glyphBatch.ClipRects,
clip[0], clip[1], clip[2], clip[3],
clip[0], clip[1], clip[2], clip[3],
clip[0], clip[1], clip[2], clip[3],
clip[0], clip[1], clip[2], clip[3],
)
c = c.MulRGB(c.A) // PMA
b.glyphBatch.Colors = append(b.glyphBatch.Colors,
c.R, c.G, c.B, c.A,
c.R, c.G, c.B, c.A,
c.R, c.G, c.B, c.A,
c.R, c.G, c.B, c.A,
)
b.glyphBatch.Indices = append(b.glyphBatch.Indices,
i, i+1, i+2,
i+2, i+1, i+3,
)
}
func (b *blitter) blitShape(ctx *context, shape shape, color gxui.Color, ds *drawState) {
b.commitGlyphs(ctx)
dipsToPixels := ctx.resolution.dipsToPixels()
dw, dh := ctx.sizePixels.WH()
mPos := math.CreateMat3(
+2.0*dipsToPixels/float32(dw), 0, 0,
0, -2.0*dipsToPixels/float32(dh), 0,
-1.0+2.0*float32(ds.OriginPixels.X)/float32(dw),
+1.0-2.0*float32(ds.OriginPixels.Y)/float32(dh), 1,
)
shape.draw(ctx, b.colorShader, uniformBindings{
"mPos": mPos,
"Color": color,
})
if debugWireframePolygons {
// glPolygonMode is not available in OpenGL ES/WebGL (since its implementation is very inefficient; a shame because it's useful for debugging).
//gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE)
shape.draw(ctx, b.colorShader, uniformBindings{
"mPos": mPos,
"Color": gxui.Blue,
})
//gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL)
}
b.stats.drawCallCount++
}
func (b *blitter) blitRect(ctx *context, dstRect math.Rect, color gxui.Color, ds *drawState) {
b.commitGlyphs(ctx)
dstRect = dstRect.Offset(ds.OriginPixels)
dw, dh := ctx.sizePixels.WH()
mPos := math.CreateMat3(
+2.0*float32(dstRect.W())/float32(dw), 0, 0,
0, -2.0*float32(dstRect.H())/float32(dh), 0,
-1.0+2.0*float32(dstRect.Min.X)/float32(dw),
+1.0-2.0*float32(dstRect.Min.Y)/float32(dh), 1,
)
b.quad.draw(ctx, b.colorShader, uniformBindings{
"mPos": mPos,
"Color": color,
})
b.stats.drawCallCount++
}
func (b *blitter) commit(ctx *context) {
b.commitGlyphs(ctx)
}
func (b *blitter) commitGlyphs(ctx *context) {
tc := b.glyphBatch.GlyphPage
if tc == nil {
return
}
sw, sh := tc.sizePixels.WH()
dw, dh := ctx.sizePixels.WH()
mSrc := math.CreateMat3(
1.0/float32(sw), 0, 0,
0, 1.0/float32(sh), 0,
0.0, 0.0, 1,
)
mDst := math.CreateMat3(
+2.0/float32(dw), 0, 0,
0, -2.0/float32(dh), 0,
-1.0, +1.0, 1,
)
vb := newVertexBuffer(
newVertexStream("aDst", stFloatVec2, b.glyphBatch.DstRects),
newVertexStream("aSrc", stFloatVec2, b.glyphBatch.SrcRects),
newVertexStream("aClp", stFloatVec4, b.glyphBatch.ClipRects),
newVertexStream("aCol", stFloatVec4, b.glyphBatch.Colors),
)
ib := newIndexBuffer(ptUshort, b.glyphBatch.Indices)
s := newShape(vb, ib, dmTriangles)
gl.Disable(gl.SCISSOR_TEST)
s.draw(ctx, b.fontShader, uniformBindings{
"source": tc,
"mDst": mDst,
"mSrc": mSrc,
})
gl.Enable(gl.SCISSOR_TEST)
b.glyphBatch.GlyphPage = nil
b.glyphBatch.DstRects = b.glyphBatch.DstRects[:0]
b.glyphBatch.SrcRects = b.glyphBatch.SrcRects[:0]
b.glyphBatch.ClipRects = b.glyphBatch.ClipRects[:0]
b.glyphBatch.Colors = b.glyphBatch.Colors[:0]
b.glyphBatch.Indices = b.glyphBatch.Indices[:0]
b.stats.drawCallCount++
}