X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali%2Finternal%2Frender%2Frenderers%2Frender-geometry.cpp;h=17ba522691c97bfef71721a44a1e8190662f4a42;hb=403f5989d41f4e5c5a657e24020fe32c77fc1414;hp=24a952221b81a2a285075ffda0f195086b46d0d4;hpb=b823ef4b5c9307244f043b3006344aa9db8e3b39;p=platform%2Fcore%2Fuifw%2Fdali-core.git diff --git a/dali/internal/render/renderers/render-geometry.cpp b/dali/internal/render/renderers/render-geometry.cpp index 24a9522..17ba522 100644 --- a/dali/internal/render/renderers/render-geometry.cpp +++ b/dali/internal/render/renderers/render-geometry.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * Copyright (c) 2018 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,10 @@ * limitations under the License. */ +// CLASS HEADER #include + +// INTERNAL INCLUDES #include #include #include @@ -25,76 +28,69 @@ namespace Dali { namespace Internal { -namespace SceneGraph +namespace Render { -RenderGeometry::RenderGeometry( GeometryType type, bool requiresDepthTest ) -: mIndexBuffer(0), - mGeometryType( type ), - mRequiresDepthTest(requiresDepthTest ), +Geometry::Geometry() +: mIndices(), + mIndexBuffer(NULL), + mGeometryType( Dali::Geometry::TRIANGLES ), + mIndicesChanged(false), mHasBeenUpdated(false), mAttributesChanged(true) { } -RenderGeometry::~RenderGeometry() +Geometry::~Geometry() { } -void RenderGeometry::GlContextCreated( Context& context ) +void Geometry::GlContextCreated( Context& context ) { } -void RenderGeometry::GlContextDestroyed() +void Geometry::GlContextDestroyed() { } -void RenderGeometry::AddPropertyBuffer( Render::PropertyBuffer* propertyBuffer, bool isIndexBuffer ) +void Geometry::AddPropertyBuffer( Render::PropertyBuffer* propertyBuffer ) { - if( isIndexBuffer ) - { - mIndexBuffer = propertyBuffer; - } - else - { - mVertexBuffers.PushBack( propertyBuffer ); - mAttributesChanged = true; - } + mVertexBuffers.PushBack( propertyBuffer ); + mAttributesChanged = true; } -void RenderGeometry::RemovePropertyBuffer( const Render::PropertyBuffer* propertyBuffer ) +void Geometry::SetIndexBuffer( Dali::Vector& indices ) { - if( propertyBuffer == mIndexBuffer ) - { - mIndexBuffer = 0; - } - else + mIndices.Swap( indices ); + mIndicesChanged = true; +} + +void Geometry::RemovePropertyBuffer( const Render::PropertyBuffer* propertyBuffer ) +{ + const auto&& end = mVertexBuffers.End(); + for( auto&& iter = mVertexBuffers.Begin(); iter != end; ++iter ) { - size_t bufferCount = mVertexBuffers.Size(); - for( size_t i(0); i& attributeLocation, Program& program, BufferIndex bufferIndex ) const +void Geometry::GetAttributeLocationFromProgram( Vector& attributeLocation, Program& program, BufferIndex bufferIndex ) const { attributeLocation.Clear(); - for( size_t i(0); i< mVertexBuffers.Size(); ++i ) + for( auto&& vertexBuffer : mVertexBuffers ) { - unsigned int attributeCount = mVertexBuffers[i]->GetAttributeCount(); - for( unsigned int j = 0; j < attributeCount; ++j ) + const uint32_t attributeCount = vertexBuffer->GetAttributeCount(); + for( uint32_t j = 0; j < attributeCount; ++j ) { - const std::string& attributeName = mVertexBuffers[i]->GetAttributeName( j ); - unsigned int index = program.RegisterCustomAttribute( attributeName ); + const std::string& attributeName = vertexBuffer->GetAttributeName( j ); + uint32_t index = program.RegisterCustomAttribute( attributeName ); GLint location = program.GetCustomAttributeLocation( index ); if( -1 == location ) @@ -107,34 +103,40 @@ void RenderGeometry::GetAttributeLocationFromProgram( Vector& attributeLo } } -void RenderGeometry::OnRenderFinished() +void Geometry::OnRenderFinished() { mHasBeenUpdated = false; mAttributesChanged = false; } -void RenderGeometry::UploadAndDraw( - Context& context, - BufferIndex bufferIndex, - Vector& attributeLocation, - size_t elementBufferOffset, - size_t elementBufferCount ) +void Geometry::Upload( Context& context ) { if( !mHasBeenUpdated ) { // Update buffers - if( mIndexBuffer ) + if( mIndicesChanged ) { - if(!mIndexBuffer->Update( context, true ) ) + if( mIndices.Empty() ) { - //Index buffer is not ready ( Size, data or format has not been specified yet ) - return; + mIndexBuffer = NULL; } + else + { + if ( mIndexBuffer == NULL ) + { + mIndexBuffer = new GpuBuffer( context ); + } + + uint32_t bufferSize = static_cast( sizeof( uint16_t ) * mIndices.Size() ); + mIndexBuffer->UpdateDataBuffer( context, bufferSize, &mIndices[0], GpuBuffer::STATIC_DRAW, GpuBuffer::ELEMENT_ARRAY_BUFFER ); + } + + mIndicesChanged = false; } - for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i ) + for( auto&& buffer : mVertexBuffers ) { - if( !mVertexBuffers[i]->Update( context, false ) ) + if( !buffer->Update( context ) ) { //Vertex buffer is not ready ( Size, data or format has not been specified yet ) return; @@ -143,133 +145,110 @@ void RenderGeometry::UploadAndDraw( mHasBeenUpdated = true; } +} +void Geometry::Draw( + Context& context, + BufferIndex bufferIndex, + Vector& attributeLocation, + uint32_t elementBufferOffset, + uint32_t elementBufferCount ) +{ //Bind buffers to attribute locations - unsigned int base = 0; - for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i ) + uint32_t base = 0u; + const uint32_t vertexBufferCount = static_cast( mVertexBuffers.Count() ); + for( uint32_t i = 0; i < vertexBufferCount; ++i ) { - mVertexBuffers[i]->BindBuffer( GpuBuffer::ARRAY_BUFFER ); + mVertexBuffers[i]->BindBuffer( context, GpuBuffer::ARRAY_BUFFER ); base += mVertexBuffers[i]->EnableVertexAttributes( context, attributeLocation, base ); } - if( mIndexBuffer ) - { - mIndexBuffer->BindBuffer( GpuBuffer::ELEMENT_ARRAY_BUFFER ); - } - - //Bind index buffer - unsigned int numIndices(0u); + uint32_t numIndices(0u); intptr_t firstIndexOffset(0u); if( mIndexBuffer ) { - numIndices = mIndexBuffer->GetDataSize() / mIndexBuffer->GetElementSize(); - if ( elementBufferCount ) + numIndices = static_cast( mIndices.Size() ); + + if( elementBufferOffset != 0u ) { - numIndices = elementBufferCount > numIndices ? numIndices : elementBufferCount; + elementBufferOffset = (elementBufferOffset >= numIndices ) ? numIndices - 1 : elementBufferOffset; + firstIndexOffset = elementBufferOffset * sizeof(GLushort); + numIndices -= elementBufferOffset; + } + + if( elementBufferCount != 0u ) + { + numIndices = std::min( elementBufferCount, numIndices ); } - firstIndexOffset = elementBufferOffset * sizeof(GLushort); } - //Draw call + GLenum geometryGLType(GL_NONE); switch(mGeometryType) { case Dali::Geometry::TRIANGLES: { - if( numIndices ) - { - context.DrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast(firstIndexOffset) ); - } - else - { - unsigned int numVertices = mVertexBuffers[0]->GetElementCount(); - context.DrawArrays( GL_TRIANGLES, 0, numVertices ); - } + geometryGLType = GL_TRIANGLES; break; } case Dali::Geometry::LINES: { - if( numIndices ) - { - context.DrawElements(GL_LINES, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast(firstIndexOffset) ); - } - else - { - unsigned int numVertices = mVertexBuffers[0]->GetElementCount(); - context.DrawArrays( GL_LINES, 0, numVertices ); - } + geometryGLType = GL_LINES; break; } case Dali::Geometry::POINTS: { - unsigned int numVertices = mVertexBuffers[0]->GetElementCount(); - context.DrawArrays(GL_POINTS, 0, numVertices ); + geometryGLType = GL_POINTS; break; } case Dali::Geometry::TRIANGLE_STRIP: { - if( numIndices ) - { - context.DrawElements(GL_TRIANGLE_STRIP, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast(firstIndexOffset) ); - } - else - { - unsigned int numVertices = mVertexBuffers[0]->GetElementCount(); - context.DrawArrays(GL_TRIANGLE_STRIP, 0, numVertices ); - } + geometryGLType = GL_TRIANGLE_STRIP; break; } case Dali::Geometry::TRIANGLE_FAN: { - if( numIndices ) - { - context.DrawElements(GL_TRIANGLE_FAN, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast(firstIndexOffset) ); - } - else - { - unsigned int numVertices = mVertexBuffers[0]->GetElementCount(); - context.DrawArrays(GL_TRIANGLE_FAN, 0, numVertices ); - } + geometryGLType = GL_TRIANGLE_FAN; break; } case Dali::Geometry::LINE_LOOP: { - if( numIndices ) - { - context.DrawElements(GL_LINE_LOOP, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast(firstIndexOffset) ); - } - else - { - unsigned int numVertices = mVertexBuffers[0]->GetElementCount(); - context.DrawArrays(GL_LINE_LOOP, 0, numVertices ); - } + geometryGLType = GL_LINE_LOOP; break; } case Dali::Geometry::LINE_STRIP: { - if( numIndices ) - { - context.DrawElements(GL_LINE_STRIP, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast(firstIndexOffset) ); - } - else - { - unsigned int numVertices = mVertexBuffers[0]->GetElementCount(); - context.DrawArrays(GL_LINE_STRIP, 0, numVertices ); - } + geometryGLType = GL_LINE_STRIP; break; } - default: + } + + //Draw call + if( mIndexBuffer && geometryGLType != GL_POINTS ) + { + //Indexed draw call + mIndexBuffer->Bind( context, GpuBuffer::ELEMENT_ARRAY_BUFFER ); + // numIndices truncated, no value loss happening in practice + context.DrawElements( geometryGLType, static_cast( numIndices ), GL_UNSIGNED_SHORT, reinterpret_cast( firstIndexOffset ) ); + } + else + { + //Unindex draw call + GLsizei numVertices(0u); + if( vertexBufferCount > 0 ) { - DALI_ASSERT_ALWAYS( 0 && "Geometry type not supported (yet)" ); - break; + // truncated, no value loss happening in practice + numVertices = static_cast( mVertexBuffers[0]->GetElementCount() ); } + + context.DrawArrays( geometryGLType, 0, numVertices ); } - //Disable atrributes - for( unsigned int i = 0; i < attributeLocation.Count(); ++i ) + //Disable attributes + for( auto&& attribute : attributeLocation ) { - if( attributeLocation[i] != -1 ) + if( attribute != -1 ) { - context.DisableVertexAttributeArray( attributeLocation[i] ); + context.DisableVertexAttributeArray( static_cast( attribute ) ); } } }