[Vulkan] Image and Swapchain upgrade
[platform/core/uifw/dali-core.git] / dali / graphics / vulkan / vulkan-command-buffer.h
1 #ifndef DALI_GRAPHICS_VULKAN_COMMAND_BUFFER_H
2 #define DALI_GRAPHICS_VULKAN_COMMAND_BUFFER_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 // INTERNAL INCLUDES
22 #include <dali/graphics/vulkan/vulkan-types.h>
23
24 namespace Dali
25 {
26 namespace Graphics
27 {
28 namespace Vulkan
29 {
30 class Image;
31 class Graphics;
32 class Buffer;
33 class Pipeline;
34 class DescriptorSet;
35 class CommandBuffer : public VkManaged
36 {
37   friend class CommandPool;
38
39 public:
40
41   CommandBuffer() = delete;
42
43   ~CommandBuffer() override;
44
45   /** Begin recording */
46   void Begin(vk::CommandBufferUsageFlags       usageFlags      = vk::CommandBufferUsageFlags{},
47              vk::CommandBufferInheritanceInfo* inheritanceInfo = nullptr);
48
49   /** Finish recording */
50   void End();
51
52   /** Reset command buffer */
53   void Reset();
54
55   /** Free command buffer */
56   void Free();
57
58   /** Push wait semaphores */
59   void PushWaitSemaphores(const std::vector< vk::Semaphore >&          semaphores,
60                           const std::vector< vk::PipelineStageFlags >& stages);
61
62   /** Push signal semaphores */
63   void PushSignalSemaphores(const std::vector< vk::Semaphore >& semaphores);
64
65   /**
66    *
67    * @return
68    */
69   const std::vector< vk::Semaphore >& GetSignalSemaphores() const;
70
71   /**
72    *
73    * @return
74    */
75   const std::vector< vk::Semaphore >& GetSWaitSemaphores() const;
76
77   /**
78    *
79    * @return
80    */
81   const std::vector< vk::PipelineStageFlags >& GetWaitSemaphoreStages() const;
82
83   /** Returns Vulkan object associated with the buffer */
84   vk::CommandBuffer GetVkCommandBuffer() const;
85
86   operator vk::CommandBuffer() const
87   {
88     return GetVkCommandBuffer();
89   }
90
91   /**
92    * Tests if the command buffer is primary
93    * @return Returns true if the command buffer is primary
94    */
95   bool IsPrimary() const;
96
97   /**
98    * Binds an array of vertex buffers
99    * @param firstBinding
100    * @param bindingCount
101    * @param buffers
102    * @param pOffsets
103    */
104   void BindVertexBuffers(uint32_t firstBinding, uint32_t bindingCount,
105                          std::vector<Dali::Graphics::Vulkan::Handle<Buffer>> buffers,
106                          const vk::DeviceSize *pOffsets);
107
108   /**
109    * Binds an index buffer
110    * @param buffer
111    * @param offset
112    * @param indexType
113    */
114   void BindIndexBuffer( BufferRef buffer, uint32_t offset, vk::IndexType indexType);
115
116   /**
117    * Binds single vertex buffer
118    * @param binding
119    * @param buffer
120    * @param offset
121    */
122   void BindVertexBuffer(uint32_t binding, Dali::Graphics::Vulkan::Handle<Buffer> buffer, vk::DeviceSize offset );
123
124   /**
125    * Binds graphics pipeline
126    * @param pipeline
127    */
128   void BindGraphicsPipeline( Handle<Pipeline> pipeline );
129
130   /**
131    *
132    * @param descriptorSets
133    * @param pipeline
134    * @param firstSet
135    * @param descriptorSetCount
136    */
137   void BindDescriptorSets( std::vector<Dali::Graphics::Vulkan::Handle<DescriptorSet>> descriptorSets,
138                            Handle<Pipeline> pipeline, uint32_t firstSet, uint32_t descriptorSetCount );
139
140   /**
141    * Binds descriptor sets to the most recently bound Pipeline
142    * @param descriptorSets
143    * @param firstSet
144    */
145   void BindDescriptorSets( std::vector<Dali::Graphics::Vulkan::Handle<DescriptorSet>> descriptorSets, uint32_t firstSet );
146
147   /**
148    * Issues draw command
149    * @param vertexCount
150    * @param instanceCount
151    * @param firstVertex
152    * @param firstInstance
153    */
154   void Draw( uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance );
155
156   /**
157    * Issues draw indexed primiteve command
158    * @param indexCount
159    * @param instanceCount
160    * @param firstIndex
161    * @param vertexOffset
162    * @param firstInstance
163    */
164   void DrawIndexed( uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, uint32_t vertexOffset, uint32_t firstInstance );
165
166   /**
167    * Begins render pass using VkRenderPass and VkFramebuffer associated with FBID
168    * @todo should be replaced with proper implementation and use the framebuffer
169    * @param framebufferId
170    * @param bufferIndex
171    */
172   void BeginRenderPass( FBID framebufferId, uint32_t bufferIndex );
173
174   /**
175    * Allows to issue custom VkRenderPassBeginInfo structure
176    * @param renderPassBeginInfo
177    * @param subpassContents
178    */
179   void BeginRenderPass( vk::RenderPassBeginInfo renderPassBeginInfo, vk::SubpassContents subpassContents );
180
181   /**
182    * Ends current render pass
183    */
184   void EndRenderPass();
185
186   /**
187    * Records pipeline barrier
188    * @param srcStageMask
189    * @param dstStageMask
190    * @param dependencyFlags
191    * @param memoryBarriers
192    * @param bufferBarriers
193    * @param imageBarriers
194    */
195   void PipelineBarrier( vk::PipelineStageFlags srcStageMask,
196                         vk::PipelineStageFlags dstStageMask,
197                         vk::DependencyFlags dependencyFlags,
198                         std::vector<vk::MemoryBarrier> memoryBarriers,
199                         std::vector<vk::BufferMemoryBarrier> bufferBarriers,
200                         std::vector<vk::ImageMemoryBarrier> imageBarriers );
201
202   /**
203    * Executes secondary command buffers within primary command buffer
204    * @param commandBuffers
205    */
206   void ExecuteCommands( std::vector<Dali::Graphics::Vulkan::Handle<CommandBuffer>> commandBuffers );
207
208   /**
209    * Copies buffer into the specified image
210    * @param srcBuffer
211    * @param dstImage
212    * @param dtsLayout
213    * @param regions
214    */
215   void CopyBufferToImage( BufferRef srcBuffer, ImageRef dstImage, vk::ImageLayout dstLayout,
216                           std::vector<vk::BufferImageCopy> regions );
217
218   void OnRelease( uint32_t refcount ) override;
219
220   /**
221    * Creates layout transition barrier
222    * @return
223    */
224   vk::ImageMemoryBarrier ImageLayoutTransitionBarrier( ImageRef image,
225                                                   vk::AccessFlags        srcAccessMask,
226                                                   vk::AccessFlags        dstAccessMask,
227                                                   vk::ImageLayout        oldLayout,
228                                                   vk::ImageLayout        newLayout,
229                                                   vk::ImageAspectFlags   aspectMask
230   ) const;
231
232   /**
233    * Simplified version of memory barrier generation based on data stored inside the Image
234    * @param image
235    * @param newLayout
236    * @param aspectMask
237    * @return
238    */
239   vk::ImageMemoryBarrier ImageLayoutTransitionBarrier( ImageRef image,
240                                                        vk::ImageLayout        newLayout,
241                                                        vk::ImageAspectFlags   aspectMask
242   ) const;
243
244 private:
245
246   // Constructor called by the CommandPool only
247   CommandBuffer( CommandPool& commandPool, const vk::CommandBufferAllocateInfo& allocateInfo, vk::CommandBuffer vkCommandBuffer );
248
249   class Impl;
250   std::unique_ptr<Impl> mImpl;
251
252 };
253
254 } // namespace Vulkan
255 } // namespace Graphics
256 } // namespace Dali
257
258 #endif // DALI_GRAPHICS_VULKAN_COMMAND_BUFFER_H