[Vulkan] Basic Vulkan backend
[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   /** Records image layout transition barrier for one image */
59   void ImageLayoutTransition(vk::Image image, vk::ImageLayout oldLayout, vk::ImageLayout newLayout, vk::ImageAspectFlags aspectMask);
60
61   /** Push wait semaphores */
62   void PushWaitSemaphores(const std::vector< vk::Semaphore >&          semaphores,
63                           const std::vector< vk::PipelineStageFlags >& stages);
64
65   /** Push signal semaphores */
66   void PushSignalSemaphores(const std::vector< vk::Semaphore >& semaphores);
67
68   /**
69    *
70    * @return
71    */
72   const std::vector< vk::Semaphore >& GetSignalSemaphores() const;
73
74   /**
75    *
76    * @return
77    */
78   const std::vector< vk::Semaphore >& GetSWaitSemaphores() const;
79
80   /**
81    *
82    * @return
83    */
84   const std::vector< vk::PipelineStageFlags >& GetWaitSemaphoreStages() const;
85
86   /** Returns Vulkan object associated with the buffer */
87   vk::CommandBuffer GetVkCommandBuffer() const;
88
89   operator vk::CommandBuffer() const
90   {
91     return GetVkCommandBuffer();
92   }
93
94   /**
95    * Tests if the command buffer is primary
96    * @return Returns true if the command buffer is primary
97    */
98   bool IsPrimary() const;
99
100   /**
101    * Binds an array of vertex buffers
102    * @param firstBinding
103    * @param bindingCount
104    * @param buffers
105    * @param pOffsets
106    */
107   void BindVertexBuffers(uint32_t firstBinding, uint32_t bindingCount,
108                          std::vector<Dali::Graphics::Vulkan::Handle<Buffer>> buffers,
109                          const vk::DeviceSize *pOffsets);
110
111   /**
112    * Binds an index buffer
113    * @param buffer
114    * @param offset
115    * @param indexType
116    */
117   void BindIndexBuffer( BufferRef buffer, uint32_t offset, vk::IndexType indexType);
118
119   /**
120    * Binds single vertex buffer
121    * @param binding
122    * @param buffer
123    * @param offset
124    */
125   void BindVertexBuffer(uint32_t binding, Dali::Graphics::Vulkan::Handle<Buffer> buffer, vk::DeviceSize offset );
126
127   /**
128    * Binds graphics pipeline
129    * @param pipeline
130    */
131   void BindGraphicsPipeline( Handle<Pipeline> pipeline );
132
133   /**
134    *
135    * @param descriptorSets
136    * @param pipeline
137    * @param firstSet
138    * @param descriptorSetCount
139    */
140   void BindDescriptorSets( std::vector<Dali::Graphics::Vulkan::Handle<DescriptorSet>> descriptorSets,
141                            Handle<Pipeline> pipeline, uint32_t firstSet, uint32_t descriptorSetCount );
142
143   /**
144    * Binds descriptor sets to the most recently bound Pipeline
145    * @param descriptorSets
146    * @param firstSet
147    */
148   void BindDescriptorSets( std::vector<Dali::Graphics::Vulkan::Handle<DescriptorSet>> descriptorSets, uint32_t firstSet );
149
150   /**
151    * Issues draw command
152    * @param vertexCount
153    * @param instanceCount
154    * @param firstVertex
155    * @param firstInstance
156    */
157   void Draw( uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance );
158
159   /**
160    * Issues draw indexed primiteve command
161    * @param indexCount
162    * @param instanceCount
163    * @param firstIndex
164    * @param vertexOffset
165    * @param firstInstance
166    */
167   void DrawIndexed( uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, uint32_t vertexOffset, uint32_t firstInstance );
168
169   /**
170    * Begins render pass using VkRenderPass and VkFramebuffer associated with FBID
171    * @todo should be replaced with proper implementation and use the framebuffer
172    * @param framebufferId
173    * @param bufferIndex
174    */
175   void BeginRenderPass( FBID framebufferId, uint32_t bufferIndex );
176
177   /**
178    * Allows to issue custom VkRenderPassBeginInfo structure
179    * @param renderPassBeginInfo
180    * @param subpassContents
181    */
182   void BeginRenderPass( vk::RenderPassBeginInfo renderPassBeginInfo, vk::SubpassContents subpassContents );
183
184   /**
185    * Ends current render pass
186    */
187   void EndRenderPass();
188
189   /**
190    * Executes secondary command buffers within primary command buffer
191    * @param commandBuffers
192    */
193   void ExecuteCommands( std::vector<Dali::Graphics::Vulkan::Handle<CommandBuffer>> commandBuffers );
194
195 private:
196
197   /**
198    *
199    * @param image
200    * @param srcAccessMask
201    * @param dstAccessMask
202    * @param srcStageMask
203    * @param dstStageMask
204    * @param oldLayout
205    * @param newLayout
206    * @param aspectMask
207    */
208   void RecordImageLayoutTransition(vk::Image             image,
209                                    vk::AccessFlags        srcAccessMask,
210                                    vk::AccessFlags        dstAccessMask,
211                                    vk::PipelineStageFlags srcStageMask,
212                                    vk::PipelineStageFlags dstStageMask,
213                                    vk::ImageLayout        oldLayout,
214                                    vk::ImageLayout        newLayout,
215                                    vk::ImageAspectFlags   aspectMask);
216
217 private:
218
219   // Constructor called by the CommandPool only
220   CommandBuffer( CommandPool& commandPool, const vk::CommandBufferAllocateInfo& allocateInfo, vk::CommandBuffer vkCommandBuffer );
221
222   class Impl;
223   std::unique_ptr<Impl> mImpl;
224
225 };
226
227 } // namespace Vulkan
228 } // namespace Graphics
229 } // namespace Dali
230
231 #endif // DALI_GRAPHICS_VULKAN_COMMAND_BUFFER_H