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 mRequiresDepthTest(false ),
37 mHasBeenUpdated(false),
38 mAttributesChanged(true)
46 void Geometry::GlContextCreated( Context& context )
50 void Geometry::GlContextDestroyed()
54 void Geometry::AddPropertyBuffer( Render::PropertyBuffer* propertyBuffer )
56 mVertexBuffers.PushBack( propertyBuffer );
57 mAttributesChanged = true;
60 void Geometry::SetIndexBuffer( Dali::Vector<unsigned short>& indices )
62 mIndices.Swap( indices );
63 mIndicesChanged = true;
66 void Geometry::RemovePropertyBuffer( const Render::PropertyBuffer* propertyBuffer )
68 size_t bufferCount = mVertexBuffers.Size();
69 for( size_t i(0); i<bufferCount; ++i )
71 if( propertyBuffer == mVertexBuffers[i] )
73 //This will delete the gpu buffer associated to the RenderPropertyBuffer if there is one
74 mVertexBuffers.Remove( mVertexBuffers.Begin()+i);
75 mAttributesChanged = true;
81 void Geometry::GetAttributeLocationFromProgram( Vector<GLint>& attributeLocation, Program& program, BufferIndex bufferIndex ) const
83 attributeLocation.Clear();
85 for( size_t i(0); i< mVertexBuffers.Size(); ++i )
87 unsigned int attributeCount = mVertexBuffers[i]->GetAttributeCount();
88 for( unsigned int j = 0; j < attributeCount; ++j )
90 const std::string& attributeName = mVertexBuffers[i]->GetAttributeName( j );
91 unsigned int index = program.RegisterCustomAttribute( attributeName );
92 GLint location = program.GetCustomAttributeLocation( index );
96 DALI_LOG_WARNING( "Attribute not found in the shader: %s\n", attributeName.c_str() );
99 attributeLocation.PushBack( location );
104 void Geometry::OnRenderFinished()
106 mHasBeenUpdated = false;
107 mAttributesChanged = false;
110 void Geometry::UploadAndDraw(
112 BufferIndex bufferIndex,
113 Vector<GLint>& attributeLocation,
114 size_t elementBufferOffset,
115 size_t elementBufferCount )
117 if( !mHasBeenUpdated )
120 if( mIndicesChanged )
122 if( mIndices.Empty() )
128 if ( mIndexBuffer == NULL )
130 mIndexBuffer = new GpuBuffer( context );
133 std::size_t bufferSize = sizeof( unsigned short ) * mIndices.Size();
134 mIndexBuffer->UpdateDataBuffer( bufferSize, &mIndices[0], GpuBuffer::STATIC_DRAW, GpuBuffer::ELEMENT_ARRAY_BUFFER );
137 mIndicesChanged = false;
140 for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
142 if( !mVertexBuffers[i]->Update( context ) )
144 //Vertex buffer is not ready ( Size, data or format has not been specified yet )
149 mHasBeenUpdated = true;
152 //Bind buffers to attribute locations
153 unsigned int base = 0u;
154 size_t vertexBufferCount(mVertexBuffers.Count());
155 for( unsigned int i = 0; i < vertexBufferCount; ++i )
157 mVertexBuffers[i]->BindBuffer( GpuBuffer::ARRAY_BUFFER );
158 base += mVertexBuffers[i]->EnableVertexAttributes( context, attributeLocation, base );
161 GLenum geometryGLType(0);
162 unsigned int numIndices(0u);
163 intptr_t firstIndexOffset(0u);
166 numIndices = mIndices.Size();
168 if( elementBufferOffset )
170 elementBufferOffset = elementBufferOffset >= numIndices ? numIndices- 1 : elementBufferOffset;
171 firstIndexOffset = elementBufferOffset * sizeof(GLushort);
173 if ( elementBufferCount )
175 numIndices = std::min( elementBufferCount, numIndices - elementBufferOffset );
179 switch(mGeometryType)
181 case Dali::Geometry::TRIANGLES:
183 geometryGLType = GL_TRIANGLES;
186 case Dali::Geometry::LINES:
188 geometryGLType = GL_LINES;
191 case Dali::Geometry::POINTS:
193 geometryGLType = GL_POINTS;
196 case Dali::Geometry::TRIANGLE_STRIP:
198 geometryGLType = GL_TRIANGLE_STRIP;
201 case Dali::Geometry::TRIANGLE_FAN:
203 geometryGLType = GL_TRIANGLE_FAN;
206 case Dali::Geometry::LINE_LOOP:
208 geometryGLType = GL_LINE_LOOP;
211 case Dali::Geometry::LINE_STRIP:
213 geometryGLType = GL_LINE_STRIP;
218 DALI_ASSERT_ALWAYS( 0 && "Geometry type not supported (yet)" );
224 if( mIndexBuffer && geometryGLType != GL_POINTS )
227 mIndexBuffer->Bind( GpuBuffer::ELEMENT_ARRAY_BUFFER );
228 context.DrawElements(geometryGLType, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast<void*>(firstIndexOffset));
233 unsigned int numVertices(0u);
234 if( vertexBufferCount > 0 )
236 numVertices = mVertexBuffers[0]->GetElementCount();
239 context.DrawArrays( geometryGLType, 0, numVertices );
243 for( unsigned int i = 0; i < attributeLocation.Count(); ++i )
245 if( attributeLocation[i] != -1 )
247 context.DisableVertexAttributeArray( attributeLocation[i] );
252 } // namespace SceneGraph
253 } // namespace Internal