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