CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/GPU/Common/TextureDecoder.h
Views: 1401
// Copyright (c) 2012- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#pragma once1819#include "ppsspp_config.h"2021#include "Common/Common.h"22#include "Common/Swap.h"23#include "Core/MemMap.h"24#include "GPU/ge_constants.h"25#include "GPU/GPUState.h"2627enum CheckAlphaResult {28// These are intended to line up with TexCacheEntry::STATUS_ALPHA_UNKNOWN, etc.29CHECKALPHA_FULL = 0,30CHECKALPHA_ANY = 4,31};3233// For both of these, pitch must be aligned to 16 bits (as is the case on a PSP).34void DoSwizzleTex16(const u32 *ysrcp, u8 *texptr, int bxc, int byc, u32 pitch);35void DoUnswizzleTex16(const u8 *texptr, u32 *ydestp, int bxc, int byc, u32 pitch);3637u32 StableQuickTexHash(const void *checkp, u32 size);3839// outMask is an in/out parameter.40void CopyAndSumMask16(u16 *dst, const u16 *src, int width, u32 *outMask);41void CopyAndSumMask32(u32 *dst, const u32 *src, int width, u32 *outMask);42void CheckMask16(const u16 *src, int width, u32 *outMask);43void CheckMask32(const u32 *src, int width, u32 *outMask);4445// All these DXT structs are in the reverse order, as compared to PC.46// On PC, alpha comes before color, and interpolants are before the tile data.4748struct DXT1Block {49u8 lines[4];50u16_le color1;51u16_le color2;52};5354struct DXT3Block {55DXT1Block color;56u16_le alphaLines[4];57};5859struct DXT5Block {60DXT1Block color;61u32_le alphadata2;62u16_le alphadata1;63u8 alpha1; u8 alpha2;64};6566void DecodeDXT1Block(u32 *dst, const DXT1Block *src, int pitch, int width, int height, u32 *alpha);67void DecodeDXT3Block(u32 *dst, const DXT3Block *src, int pitch, int width, int height);68void DecodeDXT5Block(u32 *dst, const DXT5Block *src, int pitch, int width, int height);6970uint32_t GetDXT1Texel(const DXT1Block *src, int x, int y);71uint32_t GetDXT3Texel(const DXT3Block *src, int x, int y);72uint32_t GetDXT5Texel(const DXT5Block *src, int x, int y);7374extern const u8 textureBitsPerPixel[16];7576u32 GetTextureBufw(int level, u32 texaddr, GETextureFormat format);7778// WARNING: Bits not bytes, this is needed due to the presence of 4 - bit formats.79inline u32 TextureFormatBitsPerPixel(GETextureFormat format) {80u32 bits = textureBitsPerPixel[(int)format];81return bits != 0 ? bits : 1; // Best to return 1 here to survive divisions in case of invalid data.82}8384inline bool AlphaSumIsFull(u32 alphaSum, u32 fullAlphaMask) {85return fullAlphaMask != 0 && (alphaSum & fullAlphaMask) == fullAlphaMask;86}8788inline CheckAlphaResult CheckAlpha16(const u16 *pixelData, int width, u32 fullAlphaMask) {89u32 alphaSum = 0xFFFFFFFF;90CheckMask16(pixelData, width, &alphaSum);91return AlphaSumIsFull(alphaSum, fullAlphaMask) ? CHECKALPHA_FULL : CHECKALPHA_ANY;92}9394inline CheckAlphaResult CheckAlpha32(const u32 *pixelData, int width, u32 fullAlphaMask) {95u32 alphaSum = 0xFFFFFFFF;96CheckMask32(pixelData, width, &alphaSum);97return AlphaSumIsFull(alphaSum, fullAlphaMask) ? CHECKALPHA_FULL : CHECKALPHA_ANY;98}99100inline CheckAlphaResult CheckAlpha32Rect(const u32 *pixelData, int stride, int width, int height, u32 fullAlphaMask) {101u32 alphaSum = 0xFFFFFFFF;102for (int y = 0; y < height; y++) {103CheckMask32(pixelData + stride * y, width, &alphaSum);104}105return AlphaSumIsFull(alphaSum, fullAlphaMask) ? CHECKALPHA_FULL : CHECKALPHA_ANY;106}107108template <typename IndexT, typename ClutT>109inline void DeIndexTexture(/*WRITEONLY*/ ClutT *dest, const IndexT *indexed, int length, const ClutT *clut, u32 *outAlphaSum) {110// Usually, there is no special offset, mask, or shift.111const bool nakedIndex = gstate.isClutIndexSimple();112113ClutT alphaSum = (ClutT)(-1);114115if (nakedIndex) {116if (sizeof(IndexT) == 1) {117for (int i = 0; i < length; ++i) {118ClutT color = clut[*indexed++];119alphaSum &= color;120*dest++ = color;121}122} else {123for (int i = 0; i < length; ++i) {124ClutT color = clut[(*indexed++) & 0xFF];125alphaSum &= color;126*dest++ = color;127}128}129} else {130for (int i = 0; i < length; ++i) {131ClutT color = clut[gstate.transformClutIndex(*indexed++)];132alphaSum &= color;133*dest++ = color;134}135}136137*outAlphaSum &= (u32)alphaSum;138}139140template <typename IndexT, typename ClutT>141inline void DeIndexTexture(/*WRITEONLY*/ ClutT *dest, const u32 texaddr, int length, const ClutT *clut, u32 *outAlphaSum) {142const IndexT *indexed = (const IndexT *) Memory::GetPointer(texaddr);143DeIndexTexture(dest, indexed, length, clut, outAlphaSum);144}145146template <typename ClutT>147inline void DeIndexTexture4(/*WRITEONLY*/ ClutT *dest, const u8 *indexed, int length, const ClutT *clut, u32 *outAlphaSum) {148// Usually, there is no special offset, mask, or shift.149const bool nakedIndex = gstate.isClutIndexSimple();150151ClutT alphaSum = (ClutT)(-1);152if (nakedIndex) {153while (length >= 2) {154u8 index = *indexed++;155ClutT color0 = clut[index & 0xf];156ClutT color1 = clut[index >> 4];157*dest++ = color0;158*dest++ = color1;159alphaSum &= color0 & color1;160length -= 2;161}162if (length) { // Last pixel. Can really only happen in 1xY textures, but making this work generically.163u8 index = *indexed++;164ClutT color0 = clut[index & 0xf];165*dest = color0;166alphaSum &= color0;167}168} else {169while (length >= 2) {170u8 index = *indexed++;171ClutT color0 = clut[gstate.transformClutIndex((index >> 0) & 0xf)];172ClutT color1 = clut[gstate.transformClutIndex((index >> 4) & 0xf)];173*dest++ = color0;174*dest++ = color1;175alphaSum &= color0 & color1;176length -= 2;177}178if (length) {179u8 index = *indexed++;180ClutT color0 = clut[gstate.transformClutIndex((index >> 0) & 0xf)];181*dest = color0;182alphaSum &= color0;183}184}185186*outAlphaSum &= (u32)alphaSum;187}188189template <typename ClutT>190inline void DeIndexTexture4Optimal(ClutT *dest, const u8 *indexed, int length, ClutT color) {191while (length >= 2) {192u8 index = *indexed++;193*dest++ = color | ((index >> 0) & 0xf);194*dest++ = color | ((index >> 4) & 0xf);195length -= 2;196}197if (length) {198u8 index = *indexed++;199*dest++ = color | ((index >> 0) & 0xf);200}201}202203template <>204inline void DeIndexTexture4Optimal<u16>(u16 *dest, const u8 *indexed, int length, u16 color) {205const u16_le *indexed16 = (const u16_le *)indexed;206const u32 color32 = (color << 16) | color;207u32 *dest32 = (u32 *)dest;208for (int i = 0; i < length / 2; i += 2) {209u16 index = *indexed16++;210dest32[i + 0] = color32 | ((index & 0x00f0) << 12) | ((index & 0x000f) >> 0);211dest32[i + 1] = color32 | ((index & 0xf000) << 4) | ((index & 0x0f00) >> 8);212}213}214215inline void DeIndexTexture4OptimalRev(u16 *dest, const u8 *indexed, int length, u16 color) {216const u16_le *indexed16 = (const u16_le *)indexed;217const u32 color32 = (color << 16) | color;218u32 *dest32 = (u32 *)dest;219for (int i = 0; i < length / 2; i += 2) {220u16 index = *indexed16++;221dest32[i + 0] = color32 | ((index & 0x00f0) << 24) | ((index & 0x000f) << 12);222dest32[i + 1] = color32 | ((index & 0xf000) << 16) | ((index & 0x0f00) << 4);223}224}225226template <typename ClutT>227inline void DeIndexTexture4(ClutT *dest, const u32 texaddr, int length, const ClutT *clut) {228const u8 *indexed = (const u8 *) Memory::GetPointer(texaddr);229DeIndexTexture4(dest, indexed, length, clut);230}231232template <typename ClutT>233inline void DeIndexTexture4Optimal(ClutT *dest, const u32 texaddr, int length, ClutT color) {234const u8 *indexed = (const u8 *) Memory::GetPointer(texaddr);235DeIndexTexture4Optimal(dest, indexed, length, color);236}237238239