| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/TextureDecoder.h" |
| 5 | |
| 6 | #ifdef CHECK |
| 7 | #include "Common/Assert.h" |
| 8 | #endif |
| 9 | |
| 10 | #include "Common/CPUDetect.h" |
| 11 | #include "Common/CommonTypes.h" |
| 12 | #include "Common/Intrinsics.h" |
| 13 | #include "Common/MsgHandler.h" |
| 14 | #include "Common/Swap.h" |
| 15 | |
| 16 | #include "VideoCommon/LookUpTables.h" |
| 17 | #include "VideoCommon/TextureDecoder_Util.h" |
| 18 | |
| 19 | // GameCube/Wii texture decoder |
| 20 | |
| 21 | // Decodes all known GameCube/Wii texture formats. |
| 22 | // by ector |
| 23 | |
| 24 | static inline u32 DecodePixel_IA8(u16 val) |
| 25 | { |
| 26 | int a = val & 0xFF; |
| 27 | int i = val >> 8; |
| 28 | return i | (i << 8) | (i << 16) | (a << 24); |
| 29 | } |
| 30 | |
| 31 | static inline u32 DecodePixel_RGB565(u16 val) |
| 32 | { |
| 33 | int r, g, b, a; |
| 34 | r = Convert5To8((val >> 11) & 0x1f); |
| 35 | g = Convert6To8((val >> 5) & 0x3f); |
| 36 | b = Convert5To8((val) & 0x1f); |
| 37 | a = 0xFF; |
| 38 | return r | (g << 8) | (b << 16) | (a << 24); |
| 39 | } |
| 40 | |
| 41 | static inline u32 DecodePixel_RGB5A3(u16 val) |
| 42 | { |
| 43 | int r, g, b, a; |
| 44 | if ((val & 0x8000)) |
| 45 | { |
| 46 | r = Convert5To8((val >> 10) & 0x1f); |
| 47 | g = Convert5To8((val >> 5) & 0x1f); |
| 48 | b = Convert5To8((val) & 0x1f); |
| 49 | a = 0xFF; |
| 50 | } |
| 51 | else |
| 52 | { |
| 53 | a = Convert3To8((val >> 12) & 0x7); |
| 54 | r = Convert4To8((val >> 8) & 0xf); |
| 55 | g = Convert4To8((val >> 4) & 0xf); |
| 56 | b = Convert4To8((val) & 0xf); |
| 57 | } |
| 58 | return r | (g << 8) | (b << 16) | (a << 24); |
| 59 | } |
| 60 | |
| 61 | static inline void DecodeBytes_C4_IA8(u32* dst, const u8* src, const u8* tlut_) |
| 62 | { |
| 63 | const u16* tlut = (u16*)tlut_; |
| 64 | for (int x = 0; x < 4; x++) |
| 65 | { |
| 66 | u8 val = src[x]; |
| 67 | *dst++ = DecodePixel_IA8(tlut[val >> 4]); |
| 68 | *dst++ = DecodePixel_IA8(tlut[val & 0xF]); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static inline void DecodeBytes_C4_RGB565(u32* dst, const u8* src, const u8* tlut_) |
| 73 | { |
| 74 | const u16* tlut = (u16*)tlut_; |
| 75 | for (int x = 0; x < 4; x++) |
| 76 | { |
| 77 | u8 val = src[x]; |
| 78 | *dst++ = DecodePixel_RGB565(Common::swap16(tlut[val >> 4])); |
| 79 | *dst++ = DecodePixel_RGB565(Common::swap16(tlut[val & 0xF])); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static inline void DecodeBytes_C4_RGB5A3(u32* dst, const u8* src, const u8* tlut_) |
| 84 | { |
| 85 | const u16* tlut = (u16*)tlut_; |
| 86 | for (int x = 0; x < 4; x++) |
| 87 | { |
| 88 | u8 val = src[x]; |
| 89 | *dst++ = DecodePixel_RGB5A3(Common::swap16(tlut[val >> 4])); |
| 90 | *dst++ = DecodePixel_RGB5A3(Common::swap16(tlut[val & 0xF])); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static inline void DecodeBytes_C8_IA8(u32* dst, const u8* src, const u8* tlut_) |
| 95 | { |
| 96 | const u16* tlut = (u16*)tlut_; |
| 97 | for (int x = 0; x < 8; x++) |
| 98 | { |
| 99 | *dst++ = DecodePixel_IA8(tlut[src[x]]); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | static inline void DecodeBytes_C8_RGB565(u32* dst, const u8* src, const u8* tlut_) |
| 104 | { |
| 105 | const u16* tlut = (u16*)tlut_; |
| 106 | for (int x = 0; x < 8; x++) |
| 107 | { |
| 108 | u8 val = src[x]; |
| 109 | *dst++ = DecodePixel_RGB565(Common::swap16(tlut[val])); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | static inline void DecodeBytes_C8_RGB5A3(u32* dst, const u8* src, const u8* tlut_) |
| 114 | { |
| 115 | const u16* tlut = (u16*)tlut_; |
| 116 | for (int x = 0; x < 8; x++) |
| 117 | { |
| 118 | u8 val = src[x]; |
| 119 | *dst++ = DecodePixel_RGB5A3(Common::swap16(tlut[val])); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | static inline void DecodeBytes_C14X2_IA8(u32* dst, const u16* src, const u8* tlut_) |
| 124 | { |
| 125 | const u16* tlut = (u16*)tlut_; |
| 126 | for (int x = 0; x < 4; x++) |
| 127 | { |
| 128 | u16 val = Common::swap16(src[x]); |
| 129 | *dst++ = DecodePixel_IA8(tlut[(val & 0x3FFF)]); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static inline void DecodeBytes_C14X2_RGB565(u32* dst, const u16* src, const u8* tlut_) |
| 134 | { |
| 135 | const u16* tlut = (u16*)tlut_; |
| 136 | for (int x = 0; x < 4; x++) |
| 137 | { |
| 138 | u16 val = Common::swap16(src[x]); |
| 139 | *dst++ = DecodePixel_RGB565(Common::swap16(tlut[(val & 0x3FFF)])); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static inline void DecodeBytes_C14X2_RGB5A3(u32* dst, const u16* src, const u8* tlut_) |
| 144 | { |
| 145 | const u16* tlut = (u16*)tlut_; |
| 146 | for (int x = 0; x < 4; x++) |
| 147 | { |
| 148 | u16 val = Common::swap16(src[x]); |
| 149 | *dst++ = DecodePixel_RGB5A3(Common::swap16(tlut[(val & 0x3FFF)])); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static inline void DecodeBytes_IA4(u32* dst, const u8* src) |
| 154 | { |
| 155 | for (int x = 0; x < 8; x++) |
| 156 | { |
| 157 | const u8 val = src[x]; |
| 158 | u8 a = Convert4To8(val >> 4); |
| 159 | u8 l = Convert4To8(val & 0xF); |
| 160 | dst[x] = (a << 24) | l << 16 | l << 8 | l; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | #ifdef CHECK |
| 165 | static void DecodeDXTBlock(u32* dst, const DXTBlock* src, int pitch) |
| 166 | { |
| 167 | // S3TC Decoder (Note: GCN decodes differently from PC so we can't use native support) |
| 168 | // Needs more speed. |
| 169 | u16 c1 = Common::swap16(src->color1); |
| 170 | u16 c2 = Common::swap16(src->color2); |
| 171 | int blue1 = Convert5To8(c1 & 0x1F); |
| 172 | int blue2 = Convert5To8(c2 & 0x1F); |
| 173 | int green1 = Convert6To8((c1 >> 5) & 0x3F); |
| 174 | int green2 = Convert6To8((c2 >> 5) & 0x3F); |
| 175 | int red1 = Convert5To8((c1 >> 11) & 0x1F); |
| 176 | int red2 = Convert5To8((c2 >> 11) & 0x1F); |
| 177 | int colors[4]; |
| 178 | colors[0] = MakeRGBA(red1, green1, blue1, 255); |
| 179 | colors[1] = MakeRGBA(red2, green2, blue2, 255); |
| 180 | if (c1 > c2) |
| 181 | { |
| 182 | colors[2] = |
| 183 | MakeRGBA(DXTBlend(red2, red1), DXTBlend(green2, green1), DXTBlend(blue2, blue1), 255); |
| 184 | colors[3] = |
| 185 | MakeRGBA(DXTBlend(red1, red2), DXTBlend(green1, green2), DXTBlend(blue1, blue2), 255); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | // color[3] is the same as color[2] (average of both colors), but transparent. |
| 190 | // This differs from DXT1 where color[3] is transparent black. |
| 191 | colors[2] = MakeRGBA((red1 + red2) / 2, (green1 + green2) / 2, (blue1 + blue2) / 2, 255); |
| 192 | colors[3] = MakeRGBA((red1 + red2) / 2, (green1 + green2) / 2, (blue1 + blue2) / 2, 0); |
| 193 | } |
| 194 | |
| 195 | for (int y = 0; y < 4; y++) |
| 196 | { |
| 197 | int val = src->lines[y]; |
| 198 | for (int x = 0; x < 4; x++) |
| 199 | { |
| 200 | dst[x] = colors[(val >> 6) & 3]; |
| 201 | val <<= 2; |
| 202 | } |
| 203 | dst += pitch; |
| 204 | } |
| 205 | } |
| 206 | #endif |
| 207 | |
| 208 | // JSD 01/06/11: |
| 209 | // TODO: we really should ensure BOTH the source and destination addresses are aligned to 16-byte |
| 210 | // boundaries to squeeze out a little more performance. _mm_loadu_si128/_mm_storeu_si128 is slower |
| 211 | // than _mm_load_si128/_mm_store_si128 because they work on unaligned addresses. The processor is |
| 212 | // free to make the assumption that addresses are multiples of 16 in the aligned case. |
| 213 | // TODO: complete SSE2 optimization of less often used texture formats. |
| 214 | // TODO: refactor algorithms using _mm_loadl_epi64 unaligned loads to prefer 128-bit aligned loads. |
| 215 | static void TexDecoder_DecodeImpl_C4(u32* dst, const u8* src, int width, int height, |
| 216 | TextureFormat texformat, const u8* tlut, TLUTFormat tlutfmt, |
| 217 | int Wsteps4, int Wsteps8) |
| 218 | { |
| 219 | switch (tlutfmt) |
| 220 | { |
| 221 | case TLUTFormat::RGB5A3: |
| 222 | { |
| 223 | for (int y = 0; y < height; y += 8) |
| 224 | for (int x = 0, yStep = (y / 8) * Wsteps8; x < width; x += 8, yStep++) |
| 225 | for (int iy = 0, xStep = 8 * yStep; iy < 8; iy++, xStep++) |
| 226 | DecodeBytes_C4_RGB5A3(dst + (y + iy) * width + x, src + 4 * xStep, tlut); |
| 227 | } |
| 228 | break; |
| 229 | |
| 230 | case TLUTFormat::IA8: |
| 231 | { |
| 232 | for (int y = 0; y < height; y += 8) |
| 233 | for (int x = 0, yStep = (y / 8) * Wsteps8; x < width; x += 8, yStep++) |
| 234 | for (int iy = 0, xStep = 8 * yStep; iy < 8; iy++, xStep++) |
| 235 | DecodeBytes_C4_IA8(dst + (y + iy) * width + x, src + 4 * xStep, tlut); |
| 236 | } |
| 237 | break; |
| 238 | |
| 239 | case TLUTFormat::RGB565: |
| 240 | { |
| 241 | for (int y = 0; y < height; y += 8) |
| 242 | for (int x = 0, yStep = (y / 8) * Wsteps8; x < width; x += 8, yStep++) |
| 243 | for (int iy = 0, xStep = 8 * yStep; iy < 8; iy++, xStep++) |
| 244 | DecodeBytes_C4_RGB565(dst + (y + iy) * width + x, src + 4 * xStep, tlut); |
| 245 | } |
| 246 | break; |
| 247 | |
| 248 | default: |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | FUNCTION_TARGET_SSSE3 |
| 254 | static void TexDecoder_DecodeImpl_I4_SSSE3(u32* dst, const u8* src, int width, int height, |
| 255 | TextureFormat texformat, const u8* tlut, |
| 256 | TLUTFormat tlutfmt, int Wsteps4, int Wsteps8) |
| 257 | { |
| 258 | const __m128i kMask_x0f = _mm_set1_epi32(0x0f0f0f0fL); |
| 259 | const __m128i kMask_xf0 = _mm_set1_epi32(0xf0f0f0f0L); |
| 260 | |
| 261 | // xsacha optimized with SSSE3 intrinsics |
| 262 | // Produces a ~40% speed improvement over SSE2 implementation |
| 263 | const __m128i mask9180 = _mm_set_epi8(9, 9, 9, 9, 1, 1, 1, 1, 8, 8, 8, 8, 0, 0, 0, 0); |
| 264 | const __m128i maskB3A2 = _mm_set_epi8(11, 11, 11, 11, 3, 3, 3, 3, 10, 10, 10, 10, 2, 2, 2, 2); |
| 265 | const __m128i maskD5C4 = _mm_set_epi8(13, 13, 13, 13, 5, 5, 5, 5, 12, 12, 12, 12, 4, 4, 4, 4); |
| 266 | const __m128i maskF7E6 = _mm_set_epi8(15, 15, 15, 15, 7, 7, 7, 7, 14, 14, 14, 14, 6, 6, 6, 6); |
| 267 | for (int y = 0; y < height; y += 8) |
| 268 | { |
| 269 | for (int x = 0, yStep = (y / 8) * Wsteps8; x < width; x += 8, yStep++) |
| 270 | { |
| 271 | for (int iy = 0, xStep = 4 * yStep; iy < 8; iy += 2, xStep++) |
| 272 | { |
| 273 | const __m128i r0 = _mm_loadl_epi64((const __m128i*)(src + 8 * xStep)); |
| 274 | // We want the hi 4 bits of each 8-bit word replicated to 32-bit words: |
| 275 | // (00000000 00000000 HhGgFfEe DdCcBbAa) -> (00000000 00000000 HHGGFFEE DDCCBBAA) |
| 276 | const __m128i i1 = _mm_and_si128(r0, kMask_xf0); |
| 277 | const __m128i i11 = _mm_or_si128(i1, _mm_srli_epi16(i1, 4)); |
| 278 | |
| 279 | // Now we do same as above for the second half of the byte |
| 280 | const __m128i i2 = _mm_and_si128(r0, kMask_x0f); |
| 281 | const __m128i i22 = _mm_or_si128(i2, _mm_slli_epi16(i2, 4)); |
| 282 | |
| 283 | // Combine both sides |
| 284 | const __m128i base = _mm_unpacklo_epi64(i11, i22); |
| 285 | // Achieve the pattern visible in the masks. |
| 286 | const __m128i o1 = _mm_shuffle_epi8(base, mask9180); |
| 287 | const __m128i o2 = _mm_shuffle_epi8(base, maskB3A2); |
| 288 | const __m128i o3 = _mm_shuffle_epi8(base, maskD5C4); |
| 289 | const __m128i o4 = _mm_shuffle_epi8(base, maskF7E6); |
| 290 | |
| 291 | // Write row 0: |
| 292 | _mm_storeu_si128((__m128i*)(dst + (y + iy) * width + x), o1); |
| 293 | _mm_storeu_si128((__m128i*)(dst + (y + iy) * width + x + 4), o2); |
| 294 | // Write row 1: |
| 295 | _mm_storeu_si128((__m128i*)(dst + (y + iy + 1) * width + x), o3); |
| 296 | _mm_storeu_si128((__m128i*)(dst + (y + iy + 1) * width + x + 4), o4); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | static void TexDecoder_DecodeImpl_I4(u32* dst, const u8* src, int width, int height, |
| 303 | TextureFormat texformat, const u8* tlut, TLUTFormat tlutfmt, |
| 304 | int Wsteps4, int Wsteps8) |
| 305 | { |
| 306 | const __m128i kMask_x0f = _mm_set1_epi32(0x0f0f0f0fL); |
| 307 | const __m128i kMask_xf0 = _mm_set1_epi32(0xf0f0f0f0L); |
| 308 | |
| 309 | // JSD optimized with SSE2 intrinsics. |
| 310 | // Produces a ~76% speed improvement over reference C implementation. |
| 311 | for (int y = 0; y < height; y += 8) |
| 312 | { |
| 313 | for (int x = 0, yStep = (y / 8) * Wsteps8; x < width; x += 8, yStep++) |
| 314 | { |
| 315 | for (int iy = 0, xStep = 4 * yStep; iy < 8; iy += 2, xStep++) |
| 316 | { |
| 317 | const __m128i r0 = _mm_loadl_epi64((const __m128i*)(src + 8 * xStep)); |
| 318 | // Shuffle low 64-bits with itself to expand from (0000 0000 hgfe dcba) to (hhgg ffee |
| 319 | // ddcc bbaa) |
| 320 | const __m128i r1 = _mm_unpacklo_epi8(r0, r0); |
| 321 | |
| 322 | // We want the hi 4 bits of each 8-bit word replicated to 32-bit words: |
| 323 | // (HhHhGgGg FfFfEeEe DdDdCcCc BbBbAaAa) & kMask_xf0 -> (H0H0G0G0 F0F0E0E0 D0D0C0C0 |
| 324 | // B0B0A0A0) |
| 325 | const __m128i i1 = _mm_and_si128(r1, kMask_xf0); |
| 326 | // -> (HHHHGGGG FFFFEEEE DDDDCCCC BBBBAAAA) |
| 327 | const __m128i i11 = _mm_or_si128(i1, _mm_srli_epi16(i1, 4)); |
| 328 | |
| 329 | // Shuffle low 64-bits with itself to expand from (HHHHGGGG FFFFEEEE DDDDCCCC BBBBAAAA) |
| 330 | // to (DDDDDDDD CCCCCCCC BBBBBBBB AAAAAAAA) |
| 331 | const __m128i i15 = _mm_unpacklo_epi8(i11, i11); |
| 332 | // (DDDDDDDD CCCCCCCC BBBBBBBB AAAAAAAA) -> (BBBBBBBB BBBBBBBB AAAAAAAA AAAAAAAA) |
| 333 | const __m128i i151 = _mm_unpacklo_epi8(i15, i15); |
| 334 | // (DDDDDDDD CCCCCCCC BBBBBBBB AAAAAAAA) -> (DDDDDDDD DDDDDDDD CCCCCCCC CCCCCCCC) |
| 335 | const __m128i i152 = _mm_unpackhi_epi8(i15, i15); |
| 336 | |
| 337 | // Shuffle hi 64-bits with itself to expand from (HHHHGGGG FFFFEEEE DDDDCCCC BBBBAAAA) |
| 338 | // to (HHHHHHHH GGGGGGGG FFFFFFFF EEEEEEEE) |
| 339 | const __m128i i16 = _mm_unpackhi_epi8(i11, i11); |
| 340 | // (HHHHHHHH GGGGGGGG FFFFFFFF EEEEEEEE) -> (FFFFFFFF FFFFFFFF EEEEEEEE EEEEEEEE) |
| 341 | const __m128i i161 = _mm_unpacklo_epi8(i16, i16); |
| 342 | // (HHHHHHHH GGGGGGGG FFFFFFFF EEEEEEEE) -> (HHHHHHHH HHHHHHHH GGGGGGGG GGGGGGGG) |
| 343 | const __m128i i162 = _mm_unpackhi_epi8(i16, i16); |
| 344 | |
| 345 | // Now find the lo 4 bits of each input 8-bit word: |
| 346 | const __m128i i2 = _mm_and_si128(r1, kMask_x0f); |
| 347 | const __m128i i22 = _mm_or_si128(i2, _mm_slli_epi16(i2, 4)); |
| 348 | |
| 349 | const __m128i i25 = _mm_unpacklo_epi8(i22, i22); |
| 350 | const __m128i i251 = _mm_unpacklo_epi8(i25, i25); |
| 351 | const __m128i i252 = _mm_unpackhi_epi8(i25, i25); |
| 352 | |
| 353 | const __m128i i26 = _mm_unpackhi_epi8(i22, i22); |
| 354 | const __m128i i261 = _mm_unpacklo_epi8(i26, i26); |
| 355 | const __m128i i262 = _mm_unpackhi_epi8(i26, i26); |
| 356 | |
| 357 | // _mm_and_si128(i151, kMask_x00000000ffffffff) takes i151 and masks off 1st and 3rd |
| 358 | // 32-bit words |
| 359 | // (BBBBBBBB BBBBBBBB AAAAAAAA AAAAAAAA) -> (00000000 BBBBBBBB 00000000 AAAAAAAA) |
| 360 | // _mm_and_si128(i251, kMask_xffffffff00000000) takes i251 and masks off 2nd and 4th |
| 361 | // 32-bit words |
| 362 | // (bbbbbbbb bbbbbbbb aaaaaaaa aaaaaaaa) -> (bbbbbbbb 00000000 aaaaaaaa 00000000) |
| 363 | // And last but not least, _mm_or_si128 ORs those two together, giving us the |
| 364 | // interleaving we desire: |
| 365 | // (00000000 BBBBBBBB 00000000 AAAAAAAA) | (bbbbbbbb 00000000 aaaaaaaa 00000000) -> |
| 366 | // (bbbbbbbb BBBBBBBB aaaaaaaa AAAAAAAA) |
| 367 | const __m128i kMask_x00000000ffffffff = |
| 368 | _mm_set_epi32(0x00000000L, 0xffffffffL, 0x00000000L, 0xffffffffL); |
| 369 | const __m128i kMask_xffffffff00000000 = |
| 370 | _mm_set_epi32(0xffffffffL, 0x00000000L, 0xffffffffL, 0x00000000L); |
| 371 | const __m128i o1 = _mm_or_si128(_mm_and_si128(i151, kMask_x00000000ffffffff), |
| 372 | _mm_and_si128(i251, kMask_xffffffff00000000)); |
| 373 | const __m128i o2 = _mm_or_si128(_mm_and_si128(i152, kMask_x00000000ffffffff), |
| 374 | _mm_and_si128(i252, kMask_xffffffff00000000)); |
| 375 | |
| 376 | // These two are for the next row; same pattern as above. We batched up two rows because |
| 377 | // our input was 64 bits. |
| 378 | const __m128i o3 = _mm_or_si128(_mm_and_si128(i161, kMask_x00000000ffffffff), |
| 379 | _mm_and_si128(i261, kMask_xffffffff00000000)); |
| 380 | const __m128i o4 = _mm_or_si128(_mm_and_si128(i162, kMask_x00000000ffffffff), |
| 381 | _mm_and_si128(i262, kMask_xffffffff00000000)); |
| 382 | // Write row 0: |
| 383 | _mm_storeu_si128((__m128i*)(dst + (y + iy) * width + x), o1); |
| 384 | _mm_storeu_si128((__m128i*)(dst + (y + iy) * width + x + 4), o2); |
| 385 | // Write row 1: |
| 386 | _mm_storeu_si128((__m128i*)(dst + (y + iy + 1) * width + x), o3); |
| 387 | _mm_storeu_si128((__m128i*)(dst + (y + iy + 1) * width + x + 4), o4); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | FUNCTION_TARGET_SSSE3 |
| 394 | static void TexDecoder_DecodeImpl_I8_SSSE3(u32* dst, const u8* src, int width, int height, |
| 395 | TextureFormat texformat, const u8* tlut, |
| 396 | TLUTFormat tlutfmt, int Wsteps4, int Wsteps8) |
| 397 | { |
| 398 | // xsacha optimized with SSSE3 intrinsics |
| 399 | // Produces a ~10% speed improvement over SSE2 implementation |
| 400 | for (int y = 0; y < height; y += 4) |
| 401 | { |
| 402 | for (int x = 0, yStep = (y / 4) * Wsteps8; x < width; x += 8, yStep++) |
| 403 | { |
| 404 | for (int iy = 0, xStep = 4 * yStep; iy < 4; ++iy, xStep++) |
| 405 | { |
| 406 | const __m128i mask3210 = _mm_set_epi8(3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0); |
| 407 | |
| 408 | const __m128i mask7654 = _mm_set_epi8(7, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4); |
| 409 | __m128i *quaddst, r, rgba0, rgba1; |
| 410 | // Load 64 bits from `src` into an __m128i with upper 64 bits zeroed: (0000 0000 hgfe dcba) |
| 411 | r = _mm_loadl_epi64((const __m128i*)(src + 8 * xStep)); |
| 412 | // Shuffle select bytes to expand from (0000 0000 hgfe dcba) to: |
| 413 | rgba0 = _mm_shuffle_epi8(r, mask3210); // (dddd cccc bbbb aaaa) |
| 414 | rgba1 = _mm_shuffle_epi8(r, mask7654); // (hhhh gggg ffff eeee) |
| 415 | |
| 416 | quaddst = (__m128i*)(dst + (y + iy) * width + x); |
| 417 | _mm_storeu_si128(quaddst, rgba0); |
| 418 | _mm_storeu_si128(quaddst + 1, rgba1); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | static void TexDecoder_DecodeImpl_I8(u32* dst, const u8* src, int width, int height, |
| 425 | TextureFormat texformat, const u8* tlut, TLUTFormat tlutfmt, |
| 426 | int Wsteps4, int Wsteps8) |
| 427 | { |
| 428 | // JSD optimized with SSE2 intrinsics. |
| 429 | // Produces an ~86% speed improvement over reference C implementation. |
| 430 | for (int y = 0; y < height; y += 4) |
| 431 | { |
| 432 | for (int x = 0, yStep = (y / 4) * Wsteps8; x < width; x += 8, yStep++) |
| 433 | { |
| 434 | // Each loop iteration processes 4 rows from 4 64-bit reads. |
| 435 | const u8* src2 = src + 32 * yStep; |
| 436 | // TODO: is it more efficient to group the loads together sequentially and also the stores |
| 437 | // at the end? _mm_stream instead of _mm_store on my AMD Phenom II x410 made performance |
| 438 | // significantly WORSE, so I went with _mm_stores. Perhaps there is some edge case here |
| 439 | // creating the terrible performance or we're not aligned to 16-byte boundaries. I don't know. |
| 440 | __m128i* quaddst; |
| 441 | |
| 442 | // Load 64 bits from `src` into an __m128i with upper 64 bits zeroed: (0000 0000 hgfe dcba) |
| 443 | const __m128i r0 = _mm_loadl_epi64((const __m128i*)src2); |
| 444 | // Shuffle low 64-bits with itself to expand from (0000 0000 hgfe dcba) to (hhgg ffee ddcc |
| 445 | // bbaa) |
| 446 | const __m128i r1 = _mm_unpacklo_epi8(r0, r0); |
| 447 | |
| 448 | // Shuffle low 64-bits with itself to expand from (hhgg ffee ddcc bbaa) to (dddd cccc bbbb |
| 449 | // aaaa) |
| 450 | const __m128i rgba0 = _mm_unpacklo_epi8(r1, r1); |
| 451 | // Shuffle hi 64-bits with itself to expand from (hhgg ffee ddcc bbaa) to (hhhh gggg ffff |
| 452 | // eeee) |
| 453 | const __m128i rgba1 = _mm_unpackhi_epi8(r1, r1); |
| 454 | |
| 455 | // Store (dddd cccc bbbb aaaa) out: |
| 456 | quaddst = (__m128i*)(dst + (y + 0) * width + x); |
| 457 | _mm_storeu_si128(quaddst, rgba0); |
| 458 | // Store (hhhh gggg ffff eeee) out: |
| 459 | _mm_storeu_si128(quaddst + 1, rgba1); |
| 460 | |
| 461 | // Load 64 bits from `src` into an __m128i with upper 64 bits zeroed: (0000 0000 hgfe dcba) |
| 462 | src2 += 8; |
| 463 | const __m128i r2 = _mm_loadl_epi64((const __m128i*)src2); |
| 464 | // Shuffle low 64-bits with itself to expand from (0000 0000 hgfe dcba) to (hhgg ffee ddcc |
| 465 | // bbaa) |
| 466 | const __m128i r3 = _mm_unpacklo_epi8(r2, r2); |
| 467 | |
| 468 | // Shuffle low 64-bits with itself to expand from (hhgg ffee ddcc bbaa) to (dddd cccc bbbb |
| 469 | // aaaa) |
| 470 | const __m128i rgba2 = _mm_unpacklo_epi8(r3, r3); |
| 471 | // Shuffle hi 64-bits with itself to expand from (hhgg ffee ddcc bbaa) to (hhhh gggg ffff |
| 472 | // eeee) |
| 473 | const __m128i rgba3 = _mm_unpackhi_epi8(r3, r3); |
| 474 | |
| 475 | // Store (dddd cccc bbbb aaaa) out: |
| 476 | quaddst = (__m128i*)(dst + (y + 1) * width + x); |
| 477 | _mm_storeu_si128(quaddst, rgba2); |
| 478 | // Store (hhhh gggg ffff eeee) out: |
| 479 | _mm_storeu_si128(quaddst + 1, rgba3); |
| 480 | |
| 481 | // Load 64 bits from `src` into an __m128i with upper 64 bits zeroed: (0000 0000 hgfe dcba) |
| 482 | src2 += 8; |
| 483 | const __m128i r4 = _mm_loadl_epi64((const __m128i*)src2); |
| 484 | // Shuffle low 64-bits with itself to expand from (0000 0000 hgfe dcba) to (hhgg ffee ddcc |
| 485 | // bbaa) |
| 486 | const __m128i r5 = _mm_unpacklo_epi8(r4, r4); |
| 487 | |
| 488 | // Shuffle low 64-bits with itself to expand from (hhgg ffee ddcc bbaa) to (dddd cccc bbbb |
| 489 | // aaaa) |
| 490 | const __m128i rgba4 = _mm_unpacklo_epi8(r5, r5); |
| 491 | // Shuffle hi 64-bits with itself to expand from (hhgg ffee ddcc bbaa) to (hhhh gggg ffff |
| 492 | // eeee) |
| 493 | const __m128i rgba5 = _mm_unpackhi_epi8(r5, r5); |
| 494 | |
| 495 | // Store (dddd cccc bbbb aaaa) out: |
| 496 | quaddst = (__m128i*)(dst + (y + 2) * width + x); |
| 497 | _mm_storeu_si128(quaddst, rgba4); |
| 498 | // Store (hhhh gggg ffff eeee) out: |
| 499 | _mm_storeu_si128(quaddst + 1, rgba5); |
| 500 | |
| 501 | // Load 64 bits from `src` into an __m128i with upper 64 bits zeroed: (0000 0000 hgfe dcba) |
| 502 | src2 += 8; |
| 503 | const __m128i r6 = _mm_loadl_epi64((const __m128i*)src2); |
| 504 | // Shuffle low 64-bits with itself to expand from (0000 0000 hgfe dcba) to (hhgg ffee ddcc |
| 505 | // bbaa) |
| 506 | const __m128i r7 = _mm_unpacklo_epi8(r6, r6); |
| 507 | |
| 508 | // Shuffle low 64-bits with itself to expand from (hhgg ffee ddcc bbaa) to (dddd cccc bbbb |
| 509 | // aaaa) |
| 510 | const __m128i rgba6 = _mm_unpacklo_epi8(r7, r7); |
| 511 | // Shuffle hi 64-bits with itself to expand from (hhgg ffee ddcc bbaa) to (hhhh gggg ffff |
| 512 | // eeee) |
| 513 | const __m128i rgba7 = _mm_unpackhi_epi8(r7, r7); |
| 514 | |
| 515 | // Store (dddd cccc bbbb aaaa) out: |
| 516 | quaddst = (__m128i*)(dst + (y + 3) * width + x); |
| 517 | _mm_storeu_si128(quaddst, rgba6); |
| 518 | // Store (hhhh gggg ffff eeee) out: |
| 519 | _mm_storeu_si128(quaddst + 1, rgba7); |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | static void TexDecoder_DecodeImpl_C8(u32* dst, const u8* src, int width, int height, |
| 525 | TextureFormat texformat, const u8* tlut, TLUTFormat tlutfmt, |
| 526 | int Wsteps4, int Wsteps8) |
| 527 | { |
| 528 | switch (tlutfmt) |
| 529 | { |
| 530 | case TLUTFormat::RGB5A3: |
| 531 | { |
| 532 | for (int y = 0; y < height; y += 4) |
| 533 | for (int x = 0, yStep = (y / 4) * Wsteps8; x < width; x += 8, yStep++) |
| 534 | for (int iy = 0, xStep = 4 * yStep; iy < 4; iy++, xStep++) |
| 535 | DecodeBytes_C8_RGB5A3((u32*)dst + (y + iy) * width + x, src + 8 * xStep, tlut); |
| 536 | } |
| 537 | break; |
| 538 | |
| 539 | case TLUTFormat::IA8: |
| 540 | { |
| 541 | for (int y = 0; y < height; y += 4) |
| 542 | for (int x = 0, yStep = (y / 4) * Wsteps8; x < width; x += 8, yStep++) |
| 543 | for (int iy = 0, xStep = 4 * yStep; iy < 4; iy++, xStep++) |
| 544 | DecodeBytes_C8_IA8(dst + (y + iy) * width + x, src + 8 * xStep, tlut); |
| 545 | } |
| 546 | break; |
| 547 | |
| 548 | case TLUTFormat::RGB565: |
| 549 | { |
| 550 | for (int y = 0; y < height; y += 4) |
| 551 | for (int x = 0, yStep = (y / 4) * Wsteps8; x < width; x += 8, yStep++) |
| 552 | for (int iy = 0, xStep = 4 * yStep; iy < 4; iy++, xStep++) |
| 553 | DecodeBytes_C8_RGB565(dst + (y + iy) * width + x, src + 8 * xStep, tlut); |
| 554 | } |
| 555 | break; |
| 556 | |
| 557 | default: |
| 558 | break; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | static void TexDecoder_DecodeImpl_IA4(u32* dst, const u8* src, int width, int height, |
| 563 | TextureFormat texformat, const u8* tlut, TLUTFormat tlutfmt, |
| 564 | int Wsteps4, int Wsteps8) |
| 565 | { |
| 566 | for (int y = 0; y < height; y += 4) |
| 567 | { |
| 568 | for (int x = 0, yStep = (y / 4) * Wsteps8; x < width; x += 8, yStep++) |
| 569 | { |
| 570 | for (int iy = 0, xStep = 4 * yStep; iy < 4; iy++, xStep++) |
| 571 | { |
| 572 | DecodeBytes_IA4(dst + (y + iy) * width + x, src + 8 * xStep); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | FUNCTION_TARGET_SSSE3 |
| 579 | static void TexDecoder_DecodeImpl_IA8_SSSE3(u32* dst, const u8* src, int width, int height, |
| 580 | TextureFormat texformat, const u8* tlut, |
| 581 | TLUTFormat tlutfmt, int Wsteps4, int Wsteps8) |
| 582 | { |
| 583 | // xsacha optimized with SSSE3 intrinsics. |
| 584 | // Produces an ~50% speed improvement over SSE2 implementation. |
| 585 | for (int y = 0; y < height; y += 4) |
| 586 | { |
| 587 | for (int x = 0, yStep = (y / 4) * Wsteps4; x < width; x += 4, yStep++) |
| 588 | { |
| 589 | for (int iy = 0, xStep = 4 * yStep; iy < 4; iy++, xStep++) |
| 590 | { |
| 591 | const __m128i mask = _mm_set_epi8(6, 7, 7, 7, 4, 5, 5, 5, 2, 3, 3, 3, 0, 1, 1, 1); |
| 592 | // Load 4x 16-bit IA8 samples from `src` into an __m128i with upper 64 bits zeroed: |
| 593 | // (0000 0000 hgfe dcba) |
| 594 | const __m128i r0 = _mm_loadl_epi64((const __m128i*)(src + 8 * xStep)); |
| 595 | // Shuffle to (ghhh efff cddd abbb) |
| 596 | const __m128i r1 = _mm_shuffle_epi8(r0, mask); |
| 597 | _mm_storeu_si128((__m128i*)(dst + (y + iy) * width + x), r1); |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | static void TexDecoder_DecodeImpl_IA8(u32* dst, const u8* src, int width, int height, |
| 604 | TextureFormat texformat, const u8* tlut, TLUTFormat tlutfmt, |
| 605 | int Wsteps4, int Wsteps8) |
| 606 | { |
| 607 | // JSD optimized with SSE2 intrinsics. |
| 608 | // Produces an ~80% speed improvement over reference C implementation. |
| 609 | const __m128i kMask_xf0 = _mm_set_epi32(0x00000000L, 0x00000000L, 0xff00ff00L, 0xff00ff00L); |
| 610 | const __m128i kMask_x0f = _mm_set_epi32(0x00000000L, 0x00000000L, 0x00ff00ffL, 0x00ff00ffL); |
| 611 | const __m128i kMask_xf000 = _mm_set_epi32(0xff000000L, 0xff000000L, 0xff000000L, 0xff000000L); |
| 612 | const __m128i kMask_x0fff = _mm_set_epi32(0x00ffffffL, 0x00ffffffL, 0x00ffffffL, 0x00ffffffL); |
| 613 | for (int y = 0; y < height; y += 4) |
| 614 | { |
| 615 | for (int x = 0, yStep = (y / 4) * Wsteps4; x < width; x += 4, yStep++) |
| 616 | { |
| 617 | for (int iy = 0, xStep = 4 * yStep; iy < 4; iy++, xStep++) |
| 618 | { |
| 619 | // Expands a 16-bit "IA" to a 32-bit "AIII". Each char is an 8-bit value. |
| 620 | |
| 621 | // Load 4x 16-bit IA8 samples from `src` into an __m128i with upper 64 bits zeroed: |
| 622 | // (0000 0000 hgfe dcba) |
| 623 | const __m128i r0 = _mm_loadl_epi64((const __m128i*)(src + 8 * xStep)); |
| 624 | |
| 625 | // Logical shift all 16-bit words right by 8 bits (0000 0000 hgfe dcba) to (0000 0000 |
| 626 | // 0h0f 0d0b). This gets us only the I components. |
| 627 | const __m128i i0 = _mm_srli_epi16(r0, 8); |
| 628 | |
| 629 | // Now join up the I components from their original positions but mask out the A |
| 630 | // components. |
| 631 | // (0000 0000 hgfe dcba) & kMask_xFF00 -> (0000 0000 h0f0 d0b0) |
| 632 | // (0000 0000 h0f0 d0b0) | (0000 0000 0h0f 0d0b) -> (0000 0000 hhff ddbb) |
| 633 | const __m128i i1 = _mm_or_si128(_mm_and_si128(r0, kMask_xf0), i0); |
| 634 | |
| 635 | // Shuffle low 64-bits with itself to expand from (0000 0000 hhff ddbb) to (hhhh ffff |
| 636 | // dddd bbbb) |
| 637 | const __m128i i2 = _mm_unpacklo_epi8(i1, i1); |
| 638 | // (hhhh ffff dddd bbbb) & kMask_x0fff -> (0hhh 0fff 0ddd 0bbb) |
| 639 | const __m128i i3 = _mm_and_si128(i2, kMask_x0fff); |
| 640 | |
| 641 | // Now that we have the I components in 32-bit word form, time work out the A components |
| 642 | // into their final positions. |
| 643 | |
| 644 | // (0000 0000 hgfe dcba) & kMask_x00FF -> (0000 0000 0g0e 0c0a) |
| 645 | const __m128i a0 = _mm_and_si128(r0, kMask_x0f); |
| 646 | // (0000 0000 0g0e 0c0a) -> (00gg 00ee 00cc 00aa) |
| 647 | const __m128i a1 = _mm_unpacklo_epi8(a0, a0); |
| 648 | // (00gg 00ee 00cc 00aa) << 16 -> (gg00 ee00 cc00 aa00) |
| 649 | const __m128i a2 = _mm_slli_epi32(a1, 16); |
| 650 | // (gg00 ee00 cc00 aa00) & kMask_xf000 -> (g000 e000 c000 a000) |
| 651 | const __m128i a3 = _mm_and_si128(a2, kMask_xf000); |
| 652 | |
| 653 | // Simply OR up i3 and a3 now and that's our result: |
| 654 | // (0hhh 0fff 0ddd 0bbb) | (g000 e000 c000 a000) -> (ghhh efff cddd abbb) |
| 655 | const __m128i r1 = _mm_or_si128(i3, a3); |
| 656 | |
| 657 | // write out the 128-bit result: |
| 658 | _mm_storeu_si128((__m128i*)(dst + (y + iy) * width + x), r1); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | static void TexDecoder_DecodeImpl_C14X2(u32* dst, const u8* src, int width, int height, |
| 665 | TextureFormat texformat, const u8* tlut, TLUTFormat tlutfmt, |
| 666 | int Wsteps4, int Wsteps8) |
| 667 | { |
| 668 | switch (tlutfmt) |
| 669 | { |
| 670 | case TLUTFormat::RGB5A3: |
| 671 | { |
| 672 | for (int y = 0; y < height; y += 4) |
| 673 | for (int x = 0, yStep = (y / 4) * Wsteps4; x < width; x += 4, yStep++) |
| 674 | for (int iy = 0, xStep = 4 * yStep; iy < 4; iy++, xStep++) |
| 675 | DecodeBytes_C14X2_RGB5A3(dst + (y + iy) * width + x, (u16*)(src + 8 * xStep), tlut); |
| 676 | } |
| 677 | break; |
| 678 | |
| 679 | case TLUTFormat::IA8: |
| 680 | { |
| 681 | for (int y = 0; y < height; y += 4) |
| 682 | for (int x = 0, yStep = (y / 4) * Wsteps4; x < width; x += 4, yStep++) |
| 683 | for (int iy = 0, xStep = 4 * yStep; iy < 4; iy++, xStep++) |
| 684 | DecodeBytes_C14X2_IA8(dst + (y + iy) * width + x, (u16*)(src + 8 * xStep), tlut); |
| 685 | } |
| 686 | break; |
| 687 | |
| 688 | case TLUTFormat::RGB565: |
| 689 | { |
| 690 | for (int y = 0; y < height; y += 4) |
| 691 | for (int x = 0, yStep = (y / 4) * Wsteps4; x < width; x += 4, yStep++) |
| 692 | for (int iy = 0, xStep = 4 * yStep; iy < 4; iy++, xStep++) |
| 693 | DecodeBytes_C14X2_RGB565(dst + (y + iy) * width + x, (u16*)(src + 8 * xStep), tlut); |
| 694 | } |
| 695 | break; |
| 696 | |
| 697 | default: |
| 698 | break; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | static void TexDecoder_DecodeImpl_RGB565(u32* dst, const u8* src, int width, int height, |
| 703 | TextureFormat texformat, const u8* tlut, |
| 704 | TLUTFormat tlutfmt, int Wsteps4, int Wsteps8) |
| 705 | { |
| 706 | // JSD optimized with SSE2 intrinsics. |
| 707 | // Produces an ~78% speed improvement over reference C implementation. |
| 708 | const __m128i kMaskR0 = _mm_set1_epi32(0x000000F8); |
| 709 | const __m128i kMaskG0 = _mm_set1_epi32(0x0000FC00); |
| 710 | const __m128i kMaskG1 = _mm_set1_epi32(0x00000300); |
| 711 | const __m128i kMaskB0 = _mm_set1_epi32(0x00F80000); |
| 712 | const __m128i kAlpha = _mm_set1_epi32(0xFF000000); |
| 713 | for (int y = 0; y < height; y += 4) |
| 714 | { |
| 715 | for (int x = 0, yStep = (y / 4) * Wsteps4; x < width; x += 4, yStep++) |
| 716 | { |
| 717 | for (int iy = 0, xStep = 4 * yStep; iy < 4; iy++, xStep++) |
| 718 | { |
| 719 | __m128i* dxtsrc = (__m128i*)(src + 8 * xStep); |
| 720 | // Load 4x 16-bit colors: (0000 0000 hgfe dcba) |
| 721 | // where hg, fe, ba, and dc are 16-bit colors in big-endian order |
| 722 | const __m128i rgb565x4 = _mm_loadl_epi64(dxtsrc); |
| 723 | |
| 724 | // The big-endian 16-bit colors `ba` and `dc` look like 0b_gggBBBbb_RRRrrGGg in a little |
| 725 | // endian xmm register Unpack `hgfe dcba` to `hhgg ffee ddcc bbaa`, where each 32-bit word |
| 726 | // is now 0b_gggBBBbb_RRRrrGGg_gggBBBbb_RRRrrGGg |
| 727 | const __m128i c0 = _mm_unpacklo_epi16(rgb565x4, rgb565x4); |
| 728 | |
| 729 | // swizzle 0b_gggBBBbb_RRRrrGGg_gggBBBbb_RRRrrGGg |
| 730 | // to 0b_11111111_BBBbbBBB_GGggggGG_RRRrrRRR |
| 731 | |
| 732 | // 0b_gggBBBbb_RRRrrGGg_gggBBBbb_RRRrrGGg & |
| 733 | // 0b_00000000_00000000_00000000_11111000 = |
| 734 | // 0b_00000000_00000000_00000000_RRRrr000 |
| 735 | const __m128i r0 = _mm_and_si128(c0, kMaskR0); |
| 736 | // 0b_00000000_00000000_00000000_RRRrr000 >> 5 [32] = |
| 737 | // 0b_00000000_00000000_00000000_00000RRR |
| 738 | const __m128i r1 = _mm_srli_epi32(r0, 5); |
| 739 | |
| 740 | // 0b_gggBBBbb_RRRrrGGg_gggBBBbb_RRRrrGGg >> 3 [32] = |
| 741 | // 0b_000gggBB_BbbRRRrr_GGggggBB_BbbRRRrr & |
| 742 | // 0b_00000000_00000000_11111100_00000000 = |
| 743 | // 0b_00000000_00000000_GGgggg00_00000000 |
| 744 | const __m128i gtmp = _mm_srli_epi32(c0, 3); |
| 745 | const __m128i g0 = _mm_and_si128(gtmp, kMaskG0); |
| 746 | // 0b_GGggggBB_BbbRRRrr_GGggggBB_Bbb00000 >> 6 [32] = |
| 747 | // 0b_000000GG_ggggBBBb_bRRRrrGG_ggggBBBb & |
| 748 | // 0b_00000000_00000000_00000011_00000000 = |
| 749 | // 0b_00000000_00000000_000000GG_00000000 = |
| 750 | const __m128i g1 = _mm_and_si128(_mm_srli_epi32(gtmp, 6), kMaskG1); |
| 751 | |
| 752 | // 0b_gggBBBbb_RRRrrGGg_gggBBBbb_RRRrrGGg >> 5 [32] = |
| 753 | // 0b_00000ggg_BBBbbRRR_rrGGgggg_BBBbbRRR & |
| 754 | // 0b_00000000_11111000_00000000_00000000 = |
| 755 | // 0b_00000000_BBBbb000_00000000_00000000 |
| 756 | const __m128i b0 = _mm_and_si128(_mm_srli_epi32(c0, 5), kMaskB0); |
| 757 | // 0b_00000000_BBBbb000_00000000_00000000 >> 5 [16] = |
| 758 | // 0b_00000000_00000BBB_00000000_00000000 |
| 759 | const __m128i b1 = _mm_srli_epi16(b0, 5); |
| 760 | |
| 761 | // OR together the final RGB bits and the alpha component: |
| 762 | const __m128i abgr888x4 = |
| 763 | _mm_or_si128(_mm_or_si128(_mm_or_si128(r0, r1), _mm_or_si128(g0, g1)), |
| 764 | _mm_or_si128(_mm_or_si128(b0, b1), kAlpha)); |
| 765 | |
| 766 | __m128i* ptr = (__m128i*)(dst + (y + iy) * width + x); |
| 767 | _mm_storeu_si128(ptr, abgr888x4); |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | FUNCTION_TARGET_SSSE3 |
| 774 | static void TexDecoder_DecodeImpl_RGB5A3_SSSE3(u32* dst, const u8* src, int width, int height, |
| 775 | TextureFormat texformat, const u8* tlut, |
| 776 | TLUTFormat tlutfmt, int Wsteps4, int Wsteps8) |
| 777 | { |
| 778 | const __m128i kMask_x1f = _mm_set1_epi32(0x0000001fL); |
| 779 | const __m128i kMask_x0f = _mm_set1_epi32(0x0000000fL); |
| 780 | const __m128i kMask_x07 = _mm_set1_epi32(0x00000007L); |
| 781 | // This is the hard-coded 0xFF alpha constant that is ORed in place after the RGB are calculated |
| 782 | // for the RGB555 case when (s[x] & 0x8000) is true for all pixels. |
| 783 | const __m128i aVxff00 = _mm_set1_epi32(0xFF000000L); |
| 784 | |
| 785 | // xsacha optimized with SSSE3 intrinsics (2 in 4 cases) |
| 786 | // Produces a ~10% speed improvement over SSE2 implementation |
| 787 | for (int y = 0; y < height; y += 4) |
| 788 | { |
| 789 | for (int x = 0, yStep = (y / 4) * Wsteps4; x < width; x += 4, yStep++) |
| 790 | { |
| 791 | for (int iy = 0, xStep = 4 * yStep; iy < 4; iy++, xStep++) |
| 792 | { |
| 793 | u32* newdst = dst + (y + iy) * width + x; |
| 794 | const __m128i mask = |
| 795 | _mm_set_epi8(-128, -128, 6, 7, -128, -128, 4, 5, -128, -128, 2, 3, -128, -128, 0, 1); |
| 796 | const __m128i valV = |
| 797 | _mm_shuffle_epi8(_mm_loadl_epi64((const __m128i*)(src + 8 * xStep)), mask); |
| 798 | int cmp = _mm_movemask_epi8(valV); // MSB: 0x2 = val0; 0x20=val1; 0x200 = val2; 0x2000=val3 |
| 799 | if ((cmp & 0x2222) == |
| 800 | 0x2222) // SSSE3 case #1: all 4 pixels are in RGB555 and alpha = 0xFF. |
| 801 | { |
| 802 | // Swizzle bits: 00012345 -> 12345123 |
| 803 | |
| 804 | // r0 = (((val0>>10) & 0x1f) << 3) | (((val0>>10) & 0x1f) >> 2); |
| 805 | const __m128i tmprV = _mm_and_si128(_mm_srli_epi16(valV, 10), kMask_x1f); |
| 806 | const __m128i rV = _mm_or_si128(_mm_slli_epi16(tmprV, 3), _mm_srli_epi16(tmprV, 2)); |
| 807 | |
| 808 | // g0 = (((val0>>5 ) & 0x1f) << 3) | (((val0>>5 ) & 0x1f) >> 2); |
| 809 | const __m128i tmpgV = _mm_and_si128(_mm_srli_epi16(valV, 5), kMask_x1f); |
| 810 | const __m128i gV = _mm_or_si128(_mm_slli_epi16(tmpgV, 3), _mm_srli_epi16(tmpgV, 2)); |
| 811 | |
| 812 | // b0 = (((val0 ) & 0x1f) << 3) | (((val0 ) & 0x1f) >> 2); |
| 813 | const __m128i tmpbV = _mm_and_si128(valV, kMask_x1f); |
| 814 | const __m128i bV = _mm_or_si128(_mm_slli_epi16(tmpbV, 3), _mm_srli_epi16(tmpbV, 2)); |
| 815 | |
| 816 | // newdst[0] = r0 | (g0 << 8) | (b0 << 16) | (a0 << 24); |
| 817 | const __m128i final = _mm_or_si128(_mm_or_si128(rV, _mm_slli_epi32(gV, 8)), |
| 818 | _mm_or_si128(_mm_slli_epi32(bV, 16), aVxff00)); |
| 819 | _mm_storeu_si128((__m128i*)newdst, final); |
| 820 | } |
| 821 | else if (!(cmp & 0x2222)) // SSSE3 case #2: all 4 pixels are in RGBA4443. |
| 822 | { |
| 823 | // Swizzle bits: 00001234 -> 12341234 |
| 824 | |
| 825 | // r0 = (((val0>>8 ) & 0xf) << 4) | ((val0>>8 ) & 0xf); |
| 826 | const __m128i tmprV = _mm_and_si128(_mm_srli_epi16(valV, 8), kMask_x0f); |
| 827 | const __m128i rV = _mm_or_si128(_mm_slli_epi16(tmprV, 4), tmprV); |
| 828 | |
| 829 | // g0 = (((val0>>4 ) & 0xf) << 4) | ((val0>>4 ) & 0xf); |
| 830 | const __m128i tmpgV = _mm_and_si128(_mm_srli_epi16(valV, 4), kMask_x0f); |
| 831 | const __m128i gV = _mm_or_si128(_mm_slli_epi16(tmpgV, 4), tmpgV); |
| 832 | |
| 833 | // b0 = (((val0 ) & 0xf) << 4) | ((val0 ) & 0xf); |
| 834 | const __m128i tmpbV = _mm_and_si128(valV, kMask_x0f); |
| 835 | const __m128i bV = _mm_or_si128(_mm_slli_epi16(tmpbV, 4), tmpbV); |
| 836 | // a0 = (((val0>>12) & 0x7) << 5) | (((val0>>12) & 0x7) << 2) | (((val0>>12) & 0x7) >> 1); |
| 837 | const __m128i tmpaV = _mm_and_si128(_mm_srli_epi16(valV, 12), kMask_x07); |
| 838 | const __m128i aV = |
| 839 | _mm_or_si128(_mm_slli_epi16(tmpaV, 5), |
| 840 | _mm_or_si128(_mm_slli_epi16(tmpaV, 2), _mm_srli_epi16(tmpaV, 1))); |
| 841 | |
| 842 | // newdst[0] = r0 | (g0 << 8) | (b0 << 16) | (a0 << 24); |
| 843 | const __m128i final = |
| 844 | _mm_or_si128(_mm_or_si128(rV, _mm_slli_epi32(gV, 8)), |
| 845 | _mm_or_si128(_mm_slli_epi32(bV, 16), _mm_slli_epi32(aV, 24))); |
| 846 | _mm_storeu_si128((__m128i*)newdst, final); |
| 847 | } |
| 848 | else |
| 849 | { |
| 850 | // TODO: Vectorise (Either 4-way branch or do both and select is better than this) |
| 851 | u32* vals = (u32*)&valV; |
| 852 | int r, g, b, a; |
| 853 | for (int i = 0; i < 4; ++i) |
| 854 | { |
| 855 | if (vals[i] & 0x8000) |
| 856 | { |
| 857 | // Swizzle bits: 00012345 -> 12345123 |
| 858 | r = (((vals[i] >> 10) & 0x1f) << 3) | (((vals[i] >> 10) & 0x1f) >> 2); |
| 859 | g = (((vals[i] >> 5) & 0x1f) << 3) | (((vals[i] >> 5) & 0x1f) >> 2); |
| 860 | b = (((vals[i]) & 0x1f) << 3) | (((vals[i]) & 0x1f) >> 2); |
| 861 | a = 0xFF; |
| 862 | } |
| 863 | else |
| 864 | { |
| 865 | a = (((vals[i] >> 12) & 0x7) << 5) | (((vals[i] >> 12) & 0x7) << 2) | |
| 866 | (((vals[i] >> 12) & 0x7) >> 1); |
| 867 | // Swizzle bits: 00001234 -> 12341234 |
| 868 | r = (((vals[i] >> 8) & 0xf) << 4) | ((vals[i] >> 8) & 0xf); |
| 869 | g = (((vals[i] >> 4) & 0xf) << 4) | ((vals[i] >> 4) & 0xf); |
| 870 | b = (((vals[i]) & 0xf) << 4) | ((vals[i]) & 0xf); |
| 871 | } |
| 872 | newdst[i] = r | (g << 8) | (b << 16) | (a << 24); |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | static void TexDecoder_DecodeImpl_RGB5A3(u32* dst, const u8* src, int width, int height, |
| 881 | TextureFormat texformat, const u8* tlut, |
| 882 | TLUTFormat tlutfmt, int Wsteps4, int Wsteps8) |
| 883 | { |
| 884 | const __m128i kMask_x1f = _mm_set1_epi32(0x0000001fL); |
| 885 | const __m128i kMask_x0f = _mm_set1_epi32(0x0000000fL); |
| 886 | const __m128i kMask_x07 = _mm_set1_epi32(0x00000007L); |
| 887 | // This is the hard-coded 0xFF alpha constant that is ORed in place after the RGB are calculated |
| 888 | // for the RGB555 case when (s[x] & 0x8000) is true for all pixels. |
| 889 | const __m128i aVxff00 = _mm_set1_epi32(0xFF000000L); |
| 890 | |
| 891 | // JSD optimized with SSE2 intrinsics (2 in 4 cases) |
| 892 | // Produces a ~25% speed improvement over reference C implementation. |
| 893 | for (int y = 0; y < height; y += 4) |
| 894 | { |
| 895 | for (int x = 0, yStep = (y / 4) * Wsteps4; x < width; x += 4, yStep++) |
| 896 | { |
| 897 | for (int iy = 0, xStep = 4 * yStep; iy < 4; iy++, xStep++) |
| 898 | { |
| 899 | u32* newdst = dst + (y + iy) * width + x; |
| 900 | const u16* newsrc = (const u16*)(src + 8 * xStep); |
| 901 | |
| 902 | // TODO: weak point |
| 903 | const u16 val0 = Common::swap16(newsrc[0]); |
| 904 | const u16 val1 = Common::swap16(newsrc[1]); |
| 905 | const u16 val2 = Common::swap16(newsrc[2]); |
| 906 | const u16 val3 = Common::swap16(newsrc[3]); |
| 907 | |
| 908 | const __m128i valV = _mm_set_epi16(0, val3, 0, val2, 0, val1, 0, val0); |
| 909 | |
| 910 | // Need to check all 4 pixels' MSBs to ensure we can do data-parallelism: |
| 911 | if (((val0 & 0x8000) & (val1 & 0x8000) & (val2 & 0x8000) & (val3 & 0x8000)) == 0x8000) |
| 912 | { |
| 913 | // SSE2 case #1: all 4 pixels are in RGB555 and alpha = 0xFF. |
| 914 | |
| 915 | // Swizzle bits: 00012345 -> 12345123 |
| 916 | |
| 917 | // r0 = (((val0>>10) & 0x1f) << 3) | (((val0>>10) & 0x1f) >> 2); |
| 918 | const __m128i tmprV = _mm_and_si128(_mm_srli_epi16(valV, 10), kMask_x1f); |
| 919 | const __m128i rV = _mm_or_si128(_mm_slli_epi16(tmprV, 3), _mm_srli_epi16(tmprV, 2)); |
| 920 | |
| 921 | // g0 = (((val0>>5 ) & 0x1f) << 3) | (((val0>>5 ) & 0x1f) >> 2); |
| 922 | const __m128i tmpgV = _mm_and_si128(_mm_srli_epi16(valV, 5), kMask_x1f); |
| 923 | const __m128i gV = _mm_or_si128(_mm_slli_epi16(tmpgV, 3), _mm_srli_epi16(tmpgV, 2)); |
| 924 | |
| 925 | // b0 = (((val0 ) & 0x1f) << 3) | (((val0 ) & 0x1f) >> 2); |
| 926 | const __m128i tmpbV = _mm_and_si128(valV, kMask_x1f); |
| 927 | const __m128i bV = _mm_or_si128(_mm_slli_epi16(tmpbV, 3), _mm_srli_epi16(tmpbV, 2)); |
| 928 | |
| 929 | // newdst[0] = r0 | (g0 << 8) | (b0 << 16) | (a0 << 24); |
| 930 | const __m128i final = _mm_or_si128(_mm_or_si128(rV, _mm_slli_epi32(gV, 8)), |
| 931 | _mm_or_si128(_mm_slli_epi32(bV, 16), aVxff00)); |
| 932 | |
| 933 | // write the final result: |
| 934 | _mm_storeu_si128((__m128i*)newdst, final); |
| 935 | } |
| 936 | else if (((val0 & 0x8000) | (val1 & 0x8000) | (val2 & 0x8000) | (val3 & 0x8000)) == 0x0000) |
| 937 | { |
| 938 | // SSE2 case #2: all 4 pixels are in RGBA4443. |
| 939 | |
| 940 | // Swizzle bits: 00001234 -> 12341234 |
| 941 | |
| 942 | // r0 = (((val0>>8 ) & 0xf) << 4) | ((val0>>8 ) & 0xf); |
| 943 | const __m128i tmprV = _mm_and_si128(_mm_srli_epi16(valV, 8), kMask_x0f); |
| 944 | const __m128i rV = _mm_or_si128(_mm_slli_epi16(tmprV, 4), tmprV); |
| 945 | |
| 946 | // g0 = (((val0>>4 ) & 0xf) << 4) | ((val0>>4 ) & 0xf); |
| 947 | const __m128i tmpgV = _mm_and_si128(_mm_srli_epi16(valV, 4), kMask_x0f); |
| 948 | const __m128i gV = _mm_or_si128(_mm_slli_epi16(tmpgV, 4), tmpgV); |
| 949 | |
| 950 | // b0 = (((val0 ) & 0xf) << 4) | ((val0 ) & 0xf); |
| 951 | const __m128i tmpbV = _mm_and_si128(valV, kMask_x0f); |
| 952 | const __m128i bV = _mm_or_si128(_mm_slli_epi16(tmpbV, 4), tmpbV); |
| 953 | |
| 954 | // a0 = (((val0>>12) & 0x7) << 5) | (((val0>>12) & 0x7) << 2) | (((val0>>12) & 0x7) >> 1); |
| 955 | const __m128i tmpaV = _mm_and_si128(_mm_srli_epi16(valV, 12), kMask_x07); |
| 956 | const __m128i aV = |
| 957 | _mm_or_si128(_mm_slli_epi16(tmpaV, 5), |
| 958 | _mm_or_si128(_mm_slli_epi16(tmpaV, 2), _mm_srli_epi16(tmpaV, 1))); |
| 959 | |
| 960 | // newdst[0] = r0 | (g0 << 8) | (b0 << 16) | (a0 << 24); |
| 961 | const __m128i final = |
| 962 | _mm_or_si128(_mm_or_si128(rV, _mm_slli_epi32(gV, 8)), |
| 963 | _mm_or_si128(_mm_slli_epi32(bV, 16), _mm_slli_epi32(aV, 24))); |
| 964 | |
| 965 | // write the final result: |
| 966 | _mm_storeu_si128((__m128i*)newdst, final); |
| 967 | } |
| 968 | else |
| 969 | { |
| 970 | // TODO: Vectorise (Either 4-way branch or do both and select is better than this) |
| 971 | u32* vals = (u32*)&valV; |
| 972 | int r, g, b, a; |
| 973 | for (int i = 0; i < 4; ++i) |
| 974 | { |
| 975 | if (vals[i] & 0x8000) |
| 976 | { |
| 977 | // Swizzle bits: 00012345 -> 12345123 |
| 978 | r = (((vals[i] >> 10) & 0x1f) << 3) | (((vals[i] >> 10) & 0x1f) >> 2); |
| 979 | g = (((vals[i] >> 5) & 0x1f) << 3) | (((vals[i] >> 5) & 0x1f) >> 2); |
| 980 | b = (((vals[i]) & 0x1f) << 3) | (((vals[i]) & 0x1f) >> 2); |
| 981 | a = 0xFF; |
| 982 | } |
| 983 | else |
| 984 | { |
| 985 | a = (((vals[i] >> 12) & 0x7) << 5) | (((vals[i] >> 12) & 0x7) << 2) | |
| 986 | (((vals[i] >> 12) & 0x7) >> 1); |
| 987 | // Swizzle bits: 00001234 -> 12341234 |
| 988 | r = (((vals[i] >> 8) & 0xf) << 4) | ((vals[i] >> 8) & 0xf); |
| 989 | g = (((vals[i] >> 4) & 0xf) << 4) | ((vals[i] >> 4) & 0xf); |
| 990 | b = (((vals[i]) & 0xf) << 4) | ((vals[i]) & 0xf); |
| 991 | } |
| 992 | newdst[i] = r | (g << 8) | (b << 16) | (a << 24); |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | FUNCTION_TARGET_SSSE3 |
| 1001 | static void TexDecoder_DecodeImpl_RGBA8_SSSE3(u32* dst, const u8* src, int width, int height, |
| 1002 | TextureFormat texformat, const u8* tlut, |
| 1003 | TLUTFormat tlutfmt, int Wsteps4, int Wsteps8) |
| 1004 | { |
| 1005 | // xsacha optimized with SSSE3 intrinsics |
| 1006 | // Produces a ~30% speed improvement over SSE2 implementation |
| 1007 | for (int y = 0; y < height; y += 4) |
| 1008 | { |
| 1009 | for (int x = 0, yStep = (y / 4) * Wsteps4; x < width; x += 4, yStep++) |
| 1010 | { |
| 1011 | const u8* src2 = src + 64 * yStep; |
| 1012 | const __m128i mask0312 = _mm_set_epi8(12, 15, 13, 14, 8, 11, 9, 10, 4, 7, 5, 6, 0, 3, 1, 2); |
| 1013 | const __m128i ar0 = _mm_loadu_si128((__m128i*)src2); |
| 1014 | const __m128i ar1 = _mm_loadu_si128((__m128i*)src2 + 1); |
| 1015 | const __m128i gb0 = _mm_loadu_si128((__m128i*)src2 + 2); |
| 1016 | const __m128i gb1 = _mm_loadu_si128((__m128i*)src2 + 3); |
| 1017 | |
| 1018 | const __m128i rgba00 = _mm_shuffle_epi8(_mm_unpacklo_epi8(ar0, gb0), mask0312); |
| 1019 | const __m128i rgba01 = _mm_shuffle_epi8(_mm_unpackhi_epi8(ar0, gb0), mask0312); |
| 1020 | const __m128i rgba10 = _mm_shuffle_epi8(_mm_unpacklo_epi8(ar1, gb1), mask0312); |
| 1021 | const __m128i rgba11 = _mm_shuffle_epi8(_mm_unpackhi_epi8(ar1, gb1), mask0312); |
| 1022 | |
| 1023 | __m128i* dst128 = (__m128i*)(dst + (y + 0) * width + x); |
| 1024 | _mm_storeu_si128(dst128, rgba00); |
| 1025 | dst128 = (__m128i*)(dst + (y + 1) * width + x); |
| 1026 | _mm_storeu_si128(dst128, rgba01); |
| 1027 | dst128 = (__m128i*)(dst + (y + 2) * width + x); |
| 1028 | _mm_storeu_si128(dst128, rgba10); |
| 1029 | dst128 = (__m128i*)(dst + (y + 3) * width + x); |
| 1030 | _mm_storeu_si128(dst128, rgba11); |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | static void TexDecoder_DecodeImpl_RGBA8(u32* dst, const u8* src, int width, int height, |
| 1036 | TextureFormat texformat, const u8* tlut, TLUTFormat tlutfmt, |
| 1037 | int Wsteps4, int Wsteps8) |
| 1038 | { |
| 1039 | // JSD optimized with SSE2 intrinsics |
| 1040 | // Produces a ~68% speed improvement over reference C implementation. |
| 1041 | for (int y = 0; y < height; y += 4) |
| 1042 | { |
| 1043 | for (int x = 0, yStep = (y / 4) * Wsteps4; x < width; x += 4, yStep++) |
| 1044 | { |
| 1045 | // Input is divided up into 16-bit words. The texels are split up into AR and GB |
| 1046 | // components where all AR components come grouped up first in 32 bytes followed by the GB |
| 1047 | // components in 32 bytes. We are processing 16 texels per each loop iteration, numbered from |
| 1048 | // 0-f. |
| 1049 | // |
| 1050 | // Convention is: |
| 1051 | // one byte is [component-name texel-number] |
| 1052 | // __m128i is (4-bytes 4-bytes 4-bytes 4-bytes) |
| 1053 | // |
| 1054 | // Input is: |
| 1055 | // ([A 7][R 7][A 6][R 6] [A 5][R 5][A 4][R 4] [A 3][R 3][A 2][R 2] [A 1][R 1][A 0][R 0]) |
| 1056 | // ([A f][R f][A e][R e] [A d][R d][A c][R c] [A b][R b][A a][R a] [A 9][R 9][A 8][R 8]) |
| 1057 | // ([G 7][B 7][G 6][B 6] [G 5][B 5][G 4][B 4] [G 3][B 3][G 2][B 2] [G 1][B 1][G 0][B 0]) |
| 1058 | // ([G f][B f][G e][B e] [G d][B d][G c][B c] [G b][B b][G a][B a] [G 9][B 9][G 8][B 8]) |
| 1059 | // |
| 1060 | // Output is: |
| 1061 | // (RGBA3 RGBA2 RGBA1 RGBA0) |
| 1062 | // (RGBA7 RGBA6 RGBA5 RGBA4) |
| 1063 | // (RGBAb RGBAa RGBA9 RGBA8) |
| 1064 | // (RGBAf RGBAe RGBAd RGBAc) |
| 1065 | const u8* src2 = src + 64 * yStep; |
| 1066 | // Loads the 1st half of AR components ([A 7][R 7][A 6][R 6] [A 5][R 5][A 4][R 4] [A 3][R |
| 1067 | // 3][A 2][R 2] [A 1][R 1][A 0][R 0]) |
| 1068 | const __m128i ar0 = _mm_loadu_si128((__m128i*)src2); |
| 1069 | // Loads the 2nd half of AR components ([A f][R f][A e][R e] [A d][R d][A c][R c] [A b][R |
| 1070 | // b][A a][R a] [A 9][R 9][A 8][R 8]) |
| 1071 | const __m128i ar1 = _mm_loadu_si128((__m128i*)src2 + 1); |
| 1072 | // Loads the 1st half of GB components ([G 7][B 7][G 6][B 6] [G 5][B 5][G 4][B 4] [G 3][B |
| 1073 | // 3][G 2][B 2] [G 1][B 1][G 0][B 0]) |
| 1074 | const __m128i gb0 = _mm_loadu_si128((__m128i*)src2 + 2); |
| 1075 | // Loads the 2nd half of GB components ([G f][B f][G e][B e] [G d][B d][G c][B c] [G b][B |
| 1076 | // b][G a][B a] [G 9][B 9][G 8][B 8]) |
| 1077 | const __m128i gb1 = _mm_loadu_si128((__m128i*)src2 + 3); |
| 1078 | __m128i rgba00, rgba01, rgba10, rgba11; |
| 1079 | const __m128i kMask_x000f = _mm_set_epi32(0x000000FFL, 0x000000FFL, 0x000000FFL, 0x000000FFL); |
| 1080 | const __m128i kMask_xf000 = _mm_set_epi32(0xFF000000L, 0xFF000000L, 0xFF000000L, 0xFF000000L); |
| 1081 | const __m128i kMask_x0ff0 = _mm_set_epi32(0x00FFFF00L, 0x00FFFF00L, 0x00FFFF00L, 0x00FFFF00L); |
| 1082 | // Expand the AR components to fill out 32-bit words: |
| 1083 | // ([A 7][R 7][A 6][R 6] [A 5][R 5][A 4][R 4] [A 3][R 3][A 2][R 2] [A 1][R 1][A 0][R 0]) |
| 1084 | // -> ([A 3][A 3][R 3][R 3] [A 2][A 2][R 2][R 2] [A 1][A 1][R 1][R 1] [A 0][A 0][R 0][R 0]) |
| 1085 | const __m128i aarr00 = _mm_unpacklo_epi8(ar0, ar0); |
| 1086 | // ([A 7][R 7][A 6][R 6] [A 5][R 5][A 4][R 4] [A 3][R 3][A 2][R 2] [A 1][R 1][A 0][R 0]) |
| 1087 | // -> ([A 7][A 7][R 7][R 7] [A 6][A 6][R 6][R 6] [A 5][A 5][R 5][R 5] [A 4][A 4][R 4][R 4]) |
| 1088 | const __m128i aarr01 = _mm_unpackhi_epi8(ar0, ar0); |
| 1089 | // ([A f][R f][A e][R e] [A d][R d][A c][R c] [A b][R b][A a][R a] [A 9][R 9][A 8][R 8]) |
| 1090 | // -> ([A b][A b][R b][R b] [A a][A a][R a][R a] [A 9][A 9][R 9][R 9] [A 8][A 8][R 8][R 8]) |
| 1091 | const __m128i aarr10 = _mm_unpacklo_epi8(ar1, ar1); |
| 1092 | // ([A f][R f][A e][R e] [A d][R d][A c][R c] [A b][R b][A a][R a] [A 9][R 9][A 8][R 8]) |
| 1093 | // -> ([A f][A f][R f][R f] [A e][A e][R e][R e] [A d][A d][R d][R d] [A c][A c][R c][R c]) |
| 1094 | const __m128i aarr11 = _mm_unpackhi_epi8(ar1, ar1); |
| 1095 | |
| 1096 | // Move A right 16 bits and mask off everything but the lowest 8 bits to get A in its |
| 1097 | // final place: |
| 1098 | const __m128i ___a00 = _mm_and_si128(_mm_srli_epi32(aarr00, 16), kMask_x000f); |
| 1099 | // Move R left 16 bits and mask off everything but the highest 8 bits to get R in its |
| 1100 | // final place: |
| 1101 | const __m128i r___00 = _mm_and_si128(_mm_slli_epi32(aarr00, 16), kMask_xf000); |
| 1102 | // OR the two together to get R and A in their final places: |
| 1103 | const __m128i r__a00 = _mm_or_si128(r___00, ___a00); |
| 1104 | |
| 1105 | const __m128i ___a01 = _mm_and_si128(_mm_srli_epi32(aarr01, 16), kMask_x000f); |
| 1106 | const __m128i r___01 = _mm_and_si128(_mm_slli_epi32(aarr01, 16), kMask_xf000); |
| 1107 | const __m128i r__a01 = _mm_or_si128(r___01, ___a01); |
| 1108 | |
| 1109 | const __m128i ___a10 = _mm_and_si128(_mm_srli_epi32(aarr10, 16), kMask_x000f); |
| 1110 | const __m128i r___10 = _mm_and_si128(_mm_slli_epi32(aarr10, 16), kMask_xf000); |
| 1111 | const __m128i r__a10 = _mm_or_si128(r___10, ___a10); |
| 1112 | |
| 1113 | const __m128i ___a11 = _mm_and_si128(_mm_srli_epi32(aarr11, 16), kMask_x000f); |
| 1114 | const __m128i r___11 = _mm_and_si128(_mm_slli_epi32(aarr11, 16), kMask_xf000); |
| 1115 | const __m128i r__a11 = _mm_or_si128(r___11, ___a11); |
| 1116 | |
| 1117 | // Expand the GB components to fill out 32-bit words: |
| 1118 | // ([G 7][B 7][G 6][B 6] [G 5][B 5][G 4][B 4] [G 3][B 3][G 2][B 2] [G 1][B 1][G 0][B 0]) |
| 1119 | // -> ([G 3][G 3][B 3][B 3] [G 2][G 2][B 2][B 2] [G 1][G 1][B 1][B 1] [G 0][G 0][B 0][B 0]) |
| 1120 | const __m128i ggbb00 = _mm_unpacklo_epi8(gb0, gb0); |
| 1121 | // ([G 7][B 7][G 6][B 6] [G 5][B 5][G 4][B 4] [G 3][B 3][G 2][B 2] [G 1][B 1][G 0][B 0]) |
| 1122 | // -> ([G 7][G 7][B 7][B 7] [G 6][G 6][B 6][B 6] [G 5][G 5][B 5][B 5] [G 4][G 4][B 4][B 4]) |
| 1123 | const __m128i ggbb01 = _mm_unpackhi_epi8(gb0, gb0); |
| 1124 | // ([G f][B f][G e][B e] [G d][B d][G c][B c] [G b][B b][G a][B a] [G 9][B 9][G 8][B 8]) |
| 1125 | // -> ([G b][G b][B b][B b] [G a][G a][B a][B a] [G 9][G 9][B 9][B 9] [G 8][G 8][B 8][B 8]) |
| 1126 | const __m128i ggbb10 = _mm_unpacklo_epi8(gb1, gb1); |
| 1127 | // ([G f][B f][G e][B e] [G d][B d][G c][B c] [G b][B b][G a][B a] [G 9][B 9][G 8][B 8]) |
| 1128 | // -> ([G f][G f][B f][B f] [G e][G e][B e][B e] [G d][G d][B d][B d] [G c][G c][B c][B c]) |
| 1129 | const __m128i ggbb11 = _mm_unpackhi_epi8(gb1, gb1); |
| 1130 | |
| 1131 | // G and B are already in perfect spots in the center, just remove the extra copies in the |
| 1132 | // 1st and 4th positions: |
| 1133 | const __m128i _gb_00 = _mm_and_si128(ggbb00, kMask_x0ff0); |
| 1134 | const __m128i _gb_01 = _mm_and_si128(ggbb01, kMask_x0ff0); |
| 1135 | const __m128i _gb_10 = _mm_and_si128(ggbb10, kMask_x0ff0); |
| 1136 | const __m128i _gb_11 = _mm_and_si128(ggbb11, kMask_x0ff0); |
| 1137 | |
| 1138 | // Now join up R__A and _GB_ to get RGBA! |
| 1139 | rgba00 = _mm_or_si128(r__a00, _gb_00); |
| 1140 | rgba01 = _mm_or_si128(r__a01, _gb_01); |
| 1141 | rgba10 = _mm_or_si128(r__a10, _gb_10); |
| 1142 | rgba11 = _mm_or_si128(r__a11, _gb_11); |
| 1143 | // Write em out! |
| 1144 | __m128i* dst128 = (__m128i*)(dst + (y + 0) * width + x); |
| 1145 | _mm_storeu_si128(dst128, rgba00); |
| 1146 | dst128 = (__m128i*)(dst + (y + 1) * width + x); |
| 1147 | _mm_storeu_si128(dst128, rgba01); |
| 1148 | dst128 = (__m128i*)(dst + (y + 2) * width + x); |
| 1149 | _mm_storeu_si128(dst128, rgba10); |
| 1150 | dst128 = (__m128i*)(dst + (y + 3) * width + x); |
| 1151 | _mm_storeu_si128(dst128, rgba11); |
| 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | static void TexDecoder_DecodeImpl_CMPR(u32* dst, const u8* src, int width, int height, |
| 1157 | TextureFormat texformat, const u8* tlut, TLUTFormat tlutfmt, |
| 1158 | int Wsteps4, int Wsteps8) |
| 1159 | { |
| 1160 | // The metroid games use this format almost exclusively. |
| 1161 | // JSD optimized with SSE2 intrinsics. |
| 1162 | // Produces a ~50% improvement for x86 and a ~40% improvement for x64 in speed over reference |
| 1163 | // C implementation. The x64 compiled reference C code is faster than the x86 compiled reference |
| 1164 | // C code, but the SSE2 is faster than both. |
| 1165 | for (int y = 0; y < height; y += 8) |
| 1166 | { |
| 1167 | for (int x = 0, yStep = (y / 8) * Wsteps8; x < width; x += 8, yStep++) |
| 1168 | { |
| 1169 | // We handle two DXT blocks simultaneously to take full advantage of SSE2's 128-bit registers. |
| 1170 | // This is ideal because a single DXT block contains 2 RGBA colors when decoded from their |
| 1171 | // 16-bit. Two DXT blocks therefore contain 4 RGBA colors to be processed. The processing is |
| 1172 | // parallelizable at this level, so we do. |
| 1173 | for (int z = 0, xStep = 2 * yStep; z < 2; ++z, xStep++) |
| 1174 | { |
| 1175 | // JSD NOTE: You may see many strange patterns of behavior in the below code, but they |
| 1176 | // are for performance reasons. Sometimes, calculating what should be obvious hard-coded |
| 1177 | // constants is faster than loading their values from memory. Unfortunately, there is no |
| 1178 | // way to inline 128-bit constants from opcodes so they must be loaded from memory. This |
| 1179 | // seems a little ridiculous to me in that you can't even generate a constant value of 1 |
| 1180 | // without having to load it from memory. So, I stored the minimal constant I could, |
| 1181 | // 128-bits worth of 1s :). Then I use sequences of shifts to squash it to the appropriate |
| 1182 | // size and bitpositions that I need. |
| 1183 | const __m128i allFFs128 = _mm_cmpeq_epi32(_mm_setzero_si128(), _mm_setzero_si128()); |
| 1184 | |
| 1185 | // Load 128 bits, i.e. two DXTBlocks (64-bits each) |
| 1186 | const __m128i dxt = _mm_loadu_si128((__m128i*)(src + sizeof(struct DXTBlock) * 2 * xStep)); |
| 1187 | |
| 1188 | // Copy the 2-bit indices from each DXT block: |
| 1189 | alignas(16) u32 dxttmp[4]; |
| 1190 | _mm_store_si128((__m128i*)dxttmp, dxt); |
| 1191 | |
| 1192 | u32 dxt0sel = dxttmp[1]; |
| 1193 | u32 dxt1sel = dxttmp[3]; |
| 1194 | |
| 1195 | __m128i argb888x4; |
| 1196 | __m128i c1 = _mm_unpackhi_epi16(dxt, dxt); |
| 1197 | c1 = _mm_slli_si128(c1, 8); |
| 1198 | const __m128i c0 = |
| 1199 | _mm_or_si128(c1, _mm_srli_si128(_mm_slli_si128(_mm_unpacklo_epi16(dxt, dxt), 8), 8)); |
| 1200 | |
| 1201 | // Compare rgb0 to rgb1: |
| 1202 | // Each 32-bit word will contain either 0xFFFFFFFF or 0x00000000 for true/false. |
| 1203 | const __m128i c0cmp = _mm_srli_epi32(_mm_slli_epi32(_mm_srli_epi64(c0, 8), 16), 16); |
| 1204 | const __m128i c0shr = _mm_srli_epi64(c0cmp, 32); |
| 1205 | const __m128i cmprgb0rgb1 = _mm_cmpgt_epi32(c0cmp, c0shr); |
| 1206 | |
| 1207 | int cmp0 = _mm_extract_epi16(cmprgb0rgb1, 0); |
| 1208 | int cmp1 = _mm_extract_epi16(cmprgb0rgb1, 4); |
| 1209 | |
| 1210 | // green: |
| 1211 | // NOTE: We start with the larger number of bits (6) first for G and shift the mask down |
| 1212 | // 1 bit to get a 5-bit mask later for R and B components. |
| 1213 | // low6mask == _mm_set_epi32(0x0000FC00, 0x0000FC00, 0x0000FC00, 0x0000FC00) |
| 1214 | const __m128i low6mask = _mm_slli_epi32(_mm_srli_epi32(allFFs128, 24 + 2), 8 + 2); |
| 1215 | const __m128i gtmp = _mm_srli_epi32(c0, 3); |
| 1216 | const __m128i g0 = _mm_and_si128(gtmp, low6mask); |
| 1217 | // low3mask == _mm_set_epi32(0x00000300, 0x00000300, 0x00000300, 0x00000300) |
| 1218 | const __m128i g1 = _mm_and_si128( |
| 1219 | _mm_srli_epi32(gtmp, 6), _mm_set_epi32(0x00000300, 0x00000300, 0x00000300, 0x00000300)); |
| 1220 | argb888x4 = _mm_or_si128(g0, g1); |
| 1221 | // red: |
| 1222 | // low5mask == _mm_set_epi32(0x000000F8, 0x000000F8, 0x000000F8, 0x000000F8) |
| 1223 | const __m128i low5mask = _mm_slli_epi32(_mm_srli_epi32(low6mask, 8 + 3), 3); |
| 1224 | const __m128i r0 = _mm_and_si128(c0, low5mask); |
| 1225 | const __m128i r1 = _mm_srli_epi32(r0, 5); |
| 1226 | argb888x4 = _mm_or_si128(argb888x4, _mm_or_si128(r0, r1)); |
| 1227 | // blue: |
| 1228 | // _mm_slli_epi32(low5mask, 16) == _mm_set_epi32(0x00F80000, 0x00F80000, 0x00F80000, |
| 1229 | // 0x00F80000) |
| 1230 | const __m128i b0 = _mm_and_si128(_mm_srli_epi32(c0, 5), _mm_slli_epi32(low5mask, 16)); |
| 1231 | const __m128i b1 = _mm_srli_epi16(b0, 5); |
| 1232 | // OR in the fixed alpha component |
| 1233 | // _mm_slli_epi32( allFFs128, 24 ) == _mm_set_epi32(0xFF000000, 0xFF000000, 0xFF000000, |
| 1234 | // 0xFF000000) |
| 1235 | argb888x4 = _mm_or_si128(_mm_or_si128(argb888x4, _mm_slli_epi32(allFFs128, 24)), |
| 1236 | _mm_or_si128(b0, b1)); |
| 1237 | // calculate RGB2 and RGB3: |
| 1238 | const __m128i rgb0 = _mm_shuffle_epi32(argb888x4, _MM_SHUFFLE(2, 2, 0, 0)); |
| 1239 | const __m128i rgb1 = _mm_shuffle_epi32(argb888x4, _MM_SHUFFLE(3, 3, 1, 1)); |
| 1240 | const __m128i rrggbb0 = |
| 1241 | _mm_and_si128(_mm_unpacklo_epi8(rgb0, rgb0), _mm_srli_epi16(allFFs128, 8)); |
| 1242 | const __m128i rrggbb1 = |
| 1243 | _mm_and_si128(_mm_unpacklo_epi8(rgb1, rgb1), _mm_srli_epi16(allFFs128, 8)); |
| 1244 | const __m128i rrggbb01 = |
| 1245 | _mm_and_si128(_mm_unpackhi_epi8(rgb0, rgb0), _mm_srli_epi16(allFFs128, 8)); |
| 1246 | const __m128i rrggbb11 = |
| 1247 | _mm_and_si128(_mm_unpackhi_epi8(rgb1, rgb1), _mm_srli_epi16(allFFs128, 8)); |
| 1248 | |
| 1249 | __m128i rgb2, rgb3; |
| 1250 | |
| 1251 | // if (rgb0 > rgb1): |
| 1252 | if (cmp0 != 0) |
| 1253 | { |
| 1254 | // RGB2 = (RGB0 * 5 + RGB1 * 3) / 8 = (RGB0 << 2 + RGB1 << 1 + (RGB0 + RGB1)) >> 3 |
| 1255 | // RGB3 = (RGB0 * 3 + RGB1 * 5) / 8 = (RGB0 << 1 + RGB1 << 2 + (RGB0 + RGB1)) >> 3 |
| 1256 | const __m128i rrggbbsum = _mm_add_epi16(rrggbb0, rrggbb1); |
| 1257 | |
| 1258 | const __m128i rrggbb0shl1 = _mm_slli_epi16(rrggbb0, 1); |
| 1259 | const __m128i rrggbb0shl2 = _mm_slli_epi16(rrggbb0, 2); |
| 1260 | |
| 1261 | const __m128i rrggbb1shl1 = _mm_slli_epi16(rrggbb1, 1); |
| 1262 | const __m128i rrggbb1shl2 = _mm_slli_epi16(rrggbb1, 2); |
| 1263 | |
| 1264 | const __m128i rrggbb2 = |
| 1265 | _mm_srli_epi16(_mm_add_epi16(_mm_add_epi16(rrggbb0shl2, rrggbb1shl1), rrggbbsum), 3); |
| 1266 | const __m128i rrggbb3 = |
| 1267 | _mm_srli_epi16(_mm_add_epi16(_mm_add_epi16(rrggbb0shl1, rrggbb1shl2), rrggbbsum), 3); |
| 1268 | |
| 1269 | const __m128i rgb2dup = _mm_packus_epi16(rrggbb2, rrggbb2); |
| 1270 | const __m128i rgb3dup = _mm_packus_epi16(rrggbb3, rrggbb3); |
| 1271 | |
| 1272 | rgb2 = _mm_and_si128(rgb2dup, _mm_srli_si128(allFFs128, 8)); |
| 1273 | rgb3 = _mm_and_si128(rgb3dup, _mm_srli_si128(allFFs128, 8)); |
| 1274 | } |
| 1275 | else |
| 1276 | { |
| 1277 | // RGB2b = avg(RGB0, RGB1) |
| 1278 | const __m128i rrggbb21 = _mm_srai_epi16(_mm_add_epi16(rrggbb0, rrggbb1), 1); |
| 1279 | const __m128i rgb210 = _mm_srli_si128(_mm_packus_epi16(rrggbb21, rrggbb21), 8); |
| 1280 | rgb2 = rgb210; |
| 1281 | rgb3 = _mm_and_si128(rgb210, _mm_srli_epi32(allFFs128, 8)); |
| 1282 | } |
| 1283 | |
| 1284 | // if (rgb0 > rgb1): |
| 1285 | if (cmp1 != 0) |
| 1286 | { |
| 1287 | // RGB2 = (RGB0 * 5 + RGB1 * 3) / 8 = (RGB0 << 2 + RGB1 << 1 + (RGB0 + RGB1)) >> 3 |
| 1288 | // RGB3 = (RGB0 * 3 + RGB1 * 5) / 8 = (RGB0 << 1 + RGB1 << 2 + (RGB0 + RGB1)) >> 3 |
| 1289 | const __m128i rrggbbsum = _mm_add_epi16(rrggbb01, rrggbb11); |
| 1290 | |
| 1291 | const __m128i rrggbb0shl1 = _mm_slli_epi16(rrggbb01, 1); |
| 1292 | const __m128i rrggbb0shl2 = _mm_slli_epi16(rrggbb01, 2); |
| 1293 | |
| 1294 | const __m128i rrggbb1shl1 = _mm_slli_epi16(rrggbb11, 1); |
| 1295 | const __m128i rrggbb1shl2 = _mm_slli_epi16(rrggbb11, 2); |
| 1296 | |
| 1297 | const __m128i rrggbb2 = |
| 1298 | _mm_srli_epi16(_mm_add_epi16(_mm_add_epi16(rrggbb0shl2, rrggbb1shl1), rrggbbsum), 3); |
| 1299 | const __m128i rrggbb3 = |
| 1300 | _mm_srli_epi16(_mm_add_epi16(_mm_add_epi16(rrggbb0shl1, rrggbb1shl2), rrggbbsum), 3); |
| 1301 | |
| 1302 | const __m128i rgb2dup = _mm_packus_epi16(rrggbb2, rrggbb2); |
| 1303 | const __m128i rgb3dup = _mm_packus_epi16(rrggbb3, rrggbb3); |
| 1304 | |
| 1305 | rgb2 = _mm_or_si128(rgb2, _mm_and_si128(rgb2dup, _mm_slli_si128(allFFs128, 8))); |
| 1306 | rgb3 = _mm_or_si128(rgb3, _mm_and_si128(rgb3dup, _mm_slli_si128(allFFs128, 8))); |
| 1307 | } |
| 1308 | else |
| 1309 | { |
| 1310 | // RGB2b = avg(RGB0, RGB1) |
| 1311 | const __m128i rrggbb211 = _mm_srai_epi16(_mm_add_epi16(rrggbb01, rrggbb11), 1); |
| 1312 | const __m128i rgb211 = _mm_slli_si128(_mm_packus_epi16(rrggbb211, rrggbb211), 8); |
| 1313 | rgb2 = _mm_or_si128(rgb2, rgb211); |
| 1314 | |
| 1315 | // _mm_srli_epi32( allFFs128, 8 ) == _mm_set_epi32(0x00FFFFFF, 0x00FFFFFF, 0x00FFFFFF, |
| 1316 | // 0x00FFFFFF) |
| 1317 | // Make this color fully transparent: |
| 1318 | rgb3 = _mm_or_si128(rgb3, _mm_and_si128(_mm_and_si128(rgb2, _mm_srli_epi32(allFFs128, 8)), |
| 1319 | _mm_slli_si128(allFFs128, 8))); |
| 1320 | } |
| 1321 | |
| 1322 | // Create an array for color lookups for DXT0 so we can use the 2-bit indices: |
| 1323 | const __m128i mmcolors0 = _mm_or_si128( |
| 1324 | _mm_or_si128(_mm_srli_si128(_mm_slli_si128(argb888x4, 8), 8), |
| 1325 | _mm_slli_si128(_mm_srli_si128(_mm_slli_si128(rgb2, 8), 8 + 4), 8)), |
| 1326 | _mm_slli_si128(_mm_srli_si128(rgb3, 4), 8 + 4)); |
| 1327 | |
| 1328 | // Create an array for color lookups for DXT1 so we can use the 2-bit indices: |
| 1329 | const __m128i mmcolors1 = |
| 1330 | _mm_or_si128(_mm_or_si128(_mm_srli_si128(argb888x4, 8), |
| 1331 | _mm_slli_si128(_mm_srli_si128(rgb2, 8 + 4), 8)), |
| 1332 | _mm_slli_si128(_mm_srli_si128(rgb3, 8 + 4), 8 + 4)); |
| 1333 | |
| 1334 | // The #ifdef CHECKs here and below are to compare correctness of output against the reference code. |
| 1335 | // Don't use them in a normal build. |
| 1336 | #ifdef CHECK |
| 1337 | // REFERENCE: |
| 1338 | u32 tmp0[4][4], tmp1[4][4]; |
| 1339 | |
| 1340 | DecodeDXTBlock(&(tmp0[0][0]), |
| 1341 | reinterpret_cast<const DXTBlock*>(src + sizeof(DXTBlock) * 2 * xStep), 4); |
| 1342 | DecodeDXTBlock(&(tmp1[0][0]), |
| 1343 | reinterpret_cast<const DXTBlock*>((src + sizeof(DXTBlock) * 2 * xStep) + 8), |
| 1344 | 4); |
| 1345 | #endif |
| 1346 | |
| 1347 | u32* dst32 = (dst + (y + z * 4) * width + x); |
| 1348 | |
| 1349 | // Copy the colors here: |
| 1350 | alignas(16) u32 colors0[4]; |
| 1351 | alignas(16) u32 colors1[4]; |
| 1352 | _mm_store_si128((__m128i*)colors0, mmcolors0); |
| 1353 | _mm_store_si128((__m128i*)colors1, mmcolors1); |
| 1354 | |
| 1355 | // Row 0: |
| 1356 | dst32[(width * 0) + 0] = colors0[(dxt0sel >> ((0 * 8) + 6)) & 3]; |
| 1357 | dst32[(width * 0) + 1] = colors0[(dxt0sel >> ((0 * 8) + 4)) & 3]; |
| 1358 | dst32[(width * 0) + 2] = colors0[(dxt0sel >> ((0 * 8) + 2)) & 3]; |
| 1359 | dst32[(width * 0) + 3] = colors0[(dxt0sel >> ((0 * 8) + 0)) & 3]; |
| 1360 | dst32[(width * 0) + 4] = colors1[(dxt1sel >> ((0 * 8) + 6)) & 3]; |
| 1361 | dst32[(width * 0) + 5] = colors1[(dxt1sel >> ((0 * 8) + 4)) & 3]; |
| 1362 | dst32[(width * 0) + 6] = colors1[(dxt1sel >> ((0 * 8) + 2)) & 3]; |
| 1363 | dst32[(width * 0) + 7] = colors1[(dxt1sel >> ((0 * 8) + 0)) & 3]; |
| 1364 | #ifdef CHECK |
| 1365 | ASSERT(memcmp(&(tmp0[0]), &dst32[(width * 0)], 16) == 0); |
| 1366 | ASSERT(memcmp(&(tmp1[0]), &dst32[(width * 0) + 4], 16) == 0); |
| 1367 | #endif |
| 1368 | // Row 1: |
| 1369 | dst32[(width * 1) + 0] = colors0[(dxt0sel >> ((1 * 8) + 6)) & 3]; |
| 1370 | dst32[(width * 1) + 1] = colors0[(dxt0sel >> ((1 * 8) + 4)) & 3]; |
| 1371 | dst32[(width * 1) + 2] = colors0[(dxt0sel >> ((1 * 8) + 2)) & 3]; |
| 1372 | dst32[(width * 1) + 3] = colors0[(dxt0sel >> ((1 * 8) + 0)) & 3]; |
| 1373 | dst32[(width * 1) + 4] = colors1[(dxt1sel >> ((1 * 8) + 6)) & 3]; |
| 1374 | dst32[(width * 1) + 5] = colors1[(dxt1sel >> ((1 * 8) + 4)) & 3]; |
| 1375 | dst32[(width * 1) + 6] = colors1[(dxt1sel >> ((1 * 8) + 2)) & 3]; |
| 1376 | dst32[(width * 1) + 7] = colors1[(dxt1sel >> ((1 * 8) + 0)) & 3]; |
| 1377 | #ifdef CHECK |
| 1378 | ASSERT(memcmp(&(tmp0[1]), &dst32[(width * 1)], 16) == 0); |
| 1379 | ASSERT(memcmp(&(tmp1[1]), &dst32[(width * 1) + 4], 16) == 0); |
| 1380 | #endif |
| 1381 | // Row 2: |
| 1382 | dst32[(width * 2) + 0] = colors0[(dxt0sel >> ((2 * 8) + 6)) & 3]; |
| 1383 | dst32[(width * 2) + 1] = colors0[(dxt0sel >> ((2 * 8) + 4)) & 3]; |
| 1384 | dst32[(width * 2) + 2] = colors0[(dxt0sel >> ((2 * 8) + 2)) & 3]; |
| 1385 | dst32[(width * 2) + 3] = colors0[(dxt0sel >> ((2 * 8) + 0)) & 3]; |
| 1386 | dst32[(width * 2) + 4] = colors1[(dxt1sel >> ((2 * 8) + 6)) & 3]; |
| 1387 | dst32[(width * 2) + 5] = colors1[(dxt1sel >> ((2 * 8) + 4)) & 3]; |
| 1388 | dst32[(width * 2) + 6] = colors1[(dxt1sel >> ((2 * 8) + 2)) & 3]; |
| 1389 | dst32[(width * 2) + 7] = colors1[(dxt1sel >> ((2 * 8) + 0)) & 3]; |
| 1390 | #ifdef CHECK |
| 1391 | ASSERT(memcmp(&(tmp0[2]), &dst32[(width * 2)], 16) == 0); |
| 1392 | ASSERT(memcmp(&(tmp1[2]), &dst32[(width * 2) + 4], 16) == 0); |
| 1393 | #endif |
| 1394 | // Row 3: |
| 1395 | dst32[(width * 3) + 0] = colors0[(dxt0sel >> ((3 * 8) + 6)) & 3]; |
| 1396 | dst32[(width * 3) + 1] = colors0[(dxt0sel >> ((3 * 8) + 4)) & 3]; |
| 1397 | dst32[(width * 3) + 2] = colors0[(dxt0sel >> ((3 * 8) + 2)) & 3]; |
| 1398 | dst32[(width * 3) + 3] = colors0[(dxt0sel >> ((3 * 8) + 0)) & 3]; |
| 1399 | dst32[(width * 3) + 4] = colors1[(dxt1sel >> ((3 * 8) + 6)) & 3]; |
| 1400 | dst32[(width * 3) + 5] = colors1[(dxt1sel >> ((3 * 8) + 4)) & 3]; |
| 1401 | dst32[(width * 3) + 6] = colors1[(dxt1sel >> ((3 * 8) + 2)) & 3]; |
| 1402 | dst32[(width * 3) + 7] = colors1[(dxt1sel >> ((3 * 8) + 0)) & 3]; |
| 1403 | #ifdef CHECK |
| 1404 | ASSERT(memcmp(&(tmp0[3]), &dst32[(width * 3)], 16) == 0); |
| 1405 | ASSERT(memcmp(&(tmp1[3]), &dst32[(width * 3) + 4], 16) == 0); |
| 1406 | #endif |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | void _TexDecoder_DecodeImpl(u32* dst, const u8* src, int width, int height, TextureFormat texformat, |
| 1413 | const u8* tlut, TLUTFormat tlutfmt) |
| 1414 | { |
| 1415 | int Wsteps4 = (width + 3) / 4; |
| 1416 | int Wsteps8 = (width + 7) / 8; |
| 1417 | |
| 1418 | switch (texformat) |
| 1419 | { |
| 1420 | case TextureFormat::C4: |
| 1421 | TexDecoder_DecodeImpl_C4(dst, src, width, height, texformat, tlut, tlutfmt, Wsteps4, Wsteps8); |
| 1422 | break; |
| 1423 | |
| 1424 | case TextureFormat::I4: |
| 1425 | if (cpu_info.bSSSE3) |
| 1426 | TexDecoder_DecodeImpl_I4_SSSE3(dst, src, width, height, texformat, tlut, tlutfmt, Wsteps4, |
| 1427 | Wsteps8); |
| 1428 | else |
| 1429 | TexDecoder_DecodeImpl_I4(dst, src, width, height, texformat, tlut, tlutfmt, Wsteps4, Wsteps8); |
| 1430 | break; |
| 1431 | |
| 1432 | case TextureFormat::I8: |
| 1433 | if (cpu_info.bSSSE3) |
| 1434 | TexDecoder_DecodeImpl_I8_SSSE3(dst, src, width, height, texformat, tlut, tlutfmt, Wsteps4, |
| 1435 | Wsteps8); |
| 1436 | else |
| 1437 | TexDecoder_DecodeImpl_I8(dst, src, width, height, texformat, tlut, tlutfmt, Wsteps4, Wsteps8); |
| 1438 | break; |
| 1439 | |
| 1440 | case TextureFormat::C8: |
| 1441 | TexDecoder_DecodeImpl_C8(dst, src, width, height, texformat, tlut, tlutfmt, Wsteps4, Wsteps8); |
| 1442 | break; |
| 1443 | |
| 1444 | case TextureFormat::IA4: |
| 1445 | TexDecoder_DecodeImpl_IA4(dst, src, width, height, texformat, tlut, tlutfmt, Wsteps4, Wsteps8); |
| 1446 | break; |
| 1447 | |
| 1448 | case TextureFormat::IA8: |
| 1449 | if (cpu_info.bSSSE3) |
| 1450 | TexDecoder_DecodeImpl_IA8_SSSE3(dst, src, width, height, texformat, tlut, tlutfmt, Wsteps4, |
| 1451 | Wsteps8); |
| 1452 | else |
| 1453 | TexDecoder_DecodeImpl_IA8(dst, src, width, height, texformat, tlut, tlutfmt, Wsteps4, |
| 1454 | Wsteps8); |
| 1455 | break; |
| 1456 | |
| 1457 | case TextureFormat::C14X2: |
| 1458 | TexDecoder_DecodeImpl_C14X2(dst, src, width, height, texformat, tlut, tlutfmt, Wsteps4, |
| 1459 | Wsteps8); |
| 1460 | break; |
| 1461 | |
| 1462 | case TextureFormat::RGB565: |
| 1463 | TexDecoder_DecodeImpl_RGB565(dst, src, width, height, texformat, tlut, tlutfmt, Wsteps4, |
| 1464 | Wsteps8); |
| 1465 | break; |
| 1466 | |
| 1467 | case TextureFormat::RGB5A3: |
| 1468 | if (cpu_info.bSSSE3) |
| 1469 | TexDecoder_DecodeImpl_RGB5A3_SSSE3(dst, src, width, height, texformat, tlut, tlutfmt, Wsteps4, |
| 1470 | Wsteps8); |
| 1471 | else |
| 1472 | TexDecoder_DecodeImpl_RGB5A3(dst, src, width, height, texformat, tlut, tlutfmt, Wsteps4, |
| 1473 | Wsteps8); |
| 1474 | break; |
| 1475 | |
| 1476 | case TextureFormat::RGBA8: |
| 1477 | if (cpu_info.bSSSE3) |
| 1478 | TexDecoder_DecodeImpl_RGBA8_SSSE3(dst, src, width, height, texformat, tlut, tlutfmt, Wsteps4, |
| 1479 | Wsteps8); |
| 1480 | else |
| 1481 | TexDecoder_DecodeImpl_RGBA8(dst, src, width, height, texformat, tlut, tlutfmt, Wsteps4, |
| 1482 | Wsteps8); |
| 1483 | break; |
| 1484 | |
| 1485 | case TextureFormat::CMPR: |
| 1486 | TexDecoder_DecodeImpl_CMPR(dst, src, width, height, texformat, tlut, tlutfmt, Wsteps4, Wsteps8); |
| 1487 | break; |
| 1488 | |
| 1489 | case TextureFormat::XFB: |
| 1490 | TexDecoder_DecodeXFB(reinterpret_cast<u8*>(dst), src, width, height, width * 2); |
| 1491 | break; |
| 1492 | |
| 1493 | default: |
| 1494 | PanicAlertFmt("Invalid Texture Format {}! (_TexDecoder_DecodeImpl)", texformat); |
| 1495 | break; |
| 1496 | } |
| 1497 | } |