1 #ifndef _VKBUILDERUTIL_HPP 2 #define _VKBUILDERUTIL_HPP 3 /*------------------------------------------------------------------------- 4 * Vulkan CTS Framework 5 * -------------------- 6 * 7 * Copyright (c) 2015 Google Inc. 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * 21 *//*! 22 * \file 23 * \brief Vulkan object builder utilities. 24 *//*--------------------------------------------------------------------*/ 25 26 #include "vkDefs.hpp" 27 #include "vkRef.hpp" 28 29 #include <vector> 30 31 namespace vk 32 { 33 34 class DescriptorSetLayoutBuilder 35 { 36 public: 37 DescriptorSetLayoutBuilder(void); 38 39 DescriptorSetLayoutBuilder &addBinding(VkDescriptorType descriptorType, uint32_t descriptorCount, 40 VkShaderStageFlags stageFlags, const VkSampler *pImmutableSamplers); 41 42 DescriptorSetLayoutBuilder &addIndexedBinding(VkDescriptorType descriptorType, uint32_t descriptorCount, 43 VkShaderStageFlags stageFlags, uint32_t dstBinding, 44 const VkSampler *pImmutableSamplers); 45 46 Move<VkDescriptorSetLayout> build(const DeviceInterface &vk, VkDevice device, 47 VkDescriptorSetLayoutCreateFlags extraFlags = 0) const; 48 49 // helpers 50 addSingleBinding(VkDescriptorType descriptorType,VkShaderStageFlags stageFlags)51 inline DescriptorSetLayoutBuilder &addSingleBinding(VkDescriptorType descriptorType, VkShaderStageFlags stageFlags) 52 { 53 return addBinding(descriptorType, 1u, stageFlags, (VkSampler *)DE_NULL); 54 } addSingleIndexedBinding(VkDescriptorType descriptorType,VkShaderStageFlags stageFlags,uint32_t dstBinding)55 inline DescriptorSetLayoutBuilder &addSingleIndexedBinding(VkDescriptorType descriptorType, 56 VkShaderStageFlags stageFlags, uint32_t dstBinding) 57 { 58 return addIndexedBinding(descriptorType, 1u, stageFlags, dstBinding, (VkSampler *)DE_NULL); 59 } addArrayBinding(VkDescriptorType descriptorType,uint32_t descriptorCount,VkShaderStageFlags stageFlags)60 inline DescriptorSetLayoutBuilder &addArrayBinding(VkDescriptorType descriptorType, uint32_t descriptorCount, 61 VkShaderStageFlags stageFlags) 62 { 63 return addBinding(descriptorType, descriptorCount, stageFlags, (VkSampler *)DE_NULL); 64 } addSingleSamplerBinding(VkDescriptorType descriptorType,VkShaderStageFlags stageFlags,const VkSampler * immutableSampler)65 inline DescriptorSetLayoutBuilder &addSingleSamplerBinding( 66 VkDescriptorType descriptorType, VkShaderStageFlags stageFlags, 67 const VkSampler *immutableSampler) //!< \note: Using pointer to sampler to clarify that handle is not 68 //!< copied and argument lifetime is expected to cover build() 69 //!< call. 70 { 71 return addBinding(descriptorType, 1u, stageFlags, immutableSampler); 72 } addSingleIndexedSamplerBinding(VkDescriptorType descriptorType,VkShaderStageFlags stageFlags,uint32_t dstBinding,const VkSampler * immutableSampler)73 inline DescriptorSetLayoutBuilder &addSingleIndexedSamplerBinding( 74 VkDescriptorType descriptorType, VkShaderStageFlags stageFlags, uint32_t dstBinding, 75 const VkSampler *immutableSampler) //!< \note: Using pointer to sampler to clarify that handle is not 76 //!< copied and argument lifetime is expected to cover build() 77 //!< call. 78 { 79 return addIndexedBinding(descriptorType, 1u, stageFlags, dstBinding, immutableSampler); 80 } addArraySamplerBinding(VkDescriptorType descriptorType,uint32_t descriptorCount,VkShaderStageFlags stageFlags,const VkSampler * pImmutableSamplers)81 inline DescriptorSetLayoutBuilder &addArraySamplerBinding(VkDescriptorType descriptorType, uint32_t descriptorCount, 82 VkShaderStageFlags stageFlags, 83 const VkSampler *pImmutableSamplers) 84 { 85 return addBinding(descriptorType, descriptorCount, stageFlags, pImmutableSamplers); 86 } 87 88 private: 89 DescriptorSetLayoutBuilder(const DescriptorSetLayoutBuilder &); // delete 90 DescriptorSetLayoutBuilder &operator=(const DescriptorSetLayoutBuilder &); // delete 91 92 std::vector<VkDescriptorSetLayoutBinding> m_bindings; 93 94 struct ImmutableSamplerInfo 95 { 96 uint32_t bindingIndex; 97 uint32_t samplerBaseIndex; 98 }; 99 100 std::vector<ImmutableSamplerInfo> m_immutableSamplerInfos; 101 std::vector<VkSampler> m_immutableSamplers; 102 }; 103 104 class DescriptorPoolBuilder 105 { 106 public: 107 DescriptorPoolBuilder(void); 108 109 DescriptorPoolBuilder &addType(VkDescriptorType type, uint32_t numDescriptors = 1u); 110 Move<VkDescriptorPool> build(const DeviceInterface &vk, VkDevice device, VkDescriptorPoolCreateFlags flags, 111 uint32_t maxSets, const void *pNext = DE_NULL) const; 112 113 private: 114 DescriptorPoolBuilder(const DescriptorPoolBuilder &); // delete 115 DescriptorPoolBuilder &operator=(const DescriptorPoolBuilder &); // delete 116 117 std::vector<VkDescriptorPoolSize> m_counts; 118 }; 119 120 class DescriptorSetUpdateBuilder 121 { 122 public: 123 class Location 124 { 125 public: binding(uint32_t binding_)126 static inline Location binding(uint32_t binding_) 127 { 128 return Location(binding_, 0u); 129 } bindingArrayElement(uint32_t binding_,uint32_t arrayElement)130 static inline Location bindingArrayElement(uint32_t binding_, uint32_t arrayElement) 131 { 132 return Location(binding_, arrayElement); 133 } 134 135 private: 136 // \note private to force use of factory methods that have more descriptive names Location(uint32_t binding_,uint32_t arrayElement)137 inline Location(uint32_t binding_, uint32_t arrayElement) : m_binding(binding_), m_arrayElement(arrayElement) 138 { 139 } 140 141 friend class DescriptorSetUpdateBuilder; 142 143 const uint32_t m_binding; 144 const uint32_t m_arrayElement; 145 }; 146 147 DescriptorSetUpdateBuilder(void); 148 /* DescriptorSetUpdateBuilder (const DescriptorSetUpdateBuilder&); // do not delete */ 149 150 DescriptorSetUpdateBuilder &write(VkDescriptorSet destSet, uint32_t destBinding, uint32_t destArrayElement, 151 uint32_t count, VkDescriptorType descriptorType, 152 const VkDescriptorImageInfo *pImageInfo, 153 const VkDescriptorBufferInfo *pBufferInfo, const VkBufferView *pTexelBufferView, 154 const void *pNext = DE_NULL); 155 156 DescriptorSetUpdateBuilder ©(VkDescriptorSet srcSet, uint32_t srcBinding, uint32_t srcArrayElement, 157 VkDescriptorSet destSet, uint32_t destBinding, uint32_t destArrayElement, 158 uint32_t count); 159 160 void update(const DeviceInterface &vk, VkDevice device) const; 161 void updateWithPush(const DeviceInterface &vk, VkCommandBuffer cmd, VkPipelineBindPoint bindPoint, 162 VkPipelineLayout pipelineLayout, uint32_t setIdx, uint32_t descriptorIdx = 0, 163 uint32_t numDescriptors = 0) const; 164 void clear(void); 165 166 // helpers 167 writeSingle(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,const VkDescriptorImageInfo * pImageInfo)168 inline DescriptorSetUpdateBuilder &writeSingle(VkDescriptorSet destSet, const Location &destLocation, 169 VkDescriptorType descriptorType, 170 const VkDescriptorImageInfo *pImageInfo) 171 { 172 return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, pImageInfo, 173 DE_NULL, DE_NULL); 174 } 175 writeSingle(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,const VkDescriptorBufferInfo * pBufferInfo)176 inline DescriptorSetUpdateBuilder &writeSingle(VkDescriptorSet destSet, const Location &destLocation, 177 VkDescriptorType descriptorType, 178 const VkDescriptorBufferInfo *pBufferInfo) 179 { 180 return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, DE_NULL, 181 pBufferInfo, DE_NULL); 182 } 183 writeSingle(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,const VkBufferView * pTexelBufferView)184 inline DescriptorSetUpdateBuilder &writeSingle(VkDescriptorSet destSet, const Location &destLocation, 185 VkDescriptorType descriptorType, 186 const VkBufferView *pTexelBufferView) 187 { 188 return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, DE_NULL, DE_NULL, 189 pTexelBufferView); 190 } 191 192 #ifndef CTS_USES_VULKANSC writeSingle(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,const VkWriteDescriptorSetAccelerationStructureKHR * pAccelerationStructure)193 inline DescriptorSetUpdateBuilder &writeSingle( 194 VkDescriptorSet destSet, const Location &destLocation, VkDescriptorType descriptorType, 195 const VkWriteDescriptorSetAccelerationStructureKHR *pAccelerationStructure) 196 { 197 return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, DE_NULL, DE_NULL, 198 DE_NULL, pAccelerationStructure); 199 } 200 #endif // CTS_USES_VULKANSC 201 writeArray(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,uint32_t numDescriptors,const VkDescriptorImageInfo * pImageInfo)202 inline DescriptorSetUpdateBuilder &writeArray(VkDescriptorSet destSet, const Location &destLocation, 203 VkDescriptorType descriptorType, uint32_t numDescriptors, 204 const VkDescriptorImageInfo *pImageInfo) 205 { 206 return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, numDescriptors, descriptorType, 207 pImageInfo, DE_NULL, DE_NULL); 208 } 209 writeArray(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,uint32_t numDescriptors,const VkDescriptorBufferInfo * pBufferInfo)210 inline DescriptorSetUpdateBuilder &writeArray(VkDescriptorSet destSet, const Location &destLocation, 211 VkDescriptorType descriptorType, uint32_t numDescriptors, 212 const VkDescriptorBufferInfo *pBufferInfo) 213 { 214 return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, numDescriptors, descriptorType, 215 DE_NULL, pBufferInfo, DE_NULL); 216 } 217 writeArray(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,uint32_t numDescriptors,const VkBufferView * pTexelBufferView)218 inline DescriptorSetUpdateBuilder &writeArray(VkDescriptorSet destSet, const Location &destLocation, 219 VkDescriptorType descriptorType, uint32_t numDescriptors, 220 const VkBufferView *pTexelBufferView) 221 { 222 return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, numDescriptors, descriptorType, 223 DE_NULL, DE_NULL, pTexelBufferView); 224 } 225 226 #ifndef CTS_USES_VULKANSC writeArray(VkDescriptorSet destSet,const Location & destLocation,VkDescriptorType descriptorType,uint32_t numDescriptors,const VkWriteDescriptorSetAccelerationStructureKHR * pAccelerationStructure)227 inline DescriptorSetUpdateBuilder &writeArray( 228 VkDescriptorSet destSet, const Location &destLocation, VkDescriptorType descriptorType, uint32_t numDescriptors, 229 const VkWriteDescriptorSetAccelerationStructureKHR *pAccelerationStructure) 230 { 231 return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, numDescriptors, descriptorType, 232 DE_NULL, DE_NULL, DE_NULL, pAccelerationStructure); 233 } 234 #endif // CTS_USES_VULKANSC 235 copySingle(VkDescriptorSet srcSet,const Location & srcLocation,VkDescriptorSet destSet,const Location & destLocation)236 inline DescriptorSetUpdateBuilder ©Single(VkDescriptorSet srcSet, const Location &srcLocation, 237 VkDescriptorSet destSet, const Location &destLocation) 238 { 239 return copy(srcSet, srcLocation.m_binding, srcLocation.m_arrayElement, destSet, destLocation.m_binding, 240 destLocation.m_arrayElement, 1u); 241 } 242 copyArray(VkDescriptorSet srcSet,const Location & srcLocation,VkDescriptorSet destSet,const Location & destLocation,uint32_t count)243 inline DescriptorSetUpdateBuilder ©Array(VkDescriptorSet srcSet, const Location &srcLocation, 244 VkDescriptorSet destSet, const Location &destLocation, uint32_t count) 245 { 246 return copy(srcSet, srcLocation.m_binding, srcLocation.m_arrayElement, destSet, destLocation.m_binding, 247 destLocation.m_arrayElement, count); 248 } 249 250 private: 251 DescriptorSetUpdateBuilder &operator=(const DescriptorSetUpdateBuilder &); // delete 252 253 struct WriteDescriptorInfo 254 { 255 std::vector<VkDescriptorImageInfo> imageInfos; 256 std::vector<VkDescriptorBufferInfo> bufferInfos; 257 std::vector<VkBufferView> texelBufferViews; 258 }; 259 260 std::vector<WriteDescriptorInfo> m_writeDescriptorInfos; 261 262 std::vector<VkWriteDescriptorSet> m_writes; 263 std::vector<VkCopyDescriptorSet> m_copies; 264 }; 265 266 } // namespace vk 267 268 #endif // _VKBUILDERUTIL_HPP 269