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<VkDescriptorSet>
186 {
187 public:
Deleter(const DeviceInterface & deviceIface,VkDevice device,VkDescriptorPool pool)188 							Deleter		(const DeviceInterface& deviceIface, VkDevice device, VkDescriptorPool pool)
189 								: m_deviceIface	(&deviceIface)
190 								, m_device		(device)
191 								, m_pool		(pool)
192 							{}
Deleter(void)193 							Deleter		(void)
194 								: m_deviceIface	(DE_NULL)
195 								, m_device		(DE_NULL)
196 								, m_pool		(DE_NULL)
197 							{}
198 
operator ()(VkDescriptorSet obj) const199 	void					operator()	(VkDescriptorSet obj) const { m_deviceIface->freeDescriptorSets(m_device, m_pool, 1, &obj); }
200 
201 private:
202 	const DeviceInterface*	m_deviceIface;
203 	VkDevice				m_device;
204 	VkDescriptorPool		m_pool;
205 };
206 
207 template<>
208 class Deleter<VkCommandBuffer>
209 {
210 public:
Deleter(const DeviceInterface & deviceIface,VkDevice device,VkCommandPool pool)211 							Deleter		(const DeviceInterface& deviceIface, VkDevice device, VkCommandPool 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 ()(VkCommandBuffer obj) const222 	void					operator()	(VkCommandBuffer obj) const { m_deviceIface->freeCommandBuffers(m_device, m_pool, 1, &obj); }
223 
224 private:
225 	const DeviceInterface*	m_deviceIface;
226 	VkDevice				m_device;
227 	VkCommandPool			m_pool;
228 };
229 
230 template<typename T>
231 struct RefData
232 {
RefDatavk::refdetails::RefData233 				RefData		(T object_, Deleter<T> deleter_)
234 								: object	(object_)
235 								, deleter	(deleter_)
236 				{}
RefDatavk::refdetails::RefData237 				RefData		(void)
238 								: object	(0)
239 				{}
240 
241 	T			object;
242 	Deleter<T>	deleter;
243 };
244 
245 template<typename T>
246 class RefBase
247 {
248 public:
249 						~RefBase	(void);
250 
get(void) const251 	inline const T&		get			(void) const throw() { return m_data.object;	}
operator *(void) const252 	inline const T&		operator*	(void) const throw() { return get();			}
operator bool(void) const253 	inline operator		bool		(void) const throw() { return !!get();			}
254 
255 protected:
RefBase(RefData<T> data)256 						RefBase		(RefData<T> data) : m_data(data)	{}
257 
258 	void				reset		(void);				//!< Release previous object, set to null.
259 	RefData<T>			disown		(void) throw();		//!< Disown and return object (ownership transferred to caller).
260 	void				assign		(RefData<T> data);	//!< Set new pointer, release previous pointer.
261 
262 private:
263 	RefData<T>			m_data;
264 };
265 
266 template<typename T>
~RefBase(void)267 inline RefBase<T>::~RefBase (void)
268 {
269 	this->reset();
270 }
271 
272 template<typename T>
reset(void)273 inline void RefBase<T>::reset (void)
274 {
275 	if (!!m_data.object)
276 		m_data.deleter(m_data.object);
277 
278 	m_data = RefData<T>();
279 }
280 
281 template<typename T>
disown(void)282 inline RefData<T> RefBase<T>::disown (void) throw()
283 {
284 	RefData<T> tmp;
285 	swap(m_data, tmp);
286 	return tmp;
287 }
288 
289 template<typename T>
assign(RefData<T> data)290 inline void RefBase<T>::assign (RefData<T> data)
291 {
292 	this->reset();
293 	m_data = data;
294 }
295 
296 /*--------------------------------------------------------------------*//*!
297  * \brief Movable Vulkan object reference.
298  *
299  * Similar to de::MovePtr.
300  *//*--------------------------------------------------------------------*/
301 template<typename T>
302 class Move : public RefBase<T>
303 {
304 public:
305 	template<typename U>
Move(Checked<U> object,Deleter<U> deleter)306 				Move		(Checked<U> object, Deleter<U> deleter)
307 								: RefBase<T>(RefData<T>(object.object, deleter))
308 				{}
309 
Move(RefData<T> data)310 				Move		(RefData<T> data)
311 								: RefBase<T>(data)
312 				{}
Move(Move<T> & other)313 				Move		(Move<T>& other)
314 								: RefBase<T>(other.RefBase<T>::disown())
315 				{}
Move(void)316 				Move		(void)
317 								: RefBase<T>(RefData<T>())
318 				{}
319 
disown(void)320 	T			disown		(void) { return this->RefBase<T>::disown().object; }
321 	Move<T>&	operator=	(Move<T>& other);
322 	Move<T>&	operator=	(RefData<T> data);
323 
operator RefData<T>(void)324 	operator	RefData<T>	(void) { return this->RefBase<T>::disown(); }
325 };
326 
327 template<typename T>
operator =(Move<T> & other)328 inline Move<T>& Move<T>::operator= (Move<T>& other)
329 {
330 	if (this != &other)
331 		this->assign(other.RefBase<T>::disown());
332 
333 	return *this;
334 }
335 
336 template<typename T>
operator =(RefData<T> data)337 inline Move<T>& Move<T>::operator= (RefData<T> data)
338 {
339 	this->assign(data);
340 	return *this;
341 }
342 
343 /*--------------------------------------------------------------------*//*!
344  * \brief Unique Vulkan object reference.
345  *
346  * Similar to de::UniquePtr.
347  *//*--------------------------------------------------------------------*/
348 template<typename T>
349 class Unique : public RefBase<T>
350 {
351 public:
352 	template<typename U>
Unique(Checked<U> object,Deleter<U> deleter)353 				Unique		(Checked<U> object, Deleter<U> deleter)
354 								: RefBase<T>(RefData<T>(object.object, deleter))
355 				{}
356 
Unique(RefData<T> data)357 				Unique		(RefData<T> data)
358 								: RefBase<T>(data)
359 				{}
360 
361 private:
362 				Unique		(const Unique<T>&);
363 	Unique<T>&	operator=	(const Unique<T>&);
364 };
365 
366 } // refdetails
367 
368 using refdetails::Move;
369 using refdetails::Unique;
370 using refdetails::Deleter;
371 using refdetails::check;
372 using refdetails::notNull;
373 using refdetails::allowNull;
374 
375 } // vk
376 
377 #endif // _VKREF_HPP
378