2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
/*
|
|
|
|
Portions of this file are based off work by Markus Trenkwalder.
|
|
|
|
Copyright (c) 2007, 2008 Markus Trenkwalder
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
All rights reserved.
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
* Redistributions of source code must retain the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer.
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
* Neither the name of the library's copyright owner nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived from this
|
|
|
|
software without specific prior written permission.
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/Software/Clipper.h"
|
2016-09-24 09:16:52 +00:00
|
|
|
|
|
|
|
#include "Common/Assert.h"
|
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/Software/NativeVertexFormat.h"
|
|
|
|
#include "VideoBackends/Software/Rasterizer.h"
|
2015-10-09 18:50:36 +00:00
|
|
|
|
|
|
|
#include "VideoCommon/BPMemory.h"
|
|
|
|
#include "VideoCommon/Statistics.h"
|
|
|
|
#include "VideoCommon/XFMemory.h"
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
namespace Clipper
|
|
|
|
{
|
2014-08-11 01:18:38 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
NUM_CLIPPED_VERTICES = 33,
|
|
|
|
NUM_INDICES = NUM_CLIPPED_VERTICES + 3
|
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-07-08 13:58:25 +00:00
|
|
|
static OutputVertexData ClippedVertices[NUM_CLIPPED_VERTICES];
|
|
|
|
static OutputVertexData* Vertices[NUM_INDICES];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
void Init()
|
|
|
|
{
|
2010-12-02 05:38:48 +00:00
|
|
|
for (int i = 0; i < NUM_CLIPPED_VERTICES; ++i)
|
2009-10-12 00:48:24 +00:00
|
|
|
Vertices[i + 3] = &ClippedVertices[i];
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-08-11 01:18:38 +00:00
|
|
|
enum
|
|
|
|
{
|
2009-10-12 00:48:24 +00:00
|
|
|
SKIP_FLAG = -1,
|
|
|
|
CLIP_POS_X_BIT = 0x01,
|
|
|
|
CLIP_NEG_X_BIT = 0x02,
|
|
|
|
CLIP_POS_Y_BIT = 0x04,
|
|
|
|
CLIP_NEG_Y_BIT = 0x08,
|
|
|
|
CLIP_POS_Z_BIT = 0x10,
|
|
|
|
CLIP_NEG_Z_BIT = 0x20
|
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-09-23 00:51:21 +00:00
|
|
|
static inline int CalcClipMask(const OutputVertexData* v)
|
2009-10-12 00:48:24 +00:00
|
|
|
{
|
|
|
|
int cmask = 0;
|
2010-03-09 04:38:07 +00:00
|
|
|
Vec4 pos = v->projectedPosition;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-08-11 01:55:30 +00:00
|
|
|
if (pos.w - pos.x < 0)
|
|
|
|
cmask |= CLIP_POS_X_BIT;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-08-11 01:55:30 +00:00
|
|
|
if (pos.x + pos.w < 0)
|
|
|
|
cmask |= CLIP_NEG_X_BIT;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-08-11 01:55:30 +00:00
|
|
|
if (pos.w - pos.y < 0)
|
|
|
|
cmask |= CLIP_POS_Y_BIT;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-08-11 01:55:30 +00:00
|
|
|
if (pos.y + pos.w < 0)
|
|
|
|
cmask |= CLIP_NEG_Y_BIT;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-08-11 01:55:30 +00:00
|
|
|
if (pos.w * pos.z > 0)
|
|
|
|
cmask |= CLIP_POS_Z_BIT;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-08-11 01:55:30 +00:00
|
|
|
if (pos.z + pos.w < 0)
|
|
|
|
cmask |= CLIP_NEG_Z_BIT;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
return cmask;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-10-02 06:20:46 +00:00
|
|
|
static inline void AddInterpolatedVertex(float t, int out, int in, int* numVertices)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2010-03-09 04:38:07 +00:00
|
|
|
Vertices[(*numVertices)++]->Lerp(t, Vertices[out], Vertices[in]);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
#define DIFFERENT_SIGNS(x, y) ((x <= 0 && y > 0) || (x > 0 && y <= 0))
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
#define CLIP_DOTPROD(I, A, B, C, D) \
|
2010-03-09 04:38:07 +00:00
|
|
|
(Vertices[I]->projectedPosition.x * A + Vertices[I]->projectedPosition.y * B + \
|
2009-10-12 00:48:24 +00:00
|
|
|
Vertices[I]->projectedPosition.z * C + Vertices[I]->projectedPosition.w * D)
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
#define POLY_CLIP(PLANE_BIT, A, B, C, D) \
|
2016-06-24 08:43:46 +00:00
|
|
|
{ \
|
2010-02-24 03:38:36 +00:00
|
|
|
if (mask & PLANE_BIT) \
|
|
|
|
{ \
|
|
|
|
int idxPrev = inlist[0]; \
|
2009-10-12 00:48:24 +00:00
|
|
|
float dpPrev = CLIP_DOTPROD(idxPrev, A, B, C, D); \
|
|
|
|
int outcount = 0; \
|
|
|
|
\
|
|
|
|
inlist[n] = inlist[0]; \
|
|
|
|
for (int j = 1; j <= n; j++) \
|
2016-06-24 08:43:46 +00:00
|
|
|
{ \
|
2014-10-02 06:20:46 +00:00
|
|
|
int idx = inlist[j]; \
|
2009-10-12 00:48:24 +00:00
|
|
|
float dp = CLIP_DOTPROD(idx, A, B, C, D); \
|
|
|
|
if (dpPrev >= 0) \
|
|
|
|
{ \
|
|
|
|
outlist[outcount++] = idxPrev; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
if (DIFFERENT_SIGNS(dp, dpPrev)) \
|
|
|
|
{ \
|
|
|
|
if (dp < 0) \
|
2010-03-09 04:38:07 +00:00
|
|
|
{ \
|
|
|
|
float t = dp / (dp - dpPrev); \
|
|
|
|
AddInterpolatedVertex(t, idx, idxPrev, &numVertices); \
|
2016-06-24 08:43:46 +00:00
|
|
|
} \
|
2010-03-09 04:38:07 +00:00
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
float t = dpPrev / (dpPrev - dp); \
|
|
|
|
AddInterpolatedVertex(t, idxPrev, idx, &numVertices); \
|
2016-06-24 08:43:46 +00:00
|
|
|
} \
|
2014-10-02 06:20:46 +00:00
|
|
|
outlist[outcount++] = numVertices - 1; \
|
2016-06-24 08:43:46 +00:00
|
|
|
} \
|
|
|
|
\
|
2010-03-09 04:38:07 +00:00
|
|
|
idxPrev = idx; \
|
|
|
|
dpPrev = dp; \
|
2016-06-24 08:43:46 +00:00
|
|
|
} \
|
|
|
|
\
|
2010-03-09 04:38:07 +00:00
|
|
|
if (outcount < 3) \
|
2014-10-02 06:20:46 +00:00
|
|
|
continue; \
|
2016-06-24 08:43:46 +00:00
|
|
|
\
|
|
|
|
{ \
|
2014-10-02 06:20:46 +00:00
|
|
|
int* tmp = inlist; \
|
2009-10-12 00:48:24 +00:00
|
|
|
inlist = outlist; \
|
|
|
|
outlist = tmp; \
|
|
|
|
n = outcount; \
|
2016-06-24 08:43:46 +00:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
#define LINE_CLIP(PLANE_BIT, A, B, C, D) \
|
2016-06-24 08:43:46 +00:00
|
|
|
{ \
|
2009-10-12 00:48:24 +00:00
|
|
|
if (mask & PLANE_BIT) \
|
|
|
|
{ \
|
|
|
|
const float dp0 = CLIP_DOTPROD(0, A, B, C, D); \
|
|
|
|
const float dp1 = CLIP_DOTPROD(1, A, B, C, D); \
|
|
|
|
const bool neg_dp0 = dp0 < 0; \
|
|
|
|
const bool neg_dp1 = dp1 < 0; \
|
2016-06-24 08:43:46 +00:00
|
|
|
\
|
2009-10-12 00:48:24 +00:00
|
|
|
if (neg_dp0 && neg_dp1) \
|
|
|
|
return; \
|
2016-06-24 08:43:46 +00:00
|
|
|
\
|
2009-10-12 00:48:24 +00:00
|
|
|
if (neg_dp1) \
|
2016-06-24 08:43:46 +00:00
|
|
|
{ \
|
2015-10-09 18:50:36 +00:00
|
|
|
float t = dp1 / (dp1 - dp0); \
|
2009-10-12 00:48:24 +00:00
|
|
|
if (t > t1) \
|
|
|
|
t1 = t; \
|
2016-06-24 08:43:46 +00:00
|
|
|
} \
|
2009-10-12 00:48:24 +00:00
|
|
|
else if (neg_dp0) \
|
2016-06-24 08:43:46 +00:00
|
|
|
{ \
|
2014-08-11 01:18:38 +00:00
|
|
|
float t = dp0 / (dp0 - dp1); \
|
|
|
|
if (t > t0) \
|
2014-10-02 06:20:46 +00:00
|
|
|
t0 = t; \
|
2016-06-24 08:43:46 +00:00
|
|
|
} \
|
|
|
|
} \
|
2009-10-12 00:48:24 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-07-08 12:29:26 +00:00
|
|
|
static void ClipTriangle(int* indices, int* numIndices)
|
2010-03-09 04:38:07 +00:00
|
|
|
{
|
|
|
|
int mask = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
mask |= CalcClipMask(Vertices[0]);
|
2010-03-09 04:38:07 +00:00
|
|
|
mask |= CalcClipMask(Vertices[1]);
|
|
|
|
mask |= CalcClipMask(Vertices[2]);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
if (mask != 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2010-03-09 04:38:07 +00:00
|
|
|
for (int i = 0; i < 3; i += 3)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2009-10-12 00:48:24 +00:00
|
|
|
int vlist[2][2 * 6 + 1];
|
|
|
|
int *inlist = vlist[0], *outlist = vlist[1];
|
|
|
|
int n = 3;
|
2010-03-09 04:38:07 +00:00
|
|
|
int numVertices = 3;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
inlist[0] = 0;
|
2009-10-12 00:48:24 +00:00
|
|
|
inlist[1] = 1;
|
2010-03-09 04:38:07 +00:00
|
|
|
inlist[2] = 2;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
// mark this triangle as unused in case it should be completely
|
|
|
|
// clipped
|
|
|
|
indices[0] = SKIP_FLAG;
|
|
|
|
indices[1] = SKIP_FLAG;
|
|
|
|
indices[2] = SKIP_FLAG;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
POLY_CLIP(CLIP_POS_X_BIT, -1, 0, 0, 1);
|
|
|
|
POLY_CLIP(CLIP_NEG_X_BIT, 1, 0, 0, 1);
|
|
|
|
POLY_CLIP(CLIP_POS_Y_BIT, 0, -1, 0, 1);
|
|
|
|
POLY_CLIP(CLIP_NEG_Y_BIT, 0, 1, 0, 1);
|
|
|
|
POLY_CLIP(CLIP_POS_Z_BIT, 0, 0, 0, 1);
|
|
|
|
POLY_CLIP(CLIP_NEG_Z_BIT, 0, 0, 1, 1);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-07-11 03:34:50 +00:00
|
|
|
INCSTAT(g_stats.this_frame.num_triangles_clipped);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
// transform the poly in inlist into triangles
|
|
|
|
indices[0] = inlist[0];
|
|
|
|
indices[1] = inlist[1];
|
|
|
|
indices[2] = inlist[2];
|
|
|
|
for (int j = 3; j < n; ++j)
|
|
|
|
{
|
|
|
|
indices[(*numIndices)++] = inlist[0];
|
|
|
|
indices[(*numIndices)++] = inlist[j - 1];
|
2014-10-02 06:20:46 +00:00
|
|
|
indices[(*numIndices)++] = inlist[j];
|
2010-03-09 04:38:07 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
static void ClipLine(int* indices)
|
|
|
|
{
|
2014-08-11 01:18:38 +00:00
|
|
|
int mask = 0;
|
|
|
|
int clip_mask[2] = {0, 0};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
for (int i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
clip_mask[i] = CalcClipMask(Vertices[i]);
|
|
|
|
mask |= clip_mask[i];
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
if (mask == 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
float t0 = 0;
|
2014-03-10 11:30:55 +00:00
|
|
|
float t1 = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-03-10 11:30:55 +00:00
|
|
|
// Mark unused in case of early termination
|
|
|
|
// of the macros below. (When fully clipped)
|
|
|
|
indices[0] = SKIP_FLAG;
|
|
|
|
indices[1] = SKIP_FLAG;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
LINE_CLIP(CLIP_POS_X_BIT, -1, 0, 0, 1);
|
|
|
|
LINE_CLIP(CLIP_NEG_X_BIT, 1, 0, 0, 1);
|
|
|
|
LINE_CLIP(CLIP_POS_Y_BIT, 0, -1, 0, 1);
|
|
|
|
LINE_CLIP(CLIP_NEG_Y_BIT, 0, 1, 0, 1);
|
|
|
|
LINE_CLIP(CLIP_POS_Z_BIT, 0, 0, -1, 1);
|
|
|
|
LINE_CLIP(CLIP_NEG_Z_BIT, 0, 0, 1, 1);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
// Restore the old values as this line
|
2010-03-09 04:38:07 +00:00
|
|
|
// was not fully clipped.
|
2009-10-12 00:48:24 +00:00
|
|
|
indices[0] = 0;
|
|
|
|
indices[1] = 1;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
int numVertices = 2;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-07-08 12:29:26 +00:00
|
|
|
if (clip_mask[0])
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2014-07-08 12:29:26 +00:00
|
|
|
indices[0] = numVertices;
|
|
|
|
AddInterpolatedVertex(t0, 0, 1, &numVertices);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
if (clip_mask[1])
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2010-03-09 04:38:07 +00:00
|
|
|
indices[1] = numVertices;
|
|
|
|
AddInterpolatedVertex(t1, 1, 0, &numVertices);
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
void ProcessTriangle(OutputVertexData* v0, OutputVertexData* v1, OutputVertexData* v2)
|
|
|
|
{
|
2022-01-04 05:43:11 +00:00
|
|
|
INCSTAT(g_stats.this_frame.num_triangles_in);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-11-30 01:51:02 +00:00
|
|
|
if (IsTriviallyRejected(v0, v1, v2))
|
|
|
|
{
|
2022-01-04 05:43:11 +00:00
|
|
|
INCSTAT(g_stats.this_frame.num_triangles_rejected);
|
2021-11-30 01:51:02 +00:00
|
|
|
// NOTE: The slope used by zfreeze shouldn't be updated if the triangle is
|
|
|
|
// trivially rejected during clipping
|
2010-03-09 04:38:07 +00:00
|
|
|
return;
|
2021-11-30 01:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool backface = IsBackface(v0, v1, v2);
|
|
|
|
|
|
|
|
if (!backface)
|
|
|
|
{
|
|
|
|
if (bpmem.genMode.cullmode == CullMode::Back || bpmem.genMode.cullmode == CullMode::All)
|
|
|
|
{
|
|
|
|
// cull frontfacing - we still need to update the slope for zfreeze
|
|
|
|
PerspectiveDivide(v0);
|
|
|
|
PerspectiveDivide(v1);
|
|
|
|
PerspectiveDivide(v2);
|
2021-11-10 02:38:24 +00:00
|
|
|
Rasterizer::UpdateZSlope(v0, v1, v2, bpmem.scissorOffset.x * 2, bpmem.scissorOffset.y * 2);
|
2022-01-04 05:43:11 +00:00
|
|
|
INCSTAT(g_stats.this_frame.num_triangles_culled);
|
2021-11-30 01:51:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (bpmem.genMode.cullmode == CullMode::Front || bpmem.genMode.cullmode == CullMode::All)
|
|
|
|
{
|
|
|
|
// cull backfacing - we still need to update the slope for zfreeze
|
|
|
|
PerspectiveDivide(v0);
|
|
|
|
PerspectiveDivide(v2);
|
|
|
|
PerspectiveDivide(v1);
|
2021-11-10 02:38:24 +00:00
|
|
|
Rasterizer::UpdateZSlope(v0, v2, v1, bpmem.scissorOffset.x * 2, bpmem.scissorOffset.y * 2);
|
2022-01-04 05:43:11 +00:00
|
|
|
INCSTAT(g_stats.this_frame.num_triangles_culled);
|
2021-11-30 01:51:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-03-10 11:30:55 +00:00
|
|
|
int indices[NUM_INDICES] = {0, 1, 2, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG,
|
2014-08-11 01:18:38 +00:00
|
|
|
SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG,
|
|
|
|
SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG,
|
|
|
|
SKIP_FLAG, SKIP_FLAG, SKIP_FLAG};
|
2010-03-09 04:38:07 +00:00
|
|
|
int numIndices = 3;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
if (backface)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2010-03-09 04:38:07 +00:00
|
|
|
Vertices[0] = v0;
|
|
|
|
Vertices[1] = v2;
|
|
|
|
Vertices[2] = v1;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-09 04:38:07 +00:00
|
|
|
Vertices[0] = v0;
|
|
|
|
Vertices[1] = v1;
|
|
|
|
Vertices[2] = v2;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 00:52:43 +00:00
|
|
|
// TODO: behavior when disable_clipping_detection is set doesn't quite match actual hardware;
|
|
|
|
// there does still seem to be a maximum size after which things are clipped. Also, currently
|
|
|
|
// when clipping is enabled triangles are clipped to exactly the viewport, but on hardware there
|
|
|
|
// is a guardband (and with certain scissor configurations, things can show up in it)
|
|
|
|
// Mario Party 8 in widescreen breaks without this: https://bugs.dolphin-emu.org/issues/12769
|
|
|
|
bool skip_clipping = false;
|
|
|
|
if (xfmem.clipDisable.disable_clipping_detection)
|
|
|
|
{
|
|
|
|
// If any w coordinate is negative, then the perspective divide will flip coordinates, breaking
|
|
|
|
// various assumptions (including backface). So, we still need to do clipping in that case.
|
|
|
|
// This isn't the actual condition hardware uses.
|
|
|
|
if (Vertices[0]->projectedPosition.w >= 0 && Vertices[1]->projectedPosition.w >= 0 &&
|
|
|
|
Vertices[2]->projectedPosition.w >= 0)
|
|
|
|
skip_clipping = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!skip_clipping)
|
|
|
|
ClipTriangle(indices, &numIndices);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
for (int i = 0; i + 3 <= numIndices; i += 3)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2018-03-15 00:34:35 +00:00
|
|
|
ASSERT(i < NUM_INDICES);
|
2009-10-12 00:48:24 +00:00
|
|
|
if (indices[i] != SKIP_FLAG)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2010-03-09 04:38:07 +00:00
|
|
|
PerspectiveDivide(Vertices[indices[i]]);
|
|
|
|
PerspectiveDivide(Vertices[indices[i + 1]]);
|
|
|
|
PerspectiveDivide(Vertices[indices[i + 2]]);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
Rasterizer::DrawTriangleFrontFace(Vertices[indices[i]], Vertices[indices[i + 1]],
|
2009-10-12 00:48:24 +00:00
|
|
|
Vertices[indices[i + 2]]);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 21:31:34 +00:00
|
|
|
constexpr std::array<float, 8> LINE_PT_TEX_OFFSETS = {
|
|
|
|
0, 1 / 16.f, 1 / 8.f, 1 / 4.f, 1 / 2.f, 1, 1, 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void CopyLineVertex(OutputVertexData* dst, const OutputVertexData* src, int px, int py,
|
|
|
|
bool apply_line_offset)
|
2009-10-12 00:48:24 +00:00
|
|
|
{
|
2020-12-22 21:31:34 +00:00
|
|
|
const float line_half_width = bpmem.lineptwidth.linesize / 12.0f;
|
|
|
|
|
|
|
|
dst->projectedPosition = src->projectedPosition;
|
|
|
|
dst->screenPosition.x = src->screenPosition.x + px * line_half_width;
|
|
|
|
dst->screenPosition.y = src->screenPosition.y + py * line_half_width;
|
2010-03-09 04:38:07 +00:00
|
|
|
dst->screenPosition.z = src->screenPosition.z;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-08-19 15:08:09 +00:00
|
|
|
dst->normal = src->normal;
|
2017-08-23 02:45:25 +00:00
|
|
|
dst->color = src->color;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-08-19 15:08:09 +00:00
|
|
|
dst->texCoords = src->texCoords;
|
2020-12-22 21:31:34 +00:00
|
|
|
|
|
|
|
if (apply_line_offset && LINE_PT_TEX_OFFSETS[bpmem.lineptwidth.lineoff] != 0)
|
|
|
|
{
|
|
|
|
for (u32 coord_num = 0; coord_num < xfmem.numTexGen.numTexGens; coord_num++)
|
|
|
|
{
|
|
|
|
if (bpmem.texcoords[coord_num].s.line_offset)
|
|
|
|
{
|
|
|
|
dst->texCoords[coord_num].x += (bpmem.texcoords[coord_num].s.scale_minus_1 + 1) *
|
|
|
|
LINE_PT_TEX_OFFSETS[bpmem.lineptwidth.lineoff];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
void ProcessLine(OutputVertexData* lineV0, OutputVertexData* lineV1)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2009-10-12 00:48:24 +00:00
|
|
|
int indices[4] = {0, 1, SKIP_FLAG, SKIP_FLAG};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
Vertices[0] = lineV0;
|
|
|
|
Vertices[1] = lineV1;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
// point to a valid vertex to store to when clipping
|
2015-10-09 18:50:36 +00:00
|
|
|
Vertices[2] = &ClippedVertices[17];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
ClipLine(indices);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
if (indices[0] != SKIP_FLAG)
|
|
|
|
{
|
|
|
|
OutputVertexData* v0 = Vertices[indices[0]];
|
|
|
|
OutputVertexData* v1 = Vertices[indices[1]];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
PerspectiveDivide(v0);
|
2010-03-09 04:38:07 +00:00
|
|
|
PerspectiveDivide(v1);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-12-22 21:31:34 +00:00
|
|
|
const float dx = v1->screenPosition.x - v0->screenPosition.x;
|
|
|
|
const float dy = v1->screenPosition.y - v0->screenPosition.y;
|
2013-04-14 03:54:02 +00:00
|
|
|
|
2020-12-22 21:31:34 +00:00
|
|
|
int px = 0;
|
|
|
|
int py = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-12-22 21:31:34 +00:00
|
|
|
// GameCube/Wii's line drawing algorithm is a little quirky. It does not
|
|
|
|
// use the correct line caps. Instead, the line caps are vertical or
|
|
|
|
// horizontal depending the slope of the line.
|
|
|
|
// FIXME: What does real hardware do when line is at a 45-degree angle?
|
|
|
|
|
|
|
|
// Note that py or px are set positive or negative to ensure that the triangles are drawn ccw.
|
2014-03-10 11:30:55 +00:00
|
|
|
if (fabsf(dx) > fabsf(dy))
|
2020-12-22 21:31:34 +00:00
|
|
|
py = (dx > 0) ? -1 : 1;
|
2016-06-24 08:43:46 +00:00
|
|
|
else
|
2020-12-22 21:31:34 +00:00
|
|
|
px = (dy > 0) ? 1 : -1;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
OutputVertexData triangle[3];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-12-22 21:31:34 +00:00
|
|
|
CopyLineVertex(&triangle[0], v0, px, py, false);
|
|
|
|
CopyLineVertex(&triangle[1], v1, px, py, false);
|
|
|
|
CopyLineVertex(&triangle[2], v1, -px, -py, true);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
// ccw winding
|
|
|
|
Rasterizer::DrawTriangleFrontFace(&triangle[2], &triangle[1], &triangle[0]);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-12-22 21:31:34 +00:00
|
|
|
CopyLineVertex(&triangle[1], v0, -px, -py, true);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
Rasterizer::DrawTriangleFrontFace(&triangle[0], &triangle[1], &triangle[2]);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 23:00:40 +00:00
|
|
|
static void CopyPointVertex(OutputVertexData* dst, const OutputVertexData* src, bool px, bool py)
|
|
|
|
{
|
|
|
|
const float point_radius = bpmem.lineptwidth.pointsize / 12.0f;
|
|
|
|
|
|
|
|
dst->projectedPosition = src->projectedPosition;
|
|
|
|
dst->screenPosition.x = src->screenPosition.x + (px ? 1 : -1) * point_radius;
|
|
|
|
dst->screenPosition.y = src->screenPosition.y + (py ? 1 : -1) * point_radius;
|
|
|
|
dst->screenPosition.z = src->screenPosition.z;
|
|
|
|
|
|
|
|
dst->normal = src->normal;
|
|
|
|
dst->color = src->color;
|
|
|
|
|
|
|
|
dst->texCoords = src->texCoords;
|
|
|
|
|
|
|
|
const float point_offset = LINE_PT_TEX_OFFSETS[bpmem.lineptwidth.pointoff];
|
|
|
|
if (point_offset != 0)
|
|
|
|
{
|
|
|
|
for (u32 coord_num = 0; coord_num < xfmem.numTexGen.numTexGens; coord_num++)
|
|
|
|
{
|
|
|
|
const auto coord_info = bpmem.texcoords[coord_num];
|
|
|
|
if (coord_info.s.point_offset)
|
|
|
|
{
|
|
|
|
if (px)
|
|
|
|
dst->texCoords[coord_num].x += (coord_info.s.scale_minus_1 + 1) * point_offset;
|
|
|
|
if (py)
|
|
|
|
dst->texCoords[coord_num].y += (coord_info.t.scale_minus_1 + 1) * point_offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProcessPoint(OutputVertexData* center)
|
|
|
|
{
|
|
|
|
// TODO: This isn't actually doing any clipping
|
|
|
|
PerspectiveDivide(center);
|
|
|
|
|
|
|
|
OutputVertexData ll, lr, ul, ur;
|
|
|
|
|
|
|
|
CopyPointVertex(&ll, center, false, false);
|
|
|
|
CopyPointVertex(&lr, center, true, false);
|
|
|
|
CopyPointVertex(&ur, center, true, true);
|
|
|
|
CopyPointVertex(&ul, center, false, true);
|
|
|
|
|
|
|
|
Rasterizer::DrawTriangleFrontFace(&ll, &ul, &lr);
|
|
|
|
Rasterizer::DrawTriangleFrontFace(&ur, &lr, &ul);
|
|
|
|
}
|
|
|
|
|
2021-11-30 01:51:02 +00:00
|
|
|
bool IsTriviallyRejected(const OutputVertexData* v0, const OutputVertexData* v1,
|
|
|
|
const OutputVertexData* v2)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2009-10-12 00:48:24 +00:00
|
|
|
int mask = CalcClipMask(v0);
|
|
|
|
mask &= CalcClipMask(v1);
|
|
|
|
mask &= CalcClipMask(v2);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-11-30 01:51:02 +00:00
|
|
|
return mask != 0;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-11-30 01:51:02 +00:00
|
|
|
bool IsBackface(const OutputVertexData* v0, const OutputVertexData* v1, const OutputVertexData* v2)
|
|
|
|
{
|
2010-03-09 04:38:07 +00:00
|
|
|
float x0 = v0->projectedPosition.x;
|
|
|
|
float x1 = v1->projectedPosition.x;
|
|
|
|
float x2 = v2->projectedPosition.x;
|
|
|
|
float y1 = v1->projectedPosition.y;
|
|
|
|
float y0 = v0->projectedPosition.y;
|
|
|
|
float y2 = v2->projectedPosition.y;
|
|
|
|
float w0 = v0->projectedPosition.w;
|
|
|
|
float w1 = v1->projectedPosition.w;
|
|
|
|
float w2 = v2->projectedPosition.w;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
float normalZDir = (x0 * w2 - x2 * w0) * y1 + (x2 * y0 - x0 * y2) * w1 + (y2 * w0 - y0 * w2) * x1;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-11-30 01:51:02 +00:00
|
|
|
bool backface = normalZDir <= 0.0f;
|
2020-12-22 02:20:32 +00:00
|
|
|
// Jimmie Johnson's Anything with an Engine has a positive viewport, while other games have a
|
|
|
|
// negative viewport. The positive viewport does not require vertices to be vertically mirrored,
|
|
|
|
// but the backface test does need to be inverted for things to be drawn.
|
|
|
|
if (xfmem.viewport.ht > 0)
|
|
|
|
backface = !backface;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-11-30 01:51:02 +00:00
|
|
|
return backface;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
void PerspectiveDivide(OutputVertexData* vertex)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2010-03-09 04:38:07 +00:00
|
|
|
Vec4& projected = vertex->projectedPosition;
|
|
|
|
Vec3& screen = vertex->screenPosition;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
float wInverse = 1.0f / projected.w;
|
2021-11-10 02:38:24 +00:00
|
|
|
screen.x = projected.x * wInverse * xfmem.viewport.wd + xfmem.viewport.xOrig;
|
|
|
|
screen.y = projected.y * wInverse * xfmem.viewport.ht + xfmem.viewport.yOrig;
|
2014-04-27 18:59:04 +00:00
|
|
|
screen.z = projected.z * wInverse * xfmem.viewport.zRange + xfmem.viewport.farZ;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace Clipper
|