7d5ded2f02a7a71a5777b42b56484ebe79921c43
[mesa.git] / src / gallium / drivers / llvmpipe / lp_tile_soa.py
1 #!/usr/bin/env python
2
3 '''
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_access import *
46
47
48 def generate_format_read(format, dst_type, dst_native_type, dst_suffix):
49 '''Generate the function to read pixels from a particular format'''
50
51 name = short_name(format)
52
53 src_native_type = native_type(format)
54
55 print 'static void'
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)
57 print '{'
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) {'
63
64 names = ['']*4
65 if format.colorspace == 'rgb':
66 for i in range(4):
67 swizzle = format.out_swizzle[i]
68 if swizzle < 4:
69 names[swizzle] += 'rgba'[i]
70 elif format.colorspace == 'zs':
71 swizzle = format.out_swizzle[0]
72 if swizzle < 4:
73 names[swizzle] = 'z'
74 else:
75 assert False
76 else:
77 assert False
78
79 if format.layout in (ARITH, ARRAY):
80 if not format.is_array():
81 print ' %s pixel = *src_pixel++;' % src_native_type
82 shift = 0;
83 for i in range(4):
84 src_type = format.in_types[i]
85 width = src_type.size
86 if names[i]:
87 value = 'pixel'
88 mask = (1 << width) - 1
89 if shift:
90 value = '(%s >> %u)' % (value, shift)
91 if shift + width < format.block_size():
92 value = '(%s & 0x%x)' % (value, mask)
93 value = conversion_expr(src_type, dst_type, dst_native_type, value)
94 print ' %s %s = %s;' % (dst_native_type, names[i], value)
95 shift += width
96 else:
97 for i in range(4):
98 src_type = format.in_types[i]
99 if names[i]:
100 value = '(*src_pixel++)'
101 value = conversion_expr(src_type, dst_type, dst_native_type, value)
102 print ' %s %s = %s;' % (dst_native_type, names[i], value)
103 else:
104 assert False
105
106 for i in range(4):
107 if format.colorspace == 'rgb':
108 swizzle = format.out_swizzle[i]
109 if swizzle < 4:
110 value = names[swizzle]
111 elif swizzle == SWIZZLE_0:
112 value = '0'
113 elif swizzle == SWIZZLE_1:
114 value = '1'
115 else:
116 assert False
117 elif format.colorspace == 'zs':
118 if i < 3:
119 value = 'z'
120 else:
121 value = '1'
122 else:
123 assert False
124 print ' TILE_PIXEL(dst, x, y, %u) = %s; /* %s */' % (i, value, 'rgba'[i])
125
126 print ' }'
127 print ' src_row += src_stride;'
128 print ' }'
129 print '}'
130 print
131
132
133 def compute_inverse_swizzle(format):
134 '''Return an array[4] of inverse swizzle terms'''
135 inv_swizzle = [None]*4
136 if format.colorspace == 'rgb':
137 for i in range(4):
138 swizzle = format.out_swizzle[i]
139 if swizzle < 4:
140 inv_swizzle[swizzle] = i
141 elif format.colorspace == 'zs':
142 swizzle = format.out_swizzle[0]
143 if swizzle < 4:
144 inv_swizzle[swizzle] = 0
145 return inv_swizzle
146
147
148 def pack_rgba(format, src_type, r, g, b, a):
149 """Return an expression for packing r, g, b, a into a pixel of the
150 given format. Ex: '(b << 24) | (g << 16) | (r << 8) | (a << 0)'
151 """
152 assert format.colorspace == 'rgb'
153 inv_swizzle = compute_inverse_swizzle(format)
154 shift = 0
155 expr = None
156 for i in range(4):
157 # choose r, g, b, or a depending on the inverse swizzle term
158 if inv_swizzle[i] == 0:
159 value = r
160 elif inv_swizzle[i] == 1:
161 value = g
162 elif inv_swizzle[i] == 2:
163 value = b
164 elif inv_swizzle[i] == 3:
165 value = a
166 else:
167 value = None
168
169 if value:
170 dst_type = format.in_types[i]
171 dst_native_type = native_type(format)
172 value = conversion_expr(src_type, dst_type, dst_native_type, value)
173 term = "((%s) << %d)" % (value, shift)
174 if expr:
175 expr = expr + " | " + term
176 else:
177 expr = term
178
179 width = format.in_types[i].size
180 shift = shift + width
181 return expr
182
183
184 def emit_unrolled_write_code(format, src_type):
185 '''Emit code for writing a block based on unrolled loops.
186 This is considerably faster than the TILE_PIXEL-based code below.
187 '''
188 dst_native_type = native_type(format)
189 print ' const unsigned dstpix_stride = dst_stride / %d;' % format.stride()
190 print ' %s *dstpix = (%s *) dst;' % (dst_native_type, dst_native_type)
191 print ' unsigned int qx, qy, i;'
192 print
193 print ' for (qy = 0; qy < h; qy += TILE_VECTOR_HEIGHT) {'
194 print ' const unsigned py = y0 + qy;'
195 print ' for (qx = 0; qx < w; qx += TILE_VECTOR_WIDTH) {'
196 print ' const unsigned px = x0 + qx;'
197 print ' const uint8_t *r = src + 0 * TILE_C_STRIDE;'
198 print ' const uint8_t *g = src + 1 * TILE_C_STRIDE;'
199 print ' const uint8_t *b = src + 2 * TILE_C_STRIDE;'
200 print ' const uint8_t *a = src + 3 * TILE_C_STRIDE;'
201 print ' (void) r; (void) g; (void) b; (void) a; /* silence warnings */'
202 print ' for (i = 0; i < TILE_C_STRIDE; i += 2) {'
203 print ' const uint32_t pixel0 = %s;' % pack_rgba(format, src_type, "r[i+0]", "g[i+0]", "b[i+0]", "a[i+0]")
204 print ' const uint32_t pixel1 = %s;' % pack_rgba(format, src_type, "r[i+1]", "g[i+1]", "b[i+1]", "a[i+1]")
205 print ' const unsigned offset = (py + tile_y_offset[i]) * dstpix_stride + (px + tile_x_offset[i]);'
206 print ' dstpix[offset + 0] = pixel0;'
207 print ' dstpix[offset + 1] = pixel1;'
208 print ' }'
209 print ' src += TILE_X_STRIDE;'
210 print ' }'
211 print ' }'
212
213
214 def emit_tile_pixel_write_code(format, src_type):
215 '''Emit code for writing a block based on the TILE_PIXEL macro.'''
216 dst_native_type = native_type(format)
217
218 inv_swizzle = compute_inverse_swizzle(format)
219
220 print ' unsigned x, y;'
221 print ' uint8_t *dst_row = dst + y0*dst_stride;'
222 print ' for (y = 0; y < h; ++y) {'
223 print ' %s *dst_pixel = (%s *)(dst_row + x0*%u);' % (dst_native_type, dst_native_type, format.stride())
224 print ' for (x = 0; x < w; ++x) {'
225
226 if format.layout in (ARITH, ARRAY):
227 if not format.is_array():
228 print ' %s pixel = 0;' % dst_native_type
229 shift = 0;
230 for i in range(4):
231 dst_type = format.in_types[i]
232 width = dst_type.size
233 if inv_swizzle[i] is not None:
234 value = 'TILE_PIXEL(src, x, y, %u)' % inv_swizzle[i]
235 value = conversion_expr(src_type, dst_type, dst_native_type, value)
236 if shift:
237 value = '(%s << %u)' % (value, shift)
238 print ' pixel |= %s;' % value
239 shift += width
240 print ' *dst_pixel++ = pixel;'
241 else:
242 for i in range(4):
243 dst_type = format.in_types[i]
244 if inv_swizzle[i] is not None:
245 value = 'TILE_PIXEL(src, x, y, %u)' % inv_swizzle[i]
246 value = conversion_expr(src_type, dst_type, dst_native_type, value)
247 print ' *dst_pixel++ = %s;' % value
248 else:
249 assert False
250
251 print ' }'
252 print ' dst_row += dst_stride;'
253 print ' }'
254
255
256 def generate_format_write(format, src_type, src_native_type, src_suffix):
257 '''Generate the function to write pixels to a particular format'''
258
259 name = short_name(format)
260
261 print 'static void'
262 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)
263 print '{'
264 if format.layout == ARITH and format.colorspace == 'rgb':
265 emit_unrolled_write_code(format, src_type)
266 else:
267 emit_tile_pixel_write_code(format, src_type)
268 print '}'
269 print
270
271
272 def generate_read(formats, dst_type, dst_native_type, dst_suffix):
273 '''Generate the dispatch function to read pixels from any format'''
274
275 for format in formats:
276 if is_format_supported(format):
277 generate_format_read(format, dst_type, dst_native_type, dst_suffix)
278
279 print 'void'
280 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)
281 print '{'
282 print ' void (*func)(%s *dst, const uint8_t *src, unsigned src_stride, unsigned x0, unsigned y0, unsigned w, unsigned h);' % dst_native_type
283 print ' switch(format) {'
284 for format in formats:
285 if is_format_supported(format):
286 print ' case %s:' % format.name
287 print ' func = &lp_tile_%s_read_%s;' % (short_name(format), dst_suffix)
288 print ' break;'
289 print ' default:'
290 print ' debug_printf("unsupported format\\n");'
291 print ' return;'
292 print ' }'
293 print ' func(dst, (const uint8_t *)src, src_stride, x, y, w, h);'
294 print '}'
295 print
296
297
298 def generate_write(formats, src_type, src_native_type, src_suffix):
299 '''Generate the dispatch function to write pixels to any format'''
300
301 for format in formats:
302 if is_format_supported(format):
303 generate_format_write(format, src_type, src_native_type, src_suffix)
304
305 print 'void'
306 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
308 print '{'
309 print ' void (*func)(const %s *src, uint8_t *dst, unsigned dst_stride, unsigned x0, unsigned y0, unsigned w, unsigned h);' % src_native_type
310 print ' switch(format) {'
311 for format in formats:
312 if is_format_supported(format):
313 print ' case %s:' % format.name
314 print ' func = &lp_tile_%s_write_%s;' % (short_name(format), src_suffix)
315 print ' break;'
316 print ' default:'
317 print ' debug_printf("unsupported format\\n");'
318 print ' return;'
319 print ' }'
320 print ' func(src, (uint8_t *)dst, dst_stride, x, y, w, h);'
321 print '}'
322 print
323
324
325 def main():
326 formats = []
327 for arg in sys.argv[1:]:
328 formats.extend(parse(arg))
329
330 print '/* This file is autogenerated by lp_tile_soa.py from u_format.csv. Do not edit directly. */'
331 print
332 # This will print the copyright message on the top of this file
333 print __doc__.strip()
334 print
335 print '#include "pipe/p_compiler.h"'
336 print '#include "util/u_format.h"'
337 print '#include "util/u_math.h"'
338 print '#include "lp_tile_soa.h"'
339 print
340 print 'const unsigned char'
341 print 'tile_offset[TILE_VECTOR_HEIGHT][TILE_VECTOR_WIDTH] = {'
342 print ' { 0, 1, 4, 5},'
343 print ' { 2, 3, 6, 7},'
344 print ' { 8, 9, 12, 13},'
345 print ' { 10, 11, 14, 15}'
346 print '};'
347 print
348 print '/* Note: these lookup tables could be replaced with some'
349 print ' * bit-twiddling code, but this is a little faster.'
350 print ' */'
351 print 'static unsigned tile_x_offset[TILE_VECTOR_WIDTH * TILE_VECTOR_HEIGHT] = {'
352 print ' 0, 1, 0, 1, 2, 3, 2, 3,'
353 print ' 0, 1, 0, 1, 2, 3, 2, 3'
354 print '};'
355 print
356 print 'static unsigned tile_y_offset[TILE_VECTOR_WIDTH * TILE_VECTOR_HEIGHT] = {'
357 print ' 0, 0, 1, 1, 0, 0, 1, 1,'
358 print ' 2, 2, 3, 3, 2, 2, 3, 3'
359 print '};'
360 print
361
362 generate_clamp()
363
364 type = Type(UNSIGNED, True, 8)
365 native_type = 'uint8_t'
366 suffix = '4ub'
367
368 generate_read(formats, type, native_type, suffix)
369 generate_write(formats, type, native_type, suffix)
370
371
372 if __name__ == '__main__':
373 main()