summaryrefslogtreecommitdiffstats
path: root/src/VBox/HostServices/SharedOpenGL/unpacker/unpack.py
blob: 052fff6c8ea354f9cde8558a50eafb99781e71af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# Copyright (c) 2001, Stanford University
# All rights reserved.
#
# See the file LICENSE.txt for information on redistributing this software.

from __future__ import print_function
import sys

import apiutil


apiutil.CopyrightC()

print("""/* DO NOT EDIT!  THIS CODE IS AUTOGENERATED BY unpack.py */

#include "unpacker.h"
#include "cr_opcodes.h"
#include "cr_error.h"
#include "cr_mem.h"
#include "cr_spu.h"
#include "unpack_extend.h"
#include <stdio.h>
#include <memory.h>

#include <iprt/cdefs.h>

DECLEXPORT(const unsigned char *) cr_unpackData = NULL;
DECLEXPORT(const unsigned char *) cr_unpackDataEnd = NULL;
SPUDispatchTable cr_unpackDispatch;

static void crUnpackExtend(void);
static void crUnpackExtendDbg(void);

#if 0 //def DEBUG_misha
//# define CR_UNPACK_DEBUG_OPCODES
# define CR_UNPACK_DEBUG_LAST_OPCODES
# define CR_UNPACK_DEBUG_PREV_OPCODES
#endif

#ifdef CR_UNPACK_DEBUG_PREV_OPCODES
static GLenum g_VBoxDbgCrPrevOpcode = 0;
static GLenum g_VBoxDbgCrPrevExtendOpcode = 0;
#endif
""")

nodebug_opcodes = [
    "CR_MULTITEXCOORD2FARB_OPCODE",
    "CR_VERTEX3F_OPCODE",
    "CR_NORMAL3F_OPCODE",
    "CR_COLOR4UB_OPCODE",
    "CR_LOADIDENTITY_OPCODE",
    "CR_MATRIXMODE_OPCODE",
    "CR_LOADMATRIXF_OPCODE",
    "CR_DISABLE_OPCODE",
    "CR_COLOR4F_OPCODE",
    "CR_ENABLE_OPCODE",
    "CR_BEGIN_OPCODE",
    "CR_END_OPCODE",
    "CR_SECONDARYCOLOR3FEXT_OPCODE"
]

nodebug_extopcodes = [
    "CR_ACTIVETEXTUREARB_EXTEND_OPCODE"
]

#
# Useful functions
#

def ReadData( offset, arg_type ):
    """Emit a READ_DOUBLE or READ_DATA call for pulling a GL function
    argument out of the buffer's operand area."""
    if arg_type == "GLdouble" or arg_type == "GLclampd":
        retval = "READ_DOUBLE(%d)" % offset
    else:
        retval = "READ_DATA(%d, %s)" % (offset, arg_type)
    return retval


def FindReturnPointer( return_type, params ):
    """For GL functions that return values (either as the return value or
    through a pointer parameter) emit a SET_RETURN_PTR call."""
    arg_len = apiutil.PacketLength( params )
    if (return_type != 'void'):
        print('\tSET_RETURN_PTR(%d);' % (arg_len + 8)) # extended opcode plus packet length
    else:
        paramList = [ ('foo', 'void *', 0) ]
        print('\tSET_RETURN_PTR(%d);' % (arg_len + 8 - apiutil.PacketLength(paramList)))


def FindWritebackPointer( return_type, params ):
    """Emit a SET_WRITEBACK_PTR call."""
    arg_len = apiutil.PacketLength( params )
    if return_type != 'void':
        paramList = [ ('foo', 'void *', 0) ]
        arg_len += apiutil.PacketLength( paramList )

    print('\tSET_WRITEBACK_PTR(%d);' % (arg_len + 8)) # extended opcode plus packet length


def MakeNormalCall( return_type, func_name, params, counter_init = 0 ):
    counter = counter_init
    copy_of_params = params[:]

    for i in range( 0, len(params) ):
        (name, type, vecSize) = params[i]
        if apiutil.IsPointer(copy_of_params[i][1]):
            params[i] = ('NULL', type, vecSize)
            copy_of_params[i] = (copy_of_params[i][0], 'void', 0)
            if not "get" in apiutil.Properties(func_name):
                print('\tcrError( "%s needs to be special cased!" );' % func_name)
        else:
            print("\t%s %s = %s;" % ( copy_of_params[i][1], name, ReadData( counter, copy_of_params[i][1] ) ))
        counter += apiutil.sizeof(copy_of_params[i][1])

    if ("get" in apiutil.Properties(func_name)):
        FindReturnPointer( return_type, params )
        FindWritebackPointer( return_type, params )

    if return_type != "void":
        print("\t(void)", end=" ")
    else:
        print("\t", end="")
    print("cr_unpackDispatch.%s(%s);" % (func_name, apiutil.MakeCallString(params)))


