2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali/internal/render/renderers/render-frame-buffer.h>
21 #include <dali/internal/render/renderers/render-texture.h>
31 const GLenum COLOR_ATTACHMENTS[] =
44 FrameBuffer::FrameBuffer( uint32_t width, uint32_t height, Mask attachments )
47 mDepthBuffer( attachments & Dali::FrameBuffer::Attachment::DEPTH ),
48 mStencilBuffer( attachments & Dali::FrameBuffer::Attachment::STENCIL ),
51 mColorAttachmentCount( 0u ),
52 mCaptureRenderedResult(false),
57 FrameBuffer::~FrameBuffer()
61 delete[] mRenderedBuffer;
65 void FrameBuffer::Destroy( Context& context )
69 context.DeleteFramebuffers( 1, &mId );
73 void FrameBuffer::GlContextDestroyed()
78 void FrameBuffer::Initialize(Context& context)
80 context.GenFramebuffers( 1, &mId );
81 context.BindFramebuffer( GL_FRAMEBUFFER, mId );
85 // Create a depth render target.
86 context.GenRenderbuffers( 1, &mDepthBuffer );
87 context.BindRenderbuffer( GL_RENDERBUFFER, mDepthBuffer );
88 context.RenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mWidth, mHeight );
89 context.FramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mDepthBuffer );
94 // Create a stencil render target.
95 context.GenRenderbuffers( 1, &mStencilBuffer );
96 context.BindRenderbuffer( GL_RENDERBUFFER, mStencilBuffer );
97 context.RenderbufferStorage( GL_RENDERBUFFER, GL_STENCIL_INDEX8, mWidth, mHeight );
98 context.FramebufferRenderbuffer( GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mStencilBuffer );
101 context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
104 void FrameBuffer::AttachColorTexture( Context& context, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer )
106 context.BindFramebuffer( GL_FRAMEBUFFER, mId );
108 const GLuint textureId = texture->GetId();
109 mTextureId[mColorAttachmentCount] = textureId;
111 // Create a color attachment.
112 const GLenum iAttachment = COLOR_ATTACHMENTS[mColorAttachmentCount];
113 if( texture->GetType() == TextureType::TEXTURE_2D )
115 context.FramebufferTexture2D( GL_FRAMEBUFFER, iAttachment, texture->GetTarget(), textureId, mipmapLevel );
119 context.FramebufferTexture2D( GL_FRAMEBUFFER, iAttachment, GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer, textureId, mipmapLevel );
122 ++mColorAttachmentCount;
123 context.DrawBuffers(mColorAttachmentCount, COLOR_ATTACHMENTS);
124 DALI_ASSERT_DEBUG(context.CheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
126 context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
129 void FrameBuffer::AttachDepthTexture( Context& context, Render::Texture* texture, uint32_t mipmapLevel )
131 context.BindFramebuffer( GL_FRAMEBUFFER, mId );
133 // Create a depth attachment.
134 if( texture->GetType() == TextureType::TEXTURE_2D )
136 context.FramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture->GetId(), mipmapLevel );
139 context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
142 void FrameBuffer::AttachDepthStencilTexture( Context& context, Render::Texture* texture, uint32_t mipmapLevel )
144 context.BindFramebuffer( GL_FRAMEBUFFER, mId );
146 // Create a depth/stencil attachment.
147 if( texture->GetType() == TextureType::TEXTURE_2D )
149 context.FramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, texture->GetId(), mipmapLevel );
152 context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
155 void FrameBuffer::Bind( Context& context )
157 context.BindFramebuffer( GL_FRAMEBUFFER, mId );
160 uint32_t FrameBuffer::GetWidth() const
165 uint32_t FrameBuffer::GetHeight() const
170 void FrameBuffer::DrawRenderedBuffer(Context& context)
172 if(!mCaptureRenderedResult)
179 delete[] mRenderedBuffer;
182 context.BindFramebuffer(GL_FRAMEBUFFER, mId);
183 mRenderedBuffer = new GLubyte[mWidth * mHeight * sizeof(GL_UNSIGNED_BYTE)];
184 context.ReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, mRenderedBuffer);
185 context.BindFramebuffer(GL_FRAMEBUFFER, 0);
186 mCaptureRenderedResult = false;
190 GLubyte* FrameBuffer::GetRenderedBuffer()
194 return mRenderedBuffer;
202 void FrameBuffer::CaptureRenderingResult()
204 mCaptureRenderedResult = true;