Merge branch 'devel/master' into devel/graphics
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-frame-buffer.cpp
1 /*
2  * Copyright (c) 2021 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 // CLASS HEADER
18 #include <dali/internal/render/renderers/render-frame-buffer.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/internal/render/renderers/render-texture.h>
22
23 namespace Dali
24 {
25 namespace Internal
26 {
27 namespace Render
28 {
29 namespace
30 {
31 const GLenum COLOR_ATTACHMENTS[] =
32   {
33     GL_COLOR_ATTACHMENT0,
34     GL_COLOR_ATTACHMENT1,
35     GL_COLOR_ATTACHMENT2,
36     GL_COLOR_ATTACHMENT3,
37     GL_COLOR_ATTACHMENT4,
38     GL_COLOR_ATTACHMENT5,
39     GL_COLOR_ATTACHMENT6,
40     GL_COLOR_ATTACHMENT7,
41 };
42 }
43
44 FrameBuffer::FrameBuffer(uint32_t width, uint32_t height, Mask attachments)
45 : mId(0u),
46   mTextures{0u},
47   mDepthBuffer(attachments & Dali::FrameBuffer::Attachment::DEPTH),
48   mStencilBuffer(attachments & Dali::FrameBuffer::Attachment::STENCIL),
49   mWidth(width),
50   mHeight(height),
51   mColorAttachmentCount(0u)
52 {
53 }
54
55 FrameBuffer::~FrameBuffer() = default;
56
57 void FrameBuffer::Destroy(Context& context)
58 {
59   if(mId)
60   {
61     context.DeleteFramebuffers(1, &mId);
62   }
63 }
64
65 void FrameBuffer::GlContextDestroyed()
66 {
67   mId = 0u;
68 }
69
70 void FrameBuffer::Initialize(Context& context)
71 {
72   context.GenFramebuffers(1, &mId);
73   context.BindFramebuffer(GL_FRAMEBUFFER, mId);
74
75   if(mDepthBuffer)
76   {
77     // Create a depth render target.
78     context.GenRenderbuffers(1, &mDepthBuffer);
79     context.BindRenderbuffer(GL_RENDERBUFFER, mDepthBuffer);
80     context.RenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mWidth, mHeight);
81     context.FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mDepthBuffer);
82   }
83
84   if(mStencilBuffer)
85   {
86     // Create a stencil render target.
87     context.GenRenderbuffers(1, &mStencilBuffer);
88     context.BindRenderbuffer(GL_RENDERBUFFER, mStencilBuffer);
89     context.RenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, mWidth, mHeight);
90     context.FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mStencilBuffer);
91   }
92
93   context.BindFramebuffer(GL_FRAMEBUFFER, 0);
94 }
95
96 void FrameBuffer::AttachColorTexture(Context& context, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer)
97 {
98   context.BindFramebuffer(GL_FRAMEBUFFER, mId);
99
100   mTextures[mColorAttachmentCount] = texture->GetGraphicsObject();
101   GLuint textureId                 = 0; //@todo Temp
102
103   // Create a color attachment.
104   const GLenum iAttachment = COLOR_ATTACHMENTS[mColorAttachmentCount];
105   if(texture->GetType() == TextureType::TEXTURE_2D)
106   {
107     context.FramebufferTexture2D(GL_FRAMEBUFFER, iAttachment, GL_TEXTURE_2D, textureId, mipmapLevel);
108   }
109   else
110   {
111     context.FramebufferTexture2D(GL_FRAMEBUFFER, iAttachment, GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer, textureId, mipmapLevel);
112   }
113
114   ++mColorAttachmentCount;
115   context.DrawBuffers(mColorAttachmentCount, COLOR_ATTACHMENTS);
116   DALI_ASSERT_DEBUG(context.CheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
117
118   context.BindFramebuffer(GL_FRAMEBUFFER, 0);
119 }
120
121 void FrameBuffer::AttachDepthTexture(Context& context, Render::Texture* texture, uint32_t mipmapLevel)
122 {
123   context.BindFramebuffer(GL_FRAMEBUFFER, mId);
124
125   //@todo Temp
126   GLuint textureId = 0;
127
128   // Create a depth attachment.
129   if(texture->GetType() == TextureType::TEXTURE_2D)
130   {
131     context.FramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, textureId, mipmapLevel);
132   }
133
134   context.BindFramebuffer(GL_FRAMEBUFFER, 0);
135 }
136
137 void FrameBuffer::AttachDepthStencilTexture(Context& context, Render::Texture* texture, uint32_t mipmapLevel)
138 {
139   context.BindFramebuffer(GL_FRAMEBUFFER, mId);
140
141   //@todo temp
142   GLuint textureId = 0;
143
144   // Create a depth/stencil attachment.
145   if(texture->GetType() == TextureType::TEXTURE_2D)
146   {
147     context.FramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, textureId, mipmapLevel);
148   }
149
150   context.BindFramebuffer(GL_FRAMEBUFFER, 0);
151 }
152
153 void FrameBuffer::Bind(Context& context)
154 {
155   context.BindFramebuffer(GL_FRAMEBUFFER, mId);
156 }
157
158 uint32_t FrameBuffer::GetWidth() const
159 {
160   return mWidth;
161 }
162
163 uint32_t FrameBuffer::GetHeight() const
164 {
165   return mHeight;
166 }
167
168 } // namespace Render
169
170 } // namespace Internal
171
172 } // namespace Dali