Rasterizer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. // Copyright 2009 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "VideoBackends/Software/Rasterizer.h"
  4. #include <algorithm>
  5. #include <cstring>
  6. #include <vector>
  7. #include "Common/Assert.h"
  8. #include "Common/CommonTypes.h"
  9. #include "VideoBackends/Software/NativeVertexFormat.h"
  10. #include "VideoBackends/Software/SWEfbInterface.h"
  11. #include "VideoBackends/Software/Tev.h"
  12. #include "VideoCommon/BPFunctions.h"
  13. #include "VideoCommon/BPMemory.h"
  14. #include "VideoCommon/PerfQueryBase.h"
  15. #include "VideoCommon/Statistics.h"
  16. #include "VideoCommon/VideoCommon.h"
  17. namespace Rasterizer
  18. {
  19. static constexpr int BLOCK_SIZE = 2;
  20. struct SlopeContext
  21. {
  22. SlopeContext(const OutputVertexData* v0, const OutputVertexData* v1, const OutputVertexData* v2,
  23. s32 x0_, s32 y0_, s32 x_off, s32 y_off)
  24. : x0(x0_), y0(y0_)
  25. {
  26. // adjust a little less than 0.5
  27. const float adjust = 0.495f;
  28. xOff = ((float)x0_ - (v0->screenPosition.x - x_off)) + adjust;
  29. yOff = ((float)y0_ - (v0->screenPosition.y - y_off)) + adjust;
  30. dx10 = v1->screenPosition.x - v0->screenPosition.x;
  31. dx20 = v2->screenPosition.x - v0->screenPosition.x;
  32. dy10 = v1->screenPosition.y - v0->screenPosition.y;
  33. dy20 = v2->screenPosition.y - v0->screenPosition.y;
  34. }
  35. s32 x0;
  36. s32 y0;
  37. float xOff;
  38. float yOff;
  39. float dx10;
  40. float dx20;
  41. float dy10;
  42. float dy20;
  43. };
  44. struct Slope
  45. {
  46. Slope() = default;
  47. Slope(float f0_, float f1, float f2, const SlopeContext& ctx) : f0(f0_)
  48. {
  49. float delta_20 = f2 - f0_;
  50. float delta_10 = f1 - f0_;
  51. // x2 - x0 y1 - y0 x1 - x0 y2 - y0
  52. float a = delta_20 * ctx.dy10 - delta_10 * ctx.dy20;
  53. float b = ctx.dx20 * delta_10 - ctx.dx10 * delta_20;
  54. float c = ctx.dx20 * ctx.dy10 - ctx.dx10 * ctx.dy20;
  55. dfdx = a / c;
  56. dfdy = b / c;
  57. x0 = ctx.x0;
  58. y0 = ctx.y0;
  59. xOff = ctx.xOff;
  60. yOff = ctx.yOff;
  61. }
  62. // These default values are used in the unlikely case that zfreeze is enabled when drawing the
  63. // first primitive.
  64. // TODO: This is just a guess!
  65. float dfdx = 0.0f;
  66. float dfdy = 0.0f;
  67. float f0 = 1.0f;
  68. // Both an s32 value and a float value are used to minimize rounding error
  69. // TODO: is this really needed?
  70. s32 x0 = 0;
  71. s32 y0 = 0;
  72. float xOff = 0.0f;
  73. float yOff = 0.0f;
  74. float GetValue(s32 x, s32 y) const
  75. {
  76. float dx = xOff + (float)(x - x0);
  77. float dy = yOff + (float)(y - y0);
  78. return f0 + (dfdx * dx) + (dfdy * dy);
  79. }
  80. };
  81. static Slope ZSlope;
  82. static Slope WSlope;
  83. static Slope ColorSlopes[2][4];
  84. static Slope TexSlopes[8][3];
  85. static Tev tev;
  86. static RasterBlock rasterBlock;
  87. static std::vector<BPFunctions::ScissorRect> scissors;
  88. void Init()
  89. {
  90. // The other slopes are set each for each primitive drawn, but zfreeze means that the z slope
  91. // needs to be set to an (untested) default value.
  92. ZSlope = Slope();
  93. }
  94. void ScissorChanged()
  95. {
  96. scissors = std::move(BPFunctions::ComputeScissorRects().m_result);
  97. }
  98. // Returns approximation of log2(f) in s28.4
  99. // results are close enough to use for LOD
  100. static s32 FixedLog2(float f)
  101. {
  102. u32 x;
  103. std::memcpy(&x, &f, sizeof(u32));
  104. s32 logInt = ((x & 0x7F800000) >> 19) - 2032; // integer part
  105. s32 logFract = (x & 0x007fffff) >> 19; // approximate fractional part
  106. return logInt + logFract;
  107. }
  108. static inline int iround(float x)
  109. {
  110. int t = (int)x;
  111. if ((x - t) >= 0.5)
  112. return t + 1;
  113. return t;
  114. }
  115. void SetTevKonstColors()
  116. {
  117. tev.SetKonstColors();
  118. }
  119. static void Draw(s32 x, s32 y, s32 xi, s32 yi)
  120. {
  121. INCSTAT(g_stats.this_frame.rasterized_pixels);
  122. s32 z = (s32)std::clamp<float>(ZSlope.GetValue(x, y), 0.0f, 16777215.0f);
  123. if (bpmem.GetEmulatedZ() == EmulatedZ::Early)
  124. {
  125. // TODO: Test if perf regs are incremented even if test is disabled
  126. EfbInterface::IncPerfCounterQuadCount(PQ_ZCOMP_INPUT_ZCOMPLOC);
  127. if (bpmem.zmode.testenable)
  128. {
  129. // early z
  130. if (!EfbInterface::ZCompare(x, y, z))
  131. return;
  132. }
  133. EfbInterface::IncPerfCounterQuadCount(PQ_ZCOMP_OUTPUT_ZCOMPLOC);
  134. }
  135. RasterBlockPixel& pixel = rasterBlock.Pixel[xi][yi];
  136. tev.Position[0] = x;
  137. tev.Position[1] = y;
  138. tev.Position[2] = z;
  139. // colors
  140. for (unsigned int i = 0; i < bpmem.genMode.numcolchans; i++)
  141. {
  142. for (int comp = 0; comp < 4; comp++)
  143. {
  144. u16 color = (u16)ColorSlopes[i][comp].GetValue(x, y);
  145. // clamp color value to 0
  146. u16 mask = ~(color >> 8);
  147. tev.Color[i][comp] = color & mask;
  148. }
  149. }
  150. // tex coords
  151. for (unsigned int i = 0; i < bpmem.genMode.numtexgens; i++)
  152. {
  153. // multiply by 128 because TEV stores UVs as s17.7
  154. tev.Uv[i].s = (s32)(pixel.Uv[i][0] * 128);
  155. tev.Uv[i].t = (s32)(pixel.Uv[i][1] * 128);
  156. }
  157. for (unsigned int i = 0; i < bpmem.genMode.numindstages; i++)
  158. {
  159. tev.IndirectLod[i] = rasterBlock.IndirectLod[i];
  160. tev.IndirectLinear[i] = rasterBlock.IndirectLinear[i];
  161. }
  162. for (unsigned int i = 0; i <= bpmem.genMode.numtevstages; i++)
  163. {
  164. tev.TextureLod[i] = rasterBlock.TextureLod[i];
  165. tev.TextureLinear[i] = rasterBlock.TextureLinear[i];
  166. }
  167. tev.Draw();
  168. }
  169. static inline void CalculateLOD(s32* lodp, bool* linear, u32 texmap, u32 texcoord)
  170. {
  171. auto texUnit = bpmem.tex.GetUnit(texmap);
  172. // LOD calculation requires data from the texture mode for bias, etc.
  173. // it does not seem to use the actual texture size
  174. const TexMode0& tm0 = texUnit.texMode0;
  175. const TexMode1& tm1 = texUnit.texMode1;
  176. float sDelta, tDelta;
  177. float* uv00 = rasterBlock.Pixel[0][0].Uv[texcoord];
  178. float* uv10 = rasterBlock.Pixel[1][0].Uv[texcoord];
  179. float* uv01 = rasterBlock.Pixel[0][1].Uv[texcoord];
  180. float dudx = fabsf(uv00[0] - uv10[0]);
  181. float dvdx = fabsf(uv00[1] - uv10[1]);
  182. float dudy = fabsf(uv00[0] - uv01[0]);
  183. float dvdy = fabsf(uv00[1] - uv01[1]);
  184. if (tm0.diag_lod == LODType::Diagonal)
  185. {
  186. sDelta = dudx + dudy;
  187. tDelta = dvdx + dvdy;
  188. }
  189. else
  190. {
  191. sDelta = std::max(dudx, dudy);
  192. tDelta = std::max(dvdx, dvdy);
  193. }
  194. // get LOD in s28.4
  195. s32 lod = FixedLog2(std::max(sDelta, tDelta));
  196. // bias is s2.5
  197. int bias = tm0.lod_bias;
  198. bias >>= 1;
  199. lod += bias;
  200. *linear = ((lod > 0 && tm0.min_filter == FilterMode::Linear) ||
  201. (lod <= 0 && tm0.mag_filter == FilterMode::Linear));
  202. // NOTE: The order of comparisons for this clamp check matters.
  203. if (lod > static_cast<s32>(tm1.max_lod))
  204. lod = static_cast<s32>(tm1.max_lod);
  205. else if (lod < static_cast<s32>(tm1.min_lod))
  206. lod = static_cast<s32>(tm1.min_lod);
  207. *lodp = lod;
  208. }
  209. static void BuildBlock(s32 blockX, s32 blockY)
  210. {
  211. for (s32 yi = 0; yi < BLOCK_SIZE; yi++)
  212. {
  213. for (s32 xi = 0; xi < BLOCK_SIZE; xi++)
  214. {
  215. RasterBlockPixel& pixel = rasterBlock.Pixel[xi][yi];
  216. s32 x = xi + blockX;
  217. s32 y = yi + blockY;
  218. float invW = 1.0f / WSlope.GetValue(x, y);
  219. pixel.InvW = invW;
  220. // tex coords
  221. for (unsigned int i = 0; i < bpmem.genMode.numtexgens; i++)
  222. {
  223. float projection = invW;
  224. float q = TexSlopes[i][2].GetValue(x, y) * invW;
  225. if (q != 0.0f)
  226. projection = invW / q;
  227. pixel.Uv[i][0] = TexSlopes[i][0].GetValue(x, y) * projection;
  228. pixel.Uv[i][1] = TexSlopes[i][1].GetValue(x, y) * projection;
  229. }
  230. }
  231. }
  232. for (unsigned int i = 0; i < bpmem.genMode.numindstages; i++)
  233. {
  234. u32 texmap = bpmem.tevindref.getTexMap(i);
  235. u32 texcoord = bpmem.tevindref.getTexCoord(i);
  236. CalculateLOD(&rasterBlock.IndirectLod[i], &rasterBlock.IndirectLinear[i], texmap, texcoord);
  237. }
  238. for (unsigned int i = 0; i <= bpmem.genMode.numtevstages; i++)
  239. {
  240. int stageOdd = i & 1;
  241. const TwoTevStageOrders& order = bpmem.tevorders[i >> 1];
  242. if (order.getEnable(stageOdd))
  243. {
  244. u32 texmap = order.getTexMap(stageOdd);
  245. u32 texcoord = order.getTexCoord(stageOdd);
  246. CalculateLOD(&rasterBlock.TextureLod[i], &rasterBlock.TextureLinear[i], texmap, texcoord);
  247. }
  248. }
  249. }
  250. void UpdateZSlope(const OutputVertexData* v0, const OutputVertexData* v1,
  251. const OutputVertexData* v2, s32 x_off, s32 y_off)
  252. {
  253. if (!bpmem.genMode.zfreeze)
  254. {
  255. const s32 X1 = iround(16.0f * (v0->screenPosition.x - x_off)) - 9;
  256. const s32 Y1 = iround(16.0f * (v0->screenPosition.y - y_off)) - 9;
  257. const SlopeContext ctx(v0, v1, v2, (X1 + 0xF) >> 4, (Y1 + 0xF) >> 4, x_off, y_off);
  258. ZSlope = Slope(v0->screenPosition.z, v1->screenPosition.z, v2->screenPosition.z, ctx);
  259. }
  260. }
  261. static void DrawTriangleFrontFace(const OutputVertexData* v0, const OutputVertexData* v1,
  262. const OutputVertexData* v2,
  263. const BPFunctions::ScissorRect& scissor)
  264. {
  265. // The zslope should be updated now, even if the triangle is rejected by the scissor test, as
  266. // zfreeze depends on it
  267. UpdateZSlope(v0, v1, v2, scissor.x_off, scissor.y_off);
  268. // adapted from http://devmaster.net/posts/6145/advanced-rasterization
  269. // 28.4 fixed-point coordinates. rounded to nearest and adjusted to match hardware output
  270. // could also take floor and adjust -8
  271. const s32 Y1 = iround(16.0f * (v0->screenPosition.y - scissor.y_off)) - 9;
  272. const s32 Y2 = iround(16.0f * (v1->screenPosition.y - scissor.y_off)) - 9;
  273. const s32 Y3 = iround(16.0f * (v2->screenPosition.y - scissor.y_off)) - 9;
  274. const s32 X1 = iround(16.0f * (v0->screenPosition.x - scissor.x_off)) - 9;
  275. const s32 X2 = iround(16.0f * (v1->screenPosition.x - scissor.x_off)) - 9;
  276. const s32 X3 = iround(16.0f * (v2->screenPosition.x - scissor.x_off)) - 9;
  277. // Deltas
  278. const s32 DX12 = X1 - X2;
  279. const s32 DX23 = X2 - X3;
  280. const s32 DX31 = X3 - X1;
  281. const s32 DY12 = Y1 - Y2;
  282. const s32 DY23 = Y2 - Y3;
  283. const s32 DY31 = Y3 - Y1;
  284. // Fixed-point deltas
  285. const s32 FDX12 = DX12 * 16;
  286. const s32 FDX23 = DX23 * 16;
  287. const s32 FDX31 = DX31 * 16;
  288. const s32 FDY12 = DY12 * 16;
  289. const s32 FDY23 = DY23 * 16;
  290. const s32 FDY31 = DY31 * 16;
  291. // Bounding rectangle
  292. s32 minx = (std::min(std::min(X1, X2), X3) + 0xF) >> 4;
  293. s32 maxx = (std::max(std::max(X1, X2), X3) + 0xF) >> 4;
  294. s32 miny = (std::min(std::min(Y1, Y2), Y3) + 0xF) >> 4;
  295. s32 maxy = (std::max(std::max(Y1, Y2), Y3) + 0xF) >> 4;
  296. // scissor
  297. ASSERT(scissor.rect.left >= 0);
  298. ASSERT(scissor.rect.right <= static_cast<int>(EFB_WIDTH));
  299. ASSERT(scissor.rect.top >= 0);
  300. ASSERT(scissor.rect.bottom <= static_cast<int>(EFB_HEIGHT));
  301. minx = std::max(minx, scissor.rect.left);
  302. maxx = std::min(maxx, scissor.rect.right);
  303. miny = std::max(miny, scissor.rect.top);
  304. maxy = std::min(maxy, scissor.rect.bottom);
  305. if (minx >= maxx || miny >= maxy)
  306. return;
  307. // Set up the remaining slopes
  308. const SlopeContext ctx(v0, v1, v2, (X1 + 0xF) >> 4, (Y1 + 0xF) >> 4, scissor.x_off,
  309. scissor.y_off);
  310. float w[3] = {1.0f / v0->projectedPosition.w, 1.0f / v1->projectedPosition.w,
  311. 1.0f / v2->projectedPosition.w};
  312. WSlope = Slope(w[0], w[1], w[2], ctx);
  313. for (unsigned int i = 0; i < bpmem.genMode.numcolchans; i++)
  314. {
  315. for (int comp = 0; comp < 4; comp++)
  316. ColorSlopes[i][comp] = Slope(v0->color[i][comp], v1->color[i][comp], v2->color[i][comp], ctx);
  317. }
  318. for (unsigned int i = 0; i < bpmem.genMode.numtexgens; i++)
  319. {
  320. for (int comp = 0; comp < 3; comp++)
  321. {
  322. TexSlopes[i][comp] = Slope(v0->texCoords[i][comp] * w[0], v1->texCoords[i][comp] * w[1],
  323. v2->texCoords[i][comp] * w[2], ctx);
  324. }
  325. }
  326. // Half-edge constants
  327. s32 C1 = DY12 * X1 - DX12 * Y1;
  328. s32 C2 = DY23 * X2 - DX23 * Y2;
  329. s32 C3 = DY31 * X3 - DX31 * Y3;
  330. // Correct for fill convention
  331. if (DY12 < 0 || (DY12 == 0 && DX12 > 0))
  332. C1++;
  333. if (DY23 < 0 || (DY23 == 0 && DX23 > 0))
  334. C2++;
  335. if (DY31 < 0 || (DY31 == 0 && DX31 > 0))
  336. C3++;
  337. // Start in corner of 2x2 block
  338. s32 block_minx = minx & ~(BLOCK_SIZE - 1);
  339. s32 block_miny = miny & ~(BLOCK_SIZE - 1);
  340. // Loop through blocks
  341. for (s32 y = block_miny & ~(BLOCK_SIZE - 1); y < maxy; y += BLOCK_SIZE)
  342. {
  343. for (s32 x = block_minx; x < maxx; x += BLOCK_SIZE)
  344. {
  345. s32 x1_ = (x + BLOCK_SIZE - 1);
  346. s32 y1_ = (y + BLOCK_SIZE - 1);
  347. // Corners of block
  348. s32 x0 = x << 4;
  349. s32 x1 = x1_ << 4;
  350. s32 y0 = y << 4;
  351. s32 y1 = y1_ << 4;
  352. // Evaluate half-space functions
  353. bool a00 = C1 + DX12 * y0 - DY12 * x0 > 0;
  354. bool a10 = C1 + DX12 * y0 - DY12 * x1 > 0;
  355. bool a01 = C1 + DX12 * y1 - DY12 * x0 > 0;
  356. bool a11 = C1 + DX12 * y1 - DY12 * x1 > 0;
  357. int a = (a00 << 0) | (a10 << 1) | (a01 << 2) | (a11 << 3);
  358. bool b00 = C2 + DX23 * y0 - DY23 * x0 > 0;
  359. bool b10 = C2 + DX23 * y0 - DY23 * x1 > 0;
  360. bool b01 = C2 + DX23 * y1 - DY23 * x0 > 0;
  361. bool b11 = C2 + DX23 * y1 - DY23 * x1 > 0;
  362. int b = (b00 << 0) | (b10 << 1) | (b01 << 2) | (b11 << 3);
  363. bool c00 = C3 + DX31 * y0 - DY31 * x0 > 0;
  364. bool c10 = C3 + DX31 * y0 - DY31 * x1 > 0;
  365. bool c01 = C3 + DX31 * y1 - DY31 * x0 > 0;
  366. bool c11 = C3 + DX31 * y1 - DY31 * x1 > 0;
  367. int c = (c00 << 0) | (c10 << 1) | (c01 << 2) | (c11 << 3);
  368. // Skip block when outside an edge
  369. if (a == 0x0 || b == 0x0 || c == 0x0)
  370. continue;
  371. BuildBlock(x, y);
  372. // Accept whole block when totally covered
  373. // We still need to check min/max x/y because of the scissor
  374. if (a == 0xF && b == 0xF && c == 0xF && x >= minx && x1_ < maxx && y >= miny && y1_ < maxy)
  375. {
  376. for (s32 iy = 0; iy < BLOCK_SIZE; iy++)
  377. {
  378. for (s32 ix = 0; ix < BLOCK_SIZE; ix++)
  379. {
  380. Draw(x + ix, y + iy, ix, iy);
  381. }
  382. }
  383. }
  384. else // Partially covered block
  385. {
  386. s32 CY1 = C1 + DX12 * y0 - DY12 * x0;
  387. s32 CY2 = C2 + DX23 * y0 - DY23 * x0;
  388. s32 CY3 = C3 + DX31 * y0 - DY31 * x0;
  389. for (s32 iy = 0; iy < BLOCK_SIZE; iy++)
  390. {
  391. s32 CX1 = CY1;
  392. s32 CX2 = CY2;
  393. s32 CX3 = CY3;
  394. for (s32 ix = 0; ix < BLOCK_SIZE; ix++)
  395. {
  396. if (CX1 > 0 && CX2 > 0 && CX3 > 0)
  397. {
  398. // This check enforces the scissor rectangle, since it might not be aligned with the
  399. // blocks
  400. if (x + ix >= minx && x + ix < maxx && y + iy >= miny && y + iy < maxy)
  401. Draw(x + ix, y + iy, ix, iy);
  402. }
  403. CX1 -= FDY12;
  404. CX2 -= FDY23;
  405. CX3 -= FDY31;
  406. }
  407. CY1 += FDX12;
  408. CY2 += FDX23;
  409. CY3 += FDX31;
  410. }
  411. }
  412. }
  413. }
  414. }
  415. void DrawTriangleFrontFace(const OutputVertexData* v0, const OutputVertexData* v1,
  416. const OutputVertexData* v2)
  417. {
  418. INCSTAT(g_stats.this_frame.num_triangles_drawn);
  419. for (const auto& scissor : scissors)
  420. DrawTriangleFrontFace(v0, v1, v2, scissor);
  421. }
  422. } // namespace Rasterizer