2
0

SoftwareRasterizer.cpp 9.0 KB

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