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
48 changes: 43 additions & 5 deletions layersvt/api_dump.h
Original file line number Diff line number Diff line change
Expand Up @@ -2423,25 +2423,63 @@ void dump_pointer_array(const T *array, size_t len, const ApiDumpSettings &setti
dump_array_end<Format>(array, len, settings, indents);
}

template <ApiDumpFormat Format, typename T, typename DumpElement>
void dump_double_pointer_array(const T *const *array, size_t len, const ApiDumpSettings &settings, const char *type_string,
const char *name, const char *element_type, int indents, DumpElement dump_element) {
// The loop shared by the arrays of pointers: the null check, the start and end of the array, and the separators between the
// elements of a JSON array. `dump_inner(i, name, indents)` dumps whatever array[i] points to, under the name `name`.
template <ApiDumpFormat Format, typename T, typename DumpInner>
void dump_double_pointer_array_impl(const T *const *array, size_t len, const ApiDumpSettings &settings, const char *type_string,
const char *name, int indents, DumpInner dump_inner) {
if (array == nullptr || len == 0) {
dump_nullptr<Format>(settings, type_string, name, indents);
return;
}
dump_array_start<Format>(array, len, settings, type_string, name, indents);
for (size_t i = 0; i < len; ++i) {
const ArrayElementName<Format> indexName(name, i);
dump_pointer<Format>(array[i], settings, element_type, indexName.c_str(), indents + (Format == ApiDumpFormat::Json ? 2 : 1),
dump_element);
dump_inner(i, indexName.c_str(), indents + (Format == ApiDumpFormat::Json ? 2 : 1));
if constexpr (Format == ApiDumpFormat::Json) {
dump_json_element_end(settings, !(i < len - 1));
}
}
dump_array_end<Format>(array, len, settings, indents);
}

// An array of pointers where each pointer refers to a single element
template <ApiDumpFormat Format, typename T, typename DumpElement>
void dump_double_pointer_array(const T *const *array, size_t len, const ApiDumpSettings &settings, const char *type_string,
const char *name, const char *element_type, int indents, DumpElement dump_element) {
dump_double_pointer_array_impl<Format>(array, len, settings, type_string, name, indents,
[&](size_t i, const char *element_name, int element_indents) {
dump_pointer<Format>(array[i], settings, element_type, element_name,
element_indents, dump_element);
});
}

// An array of `len` pointers where each pointer refers to its own array, whose length differs per element and is given by
// `inner_length(i)`. Used for parameters the registry can't describe fully, such as vkCmdBuildAccelerationStructuresKHR
// ppBuildRangeInfos, where ppBuildRangeInfos[i] points to pInfos[i].geometryCount structures. Unlike
// dump_double_pointer_array (one element per pointer), each element is dumped as an array of its own:
// [i] -> { "type": element_type, "name": "[i]", "address": ..., "elements": [ ... inner_length(i) elements ... ] }
// A null inner pointer is dumped as a null pointer. A non-null one whose length is zero (or not known) is dumped as an empty
// array, so its address is still shown and it isn't mistaken for a null pointer.
template <ApiDumpFormat Format, typename T, typename InnerLength, typename DumpElement>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this copies the whole body of dump_double_pointer_array (null check, array start/end, index-name loop, JSON element end) and changes only the per-element call. A shared outer-loop helper that takes a per-element callback would keep the two from drifting apart if the JSON comma or indentation handling changes later.

void dump_double_pointer_array_variable_length(const T *const *array, size_t len, InnerLength inner_length,
const ApiDumpSettings &settings, const char *type_string, const char *name,
const char *element_type, const char *inner_element_type, int indents,
DumpElement dump_element) {
dump_double_pointer_array_impl<Format>(
array, len, settings, type_string, name, indents, [&](size_t i, const char *element_name, int element_indents) {
const T *inner = array[i];
const size_t inner_len = inner_length(i);
if (inner != nullptr && inner_len == 0) {
dump_array_start<Format>(inner, 0, settings, element_type, element_name, element_indents);
dump_array_end<Format>(inner, 0, settings, element_indents);
} else {
dump_pointer_array<Format>(inner, inner_len, settings, element_type, element_name, inner_element_type,
element_indents, dump_element);
}
});
}

// Designed to be viewed in something like https://www.khronos.org/spir/visualizer/
// There is also now support in SPIRV-Tools (https://github.com/KhronosGroup/SPIRV-Tools/pull/5870) to consume the array
template <ApiDumpFormat Format, typename T>
Expand Down
6 changes: 3 additions & 3 deletions layersvt/generated/api_dump_implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -70344,7 +70344,7 @@ void dump_params_vkCmdBuildAccelerationStructuresKHR(ApiDumpInstance& dump_inst,
dump_separate_members<Format>(settings);
dump_pointer_array<Format>(pInfos, infoCount, settings, "const VkAccelerationStructureBuildGeometryInfoKHR*", "pInfos", "const VkAccelerationStructureBuildGeometryInfoKHR", (Format == ApiDumpFormat::Json ? 4 : 1), dump_VkAccelerationStructureBuildGeometryInfoKHR<Format>);
dump_separate_members<Format>(settings);
dump_double_pointer_array<Format>(ppBuildRangeInfos, infoCount, settings, "const VkAccelerationStructureBuildRangeInfoKHR* const*", "ppBuildRangeInfos", "const VkAccelerationStructureBuildRangeInfoKHR*", (Format == ApiDumpFormat::Json ? 4 : 1), dump_VkAccelerationStructureBuildRangeInfoKHR<Format>);
dump_double_pointer_array_variable_length<Format>(ppBuildRangeInfos, infoCount, [&](size_t i) -> size_t { return pInfos ? pInfos[i].geometryCount : 0; }, settings, "const VkAccelerationStructureBuildRangeInfoKHR* const*", "ppBuildRangeInfos", "const VkAccelerationStructureBuildRangeInfoKHR*", "const VkAccelerationStructureBuildRangeInfoKHR", (Format == ApiDumpFormat::Json ? 4 : 1), dump_VkAccelerationStructureBuildRangeInfoKHR<Format>);
dump_post_params_formatting<Format>(settings);
flush(settings);
}
Expand All @@ -70365,7 +70365,7 @@ void dump_params_vkCmdBuildAccelerationStructuresIndirectKHR(ApiDumpInstance& du
dump_separate_members<Format>(settings);
dump_pointer_array<Format>(pIndirectStrides, infoCount, settings, "const uint32_t*", "pIndirectStrides", "const uint32_t", (Format == ApiDumpFormat::Json ? 4 : 1), dump_type<Format, const uint32_t>);
dump_separate_members<Format>(settings);
dump_pointer_array<Format>(ppMaxPrimitiveCounts, infoCount, settings, "const uint32_t* const*", "ppMaxPrimitiveCounts", "const uint32_t*", (Format == ApiDumpFormat::Json ? 4 : 1), dump_type<Format, const uint32_t*>);
dump_double_pointer_array_variable_length<Format>(ppMaxPrimitiveCounts, infoCount, [&](size_t i) -> size_t { return pInfos ? pInfos[i].geometryCount : 0; }, settings, "const uint32_t* const*", "ppMaxPrimitiveCounts", "const uint32_t*", "const uint32_t", (Format == ApiDumpFormat::Json ? 4 : 1), dump_type<Format, const uint32_t>);
dump_post_params_formatting<Format>(settings);
flush(settings);
}
Expand All @@ -70384,7 +70384,7 @@ void dump_params_vkBuildAccelerationStructuresKHR(ApiDumpInstance& dump_inst, Vk
dump_separate_members<Format>(settings);
dump_pointer_array<Format>(pInfos, infoCount, settings, "const VkAccelerationStructureBuildGeometryInfoKHR*", "pInfos", "const VkAccelerationStructureBuildGeometryInfoKHR", (Format == ApiDumpFormat::Json ? 4 : 1), dump_VkAccelerationStructureBuildGeometryInfoKHR<Format>);
dump_separate_members<Format>(settings);
dump_double_pointer_array<Format>(ppBuildRangeInfos, infoCount, settings, "const VkAccelerationStructureBuildRangeInfoKHR* const*", "ppBuildRangeInfos", "const VkAccelerationStructureBuildRangeInfoKHR*", (Format == ApiDumpFormat::Json ? 4 : 1), dump_VkAccelerationStructureBuildRangeInfoKHR<Format>);
dump_double_pointer_array_variable_length<Format>(ppBuildRangeInfos, infoCount, [&](size_t i) -> size_t { return pInfos ? pInfos[i].geometryCount : 0; }, settings, "const VkAccelerationStructureBuildRangeInfoKHR* const*", "ppBuildRangeInfos", "const VkAccelerationStructureBuildRangeInfoKHR*", "const VkAccelerationStructureBuildRangeInfoKHR", (Format == ApiDumpFormat::Json ? 4 : 1), dump_VkAccelerationStructureBuildRangeInfoKHR<Format>);
dump_post_params_formatting<Format>(settings);
flush(settings);
}
Expand Down
33 changes: 32 additions & 1 deletion scripts/generators/api_dump_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,23 @@
}
}

