Debian Codegraph @

Source/Core/VideoBackends/Software/Rasterizer.cpp

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