0030748: Visualization - Marker displayed in immediate layer ruins QT Quick view...
[occt.git] / src / OpenGl / OpenGl_PrimitiveArray.cxx
old mode 100755 (executable)
new mode 100644 (file)
index b8bbcd5..cec64bb
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
-#include <OpenGl_AspectFace.hxx>
+#include <OpenGl_Aspects.hxx>
 #include <OpenGl_Context.hxx>
 #include <OpenGl_GraphicDriver.hxx>
 #include <OpenGl_IndexBuffer.hxx>
 #include <OpenGl_PointSprite.hxx>
 #include <OpenGl_PrimitiveArray.hxx>
+#include <OpenGl_Sampler.hxx>
 #include <OpenGl_ShaderManager.hxx>
 #include <OpenGl_ShaderProgram.hxx>
 #include <OpenGl_Structure.hxx>
+#include <OpenGl_VertexBufferCompat.hxx>
+#include <OpenGl_View.hxx>
 #include <OpenGl_Workspace.hxx>
-
-#include <InterfaceGraphic_PrimitiveArray.hxx>
+#include <Graphic3d_TextureParams.hxx>
+#include <NCollection_AlignedAllocator.hxx>
 
 namespace
 {
-  template<class T>
-  void BindProgramWithMaterial (const Handle(OpenGl_Workspace)& theWS,
-                                const T*                      theAspect)
+  //! Convert data type to GL info
+  inline GLenum toGlDataType (const Graphic3d_TypeOfData theType,
+                              GLint&                     theNbComp)
+  {
+    switch (theType)
+    {
+      case Graphic3d_TOD_USHORT:
+        theNbComp = 1;
+        return GL_UNSIGNED_SHORT;
+      case Graphic3d_TOD_UINT:
+        theNbComp = 1;
+        return GL_UNSIGNED_INT;
+      case Graphic3d_TOD_VEC2:
+        theNbComp = 2;
+        return GL_FLOAT;
+      case Graphic3d_TOD_VEC3:
+        theNbComp = 3;
+        return GL_FLOAT;
+      case Graphic3d_TOD_VEC4:
+        theNbComp = 4;
+        return GL_FLOAT;
+      case Graphic3d_TOD_VEC4UB:
+        theNbComp = 4;
+        return GL_UNSIGNED_BYTE;
+      case Graphic3d_TOD_FLOAT:
+        theNbComp = 1;
+        return GL_FLOAT;
+    }
+    theNbComp = 0;
+    return GL_NONE;
+  }
+
+}
+
+//! Auxiliary template for VBO with interleaved attributes.
+template<class TheBaseClass, int NbAttributes>
+class OpenGl_VertexBufferT : public TheBaseClass
+{
+public:
+
+  //! Create uninitialized VBO.
+  OpenGl_VertexBufferT (const Graphic3d_Buffer& theAttribs)
+  : Stride (theAttribs.IsInterleaved() ? theAttribs.Stride : 0)
+  {
+    memcpy (Attribs, theAttribs.AttributesArray(), sizeof(Graphic3d_Attribute) * NbAttributes);
+  }
+
+  virtual bool HasColorAttribute() const
+  {
+    for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
+    {
+      const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
+      if (anAttrib.Id == Graphic3d_TOA_COLOR)
+      {
+        return true;
+      }
+    }
+    return false;
+  }
+
+  virtual bool HasNormalAttribute() const
+  {
+    for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
+    {
+      const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
+      if (anAttrib.Id == Graphic3d_TOA_NORM)
+      {
+        return true;
+      }
+    }
+    return false;
+  }
+
+  virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const
   {
-    const Handle(OpenGl_Context)& aCtx = theWS->GetGlContext();
-    const Handle(OpenGl_ShaderProgram)& aProgram = theAspect->ShaderProgramRes (theWS);
-    if (aProgram.IsNull())
+    if (!TheBaseClass::IsValid())
     {
-      OpenGl_ShaderProgram::Unbind (aCtx);
       return;
     }
-    aProgram->BindWithVariables (aCtx);
 
-    const OpenGl_MaterialState* aMaterialState = aCtx->ShaderManager()->MaterialState (aProgram);
-    if (aMaterialState == NULL || aMaterialState->Aspect() != theAspect)
+    TheBaseClass::Bind (theGlCtx);
+    GLint aNbComp;
+    const GLubyte* anOffset = TheBaseClass::myOffset;
+    const Standard_Size aMuliplier = Stride != 0 ? 1 : TheBaseClass::myElemsNb;
+    for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
     {
-      aCtx->ShaderManager()->UpdateMaterialStateTo (aProgram, theAspect);
+      const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
+      const GLenum   aDataType = toGlDataType (anAttrib.DataType, aNbComp);
+      if (anAttrib.Id == Graphic3d_TOA_POS
+       && aDataType != GL_NONE)
+      {
+        TheBaseClass::bindAttribute (theGlCtx, Graphic3d_TOA_POS, aNbComp, aDataType, Stride, anOffset);
+        break;
+      }
+
+      anOffset += aMuliplier * Graphic3d_Attribute::Stride (anAttrib.DataType);
     }
+  }
 
-    aCtx->ShaderManager()->PushState (aProgram);
+  virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
+  {
+    if (!TheBaseClass::IsValid())
+    {
+      return;
+    }
+
+    TheBaseClass::Bind (theGlCtx);
+    GLint aNbComp;
+    const GLubyte* anOffset = TheBaseClass::myOffset;
+    const Standard_Size aMuliplier = Stride != 0 ? 1 : TheBaseClass::myElemsNb;
+    for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
+    {
+      const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
+      const GLenum   aDataType = toGlDataType (anAttrib.DataType, aNbComp);
+      if (aDataType != GL_NONE)
+      {
+        TheBaseClass::bindAttribute (theGlCtx, anAttrib.Id, aNbComp, aDataType, Stride, anOffset);
+      }
+      anOffset += aMuliplier * Graphic3d_Attribute::Stride (anAttrib.DataType);
+    }
   }
-}
 
-// =======================================================================
-// function : clearMemoryOwn
-// purpose  :
-// =======================================================================
-void OpenGl_PrimitiveArray::clearMemoryOwn() const
-{
-  Standard::Free (myPArray->edges);
-  Standard::Free (myPArray->vertices);
-  Standard::Free (myPArray->vcolours);
-  Standard::Free (myPArray->vnormals);
-  Standard::Free (myPArray->vtexels);
-  Standard::Free (myPArray->edge_vis); /// ???
-
-  myPArray->edges    = NULL;
-  myPArray->vertices = NULL;
-  myPArray->vcolours = NULL;
-  myPArray->vnormals = NULL;
-  myPArray->vtexels  = NULL;
-  myPArray->edge_vis = NULL;
-}
+  virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
+  {
+    if (!TheBaseClass::IsValid())
+    {
+      return;
+    }
+    TheBaseClass::Unbind (theGlCtx);
+
+    for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
+    {
+      const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
+      TheBaseClass::unbindAttribute (theGlCtx, anAttrib.Id);
+    }
+  }
+
+private:
+
+  Graphic3d_Attribute Attribs[NbAttributes];
+  Standard_Integer    Stride;
+
+};
 
 // =======================================================================
 // function : clearMemoryGL
@@ -78,618 +181,549 @@ void OpenGl_PrimitiveArray::clearMemoryOwn() const
 // =======================================================================
 void OpenGl_PrimitiveArray::clearMemoryGL (const Handle(OpenGl_Context)& theGlCtx) const
 {
-  for (Standard_Integer anIter = 0; anIter < VBOMaxType; ++anIter)
+  if (!myVboIndices.IsNull())
   {
-    if (!myVbos[anIter].IsNull())
-    {
-      myVbos[anIter]->Release (theGlCtx.operator->());
-      myVbos[anIter].Nullify();
-    }
+    myVboIndices->Release (theGlCtx.operator->());
+    myVboIndices.Nullify();
+  }
+  if (!myVboAttribs.IsNull())
+  {
+    myVboAttribs->Release (theGlCtx.operator->());
+    myVboAttribs.Nullify();
   }
 }
 
 // =======================================================================
-// function : BuildVBO
+// function : initNormalVbo
 // purpose  :
 // =======================================================================
