Added setIndicesRange(offset, count) function to the Dali::Renderer which allows...
[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     size_t elementBufferOffset,
121     size_t elementBufferCount )
122 {
123   if( !mHasBeenUpdated )
124   {
125     // Update buffers
126     if( mIndexBuffer )
127     {
128       if(!mIndexBuffer->Update( context, true ) )
129       {
130         //Index buffer is not ready ( Size, data or format has not been specified yet )
131         return;
132       }
133     }
134
135     for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
136     {
137       if( !mVertexBuffers[i]->Update( context, false ) )
138       {
139         //Vertex buffer is not ready ( Size, data or format has not been specified yet )
140         return;
141       }
142     }
143
144     mHasBeenUpdated = true;
145   }
146
147   //Bind buffers to attribute locations
148   unsigned int base = 0;
149   for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
150   {
151     mVertexBuffers[i]->BindBuffer( GpuBuffer::ARRAY_BUFFER );
152     base += mVertexBuffers[i]->EnableVertexAttributes( context, attributeLocation, base );
153   }
154
155   if( mIndexBuffer )
156   {
157     mIndexBuffer->BindBuffer( GpuBuffer::ELEMENT_ARRAY_BUFFER );
158   }
159
160   //Bind index buffer
161   unsigned int numIndices(0u);
162   intptr_t firstIndexOffset(0u);
163   if( mIndexBuffer )
164   {
165     numIndices = mIndexBuffer->GetDataSize() / mIndexBuffer->GetElementSize();
166     if ( elementBufferCount )
167     {
168       numIndices = elementBufferCount > numIndices ? numIndices : elementBufferCount;
169     }
170     firstIndexOffset = elementBufferOffset * sizeof(GLushort);
171   }
172
173   //Draw call
174   switch(mGeometryType)
175   {
176     case Dali::Geometry::TRIANGLES:
177     {
178       if( numIndices )
179       {
180         context.DrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast<void*>(firstIndexOffset) );
181       }
182       else
183       {
184         unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
185         context.DrawArrays( GL_TRIANGLES, 0, numVertices );
186       }
187       break;
188     }
189     case Dali::Geometry::LINES:
190     {
191       if( numIndices )
192       {
193         context.DrawElements(GL_LINES, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast<void*>(firstIndexOffset) );
194       }
195       else
196       {
197         unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
198         context.DrawArrays( GL_LINES, 0, numVertices );
199       }
200       break;
201     }
202     case Dali::Geometry::POINTS:
203     {
204       unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
205       context.DrawArrays(GL_POINTS, 0, numVertices );
206       break;
207     }
208     case Dali::Geometry::TRIANGLE_STRIP:
209     {
210       if( numIndices )
211       {
212         context.DrawElements(GL_TRIANGLE_STRIP, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast<void*>(firstIndexOffset) );
213       }
214       else
215       {
216         unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
217         context.DrawArrays(GL_TRIANGLE_STRIP, 0, numVertices );
218       }
219       break;
220     }
221     case Dali::Geometry::TRIANGLE_FAN:
222     {
223       if( numIndices )
224       {
225         context.DrawElements(GL_TRIANGLE_FAN, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast<void*>(firstIndexOffset) );
226       }
227       else
228       {
229         unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
230         context.DrawArrays(GL_TRIANGLE_FAN, 0, numVertices );
231       }
232       break;
233     }
234     case Dali::Geometry::LINE_LOOP:
235     {
236       if( numIndices )
237       {
238         context.DrawElements(GL_LINE_LOOP, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast<void*>(firstIndexOffset) );
239       }
240       else
241       {
242         unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
243         context.DrawArrays(GL_LINE_LOOP, 0, numVertices );
244       }
245       break;
246     }
247     case Dali::Geometry::LINE_STRIP:
248     {
249       if( numIndices )
250       {
251         context.DrawElements(GL_LINE_STRIP, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast<void*>(firstIndexOffset) );
252       }
253       else
254       {
255         unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
256         context.DrawArrays(GL_LINE_STRIP, 0, numVertices );
257       }
258       break;
259     }
260     default:
261     {
262       DALI_ASSERT_ALWAYS( 0 && "Geometry type not supported (yet)" );
263       break;
264     }
265   }
266
267   //Disable atrributes
268   for( unsigned int i = 0; i < attributeLocation.Count(); ++i )
269   {
270     if( attributeLocation[i] != -1 )
271     {
272       context.DisableVertexAttributeArray( attributeLocation[i] );
273     }
274   }
275 }
276
277 } // namespace SceneGraph
278 } // namespace Internal
279 } // namespace Dali