Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 97 additions & 4 deletions src/vulkan/wrapper/vk_entrypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
wrapper_tramp_$name(
$decl_params)
{
int cmd_id = commands++;
int cmd_id = __wrapper_commands++;
struct temporary_objects temp;
list_inithead(&temp.objects);
$handle_unwrap_logic
Expand All @@ -64,6 +64,7 @@
return $return_block;
}""")


COMMAND_BLACKLIST = [
'FreeCommandBuffers', # implemented
'CreateDevice', # implemented
Expand Down Expand Up @@ -169,7 +170,7 @@ def _generate_trampoline(command, dispatch_table="device->dispatch_table"):
physical_device = "base->physical"

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

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

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

handle_unwrap_logic[idx] = f"WLOGA(\"{command.name}({', '.join(types)})\", {', '.join([p.name for p in params])});"
if command.name not in SPAMMY_COMMANDS:
handle_unwrap_logic[idx] = f" WLOGA(\"dispatch->{command.name}({', '.join(types)}) (id=%d)\", {', '.join([p.name for p in params])}, cmd_id);"
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);")

return TRAMPOLINE_TEMPLATE.substitute(
return_type=command.return_type,
Expand Down Expand Up @@ -402,6 +405,18 @@ def __init__(self, name):
def prefixed_name(self, prefix):
return prefix + '_' + self.name

WRAP_TEMPLATE = string.Template("""
VKAPI_ATTR $return_type VKAPI_CALL wrapper_$name($decl_params) {
int cmd_id = __wrapper_commands++;
WLOGT("wrapper->$name($type_params) (id=%d)", $call_params, cmd_id);
bool should_print = VK_CMD_CAN_TRACE;
$PRINT_PRE
$assign __wrapper_$name($call_params);
WLOGT("wrapper->$name $return_str (id=%d)", $return_fmt cmd_id);
$PRINT_POST
return $result;
}""")

class Entrypoint(EntrypointBase):
def __init__(self, name, return_type, params):
super(Entrypoint, self).__init__(name)
Expand Down Expand Up @@ -443,6 +458,84 @@ def trampoline(self):
return _generate_trampoline(self, dispatch_table="base->dispatch_table")
else:
return _generate_trampoline(self, dispatch_table="base->device->dispatch_table")

def wrap(self):
if self.alias:
return ""

call = [param.name for param in self.params]
types = []

for param in self.params:
is_input = param.num_pointers == 0 or param.is_const

if not is_input:
types.append(f"{param.name}: %p")
continue
is_wrapped = param.type in WrappedStructs
is_struct = param.type in TYPES
is_special = param.type in SPECIAL_TYPES

# Wrapped handles
if is_wrapped:
types.append(f"{param.name}: %p")
elif is_struct:
types.append(f"{param.name}: %p")
elif is_special:
types.append(f"{param.name}: %x")
else:
types.append(f"{param.name}: %x")
# 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);")
pre_print = []
pre_print.append(f" if (should_log) {{")
pre_print.append(f" VK_CMD_LOG(\"wrapper_{self.name} (id=%d)\", cmd_id);")
# pre_print += print_param(self, self.params[0], mode='input')
for param in self.params:
if self.name in COMMAND_BLACKLIST:
continue
is_input = param.num_pointers == 0 or param.is_const
if not is_input:
continue
pre_print += print_param(self, param, mode='input')
pre_print.append(f" VK_CMD_FLUSH();")
pre_print.append(f" }}")

post_print = []
post_print.append(f" if (should_log) {{")
if self.return_type == 'VkResult':
post_print += print_param(self, 'result', mode='output')
for param in self.params[1:]:
if self.name in COMMAND_BLACKLIST:
continue
is_input = param.num_pointers == 0 or param.is_const
if is_input:
continue
if self.return_type == 'VkResult':
post_print.append(f" if ({param.name} && result == VK_SUCCESS) {{")
else:
post_print.append(f" if ({param.name}) {{")
post_print += print_param(self, param, mode='output')
post_print.append(f" }} else {{")
post_print += [
f" VK_CMD_LOG_UNCONDITIONAL(\" out: *{param.name}: {param.type}* = %p\", {param.name});",
f" }}"
]
post_print.append(f" }}")


return WRAP_TEMPLATE.substitute(
return_type=self.return_type,
name=self.name,
assign=f"{self.return_type} result =" if self.return_type != "void" else "",
result=f"result" if self.return_type != "void" else "",
decl_params=self.decl_params(),
call_params=", ".join(call),
type_params=types,
return_str='returned %d' if self.return_type != 'void' else 'finished',
return_fmt='result,' if self.return_type != 'void' else '',
PRINT_PRE="\n".join(pre_print),
PRINT_POST="\n".join(post_print),
)

class EntrypointAlias(EntrypointBase):
def __init__(self, name, entrypoint):
Expand Down
2 changes: 1 addition & 1 deletion src/vulkan/wrapper/vk_unwrapper_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
'VkWin32SurfaceCreateInfoKHR',
# Too complex, and not needed
# 'VkDeviceCreateInfo', # 2d array of cstrings
'VkInstanceCreateInfo',
# 'VkInstanceCreateInfo',
'VkMicromapVersionInfoEXT', # latexmath
'VkPipelineMultisampleStateCreateInfo', # latexmath
'VkPipelineMultisampleStateCreateInfo', # latexmath
Expand Down
14 changes: 13 additions & 1 deletion src/vulkan/wrapper/vk_wrapper_trampolines_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@

extern struct wrapper_entry_masks wrapper_printer_masks;

_Atomic extern int __wrapper_commands;

% for e in entrypoints:
% if e.alias:
<% continue %>
Expand All @@ -75,6 +77,16 @@
#define has_device_wrapper_${e.name}(...) (wrapper_device_entrypoints.${e.name})
#define has_physical_device_wrapper_${e.name}(...) (wrapper_physical_device_entrypoints.${e.name})
#define name_of_wrapper_${e.name}(...) "wrapper_${e.name}"

#define WRAPPED_${e.name} ${"\\n".join([line + " \\\\" for line in e.wrap().split("\\n")])}

#define RETURN_${e.name} VKAPI_ATTR ${e.return_type} VKAPI_CALL
#define WRAPPER_NAME_${e.name}(...) ${e.name}
#define WRAPPER_${e.name}(...) \\\\\n
static RETURN_${e.name} __wrapper_${e.name}(__VA_ARGS__); \\\\\n
WRAPPED_${e.name} \\\\\n
static RETURN_${e.name} __wrapper_${e.name}(__VA_ARGS__)

% if e.guard is not None:
#else
#define TRY_${e.name}(TRUE, FALSE) FALSE
Expand Down Expand Up @@ -113,7 +125,7 @@
#include "vk_unwrappers.h"
#include "vk_printers.h"

_Atomic static int commands = 0;
_Atomic int __wrapper_commands = 0;

#define VK_PRINT_VkAccelerationStructureVersionInfoKHR(...)
#define VK_PRINT_VkAccelerationStructureBuildGeometryInfoKHR(...)
Expand Down
4 changes: 4 additions & 0 deletions src/vulkan/wrapper/wrapper_checks.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@
#define WCHECKV(call) {__W_WRAP__(call, { if(has_device_wrapper_##call) { __CHECKV__(wrapper_device_entrypoints. call); } else { CHECKV(call); }})}
#define WPCHECK(call) ({__W_WRAP__(call, has_physical_device_wrapper_##call ? __CHECK__(wrapper_physical_device_entrypoints. call) : PCHECK(call))})
#define WPCHECKV(call) {__W_WRAP__(call, { if(has_physical_device_wrapper_##call) { __CHECKV__(wrapper_physical_device_entrypoints. call); } else { PCHECKV(call); }})}

#define __WRAP(call) return call

#define __WRAPV(call) call
22 changes: 22 additions & 0 deletions src/vulkan/wrapper/wrapper_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,25 @@ bool use_compute_shader_mode() {

return g_use_compute_shader_mode;
}


bool use_wrapper_trace() {
static bool value;
static bool init;
if (init)
return value;
init = true;
const char* mask_bcn = getenv("WRAPPER_CMD_LOG_LEVEL");
if (!mask_bcn)
return value = false;

if (strstr(mask_bcn, "trace")) {
return value = true;
}

return value = false;
}

bool should_log_memory_debug() {
return CHECK_FLAG("DEBUG_MEMORY");
}
6 changes: 5 additions & 1 deletion src/vulkan/wrapper/wrapper_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ void initialize_cmd_print_masks(void);

bool use_image_view_mode(void);

bool use_compute_shader_mode(void);
bool use_compute_shader_mode(void);

bool use_wrapper_trace(void);

bool should_log_memory_debug(void);
1 change: 1 addition & 0 deletions src/vulkan/wrapper/wrapper_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "wrapper_trampolines.h"
#include "wrapper_debug.h"
#include "vk_unwrappers.h"
#include "vk_printers.h"
#include "spirv_edit.h"

#include "bcdec.h"
Expand Down
Loading
Loading