-Standard_Boolean OpenGl_PrimitiveArray::BuildVBO (const Handle(OpenGl_Workspace)& theWorkspace) const
+Standard_Boolean OpenGl_PrimitiveArray::initNormalVbo (const Handle(OpenGl_Context)& theCtx) const
 {
-  const Handle(OpenGl_Context)& aGlCtx = theWorkspace->GetGlContext();
-  if (myPArray->vertices == NULL)
+  switch (myAttribs->NbAttributes)
   {
-    // vertices should be always defined - others are optional
-    return Standard_False;
+    case 1:  myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 1> (*myAttribs); break;
+    case 2:  myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 2> (*myAttribs); break;
+    case 3:  myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 3> (*myAttribs); break;
+    case 4:  myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 4> (*myAttribs); break;
+    case 5:  myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 5> (*myAttribs); break;
+    case 6:  myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 6> (*myAttribs); break;
+    case 7:  myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 7> (*myAttribs); break;
+    case 8:  myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 8> (*myAttribs); break;
+    case 9:  myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 9> (*myAttribs); break;
+    case 10: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 10>(*myAttribs); break;
   }
-  myVbos[VBOVertices] = new OpenGl_VertexBuffer();
-  if (!myVbos[VBOVertices]->Init (aGlCtx, 3, myPArray->num_vertexs, &myPArray->vertices[0].xyz[0]))
+
+  const Standard_Boolean isAttribMutable     = myAttribs->IsMutable();
+  const Standard_Boolean isAttribInterleaved = myAttribs->IsInterleaved();
+  if (myAttribs->NbElements != myAttribs->NbMaxElements()
+   && myIndices.IsNull()
+   && (!isAttribInterleaved || isAttribMutable))
   {
-    clearMemoryGL (aGlCtx);
-    return Standard_False;
+    throw Standard_ProgramError ("OpenGl_PrimitiveArray::buildVBO() - vertex attribute data with reserved size is not supported");
   }
 
-  if (myPArray->edges != NULL
-   && myPArray->num_edges > 0)
+  // specify data type as Byte and NbComponents as Stride, so that OpenGl_VertexBuffer::EstimatedDataSize() will return correct value
+  const Standard_Integer aNbVertexes = (isAttribMutable || !isAttribInterleaved) ? myAttribs->NbMaxElements() : myAttribs->NbElements;
+  if (!myVboAttribs->init (theCtx, myAttribs->Stride, aNbVertexes, myAttribs->Data(), GL_UNSIGNED_BYTE, myAttribs->Stride))
   {
-    myVbos[VBOEdges] = new OpenGl_IndexBuffer();
-    if (!myVbos[VBOEdges]->Init (aGlCtx, 1, myPArray->num_edges, (GLuint* )myPArray->edges))
-    {
-      clearMemoryGL (aGlCtx);
-      return Standard_False;
-    }
+    TCollection_ExtendedString aMsg = TCollection_ExtendedString("VBO creation for Primitive Array has failed for ") + aNbVertexes + " vertices. Out of memory?";
+    theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PERFORMANCE, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
+
+    clearMemoryGL (theCtx);
+    return Standard_False;
   }
-  if (myPArray->vcolours != NULL)
+  else if (myIndices.IsNull())
   {
-    myVbos[VBOVcolours] = new OpenGl_VertexBuffer();
-    if (!myVbos[VBOVcolours]->Init (aGlCtx, 4, myPArray->num_vertexs, (GLubyte* )myPArray->vcolours))
+    if (isAttribMutable && isAttribInterleaved)
     {
-      clearMemoryGL (aGlCtx);
-      return Standard_False;
+      // for mutable interlaced array we can change dynamically number of vertexes (they will be just skipped at the end of buffer);
+      // this doesn't matter in case if we have indexed array
+      myVboAttribs->SetElemsNb (myAttribs->NbElements);
     }
+    return Standard_True;
   }
-  if (myPArray->vnormals != NULL)
+
+  const Standard_Integer aNbIndexes = !myIndices->IsMutable() ? myIndices->NbElements : myIndices->NbMaxElements();
+  myVboIndices = new OpenGl_IndexBuffer();
+  bool isOk = false;
+  switch (myIndices->Stride)
   {
-    myVbos[VBOVnormals] = new OpenGl_VertexBuffer();
-    if (!myVbos[VBOVnormals]->Init (aGlCtx, 3, myPArray->num_vertexs, &myPArray->vnormals[0].xyz[0]))
+    case 2:
     {
-      clearMemoryGL (aGlCtx);
-      return Standard_False;
+      isOk = myVboIndices->Init (theCtx, 1, aNbIndexes, reinterpret_cast<const GLushort*> (myIndices->Data()));
+      myVboIndices->SetElemsNb (myIndices->NbElements);
+      myIndices->Validate();
+      break;
     }
-  }
-  if (myPArray->vtexels)
-  {
-    myVbos[VBOVtexels] = new OpenGl_VertexBuffer();
-    if (!myVbos[VBOVtexels]->Init (aGlCtx, 2, myPArray->num_vertexs, &myPArray->vtexels[0].xy[0]))
+    case 4:
     {
-      clearMemoryGL (aGlCtx);
+      isOk = myVboIndices->Init (theCtx, 1, aNbIndexes, reinterpret_cast<const GLuint*> (myIndices->Data()));
+      myVboIndices->SetElemsNb (myIndices->NbElements);
+      myIndices->Validate();
+      break;
+    }
+    default:
+    {
+      clearMemoryGL (theCtx);
       return Standard_False;
     }
   }
-
-  if (!aGlCtx->caps->keepArrayData)
+  if (!isOk)
   {
-    clearMemoryOwn();
+    TCollection_ExtendedString aMsg = TCollection_ExtendedString("VBO creation for Primitive Array has failed for ") + aNbIndexes + " indices. Out of memory?";
+    theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PERFORMANCE, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
+    clearMemoryGL (theCtx);
+    return Standard_False;
   }
-  
   return Standard_True;
 }
 
 // =======================================================================
-// function : DrawArray
+// function : buildVBO
 // purpose  :
 // =======================================================================
