16b3a6e04db75b0bd8ddf70a619f8e22e0b436ef
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"
37 #define COMPAT (ctx->API != API_OPENGL_CORE)
39 static inline int safe_mul(int a, int b)
41 if (a < 0 || b < 0) return -1;
42 if (a == 0 || b == 0) return 0;
43 if (a > INT_MAX / b) return -1;
56 print(' '*current_indent
+ str)
61 @contextlib.contextmanager
62 def indent(delta
= 3):
64 current_indent
+= delta
66 current_indent
-= delta
69 class PrintCode(gl_XML
.gl_print_base
):
71 super(PrintCode
, self
).__init
__()
73 self
.name
= 'gl_marshal.py'
74 self
.license
= license
.bsd_license_template
% (
75 'Copyright (C) 2012 Intel Corporation', 'INTEL CORPORATION')
77 def printRealHeader(self
):
80 def printRealFooter(self
):
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
);
91 out('return {0};'.format(call
))
92 assert not func
.marshal_call_after
94 def print_sync_dispatch(self
, func
):
95 self
.print_sync_call(func
)
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()))
103 out('GET_CURRENT_CONTEXT(ctx);')
104 out('_mesa_glthread_finish_before(ctx, "{0}");'.format(func
.name
))
105 self
.print_sync_call(func
)
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
:
115 out('memcpy(cmd->{0}, {0}, {1});'.format(
116 p
.name
, p
.size_string()))
118 out('cmd->{0} = {0};'.format(p
.name
))
119 if func
.variable_params
:
120 out('char *variable_data = (char *) (cmd + 1);')
122 for p
in func
.variable_params
:
124 out('cmd->{0}_null = !{0};'.format(p
.name
))
125 out('if (!cmd->{0}_null) {{'.format(p
.name
))
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
))
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
))
137 if not func
.fixed_params
and not func
.variable_params
:
140 if func
.marshal_call_after
:
141 out(func
.marshal_call_after
);
143 # Uncomment this if you want to call _mesa_glthread_finish for debugging
144 #out('_mesa_glthread_finish(ctx);')
146 def print_async_struct(self
, func
):
147 out('struct marshal_cmd_{0}'.format(func
.name
))
150 out('struct marshal_cmd_base cmd_base;')
151 for p
in func
.fixed_params
:
153 out('{0} {1}[{2}];'.format(
154 p
.get_base_type_string(), p
.name
, p
.count
))
156 type = p
.type_string()
159 out('{0} {1};'.format(type, p
.name
))
161 for p
in func
.variable_params
:
163 out('bool {0}_null; /* If set, no data follows '
164 'for "{0}" */'.format(p
.name
))
166 for p
in func
.variable_params
:
167 if p
.count_scale
!= 1:
168 out(('/* Next {0} bytes are '
169 '{1} {2}[{3}][{4}] */').format(
170 p
.size_string(marshal
= 1), p
.get_base_type_string(),
171 p
.name
, p
.counter
, p
.count_scale
))
173 out(('/* Next {0} bytes are '
174 '{1} {2}[{3}] */').format(
175 p
.size_string(marshal
= 1), p
.get_base_type_string(),
179 def print_async_unmarshal(self
, func
):
181 out(('_mesa_unmarshal_{0}(struct gl_context *ctx, '
182 'const struct marshal_cmd_{0} *cmd)').format(func
.name
))
185 for p
in func
.fixed_params
:
187 p_decl
= '{0} * {1} = cmd->{1};'.format(
188 p
.get_base_type_string(), p
.name
)
190 p_decl
= '{0} {1} = cmd->{1};'.format(
191 p
.type_string(), p
.name
)
193 if not p_decl
.startswith('const '):
194 # Declare all local function variables as const, even if
195 # the original parameter is not const.
196 p_decl
= 'const ' + p_decl
200 if func
.variable_params
:
201 for p
in func
.variable_params
:
202 out('{0} * {1};'.format(
203 p
.get_base_type_string(), p
.name
))
204 out('const char *variable_data = (const char *) (cmd + 1);')
206 for p
in func
.variable_params
:
207 out('{0} = ({1} *) variable_data;'.format(
208 p
.name
, p
.get_base_type_string()))
211 out('if (cmd->{0}_null)'.format(p
.name
))
213 out('{0} = NULL;'.format(p
.name
))
214 if i
< len(func
.variable_params
):
217 out('variable_data += {0};'.format(p
.size_string(False, marshal
= 1)))
218 elif i
< len(func
.variable_params
):
219 out('variable_data += {0};'.format(p
.size_string(False, marshal
= 1)))
222 self
.print_sync_call(func
, unmarshal
= 1)
225 def validate_count_or_fallback(self
, func
):
226 # Check that any counts for variable-length arguments might be < 0, in
227 # which case the command alloc or the memcpy would blow up before we
228 # get to the validation in Mesa core.
230 for p
in func
.parameters
:
231 if p
.is_variable_length():
232 list.append('{0}_size < 0'.format(p
.name
))
233 list.append('({0}_size > 0 && !{0})'.format(p
.name
))
238 list.append('(unsigned)cmd_size > MARSHAL_MAX_CMD_SIZE')
240 out('if (unlikely({0})) {{'.format(' || '.join(list)))
242 out('_mesa_glthread_finish_before(ctx, "{0}");'.format(func
.name
))
243 self
.print_sync_dispatch(func
)
247 def print_async_marshal(self
, func
):
248 out('void GLAPIENTRY')
249 out('_mesa_marshal_{0}({1})'.format(
250 func
.name
, func
.get_parameter_string()))
253 out('GET_CURRENT_CONTEXT(ctx);')
254 for p
in func
.variable_params
:
255 out('int {0}_size = {1};'.format(p
.name
, p
.size_string(marshal
= 1)))
257 struct
= 'struct marshal_cmd_{0}'.format(func
.name
)
258 size_terms
= ['sizeof({0})'.format(struct
)]
259 for p
in func
.variable_params
:
261 size_terms
.append('({0} ? {0}_size : 0)'.format(p
.name
))
263 size_terms
.append('{0}_size'.format(p
.name
))
264 out('int cmd_size = {0};'.format(' + '.join(size_terms
)))
265 out('{0} *cmd;'.format(struct
))
267 self
.validate_count_or_fallback(func
)
269 if func
.marshal_sync
:
270 out('if ({0}) {{'.format(func
.marshal_sync
))
272 out('_mesa_glthread_finish_before(ctx, "{0}");'.format(func
.name
))
273 self
.print_sync_dispatch(func
)
278 self
.print_async_dispatch(func
)
281 def print_async_body(self
, func
):
282 out('/* {0}: marshalled asynchronously */'.format(func
.name
))
283 self
.print_async_struct(func
)
284 self
.print_async_unmarshal(func
)
285 self
.print_async_marshal(func
)
289 def print_unmarshal_dispatch_cmd(self
, api
):
290 out('const _mesa_unmarshal_func _mesa_unmarshal_dispatch[NUM_DISPATCH_CMD] = {')
292 for func
in api
.functionIterateAll():
293 flavor
= func
.marshal_flavor()
294 if flavor
in ('skip', 'sync'):
296 out('[DISPATCH_CMD_{0}] = (_mesa_unmarshal_func)_mesa_unmarshal_{0},'.format(func
.name
))
301 def print_create_marshal_table(self
, api
):
302 out('/* _mesa_create_marshal_table takes a long time to compile with -O2 */')
303 out('#ifdef __GNUC__')
304 out('__attribute__((optimize("O1")))')
306 out('struct _glapi_table *')
307 out('_mesa_create_marshal_table(const struct gl_context *ctx)')
310 out('struct _glapi_table *table;')
312 out('table = _mesa_alloc_dispatch_table();')
313 out('if (table == NULL)')
317 for func
in api
.functionIterateAll():
318 if func
.marshal_flavor() == 'skip':
320 # Don't use the SET_* functions, because they increase compile time
321 # by 20 seconds (on Ryzen 1700X).
322 out('if (_gloffset_{0} >= 0)'.format(func
.name
))
323 out(' ((_glapi_proc *)(table))[_gloffset_{0}] = (_glapi_proc)_mesa_marshal_{0};'
331 def printBody(self
, api
):
332 # The first file only contains the dispatch tables
334 self
.print_unmarshal_dispatch_cmd(api
)
335 self
.print_create_marshal_table(api
)
338 # The remaining files contain the marshal and unmarshal functions
339 func_per_file
= (len(api
.functionIterateAll()) // (file_count
- 1)) + 1
341 for func
in api
.functionIterateAll():
343 if i
// func_per_file
!= (file_index
- 1):
346 flavor
= func
.marshal_flavor()
347 if flavor
in ('skip', 'custom'):
349 elif flavor
== 'async':
350 self
.print_async_body(func
)
351 elif flavor
== 'sync':
352 self
.print_sync_body(func
)
356 print('Usage: %s [-f input_file_name]' % sys
.argv
[0])
360 if __name__
== '__main__':
361 file_name
= 'gl_API.xml'
364 (args
, trail
) = getopt
.getopt(sys
.argv
[1:], 'm:f:i:n:')
368 for (arg
,val
) in args
:
372 file_index
= int(val
)
374 file_count
= int(val
)
376 assert file_index
< file_count
377 printer
= PrintCode()
379 api
= gl_XML
.parse_GL_API(file_name
, marshal_XML
.marshal_item_factory())