def MakeVectorCall( return_type, func_name, arg_type ):
    """Convert a call like glVertex3f to glVertex3fv."""
    vec_func = apiutil.VectorFunction(func_name)
    params = apiutil.Parameters(vec_func)
    assert len(params) == 1
    (arg_name, vecType, vecSize) = params[0]

    if arg_type == "GLdouble" or arg_type == "GLclampd":
        print("#ifdef CR_UNALIGNED_ACCESS_OKAY")
        print("\tcr_unpackDispatch.%s((%s) cr_unpackData);" % (vec_func, vecType))
        print("#else")
        for index in range(0, vecSize):
            print("\tGLdouble v" + repr(index) + " = READ_DOUBLE(" + repr(index * 8) + ");")
        if return_type != "void":
            print("\t(void) cr_unpackDispatch.%s(" % func_name, end="")
        else:
            print("\tcr_unpackDispatch.%s(" % func_name, end="")
        for index in range(0, vecSize):
            print("v" + repr(index), end="")
            if index != vecSize - 1:
                print(",", end=" ")
        print(");")
        print("#endif")
    else:
        print("\tcr_unpackDispatch.%s((%s) cr_unpackData);" % (vec_func, vecType))



keys = apiutil.GetDispatchedFunctions(sys.argv[1]+"/APIspec.txt")


#
# Generate unpack functions for all the simple functions.
#
for func_name in keys:
    if (not "pack" in apiutil.ChromiumProps(func_name) or
        apiutil.FindSpecial( "unpacker", func_name )):
        continue

    params = apiutil.Parameters(func_name)
    return_type = apiutil.ReturnType(func_name)
    
    print("static void crUnpack%s(void)" % func_name)
    print("{")

    vector_func = apiutil.VectorFunction(func_name)
    if (vector_func and len(apiutil.Parameters(vector_func)) == 1):
        MakeVectorCall( return_type, func_name, params[0][1] )
    else:
        MakeNormalCall( return_type, func_name, params )
    packet_length = apiutil.PacketLength( params )
    if packet_length == 0:
        print("\tINCR_DATA_PTR_NO_ARGS( );")
    else:
        print("\tINCR_DATA_PTR(%d);" % packet_length)
    print("}\n")


#
# Emit some code
#
print(""" 
typedef struct __dispatchNode {
    const unsigned char *unpackData;
    struct __dispatchNode *next;
} DispatchNode;

static DispatchNode *unpackStack = NULL;

static SPUDispatchTable *cr_lastDispatch = NULL;

void crUnpackPush(void)
{
    DispatchNode *node = (DispatchNode*)crAlloc( sizeof( *node ) );
    node->next = unpackStack;
    unpackStack = node;
    node->unpackData = cr_unpackData;
}

void crUnpackPop(void)
{
    DispatchNode *node = unpackStack;

    if (!node)
    {
        crError( "crUnpackPop called with an empty stack!" );
    }
    unpackStack = node->next;
    cr_unpackData = node->unpackData;
    crFree( node );
}

CR_UNPACK_BUFFER_TYPE crUnpackGetBufferType(const void *opcodes, unsigned int num_opcodes)
{
    const uint8_t *pu8Codes = (const uint8_t *)opcodes;

    uint8_t first;
    uint8_t last;

    if (!num_opcodes)
        return CR_UNPACK_BUFFER_TYPE_GENERIC;

    first = pu8Codes[0];
    last = pu8Codes[1-(int)num_opcodes];
    
    switch (last)
    {
        case CR_CMDBLOCKFLUSH_OPCODE:
            return CR_UNPACK_BUFFER_TYPE_CMDBLOCK_FLUSH;
        case CR_CMDBLOCKEND_OPCODE:
            return (first == CR_CMDBLOCKBEGIN_OPCODE) ? CR_UNPACK_BUFFER_TYPE_GENERIC : CR_UNPACK_BUFFER_TYPE_CMDBLOCK_END;
        default:
            return (first != CR_CMDBLOCKBEGIN_OPCODE) ? CR_UNPACK_BUFFER_TYPE_GENERIC : CR_UNPACK_BUFFER_TYPE_CMDBLOCK_BEGIN;
    } 
}

void crUnpack( const void *data, const void *data_end, const void *opcodes, 
        unsigned int num_opcodes, SPUDispatchTable *table )
{
    unsigned int i;
    const unsigned char *unpack_opcodes;
    if (table != cr_lastDispatch)
    {
        crSPUCopyDispatchTable( &cr_unpackDispatch, table );
        cr_lastDispatch = table;
    }

    unpack_opcodes = (const unsigned char *)opcodes;
    cr_unpackData = (const unsigned char *)data;
    cr_unpackDataEnd = (const unsigned char *)data_end;

#if defined(CR_UNPACK_DEBUG_OPCODES) || defined(CR_UNPACK_DEBUG_LAST_OPCODES)
    crDebug("crUnpack: %d opcodes", num_opcodes);
#endif

    for (i = 0; i < num_opcodes; i++)
    {
    
        CRDBGPTR_CHECKZ(writeback_ptr);
        CRDBGPTR_CHECKZ(return_ptr);
    
        /*crDebug(\"Unpacking opcode \%d\", *unpack_opcodes);*/
#ifdef CR_UNPACK_DEBUG_PREV_OPCODES
        g_VBoxDbgCrPrevOpcode = *unpack_opcodes;
#endif
        switch( *unpack_opcodes )
        {""")

