Revert "Move new mesh API to devel-api"
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-geometry.cpp
index 0c91482..4713775 100644 (file)
  * limitations under the License.
  */
 
-#include "render-geometry.h"
+#include <dali/internal/render/renderers/render-geometry.h>
 
 #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/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/shaders/program.h>
@@ -31,7 +32,8 @@ namespace SceneGraph
 {
 
 RenderGeometry::RenderGeometry()
-: mDataNeedsUploading( true )
+: mDataNeedsUploading( true ),
+  mShaderChanged( true )
 {
 }
 
@@ -46,32 +48,33 @@ void RenderGeometry::GlContextCreated( Context& context )
 
 void RenderGeometry::GlContextDestroyed()
 {
-  for( GpuBuffers::Iterator iter=mVertexBuffers.Begin(); iter != mVertexBuffers.End(); ++iter )
+}
+
+void RenderGeometry::UploadAndDraw(
+  Context& context,
+  Program& program,
+  BufferIndex bufferIndex,
+  const RenderDataProvider* dataProviders )
+{
+  UploadVertexData( context, bufferIndex, dataProviders );
+
+  for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
   {
-    GpuBuffer* gpuBuffer = *iter;
-    if( gpuBuffer )
-    {
-      gpuBuffer->GlContextDestroyed();
-    }
+    mVertexBuffers[i]->BindBuffer( context, program );
+    mVertexBuffers[i]->EnableVertexAttributes( context, bufferIndex, program );
   }
 
   if( mIndexBuffer )
   {
-    mIndexBuffer->GlContextDestroyed();
+    mIndexBuffer->BindBuffer( context, program );
   }
-}
 
-void RenderGeometry::UploadAndDraw(
-  Context* context,
-  Program& program,
-  BufferIndex bufferIndex,
-  const GeometryDataProvider& geometryDataProvider )
-{
-  UploadVertexData( context, bufferIndex, geometryDataProvider );
-  BindBuffers();
-  EnableVertexAttributes( context, program );
-  Draw( context, bufferIndex, geometryDataProvider );
-  DisableVertexAttributes( context, program );
+  Draw( context, bufferIndex, dataProviders );
+
+  for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
+  {
+    mVertexBuffers[i]->DisableVertexAttributes( context, bufferIndex, program );
+  }
 }
 
 void RenderGeometry::GeometryUpdated()
@@ -80,143 +83,141 @@ void RenderGeometry::GeometryUpdated()
 }
 
 void RenderGeometry::UploadVertexData(
-  Context* context,
+  Context& context,
   BufferIndex bufferIndex,
-  const GeometryDataProvider& geometry )
+  const RenderDataProvider* dataProviders )
 {
-  if( mDataNeedsUploading ) // @todo Or if any of the property buffers are dirty
+  if( mDataNeedsUploading )
   {
-    DoUpload( context, bufferIndex, geometry );
+    SetUpPropertyBuffers( context, bufferIndex, dataProviders );
+
+    for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
+    {
+      mVertexBuffers[i]->Upload( context, bufferIndex );
+    }
+    if( mIndexBuffer )
+    {
+      mIndexBuffer->Upload( context, bufferIndex );
+    }
 
     mDataNeedsUploading = false;
   }
 }
 
-void RenderGeometry::DoUpload(
-  Context* context,
+void RenderGeometry::SetUpPropertyBuffers(
+  Context& context,
   BufferIndex bufferIndex,
-  const GeometryDataProvider& geometry)
+  const RenderDataProvider* dataProvider )
 {
   // Vertex buffer
-  const Geometry::VertexBuffers& vertexBuffers = geometry.GetVertexBuffers();
+  RenderDataProvider::VertexBuffers vertexBuffers = dataProvider->GetVertexBuffers();
+
   DALI_ASSERT_DEBUG( vertexBuffers.Count() > 0 && "Need vertex buffers to upload" );
 
   for( unsigned int i=0; i<vertexBuffers.Count(); ++i)
   {
-    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 PropertyBufferDataProvider* vertexBuffer = vertexBuffers[i];
 
-    std::size_t dataSize = vertexBuffer->GetDataSize( bufferIndex );
-    vertexGpuBuffer->UpdateDataBuffer( dataSize, vertexBuffer->GetData( bufferIndex ) );
-    vertexGpuBuffer->SetStride( vertexBuffer->GetElementSize( bufferIndex ) );
+    RenderPropertyBuffer* propertyBuffer = new RenderPropertyBuffer(
+      *vertexBuffer,
+      GpuBuffer::ARRAY_BUFFER,
+      GpuBuffer::STATIC_DRAW ); // TODO: MESH_REWORK: change this for animated meshes
 
-    mVertexBuffers.PushBack( vertexGpuBuffer );
+    mVertexBuffers.PushBack( propertyBuffer );
   }
 
   // Index buffer
-  const PropertyBuffer* indexBuffer = geometry.GetIndexBuffer();
+  const PropertyBufferDataProvider* indexBuffer = dataProvider->GetIndexBuffer();
   if( indexBuffer )
   {
-    GpuBuffer* indexGpuBuffer = new GpuBuffer( *context, GpuBuffer::ELEMENT_ARRAY_BUFFER, GpuBuffer::STATIC_DRAW );
-
-    unsigned int dataSize = indexBuffer->GetDataSize( bufferIndex );
-    indexGpuBuffer->UpdateDataBuffer( dataSize, indexBuffer->GetData( bufferIndex ) );
-
-    mIndexBuffer.Reset();
-    mIndexBuffer = indexGpuBuffer;
+    mIndexBuffer = new RenderPropertyBuffer(
+      *indexBuffer,
+      GpuBuffer::ELEMENT_ARRAY_BUFFER,
+      GpuBuffer::STATIC_DRAW ); // TODO: MESH_REWORK: change this for animated meshes
   }
 }
 
