2 # Copyright (C) 2012 Intel Corporation
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:
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
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
23 from __future__
import print_function
34 #include "glthread_marshal.h"
35 #include "bufferobj.h"
38 #define COMPAT (ctx->API != API_OPENGL_CORE)
40 UNUSED static inline int safe_mul(int a, int b)
42 if (a < 0 || b < 0) return -1;
43 if (a == 0 || b == 0) return 0;
44 if (a > INT_MAX / b) return -1;
57 print(' '*current_indent
+ str)
62 @contextlib.contextmanager
63 def indent(delta
= 3):
65 current_indent
+= delta
67 current_indent
-= delta
70 class PrintCode(gl_XML
.gl_print_base
):
72 super(PrintCode
, self
).__init
__()
74 self
.name
= 'gl_marshal.py'
75 self
.license
= license
.bsd_license_template
% (
76 'Copyright (C) 2012 Intel Corporation', 'INTEL CORPORATION')
78 def printRealHeader(self
):
81 def printRealFooter(self
):
84 def print_sync_call(self
, func
, unmarshal
= 0):
85 call
= 'CALL_{0}(ctx->CurrentServerDispatch, ({1}))'.format(
86 func
.name
, func
.get_called_parameter_string())
87 if func
.return_type
== 'void':
88 out('{0};'.format(call
))
89 if func
.marshal_call_after
and not unmarshal
:
90 out(func
.marshal_call_after
);
92 out('return {0};'.format(call
))
93 assert not func
.marshal_call_after
95 def print_sync_dispatch(self
, func
):
96 self
.print_sync_call(func
)
98 def print_sync_body(self
, func
):
99 out('/* {0}: marshalled synchronously */'.format(func
.name
))
100 out('{0} GLAPIENTRY'.format(func
.return_type
))
101 out('_mesa_marshal_{0}({1})'.format(func
.name
, func
.get_parameter_string()))
104 out('GET_CURRENT_CONTEXT(ctx);')
105 out('_mesa_glthread_finish_before(ctx, "{0}");'.format(func
.name
))
106 self
.print_sync_call(func
)
111 def print_async_dispatch(self
, func
):
112 out('cmd = _mesa_glthread_allocate_command(ctx, '
113 'DISPATCH_CMD_{0}, cmd_size);'.format(func
.name
))
114 for p
in func
.fixed_params
:
116 out('memcpy(cmd->{0}, {0}, {1});'.format(
117 p
.name
, p
.size_string()))
119 out('cmd->{0} = {0};'.format(p
.name
))
120 if func
.variable_params
:
121 out('char *variable_data = (char *) (cmd + 1);')
123 for p
in func
.variable_params
:
125 out('cmd->{0}_null = !{0};'.format(p
.name
))
126 out('if (!cmd->{0}_null) {{'.format(p
.name
))
128 out(('memcpy(variable_data, {0}, {0}_size);').format(p
.name
))
129 if i
< len(func
.variable_params
):
130 out('variable_data += {0}_size;'.format(p
.name
))
133 out(('memcpy(variable_data, {0}, {0}_size);').format(p
.name
))
134 if i
< len(func
.variable_params
):
135 out('variable_data += {0}_size;'.format(p
.name
))
138 if not func
.fixed_params
and not func
.variable_params
:
141 if func
.marshal_call_after
:
142 out(func
.marshal_call_after
);
144 # Uncomment this if you want to call _mesa_glthread_finish for debugging
145 #out('_mesa_glthread_finish(ctx);')
147 def get_type_size(self
, str):
148 if str.find('*') != -1:
178 val
= mapping
.get(str, 9999)
180 print('Unhandled type in gl_marshal.py.get_type_size: ' + str, file=sys
.stderr
)
183 def print_async_struct(self
, func
):
184 out('struct marshal_cmd_{0}'.format(func
.name
))
187 out('struct marshal_cmd_base cmd_base;')
189 # Sort the parameters according to their size to pack the structure optimally
190 for p
in sorted(func
.fixed_params
, key
=lambda p
: self
.get_type_size(p
.type_string())):
192 out('{0} {1}[{2}];'.format(
193 p
.get_base_type_string(), p
.name
, p
.count
))
195 out('{0} {1};'.format(p
.type_string(), p
.name
))
197 for p
in func
.variable_params
:
199 out('bool {0}_null; /* If set, no data follows '
200 'for "{0}" */'.format(p
.name
))
202 for p
in func
.variable_params
:
203 if p
.count_scale
!= 1:
204 out(('/* Next {0} bytes are '
205 '{1} {2}[{3}][{4}] */').format(
206 p
.size_string(marshal
= 1), p
.get_base_type_string(),
207 p
.name
, p
.counter
, p
.count_scale
))
209 out(('/* Next {0} bytes are '
210 '{1} {2}[{3}] */').format(
211 p
.size_string(marshal
= 1), p
.get_base_type_string(),
215 def print_async_unmarshal(self
, func
):
217 out(('_mesa_unmarshal_{0}(struct gl_context *ctx, '
218 'const struct marshal_cmd_{0} *cmd)').format(func
.name
))
221 for p
in func
.fixed_params
:
223 p_decl
= '{0} * {1} = cmd->{1};'.format(
224 p
.get_base_type_string(), p
.name
)
226 p_decl
= '{0} {1} = cmd->{1};'.format(
227 p
.type_string(), p
.name
)
229 if not p_decl
.startswith('const '):
230 # Declare all local function variables as const, even if
231 # the original parameter is not const.
232 p_decl
= 'const ' + p_decl
236 if func
.variable_params
:
237 for p
in func
.variable_params
:
238 out('{0} * {1};'.format(
239 p
.get_base_type_string(), p
.name
))
240 out('const char *variable_data = (const char *) (cmd + 1);')
242 for p
in func
.variable_params
:
243 out('{0} = ({1} *) variable_data;'.format(
244 p
.name
, p
.get_base_type_string()))
247 out('if (cmd->{0}_null)'.format(p
.name
))
249 out('{0} = NULL;'.format(p
.name
))
250 if i
< len(func
.variable_params
):
253 out('variable_data += {0};'.format(p
.size_string(False, marshal
= 1)))
254 elif i
< len(func
.variable_params
):
255 out('variable_data += {0};'.format(p
.size_string(False, marshal
= 1)))
258 self
.print_sync_call(func
, unmarshal
= 1)
261 def validate_count_or_fallback(self
, func
):
262 # Check that any counts for variable-length arguments might be < 0, in
263 # which case the command alloc or the memcpy would blow up before we
264 # get to the validation in Mesa core.
266 for p
in func
.parameters
:
267 if p
.is_variable_length():
268 list.append('{0}_size < 0'.format(p
.name
))
269 list.append('({0}_size > 0 && !{0})'.format(p
.name
))
274 list.append('(unsigned)cmd_size > MARSHAL_MAX_CMD_SIZE')
276 out('if (unlikely({0})) {{'.format(' || '.join(list)))
278 out('_mesa_glthread_finish_before(ctx, "{0}");'.format(func
.name
))
279 self
.print_sync_dispatch(func
)
283 def print_async_marshal(self
, func
):
284 out('void GLAPIENTRY')
285 out('_mesa_marshal_{0}({1})'.format(
286 func
.name
, func
.get_parameter_string()))
289 out('GET_CURRENT_CONTEXT(ctx);')
290 for p
in func
.variable_params
:
291 out('int {0}_size = {1};'.format(p
.name
, p
.size_string(marshal
= 1)))
293 struct
= 'struct marshal_cmd_{0}'.format(func
.name
)
294 size_terms
= ['sizeof({0})'.format(struct
)]
295 for p
in func
.variable_params
:
297 size_terms
.append('({0} ? {0}_size : 0)'.format(p
.name
))
299 size_terms
.append('{0}_size'.format(p
.name
))
300 out('int cmd_size = {0};'.format(' + '.join(size_terms
)))
301 out('{0} *cmd;'.format(struct
))
303 self
.validate_count_or_fallback(func
)
305 if func
.marshal_sync
:
306 out('if ({0}) {{'.format(func
.marshal_sync
))
308 out('_mesa_glthread_finish_before(ctx, "{0}");'.format(func
.name
))
309 self
.print_sync_dispatch(func
)
314 self
.print_async_dispatch(func
)
317 def print_async_body(self
, func
):
318 out('/* {0}: marshalled asynchronously */'.format(func
.name
))
319 self
.print_async_struct(func
)
320 self
.print_async_unmarshal(func
)
321 self
.print_async_marshal(func
)
325 def print_unmarshal_dispatch_cmd(self
, api
):
326 out('const _mesa_unmarshal_func _mesa_unmarshal_dispatch[NUM_DISPATCH_CMD] = {')
328 for func
in api
.functionIterateAll():
329 flavor
= func
.marshal_flavor()
330 if flavor
in ('skip', 'sync'):
332 out('[DISPATCH_CMD_{0}] = (_mesa_unmarshal_func)_mesa_unmarshal_{0},'.format(func
.name
))
337 def print_create_marshal_table(self
, api
):
338 out('/* _mesa_create_marshal_table takes a long time to compile with -O2 */')
339 out('#if defined(__GNUC__) && !defined(__clang__)')
340 out('__attribute__((optimize("O1")))')
342 out('struct _glapi_table *')
343 out('_mesa_create_marshal_table(const struct gl_context *ctx)')
346 out('struct _glapi_table *table;')
348 out('table = _mesa_alloc_dispatch_table();')
349 out('if (table == NULL)')
353 for func
in api
.functionIterateAll():
354 if func
.marshal_flavor() == 'skip':
356 # Don't use the SET_* functions, because they increase compile time
357 # by 20 seconds (on Ryzen 1700X).
358 out('if (_gloffset_{0} >= 0)'.format(func
.name
))
359 out(' ((_glapi_proc *)(table))[_gloffset_{0}] = (_glapi_proc)_mesa_marshal_{0};'
367 def printBody(self
, api
):
368 # The first file only contains the dispatch tables
370 self
.print_unmarshal_dispatch_cmd(api
)
371 self
.print_create_marshal_table(api
)
374 # The remaining files contain the marshal and unmarshal functions
375 func_per_file
= (len(api
.functionIterateAll()) // (file_count
- 1)) + 1
377 for func
in api
.functionIterateAll():
379 if i
// func_per_file
!= (file_index
- 1):
382 flavor
= func
.marshal_flavor()
383 if flavor
in ('skip', 'custom'):
385 elif flavor
== 'async':
386 self
.print_async_body(func
)
387 elif flavor
== 'sync':
388 self
.print_sync_body(func
)
392 print('Usage: %s [-f input_file_name]' % sys
.argv
[0])
396 if __name__
== '__main__':
397 file_name
= 'gl_API.xml'
400 (args
, trail
) = getopt
.getopt(sys
.argv
[1:], 'm:f:i:n:')
404 for (arg
,val
) in args
:
408 file_index
= int(val
)
410 file_count
= int(val
)
412 assert file_index
< file_count
413 printer
= PrintCode()
415 api
= gl_XML
.parse_GL_API(file_name
, marshal_XML
.marshal_item_factory())