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_vertex_sampler_states(self
, num_states
, states
):
284 self
._state
.vs
.sampler
= states
286 def bind_geometry_sampler_states(self
, num_states
, states
):
287 self
._state
.gs
.sampler
= states
289 def bind_fragment_sampler_states(self
, num_states
, states
):
290 self
._state
.fs
.sampler
= states
292 def create_rasterizer_state(self
, state
):
295 def bind_rasterizer_state(self
, state
):
296 self
._state
.rasterizer
= state
298 def delete_rasterizer_state(self
, state
):
301 def create_depth_stencil_alpha_state(self
, state
):
302 # Normalize state to avoid spurious differences
303 if not state
.alpha
.enabled
:
305 del state
.alpha
.ref_value
307 if not state
.stencil
[i
].enabled
:
308 del state
.stencil
[i
].func
311 def bind_depth_stencil_alpha_state(self
, state
):
312 self
._state
.depth_stencil_alpha
= state
314 def delete_depth_stencil_alpha_state(self
, state
):
317 _tokenLabelRE
= re
.compile('^\s*\d+: ', re
.MULTILINE
)
319 def _create_shader_state(self
, state
):
320 # Strip the labels from the tokens
321 if state
.tokens
is not None:
322 state
.tokens
= self
._tokenLabelRE
.sub('', state
.tokens
)
325 create_vs_state
= _create_shader_state
326 create_gs_state
= _create_shader_state
327 create_fs_state
= _create_shader_state
329 def bind_vs_state(self
, state
):
330 self
._state
.vs
.shader
= state
332 def bind_gs_state(self
, state
):
333 self
._state
.gs
.shader
= state
335 def bind_fs_state(self
, state
):
336 self
._state
.fs
.shader
= state
338 def _delete_shader_state(self
, state
):
341 delete_vs_state
= _delete_shader_state
342 delete_gs_state
= _delete_shader_state
343 delete_fs_state
= _delete_shader_state
345 def set_blend_color(self
, state
):
346 self
._state
.blend_color
= state
348 def set_stencil_ref(self
, state
):
349 self
._state
.stencil_ref
= state
351 def set_clip_state(self
, state
):
352 self
._state
.clip
= state
354 def _dump_constant_buffer(self
, buffer):
355 if not self
.interpreter
.verbosity(2):
358 data
= self
.real
.buffer_read(buffer)
361 for offset
in range(0, len(data
), struct
.calcsize(format
)):
362 x
, y
, z
, w
= unpack_from(format
, data
, offset
)
363 sys
.stdout
.write('\tCONST[%2u] = {%10.4f, %10.4f, %10.4f, %10.4f}\n' % (index
, x
, y
, z
, w
))
367 def _get_stage_state(self
, shader
):
368 if shader
== PIPE_SHADER_VERTEX
:
369 return self
._state
.vs
370 if shader
== PIPE_SHADER_GEOMETRY
:
371 return self
._state
.gs
372 if shader
== PIPE_SHADER_FRAGMENT
:
373 return self
._state
.fs
376 def set_constant_buffer(self
, shader
, index
, constant_buffer
):
377 self
._update
(self
._get
_stage
_state
(shader
).constant_buffer
, index
, 1, [constant_buffer
])
379 def set_framebuffer_state(self
, state
):
380 self
._state
.fb
= state
382 def set_polygon_stipple(self
, state
):
383 self
._state
.polygon_stipple
= state
385 def _update(self
, array
, start_slot
, num_slots
, states
):
386 if not isinstance(states
, list):
387 # XXX: trace is not serializing multiple scissors/viewports properly yet
390 while len(array
) < start_slot
+ num_slots
:
392 for i
in range(num_slots
):
393 array
[start_slot
+ i
] = states
[i
]
395 def set_scissor_states(self
, start_slot
, num_scissors
, states
):
396 self
._update
(self
._state
.scissors
, start_slot
, num_scissors
, states
)
398 def set_viewport_states(self
, start_slot
, num_viewports
, states
):
399 self
._update
(self
._state
.viewports
, start_slot
, num_viewports
, states
)
401 def create_sampler_view(self
, resource
, templ
):
402 templ
.resource
= resource
405 def sampler_view_destroy(self
, view
):
408 def set_fragment_sampler_views(self
, num
, views
):
409 self
._state
.fs
.sampler_views
= views
411 def set_geometry_sampler_views(self
, num
, views
):
412 self
._state
.gs
.sampler_views
= views
414 def set_vertex_sampler_views(self
, num
, views
):
415 self
._state
.vs
.sampler_views
= views
417 def set_vertex_buffers(self
, start_slot
, num_buffers
, buffers
):
418 self
._update
(self
._state
.vertex_buffers
, start_slot
, num_buffers
, buffers
)
420 def create_vertex_elements_state(self
, num_elements
, elements
):
421 return elements
[0:num_elements
]
423 def bind_vertex_elements_state(self
, state
):
424 self
._state
.vertex_elements
= state
426 def delete_vertex_elements_state(self
, state
):
429 def set_index_buffer(self
, ib
):
430 self
._state
.index_buffer
= ib
432 # Don't dump more than this number of indices/vertices
435 def _merge_indices(self
, info
):
436 '''Merge the vertices into our state.'''
438 index_size
= self
._state
.index_buffer
.index_size
446 assert struct
.calcsize(format
) == index_size
448 data
= self
._state
.index_buffer
.buffer.data
449 max_index
, min_index
= 0, 0xffffffff
451 count
= min(info
.count
, self
.MAX_ELEMENTS
)
453 for i
in xrange(info
.start
, info
.start
+ count
):
454 offset
= self
._state
.index_buffer
.offset
+ i
*index_size
455 index
, = unpack_from(format
, data
, offset
)
456 indices
.append(index
)
457 min_index
= min(min_index
, index
)
458 max_index
= max(max_index
, index
)
460 self
._state
.indices
= indices
462 return min_index
+ info
.index_bias
, max_index
+ info
.index_bias
464 def _merge_vertices(self
, start
, count
):
465 '''Merge the vertices into our state.'''
467 count
= min(count
, self
.MAX_ELEMENTS
)
469 for index
in xrange(start
, start
+ count
):
470 if index
>= start
+ 16:
471 sys
.stdout
.write('\t...\n')
474 for velem
in self
._state
.vertex_elements
:
475 vbuf
= self
._state
.vertex_buffers
[velem
.vertex_buffer_index
]
476 if vbuf
.buffer is None:
479 data
= vbuf
.buffer.data
481 offset
= vbuf
.buffer_offset
+ velem
.src_offset
+ vbuf
.stride
*index
483 'PIPE_FORMAT_R32_FLOAT': 'f',
484 'PIPE_FORMAT_R32G32_FLOAT': '2f',
485 'PIPE_FORMAT_R32G32B32_FLOAT': '3f',
486 'PIPE_FORMAT_R32G32B32A32_FLOAT': '4f',
487 'PIPE_FORMAT_R32_UINT': 'I',
488 'PIPE_FORMAT_R32G32_UINT': '2I',
489 'PIPE_FORMAT_R32G32B32_UINT': '3I',
490 'PIPE_FORMAT_R32G32B32A32_UINT': '4I',
491 'PIPE_FORMAT_R8_UINT': 'B',
492 'PIPE_FORMAT_R8G8_UINT': '2B',
493 'PIPE_FORMAT_R8G8B8_UINT': '3B',
494 'PIPE_FORMAT_R8G8B8A8_UINT': '4B',
495 'PIPE_FORMAT_A8R8G8B8_UNORM': '4B',
496 'PIPE_FORMAT_R8G8B8A8_UNORM': '4B',
497 'PIPE_FORMAT_B8G8R8A8_UNORM': '4B',
498 'PIPE_FORMAT_R16G16B16_SNORM': '3h',
501 data
= vbuf
.buffer.data
502 attribute
= unpack_from(format
, data
, offset
)
503 vertex
.append(attribute
)
505 vertices
.append(vertex
)
507 self
._state
.vertices
= vertices
509 def render_condition(self
, query
, condition
= 0, mode
= 0):
510 self
._state
.render_condition_query
= query
511 self
._state
.render_condition_condition
= condition
512 self
._state
.render_condition_mode
= mode
514 def set_stream_output_targets(self
, num_targets
, tgs
, append_bitmask
):
515 self
._state
.so_targets
= tgs
516 self
._state
.so_append_bitmask
= append_bitmask
518 def draw_vbo(self
, info
):
521 if self
.interpreter
.call_no
< self
.interpreter
.options
.call
and \
522 self
._draw
_no
< self
.interpreter
.options
.draw
:
525 # Merge the all draw state
527 self
._state
.draw
= info
530 min_index
, max_index
= self
._merge
_indices
(info
)
532 min_index
= info
.start
533 max_index
= info
.start
+ info
.count
- 1
534 self
._merge
_vertices
(min_index
, max_index
- min_index
+ 1)
538 _dclRE
= re
.compile('^DCL\s+(IN|OUT|SAMP|SVIEW)\[([0-9]+)\].*$', re
.MULTILINE
)
540 def _normalize_stage_state(self
, stage
):
544 if stage
.shader
is not None and stage
.shader
.tokens
is not None:
545 for mo
in self
._dclRE
.finditer(stage
.shader
.tokens
):
548 register
= registers
.setdefault(file_
, set())
549 register
.add(int(index
))
552 ("CONST", "constant_buffer"),
554 ("SVIEW", "sampler_views"),
557 for fileName
, attrName
in mapping
:
558 register
= registers
.setdefault(fileName
, set())
559 attr
= getattr(stage
, attrName
)
560 for index
in range(len(attr
)):
561 if index
not in register
:
563 while attr
and attr
[-1] is None:
566 def _dump_state(self
):
567 '''Dump our state to JSON and terminate.'''
569 state
= copy
.deepcopy(self
._state
)
571 self
._normalize
_stage
_state
(state
.vs
)
572 self
._normalize
_stage
_state
(state
.gs
)
573 self
._normalize
_stage
_state
(state
.fs
)
581 separators
= (',', ': ')
586 def resource_copy_region(self
, dst
, dst_level
, dstx
, dsty
, dstz
, src
, src_level
, src_box
):
587 if dst
.target
== PIPE_BUFFER
or src
.target
== PIPE_BUFFER
:
588 assert dst
.target
== PIPE_BUFFER
and src
.target
== PIPE_BUFFER
589 assert dst_level
== 0
592 assert src_level
== 0
593 assert src_box
.y
== 0
594 assert src_box
.z
== 0
595 assert src_box
.height
== 1
596 assert src_box
.depth
== 1
597 dst
.data
[dstx
: dstx
+ src_box
.width
] = src
.data
[src_box
.x
: src_box
.x
+ src_box
.width
]
600 def is_resource_referenced(self
, texture
, face
, level
):
603 def get_transfer(self
, texture
, sr
, usage
, box
):
606 transfer
= Transfer(texture
, sr
, usage
, box
)
609 def tex_transfer_destroy(self
, transfer
):
610 self
.interpreter
.unregister_object(transfer
)
612 def transfer_inline_write(self
, resource
, level
, usage
, box
, stride
, layer_stride
, data
):
613 if resource
is not None and resource
.target
== PIPE_BUFFER
:
614 data
= data
.getValue()
615 assert len(data
) == box
.width
616 assert box
.x
+ box
.width
<= len(resource
.data
)
617 resource
.data
[box
.x
: box
.x
+ box
.width
] = data
619 def flush(self
, flags
):
620 # Return a fake fence
621 return self
.interpreter
.call_no
623 def clear(self
, buffers
, color
, depth
, stencil
):
626 def clear_render_target(self
, dst
, rgba
, dstx
, dsty
, width
, height
):
629 def clear_depth_stencil(self
, dst
, clear_flags
, depth
, stencil
, dstx
, dsty
, width
, height
):
632 def create_surface(self
, resource
, surf_tmpl
):
633 surf_tmpl
.resource
= resource
636 def surface_destroy(self
, surface
):
637 self
.interpreter
.unregister_object(surface
)
639 def create_query(self
, query_type
):
642 def destroy_query(self
, query
):
645 def create_stream_output_target(self
, res
, buffer_offset
, buffer_size
):
647 so_target
.resource
= res
648 so_target
.offset
= buffer_offset
649 so_target
.size
= buffer_size
653 class Interpreter(parser
.TraceDumper
):
654 '''Specialization of a trace parser that interprets the calls as it goes
658 ('pipe_screen', 'is_format_supported'),
659 ('pipe_screen', 'get_name'),
660 ('pipe_screen', 'get_vendor'),
661 ('pipe_screen', 'get_param'),
662 ('pipe_screen', 'get_paramf'),
663 ('pipe_screen', 'get_shader_param'),
664 ('pipe_context', 'clear_render_target'), # XXX workaround trace bugs
667 def __init__(self
, stream
, options
):
668 parser
.TraceDumper
.__init
__(self
, stream
, sys
.stderr
)
669 self
.options
= options
672 self
.globl
= Global(self
)
675 def register_object(self
, address
, object):
676 self
.objects
[address
] = object
678 def unregister_object(self
, object):
682 def lookup_object(self
, address
):
683 return self
.objects
[address
]
685 def interpret(self
, trace
):
686 for call
in trace
.calls
:
687 self
.interpret_call(call
)
689 def handle_call(self
, call
):
690 if (call
.klass
, call
.method
) in self
.ignoredCalls
:
693 self
.call_no
= call
.no
695 if self
.verbosity(1):
696 # Write the call to stderr (as stdout would corrupt the JSON output)
699 parser
.TraceDumper
.handle_call(self
, call
)
703 args
= [(str(name
), self
.interpret_arg(arg
)) for name
, arg
in call
.args
]
711 method
= getattr(obj
, call
.method
)
712 ret
= method(**dict(args
))
714 # Keep track of created pointer objects.
715 if call
.ret
and isinstance(call
.ret
, model
.Pointer
):
717 sys
.stderr
.write('warning: NULL returned\n')
718 self
.register_object(call
.ret
.address
, ret
)
722 def interpret_arg(self
, node
):
723 translator
= Translator(self
)
724 return translator
.visit(node
)
726 def verbosity(self
, level
):
727 return self
.options
.verbosity
>= level
730 class Main(parser
.Main
):
732 def get_optparser(self
):
733 '''Custom options.'''
735 optparser
= parser
.Main
.get_optparser(self
)
736 optparser
.add_option("-q", "--quiet", action
="store_const", const
=0, dest
="verbosity", help="no messages")
737 optparser
.add_option("-v", "--verbose", action
="count", dest
="verbosity", default
=0, help="increase verbosity level")
738 optparser
.add_option("-c", "--call", action
="store", type="int", dest
="call", default
=0xffffffff, help="dump on this call")
739 optparser
.add_option("-d", "--draw", action
="store", type="int", dest
="draw", default
=0xffffffff, help="dump on this draw")
742 def process_arg(self
, stream
, options
):
743 parser
= Interpreter(stream
, options
)
747 if __name__
== '__main__':