2 * Copyright (c) 2015 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.
17 #include <dali/internal/render/renderers/render-geometry.h>
19 #include <dali/internal/common/buffer-index.h>
20 #include <dali/internal/update/rendering/scene-graph-geometry.h>
21 #include <dali/internal/render/data-providers/render-data-provider.h>
22 #include <dali/internal/render/gl-resources/context.h>
23 #include <dali/internal/render/gl-resources/gpu-buffer.h>
24 #include <dali/internal/render/renderers/render-property-buffer.h>
25 #include <dali/internal/render/shaders/program.h>
34 RenderGeometry::RenderGeometry( const GeometryDataProvider& geometryDataProvider )
35 : mGeometryDataProvider( geometryDataProvider ),
37 mHasBeenUpdated(false),
38 mAttributesChanged(true)
42 RenderGeometry::~RenderGeometry()
46 void RenderGeometry::GlContextCreated( Context& context )
50 void RenderGeometry::GlContextDestroyed()
54 void RenderGeometry::AddPropertyBuffer( Render::PropertyBuffer* propertyBuffer, bool isIndexBuffer )
58 mIndexBuffer = propertyBuffer;
62 mVertexBuffers.PushBack( propertyBuffer );
63 mAttributesChanged = true;
67 void RenderGeometry::RemovePropertyBuffer( const Render::PropertyBuffer* propertyBuffer )
69 if( propertyBuffer == mIndexBuffer )
75 size_t bufferCount = mVertexBuffers.Size();
76 for( size_t i(0); i<bufferCount; ++i )
78 if( propertyBuffer == mVertexBuffers[i] )
80 //This will delete the gpu buffer associated to the RenderPropertyBuffer if there is one
81 mVertexBuffers.Remove( mVertexBuffers.Begin()+i);
82 mAttributesChanged = true;
89 void RenderGeometry::GetAttributeLocationFromProgram( Vector<GLint>& attributeLocation, Program& program, BufferIndex bufferIndex ) const
91 attributeLocation.Clear();
93 for( size_t i(0); i< mVertexBuffers.Size(); ++i )
95 unsigned int attributeCount = mVertexBuffers[i]->GetAttributeCount();
96 for( unsigned int j = 0; j < attributeCount; ++j )
98 const std::string& attributeName = mVertexBuffers[i]->GetAttributeName( j );
99 unsigned int index = program.RegisterCustomAttribute( attributeName );
100 GLint location = program.GetCustomAttributeLocation( index );
104 DALI_LOG_WARNING( "Attribute not found in the shader: %s\n", attributeName.c_str() );
107 attributeLocation.PushBack( location );
112 void RenderGeometry::OnRenderFinished()
114 mHasBeenUpdated = false;
115 mAttributesChanged = false;
118 void RenderGeometry::UploadAndDraw(
120 BufferIndex bufferIndex,
121 Vector<GLint>& attributeLocation )
123 if( !mHasBeenUpdated )
128 if(!mIndexBuffer->Update( context, true ) )
130 //Index buffer is not ready ( Size, data or format has not been specified yet )
134 for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
136 if( !mVertexBuffers[i]->Update( context, false ) )
138 //Vertex buffer is not ready ( Size, data or format has not been specified yet )
142 mHasBeenUpdated = true;
145 //Bind buffers to attribute locations
146 unsigned int base = 0;
147 for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
149 mVertexBuffers[i]->BindBuffer( GpuBuffer::ARRAY_BUFFER );
150 base += mVertexBuffers[i]->EnableVertexAttributes( context, attributeLocation, base );
155 mIndexBuffer->BindBuffer( GpuBuffer::ELEMENT_ARRAY_BUFFER );
159 unsigned int numIndices(0u);
162 numIndices = mIndexBuffer->GetDataSize() / mIndexBuffer->GetElementSize();
166 GeometryDataProvider::GeometryType type = mGeometryDataProvider.GetGeometryType( bufferIndex );
169 case Dali::Geometry::TRIANGLES:
173 context.DrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
177 unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
178 context.DrawArrays( GL_TRIANGLES, 0, numVertices );
182 case Dali::Geometry::LINES:
186 context.DrawElements(GL_LINES, numIndices, GL_UNSIGNED_SHORT, 0);
190 unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
191 context.DrawArrays( GL_LINES, 0, numVertices );
195 case Dali::Geometry::POINTS:
197 unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
198 context.DrawArrays(GL_POINTS, 0, numVertices );
201 case Dali::Geometry::TRIANGLE_STRIP:
205 context.DrawElements(GL_TRIANGLE_STRIP, numIndices, GL_UNSIGNED_SHORT, 0);
209 unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
210 context.DrawArrays(GL_TRIANGLE_STRIP, 0, numVertices );
214 case Dali::Geometry::TRIANGLE_FAN:
218 context.DrawElements(GL_TRIANGLE_FAN, numIndices, GL_UNSIGNED_SHORT, 0);
222 unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
223 context.DrawArrays(GL_TRIANGLE_FAN, 0, numVertices );
229 DALI_ASSERT_ALWAYS( 0 && "Geometry type not supported (yet)" );
235 for( unsigned int i = 0; i < attributeLocation.Count(); ++i )
237 if( attributeLocation[i] != -1 )
239 context.DisableVertexAttributeArray( attributeLocation[i] );
244 } // namespace SceneGraph
245 } // namespace Internal