VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedOpenGL/dlm/dlm_generated.py@ 54905

Last change on this file since 54905 was 54905, checked in by vboxsync, 10 years ago

Host 3D: Chromium server: add Expando SPU and DLM module in order to record and save OpenGL Display Lists (currently disabled).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.7 KB
Line 
1# $Id: dlm_generated.py 54905 2015-03-23 11:20:58Z vboxsync $
2import sys, cPickle, re
3
4sys.path.append( "../glapi_parser" )
5import apiutil
6
7# A routine that can create call strings from instance names
8def InstanceCallString( params ):
9 output = ''
10 for index in range(0,len(params)):
11 if index > 0:
12 output += ", "
13 if params[index][0] != '':
14 output += 'instance->' + params[index][0]
15 return output
16
17def GetPointerType(basetype):
18 words = basetype.split()
19 if words[0] == 'const':
20 words = words[1:]
21 if words[-1].endswith('*'):
22 words[-1] = words[-1][:-1].strip()
23 if words[-1] == '':
24 words = words[:-1]
25 if words[0] == 'void' or words[0] == 'GLvoid':
26 words[0] = 'int'
27 return ' '.join(words)
28
29
30def GetPointerInfo(functionName):
31 # We'll keep track of all the parameters that require pointers.
32 # They'll require special handling later.
33 params = apiutil.Parameters(functionName)
34 pointers = []
35 pointername=''
36 pointerarg=''
37 pointertype=''
38 pointersize=0
39 pointercomment=''
40
41 index = 0
42 for (name, type, vecSize) in params:
43 # Watch out for the word "const" (which should be ignored)
44 # and for types that end in "*" (which are pointers and need
45 # special treatment)
46 words = type.split()
47 if words[-1].endswith('*'):
48 pointers.append(index)
49 index += 1
50
51 # If any argument was a pointer, we need a special pointer data
52 # array. The pointer data will be stored into this array, and
53 # references to the array will be generated as parameters.
54 if len(pointers) == 1:
55 index = pointers[0]
56 pointername = params[index][0]
57 pointerarg = pointername + 'Data'
58 pointertype = GetPointerType(params[index][1])
59 pointersize = params[index][2]
60 if pointersize == 0:
61 pointersize = "special"
62 elif len(pointers) > 1:
63 pointerarg = 'data';
64 pointertype = GetPointerType(params[pointers[0]][1])
65 for index in range(1,len(pointers)):
66 if GetPointerType(params[pointers[index]][1]) != pointertype:
67 pointertype = 'GLvoid *'
68
69 return (pointers,pointername,pointerarg,pointertype,pointersize,pointercomment)
70
71def wrap_struct(functionName):
72 params = apiutil.Parameters(functionName)
73 argstring = apiutil.MakeDeclarationString(params)
74 extendedArgstring = argstring
75 props = apiutil.Properties(functionName)
76 if "useclient" in props or "pixelstore" in props:
77 extendedArgstring += ", CRClientState *c"
78
79 # We'll keep track of all the parameters that require pointers.
80 # They'll require special handling later.
81 (pointers, pointername, pointerarg, pointertype, pointersize, pointercomment) = GetPointerInfo(functionName)
82
83 # Start writing the header
84 print 'struct instance%s {' % (functionName)
85 print ' DLMInstanceList *next;'
86 print ' DLMInstanceList *stateNext;'
87 print ' void (DLM_APIENTRY *execute)(DLMInstanceList *instance, SPUDispatchTable *dispatchTable);'
88 for (name, type, vecSize) in params:
89 # Watch out for the word "const" (which should be ignored)
90 # and for types that end in "*" (which are pointers and need
91 # special treatment)
92 words = type.split()
93 if words[0] == 'const':
94 words = words[1:]
95 if words[0] != "void":
96 print ' %s %s;' % (' '.join(words), name)
97
98 # If any argument was a pointer, we need a special pointer data
99 # array. The pointer data will be stored into this array, and
100 # references to the array will be generated as parameters.
101 if len(pointers) == 1:
102 if pointersize == None:
103 print " /* Oh no - pointer parameter %s found, but no pointer class specified and can't guess */" % pointername
104 else:
105 if pointersize == 'special':
106 print ' %s %s[1];%s' % (pointertype, pointerarg, pointercomment)
107 else:
108 print ' %s %s[%s];%s' % (pointertype, pointerarg, pointersize,pointercomment)
109 elif len(pointers) > 1:
110 print ' %s %s[1];%s' % (pointertype, pointerarg,pointercomment)
111
112 print '};'
113
114 # Pointers only happen with instances
115 if len(pointers) > 1 or (len(pointers) == 1 and pointersize == 'special'):
116 print 'int crdlm_pointers_%s(struct instance%s *instance, %s);' % (functionName, functionName, extendedArgstring)
117
118 # See if the GL function must sometimes allow passthrough even
119 # if the display list is open
120 if "checklist" in apiutil.ChromiumProps(functionName):
121 print 'int crdlm_checklist_%s(%s);' % (functionName, argstring)
122
123 return
124
125def wrap_execute(functionName):
126 params = apiutil.Parameters(functionName)
127 print 'static void execute%s(DLMInstanceList *x, SPUDispatchTable *dispatchTable)' % functionName
128 print '{'
129 if len(params) > 0:
130 print '\tstruct instance%s *instance = (struct instance%s *)x;' % (functionName, functionName)
131 print '\tif (dispatchTable->%s != NULL) {' % (functionName)
132 print '\t\tdispatchTable->%s(%s);' % (functionName, InstanceCallString(params))
133 print '\t}'
134 print '\telse {'
135 print '\t\tcrWarning("DLM warning: execute%s called with NULL dispatch entry");' % (functionName)
136 print '\t}'
137 print '}'
138
139def generate_bbox_code(functionName):
140 assert functionName[0:6] == "Vertex"
141 pattern = "(VertexAttribs|VertexAttrib|Vertex)(1|2|3|4)(N?)(f|d|i|s|b|ub|us|ui)(v?)"
142 m = re.match(pattern, functionName)
143 if m:
144 name = m.group(1)
145 size = int(m.group(2))
146 normalize = m.group(3)
147 type = m.group(4)
148 vector = m.group(5)
149
150 # only update bbox for vertex attribs if index == 0
151 if name == "VertexAttrib":
152 test = "if (index == 0) {"
153 elif name == "VertexAttribs":
154 test = "if (index == 0) {"
155 else:
156 assert name == "Vertex"
157 test = "{"
158
159 # find names of the X, Y, Z, W values
160 xName = ""
161 yName = ""
162 zName = "0.0"
163 wName = ""
164 if vector == "v":
165 xName = "v[0]"
166 if size > 1:
167 yName = "v[1]"
168 if size > 2:
169 zName = "v[2]"
170 if size > 3:
171 wName = "v[3]"
172 else:
173 xName = "x"
174 if size > 1:
175 yName = "y"
176 if size > 2:
177 zName = "z"
178 if size > 3:
179 wName = "w"
180
181 # start emitting code
182 print '\t%s' % test
183
184 if normalize == "N":
185 if type == "b":
186 denom = "128.0f"
187 elif type == "s":
188 denom = "32768.0f"
189 elif type == "i":
190 denom = "2147483647.0f"
191 elif type == "ub":
192 denom = "255.0f"
193 elif type == "us":
194 denom = "65535.0f"
195 elif type == "ui":
196 denom = "4294967295.0f"
197
198 print '\t\tGLfloat nx = (GLfloat) %s / %s;' % (xName, denom)
199 xName = "nx"
200 if yName:
201 print '\t\tGLfloat ny = (GLfloat) %s / %s;' % (yName, denom)
202 yName = "ny"
203 if zName:
204 print '\t\tGLfloat nz = (GLfloat) %s / %s;' % (zName, denom)
205 zName = "nz"
206 if 0 and wName:
207 print '\t\tGLfloat nw = (GLfloat) %s / %s;' % (wName, denom)
208 wName = "nw"
209
210 if xName:
211 print '\t\tif (%s < state->currentListInfo->bbox.xmin)' % xName
212 print '\t\t\tstate->currentListInfo->bbox.xmin = %s;' % xName
213 print '\t\tif (%s > state->currentListInfo->bbox.xmax)' % xName
214 print '\t\t\tstate->currentListInfo->bbox.xmax = %s;' % xName
215 if yName:
216 print '\t\tif (%s < state->currentListInfo->bbox.ymin)' % yName
217 print '\t\t\tstate->currentListInfo->bbox.ymin = %s;' % yName
218 print '\t\tif (%s > state->currentListInfo->bbox.ymax)' % yName
219 print '\t\t\tstate->currentListInfo->bbox.ymax = %s;' % yName
220 if zName:
221 print '\t\tif (%s < state->currentListInfo->bbox.zmin)' % zName
222 print '\t\t\tstate->currentListInfo->bbox.zmin = %s;' % zName
223 print '\t\tif (%s > state->currentListInfo->bbox.zmax)' % zName
224 print '\t\t\tstate->currentListInfo->bbox.zmax = %s;' % zName
225 # XXX what about divide by W if we have 4 components?
226 print '\t}'
227
228 else:
229 print ' /* bbox error for %s !!!!! */' % functionName
230
231# These code snippets isolate the code required to add a given instance
232# to the display list correctly. They are used during generation, to
233# generate correct code, and also to create useful utilities.
234def AddInstanceToList(pad):
235 print '%s/* Add this instance to the current display list. */' % pad
236 print '%sinstance->next = NULL;' % pad
237 print '%sinstance->stateNext = NULL;' % pad
238 print '%sif (!state->currentListInfo->first) {' % pad
239 print '%s\tstate->currentListInfo->first = (DLMInstanceList *)instance;' % pad
240 print '%s}' % pad
241 print '%selse {' % pad
242 print '%s\tstate->currentListInfo->last->next = (DLMInstanceList *)instance;' % pad
243 print '%s}' % pad
244 print '%sstate->currentListInfo->last = (DLMInstanceList *)instance;' % pad
245 print '%sstate->currentListInfo->numInstances++;' % pad
246
247def AddInstanceToStateList(pad):
248 print '%s/* Instances that change state have to be added to the state list as well. */' % pad
249 print '%sif (!state->currentListInfo->stateFirst) {' % pad
250 print '%s\tstate->currentListInfo->stateFirst = (DLMInstanceList *)instance;' % pad
251 print '%s}' % pad
252 print '%selse {' % pad
253 print '%s\tstate->currentListInfo->stateLast->stateNext = (DLMInstanceList *)instance;' % pad
254 print '%s}' % pad
255 print '%sstate->currentListInfo->stateLast = (DLMInstanceList *)instance;' % pad
256
257
258# The compile wrapper collects the parameters into a DLMInstanceList
259# element, and adds that element to the end of the display list currently
260# being compiled.
261def wrap_compile(functionName):
262 params = apiutil.Parameters(functionName)
263 return_type = apiutil.ReturnType(functionName)
264 # Make sure the return type is void. It's nonsensical to compile
265 # an element with any other return type.
266 if return_type != 'void':
267 print '/* Nonsense: DL function %s has a %s return type?!? */' % (functionName, return_type)
268 # return
269 # Define a structure to hold all the parameters. Note that the
270 # top parameters must exactly match the DLMInstanceList structure
271 # in include/cr_dlm.h, or everything will break horribly.
272 # Start off by getting all the pointer info we could ever use
273 # from the parameters
274 (pointers, pointername, pointerarg, pointertype, pointersize, pointercomment) = GetPointerInfo(functionName)
275
276 # Finally, the compile wrapper. This one will diverge strongly
277 # depending on whether or not there are pointer parameters.
278 callstring = apiutil.MakeCallString(params)
279 argstring = apiutil.MakeDeclarationString(params)
280 props = apiutil.Properties(functionName)
281 if "useclient" in props or "pixelstore" in props:
282 callstring += ", c"
283 argstring += ", CRClientState *c"
284 print 'void DLM_APIENTRY crDLMCompile%s( %s )' % (functionName, argstring)
285 print '{'
286 print ' CRDLMContextState *state = CURRENT_STATE();'
287 print ' struct instance%s *instance;' % (functionName)
288
289 # The calling SPU is supposed to verify that the element is supposed to be
290 # compiled before it is actually compiled; typically, this is done based
291 # on whether a glNewList has been executed more recently than a glEndList.
292 # But some functions are dual-natured, sometimes being compiled, and sometimes
293 # being executed immediately. We can check for this here.
294 if "checklist" in apiutil.ChromiumProps(functionName):
295 print '\tif (crDLMCheckList%s(%s)) {' % (functionName, apiutil.MakeCallString(params))
296 print '\t\tcrdlm_error(__LINE__, __FILE__, GL_INVALID_OPERATION,'
297 print '\t\t "this instance of function %s should not be compiled");' % functionName;
298 print '\t\treturn;'
299 print '\t}'
300
301 if len(pointers) > 1 or pointersize == 'special':
302 # Pass NULL, to just allocate space
303 print '\tinstance = crCalloc(sizeof(struct instance%s) + crdlm_pointers_%s(NULL, %s));' % (functionName, functionName, callstring)
304 else:
305 print '\tinstance = crCalloc(sizeof(struct instance%s));' % (functionName)
306 print '\tif (!instance) {'
307 print '\t\tcrdlm_error(__LINE__, __FILE__, GL_OUT_OF_MEMORY,'
308 print '\t\t\t"out of memory adding %s to display list");' % (functionName)
309 print '\t\treturn;'
310 print '\t}'
311
312 # Put in the fields that must always exist
313 print '\tinstance->execute = execute%s;' % functionName
314
315 # Apply all the simple (i.e. non-pointer) parameters
316 for index in range(len(params)):
317 if index not in pointers:
318 name = params[index][0]
319 print '\tinstance->%s = %s;' % (name, name)
320
321 # If there's a pointer parameter, apply it.
322 if len(pointers) == 1:
323 print '\tif (%s == NULL) {' % (params[pointers[0]][0])
324 print '\t\tinstance->%s = NULL;' % (params[pointers[0]][0])
325 print '\t}'
326 print '\telse {'
327 print '\t\tinstance->%s = instance->%s;' % (params[pointers[0]][0], pointerarg)
328 print '\t}'
329 if pointersize == 'special':
330 print '\t(void) crdlm_pointers_%s(instance, %s);' % (functionName, callstring)
331 else:
332 print '\tcrMemcpy((void *)instance->%s, (void *) %s, %s*sizeof(%s));' % (params[pointers[0]][0], params[pointers[0]][0], pointersize, pointertype)
333 elif len(pointers) == 2:
334 # this seems to work
335 print '\t(void) crdlm_pointers_%s(instance, %s);' % (functionName, callstring)
336 elif len(pointers) > 2:
337 print "#error don't know how to handle pointer parameters for %s" % (functionName)
338
339 # Add the element to the current display list
340 AddInstanceToList('\t')
341 # If the element is a state-changing element, add it to the current state list
342 if apiutil.SetsTrackedState(functionName):
343 AddInstanceToStateList('\t')
344
345 # XXX might need a better test here
346 if functionName[0:6] == "Vertex":
347 generate_bbox_code(functionName)
348
349 print '}'
350
351whichfile=sys.argv[1]
352if whichfile == 'headers':
353 print """#ifndef _DLM_GENERATED_H
354#define _DLM_GENERATED_H
355
356/* DO NOT EDIT. This file is auto-generated by dlm_generated.py. */
357"""
358else:
359 print """#include <stdio.h>
360#include "cr_spu.h"
361#include "cr_dlm.h"
362#include "cr_mem.h"
363#include "cr_error.h"
364#include "state/cr_statefuncs.h"
365#include "dlm.h"
366#include "dlm_pointers.h"
367#include "dlm_generated.h"
368
369/* DO NOT EDIT. This file is auto-generated by dlm_generated.py. */
370"""
371
372# Add in the "add_to_dl" utility function, which will be used by
373# external (i.e. non-generated) functions. The utility ensures that
374# any external functions that are written for compiling elements
375# don't have to be rewritten if the conventions for adding to display
376# lists are changed.
377print """
378void crdlm_add_to_list(
379 DLMInstanceList *instance,
380 void (*executeFunc)(DLMInstanceList *x, SPUDispatchTable *dispatchTable)"""
381
382if (whichfile == 'headers'):
383 print ");"
384else:
385 print """) {
386 CRDLMContextState *state = CURRENT_STATE();
387 instance->execute = executeFunc;"""
388
389 # Add in the common code for adding the instance to the display list
390 AddInstanceToList(" ")
391
392 print '}'
393 print ''
394
395# Now generate the functions that won't use the crdlm_add_to_list utility.
396# These all directly add their own instances to the current display list
397# themselves, without using the crdlm_add_to_list() function.
398keys = apiutil.GetDispatchedFunctions(sys.argv[3]+"/APIspec.txt")
399for func_name in keys:
400 if apiutil.CanCompile(func_name):
401 print "\n/*** %s ***/" % func_name
402 # Auto-generate an appropriate DL function. First, functions
403 # that go into the display list but that rely on state will
404 # have to have their argument strings expanded, to take pointers
405 # to that appropriate state.
406 if whichfile == "headers":
407 wrap_struct(func_name)
408 elif not apiutil.FindSpecial("dlm", func_name):
409 wrap_execute(func_name)
410 wrap_compile(func_name)
411 # All others just pass through
412
413if whichfile == 'headers':
414 print "#endif /* _DLM_GENERATED_H */"
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette