[dali_1.2.27] Merge branch '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 Render
29 {
30
31 Geometry::Geometry()
32 : mIndices(),
33   mIndexBuffer(NULL),
34   mGeometryType( Dali::Geometry::TRIANGLES ),
35   mIndicesChanged(false),
36   mHasBeenUpdated(false),
37   mAttributesChanged(true)
38 {
39 }
40
41 Geometry::~Geometry()
42 {
43 }
44
45 void Geometry::GlContextCreated( Context& context )
46 {
47 }
48
49 void Geometry::GlContextDestroyed()
50 {
51 }
52
53 void Geometry::AddPropertyBuffer( Render::PropertyBuffer* propertyBuffer )
54 {
55   mVertexBuffers.PushBack( propertyBuffer );
56   mAttributesChanged = true;
57 }
58
59 void Geometry::SetIndexBuffer( Dali::Vector<unsigned short>& indices )
60 {
61   mIndices.Swap( indices );
62   mIndicesChanged = true;
63 }
64
65 void Geometry::RemovePropertyBuffer( const Render::PropertyBuffer* propertyBuffer )
66 {
67   size_t bufferCount = mVertexBuffers.Size();
68   for( size_t i(0); i<bufferCount; ++i )
69   {
70     if( propertyBuffer == mVertexBuffers[i] )
71     {
72       //This will delete the gpu buffer associated to the RenderPropertyBuffer if there is one
73       mVertexBuffers.Remove( mVertexBuffers.Begin()+i );
74       mAttributesChanged = true;
75       break;
76     }
77   }
78 }
79
80 void Geometry::GetAttributeLocationFromProgram( Vector<GLint>& attributeLocation, Program& program, BufferIndex bufferIndex ) const
81 {
82   attributeLocation.Clear();
83
84   for( size_t i(0); i< mVertexBuffers.Size(); ++i )
85   {
86     unsigned int attributeCount = mVertexBuffers[i]->GetAttributeCount();
87     for( unsigned int j = 0; j < attributeCount; ++j )
88     {
89       const std::string& attributeName = mVertexBuffers[i]->GetAttributeName( j );
90       unsigned int index = program.RegisterCustomAttribute( attributeName );
91       GLint location = program.GetCustomAttributeLocation( index );
92
93       if( -1 == location )
94       {
95         DALI_LOG_WARNING( "Attribute not found in the shader: %s\n", attributeName.c_str() );
96       }
97
98       attributeLocation.PushBack( location );
99     }
100   }
101 }
102
103 void Geometry::OnRenderFinished()
104 {
105   mHasBeenUpdated = false;
106   mAttributesChanged = false;
107 }
108
109 void Geometry::UploadAndDraw(
110     Context& context,
111     BufferIndex bufferIndex,
112     Vector<GLint>& attributeLocation,
113     size_t elementBufferOffset,
114     size_t elementBufferCount )
115 {
116   if( !mHasBeenUpdated )
117   {
118     // Update buffers
119     if( mIndicesChanged )
120     {
121       if( mIndices.Empty() )
122       {
123         mIndexBuffer = NULL;
124       }
125       else
126       {
127         if ( mIndexBuffer == NULL )
128         {
129           mIndexBuffer = new GpuBuffer( context );
130         }
131
132         std::size_t bufferSize =  sizeof( unsigned short ) * mIndices.Size();
133         mIndexBuffer->UpdateDataBuffer( bufferSize, &mIndices[0], GpuBuffer::STATIC_DRAW, GpuBuffer::ELEMENT_ARRAY_BUFFER );
134       }
135
136       mIndicesChanged = false;
137     }
138
139     size_t count = mVertexBuffers.Count();
140     for( unsigned int i = 0; i < count; ++i )
141     {
142
143       if( !mVertexBuffers[i]->Update( context ) )
144       {
145         //Vertex buffer is not ready ( Size, data or format has not been specified yet )
146         return;
147       }
148     }
149
150     mHasBeenUpdated = true;
151   }
152
153   //Bind buffers to attribute locations
154   unsigned int base = 0u;
155   size_t vertexBufferCount(mVertexBuffers.Count());
156   for( unsigned int i = 0; i < vertexBufferCount; ++i )
157   {
158     mVertexBuffers[i]->BindBuffer( GpuBuffer::ARRAY_BUFFER );
159     base += mVertexBuffers[i]->EnableVertexAttributes( context, attributeLocation, base );
160   }
161
162   size_t numIndices(0u);
163   intptr_t firstIndexOffset(0u);
164   if( mIndexBuffer )
165   {
166     numIndices = mIndices.Size();
167
168     if( elementBufferOffset != 0u )
169     {
170       elementBufferOffset = elementBufferOffset >= numIndices ? numIndices - 1 : elementBufferOffset;
171       firstIndexOffset = elementBufferOffset * sizeof(GLushort);
172       numIndices -= elementBufferOffset;
173     }
174
175     if( elementBufferCount != 0u )
176     {
177       numIndices = std::min( elementBufferCount, numIndices );
178     }
179   }
180
181   GLenum geometryGLType(GL_NONE);
182   switch(mGeometryType)
183   {
184     case Dali::Geometry::TRIANGLES:
185     {
186       geometryGLType = GL_TRIANGLES;
187       break;
188     }
189     case Dali::Geometry::LINES:
190     {
191       geometryGLType = GL_LINES;
192       break;
193     }
194     case Dali::Geometry::POINTS:
195     {
196       geometryGLType = GL_POINTS;
197       break;
198     }
199     case Dali::Geometry::TRIANGLE_STRIP:
200     {
201       geometryGLType = GL_TRIANGLE_STRIP;
202       break;
203     }
204     case Dali::Geometry::TRIANGLE_FAN:
205     {
206       geometryGLType = GL_TRIANGLE_FAN;
207       break;
208     }
209     case Dali::Geometry::LINE_LOOP:
210     {
211       geometryGLType = GL_LINE_LOOP;
212       break;
213     }
214     case Dali::Geometry::LINE_STRIP:
215     {
216       geometryGLType = GL_LINE_STRIP;
217       break;
218     }
219   }
220
221   //Draw call
222   if( mIndexBuffer && geometryGLType != GL_POINTS )
223   {
224     //Indexed draw call
225     mIndexBuffer->Bind( GpuBuffer::ELEMENT_ARRAY_BUFFER );
226     context.DrawElements(geometryGLType, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast<void*>(firstIndexOffset));
227   }
228   else
229   {
230     //Unindex draw call
231     unsigned int numVertices(0u);
232     if( vertexBufferCount > 0 )
233     {
234       numVertices = mVertexBuffers[0]->GetElementCount();
235     }
236
237     context.DrawArrays( geometryGLType, 0, numVertices );
238   }
239
240   //Disable attributes
241   for( unsigned int i = 0; i < attributeLocation.Count(); ++i )
242   {
243     if( attributeLocation[i] != -1 )
244     {
245       context.DisableVertexAttributeArray( attributeLocation[i] );
246     }
247   }
248 }
249
250 } // namespace SceneGraph
251 } // namespace Internal
252 } // namespace Dali