Merge pull request #1599 from PeterTh/master

xBRZ texture scaling
This commit is contained in:
Henrik Rydgård 2013-04-30 04:29:36 -07:00
commit b7cd4d63e2
11 changed files with 1421 additions and 2 deletions

View File

@ -113,6 +113,8 @@ void Config::Load(const char *iniFileName)
#else
graphics->Get("MipMap", &bMipMap, false);
#endif
graphics->Get("XBRZTexScalingLevel", &iXBRZTexScalingLevel, 1);
IniFile::Section *sound = iniFile.GetOrCreateSection("Sound");
sound->Get("Enable", &bEnableSound, true);
@ -192,6 +194,7 @@ void Config::Save()
graphics->Set("StretchToDisplay", bStretchToDisplay);
graphics->Set("TrueColor", bTrueColor);
graphics->Set("MipMap", bMipMap);
graphics->Set("XBRZTexScalingLevel", iXBRZTexScalingLevel);
IniFile::Section *sound = iniFile.GetOrCreateSection("Sound");
sound->Set("Enable", bEnableSound);

View File

@ -64,13 +64,14 @@ public:
int iWindowX;
int iWindowY;
int iWindowZoom; // for Windows
bool SSAntiAliasing; //for Windows, too
bool SSAntiAliasing; // for Windows, too
bool bVertexCache;
bool bFullScreen;
int iAnisotropyLevel;
bool bTrueColor;
bool bMipMap;
bool bAnisotropicFiltering;
int iXBRZTexScalingLevel; // 1 = off, 2 = 2xBRZ, ..., 5 = 5xBRZ
// Sound
bool bEnableSound;

View File

@ -27,6 +27,7 @@
#include "Core/Config.h"
#include "native/ext/cityhash/city.h"
#include "ext/xbrz/xbrz.h"
// If a texture hasn't been seen for this many frames, get rid of it.
#define TEXTURE_KILL_AGE 200
@ -1194,8 +1195,79 @@ void TextureCache::LoadTextureLevel(TexCacheEntry &entry, int level) {
// INFO_LOG(G3D, "Creating texture level %i/%i from %08x: %i x %i (stride: %i). fmt: %i", level, entry.maxLevel, texaddr, w, h, bufw, entry.format);
u32* pixelData = (u32*)finalBuf;
ScaleTexture(pixelData, dstFmt, w, h);
GLuint components = dstFmt == GL_UNSIGNED_SHORT_5_6_5 ? GL_RGB : GL_RGBA;
glTexImage2D(GL_TEXTURE_2D, level, components, w, h, 0, components, dstFmt, finalBuf);
glTexImage2D(GL_TEXTURE_2D, level, components, w, h, 0, components, dstFmt, pixelData);
}
void TextureCache::ScaleTexture(u32* &data, GLenum &dstFmt, int &width, int &height) {
if(g_Config.iXBRZTexScalingLevel > 1) {
int factor = g_Config.iXBRZTexScalingLevel;
// I tried to reuse the existing tmpTexBuf initially, but that's not a good idea as data could already be stored in it
// depending on the factor and texture sizes, these can be pretty large (25 MB for a 512 by 512 texture with scaling factor 5)
tmpTexBufScalingInput.resize(width*height); // used to store the input image image if it needs to be reformatted
tmpTexBufScalingOutput.resize(width*height*factor*factor); // used to store the upscaled image
u32 *xbrzInputBuf = tmpTexBufScalingInput.data();
u32 *xbrzBuf = tmpTexBufScalingOutput.data();
// convert texture to correct format for xBRZ
switch(dstFmt) {
case GL_UNSIGNED_BYTE:
xbrzInputBuf = data; // already fine
break;
case GL_UNSIGNED_SHORT_4_4_4_4:
for(int y = 0; y < height; ++y) {
for(int x = 0; x < width; ++x) {
u32 val = ((u16*)data)[y*width + x];
u32 r = ((val>>12) & 0xF) * 17;
u32 g = ((val>> 8) & 0xF) * 17;
u32 b = ((val>> 4) & 0xF) * 17;
u32 a = ((val>> 0) & 0xF) * 17;
xbrzInputBuf[y*width + x] = (a << 24) | (b << 16) | (g << 8) | r;
}
}
break;
case GL_UNSIGNED_SHORT_5_6_5:
for(int y = 0; y < height; ++y) {
for(int x = 0; x < width; ++x) {
u32 val = ((u16*)data)[y*width + x];
u32 r = ((val>>11) & 0x1F) * 8;
u32 g = ((val>> 5) & 0x3F) * 4;
u32 b = ((val ) & 0x1F) * 8;
xbrzInputBuf[y*width + x] = (0xFF << 24) | (b << 16) | (g << 8) | r;
}
}
break;
case GL_UNSIGNED_SHORT_5_5_5_1:
for(int y = 0; y < height; ++y) {
for(int x = 0; x < width; ++x) {
u32 val = ((u16*)data)[y*width + x];
u32 r = ((val>>11) & 0x1F) * 8;
u32 g = ((val>> 6) & 0x1F) * 8;
u32 b = ((val>> 1) & 0x1F) * 8;
u32 a = (val & 0x1) * 255;
xbrzInputBuf[y*width + x] = (a << 24) | (b << 16) | (g << 8) | r;
}
}
break;
default:
ERROR_LOG(G3D, "iXBRZTexScaling: unsupported texture format");
}
// scale and update values accordingly
xbrz::scale(factor, xbrzInputBuf, xbrzBuf, width, height);
data = xbrzBuf;
dstFmt = GL_UNSIGNED_BYTE;
width *= factor;
height *= factor;
}
}
bool TextureCache::DecodeTexture(u8* output, GPUgstate state)

