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>
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>
31 RenderGeometry::RenderGeometry( GeometryType type, bool requiresDepthTest )
33 mGeometryType( type ),
34 mRequiresDepthTest(requiresDepthTest ),
35 mHasBeenUpdated(false),
36 mAttributesChanged(true)
40 RenderGeometry::~RenderGeometry()
44 void RenderGeometry::GlContextCreated( Context& context )
48 void RenderGeometry::GlContextDestroyed()
52 void RenderGeometry::AddPropertyBuffer( Render::PropertyBuffer* propertyBuffer, bool isIndexBuffer )
56 mIndexBuffer = propertyBuffer;
60 mVertexBuffers.PushBack( propertyBuffer );
61 mAttributesChanged = true;
65 void RenderGeometry::RemovePropertyBuffer( const Render::PropertyBuffer* propertyBuffer )
67 if( propertyBuffer == mIndexBuffer )
73 size_t bufferCount = mVertexBuffers.Size();
74 for( size_t i(0); i<bufferCount; ++i )
76 if( propertyBuffer == mVertexBuffers[i] )
78 //This will delete the gpu buffer associated to the RenderPropertyBuffer if there is one
79 mVertexBuffers.Remove( mVertexBuffers.Begin()+i);
80 mAttributesChanged = true;
87 void RenderGeometry::GetAttributeLocationFromProgram( Vector<GLint>& attributeLocation, Program& program, BufferIndex bufferIndex ) const
89 attributeLocation.Clear();
91 for( size_t i(0); i< mVertexBuffers.Size(); ++i )
93 unsigned int attributeCount = mVertexBuffers[i]->GetAttributeCount();
94 for( unsigned int j = 0; j < attributeCount; ++j )
96 const std::string& attributeName = mVertexBuffers[i]->GetAttributeName( j );
97 unsigned int index = program.RegisterCustomAttribute( attributeName );
98 GLint location = program.GetCustomAttributeLocation( index );
102 DALI_LOG_WARNING( "Attribute not found in the shader: %s\n", attributeName.c_str() );
105 attributeLocation.PushBack( location );
110 void RenderGeometry::OnRenderFinished()
112 mHasBeenUpdated = false;
113 mAttributesChanged = false;
116 void RenderGeometry::UploadAndDraw(
118 BufferIndex bufferIndex,
119 Vector<GLint>& attributeLocation )
121 if( !mHasBeenUpdated )
126 if(!mIndexBuffer->Update( context, true ) )
128 //Index buffer is not ready ( Size, data or format has not been specified yet )
133 for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
135 if( !mVertexBuffers[i]->Update( context, false ) )
137 //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 switch(mGeometryType)
168 case Dali::Geometry::TRIANGLES:
172 context.DrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
176 unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
177 context.DrawArrays( GL_TRIANGLES, 0, numVertices );
181 case Dali::Geometry::LINES:
185 context.DrawElements(GL_LINES, numIndices, GL_UNSIGNED_SHORT, 0);
189 unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
190 context.DrawArrays( GL_LINES, 0, numVertices );
194 case Dali::Geometry::POINTS:
196 unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
197 context.DrawArrays(GL_POINTS, 0, numVertices );
200 case Dali::Geometry::TRIANGLE_STRIP:
204 context.DrawElements(GL_TRIANGLE_STRIP, numIndices, GL_UNSIGNED_SHORT, 0);
208 unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
209 context.DrawArrays(GL_TRIANGLE_STRIP, 0, numVertices );
213 case Dali::Geometry::TRIANGLE_FAN:
217 context.DrawElements(GL_TRIANGLE_FAN, numIndices, GL_UNSIGNED_SHORT, 0);
221 unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
222 context.DrawArrays(GL_TRIANGLE_FAN, 0, numVertices );
228 DALI_ASSERT_ALWAYS( 0 && "Geometry type not supported (yet)" );
234 for( unsigned int i = 0; i < attributeLocation.Count(); ++i )
236 if( attributeLocation[i] != -1 )
238 context.DisableVertexAttributeArray( attributeLocation[i] );
243 } // namespace SceneGraph
244 } // namespace Internal