Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-25 09:20:43

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