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