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