HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RE_VertexState.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_VertexState.h ( RE Library, C++)
7  *
8  * COMMENTS:
9  * Encapsulates all state required for vertex processing by a vertex
10  * shader. It uses a VAO to include all vertex array state, but
11  * additionally captures constant vertex attribs.
12  *
13  * This only works if RE_EXT_VERTEX_ARRAY_OBJECT is supported.
14  */
15 #ifndef RE_VertexState_h
16 #define RE_VertexState_h
17 
18 class RE_VertexArray;
19 class RE_Render;
20 class RE_VertexMap;
21 class RE_OGLTexture;
22 
23 #include <iosfwd>
24 #include <UT/UT_Pair.h>
25 #include <UT/UT_String.h>
26 #include <UT/UT_SmallArray.h>
27 #include <UT/UT_StringArray.h>
28 #include "RE_OGLVertexArrayList.h"
30 
32 {
33 public:
34  re_TexBufData() : tex_id(0), mode(0) {}
35  int tex_id;
36  int mode;
39 };
40 
41 
43 {
44 public:
45  RE_VertexMap(int num_attribs = 0);
46  ~RE_VertexMap();
47 
48  void setMapSize(int nv);
49 
50  virtual bool init(RE_Render *r);
51 
52  // This is used by the RE_VertexState to set its attributes
53  int setAttrib(RE_VertexArray *attrib, RE_GPUType shader_type,
54  int attribute_location);
55 
56  // This is normally called by shaders to setup their vertex map
57  int setAttrib(const char *attrib_name, RE_GPUType t, int vsize,
58  int attribute_location, RE_GenericAttribID id,
59  int64 *maskptr = NULL);
60 
61  //
62  int64 setConstAttrib(const char *attrib_name, RE_GPUType t, int vsize,
63  int attribute_location, RE_GenericAttribID id);
64 
65  // if unbind_attr is true and an attribute with "attrib_name" is present, it
66  // will be removed from the map. Otherwise, it will be retained.
67  int setTexBuffer(const char *attrib_name, bool unbind_attr);
68 
69  // Remove a vertex attribute or constant from a vertex shader input
70  void removeAttrib(const char *attrib_name);
71 
72  // Remove a texture buffer attribute.
73  void removeTexBuffer(const char *attrib_name);
74 
75  // Clear a vertex attribute or constant at an attribute location
76  void clearAttrib(int attribute_location);
77 
78  // Clear the entire map - buffers, attributes, constants.
79  void clear();
80 
81  bool compatibleWith(const RE_VertexMap &shader_map,
82  const RE_VertexMap *geo_map) const;
83 
84  // Vertex attribute existence mask, indexed by (1<<RE_GenericAttribID)
85  int64 getAttributeMask() const { return myBoundAttributes; }
86  // Constant attribute existence mask, indexed by (1<<RE_GenericAttribID)
87  int64 getConstantMask() const { return myBoundConstants; }
88  // Texture attribute existence mask, indexed by (1<<RE_GenericAttribID)
89  int64 getTextureMask() const { return myBoundTexAttribs; }
90 
91  // Attribute location filled, indexed by (1<<attribute_location)
92  int64 getLocationMask() const { return myBoundLocations; }
93 
94  // Return true if the custom attribute 'attrib_name' is present
95  bool hasCustomAttrib(const char *attrib_name) const;
96 
97  void print(RE_Render *r, std::ostream &os) const;
98 
99  /// Returns the amount of main memory owned by this RE_VertexMap.
100  /// RE_VertexMap does not have an associated graphics memory.
101  int64 getMemoryUsage(bool inclusive) const
102  {
103  int64 mem = inclusive ? sizeof(*this) : 0;
104  mem += myCustomAttribs.getMemoryUsage(false);
105  mem += myAttribLocs.getMemoryUsage(false);
106  mem += myTexAttribNames.getMemoryUsage(false);
107  mem += myAttribIDs.getMemoryUsage(false);
108  return mem;
109  }
110 
111 private:
112  int myMapSize;
114  myAttribIDs;
116  myAttribLocs;
117  UT_StringArray myTexAttribNames;
118  int64 myBoundLocations;
119  int64 myBoundAttributes;
120  int64 myBoundConstants;
121  int64 myBoundTexAttribs;
122  int myUndefAttribIndex;
123 
124  class re_CustomAttrib
125  {
126  public:
127  re_CustomAttrib() : location(-1) {}
128  re_CustomAttrib(const char *name, int loc)
129  : attrib_name(name,1), location(loc) {}
130 
131  UT_String attrib_name;
132  int location;
133  };
134 
135  UT_Array<re_CustomAttrib> myCustomAttribs;
136 };
137 
138 
140 {
141 public:
142  RE_VertexState();
143  ~RE_VertexState();
144 
145  // Initialization only fails if VAOs are not supported.
146  bool init(RE_Render *r);
147 
148  void reset();
149 
150  // Bind for update only binds the VAO.
151  bool bindForUpdate(RE_Render *r);
152 
153  // Vertex stride, usually 0 (tightly packed)
154  void setVertexStride(int stride) { myVertexStride = stride; }
155 
156  // Add an array during update to the vertex state.
157  bool assignArray(RE_Render *r,
158  RE_VertexArray *attrib,
159  int attrib_location,
160  RE_GPUType shader_type);
161 
162  // Assign a zeroed, but present, attribute to the generic vertex index.
163  bool assignZero(RE_Render *r, const char *attrib_name,
164  RE_GPUType t, int vector_size,
165  int attrib_location);
166 
167  bool assignTexBuffer(RE_Render *r,
168  const char *name,
169  RE_OGLTexture *t,
170  int mode);
171 
172  // Remove an array from the vertex state.
173  bool removeArray(RE_Render *r,
174  int attrib_location);
175 
176  // Unbinds an array from the VAO.
177  void unbindElementArray(RE_Render *r);
178 
179  // Bind for draw binds both the VAO and the constants. It is an error to
180  // bind the vertex state for drawing if it is not initialized yet.
181  bool bindForDrawing(RE_Render *r, RE_Shader *sh);
182 
183  // Sets the current VAO back to 0 (none bound).
184  // Does not restore the values of constant attributes.
185  bool unbind(RE_Render *r);
186 
187  // Marks the vertex state as dirty if the passed serial index doesn't match
188  // the stored serial index.
189  bool checkDirty(RE_Render *r, int serial_index);
190 
191  // Resets the vertex state so that buffers will be rebound.
192  void reset(RE_Render *r);
193 
194  // Returns true if this vertex state is compatible with the given map.
195  // It must be the same or a superset of the map (attribute names matching
196  // and in the correct attribute locations).
197  bool compatibleWith(RE_Render *r,
198  const RE_VertexMap &shader_map,
199  const RE_VertexMap *geo_map,
200  unsigned int stride);
201 
202  void print(RE_Render *r, std::ostream &os);
203 
204  bool hasBoundTBO(int tex_unit) const;
205 
206  // Generic Attribute ID functions
207  static void assignDefaultMap(RE_Render *r, RE_Shader *shader);
208 
209  static RE_GenericAttribID getGenericAttributeID(const char *name,
210  bool *in_instance_group =
211  NULL);
212  static const char *getGenericAttributeName(RE_GenericAttribID id);
213  static const char *getGenericAttributeModeName(RE_GenericAttribID id);
214  static bool getGenericAttributeUniforms(RE_GenericAttribID id,
215  UT_String &mode,
216  UT_String &sampler);
218  getGenericAttributeLocation(RE_GenericAttribID id);
219  static bool getConstInstanceAttributeSupport(RE_GenericAttribID id);
220 
221  static bool texBufferDeleted(void *, void *);
222 
223  static void dumpGLVertexState(RE_Render *r);
224 
225  /// Returns the amount of main memory (NOT graphics memory!)
226  /// owned by this RE_VertexState.
227  int64 getMemoryUsage(bool inclusive) const;
228 
229 private:
230  RE_SharedVertexArray myVAO;
231  fpreal64 *myConstAttribValue;
232  RE_GPUType *myConstAttribType;
233  unsigned char *myConstAttribSize;
235  re_TexBufData *myTexBufferData;
236  UT_ValArray<re_TexBufData*> myUndefTexBufferData;
237  int myNumVertexAttribs;
238  int myVertexStride;
239  int myBoundState;
240  int myDirtySerial;
242  UT_IntArray myHasBoundTBO;
243 };
244 
245 inline bool
247 {
248  if(myDirtySerial != serial_index)
249  {
250  myDirtySerial = serial_index;
251  if(myBoundState >= 0)
252  myVertexMap.clear();
253 
255  myVAO.free(r);
256 
257  return true;
258  }
259  return false;
260 }
261 
262 inline void
264 {
265  checkDirty(r, myDirtySerial-1);
266 }
267 
268 inline bool
269 RE_VertexState::hasBoundTBO(int tex_unit) const
270 {
271  return (tex_unit < myHasBoundTBO.entries() && myHasBoundTBO(tex_unit));
272 }
273 #endif
274 
275 
#define RE_API
Definition: RE_API.h:10
virtual bool init(RE_Render *r)=0
int64 getLocationMask() const
int64 getMemoryUsage(bool inclusive) const
GLuint sampler
Definition: glcorearb.h:1655
static RE_GraphicsDevice getGraphicsDevice()
RE_GenericAttribID
Definition: RE_Types.h:323
long long int64
Definition: SYS_Types.h:107
RE_GPUType
Definition: RE_Types.h:44
bool hasBoundTBO(int tex_unit) const
double fpreal64
Definition: SYS_Types.h:192
int64 getTextureMask() const
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:871
GLint location
Definition: glcorearb.h:804
int64 getAttributeMask() const
GLuint const GLchar * name
Definition: glcorearb.h:785
RE_ShaderAttribLocation
Definition: RE_Types.h:358
GLenum mode
Definition: glcorearb.h:98
void setVertexStride(int stride)
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:453
GLuint shader
Definition: glcorearb.h:784
UT_String mode_uniform
int64 getConstantMask() const
UT_String samp_uniform
GLboolean r
Definition: glcorearb.h:1221
bool checkDirty(RE_Render *r, int serial_index)