View File

@ -93,6 +93,7 @@ private:
void UpdateSamplingParams(TexCacheEntry &entry, bool force);
void LoadTextureLevel(TexCacheEntry &entry, int level);
void *DecodeTextureLevel(u8 format, u8 clutformat, int level, u32 &texByteAlign, GLenum &dstFmt);
void ScaleTexture(u32* &data, GLenum &dstfmt, int &width, int &height);
TexCacheEntry *GetEntryAt(u32 texaddr);
@ -148,6 +149,9 @@ private:
SimpleBuf<u32> tmpTexBufRearrange;
SimpleBuf<u32> tmpTexBufScalingInput;
SimpleBuf<u32> tmpTexBufScalingOutput;
u32 *clutBuf32;
u16 *clutBuf16;

View File

@ -129,6 +129,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\ext\xbrz\xbrz.h" />
<ClInclude Include="ge_constants.h" />
<ClInclude Include="GLES\DisplayListInterpreter.h" />
<ClInclude Include="GLES\FragmentShaderGenerator.h" />
@ -148,6 +149,7 @@
<ClInclude Include="Null\NullGpu.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\ext\xbrz\xbrz.cpp" />
<ClCompile Include="GLES\DisplayListInterpreter.cpp" />
<ClCompile Include="GLES\FragmentShaderGenerator.cpp" />
<ClCompile Include="GLES\Framebuffer.cpp" />

View File

@ -175,6 +175,11 @@ namespace MainWindow
ResizeDisplay();
}
void setXbrzTexScaling(int num) {
g_Config.iXBRZTexScalingLevel = num;
if(gpu) gpu->InvalidateCache(0,0);
}
BOOL Show(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
@ -495,6 +500,22 @@ namespace MainWindow
g_Config.bMipMap = !g_Config.bMipMap;
break;
case ID_TEXTURESCALING_OFF:
setXbrzTexScaling(1);
break;
case ID_TEXTURESCALING_2XBRZ:
setXbrzTexScaling(2);
break;
case ID_TEXTURESCALING_3XBRZ:
setXbrzTexScaling(3);
break;
case ID_TEXTURESCALING_4XBRZ:
setXbrzTexScaling(4);
break;
case ID_TEXTURESCALING_5XBRZ:
setXbrzTexScaling(5);
break;
case ID_OPTIONS_BUFFEREDRENDERING:
g_Config.bBufferedRendering = !g_Config.bBufferedRendering;
if (gpu)
@ -785,6 +806,17 @@ namespace MainWindow
for (int i = 0; i < 4; i++) {
CheckMenuItem(menu, zoomitems[i], MF_BYCOMMAND | ((i == g_Config.iWindowZoom - 1) ? MF_CHECKED : MF_UNCHECKED));
}
static const int texscalingitems[] = {
ID_TEXTURESCALING_OFF,
ID_TEXTURESCALING_2XBRZ,
ID_TEXTURESCALING_3XBRZ,
ID_TEXTURESCALING_4XBRZ,
ID_TEXTURESCALING_5XBRZ,
};
for (int i = 0; i < 5; i++) {
CheckMenuItem(menu, texscalingitems[i], MF_BYCOMMAND | ((i == g_Config.iXBRZTexScalingLevel-1) ? MF_CHECKED : MF_UNCHECKED));
}
}

