[Vulkan] Cleanup after removing rendering backend
[platform/core/uifw/dali-core.git] / dali / graphics / vulkan / vulkan-framebuffer.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <dali/graphics/vulkan/vulkan-framebuffer.h>
19 #include <dali/graphics/vulkan/vulkan-graphics.h>
20 #include <dali/graphics/vulkan/vulkan-image.h>
21
22 namespace Dali
23 {
24 namespace Graphics
25 {
26 namespace Vulkan
27 {
28 struct Framebuffer::Impl
29 {
30   Impl( Framebuffer& owner, Graphics& graphics, uint32_t width, uint32_t height )
31   : mInterface( owner ), mGraphics( graphics ), mColorImageViewAttachments{}, mDepthStencilImageViewAttachment()
32   {
33     mWidth = width;
34     mHeight = height;
35   }
36
37   // creating render pass may happen either as deferred or
38   // when framebuffer is initialised into immutable state
39   bool Initialise()
40   {
41     mAttachmentReference.clear();
42     mAttachmentDescription.clear();
43     mDefaultClearValues.clear();
44     /*
45      * COLOR ATTACHMENTS
46      */
47     auto attachments         = std::vector<vk::ImageView>{};
48     auto colorAttachmentSize = 0u;
49     for( auto&& colorAttachment : mColorImageViewAttachments )
50     {
51       auto attRef = vk::AttachmentReference{};
52       attRef.setLayout( vk::ImageLayout::eColorAttachmentOptimal );
53       attRef.setAttachment( colorAttachmentSize++ );
54       mAttachmentReference.emplace_back( attRef );
55       attachments.emplace_back( colorAttachment->GetVkImageView() );
56
57       vk::AttachmentDescription attDesc{};
58       attDesc.setSamples( vk::SampleCountFlagBits::e1 )
59         .setInitialLayout( vk::ImageLayout::eUndefined )
60         .setFormat( colorAttachment->GetImage()->GetVkFormat() )
61         .setStencilStoreOp( vk::AttachmentStoreOp::eDontCare )
62         .setStencilLoadOp( vk::AttachmentLoadOp::eDontCare )
63         .setLoadOp( vk::AttachmentLoadOp::eClear )
64         .setStoreOp( vk::AttachmentStoreOp::eStore )
65         .setFinalLayout( vk::ImageLayout::ePresentSrcKHR );
66
67       mAttachmentDescription.emplace_back( attDesc );
68
69       // update clear color values
70       vk::ClearColorValue clear;
71       clear.setFloat32( {0.0f, 0.0f, 0.0f, 1.0f} );
72       mDefaultClearValues.emplace_back( clear );
73     }
74
75     /*
76      * DEPTH-STENCIL ATTACHMENT
77      */
78     if( mDepthStencilImageViewAttachment )
79     {
80       auto attRef = vk::AttachmentReference{};
81       attRef.setLayout( vk::ImageLayout::eDepthStencilAttachmentOptimal );
82       attRef.setAttachment( colorAttachmentSize );
83       mAttachmentReference.emplace_back( attRef );
84       attachments.emplace_back( mDepthStencilImageViewAttachment->GetVkImageView() );
85
86       vk::AttachmentDescription attDesc{};
87       attDesc.setSamples( vk::SampleCountFlagBits::e1 )
88              .setInitialLayout( vk::ImageLayout::eUndefined )
89              .setFormat( mDepthStencilImageViewAttachment->GetImage()->GetVkFormat() )
90              .setStencilStoreOp( vk::AttachmentStoreOp::eDontCare )
91              .setStencilLoadOp( vk::AttachmentLoadOp::eDontCare )
92              .setLoadOp( vk::AttachmentLoadOp::eClear )
93              .setStoreOp( vk::AttachmentStoreOp::eDontCare )
94              .setFinalLayout( vk::ImageLayout::eDepthStencilAttachmentOptimal );
95       mAttachmentDescription.emplace_back( attDesc );
96
97       // update clear depth/stencil values
98       vk::ClearDepthStencilValue clear;
99       clear.setDepth( 0.0f ).setStencil( 1.0f );
100       mDefaultClearValues.emplace_back( clear );
101     }
102
103     /*
104      * SUBPASS
105      */
106     // creating single subpass per framebuffer
107     auto subpassDesc = vk::SubpassDescription{};
108     subpassDesc.setPipelineBindPoint( vk::PipelineBindPoint::eGraphics );
109     subpassDesc.setColorAttachmentCount( colorAttachmentSize );
110     if( mDepthStencilImageViewAttachment )
111     {
112       subpassDesc.setPDepthStencilAttachment( &mAttachmentReference[colorAttachmentSize] );
113     }
114     subpassDesc.setPColorAttachments( &mAttachmentReference[0] );
115
116     /*
117      * RENDERPASS
118      */
119     // create compatible render pass
120     auto rpInfo = vk::RenderPassCreateInfo{};
121     rpInfo.setAttachmentCount( U32(mAttachmentDescription.size()) );
122     rpInfo.setPAttachments( mAttachmentDescription.data() );
123     rpInfo.setPSubpasses( &subpassDesc );
124     rpInfo.setSubpassCount( 1 );
125     mVkRenderPass = VkAssert( mGraphics.GetDevice().createRenderPass( rpInfo, mGraphics.GetAllocator() ));
126
127     /*
128      * FRAMEBUFFER
129      */
130     vk::FramebufferCreateInfo info;
131     info.setRenderPass( mVkRenderPass )
132         .setPAttachments( attachments.data() )
133         .setLayers( 1 )
134         .setWidth( mWidth )
135         .setHeight( mHeight )
136         .setAttachmentCount( U32(attachments.size()) );
137
138     mVkFramebuffer = VkAssert( mGraphics.GetDevice().createFramebuffer( info, mGraphics.GetAllocator() ) );
139
140     return true;
141   }
142
143   /**
144    * Creates immutable framebuffer object
145    */
146   bool Commit()
147   {
148     if(!mInitialised)
149     {
150       mInitialised = Initialise();
151       return mInitialised;
152     }
153     return false;
154   }
155
156   void SetAttachment( ImageViewRef imageViewRef, Framebuffer::AttachmentType type, uint32_t index )
157   {
158     // TODO: all array-type atyachments
159     if( type == AttachmentType::COLOR )
160     {
161       auto& attachments = mColorImageViewAttachments;
162       if( attachments.size() <= index )
163       {
164         attachments.resize( index + 1 );
165       }
166       attachments[index] = imageViewRef;
167     }
168     else if( type == AttachmentType::DEPTH_STENCIL )
169     {
170       mDepthStencilImageViewAttachment = imageViewRef;
171     }
172   }
173
174   ImageViewRef GetAttachment( AttachmentType type, uint32_t index ) const
175   {
176     switch( type )
177     {
178       case AttachmentType::COLOR:
179       {
180         return mColorImageViewAttachments[index];
181       }
182       case AttachmentType::DEPTH_STENCIL:
183       {
184         return mDepthStencilImageViewAttachment;
185       }
186       case AttachmentType::DEPTH:
187       case AttachmentType::INPUT:
188       case AttachmentType::RESOLVE:
189       case AttachmentType::PRESERVE:
190       {
191         return ImageViewRef();
192       }
193     }
194     return ImageViewRef();
195   }
196
197   std::vector<ImageViewRef> GetAttachments( AttachmentType type ) const
198   {
199     std::vector<ImageViewRef> retval{};
200     switch( type )
201     {
202       case AttachmentType::COLOR:
203       {
204         retval.insert( retval.end(), mColorImageViewAttachments.begin(), mColorImageViewAttachments.end() );
205         return retval;
206       }
207       case AttachmentType::DEPTH_STENCIL:
208       {
209         retval.push_back( mDepthStencilImageViewAttachment );
210         return retval;
211       }
212       case AttachmentType::DEPTH:
213       case AttachmentType::INPUT:
214       case AttachmentType::RESOLVE:
215       case AttachmentType::PRESERVE:
216       {
217         return retval;
218       }
219     }
220     return retval;
221   }
222
223   uint32_t GetAttachmentCount( AttachmentType type ) const
224   {
225     std::vector<ImageViewRef> retval{};
226     switch( type )
227     {
228       case AttachmentType::COLOR:
229       {
230         return U32(mColorImageViewAttachments.size());
231       }
232       case AttachmentType::DEPTH_STENCIL:
233       {
234         return mDepthStencilImageViewAttachment ? 1u : 0u;
235       }
236       case AttachmentType::DEPTH:
237       case AttachmentType::INPUT:
238       case AttachmentType::RESOLVE:
239       case AttachmentType::PRESERVE:
240       {
241         return 0u;
242       }
243     }
244     return 0u;
245   }
246
247   const std::vector<vk::ClearValue>& GetDefaultClearValues() const
248   {
249     return mDefaultClearValues;
250   }
251
252   ~Impl() = default;
253
254   vk::RenderPass GetVkRenderPass() const
255   {
256     return mVkRenderPass;
257   }
258
259   vk::Framebuffer GetVkFramebuffer() const
260   {
261     return mVkFramebuffer;
262   }
263
264   Framebuffer& mInterface;
265   Graphics&    mGraphics;
266
267   uint32_t mWidth;
268   uint32_t mHeight;
269
270   std::vector<ImageViewRef> mColorImageViewAttachments;
271   ImageViewRef              mDepthStencilImageViewAttachment;
272   vk::Framebuffer           mVkFramebuffer;
273   vk::RenderPass            mVkRenderPass;
274
275   // attachment references for the main subpass
276   std::vector<vk::AttachmentReference>   mAttachmentReference;
277   std::vector<vk::AttachmentDescription> mAttachmentDescription;
278
279   std::vector<vk::ClearValue> mDefaultClearValues;
280   bool mInitialised{false};
281 };
282
283 FramebufferRef Framebuffer::New( Graphics& graphics, uint32_t width, uint32_t height )
284 {
285   FramebufferRef ref( new Framebuffer( graphics, width, height ) );
286   return ref;
287 }
288
289 Framebuffer::Framebuffer( Graphics& graphics, uint32_t width, uint32_t height )
290 {
291   mImpl = std::make_unique<Impl>( *this, graphics, width, height );
292 }
293
294 void Framebuffer::SetAttachment( ImageViewRef imageViewRef, Framebuffer::AttachmentType type, uint32_t index )
295 {
296   mImpl->SetAttachment( imageViewRef, type, index );
297 }
298
299 uint32_t Framebuffer::GetWidth() const
300 {
301   return mImpl->mWidth;
302 }
303
304 uint32_t Framebuffer::GetHeight() const
305 {
306   return mImpl->mHeight;
307 }
308
309 ImageViewRef Framebuffer::GetAttachment( AttachmentType type, uint32_t index ) const
310 {
311   return mImpl->GetAttachment( type, index );
312 }
313
314 std::vector<ImageViewRef> Framebuffer::GetAttachments( AttachmentType type ) const
315 {
316   return mImpl->GetAttachments( type );
317 }
318
319 uint32_t Framebuffer::GetAttachmentCount( AttachmentType type ) const
320 {
321   return mImpl->GetAttachmentCount( type );
322 }
323
324 void Framebuffer::Commit()
325 {
326   mImpl->Commit();
327 }
328
329 vk::RenderPass Framebuffer::GetVkRenderPass() const
330 {
331   return mImpl->mVkRenderPass;
332 }
333
334 vk::Framebuffer Framebuffer::GetVkFramebuffer() const
335 {
336   return mImpl->mVkFramebuffer;
337 }
338
339 const std::vector<vk::ClearValue>& Framebuffer::GetDefaultClearValues() const
340 {
341   return mImpl->GetDefaultClearValues();
342 }
343
344 } // Namespace Vulkan
345
346 } // Namespace Graphics
347
348 } // Namespace Dali