Vulkan: Fix flat texture scaling.

This commit is contained in:
Unknown W. Brackets 2016-03-26 17:21:29 -07:00
parent 5e23d63538
commit b6778bf4ad
5 changed files with 48 additions and 5 deletions

View file

@ -495,18 +495,21 @@ TextureScaler::~TextureScaler() {
bool TextureScaler::IsEmptyOrFlat(u32* data, int pixels, int fmt) {
int pixelsPerWord = 4 / BytesPerPixel(fmt);
u32 ref = data[0];
if (pixelsPerWord > 1 && (ref & 0x0000FFFF) != (ref >> 16)) {
return false;
}
for (int i = 0; i < pixels / pixelsPerWord; ++i) {
if (data[i] != ref) return false;
}
return true;
}
void TextureScaler::Scale(u32* &data, u32 &dstFmt, int &width, int &height, int factor) {
bool TextureScaler::Scale(u32* &data, u32 &dstFmt, int &width, int &height, int factor) {
// prevent processing empty or flat textures (this happens a lot in some games)
// doesn't hurt the standard case, will be very quick for textures with actual texture
if (IsEmptyOrFlat(data, width*height, dstFmt)) {
INFO_LOG(G3D, "TextureScaler: early exit -- empty/flat texture");
return;
return false;
}
#ifdef SCALING_MEASURE_TIME
@ -559,6 +562,8 @@ void TextureScaler::Scale(u32* &data, u32 &dstFmt, int &width, int &height, int
width*height, t, (width*height) / (t * 1000 * 1000));
}
#endif
return true;
}
void TextureScaler::ScaleXBRZ(int factor, u32* source, u32* dest, int width, int height) {

View file

@ -27,11 +27,11 @@ public:
TextureScaler();
~TextureScaler();
void Scale(u32* &data, u32 &dstfmt, int &width, int &height, int factor);
bool Scale(u32* &data, u32 &dstfmt, int &width, int &height, int factor);
enum { XBRZ = 0, HYBRID = 1, BICUBIC = 2, HYBRID_BICUBIC = 3 };
private:
protected:
virtual void ConvertTo8888(u32 format, u32* source, u32* &dest, int width, int height) = 0;
virtual int BytesPerPixel(u32 format) = 0;
virtual u32 Get8888Format() = 0;

View file

@ -1612,6 +1612,7 @@ void TextureCacheVulkan::LoadTextureLevel(TexCacheEntry &entry, uint8_t *writePt
bool decSuccess = DecodeTextureLevel((u8 *)pixelData, decPitch, tfmt, clutformat, texaddr, level, dstFmt, scaleFactor, bufw);
if (!decSuccess) {
memset(writePtr, 0, rowPitch * h);
return;
}
rowBytes = w * bpp;
@ -1619,7 +1620,7 @@ void TextureCacheVulkan::LoadTextureLevel(TexCacheEntry &entry, uint8_t *writePt
if (scaleFactor > 1) {
u32 fmt = dstFmt;
scaler.Scale(pixelData, fmt, w, h, scaleFactor);
scaler.ScaleAlways(pixelData, fmt, w, h, scaleFactor);
dstFmt = (VkFormat)fmt;
// We always end up at 8888. Other parts assume this.

View file

@ -44,6 +44,39 @@ u32 TextureScalerVulkan::Get8888Format() {
return VULKAN_8888_FORMAT;
}
void TextureScalerVulkan::ScaleAlways(u32 *&data, u32 &dstFmt, int &width, int &height, int factor) {
if (!Scale(data, dstFmt, width, height, factor)) {
// This means it was a flat texture. Vulkan wants the size up front, so we need to make it happen.
assert(IsEmptyOrFlat(data, width * height, dstFmt));
u32 pixel;
// Since it's flat, one pixel is enough. It might end up pointing to data, though.
u32 *pixelPointer = &pixel;
ConvertTo8888(dstFmt, data, pixelPointer, 1, 1);
if (pixelPointer != &pixel) {
pixel = *pixelPointer;
}
bufOutput.resize(width * height * factor * factor);
dstFmt = Get8888Format();
data = bufOutput.data();
width *= factor;
height *= factor;
// ABCD. If A = D, and AB = CD, then they must all be equal (B = C, etc.)
if ((pixel & 0x000000FF) == (pixel >> 24) && (pixel & 0x0000FFFF) == (pixel >> 16)) {
memset(data, pixel & 0xFF, width * height * sizeof(u32));
} else {
// TODO: Optimize.
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
data[y * width + x] = pixel;
}
}
}
}
}
void TextureScalerVulkan::ConvertTo8888(u32 format, u32* source, u32* &dest, int width, int height) {
switch (format) {
case VULKAN_8888_FORMAT:

View file

@ -21,6 +21,10 @@
#include "GPU/Common/TextureScalerCommon.h"
class TextureScalerVulkan : public TextureScaler {
public:
void ScaleAlways(u32* &data, u32 &dstFmt, int &width, int &height, int factor);
protected:
void ConvertTo8888(u32 format, u32* source, u32* &dest, int width, int height) override;
int BytesPerPixel(u32 format) override;
u32 Get8888Format() override;