/*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2021 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.
*/
+// CLASS HEADER
#include <dali/internal/render/renderers/render-geometry.h>
+// INTERNAL INCLUDES
#include <dali/internal/common/buffer-index.h>
-#include <dali/internal/update/common/scene-graph-property-buffer.h>
-#include <dali/internal/update/rendering/scene-graph-geometry.h>
-#include <dali/internal/render/data-providers/render-data-provider.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 ),
- mShaderChanged( true )
+Geometry::Geometry()
+: mIndices(),
+ mIndexBuffer(nullptr),
+ mGeometryType(Dali::Geometry::TRIANGLES),
+ mIndicesChanged(false),
+ mHasBeenUpdated(false),
+ mAttributesChanged(true)
{
}
-RenderGeometry::~RenderGeometry()
+Geometry::~Geometry() = default;
+
+void Geometry::AddVertexBuffer(Render::VertexBuffer* vertexBuffer)
{
+ mVertexBuffers.PushBack(vertexBuffer);
+ mAttributesChanged = true;
}
-void RenderGeometry::GlContextCreated( Context& context )
+const Vector<Render::VertexBuffer*>& Geometry::GetVertexBuffers() const
{
- mDataNeedsUploading = true;
+ return mVertexBuffers;
}
-void RenderGeometry::GlContextDestroyed()
+void Geometry::SetIndexBuffer(Dali::Vector<uint16_t>& indices)
{
+ mIndices.Swap(indices);
+ mIndicesChanged = true;
}
-void RenderGeometry::UploadAndDraw(
- Context& context,
- Program& program,
- BufferIndex bufferIndex,
- const RenderDataProvider* dataProviders )
+void Geometry::RemoveVertexBuffer(const Render::VertexBuffer* vertexBuffer)
{
- UploadVertexData( context, program, bufferIndex, dataProviders );
-
- for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
- {
- mVertexBuffers[i]->BindBuffer( context, program );
- mVertexBuffers[i]->EnableVertexAttributes( context, bufferIndex, program );
- }
-
- if( mIndexBuffer )
- {
- mIndexBuffer->BindBuffer( context, program );
- }
-
- Draw( context, bufferIndex, dataProviders );
-
- for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
+ const auto&& end = mVertexBuffers.End();
+ for(auto&& iter = mVertexBuffers.Begin(); iter != end; ++iter)
{
- mVertexBuffers[i]->DisableVertexAttributes( context, bufferIndex, program );
+ 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::GeometryUpdated()
+void Geometry::OnRenderFinished()
{
- mDataNeedsUploading = true;
+ mHasBeenUpdated = false;
+ mAttributesChanged = false;
}
-void RenderGeometry::UploadVertexData(
- Context& context,
- Program& program,
- BufferIndex bufferIndex,
- const RenderDataProvider* dataProviders )
+void Geometry::Upload(Graphics::Controller& graphicsController)
{
- if( mDataNeedsUploading )
+ if(!mHasBeenUpdated)
{
- SetUpPropertyBuffers( context, bufferIndex, dataProviders );
-
- for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
+ // Update buffers
+ if(mIndicesChanged)
{
- mVertexBuffers[i]->Upload( context, bufferIndex );
- mVertexBuffers[i]->UpdateAttributeLocations( context, bufferIndex, program );
+ if(mIndices.Empty())
+ {
+ mIndexBuffer = nullptr;
+ }
+ else
+ {
+ if(mIndexBuffer == nullptr)
+ {
+ mIndexBuffer = new GpuBuffer(graphicsController, 0 | Graphics::BufferUsage::INDEX_BUFFER);
+ }
+
+ uint32_t bufferSize = static_cast<uint32_t>(sizeof(uint16_t) * mIndices.Size());
+ mIndexBuffer->UpdateDataBuffer(graphicsController, bufferSize, &mIndices[0]);
+ }
+
+ mIndicesChanged = false;
}
- if( mIndexBuffer )
+
+ for(auto&& buffer : mVertexBuffers)
{
- mIndexBuffer->Upload( context, bufferIndex );
+ if(!buffer->Update(graphicsController))
+ {
+ //Vertex buffer is not ready ( Size, data or format has not been specified yet )
+ return;
+ }
}
- mDataNeedsUploading = false;
+ mHasBeenUpdated = true;
}
}
-void RenderGeometry::SetUpPropertyBuffers(
- Context& context,
- BufferIndex bufferIndex,
- const RenderDataProvider* dataProvider )
+bool Geometry::Draw(
+ Graphics::Controller& graphicsController,
+ Graphics::CommandBuffer& commandBuffer,
+ uint32_t elementBufferOffset,
+ uint32_t elementBufferCount)
{
- // Vertex buffer
- RenderDataProvider::VertexBuffers vertexBuffers = dataProvider->GetVertexBuffers();
+ //Bind buffers to attribute locations
+ const auto vertexBufferCount = static_cast<uint32_t>(mVertexBuffers.Count());
- DALI_ASSERT_DEBUG( vertexBuffers.Count() > 0 && "Need vertex buffers to upload" );
+ std::vector<const Graphics::Buffer*> buffers;
+ std::vector<uint32_t> offsets;
- mVertexBuffers.Clear();
- for( unsigned int i=0; i<vertexBuffers.Count(); ++i)
+ for(uint32_t i = 0; i < vertexBufferCount; ++i)
{
- const PropertyBufferDataProvider* vertexBuffer = vertexBuffers[i];
-
- RenderPropertyBuffer* propertyBuffer = new RenderPropertyBuffer(
- *vertexBuffer,
- GpuBuffer::ARRAY_BUFFER,
- GpuBuffer::STATIC_DRAW ); // TODO: MESH_REWORK: change this for animated meshes
+ const GpuBuffer* gpuBuffer = mVertexBuffers[i]->GetGpuBuffer();
+ if(gpuBuffer)
+ {
+ const Graphics::Buffer* buffer = gpuBuffer->GetGraphicsObject();
- mVertexBuffers.PushBack( propertyBuffer );
+ if(buffer)
+ {
+ buffers.push_back(buffer);
+ offsets.push_back(0u);
+ }
+ }
+ //@todo Figure out why this is being drawn without geometry having been uploaded
}
-
- // Index buffer
- const PropertyBufferDataProvider* indexBuffer = dataProvider->GetIndexBuffer();
- if( indexBuffer )
+ if(buffers.empty())
{
- mIndexBuffer = new RenderPropertyBuffer(
- *indexBuffer,
- GpuBuffer::ELEMENT_ARRAY_BUFFER,
- GpuBuffer::STATIC_DRAW ); // TODO: MESH_REWORK: change this for animated meshes
+ return false;
}
-}
-void RenderGeometry::BindBuffers( Context& context, BufferIndex bufferIndex, Program& program )
-{
- for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
- {
- mVertexBuffers[i]->BindBuffer( context, program );
- }
+ commandBuffer.BindVertexBuffers(0, buffers, offsets);
- if( mIndexBuffer )
+ uint32_t numIndices(0u);
+ intptr_t firstIndexOffset(0u);
+ if(mIndexBuffer)
{
- mIndexBuffer->BindBuffer( context, program );
+ numIndices = static_cast<uint32_t>(mIndices.Size());
+
+ if(elementBufferOffset != 0u)
+ {
+ elementBufferOffset = (elementBufferOffset >= numIndices) ? numIndices - 1 : elementBufferOffset;
+ firstIndexOffset = intptr_t(elementBufferOffset * sizeof(uint16_t));
+ numIndices -= elementBufferOffset;
+ }
+
+ if(elementBufferCount != 0u)
+ {
+ numIndices = std::min(elementBufferCount, numIndices);
+ }
}
-}
-void RenderGeometry::EnableVertexAttributes( Context& context, BufferIndex bufferIndex, Program& program )
-{
- OwnerContainer< RenderPropertyBuffer* >::Iterator it = mVertexBuffers.Begin();
- OwnerContainer< RenderPropertyBuffer* >::ConstIterator end = mVertexBuffers.End();
- for( ; it != end; ++it )
+ //Draw call
+ if(mIndexBuffer && mGeometryType != Dali::Geometry::POINTS)
{
- (*it)->EnableVertexAttributes( context, bufferIndex, program );
- }
-}
+ //Indexed draw call
+ const Graphics::Buffer* ibo = mIndexBuffer->GetGraphicsObject();
+ if(ibo)
+ {
+ commandBuffer.BindIndexBuffer(*ibo, 0, Graphics::Format::R16_UINT);
+ }
-void RenderGeometry::DisableVertexAttributes( Context& context, BufferIndex bufferIndex, Program& program )
-{
- OwnerContainer< RenderPropertyBuffer* >::Iterator it = mVertexBuffers.Begin();
- OwnerContainer< RenderPropertyBuffer* >::ConstIterator end = mVertexBuffers.End();
- for( ; it != end; ++it )
+ commandBuffer.DrawIndexed(numIndices, 1, firstIndexOffset, 0, 0);
+ }
+ else
{
- (*it)->DisableVertexAttributes( context, bufferIndex, program );
+ // Un-indexed draw call
+ uint32_t numVertices(0u);
+ if(vertexBufferCount > 0)
+ {
+ // truncated, no value loss happening in practice
+ numVertices = static_cast<uint32_t>(mVertexBuffers[0]->GetElementCount());
+ }
+
+ commandBuffer.Draw(numVertices, 1, 0, 0);
}
+ return true;
}
-void RenderGeometry::Draw( Context& context, BufferIndex bufferIndex, const RenderDataProvider* dataProvider )
+Graphics::PrimitiveTopology Geometry::GetTopology() const
{
- const GeometryDataProvider& geometry = dataProvider->GetGeometry();
- const PropertyBufferDataProvider* indexBuffer = dataProvider->GetIndexBuffer();
-
- GeometryDataProvider::GeometryType type = geometry.GetGeometryType( bufferIndex );
+ Graphics::PrimitiveTopology topology = Graphics::PrimitiveTopology::TRIANGLE_LIST;
- unsigned int numIndices = 0;
- if( indexBuffer )
- {
- numIndices = indexBuffer->GetDataSize(bufferIndex) / indexBuffer->GetElementSize(bufferIndex);
- }
-
- switch(type)
+ switch(mGeometryType)
{
case Dali::Geometry::TRIANGLES:
{
- if( numIndices )
- {
- context.DrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
- }
- else
- {
- const PropertyBufferDataProvider* firstVertexBuffer = dataProvider->GetVertexBuffers()[0];
- unsigned int numVertices = firstVertexBuffer->GetElementCount( bufferIndex );
- context.DrawArrays( GL_TRIANGLES, 0, numVertices );
- }
+ topology = Graphics::PrimitiveTopology::TRIANGLE_LIST;
break;
}
case Dali::Geometry::LINES:
{
- if( numIndices )
- {
- context.DrawElements(GL_LINES, numIndices, GL_UNSIGNED_SHORT, 0);
- }
- else
- {
- const PropertyBufferDataProvider* firstVertexBuffer = dataProvider->GetVertexBuffers()[0];
- unsigned int numVertices = firstVertexBuffer->GetElementCount( bufferIndex );
- context.DrawArrays( GL_LINES, 0, numVertices );
- }
+ topology = Graphics::PrimitiveTopology::LINE_LIST;
break;
}
case Dali::Geometry::POINTS:
{
- const PropertyBufferDataProvider* firstVertexBuffer = dataProvider->GetVertexBuffers()[0];
- unsigned int numVertices = firstVertexBuffer->GetElementCount( bufferIndex );
- context.DrawArrays(GL_POINTS, 0, numVertices );
+ topology = Graphics::PrimitiveTopology::POINT_LIST;
+ break;
+ }
+ case Dali::Geometry::TRIANGLE_STRIP:
+ {
+ topology = Graphics::PrimitiveTopology::TRIANGLE_STRIP;
+ break;
+ }
+ case Dali::Geometry::TRIANGLE_FAN:
+ {
+ topology = Graphics::PrimitiveTopology::TRIANGLE_FAN;
+ break;
+ }
+ case Dali::Geometry::LINE_LOOP:
+ {
+ topology = Graphics::PrimitiveTopology::LINE_LOOP;
break;
}
- default:
+ case Dali::Geometry::LINE_STRIP:
{
- DALI_ASSERT_ALWAYS( 0 && "Geometry type not supported (yet)" );
+ topology = Graphics::PrimitiveTopology::LINE_STRIP;
break;
}
}
+ return topology;
}
-} // namespace SceneGraph
+} // namespace Render
} // namespace Internal
} // namespace Dali