af31ca3c55e7ebb5a46ab132f42e8791fcd8d261
[platform/core/uifw/dali-core.git] / dali / graphics / vulkan / vulkan-graphics.h
1 #ifndef DALI_GRAPHICS_VULKAN_GRAPHICS
2 #define DALI_GRAPHICS_VULKAN_GRAPHICS
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 #ifndef VULKAN_HPP_NO_EXCEPTIONS
22 #define VULKAN_HPP_NO_EXCEPTIONS
23 #endif
24
25 // INTERNAL INCLUDES
26 #include <dali/graphics/vulkan/vulkan-types.h>
27 #include <dali/integration-api/graphics/surface-factory.h>
28 #include <dali/graphics/vulkan/vulkan-swapchain.h>
29 #include <thread>
30 #include <mutex>
31 #include <map>
32 #include <functional>
33
34 namespace Dali
35 {
36 namespace Graphics
37 {
38 namespace API
39 {
40 class Controller;
41 }
42
43 namespace VulkanAPI
44 {
45 class Controller;
46 }
47
48 namespace Vulkan
49 {
50
51 class Buffer;
52
53 class Image;
54
55 class Pipeline;
56
57 class Shader;
58
59 class Framebuffer;
60
61 class Surface;
62
63 class CommandPool;
64
65 class DescriptorPool;
66
67 class GpuMemoryManager;
68
69 class PipelineCache;
70
71 class ResourceCache;
72
73 using SurfaceFactory = Dali::Integration::Graphics::SurfaceFactory;
74
75 struct SwapchainSurfacePair
76 {
77   RefCountedSwapchain swapchain;
78   RefCountedSurface surface;
79 };
80
81 class Graphics
82 {
83
84 public:
85   Graphics();
86
87   Graphics( const Graphics& ) = delete;
88
89   Graphics& operator=( const Graphics& ) = delete;
90
91   ~Graphics();
92
93 public: // Create methods
94
95   void Create();
96
97   void CreateDevice();
98
99   FBID CreateSurface( std::unique_ptr< SurfaceFactory > surfaceFactory );
100
101   RefCountedSwapchain CreateSwapchainForSurface( RefCountedSurface surface );
102
103   RefCountedShader CreateShader(); //will see if this will work
104   RefCountedPipeline CreatePipeline();
105
106   RefCountedFence CreateFence( const vk::FenceCreateInfo& fenceCreateInfo );
107
108   RefCountedBuffer CreateBuffer( size_t size, BufferType type );
109
110   RefCountedBuffer CreateBuffer( const vk::BufferCreateInfo& bufferCreateInfo );
111
112   RefCountedFramebuffer CreateFramebuffer();
113
114   RefCountedImage CreateImage( const vk::ImageCreateInfo& imageCreateInfo );
115
116   RefCountedImageView CreateImageView( const vk::ImageViewCreateFlags& flags,
117                                        const RefCountedImage& image,
118                                        vk::ImageViewType viewType,
119                                        vk::Format format,
120                                        vk::ComponentMapping components,
121                                        vk::ImageSubresourceRange subresourceRange );
122
123   RefCountedImageView CreateImageView( RefCountedImage image );
124
125   RefCountedDescriptorPool CreateDescriptorPool();
126
127   RefCountedCommandPool CreateCommandPool( const vk::CommandPoolCreateInfo& info );
128
129   RefCountedCommandBuffer CreateCommandBuffer();
130
131   std::vector< RefCountedCommandBuffer > CreateCommandBuffers();
132
133   RefCountedGpuMemoryBlock CreateGpuMemoryBlock();
134
135   RefCountedDescriptorSet CreateDescriptorSet();
136
137   RefCountedSampler CreateSampler();
138
139 public: // Actions
140   vk::Result WaitForFence( RefCountedFence fence, uint32_t timeout = 0 );
141
142   vk::Result WaitForFences( const std::vector< RefCountedFence >& fences,
143                             bool waitAll = true,
144                             uint32_t timeout = std::numeric_limits< uint32_t >::max());
145
146   vk::Result ResetFence( RefCountedFence fence );
147
148   vk::Result ResetFences( const std::vector< RefCountedFence >& fences );
149
150   vk::Result BindImageMemory( RefCountedImage image, RefCountedGpuMemoryBlock memory, uint32_t offset);
151
152 public: // Getters
153   RefCountedSurface GetSurface( FBID surfaceId );
154
155   RefCountedSwapchain GetSwapchainForSurface( RefCountedSurface surface );
156
157   RefCountedSwapchain GetSwapchainForFBID( FBID surfaceId );
158
159   vk::Device GetDevice() const;
160
161   vk::PhysicalDevice GetPhysicalDevice() const;
162
163   vk::Instance GetInstance() const;
164
165   const vk::AllocationCallbacks& GetAllocator() const;
166
167   GpuMemoryManager& GetDeviceMemoryManager() const;
168
169   const vk::PhysicalDeviceMemoryProperties& GetMemoryProperties() const;
170
171   Queue& GetGraphicsQueue( uint32_t index = 0u ) const;
172
173   Queue& GetTransferQueue( uint32_t index = 0u ) const;
174
175   Queue& GetComputeQueue( uint32_t index = 0u ) const;
176
177   Queue& GetPresentQueue() const;
178
179   Platform GetDefaultPlatform() const;
180
181   Dali::Graphics::API::Controller& GetController();
182
183   PipelineCache& GetPipelineCache();
184
185 public: //Cache management methods
186
187   void AddBuffer( RefCountedBuffer buffer );
188
189   void AddImage( RefCountedImage image );
190
191   void AddShader( RefCountedShader shader );
192
193   void AddCommandPool( RefCountedCommandPool pool );
194
195   void AddDescriptorPool( RefCountedDescriptorPool pool );
196
197   void AddFramebuffer( RefCountedFramebuffer framebuffer );
198
199   RefCountedShader FindShader( vk::ShaderModule shaderModule );
200
201   RefCountedImage FindImage( vk::Image image );
202
203   void RemoveBuffer( Buffer& buffer );
204
205   void RemoveImage( Image& image );
206
207   void RemoveShader( Shader& shader );
208
209   void RemoveCommandPool( CommandPool& commandPool );
210
211   void RemoveDescriptorPool( DescriptorPool& pool );
212
213   void RemoveFramebuffer( Framebuffer& framebuffer );
214
215   void RemoveSampler( Sampler& sampler );
216
217   void CollectGarbage();
218
219   void DiscardResource( std::function< void() > deleter );
220
221 private: // Methods
222
223   void CreateInstance( const std::vector< const char* >& extensions,
224                        const std::vector< const char* >& validationLayers );
225
226   void DestroyInstance();
227
228   void PreparePhysicalDevice();
229
230   void GetPhysicalDeviceProperties();
231
232   void GetQueueFamilyProperties();
233
234   std::vector< vk::DeviceQueueCreateInfo > GetQueueCreateInfos();
235
236   std::vector< const char* > PrepareDefaultInstanceExtensions();
237
238 private: // Members
239
240   std::unique_ptr< GpuMemoryManager > mDeviceMemoryManager;
241
242   vk::Instance mInstance;
243   std::unique_ptr< vk::AllocationCallbacks > mAllocator{ nullptr };
244
245   // physical device
246   vk::PhysicalDevice mPhysicalDevice;
247
248   // logical device
249   vk::Device mDevice;
250
251   // physical device properties
252   std::unique_ptr< vk::PhysicalDeviceProperties > mPhysicalDeviceProperties;
253   std::unique_ptr< vk::PhysicalDeviceMemoryProperties > mPhysicalDeviceMemoryProperties;
254   std::unique_ptr< vk::PhysicalDeviceFeatures > mPhysicalDeviceFeatures;
255
256   // queue family properties
257   std::vector< vk::QueueFamilyProperties > mQueueFamilyProperties;
258
259   std::unordered_map< FBID, SwapchainSurfacePair > mSurfaceFBIDMap;
260   FBID mBaseFBID{ 0u };
261
262   // Sets of queues
263   std::vector< std::unique_ptr< Queue > > mGraphicsQueues;
264   std::vector< std::unique_ptr< Queue > > mTransferQueues;
265   std::vector< std::unique_ptr< Queue > > mComputeQueues;
266   //std::unique_ptr< Queue > mPresentQueue;
267
268   Platform mPlatform{ Platform::UNDEFINED };
269
270   std::unique_ptr< Dali::Graphics::VulkanAPI::Controller > mGfxController;
271
272   // TODO: rename
273   std::unique_ptr< PipelineCache > mPipelineDatabase;
274
275   std::mutex mMutex;
276   std::unique_ptr< ResourceCache > mResourceCache;
277
278 };
279
280 } // namespace Vulkan
281 } // namespace Graphics
282 } // namespace Dali
283
284 #endif // DALI_GRAPHICS_VULKAN_GRAPHICS