SoftwareRasterizer.cpp 9.1 KB

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