|
| 1 | +/* Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 the "License"; |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#pragma once |
| 19 | + |
| 20 | +#include <any> |
| 21 | +#include <vulkan/vulkan.hpp> |
| 22 | + |
| 23 | +namespace vkb |
| 24 | +{ |
| 25 | +template <vkb::BindingType bindingType, typename AnchorStructType> |
| 26 | +class StructureChainBuilder |
| 27 | +{ |
| 28 | + public: |
| 29 | + StructureChainBuilder(); |
| 30 | + |
| 31 | + template <typename T> |
| 32 | + T &add_chain_data(T const &data_to_add = {}); // Adds data to the structure chain builder that is not part of the structure chain itself, but is used by the |
| 33 | + // structures in the chain (e.g. pointed to by a member of a struct in the structure chain) |
| 34 | + |
| 35 | + template <typename StructType> |
| 36 | + StructType &add_struct(StructType const &struct_to_add = {}); |
| 37 | + |
| 38 | + template <typename StructType> |
| 39 | + StructType const *get_struct(size_t skip = 0) const; |
| 40 | + |
| 41 | + void set_anchor_struct(AnchorStructType const &anchor_struct); |
| 42 | + |
| 43 | + private: |
| 44 | + template <typename StructType> |
| 45 | + StructType &add_struct_impl(StructType const &struct_to_add); |
| 46 | + template <typename StructType> |
| 47 | + StructType const *get_struct_impl(size_t skip) const; |
| 48 | + void set_anchor_struct_impl(AnchorStructType const &anchor_struct); |
| 49 | + |
| 50 | + private: |
| 51 | + std::vector<std::unique_ptr<std::any>> structure_chain; |
| 52 | + std::vector<std::unique_ptr<std::any>> chain_data; // data used with the structure chain, stored separately to ensure correct destruction order (the structs |
| 53 | + // in the structure chain may contain pointers to data owned by the chain_data) |
| 54 | +}; |
| 55 | + |
| 56 | +template <typename AnchorStructType> |
| 57 | +using StructureChainBuilderC = StructureChainBuilder<vkb::BindingType::C, AnchorStructType>; |
| 58 | +template <typename AnchorStructType> |
| 59 | +using StructureChainBuilderCpp = StructureChainBuilder<vkb::BindingType::Cpp, AnchorStructType>; |
| 60 | + |
| 61 | +template <vkb::BindingType bindingType, typename AnchorStructType> |
| 62 | +template <typename T> |
| 63 | +T &StructureChainBuilder<bindingType, AnchorStructType>::add_chain_data(T const &data_to_add) |
| 64 | +{ |
| 65 | + chain_data.push_back(std::make_unique<std::any>(std::make_any<T>(data_to_add))); |
| 66 | + return *std::any_cast<T>(chain_data.back().get()); |
| 67 | +} |
| 68 | + |
| 69 | +template <vkb::BindingType bindingType, typename AnchorStructType> |
| 70 | +inline StructureChainBuilder<bindingType, AnchorStructType>::StructureChainBuilder() |
| 71 | +{ |
| 72 | + static_assert((offsetof(AnchorStructType, sType) == 0) && (offsetof(AnchorStructType, pNext) == sizeof(void *))); |
| 73 | + structure_chain.push_back(std::make_unique<std::any>(std::make_any<AnchorStructType>())); |
| 74 | +} |
| 75 | + |
| 76 | +template <vkb::BindingType bindingType, typename AnchorStructType> |
| 77 | +template <typename StructType> |
| 78 | +inline StructType &StructureChainBuilder<bindingType, AnchorStructType>::add_struct(StructType const &struct_to_add) |
| 79 | +{ |
| 80 | + if constexpr (bindingType == vkb::BindingType::Cpp) |
| 81 | + { |
| 82 | + return add_struct_impl(struct_to_add); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + return reinterpret_cast<StructureChainBuilder<vkb::BindingType::Cpp, typename vk::CppType<AnchorStructType>::Type> *>(this)->add_struct( |
| 87 | + reinterpret_cast<typename vk::CppType<StructType>::Type const &>(struct_to_add)); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +template <vkb::BindingType bindingType, typename AnchorStructType> |
| 92 | +template <typename StructType> |
| 93 | +inline StructType &StructureChainBuilder<bindingType, AnchorStructType>::add_struct_impl(StructType const &struct_to_add) |
| 94 | +{ |
| 95 | +#if !defined(NDEBUG) |
| 96 | + auto it = std::ranges::find_if(structure_chain, [](auto const &chain_element) { |
| 97 | + return std::any_cast<StructType>(chain_element.get()) != nullptr; |
| 98 | + }); |
| 99 | + assert(it == structure_chain.end() || StructType::allowDuplicate); // If the struct type already exists in the structure chain, it must allow duplicates |
| 100 | +#endif |
| 101 | + |
| 102 | + structure_chain.push_back(std::make_unique<std::any>(std::make_any<StructType>(struct_to_add))); |
| 103 | + std::any_cast<StructType>(structure_chain.back().get())->pNext = std::any_cast<AnchorStructType>(structure_chain.front().get())->pNext; |
| 104 | + std::any_cast<AnchorStructType>(structure_chain.front().get())->pNext = std::any_cast<StructType>(structure_chain.back().get()); |
| 105 | + return *std::any_cast<StructType>(structure_chain.back().get()); |
| 106 | +} |
| 107 | + |
| 108 | +template <vkb::BindingType bindingType, typename AnchorStructType> |
| 109 | +template <typename StructType> |
| 110 | +inline StructType const *StructureChainBuilder<bindingType, AnchorStructType>::get_struct(size_t skip) const |
| 111 | +{ |
| 112 | + if constexpr (bindingType == vkb::BindingType::Cpp) |
| 113 | + { |
| 114 | + return get_struct_impl<StructType>(skip); |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + return reinterpret_cast<StructType const *>(get_struct_impl<vk::CppType<StructType>::Type>(skip)); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +template <vkb::BindingType bindingType, typename AnchorStructType> |
| 123 | +template <typename StructType> |
| 124 | +inline StructType const *StructureChainBuilder<bindingType, AnchorStructType>::get_struct_impl(size_t skip) const |
| 125 | +{ |
| 126 | + assert(!skip || StructType::allowDuplicate); // If skip is non-zero, the struct type must allow duplicates in the structure chain |
| 127 | + |
| 128 | + auto it = std::ranges::find_if(structure_chain, [](auto const &chain_element) { |
| 129 | + return std::any_cast<StructType>(chain_element.get()) != nullptr; |
| 130 | + }); |
| 131 | + for (size_t i = 0; (i < skip) && (it != structure_chain.end()); ++i) |
| 132 | + { |
| 133 | + it = std::find_if(std::next(it), structure_chain.end(), [](auto const &chain_element) { |
| 134 | + return std::any_cast<StructType>(chain_element.get()) != nullptr; |
| 135 | + }); |
| 136 | + } |
| 137 | + return (it != structure_chain.end()) ? std::any_cast<StructType>(it->get()) : nullptr; |
| 138 | +} |
| 139 | + |
| 140 | +template <vkb::BindingType bindingType, typename AnchorStructType> |
| 141 | +inline void StructureChainBuilder<bindingType, AnchorStructType>::set_anchor_struct(AnchorStructType const &anchor_struct) |
| 142 | +{ |
| 143 | + if constexpr (bindingType == vkb::BindingType::Cpp) |
| 144 | + { |
| 145 | + set_anchor_struct_impl(anchor_struct); |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + return reinterpret_cast<StructureChainBuilder<vkb::BindingType::Cpp, typename vk::CppType<AnchorStructType>::Type> *>(this)->add_anchor_struct( |
| 150 | + reinterpret_cast<typename vk::CppType<AnchorStructType>::Type const &>(anchor_struct)); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +template <vkb::BindingType bindingType, typename AnchorStructType> |
| 155 | +inline void StructureChainBuilder<bindingType, AnchorStructType>::set_anchor_struct_impl(AnchorStructType const &anchor_struct) |
| 156 | +{ |
| 157 | + void const *pNext = std::any_cast<AnchorStructType>(structure_chain.front().get())->pNext; |
| 158 | + *std::any_cast<AnchorStructType>(structure_chain.front().get()) = anchor_struct; |
| 159 | + std::any_cast<AnchorStructType>(structure_chain.front().get())->pNext = pNext; |
| 160 | +} |
| 161 | + |
| 162 | +} // namespace vkb |
0 commit comments