VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedOpenGL/unpacker/unpack.py@ 78086

Last change on this file since 78086 was 78086, checked in by vboxsync, 6 years ago

GuestHost/OpenGL,HostServices/SharedOpenGL: More Windows build fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.5 KB
Line 
1# Copyright (c) 2001, Stanford University
2# All rights reserved.
3#
4# See the file LICENSE.txt for information on redistributing this software.
5
6from __future__ import print_function
7import sys
8
9import apiutil
10
11
12apiutil.CopyrightC()
13
14print("""/* DO NOT EDIT! THIS CODE IS AUTOGENERATED BY unpack.py */
15
16#include "unpacker.h"
17#include "cr_opcodes.h"
18#include "cr_error.h"
19#include "cr_mem.h"
20#include "cr_spu.h"
21#include "unpack_extend.h"
22#include <stdio.h>
23#include <memory.h>
24
25#include <iprt/cdefs.h>
26
27DECLEXPORT(const unsigned char *) cr_unpackData = NULL;
28DECLEXPORT(const unsigned char *) cr_unpackDataEnd = NULL;
29SPUDispatchTable cr_unpackDispatch;
30
31static void crUnpackExtend(void);
32static void crUnpackExtendDbg(void);
33
34#if 0 //def DEBUG_misha
35//# define CR_UNPACK_DEBUG_OPCODES
36# define CR_UNPACK_DEBUG_LAST_OPCODES
37# define CR_UNPACK_DEBUG_PREV_OPCODES
38#endif
39
40#ifdef CR_UNPACK_DEBUG_PREV_OPCODES
41static GLenum g_VBoxDbgCrPrevOpcode = 0;
42static GLenum g_VBoxDbgCrPrevExtendOpcode = 0;
43#endif
44""")
45
46nodebug_opcodes = [
47 "CR_MULTITEXCOORD2FARB_OPCODE",
48 "CR_VERTEX3F_OPCODE",
49 "CR_NORMAL3F_OPCODE",
50 "CR_COLOR4UB_OPCODE",
51 "CR_LOADIDENTITY_OPCODE",
52 "CR_MATRIXMODE_OPCODE",
53 "CR_LOADMATRIXF_OPCODE",
54 "CR_DISABLE_OPCODE",
55 "CR_COLOR4F_OPCODE",
56 "CR_ENABLE_OPCODE",
57 "CR_BEGIN_OPCODE",
58 "CR_END_OPCODE",
59 "CR_SECONDARYCOLOR3FEXT_OPCODE"
60]
61
62nodebug_extopcodes = [
63 "CR_ACTIVETEXTUREARB_EXTEND_OPCODE"
64]
65
66#
67# Useful functions
68#
69
70def ReadData( offset, arg_type ):
71 """Emit a READ_DOUBLE or READ_DATA call for pulling a GL function
72 argument out of the buffer's operand area."""
73 if arg_type == "GLdouble" or arg_type == "GLclampd":
74 retval = "READ_DOUBLE(%d)" % offset
75 else:
76 retval = "READ_DATA(%d, %s)" % (offset, arg_type)
77 return retval
78
79
80def FindReturnPointer( return_type, params ):
81 """For GL functions that return values (either as the return value or
82 through a pointer parameter) emit a SET_RETURN_PTR call."""
83 arg_len = apiutil.PacketLength( params )
84 if (return_type != 'void'):
85 print('\tSET_RETURN_PTR(%d);' % (arg_len + 8)) # extended opcode plus packet length
86 else:
87 paramList = [ ('foo', 'void *', 0) ]
88 print('\tSET_RETURN_PTR(%d);' % (arg_len + 8 - apiutil.PacketLength(paramList)))
89
90
91def FindWritebackPointer( return_type, params ):
92 """Emit a SET_WRITEBACK_PTR call."""
93 arg_len = apiutil.PacketLength( params )
94 if return_type != 'void':
95 paramList = [ ('foo', 'void *', 0) ]
96 arg_len += apiutil.PacketLength( paramList )
97
98 print('\tSET_WRITEBACK_PTR(%d);' % (arg_len + 8)) # extended opcode plus packet length
99
100
101def MakeNormalCall( return_type, func_name, params, counter_init = 0 ):
102 counter = counter_init
103 copy_of_params = params[:]
104
105 for i in range( 0, len(params) ):
106 (name, type, vecSize) = params[i]
107 if apiutil.IsPointer(copy_of_params[i][1]):
108 params[i] = ('NULL', type, vecSize)
109 copy_of_params[i] = (copy_of_params[i][0], 'void', 0)
110 if not "get" in apiutil.Properties(func_name):
111 print('\tcrError( "%s needs to be special cased!" );' % func_name)
112 else:
113 print("\t%s %s = %s;" % ( copy_of_params[i][1], name, ReadData( counter, copy_of_params[i][1] ) ))
114 counter += apiutil.sizeof(copy_of_params[i][1])
115
116 if ("get" in apiutil.Properties(func_name)):
117 FindReturnPointer( return_type, params )
118 FindWritebackPointer( return_type, params )
119
120 if return_type != "void":
121 print("\t(void)", end=" ")
122 else:
123 print("\t", end="")
124 print("cr_unpackDispatch.%s(%s);" % (func_name, apiutil.MakeCallString(params)))
125
126
127def MakeVectorCall( return_type, func_name, arg_type ):
128 """Convert a call like glVertex3f to glVertex3fv."""
129 vec_func = apiutil.VectorFunction(func_name)
130 params = apiutil.Parameters(vec_func)
131 assert len(params) == 1
132 (arg_name, vecType, vecSize) = params[0]
133
134 if arg_type == "GLdouble" or arg_type == "GLclampd":
135 print("#ifdef CR_UNALIGNED_ACCESS_OKAY")
136 print("\tcr_unpackDispatch.%s((%s) cr_unpackData);" % (vec_func, vecType))
137 print("#else")
138 for index in range(0, vecSize):
139 print("\tGLdouble v" + repr(index) + " = READ_DOUBLE(" + repr(index * 8) + ");")
140 if return_type != "void":
141 print("\t(void) cr_unpackDispatch.%s(" % func_name, end="")
142 else:
143 print("\tcr_unpackDispatch.%s(" % func_name, end="")
144 for index in range(0, vecSize):
145 print("v" + repr(index), end="")
146 if index != vecSize - 1:
147 print(",", end=" ")
148 print(");")
149 print("#endif")
150 else:
151 print("\tcr_unpackDispatch.%s((%s) cr_unpackData);" % (vec_func, vecType))
152
153
154
155keys = apiutil.GetDispatchedFunctions(sys.argv[1]+"/APIspec.txt")
156
157
158#
159# Generate unpack functions for all the simple functions.
160#
161for func_name in keys:
162 if (not "pack" in apiutil.ChromiumProps(func_name) or
163 apiutil.FindSpecial( "unpacker", func_name )):
164 continue
165
166 params = apiutil.Parameters(func_name)
167 return_type = apiutil.ReturnType(func_name)
168
169 print("static void crUnpack%s(void)" % func_name)
170 print("{")
171
172 # Verify that the provided buffer length is what we expect.
173 packet_length = apiutil.PacketLength( params )
174 if packet_length > 0:
175 print("\tif(!DATA_POINTER_CHECK(%d))" % packet_length);
176 print("\t{");
177 print("\t\tcrError(\"crUnpack%s: parameters out of range\");" % func_name);
178 print("\t\treturn;");
179 print("\t}");
180
181 vector_func = apiutil.VectorFunction(func_name)
182 if (vector_func and len(apiutil.Parameters(vector_func)) == 1):
183 MakeVectorCall( return_type, func_name, params[0][1] )
184 else:
185 MakeNormalCall( return_type, func_name, params )
186 if packet_length == 0:
187 print("\tINCR_DATA_PTR_NO_ARGS( );")
188 else:
189 print("\tINCR_DATA_PTR(%d);" % packet_length)
190 print("}\n")
191
192
193#
194# Emit some code
195#
196print("""
197typedef struct __dispatchNode {
198 const unsigned char *unpackData;
199 struct __dispatchNode *next;
200} DispatchNode;
201
202static DispatchNode *unpackStack = NULL;
203
204static SPUDispatchTable *cr_lastDispatch = NULL;
205
206void crUnpackPush(void)
207{
208 DispatchNode *node = (DispatchNode*)crAlloc( sizeof( *node ) );
209 node->next = unpackStack;
210 unpackStack = node;
211 node->unpackData = cr_unpackData;
212}
213
214void crUnpackPop(void)
215{
216 DispatchNode *node = unpackStack;
217
218 if (!node)
219 {
220 crError( "crUnpackPop called with an empty stack!" );
221 }
222 unpackStack = node->next;
223 cr_unpackData = node->unpackData;
224 crFree( node );
225}
226
227CR_UNPACK_BUFFER_TYPE crUnpackGetBufferType(const void *opcodes, unsigned int num_opcodes)
228{
229 const uint8_t *pu8Codes = (const uint8_t *)opcodes;
230
231 uint8_t first;
232 uint8_t last;
233
234 if (!num_opcodes)
235 return CR_UNPACK_BUFFER_TYPE_GENERIC;
236
237 first = pu8Codes[0];
238 last = pu8Codes[1-(int)num_opcodes];
239
240 switch (last)
241 {
242 case CR_CMDBLOCKFLUSH_OPCODE:
243 return CR_UNPACK_BUFFER_TYPE_CMDBLOCK_FLUSH;
244 case CR_CMDBLOCKEND_OPCODE:
245 return (first == CR_CMDBLOCKBEGIN_OPCODE) ? CR_UNPACK_BUFFER_TYPE_GENERIC : CR_UNPACK_BUFFER_TYPE_CMDBLOCK_END;
246 default:
247 return (first != CR_CMDBLOCKBEGIN_OPCODE) ? CR_UNPACK_BUFFER_TYPE_GENERIC : CR_UNPACK_BUFFER_TYPE_CMDBLOCK_BEGIN;
248 }
249}
250
251void crUnpack( const void *data, const void *data_end, const void *opcodes,
252 unsigned int num_opcodes, SPUDispatchTable *table )
253{
254 unsigned int i;
255 const unsigned char *unpack_opcodes;
256 if (table != cr_lastDispatch)
257 {
258 crSPUCopyDispatchTable( &cr_unpackDispatch, table );
259 cr_lastDispatch = table;
260 }
261
262 unpack_opcodes = (const unsigned char *)opcodes;
263 cr_unpackData = (const unsigned char *)data;
264 cr_unpackDataEnd = (const unsigned char *)data_end;
265
266#if defined(CR_UNPACK_DEBUG_OPCODES) || defined(CR_UNPACK_DEBUG_LAST_OPCODES)
267 crDebug("crUnpack: %d opcodes", num_opcodes);
268#endif
269
270 for (i = 0; i < num_opcodes; i++)
271 {
272
273 CRDBGPTR_CHECKZ(writeback_ptr);
274 CRDBGPTR_CHECKZ(return_ptr);
275
276 /*crDebug(\"Unpacking opcode \%d\", *unpack_opcodes);*/
277#ifdef CR_UNPACK_DEBUG_PREV_OPCODES
278 g_VBoxDbgCrPrevOpcode = *unpack_opcodes;
279#endif
280 switch( *unpack_opcodes )
281 {""")
282
283#
284# Emit switch cases for all unextended opcodes
285#
286for func_name in keys:
287 if "pack" in apiutil.ChromiumProps(func_name):
288 print('\t\t\tcase %s:' % apiutil.OpcodeName( func_name ))
289 if not apiutil.OpcodeName(func_name) in nodebug_opcodes:
290 print("""
291#ifdef CR_UNPACK_DEBUG_LAST_OPCODES
292 if (i==(num_opcodes-1))
293#endif
294#if defined(CR_UNPACK_DEBUG_OPCODES) || defined(CR_UNPACK_DEBUG_LAST_OPCODES)
295 crDebug("Unpack: %s");
296#endif """ % apiutil.OpcodeName(func_name))
297 print('\t\t\t\tcrUnpack%s(); \n\t\t\t\tbreak;' % func_name)
298
299print("""
300 case CR_EXTEND_OPCODE:
301 #ifdef CR_UNPACK_DEBUG_OPCODES
302 crUnpackExtendDbg();
303 #else
304 # ifdef CR_UNPACK_DEBUG_LAST_OPCODES
305 if (i==(num_opcodes-1)) crUnpackExtendDbg();
306 else
307 # endif
308 crUnpackExtend();
309 #endif
310 break;
311 case CR_CMDBLOCKBEGIN_OPCODE:
312 case CR_CMDBLOCKEND_OPCODE:
313 case CR_CMDBLOCKFLUSH_OPCODE:
314 case CR_NOP_OPCODE:
315 INCR_DATA_PTR_NO_ARGS( );
316 break;
317 default:
318 crError( "Unknown opcode: %d", *unpack_opcodes );
319 break;
320 }
321
322 CRDBGPTR_CHECKZ(writeback_ptr);
323 CRDBGPTR_CHECKZ(return_ptr);
324
325 unpack_opcodes--;
326 }
327}""")
328
329
330#
331# Emit unpack functions for extended opcodes, non-special functions only.
332#
333for func_name in keys:
334 if ("extpack" in apiutil.ChromiumProps(func_name)
335 and not apiutil.FindSpecial("unpacker", func_name)):
336 return_type = apiutil.ReturnType(func_name)
337 params = apiutil.Parameters(func_name)
338 print('static void crUnpackExtend%s(void)' % func_name)
339 print('{')
340
341 # Verify that the provided buffer length is what we expect.
342 packet_length = apiutil.PacketLength( params )
343 if packet_length > 0:
344 print("\tif(!DATA_POINTER_CHECK(%d))" % packet_length);
345 print("\t{");
346 print("\t\tcrError(\"crUnpack%s: parameters out of range\");" % func_name);
347 print("\t\treturn;");
348 print("\t}");
349
350 MakeNormalCall( return_type, func_name, params, 8 )
351 print('}\n')
352
353print('static void crUnpackExtend(void)')
354print('{')
355print('\tGLenum extend_opcode = %s;' % ReadData( 4, 'GLenum' ))
356print('')
357print('#ifdef CR_UNPACK_DEBUG_PREV_OPCODES')
358print('\tg_VBoxDbgCrPrevExtendOpcode = extend_opcode;')
359print('#endif')
360print('')
361print('\t/*crDebug(\"Unpacking extended opcode \%d", extend_opcode);*/')
362print('\tswitch( extend_opcode )')
363print('\t{')
364
365
366#
367# Emit switch statement for extended opcodes
368#
369for func_name in keys:
370 if "extpack" in apiutil.ChromiumProps(func_name):
371 print('\t\tcase %s:' % apiutil.ExtendedOpcodeName( func_name ))
372# print('\t\t\t\tcrDebug("Unpack: %s");' % apiutil.ExtendedOpcodeName( func_name )))
373 print('\t\t\tcrUnpackExtend%s( );' % func_name)
374 print('\t\t\tbreak;')
375
376print(""" default:
377 crError( "Unknown extended opcode: %d", (int) extend_opcode );
378 break;
379 }
380 INCR_VAR_PTR();
381}""")
382
383print('static void crUnpackExtendDbg(void)')
384print('{')
385print('\tGLenum extend_opcode = %s;' % ReadData( 4, 'GLenum' ))
386print('')
387print('#ifdef CR_UNPACK_DEBUG_PREV_OPCODES')
388print('\tg_VBoxDbgCrPrevExtendOpcode = extend_opcode;')
389print('#endif')
390print('')
391print('\t/*crDebug(\"Unpacking extended opcode \%d", extend_opcode);*/')
392print('\tswitch( extend_opcode )')
393print('\t{')
394
395
396#
397# Emit switch statement for extended opcodes
398#
399for func_name in keys:
400 if "extpack" in apiutil.ChromiumProps(func_name):
401 print('\t\tcase %s:' % apiutil.ExtendedOpcodeName( func_name ))
402 if not apiutil.ExtendedOpcodeName(func_name) in nodebug_extopcodes:
403 print('\t\t\tcrDebug("Unpack: %s");' % apiutil.ExtendedOpcodeName( func_name ))
404 print('\t\t\tcrUnpackExtend%s( );' % func_name)
405 print('\t\t\tbreak;')
406
407print(""" default:
408 crError( "Unknown extended opcode: %d", (int) extend_opcode );
409 break;
410 }
411 INCR_VAR_PTR();
412}""")
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