-void OpenGl_PrimitiveArray::DrawArray (Tint theLightingModel,
-                                       const Aspect_InteriorStyle theInteriorStyle,
-                                       Tint theEdgeFlag,
-                                       const TEL_COLOUR* theInteriorColour,
-                                       const TEL_COLOUR* theLineColour,
-                                       const TEL_COLOUR* theEdgeColour,
-                                       const OPENGL_SURF_PROP* theFaceProp,
-                                       const Handle(OpenGl_Workspace)& theWorkspace) const
+Standard_Boolean OpenGl_PrimitiveArray::buildVBO (const Handle(OpenGl_Context)& theCtx,
+                                                  const Standard_Boolean        theToKeepData) const
 {
-  const Handle(OpenGl_Context)& aGlContext = theWorkspace->GetGlContext();
-
-  Tint i,n;
-  Tint transp = 0;
-  // Following pointers have been provided for performance improvement
-  tel_colour pfc = myPArray->fcolours;
-  Tint* pvc = myPArray->vcolours;
-  if (pvc != NULL)
-  {
-    for (i = 0; i < myPArray->num_vertexs; ++i)
-    {
-      transp = int(theFaceProp->trans * 255.0f);
-    #if defined (sparc) || defined (__sparc__) || defined (__sparc)
-      pvc[i] = (pvc[i] & 0xffffff00);
-      pvc[i] += transp;
-    #else
-      pvc[i] = (pvc[i] & 0x00ffffff);
-      pvc[i] += transp << 24;
-    #endif
-    }
-  }
-
-  switch (myPArray->type)
-  {
-    case TelPointsArrayType:
-    case TelPolylinesArrayType:
-    case TelSegmentsArrayType:
-      glColor3fv (theLineColour->rgb);
-      break;
-    case TelPolygonsArrayType:
-    case TelTrianglesArrayType:
-    case TelQuadranglesArrayType:
-    case TelTriangleStripsArrayType:
-    case TelQuadrangleStripsArrayType:
-    case TelTriangleFansArrayType:
-      glColor3fv (theInteriorColour->rgb);
-      break;
-    case TelUnknownArrayType:
-      break;
-  }
-
-  // Temporarily disable environment mapping
-  if (myDrawMode <= GL_LINE_STRIP)
+  bool isNormalMode = theCtx->ToUseVbo();
+  clearMemoryGL (theCtx);
+  if (myAttribs.IsNull()
+   || myAttribs->IsEmpty()
+   || myAttribs->NbElements < 1
+   || myAttribs->NbAttributes < 1
+   || myAttribs->NbAttributes > 10)
   {
-    glPushAttrib (GL_ENABLE_BIT);
-    glDisable (GL_TEXTURE_1D);
-    glDisable (GL_TEXTURE_2D);
+    // vertices should be always defined - others are optional
+    return Standard_False;
   }
 
-  if ((myDrawMode >  GL_LINE_STRIP && theInteriorStyle != Aspect_IS_EMPTY) ||
-      (myDrawMode <= GL_LINE_STRIP))
+  if (isNormalMode
+   && initNormalVbo (theCtx))
   {
-    if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
+    if (!theCtx->caps->keepArrayData
+     && !theToKeepData
+     && !myAttribs->IsMutable())
     {
-      pfc = NULL;
-      pvc = NULL;
+      myIndices.Nullify();
+      myAttribs.Nullify();
     }
-
-    if (theInteriorStyle == Aspect_IS_HIDDENLINE)
+    else
     {
-      theEdgeFlag = 1;
-      pfc = NULL;
-      pvc = NULL;
+      myAttribs->Validate();
     }
+    return Standard_True;
+  }
 
-    // Sometimes the GL_LIGHTING mode is activated here
-    // without glEnable(GL_LIGHTING) call for an unknown reason, so it is necessary
-    // to call glEnable(GL_LIGHTING) to synchronize Light On/Off mechanism*
-    if (theLightingModel == 0 || myDrawMode <= GL_LINE_STRIP)
-      glDisable (GL_LIGHTING);
-    else
-      glEnable (GL_LIGHTING);
-
-    if (!toDrawVbo())
+  Handle(OpenGl_VertexBufferCompat) aVboAttribs;
+  switch (myAttribs->NbAttributes)
+  {
+    case 1:  aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 1> (*myAttribs); break;
+    case 2:  aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 2> (*myAttribs); break;
+    case 3:  aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 3> (*myAttribs); break;
+    case 4:  aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 4> (*myAttribs); break;
+    case 5:  aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 5> (*myAttribs); break;
+    case 6:  aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 6> (*myAttribs); break;
+    case 7:  aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 7> (*myAttribs); break;
+    case 8:  aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 8> (*myAttribs); break;
+    case 9:  aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 9> (*myAttribs); break;
+    case 10: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 10>(*myAttribs); break;
+  }
+  aVboAttribs->initLink (myAttribs, 0, myAttribs->NbElements, GL_NONE);
+  if (!myIndices.IsNull())
+  {
+    Handle(OpenGl_VertexBufferCompat) aVboIndices = new OpenGl_VertexBufferCompat();
+    switch (myIndices->Stride)
     {
-      if (myPArray->vertices != NULL)
+      case 2:
       {
-        glVertexPointer (3, GL_FLOAT, 0, myPArray->vertices); // array of vertices
-        glEnableClientState (GL_VERTEX_ARRAY);
+        aVboIndices->initLink (myIndices, 1, myIndices->NbElements, GL_UNSIGNED_SHORT);
+        break;
       }
-      if (myPArray->vnormals != NULL)
+      case 4:
       {
-        glNormalPointer (GL_FLOAT, 0, myPArray->vnormals); // array of normals
-        glEnableClientState (GL_NORMAL_ARRAY);
+        aVboIndices->initLink (myIndices, 1, myIndices->NbElements, GL_UNSIGNED_INT);
+        break;
       }
-      if (myPArray->vtexels != NULL)
+      default:
       {
-        glTexCoordPointer (2, GL_FLOAT, 0, myPArray->vtexels); // array of texture coordinates
-        glEnableClientState (GL_TEXTURE_COORD_ARRAY);
+        return Standard_False;
       }
+    }
+    myVboIndices = aVboIndices;
+  }
+  myVboAttribs = aVboAttribs;
+  if (!theCtx->caps->keepArrayData
+   && !theToKeepData)
+  {
+    // does not make sense for compatibility mode
+    //myIndices.Nullify();
+    //myAttribs.Nullify();
+  }
+
+  return Standard_True;
+}
 
