VirtualBox

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

Last change on this file since 74942 was 71487, checked in by vboxsync, 7 years ago

HostServices/SharedOpenGL: fixed untested code from r121437

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 11.7 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 vector_func = apiutil.VectorFunction(func_name)
173 if (vector_func and len(apiutil.Parameters(vector_func)) == 1):
174 MakeVectorCall( return_type, func_name, params[0][1] )
175 else:
176 MakeNormalCall( return_type, func_name, params )
177 packet_length = apiutil.PacketLength( params )
178 if packet_length == 0:
179 print("\tINCR_DATA_PTR_NO_ARGS( );")
180 else:
181 print("\tINCR_DATA_PTR(%d);" % packet_length)
182 print("}\n")
183
184
185#
186# Emit some code
187#
188print("""
189typedef struct __dispatchNode {
190 const unsigned char *unpackData;
191 struct __dispatchNode *next;
192} DispatchNode;
193
194static DispatchNode *unpackStack = NULL;
195
196static SPUDispatchTable *cr_lastDispatch = NULL;
197
198void crUnpackPush(void)
199{
200 DispatchNode *node = (DispatchNode*)crAlloc( sizeof( *node ) );
201 node->next = unpackStack;
202 unpackStack = node;
203 node->unpackData = cr_unpackData;
204}
205
206void crUnpackPop(void)
207{
208 DispatchNode *node = unpackStack;
209
210 if (!node)
211 {
212 crError( "crUnpackPop called with an empty stack!" );
213 }
214 unpackStack = node->next;
215 cr_unpackData = node->unpackData;
216 crFree( node );
217}
218
219CR_UNPACK_BUFFER_TYPE crUnpackGetBufferType(const void *opcodes, unsigned int num_opcodes)
220{
221 const uint8_t *pu8Codes = (const uint8_t *)opcodes;
222
223 uint8_t first;
224 uint8_t last;
225
226 if (!num_opcodes)
227 return CR_UNPACK_BUFFER_TYPE_GENERIC;
228
229 first = pu8Codes[0];
230 last = pu8Codes[1-(int)num_opcodes];
231
232 switch (last)
233 {
234 case CR_CMDBLOCKFLUSH_OPCODE:
235 return CR_UNPACK_BUFFER_TYPE_CMDBLOCK_FLUSH;
236 case CR_CMDBLOCKEND_OPCODE:
237 return (first == CR_CMDBLOCKBEGIN_OPCODE) ? CR_UNPACK_BUFFER_TYPE_GENERIC : CR_UNPACK_BUFFER_TYPE_CMDBLOCK_END;
238 default:
239 return (first != CR_CMDBLOCKBEGIN_OPCODE) ? CR_UNPACK_BUFFER_TYPE_GENERIC : CR_UNPACK_BUFFER_TYPE_CMDBLOCK_BEGIN;
240 }
241}
242
243void crUnpack( const void *data, const void *data_end, const void *opcodes,
244 unsigned int num_opcodes, SPUDispatchTable *table )
245{
246 unsigned int i;
247 const unsigned char *unpack_opcodes;
248 if (table != cr_lastDispatch)
249 {
250 crSPUCopyDispatchTable( &cr_unpackDispatch, table );
251 cr_lastDispatch = table;
252 }
253
254 unpack_opcodes = (const unsigned char *)opcodes;
255 cr_unpackData = (const unsigned char *)data;
256 cr_unpackDataEnd = (const unsigned char *)data_end;
257
258#if defined(CR_UNPACK_DEBUG_OPCODES) || defined(CR_UNPACK_DEBUG_LAST_OPCODES)
259 crDebug("crUnpack: %d opcodes", num_opcodes);
260#endif
261
262 for (i = 0; i < num_opcodes; i++)
263 {
264
265 CRDBGPTR_CHECKZ(writeback_ptr);
266 CRDBGPTR_CHECKZ(return_ptr);
267
268 /*crDebug(\"Unpacking opcode \%d\", *unpack_opcodes);*/
269#ifdef CR_UNPACK_DEBUG_PREV_OPCODES
270 g_VBoxDbgCrPrevOpcode = *unpack_opcodes;
271#endif
272 switch( *unpack_opcodes )
273 {""")
274
275#
276# Emit switch cases for all unextended opcodes
277#
278for func_name in keys:
279 if "pack" in apiutil.ChromiumProps(func_name):
280 print('\t\t\tcase %s:' % apiutil.OpcodeName( func_name ))
281 if not apiutil.OpcodeName(func_name) in nodebug_opcodes:
282 print("""
283#ifdef CR_UNPACK_DEBUG_LAST_OPCODES
284 if (i==(num_opcodes-1))
285#endif
286#if defined(CR_UNPACK_DEBUG_OPCODES) || defined(CR_UNPACK_DEBUG_LAST_OPCODES)
287 crDebug("Unpack: %s");
288#endif """ % apiutil.OpcodeName(func_name))
289 print('\t\t\t\tcrUnpack%s(); \n\t\t\t\tbreak;' % func_name)
290
291print("""
292 case CR_EXTEND_OPCODE:
293 #ifdef CR_UNPACK_DEBUG_OPCODES
294 crUnpackExtendDbg();
295 #else
296 # ifdef CR_UNPACK_DEBUG_LAST_OPCODES
297 if (i==(num_opcodes-1)) crUnpackExtendDbg();
298 else
299 # endif
300 crUnpackExtend();
301 #endif
302 break;
303 case CR_CMDBLOCKBEGIN_OPCODE:
304 case CR_CMDBLOCKEND_OPCODE:
305 case CR_CMDBLOCKFLUSH_OPCODE:
306 case CR_NOP_OPCODE:
307 INCR_DATA_PTR_NO_ARGS( );
308 break;
309 default:
310 crError( "Unknown opcode: %d", *unpack_opcodes );
311 break;
312 }
313
314 CRDBGPTR_CHECKZ(writeback_ptr);
315 CRDBGPTR_CHECKZ(return_ptr);
316
317 unpack_opcodes--;
318 }
319}""")
320
321
322#
323# Emit unpack functions for extended opcodes, non-special functions only.
324#
325for func_name in keys:
326 if ("extpack" in apiutil.ChromiumProps(func_name)
327 and not apiutil.FindSpecial("unpacker", func_name)):
328 return_type = apiutil.ReturnType(func_name)
329 params = apiutil.Parameters(func_name)
330 print('static void crUnpackExtend%s(void)' % func_name)
331 print('{')
332 MakeNormalCall( return_type, func_name, params, 8 )
333 print('}\n')
334
335print('static void crUnpackExtend(void)')
336print('{')
337print('\tGLenum extend_opcode = %s;' % ReadData( 4, 'GLenum' ))
338print('')
339print('#ifdef CR_UNPACK_DEBUG_PREV_OPCODES')
340print('\tg_VBoxDbgCrPrevExtendOpcode = extend_opcode;')
341print('#endif')
342print('')
343print('\t/*crDebug(\"Unpacking extended opcode \%d", extend_opcode);*/')
344print('\tswitch( extend_opcode )')
345print('\t{')
346
347
348#
349# Emit switch statement for extended opcodes
350#
351for func_name in keys:
352 if "extpack" in apiutil.ChromiumProps(func_name):
353 print('\t\tcase %s:' % apiutil.ExtendedOpcodeName( func_name ))
354# print('\t\t\t\tcrDebug("Unpack: %s");' % apiutil.ExtendedOpcodeName( func_name )))
355 print('\t\t\tcrUnpackExtend%s( );' % func_name)
356 print('\t\t\tbreak;')
357
358print(""" default:
359 crError( "Unknown extended opcode: %d", (int) extend_opcode );
360 break;
361 }
362 INCR_VAR_PTR();
363}""")
364
365print('static void crUnpackExtendDbg(void)')
366print('{')
367print('\tGLenum extend_opcode = %s;' % ReadData( 4, 'GLenum' ))
368print('')
369print('#ifdef CR_UNPACK_DEBUG_PREV_OPCODES')
370print('\tg_VBoxDbgCrPrevExtendOpcode = extend_opcode;')
371print('#endif')
372print('')
373print('\t/*crDebug(\"Unpacking extended opcode \%d", extend_opcode);*/')
374print('\tswitch( extend_opcode )')
375print('\t{')
376
377
378#
379# Emit switch statement for extended opcodes
380#
381for func_name in keys:
382 if "extpack" in apiutil.ChromiumProps(func_name):
383 print('\t\tcase %s:' % apiutil.ExtendedOpcodeName( func_name ))
384 if not apiutil.ExtendedOpcodeName(func_name) in nodebug_extopcodes:
385 print('\t\t\tcrDebug("Unpack: %s");' % apiutil.ExtendedOpcodeName( func_name ))
386 print('\t\t\tcrUnpackExtend%s( );' % func_name)
387 print('\t\t\tbreak;')
388
389print(""" default:
390 crError( "Unknown extended opcode: %d", (int) extend_opcode );
391 break;
392 }
393 INCR_VAR_PTR();
394}""")
Note: See TracBrowser for help on using the repository browser.

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