/*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 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.
* limitations under the License.
*/
-#include "render-geometry.h"
+// CLASS HEADER
+#include <dali/internal/render/renderers/render-geometry.h>
+// INTERNAL INCLUDES
#include <dali/internal/common/buffer-index.h>
-#include <dali/internal/update/geometry/scene-graph-geometry.h>
-#include <dali/internal/update/common/scene-graph-property-buffer.h>
#include <dali/internal/render/gl-resources/context.h>
#include <dali/internal/render/gl-resources/gpu-buffer.h>
+#include <dali/internal/render/renderers/render-vertex-buffer.h>
#include <dali/internal/render/shaders/program.h>
namespace Dali
{
namespace Internal
{
-namespace SceneGraph
+namespace Render
{
-RenderGeometry::RenderGeometry()
-: mDataNeedsUploading( true )
+Geometry::Geometry()
+: mIndices(),
+ mIndexBuffer(nullptr),
+ mGeometryType( Dali::Geometry::TRIANGLES ),
+ mIndicesChanged(false),
+ mHasBeenUpdated(false),
+ mAttributesChanged(true)
{
}
-RenderGeometry::~RenderGeometry()
-{
-}
+Geometry::~Geometry() = default;
-void RenderGeometry::GlContextCreated( Context& context )
+void Geometry::GlContextCreated( Context& context )
{
- mDataNeedsUploading = true;
}
-void RenderGeometry::GlContextDestroyed()
+void Geometry::GlContextDestroyed()
{
- for( GpuBuffers::Iterator iter=mVertexBuffers.Begin(); iter != mVertexBuffers.End(); ++iter )
- {
- GpuBuffer* gpuBuffer = *iter;
- if( gpuBuffer )
- {
- gpuBuffer->GlContextDestroyed();
- }
- }
-
- if( mIndexBuffer )
- {
- mIndexBuffer->GlContextDestroyed();
- }
}
-void RenderGeometry::UploadAndDraw(
- Context* context,
- Program& program,
- BufferIndex bufferIndex,
- const GeometryDataProvider& geometryDataProvider )
+void Geometry::AddVertexBuffer( Render::VertexBuffer* vertexBuffer )
{
- UploadVertexData( context, bufferIndex, geometryDataProvider );
- BindBuffers();
- EnableVertexAttributes( context, program );
- Draw( context, bufferIndex, geometryDataProvider );
- DisableVertexAttributes( context, program );
+ mVertexBuffers.PushBack( vertexBuffer );
+ mAttributesChanged = true;
}
-void RenderGeometry::GeometryUpdated()
+void Geometry::SetIndexBuffer( Dali::Vector<uint16_t>& indices )
{
- mDataNeedsUploading = true;
+ mIndices.Swap( indices );
+ mIndicesChanged = true;
}
-void RenderGeometry::UploadVertexData(
- Context* context,
- BufferIndex bufferIndex,
- const GeometryDataProvider& geometry )
+void Geometry::RemoveVertexBuffer( const Render::VertexBuffer* vertexBuffer )
{
- if( mDataNeedsUploading ) // @todo Or if any of the property buffers are dirty
+ const auto&& end = mVertexBuffers.End();
+ for( auto&& iter = mVertexBuffers.Begin(); iter != end; ++iter )
{
- DoUpload( context, bufferIndex, geometry );
-
- mDataNeedsUploading = false;
+ if( *iter == vertexBuffer )
+ {
+ //This will delete the gpu buffer associated to the RenderVertexBuffer if there is one
+ mVertexBuffers.Remove( iter );
+ mAttributesChanged = true;
+ break;
+ }
}
}
-void RenderGeometry::DoUpload(
- Context* context,
- BufferIndex bufferIndex,
- const GeometryDataProvider& geometry)
+void Geometry::GetAttributeLocationFromProgram( Vector<GLint>& attributeLocation, Program& program, BufferIndex bufferIndex ) const
{
- // Vertex buffer
- const Geometry::VertexBuffers& vertexBuffers = geometry.GetVertexBuffers();
- DALI_ASSERT_DEBUG( vertexBuffers.Count() > 0 && "Need vertex buffers to upload" );
+ attributeLocation.Clear();
- for( unsigned int i=0; i<vertexBuffers.Count(); ++i)
+ for( auto&& vertexBuffer : mVertexBuffers )
{
- const PropertyBuffer* vertexBuffer = vertexBuffers[i];
-
- // @todo MESH_REWORK STATIC_DRAW or DYNAMIC_DRAW depends on property buffer type (static / animated)
- GpuBuffer* vertexGpuBuffer = new GpuBuffer( *context, GpuBuffer::ARRAY_BUFFER, GpuBuffer::STATIC_DRAW );
+ const uint32_t attributeCount = vertexBuffer->GetAttributeCount();
+ for( uint32_t j = 0; j < attributeCount; ++j )
+ {
+ const std::string& attributeName = vertexBuffer->GetAttributeName( j );
+ uint32_t index = program.RegisterCustomAttribute( attributeName );
+ GLint location = program.GetCustomAttributeLocation( index );
- std::size_t dataSize = vertexBuffer->GetDataSize( bufferIndex );
- vertexGpuBuffer->UpdateDataBuffer( dataSize, vertexBuffer->GetData( bufferIndex ) );
- vertexGpuBuffer->SetStride( vertexBuffer->GetElementSize( bufferIndex ) );
+ if( -1 == location )
+ {
+ DALI_LOG_WARNING( "Attribute not found in the shader: %s\n", attributeName.c_str() );
+ }
- mVertexBuffers.PushBack( vertexGpuBuffer );
+ attributeLocation.PushBack( location );
+ }
}
+}
- // Index buffer
- const PropertyBuffer* indexBuffer = geometry.GetIndexBuffer();
- if( indexBuffer )
+void Geometry::OnRenderFinished()
+{
+ mHasBeenUpdated = false;
+ mAttributesChanged = false;
+}
+
+void Geometry::Upload( Context& context )
+{
+ if( !mHasBeenUpdated )
{
- GpuBuffer* indexGpuBuffer = new GpuBuffer( *context, GpuBuffer::ELEMENT_ARRAY_BUFFER, GpuBuffer::STATIC_DRAW );
+ // Update buffers
+ if( mIndicesChanged )
+ {
+ if( mIndices.Empty() )
+ {
+ mIndexBuffer = nullptr;
+ }
+ else
+ {
+ if ( mIndexBuffer == nullptr )
+ {
+ mIndexBuffer = new GpuBuffer( context );
+ }
- unsigned int dataSize = indexBuffer->GetDataSize( bufferIndex );
- indexGpuBuffer->UpdateDataBuffer( dataSize, indexBuffer->GetData( bufferIndex ) );
+ uint32_t bufferSize = static_cast<uint32_t>( sizeof( uint16_t ) * mIndices.Size() );
+ mIndexBuffer->UpdateDataBuffer( context, bufferSize, &mIndices[0], GpuBuffer::STATIC_DRAW, GpuBuffer::ELEMENT_ARRAY_BUFFER );
+ }
- mIndexBuffer.Reset();
- mIndexBuffer = indexGpuBuffer;
+ mIndicesChanged = false;
+ }
+
+ for( auto&& buffer : mVertexBuffers )
+ {
+ if( !buffer->Update( context ) )
+ {
+ //Vertex buffer is not ready ( Size, data or format has not been specified yet )
+ return;
+ }
+ }
+
+ mHasBeenUpdated = true;
}
}
-void RenderGeometry::BindBuffers()
+void Geometry::Draw(
+ Context& context,
+ BufferIndex bufferIndex,
+ Vector<GLint>& attributeLocation,
+ uint32_t elementBufferOffset,
+ uint32_t elementBufferCount )
{
- for( GpuBuffers::Iterator iter=mVertexBuffers.Begin(); iter != mVertexBuffers.End(); ++iter )
+ //Bind buffers to attribute locations
+ uint32_t base = 0u;
+ const uint32_t vertexBufferCount = static_cast<uint32_t>( mVertexBuffers.Count() );
+ for( uint32_t i = 0; i < vertexBufferCount; ++i )
{
- (*iter)->Bind();
+ mVertexBuffers[i]->BindBuffer( context, GpuBuffer::ARRAY_BUFFER );
+ base += mVertexBuffers[i]->EnableVertexAttributes( context, attributeLocation, base );
}
+ uint32_t numIndices(0u);
+ intptr_t firstIndexOffset(0u);
if( mIndexBuffer )
{
- mIndexBuffer->Bind();
- }
-}
+ numIndices = static_cast<uint32_t>( mIndices.Size() );
-void RenderGeometry::EnableVertexAttributes( Context* context, Program& program )
-{
- // @todo Loop thru the array of vertex buffers
- // @todo Use AttributeDataProvider to get the attrs and enable them
- // Need mapping from gpu buffers index to a particular attributes
- Vector4 *vertex=0;
-
- unsigned int gpuBufferIndex = 0;
-
- GLint positionLoc = program.GetAttribLocation( Program::ATTRIB_POSITION );
- context->VertexAttribPointer( positionLoc,
- 2, // 2D position
- GL_FLOAT,
- GL_FALSE, // Not normalized
- mVertexBuffers[gpuBufferIndex]->GetStride(),
- &vertex->x );
-
- context->EnableVertexAttributeArray( positionLoc );
-
- GLint textureCoordsLoc = program.GetAttribLocation( Program::ATTRIB_TEXCOORD );
- context->VertexAttribPointer( textureCoordsLoc,
- 2, // Texture Coords = U, V
- GL_FLOAT,
- GL_FALSE,
- mVertexBuffers[gpuBufferIndex]->GetStride(),
- &vertex->z );
- context->EnableVertexAttributeArray( textureCoordsLoc );
-}
-
-void RenderGeometry::DisableVertexAttributes( Context* context, Program& program )
-{
- // @todo Loop thru the array of vertex buffers
- // @todo Use AttributeDataProvider to get the attrs and disable them
- GLint positionLoc = program.GetAttribLocation( Program::ATTRIB_POSITION );
- GLint textureCoordsLoc = program.GetAttribLocation( Program::ATTRIB_TEXCOORD );
- context->DisableVertexAttributeArray( positionLoc );
- context->DisableVertexAttributeArray( textureCoordsLoc );
-}
-
-void RenderGeometry::Draw( Context* context, BufferIndex bufferIndex, const GeometryDataProvider& geometry )
-{
- GeometryDataProvider::GeometryType type = geometry.GetGeometryType( bufferIndex );
-
- unsigned int numIndices = 0;
- const PropertyBuffer* indexBuffer = geometry.GetIndexBuffer();
+ if( elementBufferOffset != 0u )
+ {
+ elementBufferOffset = (elementBufferOffset >= numIndices ) ? numIndices - 1 : elementBufferOffset;
+ firstIndexOffset = elementBufferOffset * sizeof(GLushort);
+ numIndices -= elementBufferOffset;
+ }
- if( indexBuffer )
- {
- numIndices = /* TODO: MESH_REWORK remove this 2, should implement unsigned short properties */ 2 * indexBuffer->GetDataSize(bufferIndex) / indexBuffer->GetElementSize(bufferIndex);
+ if( elementBufferCount != 0u )
+ {
+ numIndices = std::min( elementBufferCount, numIndices );
+ }
}
- switch(type)
+ GLenum geometryGLType(GL_NONE);
+ switch(mGeometryType)
{
case Dali::Geometry::TRIANGLES:
{
- context->DrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
+ geometryGLType = GL_TRIANGLES;
break;
}
case Dali::Geometry::LINES:
{
- context->DrawElements(GL_LINES, numIndices, GL_UNSIGNED_SHORT, 0);
+ geometryGLType = GL_LINES;
break;
}
case Dali::Geometry::POINTS:
{
- GpuBuffer* firstVertexBuffer = mVertexBuffers[0];
-
- unsigned int numVertices = 0;
- GLuint stride = firstVertexBuffer->GetStride();
- if( stride != 0 )
- {
- numVertices = firstVertexBuffer->GetBufferSize() / stride;
- }
-
- context->DrawArrays(GL_POINTS, 0, numVertices );
+ geometryGLType = GL_POINTS;
+ break;
+ }
+ case Dali::Geometry::TRIANGLE_STRIP:
+ {
+ geometryGLType = GL_TRIANGLE_STRIP;
break;
}
- default:
+ case Dali::Geometry::TRIANGLE_FAN:
{
- DALI_ASSERT_ALWAYS( 0 && "Geometry type not supported (yet)" );
+ geometryGLType = GL_TRIANGLE_FAN;
break;
}
+ case Dali::Geometry::LINE_LOOP:
+ {
+ geometryGLType = GL_LINE_LOOP;
+ break;
+ }
+ case Dali::Geometry::LINE_STRIP:
+ {
+ geometryGLType = GL_LINE_STRIP;
+ break;
+ }
+ }
+
+ //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<GLsizei>( numIndices ), GL_UNSIGNED_SHORT, reinterpret_cast<void*>( firstIndexOffset ) );
+ }
+ else
+ {
+ //Unindex draw call
+ GLsizei numVertices(0u);
+ if( vertexBufferCount > 0 )
+ {
+ // truncated, no value loss happening in practice
+ numVertices = static_cast<GLsizei>( mVertexBuffers[0]->GetElementCount() );
+ }
+
+ context.DrawArrays( geometryGLType, 0, numVertices );
+ }
+
+ //Disable attributes
+ for( auto&& attribute : attributeLocation )
+ {
+ if( attribute != -1 )
+ {
+ context.DisableVertexAttributeArray( static_cast<GLuint>( attribute ) );
+ }
}
}