software rendering
[retroray] / src / gaw / polyfill.h
1 /*
2 RetroRay - integrated standalone vintage modeller/renderer
3 Copyright (C) 2023  John Tsiombikas <nuclear@mutantstargoat.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #ifndef POLYFILL_H_
19 #define POLYFILL_H_
20
21 #include "../util.h"
22 #include "gaw.h"
23
24 #define POLYFILL_MODE_MASK      0x03
25 #define POLYFILL_TEX_BIT        0x04
26 #define POLYFILL_ALPHA_BIT      0x08
27 #define POLYFILL_ADD_BIT        0x10
28 #define POLYFILL_ZBUF_BIT       0x20
29
30 enum {
31         POLYFILL_WIRE                   = 0,
32         POLYFILL_FLAT,
33         POLYFILL_GOURAUD,
34
35         POLYFILL_TEX_WIRE               = 4,
36         POLYFILL_TEX_FLAT,
37         POLYFILL_TEX_GOURAUD,
38
39         POLYFILL_ALPHA_WIRE             = 8,
40         POLYFILL_ALPHA_FLAT,
41         POLYFILL_ALPHA_GOURAUD,
42
43         POLYFILL_ALPHA_TEX_WIRE = 12,
44         POLYFILL_ALPHA_TEX_FLAT,
45         POLYFILL_ALPHA_TEX_GOURAUD,
46
47         POLYFILL_ADD_WIRE               = 16,
48         POLYFILL_ADD_FLAT,
49         POLYFILL_ADD_GOURAUD,
50
51         POLYFILL_ADD_TEX_WIRE   = 20,
52         POLYFILL_ADD_TEX_FLAT,
53         POLYFILL_ADD_TEX_GOURAUD,
54
55
56         POLYFILL_WIRE_ZBUF                      = 32,
57         POLYFILL_FLAT_ZBUF,
58         POLYFILL_GOURAUD_ZBUF,
59
60         POLYFILL_TEX_WIRE_ZBUF          = 36,
61         POLYFILL_TEX_FLAT_ZBUF,
62         POLYFILL_TEX_GOURAUD_ZBUF,
63
64         POLYFILL_ALPHA_WIRE_ZBUF        = 40,
65         POLYFILL_ALPHA_FLAT_ZBUF,
66         POLYFILL_ALPHA_GOURAUD_ZBUF,
67
68         POLYFILL_ALPHA_TEX_WIRE_ZBUF = 44,
69         POLYFILL_ALPHA_TEX_FLAT_ZBUF,
70         POLYFILL_ALPHA_TEX_GOURAUD_ZBUF,
71
72         POLYFILL_ADD_WIRE_ZBUF          = 48,
73         POLYFILL_ADD_FLAT_ZBUF,
74         POLYFILL_ADD_GOURAUD_ZBUF,
75
76         POLYFILL_ADD_TEX_WIRE_ZBUF      = 52,
77         POLYFILL_ADD_TEX_FLAT_ZBUF,
78         POLYFILL_ADD_TEX_GOURAUD_ZBUF
79 };
80
81 typedef uint32_t gaw_pixel;
82
83 #define PACK_RGB(r, g, b) \
84         (0xff000000 | ((r) << 16) | ((g) << 8) | (b))
85 #define PACK_RGBA(r, g, b, a) \
86         (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
87 #define UNPACK_R(pix)   ((pix) & 0xff)
88 #define UNPACK_G(pix)   (((pix) >> 8) & 0xff)
89 #define UNPACK_B(pix)   (((pix) >> 16) & 0xff)
90 #define UNPACK_A(pix)   ((pix) >> 24)
91
92 struct vertex {
93         float x, y, z, w;
94         float nx, ny, nz;
95         float u, v;
96         int r, g, b, a;
97 };
98
99 /* projected vertices for the rasterizer */
100 struct pvertex {
101         int32_t x, y; /* 24.8 fixed point */
102         int32_t u, v; /* 16.16 fixed point */
103         int32_t r, g, b, a;  /* int 0-255 */
104         int32_t z;      /* 0-65535 */
105 };
106
107 struct pimage {
108         gaw_pixel *pixels;
109         int width, height;
110
111         int xshift, yshift;
112         unsigned int xmask, ymask;
113 };
114
115 extern struct pimage pfill_fb;
116 extern struct pimage pfill_tex;
117 extern uint32_t *pfill_zbuf;
118
119 void polyfill_fbheight(int height);
120
121 void polyfill(int mode, struct pvertex *verts, int nverts);
122
123 void polyfill_wire(struct pvertex *verts, int nverts);
124 void polyfill_flat(struct pvertex *verts, int nverts);
125 void polyfill_gouraud(struct pvertex *verts, int nverts);
126 void polyfill_tex_wire(struct pvertex *verts, int nverts);
127 void polyfill_tex_flat(struct pvertex *verts, int nverts);
128 void polyfill_tex_gouraud(struct pvertex *verts, int nverts);
129 void polyfill_alpha_wire(struct pvertex *verts, int nverts);
130 void polyfill_alpha_flat(struct pvertex *verts, int nverts);
131 void polyfill_alpha_gouraud(struct pvertex *verts, int nverts);
132 void polyfill_alpha_tex_wire(struct pvertex *verts, int nverts);
133 void polyfill_alpha_tex_flat(struct pvertex *verts, int nverts);
134 void polyfill_alpha_tex_gouraud(struct pvertex *verts, int nverts);
135 void polyfill_add_wire(struct pvertex *verts, int nverts);
136 void polyfill_add_flat(struct pvertex *verts, int nverts);
137 void polyfill_add_gouraud(struct pvertex *verts, int nverts);
138 void polyfill_add_tex_wire(struct pvertex *verts, int nverts);
139 void polyfill_add_tex_flat(struct pvertex *verts, int nverts);
140 void polyfill_add_tex_gouraud(struct pvertex *verts, int nverts);
141 void polyfill_flat_zbuf(struct pvertex *verts, int nverts);
142 void polyfill_gouraud_zbuf(struct pvertex *verts, int nverts);
143 void polyfill_tex_flat_zbuf(struct pvertex *verts, int nverts);
144 void polyfill_tex_gouraud_zbuf(struct pvertex *verts, int nverts);
145 void polyfill_alpha_flat_zbuf(struct pvertex *verts, int nverts);
146 void polyfill_alpha_gouraud_zbuf(struct pvertex *verts, int nverts);
147 void polyfill_alpha_tex_flat_zbuf(struct pvertex *verts, int nverts);
148 void polyfill_alpha_tex_gouraud_zbuf(struct pvertex *verts, int nverts);
149 void polyfill_add_flat_zbuf(struct pvertex *verts, int nverts);
150 void polyfill_add_gouraud_zbuf(struct pvertex *verts, int nverts);
151 void polyfill_add_tex_flat_zbuf(struct pvertex *verts, int nverts);
152 void polyfill_add_tex_gouraud_zbuf(struct pvertex *verts, int nverts);
153
154 void draw_line(struct pvertex *verts);
155
156 #endif  /* POLYFILL_H_ */