0030483: Visualization, Path Tracing - make Tile Size configurable
[occt.git] / src / OpenGl / OpenGl_VertexBuffer.hxx
1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2013-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_VertexBuffer_H__
16 #define _OpenGl_VertexBuffer_H__
17
18 #include <OpenGl_GlCore20.hxx>
19 #include <OpenGl_Resource.hxx>
20 #include <OpenGl_Context.hxx>
21 #include <OpenGl_ShaderProgram.hxx>
22
23 #include <Graphic3d_IndexBuffer.hxx>
24
25 //! Vertex Buffer Object - is a general storage object for vertex attributes (position, normal, color).
26 //! Notice that you should use OpenGl_IndexBuffer specialization for array of indices.
27 class OpenGl_VertexBuffer : public OpenGl_Resource
28 {
29
30 public:
31
32   //! Helpful constants
33   static const GLuint NO_BUFFER = 0;
34
35 public:
36
37   //! Create uninitialized VBO.
38   Standard_EXPORT OpenGl_VertexBuffer();
39
40   //! Destroy object.
41   Standard_EXPORT virtual ~OpenGl_VertexBuffer();
42
43   Standard_EXPORT virtual GLenum GetTarget() const;
44
45   //! Return TRUE if this is a virtual (for backward compatibility) VBO object.
46   virtual bool IsVirtual() const { return false; }
47
48   //! @return true if current object was initialized
49   inline bool IsValid() const
50   {
51     return myBufferId != NO_BUFFER;
52   }
53
54   //! @return the number of components per generic vertex attribute.
55   inline GLuint GetComponentsNb() const
56   {
57     return myComponentsNb;
58   }
59
60   //! @return number of vertex attributes / number of vertices specified within ::Init()
61   inline GLsizei GetElemsNb() const
62   {
63     return myElemsNb;
64   }
65
66   //! Overrides the number of vertex attributes / number of vertexes.
67   //! It is up to user specifying this number correct (e.g. below initial value)!
68   void SetElemsNb (GLsizei theNbElems) { myElemsNb = theNbElems; }
69
70   //! @return data type of each component in the array.
71   inline GLenum GetDataType() const
72   {
73     return myDataType;
74   }
75
76   //! @return offset to data, NULL by default
77   inline GLubyte* GetDataOffset() const
78   {
79     return myOffset;
80   }
81
82   //! Creates VBO name (id) if not yet generated.
83   //! Data should be initialized by another method.
84   Standard_EXPORT virtual bool Create (const Handle(OpenGl_Context)& theGlCtx);
85
86   //! Destroy object - will release GPU memory if any.
87   Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE;
88
89   //! Bind this VBO.
90   Standard_EXPORT virtual void Bind (const Handle(OpenGl_Context)& theGlCtx) const;
91
92   //! Unbind this VBO.
93   Standard_EXPORT virtual void Unbind (const Handle(OpenGl_Context)& theGlCtx) const;
94
95   //! Notice that VBO will be unbound after this call.
96   //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
97   //! @param theElemsNb      - elements count;
98   //! @param theData         - pointer to GLfloat data (vertices/normals etc.).
99   bool Init (const Handle(OpenGl_Context)& theGlCtx,
100              const GLuint   theComponentsNb,
101              const GLsizei  theElemsNb,
102              const GLfloat* theData)
103   {
104     return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_FLOAT);
105   }
106
107   //! Notice that VBO will be unbound after this call.
108   //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
109   //! @param theElemsNb      - elements count;
110   //! @param theData         - pointer to GLuint data (indices etc.).
111   bool Init (const Handle(OpenGl_Context)& theGlCtx,
112              const GLuint  theComponentsNb,
113              const GLsizei theElemsNb,
114              const GLuint* theData)
115   {
116     return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_UNSIGNED_INT);
117   }
118
119   //! Notice that VBO will be unbound after this call.
120   //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
121   //! @param theElemsNb      - elements count;
122   //! @param theData         - pointer to GLushort data (indices etc.).
123   bool Init (const Handle(OpenGl_Context)& theGlCtx,
124              const GLuint    theComponentsNb,
125              const GLsizei   theElemsNb,
126              const GLushort* theData)
127   {
128     return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_UNSIGNED_SHORT);
129   }
130
131   //! Notice that VBO will be unbound after this call.
132   //! @param theComponentsNb - specifies the number of components per generic vertex attribute; must be 1, 2, 3, or 4;
133   //! @param theElemsNb      - elements count;
134   //! @param theData         - pointer to GLubyte data (indices/colors etc.).
135   bool Init (const Handle(OpenGl_Context)& theGlCtx,
136              const GLuint   theComponentsNb,
137              const GLsizei  theElemsNb,
138              const GLubyte* theData)
139   {
140     return init (theGlCtx, theComponentsNb, theElemsNb, theData, GL_UNSIGNED_BYTE);
141   }
142
143   //! Notice that VBO will be unbound after this call.
144   //! Function replaces portion of data within this VBO using glBufferSubData().
145   //! The VBO should be initialized before call.
146   //! @param theElemFrom - element id from which replace buffer data (>=0);
147   //! @param theElemsNb  - elements count (theElemFrom + theElemsNb <= GetElemsNb());
148   //! @param theData     - pointer to GLfloat data.
149   bool SubData (const Handle(OpenGl_Context)& theGlCtx,
150                 const GLsizei  theElemFrom,
151                 const GLsizei  theElemsNb,
152                 const GLfloat* theData)
153   {
154     return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_FLOAT);
155   }
156
157   //! Notice that VBO will be unbound after this call.
158   //! Function replaces portion of data within this VBO using glBufferSubData().
159   //! The VBO should be initialized before call.
160   //! @param theElemFrom element id from which replace buffer data (>=0);
161   //! @param theElemsNb  elements count (theElemFrom + theElemsNb <= GetElemsNb());
162   //! @param theData     pointer to GLuint data.
163   bool SubData (const Handle(OpenGl_Context)& theGlCtx,
164                 const GLsizei theElemFrom,
165                 const GLsizei theElemsNb,
166                 const GLuint* theData)
167   {
168     return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_INT);
169   }
170
171   //! Notice that VBO will be unbound after this call.
172   //! Function replaces portion of data within this VBO using glBufferSubData().
173   //! The VBO should be initialized before call.
174   //! @param theElemFrom element id from which replace buffer data (>=0);
175   //! @param theElemsNb  elements count (theElemFrom + theElemsNb <= GetElemsNb());
176   //! @param theData     pointer to GLushort data.
177   bool SubData (const Handle(OpenGl_Context)& theGlCtx,
178                 const GLsizei   theElemFrom,
179                 const GLsizei   theElemsNb,
180                 const GLushort* theData)
181   {
182     return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_SHORT);
183   }
184
185   //! Notice that VBO will be unbound after this call.
186   //! Function replaces portion of data within this VBO using glBufferSubData().
187   //! The VBO should be initialized before call.
188   //! @param theElemFrom element id from which replace buffer data (>=0);
189   //! @param theElemsNb  elements count (theElemFrom + theElemsNb <= GetElemsNb());
190   //! @param theData     pointer to GLubyte data.
191   bool SubData (const Handle(OpenGl_Context)& theGlCtx,
192                 const GLsizei  theElemFrom,
193                 const GLsizei  theElemsNb,
194                 const GLubyte* theData)
195   {
196     return subData (theGlCtx, theElemFrom, theElemsNb, theData, GL_UNSIGNED_BYTE);
197   }
198
199   //! Bind this VBO to active GLSL program.
200   Standard_EXPORT void BindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx,
201                                          const GLuint                  theAttribLoc) const;
202
203   //! Unbind any VBO from active GLSL program.
204   Standard_EXPORT void UnbindVertexAttrib (const Handle(OpenGl_Context)& theGlCtx,
205                                            const GLuint                  theAttribLoc) const;
206
207   //! Bind this VBO and enable specified attribute in OpenGl_Context::ActiveProgram() or FFP.
208   //! @param theGlCtx - handle to bound GL context;
209   //! @param theMode  - array mode (GL_VERTEX_ARRAY, GL_NORMAL_ARRAY, GL_COLOR_ARRAY, GL_INDEX_ARRAY, GL_TEXTURE_COORD_ARRAY).
210   void BindAttribute (const Handle(OpenGl_Context)&   theCtx,
211                       const Graphic3d_TypeOfAttribute theMode) const
212   {
213     if (IsValid())
214     {
215       Bind (theCtx);
216       bindAttribute (theCtx, theMode, static_cast<GLint> (myComponentsNb), myDataType, 0, myOffset);
217     }
218   }
219
220   //! Unbind this VBO and disable specified attribute in OpenGl_Context::ActiveProgram() or FFP.
221   //! @param theCtx handle to bound GL context
222   //! @param theMode  array mode
223   void UnbindAttribute (const Handle(OpenGl_Context)&   theCtx,
224                         const Graphic3d_TypeOfAttribute theMode) const
225   {
226     if (IsValid())
227     {
228       Unbind (theCtx);
229       unbindAttribute (theCtx, theMode);
230     }
231   }
232
233 public: //! @name advanced methods
234
235   //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules.
236   virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE
237   {
238     return IsValid()
239          ? sizeOfGlType (myDataType) * myComponentsNb * myElemsNb
240          : 0;
241   }
242
243   //! @return size of specified GL type
244   static size_t sizeOfGlType (const GLenum theType)
245   {
246     switch (theType)
247     {
248       case GL_BYTE:
249       case GL_UNSIGNED_BYTE:  return sizeof(GLubyte);
250       case GL_SHORT:
251       case GL_UNSIGNED_SHORT: return sizeof(GLushort);
252     #ifdef GL_INT
253       case GL_INT:
254     #endif
255       case GL_UNSIGNED_INT:   return sizeof(GLuint);
256       case GL_FLOAT:          return sizeof(GLfloat);
257     #ifdef GL_DOUBLE
258       case GL_DOUBLE:         return sizeof(GLdouble);
259     #endif
260       default:                return 0;
261     }
262   }
263
264   //! Initialize buffer with new data.
265   Standard_EXPORT virtual bool init (const Handle(OpenGl_Context)& theGlCtx,
266                                      const GLuint   theComponentsNb,
267                                      const GLsizei  theElemsNb,
268                                      const void*    theData,
269                                      const GLenum   theDataType,
270                                      const GLsizei  theStride);
271
272   //! Initialize buffer with new data.
273   bool init (const Handle(OpenGl_Context)& theGlCtx,
274              const GLuint   theComponentsNb,
275              const GLsizei  theElemsNb,
276              const void*    theData,
277              const GLenum   theDataType)
278   {
279     return init (theGlCtx, theComponentsNb, theElemsNb, theData, theDataType, GLsizei(theComponentsNb) * GLsizei(sizeOfGlType (theDataType)));
280   }
281
282   //! Update part of the buffer with new data.
283   Standard_EXPORT virtual bool subData (const Handle(OpenGl_Context)& theGlCtx,
284                                         const GLsizei  theElemFrom,
285                                         const GLsizei  theElemsNb,
286                                         const void*    theData,
287                                         const GLenum   theDataType);
288
289   //! Setup array pointer - either for active GLSL program OpenGl_Context::ActiveProgram()
290   //! or for FFP using bindFixed() when no program bound.
291   static void bindAttribute (const Handle(OpenGl_Context)&   theGlCtx,
292                              const Graphic3d_TypeOfAttribute theMode,
293                              const GLint                     theNbComp,
294                              const GLenum                    theDataType,
295                              const GLsizei                   theStride,
296                              const GLvoid*                   theOffset);
297
298   //! Disable GLSL array pointer - either for active GLSL program OpenGl_Context::ActiveProgram()
299   //! or for FFP using unbindFixed() when no program bound.
300   static void unbindAttribute (const Handle(OpenGl_Context)&   theGlCtx,
301                                const Graphic3d_TypeOfAttribute theMode);
302
303 private:
304 #if !defined(GL_ES_VERSION_2_0)
305   //! Setup FFP array pointer.
306   static void bindFixed (const Handle(OpenGl_Context)&   theGlCtx,
307                          const Graphic3d_TypeOfAttribute theMode,
308                          const GLint                     theNbComp,
309                          const GLenum                    theDataType,
310                          const GLsizei                   theStride,
311                          const GLvoid*                   theOffset);
312
313   //! Disable FFP array pointer.
314   static void unbindFixed (const Handle(OpenGl_Context)&   theGlCtx,
315                            const Graphic3d_TypeOfAttribute theMode);
316
317   //! Disable FFP color array pointer.
318   Standard_EXPORT static void unbindFixedColor (const Handle(OpenGl_Context)& theCtx);
319
320 #endif
321 public: //! @name methods for interleaved attributes array
322
323   //! @return true if buffer contains per-vertex color attribute
324   Standard_EXPORT virtual bool HasColorAttribute() const;
325
326   //! @return true if buffer contains per-vertex normal attribute
327   Standard_EXPORT virtual bool HasNormalAttribute() const;
328
329   //! Bind all vertex attributes to active program OpenGl_Context::ActiveProgram() or for FFP.
330   //! Default implementation does nothing.
331   Standard_EXPORT virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const;
332
333   //! Bind vertex position attribute only. Default implementation does nothing.
334   Standard_EXPORT virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const;
335
336   //! Unbind all vertex attributes. Default implementation does nothing.
337   Standard_EXPORT virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const;
338
339 protected:
340
341   GLubyte* myOffset;       //!< offset to data
342   GLuint   myBufferId;     //!< VBO name (index)
343   GLuint   myComponentsNb; //!< Number of components per generic vertex attribute, must be 1, 2, 3, or 4
344   GLsizei  myElemsNb;      //!< Number of vertex attributes / number of vertices
345   GLenum   myDataType;     //!< Data type (GL_FLOAT, GL_UNSIGNED_INT, GL_UNSIGNED_BYTE etc.)
346
347 public:
348
349   DEFINE_STANDARD_RTTIEXT(OpenGl_VertexBuffer,OpenGl_Resource) // Type definition
350
351 };
352
353 DEFINE_STANDARD_HANDLE(OpenGl_VertexBuffer, OpenGl_Resource)
354
355 #include <OpenGl_VertexBuffer.lxx>
356
357 #endif // _OpenGl_VertexBuffer_H__