Skip to content

Commit c8ab9d2

Browse files
committed
Add WRAPPER macros that logs all wrapper entry/exist
1 parent a5f7b6e commit c8ab9d2

13 files changed

+166
-62
lines changed

src/vulkan/wrapper/vk_entrypoints.py

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
wrapper_tramp_$name(
5555
$decl_params)
5656
{
57-
int cmd_id = commands++;
57+
int cmd_id = __wrapper_commands++;
5858
struct temporary_objects temp;
5959
list_inithead(&temp.objects);
6060
$handle_unwrap_logic
@@ -64,6 +64,7 @@
6464
return $return_block;
6565
}""")
6666

67+
6768
COMMAND_BLACKLIST = [
6869
'FreeCommandBuffers', # implemented
6970
'CreateDevice', # implemented
@@ -169,7 +170,7 @@ def _generate_trampoline(command, dispatch_table="device->dispatch_table"):
169170
physical_device = "base->physical"
170171

171172
# handle_unwrap_logic.append(f"#ifdef NEEDS_PRINTING_{command.name}")
172-
handle_unwrap_logic.append(f" VK_CMD_LOG(\"{command.name} (id=%d)\", cmd_id);")
173+
handle_unwrap_logic.append(f" VK_CMD_LOG(\"dispatch->{command.name} (id=%d)\", cmd_id);")
173174
# handle_unwrap_logic.append(f"#endif")
174175

175176
handle_unwrap_logic.append(f"")
@@ -359,7 +360,7 @@ def _generate_trampoline(command, dispatch_table="device->dispatch_table"):
359360
if command.return_type == 'VkResult' and command.name not in SPAMMY_COMMANDS:
360361
logger = "WLOGE" if command.name not in ("AllocateDescriptorSets", "GetPhysicalDeviceImageFormatProperties") else "WLOG"
361362
handle_wrap_logic.append(f" if (result != VK_SUCCESS) {{")
362-
handle_wrap_logic.append(f" {logger}(\"Call to {command.name} with ({",".join(types)}) failed with result: %d\", {",".join(call)}, result);")
363+
handle_wrap_logic.append(f" {logger}(\"dispatch->{command.name}({",".join(types)}) failed with result: %d (id=%d)\", {",".join(call)}, result, cmd_id);")
363364
handle_wrap_logic.append(f" }}")
364365

365366
if command.return_type == 'VkResult':
@@ -371,7 +372,9 @@ def _generate_trampoline(command, dispatch_table="device->dispatch_table"):
371372
return_block = "" if command.return_type == 'void' else "result"
372373
assign_block = "" if command.return_type == 'void' else f"{command.return_type} result = "
373374

374-
handle_unwrap_logic[idx] = f"WLOGA(\"{command.name}({', '.join(types)})\", {', '.join([p.name for p in params])});"
375+
if command.name not in SPAMMY_COMMANDS:
376+
handle_unwrap_logic[idx] = f" WLOGA(\"dispatch->{command.name}({', '.join(types)}) (id=%d)\", {', '.join([p.name for p in params])}, cmd_id);"
377+
handle_wrap_logic.append(f" WLOGA(\"dispatch->{command.name} {'returned %d' if command.return_type != 'void' else 'finished'} (id=%d)\"{', result' if command.return_type != 'void' else ''}, cmd_id);")
375378

376379
return TRAMPOLINE_TEMPLATE.substitute(
377380
return_type=command.return_type,
@@ -402,6 +405,18 @@ def __init__(self, name):
402405
def prefixed_name(self, prefix):
403406
return prefix + '_' + self.name
404407

408+
WRAP_TEMPLATE = string.Template("""
409+
VKAPI_ATTR $return_type VKAPI_CALL wrapper_$name($decl_params) {
410+
int cmd_id = __wrapper_commands++;
411+
WLOGT("wrapper->$name($type_params) (id=%d)", $call_params, cmd_id);
412+
bool should_print = VK_CMD_CAN_TRACE;
413+
$PRINT_PRE
414+
$assign __wrapper_$name($call_params);
415+
WLOGT("wrapper->$name $return_str (id=%d)", $return_fmt cmd_id);
416+
$PRINT_POST
417+
return $result;
418+
}""")
419+
405420
class Entrypoint(EntrypointBase):
406421
def __init__(self, name, return_type, params):
407422
super(Entrypoint, self).__init__(name)
@@ -443,6 +458,84 @@ def trampoline(self):
443458
return _generate_trampoline(self, dispatch_table="base->dispatch_table")
444459
else:
445460
return _generate_trampoline(self, dispatch_table="base->device->dispatch_table")
461+
462+
def wrap(self):
463+
if self.alias:
464+
return ""
465+
466+
call = [param.name for param in self.params]
467+
types = []
468+
469+
for param in self.params:
470+
is_input = param.num_pointers == 0 or param.is_const
471+
472+
if not is_input:
473+
types.append(f"{param.name}: %p")
474+
continue
475+
is_wrapped = param.type in WrappedStructs
476+
is_struct = param.type in TYPES
477+
is_special = param.type in SPECIAL_TYPES
478+
479+
# Wrapped handles
480+
if is_wrapped:
481+
types.append(f"{param.name}: %p")
482+
elif is_struct:
483+
types.append(f"{param.name}: %p")
484+
elif is_special:
485+
types.append(f"{param.name}: %x")
486+
else:
487+
types.append(f"{param.name}: %x")
488+
# handle_wrap_logic.append(f" WLOGA(\"dispatch->{command.name} {'returned %d' if command.return_type != 'void' else 'finished'} (id=%d)\"{', result' if command.return_type != 'void' else ''}, cmd_id);")
489+
pre_print = []
490+
pre_print.append(f" if (should_log) {{")
491+
pre_print.append(f" VK_CMD_LOG(\"wrapper_{self.name} (id=%d)\", cmd_id);")
492+
# pre_print += print_param(self, self.params[0], mode='input')
493+
for param in self.params:
494+
if self.name in COMMAND_BLACKLIST:
495+
continue
496+
is_input = param.num_pointers == 0 or param.is_const
497+
if not is_input:
498+
continue
499+
pre_print += print_param(self, param, mode='input')
500+
pre_print.append(f" VK_CMD_FLUSH();")
501+
pre_print.append(f" }}")
502+
503+
post_print = []
504+
post_print.append(f" if (should_log) {{")
505+
if self.return_type == 'VkResult':
506+
post_print += print_param(self, 'result', mode='output')
507+
for param in self.params[1:]:
508+
if self.name in COMMAND_BLACKLIST:
509+
continue
510+
is_input = param.num_pointers == 0 or param.is_const
511+
if is_input:
512+
continue
513+
if self.return_type == 'VkResult':
514+
post_print.append(f" if ({param.name} && result == VK_SUCCESS) {{")
515+
else:
516+
post_print.append(f" if ({param.name}) {{")
517+
post_print += print_param(self, param, mode='output')
518+
post_print.append(f" }} else {{")
519+
post_print += [
520+
f" VK_CMD_LOG_UNCONDITIONAL(\" out: *{param.name}: {param.type}* = %p\", {param.name});",
521+
f" }}"
522+
]
523+
post_print.append(f" }}")
524+
525+
526+
return WRAP_TEMPLATE.substitute(
527+
return_type=self.return_type,
528+
name=self.name,
529+
assign=f"{self.return_type} result =" if self.return_type != "void" else "",
530+
result=f"result" if self.return_type != "void" else "",
531+
decl_params=self.decl_params(),
532+
call_params=", ".join(call),
533+
type_params=types,
534+
return_str='returned %d' if self.return_type != 'void' else 'finished',
535+
return_fmt='result,' if self.return_type != 'void' else '',
536+
PRINT_PRE="\n".join(pre_print),
537+
PRINT_POST="\n".join(post_print),
538+
)
446539

447540
class EntrypointAlias(EntrypointBase):
448541
def __init__(self, name, entrypoint):

src/vulkan/wrapper/vk_unwrapper_gen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@
191191
'VkWin32SurfaceCreateInfoKHR',
192192
# Too complex, and not needed
193193
# 'VkDeviceCreateInfo', # 2d array of cstrings
194-
'VkInstanceCreateInfo',
194+
# 'VkInstanceCreateInfo',
195195
'VkMicromapVersionInfoEXT', # latexmath
196196
'VkPipelineMultisampleStateCreateInfo', # latexmath
197197
'VkPipelineMultisampleStateCreateInfo', # latexmath

src/vulkan/wrapper/vk_wrapper_trampolines_gen.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
5858
extern struct wrapper_entry_masks wrapper_printer_masks;
5959
60+
_Atomic extern int __wrapper_commands;
61+
6062
% for e in entrypoints:
6163
% if e.alias:
6264
<% continue %>
@@ -75,6 +77,16 @@
7577
#define has_device_wrapper_${e.name}(...) (wrapper_device_entrypoints.${e.name})
7678
#define has_physical_device_wrapper_${e.name}(...) (wrapper_physical_device_entrypoints.${e.name})
7779
#define name_of_wrapper_${e.name}(...) "wrapper_${e.name}"
80+
81+
#define WRAPPED_${e.name} ${"\\n".join([line + " \\\\" for line in e.wrap().split("\\n")])}
82+
83+
#define RETURN_${e.name} VKAPI_ATTR ${e.return_type} VKAPI_CALL
84+
#define WRAPPER_NAME_${e.name}(...) ${e.name}
85+
#define WRAPPER_${e.name}(...) \\\\\n
86+
static RETURN_${e.name} __wrapper_${e.name}(__VA_ARGS__); \\\\\n
87+
WRAPPED_${e.name} \\\\\n
88+
static RETURN_${e.name} __wrapper_${e.name}(__VA_ARGS__)
89+
7890
% if e.guard is not None:
7991
#else
8092
#define TRY_${e.name}(TRUE, FALSE) FALSE
@@ -113,7 +125,7 @@
113125
#include "vk_unwrappers.h"
114126
#include "vk_printers.h"
115127
116-
_Atomic static int commands = 0;
128+
_Atomic int __wrapper_commands = 0;
117129
118130
#define VK_PRINT_VkAccelerationStructureVersionInfoKHR(...)
119131
#define VK_PRINT_VkAccelerationStructureBuildGeometryInfoKHR(...)

src/vulkan/wrapper/wrapper_checks.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@
2727
#define WCHECKV(call) {__W_WRAP__(call, { if(has_device_wrapper_##call) { __CHECKV__(wrapper_device_entrypoints. call); } else { CHECKV(call); }})}
2828
#define WPCHECK(call) ({__W_WRAP__(call, has_physical_device_wrapper_##call ? __CHECK__(wrapper_physical_device_entrypoints. call) : PCHECK(call))})
2929
#define WPCHECKV(call) {__W_WRAP__(call, { if(has_physical_device_wrapper_##call) { __CHECKV__(wrapper_physical_device_entrypoints. call); } else { PCHECKV(call); }})}
30+
31+
#define __WRAP(call) return call
32+
33+
#define __WRAPV(call) call

src/vulkan/wrapper/wrapper_debug.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,21 @@ bool use_compute_shader_mode() {
348348

349349
return g_use_compute_shader_mode;
350350
}
351+
352+
353+
bool use_wrapper_trace() {
354+
static bool value;
355+
static bool init;
356+
if (init)
357+
return value;
358+
init = true;
359+
const char* mask_bcn = getenv("WRAPPER_CMD_LOG_LEVEL");
360+
if (!mask_bcn)
361+
return value = false;
362+
363+
if (strstr(mask_bcn, "trace")) {
364+
return value = true;
365+
}
366+
367+
return value = false;
368+
}

src/vulkan/wrapper/wrapper_debug.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ void initialize_cmd_print_masks(void);
2626

2727
bool use_image_view_mode(void);
2828

29-
bool use_compute_shader_mode(void);
29+
bool use_compute_shader_mode(void);
30+
31+
bool use_wrapper_trace(void);

src/vulkan/wrapper/wrapper_device.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "wrapper_trampolines.h"
1717
#include "wrapper_debug.h"
1818
#include "vk_unwrappers.h"
19+
#include "vk_printers.h"
1920
#include "spirv_edit.h"
2021

2122
#include "bcdec.h"

src/vulkan/wrapper/wrapper_device_memory.c

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -295,28 +295,14 @@ wrapper_device_memory_from_handle(struct wrapper_device *device,
295295
return mem;
296296
}
297297

298-
VKAPI_ATTR VkResult VKAPI_CALL
299-
wrapper_AllocateMemory(VkDevice _device,
298+
WRAPPER_AllocateMemory(VkDevice _device,
300299
const VkMemoryAllocateInfo* pAllocateInfo,
301300
const VkAllocationCallbacks* pAllocator,
302301
VkDeviceMemory* pMemory) {
303302
VK_FROM_HANDLE(wrapper_device, device, _device);
304303
struct wrapper_device_memory *mem;
305304
VkResult result;
306305

307-
// LOG("in wrapper_AllocateMemory");
308-
309-
// __vk_println_all("AllocateMemory");
310-
// __loga("AllocateMemory(device: %p, pAllocateInfo: %p, pAllocator: %p, pMemory: %p)", device, pAllocateInfo, pAllocator, pMemory);
311-
// __vk_println(" in: device: VkDevice (handle) = %p", device);
312-
// __vk_flush(2);
313-
// __vk_println(" in: pAllocateInfo: VkMemoryAllocateInfo*");
314-
// vk_print_VkMemoryAllocateInfo(" ", pAllocateInfo);
315-
// __vk_flush(2);
316-
// __vk_println(" in: pAllocator: VkAllocationCallbacks*");
317-
// vk_print_VkAllocationCallbacks(" ", pAllocator);
318-
// __vk_flush(2);
319-
320306
VkMemoryPropertyFlags property_flags =
321307
device->physical->memory_properties.memoryTypes[
322308
pAllocateInfo->memoryTypeIndex].propertyFlags;
@@ -376,8 +362,7 @@ wrapper_AllocateMemory(VkDevice _device,
376362
return result;
377363
}
378364

379-
VKAPI_ATTR void VKAPI_CALL
380-
wrapper_FreeMemory(VkDevice _device, VkDeviceMemory _memory,
365+
WRAPPER_FreeMemory(VkDevice _device, VkDeviceMemory _memory,
381366
const VkAllocationCallbacks* pAllocator)
382367
{
383368
VK_FROM_HANDLE(wrapper_device, device, _device);
@@ -394,8 +379,7 @@ wrapper_FreeMemory(VkDevice _device, VkDeviceMemory _memory,
394379
pAllocator);
395380
}
396381

397-
VKAPI_ATTR VkResult VKAPI_CALL
398-
wrapper_MapMemory2KHR(VkDevice _device,
382+
WRAPPER_MapMemory2KHR(VkDevice _device,
399383
const VkMemoryMapInfoKHR* pMemoryMapInfo,
400384
void** ppData)
401385
{
@@ -467,13 +451,11 @@ wrapper_MapMemory2KHR(VkDevice _device,
467451
return VK_SUCCESS;
468452
}
469453

470-
VKAPI_ATTR void VKAPI_CALL
471-
wrapper_UnmapMemory(VkDevice _device, VkDeviceMemory _memory) {
454+
WRAPPER_UnmapMemory(VkDevice _device, VkDeviceMemory _memory) {
472455
vk_common_UnmapMemory(_device, _memory);
473456
}
474457

475-
VKAPI_ATTR VkResult VKAPI_CALL
476-
wrapper_UnmapMemory2KHR(VkDevice _device,
458+
WRAPPER_UnmapMemory2KHR(VkDevice _device,
477459
const VkMemoryUnmapInfoKHR* pMemoryUnmapInfo)
478460
{
479461
VK_FROM_HANDLE(wrapper_device, device, _device);

src/vulkan/wrapper/wrapper_image.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
#include <drm-uapi/drm_fourcc.h>
1414
#endif
1515

16-
17-
VKAPI_ATTR VkResult VKAPI_CALL
18-
wrapper_CreateImage(VkDevice _device,
16+
WRAPPER_CreateImage(VkDevice _device,
1917
const VkImageCreateInfo* pCreateInfo,
2018
const VkAllocationCallbacks* pAllocator,
2119
VkImage* pImage)
@@ -73,8 +71,7 @@ wrapper_CreateImage(VkDevice _device,
7371
return result;
7472
}
7573

76-
VKAPI_ATTR void VKAPI_CALL
77-
wrapper_DestroyImage(VkDevice _device,
74+
WRAPPER_DestroyImage(VkDevice _device,
7875
VkImage _image,
7976
const VkAllocationCallbacks* pAllocator)
8077
{
@@ -91,8 +88,7 @@ wrapper_DestroyImage(VkDevice _device,
9188
}
9289

9390

94-
VKAPI_ATTR VkResult VKAPI_CALL
95-
wrapper_CreateImageView(
91+
WRAPPER_CreateImageView(
9692
VkDevice device,
9793
const VkImageViewCreateInfo* pCreateInfo,
9894
const VkAllocationCallbacks* pAllocator,

src/vulkan/wrapper/wrapper_instance.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "vk_extensions.h"
77
#include "vk_debug_utils.h"
88
#include "wrapper_debug.h"
9+
#include "vk_printers.h"
910

1011
const struct vk_instance_extension_table wrapper_instance_extensions = {
1112
.KHR_get_surface_capabilities2 = true,
@@ -161,8 +162,7 @@ static VkResult wrapper_vulkan_init()
161162
return VK_SUCCESS;
162163
}
163164

164-
VKAPI_ATTR VkResult VKAPI_CALL
165-
wrapper_EnumerateInstanceVersion(uint32_t* pApiVersion)
165+
WRAPPER_EnumerateInstanceVersion(uint32_t* pApiVersion)
166166
{
167167

168168
if (!vulkan_library_init())
@@ -171,8 +171,7 @@ wrapper_EnumerateInstanceVersion(uint32_t* pApiVersion)
171171
return enumerate_instance_version(pApiVersion);
172172
}
173173

174-
VKAPI_ATTR VkResult VKAPI_CALL
175-
wrapper_EnumerateInstanceExtensionProperties(const char* pLayerName,
174+
WRAPPER_EnumerateInstanceExtensionProperties(const char* pLayerName,
176175
uint32_t* pPropertyCount,
177176
VkExtensionProperties* pProperties)
178177
{
@@ -235,8 +234,7 @@ static void wrapper_register_internal_log_callback(struct wrapper_instance *inst
235234
instance->internal_debug_messenger = messenger;
236235
}
237236

238-
VKAPI_ATTR VkResult VKAPI_CALL
239-
wrapper_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
237+
WRAPPER_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
240238
const VkAllocationCallbacks *pAllocator,
241239
VkInstance *pInstance)
242240
{
@@ -352,8 +350,7 @@ wrapper_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
352350
return VK_SUCCESS;
353351
}
354352

355-
VKAPI_ATTR void VKAPI_CALL
356-
wrapper_DestroyInstance(VkInstance _instance,
353+
WRAPPER_DestroyInstance(VkInstance _instance,
357354
const VkAllocationCallbacks *pAllocator)
358355
{
359356
VK_FROM_HANDLE(wrapper_instance, instance, _instance);
@@ -372,8 +369,7 @@ wrapper_DestroyInstance(VkInstance _instance,
372369
pAllocator);
373370
}
374371

375-
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
376-
wrapper_GetInstanceProcAddr(VkInstance _instance,
372+
WRAPPER_GetInstanceProcAddr(VkInstance _instance,
377373
const char *pName)
378374
{
379375

0 commit comments

Comments
 (0)