# ppBuildRangeInfos[i] and ppMaxPrimitiveCounts[i] each point to pInfos[i].geometryCount elements. When pInfos is null that
# length isn't known, so it is 0 and only the pointer is dumped, rather than reading through one that may not be valid.
GEOMETRY_COUNT_OF_INFO = 'pInfos ? pInfos[i].geometryCount : 0'

# Command parameters that are arrays of pointers (T* const*) whose inner arrays have a length of their own, which the
# registry has no way to express (it only gives the length of the outer array). Maps parameter name -> C++ expression for
# the length of inner array 'i' (in terms of the command's other parameters). These are dumped with
# dump_double_pointer_array_variable_length; dump_double_pointer_array would only dump the first element each pointer
# refers to.
# This is keyed by parameter name, not command, so any alias or promotion of a command with such a parameter is covered.
# Every entry has to match at least one command parameter, which generate_implementation checks, so one that stops
# matching (for example after a rename in the registry) fails the generator instead of silently truncating the output.
SPECIAL_INNER_LENGTH = {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vkCmdBuildAccelerationStructuresIndirectKHR::ppMaxPrimitiveCounts has the same layout: ppMaxPrimitiveCounts[i] points to pInfos[i].geometryCount uint32_t values. It isn't in this table, so the generated code (api_dump_implementation.h:70368) still uses dump_pointer_array with dump_type<Format, const uint32_t*>. That prints only infoCount pointer addresses and none of the counts, so the indirect build path keeps the truncation.

Note: the element type is uint32_t, not a Vulkan-defined struct. The new branch would need to pass something like dump_type<Format, uint32_t> as the element dumper instead of the pointer-typed one.

'ppBuildRangeInfos': GEOMETRY_COUNT_OF_INFO,
'ppMaxPrimitiveCounts': GEOMETRY_COUNT_OF_INFO,
}

