Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/opencascade/Graphic3d_Buffer.hxx was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright (c) 2014 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _Graphic3d_Buffer_HeaderFile
0015 #define _Graphic3d_Buffer_HeaderFile
0016 
0017 #include <Graphic3d_BufferRange.hxx>
0018 #include <Graphic3d_Vec.hxx>
0019 #include <NCollection_Array1.hxx>
0020 #include <NCollection_Buffer.hxx>
0021 #include <Standard_NotImplemented.hxx>
0022 
0023 //! Type of attribute in Vertex Buffer
0024 enum Graphic3d_TypeOfAttribute
0025 {
0026   Graphic3d_TOA_POS = 0, //!< vertex position
0027   Graphic3d_TOA_NORM,    //!< normal
0028   Graphic3d_TOA_UV,      //!< texture coordinates
0029   Graphic3d_TOA_COLOR,   //!< per-vertex color
0030   Graphic3d_TOA_CUSTOM,  //!< custom attributes
0031 };
0032 
0033 //! Type of the element in Vertex or Index Buffer
0034 enum Graphic3d_TypeOfData
0035 {
0036   Graphic3d_TOD_USHORT, //!< unsigned 16-bit integer
0037   Graphic3d_TOD_UINT,   //!< unsigned 32-bit integer
0038   Graphic3d_TOD_VEC2,   //!< 2-components float vector
0039   Graphic3d_TOD_VEC3,   //!< 3-components float vector
0040   Graphic3d_TOD_VEC4,   //!< 4-components float vector
0041   Graphic3d_TOD_VEC4UB, //!< 4-components unsigned byte vector
0042   Graphic3d_TOD_FLOAT,  //!< float value
0043 };
0044 
0045 //! Vertex attribute definition.
0046 struct Graphic3d_Attribute
0047 {
0048   // clang-format off
0049   Graphic3d_TypeOfAttribute Id;       //!< attribute identifier in vertex shader, 0 is reserved for vertex position
0050   // clang-format on
0051   Graphic3d_TypeOfData DataType; //!< vec2,vec3,vec4,vec4ub
0052 
0053   Standard_Integer Stride() const { return Stride(DataType); }
0054 
0055   //! @return size of attribute of specified data type
0056   static Standard_Integer Stride(const Graphic3d_TypeOfData theType)
0057   {
0058     switch (theType)
0059     {
0060       case Graphic3d_TOD_USHORT:
0061         return sizeof(unsigned short);
0062       case Graphic3d_TOD_UINT:
0063         return sizeof(unsigned int);
0064       case Graphic3d_TOD_VEC2:
0065         return sizeof(Graphic3d_Vec2);
0066       case Graphic3d_TOD_VEC3:
0067         return sizeof(Graphic3d_Vec3);
0068       case Graphic3d_TOD_VEC4:
0069         return sizeof(Graphic3d_Vec4);
0070       case Graphic3d_TOD_VEC4UB:
0071         return sizeof(Graphic3d_Vec4ub);
0072       case Graphic3d_TOD_FLOAT:
0073         return sizeof(float);
0074     }
0075     return 0;
0076   }
0077 };
0078 
0079 typedef NCollection_Array1<Graphic3d_Attribute> Graphic3d_Array1OfAttribute;
0080 
0081 //! Buffer of vertex attributes.
0082 class Graphic3d_Buffer : public NCollection_Buffer
0083 {
0084   DEFINE_STANDARD_RTTIEXT(Graphic3d_Buffer, NCollection_Buffer)
0085 public:
0086   //! Return default vertex data allocator.
0087   Standard_EXPORT static const Handle(NCollection_BaseAllocator)& DefaultAllocator();
0088 
0089 public:
0090   //! Empty constructor.
0091   Graphic3d_Buffer(const Handle(NCollection_BaseAllocator)& theAlloc)
0092       : NCollection_Buffer(theAlloc),
0093         Stride(0),
0094         NbElements(0),
0095         NbAttributes(0)
0096   {
0097     //
0098   }
0099 
0100   //! Return number of initially allocated elements which can fit into this buffer,
0101   //! while NbElements can be overwritten to smaller value.
0102   Standard_Integer NbMaxElements() const
0103   {
0104     return Stride != 0 ? Standard_Integer(mySize / size_t(Stride)) : 0;
0105   }
0106 
0107   //! @return array of attributes definitions
0108   const Graphic3d_Attribute* AttributesArray() const
0109   {
0110     return (Graphic3d_Attribute*)(myData + mySize);
0111   }
0112 
0113   //! @return attribute definition
0114   const Graphic3d_Attribute& Attribute(const Standard_Integer theAttribIndex) const
0115   {
0116     return AttributesArray()[theAttribIndex];
0117   }
0118 
0119   //! @return attribute definition
0120   Graphic3d_Attribute& ChangeAttribute(const Standard_Integer theAttribIndex)
0121   {
0122     return *((Graphic3d_Attribute*)(myData + mySize) + theAttribIndex);
0123   }
0124 
0125   //! Find attribute index.
0126   //! @param theAttrib attribute to find
0127   //! @return attribute index or -1 if not found
0128   Standard_Integer FindAttribute(Graphic3d_TypeOfAttribute theAttrib) const
0129   {
0130     for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
0131     {
0132       const Graphic3d_Attribute& anAttrib = Attribute(anAttribIter);
0133       if (anAttrib.Id == theAttrib)
0134       {
0135         return anAttribIter;
0136       }
0137     }
0138     return -1;
0139   }
0140 
0141   //! @name data accessors for interleaved array
0142 public:
0143   //! @return data offset to specified attribute
0144   Standard_Integer AttributeOffset(const Standard_Integer theAttribIndex) const
0145   {
0146     Standard_Integer anOffset = 0;
0147     for (Standard_Integer anAttribIter = 0; anAttribIter < theAttribIndex; ++anAttribIter)
0148     {
0149       anOffset += Graphic3d_Attribute::Stride(Attribute(anAttribIter).DataType);
0150     }
0151     return anOffset;
0152   }
0153 
0154   //! @return data for specified attribute
0155   const Standard_Byte* Data(const Standard_Integer theAttribIndex) const
0156   {
0157     return myData + AttributeOffset(theAttribIndex);
0158   }
0159 
0160   //! @return data for specified attribute
0161   Standard_Byte* ChangeData(const Standard_Integer theAttribIndex)
0162   {
0163     return myData + AttributeOffset(theAttribIndex);
0164   }
0165 
0166   //! Access specified element.
0167   inline const Standard_Byte* value(const Standard_Integer theElem) const
0168   {
0169     return myData + Stride * size_t(theElem);
0170   }
0171 
0172   //! Access specified element.
0173   inline Standard_Byte* changeValue(const Standard_Integer theElem)
0174   {
0175     return myData + Stride * size_t(theElem);
0176   }
0177 
0178   //! Access element with specified position and type.
0179   template <typename Type_t>
0180   inline const Type_t& Value(const Standard_Integer theElem) const
0181   {
0182     return *reinterpret_cast<const Type_t*>(value(theElem));
0183   }
0184 
0185   //! Access element with specified position and type.
0186   template <typename Type_t>
0187   inline Type_t& ChangeValue(const Standard_Integer theElem)
0188   {
0189     return *reinterpret_cast<Type_t*>(changeValue(theElem));
0190   }
0191 
0192   //! @name general accessors
0193 public:
0194   using NCollection_Buffer::ChangeData;
0195   using NCollection_Buffer::Data;
0196 
0197   //! Return the attribute data with stride size specific to this attribute.
0198   //! @param theAttrib       attribute to find
0199   //! @param theAttribIndex  index of found attribute
0200   //! @param theAttribStride stride in bytes between values of this attribute within returned data
0201   //! pointer
0202   Standard_Byte* ChangeAttributeData(Graphic3d_TypeOfAttribute theAttrib,
0203                                      Standard_Integer&         theAttribIndex,
0204                                      Standard_Size&            theAttribStride)
0205   {
0206     return (Standard_Byte*)AttributeData(theAttrib, theAttribIndex, theAttribStride);
0207   }
0208 
0209   //! Return the attribute data with stride size specific to this attribute.
0210   //! @param theAttrib       attribute to find
0211   //! @param theAttribIndex  index of found attribute
0212   //! @param theAttribStride stride in bytes between values of this attribute within returned data
0213   //! pointer
0214   const Standard_Byte* AttributeData(Graphic3d_TypeOfAttribute theAttrib,
0215                                      Standard_Integer&         theAttribIndex,
0216                                      Standard_Size&            theAttribStride) const
0217   {
0218     const Standard_Byte* aDataPtr = Data();
0219     if (IsInterleaved())
0220     {
0221       for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
0222       {
0223         const Graphic3d_Attribute& anAttrib       = Attribute(anAttribIter);
0224         const Standard_Size        anAttribStride = Graphic3d_Attribute::Stride(anAttrib.DataType);
0225         if (anAttrib.Id == theAttrib)
0226         {
0227           theAttribIndex  = anAttribIter;
0228           theAttribStride = Stride;
0229           return aDataPtr;
0230         }
0231 
0232         aDataPtr += anAttribStride;
0233       }
0234     }
0235     else
0236     {
0237       const Standard_Integer aNbMaxVerts = NbMaxElements();
0238       for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
0239       {
0240         const Graphic3d_Attribute& anAttrib       = Attribute(anAttribIter);
0241         const Standard_Size        anAttribStride = Graphic3d_Attribute::Stride(anAttrib.DataType);
0242         if (anAttrib.Id == theAttrib)
0243         {
0244           theAttribIndex  = anAttribIter;
0245           theAttribStride = anAttribStride;
0246           return aDataPtr;
0247         }
0248 
0249         aDataPtr += anAttribStride * aNbMaxVerts;
0250       }
0251     }
0252     return NULL;
0253   }
0254 
0255 public:
0256   //! Release buffer.
0257   void release()
0258   {
0259     Free();
0260     Stride       = 0;
0261     NbElements   = 0;
0262     NbAttributes = 0;
0263   }
0264 
0265   //! Allocates new empty array
0266   bool Init(const Standard_Integer     theNbElems,
0267             const Graphic3d_Attribute* theAttribs,
0268             const Standard_Integer     theNbAttribs)
0269   {
0270     release();
0271     Standard_Integer aStride = 0;
0272     for (Standard_Integer anAttribIter = 0; anAttribIter < theNbAttribs; ++anAttribIter)
0273     {
0274       const Graphic3d_Attribute& anAttrib = theAttribs[anAttribIter];
0275       aStride += anAttrib.Stride();
0276     }
0277     if (aStride == 0)
0278     {
0279       return false;
0280     }
0281 
0282     Stride       = aStride;
0283     NbElements   = theNbElems;
0284     NbAttributes = theNbAttribs;
0285     if (NbElements != 0)
0286     {
0287       const size_t aDataSize = size_t(Stride) * size_t(NbElements);
0288       if (!Allocate(aDataSize + sizeof(Graphic3d_Attribute) * NbAttributes))
0289       {
0290         release();
0291         return false;
0292       }
0293 
0294       mySize = aDataSize;
0295       for (Standard_Integer anAttribIter = 0; anAttribIter < theNbAttribs; ++anAttribIter)
0296       {
0297         ChangeAttribute(anAttribIter) = theAttribs[anAttribIter];
0298       }
0299     }
0300     return true;
0301   }
0302 
0303   //! Allocates new empty array
0304   bool Init(const Standard_Integer theNbElems, const Graphic3d_Array1OfAttribute& theAttribs)
0305   {
0306     return Init(theNbElems, &theAttribs.First(), theAttribs.Size());
0307   }
0308 
0309 public:
0310   //! Flag indicating that attributes in the buffer are interleaved; TRUE by default.
0311   //! Requires sub-classing for creating a non-interleaved buffer (advanced usage).
0312   virtual Standard_Boolean IsInterleaved() const { return Standard_True; }
0313 
0314   //! Return TRUE if data can be invalidated; FALSE by default.
0315   //! Requires sub-classing for creating a mutable buffer (advanced usage).
0316   virtual Standard_Boolean IsMutable() const { return Standard_False; }
0317 
0318   //! Return invalidated range; EMPTY by default.
0319   //! Requires sub-classing for creating a mutable buffer (advanced usage).
0320   virtual Graphic3d_BufferRange InvalidatedRange() const { return Graphic3d_BufferRange(); }
0321 
0322   //! Reset invalidated range.
0323   //! Requires sub-classing for creating a mutable buffer (advanced usage).
0324   virtual void Validate() {}
0325 
0326   //! Invalidate entire buffer.
0327   virtual void Invalidate() {}
0328 
0329   //! Dumps the content of me into the stream
0330   Standard_EXPORT virtual void DumpJson(Standard_OStream& theOStream,
0331                                         Standard_Integer  theDepth = -1) const Standard_OVERRIDE;
0332 
0333 public:
0334   // clang-format off
0335   Standard_Integer Stride;       //!< the distance to the attributes of the next vertex within interleaved array
0336   Standard_Integer NbElements;   //!< number of the elements (@sa NbMaxElements() specifying the number of initially allocated number of elements)
0337   // clang-format on
0338   Standard_Integer NbAttributes; //!< number of vertex attributes
0339 };
0340 
0341 DEFINE_STANDARD_HANDLE(Graphic3d_Buffer, NCollection_Buffer)
0342 
0343 #endif // _Graphic3d_Buffer_HeaderFile