-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhaloo3d.h
More file actions
489 lines (444 loc) · 24.9 KB
/
haloo3d.h
File metadata and controls
489 lines (444 loc) · 24.9 KB
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#ifndef __HALOO3D_HEADER
#define __HALOO3D_HEADER
#include <stdint.h>
typedef float hfloat_t;
// Some systems get caught up on float literals for some reason...?
#ifdef H3D_VOLATILE_FLOATS
#define H3DVF(x) (volatile hfloat_t)(x)
#else
#define H3DVF(x) (x)
#endif
// All vector types and such are simply arrays of floats.
// These are indexes into the arrays for the various values
#define H3DX 0
#define H3DY 1
#define H3DZ 2
#define H3DW 3
// We NEED to put a limit on max interpolants so we don't need to malloc
// inside the triangle function! the triangle function tracks interpolants
// per "side", and thus may need more than one copy
#define H3D_MAXINTERPOLANTS 8
// ================================================
// | MATH |
// ================================================
typedef hfloat_t vec2[2];
typedef int32_t vec2i[2];
typedef hfloat_t vec3[3];
typedef int32_t vec3i[3];
typedef hfloat_t vec4[4];
typedef hfloat_t mat4[16];
// Define a rectangle with INCLUSIVE start EXCLUSIVE endpoint
typedef struct {
int x1;
int y1;
int x2;
int y2;
} h3d_recti;
#define VEC2(v, x, y) \
v[H3DX] = x; \
v[H3DY] = y;
#define VEC3(v, x, y, z) \
v[H3DX] = x; \
v[H3DY] = y; \
v[H3DZ] = z;
#define VEC4(v, x, y, z, w) \
v[H3DX] = x; \
v[H3DY] = y; \
v[H3DZ] = z; \
v[H3DW] = w;
#define H3D_CLAMP(v, min, max) (((v) < min) ? min : ((v) > max) ? max : (v))
#define H3D_IS2POW(x) (!((x) & ((x) - 1)) && (x))
#define H3D_MIN(a, b) (((a) < (b)) ? (a) : (b))
#define H3D_MAX(a, b) (((a) > (b)) ? (a) : (b))
//#define H3D_MIN3(a, b, c) (((a) < (b)) ? (((a) < (c)) ? (a) : (c)) : (b))
//#define H3D_MAX3(a, b, c) (((a) > (b)) ? (((a) > (c)) ? (a) : (c)) : (b))
// Rotate left (byte)
#define H3D_ROL8(v) ((v << 1) | (v >> 7))
// Rotate right (byte)
#define H3D_ROR8(v) ((v >> 1) | (v << 7))
// Rotate left by n (byte)
#define H3D_ROL8N(v, n) ((v << (n & 0x7)) | (v >> (8 - (n & 0x7))))
// Rotate right by n (byte)
#define H3D_ROR8N(v, n) ((v >> (n & 0x7)) | (v << (8 - (n & 0x7))))
// Edge function for a point being on one side or another of a line given by
// v0 and v1. We use counter-clockwise winding. Technically returns 2 * area
// of triangle created by v0, v1, and p.
#define H3D_EDGEFUNC(v0, v1, p) \
(p[H3DX] - v0[H3DX]) * (v1[H3DY] - v0[H3DY]) - \
(p[H3DY] - v0[H3DY]) * (v1[H3DX] - v0[H3DX])
// return horizontal difference per pixel for any generic value in vertex
#define H3D_HLERP(v0, v1, v2, t0, t1, t2) \
(((t0) - (t2)) * (v1->pos[H3DY] - v2->pos[H3DY]) - \
((t1) - (t2)) * (v0->pos[H3DY] - v2->pos[H3DY])) / \
((v0->pos[H3DX] - v2->pos[H3DX]) * (v1->pos[H3DY] - v2->pos[H3DY]) - \
(v1->pos[H3DX] - v2->pos[H3DX]) * (v0->pos[H3DY] - v2->pos[H3DY]))
// ================================================
// | RASTER |
// ================================================
// A single vertex for rasterization. Vertices are 2d and only used for
// rasterization
typedef struct {
int16_t pos[2];
hfloat_t interpolants[H3D_MAXINTERPOLANTS];
} h3d_rastervert;
typedef h3d_rastervert h3d_rasterface[3];
// Represents tracking information for one side of a triangle.
// It may not use or calculate all fields; the left sides require
// the interpolation and the right side only needs x. It uses a
// stack of vectors setup by determining the "direction" of the
// scanline; the "stack" is popped when a vector section is complete
typedef struct {
h3d_rastervert *stack[3]; // Vertex order
int top;
int sectionheight; // Tracking for how much is left in the current section
hfloat_t x, x_dy; // Tracking for specifically the x coordinate.
hfloat_t interpolants[H3D_MAXINTERPOLANTS]; // Tracking variables
hfloat_t interpolant_dy[H3D_MAXINTERPOLANTS]; // Y Delta along current edge
uint8_t num_interpolants;
} _h3dtriside;
// Initialize h3dtriside struct for tracking variables along edge of tri
static inline void _h3dtriside_init(_h3dtriside *s, uint8_t num_interpolants) {
s->top = 0;
s->sectionheight = 0;
s->num_interpolants = num_interpolants;
}
// Push a raster vector onto the vector stack. Remember it is a stack,
// so the ones you want on top should go last
static inline int _h3dtriside_push(_h3dtriside *s, h3d_rastervert *v) {
s->stack[s->top] = v;
return ++s->top;
}
// Pop a vector, returning the new top
static inline int _h3dtriside_pop(_h3dtriside *s) { return --s->top; }
// Calculate all deltas and return the height of this section.
static inline int _h3dtriside_start(_h3dtriside *s) {
const h3d_rastervert *const v1 = s->stack[s->top - 1];
const h3d_rastervert *const v2 = s->stack[s->top - 2];
const int height = v2->pos[H3DY] - v1->pos[H3DY];
if (height == 0) {
return 0;
}
const hfloat_t invheight = H3DVF(1.0) / height;
s->x_dy = (v2->pos[H3DX] - v1->pos[H3DX]) * invheight;
s->x = v1->pos[H3DX];
// eprintf("X and x_dy: %f %f\n", s->x, s->x_dy);
// Copy interpolants from v1, the top of the stack (and current line)
for (int i = 0; i < s->num_interpolants; i++) {
s->interpolants[i] = v1->interpolants[i];
// Your interpolants need to be linear across the triangle, otherwise
// this doesn't work!
s->interpolant_dy[i] =
(v2->interpolants[i] - v1->interpolants[i]) * invheight;
}
s->sectionheight = height;
return height;
}
// Move to the next line along the side. Returns 1 if the
// entire side is done
static inline int _h3dtriside_next(_h3dtriside *s) {
// At bottom of current section
if (--s->sectionheight <= 0) {
// There needs to be at least two vertices to work
if (_h3dtriside_pop(s) < 2) {
return 1;
}
// The next section has no height. Note that if this succeeds,
// this begins the next section, and thus no recalcs are needed
if (_h3dtriside_start(s) <= 0) {
return 1;
}
} else {
s->x += s->x_dy;
for (int i = 0; i < s->num_interpolants; i++) {
s->interpolants[i] += s->interpolant_dy[i];
}
}
return 0;
}
// TODO: remember to invert v for linear interpolation?
// TODO: remember to make textures wrap to prevent buffer overflow
// TODO: dithering, scaled intensity per tri, etc.
// Clamp the values. This may be wasteful but it's safer...
// - rv = rasterize vertex
// - bw = buffer width
// - bh = buffer height
// NOTE: because the triangle scanline right value is exclusive, there's no need
// to subtract 1 from the width and height, let it go to the edge.
#define H3DTRI_CLAMP(rv, bw, bh) \
for (int _i = 0; _i < 3; _i++) { \
rv[_i].pos[H3DX] = H3D_CLAMP(rv[_i].pos[H3DX], H3DVF(0), bw); \
rv[_i].pos[H3DY] = H3D_CLAMP(rv[_i].pos[H3DY], H3DVF(0), bh); \
}
// Helper function for initializing triangle functions. This enables
// shader-like features.
// - rv = initial raster vector,
// - sv = how to name the sorted vector pointer array,
// - parea = how to name variable for calculating the edge-function 2xarea
// of the tri (useful for backface culling)
#define H3DTRI_BEGIN(rv, sv, parea) \
h3d_rastervert *sv[3]; \
for (int _i = 0; _i < 3; _i++) { \
sv[_i] = &rv[_i]; \
} \
/* Make sure vertices are sorted top to bottom */ \
if (sv[0]->pos[H3DY] > sv[1]->pos[H3DY]) { \
h3d_rastervert *tmp = sv[0]; \
sv[0] = sv[1]; \
sv[1] = tmp; \
} \
if (sv[1]->pos[H3DY] > sv[2]->pos[H3DY]) { \
h3d_rastervert *tmp = sv[1]; \
sv[1] = sv[2]; \
sv[2] = tmp; \
} \
if (sv[0]->pos[H3DY] > sv[1]->pos[H3DY]) { \
h3d_rastervert *tmp = sv[0]; \
sv[0] = sv[1]; \
sv[1] = tmp; \
} \
int32_t parea = H3D_EDGEFUNC(sv[0]->pos, sv[1]->pos, sv[2]->pos);
// Helper macro for beginning the triangle loop. After this, you can write
// code meant to be run "per row" of the triangle. perhaps setup for the
// scanline?
// - sv = sorted vertex pointer array
// - parea = parea calculated in H3DTRI_BEGIN
// - linpol = name of the linear array (you will use this in shader)
// - numlinpol = number of linear values
// - bw = buffer width
// - bh = buffer height
// - bufi = name of buffer index variable (you will use this in shder)
#define H3DTRI_SCAN_BEGIN(sv, parea, linpol, numlinpol, bw, bh, bufi) \
/* Tracking info for left and right side. Only tracks vertical strides */ \
_h3dtriside _right, _left; \
_h3dtriside_init(&_right, numlinpol); \
_h3dtriside_init(&_left, numlinpol); \
{ /* Scoped pointers */ \
_h3dtriside *onesec, *twosec; \
if (parea == 0) { \
return; \
} else if (parea < 0) { \
/* The middle point is on the right side, because it's wound clockwise*/ \
parea = -parea; \
onesec = &_left; \
twosec = &_right; \
} else { \
/* The middle point is on the left side, as expected for our winding */ \
onesec = &_right; \
twosec = &_left; \
} \
_h3dtriside_push(twosec, sv[2]); \
_h3dtriside_push(twosec, sv[1]); \
_h3dtriside_push(twosec, sv[0]); \
_h3dtriside_push(onesec, sv[2]); \
_h3dtriside_push(onesec, sv[0]); \
/* Calculate the deltas. If the "onesection" side has no height, we die */ \
if (_h3dtriside_start(onesec) <= 0) { \
return; \
} \
if (_h3dtriside_start(twosec) <= 0) { \
/* The "twosection" side has another section. */ \
_h3dtriside_pop(twosec); \
if (_h3dtriside_start(twosec) <= 0) { \
return; \
} \
} \
} \
/* need to calc all the constant horizontal diffs. */ \
hfloat_t _dx[H3D_MAXINTERPOLANTS]; \
hfloat_t linpol[H3D_MAXINTERPOLANTS]; \
/* If linear interpolation is not used, the compiler will warn */ \
(void)linpol; \
for (int _i = 0; _i < numlinpol; _i++) { \
_dx[_i] = H3D_HLERP(sv[0], sv[1], sv[2], sv[0]->interpolants[_i], \
sv[1]->interpolants[_i], sv[2]->interpolants[_i]); \
} \
uint16_t _y = sv[0]->pos[H3DY]; \
uint32_t _bufstart = (uint32_t)_y * bw; \
while (1) { \
/* Supposed to use ceiling but idk, mine works with floor... kinda. */ \
/* I have holes but with ceil I get actual seams. */ \
uint16_t _xl = _left.x; \
uint16_t _xr = _right.x; \
if (_xl < _xr) { \
hfloat_t xofs = _xl - _left.x; \
/* Setup interpolants for inner loop, shifting by appropriate amount*/ \
for (int _i = 0; _i < numlinpol; _i++) { \
linpol[_i] = _left.interpolants[_i] + xofs * _dx[_i]; \
}
// The final inner loop, run once per pixel in the triangle. Follow this with
// brackets indicating the contents of a loop.
#define H3DTRI_SHADER(bufi) \
for (uint32_t bufi = _bufstart + _xl; bufi < _bufstart + _xr; bufi++)
// A generic linear interpolants update you may need to call in your inner loop
// (shader) to update linear values. You should ALWAYS prefer the static count
// macros such as H3DTRI_LINPOL3, as they should be faster.
// clang-format off
#define H3DTRI_LINPOL(linpol, n) \
switch(H3D_MAXINTERPOLANTS - n) { \
case 0: linpol[7] += _dx[7]; \
/* fall through */ \
case 1: linpol[6] += _dx[6]; \
/* fall through */ \
case 2: linpol[5] += _dx[5]; \
/* fall through */ \
case 3: linpol[4] += _dx[4]; \
/* fall through */ \
case 4: linpol[3] += _dx[3]; \
/* fall through */ \
case 5: linpol[2] += _dx[2]; \
/* fall through */ \
case 6: linpol[1] += _dx[1]; \
/* fall through */ \
case 7: linpol[0] += _dx[0]; \
}
// clang-format on
// Optimized macros you need to call in your inner loop (shader) to update
// the linear values. Spreading them out like this makes it easier for
// compilers to auto-vectorize (just in case your version of gcc
// or whatever is bad at auto-vectorizing loops). Plus, for compilers that
// DON'T auto-vectorize, not looping is still faster.
#define H3DTRI_LINPOL1(linpol) linpol[0] += _dx[0];
#define H3DTRI_LINPOL2(linpol) \
linpol[0] += _dx[0]; \
linpol[1] += _dx[1];
#define H3DTRI_LINPOL3(linpol) \
linpol[0] += _dx[0]; \
linpol[1] += _dx[1]; \
linpol[2] += _dx[2];
#define H3DTRI_LINPOL4(linpol) \
linpol[0] += _dx[0]; \
linpol[1] += _dx[1]; \
linpol[2] += _dx[2]; \
linpol[3] += _dx[3];
#define H3DTRI_LINPOL5(linpol) \
linpol[0] += _dx[0]; \
linpol[1] += _dx[1]; \
linpol[2] += _dx[2]; \
linpol[3] += _dx[3]; \
linpol[4] += _dx[4];
#define H3DTRI_LINPOL6(linpol) \
linpol[0] += _dx[0]; \
linpol[1] += _dx[1]; \
linpol[2] += _dx[2]; \
linpol[3] += _dx[3]; \
linpol[4] += _dx[4]; \
linpol[5] += _dx[5];
#define H3DTRI_LINPOL7(linpol) \
linpol[0] += _dx[0]; \
linpol[1] += _dx[1]; \
linpol[2] += _dx[2]; \
linpol[3] += _dx[3]; \
linpol[4] += _dx[4]; \
linpol[5] += _dx[5]; \
linpol[6] += _dx[6];
#define H3DTRI_LINPOL8(linpol) \
linpol[0] += _dx[0]; \
linpol[1] += _dx[1]; \
linpol[2] += _dx[2]; \
linpol[3] += _dx[3]; \
linpol[4] += _dx[4]; \
linpol[5] += _dx[5]; \
linpol[6] += _dx[6]; \
linpol[7] += _dx[7];
// end the loop created by H3D_SCAN_BEGIN
#define H3DTRI_SCAN_END(bw) \
} \
_y++; \
_bufstart += bw; \
if (_h3dtriside_next(&_left)) { \
return; \
} \
if (_h3dtriside_next(&_right)) { \
return; \
} \
}
// If your needs are small, this wrapper macro makes shader setup slightly
// easier by choosing some default names and assuming you need clamping and
// don't need to use parea for anything.
#define H3DTRI_EASY_BEGIN(rv, bw, bh, linpol, numlinpol, bufi) \
H3DTRI_CLAMP(rv, bw, bh); \
H3DTRI_BEGIN(rv, sv, parea); \
H3DTRI_SCAN_BEGIN(sv, parea, linpol, numlinpol, bw, bh, bufi) \
H3DTRI_SHADER(bufi)
// ========================================
// | FRAMEBUFFER |
// ========================================
// The framebuffer object, which stores stuff like the 16 bit
// framebuffer, the depth buffer, etc. Framebuffers are so simple,
// they might as well be included in the main library
typedef struct {
uint16_t *buffer; // actual buffer (managed manually)
hfloat_t *dbuffer; // Depth buffer, probably using w value instead of z
uint16_t width; // width of the framebuffer
uint16_t height; // height of the framebuffer
} h3d_fb;
typedef struct {
uint32_t *buffer; // actual buffer (managed manually)
hfloat_t *dbuffer; // Depth buffer, probably using w value instead of z
uint16_t width; // width of the framebuffer
uint16_t height; // height of the framebuffer
} h3d_fb32;
typedef struct {
uint8_t *buffer; // actual buffer (managed manually)
hfloat_t *dbuffer; // Depth buffer, probably using w value instead of z
uint16_t width; // width of the framebuffer
uint16_t height; // height of the framebuffer
} h3d_fb8;
#define H3D_FB_GET(fb, x, y) ((fb)->buffer[(x) + (y) * (fb)->width])
#define H3D_FB_DGET(fb, x, y) ((fb)->dbuffer[(x) + (y) * (fb)->width])
#define H3D_FB_SET(fb, x, y, v) (fb)->buffer[(x) + (y) * (fb)->width] = (v)
#define H3D_FB_DSET(fb, x, y, v) (fb)->dbuffer[x + (y) * (fb)->width] = (v)
#define H3D_FB_GETUV(fb, u, v) \
((fb)->buffer[((uint16_t)((fb)->width * u) & ((fb)->width - 1)) + \
((uint16_t)((fb)->height * (H3DVF(1) - v)) & \
((fb)->height - 1)) * \
(fb)->width])
#define H3D_FB_SIZE(fb) ((fb)->width * (fb)->height)
// Convert given color to full transparency in whole fb
#define H3D_FB_TOTRANSPARENT(fb, col) \
{ \
const int size = H3D_FB_SIZE(fb); \
for (int _i = 0; _i < size; _i++) { \
if ((fb)->buffer[_i] == col) \
(fb)->buffer[_i] = 0; \
} \
}
// Fill buffer with col
#define H3D_FB_FILL(fb, col) \
{ \
const int size = H3D_FB_SIZE(fb); \
for (int _i = 0; _i < size; _i++) { \
(fb)->buffer[_i] = col; \
} \
}
#define H3D_FB_DFILL(fb, val) \
{ \
const int size = H3D_FB_SIZE(fb); \
for (int _i = 0; _i < size; _i++) { \
(fb)->dbuffer[_i] = val; \
} \
}
// ========================================
// | COLOR |
// ========================================
// Color is another thing that's not a big deal to include imo
#define H3DC_A4(c) (((c) >> 12) & 0xF)
#define H3DC_R4(c) (((c) >> 8) & 0xF)
#define H3DC_G4(c) (((c) >> 4) & 0xF)
#define H3DC_B4(c) ((c) & 0xF)
#define H3DC_A4R4G4B4(a, r, g, b) \
((((a) & 0xF) << 12) | (((r) & 0xF) << 8) | (((g) & 0xF) << 4) | ((b) & 0xF))
#define H3DC_A4R4G4B4_F(a, r, g, b) \
(((uint16_t)((a) * 15 + 0.5) << 12) | ((uint16_t)((r) * 15 + 0.5) << 8) | \
((uint16_t)((g) * 15 + 0.5) << 4) | (uint16_t)((b) * 15 + 0.5))
#define H3DC_A1(c) (((c) >> 15) & 0x1)
#define H3DC_R5(c) (((c) >> 10) & 0x1F)
#define H3DC_G5(c) (((c) >> 5) & 0x1F)
#define H3DC_B5(c) ((c) & 0x1F)
#define H3DC_A1R5G5B5(a, r, g, b) \
((((a) & 0x1) << 15) | (((r) & 0x1F) << 10) | (((g) & 0x1F) << 5) | \
((b) & 0x1F))
#define H3DC_A1R5G5B5_F(a, r, g, b) \
((((a) > 0 ? 1 : 0) << 15) | ((uint16_t)((r) * 31 + 0.5) << 10) | \
((uint16_t)((g) * 31 + 0.5) << 5) | (uint16_t)((b) * 31 + 0.5))
#endif