• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'''
2Generates dumper for the SVGA 3D command stream using pygccxml.
3
4Jose Fonseca <jfonseca@vmware.com>
5'''
6
7copyright = '''
8/**********************************************************
9 * Copyright 2009 VMware, Inc.  All rights reserved.
10 *
11 * Permission is hereby granted, free of charge, to any person
12 * obtaining a copy of this software and associated documentation
13 * files (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy,
15 * modify, merge, publish, distribute, sublicense, and/or sell copies
16 * of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be
20 * included in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * SOFTWARE.
30 *
31 **********************************************************/
32 '''
33
34import os
35import sys
36
37from pygccxml import parser
38from pygccxml import declarations
39
40from pygccxml.declarations import algorithm
41from pygccxml.declarations import decl_visitor
42from pygccxml.declarations import type_traits
43from pygccxml.declarations import type_visitor
44
45
46enums = True
47
48
49class decl_dumper_t(decl_visitor.decl_visitor_t):
50
51    def __init__(self, instance = '', decl = None):
52        decl_visitor.decl_visitor_t.__init__(self)
53        self._instance = instance
54        self.decl = decl
55
56    def clone(self):
57        return decl_dumper_t(self._instance, self.decl)
58
59    def visit_class(self):
60        class_ = self.decl
61        assert self.decl.class_type in ('struct', 'union')
62
63        for variable in class_.variables():
64            if variable.name != '':
65                #print 'variable = %r' % variable.name
66                dump_type(self._instance + '.' + variable.name, variable.type)
67
68    def visit_enumeration(self):
69        if enums:
70            print '   switch(%s) {' % ("(*cmd)" + self._instance,)
71            for name, value in self.decl.values:
72                print '   case %s:' % (name,)
73                print '      _debug_printf("\\t\\t%s = %s\\n");' % (self._instance, name)
74                print '      break;'
75            print '   default:'
76            print '      _debug_printf("\\t\\t%s = %%i\\n", %s);' % (self._instance, "(*cmd)" + self._instance)
77            print '      break;'
78            print '   }'
79        else:
80            print '   _debug_printf("\\t\\t%s = %%i\\n", %s);' % (self._instance, "(*cmd)" + self._instance)
81
82
83def dump_decl(instance, decl):
84    dumper = decl_dumper_t(instance, decl)
85    algorithm.apply_visitor(dumper, decl)
86
87
88class type_dumper_t(type_visitor.type_visitor_t):
89
90    def __init__(self, instance, type_):
91        type_visitor.type_visitor_t.__init__(self)
92        self.instance = instance
93        self.type = type_
94
95    def clone(self):
96        return type_dumper_t(self.instance, self.type)
97
98    def visit_char(self):
99        self.print_instance('%i')
100
101    def visit_unsigned_char(self):
102        self.print_instance('%u')
103
104    def visit_signed_char(self):
105        self.print_instance('%i')
106
107    def visit_wchar(self):
108        self.print_instance('%i')
109
110    def visit_short_int(self):
111        self.print_instance('%i')
112
113    def visit_short_unsigned_int(self):
114        self.print_instance('%u')
115
116    def visit_bool(self):
117        self.print_instance('%i')
118
119    def visit_int(self):
120        self.print_instance('%i')
121
122    def visit_unsigned_int(self):
123        self.print_instance('%u')
124
125    def visit_long_int(self):
126        self.print_instance('%li')
127
128    def visit_long_unsigned_int(self):
129        self.print_instance('%lu')
130
131    def visit_long_long_int(self):
132        self.print_instance('%lli')
133
134    def visit_long_long_unsigned_int(self):
135        self.print_instance('%llu')
136
137    def visit_float(self):
138        self.print_instance('%f')
139
140    def visit_double(self):
141        self.print_instance('%f')
142
143    def visit_array(self):
144        for i in range(type_traits.array_size(self.type)):
145            dump_type(self.instance + '[%i]' % i, type_traits.base_type(self.type))
146
147    def visit_pointer(self):
148        self.print_instance('%p')
149
150    def visit_declarated(self):
151        #print 'decl = %r' % self.type.decl_string
152        decl = type_traits.remove_declarated(self.type)
153        dump_decl(self.instance, decl)
154
155    def print_instance(self, format):
156        print '   _debug_printf("\\t\\t%s = %s\\n", %s);' % (self.instance, format, "(*cmd)" + self.instance)
157
158
159def dump_type(instance, type_):
160    type_ = type_traits.remove_alias(type_)
161    visitor = type_dumper_t(instance, type_)
162    algorithm.apply_visitor(visitor, type_)
163
164
165def dump_struct(decls, class_):
166    print 'static void'
167    print 'dump_%s(const %s *cmd)' % (class_.name, class_.name)
168    print '{'
169    dump_decl('', class_)
170    print '}'
171    print ''
172
173
174cmds = [
175    ('SVGA_3D_CMD_SURFACE_DEFINE', 'SVGA3dCmdDefineSurface', (), 'SVGA3dSize'),
176    ('SVGA_3D_CMD_SURFACE_DESTROY', 'SVGA3dCmdDestroySurface', (), None),
177    ('SVGA_3D_CMD_SURFACE_COPY', 'SVGA3dCmdSurfaceCopy', (), 'SVGA3dCopyBox'),
178    ('SVGA_3D_CMD_SURFACE_STRETCHBLT', 'SVGA3dCmdSurfaceStretchBlt', (), None),
179    ('SVGA_3D_CMD_SURFACE_DMA', 'SVGA3dCmdSurfaceDMA', (), 'SVGA3dCopyBox'),
180    ('SVGA_3D_CMD_CONTEXT_DEFINE', 'SVGA3dCmdDefineContext', (), None),
181    ('SVGA_3D_CMD_CONTEXT_DESTROY', 'SVGA3dCmdDestroyContext', (), None),
182    ('SVGA_3D_CMD_SETTRANSFORM', 'SVGA3dCmdSetTransform', (), None),
183    ('SVGA_3D_CMD_SETZRANGE', 'SVGA3dCmdSetZRange', (), None),
184    ('SVGA_3D_CMD_SETRENDERSTATE', 'SVGA3dCmdSetRenderState', (), 'SVGA3dRenderState'),
185    ('SVGA_3D_CMD_SETRENDERTARGET', 'SVGA3dCmdSetRenderTarget', (), None),
186    ('SVGA_3D_CMD_SETTEXTURESTATE', 'SVGA3dCmdSetTextureState', (), 'SVGA3dTextureState'),
187    ('SVGA_3D_CMD_SETMATERIAL', 'SVGA3dCmdSetMaterial', (), None),
188    ('SVGA_3D_CMD_SETLIGHTDATA', 'SVGA3dCmdSetLightData', (), None),
189    ('SVGA_3D_CMD_SETLIGHTENABLED', 'SVGA3dCmdSetLightEnabled', (), None),
190    ('SVGA_3D_CMD_SETVIEWPORT', 'SVGA3dCmdSetViewport', (), None),
191    ('SVGA_3D_CMD_SETCLIPPLANE', 'SVGA3dCmdSetClipPlane', (), None),
192    ('SVGA_3D_CMD_CLEAR', 'SVGA3dCmdClear', (), 'SVGA3dRect'),
193    ('SVGA_3D_CMD_PRESENT', 'SVGA3dCmdPresent', (), 'SVGA3dCopyRect'),
194    ('SVGA_3D_CMD_SHADER_DEFINE', 'SVGA3dCmdDefineShader', (), None),
195    ('SVGA_3D_CMD_SHADER_DESTROY', 'SVGA3dCmdDestroyShader', (), None),
196    ('SVGA_3D_CMD_SET_SHADER', 'SVGA3dCmdSetShader', (), None),
197    ('SVGA_3D_CMD_SET_SHADER_CONST', 'SVGA3dCmdSetShaderConst', (), None),
198    ('SVGA_3D_CMD_DRAW_PRIMITIVES', 'SVGA3dCmdDrawPrimitives', (('SVGA3dVertexDecl', 'numVertexDecls'), ('SVGA3dPrimitiveRange', 'numRanges')), 'SVGA3dVertexDivisor'),
199    ('SVGA_3D_CMD_SETSCISSORRECT', 'SVGA3dCmdSetScissorRect', (), None),
200    ('SVGA_3D_CMD_BEGIN_QUERY', 'SVGA3dCmdBeginQuery', (), None),
201    ('SVGA_3D_CMD_END_QUERY', 'SVGA3dCmdEndQuery', (), None),
202    ('SVGA_3D_CMD_WAIT_FOR_QUERY', 'SVGA3dCmdWaitForQuery', (), None),
203    #('SVGA_3D_CMD_PRESENT_READBACK', None, (), None),
204    ('SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN', 'SVGA3dCmdBlitSurfaceToScreen', (), 'SVGASignedRect'),
205]
206
207def dump_cmds():
208    print r'''
209void
210svga_dump_command(uint32_t cmd_id, const void *data, uint32_t size)
211{
212   const uint8_t *body = (const uint8_t *)data;
213   const uint8_t *next = body + size;
214'''
215    print '   switch(cmd_id) {'
216    indexes = 'ijklmn'
217    for id, header, body, footer in cmds:
218        print '   case %s:' % id
219        print '      _debug_printf("\\t%s\\n");' % id
220        print '      {'
221        print '         const %s *cmd = (const %s *)body;' % (header, header)
222        if len(body):
223            print '         unsigned ' + ', '.join(indexes[:len(body)]) + ';'
224        print '         dump_%s(cmd);' % header
225        print '         body = (const uint8_t *)&cmd[1];'
226        for i in range(len(body)):
227            struct, count = body[i]
228            idx = indexes[i]
229            print '         for(%s = 0; %s < cmd->%s; ++%s) {' % (idx, idx, count, idx)
230            print '            dump_%s((const %s *)body);' % (struct, struct)
231            print '            body += sizeof(%s);' % struct
232            print '         }'
233        if footer is not None:
234            print '         while(body + sizeof(%s) <= next) {' % footer
235            print '            dump_%s((const %s *)body);' % (footer, footer)
236            print '            body += sizeof(%s);' % footer
237            print '         }'
238        if id == 'SVGA_3D_CMD_SHADER_DEFINE':
239            print '         svga_shader_dump((const uint32_t *)body,'
240            print '                          (unsigned)(next - body)/sizeof(uint32_t),'
241            print '                          FALSE);'
242            print '         body = next;'
243        print '      }'
244        print '      break;'
245    print '   default:'
246    print '      _debug_printf("\\t0x%08x\\n", cmd_id);'
247    print '      break;'
248    print '   }'
249    print r'''
250   while(body + sizeof(uint32_t) <= next) {
251      _debug_printf("\t\t0x%08x\n", *(const uint32_t *)body);
252      body += sizeof(uint32_t);
253   }
254   while(body + sizeof(uint32_t) <= next)
255      _debug_printf("\t\t0x%02x\n", *body++);
256}
257'''
258    print r'''
259void
260svga_dump_commands(const void *commands, uint32_t size)
261{
262   const uint8_t *next = commands;
263   const uint8_t *last = next + size;
264
265   assert(size % sizeof(uint32_t) == 0);
266
267   while(next < last) {
268      const uint32_t cmd_id = *(const uint32_t *)next;
269
270      if(SVGA_3D_CMD_BASE <= cmd_id && cmd_id < SVGA_3D_CMD_MAX) {
271         const SVGA3dCmdHeader *header = (const SVGA3dCmdHeader *)next;
272         const uint8_t *body = (const uint8_t *)&header[1];
273
274         next = body + header->size;
275         if(next > last)
276            break;
277
278         svga_dump_command(cmd_id, body, header->size);
279      }
280      else if(cmd_id == SVGA_CMD_FENCE) {
281         _debug_printf("\tSVGA_CMD_FENCE\n");
282         _debug_printf("\t\t0x%08x\n", ((const uint32_t *)next)[1]);
283         next += 2*sizeof(uint32_t);
284      }
285      else {
286         _debug_printf("\t0x%08x\n", cmd_id);
287         next += sizeof(uint32_t);
288      }
289   }
290}
291'''
292
293def main():
294    print copyright.strip()
295    print
296    print '/**'
297    print ' * @file'
298    print ' * Dump SVGA commands.'
299    print ' *'
300    print ' * Generated automatically from svga3d_reg.h by svga_dump.py.'
301    print ' */'
302    print
303    print '#include "svga_types.h"'
304    print '#include "svga_shader_dump.h"'
305    print '#include "svga3d_reg.h"'
306    print
307    print '#include "util/u_debug.h"'
308    print '#include "svga_dump.h"'
309    print
310
311    config = parser.config_t(
312        include_paths = ['../../../include', '../include'],
313        compiler = 'gcc',
314    )
315
316    headers = [
317        'svga_types.h',
318        'svga3d_reg.h',
319    ]
320
321    decls = parser.parse(headers, config, parser.COMPILATION_MODE.ALL_AT_ONCE)
322    global_ns = declarations.get_global_namespace(decls)
323
324    names = set()
325    for id, header, body, footer in cmds:
326        names.add(header)
327        for struct, count in body:
328            names.add(struct)
329        if footer is not None:
330            names.add(footer)
331
332    for class_ in global_ns.classes(lambda decl: decl.name in names):
333        dump_struct(decls, class_)
334
335    dump_cmds()
336
337
338if __name__ == '__main__':
339    main()
340