-      if ((pvc != NULL) && (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) == 0)
+// =======================================================================
+// function : updateVBO
+// purpose  :
+// =======================================================================
+void OpenGl_PrimitiveArray::updateVBO (const Handle(OpenGl_Context)& theCtx) const
+{
+  if (!myAttribs.IsNull())
+  {
+    Graphic3d_BufferRange aRange = myAttribs->InvalidatedRange();
+    if (!aRange.IsEmpty()
+     &&  myVboAttribs->IsValid()
+     && !myVboAttribs->IsVirtual())
+    {
+      myVboAttribs->Bind (theCtx);
+      theCtx->core15fwd->glBufferSubData (myVboAttribs->GetTarget(),
+                                          aRange.Start,
+                                          aRange.Length,
+                                          myAttribs->Data() + aRange.Start);
+      myVboAttribs->Unbind (theCtx);
+      if (myAttribs->IsInterleaved())
       {
-        glColorPointer (4, GL_UNSIGNED_BYTE, 0, pvc);  // array of colors
-        glEnableClientState (GL_COLOR_ARRAY);
-        glColorMaterial (GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
-        glEnable (GL_COLOR_MATERIAL);
+        myVboAttribs->SetElemsNb (myAttribs->NbElements);
       }
     }
-    else
+    myAttribs->Validate();
+  }
+  if (!myIndices.IsNull())
+  {
+    Graphic3d_BufferRange aRange = myIndices->InvalidatedRange();
+    if (!aRange.IsEmpty()
+     &&  myVboIndices->IsValid()
+     && !myVboIndices->IsVirtual())
     {
-      // Bindings concrete pointer in accordance with VBO buffer
-      myVbos[VBOVertices]->BindFixed (aGlContext, GL_VERTEX_ARRAY);
-      if (!myVbos[VBOVnormals].IsNull())
-      {
-        myVbos[VBOVnormals]->BindFixed (aGlContext, GL_NORMAL_ARRAY);
-      }
-      if (!myVbos[VBOVtexels].IsNull() && (theWorkspace->NamedStatus & OPENGL_NS_FORBIDSETTEX) == 0)
-      {
-        myVbos[VBOVtexels]->BindFixed (aGlContext, GL_TEXTURE_COORD_ARRAY);
-      }
-      if (!myVbos[VBOVcolours].IsNull() && (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) == 0)
-      {
-        myVbos[VBOVcolours]->BindFixed (aGlContext, GL_COLOR_ARRAY);
-        glColorMaterial (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
-        glEnable (GL_COLOR_MATERIAL);
-      }
+      myVboIndices->Bind (theCtx);
+      theCtx->core15fwd->glBufferSubData (myVboIndices->GetTarget(),
+                                          aRange.Start,
+                                          aRange.Length,
+                                          myIndices->Data() + aRange.Start);
+      myVboIndices->Unbind (theCtx);
+      myVboIndices->SetElemsNb (myIndices->NbElements);
     }
+    myIndices->Validate();
+  }
+}
 
-    /// OCC22236 NOTE: draw for all situations:
-    /// 1) draw elements from myPArray->bufferVBO[VBOEdges] indicies array
-    /// 2) draw elements from vertice array, when bounds defines count of primitive's verts.
-    /// 3) draw primitive by vertexes if no edges and bounds array is specified
-    if (toDrawVbo())
+// =======================================================================
+// function : drawArray
+// purpose  :
+// =======================================================================
+void OpenGl_PrimitiveArray::drawArray (const Handle(OpenGl_Workspace)& theWorkspace,
+                                       const Graphic3d_Vec4*           theFaceColors,
+                                       const Standard_Boolean          theHasVertColor) const
+{
+  if (myVboAttribs.IsNull())
+  {
+  #if !defined(GL_ES_VERSION_2_0)
+    if (myDrawMode == GL_POINTS)
     {
-      if (!myVbos[VBOEdges].IsNull())
-      {
-        myVbos[VBOEdges]->Bind (aGlContext);
-        if (myPArray->num_bounds > 0)
-        {
-          // draw primitives by vertex count with the indicies
-          Tint* anOffset = NULL;
-          for (i = 0; i < myPArray->num_bounds; ++i)
-          {
-            if (pfc != NULL) glColor3fv (pfc[i].rgb);
-            glDrawElements (myDrawMode, myPArray->bounds[i], myVbos[VBOEdges]->GetDataType(), anOffset);
-            anOffset += myPArray->bounds[i];
-          }
-        }
-        else
-        {
-          // draw one (or sequential) primitive by the indicies
-          glDrawElements (myDrawMode, myPArray->num_edges, myVbos[VBOEdges]->GetDataType(), NULL);
-        }
-        myVbos[VBOEdges]->Unbind (aGlContext);
-      }
-      else if (myPArray->num_bounds > 0)
-      {
-        for (i = n = 0; i < myPArray->num_bounds; ++i)
-        {
-          if (pfc != NULL) glColor3fv (pfc[i].rgb);
-          glDrawArrays (myDrawMode, n, myPArray->bounds[i]);
-          n += myPArray->bounds[i];
-        }
-      }
-      else
-      {
-        if (myDrawMode == GL_POINTS)
-        {
-          DrawMarkers (theWorkspace);
-        }
-        else
-        {
-          glDrawArrays (myDrawMode, 0, myVbos[VBOVertices]->GetElemsNb());
-        }
-      }
+      // extreme compatibility mode - without sprites but with markers
+      drawMarkers (theWorkspace);
+    }
+  #endif
+    return;
+  }
 
-      // bind with 0
-      myVbos[VBOVertices]->UnbindFixed (aGlContext, GL_VERTEX_ARRAY);
-      if (!myVbos[VBOVnormals].IsNull())
-      {
-        myVbos[VBOVnormals]->UnbindFixed (aGlContext, GL_NORMAL_ARRAY);
-      }
-      if (!myVbos[VBOVtexels].IsNull() && (theWorkspace->NamedStatus & OPENGL_NS_FORBIDSETTEX) == 0)
-      {
-        myVbos[VBOVtexels]->UnbindFixed (aGlContext, GL_TEXTURE_COORD_ARRAY);
-      }
-      if (!myVbos[VBOVcolours].IsNull())
+  const Handle(OpenGl_Context)& aGlContext = theWorkspace->GetGlContext();
+  const bool                    toHilight  = theWorkspace->ToHighlight();
+  const GLenum aDrawMode = !aGlContext->ActiveProgram().IsNull()
+                         && aGlContext->ActiveProgram()->HasTessellationStage()
+                         ? GL_PATCHES
+                         : myDrawMode;
+  myVboAttribs->BindAllAttributes (aGlContext);
+  if (theHasVertColor && toHilight)
+  {
+    // disable per-vertex color
+    OpenGl_VertexBuffer::unbindAttribute (aGlContext, Graphic3d_TOA_COLOR);
+  }
+  if (!myVboIndices.IsNull())
+  {
+    myVboIndices->Bind (aGlContext);
+    GLubyte* anOffset = myVboIndices->GetDataOffset();
+    if (!myBounds.IsNull())
+    {
+      // draw primitives by vertex count with the indices
+      const size_t aStride = myVboIndices->GetDataType() == GL_UNSIGNED_SHORT ? sizeof(unsigned short) : sizeof(unsigned int);
+      for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
       {
-        myVbos[VBOVcolours]->UnbindFixed (aGlContext, GL_COLOR_ARRAY);
-        glDisable (GL_COLOR_MATERIAL);
-        theWorkspace->NamedStatus |= OPENGL_NS_RESMAT; // Reset material
+        const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
+        if (theFaceColors != NULL) aGlContext->SetColor4fv (theFaceColors[aGroupIter]);
+        glDrawElements (aDrawMode, aNbElemsInGroup, myVboIndices->GetDataType(), anOffset);
+        anOffset += aStride * aNbElemsInGroup;
       }
     }
     else
     {
-      if (myPArray->num_bounds > 0)
-      {
-        if (myPArray->num_edges > 0)
-        {
-          for (i = n = 0; i < myPArray->num_bounds; ++i)
-          {
-            if (pfc != NULL) glColor3fv (pfc[i].rgb);
-            glDrawElements (myDrawMode, myPArray->bounds[i], GL_UNSIGNED_INT, (GLenum* )&myPArray->edges[n]);
-            n += myPArray->bounds[i];
-          }
-        }
-        else
-        {
-          for (i = n = 0; i < myPArray->num_bounds; ++i)
-          {
-            if (pfc != NULL) glColor3fv (pfc[i].rgb);
-            glDrawArrays (myDrawMode, n, myPArray->bounds[i]);
-            n += myPArray->bounds[i];
-          }
-        }
-      }
-      else if (myPArray->num_edges > 0)
-      {
-        glDrawElements (myDrawMode, myPArray->num_edges, GL_UNSIGNED_INT, (GLenum* )myPArray->edges);
-      }
-      else
-      {
-        if (myDrawMode == GL_POINTS)
-        {
-          DrawMarkers (theWorkspace);
-        }
-        else
-        {
-          glDrawArrays (myDrawMode, 0, myPArray->num_vertexs);
-        }
-      }
-
-      if (pvc != NULL)
-      {
-        glDisable (GL_COLOR_MATERIAL);
-        theWorkspace->NamedStatus |= OPENGL_NS_RESMAT; // Reset material
-      }
-
-      glDisableClientState (GL_VERTEX_ARRAY);
-      if (myPArray->vcolours != NULL)
-        glDisableClientState (GL_COLOR_ARRAY);
-      if (myPArray->vnormals != NULL)
-        glDisableClientState (GL_NORMAL_ARRAY);
-      if (myPArray->vtexels != NULL)
-        glDisableClientState (GL_TEXTURE_COORD_ARRAY);
+      // draw one (or sequential) primitive by the indices
+      glDrawElements (aDrawMode, myVboIndices->GetElemsNb(), myVboIndices->GetDataType(), anOffset);
     }
+    myVboIndices->Unbind (aGlContext);
   }
-
-  // On some NVIDIA graphic cards, using glEdgeFlagPointer() in
-  // combination with VBO (edge flag data put into a VBO buffer)
-  // leads to a crash in a driver. Therefore, edge flags are simply
-  // igonored when VBOs are enabled, so all the edges are drawn if
-  // edge visibility is turned on. In order to draw edges selectively,
-  // either disable VBO or turn off edge visibilty in the current
-  // primitive array and create a separate primitive array (segments)
-  // and put edges to be drawn into it.
-  if (theEdgeFlag && myDrawMode > GL_LINE_STRIP)
+  else if (!myBounds.IsNull())
   {
-    DrawEdges (theEdgeColour, theWorkspace);
+    GLint aFirstElem = 0;
+    for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
+    {
+      const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
+      if (theFaceColors != NULL) aGlContext->SetColor4fv (theFaceColors[aGroupIter]);
+      glDrawArrays (aDrawMode, aFirstElem, aNbElemsInGroup);
+      aFirstElem += aNbElemsInGroup;
+    }
+  }
+  else
+  {
+    if (myDrawMode == GL_POINTS)
+    {
+      drawMarkers (theWorkspace);
+    }
+    else
+    {
+      glDrawArrays (aDrawMode, 0, myVboAttribs->GetElemsNb());
+    }
   }
 
-  if (myDrawMode <= GL_LINE_STRIP)
-    glPopAttrib();
+  // bind with 0
+  myVboAttribs->UnbindAllAttributes (aGlContext);
 }
 
 // =======================================================================
-// function : DrawEdges
+// function : drawEdges
 // purpose  :
 // =======================================================================
-void OpenGl_PrimitiveArray::DrawEdges (const TEL_COLOUR*               theEdgeColour,
-                                       const Handle(OpenGl_Workspace)& theWorkspace) const
+void OpenGl_PrimitiveArray::drawEdges (const Handle(OpenGl_Workspace)& theWorkspace) const
 {
-  glDisable (GL_LIGHTING);
-
   const Handle(OpenGl_Context)& aGlContext = theWorkspace->GetGlContext();
-  const OpenGl_AspectLine* anAspectLineOld = NULL;
-  if (myDrawMode > GL_LINE_STRIP)
+  if (myVboAttribs.IsNull())
   {
-    anAspectLineOld = theWorkspace->SetAspectLine (theWorkspace->AspectFace (Standard_True)->AspectEdge());
-    const OpenGl_AspectLine* anAspect = theWorkspace->AspectLine (Standard_True);
+    return;
+  }
 
-    glPushAttrib (GL_POLYGON_BIT);
-    glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
+  const OpenGl_Aspects* anAspect = theWorkspace->Aspects();
+#if !defined(GL_ES_VERSION_2_0)
+  const Standard_Integer aPolyModeOld = aGlContext->SetPolygonMode (GL_LINE);
+#endif
 
-    if (aGlContext->IsGlGreaterEqual (2, 0))
-    {
-      BindProgramWithMaterial (theWorkspace, anAspect);
-    }
+  if (aGlContext->core20fwd != NULL)
+  {
+    aGlContext->ShaderManager()->BindLineProgram (Handle(OpenGl_TextureSet)(), anAspect->Aspect()->EdgeLineType(),
+                                                  Graphic3d_TOSM_UNLIT, Graphic3d_AlphaMode_Opaque, Standard_False,
+                                                  anAspect->ShaderProgramRes (aGlContext));
   }
-
-  Tint i, j, n;
+  aGlContext->SetSampleAlphaToCoverage (aGlContext->ShaderManager()->MaterialState().HasAlphaCutoff());
+  const GLenum aDrawMode = !aGlContext->ActiveProgram().IsNull()
+                         && aGlContext->ActiveProgram()->HasTessellationStage()
+                         ? GL_PATCHES
+                         : myDrawMode;
+#if !defined(GL_ES_VERSION_2_0)
+  if (aGlContext->ActiveProgram().IsNull()
+   && aGlContext->core11 != NULL)
+  {
+    glDisable (GL_LIGHTING);
+  }
+#endif
 
   /// OCC22236 NOTE: draw edges for all situations:
-  /// 1) draw elements with GL_LINE style as edges from myPArray->bufferVBO[VBOEdges] indicies array
-  /// 2) draw elements from vertice array, when bounds defines count of primitive's verts.
+  /// 1) draw elements with GL_LINE style as edges from myPArray->bufferVBO[VBOEdges] indices array
+  /// 2) draw elements from vertex array, when bounds defines count of primitive's vertices.
   /// 3) draw primitive's edges by vertexes if no edges and bounds array is specified
-  if (toDrawVbo())
+  myVboAttribs->BindPositionAttribute (aGlContext);
+
+  aGlContext->SetColor4fv (theWorkspace->EdgeColor().a() >= 0.1f
+                         ? theWorkspace->EdgeColor()
+                         : theWorkspace->View()->BackgroundColor());
+  aGlContext->SetTypeOfLine (anAspect->Aspect()->EdgeLineType());
+  aGlContext->SetLineWidth  (anAspect->Aspect()->EdgeWidth());
+
+  if (!myVboIndices.IsNull())
   {
-    myVbos[VBOVertices]->BindFixed (aGlContext, GL_VERTEX_ARRAY);
-    glColor3fv (theEdgeColour->rgb);
-    if (!myVbos[VBOEdges].IsNull())
-    {
-      myVbos[VBOEdges]->Bind (aGlContext);
+    myVboIndices->Bind (aGlContext);
+    GLubyte* anOffset = myVboIndices->GetDataOffset();
 
-      // draw primitives by vertex count with the indicies
-      if (myPArray->num_bounds > 0)
-      {
-        Tint* offset = 0;
-        for (i = 0, offset = 0; i < myPArray->num_bounds; ++i)
-        {
-          glDrawElements (myDrawMode, myPArray->bounds[i], myVbos[VBOEdges]->GetDataType(), offset);
-          offset += myPArray->bounds[i];
-        }
-      }
-      // draw one (or sequential) primitive by the indicies
-      else
-      {
-        glDrawElements (myDrawMode, myVbos[VBOEdges]->GetElemsNb(), myVbos[VBOEdges]->GetDataType(), NULL);
-      }
-      myVbos[VBOEdges]->Unbind (aGlContext);
-    }
-    else if (myPArray->num_bounds > 0)
+    // draw primitives by vertex count with the indices
+    if (!myBounds.IsNull())
     {
-      for (i = n = 0; i < myPArray->num_bounds; ++i)
+      const size_t aStride = myVboIndices->GetDataType() == GL_UNSIGNED_SHORT ? sizeof(unsigned short) : sizeof(unsigned int);
+      for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
       {
-        glDrawArrays (myDrawMode, n, myPArray->bounds[i]);
-        n += myPArray->bounds[i];
+        const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
+        glDrawElements (aDrawMode, aNbElemsInGroup, myVboIndices->GetDataType(), anOffset);
+        anOffset += aStride * aNbElemsInGroup;
       }
     }
+    // draw one (or sequential) primitive by the indices
     else
     {
-      glDrawArrays (myDrawMode, 0, myPArray->num_vertexs);
+      glDrawElements (aDrawMode, myVboIndices->GetElemsNb(), myVboIndices->GetDataType(), anOffset);
     }
-
-    // unbind buffers
-    myVbos[VBOVertices]->UnbindFixed (aGlContext, GL_VERTEX_ARRAY);
+    myVboIndices->Unbind (aGlContext);
   }
-  else
+  else if (!myBounds.IsNull())
   {
-    glEnableClientState (GL_VERTEX_ARRAY);
-    glVertexPointer (3, GL_FLOAT, 0, myPArray->vertices); // array of vertices
-
-    glColor3fv (theEdgeColour->rgb);
-    if (myPArray->num_bounds > 0)
+    GLint aFirstElem = 0;
+    for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
     {
-      if (myPArray->num_edges > 0)
-      {
-        for (i = n = 0; i < myPArray->num_bounds; ++i)
-        {
-          if (myPArray->edge_vis)
-          {
-            glBegin (myDrawMode);
-            for (j = 0; j < myPArray->bounds[i]; ++j)
-            {
-              glEdgeFlag (myPArray->edge_vis[n+j]);
-              glVertex3fv (&myPArray->vertices[myPArray->edges[n+j]].xyz[0]);
-            }
-            glEnd();
-          }
-          else
-          {
-            glDrawElements (myDrawMode, myPArray->bounds[i], GL_UNSIGNED_INT, (GLenum* )&myPArray->edges[n]);
-          }
-          n += myPArray->bounds[i];
-        }
-      }
-      else
-      {
-        for (i = n = 0 ; i < myPArray->num_bounds; ++i)
-        {
-          glDrawArrays (myDrawMode, n, myPArray->bounds[i]);
-          n += myPArray->bounds[i];
-        }
-      }
-    }
-    else if (myPArray->num_edges > 0)
-    {
-      if (myPArray->edge_vis)
-      {
-        glBegin (myDrawMode);
-        for (i = 0; i < myPArray->num_edges; ++i)
-        {
-          glEdgeFlag (myPArray->edge_vis[i]);
-          glVertex3fv (&myPArray->vertices[myPArray->edges[i]].xyz[0]);
-        }
-        glEnd();
-      }
-      else
-      {
-        glDrawElements (myDrawMode, myPArray->num_edges, GL_UNSIGNED_INT, (GLenum* )myPArray->edges);
-      }
-    }
-    else
-    {
-      glDrawArrays (myDrawMode, 0, myPArray->num_vertexs);
+      const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
+      glDrawArrays (aDrawMode, aFirstElem, aNbElemsInGroup);
+      aFirstElem += aNbElemsInGroup;
     }
   }
-
-  if (myDrawMode > GL_LINE_STRIP)
+  else
   {
-    // Restore line context
-    theWorkspace->SetAspectLine (anAspectLineOld);
-    glPopAttrib();
+    glDrawArrays (aDrawMode, 0, !myVboAttribs.IsNull() ? myVboAttribs->GetElemsNb() : myAttribs->NbElements);
   }
+
+  // unbind buffers
+  myVboAttribs->UnbindAttribute (aGlContext, Graphic3d_TOA_POS);
+
+  // restore line context
+#if !defined(GL_ES_VERSION_2_0)
+  aGlContext->SetPolygonMode (aPolyModeOld);
+#endif
 }
 
 // =======================================================================
-// function : DrawMarkers
+// function : drawMarkers
 // purpose  :
 // =======================================================================
-void OpenGl_PrimitiveArray::DrawMarkers (const Handle(OpenGl_Workspace)& theWorkspace) const
+void OpenGl_PrimitiveArray::drawMarkers (const Handle(OpenGl_Workspace)& theWorkspace) const
 {
-  const OpenGl_AspectMarker* anAspectMarker     = theWorkspace->AspectMarker (Standard_True);
-  const Handle(OpenGl_Context)&     aCtx        = theWorkspace->GetGlContext();
-  const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes (theWorkspace);
-  const Standard_Boolean            isHilight   = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT);
-  if (aCtx->IsGlGreaterEqual (2, 0)
-   && !aSpriteNorm.IsNull() && !aSpriteNorm->IsDisplayList())
+  const OpenGl_Aspects* anAspectMarker = theWorkspace->Aspects();
+  const Handle(OpenGl_Context)&   aCtx = theWorkspace->GetGlContext();
+  const GLenum aDrawMode = !aCtx->ActiveProgram().IsNull()
+                         && aCtx->ActiveProgram()->HasTessellationStage()
+                         ? GL_PATCHES
+                         : myDrawMode;
+  if (anAspectMarker->HasPointSprite (aCtx))
   {
     // Textured markers will be drawn with the point sprites
-    glPointSize (anAspectMarker->MarkerSize());
-
-    Handle(OpenGl_Texture) aTextureBack;
-    if (anAspectMarker->Type() != Aspect_TOM_POINT)
+    aCtx->SetPointSize (anAspectMarker->MarkerSize());
+    aCtx->SetPointSpriteOrigin();
+  #if !defined(GL_ES_VERSION_2_0)
+    if (aCtx->core11 != NULL)
     {
-      const Handle(OpenGl_PointSprite)& aSprite = (isHilight && anAspectMarker->SpriteHighlightRes (theWorkspace)->IsValid())
-                                                ? anAspectMarker->SpriteHighlightRes (theWorkspace)
-                                                : aSpriteNorm;
-      aTextureBack = theWorkspace->EnableTexture (aSprite);
-
-      glEnable (GL_ALPHA_TEST);
-      glAlphaFunc (GL_GEQUAL, 0.1f);
-
-      glEnable (GL_BLEND);
-      glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+      aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
+      aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
     }
+  #endif
+
+    aCtx->core11fwd->glEnable (GL_BLEND);
+    aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
-    glDrawArrays (myDrawMode, 0, toDrawVbo() ? myVbos[VBOVertices]->GetElemsNb() : myPArray->num_vertexs);
+    aCtx->core11fwd->glDrawArrays (aDrawMode, 0, !myVboAttribs.IsNull() ? myVboAttribs->GetElemsNb() : myAttribs->NbElements);
 
-    glDisable (GL_BLEND);
-    glDisable (GL_ALPHA_TEST);
-    if (anAspectMarker->Type() != Aspect_TOM_POINT)
+    aCtx->core11fwd->glDisable (GL_BLEND);
+  #if !defined(GL_ES_VERSION_2_0)
+    if (aCtx->core11 != NULL)
     {
-      theWorkspace->EnableTexture (aTextureBack);
+      if (aCtx->ShaderManager()->MaterialState().AlphaCutoff() >= ShortRealLast())
+      {
+        aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
+      }
+      else
+      {
+        aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, aCtx->ShaderManager()->MaterialState().AlphaCutoff());
+      }
     }
-    glPointSize (1.0f);
+  #endif
+    aCtx->SetPointSize (1.0f);
     return;
   }
-
+  else if (anAspectMarker->Aspect()->MarkerType() == Aspect_TOM_POINT)
+  {
+    aCtx->SetPointSize (anAspectMarker->MarkerSize());
+    aCtx->core11fwd->glDrawArrays (aDrawMode, 0, !myVboAttribs.IsNull() ? myVboAttribs->GetElemsNb() : myAttribs->NbElements);
+    aCtx->SetPointSize (1.0f);
+  }
+#if !defined(GL_ES_VERSION_2_0)
   // Textured markers will be drawn with the glBitmap
-  if (anAspectMarker->Type() == Aspect_TOM_POINT
-   || anAspectMarker->Type() == Aspect_TOM_O_POINT)
+  else if (anAspectMarker->Aspect()->MarkerType() != Aspect_TOM_POINT)
   {
-    const GLfloat aPntSize = anAspectMarker->Type() == Aspect_TOM_POINT
-                           ? anAspectMarker->MarkerSize()
-                           : 0.0f;
-    if (aPntSize > 0.0f)
-    {
-      glPointSize (aPntSize);
-    }
-    glDrawArrays (myDrawMode, 0, toDrawVbo() ? myVbos[VBOVertices]->GetElemsNb() : myPArray->num_vertexs);
-    if (aPntSize > 0.0f)
+    const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes (aCtx, false);
+    if (aSpriteNorm.IsNull())
     {
-      glPointSize (1.0f);
+      return;
     }
-  }
 
-  if (anAspectMarker->Type() != Aspect_TOM_POINT
-   && !aSpriteNorm.IsNull())
-  {
-    if (!isHilight && (myPArray->vcolours != NULL))
+    /**if (!isHilight && (myPArray->vcolours != NULL))
     {
-      for (Standard_Integer anIter = 0; anIter < myPArray->num_vertexs; anIter++)
+      for (Standard_Integer anIter = 0; anIter < myAttribs->NbElements; anIter++)
       {
-        glColor4ubv ((GLubyte* )&myPArray->vcolours[anIter]);
-        glRasterPos3fv (myPArray->vertices[anIter].xyz);
+        glColor4ubv    (myPArray->vcolours[anIter].GetData());
+        glRasterPos3fv (myAttribs->Value<Graphic3d_Vec3> (anIter).GetData());
         aSpriteNorm->DrawBitmap (theWorkspace->GetGlContext());
       }
     }
-    else
+    else*/
     {
-      for (Standard_Integer anIter = 0; anIter < myPArray->num_vertexs; anIter++)
+      for (Standard_Integer anIter = 0; anIter < myAttribs->NbElements; anIter++)
       {
-        glRasterPos3fv (myPArray->vertices[anIter].xyz);
+        aCtx->core11->glRasterPos3fv (myAttribs->Value<Graphic3d_Vec3> (anIter).GetData());
         aSpriteNorm->DrawBitmap (theWorkspace->GetGlContext());
       }
     }
   }
+#endif
 }
 
 // =======================================================================
 // function : OpenGl_PrimitiveArray
 // purpose  :
 // =======================================================================
