r300: respect radeon common code fallbacks
[mesa.git] / src / gallium / auxiliary / util / u_format_access.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 math
41 import sys
42
43 from u_format_pack import *
44
45
46 def is_format_supported(format):
47 '''Determines whether we actually have the plumbing necessary to generate the
48 to read/write to/from this format.'''
49
50 # FIXME: Ideally we would support any format combination here.
51
52 # XXX: It should be straightforward to support srgb
53 if format.colorspace not in ('rgb', 'zs'):
54 return False
55
56 if format.layout != PLAIN:
57 return False
58
59 for i in range(4):
60 channel = format.channels[i]
61 if channel.type not in (VOID, UNSIGNED, FLOAT):
62 return False
63
64 # We can only read a color from a depth/stencil format if the depth channel is present
65 if format.colorspace == 'zs' and format.swizzles[0] == SWIZZLE_NONE:
66 return False
67
68 return True
69
70
71 def native_type(format):
72 '''Get the native appropriate for a format.'''
73
74 if format.layout == PLAIN:
75 if not format.is_array():
76 # For arithmetic pixel formats return the integer type that matches the whole pixel
77 return 'uint%u_t' % format.block_size()
78 else:
79 # For array pixel formats return the integer type that matches the color channel
80 channel = format.channels[0]
81 if channel.type == UNSIGNED:
82 return 'uint%u_t' % channel.size
83 elif channel.type == SIGNED:
84 return 'int%u_t' % channel.size
85 elif channel.type == FLOAT:
86 if channel.size == 32:
87 return 'float'
88 elif channel.size == 64:
89 return 'double'
90 else:
91 assert False
92 else:
93 assert False
94 else:
95 assert False
96
97
98 def generate_srgb_tables():
99 print 'static ubyte srgb_to_linear[256] = {'
100 for i in range(256):
101 print ' %s,' % (int(math.pow((i / 255.0 + 0.055) / 1.055, 2.4) * 255))
102 print '};'
103 print
104 print 'static ubyte linear_to_srgb[256] = {'
105 print ' 0,'
106 for i in range(1, 256):
107 print ' %s,' % (int((1.055 * math.pow(i / 255.0, 0.41666) - 0.055) * 255))
108 print '};'
109 print
110
111
112 def generate_format_read(format, dst_channel, dst_native_type, dst_suffix):
113 '''Generate the function to read pixels from a particular format'''
114
115 name = format.short_name()
116
117 src_native_type = native_type(format)
118
119 print 'static void'
120 print 'util_format_%s_read_%s(%s *dst, unsigned dst_stride, const uint8_t *src, unsigned src_stride, unsigned x0, unsigned y0, unsigned w, unsigned h)' % (name, dst_suffix, dst_native_type)
121 print '{'
122 print ' unsigned x, y;'
123 print ' const uint8_t *src_row = src + y0*src_stride;'
124 print ' %s *dst_row = dst;' % dst_native_type
125 print ' for (y = 0; y < h; ++y) {'
126 print ' const %s *src_pixel = (const %s *)(src_row + x0*%u);' % (src_native_type, src_native_type, format.stride())
127 print ' %s *dst_pixel = dst_row;' %dst_native_type
128 print ' for (x = 0; x < w; ++x) {'
129
130 names = ['']*4
131 if format.colorspace == 'rgb':
132 for i in range(4):
133 swizzle = format.swizzles[i]
134 if swizzle < 4:
135 names[swizzle] += 'rgba'[i]
136 elif format.colorspace == 'zs':
137 swizzle = format.swizzles[0]
138 if swizzle < 4:
139 names[swizzle] = 'z'
140 else:
141 assert False
142 else:
143 assert False
144
145 if format.layout == PLAIN:
146 if not format.is_array():
147 print ' %s pixel = *src_pixel++;' % src_native_type
148 shift = 0;
149 for i in range(4):
150 src_channel = format.channels[i]
151 width = src_channel.size
152 if names[i]:
153 value = 'pixel'
154 mask = (1 << width) - 1
155 if shift:
156 value = '(%s >> %u)' % (value, shift)
157 if shift + width < format.block_size():
158 value = '(%s & 0x%x)' % (value, mask)
159 value = conversion_expr(src_channel, dst_channel, dst_native_type, value)
160 print ' %s %s = %s;' % (dst_native_type, names[i], value)
161 shift += width
162 else:
163 for i in range(4):
164 src_channel = format.channels[i]
165 if names[i]:
166 value = 'src_pixel[%u]' % i
167 value = conversion_expr(src_channel, dst_channel, dst_native_type, value)
168 print ' %s %s = %s;' % (dst_native_type, names[i], value)
169 print ' src_pixel += %u;' % (format.nr_channels())
170 else:
171 assert False
172
173 for i in range(4):
174 if format.colorspace == 'rgb':
175 swizzle = format.swizzles[i]
176 if swizzle < 4:
177 value = names[swizzle]
178 elif swizzle == SWIZZLE_0:
179 value = '0'
180 elif swizzle == SWIZZLE_1:
181 value = get_one(dst_channel)
182 else:
183 assert False
184 elif format.colorspace == 'zs':
185 if i < 3:
186 value = 'z'
187 else:
188 value = get_one(dst_channel)
189 else:
190 assert False
191 print ' *dst_pixel++ = %s; /* %s */' % (value, 'rgba'[i])
192
193 print ' }'
194 print ' src_row += src_stride;'
195 print ' dst_row += dst_stride/sizeof(*dst_row);'
196 print ' }'
197 print '}'
198 print
199
200
201 def generate_format_write(format, src_channel, src_native_type, src_suffix):
202 '''Generate the function to write pixels to a particular format'''
203
204 name = format.short_name()
205
206 dst_native_type = native_type(format)
207
208 print 'static void'
209 print 'util_format_%s_write_%s(const %s *src, unsigned src_stride, uint8_t *dst, unsigned dst_stride, unsigned x0, unsigned y0, unsigned w, unsigned h)' % (name, src_suffix, src_native_type)
210 print '{'
211 print ' unsigned x, y;'
212 print ' uint8_t *dst_row = dst + y0*dst_stride;'
213 print ' const %s *src_row = src;' % src_native_type
214 print ' for (y = 0; y < h; ++y) {'
215 print ' %s *dst_pixel = (%s *)(dst_row + x0*%u);' % (dst_native_type, dst_native_type, format.stride())
216 print ' const %s *src_pixel = src_row;' %src_native_type
217 print ' for (x = 0; x < w; ++x) {'
218
219 inv_swizzle = format.inv_swizzles()
220
221 if format.layout == PLAIN:
222 if not format.is_array():
223 print ' %s pixel = 0;' % dst_native_type
224 shift = 0;
225 for i in range(4):
226 dst_channel = format.channels[i]
227 width = dst_channel.size
228 if inv_swizzle[i] is not None:
229 value = 'src_pixel[%u]' % inv_swizzle[i]
230 value = conversion_expr(src_channel, dst_channel, dst_native_type, value)
231 if shift:
232 value = '(%s << %u)' % (value, shift)
233 print ' pixel |= %s;' % value
234 shift += width
235 print ' *dst_pixel++ = pixel;'
236 else:
237 for i in range(4):
238 dst_channel = format.channels[i]
239 if inv_swizzle[i] is not None:
240 value = 'src_pixel[%u]' % inv_swizzle[i]
241 value = conversion_expr(src_channel, dst_channel, dst_native_type, value)
242 print ' *dst_pixel++ = %s;' % value
243 else:
244 assert False
245 print ' src_pixel += 4;'
246
247 print ' }'
248 print ' dst_row += dst_stride;'
249 print ' src_row += src_stride/sizeof(*src_row);'
250 print ' }'
251 print '}'
252 print
253
254
255 def generate_read(formats, dst_channel, dst_native_type, dst_suffix):
256 '''Generate the dispatch function to read pixels from any format'''
257
258 for format in formats:
259 if is_format_supported(format):
260 generate_format_read(format, dst_channel, dst_native_type, dst_suffix)
261
262 print 'void'
263 print 'util_format_read_%s(enum pipe_format format, %s *dst, unsigned dst_stride, const void *src, unsigned src_stride, unsigned x, unsigned y, unsigned w, unsigned h)' % (dst_suffix, dst_native_type)
264 print '{'
265 print ' void (*func)(%s *dst, unsigned dst_stride, const uint8_t *src, unsigned src_stride, unsigned x0, unsigned y0, unsigned w, unsigned h);' % dst_native_type
266 print ' switch(format) {'
267 for format in formats:
268 if is_format_supported(format):
269 print ' case %s:' % format.name
270 print ' func = &util_format_%s_read_%s;' % (format.short_name(), dst_suffix)
271 print ' break;'
272 print ' default:'
273 print ' debug_printf("unsupported format\\n");'
274 print ' return;'
275 print ' }'
276 print ' func(dst, dst_stride, (const uint8_t *)src, src_stride, x, y, w, h);'
277 print '}'
278 print
279
280
281 def generate_write(formats, src_channel, src_native_type, src_suffix):
282 '''Generate the dispatch function to write pixels to any format'''
283
284 for format in formats:
285 if is_format_supported(format):
286 generate_format_write(format, src_channel, src_native_type, src_suffix)
287
288 print 'void'
289 print 'util_format_write_%s(enum pipe_format format, const %s *src, unsigned src_stride, void *dst, unsigned dst_stride, unsigned x, unsigned y, unsigned w, unsigned h)' % (src_suffix, src_native_type)
290
291 print '{'
292 print ' void (*func)(const %s *src, unsigned src_stride, uint8_t *dst, unsigned dst_stride, unsigned x0, unsigned y0, unsigned w, unsigned h);' % src_native_type
293 print ' switch(format) {'
294 for format in formats:
295 if is_format_supported(format):
296 print ' case %s:' % format.name
297 print ' func = &util_format_%s_write_%s;' % (format.short_name(), src_suffix)
298 print ' break;'
299 print ' default:'
300 print ' debug_printf("unsupported format\\n");'
301 print ' return;'
302 print ' }'
303 print ' func(src, src_stride, (uint8_t *)dst, dst_stride, x, y, w, h);'
304 print '}'
305 print
306
307
308 def main():
309 formats = []
310 for arg in sys.argv[1:]:
311 formats.extend(parse(arg))
312
313 print '/* This file is autogenerated by u_format_access.py from u_format.csv. Do not edit directly. */'
314 print
315 # This will print the copyright message on the top of this file
316 print __doc__.strip()
317 print
318 print '#include "pipe/p_compiler.h"'
319 print '#include "u_math.h"'
320 print '#include "u_format_pack.h"'
321 print
322
323 generate_srgb_tables()
324
325 type = Channel(FLOAT, False, 32)
326 native_type = 'float'
327 suffix = '4f'
328
329 generate_read(formats, type, native_type, suffix)
330 generate_write(formats, type, native_type, suffix)
331
332 type = Channel(UNSIGNED, True, 8)
333 native_type = 'uint8_t'
334 suffix = '4ub'
335
336 generate_read(formats, type, native_type, suffix)
337 generate_write(formats, type, native_type, suffix)
338
339
340 if __name__ == '__main__':
341 main()