2013-09-16 04:27:13 +00:00
|
|
|
// Copyright (c) 2012- 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/.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-08-29 15:18:54 +00:00
|
|
|
#include "ppsspp_config.h"
|
2022-04-12 20:56:05 +00:00
|
|
|
|
2013-09-26 06:07:09 +00:00
|
|
|
#include "Common/Common.h"
|
2017-08-31 15:13:18 +00:00
|
|
|
#include "Common/Swap.h"
|
2013-09-16 04:27:13 +00:00
|
|
|
#include "Core/MemMap.h"
|
|
|
|
#include "GPU/ge_constants.h"
|
2015-11-29 01:51:15 +00:00
|
|
|
#include "GPU/GPUState.h"
|
2013-09-16 04:27:13 +00:00
|
|
|
|
2022-04-12 20:56:05 +00:00
|
|
|
enum CheckAlphaResult {
|
|
|
|
// These are intended to line up with TexCacheEntry::STATUS_ALPHA_UNKNOWN, etc.
|
|
|
|
CHECKALPHA_FULL = 0,
|
|
|
|
CHECKALPHA_ANY = 4,
|
|
|
|
};
|
2014-03-23 04:35:16 +00:00
|
|
|
|
2022-04-12 20:56:05 +00:00
|
|
|
// For both of these, pitch must be aligned to 16 bits (as is the case on a PSP).
|
2016-03-27 04:29:48 +00:00
|
|
|
void DoSwizzleTex16(const u32 *ysrcp, u8 *texptr, int bxc, int byc, u32 pitch);
|
2022-04-12 20:56:05 +00:00
|
|
|
void DoUnswizzleTex16(const u8 *texptr, u32 *ydestp, int bxc, int byc, u32 pitch);
|
2015-06-14 09:25:18 +00:00
|
|
|
|
2022-04-13 08:02:16 +00:00
|
|
|
u32 StableQuickTexHash(const void *checkp, u32 size);
|
2014-03-25 07:21:04 +00:00
|
|
|
|
2022-04-15 10:34:50 +00:00
|
|
|
// outMask is an in/out parameter.
|
|
|
|
void CopyAndSumMask16(u16 *dst, const u16 *src, int width, u32 *outMask);
|
|
|
|
void CopyAndSumMask32(u32 *dst, const u32 *src, int width, u32 *outMask);
|
|
|
|
void CheckMask16(const u16 *src, int width, u32 *outMask);
|
|
|
|
void CheckMask32(const u32 *src, int width, u32 *outMask);
|
2015-05-25 05:55:43 +00:00
|
|
|
|
2013-09-26 06:07:09 +00:00
|
|
|
// All these DXT structs are in the reverse order, as compared to PC.
|
|
|
|
// On PC, alpha comes before color, and interpolants are before the tile data.
|
|
|
|
|
|
|
|
struct DXT1Block {
|
|
|
|
u8 lines[4];
|
|
|
|
u16_le color1;
|
|
|
|
u16_le color2;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DXT3Block {
|
|
|
|
DXT1Block color;
|
|
|
|
u16_le alphaLines[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DXT5Block {
|
|
|
|
DXT1Block color;
|
|
|
|
u32_le alphadata2;
|
|
|
|
u16_le alphadata1;
|
|
|
|
u8 alpha1; u8 alpha2;
|
|
|
|
};
|
|
|
|
|
2022-12-09 22:13:39 +00:00
|
|
|
void DecodeDXT1Block(u32 *dst, const DXT1Block *src, int pitch, int width, int height, u32 *alpha);
|
|
|
|
void DecodeDXT3Block(u32 *dst, const DXT3Block *src, int pitch, int width, int height);
|
|
|
|
void DecodeDXT5Block(u32 *dst, const DXT5Block *src, int pitch, int width, int height);
|
2013-09-26 06:07:09 +00:00
|
|
|
|
2021-09-12 16:37:34 +00:00
|
|
|
uint32_t GetDXT1Texel(const DXT1Block *src, int x, int y);
|
|
|
|
uint32_t GetDXT3Texel(const DXT3Block *src, int x, int y);
|
|
|
|
uint32_t GetDXT5Texel(const DXT5Block *src, int x, int y);
|
|
|
|
|
2023-04-17 07:34:26 +00:00
|
|
|
extern const u8 textureBitsPerPixel[16];
|
2013-09-16 04:27:13 +00:00
|
|
|
|
2015-07-26 20:38:40 +00:00
|
|
|
u32 GetTextureBufw(int level, u32 texaddr, GETextureFormat format);
|
2013-09-26 06:16:34 +00:00
|
|
|
|
2023-04-17 07:34:26 +00:00
|
|
|
// WARNING: Bits not bytes, this is needed due to the presence of 4 - bit formats.
|
|
|
|
inline u32 TextureFormatBitsPerPixel(GETextureFormat format) {
|
|
|
|
u32 bits = textureBitsPerPixel[(int)format];
|
|
|
|
return bits != 0 ? bits : 1; // Best to return 1 here to survive divisions in case of invalid data.
|
|
|
|
}
|
|
|
|
|
2022-04-11 18:10:22 +00:00
|
|
|
inline bool AlphaSumIsFull(u32 alphaSum, u32 fullAlphaMask) {
|
|
|
|
return fullAlphaMask != 0 && (alphaSum & fullAlphaMask) == fullAlphaMask;
|
|
|
|
}
|
|
|
|
|
2022-04-15 10:34:50 +00:00
|
|
|
inline CheckAlphaResult CheckAlpha16(const u16 *pixelData, int width, u32 fullAlphaMask) {
|
|
|
|
u32 alphaSum = 0xFFFFFFFF;
|
|
|
|
CheckMask16(pixelData, width, &alphaSum);
|
|
|
|
return AlphaSumIsFull(alphaSum, fullAlphaMask) ? CHECKALPHA_FULL : CHECKALPHA_ANY;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline CheckAlphaResult CheckAlpha32(const u32 *pixelData, int width, u32 fullAlphaMask) {
|
|
|
|
u32 alphaSum = 0xFFFFFFFF;
|
|
|
|
CheckMask32(pixelData, width, &alphaSum);
|
|
|
|
return AlphaSumIsFull(alphaSum, fullAlphaMask) ? CHECKALPHA_FULL : CHECKALPHA_ANY;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline CheckAlphaResult CheckAlpha32Rect(const u32 *pixelData, int stride, int width, int height, u32 fullAlphaMask) {
|
|
|
|
u32 alphaSum = 0xFFFFFFFF;
|
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
|
CheckMask32(pixelData + stride * y, width, &alphaSum);
|
|
|
|
}
|
|
|
|
return AlphaSumIsFull(alphaSum, fullAlphaMask) ? CHECKALPHA_FULL : CHECKALPHA_ANY;
|
|
|
|
}
|
|
|
|
|
2013-09-26 06:16:34 +00:00
|
|
|
template <typename IndexT, typename ClutT>
|
2022-04-11 18:10:22 +00:00
|
|
|
inline void DeIndexTexture(/*WRITEONLY*/ ClutT *dest, const IndexT *indexed, int length, const ClutT *clut, u32 *outAlphaSum) {
|
2013-09-26 06:16:34 +00:00
|
|
|
// Usually, there is no special offset, mask, or shift.
|
|
|
|
const bool nakedIndex = gstate.isClutIndexSimple();
|
|
|
|
|
2022-04-11 18:10:22 +00:00
|
|
|
ClutT alphaSum = (ClutT)(-1);
|
|
|
|
|
2013-09-26 06:16:34 +00:00
|
|
|
if (nakedIndex) {
|
|
|
|
if (sizeof(IndexT) == 1) {
|
|
|
|
for (int i = 0; i < length; ++i) {
|
2022-04-11 18:10:22 +00:00
|
|
|
ClutT color = clut[*indexed++];
|
2022-04-12 07:23:36 +00:00
|
|
|
alphaSum &= color;
|
2022-04-11 18:10:22 +00:00
|
|
|
*dest++ = color;
|
2013-09-26 06:16:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < length; ++i) {
|
2022-04-23 22:42:51 +00:00
|
|
|
ClutT color = clut[(*indexed++) & 0xFF];
|
2022-04-12 07:23:36 +00:00
|
|
|
alphaSum &= color;
|
|
|
|
*dest++ = color;
|
2013-09-26 06:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < length; ++i) {
|
2022-04-12 07:23:36 +00:00
|
|
|
ClutT color = clut[gstate.transformClutIndex(*indexed++)];
|
|
|
|
alphaSum &= color;
|
|
|
|
*dest++ = color;
|
2013-09-26 06:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-13 22:33:30 +00:00
|
|
|
|
2022-04-13 23:03:42 +00:00
|
|
|
*outAlphaSum &= (u32)alphaSum;
|
2013-09-26 06:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IndexT, typename ClutT>
|
2022-04-11 18:10:22 +00:00
|
|
|
inline void DeIndexTexture(/*WRITEONLY*/ ClutT *dest, const u32 texaddr, int length, const ClutT *clut, u32 *outAlphaSum) {
|
2013-09-26 06:16:34 +00:00
|
|
|
const IndexT *indexed = (const IndexT *) Memory::GetPointer(texaddr);
|
2022-04-11 18:10:22 +00:00
|
|
|
DeIndexTexture(dest, indexed, length, clut, outAlphaSum);
|
2013-09-26 06:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ClutT>
|
2022-04-11 18:10:22 +00:00
|
|
|
inline void DeIndexTexture4(/*WRITEONLY*/ ClutT *dest, const u8 *indexed, int length, const ClutT *clut, u32 *outAlphaSum) {
|
2013-09-26 06:16:34 +00:00
|
|
|
// Usually, there is no special offset, mask, or shift.
|
|
|
|
const bool nakedIndex = gstate.isClutIndexSimple();
|
|
|
|
|
2022-04-11 18:10:22 +00:00
|
|
|
ClutT alphaSum = (ClutT)(-1);
|
2013-09-26 06:16:34 +00:00
|
|
|
if (nakedIndex) {
|
2022-12-09 22:00:52 +00:00
|
|
|
while (length >= 2) {
|
2013-09-26 06:16:34 +00:00
|
|
|
u8 index = *indexed++;
|
2022-04-11 18:10:22 +00:00
|
|
|
ClutT color0 = clut[index & 0xf];
|
|
|
|
ClutT color1 = clut[index >> 4];
|
2022-12-09 22:00:52 +00:00
|
|
|
*dest++ = color0;
|
|
|
|
*dest++ = color1;
|
2022-04-11 18:10:22 +00:00
|
|
|
alphaSum &= color0 & color1;
|
2022-12-09 22:00:52 +00:00
|
|
|
length -= 2;
|
|
|
|
}
|
|
|
|
if (length) { // Last pixel. Can really only happen in 1xY textures, but making this work generically.
|
|
|
|
u8 index = *indexed++;
|
|
|
|
ClutT color0 = clut[index & 0xf];
|
|
|
|
*dest = color0;
|
|
|
|
alphaSum &= color0;
|
2013-09-26 06:16:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-12-09 22:00:52 +00:00
|
|
|
while (length >= 2) {
|
2013-09-26 06:16:34 +00:00
|
|
|
u8 index = *indexed++;
|
2022-04-11 18:10:22 +00:00
|
|
|
ClutT color0 = clut[gstate.transformClutIndex((index >> 0) & 0xf)];
|
|
|
|
ClutT color1 = clut[gstate.transformClutIndex((index >> 4) & 0xf)];
|
2022-12-09 22:00:52 +00:00
|
|
|
*dest++ = color0;
|
|
|
|
*dest++ = color1;
|
2022-04-11 18:10:22 +00:00
|
|
|
alphaSum &= color0 & color1;
|
2022-12-09 22:00:52 +00:00
|
|
|
length -= 2;
|
|
|
|
}
|
|
|
|
if (length) {
|
|
|
|
u8 index = *indexed++;
|
|
|
|
ClutT color0 = clut[gstate.transformClutIndex((index >> 0) & 0xf)];
|
|
|
|
*dest = color0;
|
|
|
|
alphaSum &= color0;
|
2013-09-26 06:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-11 18:10:22 +00:00
|
|
|
|
|
|
|
*outAlphaSum &= (u32)alphaSum;
|
2013-09-26 06:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ClutT>
|
|
|
|
inline void DeIndexTexture4Optimal(ClutT *dest, const u8 *indexed, int length, ClutT color) {
|
2022-12-09 22:00:52 +00:00
|
|
|
while (length >= 2) {
|
|
|
|
u8 index = *indexed++;
|
|
|
|
*dest++ = color | ((index >> 0) & 0xf);
|
|
|
|
*dest++ = color | ((index >> 4) & 0xf);
|
|
|
|
length -= 2;
|
|
|
|
}
|
|
|
|
if (length) {
|
2013-09-26 06:16:34 +00:00
|
|
|
u8 index = *indexed++;
|
2022-12-09 22:00:52 +00:00
|
|
|
*dest++ = color | ((index >> 0) & 0xf);
|
2013-09-26 06:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline void DeIndexTexture4Optimal<u16>(u16 *dest, const u8 *indexed, int length, u16 color) {
|
|
|
|
const u16_le *indexed16 = (const u16_le *)indexed;
|
|
|
|
const u32 color32 = (color << 16) | color;
|
|
|
|
u32 *dest32 = (u32 *)dest;
|
|
|
|
for (int i = 0; i < length / 2; i += 2) {
|
|
|
|
u16 index = *indexed16++;
|
|
|
|
dest32[i + 0] = color32 | ((index & 0x00f0) << 12) | ((index & 0x000f) >> 0);
|
|
|
|
dest32[i + 1] = color32 | ((index & 0xf000) << 4) | ((index & 0x0f00) >> 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 15:36:47 +00:00
|
|
|
inline void DeIndexTexture4OptimalRev(u16 *dest, const u8 *indexed, int length, u16 color) {
|
|
|
|
const u16_le *indexed16 = (const u16_le *)indexed;
|
|
|
|
const u32 color32 = (color << 16) | color;
|
|
|
|
u32 *dest32 = (u32 *)dest;
|
|
|
|
for (int i = 0; i < length / 2; i += 2) {
|
|
|
|
u16 index = *indexed16++;
|
|
|
|
dest32[i + 0] = color32 | ((index & 0x00f0) << 24) | ((index & 0x000f) << 12);
|
|
|
|
dest32[i + 1] = color32 | ((index & 0xf000) << 16) | ((index & 0x0f00) << 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 06:16:34 +00:00
|
|
|
template <typename ClutT>
|
|
|
|
inline void DeIndexTexture4(ClutT *dest, const u32 texaddr, int length, const ClutT *clut) {
|
|
|
|
const u8 *indexed = (const u8 *) Memory::GetPointer(texaddr);
|
|
|
|
DeIndexTexture4(dest, indexed, length, clut);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ClutT>
|
|
|
|
inline void DeIndexTexture4Optimal(ClutT *dest, const u32 texaddr, int length, ClutT color) {
|
|
|
|
const u8 *indexed = (const u8 *) Memory::GetPointer(texaddr);
|
|
|
|
DeIndexTexture4Optimal(dest, indexed, length, color);
|
|
|
|
}
|