0032121: Draw Harness, ViewerTest - implement -reset option for vlight command
[occt.git] / src / OpenGl / OpenGl_BufferCompatT.hxx
1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifndef _OpenGl_BufferCompatT_HeaderFile
16 #define _OpenGl_BufferCompatT_HeaderFile
17
18 #include <NCollection_Buffer.hxx>
19 #include <OpenGl_Buffer.hxx>
20
21 //! Compatibility layer for old OpenGL without VBO.
22 //! Make sure to pass pointer from GetDataOffset() instead of NULL.
23 //! Method GetDataOffset() returns pointer to real data in this class
24 //! (while base class OpenGl_VertexBuffer always return NULL).
25 //!
26 //! Methods Bind()/Unbind() do nothing (do not affect OpenGL state)
27 //! and ::GetTarget() is never used.
28 //! For this reason there is no analog for OpenGl_IndexBuffer.
29 //! Just pass GetDataOffset() to glDrawElements() directly as last argument.
30 //!
31 //! Class overrides methods init() and subData() to copy data into own memory buffer.
32 //! Extra method initLink() might be used to pass existing buffer through handle without copying the data.
33 //!
34 //! Method Create() creates dummy identifier for this object which should NOT be passed to OpenGL functions.
35 template<class BaseBufferT>
36 class OpenGl_BufferCompatT : public BaseBufferT
37 {
38
39 public:
40
41   //! Create uninitialized VBO.
42   OpenGl_BufferCompatT()
43   {
44     //
45   }
46
47   //! Destroy object.
48   virtual ~OpenGl_BufferCompatT()
49   {
50     Release (NULL);
51   }
52
53   //! Return TRUE.
54   virtual bool IsVirtual() const Standard_OVERRIDE { return true; }
55
56   //! Creates VBO name (id) if not yet generated.
57   //! Data should be initialized by another method.
58   inline bool Create (const Handle(OpenGl_Context)& theGlCtx) Standard_OVERRIDE;
59
60   //! Destroy object - will release memory if any.
61   inline virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE;
62
63   //! Bind this VBO.
64   virtual void Bind (const Handle(OpenGl_Context)& ) const Standard_OVERRIDE
65   {
66     //
67   }
68
69   //! Unbind this VBO.
70   virtual void Unbind (const Handle(OpenGl_Context)& ) const Standard_OVERRIDE
71   {
72     //
73   }
74
75 public: //! @name advanced methods
76
77   //! Initialize buffer with existing data.
78   //! Data will NOT be copied by this method!
79   inline bool initLink (const Handle(NCollection_Buffer)& theData,
80                         const unsigned int     theComponentsNb,
81                         const Standard_Integer theElemsNb,
82                         const unsigned int     theDataType);
83
84   //! Initialize buffer with new data (data will be copied).
85   inline virtual bool init (const Handle(OpenGl_Context)& theGlCtx,
86                             const unsigned int     theComponentsNb,
87                             const Standard_Integer theElemsNb,
88                             const void*            theData,
89                             const unsigned int     theDataType,
90                             const Standard_Integer theStride) Standard_OVERRIDE;
91
92   //! Update part of the buffer with new data.
93   inline virtual bool subData (const Handle(OpenGl_Context)& theGlCtx,
94                                const Standard_Integer theElemFrom,
95                                const Standard_Integer theElemsNb,
96                                const void*        theData,
97                                const unsigned int theDataType) Standard_OVERRIDE;
98
99   //! Read back buffer sub-range.
100   inline virtual bool getSubData (const Handle(OpenGl_Context)& theGlCtx,
101                                   const Standard_Integer theElemFrom,
102                                   const Standard_Integer theElemsNb,
103                                   void* theData,
104                                   const unsigned int theDataType) Standard_OVERRIDE;
105
106 protected:
107
108   Handle(NCollection_Buffer) myData; //!< buffer data
109
110 };
111
112 // =======================================================================
113 // function : Create
114 // purpose  :
115 // =======================================================================
116 template<class BaseBufferT>
117 bool OpenGl_BufferCompatT<BaseBufferT>::Create (const Handle(OpenGl_Context)& )
118 {
119   if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
120   {
121     BaseBufferT::myBufferId = (unsigned int )-1; // dummy identifier...
122     myData = new NCollection_Buffer (Graphic3d_Buffer::DefaultAllocator());
123   }
124   return BaseBufferT::myBufferId != OpenGl_Buffer::NO_BUFFER;
125 }
126
127 // =======================================================================
128 // function : Release
129 // purpose  :
130 // =======================================================================
131 template<class BaseBufferT>
132 void OpenGl_BufferCompatT<BaseBufferT>::Release (OpenGl_Context* )
133 {
134   if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
135   {
136     return;
137   }
138
139   BaseBufferT::myOffset   = NULL;
140   BaseBufferT::myBufferId = OpenGl_Buffer::NO_BUFFER;
141   myData.Nullify();
142 }
143
144 // =======================================================================
145 // function : initLink
146 // purpose  :
147 // =======================================================================
148 template<class BaseBufferT>
149 bool OpenGl_BufferCompatT<BaseBufferT>::initLink (const Handle(NCollection_Buffer)& theData,
150                                                   const unsigned int     theComponentsNb,
151                                                   const Standard_Integer theElemsNb,
152                                                   const unsigned int     theDataType)
153 {
154   if (theData.IsNull())
155   {
156     BaseBufferT::myOffset = NULL;
157     return false;
158   }
159
160   if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
161   {
162     BaseBufferT::myBufferId = (unsigned int )-1; // dummy identifier...
163   }
164   myData = theData;
165   BaseBufferT::myDataType     = theDataType;
166   BaseBufferT::myComponentsNb = theComponentsNb;
167   BaseBufferT::myElemsNb      = theElemsNb;
168   BaseBufferT::myOffset       = myData->ChangeData();
169   return true;
170 }
171
172 // =======================================================================
173 // function : init
174 // purpose  :
175 // =======================================================================
176 template<class BaseBufferT>
177 bool OpenGl_BufferCompatT<BaseBufferT>::init (const Handle(OpenGl_Context)& theCtx,
178                                               const unsigned int     theComponentsNb,
179                                               const Standard_Integer theElemsNb,
180                                               const void*            theData,
181                                               const unsigned int     theDataType,
182                                               const Standard_Integer theStride)
183 {
184   if (!Create (theCtx))
185   {
186     BaseBufferT::myOffset = NULL;
187     return false;
188   }
189
190   BaseBufferT::myDataType     = theDataType;
191   BaseBufferT::myComponentsNb = theComponentsNb;
192   BaseBufferT::myElemsNb      = theElemsNb;
193
194   const size_t aNbBytes = size_t(BaseBufferT::myElemsNb) * theStride;
195   if (!myData->Allocate (aNbBytes))
196   {
197     BaseBufferT::myOffset = NULL;
198     return false;
199   }
200
201   BaseBufferT::myOffset = myData->ChangeData();
202   if (theData != NULL)
203   {
204     memcpy (myData->ChangeData(), theData, aNbBytes);
205   }
206   return true;
207 }
208
209 // =======================================================================
210 // function : subData
211 // purpose  :
212 // =======================================================================
213 template<class BaseBufferT>
214 bool OpenGl_BufferCompatT<BaseBufferT>::subData (const Handle(OpenGl_Context)& ,
215                                                  const Standard_Integer theElemFrom,
216                                                  const Standard_Integer theElemsNb,
217                                                  const void*            theData,
218                                                  const unsigned int     theDataType)
219 {
220   if (!BaseBufferT::IsValid() || BaseBufferT::myDataType != theDataType
221     || theElemFrom < 0 || ((theElemFrom + theElemsNb) > BaseBufferT::myElemsNb))
222   {
223     return false;
224   }
225   else if (theData == NULL)
226   {
227     return true;
228   }
229
230   const size_t aDataSize = BaseBufferT::sizeOfGlType (theDataType);
231   const size_t anOffset  = size_t(theElemFrom) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
232   const size_t aNbBytes  = size_t(theElemsNb)  * size_t(BaseBufferT::myComponentsNb) * aDataSize;
233   memcpy (myData->ChangeData() + anOffset, theData, aNbBytes);
234   return true;
235 }
236
237 // =======================================================================
238 // function : getSubData
239 // purpose  :
240 // =======================================================================
241 template<class BaseBufferT>
242 bool OpenGl_BufferCompatT<BaseBufferT>::getSubData (const Handle(OpenGl_Context)& ,
243                                                     const Standard_Integer theElemFrom,
244                                                     const Standard_Integer theElemsNb,
245                                                     void* theData,
246                                                     const unsigned int theDataType)
247 {
248   if (!BaseBufferT::IsValid() || BaseBufferT::myDataType != theDataType
249    || theElemFrom < 0 || ((theElemFrom + theElemsNb) > BaseBufferT::myElemsNb)
250    || theData == NULL)
251   {
252     return false;
253   }
254
255   const size_t aDataSize = BaseBufferT::sizeOfGlType (theDataType);
256   const size_t anOffset  = size_t(theElemFrom) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
257   const size_t aNbBytes  = size_t(theElemsNb)  * size_t(BaseBufferT::myComponentsNb) * aDataSize;
258   memcpy (theData, myData->Data() + anOffset, aNbBytes);
259   return true;
260 }
261
262 #endif // _OpenGl_VertexBufferCompat_HeaderFile