-OpenGl_PrimitiveArray::OpenGl_PrimitiveArray (CALL_DEF_PARRAY* thePArray)
-: myPArray (thePArray),
-  myDrawMode (DRAW_MODE_NONE),
+OpenGl_PrimitiveArray::OpenGl_PrimitiveArray (const OpenGl_GraphicDriver* theDriver)
+
+: myDrawMode  (DRAW_MODE_NONE),
+  myIsFillType(Standard_False),
   myIsVboInit (Standard_False)
 {
-  switch (myPArray->type)
+  if (theDriver != NULL)
   {
-    case TelPointsArrayType:
-      myDrawMode = GL_POINTS;
-      break;
-    case TelPolylinesArrayType:
-      myDrawMode = GL_LINE_STRIP;
-      break;
-    case TelSegmentsArrayType:
-      myDrawMode = GL_LINES;
-      break;
-    case TelPolygonsArrayType:
-      myDrawMode = GL_POLYGON;
-      break;
-    case TelTrianglesArrayType:
-      myDrawMode = GL_TRIANGLES;
-      break;
-    case TelQuadranglesArrayType:
-      myDrawMode = GL_QUADS;
-      break;
-    case TelTriangleStripsArrayType:
-      myDrawMode = GL_TRIANGLE_STRIP;
-      break;
-    case TelQuadrangleStripsArrayType:
-      myDrawMode = GL_QUAD_STRIP;
-      break;
-    case TelTriangleFansArrayType:
-      myDrawMode = GL_TRIANGLE_FAN;
-      break;
-    case TelUnknownArrayType:
-      break;
+    myUID = theDriver->GetNextPrimitiveArrayUID();
   }
 }
 
