summaryrefslogtreecommitdiffstats
path: root/src/mesa/main/get_hash_generator.py
blob: ddd498fee47562d1ad0288bc83eb3caa0d5048b5 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# coding=utf-8
# -*- Mode: Python; py-indent-offset: 4 -*-
#
# Copyright © 2012 Intel Corporation
#
# Based on code by Kristian Høgsberg <krh@bitplanet.net>,
#   extracted from mesa/main/get.c
#
# 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
# on 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
# IBM 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.

# Generate a C header file containing hash tables of glGet parameter
# names for each GL API. The generated file is to be included by glGet.c

import os, sys, imp, getopt
from collections import defaultdict
import get_hash_params

cur_dir = os.path.dirname(sys.argv[0])
param_desc_file = "%s/get_hash_params.py" % cur_dir

GLAPI = "%s/../../mapi/glapi/gen" % cur_dir
sys.path.append(GLAPI)
import gl_XML

prime_factor = 89
prime_step = 281
hash_table_size = 1024

gl_apis=set(["GL", "GL_CORE", "GLES", "GLES2", "GLES3", "GLES31", "GLES32"])

def print_header():
   print "typedef const unsigned short table_t[%d];\n" % (hash_table_size)
   print "static const int prime_factor = %d, prime_step = %d;\n" % \
          (prime_factor, prime_step)

def print_params(params):
   print "static const struct value_desc values[] = {"
   for p in params:
      print "    { %s, %s }," % (p[0], p[1])

   print "};\n"

def api_name(api):
   return "API_OPEN%s" % api

# This must match gl_api enum in src/mesa/main/mtypes.h
api_enum = [
   'GL',
   'GLES',
   'GLES2',
   'GL_CORE',
   'GLES3', # Not in gl_api enum in mtypes.h
   'GLES31', # Not in gl_api enum in mtypes.h
   'GLES32', # Not in gl_api enum in mtypes.h
]

def api_index(api):
    return api_enum.index(api)

def table_name(api):
   return "table_" + api_name(api)

def print_table(api, table):
   print "static table_t %s = {" % (table_name(api))

   # convert sparse (index, value) table into a dense table
   dense_table = [0] * hash_table_size
   for i, v in table:
      dense_table[i] = v

   row_size = 4
   for i in range(0, hash_table_size, row_size):
      row = dense_table[i : i + row_size]
      idx_val = ["%4d" % v for v in row]
      print " " * 4 + ", ".join(idx_val) + ","

   print "};\n"

def print_tables(tables):
   for table in tables:
      print_table(table["apis"][0], table["indices"])

   dense_tables = ['NULL'] * len(api_enum)
   for table in tables:
      tname = table_name(table["apis"][0])
      for api in table["apis"]:
         i = api_index(api)
         dense_tables[i] = "&%s" % (tname)

   print "static table_t *table_set[] = {"
   for expr in dense_tables:
      print "   %s," % expr
   print "};\n"

   print "#define table(api) (*table_set[api])"

# Merge tables with matching parameter lists (i.e. GL and GL_CORE)
def merge_tables(tables):
   merged_tables = []
   for api, indices in sorted(tables.items()):
      matching_table = filter(lambda mt:mt["indices"] == indices,
                              merged_tables)
      if matching_table:
         matching_table[0]["apis"].append(api)
      else:
         merged_tables.append({"apis": [api], "indices": indices})

   return merged_tables

def add_to_hash_table(table, hash_val, value):
   while True:
      index = hash_val & (hash_table_size - 1)
      if index not in table:
         table[index] = value
         break
      hash_val += prime_step

def die(msg):
   sys.stderr.write("%s: %s\n" % (program, msg))
   exit(1)

program = os.path.basename(sys.argv[0])

def generate_hash_tables(enum_list, enabled_apis, param_descriptors):
   tables = defaultdict(lambda:{})

   # the first entry should be invalid, so that get.c:find_value can use
   # its index for the 'enum not found' condition.
   params = [[0, ""]]

   for param_block in param_descriptors:
      if set(["apis", "params"]) != set(param_block):
         die("missing fields (%s) in param descriptor file (%s)" %
               (", ".join(set(["apis", "params"]) - set(param_block)),
                param_desc_file))

      valid_apis = set(param_block["apis"])
      if valid_apis - gl_apis:
         die("unknown API(s) in param descriptor file (%s): %s\n" %
               (param_desc_file, ",".join(valid_apis - gl_apis)))

      if not (valid_apis & enabled_apis):
         continue

      valid_apis &= enabled_apis

      for param in param_block["params"]:
         enum_name = param[0]
         enum_val = enum_list[enum_name].value
         hash_val = enum_val * prime_factor

         for api in valid_apis:
            add_to_hash_table(tables[api], hash_val, len(params))
            # Also add GLES2 items to the GLES3+ hash tables
            if api == "GLES2":
               add_to_hash_table(tables["GLES3"], hash_val, len(params))
               add_to_hash_table(tables["GLES31"], hash_val, len(params))
               add_to_hash_table(tables["GLES32"], hash_val, len(params))
            # Also add GLES3 items to the GLES31+ hash tables
            if api == "GLES3":
               add_to_hash_table(tables["GLES31"], hash_val, len(params))
               add_to_hash_table(tables["GLES32"], hash_val, len(params))
            # Also add GLES31 items to the GLES32+ hash tables
            if api == "GLES31":
               add_to_hash_table(tables["GLES32"], hash_val, len(params))
         params.append(["GL_" + enum_name, param[1]])

   sorted_tables={}
   for api, indices in tables.items():
      sorted_tables[api] = sorted(indices.items())

   return params, merge_tables(sorted_tables)


def show_usage():
   sys.stderr.write(
"""Usage: %s [OPTIONS]
  -f <file>          specify GL API XML file
""" % (program))
   exit(1)

if __name__ == '__main__':
   try:
      (opts, args) = getopt.getopt(sys.argv[1:], "f:")
   except Exception,e:
      show_usage()

   if len(args) != 0:
      show_usage()

   api_desc_file = ""

   for opt_name, opt_val in opts:
      if opt_name == "-f":
         api_desc_file = opt_val

   if not api_desc_file:
      die("missing descriptor file (-f)\n")

   # generate the code for all APIs
   enabled_apis = set(["GLES", "GLES2", "GLES3", "GLES31", "GLES32",
                       "GL", "GL_CORE"])

   try:
      api_desc = gl_XML.parse_GL_API(api_desc_file)
   except Exception:
      die("couldn't parse API specification file %s\n" % api_desc_file)

   (params, hash_tables) = generate_hash_tables(api_desc.enums_by_name,
                              enabled_apis, get_hash_params.descriptor)

   print_header()
   print_params(params)
   print_tables(hash_tables)