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 bool USE_UNLIT =
false>
79 void uber_shader(
const int32_t oox,
const int32_t ooy,
const int32_t lx,
const int32_t ly,
80 const int32_t dx1,
const int32_t dy1, int32_t O1,
const RasterizerVec4& fP1,
81 const int32_t dx2,
const int32_t dy2, int32_t O2,
const RasterizerVec4& fP2,
82 const int32_t dx3,
const int32_t dy3, int32_t O3,
const RasterizerVec4& fP3,
83 const RasterizerParams<color_t, color_t, ZBUFFER_t>& data)
85 static_assert(!(USE_GOURAUD && USE_UNLIT),
"UNLIT and GOURAUD shader variants are mutually exclusive.");
86 static_assert((!USE_UNLIT) || USE_TEXTURE,
"The dedicated UNLIT shader variant is only useful for textured rendering.");
89 const int32_t stride = data.im->stride();
90 color_t* buf = data.im->data() + oox + (ooy * stride);
92 const uintptr_t end = (uintptr_t)(buf + (ly * stride));
93 const int32_t pa = O1 + O2 + O3;
94 const int32_t E = ((pa == 0) ? 1 : 0);
95 const int32_t aera = pa + E;
98 ZBUFFER_t* zbuf =
nullptr;
100 float wa = 0.0f, wb = 0.0f;
101 float fP1a_z = 0.0f, fP2a_z = 0.0f, fP3a_z = 0.0f;
104 if constexpr (USE_ZBUFFER)
106 zstride = data.im->lx();
107 zbuf = data.zbuf + oox + (ooy * zstride);
111 const float invaera =
fast_inv((
float)aera);
112 float invaera_wa_factor = USE_ORTHO ? 1.0f : wa;
114 fP1a_z = fP1.w * invaera * invaera_wa_factor;
115 fP2a_z = fP2.w * invaera * invaera_wa_factor;
116 fP3a_z = fP3.w * invaera * invaera_wa_factor;
118 dw_z = (dx1 * fP1a_z) + (dx2 * fP2a_z) + (dx3 * fP3a_z);
123 color_t col1_g, col2_g, col3_g;
124 int shiftC = 0, aeraShifted = 0;
125 int fPR = 0, fPG = 0, fPB = 0;
126 int fP1R = 0, fP1G = 0, fP1B = 0;
127 int fP21R = 0, fP21G = 0, fP21B = 0;
128 int fP31R = 0, fP31G = 0, fP31B = 0;
130 float invaera_persp = 0.0f;
131 float fP1a_p = 0.0f, fP2a_p = 0.0f, fP3a_p = 0.0f;
134 const color_t* tex =
nullptr;
135 int32_t texsize_x_mm = 0, texsize_y_mm = 0, texstride = 0;
136 float dtx = 0.0f, dty = 0.0f;
139 if constexpr (USE_GOURAUD)
141 if constexpr (USE_TEXTURE)
146 fP1R = (
int)(256 * cf1.
R); fP1G = (int)(256 * cf1.
G); fP1B = (int)(256 * cf1.
B);
147 fP21R = (int)(256 * (cf2.R - cf1.
R)); fP21G = (int)(256 * (cf2.G - cf1.
G)); fP21B = (int)(256 * (cf2.B - cf1.
B));
148 fP31R = (int)(256 * (cf3.
R - cf1.
R)); fP31G = (int)(256 * (cf3.
G - cf1.
G)); fP31B = (int)(256 * (cf3.
B - cf1.
B));
149 shiftC = (aera > (1 << 22)) ? 10 : 0;
150 aeraShifted = aera >> shiftC;
154 col1_g = (color_t)fP1.color;
155 col2_g = (color_t)fP2.color;
156 col3_g = (color_t)fP3.color;
157 shiftC = (aera > (1 << 22)) ? 10 : 0;
158 aeraShifted = aera >> shiftC;
163 flat_color = (color_t)data.facecolor;
164 if constexpr (USE_TEXTURE && !USE_UNLIT)
166 const RGBf& cf = (
RGBf)data.facecolor;
167 fPR = (
int)(256 * cf.
R); fPG = (int)(256 * cf.
G); fPB = (int)(256 * cf.
B);
171 if constexpr (USE_TEXTURE)
173 tex = data.tex->data();
174 const int32_t texsize_x = data.tex->width();
175 const int32_t texsize_y = data.tex->height();
176 texsize_x_mm = texsize_x - 1;
177 texsize_y_mm = texsize_y - 1;
178 texstride = data.tex->stride();
180 T1 = fP1.T; T2 = fP2.T; T3 = fP3.T;
182 const float invaera =
fast_inv((
float)aera);
184 if constexpr (USE_ORTHO)
186 T1 *= invaera; T2 *= invaera; T3 *= invaera;
190 invaera_persp = invaera;
191 fP1a_p = fP1.w * invaera_persp;
192 fP2a_p = fP2.w * invaera_persp;
193 fP3a_p = fP3.w * invaera_persp;
194 dw_p = (dx1 * fP1a_p) + (dx2 * fP2a_p) + (dx3 * fP3a_p);
196 T1 *= fP1a_p; T2 *= fP2a_p; T3 *= fP3a_p;
199 T1.
x *= texsize_x; T2.
x *= texsize_x; T3.
x *= texsize_x;
200 T1.
y *= texsize_y; T2.
y *= texsize_y; T3.
y *= texsize_y;
202 dtx = ((T1.
x * dx1) + (T2.
x * dx2) + (T3.
x * dx3));
203 dty = ((T1.
y * dx1) + (T2.
y * dx2) + (T3.
y * dx3));
207 while ((uintptr_t)(buf) < end)
213 bx = (-O1 + dx1 - 1u) / dx1;
219 if (dy2 <= 0)
return;
220 const int32_t by = (-O2 + dy2 - 1u) / dy2;
221 O1 += (by * dy1); O2 += (by * dy2); O3 += (by * dy3);
223 if constexpr (USE_ZBUFFER) zbuf += by * zstride;
226 bx =
max(bx, (int32_t)((-O2 + dx2 - 1u) / dx2));
232 if (dy3 <= 0)
return;
233 const int32_t by = (-O3 + dy3 - 1u) / dy3;
234 O1 += (by * dy1); O2 += (by * dy2); O3 += (by * dy3);
236 if constexpr (USE_ZBUFFER) zbuf += by * zstride;
239 bx =
max(bx, (int32_t)((-O3 + dx3 - 1u) / dx3));
243 int32_t C1 = O1 + (dx1 * bx) + E;
244 int32_t C2 = O2 + (dx2 * bx);
245 int32_t C3 = O3 + (dx3 * bx);
248 if constexpr (USE_ZBUFFER)
250 cw_z = ((C1 * fP1a_z) + (C2 * fP2a_z) + (C3 * fP3a_z));
251 if constexpr (!USE_ORTHO)
258 float tx = 0.0f, ty = 0.0f;
259 if constexpr (USE_TEXTURE)
261 tx = ((T1.
x * C1) + (T2.
x * C2) + (T3.
x * C3));
262 ty = ((T1.
y * C1) + (T2.
y * C2) + (T3.
y * C3));
263 if constexpr (!USE_ORTHO)
265 cw_p = ((C1 * fP1a_p) + (C2 * fP2a_p) + (C3 * fP3a_p));
270 while ((bx < lx) && ((C2 | C3) >= 0))
273 if constexpr (USE_ZBUFFER)
275 ZBUFFER_t& W = zbuf[bx];
278 if constexpr (std::is_same<ZBUFFER_t, uint16_t>::value)
280 current_z = (USE_ORTHO) ? ((ZBUFFER_t)(cw_z * wa + wb)) : ((ZBUFFER_t)cw_z);
284 current_z = (ZBUFFER_t)cw_z;
301 if constexpr (USE_TEXTURE)
304 if constexpr (!USE_ORTHO)
312 if constexpr (TEXTURE_BILINEAR)
316 const float ax = xx - ttx;
317 const float ay = yy - tty;
319 const int minx = TEXTURE_WRAP ? (ttx & texsize_x_mm) :
shaderclip(ttx, texsize_x_mm);
320 const int maxx = TEXTURE_WRAP ? ((ttx + 1) & texsize_x_mm) :
shaderclip(ttx + 1, texsize_x_mm);
321 const int miny = (TEXTURE_WRAP ? (tty & texsize_y_mm) :
shaderclip(tty, texsize_y_mm)) * texstride;
322 const int maxy = (TEXTURE_WRAP ? ((tty + 1) & texsize_y_mm) :
shaderclip(tty + 1, texsize_y_mm)) * texstride;
328 const int ttx = TEXTURE_WRAP ? ((int)(xx)) & texsize_x_mm :
shaderclip((
int)(xx), texsize_x_mm);
329 const int tty = TEXTURE_WRAP ? ((int)(yy)) & texsize_y_mm :
shaderclip((
int)(yy), texsize_y_mm);
330 final_color = tex[ttx + tty * texstride];
333 if constexpr (USE_GOURAUD)
335 const int32_t C2s = C2 >> shiftC;
336 const int32_t C3s = C3 >> shiftC;
337 const int r = fP1R + ((C2s * fP21R + C3s * fP31R) / aeraShifted);
338 const int g = fP1G + ((C2s * fP21G + C3s * fP31G) / aeraShifted);
339 const int b = fP1B + ((C2s * fP21B + C3s * fP31B) / aeraShifted);
340 final_color.mult256(r, g, b);
342 else if constexpr (!USE_UNLIT)
344 final_color.mult256(fPR, fPG, fPB);
349 if constexpr (USE_GOURAUD)
355 final_color = flat_color;
358 buf[bx] = final_color;
366 if constexpr (USE_ZBUFFER) cw_z += dw_z;
368 if constexpr (USE_TEXTURE)
372 if constexpr (!USE_ORTHO) cw_p += dw_p;
381 if constexpr (USE_ZBUFFER) zbuf += zstride;
389 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,
390 const int32_t dx1,
const int32_t dy1, int32_t O1,
const RasterizerVec4& fP1,
391 const int32_t dx2,
const int32_t dy2, int32_t O2,
const RasterizerVec4& fP2,
392 const int32_t dx3,
const int32_t dy3, int32_t O3,
const RasterizerVec4& fP3,
393 const RasterizerParams<color_t, color_t, ZBUFFER_t> & data)
395 int raster_type = data.shader_type;
396 if (TGX_SHADER_HAS_ZBUFFER(SHADER_FLAGS_ENABLED) && (TGX_SHADER_HAS_ZBUFFER(raster_type)))
398 if (TGX_SHADER_HAS_ORTHO(SHADER_FLAGS_ENABLED) && (TGX_SHADER_HAS_ORTHO(raster_type)))
400 if (TGX_SHADER_HAS_TEXTURE(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE(raster_type))
402 if (TGX_SHADER_HAS_GOURAUD(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_GOURAUD(raster_type))
404 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
406 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
407 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);
408 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
409 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);
411 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
413 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
414 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);
415 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
416 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);
419 else if (TGX_SHADER_HAS_UNLIT(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_UNLIT(raster_type))
421 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
423 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
424 uber_shader<color_t, ZBUFFER_t, true, false, true, true, true, false, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
425 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
426 uber_shader<color_t, ZBUFFER_t, true, false, true, true, true, true, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
428 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
430 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
431 uber_shader<color_t, ZBUFFER_t, true, false, true, true, false, false, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
432 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
433 uber_shader<color_t, ZBUFFER_t, true, false, true, true, false, true, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
436 else if (TGX_SHADER_HAS_FLAT(SHADER_FLAGS_ENABLED))
438 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
440 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
441 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);
442 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
443 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);
445 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
447 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
448 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);
449 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
450 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);
454 else if (TGX_SHADER_HAS_NOTEXTURE(SHADER_FLAGS_ENABLED))
456 if (TGX_SHADER_HAS_GOURAUD(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_GOURAUD(raster_type))
457 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);
459 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);
462 else if (TGX_SHADER_HAS_PERSPECTIVE(SHADER_FLAGS_ENABLED))
464 if (TGX_SHADER_HAS_TEXTURE(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE(raster_type))
466 if (TGX_SHADER_HAS_GOURAUD(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_GOURAUD(raster_type))
468 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
470 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
471 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);
472 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
473 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);
475 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
477 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
478 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);
479 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
480 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);
483 else if (TGX_SHADER_HAS_UNLIT(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_UNLIT(raster_type))
485 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
487 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
488 uber_shader<color_t, ZBUFFER_t, true, false, true, false, true, false, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
489 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
490 uber_shader<color_t, ZBUFFER_t, true, false, true, false, true, true, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
492 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
494 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
495 uber_shader<color_t, ZBUFFER_t, true, false, true, false, false, false, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
496 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
497 uber_shader<color_t, ZBUFFER_t, true, false, true, false, false, 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_FLAT(SHADER_FLAGS_ENABLED))
502 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
504 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
505 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);
506 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
507 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);
509 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
511 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
512 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);
513 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
514 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);
518 else if (TGX_SHADER_HAS_NOTEXTURE(SHADER_FLAGS_ENABLED))
520 if (TGX_SHADER_HAS_GOURAUD(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_GOURAUD(raster_type))
521 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);
523 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);
527 else if (TGX_SHADER_HAS_NOZBUFFER(SHADER_FLAGS_ENABLED))
529 if (TGX_SHADER_HAS_ORTHO(SHADER_FLAGS_ENABLED) && (TGX_SHADER_HAS_ORTHO(raster_type)))
531 if (TGX_SHADER_HAS_TEXTURE(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE(raster_type))
533 if (TGX_SHADER_HAS_GOURAUD(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_GOURAUD(raster_type))
535 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
537 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
538 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);
539 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
540 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);
542 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
544 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
545 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);
546 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
547 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);
550 else if (TGX_SHADER_HAS_UNLIT(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_UNLIT(raster_type))
552 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
554 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
555 uber_shader<color_t, ZBUFFER_t, false, false, true, true, true, false, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
556 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
557 uber_shader<color_t, ZBUFFER_t, false, false, true, true, true, true, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
559 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
561 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
562 uber_shader<color_t, ZBUFFER_t, false, false, true, true, false, false, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
563 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
564 uber_shader<color_t, ZBUFFER_t, false, false, true, true, false, true, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
567 else if (TGX_SHADER_HAS_FLAT(SHADER_FLAGS_ENABLED))
569 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
571 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
572 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);
573 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
574 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);
576 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
578 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
579 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);
580 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
581 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);
585 else if (TGX_SHADER_HAS_NOTEXTURE(SHADER_FLAGS_ENABLED))
587 if (TGX_SHADER_HAS_GOURAUD(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_GOURAUD(raster_type))
588 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);
590 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);
593 else if (TGX_SHADER_HAS_PERSPECTIVE(SHADER_FLAGS_ENABLED))
595 if (TGX_SHADER_HAS_TEXTURE(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE(raster_type))
597 if (TGX_SHADER_HAS_GOURAUD(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_GOURAUD(raster_type))
599 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
601 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
602 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);
603 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
604 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);
606 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
608 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
609 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);
610 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
611 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);
614 else if (TGX_SHADER_HAS_UNLIT(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_UNLIT(raster_type))
616 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
618 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
619 uber_shader<color_t, ZBUFFER_t, false, false, true, false, true, false, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
620 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
621 uber_shader<color_t, ZBUFFER_t, false, false, true, false, true, true, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
623 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
625 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
626 uber_shader<color_t, ZBUFFER_t, false, false, true, false, false, false, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
627 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
628 uber_shader<color_t, ZBUFFER_t, false, false, true, false, false, true, true>(oox, ooy, lx, ly, dx1, dy1, O1, fP1, dx2, dy2, O2, fP2, dx3, dy3, O3, fP3, data);
631 else if (TGX_SHADER_HAS_FLAT(SHADER_FLAGS_ENABLED))
633 if (TGX_SHADER_HAS_TEXTURE_BILINEAR(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_BILINEAR(raster_type))
635 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
636 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);
637 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
638 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);
640 else if (TGX_SHADER_HAS_TEXTURE_NEAREST(SHADER_FLAGS_ENABLED))
642 if (TGX_SHADER_HAS_TEXTURE_CLAMP(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_TEXTURE_CLAMP(raster_type))
643 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);
644 else if (TGX_SHADER_HAS_TEXTURE_WRAP_POW2(SHADER_FLAGS_ENABLED))
645 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);
649 else if (TGX_SHADER_HAS_NOTEXTURE(SHADER_FLAGS_ENABLED))
651 if (TGX_SHADER_HAS_GOURAUD(SHADER_FLAGS_ENABLED) && TGX_SHADER_HAS_GOURAUD(raster_type))
652 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);
654 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);
667 template<
bool USE_BLENDING,
typename color_t_im>
669 const int32_t dx1,
const int32_t dy1, int32_t O1,
const RasterizerVec4& fP1,
670 const int32_t dx2,
const int32_t dy2, int32_t O2,
const RasterizerVec4& fP2,
671 const int32_t dx3,
const int32_t dy3, int32_t O3,
const RasterizerVec4& fP3,
672 const RasterizerParams<color_t_im, color_t_im, float> & data)
674 const int32_t stride = data.im->stride();
675 color_t_im * buf = data.im->data() + oox + (ooy * stride);
678 const RGB32 col1 =
RGB64(fP1.color.R, fP1.color.G, fP1.color.B, fP1.A);
679 const RGB32 col2 =
RGB64(fP2.color.R, fP2.color.G, fP2.color.B, fP2.A);
680 const RGB32 col3 =
RGB64(fP3.color.R, fP3.color.G, fP3.color.B, fP3.A);
682 const uintptr_t end = (uintptr_t)(buf + (ly * stride));
683 const int32_t pa = O1 + O2 + O3;
684 const int32_t E = ((pa == 0) ? 1 : 0);
685 const int32_t aera = pa + E;
686 const int shiftC = (aera > (1 << 22)) ? 10 : 0;
688 while ((uintptr_t)(buf) < end)
694 bx = (-O1 + dx1 - 1u) / dx1;
700 if (dy2 <= 0)
return;
701 const int32_t by = (-O2 + dy2 - 1u) / dy2;
705 const int32_t offs = by * stride;
709 const int32_t bx2 = (-O2 + dx2 - 1u) / dx2;
716 if (dy3 <= 0)
return;
717 const int32_t by = (-O3 + dy3 - 1u) / dy3;
721 const int32_t offs = by * stride;
725 const int32_t bx3 = (-O3 + dx3 - 1u) / dx3;
729 int32_t C2 = O2 + (dx2 * bx);
730 int32_t C3 = O3 + (dx3 * bx);
731 while ((bx < lx) && ((C2 | C3) >= 0))
737 buf[bx] = color_t_im(c);
760 template<
bool USE_BLENDING,
bool USE_MASKING,
bool USE_GRADIENT,
typename color_t_im,
typename color_t_tex>
761 void shader_2D_texture(
const int32_t oox,
const int32_t ooy,
const int32_t lx,
const int32_t ly,
762 const int32_t dx1,
const int32_t dy1, int32_t O1,
const RasterizerVec4& fP1,
763 const int32_t dx2,
const int32_t dy2, int32_t O2,
const RasterizerVec4& fP2,
764 const int32_t dx3,
const int32_t dy3, int32_t O3,
const RasterizerVec4& fP3,
765 const RasterizerParams<color_t_im, color_t_tex, float> & data)
768 const int32_t stride = data.im->stride();
769 color_t_im* buf = data.im->data() + oox + (ooy * stride);
771 const uintptr_t end = (uintptr_t)(buf + (ly * stride));
772 const int32_t pa = O1 + O2 + O3;
773 const int32_t E = ((pa == 0) ? 1 : 0);
774 const int32_t aera = pa + E;
776 const float invaera =
fast_inv((
float)aera);
778 const color_t_tex mask_color = data.mask_color;
789 const color_t_tex * tex = data.tex->data();
790 const int32_t texsize_x = data.tex->width();
791 const int32_t texsize_y = data.tex->height();
792 const int32_t texsize_x_mm = data.tex->width() - 1;
793 const int32_t texsize_y_mm = data.tex->height() - 1;
794 const int32_t texstride = data.tex->stride();
807 const float dtx = ((T1.x * dx1) + (T2.x * dx2) + (T3.x * dx3));
808 const float dty = ((T1.y * dx1) + (T2.y * dx2) + (T3.y * dx3));
810 while ((uintptr_t)(buf) < end)
816 bx = (-O1 + dx1 - 1u) / dx1;
822 if (dy2 <= 0)
return;
823 const int32_t by = (-O2 + dy2 - 1u) / dy2;
827 const int32_t offs = by * stride;
831 const int32_t bx2 = (-O2 + dx2 - 1u) / dx2;
838 if (dy3 <= 0)
return;
839 const int32_t by = (-O3 + dy3 - 1u) / dy3;
843 const int32_t offs = by * stride;
847 const int32_t bx3 = (-O3 + dx3 - 1u) / dx3;
851 int32_t C1 = O1 + (dx1 * bx) + E;
852 int32_t C2 = O2 + (dx2 * bx);
853 int32_t C3 = O3 + (dx3 * bx);
855 float tx = ((T1.x * C1) + (T2.x * C2) + (T3.x * C3)) - 0.5f;
856 float ty = ((T1.y * C1) + (T2.y * C2) + (T3.y * C3)) - 0.5f;
858 while ((bx < lx) && ((C2 | C3) >= 0))
864 const float ax = xx - ttx;
865 const float ay = yy - tty;
867 const int minx =
shaderclip(ttx, texsize_x_mm);
868 const int maxx =
shaderclip(ttx + 1, texsize_x_mm);
869 const int miny =
shaderclip(tty, texsize_y_mm) * texstride;
870 const int maxy =
shaderclip(tty + 1, texsize_y_mm) * texstride;
874 auto col00 = tex[minx + miny];
875 auto col10 = tex[maxx + miny];
876 auto col01 = tex[minx + maxy];
877 auto col11 = tex[maxx + maxy];
879 if ((col00 != mask_color) && (col10 != mask_color) && (col01 != mask_color) && (col11 != mask_color))
886 const int sC1 = aera - C3 - C2;
887 const float m = 256.0f / aera;
888 const int r = (int)((sC1 * cf1.
R + sC2 * cf2.R + sC3 * cf3.
R) * m);
889 const int g = (int)((sC1 * cf1.
G + sC2 * cf2.G + sC3 * cf3.
G) * m);
890 const int b = (int)((sC1 * cf1.
B + sC2 * cf2.B + sC3 * cf3.
B) * m);
891 const int a = (int)((sC1 * fP1.A + sC2 * fP2.A + sC3 * fP3.A) * m);
892 col.mult256(r, g, b, a);
896 color_t_tex c = color_t_tex(buf[bx]);
897 c.blend(col, data.opacity);
898 buf[bx] = color_t_im(c);
902 buf[bx] = color_t_im(col);
918 const int sC1 = aera - C3 - C2;
919 const float m = 256.0f / aera;
920 const int r = (int)((sC1 * cf1.
R + sC2 * cf2.R + sC3 * cf3.
R) * m);
921 const int g = (int)((sC1 * cf1.
G + sC2 * cf2.G + sC3 * cf3.
G) * m);
922 const int b = (int)((sC1 * cf1.
B + sC2 * cf2.B + sC3 * cf3.
B) * m);
923 const int a = (int)((sC1 * fP1.A + sC2 * fP2.A + sC3 * fP3.A) * m);
930 buf[bx] = color_t_im(c);
936 buf[bx] = color_t_im(col);
942 buf[bx] = color_t_im(c);
949 color_t_tex col =
interpolateColorsBilinear(tex[minx + miny], tex[maxx + miny], tex[minx + maxy], tex[maxx + maxy], ax, ay);
954 const int sC1 = aera - C3 - C2;
955 const float m = 256.0f / aera;
956 const int r = (int)((sC1 * cf1.
R + sC2 * cf2.R + sC3 * cf3.
R) * m);
957 const int g = (int)((sC1 * cf1.
G + sC2 * cf2.G + sC3 * cf3.
G) * m);
958 const int b = (int)((sC1 * cf1.
B + sC2 * cf2.B + sC3 * cf3.
B) * m);
959 const int a = (int)((sC1 * fP1.A + sC2 * fP2.A + sC3 * fP3.A) * m);
960 col.mult256(r, g, b, a);
964 color_t_tex c = color_t_tex(buf[bx]);
965 c.blend(col, data.opacity);
966 buf[bx] = color_t_im(c);
970 buf[bx] = color_t_im(col);
996 template<
typename BLEND_OP,
typename color_t_im,
typename color_t_tex>
998 const int32_t dx1,
const int32_t dy1, int32_t O1,
const RasterizerVec4& fP1,
999 const int32_t dx2,
const int32_t dy2, int32_t O2,
const RasterizerVec4& fP2,
1000 const int32_t dx3,
const int32_t dy3, int32_t O3,
const RasterizerVec4& fP3,
1001 const RasterizerParams<color_t_im, color_t_tex, float, BLEND_OP> & data)
1004 const int32_t stride = data.im->stride();
1005 color_t_im * buf = data.im->data() + oox + (ooy * stride);
1007 const uintptr_t end = (uintptr_t)(buf + (ly * stride));
1008 const int32_t pa = O1 + O2 + O3;
1009 const int32_t E = ((pa == 0) ? 1 : 0);
1010 const int32_t aera = pa + E;
1012 const float invaera =
fast_inv((
float)aera);
1020 const color_t_tex * tex = data.tex->data();
1021 const int32_t texsize_x = data.tex->width();
1022 const int32_t texsize_y = data.tex->height();
1023 const int32_t texsize_x_mm = data.tex->width() - 1;
1024 const int32_t texsize_y_mm = data.tex->height() - 1;
1025 const int32_t texstride = data.tex->stride();
1038 const float dtx = ((T1.
x * dx1) + (T2.
x * dx2) + (T3.
x * dx3));
1039 const float dty = ((T1.
y * dx1) + (T2.
y * dx2) + (T3.
y * dx3));
1041 while ((uintptr_t)(buf) < end)
1047 bx = (-O1 + dx1 - 1u) / dx1;
1053 if (dy2 <= 0)
return;
1054 const int32_t by = (-O2 + dy2 - 1u) / dy2;
1058 const int32_t offs = by * stride;
1062 const int32_t bx2 = (-O2 + dx2 - 1u) / dx2;
1069 if (dy3 <= 0)
return;
1070 const int32_t by = (-O3 + dy3 - 1u) / dy3;
1074 const int32_t offs = by * stride;
1078 const int32_t bx3 = (-O3 + dx3 - 1u) / dx3;
1082 int32_t C1 = O1 + (dx1 * bx) + E;
1083 int32_t C2 = O2 + (dx2 * bx);
1084 int32_t C3 = O3 + (dx3 * bx);
1086 float tx = ((T1.
x * C1) + (T2.
x * C2) + (T3.
x * C3)) - 0.5f;
1087 float ty = ((T1.
y * C1) + (T2.
y * C2) + (T3.
y * C3)) - 0.5f;
1089 while ((bx < lx) && ((C2 | C3) >= 0))
1091 const float xx = tx;
1092 const float yy = ty;
1095 const float ax = xx - ttx;
1096 const float ay = yy - tty;
1098 const int minx =
shaderclip(ttx, texsize_x_mm);
1099 const int maxx =
shaderclip(ttx + 1, texsize_x_mm);
1100 const int miny =
shaderclip(tty, texsize_y_mm) * texstride;
1101 const int maxy =
shaderclip(tty + 1, texsize_y_mm) * texstride;
1103 color_t_tex col =
interpolateColorsBilinear(tex[minx + miny], tex[maxx + miny], tex[minx + maxy], tex[maxx + maxy], ax, ay);
1105 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:391
TGX_INLINE T max(const T &a, const T &b)
Don't know why but much faster than fmaxf() for floats.
Definition: Misc.h:153
TGX_INLINE float fast_inv(float x)
Fast (approximate) computation of 1/x.
Definition: Misc.h:197
Triangle shader parameters.
#define TGX_SHADER_CAN_USE_FLAT_OR_UNLIT(enabled_shader_type, shader_type)
True when a shader configuration can use either flat shading or unlit shading.
Definition: ShaderParams.h:112
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:761
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:668
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:997
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:389
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:79
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
uint8_t A
Alpha channel (8bits)
Definition: Color.h:1197
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