+// =======================================================================
+// function : OpenGl_PrimitiveArray
+// purpose  :
+// =======================================================================
+OpenGl_PrimitiveArray::OpenGl_PrimitiveArray (const OpenGl_GraphicDriver*          theDriver,
+                                              const Graphic3d_TypeOfPrimitiveArray theType,
+                                              const Handle(Graphic3d_IndexBuffer)& theIndices,
+                                              const Handle(Graphic3d_Buffer)&      theAttribs,
+                                              const Handle(Graphic3d_BoundBuffer)& theBounds)
+
+: myIndices   (theIndices),
+  myAttribs   (theAttribs),
+  myBounds    (theBounds),
+  myDrawMode  (DRAW_MODE_NONE),
+  myIsFillType(Standard_False),
+  myIsVboInit (Standard_False)
+{
+  if (!myIndices.IsNull()
+    && myIndices->NbElements < 1)
+  {
+    // dummy index buffer?
+    myIndices.Nullify();
+  }
+
+  if (theDriver != NULL)
+  {
+    myUID = theDriver->GetNextPrimitiveArrayUID();
+  #if defined (GL_ES_VERSION_2_0)
+    const Handle(OpenGl_Context)& aCtx = theDriver->GetSharedContext();
+    if (!aCtx.IsNull())
+    {
+      processIndices (aCtx);
+    }
+  #endif
+  }
+
+  setDrawMode (theType);
+}
+
 // =======================================================================
 // function : ~OpenGl_PrimitiveArray
 // purpose  :
@@ -703,18 +737,24 @@ OpenGl_PrimitiveArray::~OpenGl_PrimitiveArray()
 // function : Release
 // purpose  :
 // =======================================================================
-void OpenGl_PrimitiveArray::Release (const Handle(OpenGl_Context)& theContext)
+void OpenGl_PrimitiveArray::Release (OpenGl_Context* theContext)
 {
-  for (Standard_Integer anIter = 0; anIter < VBOMaxType; ++anIter)
+  myIsVboInit = Standard_False;
+  if (!myVboIndices.IsNull())
   {
-    if (!myVbos[anIter].IsNull())
+    if (theContext)
     {
-      if (!theContext.IsNull())
-      {
-        theContext->DelayedRelease (myVbos[anIter]);
-      }
-      myVbos[anIter].Nullify();
+      theContext->DelayedRelease (myVboIndices);
+    }
+    myVboIndices.Nullify();
+  }
+  if (!myVboAttribs.IsNull())
+  {
+    if (theContext)
+    {
+      theContext->DelayedRelease (myVboAttribs);
     }
+    myVboAttribs.Nullify();
   }
 }
 
