SoftwareRasterizer.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Scene/SoftwareRasterizer.h>
  6. #include <AnKi/Collision/Aabb.h>
  7. #include <AnKi/Collision/Functions.h>
  8. #include <AnKi/Util/Tracer.h>
  9. namespace anki
  10. {
  11. void SoftwareRasterizer::prepare(const Mat4& mv, const Mat4& p, U32 width, U32 height)
  12. {
  13. m_mv = mv;
  14. m_p = p;
  15. m_mvp = p * mv;
  16. extractClipPlanes(p, m_planesL);
  17. extractClipPlanes(m_mvp, m_planesW);
  18. // Reset z buffer
  19. ANKI_ASSERT(width > 0 && height > 0);
  20. m_width = width;
  21. m_height = height;
  22. U32 size = width * height;
  23. if(m_zbuffer.getSize() < size)
  24. {
  25. m_zbuffer.destroy(m_alloc);
  26. m_zbuffer.create(m_alloc, size);
  27. }
  28. memset(&m_zbuffer[0], 0xFF, sizeof(m_zbuffer[0]) * size);
  29. }
  30. void SoftwareRasterizer::clipTriangle(const Vec4* inVerts, Vec4* outVerts, U& outVertCount) const
  31. {
  32. ANKI_ASSERT(inVerts && outVerts);
  33. const Plane& plane = m_planesL[FrustumPlaneType::NEAR];
  34. F32 clipZ = -plane.getOffset() - EPSILON;
  35. ANKI_ASSERT(clipZ < 0.0);
  36. Array<Bool, 3> vertInside;
  37. U vertInsideCount = 0;
  38. for(U i = 0; i < 3; ++i)
  39. {
  40. vertInside[i] = inVerts[i].z() < clipZ;
  41. vertInsideCount += (vertInside[i]) ? 1 : 0;
  42. }
  43. switch(vertInsideCount)
  44. {
  45. case 0:
  46. // All out
  47. outVertCount = 0;
  48. break;
  49. case 3:
  50. // All in
  51. outVertCount = 3;
  52. outVerts[0] = inVerts[0];
  53. outVerts[1] = inVerts[1];
  54. outVerts[2] = inVerts[2];
  55. break;
  56. case 1:
  57. {
  58. U i, next, prev;
  59. if(vertInside[0])
  60. {
  61. i = 0;
  62. next = 1;
  63. prev = 2;
  64. }
  65. else if(vertInside[1])
  66. {
  67. i = 1;
  68. next = 2;
  69. prev = 0;
  70. }
  71. else
  72. {
  73. i = 2;
  74. next = 0;
  75. prev = 1;
  76. }
  77. // Find first intersection
  78. Vec4 rayOrigin = inVerts[i].xyz0();
  79. Vec4 rayDir = (inVerts[next].xyz0() - rayOrigin).getNormalized();
  80. Vec4 intersection0;
  81. Bool intersects = testCollision(plane, Ray(rayOrigin, rayDir), intersection0);
  82. (void)intersects;
  83. ANKI_ASSERT(intersects);
  84. // Find second intersection
  85. rayDir = (inVerts[prev].xyz0() - rayOrigin).getNormalized();
  86. Vec4 intersection1;
  87. intersects = testCollision(plane, Ray(rayOrigin, rayDir), intersection1);
  88. (void)intersects;
  89. ANKI_ASSERT(intersects);
  90. // Finalize
  91. outVerts[0] = inVerts[i];
  92. outVerts[1] = intersection0.xyz1();
  93. outVerts[2] = intersection1.xyz1();
  94. outVertCount = 3;
  95. break;
  96. }
  97. case 2:
  98. {
  99. U in0, in1, out;
  100. if(vertInside[0] && vertInside[1])
  101. {
  102. in0 = 0;
  103. in1 = 1;
  104. out = 2;
  105. }
  106. else if(vertInside[1] && vertInside[2])
  107. {
  108. in0 = 1;
  109. in1 = 2;
  110. out = 0;
  111. }
  112. else
  113. {
  114. ANKI_ASSERT(vertInside[2] && vertInside[0]);
  115. in0 = 2;
  116. in1 = 0;
  117. out = 1;
  118. }
  119. // Find first intersection
  120. Vec4 rayOrigin = inVerts[in1].xyz0();
  121. Vec4 rayDir = (inVerts[out].xyz0() - rayOrigin).getNormalized();
  122. Vec4 intersection0;
  123. Bool intersects = testCollision(plane, Ray(rayOrigin, rayDir), intersection0);
  124. (void)intersects;
  125. ANKI_ASSERT(intersects);
  126. // Find second intersection
  127. rayOrigin = inVerts[in0].xyz0();
  128. rayDir = (inVerts[out].xyz0() - rayOrigin).getNormalized();
  129. Vec4 intersection1;
  130. intersects = testCollision(plane, Ray(rayOrigin, rayDir), intersection1);
  131. (void)intersects;
  132. ANKI_ASSERT(intersects);
  133. // Two triangles
  134. outVerts[0] = inVerts[in1];
  135. outVerts[1] = intersection0;
  136. outVerts[2] = intersection1;
  137. outVerts[3] = intersection1;
  138. outVerts[4] = inVerts[in0];
  139. outVerts[5] = inVerts[in1];
  140. outVertCount = 6;
  141. break;
  142. }
  143. }
  144. }
  145. void SoftwareRasterizer::draw(const F32* verts, U vertCount, U stride, Bool backfaceCulling)
  146. {
  147. ANKI_ASSERT(verts && vertCount > 0 && (vertCount % 3) == 0);
  148. ANKI_ASSERT(stride >= sizeof(F32) * 3 && (stride % sizeof(F32)) == 0);
  149. U floatStride = stride / sizeof(F32);
  150. const F32* vertsEnd = verts + vertCount * floatStride;
  151. while(verts != vertsEnd)
  152. {
  153. // Convert triangle to view space
  154. Array<Vec4, 3> triVspace;
  155. for(U j = 0; j < 3; ++j)
  156. {
  157. triVspace[j] = m_mv * Vec4(verts[0], verts[1], verts[2], 1.0);
  158. verts += floatStride;
  159. }
  160. // Cull if backfacing
  161. if(backfaceCulling)
  162. {
  163. Vec4 norm = (triVspace[1] - triVspace[0]).cross(triVspace[2] - triVspace[1]);
  164. ANKI_ASSERT(norm.w() == 0.0f);
  165. Vec4 eye = triVspace[0].xyz0();
  166. if(norm.dot(eye) >= 0.0f)
  167. {
  168. continue;
  169. }
  170. }
  171. // Clip it
  172. Array<Vec4, 6> clippedTrisVspace;
  173. U clippedCount = 0;
  174. clipTriangle(&triVspace[0], &clippedTrisVspace[0], clippedCount);
  175. if(clippedCount == 0)
  176. {
  177. // Outside view
  178. continue;
  179. }
  180. // Rasterize
  181. Array<Vec4, 3> clip;
  182. for(U j = 0; j < clippedCount; j += 3)
  183. {
  184. for(U k = 0; k < 3; k++)
  185. {
  186. clip[k] = m_p * clippedTrisVspace[j + k].xyz1();
  187. ANKI_ASSERT(clip[k].w() > 0.0f);
  188. }
  189. rasterizeTriangle(&clip[0]);
  190. }
  191. }
  192. }
  193. Bool SoftwareRasterizer::computeBarycetrinc(const Vec2& a, const Vec2& b, const Vec2& c, const Vec2& p, Vec3& uvw) const
  194. {
  195. Vec2 dca = c - a;
  196. Vec2 dba = b - a;
  197. Vec2 dap = a - p;
  198. Vec3 n(dca.x(), dba.x(), dap.x());
  199. Vec3 m(dca.y(), dba.y(), dap.y());
  200. Vec3 k = n.cross(m);
  201. Bool skip = false;
  202. if(!isZero(k.z()))
  203. {
  204. uvw = Vec3(1.0f - (k.x() + k.y()) / k.z(), k.y() / k.z(), k.x() / k.z());
  205. if(uvw.x() < 0.0f || uvw.y() < 0.0f || uvw.z() < 0.0f)
  206. {
  207. skip = true;
  208. }
  209. }
  210. else
  211. {
  212. skip = true;
  213. }
  214. return skip;
  215. }
  216. void SoftwareRasterizer::rasterizeTriangle(const Vec4* tri)
  217. {
  218. ANKI_ASSERT(tri);
  219. const Vec2 windowSize{F32(m_width), F32(m_height)};
  220. Array<Vec3, 3> ndc;
  221. Array<Vec2, 3> window;
  222. Vec2 bboxMin(MAX_F32), bboxMax(MIN_F32);
  223. for(U i = 0; i < 3; i++)
  224. {
  225. ndc[i] = tri[i].xyz() / tri[i].w();
  226. window[i] = (ndc[i].xy() / 2.0f + 0.5f) * windowSize;
  227. for(U j = 0; j < 2; j++)
  228. {
  229. bboxMin[j] = std::floor(min(bboxMin[j], window[i][j]));
  230. bboxMin[j] = clamp(bboxMin[j], 0.0f, windowSize[j]);
  231. bboxMax[j] = std::ceil(max(bboxMax[j], window[i][j]));
  232. bboxMax[j] = clamp(bboxMax[j], 0.0f, windowSize[j]);
  233. }
  234. }
  235. for(F32 y = bboxMin.y() + 0.5f; y < bboxMax.y() + 0.5f; y += 1.0f)
  236. {
  237. for(F32 x = bboxMin.x() + 0.5f; x < bboxMax.x() + 0.5f; x += 1.0f)
  238. {
  239. Vec2 p(x, y);
  240. Vec3 bc;
  241. if(!computeBarycetrinc(window[0], window[1], window[2], p, bc))
  242. {
  243. const F32 z0 = ndc[0].z();
  244. const F32 z1 = ndc[1].z();
  245. const F32 z2 = ndc[2].z();
  246. F32 depth = z0 * bc[0] + z1 * bc[1] + z2 * bc[2];
  247. ANKI_ASSERT(depth >= 0.0 && depth <= 1.0);
  248. // Clamp it to a bit less that 1.0f because 1.0f will produce a 0 depthi
  249. depth = min(depth, 1.0f - EPSILON);
  250. // Store the min of the current value and new one
  251. const U32 depthi = U32(depth * F32(MAX_U32));
  252. m_zbuffer[U32(y) * m_width + U32(x)].min(depthi);
  253. }
  254. }
  255. }
  256. }
  257. Bool SoftwareRasterizer::visibilityTest(const Aabb& aabb) const
  258. {
  259. ANKI_TRACE_SCOPED_EVENT(SCENE_RASTERIZER_TEST);
  260. Bool inside = visibilityTestInternal(aabb);
  261. return inside;
  262. }
  263. Bool SoftwareRasterizer::visibilityTestInternal(const Aabb& aabb) const
  264. {
  265. // Set the AABB points
  266. const Vec4& minv = aabb.getMin();
  267. const Vec4& maxv = aabb.getMax();
  268. Array<Vec4, 8> boxPoints;
  269. boxPoints[0] = minv.xyz1();
  270. boxPoints[1] = Vec4(minv.x(), maxv.y(), minv.z(), 1.0f);
  271. boxPoints[2] = Vec4(minv.x(), maxv.y(), maxv.z(), 1.0f);
  272. boxPoints[3] = Vec4(minv.x(), minv.y(), maxv.z(), 1.0f);
  273. boxPoints[4] = maxv.xyz1();
  274. boxPoints[5] = Vec4(maxv.x(), minv.y(), maxv.z(), 1.0f);
  275. boxPoints[6] = Vec4(maxv.x(), minv.y(), minv.z(), 1.0f);
  276. boxPoints[7] = Vec4(maxv.x(), maxv.y(), minv.z(), 1.0f);
  277. // Transform points
  278. for(Vec4& p : boxPoints)
  279. {
  280. p = m_mvp * p;
  281. }
  282. // Check of a point touches the near plane
  283. for(const Vec4& p : boxPoints)
  284. {
  285. if(p.w() <= 0.0f)
  286. {
  287. // Don't bother clipping. Just mark it as visible.
  288. return true;
  289. }
  290. }
  291. // Compute the min and max bounds
  292. Vec4 bboxMin(MAX_F32);
  293. Vec4 bboxMax(MIN_F32);
  294. for(Vec4& p : boxPoints)
  295. {
  296. // Perspecrive divide
  297. p /= p.w();
  298. // To [0, 1]
  299. p *= Vec4(0.5f, 0.5f, 1.0f, 1.0f);
  300. p += Vec4(0.5f, 0.5f, 0.0f, 0.0f);
  301. // To [0, m_width|m_height]
  302. p *= Vec4(F32(m_width), F32(m_height), 1.0f, 1.0f);
  303. // Min
  304. bboxMin = bboxMin.min(p);
  305. // Max
  306. bboxMax = bboxMax.max(p);
  307. }
  308. // Fix the bounds
  309. bboxMin.x() = floorf(bboxMin.x());
  310. bboxMin.x() = clamp(bboxMin.x(), 0.0f, F32(m_width));
  311. bboxMax.x() = ceilf(bboxMax.x());
  312. bboxMax.x() = clamp(bboxMax.x(), 0.0f, F32(m_width));
  313. bboxMin.y() = floorf(bboxMin.y());
  314. bboxMin.y() = clamp(bboxMin.y(), 0.0f, F32(m_height));
  315. bboxMax.y() = ceilf(bboxMax.y());
  316. bboxMax.y() = clamp(bboxMax.y(), 0.0f, F32(m_height));
  317. // Loop the tiles
  318. F32 minZ = bboxMin.z();
  319. for(F32 y = bboxMin.y(); y < bboxMax.y(); y += 1.0f)
  320. {
  321. for(F32 x = bboxMin.x(); x < bboxMax.x(); x += 1.0f)
  322. {
  323. const U32 idx = U32(y) * m_width + U32(x);
  324. const U32 depthi = m_zbuffer[idx].getNonAtomically();
  325. const F32 depthf = F32(depthi) / F32(MAX_U32);
  326. if(minZ < depthf)
  327. {
  328. return true;
  329. }
  330. }
  331. }
  332. return false;
  333. }
  334. void SoftwareRasterizer::fillDepthBuffer(ConstWeakArray<F32> depthValues)
  335. {
  336. ANKI_ASSERT(m_zbuffer.getSize() == depthValues.getSize());
  337. U32 count = depthValues.getSize();
  338. while(count--)
  339. {
  340. F32 depth = depthValues[count];
  341. ANKI_ASSERT(depth >= 0.0f && depth <= 1.0f);
  342. depth = min(depth, 1.0f - EPSILON); // See a few lines above why is that
  343. const U32 depthi = U32(depth * F32(MAX_U32));
  344. m_zbuffer[count].setNonAtomically(depthi);
  345. }
  346. }
  347. } // end namespace anki