-
Notifications
You must be signed in to change notification settings - Fork 1
/
renderer.c
470 lines (387 loc) · 10.2 KB
/
renderer.c
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include "renderer.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "rpi-mailbox-interface.h"
#include "vector2.h"
#include "color.h"
#include "dma.h"
static size_t get_buf_size(const graphics_t *device)
{
return device->width * device->height * (device->bpp >> 3);
}
graphics_t *graphics_create(size_t width, size_t height, size_t depth)
{
rpi_mailbox_property_t *mp;
graphics_t *device;
device = malloc(sizeof(graphics_t));
if (!device)
goto fail;
/* Initialise a framebuffer... */
RPI_PropertyInit();
RPI_PropertyAddTag(TAG_ALLOCATE_BUFFER);
RPI_PropertyAddTag(TAG_SET_PHYSICAL_SIZE, width, height);
RPI_PropertyAddTag(TAG_SET_VIRTUAL_SIZE, width, height * 2);
RPI_PropertyAddTag(TAG_SET_DEPTH, depth);
RPI_PropertyAddTag(TAG_GET_PITCH);
RPI_PropertyAddTag(TAG_GET_PHYSICAL_SIZE);
RPI_PropertyAddTag(TAG_GET_DEPTH);
RPI_PropertyProcess();
mp = RPI_PropertyGet(TAG_GET_PHYSICAL_SIZE);
if (mp) {
device->width = mp->data.buffer_32[0];
device->height = mp->data.buffer_32[1];
}
mp = RPI_PropertyGet(TAG_GET_DEPTH);
if (mp)
device->bpp = mp->data.buffer_32[0];
mp = RPI_PropertyGet(TAG_GET_PITCH);
if (mp)
device->pitch = mp->data.buffer_32[0];
mp = RPI_PropertyGet(TAG_ALLOCATE_BUFFER);
if (mp) {
device->fb = (uint8_t *)(uintptr_t)mp->data.buffer_32[0];
device->mem = (uint8_t *)(uintptr_t)
(mp->data.buffer_32[0] + get_buf_size(device));
}
return device;
fail:
return NULL;
}
void graphics_blank(const graphics_t *device)
{
dma_zero(device->mem, get_buf_size(device));
}
void graphics_background_color(const graphics_t *device, const color_t *color)
{
dma_fill(device->mem, color, get_buf_size(device));
}
/* static color_t overrun; */
static color_t *get_pixel(const graphics_t *device, size_t x, size_t y)
{
/* size_t bpos; */
/*if (x >= device->width || y >= device->height)
* return &overrun;
*/
/*bpos = y * device->pitch + x * (device->bpp >> 3);*/
return
(void *)(&device->mem[y * device->pitch + x * (device->bpp >> 3)]);
}
static void draw_line(const graphics_t *device,
const cvertex_t *l1, const cvertex_t *l2)
{
cvertex_t c;
color_t *pixel;
float pr;
c.pos = l1->pos;
for (; c.pos.x <= l2->pos.x; c.pos.x++) {
pixel = get_pixel(device, (int)c.pos.x, (int)c.pos.y);
pr = vector2_between(&c.pos, &l1->pos, &l2->pos);
color_interpolate(&c.color, &l1->color, &l2->color, pr);
pixel->r = c.color.r;
pixel->g = c.color.g;
pixel->b = c.color.b;
if (device->bpp == 32)
pixel->a = c.color.a;
}
}
static void fill_bottom_flat_triangle(const graphics_t *device,
const cvertex_t *v1,
const cvertex_t *v2,
const cvertex_t *v3)
{
cvertex_t l1;
cvertex_t l2;
float invslope1, invslope2;
float curx1, curx2;
float pr;
int scanlineY;
invslope1 = (v2->pos.x - v1->pos.x) / (v2->pos.y - v1->pos.y);
invslope2 = (v3->pos.x - v1->pos.x) / (v3->pos.y - v1->pos.y);
curx1 = v1->pos.x;
curx2 = v1->pos.x;
for (scanlineY = v1->pos.y; scanlineY <= v2->pos.y; scanlineY++) {
l1.pos.y = l2.pos.y = scanlineY;
l1.pos.x = curx1;
l2.pos.x = curx2;
pr = vector2_between(&l1.pos, &v1->pos, &v2->pos);
color_interpolate(&l1.color, &v1->color, &v2->color, pr);
pr = vector2_between(&l2.pos, &v1->pos, &v3->pos);
color_interpolate(&l2.color, &v1->color, &v3->color, pr);
draw_line(device, &l1, &l2);
curx1 += invslope1;
curx2 += invslope2;
}
}
static void fill_top_flat_triangle(const graphics_t *device,
const cvertex_t *v1,
const cvertex_t *v2,
const cvertex_t *v3)
{
cvertex_t l1;
cvertex_t l2;
float invslope1, invslope2;
float curx1, curx2;
float pr;
int scanlineY;
invslope1 = (v3->pos.x - v1->pos.x) / (v3->pos.y - v1->pos.y);
invslope2 = (v3->pos.x - v2->pos.x) / (v3->pos.y - v2->pos.y);
curx1 = v3->pos.x;
curx2 = v3->pos.x;
for (scanlineY = v3->pos.y; scanlineY > v1->pos.y; scanlineY--) {
l1.pos.y = l2.pos.y = scanlineY;
l1.pos.x = curx1;
l2.pos.x = curx2;
pr = vector2_between(&l1.pos, &v1->pos, &v3->pos);
color_interpolate(&l1.color, &v1->color, &v3->color, pr);
pr = vector2_between(&l2.pos, &v2->pos, &v3->pos);
color_interpolate(&l2.color, &v2->color, &v3->color, pr);
draw_line(device, &l1, &l2);
curx1 -= invslope1;
curx2 -= invslope2;
}
}
static int vert_cmp(const void *va, const void *vb)
{
int ret;
const cvertex_t * const *a;
const cvertex_t * const *b;
a = va;
b = vb;
ret = (*a)->pos.y - (*b)->pos.y;
if (!ret)
ret = (*a)->pos.x - (*b)->pos.x;
return ret;
}
static void vert_sort(const cvertex_t **v1,
const cvertex_t **v2,
const cvertex_t **v3)
{
const cvertex_t *vert[3];
vert[0] = *v1;
vert[1] = *v2;
vert[2] = *v3;
qsort(vert, 3, sizeof(cvertex_t *), vert_cmp);
*v1 = vert[0];
*v2 = vert[1];
*v3 = vert[2];
}
static void rasterize(const graphics_t *device,
const cvertex_t *v1,
const cvertex_t *v2,
const cvertex_t *v3)
{
vert_sort(&v1, &v2, &v3);
cvertex_t v4;
float pr;
if ((int)v2->pos.y == (int)v3->pos.y) {
fill_bottom_flat_triangle(device, v1, v2, v3);
} else if ((int)v1->pos.y == (int)v2->pos.y) {
fill_top_flat_triangle(device, v1, v2, v3);
} else {
v4.pos.x = (v1->pos.x + ((v2->pos.y - v1->pos.y) /
(v3->pos.y - v1->pos.y)) * (v3->pos.x - v1->pos.x));
v4.pos.y = v2->pos.y;
pr = vector2_between(&v4.pos, &v1->pos, &v3->pos);
color_interpolate(&v4.color, &v1->color, &v3->color, pr);
rasterize(device, v1, v2, &v4);
rasterize(device, v2, &v4, v3);
}
}
void graphics_draw(const graphics_t *device,
const cvertex_t *vertices, size_t len)
{
size_t i;
for (i = 0; i < len; i += 3) {
rasterize(device, &vertices[i],
&vertices[i + 1],
&vertices[i + 2]);
}
}
void graphics_draw_rectangle_outline(const graphics_t *device,
const color_t *color, const vector2_t *pos, const vector2_t *hw)
{
size_t dx, dy;
color_t *p;
size_t xmin = 0;
size_t xmax = hw->x;
size_t ymin = 0;
size_t ymax = hw->y;
if (pos->x + xmax > device->width)
xmax = device->width - pos->x;
if (pos->x < 0)
xmin = -pos->x;
if (pos->y + ymax > device->height)
ymax = device->height - pos->y;
if (pos->y < 0)
ymin = -pos->y;
/* horizontal */
for (dx = xmin; dx < xmax; dx++) {
p = get_pixel(device, pos->x + dx, pos->y);
p->b = color->b;
p->g = color->g;
p->r = color->r;
p->a = color->a;
p = get_pixel(device, pos->x + dx, pos->y + ymax - 1);
p->b = color->b;
p->g = color->g;
p->r = color->r;
p->a = color->a;
}
/* vertical */
for (dy = ymin + 1; dy < ymax - 1; dy++) {
p = get_pixel(device, pos->x, pos->y + dy);
p->b = color->b;
p->g = color->g;
p->r = color->r;
p->a = color->a;
p = get_pixel(device, pos->x + xmax - 1, pos->y + dy);
p->b = color->b;
p->g = color->g;
p->r = color->r;
p->a = color->a;
}
}
void graphics_draw_rectangle_dither(const graphics_t *device,
const color_t *color, const vector2_t *pos, const vector2_t *hw)
{
size_t dx, dy;
color_t *p;
size_t xmin = 0;
size_t xmax = hw->x;
size_t ymin = 0;
size_t ymax = hw->y;
if (pos->x + xmax > device->width)
xmax = device->width - pos->x;
if (pos->x < 0)
xmin = -pos->x;
if (pos->y + ymax > device->height)
ymax = device->height - pos->y;
if (pos->y < 0)
ymin = -pos->y;
for (dx = xmin; dx < xmax; dx += 2) {
for (dy = ymin; dy < ymax; dy += 2) {
p = get_pixel(device, pos->x + dx, pos->y + dy);
p->b = color->b;
p->g = color->g;
p->r = color->r;
p->a = color->a;
}
}
}
void graphics_draw_rectangle(const graphics_t *device, const color_t *color,
const vector2_t *pos, const vector2_t *hw)
{
size_t dx, dy;
color_t *p;
size_t xmin = 0;
size_t xmax = hw->x;
size_t ymin = 0;
size_t ymax = hw->y;
if (pos->x + xmax > device->width)
xmax = device->width - pos->x;
if (pos->x < 0)
xmin = -pos->x;
if (pos->y + ymax > device->height)
ymax = device->height - pos->y;
if (pos->y < 0)
ymin = -pos->y;
for (dx = xmin; dx < xmax; dx++) {
for (dy = ymin; dy < ymax; dy++) {
p = get_pixel(device, pos->x + dx, pos->y + dy);
p->b = color->b;
p->g = color->g;
p->r = color->r;
p->a = color->a;
}
}
}
static void graphics_draw_image_fast(const graphics_t *device,
const unsigned char *image, size_t imglen)
{
size_t copysize;
copysize = get_buf_size(device);
if (imglen < copysize)
copysize = imglen;
dma_copy(device->mem, image + 8, copysize);
}
void graphics_draw_image(const graphics_t *device, const vector2_t *pos,
const unsigned char *image, const color_t *tint)
{
size_t width, height;
size_t x, y;
size_t imgpos;
color_t *p;
const color_t *src;
memcpy(&height, image, 4);
memcpy(&width, image + 4, 4);
if ((int)pos->x == 0 && (int)pos->y == 0 && width == device->width) {
graphics_draw_image_fast(device, image, height * width * 4);
return;
}
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
p = get_pixel(device, pos->x + x, pos->y + y);
imgpos = 8 + (y * width * (device->bpp >> 3)) +
(x * (device->bpp >> 3));
if (device->bpp == 32 && image[imgpos + 3] == 0)
continue;
if (!tint || device->bpp != 32)
src = (void *)(&image[imgpos]);
else
src = tint;
p->b = src->b;
p->g = src->g;
p->r = src->r;
if (device->bpp == 32)
p->a = src->a;
}
}
}
void graphics_draw_line(const graphics_t *device,
const cvertex_t *v1, const cvertex_t *v2)
{
color_t color;
color_t *pixel;
float x, y;
float dx, dy;
float xinc, yinc;
int i, steps;
dx = v2->pos.x - v1->pos.x;
dy = v2->pos.y - v1->pos.y;
if (fabsf(dx) > fabsf(dy))
steps = (int)fabsf(dx);
else
steps = (int)fabsf(dy);
xinc = dx / steps;
yinc = dy / steps;
x = v1->pos.x;
y = v1->pos.y;
for (i = 0; i < steps; i++, x += xinc, y += yinc) {
color_interpolate(&color, &v1->color, &v2->color,
(float)i / steps);
pixel = get_pixel(device, (size_t)x, (size_t)y);
pixel->r = color.r;
pixel->g = color.g;
pixel->b = color.b;
if (device->bpp == 32)
pixel->a = color.a;
}
}
void graphics_redraw(const graphics_t *device)
{
dma_copy(device->mem, device->fb, get_buf_size(device));
}
void graphics_flush(graphics_t *device)
{
uint8_t *tmp;
RPI_PropertyInit();
if (device->fb < device->mem)
RPI_PropertyAddTag(TAG_SET_VIRTUAL_OFFSET, 0, device->height);
else
RPI_PropertyAddTag(TAG_SET_VIRTUAL_OFFSET, 0, 0);
RPI_PropertyProcess();
tmp = device->fb;
device->fb = device->mem;
device->mem = tmp;
}