Merge "Remove double-buffered properties from SceneGraph::Geometry" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-geometry.cpp
1 /*
2  * Copyright (c) 2015 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 #include <dali/internal/render/renderers/render-geometry.h>
18 #include <dali/internal/common/buffer-index.h>
19 #include <dali/internal/render/gl-resources/context.h>
20 #include <dali/internal/render/gl-resources/gpu-buffer.h>
21 #include <dali/internal/render/renderers/render-property-buffer.h>
22 #include <dali/internal/render/shaders/program.h>
23
24 namespace Dali
25 {
26 namespace Internal
27 {
28 namespace SceneGraph
29 {
30
31 RenderGeometry::RenderGeometry( GeometryType type, bool requiresDepthTest )
32 : mIndexBuffer(0),
33   mGeometryType( type ),
34   mRequiresDepthTest(requiresDepthTest ),
35   mHasBeenUpdated(false),
36   mAttributesChanged(true)
37 {
38 }
39
40 RenderGeometry::~RenderGeometry()
41 {
42 }
43
44 void RenderGeometry::GlContextCreated( Context& context )
45 {
46 }
47
48 void RenderGeometry::GlContextDestroyed()
49 {
50 }
51
52 void RenderGeometry::AddPropertyBuffer( Render::PropertyBuffer* propertyBuffer, bool isIndexBuffer )
53 {
54   if( isIndexBuffer )
55   {
56     mIndexBuffer = propertyBuffer;
57   }
58   else
59   {
60     mVertexBuffers.PushBack( propertyBuffer );
61     mAttributesChanged = true;
62   }
63 }
64
65 void RenderGeometry::RemovePropertyBuffer( const Render::PropertyBuffer* propertyBuffer )
66 {
67   if( propertyBuffer == mIndexBuffer )
68   {
69     mIndexBuffer = 0;
70   }
71   else
72   {
73     size_t bufferCount = mVertexBuffers.Size();
74     for( size_t i(0); i<bufferCount; ++i )
75     {
76       if( propertyBuffer == mVertexBuffers[i] )
77       {
78         //This will delete the gpu buffer associated to the RenderPropertyBuffer if there is one
79         mVertexBuffers.Remove( mVertexBuffers.Begin()+i);
80         mAttributesChanged = true;
81         break;
82       }
83     }
84   }
85 }
86
87 void RenderGeometry::GetAttributeLocationFromProgram( Vector<GLint>& attributeLocation, Program& program, BufferIndex bufferIndex ) const
88 {
89   attributeLocation.Clear();
90
91   for( size_t i(0); i< mVertexBuffers.Size(); ++i )
92   {
93     unsigned int attributeCount = mVertexBuffers[i]->GetAttributeCount();
94     for( unsigned int j = 0; j < attributeCount; ++j )
95     {
96       const std::string& attributeName = mVertexBuffers[i]->GetAttributeName( j );
97       unsigned int index = program.RegisterCustomAttribute( attributeName );
98       GLint location = program.GetCustomAttributeLocation( index );
99
100       if( -1 == location )
101       {
102         DALI_LOG_WARNING( "Attribute not found in the shader: %s\n", attributeName.c_str() );
103       }
104
105       attributeLocation.PushBack( location );
106     }
107   }
108 }
109
110 void RenderGeometry::OnRenderFinished()
111 {
112   mHasBeenUpdated = false;
113   mAttributesChanged = false;
114 }
115
116 void RenderGeometry::UploadAndDraw(
117     Context& context,
118     BufferIndex bufferIndex,
119     Vector<GLint>& attributeLocation )
120 {
121   if( !mHasBeenUpdated )
122   {
123     //Update buffers
124     if( mIndexBuffer )
125     {
126       if(!mIndexBuffer->Update( context, true ) )
127       {
128         //Index buffer is not ready ( Size, data or format has not been specified yet )
129         return;
130       }
131     }
132     for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
133     {
134       if( !mVertexBuffers[i]->Update( context, false ) )
135       {
136         //Vertex buffer is not ready ( Size, data or format has not been specified yet )
137         return;
138       }
139     }
140     mHasBeenUpdated = true;
141   }
142
143   //Bind buffers to attribute locations
144   unsigned int base = 0;
145   for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
146   {
147     mVertexBuffers[i]->BindBuffer( GpuBuffer::ARRAY_BUFFER );
148     base += mVertexBuffers[i]->EnableVertexAttributes( context, attributeLocation, base );
149   }
150
151   if( mIndexBuffer )
152   {
153     mIndexBuffer->BindBuffer( GpuBuffer::ELEMENT_ARRAY_BUFFER );
154   }
155
156   //Bind index buffer
157   unsigned int numIndices(0u);
158   if( mIndexBuffer )
159   {
160     numIndices = mIndexBuffer->GetDataSize() / mIndexBuffer->GetElementSize();
161   }
162
163   //Draw call
164   switch(mGeometryType)
165   {
166     case Dali::Geometry::TRIANGLES:
167     {
168       if( numIndices )
169       {
170         context.DrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
171       }
172       else
173       {
174         unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
175         context.DrawArrays( GL_TRIANGLES, 0, numVertices );
176       }
177       break;
178     }
179     case Dali::Geometry::LINES:
180     {
181       if( numIndices )
182       {
183         context.DrawElements(GL_LINES, numIndices, GL_UNSIGNED_SHORT, 0);
184       }
185       else
186       {
187         unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
188         context.DrawArrays( GL_LINES, 0, numVertices );
189       }
190       break;
191     }
192     case Dali::Geometry::POINTS:
193     {
194       unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
195       context.DrawArrays(GL_POINTS, 0, numVertices );
196       break;
197     }
198     case Dali::Geometry::TRIANGLE_STRIP:
199     {
200       if( numIndices )
201       {
202         context.DrawElements(GL_TRIANGLE_STRIP, numIndices, GL_UNSIGNED_SHORT, 0);
203       }
204       else
205       {
206         unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
207         context.DrawArrays(GL_TRIANGLE_STRIP, 0, numVertices );
208       }
209       break;
210     }
211     case Dali::Geometry::TRIANGLE_FAN:
212     {
213       if( numIndices )
214       {
215         context.DrawElements(GL_TRIANGLE_FAN, numIndices, GL_UNSIGNED_SHORT, 0);
216       }
217       else
218       {
219         unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
220         context.DrawArrays(GL_TRIANGLE_FAN, 0, numVertices );
221       }
222       break;
223     }
224     default:
225     {
226       DALI_ASSERT_ALWAYS( 0 && "Geometry type not supported (yet)" );
227       break;
228     }
229   }
230
231   //Disable atrributes
232   for( unsigned int i = 0; i < attributeLocation.Count(); ++i )
233   {
234     if( attributeLocation[i] != -1 )
235     {
236       context.DisableVertexAttributeArray( attributeLocation[i] );
237     }
238   }
239 }
240
241 } // namespace SceneGraph
242 } // namespace Internal
243 } // namespace Dali