Skip to content

Commit 8ad37e8

Browse files
authored
Merge pull request #121 from leegao/klark231-memmap
Fix addr_place mmap bug where the AHB query sometimes returns a sync-fence fd first instead of the memory fd, also add log wrapping to all wrapper functions.
2 parents a5f7b6e + 068afd7 commit 8ad37e8

13 files changed

+237
-72
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,25 @@ 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+
}
369+
370+
bool should_log_memory_debug() {
371+
return CHECK_FLAG("DEBUG_MEMORY");
372+
}

src/vulkan/wrapper/wrapper_debug.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ 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);
32+
33+
bool should_log_memory_debug(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"

0 commit comments

Comments
 (0)