Simplified Vulkan backend [WIP]
[platform/core/uifw/dali-core.git] / dali / graphics / vulkan / vulkan-types.h
1 #ifndef DALI_GRAPHICS_VULKAN_TYPES_H
2 #define DALI_GRAPHICS_VULKAN_TYPES_H
3
4 /*
5  * Copyright (c) 2017 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
23 // Vulkan
24 #pragma GCC diagnostic push
25 #pragma GCC diagnostic ignored "-Wfloat-equal"
26 #include <vulkan/vulkan.hpp>
27 #pragma GCC diagnostic pop
28
29 // StdC++
30 #include <atomic>
31 #include <unordered_map>
32
33 #ifndef VULKAN_HPP_NO_EXCEPTIONS
34 #define VULKAN_HPP_NO_EXCEPTIONS
35 #endif
36
37 namespace Dali
38 {
39 namespace Graphics
40 {
41
42 template< typename T, typename... Args >
43 std::unique_ptr< T > MakeUnique(Args&&... args)
44 {
45   return std::unique_ptr< T >(new T(std::forward< Args >(args)...));
46 }
47
48 namespace Vulkan
49 {
50
51 /**
52  * Forward class declarations
53  */
54 class Graphics;
55 class CommandBuffer;
56 class CommandPool;
57 class Surface;
58 class Queue;
59 class Fence;
60 class DeviceMemory;
61 class Image;
62 class ImageView;
63 class Buffer;
64
65 /**
66  * Unique pointers to Vulkan types
67  */
68 using UniqueSurface       = std::unique_ptr< Surface >;
69 using UniqueCommandBuffer = std::unique_ptr< CommandBuffer >;
70 using UniqueCommandPool   = std::unique_ptr< CommandPool >;
71 using UniqueQueue         = std::unique_ptr< Queue >;
72 using UniqueFence         = std::unique_ptr< Fence >;
73 using UniqueDeviceMemory  = std::unique_ptr< DeviceMemory >;
74 using UniqueBuffer        = std::unique_ptr< Buffer >;
75 using UniqueImage         = std::unique_ptr< Image >;
76 using UniqueImageView     = std::unique_ptr< ImageView >;
77
78 /**
79  * Reference wrappers
80  */
81 using CommandBufferRef = std::reference_wrapper< CommandBuffer >;
82 using QueueRef         = std::reference_wrapper< Queue >;
83 using FenceRef         = std::reference_wrapper< Fence >;
84 using SurfaceRef       = std::reference_wrapper< Surface >;
85
86
87
88 template< typename T >
89 T VkAssert(const vk::ResultValue< T >& result, vk::Result expected = vk::Result::eSuccess)
90 {
91   assert(result.result == expected);
92   return result.value;
93 }
94
95 inline vk::Result VkAssert(vk::Result result, vk::Result expected = vk::Result::eSuccess)
96 {
97   assert(result == expected);
98   return result;
99 }
100
101 inline vk::Result VkTest(vk::Result result, vk::Result expected = vk::Result::eSuccess)
102 {
103   // todo: log if result different than expected?
104   return result;
105 }
106
107 template< typename T >
108 inline uint32_t U32(T value)
109 {
110   return static_cast< uint32_t >(value);
111 }
112
113 class Resource
114 {
115 public:
116   Resource() : mUserCount{0u} {}
117   virtual ~Resource() = default;
118
119   void IncreaseUserCount()
120   {
121     ++mUserCount;
122   }
123
124   void DecreaseUserCount()
125   {
126     --mUserCount;
127   }
128
129   uint32_t GetUserCount() const
130   {
131     return mUserCount;
132   }
133
134 private:
135
136   std::atomic<uint32_t> mUserCount;
137 };
138
139 template< typename T>
140 class ResourceRef
141 {
142 public:
143
144   ResourceRef( T& object )
145   : mObject( &object )
146   {
147     mObject->IncreaseUserCount();
148   }
149
150   ResourceRef( ResourceRef& object )
151   {
152     if(mObject)
153     {
154       mObject->DecreaseUserCount();
155     }
156
157     mObject = object.mObject;
158     mObject->IncreaseUserCount();
159   }
160
161   ResourceRef operator=(ResourceRef& object )
162   {
163     if(mObject)
164     {
165       mObject->DecreaseUserCount();
166     }
167
168     mObject = object.mObject;
169     mObject->IncreaseUserCount();
170   }
171
172   ~ResourceRef()
173   {
174     if(mObject)
175     {
176       mObject->DecreaseUserCount();
177     }
178   }
179
180   T& GetResource() const
181   {
182     return *mObject;
183   }
184
185 private:
186
187   T* mObject;
188 };
189
190 using FBID = uint32_t;
191
192 #define NotImplemented() \
193 {\
194 printf("Function %s isn't implemented!\n", __FUNCTION__);\
195 assert( "Function no implemented" );\
196 }
197
198 /*
199 #pragma GCC diagnostic push
200 #pragma GCC diagnostic ignored "-Wframe-larger-than="
201 #pragma GCC diagnostic pop
202 */
203 } // namespace Vulkan
204 } // namespace Graphics
205 } // namespace Dali
206
207 #endif // DALI_GRAPHICS_VULKAN_TYPES_H