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
.sampler
= {}
243 self
._state
.vs
.sampler
= []
244 self
._state
.gs
.sampler
= []
245 self
._state
.fs
.sampler
= []
246 self
._state
.sampler_views
= {}
247 self
._state
.vs
.constant_buffer
= []
248 self
._state
.gs
.constant_buffer
= []
249 self
._state
.fs
.constant_buffer
= []
256 def create_blend_state(self
, state
):
257 # Normalize state to avoid spurious differences
258 if not state
.logicop_enable
:
259 del state
.logicop_func
260 if not state
.rt
[0].blend_enable
:
261 del state
.rt
[0].rgb_src_factor
262 del state
.rt
[0].rgb_dst_factor
263 del state
.rt
[0].rgb_func
264 del state
.rt
[0].alpha_src_factor
265 del state
.rt
[0].alpha_dst_factor
266 del state
.rt
[0].alpha_func
269 def bind_blend_state(self
, state
):
271 self
._state
.blend
= state
273 def delete_blend_state(self
, state
):
276 def create_sampler_state(self
, state
):
279 def delete_sampler_state(self
, state
):
282 def bind_sampler_states(self
, shader
, start
, num_states
, states
):
283 self
._state
.sampler
[shader
] = states
285 def create_rasterizer_state(self
, state
):
288 def bind_rasterizer_state(self
, state
):
289 self
._state
.rasterizer
= state
291 def delete_rasterizer_state(self
, state
):
294 def create_depth_stencil_alpha_state(self
, state
):
295 # Normalize state to avoid spurious differences
296 if not state
.alpha
.enabled
:
298 del state
.alpha
.ref_value
300 if not state
.stencil
[i
].enabled
:
301 del state
.stencil
[i
].func
304 def bind_depth_stencil_alpha_state(self
, state
):
305 self
._state
.depth_stencil_alpha
= state
307 def delete_depth_stencil_alpha_state(self
, state
):
310 _tokenLabelRE
= re
.compile('^\s*\d+: ', re
.MULTILINE
)
312 def _create_shader_state(self
, state
):
313 # Strip the labels from the tokens
314 if state
.tokens
is not None:
315 state
.tokens
= self
._tokenLabelRE
.sub('', state
.tokens
)
318 create_vs_state
= _create_shader_state
319 create_gs_state
= _create_shader_state
320 create_fs_state
= _create_shader_state
322 def bind_vs_state(self
, state
):
323 self
._state
.vs
.shader
= state
325 def bind_gs_state(self
, state
):
326 self
._state
.gs
.shader
= state
328 def bind_fs_state(self
, state
):
329 self
._state
.fs
.shader
= state
331 def _delete_shader_state(self
, state
):
334 delete_vs_state
= _delete_shader_state
335 delete_gs_state
= _delete_shader_state
336 delete_fs_state
= _delete_shader_state
338 def set_blend_color(self
, state
):
339 self
._state
.blend_color
= state
341 def set_stencil_ref(self
, state
):
342 self
._state
.stencil_ref
= state
344 def set_clip_state(self
, state
):
345 self
._state
.clip
= state
347 def _dump_constant_buffer(self
, buffer):
348 if not self
.interpreter
.verbosity(2):
351 data
= self
.real
.buffer_read(buffer)
354 for offset
in range(0, len(data
), struct
.calcsize(format
)):
355 x
, y
, z
, w
= unpack_from(format
, data
, offset
)
356 sys
.stdout
.write('\tCONST[%2u] = {%10.4f, %10.4f, %10.4f, %10.4f}\n' % (index
, x
, y
, z
, w
))
360 def _get_stage_state(self
, shader
):
361 if shader
== PIPE_SHADER_VERTEX
:
362 return self
._state
.vs
363 if shader
== PIPE_SHADER_GEOMETRY
:
364 return self
._state
.gs
365 if shader
== PIPE_SHADER_FRAGMENT
:
366 return self
._state
.fs
369 def set_constant_buffer(self
, shader
, index
, constant_buffer
):
370 self
._update
(self
._get
_stage
_state
(shader
).constant_buffer
, index
, 1, [constant_buffer
])
372 def set_framebuffer_state(self
, state
):
373 self
._state
.fb
= state
375 def set_polygon_stipple(self
, state
):
376 self
._state
.polygon_stipple
= state
378 def _update(self
, array
, start_slot
, num_slots
, states
):
379 if not isinstance(states
, list):
380 # XXX: trace is not serializing multiple scissors/viewports properly yet
383 while len(array
) < start_slot
+ num_slots
:
385 for i
in range(num_slots
):
386 array
[start_slot
+ i
] = states
[i
]
388 def set_scissor_states(self
, start_slot
, num_scissors
, states
):
389 self
._update
(self
._state
.scissors
, start_slot
, num_scissors
, states
)
391 def set_viewport_states(self
, start_slot
, num_viewports
, states
):
392 self
._update
(self
._state
.viewports
, start_slot
, num_viewports
, states
)
394 def create_sampler_view(self
, resource
, templ
):
395 templ
.resource
= resource
398 def sampler_view_destroy(self
, view
):
401 def set_sampler_views(self
, shader
, start
, num
, views
):
402 self
._state
.sampler_views
[shader
] = views
404 def set_vertex_buffers(self
, start_slot
, num_buffers
, buffers
):
405 self
._update
(self
._state
.vertex_buffers
, start_slot
, num_buffers
, buffers
)
407 def create_vertex_elements_state(self
, num_elements
, elements
):
408 return elements
[0:num_elements
]
410 def bind_vertex_elements_state(self
, state
):
411 self
._state
.vertex_elements
= state
413 def delete_vertex_elements_state(self
, state
):
416 def set_index_buffer(self
, ib
):
417 self
._state
.index_buffer
= ib
419 # Don't dump more than this number of indices/vertices
422 def _merge_indices(self
, info
):
423 '''Merge the vertices into our state.'''
425 index_size
= self
._state
.index_buffer
.index_size
433 assert struct
.calcsize(format
) == index_size
435 data
= self
._state
.index_buffer
.buffer.data
436 max_index
, min_index
= 0, 0xffffffff
438 count
= min(info
.count
, self
.MAX_ELEMENTS
)
440 for i
in xrange(info
.start
, info
.start
+ count
):
441 offset
= self
._state
.index_buffer
.offset
+ i
*index_size
442 index
, = unpack_from(format
, data
, offset
)
443 indices
.append(index
)
444 min_index
= min(min_index
, index
)
445 max_index
= max(max_index
, index
)
447 self
._state
.indices
= indices
449 return min_index
+ info
.index_bias
, max_index
+ info
.index_bias
451 def _merge_vertices(self
, start
, count
):
452 '''Merge the vertices into our state.'''
454 count
= min(count
, self
.MAX_ELEMENTS
)
456 for index
in xrange(start
, start
+ count
):
457 if index
>= start
+ 16:
458 sys
.stdout
.write('\t...\n')
461 for velem
in self
._state
.vertex_elements
:
462 vbuf
= self
._state
.vertex_buffers
[velem
.vertex_buffer_index
]
463 if vbuf
.buffer is None:
466 data
= vbuf
.buffer.data
468 offset
= vbuf
.buffer_offset
+ velem
.src_offset
+ vbuf
.stride
*index
470 'PIPE_FORMAT_R32_FLOAT': 'f',
471 'PIPE_FORMAT_R32G32_FLOAT': '2f',
472 'PIPE_FORMAT_R32G32B32_FLOAT': '3f',
473 'PIPE_FORMAT_R32G32B32A32_FLOAT': '4f',
474 'PIPE_FORMAT_R32_UINT': 'I',
475 'PIPE_FORMAT_R32G32_UINT': '2I',
476 'PIPE_FORMAT_R32G32B32_UINT': '3I',
477 'PIPE_FORMAT_R32G32B32A32_UINT': '4I',
478 'PIPE_FORMAT_R8_UINT': 'B',
479 'PIPE_FORMAT_R8G8_UINT': '2B',
480 'PIPE_FORMAT_R8G8B8_UINT': '3B',
481 'PIPE_FORMAT_R8G8B8A8_UINT': '4B',
482 'PIPE_FORMAT_A8R8G8B8_UNORM': '4B',
483 'PIPE_FORMAT_R8G8B8A8_UNORM': '4B',
484 'PIPE_FORMAT_B8G8R8A8_UNORM': '4B',
485 'PIPE_FORMAT_R16G16B16_SNORM': '3h',
488 data
= vbuf
.buffer.data
489 attribute
= unpack_from(format
, data
, offset
)
490 vertex
.append(attribute
)
492 vertices
.append(vertex
)
494 self
._state
.vertices
= vertices
496 def render_condition(self
, query
, condition
= 0, mode
= 0):
497 self
._state
.render_condition_query
= query
498 self
._state
.render_condition_condition
= condition
499 self
._state
.render_condition_mode
= mode
501 def set_stream_output_targets(self
, num_targets
, tgs
, append_bitmask
):
502 self
._state
.so_targets
= tgs
503 self
._state
.so_append_bitmask
= append_bitmask
505 def draw_vbo(self
, info
):
508 if self
.interpreter
.call_no
< self
.interpreter
.options
.call
and \
509 self
._draw
_no
< self
.interpreter
.options
.draw
:
512 # Merge the all draw state
514 self
._state
.draw
= info
517 min_index
, max_index
= self
._merge
_indices
(info
)
519 min_index
= info
.start
520 max_index
= info
.start
+ info
.count
- 1
521 self
._merge
_vertices
(min_index
, max_index
- min_index
+ 1)
525 _dclRE
= re
.compile('^DCL\s+(IN|OUT|SAMP|SVIEW)\[([0-9]+)\].*$', re
.MULTILINE
)
527 def _normalize_stage_state(self
, stage
):
531 if stage
.shader
is not None and stage
.shader
.tokens
is not None:
532 for mo
in self
._dclRE
.finditer(stage
.shader
.tokens
):
535 register
= registers
.setdefault(file_
, set())
536 register
.add(int(index
))
539 ("CONST", "constant_buffer"),
541 ("SVIEW", "sampler_views"),
544 for fileName
, attrName
in mapping
:
545 register
= registers
.setdefault(fileName
, set())
546 attr
= getattr(stage
, attrName
)
547 for index
in range(len(attr
)):
548 if index
not in register
:
550 while attr
and attr
[-1] is None:
553 def _dump_state(self
):
554 '''Dump our state to JSON and terminate.'''
556 state
= copy
.deepcopy(self
._state
)
558 self
._normalize
_stage
_state
(state
.vs
)
559 self
._normalize
_stage
_state
(state
.gs
)
560 self
._normalize
_stage
_state
(state
.fs
)
568 separators
= (',', ': ')
573 def resource_copy_region(self
, dst
, dst_level
, dstx
, dsty
, dstz
, src
, src_level
, src_box
):
574 if dst
.target
== PIPE_BUFFER
or src
.target
== PIPE_BUFFER
:
575 assert dst
.target
== PIPE_BUFFER
and src
.target
== PIPE_BUFFER
576 assert dst_level
== 0
579 assert src_level
== 0
580 assert src_box
.y
== 0
581 assert src_box
.z
== 0
582 assert src_box
.height
== 1
583 assert src_box
.depth
== 1
584 dst
.data
[dstx
: dstx
+ src_box
.width
] = src
.data
[src_box
.x
: src_box
.x
+ src_box
.width
]
587 def is_resource_referenced(self
, texture
, face
, level
):
590 def get_transfer(self
, texture
, sr
, usage
, box
):
593 transfer
= Transfer(texture
, sr
, usage
, box
)
596 def tex_transfer_destroy(self
, transfer
):
597 self
.interpreter
.unregister_object(transfer
)
599 def transfer_inline_write(self
, resource
, level
, usage
, box
, stride
, layer_stride
, data
):
600 if resource
is not None and resource
.target
== PIPE_BUFFER
:
601 data
= data
.getValue()
602 assert len(data
) == box
.width
603 assert box
.x
+ box
.width
<= len(resource
.data
)
604 resource
.data
[box
.x
: box
.x
+ box
.width
] = data
606 def flush(self
, flags
):
607 # Return a fake fence
608 return self
.interpreter
.call_no
610 def clear(self
, buffers
, color
, depth
, stencil
):
613 def clear_render_target(self
, dst
, rgba
, dstx
, dsty
, width
, height
):
616 def clear_depth_stencil(self
, dst
, clear_flags
, depth
, stencil
, dstx
, dsty
, width
, height
):
619 def create_surface(self
, resource
, surf_tmpl
):
620 surf_tmpl
.resource
= resource
623 def surface_destroy(self
, surface
):
624 self
.interpreter
.unregister_object(surface
)
626 def create_query(self
, query_type
):
629 def destroy_query(self
, query
):
632 def create_stream_output_target(self
, res
, buffer_offset
, buffer_size
):
634 so_target
.resource
= res
635 so_target
.offset
= buffer_offset
636 so_target
.size
= buffer_size
640 class Interpreter(parser
.TraceDumper
):
641 '''Specialization of a trace parser that interprets the calls as it goes
645 ('pipe_screen', 'is_format_supported'),
646 ('pipe_screen', 'get_name'),
647 ('pipe_screen', 'get_vendor'),
648 ('pipe_screen', 'get_param'),
649 ('pipe_screen', 'get_paramf'),
650 ('pipe_screen', 'get_shader_param'),
651 ('pipe_context', 'clear_render_target'), # XXX workaround trace bugs
654 def __init__(self
, stream
, options
):
655 parser
.TraceDumper
.__init
__(self
, stream
, sys
.stderr
)
656 self
.options
= options
659 self
.globl
= Global(self
)
662 def register_object(self
, address
, object):
663 self
.objects
[address
] = object
665 def unregister_object(self
, object):
669 def lookup_object(self
, address
):
670 return self
.objects
[address
]
672 def interpret(self
, trace
):
673 for call
in trace
.calls
:
674 self
.interpret_call(call
)
676 def handle_call(self
, call
):
677 if (call
.klass
, call
.method
) in self
.ignoredCalls
:
680 self
.call_no
= call
.no
682 if self
.verbosity(1):
683 # Write the call to stderr (as stdout would corrupt the JSON output)
686 parser
.TraceDumper
.handle_call(self
, call
)
690 args
= [(str(name
), self
.interpret_arg(arg
)) for name
, arg
in call
.args
]
698 method
= getattr(obj
, call
.method
)
699 ret
= method(**dict(args
))
701 # Keep track of created pointer objects.
702 if call
.ret
and isinstance(call
.ret
, model
.Pointer
):
704 sys
.stderr
.write('warning: NULL returned\n')
705 self
.register_object(call
.ret
.address
, ret
)
709 def interpret_arg(self
, node
):
710 translator
= Translator(self
)
711 return translator
.visit(node
)
713 def verbosity(self
, level
):
714 return self
.options
.verbosity
>= level
717 class Main(parser
.Main
):
719 def get_optparser(self
):
720 '''Custom options.'''
722 optparser
= parser
.Main
.get_optparser(self
)
723 optparser
.add_option("-q", "--quiet", action
="store_const", const
=0, dest
="verbosity", help="no messages")
724 optparser
.add_option("-v", "--verbose", action
="count", dest
="verbosity", default
=0, help="increase verbosity level")
725 optparser
.add_option("-c", "--call", action
="store", type="int", dest
="call", default
=0xffffffff, help="dump on this call")
726 optparser
.add_option("-d", "--draw", action
="store", type="int", dest
="draw", default
=0xffffffff, help="dump on this draw")
729 def process_arg(self
, stream
, options
):
730 parser
= Interpreter(stream
, options
)
734 if __name__
== '__main__':