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::GlContextCreated( Context& context )
49 void Geometry::GlContextDestroyed()
53 void Geometry::AddPropertyBuffer( Render::PropertyBuffer* propertyBuffer )
55 mVertexBuffers.PushBack( propertyBuffer );
56 mAttributesChanged = true;
59 void Geometry::SetIndexBuffer( Dali::Vector<unsigned short>& indices )
61 mIndices.Swap( indices );
62 mIndicesChanged = true;
65 void Geometry::RemovePropertyBuffer( const Render::PropertyBuffer* propertyBuffer )
67 size_t bufferCount = mVertexBuffers.Size();
68 for( size_t i(0); i<bufferCount; ++i )
70 if( propertyBuffer == mVertexBuffers[i] )
72 //This will delete the gpu buffer associated to the RenderPropertyBuffer if there is one
73 mVertexBuffers.Remove( mVertexBuffers.Begin()+i);
74 mAttributesChanged = true;
80 void Geometry::GetAttributeLocationFromProgram( Vector<GLint>& attributeLocation, Program& program, BufferIndex bufferIndex ) const
82 attributeLocation.Clear();
84 for( size_t i(0); i< mVertexBuffers.Size(); ++i )
86 unsigned int attributeCount = mVertexBuffers[i]->GetAttributeCount();
87 for( unsigned int j = 0; j < attributeCount; ++j )
89 const std::string& attributeName = mVertexBuffers[i]->GetAttributeName( j );
90 unsigned int index = program.RegisterCustomAttribute( attributeName );
91 GLint location = program.GetCustomAttributeLocation( index );
95 DALI_LOG_WARNING( "Attribute not found in the shader: %s\n", attributeName.c_str() );
98 attributeLocation.PushBack( location );
103 void Geometry::OnRenderFinished()
105 mHasBeenUpdated = false;
106 mAttributesChanged = false;
109 void Geometry::UploadAndDraw(
111 BufferIndex bufferIndex,
112 Vector<GLint>& attributeLocation,
113 size_t elementBufferOffset,
114 size_t elementBufferCount )
116 if( !mHasBeenUpdated )
119 if( mIndicesChanged )
121 if( mIndices.Empty() )
127 if ( mIndexBuffer == NULL )
129 mIndexBuffer = new GpuBuffer( context );
132 std::size_t bufferSize = sizeof( unsigned short ) * mIndices.Size();
133 mIndexBuffer->UpdateDataBuffer( bufferSize, &mIndices[0], GpuBuffer::STATIC_DRAW, GpuBuffer::ELEMENT_ARRAY_BUFFER );
136 mIndicesChanged = false;
139 for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
141 if( !mVertexBuffers[i]->Update( context ) )
143 //Vertex buffer is not ready ( Size, data or format has not been specified yet )
148 mHasBeenUpdated = true;
151 //Bind buffers to attribute locations
152 unsigned int base = 0u;
153 size_t vertexBufferCount(mVertexBuffers.Count());
154 for( unsigned int i = 0; i < vertexBufferCount; ++i )
156 mVertexBuffers[i]->BindBuffer( GpuBuffer::ARRAY_BUFFER );
157 base += mVertexBuffers[i]->EnableVertexAttributes( context, attributeLocation, base );
160 GLenum geometryGLType(0);
161 unsigned int numIndices(0u);
162 intptr_t firstIndexOffset(0u);
165 numIndices = mIndices.Size();
167 if( elementBufferOffset )
169 elementBufferOffset = elementBufferOffset >= numIndices ? numIndices- 1 : elementBufferOffset;
170 firstIndexOffset = elementBufferOffset * sizeof(GLushort);
172 if ( elementBufferCount )
174 numIndices = std::min( elementBufferCount, numIndices - elementBufferOffset );
178 switch(mGeometryType)
180 case Dali::Geometry::TRIANGLES:
182 geometryGLType = GL_TRIANGLES;
185 case Dali::Geometry::LINES:
187 geometryGLType = GL_LINES;
190 case Dali::Geometry::POINTS:
192 geometryGLType = GL_POINTS;
195 case Dali::Geometry::TRIANGLE_STRIP:
197 geometryGLType = GL_TRIANGLE_STRIP;
200 case Dali::Geometry::TRIANGLE_FAN:
202 geometryGLType = GL_TRIANGLE_FAN;
205 case Dali::Geometry::LINE_LOOP:
207 geometryGLType = GL_LINE_LOOP;
210 case Dali::Geometry::LINE_STRIP:
212 geometryGLType = GL_LINE_STRIP;
217 DALI_ASSERT_ALWAYS( 0 && "Geometry type not supported (yet)" );
223 if( mIndexBuffer && geometryGLType != GL_POINTS )
226 mIndexBuffer->Bind( GpuBuffer::ELEMENT_ARRAY_BUFFER );
227 context.DrawElements(geometryGLType, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast<void*>(firstIndexOffset));
232 unsigned int numVertices(0u);
233 if( vertexBufferCount > 0 )
235 numVertices = mVertexBuffers[0]->GetElementCount();
238 context.DrawArrays( geometryGLType, 0, numVertices );
242 for( unsigned int i = 0; i < attributeLocation.Count(); ++i )
244 if( attributeLocation[i] != -1 )
246 context.DisableVertexAttributeArray( attributeLocation[i] );
251 } // namespace SceneGraph
252 } // namespace Internal