Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change BGRA to be a texture-specific flag. Fixes R/B swap in DDS textures in D3D11. #17096

Merged
merged 1 commit into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion GPU/Common/ReplacedTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ bool ReplacedTexture::LoadLevelData(ReplacedTextureLevel &level, int mipLevel, D
u32 format;
if (good && (header.ddspf.dwFlags & DDPF_FOURCC)) {
char *fcc = (char *)&header.ddspf.dwFourCC;
INFO_LOG(G3D, "DDS fourcc: %c%c%c%c", fcc[0], fcc[1], fcc[2], fcc[3]);
// INFO_LOG(G3D, "DDS fourcc: %c%c%c%c", fcc[0], fcc[1], fcc[2], fcc[3]);
if (header.ddspf.dwFourCC == MK_FOURCC("DX10")) {
ddsDX10 = true;
good = good && vfs_->Read(openFile, &header10, sizeof(header10)) == sizeof(header10);
Expand Down
11 changes: 7 additions & 4 deletions GPU/Common/TextureCacheCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,9 @@ TexCacheEntry *TextureCacheCommon::SetTexture() {
gstate_c.SetNeedShaderTexclamp(false);
gstate_c.skipDrawReason &= ~SKIPDRAW_BAD_FB_TEXTURE;

bool isBgraTexture = isBgraBackend_ && !hasClutGPU;
gstate_c.SetTextureIsBGRA(isBgraTexture);

if (entryIter != cache_.end()) {
entry = entryIter->second.get();

// Validate the texture still matches the cache entry.
bool match = entry->Matches(dim, texFormat, maxLevel);
const char *reason = "different params";
Expand Down Expand Up @@ -540,7 +538,7 @@ TexCacheEntry *TextureCacheCommon::SetTexture() {
switch (replaced->State()) {
case ReplacementState::NOT_FOUND:
// Didn't find a replacement, so stop looking.
WARN_LOG(G3D, "No replacement for texture %dx%d", w0, h0);
// DEBUG_LOG(G3D, "No replacement for texture %dx%d", w0, h0);
entry->status &= ~TexCacheEntry::STATUS_TO_REPLACE;
if (g_Config.bSaveNewTextures) {
// Load it once more to actually save it. Since we don't set STATUS_TO_REPLACE, we won't end up looping.
Expand All @@ -567,6 +565,8 @@ TexCacheEntry *TextureCacheCommon::SetTexture() {
gstate_c.curTextureHeight = h;
gstate_c.SetTextureIs3D((entry->status & TexCacheEntry::STATUS_3D) != 0);
gstate_c.SetTextureIsArray(false);
gstate_c.SetTextureIsBGRA((entry->status & TexCacheEntry::STATUS_BGRA) != 0);

if (rehash) {
// Update in case any of these changed.
entry->sizeInRAM = (textureBitsPerPixel[texFormat] * bufw * h / 2) / 8;
Expand Down Expand Up @@ -661,6 +661,7 @@ TexCacheEntry *TextureCacheCommon::SetTexture() {
entry->dim = dim;
entry->format = texFormat;
entry->maxLevel = maxLevel;
entry->status &= ~TexCacheEntry::STATUS_BGRA;

// This would overestimate the size in many case so we underestimate instead
// to avoid excessive clearing caused by cache invalidations.
Expand Down Expand Up @@ -2119,12 +2120,14 @@ void TextureCacheCommon::ApplyTexture() {
gstate_c.SetTextureFullAlpha(false);
gstate_c.SetTextureIs3D(false);
gstate_c.SetTextureIsArray(false);
gstate_c.SetTextureIsBGRA(false);
} else {
entry->lastFrame = gpuStats.numFlips;
BindTexture(entry);
gstate_c.SetTextureFullAlpha(entry->GetAlphaStatus() == TexCacheEntry::STATUS_ALPHA_FULL);
gstate_c.SetTextureIs3D((entry->status & TexCacheEntry::STATUS_3D) != 0);
gstate_c.SetTextureIsArray(false);
gstate_c.SetTextureIsBGRA((entry->status & TexCacheEntry::STATUS_BGRA) != 0);
}
}

Expand Down
3 changes: 1 addition & 2 deletions GPU/Common/TextureCacheCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ struct TexCacheEntry {
STATUS_CLUT_GPU = 0x8000,

STATUS_VIDEO = 0x10000,
STATUS_BGRA = 0x20000,
};

// TexStatus enum flag combination.
Expand Down Expand Up @@ -511,8 +512,6 @@ class TextureCacheCommon {
bool nextNeedsChange_;
bool nextNeedsRebuild_;

bool isBgraBackend_ = false;

u32 *expandClut_;
};

Expand Down
7 changes: 6 additions & 1 deletion GPU/D3D11/TextureCacheD3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ TextureCacheD3D11::TextureCacheD3D11(Draw::DrawContext *draw, Draw2D *draw2D)
device_ = (ID3D11Device *)draw->GetNativeObject(Draw::NativeObject::DEVICE);
context_ = (ID3D11DeviceContext *)draw->GetNativeObject(Draw::NativeObject::CONTEXT);

isBgraBackend_ = true;
lastBoundTexture = INVALID_TEX;

D3D11_BUFFER_DESC desc{ sizeof(DepthPushConstants), D3D11_USAGE_DYNAMIC, D3D11_BIND_CONSTANT_BUFFER, D3D11_CPU_ACCESS_WRITE };
Expand Down Expand Up @@ -414,6 +413,12 @@ void TextureCacheD3D11::BuildTexture(TexCacheEntry *const entry) {

if (plan.replaceValid) {
entry->SetAlphaStatus(TexCacheEntry::TexStatus(plan.replaced->AlphaStatus()));

if (!Draw::DataFormatIsBlockCompressed(plan.replaced->Format(), nullptr)) {
entry->status |= TexCacheEntry::STATUS_BGRA;
}
} else {
entry->status |= TexCacheEntry::STATUS_BGRA;
}
}

Expand Down
8 changes: 6 additions & 2 deletions GPU/Directx9/TextureCacheDX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ static const D3DVERTEXELEMENT9 g_FramebufferVertexElements[] = {
TextureCacheDX9::TextureCacheDX9(Draw::DrawContext *draw, Draw2D *draw2D)
: TextureCacheCommon(draw, draw2D) {
lastBoundTexture = INVALID_TEX;
isBgraBackend_ = true;

device_ = (LPDIRECT3DDEVICE9)draw->GetNativeObject(Draw::NativeObject::DEVICE);
deviceEx_ = (LPDIRECT3DDEVICE9EX)draw->GetNativeObject(Draw::NativeObject::DEVICE_EX);
D3DCAPS9 pCaps;
Expand Down Expand Up @@ -322,6 +320,12 @@ void TextureCacheDX9::BuildTexture(TexCacheEntry *const entry) {

if (plan.replaceValid) {
entry->SetAlphaStatus(TexCacheEntry::TexStatus(plan.replaced->AlphaStatus()));

if (!Draw::DataFormatIsBlockCompressed(plan.replaced->Format(), nullptr)) {
entry->status |= TexCacheEntry::STATUS_BGRA;
}
} else {
entry->status |= TexCacheEntry::STATUS_BGRA;
}
}

Expand Down