1af69d88dSmrg#
2af69d88dSmrg# Copyright 2014 Intel Corporation
3af69d88dSmrg#
4af69d88dSmrg# Permission is hereby granted, free of charge, to any person obtaining a
5af69d88dSmrg# copy of this software and associated documentation files (the
6af69d88dSmrg# "Software"), to deal in the Software without restriction, including
7af69d88dSmrg# without limitation the rights to use, copy, modify, merge, publish,
8af69d88dSmrg# distribute, sub license, and/or sell copies of the Software, and to
9af69d88dSmrg# permit persons to whom the Software is furnished to do so, subject to
10af69d88dSmrg# the following conditions:
11af69d88dSmrg#
12af69d88dSmrg# The above copyright notice and this permission notice (including the
13af69d88dSmrg# next paragraph) shall be included in all copies or substantial portions
14af69d88dSmrg# of the Software.
15af69d88dSmrg#
16af69d88dSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17af69d88dSmrg# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18af69d88dSmrg# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19af69d88dSmrg# IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
20af69d88dSmrg# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21af69d88dSmrg# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22af69d88dSmrg# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23af69d88dSmrg
24af69d88dSmrgimport format_parser as parser
25af69d88dSmrgimport sys
26af69d88dSmrg
27af69d88dSmrgdef get_gl_base_format(fmat):
28af69d88dSmrg   if fmat.name == 'MESA_FORMAT_NONE':
29af69d88dSmrg      return 'GL_NONE'
30af69d88dSmrg   elif fmat.name in ['MESA_FORMAT_YCBCR', 'MESA_FORMAT_YCBCR_REV']:
31af69d88dSmrg      return 'GL_YCBCR_MESA'
32af69d88dSmrg   elif fmat.has_channel('r'):
33af69d88dSmrg      if fmat.has_channel('g'):
34af69d88dSmrg         if fmat.has_channel('b'):
35af69d88dSmrg            if fmat.has_channel('a'):
36af69d88dSmrg               return 'GL_RGBA'
37af69d88dSmrg            else:
38af69d88dSmrg               return 'GL_RGB'
39af69d88dSmrg         else:
40af69d88dSmrg            return 'GL_RG'
41af69d88dSmrg      else:
42af69d88dSmrg         return 'GL_RED'
43af69d88dSmrg   elif fmat.has_channel('l'):
44af69d88dSmrg      if fmat.has_channel('a'):
45af69d88dSmrg         return 'GL_LUMINANCE_ALPHA'
46af69d88dSmrg      else:
47af69d88dSmrg         return 'GL_LUMINANCE'
48af69d88dSmrg   elif fmat.has_channel('a') and fmat.num_channels() == 1:
49af69d88dSmrg      return 'GL_ALPHA'
50af69d88dSmrg   elif fmat.has_channel('z'):
51af69d88dSmrg      if fmat.has_channel('s'):
52af69d88dSmrg         return 'GL_DEPTH_STENCIL'
53af69d88dSmrg      else:
54af69d88dSmrg         return 'GL_DEPTH_COMPONENT'
55af69d88dSmrg   elif fmat.has_channel('s'):
56af69d88dSmrg      return 'GL_STENCIL_INDEX'
57af69d88dSmrg   elif fmat.has_channel('i') and fmat.num_channels() == 1:
58af69d88dSmrg      return 'GL_INTENSITY'
59af69d88dSmrg   else:
6001e04c3fSmrg      sys.exit("error, could not determine base format for {0}, check swizzle".format(fmat.name));
61af69d88dSmrg
62af69d88dSmrgdef get_gl_data_type(fmat):
63af69d88dSmrg   if fmat.is_compressed():
64af69d88dSmrg      if 'FLOAT' in fmat.name:
65af69d88dSmrg         return 'GL_FLOAT'
66af69d88dSmrg      elif 'SIGNED' in fmat.name or 'SNORM' in fmat.name:
67af69d88dSmrg         return 'GL_SIGNED_NORMALIZED'
68af69d88dSmrg      else:
69af69d88dSmrg         return 'GL_UNSIGNED_NORMALIZED'
70af69d88dSmrg   elif fmat.name in ['MESA_FORMAT_YCBCR', 'MESA_FORMAT_YCBCR_REV']:
71af69d88dSmrg      return 'GL_UNSIGNED_NORMALIZED'
72af69d88dSmrg
73af69d88dSmrg   channel = None
74af69d88dSmrg   for chan in fmat.channels:
75af69d88dSmrg      if chan.type == 'x' and len(fmat.channels) > 1:
76af69d88dSmrg         continue # We can do better
77af69d88dSmrg      elif chan.name == 's' and fmat.has_channel('z'):
78af69d88dSmrg         continue # We'll use the type from the depth instead
79af69d88dSmrg
80af69d88dSmrg      channel = chan
81af69d88dSmrg      break;
82af69d88dSmrg
83af69d88dSmrg   if channel.type == parser.UNSIGNED:
84af69d88dSmrg      if channel.norm:
85af69d88dSmrg         return 'GL_UNSIGNED_NORMALIZED'
86af69d88dSmrg      else:
87af69d88dSmrg         return 'GL_UNSIGNED_INT'
88af69d88dSmrg   elif channel.type == parser.SIGNED:
89af69d88dSmrg      if channel.norm:
90af69d88dSmrg         return 'GL_SIGNED_NORMALIZED'
91af69d88dSmrg      else:
92af69d88dSmrg         return 'GL_INT'
93af69d88dSmrg   elif channel.type == parser.FLOAT:
94af69d88dSmrg      return 'GL_FLOAT'
95af69d88dSmrg   elif channel.type == parser.VOID:
96af69d88dSmrg      return 'GL_NONE'
97af69d88dSmrg   else:
98af69d88dSmrg      assert False
99af69d88dSmrg
100af69d88dSmrgdef get_channel_bits(fmat, chan_name):
101af69d88dSmrg   if fmat.is_compressed():
102af69d88dSmrg      # These values are pretty-much bogus, but OpenGL requires that we
103af69d88dSmrg      # return an "approximate" number of bits.
104af69d88dSmrg      if fmat.layout == 's3tc':
105af69d88dSmrg         return 4 if fmat.has_channel(chan_name) else 0
106af69d88dSmrg      elif fmat.layout == 'fxt1':
107af69d88dSmrg         if chan_name in 'rgb':
108af69d88dSmrg            return 4
109af69d88dSmrg         elif chan_name == 'a':
110af69d88dSmrg            return 1 if fmat.has_channel('a') else 0
111af69d88dSmrg         else:
112af69d88dSmrg            return 0
11301e04c3fSmrg      elif fmat.layout in ('rgtc', 'latc'):
114af69d88dSmrg         return 8 if fmat.has_channel(chan_name) else 0
115af69d88dSmrg      elif fmat.layout in ('etc1', 'etc2'):
116af69d88dSmrg         if fmat.name.endswith('_ALPHA1') and chan_name == 'a':
117af69d88dSmrg            return 1
118af69d88dSmrg
119af69d88dSmrg         bits = 11 if fmat.name.endswith('11_EAC') else 8
120af69d88dSmrg         return bits if fmat.has_channel(chan_name) else 0
121af69d88dSmrg      elif fmat.layout == 'bptc':
122af69d88dSmrg         bits = 16 if fmat.name.endswith('_FLOAT') else 8
123af69d88dSmrg         return bits if fmat.has_channel(chan_name) else 0
12401e04c3fSmrg      elif fmat.layout == 'astc':
12501e04c3fSmrg         bits = 16 if 'RGBA' in fmat.name else 8
12601e04c3fSmrg         return bits if fmat.has_channel(chan_name) else 0
127a8bb7a65Smaya      elif fmat.layout == 'atc':
128a8bb7a65Smaya         return 8 if fmat.has_channel(chan_name) else 0
1297ec681f3Smrg      elif fmat.layout == 'other' and ('RG_RB' in fmat.name or 'GR_BR' in fmat.name):
1307ec681f3Smrg         return 8 if fmat.has_channel(chan_name) else 0
131af69d88dSmrg      else:
132af69d88dSmrg         assert False
133af69d88dSmrg   else:
134af69d88dSmrg      # Uncompressed textures
135af69d88dSmrg      for chan in fmat.channels:
136af69d88dSmrg         if chan.name == chan_name:
137af69d88dSmrg            return chan.size
138af69d88dSmrg      return 0
139af69d88dSmrg
140af69d88dSmrgformats = parser.parse(sys.argv[1])
141af69d88dSmrg
14201e04c3fSmrgprint('''
143af69d88dSmrg/*
144af69d88dSmrg * Mesa 3-D graphics library
145af69d88dSmrg *
146af69d88dSmrg * Copyright (c) 2014 Intel Corporation
147af69d88dSmrg *
148af69d88dSmrg * Permission is hereby granted, free of charge, to any person obtaining a
149af69d88dSmrg * copy of this software and associated documentation files (the "Software"),
150af69d88dSmrg * to deal in the Software without restriction, including without limitation
151af69d88dSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
152af69d88dSmrg * and/or sell copies of the Software, and to permit persons to whom the
153af69d88dSmrg * Software is furnished to do so, subject to the following conditions:
154af69d88dSmrg *
155af69d88dSmrg * The above copyright notice and this permission notice shall be included
156af69d88dSmrg * in all copies or substantial portions of the Software.
157af69d88dSmrg *
158af69d88dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
159af69d88dSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
160af69d88dSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
161af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
162af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
163af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
164af69d88dSmrg * OTHER DEALINGS IN THE SOFTWARE.
165af69d88dSmrg */
166af69d88dSmrg
167af69d88dSmrg /*
168af69d88dSmrg  * This file is AUTOGENERATED by format_info.py.  Do not edit it
169af69d88dSmrg  * manually or commit it into version control.
170af69d88dSmrg  */
171af69d88dSmrg
1727ec681f3Smrgstatic const struct mesa_format_info format_info[MESA_FORMAT_COUNT] =
173af69d88dSmrg{
17401e04c3fSmrg''')
175af69d88dSmrg
17601e04c3fSmrgdef format_channel_bits(fmat, tuple_list):
17701e04c3fSmrg   return ['.%s = %s' % (field, str(get_channel_bits(fmat, name))) for (field, name) in tuple_list]
178af69d88dSmrg
1797ec681f3Smrgbf_map = {
1807ec681f3Smrg   "GL_DEPTH_COMPONENT" : "MESA_ARRAY_FORMAT_BASE_FORMAT_DEPTH",
1817ec681f3Smrg   "GL_STENCIL_INDEX" : "MESA_ARRAY_FORMAT_BASE_FORMAT_STENCIL",
1827ec681f3Smrg}
183af69d88dSmrg
18401e04c3fSmrgfor fmat in formats:
1857ec681f3Smrg   print('   [{0}] = {{'.format(fmat.name))
18601e04c3fSmrg   print('      .Name = {0},'.format(fmat.name))
18701e04c3fSmrg   print('      .StrName = "{0}",'.format(fmat.name))
18801e04c3fSmrg   print('      .Layout = {0},'.format('MESA_FORMAT_LAYOUT_' + fmat.layout.upper()))
18901e04c3fSmrg   print('      .BaseFormat = {0},'.format(get_gl_base_format(fmat)))
19001e04c3fSmrg   print('      .DataType = {0},'.format(get_gl_data_type(fmat)))
19101e04c3fSmrg
19201e04c3fSmrg   bits = [('RedBits', 'r'), ('GreenBits', 'g'), ('BlueBits', 'b'), ('AlphaBits', 'a')]
19301e04c3fSmrg   print('      {0},'.format(', '.join(format_channel_bits(fmat, bits))))
19401e04c3fSmrg   bits = [('LuminanceBits', 'l'), ('IntensityBits', 'i'), ('DepthBits', 'z'), ('StencilBits', 's')]
19501e04c3fSmrg   print('      {0},'.format(', '.join(format_channel_bits(fmat, bits))))
19601e04c3fSmrg
19701e04c3fSmrg   print('      .IsSRGBFormat = {0:d},'.format(fmat.colorspace == 'srgb'))
19801e04c3fSmrg
19901e04c3fSmrg   print('      .BlockWidth = {0}, .BlockHeight = {1}, .BlockDepth = {2},'.format(fmat.block_width, fmat.block_height, fmat.block_depth))
20001e04c3fSmrg   print('      .BytesPerBlock = {0},'.format(int(fmat.block_size() / 8)))
20101e04c3fSmrg
20201e04c3fSmrg   print('      .Swizzle = {{ {0} }},'.format(', '.join(map(str, fmat.swizzle))))
20301e04c3fSmrg   if fmat.is_array():
20401e04c3fSmrg      chan = fmat.array_element()
20501e04c3fSmrg      norm = chan.norm or chan.type == parser.FLOAT
20601e04c3fSmrg      print('      .ArrayFormat = MESA_ARRAY_FORMAT({0}),'.format(', '.join([
2077ec681f3Smrg         bf_map.get(get_gl_base_format(fmat), "MESA_ARRAY_FORMAT_BASE_FORMAT_RGBA_VARIANTS"),
20801e04c3fSmrg         str(chan.size // 8),
20901e04c3fSmrg         str(int(chan.sign)),
21001e04c3fSmrg         str(int(chan.type == parser.FLOAT)),
21101e04c3fSmrg         str(int(norm)),
21201e04c3fSmrg         str(len(fmat.channels)),
21301e04c3fSmrg         str(fmat.swizzle[0]),
21401e04c3fSmrg         str(fmat.swizzle[1]),
21501e04c3fSmrg         str(fmat.swizzle[2]),
21601e04c3fSmrg         str(fmat.swizzle[3]),
21701e04c3fSmrg      ])))
21801e04c3fSmrg   else:
21901e04c3fSmrg      print('      .ArrayFormat = 0,')
22001e04c3fSmrg   print('   },')
221af69d88dSmrg
22201e04c3fSmrgprint('};')
223