Debian Codegraph @

thirdparty/thorvg/src/renderer/sw_engine/tvgSwRasterAvx.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved.
3
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
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 //1. set the masks for the A/G and R/B channels
33 auto AG = _mm_set1_epi32(0xff00ff00);
34 auto RB = _mm_set1_epi32(0x00ff00ff);
35
36 //2. mask the alpha vector - originally quartet [a, a, a, a]
37 auto aAG = _mm_and_si128(a, AG);
38 auto aRB = _mm_and_si128(a, RB);
39
40 //3. calculate the alpha blending of the 2nd and 4th channel
41 //- mask the color vector
42 //- multiply it by the masked alpha vector
43 //- add the correction to compensate bit shifting used instead of dividing by 255
44 //- shift bits - corresponding to division by 256
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 //4. calculate the alpha blending of the 1st and 3rd channel:
51 //- mask the color vector
52 //- multiply it by the corresponding masked alpha vector and store the high bits of the result
53 //- add the correction to compensate division by 256 instead of by 255 (next step)
54 //- remove the low 8 bits to mimic the division by 256
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 //5. the final result
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 //1. calculate how many iterations we need to cover the length
85 uint32_t iterations = len / N_32BITS_IN_256REG;
86 uint32_t avxFilled = iterations * N_32BITS_IN_256REG;
87
88 //2. set the beginning of the array
89 dst += offset;
90
91 //3. fill the octets
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 //4. fill leftovers (in the first step we have to set the pointer to the place where the avx job is done)
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 //32bits channels
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 //1. fill the not aligned memory (for 128-bit registers a 16-bytes alignment is required)
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 //2. fill the aligned memory - N_32BITS_IN_128REG pixels processed at once
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 //3. fill the remaining pixels
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 //8bit grayscale
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 //32bit channels
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 //1. fill the not aligned memory (for 128-bit registers a 16-bytes alignment is required)
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 //2. fill the aligned memory using avx - N_32BITS_IN_128REG pixels processed at once
188 //In order to avoid unnecessary avx variables declarations a check is made whether there are any iterations at all
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 //3. fill the remaining pixels
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 //8bit grayscale
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