Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:03:47

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   Graphic3d_TypeOfAttribute Id;       //!< attribute identifier in vertex shader, 0 is reserved for vertex position
0049   Graphic3d_TypeOfData      DataType; //!< vec2,vec3,vec4,vec4ub
0050 
0051   Standard_Integer Stride() const { return Stride (DataType); }
0052 
0053   //! @return size of attribute of specified data type
0054   static Standard_Integer Stride (const Graphic3d_TypeOfData theType)
0055   {
0056     switch (theType)
0057     {
0058       case Graphic3d_TOD_USHORT: return sizeof(unsigned short);
0059       case Graphic3d_TOD_UINT:   return sizeof(unsigned int);
0060       case Graphic3d_TOD_VEC2:   return sizeof(Graphic3d_Vec2);
0061       case Graphic3d_TOD_VEC3:   return sizeof(Graphic3d_Vec3);
0062       case Graphic3d_TOD_VEC4:   return sizeof(Graphic3d_Vec4);
0063       case Graphic3d_TOD_VEC4UB: return sizeof(Graphic3d_Vec4ub);
0064       case Graphic3d_TOD_FLOAT:  return sizeof(float);
0065     }
0066     return 0;
0067   }
0068 
0069 };
0070 
0071 typedef NCollection_Array1<Graphic3d_Attribute> Graphic3d_Array1OfAttribute;
0072 
0073 //! Buffer of vertex attributes.
0074 class Graphic3d_Buffer : public NCollection_Buffer
0075 {
0076   DEFINE_STANDARD_RTTIEXT(Graphic3d_Buffer, NCollection_Buffer)
0077 public:
0078 
0079   //! Return default vertex data allocator.
0080   Standard_EXPORT static const Handle(NCollection_BaseAllocator)& DefaultAllocator();
0081 
0082 public:
0083 
0084   //! Empty constructor.
0085   Graphic3d_Buffer (const Handle(NCollection_BaseAllocator)& theAlloc)
0086   : NCollection_Buffer (theAlloc),
0087     Stride       (0),
0088     NbElements   (0),
0089     NbAttributes (0)
0090   {
0091     //
0092   }
0093 
0094   //! Return number of initially allocated elements which can fit into this buffer,
0095   //! while NbElements can be overwritten to smaller value.
0096   Standard_Integer NbMaxElements() const { return Stride != 0 ? Standard_Integer(mySize / size_t(Stride)) : 0; }
0097 
0098   //! @return array of attributes definitions
0099   const Graphic3d_Attribute* AttributesArray() const
0100   {
0101     return (Graphic3d_Attribute* )(myData + mySize);
0102   }
0103 
0104   //! @return attribute definition
0105   const Graphic3d_Attribute& Attribute (const Standard_Integer theAttribIndex) const
0106   {
0107     return AttributesArray()[theAttribIndex];
0108   }
0109 
0110   //! @return attribute definition
0111   Graphic3d_Attribute& ChangeAttribute (const Standard_Integer theAttribIndex)
0112   {
0113     return *((Graphic3d_Attribute* )(myData + mySize) + theAttribIndex);
0114   }
0115 
0116   //! Find attribute index.
0117   //! @param theAttrib attribute to find
0118   //! @return attribute index or -1 if not found
0119   Standard_Integer FindAttribute (Graphic3d_TypeOfAttribute theAttrib) const
0120   {
0121     for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
0122     {
0123       const Graphic3d_Attribute& anAttrib = Attribute (anAttribIter);
0124       if (anAttrib.Id == theAttrib)
0125       {
0126         return anAttribIter;
0127       }
0128     }
0129     return -1;
0130   }
0131 
0132 //! @name data accessors for interleaved array
0133 public:
0134 
0135   //! @return data offset to specified attribute
0136   Standard_Integer AttributeOffset (const Standard_Integer theAttribIndex) const
0137   {
0138     Standard_Integer anOffset = 0;
0139     for (Standard_Integer anAttribIter = 0; anAttribIter < theAttribIndex; ++anAttribIter)
0140     {
0141       anOffset += Graphic3d_Attribute::Stride (Attribute (anAttribIter).DataType);
0142     }
0143     return anOffset;
0144   }
0145 
0146   //! @return data for specified attribute
0147   const Standard_Byte* Data (const Standard_Integer theAttribIndex) const
0148   {
0149     return myData + AttributeOffset (theAttribIndex);
0150   }
0151 
0152   //! @return data for specified attribute
0153   Standard_Byte* ChangeData (const Standard_Integer theAttribIndex)
0154   {
0155     return myData + AttributeOffset (theAttribIndex);
0156   }
0157 
0158   //! Access specified element.
0159   inline const Standard_Byte* value (const Standard_Integer theElem) const
0160   {
0161     return myData + Stride * size_t(theElem);
0162   }
0163 
0164   //! Access specified element.
0165   inline Standard_Byte* changeValue (const Standard_Integer theElem)
0166   {
0167     return myData + Stride * size_t(theElem);
0168   }
0169 
0170   //! Access element with specified position and type.
0171   template <typename Type_t>
0172   inline const Type_t& Value (const Standard_Integer theElem) const
0173   {
0174     return *reinterpret_cast<const Type_t*>(value (theElem));
0175   }
0176 
0177   //! Access element with specified position and type.
0178   template <typename Type_t>
0179   inline Type_t& ChangeValue (const Standard_Integer theElem)
0180   {
0181     return *reinterpret_cast<Type_t* >(changeValue (theElem));
0182   }
0183 
0184 //! @name general accessors
0185 public:
0186 
0187   using NCollection_Buffer::Data;
0188   using NCollection_Buffer::ChangeData;
0189 
0190   //! Return the attribute data with stride size specific to this attribute.
0191   //! @param theAttrib       attribute to find
0192   //! @param theAttribIndex  index of found attribute
0193   //! @param theAttribStride stride in bytes between values of this attribute within returned data pointer
0194   Standard_Byte* ChangeAttributeData (Graphic3d_TypeOfAttribute theAttrib,
0195                                       Standard_Integer& theAttribIndex,
0196                                       Standard_Size& theAttribStride)
0197   {
0198     return (Standard_Byte* )AttributeData (theAttrib, theAttribIndex, theAttribStride);
0199   }
0200 
0201   //! Return the attribute data with stride size specific to this attribute.
0202   //! @param theAttrib       attribute to find
0203   //! @param theAttribIndex  index of found attribute
0204   //! @param theAttribStride stride in bytes between values of this attribute within returned data pointer
0205   const Standard_Byte* AttributeData (Graphic3d_TypeOfAttribute theAttrib,
0206                                       Standard_Integer& theAttribIndex,
0207                                       Standard_Size& theAttribStride) const
0208   {
0209     const Standard_Byte* aDataPtr = Data();
0210     if (IsInterleaved())
0211     {
0212       for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
0213       {
0214         const Graphic3d_Attribute& anAttrib = Attribute (anAttribIter);
0215         const Standard_Size anAttribStride = Graphic3d_Attribute::Stride (anAttrib.DataType);
0216         if (anAttrib.Id == theAttrib)
0217         {
0218           theAttribIndex  = anAttribIter;
0219           theAttribStride = Stride;
0220           return aDataPtr;
0221         }
0222 
0223         aDataPtr += anAttribStride;
0224       }
0225     }
0226     else
0227     {
0228       const Standard_Integer aNbMaxVerts = NbMaxElements();
0229       for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
0230       {
0231         const Graphic3d_Attribute& anAttrib = Attribute (anAttribIter);
0232         const Standard_Size anAttribStride = Graphic3d_Attribute::Stride (anAttrib.DataType);
0233         if (anAttrib.Id == theAttrib)
0234         {
0235           theAttribIndex  = anAttribIter;
0236           theAttribStride = anAttribStride;
0237           return aDataPtr;
0238         }
0239 
0240         aDataPtr += anAttribStride * aNbMaxVerts;
0241       }
0242     }
0243     return NULL;
0244   }
0245 
0246 public:
0247 
0248   //! Release buffer.
0249   void release()
0250   {
0251     Free();
0252     Stride       = 0;
0253     NbElements   = 0;
0254     NbAttributes = 0;
0255   }
0256 
0257   //! Allocates new empty array
0258   bool Init (const Standard_Integer     theNbElems,
0259              const Graphic3d_Attribute* theAttribs,
0260              const Standard_Integer     theNbAttribs)
0261   {
0262     release();
0263     Standard_Integer aStride = 0;
0264     for (Standard_Integer anAttribIter = 0; anAttribIter < theNbAttribs; ++anAttribIter)
0265     {
0266       const Graphic3d_Attribute& anAttrib = theAttribs[anAttribIter];
0267       aStride += anAttrib.Stride();
0268     }
0269     if (aStride == 0)
0270     {
0271       return false;
0272     }
0273 
0274     Stride       = aStride;
0275     NbElements   = theNbElems;
0276     NbAttributes = theNbAttribs;
0277     if (NbElements != 0)
0278     {
0279       const size_t aDataSize = size_t(Stride) * size_t(NbElements);
0280       if (!Allocate (aDataSize + sizeof(Graphic3d_Attribute) * NbAttributes))
0281       {
0282         release();
0283         return false;
0284       }
0285 
0286       mySize = aDataSize;
0287       for (Standard_Integer anAttribIter = 0; anAttribIter < theNbAttribs; ++anAttribIter)
0288       {
0289         ChangeAttribute (anAttribIter) = theAttribs[anAttribIter];
0290       }
0291     }
0292     return true;
0293   }
0294 
0295   //! Allocates new empty array
0296   bool Init (const Standard_Integer             theNbElems,
0297              const Graphic3d_Array1OfAttribute& theAttribs)
0298   {
0299     return Init (theNbElems, &theAttribs.First(), theAttribs.Size());
0300   }
0301 
0302 public:
0303 
0304   //! Flag indicating that attributes in the buffer are interleaved; TRUE by default.
0305   //! Requires sub-classing for creating a non-interleaved buffer (advanced usage).
0306   virtual Standard_Boolean IsInterleaved() const { return Standard_True; }
0307 
0308   //! Return TRUE if data can be invalidated; FALSE by default.
0309   //! Requires sub-classing for creating a mutable buffer (advanced usage).
0310   virtual Standard_Boolean IsMutable() const { return Standard_False; }
0311 
0312   //! Return invalidated range; EMPTY by default.
0313   //! Requires sub-classing for creating a mutable buffer (advanced usage).
0314   virtual Graphic3d_BufferRange InvalidatedRange() const { return Graphic3d_BufferRange(); }
0315 
0316   //! Reset invalidated range.
0317   //! Requires sub-classing for creating a mutable buffer (advanced usage).
0318   virtual void Validate() {}
0319 
0320   //! Invalidate entire buffer.
0321   virtual void Invalidate() {}
0322 
0323   //! Dumps the content of me into the stream
0324   Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE;
0325 
0326 public:
0327 
0328   Standard_Integer Stride;       //!< the distance to the attributes of the next vertex within interleaved array
0329   Standard_Integer NbElements;   //!< number of the elements (@sa NbMaxElements() specifying the number of initially allocated number of elements)
0330   Standard_Integer NbAttributes; //!< number of vertex attributes
0331 
0332 };
0333 
0334 DEFINE_STANDARD_HANDLE(Graphic3d_Buffer, NCollection_Buffer)
0335 
0336 #endif // _Graphic3d_Buffer_HeaderFile