Fix MSVC warning.
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-texture-frame-buffer.cpp
1 /*
2  * Copyright (c) 2019 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-texture-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 TextureFrameBuffer::TextureFrameBuffer( uint32_t width, uint32_t height, Mask attachments )
45 : FrameBuffer(),
46   mId( 0u ),
47   mTextureId{ 0u },
48   mDepthBuffer( attachments & Dali::FrameBuffer::Attachment::DEPTH ),
49   mStencilBuffer( attachments & Dali::FrameBuffer::Attachment::STENCIL ),
50   mWidth( width ),
51   mHeight( height ),
52   mColorAttachmentCount( 0u )
53 {
54 }
55
56 TextureFrameBuffer::~TextureFrameBuffer()
57 {}
58
59 void TextureFrameBuffer::Destroy( Context& context )
60 {
61   if( mId )
62   {
63     context.DeleteFramebuffers( 1, &mId );
64   }
65 }
66
67 void TextureFrameBuffer::GlContextDestroyed()
68 {
69   mId = 0u;
70 }
71
72 void TextureFrameBuffer::Initialize(Context& context)
73 {
74   context.GenFramebuffers( 1, &mId );
75   context.BindFramebuffer( GL_FRAMEBUFFER, mId );
76
77   if( mDepthBuffer )
78   {
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 );
84   }
85
86   if( mStencilBuffer )
87   {
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 );
93   }
94
95   context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
96 }
97
98 void TextureFrameBuffer::AttachColorTexture( Context& context, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer )
99 {
100   context.BindFramebuffer( GL_FRAMEBUFFER, mId );
101
102   const GLuint textureId = texture->GetId();
103   mTextureId[mColorAttachmentCount] = textureId;
104
105   // Create a color attachment.
106   const GLenum iAttachment = COLOR_ATTACHMENTS[mColorAttachmentCount];
107   if( texture->GetType() == TextureType::TEXTURE_2D )
108   {
109     context.FramebufferTexture2D( GL_FRAMEBUFFER, iAttachment, texture->GetTarget(), textureId, mipmapLevel );
110   }
111   else
112   {
113     context.FramebufferTexture2D( GL_FRAMEBUFFER, iAttachment, GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer, textureId, mipmapLevel );
114   }
115
116   ++mColorAttachmentCount;
117   context.DrawBuffers(mColorAttachmentCount, COLOR_ATTACHMENTS);
118   DALI_ASSERT_DEBUG(context.CheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
119
120   context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
121 }
122
123 void TextureFrameBuffer::Bind( Context& context )
124 {
125   context.BindFramebuffer( GL_FRAMEBUFFER, mId );
126 }
127
128 uint32_t TextureFrameBuffer::GetWidth() const
129 {
130   return mWidth;
131 }
132
133 uint32_t TextureFrameBuffer::GetHeight() const
134 {
135   return mHeight;
136 }
137
138
139 } //Render
140
141 } //Internal
142
143 } //Dali