mesa: add ASTC 2D LDR decoder
[mesa.git] / src / mesa / main / format_info.py
1 #
2 # Copyright 2014 Intel Corporation
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining a
5 # copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sub license, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
11 #
12 # The above copyright notice and this permission notice (including the
13 # next paragraph) shall be included in all copies or substantial portions
14 # of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19 # IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
20 # ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 from __future__ import print_function
25
26 import format_parser as parser
27 import sys
28
29 def get_gl_base_format(fmat):
30 if fmat.name == 'MESA_FORMAT_NONE':
31 return 'GL_NONE'
32 elif fmat.name in ['MESA_FORMAT_YCBCR', 'MESA_FORMAT_YCBCR_REV']:
33 return 'GL_YCBCR_MESA'
34 elif fmat.has_channel('r'):
35 if fmat.has_channel('g'):
36 if fmat.has_channel('b'):
37 if fmat.has_channel('a'):
38 return 'GL_RGBA'
39 else:
40 return 'GL_RGB'
41 else:
42 return 'GL_RG'
43 else:
44 return 'GL_RED'
45 elif fmat.has_channel('l'):
46 if fmat.has_channel('a'):
47 return 'GL_LUMINANCE_ALPHA'
48 else:
49 return 'GL_LUMINANCE'
50 elif fmat.has_channel('a') and fmat.num_channels() == 1:
51 return 'GL_ALPHA'
52 elif fmat.has_channel('z'):
53 if fmat.has_channel('s'):
54 return 'GL_DEPTH_STENCIL'
55 else:
56 return 'GL_DEPTH_COMPONENT'
57 elif fmat.has_channel('s'):
58 return 'GL_STENCIL_INDEX'
59 elif fmat.has_channel('i') and fmat.num_channels() == 1:
60 return 'GL_INTENSITY'
61 else:
62 sys.exit("error, could not determine base format for {0}, check swizzle".format(fmat.name));
63
64 def get_gl_data_type(fmat):
65 if fmat.is_compressed():
66 if 'FLOAT' in fmat.name:
67 return 'GL_FLOAT'
68 elif 'SIGNED' in fmat.name or 'SNORM' in fmat.name:
69 return 'GL_SIGNED_NORMALIZED'
70 else:
71 return 'GL_UNSIGNED_NORMALIZED'
72 elif fmat.name in ['MESA_FORMAT_YCBCR', 'MESA_FORMAT_YCBCR_REV']:
73 return 'GL_UNSIGNED_NORMALIZED'
74
75 channel = None
76 for chan in fmat.channels:
77 if chan.type == 'x' and len(fmat.channels) > 1:
78 continue # We can do better
79 elif chan.name == 's' and fmat.has_channel('z'):
80 continue # We'll use the type from the depth instead
81
82 channel = chan
83 break;
84
85 if channel.type == parser.UNSIGNED:
86 if channel.norm:
87 return 'GL_UNSIGNED_NORMALIZED'
88 else:
89 return 'GL_UNSIGNED_INT'
90 elif channel.type == parser.SIGNED:
91 if channel.norm:
92 return 'GL_SIGNED_NORMALIZED'
93 else:
94 return 'GL_INT'
95 elif channel.type == parser.FLOAT:
96 return 'GL_FLOAT'
97 elif channel.type == parser.VOID:
98 return 'GL_NONE'
99 else:
100 assert False
101
102 def get_channel_bits(fmat, chan_name):
103 if fmat.is_compressed():
104 # These values are pretty-much bogus, but OpenGL requires that we
105 # return an "approximate" number of bits.
106 if fmat.layout == 's3tc':
107 return 4 if fmat.has_channel(chan_name) else 0
108 elif fmat.layout == 'fxt1':
109 if chan_name in 'rgb':
110 return 4
111 elif chan_name == 'a':
112 return 1 if fmat.has_channel('a') else 0
113 else:
114 return 0
115 elif fmat.layout in ('rgtc', 'latc'):
116 return 8 if fmat.has_channel(chan_name) else 0
117 elif fmat.layout in ('etc1', 'etc2'):
118 if fmat.name.endswith('_ALPHA1') and chan_name == 'a':
119 return 1
120
121 bits = 11 if fmat.name.endswith('11_EAC') else 8
122 return bits if fmat.has_channel(chan_name) else 0
123 elif fmat.layout == 'bptc':
124 bits = 16 if fmat.name.endswith('_FLOAT') else 8
125 return bits if fmat.has_channel(chan_name) else 0
126 elif fmat.layout == 'astc':
127 bits = 16 if 'RGBA' in fmat.name else 8
128 return bits if fmat.has_channel(chan_name) else 0
129 else:
130 assert False
131 else:
132 # Uncompressed textures
133 for chan in fmat.channels:
134 if chan.name == chan_name:
135 return chan.size
136 return 0
137
138 formats = parser.parse(sys.argv[1])
139
140 print('''
141 /*
142 * Mesa 3-D graphics library
143 *
144 * Copyright (c) 2014 Intel Corporation
145 *
146 * Permission is hereby granted, free of charge, to any person obtaining a
147 * copy of this software and associated documentation files (the "Software"),
148 * to deal in the Software without restriction, including without limitation
149 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
150 * and/or sell copies of the Software, and to permit persons to whom the
151 * Software is furnished to do so, subject to the following conditions:
152 *
153 * The above copyright notice and this permission notice shall be included
154 * in all copies or substantial portions of the Software.
155 *
156 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
157 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
158 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
159 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
160 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
161 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
162 * OTHER DEALINGS IN THE SOFTWARE.
163 */
164
165 /*
166 * This file is AUTOGENERATED by format_info.py. Do not edit it
167 * manually or commit it into version control.
168 */
169
170 static const struct gl_format_info format_info[MESA_FORMAT_COUNT] =
171 {
172 ''')
173
174 def format_channel_bits(fmat, tuple_list):
175 return ['.%s = %s' % (field, str(get_channel_bits(fmat, name))) for (field, name) in tuple_list]
176
177
178 for fmat in formats:
179 print(' {')
180 print(' .Name = {0},'.format(fmat.name))
181 print(' .StrName = "{0}",'.format(fmat.name))
182 print(' .Layout = {0},'.format('MESA_FORMAT_LAYOUT_' + fmat.layout.upper()))
183 print(' .BaseFormat = {0},'.format(get_gl_base_format(fmat)))
184 print(' .DataType = {0},'.format(get_gl_data_type(fmat)))
185
186 bits = [('RedBits', 'r'), ('GreenBits', 'g'), ('BlueBits', 'b'), ('AlphaBits', 'a')]
187 print(' {0},'.format(', '.join(format_channel_bits(fmat, bits))))
188 bits = [('LuminanceBits', 'l'), ('IntensityBits', 'i'), ('DepthBits', 'z'), ('StencilBits', 's')]
189 print(' {0},'.format(', '.join(format_channel_bits(fmat, bits))))
190
191 print(' .IsSRGBFormat = {0:d},'.format(fmat.colorspace == 'srgb'))
192
193 print(' .BlockWidth = {0}, .BlockHeight = {1}, .BlockDepth = {2},'.format(fmat.block_width, fmat.block_height, fmat.block_depth))
194 print(' .BytesPerBlock = {0},'.format(int(fmat.block_size() / 8)))
195
196 print(' .Swizzle = {{ {0} }},'.format(', '.join(map(str, fmat.swizzle))))
197 if fmat.is_array():
198 chan = fmat.array_element()
199 norm = chan.norm or chan.type == parser.FLOAT
200 print(' .ArrayFormat = MESA_ARRAY_FORMAT({0}),'.format(', '.join([
201 str(chan.size / 8),
202 str(int(chan.sign)),
203 str(int(chan.type == parser.FLOAT)),
204 str(int(norm)),
205 str(len(fmat.channels)),
206 str(fmat.swizzle[0]),
207 str(fmat.swizzle[1]),
208 str(fmat.swizzle[2]),
209 str(fmat.swizzle[3]),
210 ])))
211 else:
212 print(' .ArrayFormat = 0,')
213 print(' },')
214
215 print('};')