renderCore.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #include <cassert>
  24. #include "renderCore.h"
  25. #include "../image/internal/imageInternal.h"
  26. #include "shader/Shader.h"
  27. #include "shader/RgbaMultiply.h"
  28. #include "constants.h"
  29. using namespace dsr;
  30. class SubVertex {
  31. public:
  32. FVector3D cs; // Camera space position based on the weights
  33. float subB, subC; // Weights for second and third vertices in the parent triangle
  34. int state = 0; // Used by algorithms
  35. float value = 0.0f; // Used by algorithms
  36. SubVertex() : cs(FVector3D()), subB(0.0f), subC(0.0f) {}
  37. SubVertex(FVector3D cs, float subB, float subC) : cs(cs), subB(subB), subC(subC) {}
  38. SubVertex(SubVertex vertexA, SubVertex vertexB, float ratio) {
  39. float invRatio = 1.0f - ratio;
  40. this->cs = vertexA.cs * invRatio + vertexB.cs * ratio;
  41. this->subB = vertexA.subB * invRatio + vertexB.subB * ratio;
  42. this->subC = vertexA.subC * invRatio + vertexB.subC * ratio;
  43. }
  44. };
  45. // Returns 0 when value = a
  46. // Returns 0.5 when value = (a + b) / 2
  47. // Returns 1 when value = b
  48. static float inverseLerp(float a, float b, float value) {
  49. float c = b - a;
  50. if (c == 0.0f) {
  51. return 0.5f;
  52. } else {
  53. return (value - a) / c;
  54. }
  55. }
  56. static const int maxPoints = 9; // If a triangle starts with 3 points and each of 6 planes in the view frustum can add one point each then the maximum is 9 points
  57. class ClippedTriangle {
  58. private:
  59. int vertexCount = 0;
  60. public:
  61. int getVertexCount() {
  62. return this->vertexCount;
  63. }
  64. SubVertex vertices[maxPoints] = {};
  65. ClippedTriangle(const ITriangle2D &triangle) {
  66. this->vertices[0] = SubVertex(triangle.position[0].cs, 0.0f, 0.0f);
  67. this->vertices[1] = SubVertex(triangle.position[1].cs, 1.0f, 0.0f);
  68. this->vertices[2] = SubVertex(triangle.position[2].cs, 0.0f, 1.0f);
  69. this->vertexCount = 3;
  70. }
  71. void deleteVertex(int removeIndex) {
  72. assert(removeIndex >= 0);
  73. assert(removeIndex < this->vertexCount);
  74. if (this->vertexCount > 0 && this->vertexCount <= maxPoints) {
  75. for (int v = removeIndex; v < this->vertexCount - 1; v++) {
  76. assert(v >= 0 && v + 1 < maxPoints);
  77. this->vertices[v] = this->vertices[v + 1];
  78. }
  79. this->vertexCount--;
  80. }
  81. }
  82. void insertVertex(int newIndex, const SubVertex &newVertex) {
  83. // Check against buffer overflow in case of bugs from rounding errors
  84. assert(newIndex >= 0);
  85. assert(newIndex <= this->vertexCount);
  86. if (this->vertexCount < maxPoints) {
  87. for (int v = this->vertexCount - 1; v >= newIndex; v--) {
  88. assert(v >= 0 && v + 1 < maxPoints);
  89. this->vertices[v + 1] = this->vertices[v];
  90. }
  91. this->vertices[newIndex] = newVertex;
  92. this->vertexCount++;
  93. }
  94. }
  95. void deleteAll() {
  96. this->vertexCount = 0;
  97. }
  98. // Cut away parts of the triangle that are on the positive side of the plane
  99. void clip(const FPlane3D &plane) {
  100. static const int state_use = 0;
  101. static const int state_delete = 1;
  102. static const int state_modified = 2;
  103. if (this->vertexCount >= 3 && this->vertexCount < maxPoints) {
  104. int outsideCount = 0;
  105. int lastOutside = 0;
  106. for (int v = 0; v < this->vertexCount; v++) {
  107. assert(v >= 0 && v < maxPoints);
  108. float distance = plane.signedDistance(this->vertices[v].cs);
  109. this->vertices[v].value = distance;
  110. if (distance > 0.0f) {
  111. outsideCount++;
  112. lastOutside = v;
  113. this->vertices[v].state = state_delete;
  114. } else {
  115. this->vertices[v].state = state_use;
  116. }
  117. }
  118. if (outsideCount > 0) {
  119. if (outsideCount >= this->vertexCount) {
  120. this->deleteAll();
  121. } else if (outsideCount == 1) {
  122. // Split a single vertex into two corners by interpolating with the previous and next corners
  123. int currentVertex = lastOutside;
  124. int previousVertex = (lastOutside - 1 + this->vertexCount) % this->vertexCount;
  125. int nextVertex = (lastOutside + 1) % this->vertexCount;
  126. float previousToCurrentRatio = inverseLerp(this->vertices[previousVertex].value, this->vertices[currentVertex].value, 0.0f);
  127. float currentToNextRatio = inverseLerp(this->vertices[currentVertex].value, this->vertices[nextVertex].value, 0.0f);
  128. SubVertex cutStart(this->vertices[previousVertex], this->vertices[currentVertex], previousToCurrentRatio);
  129. SubVertex cutEnd(this->vertices[currentVertex], this->vertices[nextVertex], currentToNextRatio);
  130. this->vertices[lastOutside] = cutStart;
  131. insertVertex(nextVertex, cutEnd);
  132. } else {
  133. // Start and end of the cut
  134. for (int currentVertex = 0; currentVertex < this->vertexCount; currentVertex++) {
  135. int previousVertex = (currentVertex - 1 + this->vertexCount) % this->vertexCount;
  136. int nextVertex = (currentVertex + 1) % this->vertexCount;
  137. if (this->vertices[currentVertex].state == state_delete) {
  138. if (this->vertices[previousVertex].state == state_use) {
  139. // Begin the cut
  140. float previousToCurrentRatio = inverseLerp(this->vertices[previousVertex].value, this->vertices[currentVertex].value, 0.0f);
  141. this->vertices[currentVertex] = SubVertex(this->vertices[previousVertex], this->vertices[currentVertex], previousToCurrentRatio);
  142. this->vertices[currentVertex].state = state_modified;
  143. } else if (this->vertices[nextVertex].state == state_use) {
  144. // End the cut
  145. float currentToNextRatio = inverseLerp(this->vertices[currentVertex].value, this->vertices[nextVertex].value, 0.0f);
  146. this->vertices[currentVertex] = SubVertex(this->vertices[currentVertex], this->vertices[nextVertex], currentToNextRatio);
  147. this->vertices[currentVertex].state = state_modified;
  148. }
  149. }
  150. }
  151. // Delete every vertex that is marked for removal
  152. // Looping backwards will avoid using altered indices while deleting
  153. if (outsideCount > 2) {
  154. for (int v = this->vertexCount - 1; v >= 0; v--) {
  155. assert(v >= 0 && v < maxPoints);
  156. if (this->vertices[v].state == state_delete) {
  157. this->deleteVertex(v);
  158. }
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. };
  166. Visibility dsr::getTriangleVisibility(const ITriangle2D &triangle, const Camera &camera, bool clipFrustum) {
  167. static const int cornerCount = 3;
  168. int planeCount = camera.getFrustumPlaneCount(clipFrustum);
  169. bool outside[cornerCount * planeCount];
  170. // Check which corners are outside of the different planes
  171. int offset = 0;
  172. for (int c = 0; c < cornerCount; c++) {
  173. FVector3D triangleCorner = triangle.position[c].cs;
  174. for (int s = 0; s < planeCount; s++) {
  175. outside[offset + s] = !(camera.getFrustumPlane(s, clipFrustum).inside(triangleCorner));
  176. }
  177. offset += planeCount;
  178. }
  179. // Do not render if all corners are outside of the same side
  180. for (int s = 0; s < planeCount; s++) {
  181. if (outside[s] && outside[s + planeCount] && outside[s + 2 * planeCount]) {
  182. return Visibility::Hidden; // All corners outside of the same plane
  183. }
  184. }
  185. // Partial visibility if any corner is outside of a side
  186. for (int i = 0; i < planeCount * cornerCount; i++) {
  187. if (outside[i]) {
  188. return Visibility::Partial; // Any corner outside of a plane
  189. }
  190. }
  191. return Visibility::Full;
  192. }
  193. static const int alignX = 2;
  194. static const int alignY = 2;
  195. void dsr::executeTriangleDrawing(const TriangleDrawCommand &command, const IRect &clipBound) {
  196. IRect finalClipBound = IRect::cut(command.clipBound, clipBound);
  197. int32_t rowCount = command.triangle.getBufferSize(finalClipBound, alignX, alignY);
  198. if (rowCount > 0) {
  199. int startRow;
  200. RowInterval rows[rowCount];
  201. command.triangle.getShape(startRow, rows, finalClipBound, alignX, alignY);
  202. Projection projection = command.triangle.getProjection(command.subB, command.subC, command.perspective);
  203. command.processTriangle(command.triangleInput, command.targetImage, command.depthBuffer, command.triangle, projection, RowShape(startRow, rowCount, rows), command.filter);
  204. #ifdef SHOW_POST_CLIPPING_WIREFRAME
  205. drawWireframe(command.targetImage, command.triangle);
  206. #endif
  207. }
  208. }
  209. // Draw a linearly interpolated sub-triangle for clipping
  210. static void drawSubTriangle(CommandQueue *commandQueue, const TriangleDrawData &triangleDrawData, const Camera &camera, const IRect &clipBound, const SubVertex &vertexA, const SubVertex &vertexB, const SubVertex &vertexC) {
  211. //Get the weight of the first corner from the other weights
  212. FVector3D subB(vertexA.subB, vertexB.subB, vertexC.subB);
  213. FVector3D subC(vertexA.subC, vertexB.subC, vertexC.subC);
  214. //FVector3D subA = FVector3D(1.0f, 1.0f, 1.0f) - (subB + subC);
  215. ProjectedPoint posA = camera.cameraToScreen(vertexA.cs);
  216. ProjectedPoint posB = camera.cameraToScreen(vertexB.cs);
  217. ProjectedPoint posC = camera.cameraToScreen(vertexC.cs);
  218. // Create the sub-triangle
  219. ITriangle2D triangle(posA, posB, posC);
  220. // Rounding sub-triangles to integer locations may reverse the direction of zero area triangles
  221. if (triangle.isFrontfacing()) {
  222. TriangleDrawCommand command(triangleDrawData, triangle, subB, subC, clipBound);
  223. if (commandQueue) {
  224. commandQueue->add(command);
  225. } else {
  226. executeTriangleDrawing(command, clipBound);
  227. }
  228. }
  229. }
  230. // Clip triangles against the clip bounds outside of the image
  231. // Precondition: The triangle needs to be clipped
  232. // TODO: Take drawSubTriangle as a lambda to drawClippedTriangle using vertex weights as arguments and vertex data as captured variables
  233. static void drawClippedTriangle(CommandQueue *commandQueue, const TriangleDrawData &triangleDrawData, const Camera &camera, const ITriangle2D &triangle, const IRect &clipBound) {
  234. ClippedTriangle clipped(triangle);
  235. int planeCount = camera.getFrustumPlaneCount(true);
  236. for (int s = 0; s < planeCount; s++) {
  237. clipped.clip(camera.getFrustumPlane(s, true));
  238. }
  239. // Draw a convex triangle fan from the clipped triangle
  240. for (int triangleIndex = 0; triangleIndex < clipped.getVertexCount() - 2; triangleIndex++) {
  241. int indexA = 0;
  242. int indexB = 1 + triangleIndex;
  243. int indexC = 2 + triangleIndex;
  244. drawSubTriangle(commandQueue, triangleDrawData, camera, clipBound, clipped.vertices[indexA], clipped.vertices[indexB], clipped.vertices[indexC]);
  245. }
  246. }
  247. // Clipping is applied automatically if needed
  248. void dsr::renderTriangleWithShader(CommandQueue *commandQueue, const TriangleDrawData &triangleDrawData, const Camera &camera, const ITriangle2D &triangle, const IRect &clipBound) {
  249. // Allow small triangles to be a bit outside of the view frustum without being clipped by increasing the width and height slopes in a second test
  250. // This reduces redundant clipping to improve both speed and quality
  251. Visibility paddedVisibility = getTriangleVisibility(triangle, camera, true);
  252. // Draw the triangle
  253. if (paddedVisibility == Visibility::Full) {
  254. // Only check if the triangle is front facing once we know that the projection is in positive depth
  255. if (triangle.isFrontfacing()) {
  256. // Draw the full triangle
  257. TriangleDrawCommand command(triangleDrawData, triangle, FVector3D(0.0f, 1.0f, 0.0f), FVector3D(0.0f, 0.0f, 1.0f), clipBound);
  258. if (commandQueue) {
  259. commandQueue->add(command);
  260. } else {
  261. executeTriangleDrawing(command, clipBound);
  262. }
  263. }
  264. } else {
  265. // Draw a clipped triangle
  266. drawClippedTriangle(commandQueue, triangleDrawData, camera, triangle, clipBound);
  267. }
  268. }
  269. // TODO: Move shader selection to Shader_RgbaMultiply and let models default to its shader factory function pointer as shader selection
  270. void dsr::renderTriangleFromData(
  271. CommandQueue *commandQueue, ImageRgbaU8Impl *targetImage, ImageF32Impl *depthBuffer,
  272. const Camera &camera, const ProjectedPoint &posA, const ProjectedPoint &posB, const ProjectedPoint &posC,
  273. Filter filter, const ImageRgbaU8Impl *diffuse, const ImageRgbaU8Impl *light,
  274. TriangleTexCoords texCoords, TriangleColors colors) {
  275. // Get dimensions from both buffers
  276. int colorWidth = imageInternal::getWidth(targetImage);
  277. int colorHeight = imageInternal::getHeight(targetImage);
  278. int depthWidth = imageInternal::getWidth(depthBuffer);
  279. int depthHeight = imageInternal::getHeight(depthBuffer);
  280. // Combine dimensions
  281. int targetWidth, targetHeight;
  282. if (targetImage != nullptr) {
  283. targetWidth = colorWidth;
  284. targetHeight = colorHeight;
  285. if (depthBuffer != nullptr) {
  286. assert(targetWidth == depthWidth);
  287. assert(targetHeight == depthHeight);
  288. }
  289. } else {
  290. if (depthBuffer != nullptr) {
  291. targetWidth = depthWidth;
  292. targetHeight = depthHeight;
  293. } else {
  294. return; // No target buffer to draw on
  295. }
  296. }
  297. // Select a bound
  298. IRect clipBound = IRect::FromSize(targetWidth, targetHeight);
  299. // Create a triangle
  300. ITriangle2D triangle(posA, posB, posC);
  301. // Only draw visible triangles
  302. Visibility visibility = getTriangleVisibility(triangle, camera, false);
  303. if (visibility != Visibility::Hidden) {
  304. // Select an instance of the default shader
  305. if (!(filter == Filter::Alpha && almostZero(colors.alpha))) {
  306. renderTriangleWithShader(commandQueue, TriangleDrawData(targetImage, depthBuffer, camera.perspective, filter, TriangleInput(diffuse, light, texCoords, colors), &processTriangle_RgbaMultiply), camera, triangle, clipBound);
  307. }
  308. }
  309. }
  310. template<bool AFFINE>
  311. static void executeTriangleDrawingDepth(ImageF32Impl *depthBuffer, const ITriangle2D& triangle, const IRect &clipBound) {
  312. int32_t rowCount = triangle.getBufferSize(clipBound, 1, 1);
  313. if (rowCount > 0) {
  314. int startRow;
  315. RowInterval rows[rowCount];
  316. triangle.getShape(startRow, rows, clipBound, 1, 1);
  317. Projection projection = triangle.getProjection(FVector3D(), FVector3D(), !AFFINE); // TODO: Create a weight using only depth to save time
  318. RowShape shape = RowShape(startRow, rowCount, rows);
  319. // Draw the triangle
  320. const int depthBufferStride = imageInternal::getStride(depthBuffer);
  321. SafePointer<float> depthDataRow = imageInternal::getSafeData<float>(depthBuffer, shape.startRow);
  322. for (int32_t y = shape.startRow; y < shape.startRow + shape.rowCount; y++) {
  323. RowInterval row = shape.rows[y - shape.startRow];
  324. SafePointer<float> depthData = depthDataRow + row.left;
  325. // Initialize depth iteration
  326. float depthValue;
  327. if (AFFINE) {
  328. depthValue = projection.getWeight_affine(IVector2D(row.left, y)).x;
  329. } else {
  330. depthValue = projection.getDepthDividedWeight_perspective(IVector2D(row.left, y)).x;
  331. }
  332. float depthDx = projection.pWeightDx.x;
  333. // Loop over a row of depth pixels
  334. for (int32_t x = row.left; x < row.right; x++) {
  335. float oldValue = *depthData;
  336. if (AFFINE) {
  337. // Write lower depthValue for orthogonal cameras
  338. if (depthValue < oldValue) {
  339. *depthData = depthValue;
  340. }
  341. } else {
  342. // Write higher depthValue for perspective cameras
  343. if (depthValue > oldValue) {
  344. *depthData = depthValue;
  345. }
  346. }
  347. depthValue += depthDx;
  348. depthData += 1;
  349. }
  350. // Iterate to the next row
  351. depthDataRow.increaseBytes(depthBufferStride);
  352. }
  353. }
  354. }
  355. static void drawTriangleDepth(ImageF32Impl *depthBuffer, const Camera &camera, const IRect &clipBound, const ITriangle2D& triangle) {
  356. // Rounding sub-triangles to integer locations may reverse the direction of zero area triangles
  357. if (triangle.isFrontfacing()) {
  358. if (camera.perspective) {
  359. executeTriangleDrawingDepth<false>(depthBuffer, triangle, clipBound);
  360. } else {
  361. executeTriangleDrawingDepth<true>(depthBuffer, triangle, clipBound);
  362. }
  363. }
  364. }
  365. static void drawSubTriangleDepth(ImageF32Impl *depthBuffer, const Camera &camera, const IRect &clipBound, const SubVertex &vertexA, const SubVertex &vertexB, const SubVertex &vertexC) {
  366. ProjectedPoint posA = camera.cameraToScreen(vertexA.cs);
  367. ProjectedPoint posB = camera.cameraToScreen(vertexB.cs);
  368. ProjectedPoint posC = camera.cameraToScreen(vertexC.cs);
  369. drawTriangleDepth(depthBuffer, camera, clipBound, ITriangle2D(posA, posB, posC));
  370. }
  371. void dsr::renderTriangleFromDataDepth(ImageF32Impl *depthBuffer, const Camera &camera, const ProjectedPoint &posA, const ProjectedPoint &posB, const ProjectedPoint &posC) {
  372. // Skip rendering if there's no target buffer
  373. if (depthBuffer == nullptr) { return; }
  374. // Select a bound
  375. IRect clipBound = IRect::FromSize(imageInternal::getWidth(depthBuffer), imageInternal::getHeight(depthBuffer));
  376. // Create a triangle
  377. ITriangle2D triangle(posA, posB, posC);
  378. // Only draw visible triangles
  379. Visibility visibility = getTriangleVisibility(triangle, camera, false);
  380. if (visibility != Visibility::Hidden) {
  381. // Allow small triangles to be a bit outside of the view frustum without being clipped by increasing the width and height slopes in a second test
  382. // This reduces redundant clipping to improve both speed and quality
  383. Visibility paddedVisibility = getTriangleVisibility(triangle, camera, true);
  384. // Draw the triangle
  385. if (paddedVisibility == Visibility::Full) {
  386. // Only check if the triangle is front facing once we know that the projection is in positive depth
  387. if (triangle.isFrontfacing()) {
  388. // Draw the full triangle
  389. drawTriangleDepth(depthBuffer, camera, clipBound, triangle);
  390. }
  391. } else {
  392. // Draw a clipped triangle
  393. ClippedTriangle clipped(triangle); // TODO: Simpler vertex clipping using only positions
  394. int planeCount = camera.getFrustumPlaneCount(true);
  395. for (int s = 0; s < planeCount; s++) {
  396. clipped.clip(camera.getFrustumPlane(s, true));
  397. }
  398. // Draw a convex triangle fan from the clipped triangle
  399. for (int triangleIndex = 0; triangleIndex < clipped.getVertexCount() - 2; triangleIndex++) {
  400. int indexA = 0;
  401. int indexB = 1 + triangleIndex;
  402. int indexC = 2 + triangleIndex;
  403. drawSubTriangleDepth(depthBuffer, camera, clipBound, clipped.vertices[indexA], clipped.vertices[indexB], clipped.vertices[indexC]);
  404. }
  405. }
  406. }
  407. }
  408. void CommandQueue::add(const TriangleDrawCommand &command) {
  409. this->buffer.push(command);
  410. }
  411. void CommandQueue::execute(const IRect &clipBound, int jobCount) const {
  412. if (jobCount <= 1) {
  413. // TODO: Make a setting for sorting triangles using indices within each job
  414. for (int i = 0; i < this->buffer.length(); i++) {
  415. if (!this->buffer[i].occluded) {
  416. executeTriangleDrawing(this->buffer[i], clipBound);
  417. }
  418. }
  419. } else {
  420. std::function<void()> jobs[jobCount];
  421. int y1 = clipBound.top();
  422. for (int j = 0; j < jobCount; j++) {
  423. int y2 = clipBound.top() + ((clipBound.bottom() * (j + 1)) / jobCount);
  424. // Align to multiples of two lines if it's not at the bottom
  425. if (j < jobCount - 1) {
  426. y2 = (y2 / 2) * 2;
  427. }
  428. int height = y2 - y1;
  429. IRect subBound = IRect(clipBound.left(), y1, clipBound.width(), height);
  430. jobs[j] = [this, subBound]() {
  431. //this->execute(subBound, 1);
  432. for (int i = 0; i < this->buffer.length(); i++) {
  433. if (!this->buffer[i].occluded) {
  434. executeTriangleDrawing(this->buffer[i], subBound);
  435. }
  436. }
  437. };
  438. y1 = y2;
  439. }
  440. threadedWorkFromArray(jobs, jobCount);
  441. }
  442. }
  443. void CommandQueue::clear() {
  444. this->buffer.clear();
  445. }