1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkOpenGLTexture.h
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 /**
16  * @class   vtkOpenGLVertexBufferObjectGroup
17  * @brief   manage vertex buffer objects shared within a mapper
18  *
19  * This class holds onto the VBOs that a mapper is using.
20  * The basic operation is that during the render process
21  * the mapper may cache a number of dataArrays as VBOs
22  * associated with attributes. This class keep track of
23  * freeing VBOs no longer used by the mapper and uploading
24  * new data as needed.
25  *
26  * When using CacheCataArray the same array can be set
27  * each time and this class will not rebuild or upload
28  * unless needed.
29  *
30  * When using the AppendDataArray API no caching is done
31  * and the VBOs will be rebuilt and uploaded each time.
32  * So when appending th emapper need to handle checking
33  * if the VBO should be updated.
34  *
35  * Use case:
36  *   make this an ivar of your mapper
37  *   vbg->CacheDataArray("vertexMC", vtkDataArray);
38  *   vbg->BuildAllVBOs();
39  *   if (vbg->GetMTime() > your VAO update time)
40  *   {
41  *     vbg->AddAllAttributesToVAO(...);
42  *   }
43  *
44  * Appended Use case:
45  *   make this an ivar of your mapper
46  *   if (you need to update your VBOs)
47  *   {
48  *     vbg->ClearAllVBOs();
49  *     vbg->AppendDataArray("vertexMC", vtkDataArray1);
50  *     vbg->AppendDataArray("vertexMC", vtkDataArray2);
51  *     vbg->AppendDataArray("vertexMC", vtkDataArray3);
52  *     vbg->BuildAllVBOs();
53  *     vbg->AddAllAttributesToVAO(...);
54  *   }
55  *
56  * use VAO
57  *
58  */
59 
60 #ifndef vtkOpenGLVertexBufferObjectGroup_h
61 #define vtkOpenGLVertexBufferObjectGroup_h
62 
63 #include "vtkObject.h"
64 #include "vtkRenderingOpenGL2Module.h" // For export macro
65 #include <map>                         // for methods
66 #include <string>                      // for ivars
67 #include <vector>                      // for ivars
68 
69 class vtkDataArray;
70 class vtkOpenGLVertexArrayObject;
71 class vtkOpenGLVertexBufferObject;
72 class vtkOpenGLVertexBufferObjectCache;
73 class vtkShaderProgram;
74 class vtkViewport;
75 class vtkWindow;
76 
77 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLVertexBufferObjectGroup : public vtkObject
78 {
79 public:
80   static vtkOpenGLVertexBufferObjectGroup* New();
81   vtkTypeMacro(vtkOpenGLVertexBufferObjectGroup, vtkObject);
82   void PrintSelf(ostream& os, vtkIndent indent) override;
83 
84   /**
85    * Returns the number of components for this attribute
86    * zero if the attribute does not exist
87    */
88   int GetNumberOfComponents(const char* attribute);
89 
90   /**
91    * Returns the number of tuples for this attribute
92    * zero if the attribute does not exist
93    */
94   int GetNumberOfTuples(const char* attribute);
95 
96   /**
97    * Release any graphics resources that are being consumed by this mapper.
98    * The parameter window could be used to determine which graphic
99    * resources to release.
100    */
101   void ReleaseGraphicsResources(vtkWindow*);
102 
103   /**
104    * Returns the VBO for an attribute, NULL if
105    * it is not present.
106    */
107   vtkOpenGLVertexBufferObject* GetVBO(const char* attribute);
108 
109   /**
110    * Attach all VBOs to their attributes
111    */
112   void AddAllAttributesToVAO(vtkShaderProgram* program, vtkOpenGLVertexArrayObject* vao);
113 
114   /**
115    * used to remove a no longer needed attribute
116    * Calling CacheDataArray with a nullptr
117    * attribute will also work.
118    */
119   void RemoveAttribute(const char* attribute);
120 
121   /**
122    * Set the data array for an attribute in the VBO Group
123    * registers the data array until build is called
124    * once this is called a valid VBO will exist
125    */
126   void CacheDataArray(
127     const char* attribute, vtkDataArray* da, vtkOpenGLVertexBufferObjectCache* cache, int destType);
128   void CacheDataArray(const char* attribute, vtkDataArray* da, vtkViewport* vp, int destType);
129 
130   /**
131    * Check if the array already exists.
132    * offset is the index of the first vertex of the array if it exists.
133    * totalOffset is the total number of vertices in the appended arrays.
134    * Note that if the array does not exist, offset is equal to totalOffset.
135    */
136   bool ArrayExists(
137     const char* attribute, vtkDataArray* da, vtkIdType& offset, vtkIdType& totalOffset);
138 
139   /**
140    * Append a data array for an attribute in the VBO Group
141    * registers the data array until build is called
142    */
143   void AppendDataArray(const char* attribute, vtkDataArray* da, int destType);
144 
145   /**
146    * using the data arrays in this group
147    * build all the VBOs, once this has been called the
148    * reference to the data arrays will be freed.
149    */
150   void BuildAllVBOs(vtkOpenGLVertexBufferObjectCache*);
151   void BuildAllVBOs(vtkViewport*);
152 
153   /**
154    * Force all the VBOs to be freed from this group.
155    * Call this prior to starting appending operations.
156    * Not needed for single array caching.
157    */
158   void ClearAllVBOs();
159 
160   /**
161    * Clear all the data arrays. Typically an internal method.
162    * Automatically called at the end of BuildAllVBOs to prepare
163    * for the next set of attributes.
164    */
165   void ClearAllDataArrays();
166 
167   /**
168    * Get the mtime of this groups VBOs
169    */
170   vtkMTimeType GetMTime() override;
171 
172 protected:
173   vtkOpenGLVertexBufferObjectGroup();
174   ~vtkOpenGLVertexBufferObjectGroup() override;
175 
176   std::map<std::string, vtkOpenGLVertexBufferObject*> UsedVBOs;
177   std::map<std::string, std::vector<vtkDataArray*>> UsedDataArrays;
178   std::map<std::string, std::map<vtkDataArray*, vtkIdType>> UsedDataArrayMaps;
179   std::map<std::string, vtkIdType> UsedDataArraySizes;
180 
181 private:
182   vtkOpenGLVertexBufferObjectGroup(const vtkOpenGLVertexBufferObjectGroup&) = delete;
183   void operator=(const vtkOpenGLVertexBufferObjectGroup&) = delete;
184 };
185 
186 #endif
187