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 "../base/virtualStack.h"
  26. #include "shader/RgbaMultiply.h"
  27. #include "constants.h"
  28. using namespace dsr;
  29. class SubVertex {
  30. public:
  31. FVector3D cs; // Camera space position based on the weights
  32. float subB, subC; // Weights for second and third vertices in the parent triangle
  33. int state = 0; // Used by algorithms
  34. float value = 0.0f; // Used by algorithms
  35. SubVertex() : cs(FVector3D()), subB(0.0f), subC(0.0f) {}
  36. SubVertex(FVector3D cs, float subB, float subC) : cs(cs), subB(subB), subC(subC) {}
  37. SubVertex(SubVertex vertexA, SubVertex vertexB, float ratio) {
  38. float invRatio = 1.0f - ratio;
  39. this->cs = vertexA.cs * invRatio + vertexB.cs * ratio;
  40. this->subB = vertexA.subB * invRatio + vertexB.subB * ratio;
  41. this->subC = vertexA.subC * invRatio + vertexB.subC * ratio;
  42. }
  43. };
  44. // Returns 0 when value = a
  45. // Returns 0.5 when value = (a + b) / 2
  46. // Returns 1 when value = b
  47. static float inverseLerp(float a, float b, float value) {
  48. float c = b - a;
  49. if (c == 0.0f) {
  50. return 0.5f;
  51. } else {
  52. return (value - a) / c;
  53. }
  54. }
  55. 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
  56. class ClippedTriangle {
  57. private:
  58. int vertexCount = 0;
  59. public:
  60. int getVertexCount() {
  61. return this->vertexCount;
  62. }
  63. SubVertex vertices[maxPoints] = {};
  64. ClippedTriangle(const ITriangle2D &triangle) {
  65. this->vertices[0] = SubVertex(triangle.position[0].cs, 0.0f, 0.0f);
  66. this->vertices[1] = SubVertex(triangle.position[1].cs, 1.0f, 0.0f);
  67. this->vertices[2] = SubVertex(triangle.position[2].cs, 0.0f, 1.0f);
  68. this->vertexCount = 3;
  69. }
  70. void deleteVertex(int removeIndex) {
  71. assert(removeIndex >= 0);
  72. assert(removeIndex < this->vertexCount);
  73. if (this->vertexCount > 0 && this->vertexCount <= maxPoints) {
  74. for (int v = removeIndex; v < this->vertexCount - 1; v++) {
  75. assert(v >= 0 && v + 1 < maxPoints);
  76. this->vertices[v] = this->vertices[v + 1];
  77. }
  78. this->vertexCount--;
  79. }
  80. }
  81. void insertVertex(int newIndex, const SubVertex &newVertex) {
  82. // Check against buffer overflow in case of bugs from rounding errors
  83. assert(newIndex >= 0);
  84. assert(newIndex <= this->vertexCount);
  85. if (this->vertexCount < maxPoints) {
  86. for (int v = this->vertexCount - 1; v >= newIndex; v--) {
  87. assert(v >= 0 && v + 1 < maxPoints);
  88. this->vertices[v + 1] = this->vertices[v];
  89. }
  90. this->vertices[newIndex] = newVertex;
  91. this->vertexCount++;
  92. }
  93. }
  94. void deleteAll() {
  95. this->vertexCount = 0;
  96. }
  97. // Cut away parts of the triangle that are on the positive side of the plane
  98. void clip(const FPlane3D &plane) {
  99. static const int state_use = 0;
  100. static const int state_delete = 1;
  101. static const int state_modified = 2;
  102. if (this->vertexCount >= 3 && this->vertexCount < maxPoints) {
  103. int outsideCount = 0;
  104. int lastOutside = 0;
  105. for (int v = 0; v < this->vertexCount; v++) {
  106. assert(v >= 0 && v < maxPoints);
  107. float distance = plane.signedDistance(this->vertices[v].cs);
  108. this->vertices[v].value = distance;
  109. if (distance > 0.0f) {
  110. outsideCount++;
  111. lastOutside = v;
  112. this->vertices[v].state = state_delete;
  113. } else {
  114. this->vertices[v].state = state_use;
  115. }
  116. }
  117. if (outsideCount > 0) {
  118. if (outsideCount >= this->vertexCount) {
  119. this->deleteAll();
  120. } else if (outsideCount == 1) {
  121. // Split a single vertex into two corners by interpolating with the previous and next corners
  122. int currentVertex = lastOutside;
  123. int previousVertex = (lastOutside - 1 + this->vertexCount) % this->vertexCount;
  124. int nextVertex = (lastOutside + 1) % this->vertexCount;
  125. float previousToCurrentRatio = inverseLerp(this->vertices[previousVertex].value, this->vertices[currentVertex].value, 0.0f);
  126. float currentToNextRatio = inverseLerp(this->vertices[currentVertex].value, this->vertices[nextVertex].value, 0.0f);
  127. SubVertex cutStart(this->vertices[previousVertex], this->vertices[currentVertex], previousToCurrentRatio);
  128. SubVertex cutEnd(this->vertices[currentVertex], this->vertices[nextVertex], currentToNextRatio);
  129. this->vertices[lastOutside] = cutStart;
  130. insertVertex(nextVertex, cutEnd);
  131. } else {
  132. // Start and end of the cut
  133. for (int currentVertex = 0; currentVertex < this->vertexCount; currentVertex++) {
  134. int previousVertex = (currentVertex - 1 + this->vertexCount) % this->vertexCount;
  135. int nextVertex = (currentVertex + 1) % this->vertexCount;
  136. if (this->vertices[currentVertex].state == state_delete) {
  137. if (this->vertices[previousVertex].state == state_use) {
  138. // Begin the cut
  139. float previousToCurrentRatio = inverseLerp(this->vertices[previousVertex].value, this->vertices[currentVertex].value, 0.0f);
  140. this->vertices[currentVertex] = SubVertex(this->vertices[previousVertex], this->vertices[currentVertex], previousToCurrentRatio);
  141. this->vertices[currentVertex].state = state_modified;
  142. } else if (this->vertices[nextVertex].state == state_use) {
  143. // End the cut
  144. float currentToNextRatio = inverseLerp(this->vertices[currentVertex].value, this->vertices[nextVertex].value, 0.0f);
  145. this->vertices[currentVertex] = SubVertex(this->vertices[currentVertex], this->vertices[nextVertex], currentToNextRatio);
  146. this->vertices[currentVertex].state = state_modified;
  147. }
  148. }
  149. }
  150. // Delete every vertex that is marked for removal
  151. // Looping backwards will avoid using altered indices while deleting
  152. if (outsideCount > 2) {
  153. for (int v = this->vertexCount - 1; v >= 0; v--) {
  154. assert(v >= 0 && v < maxPoints);
  155. if (this->vertices[v].state == state_delete) {
  156. this->deleteVertex(v);
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. }
  164. };
  165. Visibility dsr::getTriangleVisibility(const ITriangle2D &triangle, const Camera &camera, bool clipFrustum) {
  166. static const int cornerCount = 3;
  167. int planeCount = camera.getFrustumPlaneCount(clipFrustum);
  168. VirtualStackAllocation<bool> outside(cornerCount * planeCount, "Corner outside buffer in getTriangleVIsibility");
  169. // Check which corners are outside of the different planes
  170. int offset = 0;
  171. for (int c = 0; c < cornerCount; c++) {
  172. FVector3D triangleCorner = triangle.position[c].cs;
  173. for (int s = 0; s < planeCount; s++) {
  174. outside[offset + s] = !(camera.getFrustumPlane(s, clipFrustum).inside(triangleCorner));
  175. }
  176. offset += planeCount;
  177. }
  178. // Do not render if all corners are outside of the same side
  179. for (int s = 0; s < planeCount; s++) {
  180. if (outside[s] && outside[s + planeCount] && outside[s + 2 * planeCount]) {
  181. return Visibility::Hidden; // All corners outside of the same plane
  182. }
  183. }
  184. // Partial visibility if any corner is outside of a side
  185. for (int i = 0; i < planeCount * cornerCount; i++) {
  186. if (outside[i]) {
  187. return Visibility::Partial; // Any corner outside of a plane
  188. }
  189. }
  190. return Visibility::Full;
  191. }
  192. static const int alignX = 2;
  193. static const int alignY = 2;
  194. void dsr::executeTriangleDrawing(const TriangleDrawCommand &command, const IRect &clipBound) {
  195. IRect finalClipBound = IRect::cut(command.clipBound, clipBound);
  196. int32_t rowCount = command.triangle.getBufferSize(finalClipBound, alignX, alignY);
  197. if (rowCount > 0) {
  198. int startRow;
  199. // TODO: Use SafePointer in shape functions.
  200. VirtualStackAllocation<RowInterval> rows(rowCount, "Row intervals in executeTriangleDrawing");
  201. command.triangle.getShape(startRow, rows.getUnsafe(), 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.getUnsafe()), 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. static void 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, ImageRgbaU8 *targetImage, ImageF32 *depthBuffer,
  272. const Camera &camera, const ProjectedPoint &posA, const ProjectedPoint &posB, const ProjectedPoint &posC,
  273. Filter filter, const TextureRgbaU8 *diffuse, const TextureRgbaU8 *light,
  274. TriangleTexCoords texCoords, TriangleColors colors) {
  275. // Get dimensions from both buffers
  276. int colorWidth = targetImage != nullptr ? image_getWidth(*targetImage) : 0;
  277. int colorHeight = targetImage != nullptr ? image_getHeight(*targetImage) : 0;
  278. int depthWidth = depthBuffer != nullptr ? image_getWidth(*depthBuffer) : 0;
  279. int depthHeight = depthBuffer != nullptr ? image_getHeight(*depthBuffer) : 0;
  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(ImageF32 *depthBuffer, const ITriangle2D& triangle, const IRect &clipBound) {
  312. int32_t rowCount = triangle.getBufferSize(clipBound, 1, 1);
  313. if (rowCount > 0) {
  314. int startRow;
  315. VirtualStackAllocation<RowInterval> rows(rowCount, "Row intervals in executeTriangleDrawingDepth");
  316. triangle.getShape(startRow, rows.getUnsafe(), 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.getUnsafe());
  319. // Draw the triangle
  320. const int depthBufferStride = image_getStride(*depthBuffer);
  321. SafePointer<float> depthDataRow = image_getSafePointer<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(ImageF32 *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(ImageF32 *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(ImageF32 *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(image_getWidth(*depthBuffer), image_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. VirtualStackAllocation<std::function<void()>> jobs(jobCount, "Triangle draw jobs in CommandQueue::execute");
  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. }