-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSWframebuffer.c
137 lines (119 loc) · 4.74 KB
/
SWframebuffer.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
#include "SWframebuffer.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "SWtexture.h"
#include "SWzbuffer.h"
void swFbufInit(SWframebuffer *f, const SWenum type, const SWint w, const SWint h,
const SWint with_depth) {
f->type = type;
f->w = w;
f->h = h;
f->zbuf = NULL;
SWuint num_bytes = 0;
if (type == SW_BGRA8888) {
num_bytes = (SWuint)w * h * 4;
} else if (type == SW_FRGBA) {
num_bytes = (SWuint)w * h * 4 * sizeof(SWfloat);
}
f->pixels = calloc(num_bytes, 1);
if (with_depth) {
f->zbuf = (SWzbuffer *)malloc(sizeof(SWzbuffer));
swZbufInit(f->zbuf, w, h, 1.0f);
swFbufClearDepth(f, (SWfloat)1);
}
}
void swFbufDestroy(SWframebuffer *f) {
free(f->pixels);
if (f->zbuf) {
swZbufDestroy(f->zbuf);
free(f->zbuf);
}
memset(f, 0, sizeof(SWframebuffer));
}
void swFbufClearColor_RGBA(SWframebuffer *f, SWubyte *rgba) {
SWint y, span_size = 0;
if (f->type == SW_BGRA8888) {
SWint x;
for (x = 0; x < f->w; x++) {
swPx_BGRA8888_SetColor_RGBA8888(f->w, f->h, f->pixels, x, 0, rgba);
}
span_size = f->w * 4;
}
for (y = 1; y < f->h; y++) {
memcpy(((char *)f->pixels) + y * span_size, f->pixels, (size_t)span_size);
}
}
void swFbufClearColorFloat(SWframebuffer *f, const SWfloat r, const SWfloat g,
const SWfloat b, const SWfloat a) {
if (f->type == SW_BGRA8888) {
SWubyte rgba[4];
_swPx_RGBA8888_SetColor_FRGBA_(rgba, r, g, b, a);
swFbufClearColor_RGBA(f, rgba);
} else if (f->type == SW_FRGBA) {
SWfloat rgba[4] = {r, g, b, a};
SWint x, y, span_size = f->w * 4 * sizeof(SWfloat);
for (x = 0; x < f->w; x++) {
swPx_FRGBA_SetColor_FRGBA(f->w, f->h, f->pixels, x, 0, rgba);
}
for (y = 1; y < f->h; y++) {
memcpy(((char *)f->pixels) + y * span_size, f->pixels, (size_t)span_size);
}
}
}
void swFbufBlitPixels(SWframebuffer *f, const SWint x, const SWint y, SWint pitch,
const SWenum type, const SWenum mode, const SWint w, const SWint h,
const void *pixels, const SWfloat scale) {
const SWint beg_x = sw_max(x, 0), beg_y = sw_max(y, 0),
end_x = sw_min(f->w, (SWint)(x + scale * w)),
end_y = sw_min(f->h, (SWint)(y + scale * h));
if (!pitch) {
pitch = w;
}
const SWfloat u_step = (SWfloat)1.0 / (w * scale),
v_step = (SWfloat)1.0 / (h * scale);
SWint i, j;
if (type == SW_UNSIGNED_BYTE) {
SWfloat v = 0;
#define LOOP(__fun__) \
for (j = beg_y; j < end_y; j++) { \
SWfloat u = 0; \
for (i = beg_x; i < end_x; i++) { \
SWubyte *p = (SWubyte *)f->pixels + (j * f->w + i) * 4; \
__fun__(w, h, pixels, u, v, p); \
u += u_step; \
} \
v += v_step; \
}
if (mode == SW_RGB) {
if (f->type == SW_BGRA8888) {
LOOP(swPx_RGB888_GetColor_BGRA8888_UV_norepeat_unsafe)
}
} else if (mode == SW_RGBA) {
if (f->type == SW_BGRA8888) {
LOOP(swPx_RGBA8888_GetColor_BGRA8888_UV_norepeat_unsafe)
}
}
#undef LOOP
} else if (type == SW_FLOAT) {
assert(scale == 1.0f);
const SWfloat *fp = (SWfloat *)pixels;
if (mode == SW_FRGBA) {
if (f->type == SW_BGRA8888) {
for (j = beg_y; j < end_y; j++) {
for (i = beg_x; i < end_x; i++) {
swPx_BGRA8888_SetColor_FRGBA(f->w, f->h, (f->pixels), i, j,
&fp[(i - beg_x) * 4]);
}
fp += pitch * 4;
}
}
}
} else if (type == SW_COMPRESSED) {
assert(0);
}
}
void swFbufBlitTexture(SWframebuffer *f, const SWint x, const SWint y, const SWtexture *t,
const SWfloat scale) {
swFbufBlitPixels(f, x, y, 0, t->type, t->mode, t->w, t->h, t->pixels, scale);
}