Merge branch 'gallium-polygon-stipple'
[mesa.git] / src / gallium / drivers / llvmpipe / lp_tile_soa.py
1 #!/usr/bin/env python
2
3 CopyRight = '''
4 /**************************************************************************
5 *
6 * Copyright 2009 VMware, Inc.
7 * All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sub license, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
16 *
17 * The above copyright notice and this permission notice (including the
18 * next paragraph) shall be included in all copies or substantial portions
19 * of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
25 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 *
29 **************************************************************************/
30
31 /**
32 * @file
33 * Pixel format accessor functions.
34 *
35 * @author Jose Fonseca <jfonseca@vmware.com>
36 */
37 '''
38
39
40 import sys
41 import os.path
42
43 sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), '../../auxiliary/util'))
44
45 from u_format_pack import *
46
47
48 def is_format_supported(format):
49 '''Determines whether we actually have the plumbing necessary to generate the
50 to read/write to/from this format.'''
51
52 # FIXME: Ideally we would support any format combination here.
53
54 if format.layout != PLAIN:
55 return False
56
57 for i in range(4):
58 channel = format.channels[i]
59 if channel.type not in (VOID, UNSIGNED, SIGNED, FLOAT):
60 return False
61 if channel.type == FLOAT and channel.size not in (16, 32 ,64):
62 return False
63
64 if format.colorspace not in ('rgb', 'srgb'):
65 return False
66
67 return True
68
69
70 def generate_format_read(format, dst_channel, dst_native_type, dst_suffix):
71 '''Generate the function to read pixels from a particular format'''
72
73 name = format.short_name()
74
75 src_native_type = native_type(format)
76
77 print 'static void'
78 print 'lp_tile_%s_swizzle_%s(%s *dst, const uint8_t *src, unsigned src_stride, unsigned x0, unsigned y0)' % (name, dst_suffix, dst_native_type)
79 print '{'
80 print ' unsigned x, y;'
81 print ' const uint8_t *src_row = src + y0*src_stride;'
82 print ' for (y = 0; y < TILE_SIZE; ++y) {'
83 print ' const %s *src_pixel = (const %s *)(src_row + x0*%u);' % (src_native_type, src_native_type, format.stride())
84 print ' for (x = 0; x < TILE_SIZE; ++x) {'
85
86 names = ['']*4
87 if format.colorspace in ('rgb', 'srgb'):
88 for i in range(4):
89 swizzle = format.swizzles[i]
90 if swizzle < 4:
91 names[swizzle] += 'rgba'[i]
92 elif format.colorspace == 'zs':
93 swizzle = format.swizzles[0]
94 if swizzle < 4:
95 names[swizzle] = 'z'
96 else:
97 assert False
98 else:
99 assert False
100
101 if format.layout == PLAIN:
102 if not format.is_array():
103 print ' %s pixel = *src_pixel++;' % src_native_type
104 shift = 0;
105 for i in range(4):
106 src_channel = format.channels[i]
107 width = src_channel.size
108 if names[i]:
109 value = 'pixel'
110 mask = (1 << width) - 1
111 if shift:
112 value = '(%s >> %u)' % (value, shift)
113 if shift + width < format.block_size():
114 value = '(%s & 0x%x)' % (value, mask)
115 value = conversion_expr(src_channel, dst_channel, dst_native_type, value, clamp=False)
116 print ' %s %s = %s;' % (dst_native_type, names[i], value)
117 shift += width
118 else:
119 for i in range(4):
120 if names[i]:
121 print ' %s %s;' % (dst_native_type, names[i])
122 for i in range(4):
123 src_channel = format.channels[i]
124 if names[i]:
125 value = '(*src_pixel++)'
126 value = conversion_expr(src_channel, dst_channel, dst_native_type, value, clamp=False)
127 print ' %s = %s;' % (names[i], value)
128 elif src_channel.size:
129 print ' ++src_pixel;'
130 else:
131 assert False
132
133 for i in range(4):
134 if format.colorspace in ('rgb', 'srgb'):
135 swizzle = format.swizzles[i]
136 if swizzle < 4:
137 value = names[swizzle]
138 elif swizzle == SWIZZLE_0:
139 value = '0'
140 elif swizzle == SWIZZLE_1:
141 value = get_one(dst_channel)
142 else:
143 assert False
144 elif format.colorspace == 'zs':
145 if i < 3:
146 value = 'z'
147 else:
148 value = get_one(dst_channel)
149 else:
150 assert False
151 print ' TILE_PIXEL(dst, x, y, %u) = %s; /* %s */' % (i, value, 'rgba'[i])
152
153 print ' }'
154 print ' src_row += src_stride;'
155 print ' }'
156 print '}'
157 print
158
159
160 def pack_rgba(format, src_channel, r, g, b, a):
161 """Return an expression for packing r, g, b, a into a pixel of the
162 given format. Ex: '(b << 24) | (g << 16) | (r << 8) | (a << 0)'
163 """
164 assert format.colorspace in ('rgb', 'srgb')
165 inv_swizzle = format.inv_swizzles()
166 shift = 0
167 expr = None
168 for i in range(4):
169 # choose r, g, b, or a depending on the inverse swizzle term
170 if inv_swizzle[i] == 0:
171 value = r
172 elif inv_swizzle[i] == 1:
173 value = g
174 elif inv_swizzle[i] == 2:
175 value = b
176 elif inv_swizzle[i] == 3:
177 value = a
178 else:
179 value = None
180
181 if value:
182 dst_channel = format.channels[i]
183 dst_native_type = native_type(format)
184 value = conversion_expr(src_channel, dst_channel, dst_native_type, value, clamp=False)
185 term = "((%s) << %d)" % (value, shift)
186 if expr:
187 expr = expr + " | " + term
188 else:
189 expr = term
190
191 width = format.channels[i].size
192 shift = shift + width
193 return expr
194
195
196 def emit_unrolled_unswizzle_code(format, src_channel):
197 '''Emit code for writing a block based on unrolled loops.
198 This is considerably faster than the TILE_PIXEL-based code below.
199 '''
200 dst_native_type = 'uint%u_t' % format.block_size()
201 print ' const unsigned dstpix_stride = dst_stride / %d;' % format.stride()
202 print ' %s *dstpix = (%s *) dst;' % (dst_native_type, dst_native_type)
203 print ' unsigned int qx, qy, i;'
204 print
205 print ' for (qy = 0; qy < TILE_SIZE; qy += TILE_VECTOR_HEIGHT) {'
206 print ' const unsigned py = y0 + qy;'
207 print ' for (qx = 0; qx < TILE_SIZE; qx += TILE_VECTOR_WIDTH) {'
208 print ' const unsigned px = x0 + qx;'
209 print ' const uint8_t *r = src + 0 * TILE_C_STRIDE;'
210 print ' const uint8_t *g = src + 1 * TILE_C_STRIDE;'
211 print ' const uint8_t *b = src + 2 * TILE_C_STRIDE;'
212 print ' const uint8_t *a = src + 3 * TILE_C_STRIDE;'
213 print ' (void) r; (void) g; (void) b; (void) a; /* silence warnings */'
214 print ' for (i = 0; i < TILE_C_STRIDE; i += 2) {'
215 print ' const uint32_t pixel0 = %s;' % pack_rgba(format, src_channel, "r[i+0]", "g[i+0]", "b[i+0]", "a[i+0]")
216 print ' const uint32_t pixel1 = %s;' % pack_rgba(format, src_channel, "r[i+1]", "g[i+1]", "b[i+1]", "a[i+1]")
217 print ' const unsigned offset = (py + tile_y_offset[i]) * dstpix_stride + (px + tile_x_offset[i]);'
218 print ' dstpix[offset + 0] = pixel0;'
219 print ' dstpix[offset + 1] = pixel1;'
220 print ' }'
221 print ' src += TILE_X_STRIDE;'
222 print ' }'
223 print ' }'
224
225
226 def emit_tile_pixel_unswizzle_code(format, src_channel):
227 '''Emit code for writing a block based on the TILE_PIXEL macro.'''
228 dst_native_type = native_type(format)
229
230 inv_swizzle = format.inv_swizzles()
231
232 print ' unsigned x, y;'
233 print ' uint8_t *dst_row = dst + y0*dst_stride;'
234 print ' for (y = 0; y < TILE_SIZE; ++y) {'
235 print ' %s *dst_pixel = (%s *)(dst_row + x0*%u);' % (dst_native_type, dst_native_type, format.stride())
236 print ' for (x = 0; x < TILE_SIZE; ++x) {'
237
238 if format.layout == PLAIN:
239 if not format.is_array():
240 print ' %s pixel = 0;' % dst_native_type
241 shift = 0;
242 for i in range(4):
243 dst_channel = format.channels[i]
244 width = dst_channel.size
245 if inv_swizzle[i] is not None:
246 value = 'TILE_PIXEL(src, x, y, %u)' % inv_swizzle[i]
247 value = conversion_expr(src_channel, dst_channel, dst_native_type, value, clamp=False)
248 if shift:
249 value = '(%s << %u)' % (value, shift)
250 print ' pixel |= %s;' % value
251 shift += width
252 print ' *dst_pixel++ = pixel;'
253 else:
254 for i in range(4):
255 dst_channel = format.channels[i]
256 if inv_swizzle[i] is not None:
257 value = 'TILE_PIXEL(src, x, y, %u)' % inv_swizzle[i]
258 value = conversion_expr(src_channel, dst_channel, dst_native_type, value, clamp=False)
259 print ' *dst_pixel++ = %s;' % value
260 elif dst_channel.size:
261 print ' ++dst_pixel;'
262 else:
263 assert False
264
265 print ' }'
266 print ' dst_row += dst_stride;'
267 print ' }'
268
269
270 def generate_format_write(format, src_channel, src_native_type, src_suffix):
271 '''Generate the function to write pixels to a particular format'''
272
273 name = format.short_name()
274
275 print 'static void'
276 print 'lp_tile_%s_unswizzle_%s(const %s *src, uint8_t *dst, unsigned dst_stride, unsigned x0, unsigned y0)' % (name, src_suffix, src_native_type)
277 print '{'
278 if format.layout == PLAIN \
279 and format.colorspace == 'rgb' \
280 and format.block_size() <= 32 \
281 and format.is_pot() \
282 and not format.is_mixed() \
283 and (format.channels[0].type == UNSIGNED \
284 or format.channels[1].type == UNSIGNED):
285 emit_unrolled_unswizzle_code(format, src_channel)
286 else:
287 emit_tile_pixel_unswizzle_code(format, src_channel)
288 print '}'
289 print
290
291
292 def generate_sse2():
293 print '''
294 #if defined(PIPE_ARCH_SSE)
295
296 #include "util/u_sse.h"
297
298 static ALWAYS_INLINE void
299 swz4( const __m128i * restrict x,
300 const __m128i * restrict y,
301 const __m128i * restrict z,
302 const __m128i * restrict w,
303 __m128i * restrict a,
304 __m128i * restrict b,
305 __m128i * restrict c,
306 __m128i * restrict d)
307 {
308 __m128i i, j, k, l;
309 __m128i m, n, o, p;
310 __m128i e, f, g, h;
311
312 m = _mm_unpacklo_epi8(*x,*y);
313 n = _mm_unpackhi_epi8(*x,*y);
314 o = _mm_unpacklo_epi8(*z,*w);
315 p = _mm_unpackhi_epi8(*z,*w);
316
317 i = _mm_unpacklo_epi16(m,n);
318 j = _mm_unpackhi_epi16(m,n);
319 k = _mm_unpacklo_epi16(o,p);
320 l = _mm_unpackhi_epi16(o,p);
321
322 e = _mm_unpacklo_epi8(i,j);
323 f = _mm_unpackhi_epi8(i,j);
324 g = _mm_unpacklo_epi8(k,l);
325 h = _mm_unpackhi_epi8(k,l);
326
327 *a = _mm_unpacklo_epi64(e,g);
328 *b = _mm_unpackhi_epi64(e,g);
329 *c = _mm_unpacklo_epi64(f,h);
330 *d = _mm_unpackhi_epi64(f,h);
331 }
332
333 static ALWAYS_INLINE void
334 unswz4( const __m128i * restrict a,
335 const __m128i * restrict b,
336 const __m128i * restrict c,
337 const __m128i * restrict d,
338 __m128i * restrict x,
339 __m128i * restrict y,
340 __m128i * restrict z,
341 __m128i * restrict w)
342 {
343 __m128i i, j, k, l;
344 __m128i m, n, o, p;
345
346 i = _mm_unpacklo_epi8(*a,*b);
347 j = _mm_unpackhi_epi8(*a,*b);
348 k = _mm_unpacklo_epi8(*c,*d);
349 l = _mm_unpackhi_epi8(*c,*d);
350
351 m = _mm_unpacklo_epi16(i,k);
352 n = _mm_unpackhi_epi16(i,k);
353 o = _mm_unpacklo_epi16(j,l);
354 p = _mm_unpackhi_epi16(j,l);
355
356 *x = _mm_unpacklo_epi64(m,n);
357 *y = _mm_unpackhi_epi64(m,n);
358 *z = _mm_unpacklo_epi64(o,p);
359 *w = _mm_unpackhi_epi64(o,p);
360 }
361
362 static void
363 lp_tile_b8g8r8a8_unorm_swizzle_4ub_sse2(uint8_t * restrict dst,
364 const uint8_t * restrict src, unsigned src_stride,
365 unsigned x0, unsigned y0)
366 {
367 __m128i *dst128 = (__m128i *) dst;
368 unsigned x, y;
369
370 src += y0 * src_stride;
371 src += x0 * sizeof(uint32_t);
372
373 for (y = 0; y < TILE_SIZE; y += 4) {
374 const uint8_t *src_row = src;
375
376 for (x = 0; x < TILE_SIZE; x += 4) {
377 swz4((const __m128i *) (src_row + 0 * src_stride),
378 (const __m128i *) (src_row + 1 * src_stride),
379 (const __m128i *) (src_row + 2 * src_stride),
380 (const __m128i *) (src_row + 3 * src_stride),
381 dst128 + 2, /* b */
382 dst128 + 1, /* g */
383 dst128 + 0, /* r */
384 dst128 + 3); /* a */
385
386 dst128 += 4;
387 src_row += sizeof(__m128i);
388 }
389
390 src += 4 * src_stride;
391 }
392 }
393
394 static void
395 lp_tile_b8g8r8a8_unorm_unswizzle_4ub_sse2(const uint8_t * restrict src,
396 uint8_t * restrict dst, unsigned dst_stride,
397 unsigned x0, unsigned y0)
398 {
399 unsigned int x, y;
400 const __m128i *src128 = (const __m128i *) src;
401
402 dst += y0 * dst_stride;
403 dst += x0 * sizeof(uint32_t);
404
405 for (y = 0; y < TILE_SIZE; y += 4) {
406 const uint8_t *dst_row = dst;
407
408 for (x = 0; x < TILE_SIZE; x += 4) {
409 unswz4( &src128[2], /* b */
410 &src128[1], /* g */
411 &src128[0], /* r */
412 &src128[3], /* a */
413 (__m128i *) (dst_row + 0 * dst_stride),
414 (__m128i *) (dst_row + 1 * dst_stride),
415 (__m128i *) (dst_row + 2 * dst_stride),
416 (__m128i *) (dst_row + 3 * dst_stride));
417
418 src128 += 4;
419 dst_row += sizeof(__m128i);;
420 }
421
422 dst += 4 * dst_stride;
423 }
424 }
425
426 static void
427 lp_tile_b8g8r8x8_unorm_swizzle_4ub_sse2(uint8_t * restrict dst,
428 const uint8_t * restrict src, unsigned src_stride,
429 unsigned x0, unsigned y0)
430 {
431 __m128i *dst128 = (__m128i *) dst;
432 unsigned x, y;
433
434 src += y0 * src_stride;
435 src += x0 * sizeof(uint32_t);
436
437 for (y = 0; y < TILE_SIZE; y += 4) {
438 const uint8_t *src_row = src;
439
440 for (x = 0; x < TILE_SIZE; x += 4) {
441 swz4((const __m128i *) (src_row + 0 * src_stride),
442 (const __m128i *) (src_row + 1 * src_stride),
443 (const __m128i *) (src_row + 2 * src_stride),
444 (const __m128i *) (src_row + 3 * src_stride),
445 dst128 + 2, /* b */
446 dst128 + 1, /* g */
447 dst128 + 0, /* r */
448 dst128 + 3); /* a */
449
450 dst128 += 4;
451 src_row += sizeof(__m128i);
452 }
453
454 src += 4 * src_stride;
455 }
456 }
457
458 static void
459 lp_tile_b8g8r8x8_unorm_unswizzle_4ub_sse2(const uint8_t * restrict src,
460 uint8_t * restrict dst, unsigned dst_stride,
461 unsigned x0, unsigned y0)
462 {
463 unsigned int x, y;
464 const __m128i *src128 = (const __m128i *) src;
465
466 dst += y0 * dst_stride;
467 dst += x0 * sizeof(uint32_t);
468
469 for (y = 0; y < TILE_SIZE; y += 4) {
470 const uint8_t *dst_row = dst;
471
472 for (x = 0; x < TILE_SIZE; x += 4) {
473 unswz4( &src128[2], /* b */
474 &src128[1], /* g */
475 &src128[0], /* r */
476 &src128[3], /* a */
477 (__m128i *) (dst_row + 0 * dst_stride),
478 (__m128i *) (dst_row + 1 * dst_stride),
479 (__m128i *) (dst_row + 2 * dst_stride),
480 (__m128i *) (dst_row + 3 * dst_stride));
481
482 src128 += 4;
483 dst_row += sizeof(__m128i);;
484 }
485
486 dst += 4 * dst_stride;
487 }
488 }
489
490 #endif /* PIPE_ARCH_SSE */
491 '''
492
493
494 def generate_swizzle(formats, dst_channel, dst_native_type, dst_suffix):
495 '''Generate the dispatch function to read pixels from any format'''
496
497 for format in formats:
498 if is_format_supported(format):
499 generate_format_read(format, dst_channel, dst_native_type, dst_suffix)
500
501 print 'void'
502 print 'lp_tile_swizzle_%s(enum pipe_format format, %s *dst, const void *src, unsigned src_stride, unsigned x, unsigned y)' % (dst_suffix, dst_native_type)
503 print '{'
504 print ' void (*func)(%s *dst, const uint8_t *src, unsigned src_stride, unsigned x0, unsigned y0);' % dst_native_type
505 print '#ifdef DEBUG'
506 print ' lp_tile_swizzle_count += 1;'
507 print '#endif'
508 print ' switch(format) {'
509 for format in formats:
510 if is_format_supported(format):
511 print ' case %s:' % format.name
512 func_name = 'lp_tile_%s_swizzle_%s' % (format.short_name(), dst_suffix)
513 if format.name == 'PIPE_FORMAT_B8G8R8A8_UNORM' or format.name == 'PIPE_FORMAT_B8G8R8X8_UNORM':
514 print '#ifdef PIPE_ARCH_SSE'
515 print ' func = util_cpu_caps.has_sse2 ? %s_sse2 : %s;' % (func_name, func_name)
516 print '#else'
517 print ' func = %s;' % (func_name,)
518 print '#endif'
519 else:
520 print ' func = %s;' % (func_name,)
521 print ' break;'
522 print ' default:'
523 print ' debug_printf("%s: unsupported format %s\\n", __FUNCTION__, util_format_name(format));'
524 print ' return;'
525 print ' }'
526 print ' func(dst, (const uint8_t *)src, src_stride, x, y);'
527 print '}'
528 print
529
530
531 def generate_unswizzle(formats, src_channel, src_native_type, src_suffix):
532 '''Generate the dispatch function to write pixels to any format'''
533
534 for format in formats:
535 if is_format_supported(format):
536 generate_format_write(format, src_channel, src_native_type, src_suffix)
537
538 print 'void'
539 print 'lp_tile_unswizzle_%s(enum pipe_format format, const %s *src, void *dst, unsigned dst_stride, unsigned x, unsigned y)' % (src_suffix, src_native_type)
540
541 print '{'
542 print ' void (*func)(const %s *src, uint8_t *dst, unsigned dst_stride, unsigned x0, unsigned y0);' % src_native_type
543 print '#ifdef DEBUG'
544 print ' lp_tile_unswizzle_count += 1;'
545 print '#endif'
546 print ' switch(format) {'
547 for format in formats:
548 if is_format_supported(format):
549 print ' case %s:' % format.name
550 func_name = 'lp_tile_%s_unswizzle_%s' % (format.short_name(), src_suffix)
551 if format.name == 'PIPE_FORMAT_B8G8R8A8_UNORM' or format.name == 'PIPE_FORMAT_B8G8R8X8_UNORM':
552 print '#ifdef PIPE_ARCH_SSE'
553 print ' func = util_cpu_caps.has_sse2 ? %s_sse2 : %s;' % (func_name, func_name)
554 print '#else'
555 print ' func = %s;' % (func_name,)
556 print '#endif'
557 else:
558 print ' func = %s;' % (func_name,)
559 print ' break;'
560 print ' default:'
561 print ' debug_printf("%s: unsupported format %s\\n", __FUNCTION__, util_format_name(format));'
562 print ' return;'
563 print ' }'
564 print ' func(src, (uint8_t *)dst, dst_stride, x, y);'
565 print '}'
566 print
567
568
569 def main():
570 formats = []
571 for arg in sys.argv[1:]:
572 formats.extend(parse(arg))
573
574 print '/* This file is autogenerated by lp_tile_soa.py from u_format.csv. Do not edit directly. */'
575 print
576 # This will print the copyright message on the top of this file
577 print CopyRight.strip()
578 print
579 print '#include "pipe/p_compiler.h"'
580 print '#include "util/u_format.h"'
581 print '#include "util/u_math.h"'
582 print '#include "util/u_half.h"'
583 print '#include "util/u_cpu_detect.h"'
584 print '#include "lp_tile_soa.h"'
585 print
586 print '#ifdef DEBUG'
587 print 'unsigned lp_tile_unswizzle_count = 0;'
588 print 'unsigned lp_tile_swizzle_count = 0;'
589 print '#endif'
590 print
591 print 'const unsigned char'
592 print 'tile_offset[TILE_VECTOR_HEIGHT][TILE_VECTOR_WIDTH] = {'
593 print ' { 0, 1, 4, 5},'
594 print ' { 2, 3, 6, 7},'
595 print ' { 8, 9, 12, 13},'
596 print ' { 10, 11, 14, 15}'
597 print '};'
598 print
599 print '/* Note: these lookup tables could be replaced with some'
600 print ' * bit-twiddling code, but this is a little faster.'
601 print ' */'
602 print 'static unsigned tile_x_offset[TILE_VECTOR_WIDTH * TILE_VECTOR_HEIGHT] = {'
603 print ' 0, 1, 0, 1, 2, 3, 2, 3,'
604 print ' 0, 1, 0, 1, 2, 3, 2, 3'
605 print '};'
606 print
607 print 'static unsigned tile_y_offset[TILE_VECTOR_WIDTH * TILE_VECTOR_HEIGHT] = {'
608 print ' 0, 0, 1, 1, 0, 0, 1, 1,'
609 print ' 2, 2, 3, 3, 2, 2, 3, 3'
610 print '};'
611 print
612
613 generate_sse2()
614
615 channel = Channel(UNSIGNED, True, 8)
616 native_type = 'uint8_t'
617 suffix = '4ub'
618
619 generate_swizzle(formats, channel, native_type, suffix)
620 generate_unswizzle(formats, channel, native_type, suffix)
621
622
623 if __name__ == '__main__':
624 main()