2 * Copyright (c) 2018 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.
18 #include <dali/internal/render/renderers/render-geometry.h>
21 #include <dali/internal/common/buffer-index.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>
37 mGeometryType( Dali::Geometry::TRIANGLES ),
38 mIndicesChanged(false),
39 mHasBeenUpdated(false),
40 mAttributesChanged(true)
48 void Geometry::GlContextCreated( Context& context )
52 void Geometry::GlContextDestroyed()
56 void Geometry::AddPropertyBuffer( Render::PropertyBuffer* propertyBuffer )
58 mVertexBuffers.PushBack( propertyBuffer );
59 mAttributesChanged = true;
62 void Geometry::SetIndexBuffer( Dali::Vector<uint16_t>& indices )
64 mIndices.Swap( indices );
65 mIndicesChanged = true;
68 void Geometry::RemovePropertyBuffer( const Render::PropertyBuffer* propertyBuffer )
70 const auto&& end = mVertexBuffers.End();
71 for( auto&& iter = mVertexBuffers.Begin(); iter != end; ++iter )
73 if( *iter == propertyBuffer )
75 //This will delete the gpu buffer associated to the RenderPropertyBuffer if there is one
76 mVertexBuffers.Remove( iter );
77 mAttributesChanged = true;
83 void Geometry::GetAttributeLocationFromProgram( Vector<GLint>& attributeLocation, Program& program, BufferIndex bufferIndex ) const
85 attributeLocation.Clear();
87 for( auto&& vertexBuffer : mVertexBuffers )
89 const uint32_t attributeCount = vertexBuffer->GetAttributeCount();
90 for( uint32_t j = 0; j < attributeCount; ++j )
92 const std::string& attributeName = vertexBuffer->GetAttributeName( j );
93 uint32_t index = program.RegisterCustomAttribute( attributeName );
94 GLint location = program.GetCustomAttributeLocation( index );
98 DALI_LOG_WARNING( "Attribute not found in the shader: %s\n", attributeName.c_str() );
101 attributeLocation.PushBack( location );
106 void Geometry::OnRenderFinished()
108 mHasBeenUpdated = false;
109 mAttributesChanged = false;
112 void Geometry::UploadAndDraw(
114 BufferIndex bufferIndex,
115 Vector<GLint>& attributeLocation,
116 uint32_t elementBufferOffset,
117 uint32_t elementBufferCount )
119 if( !mHasBeenUpdated )
122 if( mIndicesChanged )
124 if( mIndices.Empty() )
130 if ( mIndexBuffer == NULL )
132 mIndexBuffer = new GpuBuffer( context );
135 uint32_t bufferSize = static_cast<uint32_t>( sizeof( uint16_t ) * mIndices.Size() );
136 mIndexBuffer->UpdateDataBuffer( bufferSize, &mIndices[0], GpuBuffer::STATIC_DRAW, GpuBuffer::ELEMENT_ARRAY_BUFFER );
139 mIndicesChanged = false;
142 for( auto&& buffer : mVertexBuffers )
144 if( !buffer->Update( context ) )
146 //Vertex buffer is not ready ( Size, data or format has not been specified yet )
151 mHasBeenUpdated = true;
154 //Bind buffers to attribute locations
156 const uint32_t vertexBufferCount = static_cast<uint32_t>( mVertexBuffers.Count() );
157 for( uint32_t i = 0; i < vertexBufferCount; ++i )
159 mVertexBuffers[i]->BindBuffer( GpuBuffer::ARRAY_BUFFER );
160 base += mVertexBuffers[i]->EnableVertexAttributes( context, attributeLocation, base );
163 uint32_t numIndices(0u);
164 intptr_t firstIndexOffset(0u);
167 numIndices = static_cast<uint32_t>( mIndices.Size() );
169 if( elementBufferOffset != 0u )
171 elementBufferOffset = (elementBufferOffset >= numIndices ) ? numIndices - 1 : elementBufferOffset;
172 firstIndexOffset = elementBufferOffset * sizeof(GLushort);
173 numIndices -= elementBufferOffset;
176 if( elementBufferCount != 0u )
178 numIndices = std::min( elementBufferCount, numIndices );
182 GLenum geometryGLType(GL_NONE);
183 switch(mGeometryType)
185 case Dali::Geometry::TRIANGLES:
187 geometryGLType = GL_TRIANGLES;
190 case Dali::Geometry::LINES:
192 geometryGLType = GL_LINES;
195 case Dali::Geometry::POINTS:
197 geometryGLType = GL_POINTS;
200 case Dali::Geometry::TRIANGLE_STRIP:
202 geometryGLType = GL_TRIANGLE_STRIP;
205 case Dali::Geometry::TRIANGLE_FAN:
207 geometryGLType = GL_TRIANGLE_FAN;
210 case Dali::Geometry::LINE_LOOP:
212 geometryGLType = GL_LINE_LOOP;
215 case Dali::Geometry::LINE_STRIP:
217 geometryGLType = GL_LINE_STRIP;
223 if( mIndexBuffer && geometryGLType != GL_POINTS )
226 mIndexBuffer->Bind( GpuBuffer::ELEMENT_ARRAY_BUFFER );
227 // numIndices truncated, no value loss happening in practice
228 context.DrawElements( geometryGLType, static_cast<GLsizei>( numIndices ), GL_UNSIGNED_SHORT, reinterpret_cast<void*>( firstIndexOffset ) );
233 GLsizei numVertices(0u);
234 if( vertexBufferCount > 0 )
236 // truncated, no value loss happening in practice
237 numVertices = static_cast<GLsizei>( mVertexBuffers[0]->GetElementCount() );
240 context.DrawArrays( geometryGLType, 0, numVertices );
244 for( auto&& attribute : attributeLocation )
246 if( attribute != -1 )
248 context.DisableVertexAttributeArray( static_cast<GLuint>( attribute ) );
253 } // namespace SceneGraph
254 } // namespace Internal