This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
/
gldrawvert.go
195 lines (172 loc) · 5.77 KB
/
gldrawvert.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
package pixel
const drawVertexShader = "\n" + `#version 460 core
////////////////////////////////////////////////////////////////////////////////
const uint cmdPicture = 1;
const uint cmdPictureExt = 2;
const uint cmdText = 3;
const uint cmdPoint = 4;
const uint cmdLines = 5;
const uint cmdTriangles = 6;
const uint cmdBox = 7;
const vec2 corners[4] = vec2[4](
vec2(0, 0),
vec2(1, 0),
vec2(0, 1),
vec2(1, 1)
);
////////////////////////////////////////////////////////////////////////////////
layout(std140, binding = 0) uniform ScreenUBO {
vec2 PixelSize;
ivec2 CanvasMargin;
};
////////////////////////////////////////////////////////////////////////////////
layout(binding = 0) uniform isamplerBuffer parameters;
layout(binding = 1) uniform isamplerBuffer pictureMap;
////////////////////////////////////////////////////////////////////////////////
out gl_PerVertex {
vec4 gl_Position;
};
out PerVertex {
layout(location=0) flat uint Command;
layout(location=1) flat uint Bin;
layout(location=2) vec2 UV;
layout(location=3) flat uint ColorIndex;
layout(location=4) flat vec4 Box;
layout(location=5) flat float Slope;
layout(location=6) flat uint Flags;
};
const uint steep = 0x01;
////////////////////////////////////////////////////////////////////////////////
float floatZ(int z) {
return float(z)/float(0x7FFF);
}
////////////////////////////////////////////////////////////////////////////////
void main(void)
{
Command = gl_BaseInstance >> 24;
int param = gl_BaseInstance & 0xFFFFFF;
int offset;
int instance = gl_InstanceID;
int vertex = gl_VertexID;
int m, x, y, z, x2, y2, x3, y3, dx, dy;
uint c;
vec2 p, wh;
vec2 t, n, pts[4];
switch (Command) {
case cmdPicture:
// Parameters
offset = 4*instance;
m = texelFetch(parameters, param+0+offset).r;
z = texelFetch(parameters, param+1+offset).r;
x = texelFetch(parameters, param+2+offset).r;
y = texelFetch(parameters, param+3+offset).r;
// Mapping of the picture
m *= 5;
Bin = texelFetch(pictureMap, m+0).r;
UV = vec2(texelFetch(pictureMap, m+1).r, texelFetch(pictureMap, m+2).r);
wh = vec2(texelFetch(pictureMap, m+3).r, texelFetch(pictureMap, m+4).r);
// Picture quad
p = (CanvasMargin + vec2(x, y) + corners[vertex] * wh) * PixelSize;
gl_Position = vec4(p * vec2(2, -2) + vec2(-1,1), floatZ(z), 1);
UV += corners[vertex] * wh;
break;
case cmdText:
// Parameters
offset = 2*instance;
c = texelFetch(parameters, param+0).r;
z = texelFetch(parameters, param+1).r;
y = texelFetch(parameters, param+2).r;
m = texelFetch(parameters, param+3+offset).r;
x = texelFetch(parameters, param+4+offset).r;
// Mapping of the current character
m *= 5;
Bin = texelFetch(pictureMap, m+0).r;
UV = vec2(texelFetch(pictureMap, m+1).r, texelFetch(pictureMap, m+2).r);
wh = vec2(texelFetch(pictureMap, m+3).r, texelFetch(pictureMap, m+4).r);
// Character quad
p = (CanvasMargin + vec2(x, y) + corners[vertex] * wh) * PixelSize;
gl_Position = vec4(p * vec2(2, -2) + vec2(-1,1), floatZ(z), 1);
UV += corners[vertex] * wh;
ColorIndex = uint(c&0xFFFF);
break;
case cmdPoint:
offset = 4*instance;
// Parameters
c = texelFetch(parameters, param+0+offset).r;
z = texelFetch(parameters, param+1+offset).r;
x = texelFetch(parameters, param+2+offset).r;
y = texelFetch(parameters, param+3+offset).r;
// Position
p = (CanvasMargin + vec2(x, y) + corners[vertex] * vec2(1.5,1.5)) * PixelSize;
gl_Position = vec4(p * vec2(2, -2) + vec2(-1,1), floatZ(z), 1);
// Color
ColorIndex = uint(c&0xFFFF);
break;
case cmdLines:
offset = 2*instance;
// Parameters
c = texelFetch(parameters, param+0).r;
z = texelFetch(parameters, param+1).r;
x = texelFetch(parameters, param+2+offset).r;
y = texelFetch(parameters, param+3+offset).r;
x2 = texelFetch(parameters, param+4+offset).r;
y2 = texelFetch(parameters, param+5+offset).r;
// Position
Box = vec4(x+CanvasMargin.x, y+CanvasMargin.y, x2+CanvasMargin.x, y2+CanvasMargin.y);
dx = x2-x;
dy = y2-y;
Flags = uint(abs(dx) < abs(dy)) * steep;
t = 0.25*normalize(vec2(dx, dy));
n = 0.75*normalize(vec2(-dy, dx));
pts = vec2[4](
vec2(x, y)+n-t,
vec2(x, y)-n-t,
vec2(x2, y2)+n+t,
vec2(x2, y2)-n+t
);
p = (CanvasMargin + vec2(0.5,0.5) + pts[vertex].xy) * PixelSize;
gl_Position = vec4(p * vec2(2, -2) + vec2(-1,1), floatZ(z), 1);
if (Flags == steep) {
Slope = float(dx)/float(dy);
} else {
Slope = float(dy)/float(dx);
}
// Color
ColorIndex = uint(c&0xFFFF);
break;
case cmdTriangles:
offset = 2*gl_VertexID;
// Parameters
c = texelFetch(parameters, param+0).r;
z = texelFetch(parameters, param+1).r;
x = texelFetch(parameters, param+2+offset).r;
y = texelFetch(parameters, param+3+offset).r;
p = (CanvasMargin + vec2(0.5,0.5) + vec2(x, y)) * PixelSize;
gl_Position = vec4(p * vec2(2, -2) + vec2(-1,1), floatZ(z), 1);
// Color
ColorIndex = uint(c&0xFFFF);
break;
case cmdBox:
offset = 7*instance;
// Parameters
c = texelFetch(parameters, param+0+offset).r;
z = texelFetch(parameters, param+1+offset).r;
Flags = texelFetch(parameters, param+2+offset).r;
x = texelFetch(parameters, param+3+offset).r;
y = texelFetch(parameters, param+4+offset).r;
x2 = texelFetch(parameters, param+5+offset).r;
y2 = texelFetch(parameters, param+6+offset).r;
wh = vec2(x2 -x+1, y2-y+1);
// Position
Box = vec4(x+CanvasMargin.x, y+CanvasMargin.y, x2+CanvasMargin.x, y2+CanvasMargin.y);
p = (CanvasMargin + vec2(x, y) + corners[vertex] * wh) * PixelSize;
gl_Position = vec4(p * vec2(2, -2) + vec2(-1,1), floatZ(z), 1);
// Color
ColorIndex = uint(c&0xFFFF);
break;
}
}
`
// Copyright (c) 2013-2018 Laurent Moussault. All rights reserved.
// Licensed under a simplified BSD license (see LICENSE file).
////////////////////////////////////////////////////////////////////////////////