2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:29:41 +00:00
|
|
|
// Refer to the license.txt file included.
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-05-03 02:47:04 +00:00
|
|
|
#include <algorithm>
|
2014-02-19 11:14:09 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2014-10-21 06:01:38 +00:00
|
|
|
#include "Common/Common.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/HW/Memmap.h"
|
|
|
|
#include "VideoBackends/Software/TextureSampler.h"
|
2015-10-09 18:50:36 +00:00
|
|
|
|
|
|
|
#include "VideoCommon/BPMemory.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/TextureDecoder.h"
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
#define ALLOW_MIPMAP 1
|
|
|
|
|
|
|
|
namespace TextureSampler
|
|
|
|
{
|
|
|
|
|
2014-10-02 06:20:46 +00:00
|
|
|
static inline void WrapCoord(int* coordp, int wrapMode, int imageSize)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2014-10-02 06:20:46 +00:00
|
|
|
int coord = *coordp;
|
2013-04-14 03:54:02 +00:00
|
|
|
switch (wrapMode)
|
|
|
|
{
|
|
|
|
case 0: // clamp
|
|
|
|
coord = (coord>imageSize)?imageSize:(coord<0)?0:coord;
|
|
|
|
break;
|
|
|
|
case 1: // wrap
|
|
|
|
coord = coord % (imageSize + 1);
|
|
|
|
coord = (coord<0)?imageSize+coord:coord;
|
|
|
|
break;
|
|
|
|
case 2: // mirror
|
|
|
|
{
|
|
|
|
int sizePlus1 = imageSize + 1;
|
|
|
|
int div = coord / sizePlus1;
|
|
|
|
coord = coord - (div * sizePlus1);
|
|
|
|
coord = (coord<0)?-coord:coord;
|
|
|
|
coord = (div&1)?imageSize - coord:coord;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-10-02 06:20:46 +00:00
|
|
|
*coordp = coord;
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 01:51:05 +00:00
|
|
|
static inline void SetTexel(u8 *inTexel, u32 *outTexel, u32 fract)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
outTexel[0] = inTexel[0] * fract;
|
|
|
|
outTexel[1] = inTexel[1] * fract;
|
|
|
|
outTexel[2] = inTexel[2] * fract;
|
|
|
|
outTexel[3] = inTexel[3] * fract;
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 01:51:05 +00:00
|
|
|
static inline void AddTexel(u8 *inTexel, u32 *outTexel, u32 fract)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
outTexel[0] += inTexel[0] * fract;
|
|
|
|
outTexel[1] += inTexel[1] * fract;
|
|
|
|
outTexel[2] += inTexel[2] * fract;
|
|
|
|
outTexel[3] += inTexel[3] * fract;
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sample(s32 s, s32 t, s32 lod, bool linear, u8 texmap, u8 *sample)
|
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
int baseMip = 0;
|
2010-06-09 01:37:08 +00:00
|
|
|
bool mipLinear = false;
|
|
|
|
|
|
|
|
#if (ALLOW_MIPMAP)
|
|
|
|
FourTexUnits& texUnit = bpmem.tex[(texmap >> 2) & 1];
|
2013-04-14 03:54:02 +00:00
|
|
|
TexMode0& tm0 = texUnit.texMode0[texmap & 3];
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
s32 lodFract = lod & 0xf;
|
|
|
|
|
|
|
|
if (lod > 0 && tm0.min_filter & 3)
|
|
|
|
{
|
|
|
|
// use mipmap
|
|
|
|
baseMip = lod >> 4;
|
|
|
|
mipLinear = (lodFract && tm0.min_filter & 2);
|
|
|
|
|
|
|
|
// if using nearest mip filter and lodFract >= 0.5 round up to next mip
|
|
|
|
baseMip += (lodFract >> 3) & (tm0.min_filter & 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mipLinear)
|
|
|
|
{
|
|
|
|
u8 sampledTex[4];
|
2013-04-14 03:54:02 +00:00
|
|
|
u32 texel[4];
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
SampleMip(s, t, baseMip, linear, texmap, sampledTex);
|
|
|
|
SetTexel(sampledTex, texel, (16 - lodFract));
|
|
|
|
|
|
|
|
SampleMip(s, t, baseMip + 1, linear, texmap, sampledTex);
|
|
|
|
AddTexel(sampledTex, texel, lodFract);
|
|
|
|
|
|
|
|
sample[0] = (u8)(texel[0] >> 4);
|
2013-04-14 03:54:02 +00:00
|
|
|
sample[1] = (u8)(texel[1] >> 4);
|
|
|
|
sample[2] = (u8)(texel[2] >> 4);
|
|
|
|
sample[3] = (u8)(texel[3] >> 4);
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
SampleMip(s, t, baseMip, linear, texmap, sample);
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SampleMip(s32 s, s32 t, s32 mip, bool linear, u8 texmap, u8 *sample)
|
|
|
|
{
|
|
|
|
FourTexUnits& texUnit = bpmem.tex[(texmap >> 2) & 1];
|
2013-04-14 03:54:02 +00:00
|
|
|
u8 subTexmap = texmap & 3;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
TexMode0& tm0 = texUnit.texMode0[subTexmap];
|
|
|
|
TexImage0& ti0 = texUnit.texImage0[subTexmap];
|
|
|
|
TexTLUT& texTlut = texUnit.texTlut[subTexmap];
|
2014-08-10 19:28:42 +00:00
|
|
|
TlutFormat tlutfmt = (TlutFormat) texTlut.tlut_format;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-03-09 20:14:26 +00:00
|
|
|
u8 *imageSrc, *imageSrcOdd = nullptr;
|
2012-11-03 14:36:40 +00:00
|
|
|
if (texUnit.texImage1[subTexmap].image_type)
|
|
|
|
{
|
|
|
|
imageSrc = &texMem[texUnit.texImage1[subTexmap].tmem_even * TMEM_LINE_SIZE];
|
2013-01-09 09:19:18 +00:00
|
|
|
if (ti0.format == GX_TF_RGBA8)
|
|
|
|
imageSrcOdd = &texMem[texUnit.texImage2[subTexmap].tmem_odd * TMEM_LINE_SIZE];
|
2012-11-03 14:36:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
u32 imageBase = texUnit.texImage3[subTexmap].image_base << 5;
|
2012-11-03 14:36:40 +00:00
|
|
|
imageSrc = Memory::GetPointer(imageBase);
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
int imageWidth = ti0.width;
|
|
|
|
int imageHeight = ti0.height;
|
|
|
|
|
|
|
|
int tlutAddress = texTlut.tmem_offset << 9;
|
2014-08-10 19:28:42 +00:00
|
|
|
const u8* tlut = &texMem[tlutAddress];
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2010-06-09 01:37:08 +00:00
|
|
|
// reduce sample location and texture size to mip level
|
|
|
|
// move texture pointer to mip location
|
|
|
|
if (mip)
|
|
|
|
{
|
|
|
|
int mipWidth = imageWidth + 1;
|
|
|
|
int mipHeight = imageHeight + 1;
|
|
|
|
|
|
|
|
int fmtWidth = TexDecoder_GetBlockWidthInTexels(ti0.format);
|
|
|
|
int fmtHeight = TexDecoder_GetBlockHeightInTexels(ti0.format);
|
|
|
|
int fmtDepth = TexDecoder_GetTexelSizeInNibbles(ti0.format);
|
|
|
|
|
|
|
|
imageWidth >>= mip;
|
|
|
|
imageHeight >>= mip;
|
|
|
|
s >>= mip;
|
|
|
|
t >>= mip;
|
|
|
|
|
|
|
|
while (mip)
|
|
|
|
{
|
2014-05-03 02:47:04 +00:00
|
|
|
mipWidth = std::max(mipWidth, fmtWidth);
|
|
|
|
mipHeight = std::max(mipHeight, fmtHeight);
|
2010-06-09 01:37:08 +00:00
|
|
|
u32 size = (mipWidth * mipHeight * fmtDepth) >> 1;
|
|
|
|
|
|
|
|
imageSrc += size;
|
|
|
|
mipWidth >>= 1;
|
|
|
|
mipHeight >>= 1;
|
|
|
|
mip--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
if (linear)
|
|
|
|
{
|
2010-06-09 01:37:08 +00:00
|
|
|
// offset linear sampling
|
|
|
|
s -= 64;
|
|
|
|
t -= 64;
|
|
|
|
|
|
|
|
// integer part of sample location
|
|
|
|
int imageS = s >> 7;
|
|
|
|
int imageT = t >> 7;
|
|
|
|
|
|
|
|
// linear sampling
|
|
|
|
int imageSPlus1 = imageS + 1;
|
2013-04-14 03:54:02 +00:00
|
|
|
int fractS = s & 0x7f;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
int imageTPlus1 = imageT + 1;
|
|
|
|
int fractT = t & 0x7f;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
u8 sampledTex[4];
|
|
|
|
u32 texel[4];
|
|
|
|
|
2014-10-02 06:20:46 +00:00
|
|
|
WrapCoord(&imageS, tm0.wrap_s, imageWidth);
|
|
|
|
WrapCoord(&imageT, tm0.wrap_t, imageHeight);
|
|
|
|
WrapCoord(&imageSPlus1, tm0.wrap_s, imageWidth);
|
|
|
|
WrapCoord(&imageTPlus1, tm0.wrap_t, imageHeight);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-01-09 09:19:18 +00:00
|
|
|
if (!(ti0.format == GX_TF_RGBA8 && texUnit.texImage1[subTexmap].image_type))
|
|
|
|
{
|
2014-08-10 19:28:42 +00:00
|
|
|
TexDecoder_DecodeTexel(sampledTex, imageSrc, imageS, imageT, imageWidth, ti0.format, tlut, tlutfmt);
|
2013-01-09 09:19:18 +00:00
|
|
|
SetTexel(sampledTex, texel, (128 - fractS) * (128 - fractT));
|
|
|
|
|
2014-08-10 19:28:42 +00:00
|
|
|
TexDecoder_DecodeTexel(sampledTex, imageSrc, imageSPlus1, imageT, imageWidth, ti0.format, tlut, tlutfmt);
|
2013-01-09 09:19:18 +00:00
|
|
|
AddTexel(sampledTex, texel, (fractS) * (128 - fractT));
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-08-10 19:28:42 +00:00
|
|
|
TexDecoder_DecodeTexel(sampledTex, imageSrc, imageS, imageTPlus1, imageWidth, ti0.format, tlut, tlutfmt);
|
2013-01-09 09:19:18 +00:00
|
|
|
AddTexel(sampledTex, texel, (128 - fractS) * (fractT));
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-08-10 19:28:42 +00:00
|
|
|
TexDecoder_DecodeTexel(sampledTex, imageSrc, imageSPlus1, imageTPlus1, imageWidth, ti0.format, tlut, tlutfmt);
|
2013-01-09 09:19:18 +00:00
|
|
|
AddTexel(sampledTex, texel, (fractS) * (fractT));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TexDecoder_DecodeTexelRGBA8FromTmem(sampledTex, imageSrc, imageSrcOdd, imageS, imageT, imageWidth);
|
|
|
|
SetTexel(sampledTex, texel, (128 - fractS) * (128 - fractT));
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-01-09 09:19:18 +00:00
|
|
|
TexDecoder_DecodeTexelRGBA8FromTmem(sampledTex, imageSrc, imageSrcOdd, imageSPlus1, imageT, imageWidth);
|
|
|
|
AddTexel(sampledTex, texel, (fractS) * (128 - fractT));
|
|
|
|
|
|
|
|
TexDecoder_DecodeTexelRGBA8FromTmem(sampledTex, imageSrc, imageSrcOdd, imageS, imageTPlus1, imageWidth);
|
|
|
|
AddTexel(sampledTex, texel, (128 - fractS) * (fractT));
|
|
|
|
|
|
|
|
TexDecoder_DecodeTexelRGBA8FromTmem(sampledTex, imageSrc, imageSrcOdd, imageSPlus1, imageTPlus1, imageWidth);
|
|
|
|
AddTexel(sampledTex, texel, (fractS) * (fractT));
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
sample[0] = (u8)(texel[0] >> 14);
|
|
|
|
sample[1] = (u8)(texel[1] >> 14);
|
|
|
|
sample[2] = (u8)(texel[2] >> 14);
|
|
|
|
sample[3] = (u8)(texel[3] >> 14);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-06-09 01:37:08 +00:00
|
|
|
// integer part of sample location
|
|
|
|
int imageS = s >> 7;
|
|
|
|
int imageT = t >> 7;
|
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
// nearest neighbor sampling
|
2014-10-02 06:20:46 +00:00
|
|
|
WrapCoord(&imageS, tm0.wrap_s, imageWidth);
|
|
|
|
WrapCoord(&imageT, tm0.wrap_t, imageHeight);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-01-09 09:19:18 +00:00
|
|
|
if (!(ti0.format == GX_TF_RGBA8 && texUnit.texImage1[subTexmap].image_type))
|
2014-08-10 19:28:42 +00:00
|
|
|
TexDecoder_DecodeTexel(sample, imageSrc, imageS, imageT, imageWidth, ti0.format, tlut, tlutfmt);
|
2013-01-09 09:19:18 +00:00
|
|
|
else
|
2013-04-14 03:54:02 +00:00
|
|
|
TexDecoder_DecodeTexelRGBA8FromTmem(sample, imageSrc, imageSrcOdd, imageS, imageT, imageWidth);
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|