VirtualBox

source: vbox/trunk/src/VBox/Devices/Graphics/svgadump/svga_dump.py@ 105745

Last change on this file since 105745 was 104831, checked in by vboxsync, 6 months ago

3D: Added dumps for SVGA commands used in 3D-off VM config

  • Property svn:eol-style set to native
File size: 13.0 KB
Line 
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
34'''
35The script was reworked for Python 3 by VirtualBox.
36'''
37
38import os
39import sys
40
41from pygccxml import parser
42from pygccxml import declarations
43from pygccxml import utils
44
45from pygccxml.declarations import algorithm
46from pygccxml.declarations import decl_visitor
47from pygccxml.declarations import type_traits
48from pygccxml.declarations import type_visitor
49
50
51enums = True
52
53
54class decl_dumper_t(decl_visitor.decl_visitor_t):
55
56 def __init__(self, instance = '', decl = None):
57 decl_visitor.decl_visitor_t.__init__(self)
58 self._instance = instance
59 self.decl = decl
60
61 def clone(self):
62 return decl_dumper_t(self._instance, self.decl)
63
64 def visit_class(self):
65 class_ = self.decl
66 assert self.decl.class_type in ('struct', 'union')
67
68 for variable in class_.variables():
69 if variable.name != '':
70 #print 'variable = %r' % variable.name
71 dump_type(self._instance + '.' + variable.name, variable.decl_type)
72
73 def visit_enumeration(self):
74 if enums:
75 print(' switch(%s) {' % ("(*cmd)" + self._instance,))
76 for name, value in self.decl.values:
77 if name.endswith('_MIN') or name.endswith('_MAX') or name == 'SVGA3D_R11G11B10_TYPELESS':
78 continue
79 print(' case %s:' % (name,))
80 print(' _debug_printf("\\t\\t%s = %s\\n");' % (self._instance, name))
81 print(' break;')
82 print(' default:')
83 print(' _debug_printf("\\t\\t%s = %%i\\n", %s);' % (self._instance, "(*cmd)" + self._instance))
84 print(' break;')
85 print(' }')
86 else:
87 print(' _debug_printf("\\t\\t%s = %%i\\n", %s);' % (self._instance, "(*cmd)" + self._instance))
88
89
90def dump_decl(instance, decl):
91 dumper = decl_dumper_t(instance, decl)
92 algorithm.apply_visitor(dumper, decl)
93
94
95class type_dumper_t(type_visitor.type_visitor_t):
96
97 def __init__(self, instance, type_):
98 type_visitor.type_visitor_t.__init__(self)
99 self.instance = instance
100 self.type = type_
101
102 def clone(self):
103 return type_dumper_t(self.instance, self.type)
104
105 def visit_char(self):
106 self.print_instance('%i')
107
108 def visit_unsigned_char(self):
109 self.print_instance('%u')
110
111 def visit_signed_char(self):
112 self.print_instance('%i')
113
114 def visit_wchar(self):
115 self.print_instance('%i')
116
117 def visit_short_int(self):
118 self.print_instance('%i')
119
120 def visit_short_unsigned_int(self):
121 self.print_instance('%u')
122
123 def visit_bool(self):
124 self.print_instance('%i')
125
126 def visit_int(self):
127 self.print_instance('%i')
128
129 def visit_unsigned_int(self):
130 self.print_instance('%u')
131
132 def visit_long_int(self):
133 self.print_instance('%li')
134
135 def visit_long_unsigned_int(self):
136 self.print_instance('%lu')
137
138 def visit_long_long_int(self):
139 self.print_instance('%lli')
140
141 def visit_long_long_unsigned_int(self):
142 self.print_instance('%llu')
143
144 def visit_float(self):
145 self.print_instance('%f')
146
147 def visit_double(self):
148 self.print_instance('%f')
149
150 def visit_array(self):
151 for i in range(type_traits.array_size(self.type)):
152 dump_type(self.instance + '[%i]' % i, type_traits.base_type(self.type))
153
154 def visit_pointer(self):
155 self.print_instance('%p')
156
157 def visit_declarated(self):
158 #print 'decl = %r' % self.type.decl_string
159 decl = type_traits.remove_declarated(self.type)
160 dump_decl(self.instance, decl)
161
162 def print_instance(self, format):
163 print(' _debug_printf("\\t\\t%s = %s\\n", %s);' % (self.instance, format, "(*cmd)" + self.instance))
164
165
166def dump_type(instance, type_):
167 type_ = type_traits.remove_alias(type_)
168 visitor = type_dumper_t(instance, type_)
169 algorithm.apply_visitor(visitor, type_)
170
171
172def dump_struct(decls, class_):
173 print('static void')
174 print('dump_%s(const %s *cmd)' % (class_.name, class_.name))
175 print('{')
176 dump_decl('', class_)
177 print('}')
178 print('')
179
180
181cmds = [
182 ('SVGA_3D_CMD_SURFACE_DEFINE', 'SVGA3dCmdDefineSurface', (), 'SVGA3dSize'),
183 ('SVGA_3D_CMD_SURFACE_DESTROY', 'SVGA3dCmdDestroySurface', (), None),
184 ('SVGA_3D_CMD_SURFACE_COPY', 'SVGA3dCmdSurfaceCopy', (), 'SVGA3dCopyBox'),
185 ('SVGA_3D_CMD_SURFACE_STRETCHBLT', 'SVGA3dCmdSurfaceStretchBlt', (), None),
186 ('SVGA_3D_CMD_SURFACE_DMA', 'SVGA3dCmdSurfaceDMA', (), 'SVGA3dCopyBox'),
187 ('SVGA_3D_CMD_CONTEXT_DEFINE', 'SVGA3dCmdDefineContext', (), None),
188 ('SVGA_3D_CMD_CONTEXT_DESTROY', 'SVGA3dCmdDestroyContext', (), None),
189 ('SVGA_3D_CMD_SETTRANSFORM', 'SVGA3dCmdSetTransform', (), None),
190 ('SVGA_3D_CMD_SETZRANGE', 'SVGA3dCmdSetZRange', (), None),
191 ('SVGA_3D_CMD_SETRENDERSTATE', 'SVGA3dCmdSetRenderState', (), 'SVGA3dRenderState'),
192 ('SVGA_3D_CMD_SETRENDERTARGET', 'SVGA3dCmdSetRenderTarget', (), None),
193 ('SVGA_3D_CMD_SETTEXTURESTATE', 'SVGA3dCmdSetTextureState', (), 'SVGA3dTextureState'),
194 ('SVGA_3D_CMD_SETMATERIAL', 'SVGA3dCmdSetMaterial', (), None),
195 ('SVGA_3D_CMD_SETLIGHTDATA', 'SVGA3dCmdSetLightData', (), None),
196 ('SVGA_3D_CMD_SETLIGHTENABLED', 'SVGA3dCmdSetLightEnabled', (), None),
197 ('SVGA_3D_CMD_SETVIEWPORT', 'SVGA3dCmdSetViewport', (), None),
198 ('SVGA_3D_CMD_SETCLIPPLANE', 'SVGA3dCmdSetClipPlane', (), None),
199 ('SVGA_3D_CMD_CLEAR', 'SVGA3dCmdClear', (), 'SVGA3dRect'),
200 ('SVGA_3D_CMD_PRESENT', 'SVGA3dCmdPresent', (), 'SVGA3dCopyRect'),
201 ('SVGA_3D_CMD_SHADER_DEFINE', 'SVGA3dCmdDefineShader', (), None),
202 ('SVGA_3D_CMD_SHADER_DESTROY', 'SVGA3dCmdDestroyShader', (), None),
203 ('SVGA_3D_CMD_SET_SHADER', 'SVGA3dCmdSetShader', (), None),
204 ('SVGA_3D_CMD_SET_SHADER_CONST', 'SVGA3dCmdSetShaderConst', (), None),
205 ('SVGA_3D_CMD_DRAW_PRIMITIVES', 'SVGA3dCmdDrawPrimitives', (('SVGA3dVertexDecl', 'numVertexDecls'), ('SVGA3dPrimitiveRange', 'numRanges')), 'SVGA3dVertexDivisor'),
206 ('SVGA_3D_CMD_SETSCISSORRECT', 'SVGA3dCmdSetScissorRect', (), None),
207 ('SVGA_3D_CMD_BEGIN_QUERY', 'SVGA3dCmdBeginQuery', (), None),
208 ('SVGA_3D_CMD_END_QUERY', 'SVGA3dCmdEndQuery', (), None),
209 ('SVGA_3D_CMD_WAIT_FOR_QUERY', 'SVGA3dCmdWaitForQuery', (), None),
210 #('SVGA_3D_CMD_PRESENT_READBACK', None, (), None),
211 ('SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN', 'SVGA3dCmdBlitSurfaceToScreen', (), 'SVGASignedRect'),
212 ('SVGA_3D_CMD_DEFINE_GB_SCREENTARGET', 'SVGA3dCmdDefineGBScreenTarget', (), None),
213 ('SVGA_3D_CMD_BIND_GB_SCREENTARGET', 'SVGA3dCmdBindGBScreenTarget', (), None),
214 ('SVGA_3D_CMD_UPDATE_GB_SCREENTARGET', 'SVGA3dCmdUpdateGBScreenTarget', (), None),
215 ('SVGA_3D_CMD_UPDATE_GB_IMAGE', 'SVGA3dCmdUpdateGBImage', (), None),
216 ('SVGA_3D_CMD_DEFINE_GB_SURFACE', 'SVGA3dCmdDefineGBSurface', (), None),
217 ('SVGA_3D_CMD_BIND_GB_SURFACE', 'SVGA3dCmdBindGBSurface', (), None),
218 ('SVGA_3D_CMD_INVALIDATE_GB_SURFACE', 'SVGA3dCmdInvalidateGBSurface', (), None),
219 ('SVGA_3D_CMD_DESTROY_GB_SURFACE', 'SVGA3dCmdDestroyGBSurface', (), None),
220]
221
222def dump_cmds():
223 print(r'''
224void
225svga_dump_command(uint32_t cmd_id, const void *data, uint32_t size)
226{
227 const uint8_t *body = (const uint8_t *)data;
228 const uint8_t *next = body + size;
229''')
230 print(' switch(cmd_id) {')
231 indexes = 'ijklmn'
232 for id, header, body, footer in cmds:
233 print(' case %s:' % id)
234 print(' _debug_printf("\\t%s\\n");' % id)
235 print(' {')
236 print(' const %s *cmd = (const %s *)body;' % (header, header))
237 if len(body):
238 print(' unsigned ' + ', '.join(indexes[:len(body)]) + ';')
239 print(' dump_%s(cmd);' % header)
240 print(' body = (const uint8_t *)&cmd[1];')
241 for i in range(len(body)):
242 struct, count = body[i]
243 idx = indexes[i]
244 print(' for(%s = 0; %s < cmd->%s; ++%s) {' % (idx, idx, count, idx))
245 print(' dump_%s((const %s *)body);' % (struct, struct))
246 print(' body += sizeof(%s);' % struct)
247 print(' }')
248 if footer is not None:
249 print(' while(body + sizeof(%s) <= next) {' % footer)
250 print(' dump_%s((const %s *)body);' % (footer, footer))
251 print(' body += sizeof(%s);' % footer)
252 print(' }')
253 if id == 'SVGA_3D_CMD_SHADER_DEFINE':
254 print(' //svga_shader_dump((const uint32_t *)body,')
255 print(' // (unsigned)(next - body)/sizeof(uint32_t),')
256 print(' // FALSE);')
257 print(' body = next;')
258 print(' }')
259 print(' break;')
260 print(' default:')
261 print(' _debug_printf("\\t0x%08x\\n", cmd_id);')
262 print(' break;')
263 print(' }')
264 print(r'''
265 while(body + sizeof(uint32_t) <= next) {
266 _debug_printf("\t\t0x%08x\n", *(const uint32_t *)body);
267 body += sizeof(uint32_t);
268 }
269 while(body + sizeof(uint32_t) <= next)
270 _debug_printf("\t\t0x%02x\n", *body++);
271}
272''')
273 print(r'''
274void
275svga_dump_commands(const void *commands, uint32_t size)
276{
277 const uint8_t *next = commands;
278 const uint8_t *last = next + size;
279
280 //assert(size % sizeof(uint32_t) == 0);
281
282 while(next < last) {
283 const uint32_t cmd_id = *(const uint32_t *)next;
284
285 if(SVGA_3D_CMD_BASE <= cmd_id && cmd_id < SVGA_3D_CMD_MAX) {
286 const SVGA3dCmdHeader *header = (const SVGA3dCmdHeader *)next;
287 const uint8_t *body = (const uint8_t *)&header[1];
288
289 next = body + header->size;
290 if(next > last)
291 break;
292
293 svga_dump_command(cmd_id, body, header->size);
294 }
295 else if(cmd_id == SVGA_CMD_FENCE) {
296 _debug_printf("\tSVGA_CMD_FENCE\n");
297 _debug_printf("\t\t0x%08x\n", ((const uint32_t *)next)[1]);
298 next += 2*sizeof(uint32_t);
299 }
300 else {
301 _debug_printf("\t0x%08x\n", cmd_id);
302 next += sizeof(uint32_t);
303 }
304 }
305}
306''')
307
308def main():
309 print(copyright.strip())
310 print()
311 print('/**')
312 print(' * @file')
313 print(' * Dump SVGA commands.')
314 print(' *')
315 print(' * Generated automatically from svga3d_reg.h by svga_dump.py.')
316 print(' * Modified for VirtualBox.')
317 print(' */')
318 print(r'''
319#include "svga_types.h"
320#include "vmsvga_headers_begin.h"
321#pragma pack(1) /* VMSVGA structures are '__packed'. */
322#include "svga3d_reg.h"
323#pragma pack()
324#include "vmsvga_headers_end.h"
325
326#include "svga_dump.h"
327
328#define LOG_GROUP LOG_GROUP_DEV_VMSVGA
329#include <VBox/log.h>
330
331#define _debug_printf(...) Log7((__VA_ARGS__));
332
333#ifdef LOG_ENABLED''')
334
335 path2mesa = './../../../../../src/VBox/Additions/3D/mesa/mesa-24.0.2/'
336
337 generator_path, generator_name = utils.find_xml_generator()
338
339 # Configure the xml generator
340 config = parser.xml_generator_configuration_t(
341 xml_generator_path = generator_path,
342 xml_generator = generator_name,
343 include_paths = [ path2mesa + 'src/gallium/drivers/svga/',
344 path2mesa + 'src/gallium/drivers/svga/include',
345 path2mesa + 'src'],
346 cflags = '-D UTIL_ARCH_LITTLE_ENDIAN=1 -D UTIL_ARCH_BIG_ENDIAN=0',
347 compiler = 'gcc'
348 )
349
350 headers = [
351 'svga_types.h',
352 'svga3d_reg.h',
353 ]
354
355 decls = parser.parse(headers, config, parser.COMPILATION_MODE.ALL_AT_ONCE)
356 global_ns = declarations.get_global_namespace(decls)
357
358 names = set()
359 for id, header, body, footer in cmds:
360 names.add(header)
361 for struct, count in body:
362 names.add(struct)
363 if footer is not None:
364 names.add(footer)
365
366 for class_ in global_ns.classes(lambda decl: decl.name in names):
367 dump_struct(decls, class_)
368
369 dump_cmds()
370 print('#endif //LOG_ENABLED')
371
372
373if __name__ == '__main__':
374 main()
375
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