S4DVertex.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. #ifndef __S_4D_VERTEX_H_INCLUDED__
  5. #define __S_4D_VERTEX_H_INCLUDED__
  6. #include "SoftwareDriver2_compile_config.h"
  7. #include "SoftwareDriver2_helper.h"
  8. #include "irrAllocator.h"
  9. namespace irr
  10. {
  11. namespace video
  12. {
  13. struct sVec2
  14. {
  15. f32 x;
  16. f32 y;
  17. sVec2 () {}
  18. sVec2 ( f32 s) : x ( s ), y ( s ) {}
  19. sVec2 ( f32 _x, f32 _y )
  20. : x ( _x ), y ( _y ) {}
  21. void set ( f32 _x, f32 _y )
  22. {
  23. x = _x;
  24. y = _y;
  25. }
  26. // f = a * t + b * ( 1 - t )
  27. void interpolate(const sVec2& a, const sVec2& b, const f32 t)
  28. {
  29. x = b.x + ( ( a.x - b.x ) * t );
  30. y = b.y + ( ( a.y - b.y ) * t );
  31. }
  32. sVec2 operator-(const sVec2& other) const
  33. {
  34. return sVec2(x - other.x, y - other.y);
  35. }
  36. sVec2 operator+(const sVec2& other) const
  37. {
  38. return sVec2(x + other.x, y + other.y);
  39. }
  40. void operator+=(const sVec2& other)
  41. {
  42. x += other.x;
  43. y += other.y;
  44. }
  45. sVec2 operator*(const f32 s) const
  46. {
  47. return sVec2(x * s , y * s);
  48. }
  49. void operator*=( const f32 s)
  50. {
  51. x *= s;
  52. y *= s;
  53. }
  54. void operator=(const sVec2& other)
  55. {
  56. x = other.x;
  57. y = other.y;
  58. }
  59. };
  60. // A8R8G8B8
  61. struct sVec4;
  62. struct sCompressedVec4
  63. {
  64. u32 argb;
  65. void setA8R8G8B8 ( u32 value )
  66. {
  67. argb = value;
  68. }
  69. void setColorf ( const video::SColorf & color )
  70. {
  71. argb = core::floor32 ( color.a * 255.f ) << 24 |
  72. core::floor32 ( color.r * 255.f ) << 16 |
  73. core::floor32 ( color.g * 255.f ) << 8 |
  74. core::floor32 ( color.b * 255.f );
  75. }
  76. void setVec4 ( const sVec4 & v );
  77. // f = a * t + b * ( 1 - t )
  78. void interpolate(const sCompressedVec4& a, const sCompressedVec4& b, const f32 t)
  79. {
  80. argb = PixelBlend32 ( b.argb, a.argb, core::floor32 ( t * 256.f ) );
  81. }
  82. };
  83. struct sVec4
  84. {
  85. union
  86. {
  87. struct { f32 x, y, z, w; };
  88. struct { f32 a, r, g, b; };
  89. // struct { sVec2 xy, zw; }; // sorry, this does not compile with gcc
  90. };
  91. sVec4 () {}
  92. sVec4 ( f32 s) : x ( s ), y ( s ), z ( s ), w ( s ) {}
  93. sVec4 ( f32 _x, f32 _y, f32 _z, f32 _w )
  94. : x ( _x ), y ( _y ), z( _z ), w ( _w ){}
  95. void set ( f32 _x, f32 _y, f32 _z, f32 _w )
  96. {
  97. x = _x;
  98. y = _y;
  99. z = _z;
  100. w = _w;
  101. }
  102. void setA8R8G8B8 ( u32 argb )
  103. {
  104. x = ( ( argb & 0xFF000000 ) >> 24 ) * ( 1.f / 255.f );
  105. y = ( ( argb & 0x00FF0000 ) >> 16 ) * ( 1.f / 255.f );
  106. z = ( ( argb & 0x0000FF00 ) >> 8 ) * ( 1.f / 255.f );
  107. w = ( ( argb & 0x000000FF ) ) * ( 1.f / 255.f );
  108. }
  109. void setColorf ( const video::SColorf & color )
  110. {
  111. x = color.a;
  112. y = color.r;
  113. z = color.g;
  114. w = color.b;
  115. }
  116. // f = a * t + b * ( 1 - t )
  117. void interpolate(const sVec4& a, const sVec4& b, const f32 t)
  118. {
  119. x = b.x + ( ( a.x - b.x ) * t );
  120. y = b.y + ( ( a.y - b.y ) * t );
  121. z = b.z + ( ( a.z - b.z ) * t );
  122. w = b.w + ( ( a.w - b.w ) * t );
  123. }
  124. f32 dotProduct(const sVec4& other) const
  125. {
  126. return x*other.x + y*other.y + z*other.z + w*other.w;
  127. }
  128. f32 dot_xyz( const sVec4& other) const
  129. {
  130. return x*other.x + y*other.y + z*other.z;
  131. }
  132. f32 get_length_xyz_square () const
  133. {
  134. return x * x + y * y + z * z;
  135. }
  136. f32 get_length_xyz () const
  137. {
  138. return core::squareroot ( x * x + y * y + z * z );
  139. }
  140. void normalize_xyz ()
  141. {
  142. const f32 l = core::reciprocal_squareroot ( x * x + y * y + z * z );
  143. x *= l;
  144. y *= l;
  145. z *= l;
  146. }
  147. void project_xyz ()
  148. {
  149. w = core::reciprocal ( w );
  150. x *= w;
  151. y *= w;
  152. z *= w;
  153. }
  154. sVec4 operator-(const sVec4& other) const
  155. {
  156. return sVec4(x - other.x, y - other.y, z - other.z,w - other.w);
  157. }
  158. sVec4 operator+(const sVec4& other) const
  159. {
  160. return sVec4(x + other.x, y + other.y, z + other.z,w + other.w);
  161. }
  162. void operator+=(const sVec4& other)
  163. {
  164. x += other.x;
  165. y += other.y;
  166. z += other.z;
  167. w += other.w;
  168. }
  169. sVec4 operator*(const f32 s) const
  170. {
  171. return sVec4(x * s , y * s, z * s,w * s);
  172. }
  173. sVec4 operator*(const sVec4 &other) const
  174. {
  175. return sVec4(x * other.x , y * other.y, z * other.z,w * other.w);
  176. }
  177. void mulReciprocal ( f32 s )
  178. {
  179. const f32 i = core::reciprocal ( s );
  180. x = (f32) ( x * i );
  181. y = (f32) ( y * i );
  182. z = (f32) ( z * i );
  183. w = (f32) ( w * i );
  184. }
  185. void mul ( const f32 s )
  186. {
  187. x *= s;
  188. y *= s;
  189. z *= s;
  190. w *= s;
  191. }
  192. /*
  193. void operator*=(f32 s)
  194. {
  195. x *= s;
  196. y *= s;
  197. z *= s;
  198. w *= s;
  199. }
  200. */
  201. void operator*=(const sVec4 &other)
  202. {
  203. x *= other.x;
  204. y *= other.y;
  205. z *= other.z;
  206. w *= other.w;
  207. }
  208. void operator=(const sVec4& other)
  209. {
  210. x = other.x;
  211. y = other.y;
  212. z = other.z;
  213. w = other.w;
  214. }
  215. };
  216. struct sVec3
  217. {
  218. union
  219. {
  220. struct { f32 r, g, b; };
  221. struct { f32 x, y, z; };
  222. };
  223. sVec3 () {}
  224. sVec3 ( f32 _x, f32 _y, f32 _z )
  225. : r ( _x ), g ( _y ), b( _z ) {}
  226. sVec3 ( const sVec4 &v )
  227. : r ( v.x ), g ( v.y ), b( v.z ) {}
  228. void set ( f32 _r, f32 _g, f32 _b )
  229. {
  230. r = _r;
  231. g = _g;
  232. b = _b;
  233. }
  234. void setR8G8B8 ( u32 argb )
  235. {
  236. r = ( ( argb & 0x00FF0000 ) >> 16 ) * ( 1.f / 255.f );
  237. g = ( ( argb & 0x0000FF00 ) >> 8 ) * ( 1.f / 255.f );
  238. b = ( ( argb & 0x000000FF ) ) * ( 1.f / 255.f );
  239. }
  240. void setColorf ( const video::SColorf & color )
  241. {
  242. r = color.r;
  243. g = color.g;
  244. b = color.b;
  245. }
  246. void add (const sVec3& other)
  247. {
  248. r += other.r;
  249. g += other.g;
  250. b += other.b;
  251. }
  252. void mulAdd(const sVec3& other, const f32 v)
  253. {
  254. r += other.r * v;
  255. g += other.g * v;
  256. b += other.b * v;
  257. }
  258. void mulAdd(const sVec3& v0, const sVec3& v1)
  259. {
  260. r += v0.r * v1.r;
  261. g += v0.g * v1.g;
  262. b += v0.b * v1.b;
  263. }
  264. void saturate ( sVec4 &dest, u32 argb )
  265. {
  266. dest.x = ( ( argb & 0xFF000000 ) >> 24 ) * ( 1.f / 255.f );
  267. dest.y = core::min_ ( r, 1.f );
  268. dest.z = core::min_ ( g, 1.f );
  269. dest.w = core::min_ ( b, 1.f );
  270. }
  271. // f = a * t + b * ( 1 - t )
  272. void interpolate(const sVec3& v0, const sVec3& v1, const f32 t)
  273. {
  274. r = v1.r + ( ( v0.r - v1.r ) * t );
  275. g = v1.g + ( ( v0.g - v1.g ) * t );
  276. b = v1.b + ( ( v0.b - v1.b ) * t );
  277. }
  278. sVec3 operator-(const sVec3& other) const
  279. {
  280. return sVec3(r - other.r, b - other.b, g - other.g);
  281. }
  282. sVec3 operator+(const sVec3& other) const
  283. {
  284. return sVec3(r + other.r, g + other.g, b + other.b);
  285. }
  286. sVec3 operator*(const f32 s) const
  287. {
  288. return sVec3(r * s , g * s, b * s);
  289. }
  290. sVec3 operator/(const f32 s) const
  291. {
  292. f32 inv = 1.f / s;
  293. return sVec3(r * inv , g * inv, b * inv);
  294. }
  295. sVec3 operator*(const sVec3 &other) const
  296. {
  297. return sVec3(r * other.r , b * other.b, g * other.g);
  298. }
  299. void operator+=(const sVec3& other)
  300. {
  301. r += other.r;
  302. g += other.g;
  303. b += other.b;
  304. }
  305. void setLength ( f32 len )
  306. {
  307. const f32 l = len * core::reciprocal_squareroot ( r * r + g * g + b * b );
  308. r *= l;
  309. g *= l;
  310. b *= l;
  311. }
  312. };
  313. inline void sCompressedVec4::setVec4 ( const sVec4 & v )
  314. {
  315. argb = core::floor32 ( v.x * 255.f ) << 24 |
  316. core::floor32 ( v.y * 255.f ) << 16 |
  317. core::floor32 ( v.z * 255.f ) << 8 |
  318. core::floor32 ( v.w * 255.f );
  319. }
  320. enum e4DVertexFlag
  321. {
  322. VERTEX4D_INSIDE = 0x0000003F,
  323. VERTEX4D_CLIPMASK = 0x0000003F,
  324. VERTEX4D_PROJECTED = 0x00000100,
  325. VERTEX4D_FORMAT_MASK = 0xFFFF0000,
  326. VERTEX4D_FORMAT_MASK_TEXTURE = 0x000F0000,
  327. VERTEX4D_FORMAT_TEXTURE_1 = 0x00010000,
  328. VERTEX4D_FORMAT_TEXTURE_2 = 0x00020000,
  329. VERTEX4D_FORMAT_TEXTURE_3 = 0x00030000,
  330. VERTEX4D_FORMAT_TEXTURE_4 = 0x00040000,
  331. VERTEX4D_FORMAT_MASK_COLOR = 0x00F00000,
  332. VERTEX4D_FORMAT_COLOR_1 = 0x00100000,
  333. VERTEX4D_FORMAT_COLOR_2 = 0x00200000,
  334. VERTEX4D_FORMAT_MASK_BUMP = 0x0F000000,
  335. VERTEX4D_FORMAT_BUMP_DOT3 = 0x01000000,
  336. };
  337. const u32 MATERIAL_MAX_COLORS = 1;
  338. const u32 BURNING_MATERIAL_MAX_TEXTURES = 2;
  339. const u32 BURNING_MATERIAL_MAX_TANGENT = 1;
  340. // dummy Vertex. used for calculation vertex memory size
  341. struct s4DVertex_proxy
  342. {
  343. u32 flag;
  344. sVec4 Pos;
  345. sVec2 Tex[BURNING_MATERIAL_MAX_TEXTURES];
  346. #ifdef SOFTWARE_DRIVER_2_USE_VERTEX_COLOR
  347. sVec4 Color[MATERIAL_MAX_COLORS];
  348. #endif
  349. sVec3 LightTangent[BURNING_MATERIAL_MAX_TANGENT];
  350. };
  351. #define SIZEOF_SVERTEX 64
  352. #define SIZEOF_SVERTEX_LOG2 6
  353. /*!
  354. Internal BurningVideo Vertex
  355. */
  356. struct s4DVertex
  357. {
  358. u32 flag;
  359. sVec4 Pos;
  360. sVec2 Tex[ BURNING_MATERIAL_MAX_TEXTURES ];
  361. #ifdef SOFTWARE_DRIVER_2_USE_VERTEX_COLOR
  362. sVec4 Color[ MATERIAL_MAX_COLORS ];
  363. #endif
  364. sVec3 LightTangent[BURNING_MATERIAL_MAX_TANGENT];
  365. //u8 fill [ SIZEOF_SVERTEX - sizeof (s4DVertex_proxy) ];
  366. // f = a * t + b * ( 1 - t )
  367. void interpolate(const s4DVertex& b, const s4DVertex& a, const f32 t)
  368. {
  369. u32 i;
  370. u32 size;
  371. Pos.interpolate ( a.Pos, b.Pos, t );
  372. #ifdef SOFTWARE_DRIVER_2_USE_VERTEX_COLOR
  373. size = (flag & VERTEX4D_FORMAT_MASK_COLOR) >> 20;
  374. for ( i = 0; i!= size; ++i )
  375. {
  376. Color[i].interpolate ( a.Color[i], b.Color[i], t );
  377. }
  378. #endif
  379. size = (flag & VERTEX4D_FORMAT_MASK_TEXTURE) >> 16;
  380. for ( i = 0; i!= size; ++i )
  381. {
  382. Tex[i].interpolate ( a.Tex[i], b.Tex[i], t );
  383. }
  384. size = (flag & VERTEX4D_FORMAT_MASK_BUMP) >> 24;
  385. for ( i = 0; i!= size; ++i )
  386. {
  387. LightTangent[i].interpolate ( a.LightTangent[i], b.LightTangent[i], t );
  388. }
  389. }
  390. };
  391. // ----------------- Vertex Cache ---------------------------
  392. struct SAlignedVertex
  393. {
  394. SAlignedVertex ( u32 element, u32 aligned )
  395. : ElementSize ( element )
  396. {
  397. u32 byteSize = (ElementSize << SIZEOF_SVERTEX_LOG2 ) + aligned;
  398. mem = new u8 [ byteSize ];
  399. data = (s4DVertex*) mem;
  400. }
  401. virtual ~SAlignedVertex ()
  402. {
  403. delete [] mem;
  404. }
  405. s4DVertex *data;
  406. u8 *mem;
  407. u32 ElementSize;
  408. };
  409. // hold info for different Vertex Types
  410. struct SVSize
  411. {
  412. u32 Format;
  413. u32 Pitch;
  414. u32 TexSize;
  415. };
  416. // a cache info
  417. struct SCacheInfo
  418. {
  419. u32 index;
  420. u32 hit;
  421. };
  422. #define VERTEXCACHE_ELEMENT 16
  423. #define VERTEXCACHE_MISS 0xFFFFFFFF
  424. struct SVertexCache
  425. {
  426. SVertexCache (): mem ( VERTEXCACHE_ELEMENT * 2, 128 ) {}
  427. SCacheInfo info[VERTEXCACHE_ELEMENT];
  428. // Transformed and lite, clipping state
  429. // + Clipped, Projected
  430. SAlignedVertex mem;
  431. // source
  432. const void* vertices;
  433. u32 vertexCount;
  434. const void* indices;
  435. u32 indexCount;
  436. u32 indicesIndex;
  437. u32 indicesRun;
  438. // primitives consist of x vertices
  439. u32 primitivePitch;
  440. u32 vType; //E_VERTEX_TYPE
  441. u32 pType; //scene::E_PRIMITIVE_TYPE
  442. u32 iType; //E_INDEX_TYPE iType
  443. };
  444. // swap 2 pointer
  445. REALINLINE void swapVertexPointer(const s4DVertex** v1, const s4DVertex** v2)
  446. {
  447. const s4DVertex* b = *v1;
  448. *v1 = *v2;
  449. *v2 = b;
  450. }
  451. // ------------------------ Internal Scanline Rasterizer -----------------------------
  452. // internal scan convert
  453. struct sScanConvertData
  454. {
  455. u8 left; // major edge left/right
  456. u8 right; // !left
  457. f32 invDeltaY[3]; // inverse edge delta y
  458. f32 x[2]; // x coordinate
  459. f32 slopeX[2]; // x slope along edges
  460. #if defined ( SOFTWARE_DRIVER_2_USE_WBUFFER ) || defined ( SOFTWARE_DRIVER_2_PERSPECTIVE_CORRECT )
  461. f32 w[2]; // w coordinate
  462. fp24 slopeW[2]; // w slope along edges
  463. #else
  464. f32 z[2]; // z coordinate
  465. f32 slopeZ[2]; // z slope along edges
  466. #endif
  467. sVec4 c[MATERIAL_MAX_COLORS][2]; // color
  468. sVec4 slopeC[MATERIAL_MAX_COLORS][2]; // color slope along edges
  469. sVec2 t[BURNING_MATERIAL_MAX_TEXTURES][2]; // texture
  470. sVec2 slopeT[BURNING_MATERIAL_MAX_TEXTURES][2]; // texture slope along edges
  471. sVec3 l[BURNING_MATERIAL_MAX_TANGENT][2]; // Light Tangent
  472. sVec3 slopeL[BURNING_MATERIAL_MAX_TEXTURES][2]; // tanget slope along edges
  473. };
  474. // passed to scan Line
  475. struct sScanLineData
  476. {
  477. s32 y; // y position of scanline
  478. f32 x[2]; // x start, x end of scanline
  479. #if defined ( SOFTWARE_DRIVER_2_USE_WBUFFER ) || defined ( SOFTWARE_DRIVER_2_PERSPECTIVE_CORRECT )
  480. f32 w[2]; // w start, w end of scanline
  481. #else
  482. f32 z[2]; // z start, z end of scanline
  483. #endif
  484. #ifdef SOFTWARE_DRIVER_2_USE_VERTEX_COLOR
  485. sVec4 c[MATERIAL_MAX_COLORS][2]; // color start, color end of scanline
  486. #endif
  487. sVec2 t[BURNING_MATERIAL_MAX_TEXTURES][2]; // texture start, texture end of scanline
  488. sVec3 l[BURNING_MATERIAL_MAX_TANGENT][2]; // Light Tangent start, end
  489. };
  490. // passed to pixel Shader
  491. struct sPixelShaderData
  492. {
  493. tVideoSample *dst;
  494. fp24 *z;
  495. s32 xStart;
  496. s32 xEnd;
  497. s32 dx;
  498. s32 i;
  499. };
  500. /*
  501. load a color value
  502. */
  503. inline void getTexel_plain2 ( tFixPoint &r, tFixPoint &g, tFixPoint &b,
  504. const sVec4 &v
  505. )
  506. {
  507. r = tofix ( v.y );
  508. g = tofix ( v.z );
  509. b = tofix ( v.w );
  510. }
  511. /*
  512. load a color value
  513. */
  514. inline void getSample_color ( tFixPoint &a, tFixPoint &r, tFixPoint &g, tFixPoint &b,
  515. const sVec4 &v
  516. )
  517. {
  518. a = tofix ( v.x );
  519. r = tofix ( v.y, COLOR_MAX * FIX_POINT_F32_MUL);
  520. g = tofix ( v.z, COLOR_MAX * FIX_POINT_F32_MUL);
  521. b = tofix ( v.w, COLOR_MAX * FIX_POINT_F32_MUL);
  522. }
  523. /*
  524. load a color value
  525. */
  526. inline void getSample_color ( tFixPoint &r, tFixPoint &g, tFixPoint &b,const sVec4 &v )
  527. {
  528. r = tofix ( v.y, COLOR_MAX * FIX_POINT_F32_MUL);
  529. g = tofix ( v.z, COLOR_MAX * FIX_POINT_F32_MUL);
  530. b = tofix ( v.w, COLOR_MAX * FIX_POINT_F32_MUL);
  531. }
  532. /*
  533. load a color value
  534. */
  535. inline void getSample_color ( tFixPoint &r, tFixPoint &g, tFixPoint &b,
  536. const sVec4 &v, const f32 mulby )
  537. {
  538. r = tofix ( v.y, mulby);
  539. g = tofix ( v.z, mulby);
  540. b = tofix ( v.w, mulby);
  541. }
  542. }
  543. }
  544. #endif