@@ -724,87 +764,354 @@ void OpenGl_PrimitiveArray::Release (const Handle(OpenGl_Context)& theContext)
 // =======================================================================
 void OpenGl_PrimitiveArray::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
 {
-  if (myPArray == NULL || myDrawMode == DRAW_MODE_NONE || myPArray->num_vertexs <= 0)
+  if (myDrawMode == DRAW_MODE_NONE)
   {
     return;
   }
 
-  const OpenGl_AspectFace*   anAspectFace   = theWorkspace->AspectFace   (Standard_True);
-  const OpenGl_AspectLine*   anAspectLine   = theWorkspace->AspectLine   (Standard_True);
-  const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker (myDrawMode == GL_POINTS);
-
-  // create VBOs on first render call
+  const OpenGl_Aspects* anAspectFace = theWorkspace->ApplyAspects();
   const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
-  if (!myIsVboInit
-   && !aCtx->caps->vboDisable
-   && aCtx->core15 != NULL
-   && (myDrawMode != GL_POINTS || anAspectMarker->SpriteRes (theWorkspace).IsNull() || !anAspectMarker->SpriteRes (theWorkspace)->IsDisplayList()))
+
+  const bool toEnableEnvMap = !aCtx->ActiveTextures().IsNull()
+                            && aCtx->ActiveTextures() == theWorkspace->EnvironmentTexture();
+  bool toDrawArray = true;
+  int toDrawInteriorEdges = 0; // 0 - no edges, 1 - glsl edges, 2 - polygonMode
+  if (myIsFillType)
   {
-    if (!BuildVBO (theWorkspace))
+    toDrawArray = anAspectFace->Aspect()->InteriorStyle() != Aspect_IS_EMPTY;
+    if (anAspectFace->Aspect()->ToDrawEdges())
     {
-      TCollection_ExtendedString aMsg;
-      aMsg += "VBO creation for Primitive Array has failed for ";
-      aMsg += myPArray->num_vertexs;
-      aMsg += " vertices. Out of memory?";
-      aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_PERFORMANCE_ARB, 0, GL_DEBUG_SEVERITY_LOW_ARB, aMsg);
+      toDrawInteriorEdges = 1;
+      toDrawArray = true;
+    #if !defined(GL_ES_VERSION_2_0)
+      if (anAspectFace->Aspect()->EdgeLineType() != Aspect_TOL_SOLID
+       || aCtx->hasGeometryStage == OpenGl_FeatureNotAvailable
+       || aCtx->caps->usePolygonMode)
+      {
+        toDrawInteriorEdges = 2;
+        if (anAspectFace->Aspect()->InteriorStyle() == Aspect_IS_EMPTY)
+        {
+          if (anAspectFace->Aspect()->EdgeLineType() != Aspect_TOL_SOLID)
+          {
+            toDrawArray = false;
+          }
+          else
+          {
+            aCtx->SetPolygonMode (GL_LINE);
+          }
+        }
+      }
+    #endif
     }
-    myIsVboInit = Standard_True;
   }
-
-  switch (myPArray->type)
+  else
   {
-    case TelPointsArrayType:
-    case TelPolylinesArrayType:
-    case TelSegmentsArrayType:
+    if (myDrawMode == GL_POINTS)
     {
-      glDisable (GL_LIGHTING);
-      break;
+      if (anAspectFace->Aspect()->MarkerType() == Aspect_TOM_EMPTY)
+      {
+        return;
+      }
+    }
+    else
+    {
+      if (anAspectFace->Aspect()->LineType() == Aspect_TOL_EMPTY)
+      {
+        return;
+      }
     }
-    default:
-      break;
   }
 
-  Tint aFrontLightingModel = anAspectFace->IntFront().color_mask;
-  const TEL_COLOUR* anInteriorColor = &anAspectFace->IntFront().matcol;
-  const TEL_COLOUR* anEdgeColor = &anAspectFace->AspectEdge()->Color();
-  const TEL_COLOUR* aLineColor = (myPArray->type == TelPointsArrayType) ? &anAspectMarker->Color() : &anAspectLine->Color();
-
-  // Use highlight colors
-  if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
+  // create VBOs on first render call
+  if (!myIsVboInit)
   {
-    anEdgeColor = anInteriorColor = aLineColor = theWorkspace->HighlightColor;
-    aFrontLightingModel = 0;
+    // compatibility - keep data to draw markers using display lists
+    Standard_Boolean toKeepData = myDrawMode == GL_POINTS
+                               && anAspectFace->IsDisplayListSprite (aCtx);
+  #if defined (GL_ES_VERSION_2_0)
+    processIndices (aCtx);
+  #endif
+    buildVBO (aCtx, toKeepData);
+    myIsVboInit = Standard_True;
+  }
+  else if ((!myAttribs.IsNull()
+         &&  myAttribs->IsMutable())
+        || (!myIndices.IsNull()
+         &&  myIndices->IsMutable()))
+  {
+    updateVBO (aCtx);
   }
 
