Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Software/RasterizerRectangle.cpp
3187 views
1
// See comment in header for the purpose of the code in this file.
2
3
#include "ppsspp_config.h"
4
#include <cmath>
5
6
#include "Common/Common.h"
7
#include "Common/Data/Convert/ColorConv.h"
8
#include "Common/Profiler/Profiler.h"
9
10
#include "Core/Config.h"
11
#include "Core/Debugger/MemBlockInfo.h"
12
#include "Core/System.h"
13
#include "GPU/GPUState.h"
14
15
#include "GPU/Software/BinManager.h"
16
#include "GPU/Software/DrawPixel.h"
17
#include "GPU/Software/Rasterizer.h"
18
#include "GPU/Software/Sampler.h"
19
#include "GPU/Software/SoftGpu.h"
20
#include "Common/Math/SIMDHeaders.h"
21
22
extern DSStretch g_DarkStalkerStretch;
23
// For Darkstalkers hack. Ugh.
24
extern bool currentDialogActive;
25
26
namespace Rasterizer {
27
28
// This essentially AlphaBlendingResult() with fixed src.a / 1 - src.a factors and ADD equation.
29
// It allows us to skip round trips between 32-bit and 16-bit color values.
30
static uint32_t StandardAlphaBlend(uint32_t source, uint32_t dst) {
31
#if defined(_M_SSE)
32
const __m128i alpha = _mm_cvtsi32_si128(source >> 24);
33
// Keep the alpha lane of the srcfactor zero, so we keep dest alpha.
34
const __m128i srcfactor = _mm_shufflelo_epi16(alpha, _MM_SHUFFLE(1, 0, 0, 0));
35
const __m128i dstfactor = _mm_sub_epi16(_mm_set1_epi16(255), srcfactor);
36
37
const __m128i z = _mm_setzero_si128();
38
const __m128i sourcevec = _mm_unpacklo_epi8(_mm_cvtsi32_si128(source), z);
39
const __m128i dstvec = _mm_unpacklo_epi8(_mm_cvtsi32_si128(dst), z);
40
41
// We switch to 16 bit to use mulhi, and we use 4 bits of decimal to make the 16 bit shift free.
42
const __m128i half = _mm_set1_epi16(1 << 3);
43
44
const __m128i srgb = _mm_add_epi16(_mm_slli_epi16(sourcevec, 4), half);
45
const __m128i sf = _mm_add_epi16(_mm_slli_epi16(srcfactor, 4), half);
46
const __m128i s = _mm_mulhi_epi16(srgb, sf);
47
48
const __m128i drgb = _mm_add_epi16(_mm_slli_epi16(dstvec, 4), half);
49
const __m128i df = _mm_add_epi16(_mm_slli_epi16(dstfactor, 4), half);
50
const __m128i d = _mm_mulhi_epi16(drgb, df);
51
52
const __m128i blended16 = _mm_adds_epi16(s, d);
53
return _mm_cvtsi128_si32(_mm_packus_epi16(blended16, blended16));
54
#elif PPSSPP_ARCH(ARM64_NEON)
55
uint16x4_t sf = vdup_n_u16((source >> 24) * 2 + 1);
56
uint16x4_t df = vdup_n_u16((255 - (source >> 24)) * 2 + 1);
57
58
// Convert both to 16-bit, double, and add the half before even going to 32 bit.
59
uint16x8_t sd_c16 = vmovl_u8(vcreate_u8((uint64_t)source | ((uint64_t)dst << 32)));
60
sd_c16 = vaddq_u16(vshlq_n_u16(sd_c16, 1), vdupq_n_u16(1));
61
62
uint16x4_t srgb = vget_low_u16(sd_c16);
63
uint16x4_t drgb = vget_high_u16(sd_c16);
64
65
uint16x4_t s = vshrn_n_u32(vmull_u16(srgb, sf), 10);
66
uint16x4_t d = vshrn_n_u32(vmull_u16(drgb, df), 10);
67
68
uint16x4_t blended = vset_lane_u16(0, vadd_u16(s, d), 3);
69
uint8x8_t blended8 = vqmovn_u16(vcombine_u16(blended, blended));
70
return vget_lane_u32(vreinterpret_u32_u8(blended8), 0);
71
#else
72
Vec3<int> srcfactor = Vec3<int>::AssignToAll(source >> 24);
73
Vec3<int> dstfactor = Vec3<int>::AssignToAll(255 - (source >> 24));
74
75
static constexpr Vec3<int> half = Vec3<int>::AssignToAll(1);
76
Vec3<int> lhs = ((Vec3<int>::FromRGB(source) * 2 + half) * (srcfactor * 2 + half)) / 1024;
77
Vec3<int> rhs = ((Vec3<int>::FromRGB(dst) * 2 + half) * (dstfactor * 2 + half)) / 1024;
78
Vec3<int> blended = lhs + rhs;
79
80
return clamp_u8(blended.r()) | (clamp_u8(blended.g()) << 8) | (clamp_u8(blended.b()) << 16);
81
#endif
82
}
83
84
// Through mode, with the specific Darkstalker settings.
85
template <GEBufferFormat fmt, bool alphaBlend>
86
static inline void DrawSinglePixel(u16 *pixel, const u32 color_in) {
87
u32 new_color;
88
// Because of this check, we only support src.a / 1-src.a blending.
89
if (!alphaBlend || (color_in >> 24) == 255) {
90
new_color = color_in & 0xFFFFFF;
91
} else {
92
u32 old_color;
93
switch (fmt) {
94
case GE_FORMAT_565:
95
old_color = RGB565ToRGBA8888(*pixel);
96
break;
97
case GE_FORMAT_5551:
98
old_color = RGBA5551ToRGBA8888(*pixel);
99
break;
100
case GE_FORMAT_4444:
101
old_color = RGBA4444ToRGBA8888(*pixel);
102
break;
103
default:
104
old_color = 0; // avoid warning. can't get here though since we enumerate all 16-bit formats above.
105
break;
106
}
107
108
new_color = StandardAlphaBlend(color_in, old_color);
109
}
110
111
switch (fmt) {
112
case GE_FORMAT_565:
113
*pixel = RGBA8888ToRGB565(new_color);
114
break;
115
case GE_FORMAT_5551:
116
*pixel = RGBA8888ToRGBA555X(new_color) | (*pixel & 0x8000);
117
break;
118
case GE_FORMAT_4444:
119
*pixel = RGBA8888ToRGBA444X(new_color) | (*pixel & 0xF000);
120
break;
121
default:
122
break;
123
}
124
}
125
126
template <bool alphaBlend>
127
static inline void DrawSinglePixel32(u32 *pixel, const u32 color_in) {
128
u32 new_color;
129
// Because of this check, we only support src.a / 1-src.a blending.
130
if (!alphaBlend || (color_in >> 24) == 255) {
131
new_color = color_in & 0xFFFFFF;
132
} else {
133
const u32 old_color = *pixel;
134
new_color = StandardAlphaBlend(color_in, old_color);
135
}
136
new_color |= *pixel & 0xFF000000;
137
*pixel = new_color;
138
}
139
140
// Check if we can safely ignore the alpha test, assuming standard alpha blending.
141
static inline bool AlphaTestIsNeedless(const PixelFuncID &pixelID) {
142
switch (pixelID.AlphaTestFunc()) {
143
case GE_COMP_NEVER:
144
case GE_COMP_EQUAL:
145
case GE_COMP_LESS:
146
case GE_COMP_LEQUAL:
147
return false;
148
149
case GE_COMP_ALWAYS:
150
return true;
151
152
case GE_COMP_NOTEQUAL:
153
case GE_COMP_GREATER:
154
case GE_COMP_GEQUAL:
155
if (pixelID.alphaTestRef != 0 || pixelID.hasAlphaTestMask)
156
return false;
157
return true;
158
}
159
160
return false;
161
}
162
163
static bool UseDrawSinglePixel(const PixelFuncID &pixelID) {
164
if (pixelID.clearMode || pixelID.colorTest || pixelID.stencilTest)
165
return false;
166
if (!AlphaTestIsNeedless(pixelID) || pixelID.DepthTestFunc() != GE_COMP_ALWAYS)
167
return false;
168
// We skip blending when alpha = FF, so we can't allow other blend modes.
169
if (pixelID.alphaBlend) {
170
if (pixelID.AlphaBlendEq() != GE_BLENDMODE_MUL_AND_ADD || pixelID.AlphaBlendSrc() != PixelBlendFactor::SRCALPHA)
171
return false;
172
if (pixelID.AlphaBlendDst() != PixelBlendFactor::INVSRCALPHA)
173
return false;
174
}
175
if (pixelID.dithering || pixelID.applyLogicOp || pixelID.applyColorWriteMask)
176
return false;
177
178
return true;
179
}
180
181
static inline Vec4IntResult SOFTRAST_CALL ModulateRGBA(Vec4IntArg prim_in, Vec4IntArg texcolor_in, const SamplerID &samplerID) {
182
Vec4<int> out;
183
Vec4<int> prim_color = prim_in;
184
Vec4<int> texcolor = texcolor_in;
185
186
#if defined(_M_SSE)
187
// Modulate weights slightly on the tex color, by adding one to prim and dividing by 256.
188
const __m128i p = _mm_slli_epi16(_mm_packs_epi32(prim_color.ivec, prim_color.ivec), 4);
189
const __m128i pboost = _mm_add_epi16(p, _mm_set1_epi16(1 << 4));
190
__m128i t = _mm_slli_epi16(_mm_packs_epi32(texcolor.ivec, texcolor.ivec), 4);
191
if (samplerID.useColorDoubling) {
192
const __m128i amask = _mm_set_epi16(-1, 0, 0, 0, -1, 0, 0, 0);
193
const __m128i a = _mm_and_si128(t, amask);
194
const __m128i rgb = _mm_andnot_si128(amask, t);
195
t = _mm_or_si128(_mm_slli_epi16(rgb, 1), a);
196
}
197
const __m128i b = _mm_mulhi_epi16(pboost, t);
198
out.ivec = _mm_unpacklo_epi16(b, _mm_setzero_si128());
199
#elif PPSSPP_ARCH(ARM64_NEON)
200
int32x4_t pboost = vaddq_s32(prim_color.ivec, vdupq_n_s32(1));
201
int32x4_t t = texcolor.ivec;
202
if (samplerID.useColorDoubling) {
203
static const int32_t rgbDouble[4] = {1, 1, 1, 0};
204
t = vshlq_s32(t, vld1q_s32(rgbDouble));
205
}
206
out.ivec = vshrq_n_s32(vmulq_s32(pboost, t), 8);
207
#else
208
if (samplerID.useColorDoubling) {
209
Vec4<int> tex = texcolor * Vec4<int>(2, 2, 2, 1);
210
out = ((prim_color + Vec4<int>::AssignToAll(1)) * tex) / 256;
211
} else {
212
out = (prim_color + Vec4<int>::AssignToAll(1)) * texcolor / 256;
213
}
214
#endif
215
216
return ToVec4IntResult(out);
217
}
218
219
template <GEBufferFormat fmt, bool isWhite, bool alphaBlend, bool alphaTestZero>
220
static void DrawSpriteTex(const DrawingCoords &pos0, const DrawingCoords &pos1, int s_start, int t_start, int ds, int dt, u32 color0, const RasterizerState &state, Sampler::FetchFunc fetchFunc) {
221
const u8 *texptr = state.texptr[0];
222
uint16_t texbufw = state.texbufw[0];
223
224
int t = t_start;
225
const Vec4<int> c0 = Vec4<int>::FromRGBA(color0);
226
for (int y = pos0.y; y < pos1.y; y++) {
227
int s = s_start;
228
u16 *pixel16 = fb.Get16Ptr(pos0.x, y, state.pixelID.cached.framebufStride);
229
u32 *pixel32 = fb.Get32Ptr(pos0.x, y, state.pixelID.cached.framebufStride);
230
for (int x = pos0.x; x < pos1.x; x++) {
231
Vec4<int> tex_color = fetchFunc(s, t, texptr, texbufw, 0, state.samplerID);
232
if (isWhite) {
233
if (!alphaTestZero || tex_color.a() != 0) {
234
u32 tex_color32 = tex_color.ToRGBA();
235
if (fmt == GE_FORMAT_8888)
236
DrawSinglePixel32<alphaBlend>(pixel32, tex_color32);
237
else
238
DrawSinglePixel<fmt, alphaBlend>(pixel16, tex_color32);
239
}
240
} else {
241
Vec4<int> prim_color = c0;
242
prim_color = Vec4<int>(ModulateRGBA(ToVec4IntArg(prim_color), ToVec4IntArg(tex_color), state.samplerID));
243
if (!alphaTestZero || prim_color.a() > 0) {
244
if (fmt == GE_FORMAT_8888)
245
DrawSinglePixel32<alphaBlend>(pixel32, prim_color.ToRGBA());
246
else
247
DrawSinglePixel<fmt, alphaBlend>(pixel16, prim_color.ToRGBA());
248
}
249
}
250
s += ds;
251
if (fmt == GE_FORMAT_8888)
252
pixel32++;
253
else
254
pixel16++;
255
}
256
t += dt;
257
}
258
}
259
260
template <bool isWhite, bool alphaBlend, bool alphaTestZero>
261
static void DrawSpriteTex(const DrawingCoords &pos0, const DrawingCoords &pos1, int s_start, int t_start, int ds, int dt, u32 color0, const RasterizerState &state, Sampler::FetchFunc fetchFunc) {
262
switch (state.pixelID.FBFormat()) {
263
case GE_FORMAT_565:
264
DrawSpriteTex<GE_FORMAT_565, isWhite, alphaBlend, alphaTestZero>(pos0, pos1, s_start, t_start, ds, dt, color0, state, fetchFunc);
265
break;
266
case GE_FORMAT_5551:
267
DrawSpriteTex<GE_FORMAT_5551, isWhite, alphaBlend, alphaTestZero>(pos0, pos1, s_start, t_start, ds, dt, color0, state, fetchFunc);
268
break;
269
case GE_FORMAT_4444:
270
DrawSpriteTex<GE_FORMAT_4444, isWhite, alphaBlend, alphaTestZero>(pos0, pos1, s_start, t_start, ds, dt, color0, state, fetchFunc);
271
break;
272
case GE_FORMAT_8888:
273
DrawSpriteTex<GE_FORMAT_8888, isWhite, alphaBlend, alphaTestZero>(pos0, pos1, s_start, t_start, ds, dt, color0, state, fetchFunc);
274
break;
275
default:
276
// Invalid, don't draw anything...
277
break;
278
}
279
}
280
281
template <bool isWhite>
282
static inline void DrawSpriteTex(const DrawingCoords &pos0, const DrawingCoords &pos1, int s_start, int t_start, int ds, int dt, u32 color0, const RasterizerState &state, Sampler::FetchFunc fetchFunc) {
283
// Standard alpha blending implies skipping alpha zero.
284
if (state.pixelID.alphaBlend)
285
DrawSpriteTex<isWhite, true, true>(pos0, pos1, s_start, t_start, ds, dt, color0, state, fetchFunc);
286
else if (state.pixelID.AlphaTestFunc() != GE_COMP_ALWAYS)
287
DrawSpriteTex<isWhite, false, true>(pos0, pos1, s_start, t_start, ds, dt, color0, state, fetchFunc);
288
else
289
DrawSpriteTex<isWhite, false, false>(pos0, pos1, s_start, t_start, ds, dt, color0, state, fetchFunc);
290
}
291
292
template <GEBufferFormat fmt, bool alphaBlend>
293
static void DrawSpriteNoTex(const DrawingCoords &pos0, const DrawingCoords &pos1, u32 color0, const RasterizerState &state) {
294
if constexpr (alphaBlend)
295
if (Vec4<int>::FromRGBA(color0).a() == 0)
296
return;
297
298
for (int y = pos0.y; y < pos1.y; y++) {
299
if (fmt == GE_FORMAT_8888) {
300
u32 *pixel = fb.Get32Ptr(pos0.x, y, state.pixelID.cached.framebufStride);
301
for (int x = pos0.x; x < pos1.x; x++) {
302
DrawSinglePixel32<alphaBlend>(pixel, color0);
303
pixel++;
304
}
305
} else {
306
u16 *pixel = fb.Get16Ptr(pos0.x, y, state.pixelID.cached.framebufStride);
307
for (int x = pos0.x; x < pos1.x; x++) {
308
DrawSinglePixel<fmt, alphaBlend>(pixel, color0);
309
pixel++;
310
}
311
}
312
}
313
}
314
315
template <bool alphaBlend>
316
static void DrawSpriteNoTex(const DrawingCoords &pos0, const DrawingCoords &pos1, u32 color0, const RasterizerState &state) {
317
switch (state.pixelID.FBFormat()) {
318
case GE_FORMAT_565:
319
DrawSpriteNoTex<GE_FORMAT_565, alphaBlend>(pos0, pos1, color0, state);
320
break;
321
case GE_FORMAT_5551:
322
DrawSpriteNoTex<GE_FORMAT_5551, alphaBlend>(pos0, pos1, color0, state);
323
break;
324
case GE_FORMAT_4444:
325
DrawSpriteNoTex<GE_FORMAT_4444, alphaBlend>(pos0, pos1, color0, state);
326
break;
327
case GE_FORMAT_8888:
328
DrawSpriteNoTex<GE_FORMAT_8888, alphaBlend>(pos0, pos1, color0, state);
329
break;
330
default:
331
// Invalid, don't draw anything...
332
break;
333
}
334
}
335
336
void DrawSprite(const VertexData &v0, const VertexData &v1, const BinCoords &range, const RasterizerState &state) {
337
const u8 *texptr = state.texptr[0];
338
339
GETextureFormat texfmt = state.samplerID.TexFmt();
340
uint16_t texbufw = state.texbufw[0];
341
342
// We won't flush, since we compile all samplers together.
343
Sampler::FetchFunc fetchFunc = Sampler::GetFetchFunc(state.samplerID, nullptr);
344
_dbg_assert_msg_(fetchFunc != nullptr, "Failed to get precompiled fetch func");
345
auto &pixelID = state.pixelID;
346
auto &samplerID = state.samplerID;
347
348
DrawingCoords pos0 = TransformUnit::ScreenToDrawing(v0.screenpos);
349
// Include the ending pixel based on its center, not start.
350
DrawingCoords pos1 = TransformUnit::ScreenToDrawing(v1.screenpos + ScreenCoords(7, 7, 0));
351
352
DrawingCoords scissorTL = TransformUnit::ScreenToDrawing(range.x1, range.y1);
353
DrawingCoords scissorBR = TransformUnit::ScreenToDrawing(range.x2, range.y2);
354
355
const int z = v1.screenpos.z;
356
constexpr int fog = 255;
357
358
// Since it's flat, we can check depth range early. Matters for earlyZChecks.
359
if (pixelID.applyDepthRange && (z < pixelID.cached.minz || z > pixelID.cached.maxz))
360
return;
361
362
bool isWhite = v1.color0 == 0xFFFFFFFF;
363
364
if (state.enableTextures) {
365
// 1:1 (but with mirror support) texture mapping!
366
int s_start = v0.texturecoords.x;
367
int t_start = v0.texturecoords.y;
368
int ds = v1.texturecoords.x > v0.texturecoords.x ? 1 : -1;
369
int dt = v1.texturecoords.y > v0.texturecoords.y ? 1 : -1;
370
371
if (ds < 0) {
372
s_start += ds;
373
}
374
if (dt < 0) {
375
t_start += dt;
376
}
377
378
// First clip the right and bottom sides, since we don't need to adjust the deltas.
379
if (pos1.x > scissorBR.x) pos1.x = scissorBR.x + 1;
380
if (pos1.y > scissorBR.y) pos1.y = scissorBR.y + 1;
381
// Now clip the other sides.
382
if (pos0.x < scissorTL.x) {
383
s_start += (scissorTL.x - pos0.x) * ds;
384
pos0.x = scissorTL.x;
385
}
386
if (pos0.y < scissorTL.y) {
387
t_start += (scissorTL.y - pos0.y) * dt;
388
pos0.y = scissorTL.y;
389
}
390
391
if (UseDrawSinglePixel(pixelID) && (samplerID.TexFunc() == GE_TEXFUNC_MODULATE || samplerID.TexFunc() == GE_TEXFUNC_REPLACE) && samplerID.useTextureAlpha) {
392
if (isWhite || samplerID.TexFunc() == GE_TEXFUNC_REPLACE) {
393
DrawSpriteTex<true>(pos0, pos1, s_start, t_start, ds, dt, v1.color0, state, fetchFunc);
394
} else {
395
DrawSpriteTex<false>(pos0, pos1, s_start, t_start, ds, dt, v1.color0, state, fetchFunc);
396
}
397
} else {
398
float dsf = ds * (1.0f / (float)(1 << state.samplerID.width0Shift));
399
float dtf = dt * (1.0f / (float)(1 << state.samplerID.height0Shift));
400
float sf_start = s_start * (1.0f / (float)(1 << state.samplerID.width0Shift));
401
float tf_start = t_start * (1.0f / (float)(1 << state.samplerID.height0Shift));
402
403
float t = tf_start;
404
const Vec4<int> c0 = Vec4<int>::FromRGBA(v1.color0);
405
if (pixelID.earlyZChecks) {
406
for (int y = pos0.y; y < pos1.y; y++) {
407
float s = sf_start;
408
// Not really that fast but faster than triangle.
409
for (int x = pos0.x; x < pos1.x; x++) {
410
if (CheckDepthTestPassed(pixelID.DepthTestFunc(), x, y, pixelID.cached.depthbufStride, z)) {
411
Vec4<int> prim_color = state.nearest(s, t, ToVec4IntArg(c0), &texptr, &texbufw, 0, 0, state.samplerID);
412
state.drawPixel(x, y, z, fog, ToVec4IntArg(prim_color), pixelID);
413
}
414
415
s += dsf;
416
}
417
t += dtf;
418
}
419
} else {
420
for (int y = pos0.y; y < pos1.y; y++) {
421
float s = sf_start;
422
// Not really that fast but faster than triangle.
423
for (int x = pos0.x; x < pos1.x; x++) {
424
Vec4<int> prim_color = state.nearest(s, t, ToVec4IntArg(c0), &texptr, &texbufw, 0, 0, state.samplerID);
425
state.drawPixel(x, y, z, fog, ToVec4IntArg(prim_color), pixelID);
426
s += dsf;
427
}
428
t += dtf;
429
}
430
}
431
}
432
} else {
433
if (pos1.x > scissorBR.x) pos1.x = scissorBR.x + 1;
434
if (pos1.y > scissorBR.y) pos1.y = scissorBR.y + 1;
435
if (pos0.x < scissorTL.x) pos0.x = scissorTL.x;
436
if (pos0.y < scissorTL.y) pos0.y = scissorTL.y;
437
if (UseDrawSinglePixel(pixelID)) {
438
if (pixelID.alphaBlend)
439
DrawSpriteNoTex<true>(pos0, pos1, v1.color0, state);
440
else
441
DrawSpriteNoTex<false>(pos0, pos1, v1.color0, state);
442
} else if (pixelID.earlyZChecks) {
443
const Vec4<int> prim_color = Vec4<int>::FromRGBA(v1.color0);
444
for (int y = pos0.y; y < pos1.y; y++) {
445
for (int x = pos0.x; x < pos1.x; x++) {
446
if (!CheckDepthTestPassed(pixelID.DepthTestFunc(), x, y, pixelID.cached.depthbufStride, z))
447
continue;
448
449
state.drawPixel(x, y, z, fog, ToVec4IntArg(prim_color), pixelID);
450
}
451
}
452
} else {
453
const Vec4<int> prim_color = Vec4<int>::FromRGBA(v1.color0);
454
for (int y = pos0.y; y < pos1.y; y++) {
455
for (int x = pos0.x; x < pos1.x; x++) {
456
state.drawPixel(x, y, z, fog, ToVec4IntArg(prim_color), pixelID);
457
}
458
}
459
}
460
}
461
462
#if defined(SOFTGPU_MEMORY_TAGGING_BASIC) || defined(SOFTGPU_MEMORY_TAGGING_DETAILED)
463
uint32_t bpp = pixelID.FBFormat() == GE_FORMAT_8888 ? 4 : 2;
464
char tag[64]{};
465
// char ztag[64]{};
466
int tagLen = snprintf(tag, sizeof(tag), "DisplayListR_%08x", state.listPC);
467
// int ztagLen = snprintf(ztag, sizeof(ztag), "DisplayListRZ_%08x", state.listPC);
468
469
for (int y = pos0.y; y < pos1.y; y++) {
470
uint32_t row = gstate.getFrameBufAddress() + y * pixelID.cached.framebufStride * bpp;
471
NotifyMemInfo(MemBlockFlags::WRITE, row + pos0.x * bpp, (pos1.x - pos0.x) * bpp, tag, tagLen);
472
}
473
#endif
474
}
475
476
bool g_needsClearAfterDialog = false;
477
478
static inline bool NoClampOrWrap(const RasterizerState &state, const Vec2f &tc) {
479
if (tc.x < 0 || tc.y < 0)
480
return false;
481
if (state.samplerID.cached.sizes[0].w > 512 || state.samplerID.cached.sizes[0].h > 512)
482
return false;
483
return tc.x <= state.samplerID.cached.sizes[0].w && tc.y <= state.samplerID.cached.sizes[0].h;
484
}
485
486
// Returns true if the normal path should be skipped.
487
bool RectangleFastPath(const VertexData &v0, const VertexData &v1, BinManager &binner) {
488
const RasterizerState &state = binner.State();
489
490
g_DarkStalkerStretch = DSStretch::Off;
491
492
// Eliminate the stretch blit in DarkStalkers.
493
// We compensate for that when blitting the framebuffer in SoftGpu.cpp.
494
if (PSP_CoreParameter().compat.flags().DarkStalkersPresentHack && v0.texturecoords.x == 64.0f && v0.texturecoords.y == 16.0f && v1.texturecoords.x == 448.0f && v1.texturecoords.y == 240.0f) {
495
// check for save/load dialog.
496
if (!currentDialogActive) {
497
if (v0.screenpos.x + gstate.getOffsetX16() == 0x7100 && v0.screenpos.y + gstate.getOffsetY16() == 0x7780 && v1.screenpos.x + gstate.getOffsetX16() == 0x8f00 && v1.screenpos.y + gstate.getOffsetY16() == 0x8880) {
498
g_DarkStalkerStretch = DSStretch::Wide;
499
} else if (v0.screenpos.x + gstate.getOffsetX16() == 0x7400 && v0.screenpos.y + gstate.getOffsetY16() == 0x7780 && v1.screenpos.x + gstate.getOffsetX16() == 0x8C00 && v1.screenpos.y + gstate.getOffsetY16() == 0x8880) {
500
g_DarkStalkerStretch = DSStretch::Normal;
501
} else {
502
return false;
503
}
504
if (g_needsClearAfterDialog) {
505
g_needsClearAfterDialog = false;
506
// Afterwards, we also need to clear the actual destination. Can do a fast rectfill.
507
gstate.textureMapEnable &= ~1;
508
VertexData newV1 = v1;
509
newV1.color0 = 0xFF000000;
510
binner.AddSprite(v0, newV1);
511
gstate.textureMapEnable |= 1;
512
}
513
return true;
514
} else {
515
g_needsClearAfterDialog = true;
516
}
517
}
518
519
// Check for 1:1 texture mapping. In that case we can call DrawSprite.
520
int xdiff = v1.screenpos.x - v0.screenpos.x;
521
int ydiff = v1.screenpos.y - v0.screenpos.y;
522
int udiff = (v1.texturecoords.x - v0.texturecoords.x) * (float)SCREEN_SCALE_FACTOR;
523
int vdiff = (v1.texturecoords.y - v0.texturecoords.y) * (float)SCREEN_SCALE_FACTOR;
524
525
// Currently only works for TL/BR, which is the most common but not required.
526
bool orient_check = xdiff >= 0 && ydiff >= 0;
527
// We already have a fast path for clear in ClearRectangle.
528
bool state_check = state.throughMode && !state.pixelID.clearMode && !state.samplerID.hasAnyMips && !state.textureProj;
529
bool coord_check = true;
530
if (state.enableTextures) {
531
state_check = state_check && NoClampOrWrap(state, v0.texturecoords.uv()) && NoClampOrWrap(state, v1.texturecoords.uv());
532
coord_check = (xdiff == udiff || xdiff == -udiff) && (ydiff == vdiff || ydiff == -vdiff);
533
}
534
// This doesn't work well with offset drawing, see #15876. Through never has a subpixel offset.
535
bool subpixel_check = ((v0.screenpos.x | v0.screenpos.y | v1.screenpos.x | v1.screenpos.y) & 0xF) == 0;
536
if (coord_check && orient_check && state_check && subpixel_check) {
537
binner.AddSprite(v0, v1);
538
return true;
539
}
540
return false;
541
}
542
543
static bool IsCoordRectangleCompatible(const RasterizerState &state, const ClipVertexData &data) {
544
if (!state.throughMode) {
545
// See AreCoordsRectangleCompatible() for most of these, this just checks the main vert.
546
if (data.OutsideRange())
547
return false;
548
if (data.clippos.w < 0.0f)
549
return false;
550
if (data.clippos.z < -data.clippos.w)
551
return false;
552
}
553
return true;
554
}
555
556
static bool AreCoordsRectangleCompatible(const RasterizerState &state, const ClipVertexData &data0, const ClipVertexData &data1) {
557
if (data1.v.color0 != data0.v.color0)
558
return false;
559
if (data1.v.screenpos.z != data0.v.screenpos.z) {
560
// Sometimes, we don't actually care about z.
561
if (state.pixelID.depthWrite || state.pixelID.DepthTestFunc() != GE_COMP_ALWAYS)
562
return false;
563
}
564
if (!state.throughMode) {
565
if (data1.v.color1 != data0.v.color1)
566
return false;
567
// This means it should be culled, outside range.
568
if (data1.OutsideRange())
569
return false;
570
// Do we have to think about perspective correction or slope mip level?
571
if (state.enableTextures && data1.clippos.w != data0.clippos.w) {
572
// If the w is off by less than a factor of 1/512, it should be safe to treat as a rectangle.
573
static constexpr float halftexel = 0.5f / 512.0f;
574
if (data1.clippos.w - halftexel > data0.clippos.w || data1.clippos.w + halftexel < data0.clippos.w)
575
return false;
576
}
577
// We might need to cull this if all verts have negative w, which doesn't seem to happen for rectangles.
578
if (data1.clippos.w < 0.0f)
579
return false;
580
// And we also may need to clip, even if flat.
581
if (data1.clippos.z < -data1.clippos.w)
582
return false;
583
// If we're projecting textures, only allow an exact match for simplicity.
584
if (state.enableTextures && data1.v.texturecoords.q() != data0.v.texturecoords.q())
585
return false;
586
if (state.pixelID.applyFog && data1.v.fogdepth != data0.v.fogdepth) {
587
// Similar to w, this only matters if they're farther apart than 1/255.
588
static constexpr float foghalfstep = 0.5f / 255.0f;
589
if (data1.v.fogdepth - foghalfstep > data0.v.fogdepth || data1.v.fogdepth + foghalfstep < data0.v.fogdepth)
590
return false;
591
}
592
}
593
return true;
594
}
595
596
bool DetectRectangleFromStrip(const RasterizerState &state, const ClipVertexData data[4], int *tlIndex, int *brIndex) {
597
if (!IsCoordRectangleCompatible(state, data[0]))
598
return false;
599
600
// Color and Z must be flat. Also find the TL and BR meanwhile.
601
int tl = 0, br = 0;
602
for (int i = 1; i < 4; ++i) {
603
if (!AreCoordsRectangleCompatible(state, data[0], data[i]))
604
return false;
605
606
if (data[i].v.screenpos.x <= data[tl].v.screenpos.x && data[i].v.screenpos.y <= data[tl].v.screenpos.y)
607
tl = i;
608
if (data[i].v.screenpos.x >= data[br].v.screenpos.x && data[i].v.screenpos.y >= data[br].v.screenpos.y)
609
br = i;
610
}
611
612
*tlIndex = tl;
613
*brIndex = br;
614
615
// OK, now let's look at data to detect rectangles. There are a few possibilities
616
// but we focus on Darkstalkers for now.
617
if (data[0].v.screenpos.x == data[1].v.screenpos.x &&
618
data[0].v.screenpos.y == data[2].v.screenpos.y &&
619
data[2].v.screenpos.x == data[3].v.screenpos.x &&
620
data[1].v.screenpos.y == data[3].v.screenpos.y) {
621
// Okay, this is in the shape of a rectangle, but what about texture?
622
if (!state.enableTextures)
623
return true;
624
625
if (data[0].v.texturecoords.x == data[1].v.texturecoords.x &&
626
data[0].v.texturecoords.y == data[2].v.texturecoords.y &&
627
data[2].v.texturecoords.x == data[3].v.texturecoords.x &&
628
data[1].v.texturecoords.y == data[3].v.texturecoords.y) {
629
// It's a rectangle!
630
return true;
631
}
632
return false;
633
}
634
// There's the other vertex order too...
635
if (data[0].v.screenpos.x == data[2].v.screenpos.x &&
636
data[0].v.screenpos.y == data[1].v.screenpos.y &&
637
data[1].v.screenpos.x == data[3].v.screenpos.x &&
638
data[2].v.screenpos.y == data[3].v.screenpos.y) {
639
// Okay, this is in the shape of a rectangle, but what about texture?
640
if (!state.enableTextures)
641
return true;
642
643
if (data[0].v.texturecoords.x == data[2].v.texturecoords.x &&
644
data[0].v.texturecoords.y == data[1].v.texturecoords.y &&
645
data[1].v.texturecoords.x == data[3].v.texturecoords.x &&
646
data[2].v.texturecoords.y == data[3].v.texturecoords.y) {
647
// It's a rectangle!
648
return true;
649
}
650
return false;
651
}
652
return false;
653
}
654
655
bool DetectRectangleFromFan(const RasterizerState &state, const ClipVertexData *data, int *tlIndex, int *brIndex) {
656
if (!IsCoordRectangleCompatible(state, data[0]))
657
return false;
658
659
// Color and Z must be flat.
660
int tl = 0, br = 0;
661
for (int i = 1; i < 4; ++i) {
662
if (!AreCoordsRectangleCompatible(state, data[0], data[i]))
663
return false;
664
665
if (data[i].v.screenpos.x <= data[tl].v.screenpos.x && data[i].v.screenpos.y <= data[tl].v.screenpos.y)
666
tl = i;
667
if (data[i].v.screenpos.x >= data[br].v.screenpos.x && data[i].v.screenpos.y >= data[br].v.screenpos.y)
668
br = i;
669
}
670
671
*tlIndex = tl;
672
*brIndex = br;
673
674
int tr = 1, bl = 1;
675
for (int i = 0; i < 4; ++i) {
676
if (i == tl || i == br)
677
continue;
678
679
if (data[i].v.screenpos.x <= data[tl].v.screenpos.x && data[i].v.screenpos.y >= data[tl].v.screenpos.y)
680
bl = i;
681
if (data[i].v.screenpos.x >= data[br].v.screenpos.x && data[i].v.screenpos.y <= data[br].v.screenpos.y)
682
tr = i;
683
}
684
685
// Must have found each of the coordinates.
686
if (tl + tr + bl + br != 6)
687
return false;
688
689
// Note the common case is a single TL-TR-BR-BL.
690
const auto &postl = data[tl].v.screenpos, &postr = data[tr].v.screenpos;
691
const auto &posbr = data[br].v.screenpos, &posbl = data[bl].v.screenpos;
692
if (postl.x == posbl.x && postr.x == posbr.x && postl.y == postr.y && posbl.y == posbr.y) {
693
// Do we need to think about rotation?
694
if (!state.enableTextures)
695
return true;
696
697
const auto &textl = data[tl].v.texturecoords, &textr = data[tr].v.texturecoords;
698
const auto &texbl = data[bl].v.texturecoords, &texbr = data[br].v.texturecoords;
699
700
if (textl.x == texbl.x && textr.x == texbr.x && textl.y == textr.y && texbl.y == texbr.y) {
701
// Okay, the texture is also good, but let's avoid rotation issues.
702
return textl.y < texbr.y && postl.y < posbr.y && textl.x < texbr.x && postl.x < posbr.x;
703
}
704
}
705
706
return false;
707
}
708
709
bool DetectRectangleFromPair(const RasterizerState &state, const ClipVertexData data[6], int *tlIndex, int *brIndex) {
710
if (!IsCoordRectangleCompatible(state, data[0]))
711
return false;
712
713
// Color and Z must be flat. Also find the TL and BR meanwhile.
714
int tl = 0, br = 0;
715
for (int i = 1; i < 6; ++i) {
716
if (!AreCoordsRectangleCompatible(state, data[0], data[i]))
717
return false;
718
719
if (data[i].v.screenpos.x <= data[tl].v.screenpos.x && data[i].v.screenpos.y <= data[tl].v.screenpos.y)
720
tl = i;
721
if (data[i].v.screenpos.x >= data[br].v.screenpos.x && data[i].v.screenpos.y >= data[br].v.screenpos.y)
722
br = i;
723
}
724
725
*tlIndex = tl;
726
*brIndex = br;
727
728
auto xat = [&](int i) { return data[i].v.screenpos.x; };
729
auto yat = [&](int i) { return data[i].v.screenpos.y; };
730
auto uat = [&](int i) { return data[i].v.texturecoords.x; };
731
auto vat = [&](int i) { return data[i].v.texturecoords.y; };
732
733
// A likely order would be: TL, TR, BR, TL, BR, BL. We'd have the last index of each.
734
// TODO: Make more generic.
735
if (tl == 3 && br == 4) {
736
bool x1_match = xat(0) == xat(3) && xat(0) == xat(5);
737
bool x2_match = xat(1) == xat(2) && xat(1) == xat(4);
738
bool y1_match = yat(0) == yat(1) && yat(0) == yat(3);
739
bool y2_match = yat(2) == yat(4) && yat(2) == yat(5);
740
if (x1_match && y1_match && x2_match && y2_match) {
741
// Do we need to think about rotation or UVs?
742
if (!state.enableTextures)
743
return true;
744
745
x1_match = uat(0) == uat(3) && uat(0) == uat(5);
746
x2_match = uat(1) == uat(2) && uat(1) == uat(4);
747
y1_match = vat(0) == vat(1) && vat(0) == vat(3);
748
y2_match = vat(2) == vat(4) && vat(2) == vat(5);
749
if (x1_match && y1_match && x2_match && y2_match) {
750
// Double check rotation direction.
751
return vat(tl) < vat(br) && yat(tl) < yat(br) && uat(tl) < uat(br) && xat(tl) < xat(br);
752
}
753
}
754
}
755
756
return false;
757
}
758
759
bool DetectRectangleThroughModeSlices(const RasterizerState &state, const ClipVertexData data[4]) {
760
// Color and Z must be flat.
761
for (int i = 1; i < 4; ++i) {
762
if (!(data[i].v.color0 == data[0].v.color0))
763
return false;
764
if (!(data[i].v.screenpos.z == data[0].v.screenpos.z)) {
765
// Sometimes, we don't actually care about z.
766
if (state.pixelID.depthWrite || state.pixelID.DepthTestFunc() != GE_COMP_ALWAYS)
767
return false;
768
}
769
}
770
771
// Games very commonly use vertical strips of rectangles. Detect and combine.
772
const auto &tl1 = data[0].v.screenpos, &br1 = data[1].v.screenpos;
773
const auto &tl2 = data[2].v.screenpos, &br2 = data[3].v.screenpos;
774
if (tl1.y == tl2.y && br1.y == br2.y && br1.y > tl1.y) {
775
if (br1.x == tl2.x && tl1.x < br1.x && tl2.x < br2.x) {
776
if (!state.enableTextures)
777
return true;
778
779
const auto &textl1 = data[0].v.texturecoords, &texbr1 = data[1].v.texturecoords;
780
const auto &textl2 = data[2].v.texturecoords, &texbr2 = data[3].v.texturecoords;
781
if (textl1.y != textl2.y || texbr1.y != texbr2.y || textl1.y > texbr1.y)
782
return false;
783
if (texbr1.x != textl2.x || textl1.x > texbr1.x || textl2.x > texbr2.x)
784
return false;
785
786
// We might be able to compare ratios, but let's expect 1:1.
787
int texdiff1 = (texbr1.x - textl1.x) * (float)SCREEN_SCALE_FACTOR;
788
int texdiff2 = (texbr2.x - textl2.x) * (float)SCREEN_SCALE_FACTOR;
789
int posdiff1 = br1.x - tl1.x;
790
int posdiff2 = br2.x - tl2.x;
791
return texdiff1 == posdiff1 && texdiff2 == posdiff2;
792
}
793
}
794
795
return false;
796
}
797
798
} // namespace Rasterizer
799
800
801