Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:21

0001 // Created by: Kirill GAVRILOV
0002 // Copyright (c) 2013-2014 OPEN CASCADE SAS
0003 //
0004 // This file is part of Open CASCADE Technology software library.
0005 //
0006 // This library is free software; you can redistribute it and/or modify it under
0007 // the terms of the GNU Lesser General Public License version 2.1 as published
0008 // by the Free Software Foundation, with special exception defined in the file
0009 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0010 // distribution for complete text of the license and disclaimer of any warranty.
0011 //
0012 // Alternatively, this file may be used under the terms of Open CASCADE
0013 // commercial license or contractual agreement.
0014 
0015 #ifndef _OpenGl_Buffer_H__
0016 #define _OpenGl_Buffer_H__
0017 
0018 #include <OpenGl_Resource.hxx>
0019 #include <TCollection_AsciiString.hxx>
0020 
0021 //! Buffer Object - is a general storage object for arbitrary data (see sub-classes).
0022 class OpenGl_Buffer : public OpenGl_Resource
0023 {
0024   DEFINE_STANDARD_RTTIEXT(OpenGl_Buffer, OpenGl_Resource)
0025 public:
0026 
0027   //! Helpful constants
0028   static const unsigned int NO_BUFFER = 0;
0029 
0030   //! Format VBO target enumeration value.
0031   Standard_EXPORT static TCollection_AsciiString FormatTarget (unsigned int theTarget);
0032 
0033 public:
0034 
0035   //! Create uninitialized buffer.
0036   Standard_EXPORT OpenGl_Buffer();
0037 
0038   //! Destroy object.
0039   Standard_EXPORT virtual ~OpenGl_Buffer();
0040 
0041   //! Return buffer target.
0042   virtual unsigned int GetTarget() const = 0;
0043 
0044   //! Return TRUE if this is a virtual (for backward compatibility) VBO object.
0045   virtual bool IsVirtual() const { return false; }
0046 
0047   //! @return true if current object was initialized
0048   bool IsValid() const { return myBufferId != NO_BUFFER; }
0049 
0050   //! @return the number of components per generic vertex attribute.
0051   unsigned int GetComponentsNb() const { return myComponentsNb; }
0052 
0053   //! @return number of vertex attributes / number of vertices specified within ::Init()
0054   Standard_Integer GetElemsNb() const { return myElemsNb; }
0055 
0056   //! Overrides the number of vertex attributes / number of vertexes.
0057   //! It is up to user specifying this number correct (e.g. below initial value)!
0058   void SetElemsNb (Standard_Integer theNbElems) { myElemsNb = theNbElems; }
0059 
0060   //! @return data type of each component in the array.
0061   unsigned int GetDataType() const { return myDataType; }
0062 
0063   //! @return offset to data, NULL by default
0064   Standard_Byte* GetDataOffset() const { return myOffset; }
0065 
0066   //! Creates buffer object name (id) if not yet generated.
0067   //! Data should be initialized by another method.
0068   Standard_EXPORT virtual bool Create (const Handle(OpenGl_Context)& theGlCtx);
0069 
0070   //! Destroy object - will release GPU memory if any.
0071   Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE;
0072 
0073   //! Bind this buffer object.
0074   Standard_EXPORT virtual void Bind (const Handle(OpenGl_Context)& theGlCtx) const;
0075 
0076   //! Unbind this buffer object.
0077   Standard_EXPORT virtual void Unbind (const Handle(OpenGl_Context)& theGlCtx) const;
0078 
0079   //! Notice that buffer object will be unbound after this call.
0080   //! @param theComponentsNb [in] specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
0081   //! @param theElemsNb      [in] elements count;
0082   //! @param theData         [in] pointer to float data (vertices/normals etc.).
0083   Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx,
0084                              const unsigned int     theComponentsNb,
0085                              const Standard_Integer theElemsNb,
0086                              const float*   theData);
0087 
0088   //! Notice that buffer object will be unbound after this call.
0089   //! @param theComponentsNb [in] specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
0090   //! @param theElemsNb      [in] elements count;
0091   //! @param theData         [in] pointer to unsigned int data (indices etc.).
0092   Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx,
0093                              const unsigned int     theComponentsNb,
0094                              const Standard_Integer theElemsNb,
0095                              const unsigned int* theData);
0096 
0097   //! Notice that buffer object will be unbound after this call.
0098   //! @param theComponentsNb [in] specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
0099   //! @param theElemsNb      [in] elements count;
0100   //! @param theData         [in] pointer to unsigned short data (indices etc.).
0101   Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx,
0102                              const unsigned int     theComponentsNb,
0103                              const Standard_Integer theElemsNb,
0104                              const unsigned short*  theData);
0105 
0106   //! Notice that buffer object will be unbound after this call.
0107   //! @param theComponentsNb [in] specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
0108   //! @param theElemsNb      [in] elements count;
0109   //! @param theData         [in] pointer to Standard_Byte data (indices/colors etc.).
0110   Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theGlCtx,
0111                              const unsigned int     theComponentsNb,
0112                              const Standard_Integer theElemsNb,
0113                              const Standard_Byte*   theData);
0114 
0115   //! Notice that buffer object will be unbound after this call.
0116   //! Function replaces portion of data within this buffer object using glBufferSubData().
0117   //! The buffer object should be initialized before call.
0118   //! @param theElemFrom [in] element id from which replace buffer data (>=0);
0119   //! @param theElemsNb  [in] elements count (theElemFrom + theElemsNb <= GetElemsNb());
0120   //! @param theData     [in] pointer to float data.
0121   Standard_EXPORT bool SubData (const Handle(OpenGl_Context)& theGlCtx,
0122                                 const Standard_Integer theElemFrom,
0123                                 const Standard_Integer theElemsNb,
0124                                 const float* theData);
0125 
0126   //! Read back buffer sub-range.
0127   //! Notice that buffer object will be unbound after this call.
0128   //! Function reads portion of data from this buffer object using glGetBufferSubData().
0129   //! @param theElemFrom [in] element id from which replace buffer data (>=0);
0130   //! @param theElemsNb  [in] elements count (theElemFrom + theElemsNb <= GetElemsNb());
0131   //! @param theData    [out] destination pointer to float data.
0132   Standard_EXPORT bool GetSubData (const Handle(OpenGl_Context)& theGlCtx,
0133                                    const Standard_Integer theElemFrom,
0134                                    const Standard_Integer theElemsNb,
0135                                    float* theData);
0136 
0137   //! Notice that buffer object will be unbound after this call.
0138   //! Function replaces portion of data within this buffer object using glBufferSubData().
0139   //! The buffer object should be initialized before call.
0140   //! @param theElemFrom [in] element id from which replace buffer data (>=0);
0141   //! @param theElemsNb  [in] elements count (theElemFrom + theElemsNb <= GetElemsNb());
0142   //! @param theData     [in] pointer to unsigned int data.
0143   Standard_EXPORT bool SubData (const Handle(OpenGl_Context)& theGlCtx,
0144                                 const Standard_Integer theElemFrom,
0145                                 const Standard_Integer theElemsNb,
0146                                 const unsigned int* theData);
0147 
0148   //! Read back buffer sub-range.
0149   //! Notice that buffer object will be unbound after this call.
0150   //! Function reads portion of data from this buffer object using glGetBufferSubData().
0151   //! @param theElemFrom [in] element id from which replace buffer data (>=0);
0152   //! @param theElemsNb  [in] elements count (theElemFrom + theElemsNb <= GetElemsNb());
0153   //! @param theData    [out] destination pointer to unsigned int data.
0154   Standard_EXPORT bool GetSubData (const Handle(OpenGl_Context)& theGlCtx,
0155                                    const Standard_Integer theElemFrom,
0156                                    const Standard_Integer theElemsNb,
0157                                    unsigned int* theData);
0158 
0159   //! Notice that buffer object will be unbound after this call.
0160   //! Function replaces portion of data within this buffer object using glBufferSubData().
0161   //! The buffer object should be initialized before call.
0162   //! @param theElemFrom [in] element id from which replace buffer data (>=0);
0163   //! @param theElemsNb  [in] elements count (theElemFrom + theElemsNb <= GetElemsNb());
0164   //! @param theData     [in] pointer to unsigned short data.
0165   Standard_EXPORT bool SubData (const Handle(OpenGl_Context)& theGlCtx,
0166                                 const Standard_Integer theElemFrom,
0167                                 const Standard_Integer theElemsNb,
0168                                 const unsigned short*  theData);
0169 
0170   //! Read back buffer sub-range.
0171   //! Notice that buffer object will be unbound after this call.
0172   //! Function reads portion of data from this buffer object using glGetBufferSubData().
0173   //! @param theElemFrom [in] element id from which replace buffer data (>=0);
0174   //! @param theElemsNb  [in] elements count (theElemFrom + theElemsNb <= GetElemsNb());
0175   //! @param theData    [out] destination pointer to unsigned short data.
0176   Standard_EXPORT bool GetSubData (const Handle(OpenGl_Context)& theGlCtx,
0177                                    const Standard_Integer theElemFrom,
0178                                    const Standard_Integer theElemsNb,
0179                                    unsigned short* theData);
0180 
0181   //! Notice that buffer object will be unbound after this call.
0182   //! Function replaces portion of data within this buffer object using glBufferSubData().
0183   //! The buffer object should be initialized before call.
0184   //! @param theElemFrom [in] element id from which replace buffer data (>=0);
0185   //! @param theElemsNb  [in] elements count (theElemFrom + theElemsNb <= GetElemsNb());
0186   //! @param theData     [in] pointer to Standard_Byte data.
0187   Standard_EXPORT bool SubData (const Handle(OpenGl_Context)& theGlCtx,
0188                                 const Standard_Integer theElemFrom,
0189                                 const Standard_Integer theElemsNb,
0190                                 const Standard_Byte* theData);
0191 
0192   //! Read back buffer sub-range.
0193   //! Notice that buffer object will be unbound after this call.
0194   //! Function reads portion of data from this buffer object using glGetBufferSubData().
0195   //! @param theElemFrom [in] element id from which replace buffer data (>=0);
0196   //! @param theElemsNb  [in] elements count (theElemFrom + theElemsNb <= GetElemsNb());
0197   //! @param theData    [out] destination pointer to Standard_Byte data.
0198   Standard_EXPORT bool GetSubData (const Handle(OpenGl_Context)& theGlCtx,
0199                                    const Standard_Integer theElemFrom,
0200                                    const Standard_Integer theElemsNb,
0201                                    Standard_Byte* theData);
0202 
0203 public: //! @name advanced methods
0204 
0205   //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules.
0206   virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE
0207   {
0208     return IsValid()
0209          ? sizeOfGlType (myDataType) * myComponentsNb * myElemsNb
0210          : 0;
0211   }
0212 
0213   //! @return size of specified GL type
0214   Standard_EXPORT static size_t sizeOfGlType (unsigned int theType);
0215 
0216   //! Initialize buffer with new data.
0217   Standard_EXPORT virtual bool init (const Handle(OpenGl_Context)& theGlCtx,
0218                                      const unsigned int     theComponentsNb,
0219                                      const Standard_Integer theElemsNb,
0220                                      const void*            theData,
0221                                      const unsigned int     theDataType,
0222                                      const Standard_Integer theStride);
0223 
0224   //! Initialize buffer with new data.
0225   bool init (const Handle(OpenGl_Context)& theGlCtx,
0226              const unsigned int     theComponentsNb,
0227              const Standard_Integer theElemsNb,
0228              const void*            theData,
0229              const unsigned int     theDataType)
0230   {
0231     return init (theGlCtx, theComponentsNb, theElemsNb, theData, theDataType,
0232                  Standard_Integer(theComponentsNb) * Standard_Integer(sizeOfGlType (theDataType)));
0233   }
0234 
0235   //! Update part of the buffer with new data.
0236   Standard_EXPORT virtual bool subData (const Handle(OpenGl_Context)& theGlCtx,
0237                                         const Standard_Integer theElemFrom,
0238                                         const Standard_Integer theElemsNb,
0239                                         const void*            theData,
0240                                         const unsigned int     theDataType);
0241 
0242   //! Read back buffer sub-range.
0243   Standard_EXPORT virtual bool getSubData (const Handle(OpenGl_Context)& theGlCtx,
0244                                            const Standard_Integer theElemFrom,
0245                                            const Standard_Integer theElemsNb,
0246                                            void*                  theData,
0247                                            const unsigned int     theDataType);
0248 
0249 public:
0250 
0251   //! Dumps the content of me into the stream
0252   Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE;
0253 
0254 protected:
0255 
0256   //! Binds a buffer object to an indexed buffer target.
0257   //! Wrapper for glBindBufferBase().
0258   //! @param theGlCtx [in] active OpenGL context
0259   //! @param theIndex [in] index to bind
0260   Standard_EXPORT void BindBufferBase (const Handle(OpenGl_Context)& theGlCtx,
0261                                        unsigned int theIndex);
0262 
0263   //! Unbinds a buffer object from an indexed buffer target.
0264   //! Wrapper for glBindBufferBase().
0265   //! @param theGlCtx [in] active OpenGL context
0266   //! @param theIndex [in] index to bind
0267   Standard_EXPORT void UnbindBufferBase (const Handle(OpenGl_Context)& theGlCtx,
0268                                          unsigned int theIndex);
0269 
0270   //! Binds a buffer object to an indexed buffer target with specified offset and size.
0271   //! Wrapper for glBindBufferRange().
0272   //! @param theGlCtx  [in] active OpenGL context
0273   //! @param theIndex  [in] index to bind (@sa GL_MAX_UNIFORM_BUFFER_BINDINGS in case of uniform buffer)
0274   //! @param theOffset [in] offset within the buffer (@sa GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT in case of uniform buffer)
0275   //! @param theSize   [in] sub-section length starting from offset
0276   Standard_EXPORT void BindBufferRange (const Handle(OpenGl_Context)& theGlCtx,
0277                                         unsigned int   theIndex,
0278                                         const intptr_t theOffset,
0279                                         const size_t   theSize);
0280 
0281 protected:
0282 
0283   Standard_Byte*   myOffset;       //!< offset to data
0284   unsigned int     myBufferId;     //!< VBO name (index)
0285   unsigned int     myComponentsNb; //!< Number of components per generic vertex attribute, must be 1, 2, 3, or 4
0286   Standard_Integer myElemsNb;      //!< Number of vertex attributes / number of vertices
0287   unsigned int     myDataType;     //!< Data type (GL_FLOAT, GL_UNSIGNED_INT, GL_UNSIGNED_BYTE etc.)
0288 
0289 };
0290 
0291 DEFINE_STANDARD_HANDLE(OpenGl_Buffer, OpenGl_Resource)
0292 
0293 #endif // _OpenGl_Buffer_H__