Skip to content

Commit

Permalink
Remove SmoothedDepal compat setting, instead detect the ramp directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Aug 21, 2022
1 parent cfa7483 commit 97959f6
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 29 deletions.
1 change: 0 additions & 1 deletion Core/Compatibility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ void Compatibility::CheckSettings(IniFile &iniFile, const std::string &gameID) {
CheckSetting(iniFile, gameID, "AllowLargeFBTextureOffsets", &flags_.AllowLargeFBTextureOffsets);
CheckSetting(iniFile, gameID, "AtracLoopHack", &flags_.AtracLoopHack);
CheckSetting(iniFile, gameID, "DeswizzleDepth", &flags_.DeswizzleDepth);
CheckSetting(iniFile, gameID, "SmoothedDepal", &flags_.SmoothedDepal);
}

void Compatibility::CheckSetting(IniFile &iniFile, const std::string &gameID, const char *option, bool *flag) {
Expand Down
1 change: 0 additions & 1 deletion Core/Compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ struct CompatFlags {
bool AllowLargeFBTextureOffsets;
bool AtracLoopHack;
bool DeswizzleDepth;
bool SmoothedDepal;
};

class IniFile;
Expand Down
41 changes: 33 additions & 8 deletions GPU/Common/DepalettizeCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ void DepalShaderCache::DeviceLost() {
Clear();
}

Draw::Texture *DepalShaderCache::GetClutTexture(GEPaletteFormat clutFormat, const u32 clutHash, u32 *rawClut) {
DepalTexture DepalShaderCache::GetClutTexture(GEPaletteFormat clutFormat, const u32 clutHash, u32 *rawClut) {
u32 clutId = GetClutID(clutFormat, clutHash);

auto oldtex = texCache_.find(clutId);
if (oldtex != texCache_.end()) {
oldtex->second->lastFrame = gpuStats.numFlips;
return oldtex->second->texture;
return *oldtex->second;
}

int texturePixels = clutFormat == GE_CMODE_32BIT_ABGR8888 ? 256 : 512;
int maxClutEntries = clutFormat == GE_CMODE_32BIT_ABGR8888 ? 256 : 512;

DepalTexture *tex = new DepalTexture();

Draw::TextureDesc desc{};
desc.width = texturePixels;
desc.width = maxClutEntries;
desc.height = 1;
desc.depth = 1;
desc.mipLevels = 1;
Expand All @@ -80,24 +80,49 @@ Draw::Texture *DepalShaderCache::GetClutTexture(GEPaletteFormat clutFormat, cons
desc.initData.push_back((const uint8_t *)rawClut);
break;
case GEPaletteFormat::GE_CMODE_16BIT_BGR5650:
ConvertRGB565ToRGBA8888((u32 *)convTemp, (const u16 *)rawClut, texturePixels);
ConvertRGB565ToRGBA8888((u32 *)convTemp, (const u16 *)rawClut, maxClutEntries);
desc.initData.push_back(convTemp);
break;
case GEPaletteFormat::GE_CMODE_16BIT_ABGR5551:
ConvertRGBA5551ToRGBA8888((u32 *)convTemp, (const u16 *)rawClut, texturePixels);
ConvertRGBA5551ToRGBA8888((u32 *)convTemp, (const u16 *)rawClut, maxClutEntries);
desc.initData.push_back(convTemp);
break;
case GEPaletteFormat::GE_CMODE_16BIT_ABGR4444:
ConvertRGBA4444ToRGBA8888((u32 *)convTemp, (const u16 *)rawClut, texturePixels);
ConvertRGBA4444ToRGBA8888((u32 *)convTemp, (const u16 *)rawClut, maxClutEntries);
desc.initData.push_back(convTemp);
break;
}

int lastR = 0;
int lastG = 0;
int lastB = 0;
int lastA = 0;

int rampLength = 0;
// Quick check for how many continouosly growing entries we have at the start.
// Bilinearly filtering CLUTs only really makes sense for this kind of ramp.
for (int i = 0; i < maxClutEntries; i++) {
rampLength = i + 1;
int r = desc.initData[0][i * 4];
int g = desc.initData[0][i * 4 + 1];
int b = desc.initData[0][i * 4 + 2];
int a = desc.initData[0][i * 4 + 3];
if (r < lastR || g < lastG || b < lastB || a < lastA) {
break;
} else {
lastR = r;
lastG = g;
lastB = b;
lastA = a;
}
}

tex->texture = draw_->CreateTexture(desc);
tex->lastFrame = gpuStats.numFlips;
tex->rampLength = rampLength;

texCache_[clutId] = tex;
return tex->texture;
return *tex;
}

void DepalShaderCache::Clear() {
Expand Down
3 changes: 2 additions & 1 deletion GPU/Common/DepalettizeCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class DepalTexture {
public:
Draw::Texture *texture;
int lastFrame;
int rampLength; // How many entries are continuously growing from entry 0.
};

// Caches both shaders and palette textures.
Expand All @@ -50,7 +51,7 @@ class DepalShaderCache {

// This also uploads the palette and binds the correct texture.
DepalShader *GetDepalettizeShader(uint32_t clutMode, GETextureFormat texFormat, GEBufferFormat pixelFormat);
Draw::Texture *GetClutTexture(GEPaletteFormat clutFormat, const u32 clutHash, u32 *rawClut);
DepalTexture GetClutTexture(GEPaletteFormat clutFormat, const u32 clutHash, u32 *rawClut);

Draw::SamplerState *GetSampler();

Expand Down
2 changes: 1 addition & 1 deletion GPU/Common/FragmentShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ bool GenerateFragmentShader(const FShaderID &id, char *buffer, const ShaderLangu

bool doFlatShading = id.Bit(FS_BIT_FLATSHADE) && !flatBug;
bool shaderDepal = id.Bit(FS_BIT_SHADER_DEPAL) && !texture3D; // combination with texture3D not supported. Enforced elsewhere too.
bool smoothedDepal = PSP_CoreParameter().compat.flags().SmoothedDepal;
bool smoothedDepal = id.Bit(FS_BIT_SHADER_SMOOTHED_DEPAL);
bool bgraTexture = id.Bit(FS_BIT_BGRA_TEXTURE);
bool colorWriteMask = id.Bit(FS_BIT_COLOR_WRITEMASK) && compat.bitwiseOps;

Expand Down
2 changes: 2 additions & 0 deletions GPU/Common/ShaderId.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ void ComputeFragmentShaderID(FShaderID *id_out, const Draw::Bugs &bugs) {
bool doTextureAlpha = gstate.isTextureAlphaUsed();
bool doFlatShading = gstate.getShadeMode() == GE_SHADE_FLAT;
bool useShaderDepal = gstate_c.useShaderDepal;
bool useSmoothedDepal = gstate_c.useSmoothedShaderDepal;
bool colorWriteMask = IsColorWriteMaskComplex(gstate_c.allowFramebufferRead);

// Note how we here recompute some of the work already done in state mapping.
Expand Down Expand Up @@ -290,6 +291,7 @@ void ComputeFragmentShaderID(FShaderID *id_out, const Draw::Bugs &bugs) {
}
id.SetBit(FS_BIT_BGRA_TEXTURE, gstate_c.bgraTexture);
id.SetBit(FS_BIT_SHADER_DEPAL, useShaderDepal);
id.SetBit(FS_BIT_SHADER_SMOOTHED_DEPAL, useSmoothedDepal);
id.SetBit(FS_BIT_3D_TEXTURE, gstate_c.curTextureIs3D);
}

Expand Down
1 change: 1 addition & 0 deletions GPU/Common/ShaderId.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ enum FShaderBit : uint8_t {
FS_BIT_NO_DEPTH_CANNOT_DISCARD_STENCIL = 49,
FS_BIT_COLOR_WRITEMASK = 50,
FS_BIT_3D_TEXTURE = 51,
FS_BIT_SHADER_SMOOTHED_DEPAL = 52,
};

static inline FShaderBit operator +(FShaderBit bit, int i) {
Expand Down
14 changes: 7 additions & 7 deletions GPU/Common/TextureCacheCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1879,8 +1879,8 @@ void TextureCacheCommon::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer
const GEPaletteFormat clutFormat = gstate.getClutPaletteFormat();

// Very icky conflation here of native and thin3d rendering. This will need careful work per backend in BindAsClutTexture.
Draw::Texture *clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBufRaw_);
BindAsClutTexture(clutTexture);
DepalTexture clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBufRaw_);
BindAsClutTexture(clutTexture.texture);

framebufferManager_->BindFramebufferAsColorTexture(0, framebuffer, BINDFBCOLOR_MAY_COPY_WITH_UV | BINDFBCOLOR_APPLY_TEX_OFFSET);
// Vulkan needs to do some extra work here to pick out the native handle from Draw.
Expand All @@ -1894,7 +1894,7 @@ void TextureCacheCommon::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer

// Since we started/ended render passes, might need these.
gstate_c.Dirty(DIRTY_DEPAL);
gstate_c.SetUseShaderDepal(true);
gstate_c.SetUseShaderDepal(true, gstate.getClutIndexStartPos() == 0 && gstate.getClutIndexMask() <= clutTexture.rampLength);
gstate_c.depalFramebufferFormat = framebuffer->drawnFormat;
const u32 bytesPerColor = clutFormat == GE_CMODE_32BIT_ABGR8888 ? sizeof(u32) : sizeof(u16);
const u32 clutTotalColors = clutMaxBytes_ / bytesPerColor;
Expand All @@ -1907,12 +1907,12 @@ void TextureCacheCommon::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer
}

depalShader = depalShaderCache_->GetDepalettizeShader(clutMode, texFormat, depth ? GE_FORMAT_DEPTH16 : framebuffer->drawnFormat);
gstate_c.SetUseShaderDepal(false);
gstate_c.SetUseShaderDepal(false, false);
}

if (depalShader) {
const GEPaletteFormat clutFormat = gstate.getClutPaletteFormat();
Draw::Texture *clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBufRaw_);
DepalTexture clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBufRaw_);
Draw::Framebuffer *depalFBO = framebufferManager_->GetTempFBO(TempFBO::DEPAL, framebuffer->renderWidth, framebuffer->renderHeight);
draw_->BindTexture(0, nullptr);
draw_->BindTexture(1, nullptr);
Expand All @@ -1927,7 +1927,7 @@ void TextureCacheCommon::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer
shaderApply.Use();

draw_->BindFramebufferAsTexture(framebuffer->fbo, 0, depth ? Draw::FB_DEPTH_BIT : Draw::FB_COLOR_BIT, 0);
draw_->BindTexture(1, clutTexture);
draw_->BindTexture(1, clutTexture.texture);
Draw::SamplerState *nearest = depalShaderCache_->GetSampler();
draw_->BindSamplerStates(0, 1, &nearest);
draw_->BindSamplerStates(1, 1, &nearest);
Expand All @@ -1952,7 +1952,7 @@ void TextureCacheCommon::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer
framebufferManager_->BindFramebufferAsColorTexture(0, framebuffer, BINDFBCOLOR_MAY_COPY_WITH_UV | BINDFBCOLOR_APPLY_TEX_OFFSET);
BoundFramebufferTexture();

gstate_c.SetUseShaderDepal(false);
gstate_c.SetUseShaderDepal(false, false);
gstate_c.SetTextureFullAlpha(gstate.getTextureFormat() == GE_TFMT_5650);
}