#
# Emit switch cases for all unextended opcodes
#
for func_name in keys:
    if "pack" in apiutil.ChromiumProps(func_name):
        print('\t\t\tcase %s:' % apiutil.OpcodeName( func_name ))
        if not apiutil.OpcodeName(func_name) in nodebug_opcodes:
            print("""
#ifdef CR_UNPACK_DEBUG_LAST_OPCODES
                if (i==(num_opcodes-1))
#endif
#if defined(CR_UNPACK_DEBUG_OPCODES) || defined(CR_UNPACK_DEBUG_LAST_OPCODES)
                crDebug("Unpack: %s");
#endif """ % apiutil.OpcodeName(func_name))
        print('\t\t\t\tcrUnpack%s(); \n\t\t\t\tbreak;' % func_name)

print("""       
            case CR_EXTEND_OPCODE:
                #ifdef CR_UNPACK_DEBUG_OPCODES 
                    crUnpackExtendDbg();
                #else
                # ifdef CR_UNPACK_DEBUG_LAST_OPCODES
                    if (i==(num_opcodes-1)) crUnpackExtendDbg();
                    else
                # endif
                    crUnpackExtend();
                #endif
                break;
            case CR_CMDBLOCKBEGIN_OPCODE:
            case CR_CMDBLOCKEND_OPCODE:
            case CR_CMDBLOCKFLUSH_OPCODE:
            case CR_NOP_OPCODE:
                INCR_DATA_PTR_NO_ARGS( );
                break;
            default:
                crError( "Unknown opcode: %d", *unpack_opcodes );
                break;
        }
        
        CRDBGPTR_CHECKZ(writeback_ptr);
        CRDBGPTR_CHECKZ(return_ptr);
        
        unpack_opcodes--;
    }
}""")


#
# Emit unpack functions for extended opcodes, non-special functions only.
#
for func_name in keys:
        if ("extpack" in apiutil.ChromiumProps(func_name)
            and not apiutil.FindSpecial("unpacker", func_name)):
            return_type = apiutil.ReturnType(func_name)
            params = apiutil.Parameters(func_name)
            print('static void crUnpackExtend%s(void)' % func_name)
            print('{')
            MakeNormalCall( return_type, func_name, params, 8 )
            print('}\n')

print('static void crUnpackExtend(void)')
print('{')
print('\tGLenum extend_opcode = %s;' % ReadData( 4, 'GLenum' ))
print('')
print('#ifdef CR_UNPACK_DEBUG_PREV_OPCODES')
print('\tg_VBoxDbgCrPrevExtendOpcode = extend_opcode;')
print('#endif')
print('')
print('\t/*crDebug(\"Unpacking extended opcode \%d", extend_opcode);*/')
print('\tswitch( extend_opcode )')
print('\t{')


#
# Emit switch statement for extended opcodes
#
for func_name in keys:
    if "extpack" in apiutil.ChromiumProps(func_name):
        print('\t\tcase %s:' % apiutil.ExtendedOpcodeName( func_name ))
#        print('\t\t\t\tcrDebug("Unpack: %s");' % apiutil.ExtendedOpcodeName( func_name )))
        print('\t\t\tcrUnpackExtend%s( );' % func_name)
        print('\t\t\tbreak;')

print("""       default:
            crError( "Unknown extended opcode: %d", (int) extend_opcode );
            break;
    }
    INCR_VAR_PTR();
}""")

print('static void crUnpackExtendDbg(void)')
print('{')
print('\tGLenum extend_opcode = %s;' % ReadData( 4, 'GLenum' ))
print('')
print('#ifdef CR_UNPACK_DEBUG_PREV_OPCODES')
print('\tg_VBoxDbgCrPrevExtendOpcode = extend_opcode;')
print('#endif')
print('')
print('\t/*crDebug(\"Unpacking extended opcode \%d", extend_opcode);*/')
print('\tswitch( extend_opcode )')
print('\t{')


#
# Emit switch statement for extended opcodes
#
for func_name in keys:
    if "extpack" in apiutil.ChromiumProps(func_name):
        print('\t\tcase %s:' % apiutil.ExtendedOpcodeName( func_name ))
        if not apiutil.ExtendedOpcodeName(func_name) in nodebug_extopcodes:
            print('\t\t\tcrDebug("Unpack: %s");' % apiutil.ExtendedOpcodeName( func_name ))
        print('\t\t\tcrUnpackExtend%s( );' % func_name)
        print('\t\t\tbreak;')

print("""       default:
            crError( "Unknown extended opcode: %d", (int) extend_opcode );
            break;
    }
    INCR_VAR_PTR();
}""")