2 ##########################################################################
4 # Copyright 2008-2013, VMware, Inc.
7 # Permission is hereby granted, free of charge, to any person obtaining a
8 # copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sub license, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
15 # The above copyright notice and this permission notice (including the
16 # next paragraph) shall be included in all copies or substantial portions
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 # IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 # ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 ##########################################################################
38 import parse
as parser
42 from struct
import unpack_from
44 def unpack_from(fmt
, buf
, offset
=0):
45 size
= struct
.calcsize(fmt
)
46 return struct
.unpack(fmt
, buf
[offset
:offset
+ size
])
52 PIPE_SHADER_VERTEX
= 0
53 PIPE_SHADER_FRAGMENT
= 1
54 PIPE_SHADER_GEOMETRY
= 2
55 PIPE_SHADER_COMPUTE
= 3
60 '''JSON serializer function for non-standard Python objects.'''
62 if isinstance(obj
, bytearray
):
63 # TODO: Decide on a single way of dumping blobs
65 # Don't dump full blobs, but merely a description of their size and
67 crc32
= binascii
.crc32(obj
)
70 return 'blob(size=%u,crc32=0x%08x)' % (len(obj
), crc32
)
72 # Dump blobs as an array of 16byte hexadecimals
74 for i
in range(0, len(obj
), 16):
75 res
.append(binascii
.b2a_hex(obj
[i
: i
+16]))
77 # Dump blobs as a single hexadecimal string
78 return binascii
.b2a_hex(obj
)
80 # If the object has a __json__ method, use it.
83 except AttributeError:
92 Python doesn't have C structs, but do its dynamic nature, any object is
97 '''Convert the structure to a standard Python dict, so it can be
101 for name
, value
in self
.__dict
__.items():
102 if not name
.startswith('_'):
107 return repr(self
.__json
__())
110 class Translator(model
.Visitor
):
111 """Translate model arguments into regular Python objects"""
113 def __init__(self
, interpreter
):
114 self
.interpreter
= interpreter
117 def visit(self
, node
):
122 def visit_literal(self
, node
):
123 self
.result
= node
.value
125 def visit_blob(self
, node
):
128 def visit_named_constant(self
, node
):
129 self
.result
= node
.name
131 def visit_array(self
, node
):
133 for element
in node
.elements
:
134 array
.append(self
.visit(element
))
137 def visit_struct(self
, node
):
139 for member_name
, member_node
in node
.members
:
140 member_value
= self
.visit(member_node
)
141 setattr(struct
, member_name
, member_value
)
144 def visit_pointer(self
, node
):
145 self
.result
= self
.interpreter
.lookup_object(node
.address
)
149 '''Base class for classes whose methods can dispatch Gallium calls.'''
151 def __init__(self
, interpreter
):
152 self
.interpreter
= interpreter
155 class Global(Dispatcher
):
156 '''Global name space.
158 For calls that are not associated with objects, i.e, functions and not
162 def pipe_screen_create(self
):
163 return Screen(self
.interpreter
)
165 def pipe_context_create(self
, screen
):
166 return screen
.context_create()
172 def __init__(self
, resource
, usage
, subresource
, box
):
173 self
.resource
= resource
175 self
.subresource
= subresource
179 class Screen(Dispatcher
):
182 def __init__(self
, interpreter
):
183 Dispatcher
.__init
__(self
, interpreter
)
188 def context_create(self
):
189 return Context(self
.interpreter
)
191 def is_format_supported(self
, format
, target
, sample_count
, bind
, geom_flags
):
194 def resource_create(self
, templat
):
196 # Normalize state to avoid spurious differences
197 if resource
.nr_samples
== 0:
198 resource
.nr_samples
= 1
199 if resource
.target
== PIPE_BUFFER
:
200 # We will keep track of buffer contents
201 resource
.data
= bytearray(resource
.width
)
202 if resource
.width
== 1 and resource
.height
== 1 and resource
.depth
== 1:
203 # Ignore dummy texture
207 def resource_destroy(self
, resource
):
208 self
.interpreter
.unregister_object(resource
)
210 def fence_finish(self
, fence
, timeout
=None):
213 def fence_signalled(self
, fence
):
216 def fence_reference(self
, dst
, src
):
219 def flush_frontbuffer(self
, resource
):
223 class Context(Dispatcher
):
226 # Internal methods variable should be prefixed with '_'
228 def __init__(self
, interpreter
):
229 Dispatcher
.__init
__(self
, interpreter
)
231 # Setup initial state
232 self
._state
= Struct()
233 self
._state
.scissors
= []
234 self
._state
.viewports
= []
235 self
._state
.vertex_buffers
= []
236 self
._state
.vs
= Struct()
237 self
._state
.gs
= Struct()
238 self
._state
.fs
= Struct()
239 self
._state
.vs
.shader
= None
240 self
._state
.gs
.shader
= None
241 self
._state
.fs
.shader
= None
242 self
._state
.vs
.sampler
= []
243 self
._state
.gs
.sampler
= []
244 self
._state
.fs
.sampler
= []
245 self
._state
.vs
.sampler_views
= []
246 self
._state
.gs
.sampler_views
= []
247 self
._state
.fs
.sampler_views
= []
248 self
._state
.vs
.constant_buffer
= []
249 self
._state
.gs
.constant_buffer
= []
250 self
._state
.fs
.constant_buffer
= []
257 def create_blend_state(self
, state
):
258 # Normalize state to avoid spurious differences
259 if not state
.logicop_enable
:
260 del state
.logicop_func
261 if not state
.rt
[0].blend_enable
:
262 del state
.rt
[0].rgb_src_factor
263 del state
.rt
[0].rgb_dst_factor
264 del state
.rt
[0].rgb_func
265 del state
.rt
[0].alpha_src_factor
266 del state
.rt
[0].alpha_dst_factor
267 del state
.rt
[0].alpha_func
270 def bind_blend_state(self
, state
):
272 self
._state
.blend
= state
274 def delete_blend_state(self
, state
):
277 def create_sampler_state(self
, state
):
280 def delete_sampler_state(self
, state
):
283 def bind_sampler_states(self
, shader
, start
, num_states
, states
):
284 # FIXME: Handle non-zero start
286 self
._get
_stage
_state
(shader
).sampler
= states
288 def bind_vertex_sampler_states(self
, num_states
, states
):
289 # XXX: deprecated method
290 self
._state
.vs
.sampler
= states
292 def bind_geometry_sampler_states(self
, num_states
, states
):
293 # XXX: deprecated method
294 self
._state
.gs
.sampler
= states
296 def bind_fragment_sampler_states(self
, num_states
, states
):
297 # XXX: deprecated method
298 self
._state
.fs
.sampler
= states
300 def create_rasterizer_state(self
, state
):
303 def bind_rasterizer_state(self
, state
):
304 self
._state
.rasterizer
= state
306 def delete_rasterizer_state(self
, state
):
309 def create_depth_stencil_alpha_state(self
, state
):
310 # Normalize state to avoid spurious differences
311 if not state
.alpha
.enabled
:
313 del state
.alpha
.ref_value
315 if not state
.stencil
[i
].enabled
:
316 del state
.stencil
[i
].func
319 def bind_depth_stencil_alpha_state(self
, state
):
320 self
._state
.depth_stencil_alpha
= state
322 def delete_depth_stencil_alpha_state(self
, state
):
325 _tokenLabelRE
= re
.compile('^\s*\d+: ', re
.MULTILINE
)
327 def _create_shader_state(self
, state
):
328 # Strip the labels from the tokens
329 if state
.tokens
is not None:
330 state
.tokens
= self
._tokenLabelRE
.sub('', state
.tokens
)
333 create_vs_state
= _create_shader_state
334 create_gs_state
= _create_shader_state
335 create_fs_state
= _create_shader_state
337 def bind_vs_state(self
, state
):
338 self
._state
.vs
.shader
= state
340 def bind_gs_state(self
, state
):
341 self
._state
.gs
.shader
= state
343 def bind_fs_state(self
, state
):
344 self
._state
.fs
.shader
= state
346 def _delete_shader_state(self
, state
):
349 delete_vs_state
= _delete_shader_state
350 delete_gs_state
= _delete_shader_state
351 delete_fs_state
= _delete_shader_state
353 def set_blend_color(self
, state
):
354 self
._state
.blend_color
= state
356 def set_stencil_ref(self
, state
):
357 self
._state
.stencil_ref
= state
359 def set_clip_state(self
, state
):
360 self
._state
.clip
= state
362 def _dump_constant_buffer(self
, buffer):
363 if not self
.interpreter
.verbosity(2):
366 data
= self
.real
.buffer_read(buffer)
369 for offset
in range(0, len(data
), struct
.calcsize(format
)):
370 x
, y
, z
, w
= unpack_from(format
, data
, offset
)
371 sys
.stdout
.write('\tCONST[%2u] = {%10.4f, %10.4f, %10.4f, %10.4f}\n' % (index
, x
, y
, z
, w
))
375 def _get_stage_state(self
, shader
):
376 if shader
== PIPE_SHADER_VERTEX
:
377 return self
._state
.vs
378 if shader
== PIPE_SHADER_GEOMETRY
:
379 return self
._state
.gs
380 if shader
== PIPE_SHADER_FRAGMENT
:
381 return self
._state
.fs
384 def set_constant_buffer(self
, shader
, index
, constant_buffer
):
385 self
._update
(self
._get
_stage
_state
(shader
).constant_buffer
, index
, 1, [constant_buffer
])
387 def set_framebuffer_state(self
, state
):
388 self
._state
.fb
= state
390 def set_polygon_stipple(self
, state
):
391 self
._state
.polygon_stipple
= state
393 def _update(self
, array
, start_slot
, num_slots
, states
):
394 if not isinstance(states
, list):
395 # XXX: trace is not serializing multiple scissors/viewports properly yet
398 while len(array
) < start_slot
+ num_slots
:
400 for i
in range(num_slots
):
401 array
[start_slot
+ i
] = states
[i
]
403 def set_scissor_states(self
, start_slot
, num_scissors
, states
):
404 self
._update
(self
._state
.scissors
, start_slot
, num_scissors
, states
)
406 def set_viewport_states(self
, start_slot
, num_viewports
, states
):
407 self
._update
(self
._state
.viewports
, start_slot
, num_viewports
, states
)
409 def create_sampler_view(self
, resource
, templ
):
410 templ
.resource
= resource
413 def sampler_view_destroy(self
, view
):
416 def set_sampler_views(self
, shader
, start
, num
, views
):
417 # FIXME: Handle non-zero start
419 self
._get
_stage
_state
(shader
).sampler_views
= views
421 def set_fragment_sampler_views(self
, num
, views
):
423 self
._state
.fs
.sampler_views
= views
425 def set_geometry_sampler_views(self
, num
, views
):
427 self
._state
.gs
.sampler_views
= views
429 def set_vertex_sampler_views(self
, num
, views
):
431 self
._state
.vs
.sampler_views
= views
433 def set_vertex_buffers(self
, start_slot
, num_buffers
, buffers
):
434 self
._update
(self
._state
.vertex_buffers
, start_slot
, num_buffers
, buffers
)
436 def create_vertex_elements_state(self
, num_elements
, elements
):
437 return elements
[0:num_elements
]
439 def bind_vertex_elements_state(self
, state
):
440 self
._state
.vertex_elements
= state
442 def delete_vertex_elements_state(self
, state
):
445 def set_index_buffer(self
, ib
):
446 self
._state
.index_buffer
= ib
448 # Don't dump more than this number of indices/vertices
451 def _merge_indices(self
, info
):
452 '''Merge the vertices into our state.'''
454 index_size
= self
._state
.index_buffer
.index_size
462 assert struct
.calcsize(format
) == index_size
464 if self
._state
.index_buffer
.buffer is None:
465 # Could happen with index in user memory
468 data
= self
._state
.index_buffer
.buffer.data
469 max_index
, min_index
= 0, 0xffffffff
471 count
= min(info
.count
, self
.MAX_ELEMENTS
)
473 for i
in xrange(info
.start
, info
.start
+ count
):
474 offset
= self
._state
.index_buffer
.offset
+ i
*index_size
475 index
, = unpack_from(format
, data
, offset
)
476 indices
.append(index
)
477 min_index
= min(min_index
, index
)
478 max_index
= max(max_index
, index
)
480 self
._state
.indices
= indices
482 return min_index
+ info
.index_bias
, max_index
+ info
.index_bias
484 def _merge_vertices(self
, start
, count
):
485 '''Merge the vertices into our state.'''
487 count
= min(count
, self
.MAX_ELEMENTS
)
489 for index
in xrange(start
, start
+ count
):
490 if index
>= start
+ 16:
491 sys
.stdout
.write('\t...\n')
494 for velem
in self
._state
.vertex_elements
:
495 vbuf
= self
._state
.vertex_buffers
[velem
.vertex_buffer_index
]
496 if vbuf
.buffer is None:
499 data
= vbuf
.buffer.data
501 offset
= vbuf
.buffer_offset
+ velem
.src_offset
+ vbuf
.stride
*index
503 'PIPE_FORMAT_R32_FLOAT': 'f',
504 'PIPE_FORMAT_R32G32_FLOAT': '2f',
505 'PIPE_FORMAT_R32G32B32_FLOAT': '3f',
506 'PIPE_FORMAT_R32G32B32A32_FLOAT': '4f',
507 'PIPE_FORMAT_R32_UINT': 'I',
508 'PIPE_FORMAT_R32G32_UINT': '2I',
509 'PIPE_FORMAT_R32G32B32_UINT': '3I',
510 'PIPE_FORMAT_R32G32B32A32_UINT': '4I',
511 'PIPE_FORMAT_R8_UINT': 'B',
512 'PIPE_FORMAT_R8G8_UINT': '2B',
513 'PIPE_FORMAT_R8G8B8_UINT': '3B',
514 'PIPE_FORMAT_R8G8B8A8_UINT': '4B',
515 'PIPE_FORMAT_A8R8G8B8_UNORM': '4B',
516 'PIPE_FORMAT_R8G8B8A8_UNORM': '4B',
517 'PIPE_FORMAT_B8G8R8A8_UNORM': '4B',
518 'PIPE_FORMAT_R16G16B16_SNORM': '3h',
521 data
= vbuf
.buffer.data
522 attribute
= unpack_from(format
, data
, offset
)
523 vertex
.append(attribute
)
525 vertices
.append(vertex
)
527 self
._state
.vertices
= vertices
529 def render_condition(self
, query
, condition
= 0, mode
= 0):
530 self
._state
.render_condition_query
= query
531 self
._state
.render_condition_condition
= condition
532 self
._state
.render_condition_mode
= mode
534 def set_stream_output_targets(self
, num_targets
, tgs
, append_bitmask
):
535 self
._state
.so_targets
= tgs
536 self
._state
.so_append_bitmask
= append_bitmask
538 def draw_vbo(self
, info
):
541 if self
.interpreter
.call_no
< self
.interpreter
.options
.call
and \
542 self
._draw
_no
< self
.interpreter
.options
.draw
:
545 # Merge the all draw state
547 self
._state
.draw
= info
550 min_index
, max_index
= self
._merge
_indices
(info
)
552 min_index
= info
.start
553 max_index
= info
.start
+ info
.count
- 1
554 self
._merge
_vertices
(min_index
, max_index
- min_index
+ 1)
558 _dclRE
= re
.compile('^DCL\s+(IN|OUT|SAMP|SVIEW)\[([0-9]+)\].*$', re
.MULTILINE
)
560 def _normalize_stage_state(self
, stage
):
564 if stage
.shader
is not None and stage
.shader
.tokens
is not None:
565 for mo
in self
._dclRE
.finditer(stage
.shader
.tokens
):
568 register
= registers
.setdefault(file_
, set())
569 register
.add(int(index
))
571 if 'SAMP' in registers
and 'SVIEW' not in registers
:
572 registers
['SVIEW'] = registers
['SAMP']
575 #("CONST", "constant_buffer"),
577 ("SVIEW", "sampler_views"),
580 for fileName
, attrName
in mapping
:
581 register
= registers
.setdefault(fileName
, set())
582 attr
= getattr(stage
, attrName
)
583 for index
in range(len(attr
)):
584 if index
not in register
:
586 while attr
and attr
[-1] is None:
589 def _dump_state(self
):
590 '''Dump our state to JSON and terminate.'''
592 state
= copy
.deepcopy(self
._state
)
594 self
._normalize
_stage
_state
(state
.vs
)
595 self
._normalize
_stage
_state
(state
.gs
)
596 self
._normalize
_stage
_state
(state
.fs
)
604 separators
= (',', ': ')
609 def resource_copy_region(self
, dst
, dst_level
, dstx
, dsty
, dstz
, src
, src_level
, src_box
):
610 if dst
.target
== PIPE_BUFFER
or src
.target
== PIPE_BUFFER
:
611 assert dst
.target
== PIPE_BUFFER
and src
.target
== PIPE_BUFFER
612 assert dst_level
== 0
615 assert src_level
== 0
616 assert src_box
.y
== 0
617 assert src_box
.z
== 0
618 assert src_box
.height
== 1
619 assert src_box
.depth
== 1
620 dst
.data
[dstx
: dstx
+ src_box
.width
] = src
.data
[src_box
.x
: src_box
.x
+ src_box
.width
]
623 def is_resource_referenced(self
, texture
, face
, level
):
626 def get_transfer(self
, texture
, sr
, usage
, box
):
629 transfer
= Transfer(texture
, sr
, usage
, box
)
632 def tex_transfer_destroy(self
, transfer
):
633 self
.interpreter
.unregister_object(transfer
)
635 def transfer_inline_write(self
, resource
, level
, usage
, box
, stride
, layer_stride
, data
):
636 if resource
is not None and resource
.target
== PIPE_BUFFER
:
637 data
= data
.getValue()
638 assert len(data
) >= box
.width
639 assert box
.x
+ box
.width
<= len(resource
.data
)
640 resource
.data
[box
.x
: box
.x
+ box
.width
] = data
[:box
.width
]
642 def flush(self
, flags
):
643 # Return a fake fence
644 return self
.interpreter
.call_no
646 def clear(self
, buffers
, color
, depth
, stencil
):
649 def clear_render_target(self
, dst
, rgba
, dstx
, dsty
, width
, height
):
652 def clear_depth_stencil(self
, dst
, clear_flags
, depth
, stencil
, dstx
, dsty
, width
, height
):
655 def create_surface(self
, resource
, surf_tmpl
):
656 surf_tmpl
.resource
= resource
659 def surface_destroy(self
, surface
):
660 self
.interpreter
.unregister_object(surface
)
662 def create_query(self
, query_type
):
665 def destroy_query(self
, query
):
668 def create_stream_output_target(self
, res
, buffer_offset
, buffer_size
):
670 so_target
.resource
= res
671 so_target
.offset
= buffer_offset
672 so_target
.size
= buffer_size
676 class Interpreter(parser
.TraceDumper
):
677 '''Specialization of a trace parser that interprets the calls as it goes
681 ('pipe_screen', 'is_format_supported'),
682 ('pipe_screen', 'get_name'),
683 ('pipe_screen', 'get_vendor'),
684 ('pipe_screen', 'get_param'),
685 ('pipe_screen', 'get_paramf'),
686 ('pipe_screen', 'get_shader_param'),
687 ('pipe_context', 'clear_render_target'), # XXX workaround trace bugs
690 def __init__(self
, stream
, options
):
691 parser
.TraceDumper
.__init
__(self
, stream
, sys
.stderr
)
692 self
.options
= options
695 self
.globl
= Global(self
)
698 def register_object(self
, address
, object):
699 self
.objects
[address
] = object
701 def unregister_object(self
, object):
705 def lookup_object(self
, address
):
707 return self
.objects
[address
]
709 # Could happen, e.g., with user memory pointers
712 def interpret(self
, trace
):
713 for call
in trace
.calls
:
714 self
.interpret_call(call
)
716 def handle_call(self
, call
):
717 if (call
.klass
, call
.method
) in self
.ignoredCalls
:
720 self
.call_no
= call
.no
722 if self
.verbosity(1):
723 # Write the call to stderr (as stdout would corrupt the JSON output)
726 parser
.TraceDumper
.handle_call(self
, call
)
730 args
= [(str(name
), self
.interpret_arg(arg
)) for name
, arg
in call
.args
]
738 method
= getattr(obj
, call
.method
)
739 ret
= method(**dict(args
))
741 # Keep track of created pointer objects.
742 if call
.ret
and isinstance(call
.ret
, model
.Pointer
):
744 sys
.stderr
.write('warning: NULL returned\n')
745 self
.register_object(call
.ret
.address
, ret
)
749 def interpret_arg(self
, node
):
750 translator
= Translator(self
)
751 return translator
.visit(node
)
753 def verbosity(self
, level
):
754 return self
.options
.verbosity
>= level
757 class Main(parser
.Main
):
759 def get_optparser(self
):
760 '''Custom options.'''
762 optparser
= parser
.Main
.get_optparser(self
)
763 optparser
.add_option("-q", "--quiet", action
="store_const", const
=0, dest
="verbosity", help="no messages")
764 optparser
.add_option("-v", "--verbose", action
="count", dest
="verbosity", default
=0, help="increase verbosity level")
765 optparser
.add_option("-c", "--call", action
="store", type="int", dest
="call", default
=0xffffffff, help="dump on this call")
766 optparser
.add_option("-d", "--draw", action
="store", type="int", dest
="draw", default
=0xffffffff, help="dump on this draw")
769 def process_arg(self
, stream
, options
):
770 parser
= Interpreter(stream
, options
)
774 if __name__
== '__main__':