Merge commit 'origin/mesa_7_5_branch' into mesa_7_6_branch
[mesa.git] / src / gallium / auxiliary / util / u_format_parse.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
33 import sys
34
35
36 VOID, UNSIGNED, SIGNED, FIXED, FLOAT = range(5)
37
38 SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_0, SWIZZLE_1, SWIZZLE_NONE, = range(7)
39
40 ARITH = 'arith'
41 ARRAY = 'array'
42
43
44 class Type:
45 '''Describe the type of a color channel.'''
46
47 def __init__(self, kind, norm, size):
48 self.kind = kind
49 self.norm = norm
50 self.size = size
51 self.sign = kind in (SIGNED, FIXED, FLOAT)
52
53 def __str__(self):
54 s = str(self.kind)
55 if self.norm:
56 s += 'n'
57 s += str(self.size)
58 return s
59
60 def __eq__(self, other):
61 return self.kind == other.kind and self.norm == other.norm and self.size == other.size
62
63
64 class Format:
65 '''Describe a pixel format.'''
66
67 def __init__(self, name, layout, block_width, block_height, in_types, out_swizzle, colorspace):
68 self.name = name
69 self.layout = layout
70 self.block_width = block_width
71 self.block_height = block_height
72 self.in_types = in_types
73 self.out_swizzle = out_swizzle
74 self.name = name
75 self.colorspace = colorspace
76
77 def __str__(self):
78 return self.name
79
80 def block_size(self):
81 size = 0
82 for type in self.in_types:
83 size += type.size
84 return size
85
86 def stride(self):
87 return self.block_size()/8
88
89
90 _kind_parse_map = {
91 '': VOID,
92 'x': VOID,
93 'u': UNSIGNED,
94 's': SIGNED,
95 'h': FIXED,
96 'f': FLOAT,
97 }
98
99 _swizzle_parse_map = {
100 'x': SWIZZLE_X,
101 'y': SWIZZLE_Y,
102 'z': SWIZZLE_Z,
103 'w': SWIZZLE_W,
104 '0': SWIZZLE_0,
105 '1': SWIZZLE_1,
106 '_': SWIZZLE_NONE,
107 }
108
109 def parse(filename):
110 '''Parse the format descrition in CSV format in terms of the
111 Type and Format classes above.'''
112
113 stream = open(filename)
114 formats = []
115 for line in stream:
116 line = line.rstrip()
117 fields = [field.strip() for field in line.split(',')]
118 name = fields[0]
119 layout = fields[1]
120 block_width, block_height = map(int, fields[2:4])
121 in_types = []
122 for field in fields[4:8]:
123 if field:
124 kind = _kind_parse_map[field[0]]
125 if field[1] == 'n':
126 norm = True
127 size = int(field[2:])
128 else:
129 norm = False
130 size = int(field[1:])
131 else:
132 kind = VOID
133 norm = False
134 size = 0
135 in_type = Type(kind, norm, size)
136 in_types.append(in_type)
137 out_swizzle = [_swizzle_parse_map[swizzle] for swizzle in fields[8]]
138 colorspace = fields[9]
139 formats.append(Format(name, layout, block_width, block_height, in_types, out_swizzle, colorspace))
140 return formats
141