Home | History | Annotate | Download | only in vulkan
      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,
     40 																			 deUint32			descriptorCount,
     41 																			 VkShaderStageFlags	stageFlags,
     42 																			 const VkSampler*	pImmutableSamplers);
     43 
     44 	DescriptorSetLayoutBuilder&					addIndexedBinding			(VkDescriptorType	descriptorType,
     45 																			 deUint32			descriptorCount,
     46 																			 VkShaderStageFlags	stageFlags,
     47 																			 deUint32			dstBinding,
     48 																			 const VkSampler*	pImmutableSamplers);
     49 
     50 	Move<VkDescriptorSetLayout>					build						(const DeviceInterface& vk, VkDevice device, VkDescriptorSetLayoutCreateFlags extraFlags = 0) const;
     51 
     52 	// helpers
     53 
     54 	inline DescriptorSetLayoutBuilder&			addSingleBinding			(VkDescriptorType	descriptorType,
     55 																			 VkShaderStageFlags	stageFlags)
     56 	{
     57 		return addBinding(descriptorType, 1u, stageFlags, (VkSampler*)DE_NULL);
     58 	}
     59 	inline DescriptorSetLayoutBuilder&			addSingleIndexedBinding		(VkDescriptorType	descriptorType,
     60 																			 VkShaderStageFlags	stageFlags,
     61 																			 deUint32			dstBinding)
     62 	{
     63 		return addIndexedBinding(descriptorType, 1u, stageFlags, dstBinding, (VkSampler*)DE_NULL);
     64 	}
     65 	inline DescriptorSetLayoutBuilder&			addArrayBinding				(VkDescriptorType	descriptorType,
     66 																			 deUint32			descriptorCount,
     67 																			 VkShaderStageFlags	stageFlags)
     68 	{
     69 		return addBinding(descriptorType, descriptorCount, stageFlags, (VkSampler*)DE_NULL);
     70 	}
     71 	inline DescriptorSetLayoutBuilder&			addSingleSamplerBinding		(VkDescriptorType	descriptorType,
     72 																			 VkShaderStageFlags	stageFlags,
     73 																			 const VkSampler*	immutableSampler)	//!< \note: Using pointer to sampler to clarify that handle is not
     74 																													//!<        copied and argument lifetime is expected to cover build()
     75 																													//!<        call.
     76 	{
     77 		return addBinding(descriptorType, 1u, stageFlags, immutableSampler);
     78 	}
     79 	inline DescriptorSetLayoutBuilder&			addArraySamplerBinding		(VkDescriptorType	descriptorType,
     80 																			 deUint32			descriptorCount,
     81 																			 VkShaderStageFlags	stageFlags,
     82 																			 const VkSampler*	pImmutableSamplers)
     83 	{
     84 		return addBinding(descriptorType, descriptorCount, stageFlags, pImmutableSamplers);
     85 	}
     86 
     87 private:
     88 												DescriptorSetLayoutBuilder	(const DescriptorSetLayoutBuilder&); // delete
     89 	DescriptorSetLayoutBuilder&					operator=					(const DescriptorSetLayoutBuilder&); // delete
     90 
     91 	std::vector<VkDescriptorSetLayoutBinding>	m_bindings;
     92 
     93 	struct ImmutableSamplerInfo
     94 	{
     95 		deUint32 bindingIndex;
     96 		deUint32 samplerBaseIndex;
     97 	};
     98 
     99 	std::vector<ImmutableSamplerInfo>			m_immutableSamplerInfos;
    100 	std::vector<VkSampler>						m_immutableSamplers;
    101 };
    102 
    103 class DescriptorPoolBuilder
    104 {
    105 public:
    106 										DescriptorPoolBuilder	(void);
    107 
    108 	DescriptorPoolBuilder&				addType					(VkDescriptorType type, deUint32 numDescriptors = 1u);
    109 	Move<VkDescriptorPool>				build					(const DeviceInterface& vk, VkDevice device, VkDescriptorPoolCreateFlags flags, deUint32 maxSets) const;
    110 
    111 private:
    112 										DescriptorPoolBuilder	(const DescriptorPoolBuilder&); // delete
    113 	DescriptorPoolBuilder&				operator=				(const DescriptorPoolBuilder&); // delete
    114 
    115 	std::vector<VkDescriptorPoolSize>	m_counts;
    116 };
    117 
    118 class DescriptorSetUpdateBuilder
    119 {
    120 public:
    121 	class Location
    122 	{
    123 	public:
    124 		static inline Location	binding				(deUint32 binding_)
    125 		{
    126 			return Location(binding_, 0u);
    127 		}
    128 		static inline Location	bindingArrayElement	(deUint32 binding_, deUint32 arrayElement)
    129 		{
    130 			return Location(binding_, arrayElement);
    131 		}
    132 
    133 	private:
    134 		// \note private to force use of factory methods that have more descriptive names
    135 		inline					Location			(deUint32 binding_, deUint32 arrayElement)
    136 			: m_binding			(binding_)
    137 			, m_arrayElement	(arrayElement)
    138 		{
    139 		}
    140 
    141 		friend class DescriptorSetUpdateBuilder;
    142 
    143 		const deUint32			m_binding;
    144 		const deUint32			m_arrayElement;
    145 	};
    146 
    147 										DescriptorSetUpdateBuilder	(void);
    148 										/* DescriptorSetUpdateBuilder	(const DescriptorSetUpdateBuilder&); // do not delete */
    149 
    150 	DescriptorSetUpdateBuilder&			write						(VkDescriptorSet				destSet,
    151 																	 deUint32						destBinding,
    152 																	 deUint32						destArrayElement,
    153 																	 deUint32						count,
    154 																	 VkDescriptorType				descriptorType,
    155 																	 const VkDescriptorImageInfo*	pImageInfo,
    156 																	 const VkDescriptorBufferInfo*	pBufferInfo,
    157 																	 const VkBufferView*			pTexelBufferView);
    158 
    159 	DescriptorSetUpdateBuilder&			copy						(VkDescriptorSet	srcSet,
    160 																	 deUint32			srcBinding,
    161 																	 deUint32			srcArrayElement,
    162 																	 VkDescriptorSet	destSet,
    163 																	 deUint32			destBinding,
    164 																	 deUint32			destArrayElement,
    165 																	 deUint32			count);
    166 
    167 	void								update						(const DeviceInterface& vk, VkDevice device) const;
    168 	void								updateWithPush				(const DeviceInterface& vk, VkCommandBuffer cmd, VkPipelineBindPoint bindPoint, VkPipelineLayout pipelineLayout, deUint32 setIdx) const;
    169 
    170 	// helpers
    171 
    172 	inline DescriptorSetUpdateBuilder&	writeSingle					(VkDescriptorSet				destSet,
    173 																	 const Location&				destLocation,
    174 																	 VkDescriptorType				descriptorType,
    175 																	 const VkDescriptorImageInfo*	pImageInfo)
    176 	{
    177 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, pImageInfo, DE_NULL, DE_NULL);
    178 	}
    179 
    180 	inline DescriptorSetUpdateBuilder&	writeSingle					(VkDescriptorSet				destSet,
    181 																	 const Location&				destLocation,
    182 																	 VkDescriptorType				descriptorType,
    183 																	 const VkDescriptorBufferInfo*	pBufferInfo)
    184 	{
    185 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, DE_NULL, pBufferInfo, DE_NULL);
    186 	}
    187 
    188 	inline DescriptorSetUpdateBuilder&	writeSingle					(VkDescriptorSet				destSet,
    189 																	 const Location&				destLocation,
    190 																	 VkDescriptorType				descriptorType,
    191 																	 const VkBufferView*			pTexelBufferView)
    192 	{
    193 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u, descriptorType, DE_NULL, DE_NULL, pTexelBufferView);
    194 	}
    195 
    196 	inline DescriptorSetUpdateBuilder&	writeArray					(VkDescriptorSet				destSet,
    197 																	 const Location&				destLocation,
    198 																	 VkDescriptorType				descriptorType,
    199 																	 deUint32						numDescriptors,
    200 																	 const VkDescriptorImageInfo*	pImageInfo)
    201 	{
    202 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, numDescriptors, descriptorType, pImageInfo, DE_NULL, DE_NULL);
    203 	}
    204 
    205 	inline DescriptorSetUpdateBuilder&	writeArray					(VkDescriptorSet				destSet,
    206 																	 const Location&				destLocation,
    207 																	 VkDescriptorType				descriptorType,
    208 																	 deUint32						numDescriptors,
    209 																	 const VkDescriptorBufferInfo*	pBufferInfo)
    210 	{
    211 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, numDescriptors, descriptorType, DE_NULL, pBufferInfo, DE_NULL);
    212 	}
    213 
    214 	inline DescriptorSetUpdateBuilder&	writeArray					(VkDescriptorSet				destSet,
    215 																	 const Location&				destLocation,
    216 																	 VkDescriptorType				descriptorType,
    217 																	 deUint32						numDescriptors,
    218 																	 const VkBufferView*			pTexelBufferView)
    219 	{
    220 		return write(destSet, destLocation.m_binding, destLocation.m_arrayElement, numDescriptors, descriptorType, DE_NULL, DE_NULL, pTexelBufferView);
    221 	}
    222 
    223 	inline DescriptorSetUpdateBuilder&	copySingle					(VkDescriptorSet	srcSet,
    224 																	 const Location&	srcLocation,
    225 																	 VkDescriptorSet	destSet,
    226 																	 const Location&	destLocation)
    227 	{
    228 		return copy(srcSet, srcLocation.m_binding, srcLocation.m_arrayElement, destSet, destLocation.m_binding, destLocation.m_arrayElement, 1u);
    229 	}
    230 
    231 	inline DescriptorSetUpdateBuilder&	copyArray					(VkDescriptorSet	srcSet,
    232 																	 const Location&	srcLocation,
    233 																	 VkDescriptorSet	destSet,
    234 																	 const Location&	destLocation,
    235 																	 deUint32			count)
    236 	{
    237 		return copy(srcSet, srcLocation.m_binding, srcLocation.m_arrayElement, destSet, destLocation.m_binding, destLocation.m_arrayElement, count);
    238 	}
    239 
    240 private:
    241 	DescriptorSetUpdateBuilder&			operator=					(const DescriptorSetUpdateBuilder&); // delete
    242 
    243 	struct WriteDescriptorInfo
    244 	{
    245 		std::vector<VkDescriptorImageInfo>	imageInfos;
    246 		std::vector<VkDescriptorBufferInfo>	bufferInfos;
    247 		std::vector<VkBufferView>			texelBufferViews;
    248 	};
    249 
    250 	std::vector<WriteDescriptorInfo>	m_writeDescriptorInfos;
    251 
    252 	std::vector<VkWriteDescriptorSet>	m_writes;
    253 	std::vector<VkCopyDescriptorSet>	m_copies;
    254 };
    255 
    256 } // vk
    257 
    258 #endif // _VKBUILDERUTIL_HPP
    259