Added GpuBuffer::WritePolicy
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-geometry.cpp
index 2b84cdb..03afcd7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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/render/gl-resources/context.h>
-#include <dali/internal/render/gl-resources/gpu-buffer.h>
-#include <dali/internal/render/renderers/render-property-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( GeometryType type, bool requiresDepthTest )
-: mIndexBuffer(0),
-  mGeometryType( type ),
-  mRequiresDepthTest(requiresDepthTest ),
+namespace
+{
+inline constexpr size_t GetSizeOfIndexFromIndexType(Dali::Graphics::Format graphicsFormat)
+{
+  switch(graphicsFormat)
+  {
+    case Dali::Graphics::Format::R16_UINT:
+    {
+      return sizeof(uint16_t);
+    }
+    case Dali::Graphics::Format::R32_UINT:
+    {
+      return sizeof(uint32_t);
+    }
+    default:
+    {
+      // TODO : Not implmeneted.
+      return sizeof(uint16_t);
+    }
+  }
+}
+} // unnamed namespace
+Geometry::Geometry()
+: mIndices(),
+  mIndexBuffer(nullptr),
+  mIndexType(Dali::Graphics::Format::R16_UINT),
+  mGeometryType(Dali::Geometry::TRIANGLES),
+  mIndicesChanged(false),
   mHasBeenUpdated(false),
   mAttributesChanged(true)
 {
 }
 
-RenderGeometry::~RenderGeometry()
-{
-}
+Geometry::~Geometry() = default;
 
-void RenderGeometry::GlContextCreated( Context& context )
+void Geometry::AddVertexBuffer(Render::VertexBuffer* vertexBuffer)
 {
+  mVertexBuffers.PushBack(vertexBuffer);
+  mAttributesChanged = true;
 }
 
-void RenderGeometry::GlContextDestroyed()
+const Vector<Render::VertexBuffer*>& Geometry::GetVertexBuffers() const
 {
+  return mVertexBuffers;
 }
 
-void RenderGeometry::AddPropertyBuffer( Render::PropertyBuffer* propertyBuffer, bool isIndexBuffer )
+void Geometry::SetIndexBuffer(Uint16ContainerType& indices)
 {
-  if( isIndexBuffer )
-  {
-    mIndexBuffer = propertyBuffer;
-  }
-  else
-  {
-    mVertexBuffers.PushBack( propertyBuffer );
-    mAttributesChanged = true;
-  }
+  mIndices.Swap(indices);
+  mIndicesChanged = true;
+  mIndexType      = Graphics::Format::R16_UINT;
 }
 
-void RenderGeometry::RemovePropertyBuffer( const Render::PropertyBuffer* propertyBuffer )
+void Geometry::SetIndexBuffer(Uint32ContainerType& indices)
 {
-  if( propertyBuffer == mIndexBuffer )
-  {
-    mIndexBuffer = 0;
-  }
-  else
-  {
-    size_t bufferCount = mVertexBuffers.Size();
-    for( size_t i(0); i<bufferCount; ++i )
-    {
-      if( propertyBuffer == mVertexBuffers[i] )
-      {
-        //This will delete the gpu buffer associated to the RenderPropertyBuffer if there is one
-        mVertexBuffers.Remove( mVertexBuffers.Begin()+i);
-        mAttributesChanged = true;
-        break;
-      }
-    }
-  }
+  // mIndices type is not matched with indices. Copy memory hardly.
+  mIndices.ResizeUninitialized(indices.Count() * 2);
+  memcpy(mIndices.Begin(), indices.Begin(), indices.Count() * sizeof(uint32_t));
+  mIndicesChanged = true;
+  mIndexType      = Graphics::Format::R32_UINT;
 }
 
-void RenderGeometry::GetAttributeLocationFromProgram( Vector<GLint>& attributeLocation, Program& program, BufferIndex bufferIndex ) const
+void Geometry::RemoveVertexBuffer(const Render::VertexBuffer* vertexBuffer)
 {
-  attributeLocation.Clear();
-
-  for( size_t i(0); i< mVertexBuffers.Size(); ++i )
+  const auto&& end = mVertexBuffers.End();
+  // @todo if this buffer is the only instance buffer, reduce instance count to 1.
+  for(auto&& iter = mVertexBuffers.Begin(); iter != end; ++iter)
   {
-    unsigned int attributeCount = mVertexBuffers[i]->GetAttributeCount();
-    for( unsigned int j = 0; j < attributeCount; ++j )
+    if(*iter == vertexBuffer)
     {
-      const std::string& attributeName = mVertexBuffers[i]->GetAttributeName( j );
-      unsigned int index = program.RegisterCustomAttribute( attributeName );
-      GLint location = program.GetCustomAttributeLocation( index );
-
-      if( -1 == location )
-      {
-        DALI_LOG_WARNING( "Attribute not found in the shader: %s\n", attributeName.c_str() );
-      }
-
-      attributeLocation.PushBack( location );
+      //This will delete the gpu buffer associated to the RenderVertexBuffer if there is one
+      mVertexBuffers.Remove(iter);
+      mAttributesChanged = true;
+      break;
     }
   }
 }
 