Binary file not shown.

Binary file not shown.

40
ext/xbrz/config.h Normal file
View File

@ -0,0 +1,40 @@
// ****************************************************************************
// * This file is part of the HqMAME project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved *
// * *
// * Additionally and as a special exception, the author gives permission *
// * to link the code of this program with the MAME library (or with modified *
// * versions of MAME that use the same license as MAME), and distribute *
// * linked combinations including the two. You must obey the GNU General *
// * Public License in all respects for all of the code used other than MAME. *
// * If you modify this file, you may extend this exception to your version *
// * of the file, but you are not obligated to do so. If you do not wish to *
// * do so, delete this exception statement from your version. *
// ****************************************************************************
#ifndef XBRZ_CONFIG_HEADER_284578425345
#define XBRZ_CONFIG_HEADER_284578425345
//do NOT include any headers here! used by xBRZ_dll!!!
namespace xbrz
{
struct ScalerCfg
{
ScalerCfg() :
luminanceWeight_(1),
equalColorTolerance_(30),
dominantDirectionThreshold(3.6),
steepDirectionThreshold(2.2),
newTestAttribute_(0) {}
double luminanceWeight_;
double equalColorTolerance_;
double dominantDirectionThreshold;
double steepDirectionThreshold;
double newTestAttribute_; //unused; test new parameters
};
}
#endif

1185
ext/xbrz/xbrz.cpp Normal file

File diff suppressed because it is too large Load Diff

80
ext/xbrz/xbrz.h Normal file
View File

@ -0,0 +1,80 @@
// ****************************************************************************
// * This file is part of the HqMAME project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved *
// * *
// * Additionally and as a special exception, the author gives permission *
// * to link the code of this program with the MAME library (or with modified *
// * versions of MAME that use the same license as MAME), and distribute *
// * linked combinations including the two. You must obey the GNU General *
// * Public License in all respects for all of the code used other than MAME. *
// * If you modify this file, you may extend this exception to your version *
// * of the file, but you are not obligated to do so. If you do not wish to *
// * do so, delete this exception statement from your version. *
// ****************************************************************************
#ifndef XBRZ_HEADER_3847894708239054
#define XBRZ_HEADER_3847894708239054
#include <cstddef> //size_t
#include <cstdint> //uint32_t
#include <limits>
#include "config.h"
namespace xbrz
{
/*
-------------------------------------------------------------------------
| xBRZ: "Scale by rules" - high quality image upscaling filter by Zenju |
-------------------------------------------------------------------------
using a modified approach of xBR:
http://board.byuu.org/viewtopic.php?f=10&t=2248
- new rule set preserving small image features
- support multithreading
- support 64 bit architectures
*/
/*
-> map source (srcWidth * srcHeight) to target (scale * width x scale * height) image, optionally processing rows [yFirst, yLast) only
-> color format: ARGB (BGRA byte order)
-> optional source/target pitch in bytes!
THREAD-SAFETY: - parts of the same image may be scaled by multiple threads as long as the [yFirst, yLast) ranges do not overlap!
- there is a minor inefficiency for the first row of a slice, so avoid processing single rows only
*/
void scale(size_t factor, //valid range: 2 - 5
const uint32_t* src, uint32_t* trg, int srcWidth, int srcHeight,
const ScalerCfg& cfg = ScalerCfg(),
int yFirst = 0, int yLast = std::numeric_limits<int>::max()); //slice of source image
void nearestNeighborScale(const uint32_t* src, int srcWidth, int srcHeight,
uint32_t* trg, int trgWidth, int trgHeight);
enum SliceType
{
NN_SCALE_SLICE_SOURCE,
NN_SCALE_SLICE_TARGET,
};
void nearestNeighborScale(const uint32_t* src, int srcWidth, int srcHeight, int srcPitch, //pitch in bytes!
uint32_t* trg, int trgWidth, int trgHeight, int trgPitch,
SliceType st, int yFirst, int yLast);
//parameter tuning
bool equalColor(uint32_t col1, uint32_t col2, double luminanceWeight, double equalColorTolerance);
//########################### implementation ###########################
inline
void nearestNeighborScale(const uint32_t* src, int srcWidth, int srcHeight,
uint32_t* trg, int trgWidth, int trgHeight)
{
nearestNeighborScale(src, srcWidth, srcHeight, srcWidth * sizeof(uint32_t),
trg, trgWidth, trgHeight, trgWidth * sizeof(uint32_t),
NN_SCALE_SLICE_TARGET, 0, trgHeight);
}
}
#endif