samplerjit: Pass u/v coords as vector.

This commit is contained in:
Unknown W. Brackets committed 2021-12-27 11:37:32 -08:00
1 parent cc767622d0
commit b00a66e34c
5 files changed
+109 -52

No files matched your search

+66 -29
View File
@@ -149,26 +149,56 @@ static inline void ApplyTexelClamp(int out_u[N], int out_v[N], const int u[N], c
}
}
template <int N>
static inline void ApplyTexelClampQuad(int out_u[N * 4], int out_v[N * 4], const int u[N], const int v[N], int width, int height) {
static inline Vec4IntResult SOFTRAST_CALL ApplyTexelClampQuad(bool clamp, Vec4IntArg vec, int width) {
Vec4<int> result = vec;
#ifdef _M_SSE
if (clamp) {
// First, clamp to zero.
__m128i negmask = _mm_cmpgt_epi32(_mm_setzero_si128(), result.ivec);
result.ivec = _mm_andnot_si128(negmask, result.ivec);
// Now the high bound.
__m128i bound = _mm_set1_epi32(width - 1);
__m128i goodmask = _mm_cmpgt_epi32(bound, result.ivec);
// Clear the ones that were too high, then or in the high bound to those.
result.ivec = _mm_and_si128(goodmask, result.ivec);
result.ivec = _mm_or_si128(result.ivec, _mm_andnot_si128(goodmask, bound));
} else {
result.ivec = _mm_and_si128(result.ivec, _mm_set1_epi32(width - 1));
}
#else
if (gstate.isTexCoordClampedS()) {
for (int i = 0; i < N * 4; ++i) {
out_u[i] = ClampUV(u[i >> 2] + (i & 1), width);
for (int i = 0; i < 4; ++i) {
result[i] = ClampUV(result[i], width);
}
} else {
for (int i = 0; i < N * 4; ++i) {
out_u[i] = WrapUV(u[i >> 2] + (i & 1), width);
}
}
if (gstate.isTexCoordClampedT()) {
for (int i = 0; i < N * 4; ++i) {
out_v[i] = ClampUV(v[i >> 2] + ((i >> 1) & 1), height);
}
} else {
for (int i = 0; i < N * 4; ++i) {
out_v[i] = WrapUV(v[i >> 2] + ((i >> 1) & 1), height);
for (int i = 0; i < 4; ++i) {
result[i] = WrapUV(result[i], width);
}
}
#endif
return ToVec4IntResult(result);
}
static inline Vec4IntResult SOFTRAST_CALL ApplyTexelClampQuadS(bool clamp, int u, int width) {
#ifdef _M_SSE
__m128i uvec = _mm_add_epi32(_mm_set1_epi32(u), _mm_set_epi32(1, 0, 1, 0));
return ApplyTexelClampQuad(clamp, uvec, width);
#else
Vec4<int> result = Vec4<int>::AssignToAll(u) + Vec4<int>(0, 1, 0, 1);
return ApplyTexelClampQuad(clamp, ToVec4IntArg(result), width);
#endif
}
static inline Vec4IntResult SOFTRAST_CALL ApplyTexelClampQuadT(bool clamp, int v, int height) {
#ifdef _M_SSE
__m128i vvec = _mm_add_epi32(_mm_set1_epi32(v), _mm_set_epi32(1, 1, 0, 0));
return ApplyTexelClampQuad(clamp, vvec, height);
#else
Vec4<int> result = Vec4<int>::AssignToAll(v) + Vec4<int>(0, 0, 1, 1);
return ApplyTexelClampQuad(clamp, ToVec4IntArg(result), height);
#endif
}
static inline void GetTexelCoordinates(int level, float s, float t, int& out_u, int& out_v)
@@ -185,23 +215,26 @@ static inline void GetTexelCoordinates(int level, float s, float t, int& out_u,
ApplyTexelClamp<1>(&out_u, &out_v, &base_u, &base_v, width, height);
}
static inline void GetTexelCoordinatesQuad(int level, float in_s, float in_t, int u[4], int v[4], int &frac_u, int &frac_v)
{
// 8 bits of fractional UV
static inline Vec4IntResult SOFTRAST_CALL GetTexelCoordinatesQuadS(int level, float in_s, int &frac_u) {
int width = gstate.getTextureWidth(level);
int height = gstate.getTextureHeight(level);
int base_u = (int)(in_s * width * 256.0f + 0.375f) - 128;
int base_v = (int)(in_t * height * 256.0f + 0.375f) - 128;
frac_u = (int)(base_u) & 0xff;
frac_v = (int)(base_v) & 0xff;
base_u >>= 8;
// Need to generate and individually wrap/clamp the four sample coordinates. Ugh.
return ApplyTexelClampQuadS(gstate.isTexCoordClampedS(), base_u, width);
}
static inline Vec4IntResult SOFTRAST_CALL GetTexelCoordinatesQuadT(int level, float in_t, int &frac_v) {
int height = gstate.getTextureHeight(level);
int base_v = (int)(in_t * height * 256.0f + 0.375f) - 128;
frac_v = (int)(base_v) & 0xff;
base_v >>= 8;
// Need to generate and individually wrap/clamp the four sample coordinates. Ugh.
ApplyTexelClampQuad<1>(u, v, &base_u, &base_v, width, height);
return ApplyTexelClampQuadT(gstate.isTexCoordClampedT(), base_v, height);
}
static inline void GetTextureCoordinates(const VertexData& v0, const VertexData& v1, const float p, float &s, float &t) {
@@ -562,7 +595,6 @@ Vec3<int> AlphaBlendingResult(const PixelFuncID &pixelID, const Vec4<int> &sourc
template <bool mayHaveMipLevels>
static inline Vec4IntResult SOFTRAST_CALL ApplyTexturing(Sampler::Funcs sampler, Vec4IntArg prim_color, float s, float t, int texlevel, int frac_texlevel, bool bilinear, u8 *texptr[], int texbufw[]) {
int u[8] = {0}, v[8] = {0}; // 1.23.8 fixed point
int frac_u[2], frac_v[2];
Vec4<int> texcolor0;
@@ -573,6 +605,8 @@ static inline Vec4IntResult SOFTRAST_CALL ApplyTexturing(Sampler::Funcs sampler,
int bufw1 = texbufw[mayHaveMipLevels ? texlevel + 1 : 0];
if (!bilinear) {
int u[8] = { 0 }, v[8] = { 0 }; // 1.23.8 fixed point
// Nearest filtering only. Round texcoords.
GetTexelCoordinates(mayHaveMipLevels ? texlevel : 0, s, t, u[0], v[0]);
if (mayHaveMipLevels && frac_texlevel) {
@@ -584,14 +618,17 @@ static inline Vec4IntResult SOFTRAST_CALL ApplyTexturing(Sampler::Funcs sampler,
texcolor1 = Vec4<int>(sampler.nearest(u[1], v[1], tptr1, bufw1, texlevel + 1));
}
} else {
GetTexelCoordinatesQuad(mayHaveMipLevels ? texlevel : 0, s, t, u, v, frac_u[0], frac_v[0]);
Vec4IntResult u = GetTexelCoordinatesQuadS(mayHaveMipLevels ? texlevel : 0, s, frac_u[0]);
Vec4IntResult v = GetTexelCoordinatesQuadT(mayHaveMipLevels ? texlevel : 0, t, frac_v[0]);
Vec4IntResult u1, v1;
if (mayHaveMipLevels && frac_texlevel) {
GetTexelCoordinatesQuad(texlevel + 1, s, t, u + 4, v + 4, frac_u[1], frac_v[1]);
u1 = GetTexelCoordinatesQuadS(texlevel + 1, s, frac_u[1]);
v1 = GetTexelCoordinatesQuadT(texlevel + 1, t, frac_v[1]);
}
texcolor0 = Vec4<int>(sampler.linear(u, v, frac_u[0], frac_v[0], tptr0, bufw0, mayHaveMipLevels ? texlevel : 0));
texcolor0 = Vec4<int>(sampler.linear(ToVec4IntArg(u), ToVec4IntArg(v), frac_u[0], frac_v[0], tptr0, bufw0, mayHaveMipLevels ? texlevel : 0));
if (mayHaveMipLevels && frac_texlevel) {
texcolor1 = Vec4<int>(sampler.linear(u + 4, v + 4, frac_u[1], frac_v[1], tptr1, bufw1, texlevel + 1));
texcolor1 = Vec4<int>(sampler.linear(ToVec4IntArg(u1), ToVec4IntArg(v1), frac_u[1], frac_v[1], tptr1, bufw1, texlevel + 1));
}
}