HANDWRITTEN_FUNCTIONS = ['vkCreateInstance',
'vkCreateDevice',
'vkEnumerateInstanceExtensionProperties',
Expand Down Expand Up @@ -334,6 +351,7 @@ def __init__(self):
self.return_types = set()
self.vulkan_defined_types = set()
self.only_use_as_pointer_types = set()
self.used_inner_length = set()

def generate(self):
self.build_return_types()
Expand Down Expand Up @@ -820,6 +838,10 @@ def generate_implementation(self):
}''')
protect.add_guard(self, None)

unused_inner_length = set(SPECIAL_INNER_LENGTH) - self.used_inner_length
if unused_inner_length:
raise RuntimeError(f'SPECIAL_INNER_LENGTH entries match no command parameter: {sorted(unused_inner_length)}')

self.write('\n//========================== Type Implementations =========================//\n')
self.write('''
template <ApiDumpFormat Format>
Expand Down Expand Up @@ -976,7 +998,16 @@ def write_value(self, var, parent):

else:
array_len = get_array_length(var, parent)
if var.fullType.count('*') > 1 and var.type not in ['void', 'char'] and (call_type in self.vulkan_defined_types):
if isinstance(parent, Command) and var.name in SPECIAL_INNER_LENGTH:
if not any(param.name == 'pInfos' for param in parent.params):
raise RuntimeError(f'{parent.name} {var.name}: its inner length is in terms of pInfos, which it does not have')
self.used_inner_length.add(var.name)
inner_length = SPECIAL_INNER_LENGTH[var.name]
inner_element_type = custom_type.rstrip('*')
# Structs are dumped by their own function, anything else (such as the uint32_t of ppMaxPrimitiveCounts) as a plain value
inner_element = element_type if call_type in self.vulkan_defined_types else f'dump_type<Format, {inner_element_type}>'
self.write(f'dump_double_pointer_array_variable_length<Format>({value}, {array_len}, [&](size_t i) -> size_t {{ return {inner_length}; }}, settings, "{custom_fullType}", "{var.name}", "{custom_type}", "{inner_element_type}", {indent}, {inner_element});')
elif var.fullType.count('*') > 1 and var.type not in ['void', 'char'] and (call_type in self.vulkan_defined_types):
self.write(f'dump_double_pointer_array<Format>({value}, {array_len}, settings, "{custom_fullType}", "{var.name}", "{custom_type}", {indent}, {element_type});')
else:
self.write(f'dump_pointer_array<Format>({value}, {array_len}, settings, "{custom_fullType}", "{var.name}", "{custom_type}", {indent}, {element_type});')
Expand Down
Loading