HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RE_ElementArray.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: RE_ElementArray.h ( RE Library, C++)
7  *
8  * COMMENTS:
9  * Abstracts a vertex element array, which is used for indexing which
10  * points make up a primitive. This class provides several important
11  * abstractions:
12  *
13  * - allows primitives to be specified one at a time or in batches
14  * - dices up long element arrays into sizes the HW can support
15  * - uses a primitive restart index automatically for variable-sized prims
16  * - builds an edge array so that geometry shaders can determine false
17  * edges (the array is a 2-comp array of {index,maxindex}).
18  */
19 #ifndef RE_ElementArray_h
20 #define RE_ElementArray_h
21 
22 class re_ElementChunk;
23 
24 #include <UT/UT_ValArray.h>
25 #include "RE_VertexArray.h"
26 #include "RE_Render.h"
27 
28 #include <iosfwd>
29 
31 {
32 public:
33  explicit RE_ElementArray(bool use_buffers = true);
34  ~RE_ElementArray();
35 
36  /// Returns the amount of main memory (NOT graphics memory!)
37  /// owned by this RE_ElementArray.
38  int64 getMemoryUsage(bool inclusive) const;
39 
40  // Establish the base cache name for the element arrays created.
41  void setCacheName(const char *cachename);
42 
43  // Sets the version of this particular element array.
44  void setCacheVersion(RE_CacheVersion version);
45  RE_CacheVersion getCacheVersion() const;
46 
47  // Returns true if all element buffers were found in the cache, with
48  // the appropriate version.
49  bool findCachedVersion(RE_CacheVersion version);
50 
51  // Set up the element buffer type. These cannot be called while within a
52  // begin/endPrims(), however you can switch types and start a new
53  // begin/endPrims pair (don't do this often, however, as the buffers will
54  // be very small chunks and the #draw calls will increase substantially)
55  void setElementType(RE_GPUType type);
56 
57  // Set the primitive type that the element array represents. This cannot
58  // be changed between begin/endPrims().
59  void setPrimitiveType(RE_PrimType type, int vertices_per_patch = 0);
60 
61  // Alternate way to set the type, by the length of the vertex array being
62  // indexed.
63  void setNumPoints(int points);
64 
65  // If true, quads and polygons are tessellated to triangles. If primitive
66  // restart is not supported, polygons are always tessellated to triangles,
67  // otherwise triangle tessellation is disabled by default.
68  void tessellateToTriangles(bool enable);
69 
70  // enables or disables the generation of a primitive info buffer. It is
71  // disabled by default. This also enables triangle tessellation.
72  void requirePrimInfo(bool enable);
73 
74  // enables or disables the generation of a vertex info buffer. It is
75  // disabled by default. This also enables triangle tessellation.
76  void requireVertexInfo(bool enable);
77 
78  // Adds primitive connectivity information to the arrays. beginPrims()
79  // must be called before any addPrim() calls are done, and endPrims()
80  // before a draw() is issued.
81 
82  // beginPrims() may fail if a unbounded primitive is used without the
83  // RE_EXT_PRIMITIVE_RESTART extension. 'num_vertex_hint' gives the element
84  // array an initial hint as to how many vertices will be in the array.
85  bool beginPrims(RE_Render *r,
86  bool clear_old = false,
87  int num_vertex_hint = 0);
88 
89  // Adds a single primitive. May fail if npts doesn't fit the current
90  // primitive (4, TRIANGLES). 'real_npnts' is used if mixed size polygons
91  // up to 'npnts' is size are being output through a fixed GL size type.
92  bool addPrim(RE_Render *r, int npnts, int *pnt_indices,
93  int *prim_info = NULL,
94  uint8 *vert_info = NULL,
95  int base_prim = -1,
96  int base_vert = -1);
97 
98  // Specifies indicies for multiple primitives.
99  bool addPrims(RE_Render *r, int npnts, int *prim_indices,
100  int *prim_info = NULL,
101  uint8 *vert_info = NULL,
102  bool simple_pindex = false,
103  int pindex_start = 0);
104 
105  // Special case for pre-convexed models.
106  bool addTriangle(RE_Render *r, int *pnt_indices, int *prim_info,
107  uint8 *vert_info );
108 
109  // Must be called after a beginPrims() and before a draw().
110  bool endPrims(RE_Render *r);
111 
112  // Upload all information at once without doing begin/end.
113  bool addTriangleChunk(RE_Render *r, int num_triangles,
114  const int *pnt_indices, // num*3
115  const int *prim_info = NULL, // num*4
116  const uint8 *vert_info = NULL); // num*4
117 
118  // Draws the primitives using the element arrays. Other vertex arrays must
119  // already be enabled. Instanced drawing is only supported if
120  // RE_EXT_DRAW_INSTANCED is supported.
121  // if 'prim' is not RE_PRIM_AS_IS, it will override the primitive type
122  // specified by setPrimitiveType.
123  void draw(RE_Render *r,
124  RE_Geometry *geo = NULL,
125  RE_PrimType prim = RE_PRIM_AS_IS);
126  void drawInstanced(RE_Render *r, int num_instances,
127  RE_Geometry *geo = NULL,
128  RE_PrimType prim = RE_PRIM_AS_IS);
129 
130  // Returns the minimum and maximum number edges in the primitives added.
131  // These are not defined for element arrays fetched from the cache, only
132  // newly created ones.
133  int getMinEdges() const;
134  int getMaxEdges() const;
135 
136  // Number of arrays created, equal to the number of GL draw calls draw()
137  // performs. This is not defined for element arrays fetched from the
138  // cache, only newly created ones.
139  int getNumArrays() const;
140 
141  int getNumVertices() const;
142  int getNumOrigVertices() const;
143  int getNumPrimitives() const { return myNumPrims; }
144  RE_PrimType getPrimitiveType() const { return myPrimType; }
145  int getVerticesPerPatch() const { return myVerticesPerPatch; }
146 
147  /// Return the size of all underlying arrays
148  int64 getSizeBytes() const;
149 
150  // Only used by RE_Geometry.
151  void clearCachedData() { myHasGeoPrimInfo = -1; }
152 
153  // debug print method
154  void print(std::ostream &os);
155 
156 private:
157  void completeElementBuffer(RE_Render *r);
158  RE_VertexArray *completePrimInfoBuffer(RE_Render *r, const char *cachename);
159  RE_VertexArray *completeVertInfoBuffer(RE_Render *r, const char *cachename);
160  void drawPrivate(RE_Render *r, int num_instances,
161  RE_Geometry *geo, RE_PrimType prim);
162 
163  void bumpBufferSizes(int nprims);
164 
165  RE_GPUType myElementType;
166  RE_PrimType myPrimType;
167  UT_String myCacheName;
168  RE_CacheVersion myCacheVersion;
169  bool myUseBaseVertex;
170  bool myAddingPrims;
171  bool myUsePrimRestart;
172  bool myLineSegmentMode;
173  bool myFilledMode;
174  bool myRequirePrimInfo;
175  bool myRequireVertInfo;
176  bool myTessellateToTriangles;
177 
178  // Global
179  int myMinPrimEdges;
180  int myMaxPrimEdges;
181  int myNumPrims;
182  int myNumOrigVertices;
183  int myPrimIndex;
184  int myVerticesPerPatch;
185 
186  // Per chunk
187  int myChunkMinEdges;
188  int myChunkMaxEdges;
189  int myChunkNumPrims;
190  int myChunkMinIndex;
191  int myChunkMaxIndex;
192 
193  // Current element chunk
194  int myElementSize;
195  int myElementIndex;
196  int *myElementBuffer;
197 
198  int myHasGeoPrimInfo;
199  int myPrimInfoSize;
200  int myPrimInfoIndex;
201  int *myPrimInfoBuffer;
202 
203  int myHasGeoVertInfo;
204  int myVertInfoSize;
205  int myVertInfoIndex;
206  uint32 *myVertInfoBuffer;
207 
208  // Previously processed chunks
210 
211  // cached data for drawing
212  void *myLastShader;
213  int myLastShaderSerial;
214  int myLastShaderPrimInfoIndex;
215  int myLastShaderVertInfoIndex;
216 };
217 
218 // Inlines
219 inline int
221 {
222  return myMinPrimEdges;
223 }
224 
225 inline int
227 {
228  return myMaxPrimEdges;
229 }
230 
231 inline int
233 {
234  return myChunks.entries();
235 }
236 
237 inline void
239 {
240  drawPrivate(r, 0, geo, prim);
241 }
242 
243 inline void
245  RE_Geometry *geo,
246  RE_PrimType prim)
247 {
248  UT_ASSERT(num_instances>0);
251  drawPrivate(r, num_instances, geo, prim);
252 }
253 
254 #endif
int getMinEdges() const
#define RE_API
Definition: RE_API.h:10
A collection of vertex arrays defining a geometry object. This class acts as a wrapper around multipl...
Definition: RE_Geometry.h:53
int getVerticesPerPatch() const
RE_PrimType getPrimitiveType() const
long long int64
Definition: SYS_Types.h:107
RE_GPUType
Definition: RE_Types.h:44
int getMaxEdges() const
void drawInstanced(RE_Render *r, int num_instances, RE_Geometry *geo=NULL, RE_PrimType prim=RE_PRIM_AS_IS)
bool hasGLExtension(RE_Extension e) const
GT_API const UT_StringHolder version
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:453
int getNumArrays() const
int getNumPrimitives() const
unsigned char uint8
Definition: SYS_Types.h:32
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
void draw(RE_Render *r, RE_Geometry *geo=NULL, RE_PrimType prim=RE_PRIM_AS_IS)
Simple class for a mutli-integer cache tag.
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:126
GLboolean r
Definition: glcorearb.h:1221
RE_PrimType
Definition: RE_Types.h:193
unsigned int uint32
Definition: SYS_Types.h:36