2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2016-09-24 09:16:52 +00:00
|
|
|
#include "VideoBackends/Software/TextureSampler.h"
|
|
|
|
|
2014-05-03 02:47:04 +00:00
|
|
|
#include <algorithm>
|
2014-02-19 11:14:09 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2016-09-24 09:16:52 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2021-02-11 02:11:31 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/HW/Memmap.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
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
#define ALLOW_MIPMAP 1
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
namespace TextureSampler
|
2009-10-12 00:48:24 +00:00
|
|
|
{
|
2021-07-20 19:55:38 +00:00
|
|
|
static inline void WrapCoord(int* coordp, WrapMode wrap_mode, int image_size)
|
2009-10-12 00:48:24 +00:00
|
|
|
{
|
2014-10-02 06:20:46 +00:00
|
|
|
int coord = *coordp;
|
2021-07-20 19:55:38 +00:00
|
|
|
switch (wrap_mode)
|
2009-10-12 00:48:24 +00:00
|
|
|
{
|
2021-02-11 02:11:31 +00:00
|
|
|
case WrapMode::Clamp:
|
2021-07-20 19:55:38 +00:00
|
|
|
coord = std::clamp(coord, 0, image_size - 1);
|
2009-10-12 00:48:24 +00:00
|
|
|
break;
|
2021-02-11 02:11:31 +00:00
|
|
|
case WrapMode::Repeat:
|
2021-07-20 19:55:38 +00:00
|
|
|
// Per YAGCD's info on TX_SETMODE1_I0 (et al.), mirror "requires the texture size to be a power
|
|
|
|
// of two. (wrapping is implemented by a logical AND (SIZE-1))". So though this doesn't wrap
|
|
|
|
// nicely for non-power-of-2 sizes, that's how hardware does it.
|
|
|
|
coord = coord & (image_size - 1);
|
2009-10-12 00:48:24 +00:00
|
|
|
break;
|
2021-02-11 02:11:31 +00:00
|
|
|
case WrapMode::Mirror:
|
2009-10-12 00:48:24 +00:00
|
|
|
{
|
2021-07-20 19:55:38 +00:00
|
|
|
// YAGCD doesn't mention this, but this seems to be the check used to implement mirroring.
|
|
|
|
// With power-of-2 sizes, this correctly checks if it's an even-numbered repeat or an
|
|
|
|
// odd-numbered one, and thus can decide whether to reflect. It fails in unusual ways
|
|
|
|
// with non-power-of-2 sizes, but seems to match what happens on actual hardware.
|
|
|
|
if ((coord & image_size) != 0)
|
|
|
|
coord = ~coord;
|
|
|
|
coord = coord & (image_size - 1);
|
2021-02-11 02:11:31 +00:00
|
|
|
break;
|
2009-10-12 00:48:24 +00:00
|
|
|
}
|
2021-02-11 02:11:31 +00:00
|
|
|
default:
|
2021-07-20 19:55:38 +00:00
|
|
|
// Hardware testing indicates that wrap_mode set to 3 behaves the same as clamp.
|
|
|
|
PanicAlertFmt("Invalid wrap mode: {}", wrap_mode);
|
|
|
|
coord = std::clamp(coord, 0, image_size - 1);
|
|
|
|
break;
|
2009-10-12 00:48:24 +00:00
|
|
|
}
|
2014-10-02 06:20:46 +00:00
|
|
|
*coordp = coord;
|
2009-10-12 00:48:24 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2017-09-11 23:56:38 +00:00
|
|
|
static inline void SetTexel(const u8* inTexel, u32* outTexel, u32 fract)
|
2009-10-12 00:48:24 +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
|
|
|
|
2017-09-11 23:56:38 +00:00
|
|
|
static inline void AddTexel(const u8* inTexel, u32* outTexel, u32 fract)
|
2009-10-12 00:48:24 +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
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
void Sample(s32 s, s32 t, s32 lod, bool linear, u8 texmap, u8* sample)
|
|
|
|
{
|
|
|
|
int baseMip = 0;
|
|
|
|
bool mipLinear = false;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
#if (ALLOW_MIPMAP)
|
2021-10-09 19:16:15 +00:00
|
|
|
auto texUnit = bpmem.tex.GetUnit(texmap);
|
|
|
|
const TexMode0& tm0 = texUnit.texMode0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-09-11 23:56:38 +00:00
|
|
|
const s32 lodFract = lod & 0xf;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-08-08 18:05:32 +00:00
|
|
|
if (lod > 0 && tm0.mipmap_filter != MipMode::None)
|
2010-03-09 04:38:07 +00:00
|
|
|
{
|
|
|
|
// use mipmap
|
|
|
|
baseMip = lod >> 4;
|
2021-02-11 02:11:31 +00:00
|
|
|
mipLinear = (lodFract && tm0.mipmap_filter == MipMode::Linear);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
// if using nearest mip filter and lodFract >= 0.5 round up to next mip
|
2021-02-11 02:11:31 +00:00
|
|
|
if (tm0.mipmap_filter == MipMode::Point && lodFract >= 8)
|
|
|
|
baseMip++;
|
2010-03-09 04:38:07 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
if (mipLinear)
|
|
|
|
{
|
|
|
|
u8 sampledTex[4];
|
|
|
|
u32 texel[4];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
SampleMip(s, t, baseMip, linear, texmap, sampledTex);
|
|
|
|
SetTexel(sampledTex, texel, (16 - lodFract));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
SampleMip(s, t, baseMip + 1, linear, texmap, sampledTex);
|
|
|
|
AddTexel(sampledTex, texel, lodFract);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
sample[0] = (u8)(texel[0] >> 4);
|
|
|
|
sample[1] = (u8)(texel[1] >> 4);
|
|
|
|
sample[2] = (u8)(texel[2] >> 4);
|
|
|
|
sample[3] = (u8)(texel[3] >> 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
SampleMip(s, t, baseMip, linear, texmap, sample);
|
|
|
|
}
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
void SampleMip(s32 s, s32 t, s32 mip, bool linear, u8 texmap, u8* sample)
|
2009-10-12 00:48:24 +00:00
|
|
|
{
|
2021-10-09 19:16:15 +00:00
|
|
|
auto texUnit = bpmem.tex.GetUnit(texmap);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-10-09 19:16:15 +00:00
|
|
|
const TexMode0& tm0 = texUnit.texMode0;
|
|
|
|
const TexImage0& ti0 = texUnit.texImage0;
|
|
|
|
const TexTLUT& texTlut = texUnit.texTlut;
|
2021-02-11 02:11:31 +00:00
|
|
|
const TextureFormat texfmt = ti0.format;
|
|
|
|
const TLUTFormat tlutfmt = texTlut.tlut_format;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-09-11 23:56:38 +00:00
|
|
|
const u8* imageSrc;
|
|
|
|
const u8* imageSrcOdd = nullptr;
|
2021-10-09 19:16:15 +00:00
|
|
|
if (texUnit.texImage1.cache_manually_managed)
|
2012-11-03 14:36:40 +00:00
|
|
|
{
|
2021-10-09 19:16:15 +00:00
|
|
|
imageSrc = &texMem[texUnit.texImage1.tmem_even * TMEM_LINE_SIZE];
|
2017-07-30 19:45:55 +00:00
|
|
|
if (texfmt == TextureFormat::RGBA8)
|
2021-10-09 19:16:15 +00:00
|
|
|
imageSrcOdd = &texMem[texUnit.texImage2.tmem_odd * TMEM_LINE_SIZE];
|
2012-11-03 14:36:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-09 19:16:15 +00:00
|
|
|
const u32 imageBase = texUnit.texImage3.image_base << 5;
|
2012-11-03 14:36:40 +00:00
|
|
|
imageSrc = Memory::GetPointer(imageBase);
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-07-20 19:55:38 +00:00
|
|
|
int image_width_minus_1 = ti0.width;
|
|
|
|
int image_height_minus_1 = ti0.height;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-09-11 23:56:38 +00:00
|
|
|
const int tlutAddress = texTlut.tmem_offset << 9;
|
2014-08-10 19:28:42 +00:00
|
|
|
const u8* tlut = &texMem[tlutAddress];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
// reduce sample location and texture size to mip level
|
|
|
|
// move texture pointer to mip location
|
|
|
|
if (mip)
|
|
|
|
{
|
2021-07-20 19:55:38 +00:00
|
|
|
int mipWidth = image_width_minus_1 + 1;
|
|
|
|
int mipHeight = image_height_minus_1 + 1;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-09-11 23:56:38 +00:00
|
|
|
const int fmtWidth = TexDecoder_GetBlockWidthInTexels(texfmt);
|
|
|
|
const int fmtHeight = TexDecoder_GetBlockHeightInTexels(texfmt);
|
|
|
|
const int fmtDepth = TexDecoder_GetTexelSizeInNibbles(texfmt);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-07-20 19:55:38 +00:00
|
|
|
image_width_minus_1 >>= mip;
|
|
|
|
image_height_minus_1 >>= mip;
|
2010-03-09 04:38:07 +00:00
|
|
|
s >>= mip;
|
|
|
|
t >>= mip;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
while (mip)
|
|
|
|
{
|
2014-05-03 02:47:04 +00:00
|
|
|
mipWidth = std::max(mipWidth, fmtWidth);
|
|
|
|
mipHeight = std::max(mipHeight, fmtHeight);
|
2017-09-11 23:56:38 +00:00
|
|
|
const u32 size = (mipWidth * mipHeight * fmtDepth) >> 1;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
imageSrc += size;
|
|
|
|
mipWidth >>= 1;
|
|
|
|
mipHeight >>= 1;
|
|
|
|
mip--;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2010-03-09 04:38:07 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
if (linear)
|
|
|
|
{
|
2010-04-14 03:27:45 +00:00
|
|
|
// offset linear sampling
|
|
|
|
s -= 64;
|
|
|
|
t -= 64;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-04-14 03:27:45 +00:00
|
|
|
// integer part of sample location
|
|
|
|
int imageS = s >> 7;
|
|
|
|
int imageT = t >> 7;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-04-14 03:27:45 +00:00
|
|
|
// linear sampling
|
2010-03-09 04:38:07 +00:00
|
|
|
int imageSPlus1 = imageS + 1;
|
2017-09-11 23:56:38 +00:00
|
|
|
const int fractS = s & 0x7f;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-10-12 00:48:24 +00:00
|
|
|
int imageTPlus1 = imageT + 1;
|
2017-09-11 23:56:38 +00:00
|
|
|
const int fractT = t & 0x7f;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
u8 sampledTex[4];
|
2009-10-12 00:48:24 +00:00
|
|
|
u32 texel[4];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-07-20 19:55:38 +00:00
|
|
|
WrapCoord(&imageS, tm0.wrap_s, image_width_minus_1 + 1);
|
|
|
|
WrapCoord(&imageT, tm0.wrap_t, image_height_minus_1 + 1);
|
|
|
|
WrapCoord(&imageSPlus1, tm0.wrap_s, image_width_minus_1 + 1);
|
|
|
|
WrapCoord(&imageTPlus1, tm0.wrap_t, image_height_minus_1 + 1);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-10-09 19:16:15 +00:00
|
|
|
if (!(texfmt == TextureFormat::RGBA8 && texUnit.texImage1.cache_manually_managed))
|
2013-01-09 09:19:18 +00:00
|
|
|
{
|
2021-07-20 19:55:38 +00:00
|
|
|
TexDecoder_DecodeTexel(sampledTex, imageSrc, imageS, imageT, image_width_minus_1, texfmt,
|
|
|
|
tlut, tlutfmt);
|
2013-01-09 09:19:18 +00:00
|
|
|
SetTexel(sampledTex, texel, (128 - fractS) * (128 - fractT));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-07-20 19:55:38 +00:00
|
|
|
TexDecoder_DecodeTexel(sampledTex, imageSrc, imageSPlus1, imageT, image_width_minus_1, texfmt,
|
|
|
|
tlut, tlutfmt);
|
2013-01-09 09:19:18 +00:00
|
|
|
AddTexel(sampledTex, texel, (fractS) * (128 - fractT));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-07-20 19:55:38 +00:00
|
|
|
TexDecoder_DecodeTexel(sampledTex, imageSrc, imageS, imageTPlus1, image_width_minus_1, texfmt,
|
|
|
|
tlut, tlutfmt);
|
2013-01-09 09:19:18 +00:00
|
|
|
AddTexel(sampledTex, texel, (128 - fractS) * (fractT));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-07-20 19:55:38 +00:00
|
|
|
TexDecoder_DecodeTexel(sampledTex, imageSrc, imageSPlus1, imageTPlus1, image_width_minus_1,
|
|
|
|
texfmt, tlut, tlutfmt);
|
2013-01-09 09:19:18 +00:00
|
|
|
AddTexel(sampledTex, texel, (fractS) * (fractT));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TexDecoder_DecodeTexelRGBA8FromTmem(sampledTex, imageSrc, imageSrcOdd, imageS, imageT,
|
2021-07-20 19:55:38 +00:00
|
|
|
image_width_minus_1);
|
2013-01-09 09:19:18 +00:00
|
|
|
SetTexel(sampledTex, texel, (128 - fractS) * (128 - fractT));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-01-09 09:19:18 +00:00
|
|
|
TexDecoder_DecodeTexelRGBA8FromTmem(sampledTex, imageSrc, imageSrcOdd, imageSPlus1, imageT,
|
2021-07-20 19:55:38 +00:00
|
|
|
image_width_minus_1);
|
2013-01-09 09:19:18 +00:00
|
|
|
AddTexel(sampledTex, texel, (fractS) * (128 - fractT));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-01-09 09:19:18 +00:00
|
|
|
TexDecoder_DecodeTexelRGBA8FromTmem(sampledTex, imageSrc, imageSrcOdd, imageS, imageTPlus1,
|
2021-07-20 19:55:38 +00:00
|
|
|
image_width_minus_1);
|
2013-01-09 09:19:18 +00:00
|
|
|
AddTexel(sampledTex, texel, (128 - fractS) * (fractT));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-01-09 09:19:18 +00:00
|
|
|
TexDecoder_DecodeTexelRGBA8FromTmem(sampledTex, imageSrc, imageSrcOdd, imageSPlus1,
|
2021-07-20 19:55:38 +00:00
|
|
|
imageTPlus1, image_width_minus_1);
|
2013-01-09 09:19:18 +00:00
|
|
|
AddTexel(sampledTex, texel, (fractS) * (fractT));
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +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);
|
2009-10-12 00:48:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-04-14 03:27:45 +00:00
|
|
|
// integer part of sample location
|
|
|
|
int imageS = s >> 7;
|
|
|
|
int imageT = t >> 7;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-03-09 04:38:07 +00:00
|
|
|
// nearest neighbor sampling
|
2021-07-20 19:55:38 +00:00
|
|
|
WrapCoord(&imageS, tm0.wrap_s, image_width_minus_1 + 1);
|
|
|
|
WrapCoord(&imageT, tm0.wrap_t, image_height_minus_1 + 1);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-10-09 19:16:15 +00:00
|
|
|
if (!(texfmt == TextureFormat::RGBA8 && texUnit.texImage1.cache_manually_managed))
|
2021-07-20 19:55:38 +00:00
|
|
|
TexDecoder_DecodeTexel(sample, imageSrc, imageS, imageT, image_width_minus_1, texfmt, tlut,
|
|
|
|
tlutfmt);
|
2013-01-09 09:19:18 +00:00
|
|
|
else
|
|
|
|
TexDecoder_DecodeTexelRGBA8FromTmem(sample, imageSrc, imageSrcOdd, imageS, imageT,
|
2021-07-20 19:55:38 +00:00
|
|
|
image_width_minus_1);
|
2009-10-12 00:48:24 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace TextureSampler
|