20#ifndef _TGX_SHADERS_H_
21#define _TGX_SHADERS_H_
36 return ((v < 0) ? 0 : ((v > maxv) ? maxv : v));
44 template<
typename color_t,
typename ZBUFFER_t>
45 void shader_test(
const int32_t oox,
const int32_t ooy,
const int32_t lx,
const int32_t ly,
46 const int32_t dx1,
const int32_t dy1, int32_t O1,
const tgx::RasterizerVec4& fP1,
47 const int32_t dx2,
const int32_t dy2, int32_t O2,
const tgx::RasterizerVec4& fP2,
48 const int32_t dx3,
const int32_t dy3, int32_t O3,
const tgx::RasterizerVec4& fP3,
49 const tgx::RasterizerParams<color_t, color_t, ZBUFFER_t>& data)
52 const int32_t stride = data.im->stride();
53 color_t* buf = data.im->data() + oox + (ooy * stride);
55 for (
int y = 0; y < ly; y++)
57 for (
int x = 0; x < lx; x++)
59 const int32_t o1 = O1 + dx1 * x + dy1 * y;
60 const int32_t o2 = O2 + dx2 * x + dy2 * y;
61 const int32_t o3 = O3 + dx3 * x + dy3 * y;
62 if ((o1 >= 0) && (o2 >= 0) && (o3 >= 0))
64 buf[x + stride * y].
blend256(col, 128);
75 template<
typename color_t,
typename ZBUFFER_t,
76 bool USE_ZBUFFER,
bool USE_GOURAUD,
bool USE_TEXTURE,
77 bool USE_ORTHO,
bool TEXTURE_BILINEAR,
bool TEXTURE_WRAP>
78 void uber_shader(
const int32_t oox,
const int32_t ooy,
const int32_t lx,
const int32_t ly,
79 const int32_t dx1,
const int32_t dy1, int32_t O1,
const RasterizerVec4& fP1,
80 const int32_t dx2,
const int32_t dy2, int32_t O2,
const RasterizerVec4& fP2,
81 const int32_t dx3,
const int32_t dy3, int32_t O3,
const RasterizerVec4& fP3,
82 const RasterizerParams<color_t, color_t, ZBUFFER_t>& data)
85 const int32_t stride = data.im->stride();
86 color_t* buf = data.im->data() + oox + (ooy * stride);
88 const uintptr_t end = (uintptr_t)(buf + (ly * stride));
89 const int32_t pa = O1 + O2 + O3;
90 const int32_t E = ((pa == 0) ? 1 : 0);
91 const int32_t aera = pa + E;
94 ZBUFFER_t* zbuf =
nullptr;
96 float wa = 0.0f, wb = 0.0f;
97 float fP1a_z = 0.0f, fP2a_z = 0.0f, fP3a_z = 0.0f;
100 if constexpr (USE_ZBUFFER)
102 zstride = data.im->lx();
103 zbuf = data.zbuf + oox + (ooy * zstride);
107 const float invaera =
fast_inv((
float)aera);
108 float invaera_wa_factor = USE_ORTHO ? 1.0f : wa;
110 fP1a_z = fP1.w * invaera * invaera_wa_factor;
111 fP2a_z = fP2.w * invaera * invaera_wa_factor;
112 fP3a_z = fP3.w * invaera * invaera_wa_factor;
114 dw_z = (dx1 * fP1a_z) + (dx2 * fP2a_z) + (dx3 * fP3a_z);
119 color_t col1_g, col2_g, col3_g;
120 int shiftC = 0, aeraShifted = 0;
121 int fPR = 0, fPG = 0, fPB = 0;
122 int fP1R = 0, fP1G = 0, fP1B = 0;
123 int fP21R = 0, fP21G = 0, fP21B = 0;
124 int fP31R = 0, fP31G = 0, fP31B = 0;
126 float invaera_persp = 0.0f;
127 float fP1a_p = 0.0f, fP2a_p = 0.0f, fP3a_p = 0.0f;
130 const color_t* tex =
nullptr;
131 int32_t texsize_x_mm = 0, texsize_y_mm = 0, texstride = 0;
132 float dtx = 0.0f, dty = 0.0f;
135 if constexpr (USE_GOURAUD)
137 if constexpr (USE_TEXTURE)
142 fP1R = (
int)(256 * cf1.
R); fP1G = (int)(256 * cf1.
G); fP1B = (int)(256 * cf1.
B);
143 fP21R = (int)(256 * (cf2.R - cf1.
R)); fP21G = (int)(256 * (cf2.G - cf1.
G)); fP21B = (int)(256 * (cf2.B - cf1.
B));
144 fP31R = (int)(256 * (cf3.
R - cf1.
R)); fP31G = (int)(256 * (cf3.
G - cf1.
G)); fP31B = (int)(256 * (cf3.
B - cf1.
B));
148 col1_g = (color_t)fP1.color;
149 col2_g = (color_t)fP2.color;
150 col3_g = (color_t)fP3.color;
151 shiftC = (aera > (1 << 22)) ? 10 : 0;
152 aeraShifted = aera >> shiftC;
157 flat_color = (color_t)data.facecolor;
158 if constexpr (USE_TEXTURE)
160 const RGBf& cf = (
RGBf)data.facecolor;
161 fPR = (
int)(256 * cf.
R); fPG = (int)(256 * cf.
G); fPB = (int)(256 * cf.
B);
165 if constexpr (USE_TEXTURE)
167 tex = data.tex->data();
168 const int32_t texsize_x = data.tex->width();
169 const int32_t texsize_y = data.tex->height();
170 texsize_x_mm = texsize_x - 1;
171 texsize_y_mm = texsize_y - 1;
172 texstride = data.tex->stride();
174 T1 = fP1.T; T2 = fP2.T; T3 = fP3.T;
176 const float invaera =
fast_inv((
float)aera);
178 if constexpr (USE_ORTHO)
180 T1 *= invaera; T2 *= invaera; T3 *= invaera;
184 invaera_persp = invaera;
185 fP1a_p = fP1.w * invaera_persp;
186 fP2a_p = fP2.w * invaera_persp;
187 fP3a_p = fP3.w * invaera_persp;
188 dw_p = (dx1 * fP1a_p) + (dx2 * fP2a_p) + (dx3 * fP3a_p);
190 T1 *= fP1a_p; T2 *= fP2a_p; T3 *= fP3a_p;
193 T1.
x *= texsize_x; T2.
x *= texsize_x; T3.
x *= texsize_x;
194 T1.
y *= texsize_y; T2.
y *= texsize_y; T3.
y *= texsize_y;
196 dtx = ((T1.
x * dx1) + (T2.
x * dx2) + (T3.
x * dx3));
197 dty = ((T1.
y * dx1) + (T2.
y * dx2) + (T3.
y * dx3));
201 while ((uintptr_t)(buf) < end)
207 bx = (-O1 + dx1 - 1u) / dx1;
213 if (dy2 <= 0)
return;
214 const int32_t by = (-O2 + dy2 - 1u) / dy2;
215 O1 += (by * dy1); O2 += (by * dy2); O3 += (by * dy3);
217 if constexpr (USE_ZBUFFER) zbuf += by * zstride;
220 bx =
max(bx, (int32_t)((-O2 + dx2 - 1u) / dx2));
226 if (dy3 <= 0)
return;
227 const int32_t by = (-O3 + dy3 - 1u) / dy3;
228 O1 += (by * dy1); O2 += (by * dy2); O3 += (by * dy3);
230 if constexpr (USE_ZBUFFER) zbuf += by * zstride;
233 bx =
max(bx, (int32_t)((-O3 + dx3 - 1u) / dx3));
237 int32_t C1 = O1 + (dx1 * bx) + E;
238 int32_t C2 = O2 + (dx2 * bx);
239 int32_t C3 = O3 + (dx3 * bx);
242 if constexpr (USE_ZBUFFER)
244 cw_z = ((C1 * fP1a_z) + (C2 * fP2a_z) + (C3 * fP3a_z));
245 if constexpr (!USE_ORTHO)
252 float tx = 0.0f, ty = 0.0f;
253 if constexpr (USE_TEXTURE)
255 tx = ((T1.
x * C1) + (T2.
x * C2) + (T3.
x * C3));
256 ty = ((T1.
y * C1) + (T2.
y * C2) + (T3.
y * C3));
257 if constexpr (!USE_ORTHO)
259 cw_p = ((C1 * fP1a_p) + (C2 * fP2a_p) + (C3 * fP3a_p));
264 while ((bx < lx) && ((C2 | C3) >= 0))
267 if constexpr (USE_ZBUFFER)
269 ZBUFFER_t& W = zbuf[bx];
272 if constexpr (std::is_same<ZBUFFER_t, uint16_t>::value)
274 current_z = (USE_ORTHO) ? ((ZBUFFER_t)(cw_z * wa + wb)) : ((ZBUFFER_t)cw_z);
278 current_z = (ZBUFFER_t)cw_z;
295 if constexpr (USE_TEXTURE)
298 if constexpr (!USE_ORTHO)
306 if constexpr (TEXTURE_BILINEAR)
310 const float ax = xx - ttx;
311 const float ay = yy - tty;
313 const int minx = TEXTURE_WRAP ? (ttx & texsize_x_mm) :
shaderclip(ttx, texsize_x_mm);
314 const int maxx = TEXTURE_WRAP ? ((ttx + 1) & texsize_x_mm) :
shaderclip(ttx + 1, texsize_x_mm);
315 const int miny = (TEXTURE_WRAP ? (tty & texsize_y_mm) :
shaderclip(tty, texsize_y_mm)) * texstride;
316 const int maxy = (TEXTURE_WRAP ? ((tty + 1) & texsize_y_mm) :
shaderclip(tty + 1, texsize_y_mm)) * texstride;
322 const int ttx = TEXTURE_WRAP ? ((int)(xx)) & texsize_x_mm :
shaderclip((
int)(xx), texsize_x_mm);
323 const int tty = TEXTURE_WRAP ? ((int)(yy)) & texsize_y_mm :
shaderclip((
int)(yy), texsize_y_mm);
324 final_color = tex[ttx + tty * texstride];
327 if constexpr (USE_GOURAUD)
329 const int r = fP1R + ((C2 * fP21R + C3 * fP31R) / aera);
330 const int g = fP1G + ((C2 * fP21G + C3 * fP31G) / aera);
331 const int b = fP1B + ((C2 * fP21B + C3 * fP31B) / aera);
332 final_color.mult256(r, g, b);
336 final_color.mult256(fPR, fPG, fPB);
341 if constexpr (USE_GOURAUD)
347 final_color = flat_color;
350 buf[bx] = final_color;
358 if constexpr (USE_ZBUFFER) cw_z += dw_z;
360 if constexpr (USE_TEXTURE)
364 if constexpr (!USE_ORTHO) cw_p += dw_p;
373 if constexpr (USE_ZBUFFER) zbuf += zstride;
381 template<
int SHADER_FLAGS_ENABLED,
typename color_t,
typename ZBUFFER_t>
void shader_select(
const int32_t oox,
const int32_t ooy,
const int32_t lx,
const int32_t ly,
382 const int32_t dx1,
const int32_t dy1, int32_t O1,
const RasterizerVec4& fP1,
383 const int32_t dx2,
const int32_t dy2, int32_t O2,
const RasterizerVec4& fP2,
384 const int32_t dx3,
const int32_t dy3, int32_t O3,
const RasterizerVec4& fP3,
385 const RasterizerParams<color_t, color_t, ZBUFFER_t> & data)
387 int raster_type = data.shader_type;
388 if (TGX_SHADER_HAS_ZBUFFER(SHADER_FLAGS_ENABLED) && (TGX_SHADER_HAS_ZBUFFER(raster_type)))
390 if (TGX_SHADER_HAS_ORTHO(SHADER_FLAGS_ENABLED) && (TGX_SHADER_HAS_ORTHO(raster_type)))
392 if (TGX_SHADER_HAS_TEXTURE(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE(raster_type))
394 if (TGX_SHADER_HAS_GOURAUD(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_GOURAUD(raster_type))
396 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
398 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
399 uber_shader<color_t, ZBUFFER_t, true, true, true, true, true, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
400 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
401 uber_shader<color_t, ZBUFFER_t, true, true, true, true, true, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
403 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
405 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
406 uber_shader<color_t, ZBUFFER_t, true, true, true, true, false, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
407 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
408 uber_shader<color_t, ZBUFFER_t, true, true, true, true, false, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
411 else if (TGX_SHADER_HAS_FLAT(SHADER_FLAGS_ENABLED))
413 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
415 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
416 uber_shader<color_t, ZBUFFER_t, true, false, true, true, true, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
417 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
418 uber_shader<color_t, ZBUFFER_t, true, false, true, true, true, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
420 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
422 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
423 uber_shader<color_t, ZBUFFER_t, true, false, true, true, false, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
424 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
425 uber_shader<color_t, ZBUFFER_t, true, false, true, true, false, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
429 else if (TGX_SHADER_HAS_NOTEXTURE(SHADER_FLAGS_ENABLED))
431 if (TGX_SHADER_HAS_GOURAUD(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_GOURAUD(raster_type))
432 uber_shader<color_t, ZBUFFER_t, true, true, false, true, false, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
433 else if (TGX_SHADER_HAS_FLAT(SHADER_FLAGS_ENABLED))
434 uber_shader<color_t, ZBUFFER_t, true, false, false, true, false, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
437 else if (TGX_SHADER_HAS_PERSPECTIVE(SHADER_FLAGS_ENABLED))
439 if (TGX_SHADER_HAS_TEXTURE(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE(raster_type))
441 if (TGX_SHADER_HAS_GOURAUD(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_GOURAUD(raster_type))
443 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
445 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
446 uber_shader<color_t, ZBUFFER_t, true, true, true, false, true, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
447 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
448 uber_shader<color_t, ZBUFFER_t, true, true, true, false, true, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
450 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
452 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
453 uber_shader<color_t, ZBUFFER_t, true, true, true, false, false, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
454 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
455 uber_shader<color_t, ZBUFFER_t, true, true, true, false, false, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
458 else if (TGX_SHADER_HAS_FLAT(SHADER_FLAGS_ENABLED))
460 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
462 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
463 uber_shader<color_t, ZBUFFER_t, true, false, true, false, true, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
464 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
465 uber_shader<color_t, ZBUFFER_t, true, false, true, false, true, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
467 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
469 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
470 uber_shader<color_t, ZBUFFER_t, true, false, true, false, false, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
471 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
472 uber_shader<color_t, ZBUFFER_t, true, false, true, false, false, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
476 else if (TGX_SHADER_HAS_NOTEXTURE(SHADER_FLAGS_ENABLED))
478 if (TGX_SHADER_HAS_GOURAUD(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_GOURAUD(raster_type))
479 uber_shader<color_t, ZBUFFER_t, true, true, false, false, false, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
480 else if (TGX_SHADER_HAS_FLAT(SHADER_FLAGS_ENABLED))
481 uber_shader<color_t, ZBUFFER_t, true, false, false, false, false, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
485 else if (TGX_SHADER_HAS_NOZBUFFER(SHADER_FLAGS_ENABLED))
487 if (TGX_SHADER_HAS_ORTHO(SHADER_FLAGS_ENABLED) && (TGX_SHADER_HAS_ORTHO(raster_type)))
489 if (TGX_SHADER_HAS_TEXTURE(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE(raster_type))
491 if (TGX_SHADER_HAS_GOURAUD(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_GOURAUD(raster_type))
493 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
495 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
496 uber_shader<color_t, ZBUFFER_t, false, true, true, true, true, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
497 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
498 uber_shader<color_t, ZBUFFER_t, false, true, true, true, true, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
500 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
502 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
503 uber_shader<color_t, ZBUFFER_t, false, true, true, true, false, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
504 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
505 uber_shader<color_t, ZBUFFER_t, false, true, true, true, false, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
508 else if (TGX_SHADER_HAS_FLAT(SHADER_FLAGS_ENABLED))
510 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
512 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
513 uber_shader<color_t, ZBUFFER_t, false, false, true, true, true, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
514 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
515 uber_shader<color_t, ZBUFFER_t, false, false, true, true, true, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
517 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
519 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
520 uber_shader<color_t, ZBUFFER_t, false, false, true, true, false, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
521 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
522 uber_shader<color_t, ZBUFFER_t, false, false, true, true, false, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
526 else if (TGX_SHADER_HAS_NOTEXTURE(SHADER_FLAGS_ENABLED))
528 if (TGX_SHADER_HAS_GOURAUD(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_GOURAUD(raster_type))
529 uber_shader<color_t, ZBUFFER_t, false, true, false, true, false, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
530 else if (TGX_SHADER_HAS_FLAT(SHADER_FLAGS_ENABLED))
531 uber_shader<color_t, ZBUFFER_t, false, false, false, true, false, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
534 else if (TGX_SHADER_HAS_PERSPECTIVE(SHADER_FLAGS_ENABLED))
536 if (TGX_SHADER_HAS_TEXTURE(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE(raster_type))
538 if (TGX_SHADER_HAS_GOURAUD(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_GOURAUD(raster_type))
540 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
542 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
543 uber_shader<color_t, ZBUFFER_t, false, true, true, false, true, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
544 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
545 uber_shader<color_t, ZBUFFER_t, false, true, true, false, true, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
547 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
549 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
550 uber_shader<color_t, ZBUFFER_t, false, true, true, false, false, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
551 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
552 uber_shader<color_t, ZBUFFER_t, false, true, true, false, false, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
555 else if (TGX_SHADER_HAS_FLAT(SHADER_FLAGS_ENABLED))
557 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
559 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
560 uber_shader<color_t, ZBUFFER_t, false, false, true, false, true, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
561 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
562 uber_shader<color_t, ZBUFFER_t, false, false, true, false, true, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
564 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
566 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
567 uber_shader<color_t, ZBUFFER_t, false, false, true, false, false, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
568 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
569 uber_shader<color_t, ZBUFFER_t, false, false, true, false, false, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
573 else if (TGX_SHADER_HAS_NOTEXTURE(SHADER_FLAGS_ENABLED))
575 if (TGX_SHADER_HAS_GOURAUD(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_GOURAUD(raster_type))
576 uber_shader<color_t, ZBUFFER_t, false, true, false, false, false, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
577 else if (TGX_SHADER_HAS_FLAT(SHADER_FLAGS_ENABLED))
578 uber_shader<color_t, ZBUFFER_t, false, false, false, false, false, false>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
591 template<
bool USE_BLENDING,
typename color_t_im>
593 const int32_t dx1,
const int32_t dy1, int32_t O1,
const RasterizerVec4& fP1,
594 const int32_t dx2,
const int32_t dy2, int32_t O2,
const RasterizerVec4& fP2,
595 const int32_t dx3,
const int32_t dy3, int32_t O3,
const RasterizerVec4& fP3,
596 const RasterizerParams<color_t_im, color_t_im, float> & data)
598 const int32_t stride = data.im->stride();
599 color_t_im * buf = data.im->data() + oox + (ooy * stride);
602 const RGB32 col1 =
RGB64(fP1.color.R, fP1.color.G, fP1.color.B, fP1.A);
603 const RGB32 col2 =
RGB64(fP2.color.R, fP2.color.G, fP2.color.B, fP2.A);
604 const RGB32 col3 =
RGB64(fP3.color.R, fP3.color.G, fP3.color.B, fP3.A);
606 const uintptr_t end = (uintptr_t)(buf + (ly * stride));
607 const int32_t pa = O1 + O2 + O3;
608 const int32_t E = ((pa == 0) ? 1 : 0);
609 const int32_t aera = pa + E;
610 const int shiftC = (aera > (1 << 22)) ? 10 : 0;
612 while ((uintptr_t)(buf) < end)
618 bx = (-O1 + dx1 - 1u) / dx1;
624 if (dy2 <= 0)
return;
625 const int32_t by = (-O2 + dy2 - 1u) / dy2;
629 const int32_t offs = by * stride;
633 const int32_t bx2 = (-O2 + dx2 - 1u) / dx2;
640 if (dy3 <= 0)
return;
641 const int32_t by = (-O3 + dy3 - 1u) / dy3;
645 const int32_t offs = by * stride;
649 const int32_t bx3 = (-O3 + dx3 - 1u) / dx3;
653 int32_t C2 = O2 + (dx2 * bx);
654 int32_t C3 = O3 + (dx3 * bx);
655 while ((bx < lx) && ((C2 | C3) >= 0))
661 buf[bx] = color_t_im(c);
684 template<
bool USE_BLENDING,
bool USE_MASKING,
bool USE_GRADIENT,
typename color_t_im,
typename color_t_tex>
685 void shader_2D_texture(
const int32_t oox,
const int32_t ooy,
const int32_t lx,
const int32_t ly,
686 const int32_t dx1,
const int32_t dy1, int32_t O1,
const RasterizerVec4& fP1,
687 const int32_t dx2,
const int32_t dy2, int32_t O2,
const RasterizerVec4& fP2,
688 const int32_t dx3,
const int32_t dy3, int32_t O3,
const RasterizerVec4& fP3,
689 const RasterizerParams<color_t_im, color_t_tex, float> & data)
692 const int32_t stride = data.im->stride();
693 color_t_im* buf = data.im->data() + oox + (ooy * stride);
695 const uintptr_t end = (uintptr_t)(buf + (ly * stride));
696 const int32_t pa = O1 + O2 + O3;
697 const int32_t E = ((pa == 0) ? 1 : 0);
698 const int32_t aera = pa + E;
700 const float invaera =
fast_inv((
float)aera);
702 const color_t_tex mask_color = data.mask_color;
713 const color_t_tex * tex = data.tex->data();
714 const int32_t texsize_x = data.tex->width();
715 const int32_t texsize_y = data.tex->height();
716 const int32_t texsize_x_mm = data.tex->width() - 1;
717 const int32_t texsize_y_mm = data.tex->height() - 1;
718 const int32_t texstride = data.tex->stride();
731 const float dtx = ((T1.x * dx1) + (T2.x * dx2) + (T3.x * dx3));
732 const float dty = ((T1.y * dx1) + (T2.y * dx2) + (T3.y * dx3));
734 while ((uintptr_t)(buf) < end)
740 bx = (-O1 + dx1 - 1u) / dx1;
746 if (dy2 <= 0)
return;
747 const int32_t by = (-O2 + dy2 - 1u) / dy2;
751 const int32_t offs = by * stride;
755 const int32_t bx2 = (-O2 + dx2 - 1u) / dx2;
762 if (dy3 <= 0)
return;
763 const int32_t by = (-O3 + dy3 - 1u) / dy3;
767 const int32_t offs = by * stride;
771 const int32_t bx3 = (-O3 + dx3 - 1u) / dx3;
775 int32_t C1 = O1 + (dx1 * bx) + E;
776 int32_t C2 = O2 + (dx2 * bx);
777 int32_t C3 = O3 + (dx3 * bx);
779 float tx = ((T1.x * C1) + (T2.x * C2) + (T3.x * C3)) - 0.5f;
780 float ty = ((T1.y * C1) + (T2.y * C2) + (T3.y * C3)) - 0.5f;
782 while ((bx < lx) && ((C2 | C3) >= 0))
788 const float ax = xx - ttx;
789 const float ay = yy - tty;
791 const int minx =
shaderclip(ttx, texsize_x_mm);
792 const int maxx =
shaderclip(ttx + 1, texsize_x_mm);
793 const int miny =
shaderclip(tty, texsize_y_mm) * texstride;
794 const int maxy =
shaderclip(tty + 1, texsize_y_mm) * texstride;
798 auto col00 = tex[minx + miny];
801 auto col10 = tex[maxx + miny];
804 auto col01 = tex[minx + maxy];
807 auto col11 = tex[maxx + maxy];
816 const int sC1 = aera - C3 - C2;
817 const float m = 256.0f / aera;
818 const int r = (int)((sC1 * cf1.
R + sC2 * cf2.R + sC3 * cf3.
R) * m);
819 const int g = (int)((sC1 * cf1.
G + sC2 * cf2.G + sC3 * cf3.
G) * m);
820 const int b = (int)((sC1 * cf1.
B + sC2 * cf2.B + sC3 * cf3.
B) * m);
821 const int a = (int)((sC1 * fP1.A + sC2 * fP2.A + sC3 * fP3.A) * m);
828 buf[bx] = color_t_im(c);
832 buf[bx] = color_t_im(col);
837 color_t_tex col =
interpolateColorsBilinear(tex[minx + miny], tex[maxx + miny], tex[minx + maxy], tex[maxx + maxy], ax, ay);
842 const int sC1 = aera - C3 - C2;
843 const float m = 256.0f / aera;
844 const int r = (int)((sC1 * cf1.
R + sC2 * cf2.R + sC3 * cf3.
R) * m);
845 const int g = (int)((sC1 * cf1.
G + sC2 * cf2.G + sC3 * cf3.
G) * m);
846 const int b = (int)((sC1 * cf1.
B + sC2 * cf2.B + sC3 * cf3.
B) * m);
847 const int a = (int)((sC1 * fP1.A + sC2 * fP2.A + sC3 * fP3.A) * m);
848 col.mult256(r, g, b, a);
852 color_t_tex c = color_t_tex(buf[bx]);
853 c.blend(col, data.opacity);
854 buf[bx] = color_t_im(c);
858 buf[bx] = color_t_im(col);
884 template<
typename BLEND_OP,
typename color_t_im,
typename color_t_tex>
886 const int32_t dx1,
const int32_t dy1, int32_t O1,
const RasterizerVec4& fP1,
887 const int32_t dx2,
const int32_t dy2, int32_t O2,
const RasterizerVec4& fP2,
888 const int32_t dx3,
const int32_t dy3, int32_t O3,
const RasterizerVec4& fP3,
889 const RasterizerParams<color_t_im, color_t_tex, float, BLEND_OP> & data)
892 const int32_t stride = data.im->stride();
893 color_t_im * buf = data.im->data() + oox + (ooy * stride);
895 const uintptr_t end = (uintptr_t)(buf + (ly * stride));
896 const int32_t pa = O1 + O2 + O3;
897 const int32_t E = ((pa == 0) ? 1 : 0);
898 const int32_t aera = pa + E;
900 const float invaera =
fast_inv((
float)aera);
908 const color_t_tex * tex = data.tex->data();
909 const int32_t texsize_x = data.tex->width();
910 const int32_t texsize_y = data.tex->height();
911 const int32_t texsize_x_mm = data.tex->width() - 1;
912 const int32_t texsize_y_mm = data.tex->height() - 1;
913 const int32_t texstride = data.tex->stride();
926 const float dtx = ((T1.
x * dx1) + (T2.
x * dx2) + (T3.
x * dx3));
927 const float dty = ((T1.
y * dx1) + (T2.
y * dx2) + (T3.
y * dx3));
929 while ((uintptr_t)(buf) < end)
935 bx = (-O1 + dx1 - 1u) / dx1;
941 if (dy2 <= 0)
return;
942 const int32_t by = (-O2 + dy2 - 1u) / dy2;
946 const int32_t offs = by * stride;
950 const int32_t bx2 = (-O2 + dx2 - 1u) / dx2;
957 if (dy3 <= 0)
return;
958 const int32_t by = (-O3 + dy3 - 1u) / dy3;
962 const int32_t offs = by * stride;
966 const int32_t bx3 = (-O3 + dx3 - 1u) / dx3;
970 int32_t C1 = O1 + (dx1 * bx) + E;
971 int32_t C2 = O2 + (dx2 * bx);
972 int32_t C3 = O3 + (dx3 * bx);
974 float tx = ((T1.
x * C1) + (T2.
x * C2) + (T3.
x * C3)) - 0.5f;
975 float ty = ((T1.
y * C1) + (T2.
y * C2) + (T3.
y * C3)) - 0.5f;
977 while ((bx < lx) && ((C2 | C3) >= 0))
983 const float ax = xx - ttx;
984 const float ay = yy - tty;
986 const int minx =
shaderclip(ttx, texsize_x_mm);
987 const int maxx =
shaderclip(ttx + 1, texsize_x_mm);
988 const int miny =
shaderclip(tty, texsize_y_mm) * texstride;
989 const int maxy =
shaderclip(tty + 1, texsize_y_mm) * texstride;
991 color_t_tex col =
interpolateColorsBilinear(tex[minx + miny], tex[maxx + miny], tex[minx + maxy], tex[maxx + maxy], ax, ay);
993 buf[bx] = (color_t_im)((*data.p_blend_op)(col, buf[bx]));
const RGB32 RGB32_Red
Color red in RGB32 format.
RGB565 interpolateColorsBilinear(const RGB565 &C00, const RGB565 &C10, const RGB565 &C01, const RGB565 &C11, const float ax, const float ay)
Bilinear interpolation between 4 colors.
Definition: Color.h:621
RGB565 interpolateColorsTriangle(const RGB565 &col1, int32_t C1, const RGB565 &col2, int32_t C2, const RGB565 &col3, const int32_t totC)
Interpolate between 3 colors.
Definition: Color.h:591
TGX_INLINE int32_t lfloorf(float x)
Compute (int32_t)floorf(x).
Definition: Misc.h:374
TGX_INLINE T max(const T &a, const T &b)
Don't know why but much faster than fmaxf() for floats.
Definition: Misc.h:136
TGX_INLINE float fast_inv(float x)
Fast (approximate) computation of 1/x.
Definition: Misc.h:180
Triangle shader parameters.
void uber_shader(const int32_t oox, const int32_t ooy, const int32_t lx, const int32_t ly, const int32_t dx1, const int32_t dy1, int32_t O1, const RasterizerVec4 &fP1, const int32_t dx2, const int32_t dy2, int32_t O2, const RasterizerVec4 &fP2, const int32_t dx3, const int32_t dy3, int32_t O3, const RasterizerVec4 &fP3, const RasterizerParams< color_t, color_t, ZBUFFER_t > &data)
UBER-SHADER for all 3D rendering variants.
Definition: Shaders.h:78
void shader_2D_texture(const int32_t oox, const int32_t ooy, const int32_t lx, const int32_t ly, const int32_t dx1, const int32_t dy1, int32_t O1, const RasterizerVec4 &fP1, const int32_t dx2, const int32_t dy2, int32_t O2, const RasterizerVec4 &fP2, const int32_t dx3, const int32_t dy3, int32_t O3, const RasterizerVec4 &fP3, const RasterizerParams< color_t_im, color_t_tex, float > &data)
2D shader (texture)
Definition: Shaders.h:685
void shader_2D_gradient(const int32_t oox, const int32_t ooy, const int32_t lx, const int32_t ly, const int32_t dx1, const int32_t dy1, int32_t O1, const RasterizerVec4 &fP1, const int32_t dx2, const int32_t dy2, int32_t O2, const RasterizerVec4 &fP2, const int32_t dx3, const int32_t dy3, int32_t O3, const RasterizerVec4 &fP3, const RasterizerParams< color_t_im, color_t_im, float > &data)
2D shader (gradient)
Definition: Shaders.h:592
void shader_test(const int32_t oox, const int32_t ooy, const int32_t lx, const int32_t ly, const int32_t dx1, const int32_t dy1, int32_t O1, const tgx::RasterizerVec4 &fP1, const int32_t dx2, const int32_t dy2, int32_t O2, const tgx::RasterizerVec4 &fP2, const int32_t dx3, const int32_t dy3, int32_t O3, const tgx::RasterizerVec4 &fP3, const tgx::RasterizerParams< color_t, color_t, ZBUFFER_t > &data)
For test purposes...
Definition: Shaders.h:45
void shader_2D_texture_blend_op(const int32_t oox, const int32_t ooy, const int32_t lx, const int32_t ly, const int32_t dx1, const int32_t dy1, int32_t O1, const RasterizerVec4 &fP1, const int32_t dx2, const int32_t dy2, int32_t O2, const RasterizerVec4 &fP2, const int32_t dx3, const int32_t dy3, int32_t O3, const RasterizerVec4 &fP3, const RasterizerParams< color_t_im, color_t_tex, float, BLEND_OP > &data)
2D shader (texture with custom blending operator)
Definition: Shaders.h:885
void shader_select(const int32_t oox, const int32_t ooy, const int32_t lx, const int32_t ly, const int32_t dx1, const int32_t dy1, int32_t O1, const RasterizerVec4 &fP1, const int32_t dx2, const int32_t dy2, int32_t O2, const RasterizerVec4 &fP2, const int32_t dx3, const int32_t dy3, int32_t O3, const RasterizerVec4 &fP3, const RasterizerParams< color_t, color_t, ZBUFFER_t > &data)
META-Shader THAT DISPATCH TO THE CORRECT Shader ABOVE (IF ENABLED).
Definition: Shaders.h:381
TGX_INLINE int shaderclip(int v, int maxv)
for texture clamping
Definition: Shaders.h:34
Color in R8/G8/B8/A8 format.
Definition: Color.h:1176
void mult256(int mr, int mg, int mb)
Multiply each color component by a given factor m/256 with m in [0,256] except the A component.
Definition: Color.h:1590
void blend256(const RGB32 &fg_col, uint32_t alpha)
alpha-blend fg_col over this one with a given opacity in the range 0.0f (fully transparent) to 1....
Definition: Color.h:1561
void blend(const RGB32 &fg_col, float alpha)
alpha-blend fg_col over this one with a given opacity in the range 0.0f (fully transparent) to 1....
Definition: Color.h:1546
float opacity() const
Return the opacity (alpha channel value) of this color in the range [0,1] (0=fully transparent,...
Definition: Color.h:1629
float opacity() const
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:565
Color in R16/G16/B16/A16 format.
Definition: Color.h:1791
Color in R,G,B float format.
Definition: Color.h:2407
float R
Red channel.
Definition: Color.h:2420
float B
Blue channel.
Definition: Color.h:2422
float G
Green channel.
Definition: Color.h:2421
T x
'x' coordinate (first dimension)
Definition: Vec2.h:72
T y
'y' coordinate (second dimension)
Definition: Vec2.h:73