-void RenderGeometry::OnRenderFinished()
+void Geometry::OnRenderFinished()
 {
-  mHasBeenUpdated = false;
+  mHasBeenUpdated    = false;
   mAttributesChanged = false;
 }
 
-void RenderGeometry::UploadAndDraw(
-    Context& context,
-    BufferIndex bufferIndex,
-    Vector<GLint>& attributeLocation )
+void Geometry::Upload(Graphics::Controller& graphicsController)
 {
-  if( !mHasBeenUpdated )
+  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 = nullptr;
+      }
+      else
+      {
+        if(mIndexBuffer == nullptr)
+        {
+          // Currently we are unable to reuse index buffer so the write policy is to preserve current content
+          mIndexBuffer = new GpuBuffer(graphicsController, 0 | Graphics::BufferUsage::INDEX_BUFFER, GpuBuffer::WritePolicy::RETAIN);
+        }
+
+        uint32_t bufferSize = static_cast<uint32_t>(sizeof(uint16_t) * mIndices.Size());
+        mIndexBuffer->UpdateDataBuffer(graphicsController, bufferSize, &mIndices[0]);
       }
+
+      mIndicesChanged = false;
     }
 
-    for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
+    for(auto&& buffer : mVertexBuffers)
     {
-      if( !mVertexBuffers[i]->Update( context, false ) )
+      if(!buffer->Update(graphicsController))
       {
         //Vertex buffer is not ready ( Size, data or format has not been specified yet )
         return;
@@ -141,105 +149,146 @@ void RenderGeometry::UploadAndDraw(
 
     mHasBeenUpdated = true;
   }
+}
 
+bool Geometry::BindVertexAttributes(Graphics::CommandBuffer& commandBuffer)
+{
   //Bind buffers to attribute locations
-  unsigned int base = 0;
-  for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
+  const auto vertexBufferCount = static_cast<uint32_t>(mVertexBuffers.Count());
+
+  std::vector<const Graphics::Buffer*> buffers;
+  std::vector<uint32_t>                offsets;
+
+  for(uint32_t i = 0; i < vertexBufferCount; ++i)
   {
-    mVertexBuffers[i]->BindBuffer( GpuBuffer::ARRAY_BUFFER );
-    base += mVertexBuffers[i]->EnableVertexAttributes( context, attributeLocation, base );
-  }
+    if(mVertexBuffers[i]->GetDivisor() > 0)
+    {
+      mInstanceCount = mVertexBuffers[i]->GetElementCount();
+    }
+
+    const GpuBuffer* gpuBuffer = mVertexBuffers[i]->GetGpuBuffer();
+    if(gpuBuffer)
+    {
+      const Graphics::Buffer* buffer = gpuBuffer->GetGraphicsObject();
 
-  if( mIndexBuffer )
+      if(buffer)
+      {
+        buffers.push_back(buffer);
+        offsets.push_back(0u);
+      }
+    }
+    //@todo Figure out why this is being drawn without geometry having been uploaded
+  }
+  if(buffers.empty())
   {
-    mIndexBuffer->BindBuffer( GpuBuffer::ELEMENT_ARRAY_BUFFER );
+    return false;
   }
 
-  //Bind index buffer
-  unsigned int numIndices(0u);
-  if( mIndexBuffer )
+  commandBuffer.BindVertexBuffers(0, buffers, offsets);
+
+  return true;
+}
+
+bool Geometry::Draw(
+  Graphics::Controller&    graphicsController,
+  Graphics::CommandBuffer& commandBuffer,
+  uint32_t                 elementBufferOffset,
+  uint32_t                 elementBufferCount)
+{
+  uint32_t numIndices(0u);
+  intptr_t firstIndexOffset(0u);
+  if(mIndexBuffer)
   {
-    numIndices = mIndexBuffer->GetDataSize() / mIndexBuffer->GetElementSize();
+    std::size_t sizeOfIndex = GetSizeOfIndexFromIndexType(mIndexType);
+
+    numIndices = static_cast<uint32_t>(mIndices.Size() * sizeof(uint16_t) / sizeOfIndex);
+
+    if(elementBufferOffset != 0u)
+    {
+      elementBufferOffset = (elementBufferOffset >= numIndices) ? numIndices - 1 : elementBufferOffset;
+      firstIndexOffset    = intptr_t(elementBufferOffset * sizeOfIndex);
+      numIndices -= elementBufferOffset;
+    }
+
+    if(elementBufferCount != 0u)
+    {
+      numIndices = std::min(elementBufferCount, numIndices);
+    }
   }
 
   //Draw call
+  if(mIndexBuffer && mGeometryType != Dali::Geometry::POINTS)
+  {
+    //Indexed draw call
+    const Graphics::Buffer* ibo = mIndexBuffer->GetGraphicsObject();
+    if(ibo)
+    {
+      commandBuffer.BindIndexBuffer(*ibo, 0, mIndexType);
+    }
+
+    commandBuffer.DrawIndexed(numIndices, mInstanceCount, firstIndexOffset, 0, 0);
+  }
+  else
+  {
+    // Un-indexed draw call
+    uint32_t numVertices(0u);
+
+    if(mVertexBuffers.Count() > 0)
+    {
+      // truncated, no value loss happening in practice
+      numVertices = static_cast<uint32_t>(mVertexBuffers[0]->GetElementCount());
+    }
+
+    commandBuffer.Draw(numVertices, mInstanceCount, 0, 0);
+  }
+  return true;
+}
+
+Graphics::PrimitiveTopology Geometry::GetTopology() const
+{
+  Graphics::PrimitiveTopology topology = Graphics::PrimitiveTopology::TRIANGLE_LIST;
+
   switch(mGeometryType)
   {
     case Dali::Geometry::TRIANGLES:
     {
-      if( numIndices )
-      {
-        context.DrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
-      }
-      else
-      {
-        unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
-        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
-      {
-        unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
-        context.DrawArrays( GL_LINES, 0, numVertices );
-      }
+      topology = Graphics::PrimitiveTopology::LINE_LIST;
       break;
     }
     case Dali::Geometry::POINTS:
     {
-      unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
-      context.DrawArrays(GL_POINTS, 0, numVertices );
+      topology = Graphics::PrimitiveTopology::POINT_LIST;
       break;
     }
     case Dali::Geometry::TRIANGLE_STRIP:
     {
-      if( numIndices )
-      {
-        context.DrawElements(GL_TRIANGLE_STRIP, numIndices, GL_UNSIGNED_SHORT, 0);
-      }
-      else
-      {
-        unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
-        context.DrawArrays(GL_TRIANGLE_STRIP, 0, numVertices );
-      }
+      topology = Graphics::PrimitiveTopology::TRIANGLE_STRIP;
       break;
     }
     case Dali::Geometry::TRIANGLE_FAN:
     {
-      if( numIndices )
-      {
-        context.DrawElements(GL_TRIANGLE_FAN, numIndices, GL_UNSIGNED_SHORT, 0);
-      }
-      else
-      {
-        unsigned int numVertices = mVertexBuffers[0]->GetElementCount();
-        context.DrawArrays(GL_TRIANGLE_FAN, 0, numVertices );
-      }
+      topology = Graphics::PrimitiveTopology::TRIANGLE_FAN;
       break;
     }
-    default:
+    case Dali::Geometry::LINE_LOOP:
     {
-      DALI_ASSERT_ALWAYS( 0 && "Geometry type not supported (yet)" );
+      topology = Graphics::PrimitiveTopology::LINE_LOOP;
       break;
     }
-  }
-
-  //Disable atrributes
-  for( unsigned int i = 0; i < attributeLocation.Count(); ++i )
-  {
-    if( attributeLocation[i] != -1 )
+    case Dali::Geometry::LINE_STRIP:
     {
-      context.DisableVertexAttributeArray( attributeLocation[i] );
+      topology = Graphics::PrimitiveTopology::LINE_STRIP;
+      break;
     }
   }
+  return topology;
 }
 
-} // namespace SceneGraph
+} // namespace Render
 } // namespace Internal
 } // namespace Dali