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