Expand Down
2 changes: 1 addition & 1 deletion GPU/GLES/TextureCacheGLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ void TextureCacheGLES::BindTexture(TexCacheEntry *entry) {
int maxLevel = (entry->status & TexCacheEntry::STATUS_NO_MIPS) ? 0 : entry->maxLevel;
SamplerCacheKey samplerKey = GetSamplingParams(maxLevel, entry);
ApplySamplingParams(samplerKey);
gstate_c.SetUseShaderDepal(false);
gstate_c.SetUseShaderDepal(false, false);
}

void TextureCacheGLES::Unbind() {
Expand Down
4 changes: 3 additions & 1 deletion GPU/GPUState.h
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,10 @@ struct GPUStateCache {
bool IsDirty(u64 what) const {
return (dirty & what) != 0ULL;
}
void SetUseShaderDepal(bool depal) {
void SetUseShaderDepal(bool depal, bool smoothed) {
if (depal != useShaderDepal) {
useShaderDepal = depal;
useSmoothedShaderDepal = smoothed;
Dirty(DIRTY_FRAGMENTSHADER_STATE);
}
}
Expand Down Expand Up @@ -635,6 +636,7 @@ struct GPUStateCache {
int spline_num_points_u;

bool useShaderDepal;
bool useSmoothedShaderDepal;
GEBufferFormat depalFramebufferFormat;

u32 getRelativeAddress(u32 data) const;
Expand Down
2 changes: 1 addition & 1 deletion GPU/Vulkan/TextureCacheVulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ void TextureCacheVulkan::BindTexture(TexCacheEntry *entry) {
curSampler_ = samplerCache_.GetOrCreateSampler(samplerKey);
imageView_ = entry->vkTex->GetImageView();
drawEngine_->SetDepalTexture(VK_NULL_HANDLE);
gstate_c.SetUseShaderDepal(false);
gstate_c.SetUseShaderDepal(false, false);
}

void TextureCacheVulkan::ApplySamplingParams(const SamplerCacheKey &key) {
Expand Down
7 changes: 0 additions & 7 deletions assets/compat.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1276,10 +1276,3 @@ UCKS45048 = true
UCJS18030 = true
UCJS18047 = true
NPJG00015 = true

[SmoothedDepal]
# Test Drive Unlimited smoothed CLUT lookups. See comments in #13355
ULET00386 = true
ULES00637 = true
ULKS46126 = true
ULUS10249 = true

0 comments on commit 97959f6

Please sign in to comment.