forked from RPCS3/rpcs3
/
GLTextOut.h
219 lines (170 loc) · 4.82 KB
/
GLTextOut.h
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
#pragma once
#include "util/types.hpp"
#include "GLHelpers.h"
#include "../Common/TextGlyphs.h"
#include <string>
#include <vector>
#include <unordered_map>
namespace gl
{
class text_writer
{
private:
gl::glsl::program m_program;
gl::glsl::shader m_vs;
gl::glsl::shader m_fs;
gl::vao m_vao;
gl::buffer m_text_buffer;
gl::buffer m_scale_offsets_buffer;
std::unordered_map<u8, std::pair<u32, u32>> m_offsets;
bool initialized = false;
bool enabled = false;
double m_scale = 1.0;
void init_program()
{
std::string vs =
{
"#version 420\n"
"#extension GL_ARB_shader_draw_parameters: enable\n"
"layout(location=0) in vec2 pos;\n"
"uniform vec2 offsets[255];\n"
"uniform vec2 scale;\n"
"\n"
"void main()\n"
"{\n"
" vec2 offset = offsets[gl_DrawIDARB];\n"
" gl_Position = vec4(pos, 0., 1.);\n"
" gl_Position.xy = gl_Position.xy * scale + offset;\n"
"}\n"
};
std::string fs =
{
"#version 420\n"
"layout(location=0) out vec4 col0;\n"
"uniform vec4 draw_color;\n"
"\n"
"void main()\n"
"{\n"
" col0 = draw_color;\n"
"}\n"
};
m_fs.create(::glsl::program_domain::glsl_fragment_program, fs);
m_fs.compile();
m_vs.create(::glsl::program_domain::glsl_vertex_program, vs);
m_vs.compile();
m_program.create();
m_program.attach(m_vs);
m_program.attach(m_fs);
m_program.link();
}
void load_program(float scale_x, float scale_y, float *offsets, usz nb_offsets, color4f color)
{
float scale[] = { scale_x, scale_y };
m_program.use();
m_program.uniforms["draw_color"] = color;
glProgramUniform2fv(m_program.id(), m_program.uniforms["offsets"].location(), static_cast<GLsizei>(nb_offsets), offsets);
glProgramUniform2fv(m_program.id(), m_program.uniforms["scale"].location(), 1, scale);
}
public:
text_writer() = default;
~text_writer() = default;
void init()
{
m_text_buffer.create();
m_scale_offsets_buffer.create();
GlyphManager glyph_source;
auto points = glyph_source.generate_point_map();
const usz buffer_size = points.size() * sizeof(GlyphManager::glyph_point);
m_text_buffer.data(buffer_size, points.data());
m_offsets = glyph_source.get_glyph_offsets();
m_scale_offsets_buffer.data(512 * 4 * sizeof(float));
//Init VAO
int old_vao;
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &old_vao);
m_vao.create();
m_vao.bind();
//This is saved as part of the bound VAO's state
m_vao.array_buffer = m_text_buffer;
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, false, sizeof(GlyphManager::glyph_point), 0);
glBindVertexArray(old_vao);
init_program();
initialized = true;
}
void set_enabled(bool state)
{
enabled = state;
}
bool is_enabled()
{
return enabled;
}
void print_text(int x, int y, int target_w, int target_h, const std::string &text, color4f color = { 0.3f, 1.f, 0.3f, 1.f })
{
if (!enabled) return;
ensure(initialized);
std::vector<GLint> offsets;
std::vector<GLsizei> counts;
std::vector<float> shader_offsets;
char *s = const_cast<char *>(text.c_str());
//Y is in raster coordinates: convert to bottom-left origin
y = ((target_h / m_scale) - y - 16);
//Compress [0, w] and [0, h] into range [-1, 1]
float scale_x = m_scale * 2.f / target_w;
float scale_y = m_scale * 2.f / target_h;
float base_offset = 0.f;
shader_offsets.reserve(text.length() * 2);
while (u8 offset = static_cast<u8>(*s))
{
bool to_draw = false; //Can be false for space or unsupported characters
auto o = m_offsets.find(offset);
if (o != m_offsets.end())
{
if (o->second.second > 0)
{
to_draw = true;
offsets.push_back(o->second.first);
counts.push_back(o->second.second);
}
}
if (to_draw)
{
//Generate a scale_offset pair for this entry
float offset_x = scale_x * (x + base_offset);
offset_x -= 1.f;
float offset_y = scale_y * y;
offset_y -= 1.f;
shader_offsets.push_back(offset_x);
shader_offsets.push_back(offset_y);
}
base_offset += 9.f;
s++;
}
//TODO: Add drop shadow if deemed necessary for visibility
int old_vao;
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &old_vao);
load_program(scale_x, scale_y, shader_offsets.data(), counts.size(), color);
m_vao.bind();
glMultiDrawArrays(GL_POINTS, offsets.data(), counts.data(), static_cast<GLsizei>(counts.size()));
glBindVertexArray(old_vao);
}
void close()
{
if (initialized)
{
m_scale_offsets_buffer.remove();
m_text_buffer.remove();
m_vao.remove();
m_program.remove();
m_fs.remove();
m_vs.remove();
initialized = false;
}
}
void set_scale(double scale)
{
// Restrict scale to 2. The dots are gonna be too sparse otherwise.
m_scale = std::min(scale, 2.0);
}
};
}