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>
34 mGeometryType( Dali::Geometry::TRIANGLES ),
35 mIndicesChanged(false),
36 mHasBeenUpdated(false),
37 mAttributesChanged(true)
45 void Geometry::AddPropertyBuffer( Render::PropertyBuffer* propertyBuffer )
47 mVertexBuffers.PushBack( propertyBuffer );
48 mAttributesChanged = true;
51 void Geometry::SetIndexBuffer( Dali::Vector<unsigned short>& indices )
53 mIndices.Swap( indices );
54 mIndicesChanged = true;
57 void Geometry::RemovePropertyBuffer( const Render::PropertyBuffer* propertyBuffer )
59 size_t bufferCount = mVertexBuffers.Size();
60 for( size_t i(0); i<bufferCount; ++i )
62 if( propertyBuffer == mVertexBuffers[i] )
64 //This will delete the gpu buffer associated to the RenderPropertyBuffer if there is one
65 mVertexBuffers.Remove( mVertexBuffers.Begin()+i );
66 mAttributesChanged = true;
74 void Geometry::GetAttributeLocationFromProgram( Vector<GLint>& attributeLocation, Program& program, BufferIndex bufferIndex ) const
76 attributeLocation.Clear();
78 for( size_t i(0); i< mVertexBuffers.Size(); ++i )
80 unsigned int attributeCount = mVertexBuffers[i]->GetAttributeCount();
81 for( unsigned int j = 0; j < attributeCount; ++j )
83 const std::string& attributeName = mVertexBuffers[i]->GetAttributeName( j );
84 unsigned int index = program.RegisterCustomAttribute( attributeName );
85 GLint location = program.GetCustomAttributeLocation( index );
89 DALI_LOG_WARNING( "Attribute not found in the shader: %s\n", attributeName.c_str() );
92 attributeLocation.PushBack( location );
97 void Geometry::OnRenderFinished()
99 mHasBeenUpdated = false;
100 mAttributesChanged = false;
103 void Geometry::UploadAndDraw(
104 Graphics::API::Controller& controller,
105 BufferIndex bufferIndex,
106 Vector<GLint>& attributeLocation,
107 size_t elementBufferOffset,
108 size_t elementBufferCount )
111 if( !mHasBeenUpdated )
114 if( mIndicesChanged )
116 if( mIndices.Empty() )
122 if ( mIndexBuffer == NULL )
124 mIndexBuffer = new GpuBuffer( context );
127 std::size_t bufferSize = sizeof( unsigned short ) * mIndices.Size();
128 mIndexBuffer->UpdateDataBuffer( bufferSize, &mIndices[0], GpuBuffer::STATIC_DRAW, GpuBuffer::ELEMENT_ARRAY_BUFFER );
131 mIndicesChanged = false;
134 size_t count = mVertexBuffers.Count();
135 for( unsigned int i = 0; i < count; ++i )
138 if( !mVertexBuffers[i]->Update( ) )
140 //Vertex buffer is not ready ( Size, data or format has not been specified yet )
145 mHasBeenUpdated = true;
148 //Bind buffers to attribute locations
149 unsigned int base = 0u;
150 size_t vertexBufferCount(mVertexBuffers.Count());
151 for( unsigned int i = 0; i < vertexBufferCount; ++i )
153 mVertexBuffers[i]->BindBuffer( GpuBuffer::ARRAY_BUFFER );
154 base += mVertexBuffers[i]->EnableVertexAttributes( context, attributeLocation, base );
157 size_t numIndices(0u);
158 intptr_t firstIndexOffset(0u);
161 numIndices = mIndices.Size();
163 if( elementBufferOffset != 0u )
165 elementBufferOffset = elementBufferOffset >= numIndices ? numIndices - 1 : elementBufferOffset;
166 firstIndexOffset = elementBufferOffset * sizeof(GLushort);
167 numIndices -= elementBufferOffset;
170 if( elementBufferCount != 0u )
172 numIndices = std::min( elementBufferCount, numIndices );
176 GLenum geometryGLType(GL_NONE);
177 switch(mGeometryType)
179 case Dali::Geometry::TRIANGLES:
181 geometryGLType = GL_TRIANGLES;
184 case Dali::Geometry::LINES:
186 geometryGLType = GL_LINES;
189 case Dali::Geometry::POINTS:
191 geometryGLType = GL_POINTS;
194 case Dali::Geometry::TRIANGLE_STRIP:
196 geometryGLType = GL_TRIANGLE_STRIP;
199 case Dali::Geometry::TRIANGLE_FAN:
201 geometryGLType = GL_TRIANGLE_FAN;
204 case Dali::Geometry::LINE_LOOP:
206 geometryGLType = GL_LINE_LOOP;
209 case Dali::Geometry::LINE_STRIP:
211 geometryGLType = GL_LINE_STRIP;
217 if( mIndexBuffer && geometryGLType != GL_POINTS )
220 mIndexBuffer->Bind( GpuBuffer::ELEMENT_ARRAY_BUFFER );
221 context.DrawElements(geometryGLType, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast<void*>(firstIndexOffset));
226 unsigned int numVertices(0u);
227 if( vertexBufferCount > 0 )
229 numVertices = mVertexBuffers[0]->GetElementCount();
232 context.DrawArrays( geometryGLType, 0, numVertices );
236 for( unsigned int i = 0; i < attributeLocation.Count(); ++i )
238 if( attributeLocation[i] != -1 )
240 context.DisableVertexAttributeArray( attributeLocation[i] );
246 } // namespace SceneGraph
247 } // namespace Internal