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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/*
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
* Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
* Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Assertions.h>
#include <AK/Debug.h>
#include <LibGL/GLContext.h>
namespace GL {
void GLContext::gl_array_element(GLint i)
{
APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_array_element, i);
RETURN_WITH_ERROR_IF(i < 0, GL_INVALID_VALUE);
// This is effectively the same as `gl_draw_elements`, except we only output a single
// vertex (this is done between a `gl_begin/end` call) that is to be rendered.
if (!m_client_side_vertex_array_enabled)
return;
if (m_client_side_color_array_enabled) {
float color[4] { 0.f, 0.f, 0.f, 1.f };
read_from_vertex_attribute_pointer(m_client_color_pointer, i, color);
gl_color(color[0], color[1], color[2], color[3]);
}
for (size_t t = 0; t < m_client_tex_coord_pointer.size(); ++t) {
if (m_client_side_texture_coord_array_enabled[t]) {
float tex_coords[4] { 0.f, 0.f, 0.f, 1.f };
read_from_vertex_attribute_pointer(m_client_tex_coord_pointer[t], i, tex_coords);
gl_multi_tex_coord(GL_TEXTURE0 + t, tex_coords[0], tex_coords[1], tex_coords[2], tex_coords[3]);
}
}
if (m_client_side_normal_array_enabled) {
float normal[3];
read_from_vertex_attribute_pointer(m_client_normal_pointer, i, normal);
gl_normal(normal[0], normal[1], normal[2]);
}
float vertex[4] { 0.f, 0.f, 0.f, 1.f };
read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex);
gl_vertex(vertex[0], vertex[1], vertex[2], vertex[3]);
}
void GLContext::gl_color(GLdouble r, GLdouble g, GLdouble b, GLdouble a)
{
APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_color, r, g, b, a);
m_current_vertex_color = {
static_cast<float>(r),
static_cast<float>(g),
static_cast<float>(b),
static_cast<float>(a),
};
}
void GLContext::gl_color_pointer(GLint size, GLenum type, GLsizei stride, void const* pointer)
{
RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
RETURN_WITH_ERROR_IF(!(size == 3 || size == 4), GL_INVALID_VALUE);
RETURN_WITH_ERROR_IF(type != GL_BYTE
&& type != GL_UNSIGNED_BYTE
&& type != GL_SHORT
&& type != GL_UNSIGNED_SHORT
&& type != GL_INT
&& type != GL_UNSIGNED_INT
&& type != GL_FLOAT
&& type != GL_DOUBLE,
GL_INVALID_ENUM);
RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
m_client_color_pointer = { .size = size, .type = type, .stride = stride, .pointer = pointer };
}
void GLContext::gl_draw_arrays(GLenum mode, GLint first, GLsizei count)
{
APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_arrays, mode, first, count);
RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
// FIXME: Some modes are still missing (GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES)
RETURN_WITH_ERROR_IF(!(mode == GL_TRIANGLE_STRIP
|| mode == GL_TRIANGLE_FAN
|| mode == GL_TRIANGLES
|| mode == GL_QUADS
|| mode == GL_QUAD_STRIP
|| mode == GL_POLYGON),
GL_INVALID_ENUM);
RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE);
// At least the vertex array needs to be enabled
if (!m_client_side_vertex_array_enabled)
return;
auto last = first + count;
gl_begin(mode);
for (int i = first; i < last; i++) {
if (m_client_side_color_array_enabled) {
float color[4] { 0.f, 0.f, 0.f, 1.f };
read_from_vertex_attribute_pointer(m_client_color_pointer, i, color);
gl_color(color[0], color[1], color[2], color[3]);
}
for (size_t t = 0; t < m_client_tex_coord_pointer.size(); ++t) {
if (m_client_side_texture_coord_array_enabled[t]) {
float tex_coords[4] { 0.f, 0.f, 0.f, 1.f };
read_from_vertex_attribute_pointer(m_client_tex_coord_pointer[t], i, tex_coords);
gl_multi_tex_coord(GL_TEXTURE0 + t, tex_coords[0], tex_coords[1], tex_coords[2], tex_coords[3]);
}
}
if (m_client_side_normal_array_enabled) {
float normal[3];
read_from_vertex_attribute_pointer(m_client_normal_pointer, i, normal);
gl_normal(normal[0], normal[1], normal[2]);
}
float vertex[4] { 0.f, 0.f, 0.f, 1.f };
read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex);
gl_vertex(vertex[0], vertex[1], vertex[2], vertex[3]);
}
gl_end();
}
void GLContext::gl_draw_elements(GLenum mode, GLsizei count, GLenum type, void const* indices)
{
APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_draw_elements, mode, count, type, indices);
RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
// FIXME: Some modes are still missing (GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES)
RETURN_WITH_ERROR_IF(!(mode == GL_TRIANGLE_STRIP
|| mode == GL_TRIANGLE_FAN
|| mode == GL_TRIANGLES
|| mode == GL_QUADS
|| mode == GL_QUAD_STRIP
|| mode == GL_POLYGON),
GL_INVALID_ENUM);
RETURN_WITH_ERROR_IF(!(type == GL_UNSIGNED_BYTE
|| type == GL_UNSIGNED_SHORT
|| type == GL_UNSIGNED_INT),
GL_INVALID_ENUM);
RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE);
// At least the vertex array needs to be enabled
if (!m_client_side_vertex_array_enabled)
return;
gl_begin(mode);
for (int index = 0; index < count; index++) {
int i = 0;
switch (type) {
case GL_UNSIGNED_BYTE:
i = reinterpret_cast<GLubyte const*>(indices)[index];
break;
case GL_UNSIGNED_SHORT:
i = reinterpret_cast<GLushort const*>(indices)[index];
break;
case GL_UNSIGNED_INT:
i = reinterpret_cast<GLuint const*>(indices)[index];
break;
}
if (m_client_side_color_array_enabled) {
float color[4] { 0.f, 0.f, 0.f, 1.f };
read_from_vertex_attribute_pointer(m_client_color_pointer, i, color);
gl_color(color[0], color[1], color[2], color[3]);
}
for (size_t t = 0; t < m_client_tex_coord_pointer.size(); ++t) {
if (m_client_side_texture_coord_array_enabled[t]) {
float tex_coords[4] { 0.f, 0.f, 0.f, 1.f };
read_from_vertex_attribute_pointer(m_client_tex_coord_pointer[t], i, tex_coords);
gl_multi_tex_coord(GL_TEXTURE0 + t, tex_coords[0], tex_coords[1], tex_coords[2], tex_coords[3]);
}
}
if (m_client_side_normal_array_enabled) {
float normal[3];
read_from_vertex_attribute_pointer(m_client_normal_pointer, i, normal);
gl_normal(normal[0], normal[1], normal[2]);
}
float vertex[4] { 0.f, 0.f, 0.f, 1.f };
read_from_vertex_attribute_pointer(m_client_vertex_pointer, i, vertex);
gl_vertex(vertex[0], vertex[1], vertex[2], vertex[3]);
}
gl_end();
}
void GLContext::gl_normal(GLfloat nx, GLfloat ny, GLfloat nz)
{
APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_normal, nx, ny, nz);
m_current_vertex_normal = { nx, ny, nz };
}
void GLContext::gl_normal_pointer(GLenum type, GLsizei stride, void const* pointer)
{
RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
RETURN_WITH_ERROR_IF(type != GL_BYTE
&& type != GL_SHORT
&& type != GL_INT
&& type != GL_FLOAT
&& type != GL_DOUBLE,
GL_INVALID_ENUM);
RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
m_client_normal_pointer = { .size = 3, .type = type, .stride = stride, .pointer = pointer };
}
void GLContext::gl_vertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w)
{
APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_vertex, x, y, z, w);
GPU::Vertex vertex;
vertex.position = { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z), static_cast<float>(w) };
vertex.color = m_current_vertex_color;
for (size_t i = 0; i < m_device_info.num_texture_units; ++i)
vertex.tex_coords[i] = m_current_vertex_tex_coord[i];
vertex.normal = m_current_vertex_normal;
m_vertex_list.append(vertex);
}
void GLContext::gl_vertex_pointer(GLint size, GLenum type, GLsizei stride, void const* pointer)
{
RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
RETURN_WITH_ERROR_IF(!(size == 2 || size == 3 || size == 4), GL_INVALID_VALUE);
RETURN_WITH_ERROR_IF(!(type == GL_SHORT || type == GL_INT || type == GL_FLOAT || type == GL_DOUBLE), GL_INVALID_ENUM);
RETURN_WITH_ERROR_IF(stride < 0, GL_INVALID_VALUE);
m_client_vertex_pointer = { .size = size, .type = type, .stride = stride, .pointer = pointer };
}
// General helper function to read arbitrary vertex attribute data into a float array
void GLContext::read_from_vertex_attribute_pointer(VertexAttribPointer const& attrib, int index, float* elements)
{
auto byte_ptr = reinterpret_cast<char const*>(attrib.pointer);
auto normalize = attrib.normalize;
size_t stride = attrib.stride;
switch (attrib.type) {
case GL_BYTE: {
if (stride == 0)
stride = sizeof(GLbyte) * attrib.size;
for (int i = 0; i < attrib.size; i++) {
elements[i] = *(reinterpret_cast<GLbyte const*>(byte_ptr + stride * index) + i);
if (normalize)
elements[i] /= 0x80;
}
break;
}
case GL_UNSIGNED_BYTE: {
if (stride == 0)
stride = sizeof(GLubyte) * attrib.size;
for (int i = 0; i < attrib.size; i++) {
elements[i] = *(reinterpret_cast<GLubyte const*>(byte_ptr + stride * index) + i);
if (normalize)
elements[i] /= 0xff;
}
break;
}
case GL_SHORT: {
if (stride == 0)
stride = sizeof(GLshort) * attrib.size;
for (int i = 0; i < attrib.size; i++) {
elements[i] = *(reinterpret_cast<GLshort const*>(byte_ptr + stride * index) + i);
if (normalize)
elements[i] /= 0x8000;
}
break;
}
case GL_UNSIGNED_SHORT: {
if (stride == 0)
stride = sizeof(GLushort) * attrib.size;
for (int i = 0; i < attrib.size; i++) {
elements[i] = *(reinterpret_cast<GLushort const*>(byte_ptr + stride * index) + i);
if (normalize)
elements[i] /= 0xffff;
}
break;
}
case GL_INT: {
if (stride == 0)
stride = sizeof(GLint) * attrib.size;
for (int i = 0; i < attrib.size; i++) {
elements[i] = *(reinterpret_cast<GLint const*>(byte_ptr + stride * index) + i);
if (normalize)
elements[i] /= 0x80000000;
}
break;
}
case GL_UNSIGNED_INT: {
if (stride == 0)
stride = sizeof(GLuint) * attrib.size;
for (int i = 0; i < attrib.size; i++) {
elements[i] = *(reinterpret_cast<GLuint const*>(byte_ptr + stride * index) + i);
if (normalize)
elements[i] /= 0xffffffff;
}
break;
}
case GL_FLOAT: {
if (stride == 0)
stride = sizeof(GLfloat) * attrib.size;
for (int i = 0; i < attrib.size; i++)
elements[i] = *(reinterpret_cast<GLfloat const*>(byte_ptr + stride * index) + i);
break;
}
case GL_DOUBLE: {
if (stride == 0)
stride = sizeof(GLdouble) * attrib.size;
for (int i = 0; i < attrib.size; i++)
elements[i] = static_cast<float>(*(reinterpret_cast<GLdouble const*>(byte_ptr + stride * index) + i));
break;
}
}
}
}
|