-  if (aCtx->IsGlGreaterEqual (2, 0))
+  Graphic3d_TypeOfShadingModel aShadingModel = Graphic3d_TOSM_UNLIT;
+  if (toDrawArray)
   {
-    switch (myPArray->type)
+    const bool hasColorAttrib = !myVboAttribs.IsNull()
+                              && myVboAttribs->HasColorAttribute();
+    const bool toHilight    = theWorkspace->ToHighlight();
+    const bool hasVertColor = hasColorAttrib && !toHilight;
+    const bool hasVertNorm  = !myVboAttribs.IsNull() && myVboAttribs->HasNormalAttribute();
+    switch (myDrawMode)
     {
-      case TelPointsArrayType:
+      case GL_POINTS:
       {
-        BindProgramWithMaterial (theWorkspace, anAspectMarker);
+        aShadingModel = aCtx->ShaderManager()->ChooseMarkerShadingModel (anAspectFace->ShadingModel(), hasVertNorm);
+        aCtx->ShaderManager()->BindMarkerProgram (aCtx->ActiveTextures(),
+                                                  aShadingModel, Graphic3d_AlphaMode_Opaque,
+                                                  hasVertColor, anAspectFace->ShaderProgramRes (aCtx));
         break;
       }
-      case TelSegmentsArrayType:
-      case TelPolylinesArrayType:
+      case GL_LINES:
+      case GL_LINE_STRIP:
       {
-        BindProgramWithMaterial (theWorkspace, anAspectLine);
+        aShadingModel = aCtx->ShaderManager()->ChooseLineShadingModel (anAspectFace->ShadingModel(), hasVertNorm);
+        aCtx->ShaderManager()->BindLineProgram (Handle(OpenGl_TextureSet)(),
+                                                anAspectFace->Aspect()->LineType(),
+                                                aShadingModel,
+                                                Graphic3d_AlphaMode_Opaque,
+                                                hasVertColor,
+                                                anAspectFace->ShaderProgramRes (aCtx));
         break;
       }
-      default: // polygonal array
+      default:
       {
-        BindProgramWithMaterial (theWorkspace, anAspectFace);
+        aShadingModel = aCtx->ShaderManager()->ChooseFaceShadingModel (anAspectFace->ShadingModel(), hasVertNorm);
+        aCtx->ShaderManager()->BindFaceProgram (aCtx->ActiveTextures(),
+                                                aShadingModel,
+                                                aCtx->ShaderManager()->MaterialState().HasAlphaCutoff() ? Graphic3d_AlphaMode_Mask : Graphic3d_AlphaMode_Opaque,
+                                                toDrawInteriorEdges == 1 ? anAspectFace->Aspect()->InteriorStyle() : Aspect_IS_SOLID,
+                                                hasVertColor,
+                                                toEnableEnvMap,
+                                                toDrawInteriorEdges == 1,
+                                                anAspectFace->ShaderProgramRes (aCtx));
+        if (toDrawInteriorEdges == 1)
+        {
+          aCtx->ShaderManager()->PushInteriorState (aCtx->ActiveProgram(), anAspectFace->Aspect());
+        }
         break;
       }
     }
+
+  #if !defined(GL_ES_VERSION_2_0)
+    // manage FFP lighting
+    if (aCtx->ActiveProgram().IsNull()
+     && aCtx->core11 != NULL)
+    {
+      if (aShadingModel == Graphic3d_TOSM_UNLIT)
+      {
+        glDisable (GL_LIGHTING);
+      }
+      else
+      {
+        glEnable (GL_LIGHTING);
+      }
+    }
+  #endif
+
+    if (!aCtx->ActiveTextures().IsNull()
+     && !aCtx->ActiveTextures()->IsEmpty()
+     && !aCtx->ActiveTextures()->First().IsNull()
+     && myDrawMode != GL_POINTS) // transformation is not supported within point sprites
+    {
+      aCtx->SetTextureMatrix (aCtx->ActiveTextures()->First()->Sampler()->Parameters());
+    }
+    aCtx->SetSampleAlphaToCoverage (aCtx->ShaderManager()->MaterialState().HasAlphaCutoff());
+
+    const Graphic3d_Vec4* aFaceColors = !myBounds.IsNull() && !toHilight && anAspectFace->Aspect()->InteriorStyle() != Aspect_IS_HIDDENLINE
+                                      ?  myBounds->Colors
+                                      :  NULL;
+    const OpenGl_Vec4& anInteriorColor = theWorkspace->InteriorColor();
+    aCtx->SetColor4fv (anInteriorColor);
+    if (!myIsFillType)
+    {
+      if (myDrawMode == GL_LINES
+       || myDrawMode == GL_LINE_STRIP)
+      {
+        aCtx->SetTypeOfLine (anAspectFace->Aspect()->LineType());
+        aCtx->SetLineWidth  (anAspectFace->Aspect()->LineWidth());
+      }
+
+      drawArray (theWorkspace, aFaceColors, hasColorAttrib);
+      return;
+    }
+
+    drawArray (theWorkspace, aFaceColors, hasColorAttrib);
+
+    // draw outline - only closed triangulation with defined vertex normals can be drawn in this way
+    if (anAspectFace->Aspect()->ToDrawSilhouette()
+     && aCtx->ToCullBackFaces()
+     && aCtx->ShaderManager()->BindOutlineProgram())
+    {
+      const Graphic3d_Vec2i aViewSize (aCtx->Viewport()[2], aCtx->Viewport()[3]);
+      const Standard_Integer aMin = aViewSize.minComp();
+      const GLfloat anEdgeWidth  = (GLfloat )anAspectFace->Aspect()->EdgeWidth() * aCtx->LineWidthScale() / (GLfloat )aMin;
+      const GLfloat anOrthoScale = theWorkspace->View()->Camera()->IsOrthographic() ? (GLfloat )theWorkspace->View()->Camera()->Scale() : -1.0f;
+
+      const Handle(OpenGl_ShaderProgram)& anOutlineProgram = aCtx->ActiveProgram();
+      anOutlineProgram->SetUniform (aCtx, anOutlineProgram->GetStateLocation (OpenGl_OCCT_SILHOUETTE_THICKNESS), anEdgeWidth);
+      anOutlineProgram->SetUniform (aCtx, anOutlineProgram->GetStateLocation (OpenGl_OCCT_ORTHO_SCALE),          anOrthoScale);
+      aCtx->SetColor4fv (anAspectFace->Aspect()->EdgeColorRGBA());
+
+      aCtx->core11fwd->glCullFace (GL_FRONT);
+      drawArray (theWorkspace, NULL, false);
+
+      aCtx->core11fwd->glCullFace (GL_BACK);
+    }
+  }
+
+#if !defined(GL_ES_VERSION_2_0)
+  // draw triangulation edges using Polygon Mode
+  if (toDrawInteriorEdges == 2)
+  {
+    if (anAspectFace->Aspect()->InteriorStyle() == Aspect_IS_HOLLOW
+     && anAspectFace->Aspect()->EdgeLineType()  == Aspect_TOL_SOLID)
+    {
+      aCtx->SetPolygonMode (GL_FILL);
+    }
+    else
+    {
+      drawEdges (theWorkspace);
+    }
+  }
+#endif
+}
+
+// =======================================================================
+// function : setDrawMode
+// purpose  :
+// =======================================================================
+void OpenGl_PrimitiveArray::setDrawMode (const Graphic3d_TypeOfPrimitiveArray theType)
+{
+  if (myAttribs.IsNull())
+  {
+    myDrawMode = DRAW_MODE_NONE;
+    myIsFillType = false;
+    return;
+  }
+
+  switch (theType)
+  {
+    case Graphic3d_TOPA_POINTS:
+      myDrawMode   = GL_POINTS;
+      myIsFillType = false;
+      break;
+    case Graphic3d_TOPA_SEGMENTS:
+      myDrawMode   = GL_LINES;
+      myIsFillType = false;
+      break;
+    case Graphic3d_TOPA_POLYLINES:
+      myDrawMode   = GL_LINE_STRIP;
+      myIsFillType = false;
+      break;
+    case Graphic3d_TOPA_TRIANGLES:
+      myDrawMode   = GL_TRIANGLES;
+      myIsFillType = true;
+      break;
+    case Graphic3d_TOPA_TRIANGLESTRIPS:
+      myDrawMode   = GL_TRIANGLE_STRIP;
+      myIsFillType = true;
+      break;
+    case Graphic3d_TOPA_TRIANGLEFANS:
+      myDrawMode   = GL_TRIANGLE_FAN;
+      myIsFillType = true;
+      break;
+    //
+    case Graphic3d_TOPA_LINES_ADJACENCY:
+      myDrawMode = GL_LINES_ADJACENCY;
+      myIsFillType = false;
+      break;
+    case Graphic3d_TOPA_LINE_STRIP_ADJACENCY:
+      myDrawMode   = GL_LINE_STRIP_ADJACENCY;
+      myIsFillType = false;
+      break;
+    case Graphic3d_TOPA_TRIANGLES_ADJACENCY:
+      myDrawMode   = GL_TRIANGLES_ADJACENCY;
+      myIsFillType = true;
+      break;
+    case Graphic3d_TOPA_TRIANGLE_STRIP_ADJACENCY:
+      myDrawMode   = GL_TRIANGLE_STRIP_ADJACENCY;
+      myIsFillType = true;
+      break;
+    //
+  #if !defined(GL_ES_VERSION_2_0)
+    case Graphic3d_TOPA_QUADRANGLES:
+      myDrawMode   = GL_QUADS;
+      myIsFillType = true;
+      break;
+    case Graphic3d_TOPA_QUADRANGLESTRIPS:
+      myDrawMode   = GL_QUAD_STRIP;
+      myIsFillType = true;
+      break;
+    case Graphic3d_TOPA_POLYGONS:
+      myDrawMode   = GL_POLYGON;
+      myIsFillType = true;
+      break;
+  #else
+    case Graphic3d_TOPA_QUADRANGLES:
+    case Graphic3d_TOPA_QUADRANGLESTRIPS:
+    case Graphic3d_TOPA_POLYGONS:
+  #endif
+    case Graphic3d_TOPA_UNDEFINED:
+      myDrawMode   = DRAW_MODE_NONE;
+      myIsFillType = false;
+      break;
   }
+}
+
+// =======================================================================
+// function : processIndices
+// purpose  :
+// =======================================================================
+Standard_Boolean OpenGl_PrimitiveArray::processIndices (const Handle(OpenGl_Context)& theContext) const
+{
+  if (myIndices.IsNull()
+   || myAttribs.IsNull()
+   || theContext->hasUintIndex)
+  {
+    return Standard_True;
+  }
+
+  if (myAttribs->NbElements > std::numeric_limits<GLushort>::max())
+  {
+    Handle(Graphic3d_Buffer) anAttribs = new Graphic3d_Buffer (new NCollection_AlignedAllocator (16));
+    if (!anAttribs->Init (myIndices->NbElements, myAttribs->AttributesArray(), myAttribs->NbAttributes))
+    {
+      return Standard_False; // failed to initialize attribute array
+    }
+
+    for (Standard_Integer anIdxIdx = 0; anIdxIdx < myIndices->NbElements; ++anIdxIdx)
+    {
+      const Standard_Integer anIndex = myIndices->Index (anIdxIdx);
+      memcpy (anAttribs->ChangeData() + myAttribs->Stride * anIdxIdx,
+              myAttribs->Data()       + myAttribs->Stride * anIndex,
+              myAttribs->Stride);
+    }
+
+    myIndices.Nullify();
+    myAttribs = anAttribs;
+  }
+
+  return Standard_True;
+}
+
+// =======================================================================
+// function : InitBuffers
+// purpose  :
+// =======================================================================
+void OpenGl_PrimitiveArray::InitBuffers (const Handle(OpenGl_Context)&        theContext,
+                                         const Graphic3d_TypeOfPrimitiveArray theType,
+                                         const Handle(Graphic3d_IndexBuffer)& theIndices,
+                                         const Handle(Graphic3d_Buffer)&      theAttribs,
+                                         const Handle(Graphic3d_BoundBuffer)& theBounds)
+{
+  // Release old graphic resources
+  Release (theContext.get());
+
+  myIndices = theIndices;
+  myAttribs = theAttribs;
+  myBounds = theBounds;
+#if defined(GL_ES_VERSION_2_0)
+  processIndices (theContext);
+#endif
 
-  DrawArray (aFrontLightingModel,
-             anAspectFace->InteriorStyle(),
-             anAspectFace->Edge(),
-             anInteriorColor,
-             aLineColor,
-             anEdgeColor,
-             &anAspectFace->IntFront(),
-             theWorkspace);
+  setDrawMode (theType);
 }