glthread: sort variables in marshal structures to pack them optimally
[mesa.git] / src / mapi / glapi / gen / gl_marshal.py
1
2 # Copyright (C) 2012 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 "Software"),
6 # to deal in the Software without restriction, including without limitation
7 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Software, and to permit persons to whom the
9 # Software is furnished to do so, subject to the following conditions:
10 #
11 # The above copyright notice and this permission notice (including the next
12 # paragraph) shall be included in all copies or substantial portions of the
13 # Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 # IN THE SOFTWARE.
22
23 from __future__ import print_function
24
25 import contextlib
26 import getopt
27 import gl_XML
28 import license
29 import marshal_XML
30 import sys
31
32 header = """
33 #include "api_exec.h"
34 #include "glthread_marshal.h"
35 #include "dispatch.h"
36
37 #define COMPAT (ctx->API != API_OPENGL_CORE)
38
39 static inline int safe_mul(int a, int b)
40 {
41 if (a < 0 || b < 0) return -1;
42 if (a == 0 || b == 0) return 0;
43 if (a > INT_MAX / b) return -1;
44 return a * b;
45 }
46 """
47
48
49 file_index = 0
50 file_count = 1
51 current_indent = 0
52
53
54 def out(str):
55 if str:
56 print(' '*current_indent + str)
57 else:
58 print('')
59
60
61 @contextlib.contextmanager
62 def indent(delta = 3):
63 global current_indent
64 current_indent += delta
65 yield
66 current_indent -= delta
67
68
69 class PrintCode(gl_XML.gl_print_base):
70 def __init__(self):
71 super(PrintCode, self).__init__()
72
73 self.name = 'gl_marshal.py'
74 self.license = license.bsd_license_template % (
75 'Copyright (C) 2012 Intel Corporation', 'INTEL CORPORATION')
76
77 def printRealHeader(self):
78 print(header)
79
80 def printRealFooter(self):
81 pass
82
83 def print_sync_call(self, func, unmarshal = 0):
84 call = 'CALL_{0}(ctx->CurrentServerDispatch, ({1}))'.format(
85 func.name, func.get_called_parameter_string())
86 if func.return_type == 'void':
87 out('{0};'.format(call))
88 if func.marshal_call_after and not unmarshal:
89 out(func.marshal_call_after);
90 else:
91 out('return {0};'.format(call))
92 assert not func.marshal_call_after
93
94 def print_sync_dispatch(self, func):
95 self.print_sync_call(func)
96
97 def print_sync_body(self, func):
98 out('/* {0}: marshalled synchronously */'.format(func.name))
99 out('{0} GLAPIENTRY'.format(func.return_type))
100 out('_mesa_marshal_{0}({1})'.format(func.name, func.get_parameter_string()))
101 out('{')
102 with indent():
103 out('GET_CURRENT_CONTEXT(ctx);')
104 out('_mesa_glthread_finish_before(ctx, "{0}");'.format(func.name))
105 self.print_sync_call(func)
106 out('}')
107 out('')
108 out('')
109
110 def print_async_dispatch(self, func):
111 out('cmd = _mesa_glthread_allocate_command(ctx, '
112 'DISPATCH_CMD_{0}, cmd_size);'.format(func.name))
113 for p in func.fixed_params:
114 if p.count:
115 out('memcpy(cmd->{0}, {0}, {1});'.format(
116 p.name, p.size_string()))
117 else:
118 out('cmd->{0} = {0};'.format(p.name))
119 if func.variable_params:
120 out('char *variable_data = (char *) (cmd + 1);')
121 i = 1
122 for p in func.variable_params:
123 if p.img_null_flag:
124 out('cmd->{0}_null = !{0};'.format(p.name))
125 out('if (!cmd->{0}_null) {{'.format(p.name))
126 with indent():
127 out(('memcpy(variable_data, {0}, {0}_size);').format(p.name))
128 if i < len(func.variable_params):
129 out('variable_data += {0}_size;'.format(p.name))
130 out('}')
131 else:
132 out(('memcpy(variable_data, {0}, {0}_size);').format(p.name))
133 if i < len(func.variable_params):
134 out('variable_data += {0}_size;'.format(p.name))
135 i += 1
136
137 if not func.fixed_params and not func.variable_params:
138 out('(void) cmd;')
139
140 if func.marshal_call_after:
141 out(func.marshal_call_after);
142
143 # Uncomment this if you want to call _mesa_glthread_finish for debugging
144 #out('_mesa_glthread_finish(ctx);')
145
146 def get_type_size(self, str):
147 if str.find('*') != -1:
148 return 8;
149
150 mapping = {
151 'GLboolean': 1,
152 'GLbyte': 1,
153 'GLubyte': 1,
154 'GLenum': 2, # uses GLenum16
155 'GLshort': 2,
156 'GLushort': 2,
157 'GLint': 4,
158 'GLuint': 4,
159 'GLbitfield': 4,
160 'GLsizei': 4,
161 'GLfloat': 4,
162 'GLclampf': 4,
163 'GLfixed': 4,
164 'GLclampx': 4,
165 'GLhandleARB': 4,
166 'int': 4,
167 'float': 4,
168 'GLdouble': 8,
169 'GLclampd': 8,
170 'GLintptr': 8,
171 'GLsizeiptr': 8,
172 'GLint64': 8,
173 'GLuint64': 8,
174 'GLuint64EXT': 8,
175 'GLsync': 8,
176 }
177 val = mapping.get(str, 9999)
178 if val == 9999:
179 print('Unhandled type in gl_marshal.py.get_type_size: ' + str, file=sys.stderr)
180 return val
181
182 def print_async_struct(self, func):
183 out('struct marshal_cmd_{0}'.format(func.name))
184 out('{')
185 with indent():
186 out('struct marshal_cmd_base cmd_base;')
187
188 # Sort the parameters according to their size to pack the structure optimally
189 for p in sorted(func.fixed_params, key=lambda p: self.get_type_size(p.type_string())):
190 if p.count:
191 out('{0} {1}[{2}];'.format(
192 p.get_base_type_string(), p.name, p.count))
193 else:
194 type = p.type_string()
195 if type == 'GLenum':
196 type = 'GLenum16'
197 out('{0} {1};'.format(type, p.name))
198
199 for p in func.variable_params:
200 if p.img_null_flag:
201 out('bool {0}_null; /* If set, no data follows '
202 'for "{0}" */'.format(p.name))
203
204 for p in func.variable_params:
205 if p.count_scale != 1:
206 out(('/* Next {0} bytes are '
207 '{1} {2}[{3}][{4}] */').format(
208 p.size_string(marshal = 1), p.get_base_type_string(),
209 p.name, p.counter, p.count_scale))
210 else:
211 out(('/* Next {0} bytes are '
212 '{1} {2}[{3}] */').format(
213 p.size_string(marshal = 1), p.get_base_type_string(),
214 p.name, p.counter))
215 out('};')
216
217 def print_async_unmarshal(self, func):
218 out('void')
219 out(('_mesa_unmarshal_{0}(struct gl_context *ctx, '
220 'const struct marshal_cmd_{0} *cmd)').format(func.name))
221 out('{')
222 with indent():
223 for p in func.fixed_params:
224 if p.count:
225 p_decl = '{0} * {1} = cmd->{1};'.format(
226 p.get_base_type_string(), p.name)
227 else:
228 p_decl = '{0} {1} = cmd->{1};'.format(
229 p.type_string(), p.name)
230
231 if not p_decl.startswith('const '):
232 # Declare all local function variables as const, even if
233 # the original parameter is not const.
234 p_decl = 'const ' + p_decl
235
236 out(p_decl)
237
238 if func.variable_params:
239 for p in func.variable_params:
240 out('{0} * {1};'.format(
241 p.get_base_type_string(), p.name))
242 out('const char *variable_data = (const char *) (cmd + 1);')
243 i = 1
244 for p in func.variable_params:
245 out('{0} = ({1} *) variable_data;'.format(
246 p.name, p.get_base_type_string()))
247
248 if p.img_null_flag:
249 out('if (cmd->{0}_null)'.format(p.name))
250 with indent():
251 out('{0} = NULL;'.format(p.name))
252 if i < len(func.variable_params):
253 out('else')
254 with indent():
255 out('variable_data += {0};'.format(p.size_string(False, marshal = 1)))
256 elif i < len(func.variable_params):
257 out('variable_data += {0};'.format(p.size_string(False, marshal = 1)))
258 i += 1
259
260 self.print_sync_call(func, unmarshal = 1)
261 out('}')
262
263 def validate_count_or_fallback(self, func):
264 # Check that any counts for variable-length arguments might be < 0, in
265 # which case the command alloc or the memcpy would blow up before we
266 # get to the validation in Mesa core.
267 list = []
268 for p in func.parameters:
269 if p.is_variable_length():
270 list.append('{0}_size < 0'.format(p.name))
271 list.append('({0}_size > 0 && !{0})'.format(p.name))
272
273 if len(list) == 0:
274 return
275
276 list.append('(unsigned)cmd_size > MARSHAL_MAX_CMD_SIZE')
277
278 out('if (unlikely({0})) {{'.format(' || '.join(list)))
279 with indent():
280 out('_mesa_glthread_finish_before(ctx, "{0}");'.format(func.name))
281 self.print_sync_dispatch(func)
282 out('return;')
283 out('}')
284
285 def print_async_marshal(self, func):
286 out('void GLAPIENTRY')
287 out('_mesa_marshal_{0}({1})'.format(
288 func.name, func.get_parameter_string()))
289 out('{')
290 with indent():
291 out('GET_CURRENT_CONTEXT(ctx);')
292 for p in func.variable_params:
293 out('int {0}_size = {1};'.format(p.name, p.size_string(marshal = 1)))
294
295 struct = 'struct marshal_cmd_{0}'.format(func.name)
296 size_terms = ['sizeof({0})'.format(struct)]
297 for p in func.variable_params:
298 if p.img_null_flag:
299 size_terms.append('({0} ? {0}_size : 0)'.format(p.name))
300 else:
301 size_terms.append('{0}_size'.format(p.name))
302 out('int cmd_size = {0};'.format(' + '.join(size_terms)))
303 out('{0} *cmd;'.format(struct))
304
305 self.validate_count_or_fallback(func)
306
307 if func.marshal_sync:
308 out('if ({0}) {{'.format(func.marshal_sync))
309 with indent():
310 out('_mesa_glthread_finish_before(ctx, "{0}");'.format(func.name))
311 self.print_sync_dispatch(func)
312 out('return;')
313 out('}')
314
315 with indent():
316 self.print_async_dispatch(func)
317 out('}')
318
319 def print_async_body(self, func):
320 out('/* {0}: marshalled asynchronously */'.format(func.name))
321 self.print_async_struct(func)
322 self.print_async_unmarshal(func)
323 self.print_async_marshal(func)
324 out('')
325 out('')
326
327 def print_unmarshal_dispatch_cmd(self, api):
328 out('const _mesa_unmarshal_func _mesa_unmarshal_dispatch[NUM_DISPATCH_CMD] = {')
329 with indent():
330 for func in api.functionIterateAll():
331 flavor = func.marshal_flavor()
332 if flavor in ('skip', 'sync'):
333 continue
334 out('[DISPATCH_CMD_{0}] = (_mesa_unmarshal_func)_mesa_unmarshal_{0},'.format(func.name))
335 out('};')
336 out('')
337 out('')
338
339 def print_create_marshal_table(self, api):
340 out('/* _mesa_create_marshal_table takes a long time to compile with -O2 */')
341 out('#ifdef __GNUC__')
342 out('__attribute__((optimize("O1")))')
343 out('#endif')
344 out('struct _glapi_table *')
345 out('_mesa_create_marshal_table(const struct gl_context *ctx)')
346 out('{')
347 with indent():
348 out('struct _glapi_table *table;')
349 out('')
350 out('table = _mesa_alloc_dispatch_table();')
351 out('if (table == NULL)')
352 with indent():
353 out('return NULL;')
354 out('')
355 for func in api.functionIterateAll():
356 if func.marshal_flavor() == 'skip':
357 continue
358 # Don't use the SET_* functions, because they increase compile time
359 # by 20 seconds (on Ryzen 1700X).
360 out('if (_gloffset_{0} >= 0)'.format(func.name))
361 out(' ((_glapi_proc *)(table))[_gloffset_{0}] = (_glapi_proc)_mesa_marshal_{0};'
362 .format(func.name))
363 out('')
364 out('return table;')
365 out('}')
366 out('')
367 out('')
368
369 def printBody(self, api):
370 # The first file only contains the dispatch tables
371 if file_index == 0:
372 self.print_unmarshal_dispatch_cmd(api)
373 self.print_create_marshal_table(api)
374 return
375
376 # The remaining files contain the marshal and unmarshal functions
377 func_per_file = (len(api.functionIterateAll()) // (file_count - 1)) + 1
378 i = -1
379 for func in api.functionIterateAll():
380 i += 1
381 if i // func_per_file != (file_index - 1):
382 continue
383
384 flavor = func.marshal_flavor()
385 if flavor in ('skip', 'custom'):
386 continue
387 elif flavor == 'async':
388 self.print_async_body(func)
389 elif flavor == 'sync':
390 self.print_sync_body(func)
391
392
393 def show_usage():
394 print('Usage: %s [-f input_file_name]' % sys.argv[0])
395 sys.exit(1)
396
397
398 if __name__ == '__main__':
399 file_name = 'gl_API.xml'
400
401 try:
402 (args, trail) = getopt.getopt(sys.argv[1:], 'm:f:i:n:')
403 except Exception:
404 show_usage()
405
406 for (arg,val) in args:
407 if arg == '-f':
408 file_name = val
409 elif arg == '-i':
410 file_index = int(val)
411 elif arg == '-n':
412 file_count = int(val)
413
414 assert file_index < file_count
415 printer = PrintCode()
416
417 api = gl_XML.parse_GL_API(file_name, marshal_XML.marshal_item_factory())
418 printer.Print(api)