SoftwareRasterizer.cpp 9.0 KB

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