summaryrefslogtreecommitdiffstats
path: root/src/mesa/main/format_info.py
blob: 94017bfae0f55d5711df2fc239b5317b9a87d0c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#
# Copyright 2014 Intel Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sub license, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice (including the
# next paragraph) shall be included in all copies or substantial portions
# of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
# IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from __future__ import division, print_function

import format_parser as parser
import sys

def get_gl_base_format(fmat):
   if fmat.name == 'MESA_FORMAT_NONE':
      return 'GL_NONE'
   elif fmat.name in ['MESA_FORMAT_YCBCR', 'MESA_FORMAT_YCBCR_REV']:
      return 'GL_YCBCR_MESA'
   elif fmat.has_channel('r'):
      if fmat.has_channel('g'):
         if fmat.has_channel('b'):
            if fmat.has_channel('a'):
               return 'GL_RGBA'
            else:
               return 'GL_RGB'
         else:
            return 'GL_RG'
      else:
         return 'GL_RED'
   elif fmat.has_channel('l'):
      if fmat.has_channel('a'):
         return 'GL_LUMINANCE_ALPHA'
      else:
         return 'GL_LUMINANCE'
   elif fmat.has_channel('a') and fmat.num_channels() == 1:
      return 'GL_ALPHA'
   elif fmat.has_channel('z'):
      if fmat.has_channel('s'):
         return 'GL_DEPTH_STENCIL'
      else:
         return 'GL_DEPTH_COMPONENT'
   elif fmat.has_channel('s'):
      return 'GL_STENCIL_INDEX'
   elif fmat.has_channel('i') and fmat.num_channels() == 1:
      return 'GL_INTENSITY'
   else:
      sys.exit("error, could not determine base format for {0}, check swizzle".format(fmat.name));

def get_gl_data_type(fmat):
   if fmat.is_compressed():
      if 'FLOAT' in fmat.name:
         return 'GL_FLOAT'
      elif 'SIGNED' in fmat.name or 'SNORM' in fmat.name:
         return 'GL_SIGNED_NORMALIZED'
      else:
         return 'GL_UNSIGNED_NORMALIZED'
   elif fmat.name in ['MESA_FORMAT_YCBCR', 'MESA_FORMAT_YCBCR_REV']:
      return 'GL_UNSIGNED_NORMALIZED'

   channel = None
   for chan in fmat.channels:
      if chan.type == 'x' and len(fmat.channels) > 1:
         continue # We can do better
      elif chan.name == 's' and fmat.has_channel('z'):
         continue # We'll use the type from the depth instead

      channel = chan
      break;

   if channel.type == parser.UNSIGNED:
      if channel.norm:
         return 'GL_UNSIGNED_NORMALIZED'
      else:
         return 'GL_UNSIGNED_INT'
   elif channel.type == parser.SIGNED:
      if channel.norm:
         return 'GL_SIGNED_NORMALIZED'
      else:
         return 'GL_INT'
   elif channel.type == parser.FLOAT:
      return 'GL_FLOAT'
   elif channel.type == parser.VOID:
      return 'GL_NONE'
   else:
      assert False

def get_channel_bits(fmat, chan_name):
   if fmat.is_compressed():
      # These values are pretty-much bogus, but OpenGL requires that we
      # return an "approximate" number of bits.
      if fmat.layout == 's3tc':
         return 4 if fmat.has_channel(chan_name) else 0
      elif fmat.layout == 'fxt1':
         if chan_name in 'rgb':
            return 4
         elif chan_name == 'a':
            return 1 if fmat.has_channel('a') else 0
         else:
            return 0
      elif fmat.layout in ('rgtc', 'latc'):
         return 8 if fmat.has_channel(chan_name) else 0
      elif fmat.layout in ('etc1', 'etc2'):
         if fmat.name.endswith('_ALPHA1') and chan_name == 'a':
            return 1

         bits = 11 if fmat.name.endswith('11_EAC') else 8
         return bits if fmat.has_channel(chan_name) else 0
      elif fmat.layout == 'bptc':
         bits = 16 if fmat.name.endswith('_FLOAT') else 8
         return bits if fmat.has_channel(chan_name) else 0
      elif fmat.layout == 'astc':
         bits = 16 if 'RGBA' in fmat.name else 8
         return bits if fmat.has_channel(chan_name) else 0
      elif fmat.layout == 'atc':
         return 8 if fmat.has_channel(chan_name) else 0
      else:
         assert False
   else:
      # Uncompressed textures
      for chan in fmat.channels:
         if chan.name == chan_name:
            return chan.size
      return 0

formats = parser.parse(sys.argv[1])

print('''
/*
 * Mesa 3-D graphics library
 *
 * Copyright (c) 2014 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

 /*
  * This file is AUTOGENERATED by format_info.py.  Do not edit it
  * manually or commit it into version control.
  */

static const struct gl_format_info format_info[MESA_FORMAT_COUNT] =
{
''')

def format_channel_bits(fmat, tuple_list):
   return ['.%s = %s' % (field, str(get_channel_bits(fmat, name))) for (field, name) in tuple_list]


for fmat in formats:
   print('   {')
   print('      .Name = {0},'.format(fmat.name))
   print('      .StrName = "{0}",'.format(fmat.name))
   print('      .Layout = {0},'.format('MESA_FORMAT_LAYOUT_' + fmat.layout.upper()))
   print('      .BaseFormat = {0},'.format(get_gl_base_format(fmat)))
   print('      .DataType = {0},'.format(get_gl_data_type(fmat)))

   bits = [('RedBits', 'r'), ('GreenBits', 'g'), ('BlueBits', 'b'), ('AlphaBits', 'a')]
   print('      {0},'.format(', '.join(format_channel_bits(fmat, bits))))
   bits = [('LuminanceBits', 'l'), ('IntensityBits', 'i'), ('DepthBits', 'z'), ('StencilBits', 's')]
   print('      {0},'.format(', '.join(format_channel_bits(fmat, bits))))

   print('      .IsSRGBFormat = {0:d},'.format(fmat.colorspace == 'srgb'))

   print('      .BlockWidth = {0}, .BlockHeight = {1}, .BlockDepth = {2},'.format(fmat.block_width, fmat.block_height, fmat.block_depth))
   print('      .BytesPerBlock = {0},'.format(int(fmat.block_size() / 8)))

   print('      .Swizzle = {{ {0} }},'.format(', '.join(map(str, fmat.swizzle))))
   if fmat.is_array():
      chan = fmat.array_element()
      norm = chan.norm or chan.type == parser.FLOAT
      print('      .ArrayFormat = MESA_ARRAY_FORMAT({0}),'.format(', '.join([
         str(chan.size // 8),
         str(int(chan.sign)),
         str(int(chan.type == parser.FLOAT)),
         str(int(norm)),
         str(len(fmat.channels)),
         str(fmat.swizzle[0]),
         str(fmat.swizzle[1]),
         str(fmat.swizzle[2]),
         str(fmat.swizzle[3]),
      ])))
   else:
      print('      .ArrayFormat = 0,')
   print('   },')

print('};')