Skip to content

Commit 60c6d96

Browse files
authored
Add print_report (#334)
* Add print_report * backends do not have to implement print_report * fmt
1 parent e1aed4d commit 60c6d96

File tree

4 files changed

+92
-6
lines changed

4 files changed

+92
-6
lines changed

wgpu/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def _register_backend(cls):
3333
globals()["GPU"] = GPU
3434
globals()["request_adapter"] = gpu.request_adapter
3535
globals()["request_adapter_async"] = gpu.request_adapter_async
36+
if hasattr(gpu, "print_report"):
37+
globals()["print_report"] = gpu.print_report
38+
else:
39+
globals()["print_report"] = _base_GPU.print_report
3640

3741

3842
_base_GPU = GPU # noqa: F405, N816

wgpu/backends/rs.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,83 @@ async def request_adapter_async(self, *, canvas, power_preference=None):
363363
canvas=canvas, power_preference=power_preference
364364
) # no-cover
365365

366+
def _generate_report(self):
367+
"""Get a dictionary with info about the internal status of WGPU.
368+
The structure of the dict is not defined, for the moment. Use print_report().
369+
"""
370+
371+
# H: surfaces: WGPUStorageReport, backendType: WGPUBackendType, vulkan: WGPUHubReport, metal: WGPUHubReport, dx12: WGPUHubReport, dx11: WGPUHubReport, gl: WGPUHubReport
372+
struct = new_struct_p(
373+
"WGPUGlobalReport *",
374+
# not used: surfaces
375+
# not used: backendType
376+
# not used: vulkan
377+
# not used: metal
378+
# not used: dx12
379+
# not used: dx11
380+
# not used: gl
381+
)
382+
383+
# H: void f(WGPUInstance instance, WGPUGlobalReport* report)
384+
lib.wgpuGenerateReport(get_wgpu_instance(), struct)
385+
386+
report = {}
387+
388+
report["surfaces"] = {
389+
"occupied": struct.surfaces.numOccupied,
390+
"vacant": struct.surfaces.numVacant,
391+
"error": struct.surfaces.numError,
392+
"element_size": struct.surfaces.elementSize,
393+
}
394+
report["backend_type"] = struct.backendType # note: could make this a set
395+
for backend in ("vulkan", "metal", "dx12", "dx11", "gl"):
396+
c_hub_report = getattr(struct, backend)
397+
report[backend] = {}
398+
for key in dir(c_hub_report):
399+
c_storage_report = getattr(c_hub_report, key)
400+
storage_report = {
401+
"occupied": c_storage_report.numOccupied,
402+
"vacant": c_storage_report.numVacant,
403+
"error": c_storage_report.numError,
404+
"element_size": c_storage_report.elementSize,
405+
}
406+
# if any(x!=0 for x in storage_report.values()):
407+
report[backend][key] = storage_report
408+
409+
return report
410+
411+
def print_report(self):
412+
def print_line(topic, occupied, vacant, error, el_size):
413+
print(
414+
topic.rjust(20),
415+
str(occupied).rjust(8),
416+
str(vacant).rjust(8),
417+
str(error).rjust(8),
418+
str(el_size).rjust(8),
419+
)
420+
421+
def print_storage_report(topic, d):
422+
print_line(topic, d["occupied"], d["vacant"], d["error"], d["element_size"])
423+
424+
report = self._generate_report()
425+
426+
print(f"{self.__class__.__module__}.WGPU report:")
427+
print()
428+
print_line("", "Occupied", "Vacant", "Error", "el-size")
429+
print()
430+
print_storage_report("surfaces", report["surfaces"])
431+
for backend in ("vulkan", "metal", "dx12", "dx11", "gl"):
432+
backend_has_stuff = False
433+
for hub_report in report[backend].values():
434+
report_has_stuff = any(x != 0 for x in hub_report.values())
435+
backend_has_stuff |= report_has_stuff
436+
if backend_has_stuff:
437+
print_line(f"--- {backend} ---", "", "", "", "")
438+
for key, val in report[backend].items():
439+
print_storage_report(key, val)
440+
else:
441+
print_line(f"--- {backend} ---", "", "", "", "")
442+
366443

367444
class GPUCanvasContext(base.GPUCanvasContext):
368445
def __init__(self, canvas):

wgpu/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ def get_preferred_canvas_format(self):
108108
"""
109109
raise RuntimeError("Use canvas.get_preferred_format() instead.")
110110

111+
@apidiff.add("Usefull")
112+
def print_report(self):
113+
"""Print a report about the interals if the backend."""
114+
print(f"{self.__class__.__module__}.GPU: No report available.")
115+
111116

112117
class GPUCanvasContext:
113118
"""A context object associated with a canvas, to present what has been drawn."""

wgpu/resources/codegen_report.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@
99
* Wrote 35 enums to enums.py
1010
* Wrote 59 structs to structs.py
1111
### Patching API for base.py
12-
* Diffs for GPU: change get_preferred_canvas_format, change request_adapter, change request_adapter_async
12+
* Diffs for GPU: add print_report, change get_preferred_canvas_format, change request_adapter, change request_adapter_async
1313
* Diffs for GPUCanvasContext: add get_preferred_format, add present
1414
* Diffs for GPUDevice: add adapter, add create_buffer_with_data, hide import_external_texture, hide pop_error_scope, hide push_error_scope
1515
* Diffs for GPUBuffer: add map_read, add map_write, hide get_mapped_range, hide map_async, hide unmap
1616
* Diffs for GPUTexture: add size
1717
* Diffs for GPUTextureView: add size, add texture
1818
* Diffs for GPUQueue: add read_buffer, add read_texture, hide copy_external_image_to_texture
19-
* Validated 39 classes, 111 methods, 44 properties
19+
* Validated 39 classes, 112 methods, 44 properties
2020
### Patching API for backends/rs.py
2121
* Diffs for GPUAdapter: add request_device_tracing
22-
* Validated 39 classes, 98 methods, 0 properties
22+
* Validated 39 classes, 100 methods, 0 properties
2323
## Validating rs.py
2424
* Enum BufferMapState missing in wgpu.h
2525
* Enum PipelineErrorReason missing in wgpu.h
2626
* Enum AutoLayoutMode missing in wgpu.h
2727
* Enum CanvasAlphaMode missing in wgpu.h
2828
* Wrote 231 enum mappings and 49 struct-field mappings to rs_mappings.py
29-
* Validated 93 C function calls
30-
* Not using 90 C functions
31-
* Validated 72 C structs
29+
* Validated 94 C function calls
30+
* Not using 89 C functions
31+
* Validated 73 C structs

0 commit comments

Comments
 (0)