D3D11: Proper fix for DXT5 crash. May also help #9134.

This commit is contained in:
Henrik Rydgard 2017-02-18 02:41:17 +01:00
parent badd3669ca
commit b0cdcfca3c
5 changed files with 22 additions and 26 deletions

View File

@ -829,14 +829,13 @@ bool TextureCacheCommon::DecodeTextureLevel(u8 *out, int outPitch, GETextureForm
for (int y = 0; y < h; y += 4) {
u32 blockIndex = (y / 4) * (bufw / 4);
int blockHeight = std::min(h - y, 4);
for (int x = 0; x < minw; x += 4) {
DecodeDXT1Block(dst + outPitch32 * y + x, src + blockIndex, outPitch32);
DecodeDXT1Block(dst + outPitch32 * y + x, src + blockIndex, outPitch32, blockHeight, false);
blockIndex++;
}
}
// TODO: Not height also?
w = (w + 3) & ~3;
if (reverseColors) {
ReverseColors(out, out, GE_TFMT_8888, outPitch32 * h, useBGRA);
}
@ -852,14 +851,13 @@ bool TextureCacheCommon::DecodeTextureLevel(u8 *out, int outPitch, GETextureForm
for (int y = 0; y < h; y += 4) {
u32 blockIndex = (y / 4) * (bufw / 4);
int blockHeight = std::min(h - y, 4);
for (int x = 0; x < minw; x += 4) {
DecodeDXT3Block(dst + outPitch32 * y + x, src + blockIndex, outPitch32);
DecodeDXT3Block(dst + outPitch32 * y + x, src + blockIndex, outPitch32, blockHeight);
blockIndex++;
}
}
// TODO: Not height also?
w = (w + 3) & ~3;
if (reverseColors) {
ReverseColors(out, out, GE_TFMT_8888, outPitch32 * h, useBGRA);
}
@ -875,14 +873,13 @@ bool TextureCacheCommon::DecodeTextureLevel(u8 *out, int outPitch, GETextureForm
for (int y = 0; y < h; y += 4) {
u32 blockIndex = (y / 4) * (bufw / 4);
int blockHeight = std::min(h - y, 4);
for (int x = 0; x < minw; x += 4) {
DecodeDXT5Block(dst + outPitch32 * y + x, src + blockIndex, outPitch32);
DecodeDXT5Block(dst + outPitch32 * y + x, src + blockIndex, outPitch32, blockHeight);
blockIndex++;
}
}
// TODO: Not height also?
w = (w + 3) & ~3;
if (reverseColors) {
ReverseColors(out, out, GE_TFMT_8888, outPitch32 * h, useBGRA);
}

View File

@ -328,7 +328,7 @@ static inline u32 makecol(int r, int g, int b, int a) {
}
// This could probably be done faster by decoding two or four blocks at a time with SSE/NEON.
void DecodeDXT1Block(u32 *dst, const DXT1Block *src, int pitch, bool ignore1bitAlpha) {
void DecodeDXT1Block(u32 *dst, const DXT1Block *src, int pitch, int height, bool ignore1bitAlpha) {
// S3TC Decoder
// Needs more speed and debugging.
u16 c1 = (src->color1);
@ -356,7 +356,7 @@ void DecodeDXT1Block(u32 *dst, const DXT1Block *src, int pitch, bool ignore1bitA
colors[3] = makecol(red2, green2, blue2, 0); // Color2 but transparent
}
for (int y = 0; y < 4; y++) {
for (int y = 0; y < height; y++) {
int val = src->lines[y];
for (int x = 0; x < 4; x++) {
dst[x] = colors[val & 3];
@ -366,11 +366,11 @@ void DecodeDXT1Block(u32 *dst, const DXT1Block *src, int pitch, bool ignore1bitA
}
}
void DecodeDXT3Block(u32 *dst, const DXT3Block *src, int pitch)
void DecodeDXT3Block(u32 *dst, const DXT3Block *src, int pitch, int height)
{
DecodeDXT1Block(dst, &src->color, pitch, true);
DecodeDXT1Block(dst, &src->color, pitch, height, true);
for (int y = 0; y < 4; y++) {
for (int y = 0; y < height; y++) {
u32 line = src->alphaLines[y];
for (int x = 0; x < 4; x++) {
const u8 a4 = line & 0xF;
@ -392,8 +392,8 @@ static inline u8 lerp6(const DXT5Block *src, int n) {
}
// The alpha channel is not 100% correct
void DecodeDXT5Block(u32 *dst, const DXT5Block *src, int pitch) {
DecodeDXT1Block(dst, &src->color, pitch, true);
void DecodeDXT5Block(u32 *dst, const DXT5Block *src, int pitch, int height) {
DecodeDXT1Block(dst, &src->color, pitch, height, true);
u8 alpha[8];
alpha[0] = src->alpha1;
@ -416,7 +416,7 @@ void DecodeDXT5Block(u32 *dst, const DXT5Block *src, int pitch) {
u64 data = ((u64)(u16)src->alphadata1 << 32) | (u32)src->alphadata2;
for (int y = 0; y < 4; y++) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < 4; x++) {
dst[x] = (dst[x] & 0xFFFFFF) | (alpha[data & 7] << 24);
data >>= 3;

View File

@ -115,9 +115,9 @@ struct DXT5Block {
u8 alpha1; u8 alpha2;
};
void DecodeDXT1Block(u32 *dst, const DXT1Block *src, int pitch, bool ignore1bitAlpha = false);
void DecodeDXT3Block(u32 *dst, const DXT3Block *src, int pitch);
void DecodeDXT5Block(u32 *dst, const DXT5Block *src, int pitch);
void DecodeDXT1Block(u32 *dst, const DXT1Block *src, int pitch, int height, bool ignore1bitAlpha);
void DecodeDXT3Block(u32 *dst, const DXT3Block *src, int pitch, int height);
void DecodeDXT5Block(u32 *dst, const DXT5Block *src, int pitch, int height);
static const u8 textureBitsPerPixel[16] = {
16, //GE_TFMT_5650,

View File

@ -1138,15 +1138,14 @@ void TextureCacheD3D11::LoadTextureLevel(TexCacheEntry &entry, ReplacedTexture &
GEPaletteFormat clutformat = gstate.getClutPaletteFormat();
u32 texaddr = gstate.getTextureAddress(level);
int bufw = GetTextureBufw(level, texaddr, tfmt);
int roundH = (h + 3) & ~3; // Need to leave space for DXT decoding!
int bpp = dstFmt == DXGI_FORMAT_B8G8R8A8_UNORM ? 4 : 2;
mapRowPitch = std::max(bufw, w) * 4;
mapData = new u32[mapRowPitch / 4 * roundH]{};
mapData = new u32[mapRowPitch / 4 * h]{};
u32 *pixelData = (u32 *)mapData;
int decPitch = mapRowPitch;
if (scaleFactor > 1) {
tmpTexBufRearrange.resize(std::max(bufw, w) * roundH);
tmpTexBufRearrange.resize(std::max(bufw, w) * h);
pixelData = tmpTexBufRearrange.data();
// We want to end up with a neatly packed texture for scaling.
decPitch = w * bpp;

View File

@ -354,7 +354,7 @@ inline static Nearest4 SampleNearest(int level, int u[N], int v[N], const u8 *sr
for (int i = 0; i < N; ++i) {
const DXT1Block *block = (const DXT1Block *)srcptr + (v[i] / 4) * (texbufwidthbits / 8 / 4) + (u[i] / 4);
u32 data[4 * 4];
DecodeDXT1Block(data, block, 4);
DecodeDXT1Block(data, block, 4, 4, false);
res.v[i] = data[4 * (v[i] % 4) + (u[i] % 4)];
}
return res;
@ -363,7 +363,7 @@ inline static Nearest4 SampleNearest(int level, int u[N], int v[N], const u8 *sr
for (int i = 0; i < N; ++i) {
const DXT3Block *block = (const DXT3Block *)srcptr + (v[i] / 4) * (texbufwidthbits / 8 / 4) + (u[i] / 4);
u32 data[4 * 4];
DecodeDXT3Block(data, block, 4);
DecodeDXT3Block(data, block, 4, 4);
res.v[i] = data[4 * (v[i] % 4) + (u[i] % 4)];
}
return res;
@ -372,7 +372,7 @@ inline static Nearest4 SampleNearest(int level, int u[N], int v[N], const u8 *sr
for (int i = 0; i < N; ++i) {
const DXT5Block *block = (const DXT5Block *)srcptr + (v[i] / 4) * (texbufwidthbits / 8 / 4) + (u[i] / 4);
u32 data[4 * 4];
DecodeDXT5Block(data, block, 4);
DecodeDXT5Block(data, block, 4, 4);
res.v[i] = data[4 * (v[i] % 4) + (u[i] % 4)];
}
return res;