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