| 1 |
/* |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#ifdef THORVG_AVX_VECTOR_SUPPORT |
| 24 |
|
| 25 |
#include <immintrin.h> |
| 26 |
|
| 27 |
#define N_32BITS_IN_128REG 4 |
| 28 |
#define N_32BITS_IN_256REG 8 |
| 29 |
|
| 30 |
static inline __m128i ALPHA_BLEND(__m128i c, __m128i a) |
| 31 |
{ |
| 32 |
|
| 33 |
auto AG = _mm_set1_epi32(0xff00ff00); |
| 34 |
auto RB = _mm_set1_epi32(0x00ff00ff); |
| 35 |
|
| 36 |
|
| 37 |
auto aAG = _mm_and_si128(a, AG); |
| 38 |
auto aRB = _mm_and_si128(a, RB); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
auto even = _mm_and_si128(c, RB); |
| 46 |
even = _mm_mullo_epi16(even, aRB); |
| 47 |
even =_mm_add_epi16(even, RB); |
| 48 |
even = _mm_srli_epi16(even, 8); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
auto odd = _mm_and_si128(c, AG); |
| 56 |
odd = _mm_mulhi_epu16(odd, aAG); |
| 57 |
odd = _mm_add_epi16(odd, RB); |
| 58 |
odd = _mm_and_si128(odd, AG); |
| 59 |
|
| 60 |
|
| 61 |
return _mm_or_si128(odd, even); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
static void avxRasterGrayscale8(uint8_t* dst, uint8_t val, uint32_t offset, int32_t len) |
| 66 |
{ |
| 67 |
dst += offset; |
| 68 |
|
| 69 |
__m256i vecVal = _mm256_set1_epi8(val); |
| 70 |
|
| 71 |
int32_t i = 0; |
| 72 |
for (; i <= len - 32; i += 32) { |
| 73 |
_mm256_storeu_si256((__m256i*)(dst + i), vecVal); |
| 74 |
} |
| 75 |
|
| 76 |
for (; i < len; ++i) { |
| 77 |
dst[i] = val; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
static void avxRasterPixel32(uint32_t *dst, uint32_t val, uint32_t offset, int32_t len) |
| 83 |
{ |
| 84 |
|
| 85 |
uint32_t iterations = len / N_32BITS_IN_256REG; |
| 86 |
uint32_t avxFilled = iterations * N_32BITS_IN_256REG; |
| 87 |
|
| 88 |
|
| 89 |
dst += offset; |
| 90 |
|
| 91 |
|
| 92 |
for (uint32_t i = 0; i < iterations; ++i, dst += N_32BITS_IN_256REG) { |
| 93 |
_mm256_storeu_si256((__m256i*)dst, _mm256_set1_epi32(val)); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
int32_t leftovers = len - avxFilled; |
| 98 |
while (leftovers--) *dst++ = val; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
static bool avxRasterTranslucentRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a) |
| 103 |
{ |
| 104 |
auto h = static_cast<uint32_t>(region.max.y - region.min.y); |
| 105 |
auto w = static_cast<uint32_t>(region.max.x - region.min.x); |
| 106 |
|
| 107 |
|
| 108 |
if (surface->channelSize == sizeof(uint32_t)) { |
| 109 |
auto color = surface->join(r, g, b, a); |
| 110 |
auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x; |
| 111 |
|
| 112 |
uint32_t ialpha = 255 - a; |
| 113 |
|
| 114 |
auto avxColor = _mm_set1_epi32(color); |
| 115 |
auto avxIalpha = _mm_set1_epi8(ialpha); |
| 116 |
|
| 117 |
for (uint32_t y = 0; y < h; ++y) { |
| 118 |
auto dst = &buffer[y * surface->stride]; |
| 119 |
|
| 120 |
|
| 121 |
auto notAligned = ((uintptr_t)dst & 0xf) / 4; |
| 122 |
if (notAligned) { |
| 123 |
notAligned = (N_32BITS_IN_128REG - notAligned > w ? w : N_32BITS_IN_128REG - notAligned); |
| 124 |
for (uint32_t x = 0; x < notAligned; ++x, ++dst) { |
| 125 |
*dst = color + ALPHA_BLEND(*dst, ialpha); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
uint32_t iterations = (w - notAligned) / N_32BITS_IN_128REG; |
| 131 |
uint32_t avxFilled = iterations * N_32BITS_IN_128REG; |
| 132 |
auto avxDst = (__m128i*)dst; |
| 133 |
for (uint32_t x = 0; x < iterations; ++x, ++avxDst) { |
| 134 |
*avxDst = _mm_add_epi32(avxColor, ALPHA_BLEND(*avxDst, avxIalpha)); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
int32_t leftovers = w - notAligned - avxFilled; |
| 139 |
dst += avxFilled; |
| 140 |
while (leftovers--) { |
| 141 |
*dst = color + ALPHA_BLEND(*dst, ialpha); |
| 142 |
dst++; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
} else if (surface->channelSize == sizeof(uint8_t)) { |
| 147 |
TVGLOG("SW_ENGINE", "Require AVX Optimization, Channel Size = %d", surface->channelSize); |
| 148 |
auto buffer = surface->buf8 + (region.min.y * surface->stride) + region.min.x; |
| 149 |
auto ialpha = ~a; |
| 150 |
for (uint32_t y = 0; y < h; ++y) { |
| 151 |
auto dst = &buffer[y * surface->stride]; |
| 152 |
for (uint32_t x = 0; x < w; ++x, ++dst) { |
| 153 |
*dst = a + MULTIPLY(*dst, ialpha); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
static bool avxRasterTranslucentRle(SwSurface* surface, const SwRle* rle, uint8_t r, uint8_t g, uint8_t b, uint8_t a) |
| 162 |
{ |
| 163 |
auto span = rle->spans; |
| 164 |
|
| 165 |
|
| 166 |
if (surface->channelSize == sizeof(uint32_t)) { |
| 167 |
auto color = surface->join(r, g, b, a); |
| 168 |
uint32_t src; |
| 169 |
|
| 170 |
for (uint32_t i = 0; i < rle->size; ++i) { |
| 171 |
auto dst = &surface->buf32[span->y * surface->stride + span->x]; |
| 172 |
|
| 173 |
if (span->coverage < 255) src = ALPHA_BLEND(color, span->coverage); |
| 174 |
else src = color; |
| 175 |
|
| 176 |
auto ialpha = IA(src); |
| 177 |
|
| 178 |
|
| 179 |
auto notAligned = ((uintptr_t)dst & 0xf) / 4; |
| 180 |
if (notAligned) { |
| 181 |
notAligned = (N_32BITS_IN_128REG - notAligned > span->len ? span->len : N_32BITS_IN_128REG - notAligned); |
| 182 |
for (uint32_t x = 0; x < notAligned; ++x, ++dst) { |
| 183 |
*dst = src + ALPHA_BLEND(*dst, ialpha); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
uint32_t iterations = (span->len - notAligned) / N_32BITS_IN_128REG; |
| 190 |
uint32_t avxFilled = 0; |
| 191 |
if (iterations > 0) { |
| 192 |
auto avxSrc = _mm_set1_epi32(src); |
| 193 |
auto avxIalpha = _mm_set1_epi8(ialpha); |
| 194 |
|
| 195 |
avxFilled = iterations * N_32BITS_IN_128REG; |
| 196 |
auto avxDst = (__m128i*)dst; |
| 197 |
for (uint32_t x = 0; x < iterations; ++x, ++avxDst) { |
| 198 |
*avxDst = _mm_add_epi32(avxSrc, ALPHA_BLEND(*avxDst, avxIalpha)); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
int32_t leftovers = span->len - notAligned - avxFilled; |
| 204 |
dst += avxFilled; |
| 205 |
while (leftovers--) { |
| 206 |
*dst = src + ALPHA_BLEND(*dst, ialpha); |
| 207 |
dst++; |
| 208 |
} |
| 209 |
|
| 210 |
++span; |
| 211 |
} |
| 212 |
|
| 213 |
} else if (surface->channelSize == sizeof(uint8_t)) { |
| 214 |
TVGLOG("SW_ENGINE", "Require AVX Optimization, Channel Size = %d", surface->channelSize); |
| 215 |
uint8_t src; |
| 216 |
for (uint32_t i = 0; i < rle->size; ++i, ++span) { |
| 217 |
auto dst = &surface->buf8[span->y * surface->stride + span->x]; |
| 218 |
if (span->coverage < 255) src = MULTIPLY(span->coverage, a); |
| 219 |
else src = a; |
| 220 |
auto ialpha = ~a; |
| 221 |
for (uint32_t x = 0; x < span->len; ++x, ++dst) { |
| 222 |
*dst = src + MULTIPLY(*dst, ialpha); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
return true; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
#endif |