Files
ppsspp/unittest/TestSoftwareGPUJit.cpp
T
Henrik RydgårdandClaude Opus 5.5 a11fd22553 SoftGPU: Store depth in VRAM as the GE does
The software renderer kept depth linear in VRAM, where the GE stores it
in the layout GPU/Common/DepthSwizzle.h describes, from the EDRAM address
translation and the color format. So a texture or anything else sharing
VRAM with a depth buffer saw depth land in the wrong places (the player's
shadow in Silent Hill: Origins, #22415). Now every depth access goes
through the layout: the generic pixel functions, the 2x2 depth test, the
depth clear in 16-pixel runs, and the x86 pixel JIT, whose IDs carry the
translation so the layout's constants are compiled in. It changes with
the color format and sceGeEdramSetAddrTranslation, after a flush.

Reading VRAM through its 0x04200000 and 0x04600000 mirrors undoes the
layout, as the GE does: texturing from them (drawn from a linear copy
made after a flush), and block transfers from or to them (Hayate no
Gotoku restores its depth buffer through 0x04600000, #17878). Saved and
debugger depth buffers read through the layout, so they're linear.

Socom's lens flare dump now matches a PSP replay where it changes. About
2-3% more cycles in God of War, Tekken 6, Wipeout and Burnout.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-08 18:35:48 +02:00

193 lines
4.9 KiB
C++

// Copyright (c) 2022- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "Common/Data/Random/Rng.h"
#include "Common/StringUtils.h"
#include "Core/Config.h"
#include "GPU/Software/BinManager.h"
#include "GPU/Software/DrawPixel.h"
#include "GPU/Software/Sampler.h"
#include "GPU/Software/SoftGpu.h"
static bool TestSamplerJit() {
#if PPSSPP_ARCH(AMD64)
using namespace Sampler;
SamplerJitCache *cache = new SamplerJitCache();
BinManager binner;
auto GetLinear = [&](SamplerID &id) {
id.linear = true;
id.fetch = false;
return cache->GetLinear(id, &binner);
};
auto GetNearest = [&](SamplerID &id) {
id.linear = false;
id.fetch = false;
return cache->GetNearest(id, &binner);
};
auto GetFetch = [&](SamplerID &id) {
id.linear = false;
id.fetch = true;
return cache->GetFetch(id, &binner);
};
GMRng rng;
int successes = 0;
int count = 3000;
bool header = false;
u8 **tptr = new u8 *[8];
uint16_t *bufw = new uint16_t[8];
u8 *clut = new u8[1024];
memset(clut, 0, 1024);
for (int i = 0; i < 8; ++i) {
tptr[i] = new u8[1024 * 1024 * 4];
memset(tptr[i], 0, 1024 * 1024 * 4);
bufw[i] = 1;
}
for (int i = 0; i < count; ) {
SamplerID id;
memset(&id, 0, sizeof(id));
id.fullKey = rng.R32();
// Textures with an invalid level address are left to the generic samplers.
id.hasInvalidPtr = false;
id.cached.clut = clut;
for (int i = 0; i < 8; ++i) {
id.cached.sizes[i].w = 1;
id.cached.sizes[i].h = 1;
}
std::string desc = DescribeSamplerID(id);
if (startsWith(desc, "INVALID"))
continue;
i++;
LinearFunc linearFunc = GetLinear(id);
NearestFunc nearestFunc = GetNearest(id);
FetchFunc fetchFunc = GetFetch(id);
if (linearFunc != nullptr && nearestFunc != nullptr && fetchFunc != nullptr) {
successes++;
} else {
if (!header)
printf("Failed sampler funcs:\n");
header = true;
printf(" * %s (L:%d, N:%d, F:%d)\n", desc.c_str(), linearFunc != nullptr, nearestFunc != nullptr, fetchFunc != nullptr);
continue;
}
// Try running each to make sure they don't trivially crash.
const auto primArg = Rasterizer::ToVec4IntArg(Math3D::Vec4<int>(127, 127, 127, 127));
linearFunc(0.0f, 0.0f, primArg, tptr, bufw, 1, 7, id);
nearestFunc(0.0f, 0.0f, primArg, tptr, bufw, 1, 7, id);
fetchFunc(0, 0, tptr[0], bufw[0], 1, id);
}
if (successes < count)
printf("SamplerFunc success: %d / %d\n", successes, count);
for (int i = 0; i < 8; ++i) {
delete [] tptr[i];
}
delete [] tptr;
delete [] bufw;
delete [] clut;
delete cache;
return successes == count && !HitAnyAsserts();
#else
// Don't test sampler jit, not supported.
return true;
#endif
}
static bool TestPixelJit() {
#if PPSSPP_ARCH(AMD64)
using namespace Rasterizer;
PixelJitCache *cache = new PixelJitCache();
BinManager binner;
GMRng rng;
int successes = 0;
int count = 3000;
bool header = false;
u32 *fb_data = new u32[512 * 2];
// Random IDs pick random depth layouts, which reach up to 0x8040 bytes past the pixel's offset.
const size_t zbSize = 0x10000;
u16 *zb_data = new u16[zbSize];
fb.as32 = fb_data;
depthbuf.vram = (u8 *)zb_data;
depthbuf.base = 0;
memset(fb_data, 0, sizeof(u32) * 512 * 2);
memset(zb_data, 0, sizeof(u16) * zbSize);
for (int i = 0; i < count; ) {
PixelFuncID id;
memset(&id, 0, sizeof(id));
id.fullKey = (uint64_t)rng.R32() | ((uint64_t)rng.R32() << 32);
std::string desc = DescribePixelFuncID(id);
if (startsWith(desc, "INVALID"))
continue;
i++;
SingleFunc func = cache->GetSingle(id, &binner);
SingleFunc genericFunc = cache->GenericSingle(id);
if (func != genericFunc) {
successes++;
} else {
if (!header)
printf("Failed pixel funcs:\n");
header = true;
printf(" * %s\n", desc.c_str());
}
// Try running it to make sure it doesn't trivially crash.
func(0, 0, 1000, 255, ToVec4IntArg(Math3D::Vec4<int>(127, 127, 127, 127)), id);
}
if (successes < count)
printf("PixelFunc success: %d / %d\n", successes, count);
delete [] fb_data;
delete [] zb_data;
delete cache;
return successes == count && !HitAnyAsserts();
#else
// Not yet supported
return true;
#endif
}
bool TestSoftwareGPUJit() {
g_Config.bSoftwareRenderingJit = true;
ResetHitAnyAsserts();
if (!TestSamplerJit()) {
return false;
}
if (!TestPixelJit()) {
return false;
}
return true;
}