4 /**************************************************************************
6 * Copyright 2009 VMware, Inc.
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:
17 * The above copyright notice and this permission notice (including the
18 * next paragraph) shall be included in all copies or substantial portions
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.
29 **************************************************************************/
33 * Pixel format accessor functions.
35 * @author Jose Fonseca <jfonseca@vmware.com>
43 sys
.path
.insert(0, os
.path
.join(os
.path
.dirname(sys
.argv
[0]), '../../auxiliary/util'))
45 from u_format_access
import *
48 def generate_format_read(format
, dst_type
, dst_native_type
, dst_suffix
):
49 '''Generate the function to read pixels from a particular format'''
51 name
= short_name(format
)
53 src_native_type
= native_type(format
)
56 print 'lp_tile_%s_read_%s(%s *dst, const uint8_t *src, unsigned src_stride, unsigned x0, unsigned y0, unsigned w, unsigned h)' % (name
, dst_suffix
, dst_native_type
)
58 print ' unsigned x, y;'
59 print ' const uint8_t *src_row = src + y0*src_stride;'
60 print ' for (y = 0; y < h; ++y) {'
61 print ' const %s *src_pixel = (const %s *)(src_row + x0*%u);' % (src_native_type
, src_native_type
, format
.stride())
62 print ' for (x = 0; x < w; ++x) {'
65 if format
.colorspace
== 'rgb':
67 swizzle
= format
.out_swizzle
[i
]
69 names
[swizzle
] += 'rgba'[i
]
70 elif format
.colorspace
== 'zs':
71 swizzle
= format
.out_swizzle
[0]
79 if format
.layout
== ARITH
:
80 print ' %s pixel = *src_pixel++;' % src_native_type
83 src_type
= format
.in_types
[i
]
87 mask
= (1 << width
) - 1
89 value
= '(%s >> %u)' % (value
, shift
)
90 if shift
+ width
< format
.block_size():
91 value
= '(%s & 0x%x)' % (value
, mask
)
92 value
= conversion_expr(src_type
, dst_type
, dst_native_type
, value
)
93 print ' %s %s = %s;' % (dst_native_type
, names
[i
], value
)
95 elif format
.layout
== ARRAY
:
97 src_type
= format
.in_types
[i
]
99 value
= '(*src_pixel++)'
100 value
= conversion_expr(src_type
, dst_type
, dst_native_type
, value
)
101 print ' %s %s = %s;' % (dst_native_type
, names
[i
], value
)
106 if format
.colorspace
== 'rgb':
107 swizzle
= format
.out_swizzle
[i
]
109 value
= names
[swizzle
]
110 elif swizzle
== SWIZZLE_0
:
112 elif swizzle
== SWIZZLE_1
:
116 elif format
.colorspace
== 'zs':
123 print ' TILE_PIXEL(dst, x, y, %u) = %s; /* %s */' % (i
, value
, 'rgba'[i
])
126 print ' src_row += src_stride;'
132 def compute_inverse_swizzle(format
):
133 '''Return an array[4] of inverse swizzle terms'''
134 inv_swizzle
= [None]*4
135 if format
.colorspace
== 'rgb':
137 swizzle
= format
.out_swizzle
[i
]
139 inv_swizzle
[swizzle
] = i
140 elif format
.colorspace
== 'zs':
141 swizzle
= format
.out_swizzle
[0]
143 inv_swizzle
[swizzle
] = 0
147 def pack_rgba(format
, src_type
, r
, g
, b
, a
):
148 """Return an expression for packing r, g, b, a into a pixel of the
149 given format. Ex: '(b << 24) | (g << 16) | (r << 8) | (a << 0)'
151 assert format
.colorspace
== 'rgb'
152 inv_swizzle
= compute_inverse_swizzle(format
)
156 # choose r, g, b, or a depending on the inverse swizzle term
157 if inv_swizzle
[i
] == 0:
159 elif inv_swizzle
[i
] == 1:
161 elif inv_swizzle
[i
] == 2:
163 elif inv_swizzle
[i
] == 3:
169 dst_type
= format
.in_types
[i
]
170 dst_native_type
= native_type(format
)
171 value
= conversion_expr(src_type
, dst_type
, dst_native_type
, value
)
172 term
= "((%s) << %d)" % (value
, shift
)
174 expr
= expr
+ " | " + term
178 width
= format
.in_types
[i
].size
179 shift
= shift
+ width
183 def emit_unrolled_write_code(format
, src_type
):
184 '''Emit code for writing a block based on unrolled loops.
185 This is considerably faster than the TILE_PIXEL-based code below.
187 dst_native_type
= native_type(format
)
188 print ' const unsigned dstpix_stride = dst_stride / %d;' % format
.stride()
189 print ' %s *dstpix = (%s *) dst;' % (dst_native_type
, dst_native_type
)
190 print ' unsigned int qx, qy, i;'
192 print ' for (qy = 0; qy < h; qy += TILE_VECTOR_HEIGHT) {'
193 print ' const unsigned py = y0 + qy;'
194 print ' for (qx = 0; qx < w; qx += TILE_VECTOR_WIDTH) {'
195 print ' const unsigned px = x0 + qx;'
196 print ' const uint8_t *r = src + 0 * TILE_C_STRIDE;'
197 print ' const uint8_t *g = src + 1 * TILE_C_STRIDE;'
198 print ' const uint8_t *b = src + 2 * TILE_C_STRIDE;'
199 print ' const uint8_t *a = src + 3 * TILE_C_STRIDE;'
200 print ' (void) r; (void) g; (void) b; (void) a; /* silence warnings */'
201 print ' for (i = 0; i < TILE_C_STRIDE; i += 2) {'
202 print ' const uint32_t pixel0 = %s;' % pack_rgba(format
, src_type
, "r[i+0]", "g[i+0]", "b[i+0]", "a[i+0]")
203 print ' const uint32_t pixel1 = %s;' % pack_rgba(format
, src_type
, "r[i+1]", "g[i+1]", "b[i+1]", "a[i+1]")
204 print ' const unsigned offset = (py + tile_y_offset[i]) * dstpix_stride + (px + tile_x_offset[i]);'
205 print ' dstpix[offset + 0] = pixel0;'
206 print ' dstpix[offset + 1] = pixel1;'
208 print ' src += TILE_X_STRIDE;'
213 def emit_tile_pixel_write_code(format
, src_type
):
214 '''Emit code for writing a block based on the TILE_PIXEL macro.'''
215 dst_native_type
= native_type(format
)
217 inv_swizzle
= compute_inverse_swizzle(format
)
219 print ' unsigned x, y;'
220 print ' uint8_t *dst_row = dst + y0*dst_stride;'
221 print ' for (y = 0; y < h; ++y) {'
222 print ' %s *dst_pixel = (%s *)(dst_row + x0*%u);' % (dst_native_type
, dst_native_type
, format
.stride())
223 print ' for (x = 0; x < w; ++x) {'
225 if format
.layout
== ARITH
:
226 print ' %s pixel = 0;' % dst_native_type
229 dst_type
= format
.in_types
[i
]
230 width
= dst_type
.size
231 if inv_swizzle
[i
] is not None:
232 value
= 'TILE_PIXEL(src, x, y, %u)' % inv_swizzle
[i
]
233 value
= conversion_expr(src_type
, dst_type
, dst_native_type
, value
)
235 value
= '(%s << %u)' % (value
, shift
)
236 print ' pixel |= %s;' % value
238 print ' *dst_pixel++ = pixel;'
239 elif format
.layout
== ARRAY
:
241 dst_type
= format
.in_types
[i
]
242 if inv_swizzle
[i
] is not None:
243 value
= 'TILE_PIXEL(src, x, y, %u)' % inv_swizzle
[i
]
244 value
= conversion_expr(src_type
, dst_type
, dst_native_type
, value
)
245 print ' *dst_pixel++ = %s;' % value
250 print ' dst_row += dst_stride;'
254 def generate_format_write(format
, src_type
, src_native_type
, src_suffix
):
255 '''Generate the function to write pixels to a particular format'''
257 name
= short_name(format
)
260 print 'lp_tile_%s_write_%s(const %s *src, uint8_t *dst, unsigned dst_stride, unsigned x0, unsigned y0, unsigned w, unsigned h)' % (name
, src_suffix
, src_native_type
)
262 if format
.layout
== ARITH
and format
.colorspace
== 'rgb':
263 emit_unrolled_write_code(format
, src_type
)
265 emit_tile_pixel_write_code(format
, src_type
)
270 def generate_read(formats
, dst_type
, dst_native_type
, dst_suffix
):
271 '''Generate the dispatch function to read pixels from any format'''
273 for format
in formats
:
274 if is_format_supported(format
):
275 generate_format_read(format
, dst_type
, dst_native_type
, dst_suffix
)
278 print 'lp_tile_read_%s(enum pipe_format format, %s *dst, const void *src, unsigned src_stride, unsigned x, unsigned y, unsigned w, unsigned h)' % (dst_suffix
, dst_native_type
)
280 print ' void (*func)(%s *dst, const uint8_t *src, unsigned src_stride, unsigned x0, unsigned y0, unsigned w, unsigned h);' % dst_native_type
281 print ' switch(format) {'
282 for format
in formats
:
283 if is_format_supported(format
):
284 print ' case %s:' % format
.name
285 print ' func = &lp_tile_%s_read_%s;' % (short_name(format
), dst_suffix
)
288 print ' debug_printf("unsupported format\\n");'
291 print ' func(dst, (const uint8_t *)src, src_stride, x, y, w, h);'
296 def generate_write(formats
, src_type
, src_native_type
, src_suffix
):
297 '''Generate the dispatch function to write pixels to any format'''
299 for format
in formats
:
300 if is_format_supported(format
):
301 generate_format_write(format
, src_type
, src_native_type
, src_suffix
)
304 print 'lp_tile_write_%s(enum pipe_format format, const %s *src, void *dst, unsigned dst_stride, unsigned x, unsigned y, unsigned w, unsigned h)' % (src_suffix
, src_native_type
)
307 print ' void (*func)(const %s *src, uint8_t *dst, unsigned dst_stride, unsigned x0, unsigned y0, unsigned w, unsigned h);' % src_native_type
308 print ' switch(format) {'
309 for format
in formats
:
310 if is_format_supported(format
):
311 print ' case %s:' % format
.name
312 print ' func = &lp_tile_%s_write_%s;' % (short_name(format
), src_suffix
)
315 print ' debug_printf("unsupported format\\n");'
318 print ' func(src, (uint8_t *)dst, dst_stride, x, y, w, h);'
325 for arg
in sys
.argv
[1:]:
326 formats
.extend(parse(arg
))
328 print '/* This file is autogenerated by lp_tile_soa.py from u_format.csv. Do not edit directly. */'
330 # This will print the copyright message on the top of this file
331 print __doc__
.strip()
333 print '#include "pipe/p_compiler.h"'
334 print '#include "util/u_format.h"'
335 print '#include "util/u_math.h"'
336 print '#include "lp_tile_soa.h"'
338 print 'const unsigned char'
339 print 'tile_offset[TILE_VECTOR_HEIGHT][TILE_VECTOR_WIDTH] = {'
340 print ' { 0, 1, 4, 5},'
341 print ' { 2, 3, 6, 7},'
342 print ' { 8, 9, 12, 13},'
343 print ' { 10, 11, 14, 15}'
346 print '/* Note: these lookup tables could be replaced with some'
347 print ' * bit-twiddling code, but this is a little faster.'
349 print 'static unsigned tile_x_offset[TILE_VECTOR_WIDTH * TILE_VECTOR_HEIGHT] = {'
350 print ' 0, 1, 0, 1, 2, 3, 2, 3,'
351 print ' 0, 1, 0, 1, 2, 3, 2, 3'
354 print 'static unsigned tile_y_offset[TILE_VECTOR_WIDTH * TILE_VECTOR_HEIGHT] = {'
355 print ' 0, 0, 1, 1, 0, 0, 1, 1,'
356 print ' 2, 2, 3, 3, 2, 2, 3, 3'
362 type = Type(UNSIGNED
, True, 8)
363 native_type
= 'uint8_t'
366 generate_read(formats
, type, native_type
, suffix
)
367 generate_write(formats
, type, native_type
, suffix
)
370 if __name__
== '__main__':