From b535eda82c27b29a9b9ee55ea96875d878b8209a Mon Sep 17 00:00:00 2001 From: Chris Steinberg Date: Wed, 4 Nov 2026 15:59:16 -0600 Subject: [PATCH 1/2] Update acceleration structure range info parsing Avoids truncation of acceleration structure build's range info double pointer array by using a special length helper to know how to walk the inner loop of the array structure. Does involve a schema change which will need to be handled by integrating clients. --- layersvt/api_dump.h | 27 ++++++++++++++++++++ layersvt/generated/api_dump_implementation.h | 4 +-- scripts/generators/api_dump_generator.py | 20 ++++++++++++++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/layersvt/api_dump.h b/layersvt/api_dump.h index 9071e61edf..7d8425443d 100644 --- a/layersvt/api_dump.h +++ b/layersvt/api_dump.h @@ -2442,6 +2442,33 @@ void dump_double_pointer_array(const T *const *array, size_t len, const ApiDumpS dump_array_end(array, len, settings, indents); } +// 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 struct per pointer), each element is dumped as an array of its own: +// [i] -> { "type": element_type, "name": "[i]", "address": ..., "elements": [ ... inner_length(i) structs ... ] } +// A null inner pointer, or an inner length of zero, is dumped as a null pointer. +template +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) { + if (array == nullptr || len == 0) { + dump_nullptr(settings, type_string, name, indents); + return; + } + dump_array_start(array, len, settings, type_string, name, indents); + for (size_t i = 0; i < len; ++i) { + const ArrayElementName indexName(name, i); + dump_pointer_array(array[i], static_cast(inner_length(i)), settings, element_type, indexName.c_str(), + inner_element_type, indents + (Format == ApiDumpFormat::Json ? 2 : 1), dump_element); + if constexpr (Format == ApiDumpFormat::Json) { + dump_json_element_end(settings, !(i < len - 1)); + } + } + dump_array_end(array, len, settings, indents); +} + // 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 diff --git a/layersvt/generated/api_dump_implementation.h b/layersvt/generated/api_dump_implementation.h index a9e92099df..22ac083422 100644 --- a/layersvt/generated/api_dump_implementation.h +++ b/layersvt/generated/api_dump_implementation.h @@ -70344,7 +70344,7 @@ void dump_params_vkCmdBuildAccelerationStructuresKHR(ApiDumpInstance& dump_inst, dump_separate_members(settings); dump_pointer_array(pInfos, infoCount, settings, "const VkAccelerationStructureBuildGeometryInfoKHR*", "pInfos", "const VkAccelerationStructureBuildGeometryInfoKHR", (Format == ApiDumpFormat::Json ? 4 : 1), dump_VkAccelerationStructureBuildGeometryInfoKHR); dump_separate_members(settings); - dump_double_pointer_array(ppBuildRangeInfos, infoCount, settings, "const VkAccelerationStructureBuildRangeInfoKHR* const*", "ppBuildRangeInfos", "const VkAccelerationStructureBuildRangeInfoKHR*", (Format == ApiDumpFormat::Json ? 4 : 1), dump_VkAccelerationStructureBuildRangeInfoKHR); + dump_double_pointer_array_variable_length(ppBuildRangeInfos, infoCount, [&](size_t i) -> size_t { return pInfos ? pInfos[i].geometryCount : 1; }, settings, "const VkAccelerationStructureBuildRangeInfoKHR* const*", "ppBuildRangeInfos", "const VkAccelerationStructureBuildRangeInfoKHR*", "const VkAccelerationStructureBuildRangeInfoKHR", (Format == ApiDumpFormat::Json ? 4 : 1), dump_VkAccelerationStructureBuildRangeInfoKHR); dump_post_params_formatting(settings); flush(settings); } @@ -70384,7 +70384,7 @@ void dump_params_vkBuildAccelerationStructuresKHR(ApiDumpInstance& dump_inst, Vk dump_separate_members(settings); dump_pointer_array(pInfos, infoCount, settings, "const VkAccelerationStructureBuildGeometryInfoKHR*", "pInfos", "const VkAccelerationStructureBuildGeometryInfoKHR", (Format == ApiDumpFormat::Json ? 4 : 1), dump_VkAccelerationStructureBuildGeometryInfoKHR); dump_separate_members(settings); - dump_double_pointer_array(ppBuildRangeInfos, infoCount, settings, "const VkAccelerationStructureBuildRangeInfoKHR* const*", "ppBuildRangeInfos", "const VkAccelerationStructureBuildRangeInfoKHR*", (Format == ApiDumpFormat::Json ? 4 : 1), dump_VkAccelerationStructureBuildRangeInfoKHR); + dump_double_pointer_array_variable_length(ppBuildRangeInfos, infoCount, [&](size_t i) -> size_t { return pInfos ? pInfos[i].geometryCount : 1; }, settings, "const VkAccelerationStructureBuildRangeInfoKHR* const*", "ppBuildRangeInfos", "const VkAccelerationStructureBuildRangeInfoKHR*", "const VkAccelerationStructureBuildRangeInfoKHR", (Format == ApiDumpFormat::Json ? 4 : 1), dump_VkAccelerationStructureBuildRangeInfoKHR); dump_post_params_formatting(settings); flush(settings); } diff --git a/scripts/generators/api_dump_generator.py b/scripts/generators/api_dump_generator.py index 9229596d1c..16088f3fbc 100644 --- a/scripts/generators/api_dump_generator.py +++ b/scripts/generators/api_dump_generator.py @@ -193,6 +193,20 @@ } } +# Arrays of pointers (T* const*) whose inner arrays have their own length, which the registry has no way to express +# (it only gives the length of the outer array). Maps command -> parameter -> C++ expression for the length of inner +# array 'i'. These are dumped with dump_double_pointer_array_variable_length instead of dump_double_pointer_array, +# which would only dump the first element each pointer refers to. +SPECIAL_INNER_LENGTH = { + 'vkCmdBuildAccelerationStructuresKHR': { + # ppBuildRangeInfos[i] points to pInfos[i].geometryCount VkAccelerationStructureBuildRangeInfoKHR + 'ppBuildRangeInfos': 'pInfos ? pInfos[i].geometryCount : 1', + }, + 'vkBuildAccelerationStructuresKHR': { + 'ppBuildRangeInfos': 'pInfos ? pInfos[i].geometryCount : 1', + }, +} + HANDWRITTEN_FUNCTIONS = ['vkCreateInstance', 'vkCreateDevice', 'vkEnumerateInstanceExtensionProperties', @@ -976,7 +990,11 @@ 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 parent.name in SPECIAL_INNER_LENGTH and var.name in SPECIAL_INNER_LENGTH[parent.name]: + inner_length = SPECIAL_INNER_LENGTH[parent.name][var.name] + inner_element_type = custom_type.rstrip('*') + self.write(f'dump_double_pointer_array_variable_length({value}, {array_len}, [&](size_t i) -> size_t {{ return {inner_length}; }}, settings, "{custom_fullType}", "{var.name}", "{custom_type}", "{inner_element_type}", {indent}, {element_type});') + 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({value}, {array_len}, settings, "{custom_fullType}", "{var.name}", "{custom_type}", {indent}, {element_type});') else: self.write(f'dump_pointer_array({value}, {array_len}, settings, "{custom_fullType}", "{var.name}", "{custom_type}", {indent}, {element_type});') From 653e7ebea4cf12ede83bc8ef881c16fe08db7c4a Mon Sep 17 00:00:00 2001 From: Chris Steinberg Date: Wed, 4 Nov 2026 17:19:53 -0600 Subject: [PATCH 2/2] pr feedback --- layersvt/api_dump.h | 55 ++++++++++++-------- layersvt/generated/api_dump_implementation.h | 6 +-- scripts/generators/api_dump_generator.py | 41 ++++++++++----- 3 files changed, 63 insertions(+), 39 deletions(-) diff --git a/layersvt/api_dump.h b/layersvt/api_dump.h index 7d8425443d..8456e4ff67 100644 --- a/layersvt/api_dump.h +++ b/layersvt/api_dump.h @@ -2423,9 +2423,11 @@ void dump_pointer_array(const T *array, size_t len, const ApiDumpSettings &setti dump_array_end(array, len, settings, indents); } -template -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 +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(settings, type_string, name, indents); return; @@ -2433,8 +2435,7 @@ void dump_double_pointer_array(const T *const *array, size_t len, const ApiDumpS dump_array_start(array, len, settings, type_string, name, indents); for (size_t i = 0; i < len; ++i) { const ArrayElementName indexName(name, i); - dump_pointer(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)); } @@ -2442,31 +2443,41 @@ void dump_double_pointer_array(const T *const *array, size_t len, const ApiDumpS dump_array_end(array, len, settings, indents); } +// An array of pointers where each pointer refers to a single element +template +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(array, len, settings, type_string, name, indents, + [&](size_t i, const char *element_name, int element_indents) { + dump_pointer(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 struct per pointer), each element is dumped as an array of its own: -// [i] -> { "type": element_type, "name": "[i]", "address": ..., "elements": [ ... inner_length(i) structs ... ] } -// A null inner pointer, or an inner length of zero, is dumped as a null pointer. +// 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 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) { - if (array == nullptr || len == 0) { - dump_nullptr(settings, type_string, name, indents); - return; - } - dump_array_start(array, len, settings, type_string, name, indents); - for (size_t i = 0; i < len; ++i) { - const ArrayElementName indexName(name, i); - dump_pointer_array(array[i], static_cast(inner_length(i)), settings, element_type, indexName.c_str(), - inner_element_type, indents + (Format == ApiDumpFormat::Json ? 2 : 1), dump_element); - if constexpr (Format == ApiDumpFormat::Json) { - dump_json_element_end(settings, !(i < len - 1)); - } - } - dump_array_end(array, len, settings, indents); + dump_double_pointer_array_impl( + 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(inner, 0, settings, element_type, element_name, element_indents); + dump_array_end(inner, 0, settings, element_indents); + } else { + dump_pointer_array(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/ diff --git a/layersvt/generated/api_dump_implementation.h b/layersvt/generated/api_dump_implementation.h index 22ac083422..854601ff5e 100644 --- a/layersvt/generated/api_dump_implementation.h +++ b/layersvt/generated/api_dump_implementation.h @@ -70344,7 +70344,7 @@ void dump_params_vkCmdBuildAccelerationStructuresKHR(ApiDumpInstance& dump_inst, dump_separate_members(settings); dump_pointer_array(pInfos, infoCount, settings, "const VkAccelerationStructureBuildGeometryInfoKHR*", "pInfos", "const VkAccelerationStructureBuildGeometryInfoKHR", (Format == ApiDumpFormat::Json ? 4 : 1), dump_VkAccelerationStructureBuildGeometryInfoKHR); dump_separate_members(settings); - dump_double_pointer_array_variable_length(ppBuildRangeInfos, infoCount, [&](size_t i) -> size_t { return pInfos ? pInfos[i].geometryCount : 1; }, settings, "const VkAccelerationStructureBuildRangeInfoKHR* const*", "ppBuildRangeInfos", "const VkAccelerationStructureBuildRangeInfoKHR*", "const VkAccelerationStructureBuildRangeInfoKHR", (Format == ApiDumpFormat::Json ? 4 : 1), dump_VkAccelerationStructureBuildRangeInfoKHR); + dump_double_pointer_array_variable_length(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); dump_post_params_formatting(settings); flush(settings); } @@ -70365,7 +70365,7 @@ void dump_params_vkCmdBuildAccelerationStructuresIndirectKHR(ApiDumpInstance& du dump_separate_members(settings); dump_pointer_array(pIndirectStrides, infoCount, settings, "const uint32_t*", "pIndirectStrides", "const uint32_t", (Format == ApiDumpFormat::Json ? 4 : 1), dump_type); dump_separate_members(settings); - dump_pointer_array(ppMaxPrimitiveCounts, infoCount, settings, "const uint32_t* const*", "ppMaxPrimitiveCounts", "const uint32_t*", (Format == ApiDumpFormat::Json ? 4 : 1), dump_type); + dump_double_pointer_array_variable_length(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); dump_post_params_formatting(settings); flush(settings); } @@ -70384,7 +70384,7 @@ void dump_params_vkBuildAccelerationStructuresKHR(ApiDumpInstance& dump_inst, Vk dump_separate_members(settings); dump_pointer_array(pInfos, infoCount, settings, "const VkAccelerationStructureBuildGeometryInfoKHR*", "pInfos", "const VkAccelerationStructureBuildGeometryInfoKHR", (Format == ApiDumpFormat::Json ? 4 : 1), dump_VkAccelerationStructureBuildGeometryInfoKHR); dump_separate_members(settings); - dump_double_pointer_array_variable_length(ppBuildRangeInfos, infoCount, [&](size_t i) -> size_t { return pInfos ? pInfos[i].geometryCount : 1; }, settings, "const VkAccelerationStructureBuildRangeInfoKHR* const*", "ppBuildRangeInfos", "const VkAccelerationStructureBuildRangeInfoKHR*", "const VkAccelerationStructureBuildRangeInfoKHR", (Format == ApiDumpFormat::Json ? 4 : 1), dump_VkAccelerationStructureBuildRangeInfoKHR); + dump_double_pointer_array_variable_length(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); dump_post_params_formatting(settings); flush(settings); } diff --git a/scripts/generators/api_dump_generator.py b/scripts/generators/api_dump_generator.py index 16088f3fbc..bf58e61570 100644 --- a/scripts/generators/api_dump_generator.py +++ b/scripts/generators/api_dump_generator.py @@ -193,18 +193,21 @@ } } -# Arrays of pointers (T* const*) whose inner arrays have their own length, which the registry has no way to express -# (it only gives the length of the outer array). Maps command -> parameter -> C++ expression for the length of inner -# array 'i'. These are dumped with dump_double_pointer_array_variable_length instead of dump_double_pointer_array, -# which would only dump the first element each pointer refers to. +# 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 = { - 'vkCmdBuildAccelerationStructuresKHR': { - # ppBuildRangeInfos[i] points to pInfos[i].geometryCount VkAccelerationStructureBuildRangeInfoKHR - 'ppBuildRangeInfos': 'pInfos ? pInfos[i].geometryCount : 1', - }, - 'vkBuildAccelerationStructuresKHR': { - 'ppBuildRangeInfos': 'pInfos ? pInfos[i].geometryCount : 1', - }, + 'ppBuildRangeInfos': GEOMETRY_COUNT_OF_INFO, + 'ppMaxPrimitiveCounts': GEOMETRY_COUNT_OF_INFO, } HANDWRITTEN_FUNCTIONS = ['vkCreateInstance', @@ -348,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() @@ -834,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 @@ -990,10 +998,15 @@ def write_value(self, var, parent): else: array_len = get_array_length(var, parent) - if parent.name in SPECIAL_INNER_LENGTH and var.name in SPECIAL_INNER_LENGTH[parent.name]: - inner_length = SPECIAL_INNER_LENGTH[parent.name][var.name] + 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('*') - self.write(f'dump_double_pointer_array_variable_length({value}, {array_len}, [&](size_t i) -> size_t {{ return {inner_length}; }}, settings, "{custom_fullType}", "{var.name}", "{custom_type}", "{inner_element_type}", {indent}, {element_type});') + # 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' + self.write(f'dump_double_pointer_array_variable_length({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({value}, {array_len}, settings, "{custom_fullType}", "{var.name}", "{custom_type}", {indent}, {element_type});') else: