• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef _VKREF_HPP
2 #define _VKREF_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 reference holder.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "vkDefs.hpp"
27 #include "vkStrUtil.hpp"
28 #include "deMeta.hpp"
29 
30 #include <algorithm>
31 
32 namespace vk
33 {
34 
35 namespace refdetails
36 {
37 
38 using std::swap;
39 
40 template<typename T>
41 struct Checked
42 {
Checkedvk::refdetails::Checked43 	explicit inline		Checked		(T object_) : object(object_) {}
44 
45 	T					object;
46 };
47 
48 //! Check that object is not null
49 template<typename T>
check(T object)50 inline Checked<T> check (T object)
51 {
52 	if (!object)
53 		throw tcu::TestError("Object check() failed", (std::string(getTypeName<T>()) + " = 0").c_str(), __FILE__, __LINE__);
54 	return Checked<T>(object);
55 }
56 
57 //! Declare object as checked earlier
58 template<typename T>
notNull(T object)59 inline Checked<T> notNull (T object)
60 {
61 	if (!object)
62 		throw tcu::InternalError("Null object was given to notNull()", (std::string(getTypeName<T>()) + " = 0").c_str(), __FILE__, __LINE__);
63 	return Checked<T>(object);
64 }
65 
66 //! Allow null object
67 template<typename T>
allowNull(T object)68 inline Checked<T> allowNull (T object)
69 {
70 	return Checked<T>(object);
71 }
72 
73 template<typename T>
74 class Deleter
75 {
76 public:
Deleter(const DeviceInterface & deviceIface,VkDevice device,const VkAllocationCallbacks * allocator)77 									Deleter		(const DeviceInterface& deviceIface, VkDevice device, const VkAllocationCallbacks* allocator)
78 										: m_deviceIface	(&deviceIface)
79 										, m_device		(device)
80 										, m_allocator	(allocator)
81 									{}
Deleter(void)82 									Deleter		(void)
83 										: m_deviceIface	(DE_NULL)
84 										, m_device		(DE_NULL)
85 										, m_allocator	(DE_NULL)
86 									{}
87 
88 	void							operator()	(T obj) const;
89 
90 private:
91 	const DeviceInterface*			m_deviceIface;
92 	VkDevice						m_device;
93 	const VkAllocationCallbacks*	m_allocator;
94 };
95 
96 template<>
97 class Deleter<VkInstance>
98 {
99 public:
Deleter(const PlatformInterface & platformIface,VkInstance instance,const VkAllocationCallbacks * allocator)100 									Deleter		(const PlatformInterface& platformIface, VkInstance instance, const VkAllocationCallbacks* allocator)
101 										: m_destroyInstance	((DestroyInstanceFunc)platformIface.getInstanceProcAddr(instance, "vkDestroyInstance"))
102 										, m_allocator		(allocator)
103 									{}
Deleter(void)104 									Deleter		(void)
105 										: m_destroyInstance	((DestroyInstanceFunc)DE_NULL)
106 										, m_allocator		(DE_NULL)
107 									{}
108 
operator ()(VkInstance obj) const109 	void							operator()	(VkInstance obj) const { m_destroyInstance(obj, m_allocator); }
110 
111 private:
112 	DestroyInstanceFunc				m_destroyInstance;
113 	const VkAllocationCallbacks*	m_allocator;
114 };
115 
116 template<>
117 class Deleter<VkDevice>
118 {
119 public:
Deleter(const PlatformInterface & platformIface,VkInstance instance,VkDevice device,const VkAllocationCallbacks * allocator)120 									Deleter		(const PlatformInterface& platformIface, VkInstance instance, VkDevice device, const VkAllocationCallbacks* allocator)
121 									{
122 										GetDeviceProcAddrFunc getDeviceProcAddr = (GetDeviceProcAddrFunc)platformIface.getInstanceProcAddr(instance, "vkGetDeviceProcAddr");
123 										m_destroyDevice = (DestroyDeviceFunc)getDeviceProcAddr(device, "vkDestroyDevice");
124 										m_allocator = allocator;
125 									}
Deleter(void)126 									Deleter		(void)
127 										: m_destroyDevice	((DestroyDeviceFunc)DE_NULL)
128 										, m_allocator		(DE_NULL)
129 									{}
130 
operator ()(VkDevice obj) const131 	void							operator()	(VkDevice obj) const { m_destroyDevice(obj, m_allocator); }
132 
133 private:
134 	DestroyDeviceFunc				m_destroyDevice;
135 	const VkAllocationCallbacks*	m_allocator;
136 };
137 
138 template<>
139 class Deleter<VkSurfaceKHR>
140 {
141 public:
Deleter(const InstanceInterface & instanceIface,VkInstance instance,const VkAllocationCallbacks * allocator)142 									Deleter		(const InstanceInterface& instanceIface, VkInstance instance, const VkAllocationCallbacks* allocator)
143 										: m_instanceIface	(&instanceIface)
144 										, m_instance		(instance)
145 										, m_allocator		(allocator)
146 									{}
Deleter(void)147 									Deleter		(void)
148 										: m_instanceIface	(DE_NULL)
149 										, m_instance		((VkInstance)0)
150 										, m_allocator		(DE_NULL)
151 									{}
152 
operator ()(VkSurfaceKHR obj) const153 	void							operator()	(VkSurfaceKHR obj) const { m_instanceIface->destroySurfaceKHR(m_instance, obj, m_allocator); }
154 
155 private:
156 	const InstanceInterface*		m_instanceIface;
157 	VkInstance						m_instance;
158 	const VkAllocationCallbacks*	m_allocator;
159 };
160 
161 template<>
162 class Deleter<VkDebugReportCallbackEXT>
163 {
164 public:
Deleter(const InstanceInterface & instanceIface,VkInstance instance,const VkAllocationCallbacks * allocator)165 									Deleter		(const InstanceInterface& instanceIface, VkInstance instance, const VkAllocationCallbacks* allocator)
166 										: m_instanceIface	(&instanceIface)
167 										, m_instance		(instance)
168 										, m_allocator		(allocator)
169 									{}
Deleter(void)170 									Deleter		(void)
171 										: m_instanceIface	(DE_NULL)
172 										, m_instance		((VkInstance)0)
173 										, m_allocator		(DE_NULL)
174 									{}
175 
operator ()(VkDebugReportCallbackEXT obj) const176 	void							operator()	(VkDebugReportCallbackEXT obj) const { m_instanceIface->destroyDebugReportCallbackEXT(m_instance, obj, m_allocator); }
177 
178 private:
179 	const InstanceInterface*		m_instanceIface;
180 	VkInstance						m_instance;
181 	const VkAllocationCallbacks*	m_allocator;
182 };
183 
184 template<>
185 class Deleter<VkDebugUtilsMessengerEXT>
186 {
187 public:
Deleter(const InstanceInterface & instanceIface,VkInstance instance,const VkAllocationCallbacks * allocator)188 									Deleter		(const InstanceInterface& instanceIface, VkInstance instance, const VkAllocationCallbacks* allocator)
189 										: m_instanceIface	(&instanceIface)
190 										, m_instance		(instance)
191 										, m_allocator		(allocator)
192 									{}
Deleter(void)193 									Deleter		(void)
194 										: m_instanceIface	(DE_NULL)
195 										, m_instance		((VkInstance)0)
196 										, m_allocator		(DE_NULL)
197 									{}
198 
operator ()(VkDebugUtilsMessengerEXT obj) const199 	void							operator()	(VkDebugUtilsMessengerEXT obj) const { m_instanceIface->destroyDebugUtilsMessengerEXT(m_instance, obj, m_allocator); }
200 
201 private:
202 	const InstanceInterface*		m_instanceIface;
203 	VkInstance						m_instance;
204 	const VkAllocationCallbacks*	m_allocator;
205 };
206 
207 template<>
208 class Deleter<VkDescriptorSet>
209 {
210 public:
Deleter(const DeviceInterface & deviceIface,VkDevice device,VkDescriptorPool pool)211 							Deleter		(const DeviceInterface& deviceIface, VkDevice device, VkDescriptorPool pool)
212 								: m_deviceIface	(&deviceIface)
213 								, m_device		(device)
214 								, m_pool		(pool)
215 							{}
Deleter(void)216 							Deleter		(void)
217 								: m_deviceIface	(DE_NULL)
218 								, m_device		(DE_NULL)
219 								, m_pool		(DE_NULL)
220 							{}
221 
operator ()(VkDescriptorSet obj) const222 	void					operator()	(VkDescriptorSet obj) const { m_deviceIface->freeDescriptorSets(m_device, m_pool, 1, &obj); }
223 
224 private:
225 	const DeviceInterface*	m_deviceIface;
226 	VkDevice				m_device;
227 	VkDescriptorPool		m_pool;
228 };
229 
230 template<>
231 class Deleter<VkCommandBuffer>
232 {
233 public:
Deleter(const DeviceInterface & deviceIface,VkDevice device,VkCommandPool pool)234 							Deleter		(const DeviceInterface& deviceIface, VkDevice device, VkCommandPool pool)
235 								: m_deviceIface	(&deviceIface)
236 								, m_device		(device)
237 								, m_pool		(pool)
238 							{}
Deleter(void)239 							Deleter		(void)
240 								: m_deviceIface	(DE_NULL)
241 								, m_device		(DE_NULL)
242 								, m_pool		(DE_NULL)
243 							{}
244 
operator ()(VkCommandBuffer obj) const245 	void					operator()	(VkCommandBuffer obj) const { m_deviceIface->freeCommandBuffers(m_device, m_pool, 1, &obj); }
246 
247 private:
248 	const DeviceInterface*	m_deviceIface;
249 	VkDevice				m_device;
250 	VkCommandPool			m_pool;
251 };
252 
253 template<typename T>
254 struct RefData
255 {
RefDatavk::refdetails::RefData256 				RefData		(T object_, Deleter<T> deleter_)
257 								: object	(object_)
258 								, deleter	(deleter_)
259 				{}
RefDatavk::refdetails::RefData260 				RefData		(void)
261 								: object	(0)
262 				{}
263 
264 	T			object;
265 	Deleter<T>	deleter;
266 };
267 
268 template<typename T>
269 class RefBase
270 {
271 public:
272 						~RefBase	(void);
273 
get(void) const274 	inline const T&		get			(void) const throw() { return m_data.object;	}
operator *(void) const275 	inline const T&		operator*	(void) const throw() { return get();			}
operator bool(void) const276 	inline operator		bool		(void) const throw() { return !!get();			}
277 
278 protected:
RefBase(RefData<T> data)279 						RefBase		(RefData<T> data) : m_data(data)	{}
280 
281 	void				reset		(void);				//!< Release previous object, set to null.
282 	RefData<T>			disown		(void) throw();		//!< Disown and return object (ownership transferred to caller).
283 	void				assign		(RefData<T> data);	//!< Set new pointer, release previous pointer.
284 
285 private:
286 	RefData<T>			m_data;
287 };
288 
289 template<typename T>
~RefBase(void)290 inline RefBase<T>::~RefBase (void)
291 {
292 	this->reset();
293 }
294 
295 template<typename T>
reset(void)296 inline void RefBase<T>::reset (void)
297 {
298 	if (!!m_data.object)
299 		m_data.deleter(m_data.object);
300 
301 	m_data = RefData<T>();
302 }
303 
304 template<typename T>
disown(void)305 inline RefData<T> RefBase<T>::disown (void) throw()
306 {
307 	RefData<T> tmp;
308 	swap(m_data, tmp);
309 	return tmp;
310 }
311 
312 template<typename T>
assign(RefData<T> data)313 inline void RefBase<T>::assign (RefData<T> data)
314 {
315 	this->reset();
316 	m_data = data;
317 }
318 
319 /*--------------------------------------------------------------------*//*!
320  * \brief Movable Vulkan object reference.
321  *
322  * Similar to de::MovePtr.
323  *//*--------------------------------------------------------------------*/
324 template<typename T>
325 class Move : public RefBase<T>
326 {
327 public:
328 	template<typename U>
Move(Checked<U> object,Deleter<U> deleter)329 				Move		(Checked<U> object, Deleter<U> deleter)
330 								: RefBase<T>(RefData<T>(object.object, deleter))
331 				{}
332 
Move(RefData<T> data)333 				Move		(RefData<T> data)
334 								: RefBase<T>(data)
335 				{}
Move(Move<T> & other)336 				Move		(Move<T>& other)
337 								: RefBase<T>(other.RefBase<T>::disown())
338 				{}
Move(void)339 				Move		(void)
340 								: RefBase<T>(RefData<T>())
341 				{}
342 
disown(void)343 	T			disown		(void) { return this->RefBase<T>::disown().object; }
344 	Move<T>&	operator=	(Move<T>& other);
345 	Move<T>&	operator=	(RefData<T> data);
346 
operator RefData<T>(void)347 	operator	RefData<T>	(void) { return this->RefBase<T>::disown(); }
348 };
349 
350 template<typename T>
operator =(Move<T> & other)351 inline Move<T>& Move<T>::operator= (Move<T>& other)
352 {
353 	if (this != &other)
354 		this->assign(other.RefBase<T>::disown());
355 
356 	return *this;
357 }
358 
359 template<typename T>
operator =(RefData<T> data)360 inline Move<T>& Move<T>::operator= (RefData<T> data)
361 {
362 	this->assign(data);
363 	return *this;
364 }
365 
366 /*--------------------------------------------------------------------*//*!
367  * \brief Unique Vulkan object reference.
368  *
369  * Similar to de::UniquePtr.
370  *//*--------------------------------------------------------------------*/
371 template<typename T>
372 class Unique : public RefBase<T>
373 {
374 public:
375 	template<typename U>
Unique(Checked<U> object,Deleter<U> deleter)376 				Unique		(Checked<U> object, Deleter<U> deleter)
377 								: RefBase<T>(RefData<T>(object.object, deleter))
378 				{}
379 
Unique(RefData<T> data)380 				Unique		(RefData<T> data)
381 								: RefBase<T>(data)
382 				{}
383 
384 private:
385 				Unique		(const Unique<T>&);
386 	Unique<T>&	operator=	(const Unique<T>&);
387 };
388 
389 } // refdetails
390 
391 using refdetails::Move;
392 using refdetails::Unique;
393 using refdetails::Deleter;
394 using refdetails::check;
395 using refdetails::notNull;
396 using refdetails::allowNull;
397 
398 } // vk
399 
400 #endif // _VKREF_HPP
401