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 )
55 FrameBuffer::~FrameBuffer()
59 void FrameBuffer::Destroy( Context& context )
63 context.DeleteFramebuffers( 1, &mId );
67 void FrameBuffer::GlContextDestroyed()
72 void FrameBuffer::Initialize(Context& context)
74 context.GenFramebuffers( 1, &mId );
75 context.BindFramebuffer( GL_FRAMEBUFFER, mId );
79 // Create a depth render target.
80 context.GenRenderbuffers( 1, &mDepthBuffer );
81 context.BindRenderbuffer( GL_RENDERBUFFER, mDepthBuffer );
82 context.RenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mWidth, mHeight );
83 context.FramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mDepthBuffer );
88 // Create a stencil render target.
89 context.GenRenderbuffers( 1, &mStencilBuffer );
90 context.BindRenderbuffer( GL_RENDERBUFFER, mStencilBuffer );
91 context.RenderbufferStorage( GL_RENDERBUFFER, GL_STENCIL_INDEX8, mWidth, mHeight );
92 context.FramebufferRenderbuffer( GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mStencilBuffer );
95 context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
98 void FrameBuffer::AttachColorTexture( Context& context, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer )
100 context.BindFramebuffer( GL_FRAMEBUFFER, mId );
102 const GLuint textureId = texture->GetId();
103 mTextureId[mColorAttachmentCount] = textureId;
105 // Create a color attachment.
106 const GLenum iAttachment = COLOR_ATTACHMENTS[mColorAttachmentCount];
107 if( texture->GetType() == TextureType::TEXTURE_2D )
109 context.FramebufferTexture2D( GL_FRAMEBUFFER, iAttachment, texture->GetTarget(), textureId, mipmapLevel );
113 context.FramebufferTexture2D( GL_FRAMEBUFFER, iAttachment, GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer, textureId, mipmapLevel );
116 ++mColorAttachmentCount;
117 context.DrawBuffers(mColorAttachmentCount, COLOR_ATTACHMENTS);
118 DALI_ASSERT_DEBUG(context.CheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
120 context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
123 void FrameBuffer::AttachDepthTexture( Context& context, Render::Texture* texture, uint32_t mipmapLevel )
125 context.BindFramebuffer( GL_FRAMEBUFFER, mId );
127 // Create a depth attachment.
128 if( texture->GetType() == TextureType::TEXTURE_2D )
130 context.FramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture->GetId(), mipmapLevel );
133 context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
136 void FrameBuffer::AttachDepthStencilTexture( Context& context, Render::Texture* texture, uint32_t mipmapLevel )
138 context.BindFramebuffer( GL_FRAMEBUFFER, mId );
140 // Create a depth/stencil attachment.
141 if( texture->GetType() == TextureType::TEXTURE_2D )
143 context.FramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, texture->GetId(), mipmapLevel );
146 context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
149 void FrameBuffer::Bind( Context& context )
151 context.BindFramebuffer( GL_FRAMEBUFFER, mId );
154 uint32_t FrameBuffer::GetWidth() const
159 uint32_t FrameBuffer::GetHeight() const