[Vulkan] Sampler and texture support - something is rendering
[platform/core/uifw/dali-core.git] / dali / graphics / vulkan / vulkan-types.h
1 #ifndef DALI_GRAPHICS_VULKAN_TYPES
2 #define DALI_GRAPHICS_VULKAN_TYPES
3
4 /*
5  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <atomic>
23 #include <unordered_map>
24 #include <memory>
25
26 #include <dali/integration-api/graphics/vulkan/vulkan-hpp-wrapper.h>
27
28 namespace Dali
29 {
30 namespace Graphics
31 {
32
33 template< typename T, typename... Args >
34 std::unique_ptr< T > MakeUnique(Args&&... args)
35 {
36   return std::unique_ptr< T  >(new T(std::forward< Args >(args)...));
37 }
38
39 namespace Vulkan
40 {
41
42 /**
43  * Forward class declarations
44  */
45 class Graphics;
46 class Surface;
47 class Queue;
48
49 /**
50  * Unique pointers to Vulkan types
51  */
52 using UniqueQueue         = std::unique_ptr< Queue >;
53
54 /**
55  * Reference wrappers
56  */
57 using QueueRef         = std::reference_wrapper< Queue >;
58
59 template< typename T >
60 T VkAssert(const vk::ResultValue< T >& result, vk::Result expected = vk::Result::eSuccess)
61 {
62   assert(result.result == expected);
63   return result.value;
64 }
65
66 inline vk::Result VkAssert(vk::Result result, vk::Result expected = vk::Result::eSuccess)
67 {
68   assert(result == expected);
69   return result;
70 }
71
72 inline vk::Result VkTest(vk::Result result, vk::Result expected = vk::Result::eSuccess)
73 {
74   // todo: log if result different than expected?
75   return result;
76 }
77
78 template< typename T >
79 inline uint32_t U32(T value)
80 {
81   return static_cast< uint32_t >(value);
82 }
83
84 /**
85  * Vulkan object handle
86  * @tparam T
87  */
88 template<class T>
89 class Handle
90 {
91 public:
92
93   Handle();
94   explicit Handle(T* object );
95   Handle( const Handle& handle);
96   Handle& operator=( const Handle& handle );
97   Handle& operator=( Handle&& handle );
98   Handle( Handle&& handle ) noexcept;
99   ~Handle();
100
101   operator bool() const;
102
103   T* operator->() const
104   {
105     return mObject;
106   }
107
108   uint32_t GetRefCount() const
109   {
110     return mObject->GetRefCount();
111   }
112
113   T& operator*() const
114   {
115     return *mObject;
116   }
117
118   template <class K>
119   Handle<K> StaticCast()
120   {
121     return Handle<K>(static_cast<K*>(mObject));
122   }
123
124   template<class K>
125   bool operator==( const Handle<K>& object ) const
126   {
127     return mObject == &*object;
128   }
129
130   template <class K>
131   Handle<K> DynamicCast();
132
133   void Reset()
134   {
135     if( mObject )
136     {
137       mObject->Release();
138       mObject = nullptr;
139     }
140   }
141
142 private:
143
144   T* mObject { nullptr };
145 };
146
147 template <class K, class T>
148 static Handle<K> VkTypeCast( const Handle<T>& inval )
149 {
150   return Handle<K>(static_cast<K*>(&*inval));
151 }
152
153 template<class T>
154 Handle<T>::Handle(T* object)
155   : mObject( object )
156 {
157   if(mObject)
158   {
159     mObject->Retain();
160   }
161 }
162
163 template<class T>
164 Handle<T>::Handle()
165   : mObject( nullptr )
166 {
167 }
168
169 template<class T>
170 Handle<T>::Handle(const Handle& handle)
171 {
172   mObject = handle.mObject;
173   if(mObject)
174   {
175     mObject->Retain();
176   }
177 }
178
179 template<class T>
180 Handle<T>::Handle( Handle&& handle ) noexcept
181 {
182   mObject = handle.mObject;
183   handle.mObject = nullptr;
184 }
185
186 template<class T>
187 Handle<T>::operator bool() const
188 {
189   return mObject != nullptr;
190 }
191
192 template<class T>
193 Handle<T>& Handle<T>::operator=( Handle&& handle )
194 {
195   mObject = handle.mObject;
196   handle.mObject = nullptr;
197   return *this;
198 }
199
200 template<class T>
201 Handle<T>& Handle<T>::operator=( const Handle<T>& handle )
202 {
203   mObject = handle.mObject;
204   if(mObject)
205   {
206     mObject->Retain();
207   }
208   return *this;
209 }
210
211 template<class T>
212 Handle<T>::~Handle()
213 {
214   if(mObject)
215   {
216     mObject->Release();
217   }
218 }
219
220 template<class T>
221 template<class K>
222 Handle<K> Handle<T>::DynamicCast()
223 {
224   auto val = dynamic_cast<K*>(mObject);
225   if(val)
226   {
227     return Handle<K>(val);
228   }
229   return Handle<K>();
230 }
231
232 template< typename T, typename... Args >
233 Handle< T > MakeRef(Args&&... args)
234 {
235   return Handle< T >(new T(std::forward< Args >(args)...));
236 }
237
238 template< typename T, typename... Args >
239 Handle< T > NewRef(Args&&... args)
240 {
241   return Handle< T >(T::New(std::forward< Args >(args)...));
242 }
243
244
245 template<class T>
246 typename T::Impl& GetImpl( Handle<T>& object )
247 {
248   return static_cast<typename T::Impl&>(*object->mImpl);
249 }
250
251 class VkManaged
252 {
253 public:
254
255   VkManaged() = default;
256   virtual ~VkManaged() = default;
257
258   void Release()
259   {
260     OnRelease(--mRefCount);
261     if(mRefCount == 0)
262     {
263       // orphaned
264       if(!Destroy())
265       {
266         delete this;
267       }
268     }
269   }
270
271   void Retain()
272   {
273     OnRetain(++mRefCount);
274   }
275
276   uint32_t GetRefCount()
277   {
278     return mRefCount;
279   }
280
281   bool Destroy()
282   {
283     return OnDestroy();
284   }
285
286   virtual void OnRetain( uint32_t refcount ) {};
287
288   virtual void OnRelease( uint32_t refcount ) {};
289
290   virtual bool OnDestroy() { return false; };
291
292 private:
293
294   std::atomic_uint mRefCount { 0u };
295 };
296
297 using FBID = int32_t;
298
299 #define NotImplemented() \
300 {\
301 printf("Function %s isn't implemented!\n", __FUNCTION__);\
302 assert( "Function no implemented" );\
303 }
304
305 /*
306  * Forward declarations of reference types
307  */
308 using ShaderRef = Handle<class Shader>;
309 using PipelineRef = Handle<class Pipeline>;
310 using FenceRef = Handle<class Fence>;
311 using BufferRef = Handle<class Buffer>;
312 using FramebufferRef = Handle<class Framebuffer>;
313 using ImageRef = Handle<class Image>;
314 using ImageViewRef = Handle<class ImageView>;
315 using DescriptorPoolRef = Handle<class DescriptorPool>;
316 using CommandPoolRef = Handle<class CommandPool>;
317 using CommandBufferRef = Handle<class CommandBuffer>;
318 using GpuMemoryBlockRef = Handle<class GpuMemoryBlock>;
319 using DescriptorSetRef = Handle<class DescriptorSet>;
320 using SwapchainRef = Handle<class Swapchain>;
321 using SurfaceRef = Handle<class Surface>;
322 using SamplerRef = Handle<class Sampler>;
323 /*
324 #pragma GCC diagnostic push
325 #pragma GCC diagnostic ignored "-Wframe-larger-than="
326 #pragma GCC diagnostic pop
327 */
328 } // namespace Vulkan
329 } // namespace Graphics
330 } // namespace Dali
331
332 #endif // DALI_GRAPHICS_VULKAN_TYPES