| 1 | // Copyright 2009 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | /* |
| 5 | Portions of this file are based off work by Markus Trenkwalder. |
| 6 | Copyright (c) 2007, 2008 Markus Trenkwalder |
| 7 | |
| 8 | All rights reserved. |
| 9 | |
| 10 | Redistribution and use in source and binary forms, with or without |
| 11 | modification, are permitted provided that the following conditions are met: |
| 12 | |
| 13 | * Redistributions of source code must retain the above copyright notice, |
| 14 | this list of conditions and the following disclaimer. |
| 15 | |
| 16 | * Redistributions in binary form must reproduce the above copyright notice, |
| 17 | this list of conditions and the following disclaimer in the documentation |
| 18 | and/or other materials provided with the distribution. |
| 19 | |
| 20 | * Neither the name of the library's copyright owner nor the names of its |
| 21 | contributors may be used to endorse or promote products derived from this |
| 22 | software without specific prior written permission. |
| 23 | |
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 25 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 26 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 27 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 28 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 29 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 30 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 31 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 32 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 33 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
| 36 | |
| 37 | #include "VideoBackends/Software/Clipper.h" |
| 38 | |
| 39 | #include "Common/Assert.h" |
| 40 | |
| 41 | #include "VideoBackends/Software/NativeVertexFormat.h" |
| 42 | #include "VideoBackends/Software/Rasterizer.h" |
| 43 | |
| 44 | #include "VideoCommon/BPMemory.h" |
| 45 | #include "VideoCommon/Statistics.h" |
| 46 | #include "VideoCommon/XFMemory.h" |
| 47 | |
| 48 | namespace Clipper |
| 49 | { |
| 50 | enum |
| 51 | { |
| 52 | NUM_CLIPPED_VERTICES = 33, |
| 53 | NUM_INDICES = NUM_CLIPPED_VERTICES + 3 |
| 54 | }; |
| 55 | |
| 56 | static OutputVertexData ClippedVertices[NUM_CLIPPED_VERTICES]; |
| 57 | static OutputVertexData* Vertices[NUM_INDICES]; |
| 58 | |
| 59 | void Init() |
| 60 | { |
| 61 | for (int i = 0; i < NUM_CLIPPED_VERTICES; ++i) |
| 62 | Vertices[i + 3] = &ClippedVertices[i]; |
| 63 | } |
| 64 | |
| 65 | enum |
| 66 | { |
| 67 | SKIP_FLAG = -1, |
| 68 | CLIP_POS_X_BIT = 0x01, |
| 69 | CLIP_NEG_X_BIT = 0x02, |
| 70 | CLIP_POS_Y_BIT = 0x04, |
| 71 | CLIP_NEG_Y_BIT = 0x08, |
| 72 | CLIP_POS_Z_BIT = 0x10, |
| 73 | CLIP_NEG_Z_BIT = 0x20 |
| 74 | }; |
| 75 | |
| 76 | static inline int CalcClipMask(const OutputVertexData* v) |
| 77 | { |
| 78 | int cmask = 0; |
| 79 | Common::Vec4 pos = v->projectedPosition; |
| 80 | |
| 81 | if (pos.w - pos.x < 0) |
| 82 | cmask |= CLIP_POS_X_BIT; |
| 83 | |
| 84 | if (pos.x + pos.w < 0) |
| 85 | cmask |= CLIP_NEG_X_BIT; |
| 86 | |
| 87 | if (pos.w - pos.y < 0) |
| 88 | cmask |= CLIP_POS_Y_BIT; |
| 89 | |
| 90 | if (pos.y + pos.w < 0) |
| 91 | cmask |= CLIP_NEG_Y_BIT; |
| 92 | |
| 93 | if (pos.w * pos.z > 0) |
| 94 | cmask |= CLIP_POS_Z_BIT; |
| 95 | |
| 96 | if (pos.z + pos.w < 0) |
| 97 | cmask |= CLIP_NEG_Z_BIT; |
| 98 | |
| 99 | return cmask; |
| 100 | } |
| 101 | |
| 102 | static inline void AddInterpolatedVertex(float t, int out, int in, int* numVertices) |
| 103 | { |
| 104 | Vertices[(*numVertices)++]->Lerp(t, Vertices[out], Vertices[in]); |
| 105 | } |
| 106 | |
| 107 | #define DIFFERENT_SIGNS(x, y) ((x <= 0 && y > 0) || (x > 0 && y <= 0)) |
| 108 | |
| 109 | #define CLIP_DOTPROD(I, A, B, C, D) \ |
| 110 | (Vertices[I]->projectedPosition.x * A + Vertices[I]->projectedPosition.y * B + \ |
| 111 | Vertices[I]->projectedPosition.z * C + Vertices[I]->projectedPosition.w * D) |
| 112 | |
| 113 | #define POLY_CLIP(PLANE_BIT, A, B, C, D) \ |
| 114 | { \ |
| 115 | if (mask & PLANE_BIT) \ |
| 116 | { \ |
| 117 | int idxPrev = inlist[0]; \ |
| 118 | float dpPrev = CLIP_DOTPROD(idxPrev, A, B, C, D); \ |
| 119 | int outcount = 0; \ |
| 120 | \ |
| 121 | inlist[n] = inlist[0]; \ |
| 122 | for (int j = 1; j <= n; j++) \ |
| 123 | { \ |
| 124 | int idx = inlist[j]; \ |
| 125 | float dp = CLIP_DOTPROD(idx, A, B, C, D); \ |
| 126 | if (dpPrev >= 0) \ |
| 127 | { \ |
| 128 | outlist[outcount++] = idxPrev; \ |
| 129 | } \ |
| 130 | \ |
| 131 | if (DIFFERENT_SIGNS(dp, dpPrev)) \ |
| 132 | { \ |
| 133 | if (dp < 0) \ |
| 134 | { \ |
| 135 | float t = dp / (dp - dpPrev); \ |
| 136 | AddInterpolatedVertex(t, idx, idxPrev, &numVertices); \ |
| 137 | } \ |
| 138 | else \ |
| 139 | { \ |
| 140 | float t = dpPrev / (dpPrev - dp); \ |
| 141 | AddInterpolatedVertex(t, idxPrev, idx, &numVertices); \ |
| 142 | } \ |
| 143 | outlist[outcount++] = numVertices - 1; \ |
| 144 | } \ |
| 145 | \ |
| 146 | idxPrev = idx; \ |
| 147 | dpPrev = dp; \ |
| 148 | } \ |
| 149 | \ |
| 150 | if (outcount < 3) \ |
| 151 | continue; \ |
| 152 | \ |
| 153 | { \ |
| 154 | int* tmp = inlist; \ |
| 155 | inlist = outlist; \ |
| 156 | outlist = tmp; \ |
| 157 | n = outcount; \ |
| 158 | } \ |
| 159 | } \ |
| 160 | } |
| 161 | |
| 162 | #define LINE_CLIP(PLANE_BIT, A, B, C, D) \ |
| 163 | { \ |
| 164 | if (mask & PLANE_BIT) \ |
| 165 | { \ |
| 166 | const float dp0 = CLIP_DOTPROD(0, A, B, C, D); \ |
| 167 | const float dp1 = CLIP_DOTPROD(1, A, B, C, D); \ |
| 168 | const bool neg_dp0 = dp0 < 0; \ |
| 169 | const bool neg_dp1 = dp1 < 0; \ |
| 170 | \ |
| 171 | if (neg_dp0 && neg_dp1) \ |
| 172 | return; \ |
| 173 | \ |
| 174 | if (neg_dp1) \ |
| 175 | { \ |
| 176 | float t = dp1 / (dp1 - dp0); \ |
| 177 | if (t > t1) \ |
| 178 | t1 = t; \ |
| 179 | } \ |
| 180 | else if (neg_dp0) \ |
| 181 | { \ |
| 182 | float t = dp0 / (dp0 - dp1); \ |
| 183 | if (t > t0) \ |
| 184 | t0 = t; \ |
| 185 | } \ |
| 186 | } \ |
| 187 | } |
| 188 | |
| 189 | static void ClipTriangle(int* indices, int* numIndices) |
| 190 | { |
| 191 | int mask = 0; |
| 192 | |
| 193 | mask |= CalcClipMask(Vertices[0]); |
| 194 | mask |= CalcClipMask(Vertices[1]); |
| 195 | mask |= CalcClipMask(Vertices[2]); |
| 196 | |
| 197 | if (mask != 0) |
| 198 | { |
| 199 | for (int i = 0; i < 3; i += 3) |
| 200 | { |
| 201 | int vlist[2][2 * 6 + 1]; |
| 202 | int *inlist = vlist[0], *outlist = vlist[1]; |
| 203 | int n = 3; |
| 204 | int numVertices = 3; |
| 205 | |
| 206 | inlist[0] = 0; |
| 207 | inlist[1] = 1; |
| 208 | inlist[2] = 2; |
| 209 | |
| 210 | // mark this triangle as unused in case it should be completely |
| 211 | // clipped |
| 212 | indices[0] = SKIP_FLAG; |
| 213 | indices[1] = SKIP_FLAG; |
| 214 | indices[2] = SKIP_FLAG; |
| 215 | |
| 216 | POLY_CLIP(CLIP_POS_X_BIT, -1, 0, 0, 1); |
| 217 | POLY_CLIP(CLIP_NEG_X_BIT, 1, 0, 0, 1); |
| 218 | POLY_CLIP(CLIP_POS_Y_BIT, 0, -1, 0, 1); |
| 219 | POLY_CLIP(CLIP_NEG_Y_BIT, 0, 1, 0, 1); |
| 220 | POLY_CLIP(CLIP_POS_Z_BIT, 0, 0, 0, 1); |
| 221 | POLY_CLIP(CLIP_NEG_Z_BIT, 0, 0, 1, 1); |
| 222 | |
| 223 | INCSTAT(g_stats.this_frame.num_triangles_clipped); |
| 224 | |
| 225 | // transform the poly in inlist into triangles |
| 226 | indices[0] = inlist[0]; |
| 227 | indices[1] = inlist[1]; |
| 228 | indices[2] = inlist[2]; |
| 229 | for (int j = 3; j < n; ++j) |
| 230 | { |
| 231 | indices[(*numIndices)++] = inlist[0]; |
| 232 | indices[(*numIndices)++] = inlist[j - 1]; |
| 233 | indices[(*numIndices)++] = inlist[j]; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | static void ClipLine(int* indices) |
| 240 | { |
| 241 | int mask = 0; |
| 242 | int clip_mask[2] = {0, 0}; |
| 243 | |
| 244 | for (int i = 0; i < 2; ++i) |
| 245 | { |
| 246 | clip_mask[i] = CalcClipMask(Vertices[i]); |
| 247 | mask |= clip_mask[i]; |
| 248 | } |
| 249 | |
| 250 | if (mask == 0) |
| 251 | return; |
| 252 | |
| 253 | float t0 = 0; |
| 254 | float t1 = 0; |
| 255 | |
| 256 | // Mark unused in case of early termination |
| 257 | // of the macros below. (When fully clipped) |
| 258 | indices[0] = SKIP_FLAG; |
| 259 | indices[1] = SKIP_FLAG; |
| 260 | |
| 261 | LINE_CLIP(CLIP_POS_X_BIT, -1, 0, 0, 1); |
| 262 | LINE_CLIP(CLIP_NEG_X_BIT, 1, 0, 0, 1); |
| 263 | LINE_CLIP(CLIP_POS_Y_BIT, 0, -1, 0, 1); |
| 264 | LINE_CLIP(CLIP_NEG_Y_BIT, 0, 1, 0, 1); |
| 265 | LINE_CLIP(CLIP_POS_Z_BIT, 0, 0, -1, 1); |
| 266 | LINE_CLIP(CLIP_NEG_Z_BIT, 0, 0, 1, 1); |
| 267 | |
| 268 | // Restore the old values as this line |
| 269 | // was not fully clipped. |
| 270 | indices[0] = 0; |
| 271 | indices[1] = 1; |
| 272 | |
| 273 | int numVertices = 2; |
| 274 | |
| 275 | if (clip_mask[0]) |
| 276 | { |
| 277 | indices[0] = numVertices; |
| 278 | AddInterpolatedVertex(t0, 0, 1, &numVertices); |
| 279 | } |
| 280 | |
| 281 | if (clip_mask[1]) |
| 282 | { |
| 283 | indices[1] = numVertices; |
| 284 | AddInterpolatedVertex(t1, 1, 0, &numVertices); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | void ProcessTriangle(OutputVertexData* v0, OutputVertexData* v1, OutputVertexData* v2) |
| 289 | { |
| 290 | INCSTAT(g_stats.this_frame.num_triangles_in); |
| 291 | |
| 292 | if (IsTriviallyRejected(v0, v1, v2)) |
| 293 | { |
| 294 | INCSTAT(g_stats.this_frame.num_triangles_rejected); |
| 295 | // NOTE: The slope used by zfreeze shouldn't be updated if the triangle is |
| 296 | // trivially rejected during clipping |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | bool backface = IsBackface(v0, v1, v2); |
| 301 | |
| 302 | if (!backface) |
| 303 | { |
| 304 | if (bpmem.genMode.cull_mode == CullMode::Back || bpmem.genMode.cull_mode == CullMode::All) |
| 305 | { |
| 306 | // cull frontfacing - we still need to update the slope for zfreeze |
| 307 | PerspectiveDivide(v0); |
| 308 | PerspectiveDivide(v1); |
| 309 | PerspectiveDivide(v2); |
| 310 | Rasterizer::UpdateZSlope(v0, v1, v2, bpmem.scissorOffset.x * 2, bpmem.scissorOffset.y * 2); |
| 311 | INCSTAT(g_stats.this_frame.num_triangles_culled); |
| 312 | return; |
| 313 | } |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | if (bpmem.genMode.cull_mode == CullMode::Front || bpmem.genMode.cull_mode == CullMode::All) |
| 318 | { |
| 319 | // cull backfacing - we still need to update the slope for zfreeze |
| 320 | PerspectiveDivide(v0); |
| 321 | PerspectiveDivide(v2); |
| 322 | PerspectiveDivide(v1); |
| 323 | Rasterizer::UpdateZSlope(v0, v2, v1, bpmem.scissorOffset.x * 2, bpmem.scissorOffset.y * 2); |
| 324 | INCSTAT(g_stats.this_frame.num_triangles_culled); |
| 325 | return; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | int indices[NUM_INDICES] = {0, 1, 2, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, |
| 330 | SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, |
| 331 | SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, |
| 332 | SKIP_FLAG, SKIP_FLAG, SKIP_FLAG}; |
| 333 | int numIndices = 3; |
| 334 | |
| 335 | if (backface) |
| 336 | { |
| 337 | Vertices[0] = v0; |
| 338 | Vertices[1] = v2; |
| 339 | Vertices[2] = v1; |
| 340 | } |
| 341 | else |
| 342 | { |
| 343 | Vertices[0] = v0; |
| 344 | Vertices[1] = v1; |
| 345 | Vertices[2] = v2; |
| 346 | } |
| 347 | |
| 348 | // TODO: behavior when disable_clipping_detection is set doesn't quite match actual hardware; |
| 349 | // there does still seem to be a maximum size after which things are clipped. Also, currently |
| 350 | // when clipping is enabled triangles are clipped to exactly the viewport, but on hardware there |
| 351 | // is a guardband (and with certain scissor configurations, things can show up in it) |
| 352 | // Mario Party 8 in widescreen breaks without this: https://bugs.dolphin-emu.org/issues/12769 |
| 353 | bool skip_clipping = false; |
| 354 | if (xfmem.clipDisable.disable_clipping_detection) |
| 355 | { |
| 356 | // If any w coordinate is negative, then the perspective divide will flip coordinates, breaking |
| 357 | // various assumptions (including backface). So, we still need to do clipping in that case. |
| 358 | // This isn't the actual condition hardware uses. |
| 359 | if (Vertices[0]->projectedPosition.w >= 0 && Vertices[1]->projectedPosition.w >= 0 && |
| 360 | Vertices[2]->projectedPosition.w >= 0) |
| 361 | skip_clipping = true; |
| 362 | } |
| 363 | |
| 364 | if (!skip_clipping) |
| 365 | ClipTriangle(indices, &numIndices); |
| 366 | |
| 367 | for (int i = 0; i + 3 <= numIndices; i += 3) |
| 368 | { |
| 369 | ASSERT(i < NUM_INDICES); |
| 370 | if (indices[i] != SKIP_FLAG) |
| 371 | { |
| 372 | PerspectiveDivide(Vertices[indices[i]]); |
| 373 | PerspectiveDivide(Vertices[indices[i + 1]]); |
| 374 | PerspectiveDivide(Vertices[indices[i + 2]]); |
| 375 | |
| 376 | Rasterizer::DrawTriangleFrontFace(Vertices[indices[i]], Vertices[indices[i + 1]], |
| 377 | Vertices[indices[i + 2]]); |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | constexpr std::array<float, 8> LINE_PT_TEX_OFFSETS = { |
| 383 | 0, 1 / 16.f, 1 / 8.f, 1 / 4.f, 1 / 2.f, 1, 1, 1, |
| 384 | }; |
| 385 | |
| 386 | static void CopyLineVertex(OutputVertexData* dst, const OutputVertexData* src, int px, int py, |
| 387 | bool apply_line_offset) |
| 388 | { |
| 389 | const float line_half_width = bpmem.lineptwidth.linesize / 12.0f; |
| 390 | |
| 391 | dst->projectedPosition = src->projectedPosition; |
| 392 | dst->screenPosition.x = src->screenPosition.x + px * line_half_width; |
| 393 | dst->screenPosition.y = src->screenPosition.y + py * line_half_width; |
| 394 | dst->screenPosition.z = src->screenPosition.z; |
| 395 | |
| 396 | dst->normal = src->normal; |
| 397 | dst->color = src->color; |
| 398 | |
| 399 | dst->texCoords = src->texCoords; |
| 400 | |
| 401 | if (apply_line_offset && LINE_PT_TEX_OFFSETS[bpmem.lineptwidth.lineoff] != 0) |
| 402 | { |
| 403 | for (u32 coord_num = 0; coord_num < xfmem.numTexGen.numTexGens; coord_num++) |
| 404 | { |
| 405 | if (bpmem.texcoords[coord_num].s.line_offset) |
| 406 | { |
| 407 | dst->texCoords[coord_num].x += (bpmem.texcoords[coord_num].s.scale_minus_1 + 1) * |
| 408 | LINE_PT_TEX_OFFSETS[bpmem.lineptwidth.lineoff]; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | void ProcessLine(OutputVertexData* lineV0, OutputVertexData* lineV1) |
| 415 | { |
| 416 | int indices[4] = {0, 1, SKIP_FLAG, SKIP_FLAG}; |
| 417 | |
| 418 | Vertices[0] = lineV0; |
| 419 | Vertices[1] = lineV1; |
| 420 | |
| 421 | // point to a valid vertex to store to when clipping |
| 422 | Vertices[2] = &ClippedVertices[17]; |
| 423 | |
| 424 | ClipLine(indices); |
| 425 | |
| 426 | if (indices[0] != SKIP_FLAG) |
| 427 | { |
| 428 | OutputVertexData* v0 = Vertices[indices[0]]; |
| 429 | OutputVertexData* v1 = Vertices[indices[1]]; |
| 430 | |
| 431 | PerspectiveDivide(v0); |
| 432 | PerspectiveDivide(v1); |
| 433 | |
| 434 | const float dx = v1->screenPosition.x - v0->screenPosition.x; |
| 435 | const float dy = v1->screenPosition.y - v0->screenPosition.y; |
| 436 | |
| 437 | int px = 0; |
| 438 | int py = 0; |
| 439 | |
| 440 | // GameCube/Wii's line drawing algorithm is a little quirky. It does not |
| 441 | // use the correct line caps. Instead, the line caps are vertical or |
| 442 | // horizontal depending the slope of the line. |
| 443 | // FIXME: What does real hardware do when line is at a 45-degree angle? |
| 444 | |
| 445 | // Note that py or px are set positive or negative to ensure that the triangles are drawn ccw. |
| 446 | if (fabsf(dx) > fabsf(dy)) |
| 447 | py = (dx > 0) ? -1 : 1; |
| 448 | else |
| 449 | px = (dy > 0) ? 1 : -1; |
| 450 | |
| 451 | OutputVertexData triangle[3]; |
| 452 | |
| 453 | CopyLineVertex(&triangle[0], v0, px, py, false); |
| 454 | CopyLineVertex(&triangle[1], v1, px, py, false); |
| 455 | CopyLineVertex(&triangle[2], v1, -px, -py, true); |
| 456 | |
| 457 | // ccw winding |
| 458 | Rasterizer::DrawTriangleFrontFace(&triangle[2], &triangle[1], &triangle[0]); |
| 459 | |
| 460 | CopyLineVertex(&triangle[1], v0, -px, -py, true); |
| 461 | |
| 462 | Rasterizer::DrawTriangleFrontFace(&triangle[0], &triangle[1], &triangle[2]); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | static void CopyPointVertex(OutputVertexData* dst, const OutputVertexData* src, bool px, bool py) |
| 467 | { |
| 468 | const float point_radius = bpmem.lineptwidth.pointsize / 12.0f; |
| 469 | |
| 470 | dst->projectedPosition = src->projectedPosition; |
| 471 | dst->screenPosition.x = src->screenPosition.x + (px ? 1 : -1) * point_radius; |
| 472 | dst->screenPosition.y = src->screenPosition.y + (py ? 1 : -1) * point_radius; |
| 473 | dst->screenPosition.z = src->screenPosition.z; |
| 474 | |
| 475 | dst->normal = src->normal; |
| 476 | dst->color = src->color; |
| 477 | |
| 478 | dst->texCoords = src->texCoords; |
| 479 | |
| 480 | const float point_offset = LINE_PT_TEX_OFFSETS[bpmem.lineptwidth.pointoff]; |
| 481 | if (point_offset != 0) |
| 482 | { |
| 483 | for (u32 coord_num = 0; coord_num < xfmem.numTexGen.numTexGens; coord_num++) |
| 484 | { |
| 485 | const auto coord_info = bpmem.texcoords[coord_num]; |
| 486 | if (coord_info.s.point_offset) |
| 487 | { |
| 488 | if (px) |
| 489 | dst->texCoords[coord_num].x += (coord_info.s.scale_minus_1 + 1) * point_offset; |
| 490 | if (py) |
| 491 | dst->texCoords[coord_num].y += (coord_info.t.scale_minus_1 + 1) * point_offset; |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | void ProcessPoint(OutputVertexData* center) |
| 498 | { |
| 499 | // TODO: This isn't actually doing any clipping |
| 500 | PerspectiveDivide(center); |
| 501 | |
| 502 | OutputVertexData ll, lr, ul, ur; |
| 503 | |
| 504 | CopyPointVertex(&ll, center, false, false); |
| 505 | CopyPointVertex(&lr, center, true, false); |
| 506 | CopyPointVertex(&ur, center, true, true); |
| 507 | CopyPointVertex(&ul, center, false, true); |
| 508 | |
| 509 | Rasterizer::DrawTriangleFrontFace(&ll, &ul, &lr); |
| 510 | Rasterizer::DrawTriangleFrontFace(&ur, &lr, &ul); |
| 511 | } |
| 512 | |
| 513 | bool IsTriviallyRejected(const OutputVertexData* v0, const OutputVertexData* v1, |
| 514 | const OutputVertexData* v2) |
| 515 | { |
| 516 | int mask = CalcClipMask(v0); |
| 517 | mask &= CalcClipMask(v1); |
| 518 | mask &= CalcClipMask(v2); |
| 519 | |
| 520 | return mask != 0; |
| 521 | } |
| 522 | |
| 523 | bool IsBackface(const OutputVertexData* v0, const OutputVertexData* v1, const OutputVertexData* v2) |
| 524 | { |
| 525 | float x0 = v0->projectedPosition.x; |
| 526 | float x1 = v1->projectedPosition.x; |
| 527 | float x2 = v2->projectedPosition.x; |
| 528 | float y1 = v1->projectedPosition.y; |
| 529 | float y0 = v0->projectedPosition.y; |
| 530 | float y2 = v2->projectedPosition.y; |
| 531 | float w0 = v0->projectedPosition.w; |
| 532 | float w1 = v1->projectedPosition.w; |
| 533 | float w2 = v2->projectedPosition.w; |
| 534 | |
| 535 | float normalZDir = (x0 * w2 - x2 * w0) * y1 + (x2 * y0 - x0 * y2) * w1 + (y2 * w0 - y0 * w2) * x1; |
| 536 | |
| 537 | bool backface = normalZDir <= 0.0f; |
| 538 | // Jimmie Johnson's Anything with an Engine has a positive viewport, while other games have a |
| 539 | // negative viewport. The positive viewport does not require vertices to be vertically mirrored, |
| 540 | // but the backface test does need to be inverted for things to be drawn. |
| 541 | if (xfmem.viewport.ht > 0) |
| 542 | backface = !backface; |
| 543 | |
| 544 | return backface; |
| 545 | } |
| 546 | |
| 547 | void PerspectiveDivide(OutputVertexData* vertex) |
| 548 | { |
| 549 | Common::Vec4& projected = vertex->projectedPosition; |
| 550 | Common::Vec3& screen = vertex->screenPosition; |
| 551 | |
| 552 | float wInverse = 1.0f / projected.w; |
| 553 | screen.x = projected.x * wInverse * xfmem.viewport.wd + xfmem.viewport.xOrig; |
| 554 | screen.y = projected.y * wInverse * xfmem.viewport.ht + xfmem.viewport.yOrig; |
| 555 | screen.z = projected.z * wInverse * xfmem.viewport.zRange + xfmem.viewport.farZ; |
| 556 | } |
| 557 | } // namespace Clipper |