Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Software/Rasterizer.cpp
3186 views
1
// Copyright (c) 2013- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "ppsspp_config.h"
19
#include <cmath>
20
21
#include "Common/Common.h"
22
#include "Common/CPUDetect.h"
23
#include "Common/Data/Convert/ColorConv.h"
24
#include "Common/Profiler/Profiler.h"
25
#include "Common/StringUtils.h"
26
#include "Core/Config.h"
27
#include "Core/Debugger/MemBlockInfo.h"
28
#include "Core/MemMap.h"
29
#include "GPU/GPUState.h"
30
31
#include "GPU/Common/TextureDecoder.h"
32
#include "GPU/Software/BinManager.h"
33
#include "GPU/Software/DrawPixel.h"
34
#include "GPU/Software/Rasterizer.h"
35
#include "GPU/Software/Sampler.h"
36
#include "GPU/Software/SoftGpu.h"
37
#include "GPU/Software/TransformUnit.h"
38
39
#include "Common/Math/SIMDHeaders.h"
40
41
// For the SSE4 stuff
42
#if PPSSPP_ARCH(SSE2)
43
#include <smmintrin.h>
44
#endif
45
46
namespace Rasterizer {
47
48
// Only OK on x64 where our stack is aligned
49
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
50
static inline __m128 InterpolateF(const __m128 &c0, const __m128 &c1, const __m128 &c2, int w0, int w1, int w2, float wsum) {
51
__m128 v = _mm_mul_ps(c0, _mm_cvtepi32_ps(_mm_set1_epi32(w0)));
52
v = _mm_add_ps(v, _mm_mul_ps(c1, _mm_cvtepi32_ps(_mm_set1_epi32(w1))));
53
v = _mm_add_ps(v, _mm_mul_ps(c2, _mm_cvtepi32_ps(_mm_set1_epi32(w2))));
54
return _mm_mul_ps(v, _mm_set_ps1(wsum));
55
}
56
57
static inline __m128i InterpolateI(const __m128i &c0, const __m128i &c1, const __m128i &c2, int w0, int w1, int w2, float wsum) {
58
return _mm_cvtps_epi32(InterpolateF(_mm_cvtepi32_ps(c0), _mm_cvtepi32_ps(c1), _mm_cvtepi32_ps(c2), w0, w1, w2, wsum));
59
}
60
#elif PPSSPP_ARCH(ARM64_NEON)
61
static inline float32x4_t InterpolateF(const float32x4_t &c0, const float32x4_t &c1, const float32x4_t &c2, int w0, int w1, int w2, float wsum) {
62
float32x4_t v = vmulq_f32(c0, vcvtq_f32_s32(vdupq_n_s32(w0)));
63
v = vaddq_f32(v, vmulq_f32(c1, vcvtq_f32_s32(vdupq_n_s32(w1))));
64
v = vaddq_f32(v, vmulq_f32(c2, vcvtq_f32_s32(vdupq_n_s32(w2))));
65
return vmulq_f32(v, vdupq_n_f32(wsum));
66
}
67
68
static inline int32x4_t InterpolateI(const int32x4_t &c0, const int32x4_t &c1, const int32x4_t &c2, int w0, int w1, int w2, float wsum) {
69
return vcvtq_s32_f32(InterpolateF(vcvtq_f32_s32(c0), vcvtq_f32_s32(c1), vcvtq_f32_s32(c2), w0, w1, w2, wsum));
70
}
71
#endif
72
73
// NOTE: When not casting color0 and color1 to float vectors, this code suffers from severe overflow issues.
74
// Not sure if that should be regarded as a bug or if casting to float is a valid fix.
75
76
static inline Vec4<int> Interpolate(const Vec4<int> &c0, const Vec4<int> &c1, const Vec4<int> &c2, int w0, int w1, int w2, float wsum) {
77
#if (defined(_M_SSE) || PPSSPP_ARCH(ARM64_NEON)) && !PPSSPP_ARCH(X86)
78
return Vec4<int>(InterpolateI(c0.ivec, c1.ivec, c2.ivec, w0, w1, w2, wsum));
79
#else
80
return ((c0.Cast<float>() * w0 + c1.Cast<float>() * w1 + c2.Cast<float>() * w2) * wsum).Cast<int>();
81
#endif
82
}
83
84
static inline Vec3<int> Interpolate(const Vec3<int> &c0, const Vec3<int> &c1, const Vec3<int> &c2, int w0, int w1, int w2, float wsum) {
85
#if (defined(_M_SSE) || PPSSPP_ARCH(ARM64_NEON)) && !PPSSPP_ARCH(X86)
86
return Vec3<int>(InterpolateI(c0.ivec, c1.ivec, c2.ivec, w0, w1, w2, wsum));
87
#else
88
return ((c0.Cast<float>() * w0 + c1.Cast<float>() * w1 + c2.Cast<float>() * w2) * wsum).Cast<int>();
89
#endif
90
}
91
92
static inline Vec4<float> Interpolate(const float &c0, const float &c1, const float &c2, const Vec4<float> &w0, const Vec4<float> &w1, const Vec4<float> &w2, const Vec4<float> &wsum_recip) {
93
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
94
__m128 v = _mm_mul_ps(w0.vec, _mm_set1_ps(c0));
95
v = _mm_add_ps(v, _mm_mul_ps(w1.vec, _mm_set1_ps(c1)));
96
v = _mm_add_ps(v, _mm_mul_ps(w2.vec, _mm_set1_ps(c2)));
97
return _mm_mul_ps(v, wsum_recip.vec);
98
#elif PPSSPP_ARCH(ARM64_NEON)
99
float32x4_t v = vmulq_f32(w0.vec, vdupq_n_f32(c0));
100
v = vaddq_f32(v, vmulq_f32(w1.vec, vdupq_n_f32(c1)));
101
v = vaddq_f32(v, vmulq_f32(w2.vec, vdupq_n_f32(c2)));
102
return vmulq_f32(v, wsum_recip.vec);
103
#else
104
return (w0 * c0 + w1 * c1 + w2 * c2) * wsum_recip;
105
#endif
106
}
107
108
static inline Vec4<float> Interpolate(const float &c0, const float &c1, const float &c2, const Vec4<int> &w0, const Vec4<int> &w1, const Vec4<int> &w2, const Vec4<float> &wsum_recip) {
109
return Interpolate(c0, c1, c2, w0.Cast<float>(), w1.Cast<float>(), w2.Cast<float>(), wsum_recip);
110
}
111
112
void ComputeRasterizerState(RasterizerState *state, BinManager *binner) {
113
ComputePixelFuncID(&state->pixelID);
114
state->drawPixel = Rasterizer::GetSingleFunc(state->pixelID, binner);
115
116
state->enableTextures = gstate.isTextureMapEnabled() && !state->pixelID.clearMode;
117
if (state->enableTextures) {
118
ComputeSamplerID(&state->samplerID);
119
state->linear = Sampler::GetLinearFunc(state->samplerID, binner);
120
state->nearest = Sampler::GetNearestFunc(state->samplerID, binner);
121
122
// Since the definitions are the same, just force this setting using the func pointer.
123
if (g_Config.iTexFiltering == TEX_FILTER_FORCE_LINEAR) {
124
state->nearest = state->linear;
125
} else if (g_Config.iTexFiltering == TEX_FILTER_FORCE_NEAREST) {
126
state->linear = state->nearest;
127
}
128
129
state->maxTexLevel = state->samplerID.hasAnyMips ? gstate.getTextureMaxLevel() : 0;
130
131
GETextureFormat texfmt = state->samplerID.TexFmt();
132
for (uint8_t i = 0; i <= state->maxTexLevel; i++) {
133
u32 texaddr = gstate.getTextureAddress(i);
134
state->texaddr[i] = texaddr;
135
state->texbufw[i] = (uint16_t)GetTextureBufw(i, texaddr, texfmt);
136
if (Memory::IsValidAddress(texaddr))
137
state->texptr[i] = Memory::GetPointerUnchecked(texaddr);
138
else
139
state->texptr[i] = nullptr;
140
}
141
142
state->textureLodSlope = gstate.getTextureLodSlope();
143
state->texLevelMode = gstate.getTexLevelMode();
144
state->texLevelOffset = (int8_t)gstate.getTexLevelOffset16();
145
state->mipFilt = gstate.isMipmapFilteringEnabled();
146
state->minFilt = gstate.isMinifyFilteringEnabled();
147
state->magFilt = gstate.isMagnifyFilteringEnabled();
148
state->textureProj = gstate.getUVGenMode() == GE_TEXMAP_TEXTURE_MATRIX;
149
if (state->textureProj) {
150
// We may be able to optimize this off. This is actually kinda common.
151
const bool qZeroST = gstate.tgenMatrix[2] == 0.0f && gstate.tgenMatrix[5] == 0.0f;
152
const bool qZeroQ = gstate.tgenMatrix[8] == 0.0f;
153
154
// Two common cases: the source q factor is zero, OR source is UV.
155
const bool qFactorZero = gstate.getUVProjMode() == GE_PROJMAP_UV;
156
if (qZeroST && (qZeroQ || qFactorZero) && gstate.tgenMatrix[11] == 1.0f) {
157
state->textureProj = false;
158
}
159
}
160
}
161
162
state->shadeGouraud = !gstate.isModeClear() && gstate.getShadeMode() == GE_SHADE_GOURAUD;
163
state->throughMode = gstate.isModeThrough();
164
state->antialiasLines = gstate.isAntiAliasEnabled();
165
166
#if defined(SOFTGPU_MEMORY_TAGGING_DETAILED) || defined(SOFTGPU_MEMORY_TAGGING_BASIC)
167
DisplayList currentList{};
168
if (gpuDebug)
169
gpuDebug->GetCurrentDisplayList(currentList);
170
state->listPC = currentList.pc;
171
#endif
172
}
173
174
static inline void CalculateRasterStateFlags(RasterizerState *state, const VertexData &v0, bool useColor) {
175
if (useColor) {
176
if ((v0.color0 & 0x00FFFFFF) != 0x00FFFFFF)
177
state->flags |= RasterizerStateFlags::VERTEX_NON_FULL_WHITE;
178
uint8_t alpha = v0.color0 >> 24;
179
if (alpha != 0)
180
state->flags |= RasterizerStateFlags::VERTEX_ALPHA_NON_ZERO;
181
if (alpha != 0xFF)
182
state->flags |= RasterizerStateFlags::VERTEX_ALPHA_NON_FULL;
183
}
184
if (!(v0.fogdepth >= 1.0f))
185
state->flags |= RasterizerStateFlags::VERTEX_HAS_FOG;
186
}
187
188
void CalculateRasterStateFlags(RasterizerState *state, const VertexData &v0) {
189
CalculateRasterStateFlags(state, v0, true);
190
}
191
192
void CalculateRasterStateFlags(RasterizerState *state, const VertexData &v0, const VertexData &v1, bool forceFlat) {
193
CalculateRasterStateFlags(state, v0, !forceFlat && state->shadeGouraud);
194
CalculateRasterStateFlags(state, v1, true);
195
}
196
197
void CalculateRasterStateFlags(RasterizerState *state, const VertexData &v0, const VertexData &v1, const VertexData &v2) {
198
CalculateRasterStateFlags(state, v0, state->shadeGouraud);
199
CalculateRasterStateFlags(state, v1, state->shadeGouraud);
200
CalculateRasterStateFlags(state, v2, true);
201
}
202
203
static inline int OptimizePixelIDFlags(const RasterizerStateFlags &flags) {
204
return (int)flags & (int)RasterizerStateFlags::OPTIMIZED_PIXELID;
205
}
206
207
static inline int OptimizeSamplerIDFlags(const RasterizerStateFlags &flags) {
208
return (int)flags & (int)RasterizerStateFlags::OPTIMIZED_SAMPLERID;
209
}
210
211
static inline int OptimizeAllFlags(const RasterizerStateFlags &flags) {
212
return OptimizePixelIDFlags(flags) | OptimizeSamplerIDFlags(flags);
213
}
214
215
static inline RasterizerStateFlags ClearFlags(const RasterizerStateFlags &flags, const RasterizerStateFlags &mask) {
216
int clearBits = (int)flags & (int)mask;
217
return (RasterizerStateFlags)((int)flags & ~clearBits);
218
}
219
220
static inline RasterizerStateFlags ReplacePixelIDFlags(const RasterizerStateFlags &flags, const RasterizerStateFlags &replace) {
221
RasterizerStateFlags updated = ClearFlags(flags, RasterizerStateFlags::OPTIMIZED_PIXELID);
222
return updated | (RasterizerStateFlags)OptimizePixelIDFlags(replace);
223
}
224
225
static inline RasterizerStateFlags ReplaceSamplerIDFlags(const RasterizerStateFlags &flags, const RasterizerStateFlags &replace) {
226
RasterizerStateFlags updated = ClearFlags(flags, RasterizerStateFlags::OPTIMIZED_SAMPLERID);
227
return updated | (RasterizerStateFlags)OptimizeSamplerIDFlags(replace);
228
}
229
230
static bool CheckClutAlphaFull(RasterizerState *state) {
231
// We only need to check it once.
232
if (state->flags & RasterizerStateFlags::CLUT_ALPHA_CHECKED)
233
return !(state->flags & RasterizerStateFlags::CLUT_ALPHA_NON_FULL);
234
// For now, let's keep things simple.
235
const SamplerID &samplerID = state->samplerID;
236
if (samplerID.hasClutOffset || !samplerID.useSharedClut)
237
return false;
238
239
uint32_t count = samplerID.TexFmt() == GE_TFMT_CLUT4 ? 16 : 256;
240
if (samplerID.hasClutMask)
241
count = std::min(count, ((samplerID.cached.clutFormat >> 8) & 0xFF) + 1);
242
243
u32 alphaSum = 0xFFFFFFFF;
244
if (samplerID.ClutFmt() == GE_CMODE_32BIT_ABGR8888) {
245
CheckMask32((const uint32_t *)samplerID.cached.clut, count, &alphaSum);
246
} else {
247
CheckMask16((const uint16_t *)samplerID.cached.clut, count, &alphaSum);
248
}
249
250
bool onlyFull = true;
251
switch (samplerID.ClutFmt()) {
252
case GE_CMODE_16BIT_BGR5650:
253
break;
254
255
case GE_CMODE_16BIT_ABGR5551:
256
onlyFull = (alphaSum & 0x8000) != 0;
257
break;
258
259
case GE_CMODE_16BIT_ABGR4444:
260
onlyFull = (alphaSum & 0xF000) == 0xF000;
261
break;
262
263
case GE_CMODE_32BIT_ABGR8888:
264
onlyFull = (alphaSum & 0xFF000000) == 0xFF000000;
265
break;
266
}
267
268
// Might just be different patterns, but if alphaSum != 0, it can't contain zero.
269
if (alphaSum != 0)
270
state->flags |= RasterizerStateFlags::CLUT_ALPHA_NON_ZERO;
271
if (!onlyFull)
272
state->flags |= RasterizerStateFlags::CLUT_ALPHA_NON_FULL;
273
state->flags |= RasterizerStateFlags::CLUT_ALPHA_CHECKED;
274
275
return onlyFull;
276
}
277
278
static RasterizerStateFlags DetectStateOptimizations(RasterizerState *state) {
279
// Note: all optimizations must be undoable.
280
RasterizerStateFlags optimize = RasterizerStateFlags::NONE;
281
auto &pixelID = state->pixelID;
282
auto &samplerID = state->samplerID;
283
284
bool alphaZero = !(state->flags & RasterizerStateFlags::VERTEX_ALPHA_NON_ZERO);
285
bool alphaFull = !(state->flags & RasterizerStateFlags::VERTEX_ALPHA_NON_FULL);
286
bool needTextureAlpha = state->enableTextures && samplerID.useTextureAlpha;
287
288
if (!pixelID.clearMode) {
289
auto &cached = pixelID.cached;
290
291
bool alphaBlend = pixelID.alphaBlend || (state->flags & RasterizerStateFlags::OPTIMIZED_BLEND_OFF);
292
if (needTextureAlpha && alphaBlend && alphaFull) {
293
bool usesClut = (samplerID.texfmt & 4) != 0;
294
if (usesClut && CheckClutAlphaFull(state))
295
needTextureAlpha = false;
296
}
297
298
if (alphaBlend && !needTextureAlpha) {
299
PixelBlendFactor src = pixelID.AlphaBlendSrc();
300
PixelBlendFactor dst = pixelID.AlphaBlendDst();
301
if (state->flags & RasterizerStateFlags::OPTIMIZED_BLEND_SRC)
302
src = PixelBlendFactor::SRCALPHA;
303
if (state->flags & RasterizerStateFlags::OPTIMIZED_BLEND_DST)
304
dst = PixelBlendFactor::INVSRCALPHA;
305
306
// Okay, we may be able to convert this to a fixed value.
307
if (alphaZero || alphaFull) {
308
// If it was already set and we still can, set it again.
309
if (src == PixelBlendFactor::SRCALPHA)
310
optimize |= RasterizerStateFlags::OPTIMIZED_BLEND_SRC;
311
if (dst == PixelBlendFactor::INVSRCALPHA)
312
optimize |= RasterizerStateFlags::OPTIMIZED_BLEND_DST;
313
}
314
if (alphaFull && (src == PixelBlendFactor::SRCALPHA || src == PixelBlendFactor::ONE) && (dst == PixelBlendFactor::INVSRCALPHA || dst == PixelBlendFactor::ZERO)) {
315
optimize |= RasterizerStateFlags::OPTIMIZED_BLEND_OFF;
316
}
317
}
318
319
if (alphaBlend && (needTextureAlpha || !alphaFull)) {
320
// Okay, we're blending, and we need to. Are we alpha testing?
321
GEComparison alphaTestFunc = pixelID.AlphaTestFunc();
322
if (state->flags & RasterizerStateFlags::OPTIMIZED_ALPHATEST_OFF_NE)
323
alphaTestFunc = GE_COMP_NOTEQUAL;
324
if (state->flags & RasterizerStateFlags::OPTIMIZED_ALPHATEST_OFF_GT)
325
alphaTestFunc = GE_COMP_GREATER;
326
if (state->flags & RasterizerStateFlags::OPTIMIZED_ALPHATEST_ON)
327
alphaTestFunc = GE_COMP_ALWAYS;
328
329
PixelBlendFactor src = pixelID.AlphaBlendSrc();
330
PixelBlendFactor dst = pixelID.AlphaBlendDst();
331
if (state->flags & RasterizerStateFlags::OPTIMIZED_BLEND_SRC)
332
src = PixelBlendFactor::SRCALPHA;
333
if (state->flags & RasterizerStateFlags::OPTIMIZED_BLEND_DST)
334
dst = PixelBlendFactor::INVSRCALPHA;
335
336
if (alphaTestFunc == GE_COMP_ALWAYS && src == PixelBlendFactor::SRCALPHA && dst == PixelBlendFactor::INVSRCALPHA) {
337
bool usesClut = (samplerID.texfmt & 4) != 0;
338
bool couldHaveZeroTexAlpha = true;
339
if (usesClut && CheckClutAlphaFull(state))
340
couldHaveZeroTexAlpha = false;
341
if (state->flags & RasterizerStateFlags::CLUT_ALPHA_NON_ZERO)
342
couldHaveZeroTexAlpha = false;
343
344
// Blending is expensive, since we read the target. Force alpha testing on.
345
if (!pixelID.depthWrite && !pixelID.stencilTest && couldHaveZeroTexAlpha)
346
optimize |= RasterizerStateFlags::OPTIMIZED_ALPHATEST_ON;
347
}
348
}
349
350
bool applyFog = pixelID.applyFog || (state->flags & RasterizerStateFlags::OPTIMIZED_FOG_OFF);
351
if (applyFog) {
352
bool hasFog = state->flags & RasterizerStateFlags::VERTEX_HAS_FOG;
353
if (!hasFog)
354
optimize |= RasterizerStateFlags::OPTIMIZED_FOG_OFF;
355
}
356
}
357
358
if (state->enableTextures) {
359
bool colorFull = !(state->flags & RasterizerStateFlags::VERTEX_NON_FULL_WHITE);
360
if (colorFull && (!needTextureAlpha || alphaFull)) {
361
// Modulate is common, sometimes even with a fixed color. Replace is cheaper.
362
GETexFunc texFunc = samplerID.TexFunc();
363
if (state->flags & RasterizerStateFlags::OPTIMIZED_TEXREPLACE)
364
texFunc = GE_TEXFUNC_MODULATE;
365
366
if (texFunc == GE_TEXFUNC_MODULATE)
367
optimize |= RasterizerStateFlags::OPTIMIZED_TEXREPLACE;
368
}
369
370
bool usesClut = (samplerID.texfmt & 4) != 0;
371
if (usesClut && alphaFull && samplerID.useTextureAlpha) {
372
GEComparison alphaTestFunc = pixelID.AlphaTestFunc();
373
// We optimize > 0 to != 0, so this is especially common.
374
if (state->flags & RasterizerStateFlags::OPTIMIZED_ALPHATEST_OFF_NE)
375
alphaTestFunc = GE_COMP_NOTEQUAL;
376
// > 16, 8, or similar are also very common.
377
if (state->flags & RasterizerStateFlags::OPTIMIZED_ALPHATEST_OFF_GT)
378
alphaTestFunc = GE_COMP_GREATER;
379
if (state->flags & RasterizerStateFlags::OPTIMIZED_ALPHATEST_ON)
380
alphaTestFunc = GE_COMP_ALWAYS;
381
382
bool alphaTest = (alphaTestFunc == GE_COMP_NOTEQUAL || alphaTestFunc == GE_COMP_GREATER) && pixelID.alphaTestRef < 0xFF && !state->pixelID.hasAlphaTestMask;
383
if (alphaTest) {
384
bool canSkipAlphaTest = CheckClutAlphaFull(state);
385
if ((state->flags & RasterizerStateFlags::CLUT_ALPHA_NON_ZERO) && pixelID.alphaTestRef == 0)
386
canSkipAlphaTest = true;
387
if (canSkipAlphaTest)
388
optimize |= alphaTestFunc == GE_COMP_NOTEQUAL ? RasterizerStateFlags::OPTIMIZED_ALPHATEST_OFF_NE : RasterizerStateFlags::OPTIMIZED_ALPHATEST_OFF_GT;
389
}
390
}
391
}
392
393
return optimize;
394
}
395
396
static bool ApplyStateOptimizations(RasterizerState *state, const RasterizerStateFlags &optimize) {
397
bool changed = false;
398
399
// Check if we can compile the new funcs before replacing.
400
if (OptimizePixelIDFlags(state->flags) != OptimizePixelIDFlags(optimize)) {
401
bool canFull = !(state->flags & RasterizerStateFlags::VERTEX_ALPHA_NON_FULL);
402
403
PixelFuncID pixelID = state->pixelID;
404
if (optimize & RasterizerStateFlags::OPTIMIZED_BLEND_OFF)
405
pixelID.alphaBlend = false;
406
else if (state->flags & RasterizerStateFlags::OPTIMIZED_BLEND_OFF)
407
pixelID.alphaBlend = true;
408
if (optimize & RasterizerStateFlags::OPTIMIZED_BLEND_SRC)
409
pixelID.alphaBlendSrc = (uint8_t)(canFull ? PixelBlendFactor::ONE : PixelBlendFactor::ZERO);
410
else if (state->flags & RasterizerStateFlags::OPTIMIZED_BLEND_SRC)
411
pixelID.alphaBlendSrc = (uint8_t)PixelBlendFactor::SRCALPHA;
412
if (optimize & RasterizerStateFlags::OPTIMIZED_BLEND_DST)
413
pixelID.alphaBlendDst = (uint8_t)(canFull ? PixelBlendFactor::ZERO : PixelBlendFactor::ONE);
414
else if (state->flags & RasterizerStateFlags::OPTIMIZED_BLEND_DST)
415
pixelID.alphaBlendDst = (uint8_t)PixelBlendFactor::INVSRCALPHA;
416
if (optimize & RasterizerStateFlags::OPTIMIZED_FOG_OFF)
417
pixelID.applyFog = false;
418
else if (state->flags & RasterizerStateFlags::OPTIMIZED_FOG_OFF)
419
pixelID.applyFog = true;
420
if (optimize & (RasterizerStateFlags::OPTIMIZED_ALPHATEST_OFF_NE | RasterizerStateFlags::OPTIMIZED_ALPHATEST_OFF_GT))
421
pixelID.alphaTestFunc = GE_COMP_ALWAYS;
422
else if (state->flags & RasterizerStateFlags::OPTIMIZED_ALPHATEST_OFF_NE)
423
pixelID.alphaTestFunc = GE_COMP_NOTEQUAL;
424
else if (state->flags & RasterizerStateFlags::OPTIMIZED_ALPHATEST_OFF_GT)
425
pixelID.alphaTestFunc = GE_COMP_GREATER;
426
else if (optimize & RasterizerStateFlags::OPTIMIZED_ALPHATEST_ON) {
427
pixelID.alphaTestFunc = GE_COMP_NOTEQUAL;
428
pixelID.alphaTestRef = 0;
429
pixelID.hasAlphaTestMask = false;
430
} else if (state->flags & RasterizerStateFlags::OPTIMIZED_ALPHATEST_ON) {
431
pixelID.alphaTestFunc = GE_COMP_ALWAYS;
432
}
433
434
SingleFunc drawPixel = Rasterizer::GetSingleFunc(pixelID, nullptr);
435
// Can't compile during runtime. This failing is a bit of a problem when undoing...
436
if (drawPixel) {
437
state->drawPixel = drawPixel;
438
memcpy(&state->pixelID, &pixelID, sizeof(PixelFuncID));
439
state->flags = ReplacePixelIDFlags(state->flags, optimize) | RasterizerStateFlags::OPTIMIZED;
440
changed = true;
441
}
442
}
443
444
if (OptimizeSamplerIDFlags(state->flags) != OptimizeSamplerIDFlags(optimize)) {
445
SamplerID samplerID = state->samplerID;
446
if (optimize & RasterizerStateFlags::OPTIMIZED_TEXREPLACE)
447
samplerID.texFunc = (uint8_t)GE_TEXFUNC_REPLACE;
448
else if (state->flags & RasterizerStateFlags::OPTIMIZED_TEXREPLACE)
449
samplerID.texFunc = (uint8_t)GE_TEXFUNC_MODULATE;
450
451
Sampler::LinearFunc linear = Sampler::GetLinearFunc(samplerID, nullptr);
452
Sampler::LinearFunc nearest = Sampler::GetNearestFunc(samplerID, nullptr);
453
// Can't compile during runtime. This failing is a bit of a problem when undoing...
454
if (linear && nearest) {
455
// Since the definitions are the same, just force this setting using the func pointer.
456
if (g_Config.iTexFiltering == TEX_FILTER_FORCE_LINEAR) {
457
state->nearest = linear;
458
state->linear = linear;
459
} else if (g_Config.iTexFiltering == TEX_FILTER_FORCE_NEAREST) {
460
state->nearest = nearest;
461
state->linear = nearest;
462
} else {
463
state->nearest = nearest;
464
state->linear = linear;
465
}
466
memcpy(&state->samplerID, &samplerID, sizeof(SamplerID));
467
state->flags = ReplaceSamplerIDFlags(state->flags, optimize) | RasterizerStateFlags::OPTIMIZED;
468
changed = true;
469
}
470
}
471
472
state->lastFlags = state->flags;
473
return changed;
474
}
475
476
bool OptimizeRasterState(RasterizerState *state) {
477
if (state->flags == state->lastFlags)
478
return false;
479
480
RasterizerStateFlags optimize = DetectStateOptimizations(state);
481
482
// If it was optimized before, just revert and don't churn.
483
if ((state->flags & RasterizerStateFlags::OPTIMIZED) && OptimizeAllFlags(state->flags) != OptimizeAllFlags(optimize)) {
484
optimize = RasterizerStateFlags::NONE;
485
} else if (optimize == RasterizerStateFlags::NONE && !(state->flags & RasterizerStateFlags::OPTIMIZED)) {
486
state->lastFlags = state->flags;
487
return false;
488
}
489
490
return ApplyStateOptimizations(state, optimize);
491
}
492
493
RasterizerState OptimizeFlatRasterizerState(const RasterizerState &origState, const VertexData &v1) {
494
uint8_t alpha = v1.color0 >> 24;
495
RasterizerState state = origState;
496
497
// Sometimes, a particular draw can do better than the overall state.
498
state.flags = ClearFlags(state.flags, RasterizerStateFlags::VERTEX_FLAT_RESET);
499
CalculateRasterStateFlags(&state, v1, true);
500
501
RasterizerStateFlags optimize = DetectStateOptimizations(&state);
502
if (OptimizeAllFlags(state.flags) != OptimizeAllFlags(optimize)) {
503
ApplyStateOptimizations(&state, optimize);
504
return state;
505
}
506
507
return origState;
508
}
509
510
static inline u8 ClampFogDepth(float fogdepth) {
511
union FloatBits {
512
float f;
513
u32 u;
514
};
515
FloatBits f;
516
f.f = fogdepth;
517
518
u32 exp = f.u >> 23;
519
if ((f.u & 0x80000000) != 0 || exp <= 126 - 8)
520
return 0;
521
if (exp > 126)
522
return 255;
523
524
u32 mantissa = (f.u & 0x007FFFFF) | 0x00800000;
525
return mantissa >> (16 + 126 - exp);
526
}
527
528
static inline void GetTextureCoordinates(const VertexData& v0, const VertexData& v1, const float p, float &s, float &t) {
529
// Note that for environment mapping, texture coordinates have been calculated during lighting
530
float q0 = 1.f / v0.clipw;
531
float q1 = 1.f / v1.clipw;
532
float wq0 = p * q0;
533
float wq1 = (1.0f - p) * q1;
534
535
float q_recip = 1.0f / (wq0 + wq1);
536
s = (v0.texturecoords.s() * wq0 + v1.texturecoords.s() * wq1) * q_recip;
537
t = (v0.texturecoords.t() * wq0 + v1.texturecoords.t() * wq1) * q_recip;
538
}
539
540
static inline void GetTextureCoordinatesProj(const VertexData& v0, const VertexData& v1, const float p, float &s, float &t) {
541
// This is for texture matrix projection.
542
float q0 = 1.f / v0.clipw;
543
float q1 = 1.f / v1.clipw;
544
float wq0 = p * q0;
545
float wq1 = (1.0f - p) * q1;
546
547
float q_recip = 1.0f / (v0.texturecoords.q() * wq0 + v1.texturecoords.q() * wq1);
548
549
s = (v0.texturecoords.s() * wq0 + v1.texturecoords.s() * wq1) * q_recip;
550
t = (v0.texturecoords.t() * wq0 + v1.texturecoords.t() * wq1) * q_recip;
551
}
552
553
static inline void GetTextureCoordinates(const VertexData &v0, const VertexData &v1, const VertexData &v2, const Vec4<int> &w0, const Vec4<int> &w1, const Vec4<int> &w2, const Vec4<float> &wsum_recip, Vec4<float> &s, Vec4<float> &t) {
554
// Note that for environment mapping, texture coordinates have been calculated during lighting.
555
float q0 = 1.f / v0.clipw;
556
float q1 = 1.f / v1.clipw;
557
float q2 = 1.f / v2.clipw;
558
Vec4<float> wq0 = w0.Cast<float>() * q0;
559
Vec4<float> wq1 = w1.Cast<float>() * q1;
560
Vec4<float> wq2 = w2.Cast<float>() * q2;
561
562
Vec4<float> q_recip = (wq0 + wq1 + wq2).Reciprocal();
563
s = Interpolate(v0.texturecoords.s(), v1.texturecoords.s(), v2.texturecoords.s(), wq0, wq1, wq2, q_recip);
564
t = Interpolate(v0.texturecoords.t(), v1.texturecoords.t(), v2.texturecoords.t(), wq0, wq1, wq2, q_recip);
565
}
566
567
static inline void GetTextureCoordinatesProj(const VertexData &v0, const VertexData &v1, const VertexData &v2, const Vec4<int> &w0, const Vec4<int> &w1, const Vec4<int> &w2, const Vec4<float> &wsum_recip, Vec4<float> &s, Vec4<float> &t) {
568
// This is for texture matrix projection.
569
float q0 = 1.f / v0.clipw;
570
float q1 = 1.f / v1.clipw;
571
float q2 = 1.f / v2.clipw;
572
Vec4<float> wq0 = w0.Cast<float>() * q0;
573
Vec4<float> wq1 = w1.Cast<float>() * q1;
574
Vec4<float> wq2 = w2.Cast<float>() * q2;
575
576
// Here, Interpolate() is a bit suboptimal, since
577
// there's no need to multiply by 1.0f.
578
Vec4<float> q_recip = Interpolate(v0.texturecoords.q(), v1.texturecoords.q(), v2.texturecoords.q(), wq0, wq1, wq2, Vec4<float>::AssignToAll(1.0f)).Reciprocal();
579
580
s = Interpolate(v0.texturecoords.s(), v1.texturecoords.s(), v2.texturecoords.s(), wq0, wq1, wq2, q_recip);
581
t = Interpolate(v0.texturecoords.t(), v1.texturecoords.t(), v2.texturecoords.t(), wq0, wq1, wq2, q_recip);
582
}
583
584
static inline void SetPixelDepth(int x, int y, int stride, u16 value) {
585
depthbuf.Set16(x, y, stride, value);
586
}
587
588
static inline bool IsRightSideOrFlatBottomLine(const Vec2<int>& vertex, const Vec2<int>& line1, const Vec2<int>& line2)
589
{
590
if (line1.y == line2.y) {
591
// just check if vertex is above us => bottom line parallel to x-axis
592
return vertex.y < line1.y;
593
} else {
594
// check if vertex is on our left => right side
595
return vertex.x < line1.x + (line2.x - line1.x) * (vertex.y - line1.y) / (line2.y - line1.y);
596
}
597
}
598
599
static inline Vec4IntResult SOFTRAST_CALL ApplyTexturing(float s, float t, Vec4IntArg prim_color, int texlevel, int frac_texlevel, bool bilinear, const RasterizerState &state) {
600
const u8 **tptr0 = const_cast<const u8 **>(&state.texptr[texlevel]);
601
const uint16_t *bufw0 = &state.texbufw[texlevel];
602
603
if (!bilinear) {
604
return state.nearest(s, t, prim_color, tptr0, bufw0, texlevel, frac_texlevel, state.samplerID);
605
}
606
return state.linear(s, t, prim_color, tptr0, bufw0, texlevel, frac_texlevel, state.samplerID);
607
}
608
609
static inline Vec4IntResult SOFTRAST_CALL ApplyTexturingSingle(float s, float t, Vec4IntArg prim_color, int texlevel, int frac_texlevel, bool bilinear, const RasterizerState &state) {
610
return ApplyTexturing(s, t, prim_color, texlevel, frac_texlevel, bilinear, state);
611
}
612
613
// Produces a signed 1.27.4 value.
614
static int TexLog2(float delta) {
615
union FloatBits {
616
float f;
617
u32 u;
618
};
619
FloatBits f;
620
f.f = delta;
621
// Use the exponent as the tex level, and the top mantissa bits for a frac.
622
// We can't support more than 4 bits of frac, so truncate.
623
int useful = (f.u >> 19) & 0x0FFF;
624
// Now offset so the exponent aligns with log2f (exp=127 is 0.)
625
return useful - 127 * 16;
626
}
627
628
static inline void CalculateSamplingParams(const float ds, const float dt, float w, const RasterizerState &state, int &level, int &levelFrac, bool &filt) {
629
const int width = 1 << state.samplerID.width0Shift;
630
const int height = 1 << state.samplerID.height0Shift;
631
632
// With 8 bits of fraction (because texslope can be fairly precise.)
633
int detail;
634
switch (state.TexLevelMode()) {
635
case GE_TEXLEVEL_MODE_AUTO:
636
detail = TexLog2(std::max(std::abs(ds * width), std::abs(dt * height)));
637
break;
638
case GE_TEXLEVEL_MODE_SLOPE:
639
// This is always offset by an extra texlevel.
640
detail = TexLog2(2.0f * w * state.textureLodSlope);
641
break;
642
case GE_TEXLEVEL_MODE_CONST:
643
default:
644
// Unused value 3 operates the same as CONST.
645
detail = 0;
646
break;
647
}
648
649
// Add in the bias (used in all modes), with 4 bits of fraction.
650
detail += state.texLevelOffset;
651
652
if (detail > 0 && state.maxTexLevel > 0) {
653
bool mipFilt = state.mipFilt;
654
655
int level8 = std::min(detail, state.maxTexLevel * 16);
656
if (!mipFilt) {
657
// Round up at 1.5.
658
level8 += 8;
659
}
660
level = level8 >> 4;
661
levelFrac = mipFilt ? level8 & 0xF : 0;
662
} else {
663
level = 0;
664
levelFrac = 0;
665
}
666
667
if (detail > 0)
668
filt = state.minFilt;
669
else
670
filt = state.magFilt;
671
}
672
673
static inline void ApplyTexturing(const RasterizerState &state, Vec4<int> *prim_color, const Vec4<int> &mask, const Vec4<float> &s, const Vec4<float> &t, float w) {
674
float ds = s[1] - s[0];
675
float dt = t[2] - t[0];
676
677
int level;
678
int levelFrac;
679
bool bilinear;
680
CalculateSamplingParams(ds, dt, w, state, level, levelFrac, bilinear);
681
682
PROFILE_THIS_SCOPE("sampler");
683
for (int i = 0; i < 4; ++i) {
684
if (mask[i] >= 0)
685
prim_color[i] = ApplyTexturing(s[i], t[i], ToVec4IntArg(prim_color[i]), level, levelFrac, bilinear, state);
686
}
687
}
688
689
static inline Vec4<int> SOFTRAST_CALL CheckDepthTestPassed4(const Vec4<int> &mask, GEComparison func, int x, int y, int stride, Vec4<int> z) {
690
// Skip the depth buffer read if we're masked already.
691
#if defined(_M_SSE)
692
__m128i result = SAFE_M128I(mask.ivec);
693
int maskbits = _mm_movemask_epi8(result);
694
if (maskbits >= 0xFFFF)
695
return mask;
696
#else
697
Vec4<int> result = mask;
698
if (mask.x < 0 && mask.y < 0 && mask.z < 0 && mask.w < 0)
699
return result;
700
#endif
701
702
// Read in the existing depth values.
703
#if defined(_M_SSE)
704
// Tried using flags from maskbits to skip dwords... seemed neutral.
705
__m128i refz = _mm_cvtsi32_si128(*(u32 *)depthbuf.Get16Ptr(x, y, stride));
706
refz = _mm_unpacklo_epi32(refz, _mm_cvtsi32_si128(*(u32 *)depthbuf.Get16Ptr(x, y + 1, stride)));
707
refz = _mm_unpacklo_epi16(refz, _mm_setzero_si128());
708
#else
709
Vec4<int> refz(depthbuf.Get16(x, y, stride), depthbuf.Get16(x + 1, y, stride), depthbuf.Get16(x, y + 1, stride), depthbuf.Get16(x + 1, y + 1, stride));
710
#endif
711
712
switch (func) {
713
case GE_COMP_NEVER:
714
#if defined(_M_SSE)
715
result = _mm_set1_epi32(-1);
716
#else
717
result = Vec4<int>::AssignToAll(-1);
718
#endif
719
break;
720
721
case GE_COMP_ALWAYS:
722
break;
723
724
case GE_COMP_EQUAL:
725
#if defined(_M_SSE)
726
result = _mm_or_si128(result, _mm_xor_si128(_mm_cmpeq_epi32(z.ivec, refz), _mm_set1_epi32(-1)));
727
#else
728
for (int i = 0; i < 4; ++i)
729
result[i] |= z[i] != refz[i] ? -1 : 0;
730
#endif
731
break;
732
733
case GE_COMP_NOTEQUAL:
734
#if defined(_M_SSE)
735
result = _mm_or_si128(result, _mm_cmpeq_epi32(z.ivec, refz));
736
#else
737
for (int i = 0; i < 4; ++i)
738
result[i] |= z[i] == refz[i] ? -1 : 0;
739
#endif
740
break;
741
742
case GE_COMP_LESS:
743
#if defined(_M_SSE)
744
result = _mm_or_si128(result, _mm_cmpgt_epi32(z.ivec, refz));
745
result = _mm_or_si128(result, _mm_cmpeq_epi32(z.ivec, refz));
746
#else
747
for (int i = 0; i < 4; ++i)
748
result[i] |= z[i] >= refz[i] ? -1 : 0;
749
#endif
750
break;
751
752
case GE_COMP_LEQUAL:
753
#if defined(_M_SSE)
754
result = _mm_or_si128(result, _mm_cmpgt_epi32(z.ivec, refz));
755
#else
756
for (int i = 0; i < 4; ++i)
757
result[i] |= z[i] > refz[i] ? -1 : 0;
758
#endif
759
break;
760
761
case GE_COMP_GREATER:
762
#if defined(_M_SSE)
763
result = _mm_or_si128(result, _mm_cmplt_epi32(z.ivec, refz));
764
result = _mm_or_si128(result, _mm_cmpeq_epi32(z.ivec, refz));
765
#else
766
for (int i = 0; i < 4; ++i)
767
result[i] |= z[i] <= refz[i] ? -1 : 0;
768
#endif
769
break;
770
771
case GE_COMP_GEQUAL:
772
#if defined(_M_SSE)
773
result = _mm_or_si128(result, _mm_cmplt_epi32(z.ivec, refz));
774
#else
775
for (int i = 0; i < 4; ++i)
776
result[i] |= z[i] < refz[i] ? -1 : 0;
777
#endif
778
break;
779
}
780
781
return result;
782
}
783
784
template <bool useSSE4>
785
struct TriangleEdge {
786
Vec4<int> Start(const ScreenCoords &v0, const ScreenCoords &v1, const ScreenCoords &origin);
787
inline Vec4<int> StepX(const Vec4<int> &w);
788
inline Vec4<int> StepY(const Vec4<int> &w);
789
790
inline void NarrowMinMaxX(const Vec4<int> &w, int64_t minX, int64_t &rowMinX, int64_t &rowMaxX);
791
inline Vec4<int> StepXTimes(const Vec4<int> &w, int c);
792
793
Vec4<int> stepX;
794
Vec4<int> stepY;
795
};
796
797
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
798
#if defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)
799
[[gnu::target("sse4.1")]]
800
#endif
801
static inline __m128i SOFTRAST_CALL TriangleEdgeStartSSE4(__m128i initX, __m128i initY, int xf, int yf, int c) {
802
initX = _mm_mullo_epi32(initX, _mm_set1_epi32(xf));
803
initY = _mm_mullo_epi32(initY, _mm_set1_epi32(yf));
804
return _mm_add_epi32(_mm_add_epi32(initX, initY), _mm_set1_epi32(c));
805
}
806
#endif
807
808
template <bool useSSE4>
809
Vec4<int> TriangleEdge<useSSE4>::Start(const ScreenCoords &v0, const ScreenCoords &v1, const ScreenCoords &origin) {
810
// Start at pixel centers.
811
static constexpr int centerOff = (SCREEN_SCALE_FACTOR / 2) - 1;
812
static constexpr int centerPlus1 = SCREEN_SCALE_FACTOR + centerOff;
813
Vec4<int> initX = Vec4<int>::AssignToAll(origin.x) + Vec4<int>(centerOff, centerPlus1, centerOff, centerPlus1);
814
Vec4<int> initY = Vec4<int>::AssignToAll(origin.y) + Vec4<int>(centerOff, centerOff, centerPlus1, centerPlus1);
815
816
// orient2d refactored.
817
int xf = v0.y - v1.y;
818
int yf = v1.x - v0.x;
819
int c = v1.y * v0.x - v1.x * v0.y;
820
821
stepX = Vec4<int>::AssignToAll(xf * SCREEN_SCALE_FACTOR * 2);
822
stepY = Vec4<int>::AssignToAll(yf * SCREEN_SCALE_FACTOR * 2);
823
824
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
825
if constexpr (useSSE4)
826
return TriangleEdgeStartSSE4(initX.ivec, initY.ivec, xf, yf, c);
827
#endif
828
return Vec4<int>::AssignToAll(xf) * initX + Vec4<int>::AssignToAll(yf) * initY + Vec4<int>::AssignToAll(c);
829
}
830
831
template <bool useSSE4>
832
inline Vec4<int> TriangleEdge<useSSE4>::StepX(const Vec4<int> &w) {
833
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
834
return _mm_add_epi32(w.ivec, stepX.ivec);
835
#elif PPSSPP_ARCH(ARM64_NEON)
836
return vaddq_s32(w.ivec, stepX.ivec);
837
#else
838
return w + stepX;
839
#endif
840
}
841
842
template <bool useSSE4>
843
inline Vec4<int> TriangleEdge<useSSE4>::StepY(const Vec4<int> &w) {
844
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
845
return _mm_add_epi32(w.ivec, stepY.ivec);
846
#elif PPSSPP_ARCH(ARM64_NEON)
847
return vaddq_s32(w.ivec, stepY.ivec);
848
#else
849
return w + stepY;
850
#endif
851
}
852
853
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
854
#if defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)
855
[[gnu::target("sse4.1")]]
856
#endif
857
static inline int SOFTRAST_CALL MaxWeightSSE4(__m128i w) {
858
__m128i max2 = _mm_max_epi32(w, _mm_shuffle_epi32(w, _MM_SHUFFLE(3, 2, 3, 2)));
859
__m128i max1 = _mm_max_epi32(max2, _mm_shuffle_epi32(max2, _MM_SHUFFLE(1, 1, 1, 1)));
860
return _mm_cvtsi128_si32(max1);
861
}
862
#endif
863
864
template <bool useSSE4>
865
void TriangleEdge<useSSE4>::NarrowMinMaxX(const Vec4<int> &w, int64_t minX, int64_t &rowMinX, int64_t &rowMaxX) {
866
int wmax;
867
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
868
if constexpr (useSSE4) {
869
wmax = MaxWeightSSE4(w.ivec);
870
} else {
871
wmax = std::max(std::max(w.x, w.y), std::max(w.z, w.w));
872
}
873
#elif PPSSPP_ARCH(ARM64_NEON)
874
int32x2_t wmax_temp = vpmax_s32(vget_low_s32(w.ivec), vget_high_s32(w.ivec));
875
wmax = vget_lane_s32(vpmax_s32(wmax_temp, wmax_temp), 0);
876
#else
877
wmax = std::max(std::max(w.x, w.y), std::max(w.z, w.w));
878
#endif
879
if (wmax < 0) {
880
if (stepX.x > 0) {
881
int steps = -wmax / stepX.x;
882
rowMinX = std::max(rowMinX, minX + steps * SCREEN_SCALE_FACTOR * 2);
883
} else if (stepX.x <= 0) {
884
rowMinX = rowMaxX + 1;
885
}
886
}
887
888
if (wmax >= 0 && stepX.x < 0) {
889
int steps = (-wmax / stepX.x) + 1;
890
rowMaxX = std::min(rowMaxX, minX + steps * SCREEN_SCALE_FACTOR * 2);
891
}
892
}
893
894
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
895
#if defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)
896
[[gnu::target("sse4.1")]]
897
#endif
898
static inline __m128i SOFTRAST_CALL StepTimesSSE4(__m128i w, __m128i step, int c) {
899
return _mm_add_epi32(w, _mm_mullo_epi32(_mm_set1_epi32(c), step));
900
}
901
#endif
902
903
template <bool useSSE4>
904
inline Vec4<int> TriangleEdge<useSSE4>::StepXTimes(const Vec4<int> &w, int c) {
905
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
906
if constexpr (useSSE4)
907
return StepTimesSSE4(w.ivec, stepX.ivec, c);
908
#elif PPSSPP_ARCH(ARM64_NEON)
909
return vaddq_s32(w.ivec, vmulq_s32(vdupq_n_s32(c), stepX.ivec));
910
#endif
911
return w + stepX * c;
912
}
913
914
static inline Vec4<int> MakeMask(const Vec4<int> &w0, const Vec4<int> &w1, const Vec4<int> &w2, const Vec4<int> &bias0, const Vec4<int> &bias1, const Vec4<int> &bias2, const Vec4<int> &scissor) {
915
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
916
__m128i biased0 = _mm_add_epi32(w0.ivec, bias0.ivec);
917
__m128i biased1 = _mm_add_epi32(w1.ivec, bias1.ivec);
918
__m128i biased2 = _mm_add_epi32(w2.ivec, bias2.ivec);
919
920
return _mm_or_si128(_mm_or_si128(biased0, _mm_or_si128(biased1, biased2)), scissor.ivec);
921
#elif PPSSPP_ARCH(ARM64_NEON)
922
int32x4_t biased0 = vaddq_s32(w0.ivec, bias0.ivec);
923
int32x4_t biased1 = vaddq_s32(w1.ivec, bias1.ivec);
924
int32x4_t biased2 = vaddq_s32(w2.ivec, bias2.ivec);
925
926
return vorrq_s32(vorrq_s32(biased0, vorrq_s32(biased1, biased2)), scissor.ivec);
927
#else
928
return (w0 + bias0) | (w1 + bias1) | (w2 + bias2) | scissor;
929
#endif
930
}
931
932
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
933
#if defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)
934
[[gnu::target("sse4.1")]]
935
#endif
936
static inline bool SOFTRAST_CALL AnyMaskSSE4(__m128i mask) {
937
__m128i sig = _mm_srai_epi32(mask, 31);
938
return _mm_test_all_ones(sig) == 0;
939
}
940
#endif
941
942
template <bool useSSE4>
943
static inline bool AnyMask(const Vec4<int> &mask) {
944
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
945
if constexpr (useSSE4) {
946
return AnyMaskSSE4(mask.ivec);
947
}
948
949
// Source: https://fgiesen.wordpress.com/2013/02/10/optimizing-the-basic-rasterizer/#comment-6676
950
return _mm_movemask_ps(_mm_castsi128_ps(mask.ivec)) != 15;
951
#elif PPSSPP_ARCH(ARM64_NEON)
952
int64x2_t sig = vreinterpretq_s64_s32(vshrq_n_s32(mask.ivec, 31));
953
return vgetq_lane_s64(sig, 0) != -1 || vgetq_lane_s64(sig, 1) != -1;
954
#else
955
return mask.x >= 0 || mask.y >= 0 || mask.z >= 0 || mask.w >= 0;
956
#endif
957
}
958
959
static inline Vec4<float> EdgeRecip(const Vec4<int> &w0, const Vec4<int> &w1, const Vec4<int> &w2) {
960
#if defined(_M_SSE) && !PPSSPP_ARCH(X86)
961
__m128i wsum = _mm_add_epi32(w0.ivec, _mm_add_epi32(w1.ivec, w2.ivec));
962
// _mm_rcp_ps loses too much precision.
963
return _mm_div_ps(_mm_set1_ps(1.0f), _mm_cvtepi32_ps(wsum));
964
#elif PPSSPP_ARCH(ARM64_NEON)
965
int32x4_t wsum = vaddq_s32(w0.ivec, vaddq_s32(w1.ivec, w2.ivec));
966
return vdivq_f32(vdupq_n_f32(1.0f), vcvtq_f32_s32(wsum));
967
#else
968
return (w0 + w1 + w2).Cast<float>().Reciprocal();
969
#endif
970
}
971
972
template <bool clearMode, bool useSSE4>
973
void DrawTriangleSlice(
974
const VertexData& v0, const VertexData& v1, const VertexData& v2,
975
int x1, int y1, int x2, int y2,
976
const RasterizerState &state)
977
{
978
Vec4<int> bias0 = Vec4<int>::AssignToAll(IsRightSideOrFlatBottomLine(v0.screenpos.xy(), v1.screenpos.xy(), v2.screenpos.xy()) ? -1 : 0);
979
Vec4<int> bias1 = Vec4<int>::AssignToAll(IsRightSideOrFlatBottomLine(v1.screenpos.xy(), v2.screenpos.xy(), v0.screenpos.xy()) ? -1 : 0);
980
Vec4<int> bias2 = Vec4<int>::AssignToAll(IsRightSideOrFlatBottomLine(v2.screenpos.xy(), v0.screenpos.xy(), v1.screenpos.xy()) ? -1 : 0);
981
982
const PixelFuncID &pixelID = state.pixelID;
983
984
TriangleEdge<useSSE4> e0;
985
TriangleEdge<useSSE4> e1;
986
TriangleEdge<useSSE4> e2;
987
988
int64_t minX = x1, maxX = x2, minY = y1, maxY = y2;
989
990
ScreenCoords pprime(minX, minY, 0);
991
Vec4<int> w0_base = e0.Start(v1.screenpos, v2.screenpos, pprime);
992
Vec4<int> w1_base = e1.Start(v2.screenpos, v0.screenpos, pprime);
993
Vec4<int> w2_base = e2.Start(v0.screenpos, v1.screenpos, pprime);
994
995
// The sum of weights should remain constant as we move toward/away from the edges.
996
const Vec4<float> wsum_recip = EdgeRecip(w0_base, w1_base, w2_base);
997
998
// All the z values are the same, no interpolation required.
999
// This is common, and when we interpolate, we lose accuracy.
1000
const bool flatZ = v0.screenpos.z == v1.screenpos.z && v0.screenpos.z == v2.screenpos.z;
1001
const bool flatColorAll = !state.shadeGouraud;
1002
const bool flatColor0 = flatColorAll || (v0.color0 == v1.color0 && v0.color0 == v2.color0);
1003
const bool flatColor1 = flatColorAll || (v0.color1 == v1.color1 && v0.color1 == v2.color1);
1004
const bool noFog = clearMode || !pixelID.applyFog || (v0.fogdepth >= 1.0f && v1.fogdepth >= 1.0f && v2.fogdepth >= 1.0f);
1005
1006
if (pixelID.applyDepthRange && flatZ) {
1007
if (v0.screenpos.z < pixelID.cached.minz || v0.screenpos.z > pixelID.cached.maxz)
1008
return;
1009
}
1010
1011
#if defined(SOFTGPU_MEMORY_TAGGING_DETAILED) || defined(SOFTGPU_MEMORY_TAGGING_BASIC)
1012
uint32_t bpp = pixelID.FBFormat() == GE_FORMAT_8888 ? 4 : 2;
1013
std::string tag = StringFromFormat("DisplayListT_%08x", state.listPC);
1014
std::string ztag = StringFromFormat("DisplayListTZ_%08x", state.listPC);
1015
#endif
1016
1017
const Vec4<int> v0_c0 = Vec4<int>::FromRGBA(v0.color0);
1018
const Vec4<int> v1_c0 = Vec4<int>::FromRGBA(v1.color0);
1019
const Vec4<int> v2_c0 = Vec4<int>::FromRGBA(v2.color0);
1020
const Vec3<int> v0_c1 = Vec3<int>::FromRGB(v0.color1);
1021
const Vec3<int> v1_c1 = Vec3<int>::FromRGB(v1.color1);
1022
const Vec3<int> v2_c1 = Vec3<int>::FromRGB(v2.color1);
1023
1024
const Vec4<float> v0_z4 = Vec4<int>::AssignToAll(v0.screenpos.z).Cast<float>();
1025
const Vec4<float> v1_z4 = Vec4<int>::AssignToAll(v1.screenpos.z).Cast<float>();
1026
const Vec4<float> v2_z4 = Vec4<int>::AssignToAll(v2.screenpos.z).Cast<float>();
1027
const Vec4<int> minz = Vec4<int>::AssignToAll(pixelID.cached.minz);
1028
const Vec4<int> maxz = Vec4<int>::AssignToAll(pixelID.cached.maxz);
1029
1030
for (int64_t curY = minY; curY <= maxY; curY += SCREEN_SCALE_FACTOR * 2,
1031
w0_base = e0.StepY(w0_base),
1032
w1_base = e1.StepY(w1_base),
1033
w2_base = e2.StepY(w2_base)) {
1034
Vec4<int> w0 = w0_base;
1035
Vec4<int> w1 = w1_base;
1036
Vec4<int> w2 = w2_base;
1037
1038
DrawingCoords p = TransformUnit::ScreenToDrawing(minX, curY);
1039
1040
int64_t rowMinX = minX, rowMaxX = maxX;
1041
e0.NarrowMinMaxX(w0, minX, rowMinX, rowMaxX);
1042
e1.NarrowMinMaxX(w1, minX, rowMinX, rowMaxX);
1043
e2.NarrowMinMaxX(w2, minX, rowMinX, rowMaxX);
1044
1045
int skipX = (rowMinX - minX) / (SCREEN_SCALE_FACTOR * 2);
1046
w0 = e0.StepXTimes(w0, skipX);
1047
w1 = e1.StepXTimes(w1, skipX);
1048
w2 = e2.StepXTimes(w2, skipX);
1049
p.x = (p.x + 2 * skipX) & 0x3FF;
1050
1051
// TODO: Maybe we can clip the edges instead?
1052
int scissorYPlus1 = curY + SCREEN_SCALE_FACTOR > maxY ? -1 : 0;
1053
Vec4<int> scissor_mask = Vec4<int>(0, rowMaxX - rowMinX - SCREEN_SCALE_FACTOR, scissorYPlus1, (rowMaxX - rowMinX - SCREEN_SCALE_FACTOR) | scissorYPlus1);
1054
Vec4<int> scissor_step = Vec4<int>(0, -(SCREEN_SCALE_FACTOR * 2), 0, -(SCREEN_SCALE_FACTOR * 2));
1055
1056
for (int64_t curX = rowMinX; curX <= rowMaxX; curX += SCREEN_SCALE_FACTOR * 2,
1057
w0 = e0.StepX(w0),
1058
w1 = e1.StepX(w1),
1059
w2 = e2.StepX(w2),
1060
scissor_mask = scissor_mask + scissor_step,
1061
p.x = (p.x + 2) & 0x3FF) {
1062
1063
// If p is on or inside all edges, render pixel
1064
Vec4<int> mask = MakeMask(w0, w1, w2, bias0, bias1, bias2, scissor_mask);
1065
if (AnyMask<useSSE4>(mask)) {
1066
Vec4<int> z;
1067
if (flatZ) {
1068
z = Vec4<int>::AssignToAll(v2.screenpos.z);
1069
} else {
1070
// Z is interpolated pretty much directly.
1071
Vec4<float> zfloats = w0.Cast<float>() * v0_z4 + w1.Cast<float>() * v1_z4 + w2.Cast<float>() * v2_z4;
1072
z = (zfloats * wsum_recip).Cast<int>();
1073
}
1074
1075
if (pixelID.earlyZChecks) {
1076
if (pixelID.applyDepthRange) {
1077
#if defined(_M_SSE)
1078
mask.ivec = _mm_or_si128(mask.ivec, _mm_or_si128(_mm_cmplt_epi32(z.ivec, minz.ivec), _mm_cmpgt_epi32(z.ivec, maxz.ivec)));
1079
#else
1080
for (int i = 0; i < 4; ++i) {
1081
if (z[i] < minz[i] || z[i] > maxz[i])
1082
mask[i] = -1;
1083
}
1084
#endif
1085
}
1086
mask = CheckDepthTestPassed4(mask, pixelID.DepthTestFunc(), p.x, p.y, pixelID.cached.depthbufStride, z);
1087
if (!AnyMask<useSSE4>(mask))
1088
continue;
1089
}
1090
1091
// Color interpolation is not perspective corrected on the PSP.
1092
Vec4<int> prim_color[4];
1093
if (!flatColor0) {
1094
for (int i = 0; i < 4; ++i) {
1095
if (mask[i] >= 0)
1096
prim_color[i] = Interpolate(v0_c0, v1_c0, v2_c0, w0[i], w1[i], w2[i], wsum_recip[i]);
1097
}
1098
} else {
1099
for (int i = 0; i < 4; ++i) {
1100
prim_color[i] = v2_c0;
1101
}
1102
}
1103
Vec3<int> sec_color[4];
1104
if (!flatColor1) {
1105
for (int i = 0; i < 4; ++i) {
1106
if (mask[i] >= 0)
1107
sec_color[i] = Interpolate(v0_c1, v1_c1, v2_c1, w0[i], w1[i], w2[i], wsum_recip[i]);
1108
}
1109
} else {
1110
for (int i = 0; i < 4; ++i) {
1111
sec_color[i] = v2_c1;
1112
}
1113
}
1114
1115
if (state.enableTextures) {
1116
if constexpr (!clearMode) {
1117
Vec4<float> s, t;
1118
if (state.throughMode) {
1119
s = Interpolate(v0.texturecoords.s(), v1.texturecoords.s(), v2.texturecoords.s(), w0, w1,
1120
w2, wsum_recip);
1121
t = Interpolate(v0.texturecoords.t(), v1.texturecoords.t(), v2.texturecoords.t(), w0, w1,
1122
w2, wsum_recip);
1123
1124
// For levels > 0, mipmapping is always based on level 0. Simpler to scale first.
1125
s *= 1.0f / (float) (1 << state.samplerID.width0Shift);
1126
t *= 1.0f / (float) (1 << state.samplerID.height0Shift);
1127
} else if (state.textureProj) {
1128
// Texture coordinate interpolation must definitely be perspective-correct.
1129
GetTextureCoordinatesProj(v0, v1, v2, w0, w1, w2, wsum_recip, s, t);
1130
} else {
1131
// Texture coordinate interpolation must definitely be perspective-correct.
1132
GetTextureCoordinates(v0, v1, v2, w0, w1, w2, wsum_recip, s, t);
1133
}
1134
1135
if (state.TexLevelMode() == GE_TEXLEVEL_MODE_SLOPE) {
1136
// Not sure what's right, but we need one value for the slope.
1137
float clipw = (v0.clipw * w0.x + v1.clipw * w1.x + v2.clipw * w2.x) * wsum_recip.x;
1138
ApplyTexturing(state, prim_color, mask, s, t, clipw);
1139
} else {
1140
ApplyTexturing(state, prim_color, mask, s, t, 0.0f);
1141
}
1142
}
1143
}
1144
1145
if constexpr (!clearMode) {
1146
for (int i = 0; i < 4; ++i) {
1147
#if defined(_M_SSE)
1148
// TODO: Tried making Vec4 do this, but things got slower.
1149
const __m128i sec = _mm_and_si128(sec_color[i].ivec, _mm_set_epi32(0, -1, -1, -1));
1150
prim_color[i].ivec = _mm_add_epi32(prim_color[i].ivec, sec);
1151
#elif PPSSPP_ARCH(ARM64_NEON)
1152
int32x4_t sec = vsetq_lane_s32(0, sec_color[i].ivec, 3);
1153
prim_color[i].ivec = vaddq_s32(prim_color[i].ivec, sec);
1154
#else
1155
prim_color[i] += Vec4<int>(sec_color[i], 0);
1156
#endif
1157
}
1158
}
1159
1160
Vec4<int> fog = Vec4<int>::AssignToAll(255);
1161
if (!noFog) {
1162
Vec4<float> fogdepths = w0.Cast<float>() * v0.fogdepth + w1.Cast<float>() * v1.fogdepth + w2.Cast<float>() * v2.fogdepth;
1163
fogdepths = fogdepths * wsum_recip;
1164
for (int i = 0; i < 4; ++i) {
1165
fog[i] = ClampFogDepth(fogdepths[i]);
1166
}
1167
}
1168
1169
PROFILE_THIS_SCOPE("draw_tri_px");
1170
DrawingCoords subp = p;
1171
for (int i = 0; i < 4; ++i) {
1172
if (mask[i] < 0) {
1173
continue;
1174
}
1175
subp.x = p.x + (i & 1);
1176
subp.y = p.y + (i / 2);
1177
1178
state.drawPixel(subp.x, subp.y, z[i], fog[i], ToVec4IntArg(prim_color[i]), pixelID);
1179
1180
#if defined(SOFTGPU_MEMORY_TAGGING_DETAILED)
1181
uint32_t row = gstate.getFrameBufAddress() + subp.y * pixelID.cached.framebufStride * bpp;
1182
NotifyMemInfo(MemBlockFlags::WRITE, row + subp.x * bpp, bpp, tag.c_str(), tag.size());
1183
if (pixelID.depthWrite) {
1184
row = gstate.getDepthBufAddress() + subp.y * pixelID.cached.depthbufStride * 2;
1185
NotifyMemInfo(MemBlockFlags::WRITE, row + subp.x * 2, 2, ztag.c_str(), ztag.size());
1186
}
1187
#endif
1188
}
1189
}
1190
}
1191
}
1192
1193
#if !defined(SOFTGPU_MEMORY_TAGGING_DETAILED) && defined(SOFTGPU_MEMORY_TAGGING_BASIC)
1194
for (int y = minY; y <= maxY; y += SCREEN_SCALE_FACTOR) {
1195
DrawingCoords p = TransformUnit::ScreenToDrawing(minX, y);
1196
DrawingCoords pend = TransformUnit::ScreenToDrawing(maxX, y);
1197
uint32_t row = gstate.getFrameBufAddress() + p.y * pixelID.cached.framebufStride * bpp;
1198
NotifyMemInfo(MemBlockFlags::WRITE, row + p.x * bpp, (pend.x - p.x) * bpp, tag.c_str(), tag.size());
1199
1200
if (pixelID.depthWrite) {
1201
row = gstate.getDepthBufAddress() + p.y * pixelID.cached.depthbufStride * 2;
1202
NotifyMemInfo(MemBlockFlags::WRITE, row + p.x * 2, (pend.x - p.x) * 2, ztag.c_str(), ztag.size());
1203
}
1204
}
1205
#endif
1206
}
1207
1208
// Draws triangle, vertices specified in counter-clockwise direction
1209
void DrawTriangle(const VertexData &v0, const VertexData &v1, const VertexData &v2, const BinCoords &range, const RasterizerState &state) {
1210
PROFILE_THIS_SCOPE("draw_tri");
1211
1212
auto drawSlice = cpu_info.bSSE4_1 ?
1213
(state.pixelID.clearMode ? &DrawTriangleSlice<true, true> : &DrawTriangleSlice<false, true>) :
1214
(state.pixelID.clearMode ? &DrawTriangleSlice<true, false> : &DrawTriangleSlice<false, false>);
1215
1216
drawSlice(v0, v1, v2, range.x1, range.y1, range.x2, range.y2, state);
1217
}
1218
1219
void DrawRectangle(const VertexData &v0, const VertexData &v1, const BinCoords &range, const RasterizerState &rastState) {
1220
int entireX1 = std::min(v0.screenpos.x, v1.screenpos.x);
1221
int entireY1 = std::min(v0.screenpos.y, v1.screenpos.y);
1222
int entireX2 = std::max(v0.screenpos.x, v1.screenpos.x) - 1;
1223
int entireY2 = std::max(v0.screenpos.y, v1.screenpos.y) - 1;
1224
int minX = std::max(entireX1 & ~(SCREEN_SCALE_FACTOR - 1), range.x1) | (SCREEN_SCALE_FACTOR / 2 - 1);
1225
int minY = std::max(entireY1 & ~(SCREEN_SCALE_FACTOR - 1), range.y1) | (SCREEN_SCALE_FACTOR / 2 - 1);
1226
int maxX = std::min(entireX2, range.x2);
1227
int maxY = std::min(entireY2, range.y2);
1228
1229
// If TL x or y was after the half, we don't draw the pixel.
1230
// TODO: Verify what center is used, allowing slight offset makes gpu/primitives/trianglefan pass.
1231
if (minX < entireX1 - 1)
1232
minX += SCREEN_SCALE_FACTOR;
1233
if (minY < entireY1 - 1)
1234
minY += SCREEN_SCALE_FACTOR;
1235
1236
RasterizerState state = OptimizeFlatRasterizerState(rastState, v1);
1237
1238
Vec2f rowST(0.0f, 0.0f);
1239
// Note: this is double the x or y movement.
1240
Vec2f stx(0.0f, 0.0f);
1241
Vec2f sty(0.0f, 0.0f);
1242
if (state.enableTextures) {
1243
// Note: texture projection is not handled here, those always turn into triangles.
1244
Vec2f tc0 = v0.texturecoords.uv();
1245
Vec2f tc1 = v1.texturecoords.uv();
1246
if (state.throughMode) {
1247
// For levels > 0, mipmapping is always based on level 0. Simpler to scale first.
1248
tc0.s() *= 1.0f / (float)(1 << state.samplerID.width0Shift);
1249
tc1.s() *= 1.0f / (float)(1 << state.samplerID.width0Shift);
1250
tc0.t() *= 1.0f / (float)(1 << state.samplerID.height0Shift);
1251
tc1.t() *= 1.0f / (float)(1 << state.samplerID.height0Shift);
1252
}
1253
1254
float diffX = (entireX2 - entireX1 + 1) / (float)SCREEN_SCALE_FACTOR;
1255
float diffY = (entireY2 - entireY1 + 1) / (float)SCREEN_SCALE_FACTOR;
1256
float diffS = tc1.s() - tc0.s();
1257
float diffT = tc1.t() - tc0.t();
1258
1259
if (v0.screenpos.x < v1.screenpos.x) {
1260
if (v0.screenpos.y < v1.screenpos.y) {
1261
// Okay, simple, TL -> BR. S and T move toward v1 with X and Y.
1262
rowST = tc0;
1263
stx = Vec2f(2.0f * diffS / diffX, 0.0f);
1264
sty = Vec2f(0.0f, 2.0f * diffT / diffY);
1265
} else {
1266
// BL to TR, rotated. We start at TL still.
1267
// X moves T (not S) toward v1, and Y moves S away from v1.
1268
rowST = Vec2f(tc1.s(), tc0.t());
1269
stx = Vec2f(0.0f, 2.0f * diffT / diffX);
1270
sty = Vec2f(2.0f * -diffS / diffY, 0.0f);
1271
}
1272
} else {
1273
if (v0.screenpos.y < v1.screenpos.y) {
1274
// TR to BL. Like BL to TR, rotated.
1275
// X moves T (not s) away from v1, and Y moves S toward v1.
1276
rowST = Vec2f(tc0.s(), tc1.t());
1277
stx = Vec2f(0.0f, 2.0f * -diffT / diffX);
1278
sty = Vec2f(2.0f * diffS / diffY, 0.0f);
1279
} else {
1280
// BR to TL, just inverse of TL to BR.
1281
rowST = Vec2f(tc1.s(), tc1.t());
1282
stx = Vec2f(2.0f * -diffS / diffX, 0.0f);
1283
sty = Vec2f(0.0f, 2.0f * -diffT / diffY);
1284
}
1285
}
1286
1287
// Okay, now move ST to the minX, minY position.
1288
rowST += (stx / (float)(SCREEN_SCALE_FACTOR * 2)) * (minX - entireX1 + 1);
1289
rowST += (sty / (float)(SCREEN_SCALE_FACTOR * 2)) * (minY - entireY1 + 1);
1290
}
1291
1292
// And now what we add to spread out to 4 values.
1293
const Vec4f sto4(0.0f, 0.5f * stx.s(), 0.5f * sty.s(), 0.5f * stx.s() + 0.5f * sty.s());
1294
const Vec4f tto4(0.0f, 0.5f * stx.t(), 0.5f * sty.t(), 0.5f * stx.t() + 0.5f * sty.t());
1295
1296
ScreenCoords pprime(minX, minY, 0);
1297
const Vec4<int> fog = Vec4<int>::AssignToAll(ClampFogDepth(v1.fogdepth));
1298
const Vec4<int> z = Vec4<int>::AssignToAll(v1.screenpos.z);
1299
const Vec4<int> c0 = Vec4<int>::FromRGBA(v1.color0);
1300
const Vec3<int> sec_color = Vec3<int>::FromRGB(v1.color1);
1301
1302
if (state.pixelID.applyDepthRange) {
1303
// We can bail early since the Z is flat.
1304
if (v1.screenpos.z < state.pixelID.cached.minz || v1.screenpos.z > state.pixelID.cached.maxz)
1305
return;
1306
}
1307
1308
#if defined(SOFTGPU_MEMORY_TAGGING_DETAILED) || defined(SOFTGPU_MEMORY_TAGGING_BASIC)
1309
uint32_t bpp = state.pixelID.FBFormat() == GE_FORMAT_8888 ? 4 : 2;
1310
std::string tag = StringFromFormat("DisplayListR_%08x", state.listPC);
1311
std::string ztag = StringFromFormat("DisplayListRZ_%08x", state.listPC);
1312
#endif
1313
1314
for (int64_t curY = minY; curY < maxY; curY += SCREEN_SCALE_FACTOR * 2, rowST += sty) {
1315
DrawingCoords p = TransformUnit::ScreenToDrawing(minX, curY);
1316
1317
int scissorY2 = curY + SCREEN_SCALE_FACTOR > maxY ? -1 : 0;
1318
Vec4<int> scissor_mask = Vec4<int>(0, maxX - minX - SCREEN_SCALE_FACTOR, scissorY2, (maxX - minX - SCREEN_SCALE_FACTOR) | scissorY2);
1319
Vec4<int> scissor_step = Vec4<int>(0, -(SCREEN_SCALE_FACTOR * 2), 0, -(SCREEN_SCALE_FACTOR * 2));
1320
Vec2f st = rowST;
1321
1322
for (int64_t curX = minX; curX < maxX; curX += SCREEN_SCALE_FACTOR * 2,
1323
st += stx,
1324
scissor_mask += scissor_step,
1325
p.x = (p.x + 2) & 0x3FF) {
1326
Vec4<int> mask = scissor_mask;
1327
1328
Vec4<int> prim_color[4];
1329
for (int i = 0; i < 4; ++i) {
1330
prim_color[i] = c0;
1331
}
1332
1333
if (state.pixelID.earlyZChecks) {
1334
for (int i = 0; i < 4; ++i) {
1335
if (mask[i] < 0)
1336
continue;
1337
1338
int x = p.x + (i & 1);
1339
int y = p.y + (i / 2);
1340
if (!CheckDepthTestPassed(state.pixelID.DepthTestFunc(), x, y, state.pixelID.cached.depthbufStride, z[i])) {
1341
mask[i] = -1;
1342
}
1343
}
1344
}
1345
1346
if (state.enableTextures) {
1347
Vec4<float> s, t;
1348
s = Vec4<float>::AssignToAll(st.s()) + sto4;
1349
t = Vec4<float>::AssignToAll(st.t()) + tto4;
1350
1351
ApplyTexturing(state, prim_color, mask, s, t, v1.clipw);
1352
}
1353
1354
if (!state.pixelID.clearMode) {
1355
for (int i = 0; i < 4; ++i) {
1356
#if defined(_M_SSE)
1357
// TODO: Tried making Vec4 do this, but things got slower.
1358
const __m128i sec = _mm_and_si128(sec_color.ivec, _mm_set_epi32(0, -1, -1, -1));
1359
prim_color[i].ivec = _mm_add_epi32(prim_color[i].ivec, sec);
1360
#elif PPSSPP_ARCH(ARM64_NEON)
1361
int32x4_t sec = vsetq_lane_s32(0, sec_color.ivec, 3);
1362
prim_color[i].ivec = vaddq_s32(prim_color[i].ivec, sec);
1363
#else
1364
prim_color[i] += Vec4<int>(sec_color, 0);
1365
#endif
1366
}
1367
}
1368
1369
PROFILE_THIS_SCOPE("draw_rect_px");
1370
DrawingCoords subp = p;
1371
for (int i = 0; i < 4; ++i) {
1372
if (mask[i] < 0) {
1373
continue;
1374
}
1375
subp.x = p.x + (i & 1);
1376
subp.y = p.y + (i / 2);
1377
1378
state.drawPixel(subp.x, subp.y, z[i], fog[i], ToVec4IntArg(prim_color[i]), state.pixelID);
1379
1380
#if defined(SOFTGPU_MEMORY_TAGGING_DETAILED)
1381
uint32_t row = gstate.getFrameBufAddress() + subp.y * state.pixelID.cached.framebufStride * bpp;
1382
NotifyMemInfo(MemBlockFlags::WRITE, row + subp.x * bpp, bpp, tag.c_str(), tag.size());
1383
if (state.pixelID.depthWrite) {
1384
row = gstate.getDepthBufAddress() + subp.y * state.pixelID.cached.depthbufStride * 2;
1385
NotifyMemInfo(MemBlockFlags::WRITE, row + subp.x * 2, 2, ztag.c_str(), ztag.size());
1386
}
1387
#endif
1388
}
1389
}
1390
}
1391
1392
#if !defined(SOFTGPU_MEMORY_TAGGING_DETAILED) && defined(SOFTGPU_MEMORY_TAGGING_BASIC)
1393
for (int y = minY; y <= maxY; y += SCREEN_SCALE_FACTOR) {
1394
DrawingCoords p = TransformUnit::ScreenToDrawing(minX, y);
1395
DrawingCoords pend = TransformUnit::ScreenToDrawing(maxX, y);
1396
uint32_t row = gstate.getFrameBufAddress() + p.y * state.pixelID.cached.framebufStride * bpp;
1397
NotifyMemInfo(MemBlockFlags::WRITE, row + p.x * bpp, (pend.x - p.x) * bpp, tag.c_str(), tag.size());
1398
1399
if (state.pixelID.depthWrite) {
1400
row = gstate.getDepthBufAddress() + p.y * state.pixelID.cached.depthbufStride * 2;
1401
NotifyMemInfo(MemBlockFlags::WRITE, row + p.x * 2, (pend.x - p.x) * 2, ztag.c_str(), ztag.size());
1402
}
1403
}
1404
#endif
1405
}
1406
1407
void DrawPoint(const VertexData &v0, const BinCoords &range, const RasterizerState &state) {
1408
ScreenCoords pos = v0.screenpos;
1409
Vec4<int> prim_color = Vec4<int>::FromRGBA(v0.color0);
1410
1411
auto &pixelID = state.pixelID;
1412
auto &samplerID = state.samplerID;
1413
1414
DrawingCoords p = TransformUnit::ScreenToDrawing(pos);
1415
u16 z = pos.z;
1416
1417
if (pixelID.earlyZChecks) {
1418
if (pixelID.applyDepthRange) {
1419
if (z < pixelID.cached.minz || z > pixelID.cached.maxz)
1420
return;
1421
}
1422
1423
if (!CheckDepthTestPassed(pixelID.DepthTestFunc(), p.x, p.y, pixelID.cached.depthbufStride, z)) {
1424
return;
1425
}
1426
}
1427
1428
if (state.enableTextures) {
1429
float s = v0.texturecoords.s();
1430
float t = v0.texturecoords.t();
1431
if (state.throughMode) {
1432
s *= 1.0f / (float)(1 << state.samplerID.width0Shift);
1433
t *= 1.0f / (float)(1 << state.samplerID.height0Shift);
1434
} else if (state.textureProj) {
1435
GetTextureCoordinatesProj(v0, v0, 0.0f, s, t);
1436
} else {
1437
// Texture coordinate interpolation must definitely be perspective-correct.
1438
GetTextureCoordinates(v0, v0, 0.0f, s, t);
1439
}
1440
1441
int texLevel;
1442
int texLevelFrac;
1443
bool bilinear;
1444
CalculateSamplingParams(0.0f, 0.0f, v0.clipw, state, texLevel, texLevelFrac, bilinear);
1445
PROFILE_THIS_SCOPE("sampler");
1446
prim_color = ApplyTexturingSingle(s, t, ToVec4IntArg(prim_color), texLevel, texLevelFrac, bilinear, state);
1447
}
1448
1449
if (!pixelID.clearMode) {
1450
Vec3<int> sec_color = Vec3<int>::FromRGB(v0.color1);
1451
prim_color += Vec4<int>(sec_color, 0);
1452
}
1453
1454
u8 fog = 255;
1455
if (pixelID.applyFog) {
1456
fog = ClampFogDepth(v0.fogdepth);
1457
}
1458
1459
PROFILE_THIS_SCOPE("draw_px");
1460
state.drawPixel(p.x, p.y, z, fog, ToVec4IntArg(prim_color), pixelID);
1461
1462
#if defined(SOFTGPU_MEMORY_TAGGING_DETAILED) || defined(SOFTGPU_MEMORY_TAGGING_BASIC)
1463
uint32_t bpp = pixelID.FBFormat() == GE_FORMAT_8888 ? 4 : 2;
1464
std::string tag = StringFromFormat("DisplayListP_%08x", state.listPC);
1465
1466
uint32_t row = gstate.getFrameBufAddress() + p.y * pixelID.cached.framebufStride * bpp;
1467
NotifyMemInfo(MemBlockFlags::WRITE, row + p.x * bpp, bpp, tag.c_str(), tag.size());
1468
1469
if (pixelID.depthWrite) {
1470
std::string ztag = StringFromFormat("DisplayListPZ_%08x", state.listPC);
1471
row = gstate.getDepthBufAddress() + p.y * pixelID.cached.depthbufStride * 2;
1472
NotifyMemInfo(MemBlockFlags::WRITE, row + p.x * 2, 2, ztag.c_str(), ztag.size());
1473
}
1474
#endif
1475
}
1476
1477
void ClearRectangle(const VertexData &v0, const VertexData &v1, const BinCoords &range, const RasterizerState &state) {
1478
int entireX1 = std::min(v0.screenpos.x, v1.screenpos.x);
1479
int entireY1 = std::min(v0.screenpos.y, v1.screenpos.y);
1480
int entireX2 = std::max(v0.screenpos.x, v1.screenpos.x) - 1;
1481
int entireY2 = std::max(v0.screenpos.y, v1.screenpos.y) - 1;
1482
int minX = std::max(entireX1 & ~(SCREEN_SCALE_FACTOR - 1), range.x1) | (SCREEN_SCALE_FACTOR / 2 - 1);
1483
int minY = std::max(entireY1 & ~(SCREEN_SCALE_FACTOR - 1), range.y1) | (SCREEN_SCALE_FACTOR / 2 - 1);
1484
int maxX = std::min(entireX2, range.x2);
1485
int maxY = std::min(entireY2, range.y2);
1486
1487
// If TL x or y was after the half, we don't draw the pixel.
1488
if (minX < entireX1 - 1)
1489
minX += SCREEN_SCALE_FACTOR;
1490
if (minY < entireY1 - 1)
1491
minY += SCREEN_SCALE_FACTOR;
1492
1493
const DrawingCoords pprime = TransformUnit::ScreenToDrawing(minX, minY);
1494
// Only include the end pixel when it's >= 0.5.
1495
const DrawingCoords pend = TransformUnit::ScreenToDrawing(maxX - SCREEN_SCALE_FACTOR / 2, maxY - SCREEN_SCALE_FACTOR / 2);
1496
auto &pixelID = state.pixelID;
1497
auto &samplerID = state.samplerID;
1498
1499
const int w = pend.x - pprime.x + 1;
1500
if (w <= 0)
1501
return;
1502
1503
if (pixelID.DepthClear()) {
1504
const u16 z = v1.screenpos.z;
1505
const int stride = pixelID.cached.depthbufStride;
1506
1507
// If both bytes of Z equal, we can just use memset directly which is faster.
1508
if ((z & 0xFF) == (z >> 8)) {
1509
DrawingCoords p = pprime;
1510
for (p.y = pprime.y; p.y <= pend.y; ++p.y) {
1511
u16 *row = depthbuf.Get16Ptr(p.x, p.y, stride);
1512
memset(row, z, w * 2);
1513
}
1514
} else {
1515
DrawingCoords p = pprime;
1516
for (p.y = pprime.y; p.y <= pend.y; ++p.y) {
1517
for (int x = 0; x < w; ++x) {
1518
SetPixelDepth(p.x + x, p.y, pixelID.cached.depthbufStride, z);
1519
}
1520
}
1521
}
1522
1523
#if defined(SOFTGPU_MEMORY_TAGGING_DETAILED) || defined(SOFTGPU_MEMORY_TAGGING_BASIC)
1524
std::string tag = StringFromFormat("DisplayListXZ_%08x", state.listPC);
1525
for (int y = pprime.y; y <= pend.y; ++y) {
1526
uint32_t row = gstate.getDepthBufAddress() + y * pixelID.cached.depthbufStride * 2;
1527
NotifyMemInfo(MemBlockFlags::WRITE, row + pprime.x * 2, w * 2, tag.c_str(), tag.size());
1528
}
1529
#endif
1530
}
1531
1532
// Note: this stays 0xFFFFFFFF if keeping color and alpha, even for 16-bit.
1533
u32 keepOldMask = 0xFFFFFFFF;
1534
if (pixelID.ColorClear() && pixelID.StencilClear()) {
1535
keepOldMask = 0;
1536
} else {
1537
switch (pixelID.FBFormat()) {
1538
case GE_FORMAT_565:
1539
if (pixelID.ColorClear())
1540
keepOldMask = 0;
1541
break;
1542
1543
case GE_FORMAT_5551:
1544
if (pixelID.ColorClear())
1545
keepOldMask = 0xFFFF8000;
1546
else if (pixelID.StencilClear())
1547
keepOldMask = 0xFFFF7FFF;
1548
break;
1549
1550
case GE_FORMAT_4444:
1551
if (pixelID.ColorClear())
1552
keepOldMask = 0xFFFFF000;
1553
else if (pixelID.StencilClear())
1554
keepOldMask = 0xFFFF0FFF;
1555
break;
1556
1557
case GE_FORMAT_8888:
1558
default:
1559
if (pixelID.ColorClear())
1560
keepOldMask = 0xFF000000;
1561
else if (pixelID.StencilClear())
1562
keepOldMask = 0x00FFFFFF;
1563
break;
1564
}
1565
}
1566
1567
// The pixel write masks are respected in clear mode.
1568
if (pixelID.applyColorWriteMask) {
1569
keepOldMask |= pixelID.cached.colorWriteMask;
1570
}
1571
1572
const u32 new_color = v1.color0;
1573
u16 new_color16;
1574
switch (pixelID.FBFormat()) {
1575
case GE_FORMAT_565:
1576
new_color16 = RGBA8888ToRGB565(new_color);
1577
break;
1578
1579
case GE_FORMAT_5551:
1580
new_color16 = RGBA8888ToRGBA5551(new_color);
1581
break;
1582
1583
case GE_FORMAT_4444:
1584
new_color16 = RGBA8888ToRGBA4444(new_color);
1585
break;
1586
1587
case GE_FORMAT_8888:
1588
break;
1589
1590
case GE_FORMAT_INVALID:
1591
case GE_FORMAT_DEPTH16:
1592
case GE_FORMAT_CLUT8:
1593
_dbg_assert_msg_(false, "Software: invalid framebuf format.");
1594
break;
1595
}
1596
1597
if (keepOldMask == 0) {
1598
const int stride = pixelID.cached.framebufStride;
1599
1600
if (pixelID.FBFormat() == GE_FORMAT_8888) {
1601
const bool canMemsetColor = (new_color & 0xFF) == (new_color >> 8) && (new_color & 0xFFFF) == (new_color >> 16);
1602
if (canMemsetColor) {
1603
DrawingCoords p = pprime;
1604
for (p.y = pprime.y; p.y <= pend.y; ++p.y) {
1605
u32 *row = fb.Get32Ptr(p.x, p.y, stride);
1606
memset(row, new_color, w * 4);
1607
}
1608
} else {
1609
DrawingCoords p = pprime;
1610
for (p.y = pprime.y; p.y <= pend.y; ++p.y) {
1611
for (int x = 0; x < w; ++x) {
1612
fb.Set32(p.x + x, p.y, stride, new_color);
1613
}
1614
}
1615
}
1616
} else {
1617
const bool canMemsetColor = (new_color16 & 0xFF) == (new_color16 >> 8);
1618
if (canMemsetColor) {
1619
DrawingCoords p = pprime;
1620
for (p.y = pprime.y; p.y <= pend.y; ++p.y) {
1621
u16 *row = fb.Get16Ptr(p.x, p.y, stride);
1622
memset(row, new_color16, w * 2);
1623
}
1624
} else {
1625
DrawingCoords p = pprime;
1626
for (p.y = pprime.y; p.y <= pend.y; ++p.y) {
1627
for (int x = 0; x < w; ++x) {
1628
fb.Set16(p.x + x, p.y, stride, new_color16);
1629
}
1630
}
1631
}
1632
}
1633
} else if (keepOldMask != 0xFFFFFFFF) {
1634
const int stride = pixelID.cached.framebufStride;
1635
1636
if (pixelID.FBFormat() == GE_FORMAT_8888) {
1637
DrawingCoords p = pprime;
1638
for (p.y = pprime.y; p.y <= pend.y; ++p.y) {
1639
for (int x = 0; x < w; ++x) {
1640
const u32 old_color = fb.Get32(p.x + x, p.y, stride);
1641
const u32 c = (old_color & keepOldMask) | (new_color & ~keepOldMask);
1642
fb.Set32(p.x + x, p.y, stride, c);
1643
}
1644
}
1645
} else {
1646
DrawingCoords p = pprime;
1647
for (p.y = pprime.y; p.y <= pend.y; ++p.y) {
1648
for (int x = 0; x < w; ++x) {
1649
const u16 old_color = fb.Get16(p.x + x, p.y, stride);
1650
const u16 c = (old_color & keepOldMask) | (new_color16 & ~keepOldMask);
1651
fb.Set16(p.x + x, p.y, stride, c);
1652
}
1653
}
1654
}
1655
}
1656
1657
#if defined(SOFTGPU_MEMORY_TAGGING_DETAILED) || defined(SOFTGPU_MEMORY_TAGGING_BASIC)
1658
if (keepOldMask != 0xFFFFFFFF) {
1659
uint32_t bpp = pixelID.FBFormat() == GE_FORMAT_8888 ? 4 : 2;
1660
std::string tag = StringFromFormat("DisplayListX_%08x", state.listPC);
1661
for (int y = pprime.y; y < pend.y; ++y) {
1662
uint32_t row = gstate.getFrameBufAddress() + y * pixelID.cached.framebufStride * bpp;
1663
NotifyMemInfo(MemBlockFlags::WRITE, row + pprime.x * bpp, w * bpp, tag.c_str(), tag.size());
1664
}
1665
}
1666
#endif
1667
}
1668
1669
void DrawLine(const VertexData &v0, const VertexData &v1, const BinCoords &range, const RasterizerState &state) {
1670
// TODO: Use a proper line drawing algorithm that handles fractional endpoints correctly.
1671
Vec3<int> a(v0.screenpos.x, v0.screenpos.y, v0.screenpos.z);
1672
Vec3<int> b(v1.screenpos.x, v1.screenpos.y, v1.screenpos.z);
1673
1674
int dx = b.x - a.x;
1675
int dy = b.y - a.y;
1676
int dz = b.z - a.z;
1677
1678
int steps;
1679
if (abs(dx) < abs(dy))
1680
steps = abs(dy) / SCREEN_SCALE_FACTOR;
1681
else
1682
steps = abs(dx) / SCREEN_SCALE_FACTOR;
1683
1684
// Avoid going too far since we typically don't start at the pixel center.
1685
if (dx < 0 && dx >= -SCREEN_SCALE_FACTOR)
1686
dx++;
1687
if (dy < 0 && dy >= -SCREEN_SCALE_FACTOR)
1688
dy++;
1689
1690
double xinc = (double)dx / steps;
1691
double yinc = (double)dy / steps;
1692
double zinc = (double)dz / steps;
1693
1694
auto &pixelID = state.pixelID;
1695
auto &samplerID = state.samplerID;
1696
1697
const bool interpolateColor = state.shadeGouraud && !(v0.color0 == v1.color0 && v0.color1 == v1.color1);
1698
const Vec4<int> v0_c0 = Vec4<int>::FromRGBA(v0.color0);
1699
const Vec4<int> v1_c0 = Vec4<int>::FromRGBA(v1.color0);
1700
const Vec3<int> v0_c1 = Vec3<int>::FromRGB(v0.color1);
1701
const Vec3<int> v1_c1 = Vec3<int>::FromRGB(v1.color1);
1702
1703
#if defined(SOFTGPU_MEMORY_TAGGING_DETAILED) || defined(SOFTGPU_MEMORY_TAGGING_BASIC)
1704
std::string tag = StringFromFormat("DisplayListL_%08x", state.listPC);
1705
std::string ztag = StringFromFormat("DisplayListLZ_%08x", state.listPC);
1706
#endif
1707
1708
double x = a.x > b.x ? a.x - 1 : a.x;
1709
double y = a.y > b.y ? a.y - 1 : a.y;
1710
double z = a.z;
1711
const int steps1 = steps == 0 ? 1 : steps;
1712
for (int i = 0; i < steps; i++) {
1713
DrawingCoords p = TransformUnit::ScreenToDrawing(x, y);
1714
1715
bool maskOK = x >= range.x1 && y >= range.y1 && x <= range.x2 && y <= range.y2;
1716
if (maskOK) {
1717
if (pixelID.earlyZChecks) {
1718
if (pixelID.applyDepthRange) {
1719
if (z < pixelID.cached.minz || z > pixelID.cached.maxz)
1720
maskOK = false;
1721
}
1722
1723
if (!CheckDepthTestPassed(pixelID.DepthTestFunc(), p.x, p.y, pixelID.cached.depthbufStride, z)) {
1724
maskOK = false;
1725
}
1726
}
1727
}
1728
1729
if (maskOK) {
1730
// Interpolate between the two points.
1731
Vec4<int> prim_color;
1732
Vec3<int> sec_color;
1733
if (interpolateColor) {
1734
prim_color = (v0_c0 * (steps - i) + v1_c0 * i) / steps1;
1735
sec_color = (v0_c1 * (steps - i) + v1_c1 * i) / steps1;
1736
} else {
1737
prim_color = v1_c0;
1738
sec_color = v1_c1;
1739
}
1740
1741
u8 fog = 255;
1742
if (pixelID.applyFog) {
1743
fog = ClampFogDepth((v0.fogdepth * (float)(steps - i) + v1.fogdepth * (float)i) / steps1);
1744
}
1745
1746
if (state.antialiasLines) {
1747
// TODO: Clearmode?
1748
// TODO: Calculate.
1749
prim_color.a() = 0x7F;
1750
}
1751
1752
if (state.enableTextures) {
1753
float s, s1;
1754
float t, t1;
1755
if (state.throughMode) {
1756
Vec2<float> tc = (v0.texturecoords.uv() * (float)(steps - i) + v1.texturecoords.uv() * (float)i) / steps1;
1757
Vec2<float> tc1 = (v0.texturecoords.uv() * (float)(steps - i - 1) + v1.texturecoords.uv() * (float)(i + 1)) / steps1;
1758
1759
s = tc.s() * (1.0f / (float)(1 << state.samplerID.width0Shift));
1760
s1 = tc1.s() * (1.0f / (float)(1 << state.samplerID.width0Shift));
1761
t = tc.t() * (1.0f / (float)(1 << state.samplerID.height0Shift));
1762
t1 = tc1.t() * (1.0f / (float)(1 << state.samplerID.height0Shift));
1763
} else if (state.textureProj) {
1764
GetTextureCoordinatesProj(v0, v1, (float)(steps - i) / steps1, s, t);
1765
GetTextureCoordinatesProj(v0, v1, (float)(steps - i - 1) / steps1, s1, t1);
1766
} else {
1767
// Texture coordinate interpolation must definitely be perspective-correct.
1768
GetTextureCoordinates(v0, v1, (float)(steps - i) / steps1, s, t);
1769
GetTextureCoordinates(v0, v1, (float)(steps - i - 1) / steps1, s1, t1);
1770
}
1771
1772
// If inc is 0, force the delta to zero.
1773
float ds = xinc == 0.0 ? 0.0f : (s1 - s) * (float)SCREEN_SCALE_FACTOR * (1.0f / xinc);
1774
float dt = yinc == 0.0 ? 0.0f : (t1 - t) * (float)SCREEN_SCALE_FACTOR * (1.0f / yinc);
1775
float w = (v0.clipw * (float)(steps - i) + v1.clipw * (float)i) / steps1;
1776
1777
int texLevel;
1778
int texLevelFrac;
1779
bool texBilinear;
1780
CalculateSamplingParams(ds, dt, w, state, texLevel, texLevelFrac, texBilinear);
1781
1782
if (state.antialiasLines) {
1783
// TODO: This is a naive and wrong implementation.
1784
DrawingCoords p0 = TransformUnit::ScreenToDrawing(x, y);
1785
s = ((float)p0.x + xinc / 32.0f) / 512.0f;
1786
t = ((float)p0.y + yinc / 32.0f) / 512.0f;
1787
1788
texBilinear = true;
1789
}
1790
1791
PROFILE_THIS_SCOPE("sampler");
1792
prim_color = ApplyTexturingSingle(s, t, ToVec4IntArg(prim_color), texLevel, texLevelFrac, texBilinear, state);
1793
}
1794
1795
if (!pixelID.clearMode)
1796
prim_color += Vec4<int>(sec_color, 0);
1797
1798
PROFILE_THIS_SCOPE("draw_px");
1799
state.drawPixel(p.x, p.y, z, fog, ToVec4IntArg(prim_color), pixelID);
1800
1801
#if defined(SOFTGPU_MEMORY_TAGGING_DETAILED) || defined(SOFTGPU_MEMORY_TAGGING_BASIC)
1802
uint32_t bpp = pixelID.FBFormat() == GE_FORMAT_8888 ? 4 : 2;
1803
uint32_t row = gstate.getFrameBufAddress() + p.y * pixelID.cached.framebufStride * bpp;
1804
NotifyMemInfo(MemBlockFlags::WRITE, row + p.x * bpp, bpp, tag.c_str(), tag.size());
1805
1806
if (pixelID.depthWrite) {
1807
uint32_t row = gstate.getDepthBufAddress() + y * pixelID.cached.depthbufStride * 2;
1808
NotifyMemInfo(MemBlockFlags::WRITE, row + p.x * 2, 2, ztag.c_str(), ztag.size());
1809
}
1810
#endif
1811
}
1812
1813
x += xinc;
1814
y += yinc;
1815
z += zinc;
1816
}
1817
}
1818
1819
bool GetCurrentTexture(GPUDebugBuffer &buffer, int level)
1820
{
1821
if (!gstate.isTextureMapEnabled()) {
1822
return false;
1823
}
1824
1825
GETextureFormat texfmt = gstate.getTextureFormat();
1826
u32 texaddr = gstate.getTextureAddress(level);
1827
u32 texbufw = GetTextureBufw(level, texaddr, texfmt);
1828
int w = gstate.getTextureWidth(level);
1829
int h = gstate.getTextureHeight(level);
1830
1831
u32 sizeInBits = textureBitsPerPixel[texfmt] * (texbufw * (h - 1) + w);
1832
if (!texaddr || !Memory::IsValidRange(texaddr, sizeInBits / 8))
1833
return false;
1834
// We'll break trying to allocate this much.
1835
if (w >= 0x8000 && h >= 0x8000)
1836
return false;
1837
1838
buffer.Allocate(w, h, GE_FORMAT_8888, false);
1839
1840
SamplerID id;
1841
ComputeSamplerID(&id);
1842
id.cached.clut = clut;
1843
1844
// Slight annoyance, we may have to force a compile.
1845
Sampler::FetchFunc sampler = Sampler::GetFetchFunc(id, nullptr);
1846
if (!sampler) {
1847
Sampler::FlushJit();
1848
sampler = Sampler::GetFetchFunc(id, nullptr);
1849
if (!sampler)
1850
return false;
1851
}
1852
1853
u8 *texptr = Memory::GetPointerWrite(texaddr);
1854
u32 *row = (u32 *)buffer.GetData();
1855
for (int y = 0; y < h; ++y) {
1856
for (int x = 0; x < w; ++x) {
1857
row[x] = Vec4<int>(sampler(x, y, texptr, texbufw, level, id)).ToRGBA();
1858
}
1859
row += w;
1860
}
1861
return true;
1862
}
1863
1864
} // namespace
1865
1866