From bf2e0607356e7cf216a4078b340f2c63422581ea Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 15 Jan 2022 15:28:07 -0800 Subject: [PATCH] softgpu: Move c++ tex func to sampler. It's not used anywhere else now. --- GPU/Software/Rasterizer.cpp | 108 ------------------------------------ GPU/Software/Rasterizer.h | 1 - GPU/Software/Sampler.cpp | 106 +++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 109 deletions(-) diff --git a/GPU/Software/Rasterizer.cpp b/GPU/Software/Rasterizer.cpp index 23924937d6..2d28c22d4c 100644 --- a/GPU/Software/Rasterizer.cpp +++ b/GPU/Software/Rasterizer.cpp @@ -226,114 +226,6 @@ static inline bool IsRightSideOrFlatBottomLine(const Vec2& vertex, const Ve } } -Vec4IntResult SOFTRAST_CALL GetTextureFunctionOutput(Vec4IntArg prim_color_in, Vec4IntArg texcolor_in, const SamplerID &samplerID) { - const Vec4 prim_color = prim_color_in; - const Vec4 texcolor = texcolor_in; - - Vec3 out_rgb; - int out_a; - - bool rgba = samplerID.useTextureAlpha; - - switch (samplerID.TexFunc()) { - case GE_TEXFUNC_MODULATE: - { -#if defined(_M_SSE) - // Modulate weights slightly on the tex color, by adding one to prim and dividing by 256. - const __m128i p = _mm_slli_epi16(_mm_packs_epi32(prim_color.ivec, prim_color.ivec), 4); - const __m128i pboost = _mm_add_epi16(p, _mm_set1_epi16(1 << 4)); - __m128i t = _mm_slli_epi16(_mm_packs_epi32(texcolor.ivec, texcolor.ivec), 4); - if (samplerID.useColorDoubling) { - const __m128i amask = _mm_set_epi16(-1, 0, 0, 0, -1, 0, 0, 0); - const __m128i a = _mm_and_si128(t, amask); - const __m128i rgb = _mm_andnot_si128(amask, t); - t = _mm_or_si128(_mm_slli_epi16(rgb, 1), a); - } - const __m128i b = _mm_mulhi_epi16(pboost, t); - out_rgb.ivec = _mm_unpacklo_epi16(b, _mm_setzero_si128()); - - if (rgba) { - return ToVec4IntResult(Vec4(out_rgb.ivec)); - } else { - out_a = prim_color.a(); - } -#else - if (samplerID.useColorDoubling) { - out_rgb = ((prim_color.rgb() + Vec3::AssignToAll(1)) * texcolor.rgb() * 2) / 256; - } else { - out_rgb = (prim_color.rgb() + Vec3::AssignToAll(1)) * texcolor.rgb() / 256; - } - out_a = (rgba) ? ((prim_color.a() + 1) * texcolor.a() / 256) : prim_color.a(); -#endif - break; - } - - case GE_TEXFUNC_DECAL: - { - if (rgba) { - int t = texcolor.a(); - int invt = 255 - t; - // Both colors are boosted here, making the alpha have more weight. - Vec3 one = Vec3::AssignToAll(1); - out_rgb = ((prim_color.rgb() + one) * invt + (texcolor.rgb() + one) * t); - // Keep the bits of accuracy when doubling. - if (samplerID.useColorDoubling) - out_rgb /= 128; - else - out_rgb /= 256; - } else { - if (samplerID.useColorDoubling) - out_rgb = texcolor.rgb() * 2; - else - out_rgb = texcolor.rgb(); - } - out_a = prim_color.a(); - break; - } - - case GE_TEXFUNC_BLEND: - { - const Vec3 const255(255, 255, 255); - const Vec3 texenv(gstate.getTextureEnvColR(), gstate.getTextureEnvColG(), gstate.getTextureEnvColB()); - - // Unlike the others (and even alpha), this one simply always rounds up. - const Vec3 roundup = Vec3::AssignToAll(255); - out_rgb = ((const255 - texcolor.rgb()) * prim_color.rgb() + texcolor.rgb() * texenv + roundup); - // Must divide by less to keep the precision for doubling to be accurate. - if (samplerID.useColorDoubling) - out_rgb /= 128; - else - out_rgb /= 256; - - out_a = (rgba) ? ((prim_color.a() + 1) * texcolor.a() / 256) : prim_color.a(); - break; - } - - case GE_TEXFUNC_REPLACE: - out_rgb = texcolor.rgb(); - // Doubling even happens for replace. - if (samplerID.useColorDoubling) - out_rgb *= 2; - out_a = (rgba) ? texcolor.a() : prim_color.a(); - break; - - case GE_TEXFUNC_ADD: - case GE_TEXFUNC_UNKNOWN1: - case GE_TEXFUNC_UNKNOWN2: - case GE_TEXFUNC_UNKNOWN3: - // Don't need to clamp afterward, we always clamp before tests. - out_rgb = prim_color.rgb() + texcolor.rgb(); - if (samplerID.useColorDoubling) - out_rgb *= 2; - - // Alpha is still blended the common way. - out_a = (rgba) ? ((prim_color.a() + 1) * texcolor.a() / 256) : prim_color.a(); - break; - } - - return ToVec4IntResult(Vec4(out_rgb, out_a)); -} - static inline Vec3 GetSourceFactor(GEBlendSrcFactor factor, const Vec4 &source, const Vec4 &dst, uint32_t fix) { switch (factor) { case GE_SRCBLEND_DSTCOLOR: diff --git a/GPU/Software/Rasterizer.h b/GPU/Software/Rasterizer.h index 26829201ad..5b7776ff58 100644 --- a/GPU/Software/Rasterizer.h +++ b/GPU/Software/Rasterizer.h @@ -78,6 +78,5 @@ bool GetCurrentTexture(GPUDebugBuffer &buffer, int level); // Shared functions with RasterizerRectangle.cpp Vec3 AlphaBlendingResult(const PixelFuncID &pixelID, const Vec4 &source, const Vec4 &dst); -Vec4IntResult SOFTRAST_CALL GetTextureFunctionOutput(Vec4IntArg prim_color, Vec4IntArg texcolor, const SamplerID &samplerID); } // namespace Rasterizer diff --git a/GPU/Software/Sampler.cpp b/GPU/Software/Sampler.cpp index 6e2865a2c2..a35148fd20 100644 --- a/GPU/Software/Sampler.cpp +++ b/GPU/Software/Sampler.cpp @@ -443,6 +443,112 @@ 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, samplerID); } +Vec4IntResult SOFTRAST_CALL GetTextureFunctionOutput(Vec4IntArg prim_color_in, Vec4IntArg texcolor_in, const SamplerID &samplerID) { + const Vec4 prim_color = prim_color_in; + const Vec4 texcolor = texcolor_in; + + Vec3 out_rgb; + int out_a; + + bool rgba = samplerID.useTextureAlpha; + + switch (samplerID.TexFunc()) { + case GE_TEXFUNC_MODULATE: + { +#if defined(_M_SSE) + // Modulate weights slightly on the tex color, by adding one to prim and dividing by 256. + const __m128i p = _mm_slli_epi16(_mm_packs_epi32(prim_color.ivec, prim_color.ivec), 4); + const __m128i pboost = _mm_add_epi16(p, _mm_set1_epi16(1 << 4)); + __m128i t = _mm_slli_epi16(_mm_packs_epi32(texcolor.ivec, texcolor.ivec), 4); + if (samplerID.useColorDoubling) { + const __m128i amask = _mm_set_epi16(-1, 0, 0, 0, -1, 0, 0, 0); + const __m128i a = _mm_and_si128(t, amask); + const __m128i rgb = _mm_andnot_si128(amask, t); + t = _mm_or_si128(_mm_slli_epi16(rgb, 1), a); + } + const __m128i b = _mm_mulhi_epi16(pboost, t); + out_rgb.ivec = _mm_unpacklo_epi16(b, _mm_setzero_si128()); + + if (rgba) { + return ToVec4IntResult(Vec4(out_rgb.ivec)); + } else { + out_a = prim_color.a(); + } +#else + if (samplerID.useColorDoubling) { + out_rgb = ((prim_color.rgb() + Vec3::AssignToAll(1)) * texcolor.rgb() * 2) / 256; + } else { + out_rgb = (prim_color.rgb() + Vec3::AssignToAll(1)) * texcolor.rgb() / 256; + } + out_a = (rgba) ? ((prim_color.a() + 1) * texcolor.a() / 256) : prim_color.a(); +#endif + break; + } + + case GE_TEXFUNC_DECAL: + if (rgba) { + int t = texcolor.a(); + int invt = 255 - t; + // Both colors are boosted here, making the alpha have more weight. + Vec3 one = Vec3::AssignToAll(1); + out_rgb = ((prim_color.rgb() + one) * invt + (texcolor.rgb() + one) * t); + // Keep the bits of accuracy when doubling. + if (samplerID.useColorDoubling) + out_rgb /= 128; + else + out_rgb /= 256; + } else { + if (samplerID.useColorDoubling) + out_rgb = texcolor.rgb() * 2; + else + out_rgb = texcolor.rgb(); + } + out_a = prim_color.a(); + break; + + case GE_TEXFUNC_BLEND: + { + const Vec3 const255(255, 255, 255); + const Vec3 texenv(gstate.getTextureEnvColR(), gstate.getTextureEnvColG(), gstate.getTextureEnvColB()); + + // Unlike the others (and even alpha), this one simply always rounds up. + const Vec3 roundup = Vec3::AssignToAll(255); + out_rgb = ((const255 - texcolor.rgb()) * prim_color.rgb() + texcolor.rgb() * texenv + roundup); + // Must divide by less to keep the precision for doubling to be accurate. + if (samplerID.useColorDoubling) + out_rgb /= 128; + else + out_rgb /= 256; + + out_a = (rgba) ? ((prim_color.a() + 1) * texcolor.a() / 256) : prim_color.a(); + break; + } + + case GE_TEXFUNC_REPLACE: + out_rgb = texcolor.rgb(); + // Doubling even happens for replace. + if (samplerID.useColorDoubling) + out_rgb *= 2; + out_a = (rgba) ? texcolor.a() : prim_color.a(); + break; + + case GE_TEXFUNC_ADD: + case GE_TEXFUNC_UNKNOWN1: + case GE_TEXFUNC_UNKNOWN2: + case GE_TEXFUNC_UNKNOWN3: + // Don't need to clamp afterward, we always clamp before tests. + out_rgb = prim_color.rgb() + texcolor.rgb(); + if (samplerID.useColorDoubling) + out_rgb *= 2; + + // Alpha is still blended the common way. + out_a = (rgba) ? ((prim_color.a() + 1) * texcolor.a() / 256) : prim_color.a(); + break; + } + + return ToVec4IntResult(Vec4(out_rgb, out_a)); +} + static Vec4IntResult SOFTRAST_CALL SampleNearest(float s, float t, int x, int y, Vec4IntArg prim_color, const u8 *const *tptr, const int *bufw, int level, int levelFrac, const SamplerID &samplerID) { int u, v;