1 // Copyright (C) 2002-2012 Nikolaus Gebhardt / Thomas Alten
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
4
5 #include "IrrCompileConfig.h"
6 #include "IBurningShader.h"
7
8 #ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
9
10 // compile flag for this file
11 #undef USE_ZBUFFER
12 #undef IPOL_Z
13 #undef CMP_Z
14 #undef WRITE_Z
15
16 #undef IPOL_W
17 #undef CMP_W
18 #undef WRITE_W
19
20 #undef SUBTEXEL
21 #undef INVERSE_W
22
23 #undef IPOL_C0
24 #undef IPOL_T0
25 #undef IPOL_T1
26
27 // define render case
28 #define SUBTEXEL
29 #define INVERSE_W
30
31 #define USE_ZBUFFER
32 #define IPOL_W
33 #define CMP_W
34 #define WRITE_W
35
36 #define IPOL_C0
37 #define IPOL_T0
38 #define IPOL_T1
39
40 // apply global override
41 #ifndef SOFTWARE_DRIVER_2_PERSPECTIVE_CORRECT
42 #undef INVERSE_W
43 #endif
44
45 #ifndef SOFTWARE_DRIVER_2_SUBTEXEL
46 #undef SUBTEXEL
47 #endif
48
49 #ifndef SOFTWARE_DRIVER_2_USE_VERTEX_COLOR
50 #undef IPOL_C0
51 #endif
52
53 #if !defined ( SOFTWARE_DRIVER_2_USE_WBUFFER ) && defined ( USE_ZBUFFER )
54 #ifndef SOFTWARE_DRIVER_2_PERSPECTIVE_CORRECT
55 #undef IPOL_W
56 #endif
57 #define IPOL_Z
58
59 #ifdef CMP_W
60 #undef CMP_W
61 #define CMP_Z
62 #endif
63
64 #ifdef WRITE_W
65 #undef WRITE_W
66 #define WRITE_Z
67 #endif
68
69 #endif
70
71 namespace irr
72 {
73
74 namespace video
75 {
76
77 class CTRGTextureLightMap2_M4 : public IBurningShader
78 {
79 public:
80
81 //! constructor
82 CTRGTextureLightMap2_M4(CBurningVideoDriver* driver);
83
84 //! draws an indexed triangle list
85 virtual void drawTriangle ( const s4DVertex *a,const s4DVertex *b,const s4DVertex *c );
86
87
88 private:
89 void scanline_bilinear ();
90
91 sScanConvertData scan;
92 sScanLineData line;
93
94 };
95
96 //! constructor
CTRGTextureLightMap2_M4(CBurningVideoDriver * driver)97 CTRGTextureLightMap2_M4::CTRGTextureLightMap2_M4(CBurningVideoDriver* driver)
98 : IBurningShader(driver)
99 {
100 #ifdef _DEBUG
101 setDebugName("CTRGTextureLightMap2_M4");
102 #endif
103 }
104
105
106
107 /*!
108 */
scanline_bilinear()109 void CTRGTextureLightMap2_M4::scanline_bilinear ()
110 {
111 tVideoSample *dst;
112
113 #ifdef USE_ZBUFFER
114 fp24 *z;
115 #endif
116
117 s32 xStart;
118 s32 xEnd;
119 s32 dx;
120
121
122 #ifdef SUBTEXEL
123 f32 subPixel;
124 #endif
125
126 #ifdef IPOL_Z
127 f32 slopeZ;
128 #endif
129 #ifdef IPOL_W
130 fp24 slopeW;
131 #endif
132 #ifdef IPOL_C0
133 sVec4 slopeC;
134 #endif
135 #ifdef IPOL_T0
136 sVec2 slopeT[BURNING_MATERIAL_MAX_TEXTURES];
137 #endif
138
139 // apply top-left fill-convention, left
140 xStart = core::ceil32( line.x[0] );
141 xEnd = core::ceil32( line.x[1] ) - 1;
142
143 dx = xEnd - xStart;
144
145 if ( dx < 0 )
146 return;
147
148 // slopes
149 const f32 invDeltaX = core::reciprocal_approxim ( line.x[1] - line.x[0] );
150
151 #ifdef IPOL_Z
152 slopeZ = (line.z[1] - line.z[0]) * invDeltaX;
153 #endif
154 #ifdef IPOL_W
155 slopeW = (line.w[1] - line.w[0]) * invDeltaX;
156 #endif
157 #ifdef IPOL_C0
158 slopeC = (line.c[0][1] - line.c[0][0]) * invDeltaX;
159 #endif
160 #ifdef IPOL_T0
161 slopeT[0] = (line.t[0][1] - line.t[0][0]) * invDeltaX;
162 #endif
163 #ifdef IPOL_T1
164 slopeT[1] = (line.t[1][1] - line.t[1][0]) * invDeltaX;
165 #endif
166
167 #ifdef SUBTEXEL
168 subPixel = ( (f32) xStart ) - line.x[0];
169 #ifdef IPOL_Z
170 line.z[0] += slopeZ * subPixel;
171 #endif
172 #ifdef IPOL_W
173 line.w[0] += slopeW * subPixel;
174 #endif
175 #ifdef IPOL_C0
176 line.c[0][0] += slopeC * subPixel;
177 #endif
178 #ifdef IPOL_T0
179 line.t[0][0] += slopeT[0] * subPixel;
180 #endif
181 #ifdef IPOL_T1
182 line.t[1][0] += slopeT[1] * subPixel;
183 #endif
184 #endif
185
186 dst = (tVideoSample*)RenderTarget->lock() + ( line.y * RenderTarget->getDimension().Width ) + xStart;
187
188 #ifdef USE_ZBUFFER
189 z = (fp24*) DepthBuffer->lock() + ( line.y * RenderTarget->getDimension().Width ) + xStart;
190 #endif
191
192
193 #ifdef INVERSE_W
194 f32 inversew;
195 #endif
196
197 tFixPoint tx0, tx1;
198 tFixPoint ty0, ty1;
199
200 tFixPoint r0, g0, b0;
201 tFixPoint r1, g1, b1;
202 tFixPoint r2, g2, b2;
203
204 #ifdef IPOL_C0
205 tFixPoint r3, g3, b3;
206 #endif
207
208 for ( s32 i = 0; i <= dx; i++ )
209 {
210 #ifdef CMP_Z
211 if ( line.z[0] < z[i] )
212 #endif
213 #ifdef CMP_W
214 if ( line.w[0] >= z[i] )
215 #endif
216 {
217 #ifdef INVERSE_W
218 inversew = fix_inverse32 ( line.w[0] );
219
220 tx0 = tofix ( line.t[0][0].x,inversew);
221 ty0 = tofix ( line.t[0][0].y,inversew);
222 tx1 = tofix ( line.t[1][0].x,inversew);
223 ty1 = tofix ( line.t[1][0].y,inversew);
224
225 #ifdef IPOL_C0
226 r3 = tofix ( line.c[0][0].y ,inversew );
227 g3 = tofix ( line.c[0][0].z ,inversew );
228 b3 = tofix ( line.c[0][0].w ,inversew );
229 #endif
230
231 #else
232 tx0 = tofix ( line.t[0][0].x );
233 ty0 = tofix ( line.t[0][0].y );
234 tx1 = tofix ( line.t[1][0].x );
235 ty1 = tofix ( line.t[1][0].y );
236
237 #ifdef IPOL_C0
238 r3 = tofix ( line.c[0][0].y );
239 g3 = tofix ( line.c[0][0].z );
240 b3 = tofix ( line.c[0][0].w );
241 #endif
242
243 #endif
244 getSample_texture ( r0, g0, b0, &IT[0], tx0, ty0 );
245 getSample_texture ( r1, g1, b1, &IT[1], tx1, ty1 );
246
247 #ifdef IPOL_C0
248 r2 = imulFix ( r0, r3 );
249 g2 = imulFix ( g0, g3 );
250 b2 = imulFix ( b0, b3 );
251
252 r2 = clampfix_maxcolor ( imulFix_tex4 ( r2, r1 ) );
253 g2 = clampfix_maxcolor ( imulFix_tex4 ( g2, g1 ) );
254 b2 = clampfix_maxcolor ( imulFix_tex4 ( b2, b1 ) );
255 /*
256 r2 = r3 << 8;
257 g2 = g3 << 8;
258 b2 = b3 << 8;
259 */
260 #else
261 r2 = clampfix_maxcolor ( imulFix_tex4 ( r0, r1 ) );
262 g2 = clampfix_maxcolor ( imulFix_tex4 ( g0, g1 ) );
263 b2 = clampfix_maxcolor ( imulFix_tex4 ( b0, b1 ) );
264 #endif
265
266
267 dst[i] = fix_to_color ( r2, g2, b2 );
268
269 #ifdef WRITE_Z
270 z[i] = line.z[0];
271 #endif
272 #ifdef WRITE_W
273 z[i] = line.w[0];
274 #endif
275 }
276
277 #ifdef IPOL_Z
278 line.z[0] += slopeZ;
279 #endif
280 #ifdef IPOL_W
281 line.w[0] += slopeW;
282 #endif
283 #ifdef IPOL_C0
284 line.c[0][0] += slopeC;
285 #endif
286 #ifdef IPOL_T0
287 line.t[0][0] += slopeT[0];
288 #endif
289 #ifdef IPOL_T1
290 line.t[1][0] += slopeT[1];
291 #endif
292 }
293
294 }
295
drawTriangle(const s4DVertex * a,const s4DVertex * b,const s4DVertex * c)296 void CTRGTextureLightMap2_M4::drawTriangle ( const s4DVertex *a,const s4DVertex *b,const s4DVertex *c )
297 {
298 // sort on height, y
299 if ( F32_A_GREATER_B ( a->Pos.y , b->Pos.y ) ) swapVertexPointer(&a, &b);
300 if ( F32_A_GREATER_B ( b->Pos.y , c->Pos.y ) ) swapVertexPointer(&b, &c);
301 if ( F32_A_GREATER_B ( a->Pos.y , b->Pos.y ) ) swapVertexPointer(&a, &b);
302
303 const f32 ca = c->Pos.y - a->Pos.y;
304 const f32 ba = b->Pos.y - a->Pos.y;
305 const f32 cb = c->Pos.y - b->Pos.y;
306 // calculate delta y of the edges
307 scan.invDeltaY[0] = core::reciprocal( ca );
308 scan.invDeltaY[1] = core::reciprocal( ba );
309 scan.invDeltaY[2] = core::reciprocal( cb );
310
311 if ( F32_LOWER_0 ( scan.invDeltaY[0] ) )
312 return;
313
314 // find if the major edge is left or right aligned
315 f32 temp[4];
316
317 temp[0] = a->Pos.x - c->Pos.x;
318 temp[1] = -ca;
319 temp[2] = b->Pos.x - a->Pos.x;
320 temp[3] = ba;
321
322 scan.left = ( temp[0] * temp[3] - temp[1] * temp[2] ) > 0.f ? 0 : 1;
323 scan.right = 1 - scan.left;
324
325 // calculate slopes for the major edge
326 scan.slopeX[0] = (c->Pos.x - a->Pos.x) * scan.invDeltaY[0];
327 scan.x[0] = a->Pos.x;
328
329 #ifdef IPOL_Z
330 scan.slopeZ[0] = (c->Pos.z - a->Pos.z) * scan.invDeltaY[0];
331 scan.z[0] = a->Pos.z;
332 #endif
333
334 #ifdef IPOL_W
335 scan.slopeW[0] = (c->Pos.w - a->Pos.w) * scan.invDeltaY[0];
336 scan.w[0] = a->Pos.w;
337 #endif
338
339 #ifdef IPOL_C0
340 scan.slopeC[0][0] = (c->Color[0] - a->Color[0]) * scan.invDeltaY[0];
341 scan.c[0][0] = a->Color[0];
342 #endif
343
344 #ifdef IPOL_T0
345 scan.slopeT[0][0] = (c->Tex[0] - a->Tex[0]) * scan.invDeltaY[0];
346 scan.t[0][0] = a->Tex[0];
347 #endif
348
349 #ifdef IPOL_T1
350 scan.slopeT[1][0] = (c->Tex[1] - a->Tex[1]) * scan.invDeltaY[0];
351 scan.t[1][0] = a->Tex[1];
352 #endif
353
354 // top left fill convention y run
355 s32 yStart;
356 s32 yEnd;
357
358 #ifdef SUBTEXEL
359 f32 subPixel;
360 #endif
361
362
363 // rasterize upper sub-triangle
364 //if ( (f32) 0.0 != scan.invDeltaY[1] )
365 if ( F32_GREATER_0 ( scan.invDeltaY[1] ) )
366 {
367 // calculate slopes for top edge
368 scan.slopeX[1] = (b->Pos.x - a->Pos.x) * scan.invDeltaY[1];
369 scan.x[1] = a->Pos.x;
370
371 #ifdef IPOL_Z
372 scan.slopeZ[1] = (b->Pos.z - a->Pos.z) * scan.invDeltaY[1];
373 scan.z[1] = a->Pos.z;
374 #endif
375
376 #ifdef IPOL_W
377 scan.slopeW[1] = (b->Pos.w - a->Pos.w) * scan.invDeltaY[1];
378 scan.w[1] = a->Pos.w;
379 #endif
380
381 #ifdef IPOL_C0
382 scan.slopeC[0][1] = (b->Color[0] - a->Color[0]) * scan.invDeltaY[1];
383 scan.c[0][1] = a->Color[0];
384 #endif
385
386 #ifdef IPOL_T0
387 scan.slopeT[0][1] = (b->Tex[0] - a->Tex[0]) * scan.invDeltaY[1];
388 scan.t[0][1] = a->Tex[0];
389 #endif
390
391 #ifdef IPOL_T1
392 scan.slopeT[1][1] = (b->Tex[1] - a->Tex[1]) * scan.invDeltaY[1];
393 scan.t[1][1] = a->Tex[1];
394 #endif
395
396 // apply top-left fill convention, top part
397 yStart = core::ceil32( a->Pos.y );
398 yEnd = core::ceil32( b->Pos.y ) - 1;
399
400 #ifdef SUBTEXEL
401 subPixel = ( (f32) yStart ) - a->Pos.y;
402
403 // correct to pixel center
404 scan.x[0] += scan.slopeX[0] * subPixel;
405 scan.x[1] += scan.slopeX[1] * subPixel;
406
407 #ifdef IPOL_Z
408 scan.z[0] += scan.slopeZ[0] * subPixel;
409 scan.z[1] += scan.slopeZ[1] * subPixel;
410 #endif
411
412 #ifdef IPOL_W
413 scan.w[0] += scan.slopeW[0] * subPixel;
414 scan.w[1] += scan.slopeW[1] * subPixel;
415 #endif
416
417 #ifdef IPOL_C0
418 scan.c[0][0] += scan.slopeC[0][0] * subPixel;
419 scan.c[0][1] += scan.slopeC[0][1] * subPixel;
420 #endif
421
422 #ifdef IPOL_T0
423 scan.t[0][0] += scan.slopeT[0][0] * subPixel;
424 scan.t[0][1] += scan.slopeT[0][1] * subPixel;
425 #endif
426
427 #ifdef IPOL_T1
428 scan.t[1][0] += scan.slopeT[1][0] * subPixel;
429 scan.t[1][1] += scan.slopeT[1][1] * subPixel;
430 #endif
431
432 #endif
433
434 // rasterize the edge scanlines
435 for( line.y = yStart; line.y <= yEnd; ++line.y)
436 {
437 line.x[scan.left] = scan.x[0];
438 line.x[scan.right] = scan.x[1];
439
440 #ifdef IPOL_Z
441 line.z[scan.left] = scan.z[0];
442 line.z[scan.right] = scan.z[1];
443 #endif
444
445 #ifdef IPOL_W
446 line.w[scan.left] = scan.w[0];
447 line.w[scan.right] = scan.w[1];
448 #endif
449
450 #ifdef IPOL_C0
451 line.c[0][scan.left] = scan.c[0][0];
452 line.c[0][scan.right] = scan.c[0][1];
453 #endif
454
455 #ifdef IPOL_T0
456 line.t[0][scan.left] = scan.t[0][0];
457 line.t[0][scan.right] = scan.t[0][1];
458 #endif
459
460 #ifdef IPOL_T1
461 line.t[1][scan.left] = scan.t[1][0];
462 line.t[1][scan.right] = scan.t[1][1];
463 #endif
464
465 // render a scanline
466 scanline_bilinear ();
467
468 scan.x[0] += scan.slopeX[0];
469 scan.x[1] += scan.slopeX[1];
470
471 #ifdef IPOL_Z
472 scan.z[0] += scan.slopeZ[0];
473 scan.z[1] += scan.slopeZ[1];
474 #endif
475
476 #ifdef IPOL_W
477 scan.w[0] += scan.slopeW[0];
478 scan.w[1] += scan.slopeW[1];
479 #endif
480
481 #ifdef IPOL_C0
482 scan.c[0][0] += scan.slopeC[0][0];
483 scan.c[0][1] += scan.slopeC[0][1];
484 #endif
485
486 #ifdef IPOL_T0
487 scan.t[0][0] += scan.slopeT[0][0];
488 scan.t[0][1] += scan.slopeT[0][1];
489 #endif
490
491 #ifdef IPOL_T1
492 scan.t[1][0] += scan.slopeT[1][0];
493 scan.t[1][1] += scan.slopeT[1][1];
494 #endif
495
496 }
497 }
498
499 // rasterize lower sub-triangle
500 //if ( (f32) 0.0 != scan.invDeltaY[2] )
501 if ( F32_GREATER_0 ( scan.invDeltaY[2] ) )
502 {
503 // advance to middle point
504 //if( (f32) 0.0 != scan.invDeltaY[1] )
505 if ( F32_GREATER_0 ( scan.invDeltaY[1] ) )
506 {
507 temp[0] = b->Pos.y - a->Pos.y; // dy
508
509 scan.x[0] = a->Pos.x + scan.slopeX[0] * temp[0];
510 #ifdef IPOL_Z
511 scan.z[0] = a->Pos.z + scan.slopeZ[0] * temp[0];
512 #endif
513 #ifdef IPOL_W
514 scan.w[0] = a->Pos.w + scan.slopeW[0] * temp[0];
515 #endif
516 #ifdef IPOL_C0
517 scan.c[0][0] = a->Color[0] + scan.slopeC[0][0] * temp[0];
518 #endif
519 #ifdef IPOL_T0
520 scan.t[0][0] = a->Tex[0] + scan.slopeT[0][0] * temp[0];
521 #endif
522 #ifdef IPOL_T1
523 scan.t[1][0] = a->Tex[1] + scan.slopeT[1][0] * temp[0];
524 #endif
525
526 }
527
528 // calculate slopes for bottom edge
529 scan.slopeX[1] = (c->Pos.x - b->Pos.x) * scan.invDeltaY[2];
530 scan.x[1] = b->Pos.x;
531
532 #ifdef IPOL_Z
533 scan.slopeZ[1] = (c->Pos.z - b->Pos.z) * scan.invDeltaY[2];
534 scan.z[1] = b->Pos.z;
535 #endif
536
537 #ifdef IPOL_W
538 scan.slopeW[1] = (c->Pos.w - b->Pos.w) * scan.invDeltaY[2];
539 scan.w[1] = b->Pos.w;
540 #endif
541
542 #ifdef IPOL_C0
543 scan.slopeC[0][1] = (c->Color[0] - b->Color[0]) * scan.invDeltaY[2];
544 scan.c[0][1] = b->Color[0];
545 #endif
546
547 #ifdef IPOL_T0
548 scan.slopeT[0][1] = (c->Tex[0] - b->Tex[0]) * scan.invDeltaY[2];
549 scan.t[0][1] = b->Tex[0];
550 #endif
551
552 #ifdef IPOL_T1
553 scan.slopeT[1][1] = (c->Tex[1] - b->Tex[1]) * scan.invDeltaY[2];
554 scan.t[1][1] = b->Tex[1];
555 #endif
556
557 // apply top-left fill convention, top part
558 yStart = core::ceil32( b->Pos.y );
559 yEnd = core::ceil32( c->Pos.y ) - 1;
560
561 #ifdef SUBTEXEL
562
563 subPixel = ( (f32) yStart ) - b->Pos.y;
564
565 // correct to pixel center
566 scan.x[0] += scan.slopeX[0] * subPixel;
567 scan.x[1] += scan.slopeX[1] * subPixel;
568
569 #ifdef IPOL_Z
570 scan.z[0] += scan.slopeZ[0] * subPixel;
571 scan.z[1] += scan.slopeZ[1] * subPixel;
572 #endif
573
574 #ifdef IPOL_W
575 scan.w[0] += scan.slopeW[0] * subPixel;
576 scan.w[1] += scan.slopeW[1] * subPixel;
577 #endif
578
579 #ifdef IPOL_C0
580 scan.c[0][0] += scan.slopeC[0][0] * subPixel;
581 scan.c[0][1] += scan.slopeC[0][1] * subPixel;
582 #endif
583
584 #ifdef IPOL_T0
585 scan.t[0][0] += scan.slopeT[0][0] * subPixel;
586 scan.t[0][1] += scan.slopeT[0][1] * subPixel;
587 #endif
588
589 #ifdef IPOL_T1
590 scan.t[1][0] += scan.slopeT[1][0] * subPixel;
591 scan.t[1][1] += scan.slopeT[1][1] * subPixel;
592 #endif
593
594 #endif
595
596 // rasterize the edge scanlines
597 for( line.y = yStart; line.y <= yEnd; ++line.y)
598 {
599 line.x[scan.left] = scan.x[0];
600 line.x[scan.right] = scan.x[1];
601
602 #ifdef IPOL_Z
603 line.z[scan.left] = scan.z[0];
604 line.z[scan.right] = scan.z[1];
605 #endif
606
607 #ifdef IPOL_W
608 line.w[scan.left] = scan.w[0];
609 line.w[scan.right] = scan.w[1];
610 #endif
611
612 #ifdef IPOL_C0
613 line.c[0][scan.left] = scan.c[0][0];
614 line.c[0][scan.right] = scan.c[0][1];
615 #endif
616
617 #ifdef IPOL_T0
618 line.t[0][scan.left] = scan.t[0][0];
619 line.t[0][scan.right] = scan.t[0][1];
620 #endif
621
622 #ifdef IPOL_T1
623 line.t[1][scan.left] = scan.t[1][0];
624 line.t[1][scan.right] = scan.t[1][1];
625 #endif
626
627 // render a scanline
628 scanline_bilinear ();
629
630 scan.x[0] += scan.slopeX[0];
631 scan.x[1] += scan.slopeX[1];
632
633 #ifdef IPOL_Z
634 scan.z[0] += scan.slopeZ[0];
635 scan.z[1] += scan.slopeZ[1];
636 #endif
637
638 #ifdef IPOL_W
639 scan.w[0] += scan.slopeW[0];
640 scan.w[1] += scan.slopeW[1];
641 #endif
642
643 #ifdef IPOL_C0
644 scan.c[0][0] += scan.slopeC[0][0];
645 scan.c[0][1] += scan.slopeC[0][1];
646 #endif
647
648 #ifdef IPOL_T0
649 scan.t[0][0] += scan.slopeT[0][0];
650 scan.t[0][1] += scan.slopeT[0][1];
651 #endif
652
653 #ifdef IPOL_T1
654 scan.t[1][0] += scan.slopeT[1][0];
655 scan.t[1][1] += scan.slopeT[1][1];
656 #endif
657
658 }
659 }
660
661 }
662
663
664 } // end namespace video
665 } // end namespace irr
666
667 #endif // _IRR_COMPILE_WITH_BURNINGSVIDEO_
668
669 namespace irr
670 {
671 namespace video
672 {
673
674
675 //! creates a flat triangle renderer
createTriangleRendererGTextureLightMap2_M4(CBurningVideoDriver * driver)676 IBurningShader* createTriangleRendererGTextureLightMap2_M4(CBurningVideoDriver* driver)
677 {
678 #ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
679 return new CTRGTextureLightMap2_M4(driver);
680 #else
681 return 0;
682 #endif // _IRR_COMPILE_WITH_BURNINGSVIDEO_
683 }
684
685
686 } // end namespace video
687 } // end namespace irr
688
689
690
691