-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
GSDrawingContext.cpp
202 lines (164 loc) · 5.31 KB
/
GSDrawingContext.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2021 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "PrecompiledHeader.h"
#include "GSDrawingContext.h"
#include "GSGL.h"
#include "GS.h"
static int findmax(int tl, int br, int limit, int wm, int minuv, int maxuv)
{
// return max possible texcoord.
int uv = br;
// Confirmed on hardware if the size exceeds 1024, it basically gets masked so you end up with a 1x1 pixel (Except Region Clamp).
if (limit > 1024)
{
if (wm != CLAMP_REGION_CLAMP) // TEMPLOG
Console.Warning("Masking TEX0 to 1x1 was %d", limit + 1);
limit = 0;
}
if (wm == CLAMP_CLAMP)
{
if (uv > limit)
uv = limit;
}
else if (wm == CLAMP_REPEAT)
{
if (tl < 0)
uv = limit; // wrap around
else if (uv > limit)
uv = limit;
}
else if (wm == CLAMP_REGION_CLAMP)
{
if (uv < minuv)
uv = minuv;
if (uv > maxuv)
uv = maxuv;
}
else if (wm == CLAMP_REGION_REPEAT)
{
// REGION_REPEAT adhears to the original texture size, even if offset outside the texture (with MAXUV).
if (limit < minuv) // TEMPLOG
Console.Warning("Limiting minuv, limit %x minuv from %x to %x", limit, minuv, minuv & limit);
minuv &= limit;
if (tl < 0)
uv = minuv | maxuv; // wrap around, just use (any & mask) | fix.
else
uv = std::min(uv, minuv) | maxuv; // (any & mask) cannot be larger than mask, select br if that is smaller (not br & mask because there might be a larger value between tl and br when &'ed with the mask).
}
return uv;
}
static int reduce(int uv, int size)
{
while (size > 3 && (1 << (size - 1)) >= uv)
{
size--;
}
return size;
}
static int extend(int uv, int size)
{
while (size < 10 && (1 << size) < uv)
{
size++;
}
return size;
}
GIFRegTEX0 GSDrawingContext::GetSizeFixedTEX0(const GSVector4& st, bool linear, bool mipmap)
{
if (mipmap)
return TEX0; // no mipmaping allowed
// find the optimal value for TW/TH by analyzing vertex trace and clamping values, extending only for region modes where uv may be outside
int tw = TEX0.TW;
int th = TEX0.TH;
int wms = (int)CLAMP.WMS;
int wmt = (int)CLAMP.WMT;
int minu = (int)CLAMP.MINU;
int minv = (int)CLAMP.MINV;
int maxu = (int)CLAMP.MAXU;
int maxv = (int)CLAMP.MAXV;
GSVector4 uvf = st;
if (linear)
{
uvf += GSVector4(-0.5f, 0.5f).xxyy();
}
GSVector4i uv = GSVector4i(uvf.floor().xyzw(uvf.ceil()));
uv.x = findmax(uv.x, uv.z, (1 << tw) - 1, wms, minu, maxu);
uv.y = findmax(uv.y, uv.w, (1 << th) - 1, wmt, minv, maxv);
if (tw + th >= 19) // smaller sizes aren't worth, they just create multiple entries in the textue cache and the saved memory is less
{
tw = reduce(uv.x, tw);
th = reduce(uv.y, th);
}
if (wms == CLAMP_REGION_CLAMP || wms == CLAMP_REGION_REPEAT)
{
tw = extend(uv.x, tw);
}
if (wmt == CLAMP_REGION_CLAMP || wmt == CLAMP_REGION_REPEAT)
{
th = extend(uv.y, th);
}
GIFRegTEX0 res = TEX0;
if (tw > 10) // TEMPLOG
Console.Warning("Limiting Width to 1");
if (th > 10) // TEMPLOG
Console.Warning("Limiting Height to 1");
res.TW = tw > 10 ? 0 : tw;
res.TH = th > 10 ? 0 : th;
if (GSConfig.Renderer == GSRendererType::SW && (TEX0.TW != res.TW || TEX0.TH != res.TH))
{
GL_DBG("FixedTEX0 %05x %d %d tw %d=>%d th %d=>%d st (%.0f,%.0f,%.0f,%.0f) uvmax %d,%d wm %d,%d (%d,%d,%d,%d)",
(int)TEX0.TBP0, (int)TEX0.TBW, (int)TEX0.PSM,
(int)TEX0.TW, tw, (int)TEX0.TH, th,
uvf.x, uvf.y, uvf.z, uvf.w,
uv.x, uv.y,
wms, wmt, minu, maxu, minv, maxv);
}
return res;
}
void GSDrawingContext::ComputeFixedTEX0(const GSVector4& st)
{
// It is quite complex to handle rescaling so this function is less stricter than GetSizeFixedTEX0,
// therefore we remove the reduce optimization and we don't handle bilinear filtering which might create wrong interpolation at the border.
int tw = TEX0.TW;
int th = TEX0.TH;
int wms = (int)CLAMP.WMS;
int wmt = (int)CLAMP.WMT;
int minu = (int)CLAMP.MINU;
int minv = (int)CLAMP.MINV;
int maxu = (int)CLAMP.MAXU;
int maxv = (int)CLAMP.MAXV;
if (wms != CLAMP_REGION_CLAMP)
tw = tw > 10 ? 0 : tw;
if (wmt != CLAMP_REGION_CLAMP)
th = th > 10 ? 0 : th;
GSVector4i uv = GSVector4i(st.floor().xyzw(st.ceil()));
uv.x = findmax(uv.x, uv.z, (1 << tw) - 1, wms, minu, maxu);
uv.y = findmax(uv.y, uv.w, (1 << th) - 1, wmt, minv, maxv);
if (wms == CLAMP_REGION_CLAMP || wms == CLAMP_REGION_REPEAT)
tw = extend(uv.x, tw);
if (wmt == CLAMP_REGION_CLAMP || wmt == CLAMP_REGION_REPEAT)
th = extend(uv.y, th);
tw = std::clamp<int>(tw, 0, 10);
th = std::clamp<int>(th, 0, 10);
if ((tw != (int)TEX0.TW) || (th != (int)TEX0.TH))
{
m_fixed_tex0 = true;
TEX0.TW = tw;
TEX0.TH = th;
GL_DBG("FixedTEX0 TW %d=>%d, TH %d=>%d wm %d,%d",
(int)stack.TEX0.TW, (int)TEX0.TW, (int)stack.TEX0.TH, (int)TEX0.TH,
(int)CLAMP.WMS, (int)CLAMP.WMT);
}
}