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