renderCore.cpp 20 KB

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