-void RenderGeometry::BindBuffers()
+void RenderGeometry::BindBuffers( Context& context, BufferIndex bufferIndex, Program& program )
 {
-  for( GpuBuffers::Iterator iter=mVertexBuffers.Begin(); iter != mVertexBuffers.End(); ++iter )
+  for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i )
   {
-    (*iter)->Bind();
+    mVertexBuffers[i]->BindBuffer( context, program );
   }
 
   if( mIndexBuffer )
   {
-    mIndexBuffer->Bind();
+    mIndexBuffer->BindBuffer( context, program );
   }
 }
 
-void RenderGeometry::EnableVertexAttributes( Context* context, Program& program )
+void RenderGeometry::EnableVertexAttributes( Context& context, BufferIndex bufferIndex, 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 );
+  OwnerContainer< RenderPropertyBuffer* >::Iterator it = mVertexBuffers.Begin();
+  OwnerContainer< RenderPropertyBuffer* >::ConstIterator end = mVertexBuffers.End();
+  for( ; it != end; ++it )
+  {
+    (*it)->EnableVertexAttributes( context, bufferIndex, program );
+  }
 }
 
-void RenderGeometry::DisableVertexAttributes( Context* context, Program& program )
+void RenderGeometry::DisableVertexAttributes( Context& context, BufferIndex bufferIndex, 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 );
+  OwnerContainer< RenderPropertyBuffer* >::Iterator it = mVertexBuffers.Begin();
+  OwnerContainer< RenderPropertyBuffer* >::ConstIterator end = mVertexBuffers.End();
+  for( ; it != end; ++it )
+  {
+    (*it)->DisableVertexAttributes( context, bufferIndex, program );
+  }
 }
 
-void RenderGeometry::Draw( Context* context, BufferIndex bufferIndex, const GeometryDataProvider& geometry )
+void RenderGeometry::Draw( Context& context, BufferIndex bufferIndex, const RenderDataProvider* dataProvider )
 {
+  const GeometryDataProvider& geometry = dataProvider->GetGeometry();
+  const PropertyBufferDataProvider* indexBuffer = dataProvider->GetIndexBuffer();
+
   GeometryDataProvider::GeometryType type = geometry.GetGeometryType( bufferIndex );
 
   unsigned int numIndices = 0;
-  const PropertyBuffer* indexBuffer = geometry.GetIndexBuffer();
-
   if( indexBuffer )
   {
-    numIndices = /* TODO: MESH_REWORK remove this 2, should implement unsigned short properties  */ 2 * indexBuffer->GetDataSize(bufferIndex) / indexBuffer->GetElementSize(bufferIndex);
+    numIndices = indexBuffer->GetDataSize(bufferIndex) / indexBuffer->GetElementSize(bufferIndex);
   }
 
   switch(type)
   {
     case Dali::Geometry::TRIANGLES:
     {
-      context->DrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
+      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 );
+      }
       break;
     }
     case Dali::Geometry::LINES:
     {
-      context->DrawElements(GL_LINES, numIndices, GL_UNSIGNED_SHORT, 0);
+      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 );
+      }
       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 );
+      const PropertyBufferDataProvider* firstVertexBuffer = dataProvider->GetVertexBuffers()[0];
+      unsigned int numVertices = firstVertexBuffer->GetElementCount( bufferIndex );
+      context.DrawArrays(GL_POINTS, 0, numVertices );
       break;
     }
     default: