Merge remote branch 'origin/master' into lp-binning
[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 == ARITH:
80 print ' %s pixel = *src_pixel++;' % src_native_type
81 shift = 0;
82 for i in range(4):
83 src_type = format.in_types[i]
84 width = src_type.size
85 if names[i]:
86 value = 'pixel'
87 mask = (1 << width) - 1
88 if shift:
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)
94 shift += width
95 elif format.layout == ARRAY:
96 for i in range(4):
97 src_type = format.in_types[i]
98 if names[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)
102 else:
103 assert False
104
105 for i in range(4):
106 if format.colorspace == 'rgb':
107 swizzle = format.out_swizzle[i]
108 if swizzle < 4:
109 value = names[swizzle]
110 elif swizzle == SWIZZLE_0:
111 value = '0'
112 elif swizzle == SWIZZLE_1:
113 value = '1'
114 else:
115 assert False
116 elif format.colorspace == 'zs':
117 if i < 3:
118 value = 'z'
119 else:
120 value = '1'
121 else:
122 assert False
123 print ' TILE_PIXEL(dst, x, y, %u) = %s; /* %s */' % (i, value, 'rgba'[i])
124
125 print ' }'
126 print ' src_row += src_stride;'
127 print ' }'
128 print '}'
129 print
130
131
132 def generate_format_write(format, src_type, src_native_type, src_suffix):
133 '''Generate the function to write pixels to a particular format'''
134
135 name = short_name(format)
136
137 dst_native_type = native_type(format)
138
139 print 'static void'
140 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)
141 print '{'
142 print ' unsigned x, y;'
143 print ' uint8_t *dst_row = dst + y0*dst_stride;'
144 print ' for (y = 0; y < h; ++y) {'
145 print ' %s *dst_pixel = (%s *)(dst_row + x0*%u);' % (dst_native_type, dst_native_type, format.stride())
146 print ' for (x = 0; x < w; ++x) {'
147
148 inv_swizzle = [None]*4
149 if format.colorspace == 'rgb':
150 for i in range(4):
151 swizzle = format.out_swizzle[i]
152 if swizzle < 4:
153 inv_swizzle[swizzle] = i
154 elif format.colorspace == 'zs':
155 swizzle = format.out_swizzle[0]
156 if swizzle < 4:
157 inv_swizzle[swizzle] = 0
158 else:
159 assert False
160
161 if format.layout == ARITH:
162 print ' %s pixel = 0;' % dst_native_type
163 shift = 0;
164 for i in range(4):
165 dst_type = format.in_types[i]
166 width = dst_type.size
167 if inv_swizzle[i] is not None:
168 value = 'TILE_PIXEL(src, x, y, %u)' % inv_swizzle[i]
169 value = conversion_expr(src_type, dst_type, dst_native_type, value)
170 if shift:
171 value = '(%s << %u)' % (value, shift)
172 print ' pixel |= %s;' % value
173 shift += width
174 print ' *dst_pixel++ = pixel;'
175 elif format.layout == ARRAY:
176 for i in range(4):
177 dst_type = format.in_types[i]
178 if inv_swizzle[i] is not None:
179 value = 'TILE_PIXEL(src, x, y, %u)' % inv_swizzle[i]
180 value = conversion_expr(src_type, dst_type, dst_native_type, value)
181 print ' *dst_pixel++ = %s;' % value
182 else:
183 assert False
184
185 print ' }'
186 print ' dst_row += dst_stride;'
187 print ' }'
188 print '}'
189 print
190
191
192 def generate_read(formats, dst_type, dst_native_type, dst_suffix):
193 '''Generate the dispatch function to read pixels from any format'''
194
195 for format in formats:
196 if is_format_supported(format):
197 generate_format_read(format, dst_type, dst_native_type, dst_suffix)
198
199 print 'void'
200 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)
201 print '{'
202 print ' void (*func)(%s *dst, const uint8_t *src, unsigned src_stride, unsigned x0, unsigned y0, unsigned w, unsigned h);' % dst_native_type
203 print ' switch(format) {'
204 for format in formats:
205 if is_format_supported(format):
206 print ' case %s:' % format.name
207 print ' func = &lp_tile_%s_read_%s;' % (short_name(format), dst_suffix)
208 print ' break;'
209 print ' default:'
210 print ' debug_printf("unsupported format\\n");'
211 print ' return;'
212 print ' }'
213 print ' func(dst, (const uint8_t *)src, src_stride, x, y, w, h);'
214 print '}'
215 print
216
217
218 def generate_write(formats, src_type, src_native_type, src_suffix):
219 '''Generate the dispatch function to write pixels to any format'''
220
221 for format in formats:
222 if is_format_supported(format):
223 generate_format_write(format, src_type, src_native_type, src_suffix)
224
225 print 'void'
226 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)
227
228 print '{'
229 print ' void (*func)(const %s *src, uint8_t *dst, unsigned dst_stride, unsigned x0, unsigned y0, unsigned w, unsigned h);' % src_native_type
230 print ' switch(format) {'
231 for format in formats:
232 if is_format_supported(format):
233 print ' case %s:' % format.name
234 print ' func = &lp_tile_%s_write_%s;' % (short_name(format), src_suffix)
235 print ' break;'
236 print ' default:'
237 print ' debug_printf("unsupported format\\n");'
238 print ' return;'
239 print ' }'
240 print ' func(src, (uint8_t *)dst, dst_stride, x, y, w, h);'
241 print '}'
242 print
243
244
245 def main():
246 formats = []
247 for arg in sys.argv[1:]:
248 formats.extend(parse(arg))
249
250 print '/* This file is autogenerated by lp_tile_soa.py from u_format.csv. Do not edit directly. */'
251 print
252 # This will print the copyright message on the top of this file
253 print __doc__.strip()
254 print
255 print '#include "pipe/p_compiler.h"'
256 print '#include "util/u_format.h"'
257 print '#include "util/u_math.h"'
258 print '#include "lp_tile_soa.h"'
259 print
260 print 'const unsigned char'
261 print 'tile_offset[TILE_VECTOR_HEIGHT][TILE_VECTOR_WIDTH] = {'
262 print ' { 0, 1, 4, 5},'
263 print ' { 2, 3, 6, 7},'
264 print ' { 8, 9, 12, 13},'
265 print ' { 10, 11, 14, 15}'
266 print '};'
267 print
268
269 generate_clamp()
270
271 type = Type(UNSIGNED, True, 8)
272 native_type = 'uint8_t'
273 suffix = '4ub'
274
275 generate_read(formats, type, native_type, suffix)
276 generate_write(formats, type, native_type, suffix)
277
278
279 if __name__ == '__main__':
280 main()