Clipper.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // Copyright 2009 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. /*
  5. Portions of this file are based off work by Markus Trenkwalder.
  6. Copyright (c) 2007, 2008 Markus Trenkwalder
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions are met:
  10. * Redistributions of source code must retain the above copyright notice,
  11. this list of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. * Neither the name of the library's copyright owner nor the names of its
  16. contributors may be used to endorse or promote products derived from this
  17. software without specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "Common/ChunkFile.h"
  31. #include "VideoBackends/Software/BPMemLoader.h"
  32. #include "VideoBackends/Software/Clipper.h"
  33. #include "VideoBackends/Software/NativeVertexFormat.h"
  34. #include "VideoBackends/Software/Rasterizer.h"
  35. #include "VideoBackends/Software/SWStatistics.h"
  36. #include "VideoBackends/Software/XFMemLoader.h"
  37. namespace Clipper
  38. {
  39. enum
  40. {
  41. NUM_CLIPPED_VERTICES = 33,
  42. NUM_INDICES = NUM_CLIPPED_VERTICES + 3
  43. };
  44. static float m_ViewOffset[2];
  45. static OutputVertexData ClippedVertices[NUM_CLIPPED_VERTICES];
  46. static OutputVertexData *Vertices[NUM_INDICES];
  47. void DoState(PointerWrap &p)
  48. {
  49. p.DoArray(m_ViewOffset,2);
  50. for (auto& ClippedVertice : ClippedVertices)
  51. ClippedVertice.DoState(p);
  52. }
  53. void Init()
  54. {
  55. for (int i = 0; i < NUM_CLIPPED_VERTICES; ++i)
  56. Vertices[i+3] = &ClippedVertices[i];
  57. }
  58. void SetViewOffset()
  59. {
  60. m_ViewOffset[0] = xfmem.viewport.xOrig - 342;
  61. m_ViewOffset[1] = xfmem.viewport.yOrig - 342;
  62. }
  63. enum
  64. {
  65. SKIP_FLAG = -1,
  66. CLIP_POS_X_BIT = 0x01,
  67. CLIP_NEG_X_BIT = 0x02,
  68. CLIP_POS_Y_BIT = 0x04,
  69. CLIP_NEG_Y_BIT = 0x08,
  70. CLIP_POS_Z_BIT = 0x10,
  71. CLIP_NEG_Z_BIT = 0x20
  72. };
  73. static inline int CalcClipMask(OutputVertexData *v)
  74. {
  75. int cmask = 0;
  76. Vec4 pos = v->projectedPosition;
  77. if (pos.w - pos.x < 0)
  78. cmask |= CLIP_POS_X_BIT;
  79. if (pos.x + pos.w < 0)
  80. cmask |= CLIP_NEG_X_BIT;
  81. if (pos.w - pos.y < 0)
  82. cmask |= CLIP_POS_Y_BIT;
  83. if (pos.y + pos.w < 0)
  84. cmask |= CLIP_NEG_Y_BIT;
  85. if (pos.w * pos.z > 0)
  86. cmask |= CLIP_POS_Z_BIT;
  87. if (pos.z + pos.w < 0)
  88. cmask |= CLIP_NEG_Z_BIT;
  89. return cmask;
  90. }
  91. static inline void AddInterpolatedVertex(float t, int out, int in, int* numVertices)
  92. {
  93. Vertices[(*numVertices)++]->Lerp(t, Vertices[out], Vertices[in]);
  94. }
  95. #define DIFFERENT_SIGNS(x,y) ((x <= 0 && y > 0) || (x > 0 && y <= 0))
  96. #define CLIP_DOTPROD(I, A, B, C, D) \
  97. (Vertices[I]->projectedPosition.x * A + Vertices[I]->projectedPosition.y * B + Vertices[I]->projectedPosition.z * C + Vertices[I]->projectedPosition.w * D)
  98. #define POLY_CLIP( PLANE_BIT, A, B, C, D ) \
  99. { \
  100. if (mask & PLANE_BIT) { \
  101. int idxPrev = inlist[0]; \
  102. float dpPrev = CLIP_DOTPROD(idxPrev, A, B, C, D ); \
  103. int outcount = 0; \
  104. \
  105. inlist[n] = inlist[0]; \
  106. for (int j = 1; j <= n; j++) { \
  107. int idx = inlist[j]; \
  108. float dp = CLIP_DOTPROD(idx, A, B, C, D ); \
  109. if (dpPrev >= 0) { \
  110. outlist[outcount++] = idxPrev; \
  111. } \
  112. \
  113. if (DIFFERENT_SIGNS(dp, dpPrev)) { \
  114. if (dp < 0) { \
  115. float t = dp / (dp - dpPrev); \
  116. AddInterpolatedVertex(t, idx, idxPrev, &numVertices); \
  117. } else { \
  118. float t = dpPrev / (dpPrev - dp); \
  119. AddInterpolatedVertex(t, idxPrev, idx, &numVertices); \
  120. } \
  121. outlist[outcount++] = numVertices - 1; \
  122. } \
  123. \
  124. idxPrev = idx; \
  125. dpPrev = dp; \
  126. } \
  127. \
  128. if (outcount < 3) \
  129. continue; \
  130. \
  131. { \
  132. int *tmp = inlist; \
  133. inlist = outlist; \
  134. outlist = tmp; \
  135. n = outcount; \
  136. } \
  137. } \
  138. }
  139. #define LINE_CLIP(PLANE_BIT, A, B, C, D ) \
  140. { \
  141. if (mask & PLANE_BIT) { \
  142. const float dp0 = CLIP_DOTPROD( 0, A, B, C, D ); \
  143. const float dp1 = CLIP_DOTPROD( 1, A, B, C, D ); \
  144. const bool neg_dp0 = dp0 < 0; \
  145. const bool neg_dp1 = dp1 < 0; \
  146. \
  147. if (neg_dp0 && neg_dp1) \
  148. return; \
  149. \
  150. if (neg_dp1) { \
  151. float t = dp1 / (dp1 - dp0); \
  152. if (t > t1) t1 = t; \
  153. } else if (neg_dp0) { \
  154. float t = dp0 / (dp0 - dp1); \
  155. if (t > t0) t0 = t; \
  156. } \
  157. } \
  158. }
  159. static void ClipTriangle(int *indices, int* numIndices)
  160. {
  161. int mask = 0;
  162. mask |= CalcClipMask(Vertices[0]);
  163. mask |= CalcClipMask(Vertices[1]);
  164. mask |= CalcClipMask(Vertices[2]);
  165. if (mask != 0)
  166. {
  167. for (int i = 0; i < 3; i += 3)
  168. {
  169. int vlist[2][2*6+1];
  170. int *inlist = vlist[0], *outlist = vlist[1];
  171. int n = 3;
  172. int numVertices = 3;
  173. inlist[0] = 0;
  174. inlist[1] = 1;
  175. inlist[2] = 2;
  176. // mark this triangle as unused in case it should be completely
  177. // clipped
  178. indices[0] = SKIP_FLAG;
  179. indices[1] = SKIP_FLAG;
  180. indices[2] = SKIP_FLAG;
  181. POLY_CLIP(CLIP_POS_X_BIT, -1, 0, 0, 1);
  182. POLY_CLIP(CLIP_NEG_X_BIT, 1, 0, 0, 1);
  183. POLY_CLIP(CLIP_POS_Y_BIT, 0, -1, 0, 1);
  184. POLY_CLIP(CLIP_NEG_Y_BIT, 0, 1, 0, 1);
  185. POLY_CLIP(CLIP_POS_Z_BIT, 0, 0, 0, 1);
  186. POLY_CLIP(CLIP_NEG_Z_BIT, 0, 0, 1, 1);
  187. INCSTAT(swstats.thisFrame.numTrianglesClipped);
  188. // transform the poly in inlist into triangles
  189. indices[0] = inlist[0];
  190. indices[1] = inlist[1];
  191. indices[2] = inlist[2];
  192. for (int j = 3; j < n; ++j)
  193. {
  194. indices[(*numIndices)++] = inlist[0];
  195. indices[(*numIndices)++] = inlist[j - 1];
  196. indices[(*numIndices)++] = inlist[j];
  197. }
  198. }
  199. }
  200. }
  201. static void ClipLine(int *indices)
  202. {
  203. int mask = 0;
  204. int clip_mask[2] = { 0, 0 };
  205. for (int i = 0; i < 2; ++i)
  206. {
  207. clip_mask[i] = CalcClipMask(Vertices[i]);
  208. mask |= clip_mask[i];
  209. }
  210. if (mask == 0)
  211. return;
  212. float t0 = 0;
  213. float t1 = 0;
  214. // Mark unused in case of early termination
  215. // of the macros below. (When fully clipped)
  216. indices[0] = SKIP_FLAG;
  217. indices[1] = SKIP_FLAG;
  218. LINE_CLIP(CLIP_POS_X_BIT, -1, 0, 0, 1);
  219. LINE_CLIP(CLIP_NEG_X_BIT, 1, 0, 0, 1);
  220. LINE_CLIP(CLIP_POS_Y_BIT, 0, -1, 0, 1);
  221. LINE_CLIP(CLIP_NEG_Y_BIT, 0, 1, 0, 1);
  222. LINE_CLIP(CLIP_POS_Z_BIT, 0, 0, -1, 1);
  223. LINE_CLIP(CLIP_NEG_Z_BIT, 0, 0, 1, 1);
  224. // Restore the old values as this line
  225. // was not fully clipped.
  226. indices[0] = 0;
  227. indices[1] = 1;
  228. int numVertices = 2;
  229. if (clip_mask[0])
  230. {
  231. indices[0] = numVertices;
  232. AddInterpolatedVertex(t0, 0, 1, &numVertices);
  233. }
  234. if (clip_mask[1])
  235. {
  236. indices[1] = numVertices;
  237. AddInterpolatedVertex(t1, 1, 0, &numVertices);
  238. }
  239. }
  240. void ProcessTriangle(OutputVertexData *v0, OutputVertexData *v1, OutputVertexData *v2)
  241. {
  242. INCSTAT(swstats.thisFrame.numTrianglesIn)
  243. bool backface;
  244. if (!CullTest(v0, v1, v2, backface))
  245. return;
  246. int indices[NUM_INDICES] = {
  247. 0, 1, 2, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG,
  248. SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG,
  249. SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG
  250. };
  251. int numIndices = 3;
  252. if (backface)
  253. {
  254. Vertices[0] = v0;
  255. Vertices[1] = v2;
  256. Vertices[2] = v1;
  257. }
  258. else
  259. {
  260. Vertices[0] = v0;
  261. Vertices[1] = v1;
  262. Vertices[2] = v2;
  263. }
  264. ClipTriangle(indices, &numIndices);
  265. for (int i = 0; i+3 <= numIndices; i+=3)
  266. {
  267. _assert_(i < NUM_INDICES);
  268. if (indices[i] != SKIP_FLAG)
  269. {
  270. PerspectiveDivide(Vertices[indices[i]]);
  271. PerspectiveDivide(Vertices[indices[i+1]]);
  272. PerspectiveDivide(Vertices[indices[i+2]]);
  273. Rasterizer::DrawTriangleFrontFace(Vertices[indices[i]], Vertices[indices[i+1]], Vertices[indices[i+2]]);
  274. }
  275. }
  276. }
  277. static void CopyVertex(OutputVertexData *dst, OutputVertexData *src, float dx, float dy, unsigned int sOffset)
  278. {
  279. dst->screenPosition.x = src->screenPosition.x + dx;
  280. dst->screenPosition.y = src->screenPosition.y + dy;
  281. dst->screenPosition.z = src->screenPosition.z;
  282. for (int i = 0; i < 3; ++i)
  283. dst->normal[i] = src->normal[i];
  284. for (int i = 0; i < 4; ++i)
  285. dst->color[0][i] = src->color[0][i];
  286. // todo - s offset
  287. for (int i = 0; i < 8; ++i)
  288. dst->texCoords[i] = src->texCoords[i];
  289. }
  290. void ProcessLine(OutputVertexData *lineV0, OutputVertexData *lineV1)
  291. {
  292. int indices[4] = { 0, 1, SKIP_FLAG, SKIP_FLAG };
  293. Vertices[0] = lineV0;
  294. Vertices[1] = lineV1;
  295. // point to a valid vertex to store to when clipping
  296. Vertices[2] = &ClippedVertices[17];
  297. ClipLine(indices);
  298. if (indices[0] != SKIP_FLAG)
  299. {
  300. OutputVertexData *v0 = Vertices[indices[0]];
  301. OutputVertexData *v1 = Vertices[indices[1]];
  302. PerspectiveDivide(v0);
  303. PerspectiveDivide(v1);
  304. float dx = v1->screenPosition.x - v0->screenPosition.x;
  305. float dy = v1->screenPosition.y - v0->screenPosition.y;
  306. float screenDx = 0;
  307. float screenDy = 0;
  308. if (fabsf(dx) > fabsf(dy))
  309. {
  310. if (dx > 0)
  311. screenDy = bpmem.lineptwidth.linesize / -12.0f;
  312. else
  313. screenDy = bpmem.lineptwidth.linesize / 12.0f;
  314. }
  315. else
  316. {
  317. if (dy > 0)
  318. screenDx = bpmem.lineptwidth.linesize / 12.0f;
  319. else
  320. screenDx = bpmem.lineptwidth.linesize / -12.0f;
  321. }
  322. OutputVertexData triangle[3];
  323. CopyVertex(&triangle[0], v0, screenDx, screenDy, 0);
  324. CopyVertex(&triangle[1], v1, screenDx, screenDy, 0);
  325. CopyVertex(&triangle[2], v1, -screenDx, -screenDy, bpmem.lineptwidth.lineoff);
  326. // ccw winding
  327. Rasterizer::DrawTriangleFrontFace(&triangle[2], &triangle[1], &triangle[0]);
  328. CopyVertex(&triangle[1], v0, -screenDx, -screenDy, bpmem.lineptwidth.lineoff);
  329. Rasterizer::DrawTriangleFrontFace(&triangle[0], &triangle[1], &triangle[2]);
  330. }
  331. }
  332. bool CullTest(OutputVertexData *v0, OutputVertexData *v1, OutputVertexData *v2, bool &backface)
  333. {
  334. int mask = CalcClipMask(v0);
  335. mask &= CalcClipMask(v1);
  336. mask &= CalcClipMask(v2);
  337. if (mask)
  338. {
  339. INCSTAT(swstats.thisFrame.numTrianglesRejected)
  340. return false;
  341. }
  342. float x0 = v0->projectedPosition.x;
  343. float x1 = v1->projectedPosition.x;
  344. float x2 = v2->projectedPosition.x;
  345. float y1 = v1->projectedPosition.y;
  346. float y0 = v0->projectedPosition.y;
  347. float y2 = v2->projectedPosition.y;
  348. float w0 = v0->projectedPosition.w;
  349. float w1 = v1->projectedPosition.w;
  350. float w2 = v2->projectedPosition.w;
  351. float normalZDir = (x0*w2 - x2*w0)*y1 + (x2*y0 - x0*y2)*w1 + (y2*w0 - y0*w2)*x1;
  352. backface = normalZDir <= 0.0f;
  353. if ((bpmem.genMode.cullmode & 1) && !backface) // cull frontfacing
  354. {
  355. INCSTAT(swstats.thisFrame.numTrianglesCulled)
  356. return false;
  357. }
  358. if ((bpmem.genMode.cullmode & 2) && backface) // cull backfacing
  359. {
  360. INCSTAT(swstats.thisFrame.numTrianglesCulled)
  361. return false;
  362. }
  363. return true;
  364. }
  365. void PerspectiveDivide(OutputVertexData *vertex)
  366. {
  367. Vec4 &projected = vertex->projectedPosition;
  368. Vec3 &screen = vertex->screenPosition;
  369. float wInverse = 1.0f/projected.w;
  370. screen.x = projected.x * wInverse * xfmem.viewport.wd + m_ViewOffset[0];
  371. screen.y = projected.y * wInverse * xfmem.viewport.ht + m_ViewOffset[1];
  372. screen.z = projected.z * wInverse * xfmem.viewport.zRange + xfmem.viewport.farZ;
  373. }
  374. }