HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RE_Shader.h
Go to the documentation of this file.
1 
2 /*
3  * PROPRIETARY INFORMATION. This software is proprietary to
4  * Side Effects Software Inc., and is not to be reproduced,
5  * transmitted, or disclosed in any way without written permission.
6  *
7  * NAME: Render Library (C++)
8  *
9  * COMMENTS:
10  * OpenGL GLSL/Cg Shader base definition. Abstracts the difference
11  * between the two.
12  *
13  */
14 #ifndef __RE_Shader__
15 #define __RE_Shader__
16 
17 #include "RE_API.h"
18 #include "RE_Texture.h"
19 #include "RE_TextureTypes.h"
20 #include "RE_Types.h"
21 #include "RE_Uniform.h"
22 
23 #include <UT/UT_Array.h>
24 #include <UT/UT_Color.h>
25 #include <UT/UT_IntrusivePtr.h>
26 #include <UT/UT_Matrix2.h>
27 #include <UT/UT_Matrix3.h>
28 #include <UT/UT_Matrix4.h>
29 #include <UT/UT_String.h>
30 #include <UT/UT_StringHolder.h>
31 #include <UT/UT_SymbolTable.h>
32 #include <UT/UT_ValArray.h>
33 #include <UT/UT_Vector2.h>
34 #include <UT/UT_Vector3.h>
35 #include <UT/UT_Vector4.h>
36 #include <UT/UT_VectorTypes.h>
37 #include <UT/UT_WorkBuffer.h>
38 #include <SYS/SYS_Math.h>
39 #include <SYS/SYS_Types.h>
40 #include <iosfwd>
41 
42 class RE_ElementArray;
43 class RE_Geometry;
44 class RE_Material;
45 class RE_OGLTexture;
46 class RE_Render;
47 class RE_ShaderStage;
48 class RE_UniformBlock;
49 class RE_UniformData;
50 class RE_VertexArray;
51 class RE_VertexMap;
52 class RE_VertexState;
53 class UT_StringArray;
54 
55 class re_LibFunc;
56 class re_VertexName;
57 
59 
61 {
62 public:
63  /// Program name is readable, shader_version may be 0 or a
64  /// valid GLSL version (110, 120, 130, 140, 150, 330, 400, 410, 420..)
65  enum { USE_GLSL_BASE_VERSION = -1,
66  USE_SHADER_GLSL_VERSION = 0 };
67 
68  RE_Shader(const char *program_name, int shader_version);
69  virtual ~RE_Shader();
70 
71  int getUniqueID() const { return myUniqueID; }
72 
73  /// Returns the amount of main memory (NOT graphics memory!)
74  /// owned by this RE_Shader.
75  virtual int64 getMemoryUsage(bool inclusive) const;
76 
77  /// If true, register this shader so that RE_Shader::getShader() can find
78  /// this shader (default is true).
79  void setRegisterProgram(bool register_program);
80 
81  ///Call to allocate a specific shader type. If you want to allocate a shader
82  ///of a specific GLSL version, set the version to the same number as the
83  ///#version string in the shader (ie, 120 for 1.20, 330 for 3.30). Otherwise
84  ///the shader inherits the version from the first shader object attached.
85  static RE_Shader *create(const char *name,
86  int shader_version = 0,
88 
89  ///Create a shader and load all shader files listed in shader_files, up to
90  ///a nullptr filename. If the shader compiles and links, a non-nullptr shader will
91  ///be returned. 'compile_msg' will contain any error messages otherwise.
92  ///Shader stages must have extensions .vert, .geom and .frag.
93  static RE_Shader *createShader(RE_Render *r,
94  const char *name,
95  const char **shader_files,
96  int shader_version = USE_SHADER_GLSL_VERSION,
97  UT_String *compile_msg = nullptr,
98  bool use_houdini_libs = true,
100  bool register_program = true,
101  const char *extra_defines = nullptr);
102 
103  /// A version with a single string containing whitespace-separated filenames
104  static RE_Shader *createShader(RE_Render *r,
105  const char *name,
106  const char *shader_files,
107  int shader_version = USE_SHADER_GLSL_VERSION,
108  UT_String *compile_msg = nullptr,
109  bool use_houdini_libs = true,
111  bool register_program = true,
112  const char *extra_defines = nullptr);
113 
114  /// Readable name of the shader
115  const char *getName() const { return myProgramName; }
116 
117  /// Any define commands used to alter this shader
118  const char *getDefines() const { return myExtraDefines; }
119 
120  /// Raw GL id of the shader
121  int getProgram() const { return myProgramObject; }
122 
123  /// True if the shader has been successfully linked
124  bool isLinked() const { return myLinkedFlag; }
125 
126  /// Returns the version of the shader (110,120,130,140,150,330,400,410).
127  int getCodeVersion() const { return myCodeVersion; }
128 
129  /// Returns the minimum GLSL version supported (base requirement)
130  static int getBaseGLSLVersion();
131 
132  /// determine the version of the shader from the source (#version <num>)
133  /// return 100 if no version is specified.
134  static int determineCodeVersion(const char *shader_string);
135 
136  /// Allocate a new shader stage of the specified type, with an optional
137  /// readable name.
138  virtual RE_ShaderStage *newShader(RE_ShaderType type,
139  const char *name = 0) = 0;
140 
141  /// @{
142  /// loadShader() loads a shader from an ASCII file.
143  /// addShader() is used when the source is already available.
144  /// 'messages' returns the warnings & errors (if any) from shader
145  /// compilation.
146  /// Multiple shaders can be attached to the same shader type; it is expected
147  /// that one is the main shader, and others are functions.
148  RE_ShaderStage *loadShader(RE_Render *r,
149  RE_ShaderType stype,
150  const char *filename,
151  UT_String *messages = nullptr,
152  const char *shader_name = nullptr,
153  const char *defines = "",
154  int default_code_version=USE_GLSL_BASE_VERSION);
155 
156  RE_ShaderStage *addShader(RE_Render *r,
157  RE_ShaderType stype,
158  const char *shader_source,
159  const char *shader_name,
160  int codeversion,
161  UT_String *messages = nullptr);
162  /// @}
163 
164  /// If true, this shader may use Houdini's builtin functions.
165  void useHoudiniLibraryFuncs();
166 
167  /// If true, this shader follows the default Houdini attribute map.
168  void useDefaultAttribMap(bool enable = true);
169 
170  /// If true, this shader defines its own attribute locations in shader code
171  /// Do not move any attributes.
172  void useExplicitAttribMap(bool enable = true);
173 
174  ///Individual Shader objects can be created separately and attached. returns
175  ///true if the shader was attached, and false if it was already attached.
176  virtual bool attachShader(RE_Render *r,
178  UT_String *messages = nullptr);
179 
180  // Detaching a shader object prevents it from being deleted when the shader
181  // is deleted. Returns true if the shader was detached, or false if the
182  // shader was not present in the program.
183  virtual bool detachShader(RE_Render *r,
185 
186  /// Returns an attached shader by the given name.
187  RE_ShaderStage *getShader(const char *shader_name,
189 
190  /// bind vertex shader input 'name' to vertex array index 'vertex_index'.
191  /// Must be called before linking, or a re-link must be done.
192  virtual bool setVertexInput(RE_Render *r,
193  const char *name,
194  int vertex_index);
195 
196  /// bind fragment shader output 'name' to color attachment 'buffer_index'.
197  /// Must be called before linking. Only needed for custom fragment output
198  /// (ie, not gl_FragData[] or gl_FragColor).
199  virtual bool setFragmentOutput(RE_Render *r,
200  const char *name,
201  int buffer_index);
202 
203  /// Links all the shaders together; called implicitly if needed by
204  /// RE_Render::setShader(). Note that linking the shader will clear
205  /// out any uniforms that have been set.
206  virtual bool linkShaders(RE_Render *r, UT_String *messages = nullptr);
207 
208  /// Bumped every time a link is performed (uniforms invalidated, etc).
209  int getLinkSerial() const { return myShaderSerial; }
210 
211  /// Ensures that the shader can run given the current GL state. This mostly
212  /// deals with texture bindings matching what is expected by the shader.
213  /// If this returns false, the shader will not run, and 'messages' can be
214  /// used to fetch the reason.
215  virtual bool validateShader(RE_Render *r, UT_String *messages = nullptr);
216 
217  /// Clears and deletes all the shader objects of a certain type (or all)
218  virtual void clearShaders(RE_Render *r,
220 
221 
222  /// Returns true if this shader program has a shader of 'type' attached.
223  /// type may be a bitmask of shader types, in which case it returns true if
224  /// any of the shader types exist.
225  bool hasShader(RE_ShaderType type) const;
226 
227  // GEOMETRY SHADERS ------------------------------------------------------
228 
229  /// @name Geometry Shaders
230  /// Geometry shaders must be initialized with a cap on how many vertices
231  /// they emit per primitive, as well as the primitives they operate on,
232  /// and the primitives they produce. A vertex and fragment shader MUST be
233  /// used with a geometry shader. A geometry shader will fail if the
234  /// primitives drawn do not match the geometry shader input.
235  /// Input primitives can be points, line variants (lines, line loop, line
236  /// strip) or triangle variants (tris, tristrips, trifans). Quads and polys
237  /// are not supported. The output must be points, linestrip or tristrip.
238  //@{
239 
240  /// Specify geometry shader parameters.
241  virtual bool setGeometryParms(RE_Render *r,
242  int max_vertices,
244  RE_PrimType output);
245 
246  /// @brief Fetch current geometry shader values.
247  /// Return the geometry shader parameters specified by setGeometryParms() or
248  /// the shader itself (via layout(in=triangles), etc).
249  void getGeometryParms(int &maxverts,
250  RE_PrimType &input_type,
251  RE_PrimType &output_type) const
252  {
253  maxverts = myGeoShaderMaxVertices;
254  input_type = myGeoShaderInputPrims;
255  output_type= myGeoShaderOutputPrims;
256  }
257  //@}
258 
259  // TRANSFORM FEEDBACK ----------------------------------------------------
260 
261  /// @name Transform Feedback
262  /// Using transform feedback, it is possible to capture vertex or geometry
263  /// shader outputs and write these values to buffers (RE_VertexArrays).
264  /// Method to add or remove shader outputs should be called before the
265  /// shader is linked, otherwise they will cause a re-link of the shader.
266  /// Note that relinking with clear out any uniform values, so these buffers
267  /// should be set up early.
268  //@{
269 
270  /// @brief Specify an RE_Geometry to collect data from Transform Feedback.
271  /// Specify a geometry object to hold all the arrays for transform feedback.
272  /// You retain ownership of the object unless 'give_ownership' is true.
273  /// If no object is attached when the shader is linked, one will be
274  /// created for you (with ownership maintained by this shader).
275  void addCaptureGeometry(RE_Geometry *object,
276  bool give_ownership = false);
277 
278  /// Return the capture geometry for transform feedback.
279  RE_Geometry *getCaptureGeometry(bool take_ownership = false);
280 
281  /// Specify a generic attribute to capture during transform feedback.
282  bool addFeedbackBuffer(const char *attrib_name);
283 
284  /// Remove a generic attribute from the capture geometry.
285  bool removeFeedbackBuffer(const char *attrib_name);
286 
287  /// Will generate a new owned capture geometry if one isn't present.
288  void setFeedbackBufferSize(int num_vertices);
289 
290  /// Remove all attributes from the capture gemetry.
291  void clearAllFeedbackBuffers();
292 
293  /// True if this shader has transform feedback varyings available.
294  bool hasTransformFeedback() const;
295 
296  /// @brief Fetch a vertex array written to by the shader.
297  /// These are only valid after the shader has been linked for
298  /// transform feedback (usually due to a draw call). Returns a vertex array
299  /// for the named feedback buffer.
300  RE_VertexArray *getFeedbackBuffer(const char *attrib_name) const;
301 
302 
303  /// @brief Check if a feedback buffer will be written by this shader
304  /// Returns true if the attribute is available to be captured. Will return
305  /// false if no buffer was setup to capture the attribute, or the shader
306  /// does not output that attribute.
307  /// This method must be called after the shader is linked.
308  bool isFeedbackBufferUsed(RE_BufferType glattrib,
309  int texlevel = 0) const;
310 
311  /// @brief Check if a feedback buffer will be written by this shader
312  /// Returns true if the attribute is available to be captured. Will return
313  /// false if no buffer was setup to capture the attribute, or the shader
314  /// does not output that attribute.
315  /// This method must be called after the shader is linked.
316  bool isFeedbackBufferUsed(const char *attrib) const;
317 
318  /// @private For RE_Render only.
319  virtual bool bindForTransformFeedback(RE_Render *r);
320 
321  /// @}
322 
323  // SHADING OPTIONS -------------------------------------------------------
324 
325  /// For multisampled framebuffers, this sets the fraction of samples that
326  /// should be shaded by a fragment shader, from 0.0 to 1.0. For example,
327  /// setting this to 0.5 would guarentee _at least_ 4 color samples for a 8x
328  /// multisample buffer. The default is 0.0 (1 color sample, N coverage)
329  /// This only has an effect on GL4 hardware. It is ignored unless
330  /// RE_EXT_SAMPLE_SHADING is supported.
331  /// @{
333  { myShadingRate = SYSclamp(rate,0.0,1.0); }
335  { return SYSmax(myShadingRate,0.0); }
336  bool isSampleShading() const { return myShadingRate >= 0.0; }
337  /// @}
338 
339  /// Disables the shading rate for this shader. The rate set by
340  /// RE_Render::setShaderSampleRate() is used instead.
341  void disableShadingRate() { myShadingRate = -1.0; }
342 
343  // UNIFORMS --------------------------------------------------------------
344 
345  /// @brief Set the value of a uniform.
346  /// Set uniform value(s) for a specific type. False will be returned if the
347  /// shader is not linked (errors?), the given name does not exist, or the
348  /// type does not match the GLSL uniform type.
349  /// @{
350  bool bindInt(RE_Render *r, const UT_StringHolder &name, int v);
351  bool bindFloat(RE_Render *r, const UT_StringHolder &name, float v);
352  bool bindDouble(RE_Render *r, const UT_StringHolder &name, double v);
353 
354  bool bindColor(RE_Render *r, const UT_StringHolder &name, const UT_Color &c);
355  bool bindColor(RE_Render *r, const UT_StringHolder &name, const UT_Color &c,
356  fpreal alpha);
357 
358  bool bindVector(RE_Render *r,const UT_StringHolder &name,const UT_Vector2F &v);
359  bool bindVector(RE_Render *r,const UT_StringHolder &name,const UT_Vector3F &v);
360  bool bindVector(RE_Render *r,const UT_StringHolder &name,const UT_Vector4F &v);
361 
362  bool bindVector(RE_Render *r,const UT_StringHolder &name,const UT_Vector2D &v);
363  bool bindVector(RE_Render *r,const UT_StringHolder &name,const UT_Vector3D &v);
364  bool bindVector(RE_Render *r,const UT_StringHolder &name,const UT_Vector4D &v);
365 
366  bool bindVector(RE_Render *r,const UT_StringHolder &name,const UT_Vector2i &v);
367  bool bindVector(RE_Render *r,const UT_StringHolder &name,const UT_Vector3i &v);
368  bool bindVector(RE_Render *r,const UT_StringHolder &name,const UT_Vector4i &v);
369 
370  bool bindVectors(RE_Render *r, const UT_StringHolder &name,
371  const UT_Vector2FArray &vals);
372  bool bindVectors(RE_Render *r, const UT_StringHolder &name,
373  const UT_Vector3FArray &vals);
374  bool bindVectors(RE_Render *r, const UT_StringHolder &name,
375  const UT_Vector4FArray &vals);
376 
377  bool bindVectors(RE_Render *r, const UT_StringHolder &name,
378  const UT_Vector2DArray &vals);
379  bool bindVectors(RE_Render *r, const UT_StringHolder &name,
380  const UT_Vector3DArray &vals);
381  bool bindVectors(RE_Render *r, const UT_StringHolder &name,
382  const UT_Vector4DArray &vals);
383 
384  bool bindMatrix(RE_Render *r,const UT_StringHolder &name,const UT_Matrix2F &m);
385  bool bindMatrix(RE_Render *r,const UT_StringHolder &name,const UT_Matrix3F &m);
386  bool bindMatrix(RE_Render *r,const UT_StringHolder &name,const UT_Matrix4F &m);
387 
388  bool bindMatrix(RE_Render *r,const UT_StringHolder &name,const UT_Matrix2D &m);
389  bool bindMatrix(RE_Render *r,const UT_StringHolder &name,const UT_Matrix3D &m);
390  bool bindMatrix(RE_Render *r,const UT_StringHolder &name,const UT_Matrix4D &m);
391  /// @}
392 
393  /// Bind a texture map using named textures in TIL_TextureMap. You can also
394  /// bind a texture with its raw OpenGL ID using bindVariableInt().
395  /// @{
396  /// Bind a new texture object using the mapname (filename, op:/coppath)
397  virtual bool bindTextureMap(RE_Render *r,
398  const UT_StringHolder &name,
399  const char *mapname,
400  const char *relative_to) = 0;
401  /// @}
402 
403  /// Return the texture type for the sampler named 'name'.
404  virtual RE_TextureDimension getTextureMapType(RE_Render *r,
405  const UT_StringHolder &name) = 0;
406 
407  /// Return the number of samplers this shader references
408  int getNumTextures() const { return myTextureUniforms.entries(); }
409 
410  /// Return the number of image samplers this shader references (GL4.2
411  /// shader load/store images, not regular texture samplers)
412  int getNumImages() const { return myImageUniforms.entries(); }
413 
414  /// @brief Return the name of a sampler uniform
415  /// Return the texture sampler name of the enumerated samplers at 'tex_index'
416  /// Must be within [0, getNumTextures()-1].
417  const char *getTextureSamplerName(int tex_index) const;
418 
419  /// Return the texture sampler type at 'tex_index' [0, getNumTextures()-1].
420  RE_TextureDimension getTextureSamplerType(int tex_index) const;
421 
422  /// @brief return the texture unit of sampler 'uniform_name'
423  /// Return the texture unit this sampler has been assigned to, or -1 if
424  /// 'uniform_name' does not exist, or is not a sampler. array_index is used
425  /// to identify indices of sampler arrays.
426  int getUniformTextureUnit(const UT_StringHolder &uniform_name,
427  int array_index = 0) const;
428 
429  /// @brief return the image unit of sampler 'uniform_name'
430  /// Return the image unit this image uniform has been assigned to, or -1 if
431  /// 'uniform_name' does not exist, or is not a image uniform. Images are
432  /// different than textures, used by RE_EXT_IMAGE_LOAD_STORE (GL4.2).
433  int getUniformImageUnit(const UT_StringHolder &uniform_name) const;
434 
435  /// Return the uniform type of the uniform named 'uniform_name', or
436  /// RE_UNIFORM_INVALID if it does not exist.
437  RE_UniformType getUniformType(const UT_StringHolder &uniform_name) const;
438 
439 
440  /// Returns the number of active uniforms in this shader.
441  int getNumUniforms() const { return myUniforms.entries(); }
442 
443  /// Returns the name of active uniform 'idx' [0, getNumUniforms()-1]
444  const char *getUniformName(int idx) const;
445 
446  /// Return the GL uniform location of uniform 'idx' [0, getNumUniforms()-1]
447  int getUniformLocation(int idx) const;
448 
449  /// Return the GL uniform type 'idx' [ 0, getNumUniforms()-1]
450  RE_UniformType getUniformType(int idx) const;
451 
452  /// Return the index of the uniform block a uniform is in, -1 for none.
453  int getBlockIndexForUniform(int idx) const;
454 
455 
456  /// @name Setting Uniforms
457  /// These methods set uniform values in a shader. 'name' is the name of the
458  /// uniform in the shader. 'val' is the value that uniform should take.
459  /// 'array_size' is used if the uniform is an array. 'saved_idx' is an
460  /// optimization to speed up subsequent accesses to the uniforms. Initially
461  /// it should be -1.
462  //@{
463 
464  /// Generic Integer scalar
465  virtual bool bindVariableInt(RE_Render *r,
466  const UT_StringHolder &name, const int *val,
467  int array_size=1, int *saved_idx=0)=0;
468  /// Generic Integer vector-2
469  virtual bool bindVariableInt2(RE_Render *r,
470  const UT_StringHolder &name, const int *val,
471  int array_size=1, int *saved_idx=0)=0;
472  /// Generic Integer vector-3
473  virtual bool bindVariableInt3(RE_Render *r,
474  const UT_StringHolder &name, const int *val,
475  int array_size=1, int *saved_idx=0)=0;
476  /// Generic Integer vector-4
477  virtual bool bindVariableInt4(RE_Render *r,
478  const UT_StringHolder &name, const int *val,
479  int array_size=1, int *saved_idx=0)=0;
480 
481  /// 64b Integer handle for bindless texturing
482  virtual bool bindUint64(RE_Render *r,
483  const UT_StringHolder &name, uint64 *val,
484  int array_size = 1, int *saved_idx=0) =0;
485 
486  virtual bool bindVariable1(RE_Render *r,
487  const UT_StringHolder &name, const fpreal32 *val,
488  int array_size=1, int *saved_idx=0)=0;
489  virtual bool bindVariable2(RE_Render *r,
490  const UT_StringHolder &name, const fpreal32 *val,
491  int array_size=1, int *saved_idx=0)=0;
492  virtual bool bindVariable3(RE_Render *r,
493  const UT_StringHolder &name, const fpreal32 *val,
494  int array_size=1, int *saved_idx=0)=0;
495  virtual bool bindVariable4(RE_Render *r,
496  const UT_StringHolder &name, const fpreal32 *val,
497  int array_size=1, int *saved_idx=0)=0;
498 
499  virtual bool bindVariable1(RE_Render *r,
500  const UT_StringHolder &name, const fpreal64 *val,
501  int array_size=1, int *saved_idx=0)=0;
502  virtual bool bindVariable2(RE_Render *r,
503  const UT_StringHolder &name, const fpreal64 *val,
504  int array_size=1, int *saved_idx=0)=0;
505  virtual bool bindVariable3(RE_Render *r,
506  const UT_StringHolder &name, const fpreal64 *val,
507  int array_size=1, int *saved_idx=0)=0;
508  virtual bool bindVariable4(RE_Render *r,
509  const UT_StringHolder &name, const fpreal64 *val,
510  int array_size=1, int *saved_idx=0)=0;
511 
512  virtual bool bindMatrix2(RE_Render *r,
513  const UT_StringHolder &name, const fpreal32 *val,
514  int array_size=1, int *saved_idx=0)=0;
515  virtual bool bindMatrix3(RE_Render *r,
516  const UT_StringHolder &name, const fpreal32 *val,
517  int array_size=1, int *saved_idx=0)=0;
518  virtual bool bindMatrix4(RE_Render *r,
519  const UT_StringHolder &name, const fpreal32 *val,
520  int array_size=1, int *saved_idx=0)=0;
521 
522  virtual bool bindMatrix2(RE_Render *r,
523  const UT_StringHolder &name, const fpreal64 *val,
524  int array_size=1, int *saved_idx=0)=0;
525  virtual bool bindMatrix3(RE_Render *r,
526  const UT_StringHolder &name, const fpreal64 *val,
527  int array_size=1, int *saved_idx=0)=0;
528  virtual bool bindMatrix4(RE_Render *r,
529  const UT_StringHolder &name, const fpreal64 *val,
530  int array_size=1, int *saved_idx=0)=0;
531 
532  /// Sampler buffer bound to a texture. The shader manages the texture
533  /// unit assignment, the binding of the texture to the texture unit, and
534  /// the assingment of the sampler buffer uniform.
535  virtual bool bindTexture(RE_Render *r,
536  const UT_StringHolder &name,
537  RE_Texture *tex,
538  int *saved_idx=0)=0;
539  //@}
540 
541  /// Bind a texture to an image slot for reading, writing, or both.
542  /// Requires RE_EXT_IMAGE_LOAD_STORE (or GL4.2).
543  virtual bool bindImage(RE_Render *r,
544  const UT_StringHolder &name,
545  RE_Texture *image,
546  RE_BufferAccess image_access) = 0;
547 
548  /// Bind a texture array layer, cubemap layer, or a 3D texture slice
549  /// to an image slot for reading, writing, or both.
550  /// Requires RE_EXT_IMAGE_LOAD_STORE (or GL4.2).
551  virtual bool bindImageLayer(RE_Render *r,
552  const UT_StringHolder &name,
553  RE_Texture *image,
554  RE_BufferAccess image_access,
555  int layer) = 0;
556 
557  /// assign a bindless texture to its handle uniform. 'texture' must have
558  /// a texture handle, and uniform_name must refer to a texture handle.
559  bool setTextureHandle(RE_Render *r,
560  const UT_StringHolder &uniform_name,
562  int *saved_idx = 0);
563 
564  /// Remove a bindless handle from the uniform, clearing it to 0 (no tex).
565  bool clearTextureHandle(RE_Render *r,
566  const UT_StringHolder &uniform_name,
567  int *saved_idx = 0);
568 
569  /// @brief Bind an RE_Uniform's values to the corresponding GL uniform
570  /// Bind an RE_Uniform to this shader. If 'uniform' is an array uniform
571  /// then 'index' specifies which array element to bind. 'index' must be
572  /// less than uniform->getSize(). Pass -1 for 'index' (the default) to
573  /// bind all elements of an array; passing -1 for non-arrays is safe and
574  /// is equivalent to passing 0.
575  virtual bool bindUniform(RE_Render *r,
576  const RE_Uniform *uniform, int index = -1);
577 
578  /// returns true if the named uniform exists in the shader.
579  bool hasUniform(const UT_StringHolder &uniform_name) const;
580 
581  /// @brief Queries the use of a builtin uniform
582  /// Returns true if this shader has the specified built-in uniform.
583  /// 'uniform' must be an RE_UniformBuiltIn. Note that certain
584  /// built-in uniforms may only be supported for certain languages.
585  bool hasBuiltInUniform(RE_UniformBuiltIn uniform) const;
586 
587  /// Returns a list of RE_UniformBuiltIn IDs this shader uses.
589  { return myBuiltInUniforms; }
590 
591  /// @brief Bind all builtin uniforms.
592  /// Bind all builtin uniforms this shader uses to their current values in
593  /// RE_Render
594  void bindBuiltIns(RE_Render *r,
595  bool force_update = false);
596 
597  /// Called to invalidate the cached uniform, usually by
598  /// RE_Uniform::unregisterBuiltIn().
599  void invalidateUniform(RE_UniformBuiltIn uniform);
600 
601  /// Prints all the active uniforms plus their values to stream os (or cerr)
602  virtual void printUniforms(RE_Render *r,
603  std::ostream *os = nullptr) const {}
604 
605  // UNIFORM BLOCKS -------------------------------------------------------
606 
607  // RE_EXT_UNIFORM_BUFFER must be supported, or these will always return 0
608  // NOTE: these methods are specifically for swapping blocks in and out for
609  // optimization purposes.
610  // You do not need to use these directly to bind values to uniform
611  // blocks - the bind...() methods will do that automatically.
612  // See RE_UniformBuffer.h for more information.
613 
614  /// Returns the OpenGL maximum number of supported blocks.
615  virtual int getMaxUniformBlocks(RE_Render *) { return 0; }
616 
617  /// Returns the OpenGL maximum size of a uniform block, in bytes.
618  virtual int getMaxUniformBlockSizeB(RE_Render *) { return 0; }
619 
620 
621  /// Returns the number of blocks and specific uniform blocks.
622  virtual int getNumUniformBlocks() const { return 0; }
623 
624  /// Return an attached uniform block by index
625  virtual RE_UniformBlockHandle getUniformBlock(int ) const { return nullptr; }
626 
627  /// Return an attached uniform block by name
628  virtual RE_UniformBlockHandle getUniformBlock(const char *name) const
629  { return nullptr; }
630 
631  /// Returns the block binding point of the named block.
632  virtual int getUniformBlockIndex(const char *name) const
633  { return -1; }
634 
635  /// @brief Attach an RE_UniformBlock to this shader
636  /// Attach a single uniform block to the shader. The shader owns the block
637  /// while attached, and will delete it if the shader is deleted, or may
638  /// delete it if linkShaders() is called and it is no longer in use.
639  /// If you want to cache a block, detach it when you're done with the shader.
640  /// If a block with the same name is already attached to this shader, it will
641  /// be deleted.
642  virtual bool attachUniformBlock(RE_UniformBlock *) { return false; }
643 
644  /// @brief Detach a named RE_UniformBlock from this shader
645  /// Detach a single uniform block by name or pointer. The shader will no
646  /// longer keep a reference to it, and will not delete it when relinking or
647  /// when the shader is deleted.
648  /// Note that a shader must have all blocks attached when executed. Detaching
649  /// a uniform block does not remove the GL binding to the block, it merely
650  /// removes this shader's ownership of the block (for performance reasons)
651  virtual RE_UniformBlockHandle detachUniformBlock(const char *) {return nullptr;}
652 
653  /// Detach a specific RE_UniformBlock from this shader.
654  virtual bool detachUniformBlock(RE_UniformBlock *) { return false; }
655 
656  /// Detaches all blocks referenced by this shader.
657  virtual bool detachAllUniformBlocks() { return false; }
658 
659  /// Uploads attached uniform blocks that are dirty (uniform modifications)
660  /// and ensures they are bound to the shader.
661  virtual bool bindAllUniformBlocks(RE_Render *) { return false; }
662 
663  /// @brief Initialize a uniform block based on this shader's block layout
664  /// Initializes a uniform block with the proper offsets and size for block
665  /// index 'index'. If index is -1, block must have the name of one of the
666  /// uniform blocks in the shader.
667  virtual bool initUniformBlock(RE_Render *r,
668  RE_UniformBlock *block,
669  int index = -1) const
670  { return false; }
671 
672  /// Copies the structure of the named uniform block, and possibly the data.
674  bool data_too) const
675  { return nullptr; }
676 
677  /// Update an offset/name/size information in 'b' with the uniforms for the
678  /// correspondingly-named uniform block in this shader.
679  virtual bool updateUniformBlock(const RE_UniformBlockHandle &b) const
680  { return false; }
681 
682  /// Override the currently bound uniform block without deleting the block
683  /// currently residing at that index. If 'block_index' is -1, the name of
684  /// the block is used to find the block index. Only one override can be
685  /// active at once. Passing nullptr for the block clears the override.
687  int block_index = -1)
688  { return -1; }
689  /// Remove an override block from the shader, by index.
690  virtual void removeOverrideBlock(int ) {}
691  /// Remove a specific override block from the shader.
693  /// Remove all override blocks from the shader.
694  virtual void removeOverrideBlocks()
695  { myLightsId=-1; myLightsSerial=-1; }
696  /// Return the currently bound override block for an index, if any is bound
697  virtual RE_UniformBlockHandle getOverrideBlock(int ) const { return nullptr; }
698 
699  // STORAGE BUFFER BLOCKS -----------------------------------------------
700 
701  /// Create a buffer for buffer block 'name'. If it has an open-ended array,
702  /// array_size specifies the size of that array, othwer it is ignored.
704  int array_size)
705  { return nullptr; }
706 
707  /// Return the block definition for storage block 'name'
709  { return nullptr; }
710 
711  /// Return the storage block bound to 'name' (bound by attachStorageBlock)
713  { return nullptr; }
714 
715  /// Return the raw buffer bound to 'name' (bound by attachStorageBlock)
717  { return nullptr; }
718 
719  /// Attach a shader storage block to the binding point corresponding to its
720  /// name. Shader storage blocks should be created with createStorageBlock().
721  virtual bool attachStorageBlock(const RE_UniformBlockHandle &block)
722  { return false; }
723 
724  /// Detach a shader storage block from the binding point corresponding to
725  /// its name.
726  virtual bool detachStorageBlock(const RE_UniformBlockHandle &block)
727  { return false; }
728 
729  /// Attach a GL buffer to the shader storage block binding point that shares
730  /// its name (getAttributeName()). This method is for simple storage blocks,
731  /// such as "buffer blockname { vec4 color[]; }".
732  virtual bool attachStorageBuffer(RE_VertexArray *raw_buffer)
733  { return false; }
734  /// Detach a GL buffer from the shader storage block binding point that
735  /// shares its name (raw_buffer->getAttributeName()).
736  virtual bool detachStorageBuffer(RE_VertexArray *raw_buffer)
737  { return false; }
738 
739  /// Unbind the block or buffer attached to binding point 'bind_name'.
740  virtual bool detachStorageBinding(const char *bind_name)
741  { return false; }
742 
743  // ATTRIBUTES ----------------------------------------------------------
744 
745  /// Return if the vertex shader has input attributes
746  bool hasShaderAttribs() const
747  { return myAttribNames.entries() > 0; }
748 
749  /// Return the number of vertex shader input attributes
750  int getNumShaderAttribs() const
751  { return myAttribNames.entries(); }
752 
753  /// Return the name of vertex shader input at index 'i'
754  const char *getShaderAttribName(int i) const
755  { return myAttribNames(i); }
756 
757  /// Return the ID of the attribute if it is a known type, or
758  /// RE_GENATTRIB_UNDEF if it is not.
759  RE_GenericAttribID getShaderAttribGenID(int i) const;
760 
761  /// Return the GL attribute location of vertex shader input at index 'i'
762  int getShaderAttribLocation(int i) const
763  { return myAttribLocations(i); }
764 
765  /// Return the type of vertex shader input at index 'i'
767  { return myAttribTypes(i); }
768 
769  /// Return the vector size of vertex shader input at index 'i'
770  int getShaderAttribSize(int i) const
771  { return myAttribSizes(i); }
772 
773  /// Return a string array of all vertex shader input names.
775  { return myAttribNames; }
776 
777  /// Return an int array of all vertex shader input locations.
779  { return myAttribLocations; }
780 
781  /// Return an int array of all vertex shader input sizes.
783  { return myAttribSizes; }
784 
785  /// Returns the name of a vertex shader input at GL location 'loc'
786  const char * getAttribNameForLocation(int loc) const;
787 
788  /// Return the default GL location of generic vertex attribute 't'
790  { return myAttrModeLocs(t); }
791 
792  /// Return the texture sampler unit of a texture buffer object for 't'
794  { return myAttrSamplerLocs(t); }
795 
796  /// Return the number of GL-builtin vertex shader inputs (gl_Vertex, gl_*)
798  { return myBuiltInAttribNames.entries(); }
799  /// Return the name of GL-builtin vertex shader input at index 'i'
800  const char * getBuiltinAttrib(int i) const
801  { return myBuiltInAttribNames(i); }
802 
803  /// Return the number of texture-buffer object attributes
805  { return myBufferAttribNames.entries(); }
806  /// Return the name of texture-buffer object attribute at index 'i'
807  const char * getBufferAttrib(int i) const
808  { return myBufferAttribNames(i); }
810  { return myBufferAttribGenIDs(i); }
811 
812  /// Return the number of texture buffer object samplers in this shader
813  int getNumBufferTextures() const;
814  /// Name of the texture buffer object sampler at index 'i'
815  const char * getBufferTextureName(int i) const;
816  /// Type of the texture buffer object sampler at index 'i' (float, int, uint)
817  RE_GPUType getBufferTextureType(int i) const;
818  /// True if the shader has a texture buffer object sampler named 'name'
819  bool hasBufferTexture(const char *name) const;
820 
821  /// @brief Return information about vertex shader input 'attrib_name'
822  /// Returns the location and size of the named vertex attribute, or false if
823  /// it does not exist in this shader.
824  /// This also includes the GL built-ins, though these will not set location
825  /// type, or size; only return true/false for their presence in the shader.
826  bool getAttribute(const char *attrib_name,
827  int &location,
828  RE_GPUType &type,
829  int &size) const;
830 
831  /// Simpler version of getAttribute() - just returns if the attribute exists
832  bool hasAttribute(const char *attrib_name) const
833  {
834  int loc,size;
835  RE_GPUType t;
836  return getAttribute(attrib_name,loc,t,size);
837  }
838 
839  /// @brief Return information about vertex shader input at 'index'
840  /// Similar to getAttribute(), but the name of the input does not need to be
841  /// known. GL builtins are not supported by this method. index must be
842  /// within [0, getNumAttribs()-1].
843  bool getAttributeIndex(int index,
844  UT_String &name,
845  int &location,
846  RE_GPUType &type,
847  int &size) const;
848 
849  void printAttributes(std::ostream *os = nullptr) const;
850 
851  /// @brief Zero out the constant attribute location given by 'array'
852  /// Bind a constant zero for this array. If data is non-nullptr,
853  /// this data is used, otherwise the current constant data for this array
854  /// is used (default 0's).
855  bool bindZeroConstant(RE_Render *r, RE_VertexArray *array);
856 
857  // TEXTURES ------------------------------------------------------------
858 
859  /// Loads texture maps specified by the RE_TextureMap bindTextureMap()
860  virtual void loadShaderTexMaps(RE_Render *) = 0;
861  virtual void loadBoundTextures(RE_Render *r) = 0;
862  virtual void unloadBoundTextures(RE_Render *r) = 0;
863 
864  /// Returns the number of texture samplers used by shader 'stage'. As
865  /// RE_ShaderType is a bitfield, multiple stages can be queried at once
866  /// and the sum will be returned. Requires RE_EXT_PROGRAM_INTERFACE_QUERY.
867  int getNumTextures(RE_ShaderType stage) const;
868 
869  /// Override texture compression for texture bound via bindTextureMap()
870  // @{
871  RE_TextureCompress getTextureFormat() const { return myTexFormat; }
873  { myTexFormat = format; }
874  // @}
875 
876  /// Limit the resolution of the textures to a max width and height.
877  //@{
878  void limitTextureSize(bool limit, int w, int h);
879  bool limitTextureSize() const { return myLimitTextureSize; }
880 
881  int getMaxTextureWidth() const { return myMaxTexW; }
882  int getMaxTextureHeight() const { return myMaxTexH; }
883  //@}
884 
885  /// Scale by a constant scale factor for textures bound via bindTextureMap()
886  //@{
887  void setTextureScale(float sc) { myTextureScale = sc; }
888  float getTextureScale() const { return myTextureScale; }
889  //@}
890 
891  /// Enable or disable mipmapping for textures bound via bindTextureMap()
892  //@{
893  bool mipMapTextures() const { return myMipMapTexture; }
894  void mipMapTextures(bool yes) { myMipMapTexture = yes; }
895  //@}
896 
897  /// Set anisotropic filtering for textures bound via bindTextureMap()
898  //@{
900  { myTextureAnisotropy = samples; }
902  { return myTextureAnisotropy; }
903  //@}
904 
905  /// Returns the shader source of some or all attached shaders
906  void getShaderSource(RE_Render *r,
907  UT_String &source,
909 
910  /// @brief Prepare this shader for rendering
911  /// Binds uniform buffer objects and all builtin uniforms. Optionally
912  /// re-binds vertex attribute locations in 'obj'.
913  bool prepForDrawing(RE_Render *r,
914  RE_Geometry *obj = nullptr,
915  RE_ElementArray *element = nullptr,
916  RE_VertexState *state = nullptr,
917  unsigned int vertex_stride = 0,
918  RE_OverrideList *atrbover=0,
919  int instance_group = 0);
920 
921  bool cleanupAfterDrawing(RE_Render *r,
922  RE_Geometry *obj=nullptr,
923  RE_VertexState *vstate = nullptr,
924  RE_OverrideList *atrbover=0);
925 
926  /// Cached RE_LightList information (uniform blocks)
927  //@{
928  bool matchesLights(int light_serial, int light_id) const;
929  void setLightID(int light_serial, int light_id, int mask);
930  int getLightMask() const { return myLightMask; }
931  //@}
932 
933  /// Cached RE_Material information
934  bool setupForMaterial(const RE_Material *mat) const
935  { return (mat ==myLastMaterial); }
936  void setForMaterial(const RE_Material *mat)
937  { myLastMaterial = mat; }
938 
939  /// COMPUTE
940  virtual bool runCompute(RE_Render *r,
941  int num_wg_x,
942  int num_wg_y = 1,
943  int num_wg_z = 1);
944  virtual bool runCompute(RE_Render *r,
945  RE_VertexArray *wg_buffer, int offset);
946 
947  /// @brief Fetch a shader based on GL program id
948  /// Returns the RE_Shader object with the specified program object if it
949  /// exists; otherwise, returns nullptr.
950  static RE_Shader *getShader(int program_object);
951 
952  // Loads only the source from the Houdini OGL shader path. Returns true if
953  // the file was successfully loaded.
954  static bool loadFile(const char *filename,
955  const char *defines,
957  int &codeversion,
958  UT_String *messages,
959  int default_code_version = 0,
960  bool loadingInclude = false);
961 protected:
962  virtual void removeTextureRefs(RE_OGLTexture *) {}
963 
964  // Get the index of an attribute or uniform based on its name. The
965  // returned index can be used to look up corresponding elements in
966  // the myAttrib*s arrays or the myUniform*s arrays, respectively.
967  virtual int getAttributeIndex(const char *name) const;
968  int getUniformIndex(const UT_StringHolder &name) const;
969 
970  // Creates/destroys a mapping from the specified program id to this shader
971  // in the shader table.
972  static void registerProgram(RE_Shader *shader, int program_object);
973 
974  // Loads any built-in Houdini shaders that loadShader() noted if
975  // useHoudiniLibraryFuncs() is on.
976  bool loadHoudiniLibraryFuncs(RE_Render *r, UT_String *msg);
977 
978  const char **getTransformFeedbackAttribs(int &num_attribs);
979 
980 
981  static int loadProgramFile(RE_Render *r,
982  const char *file,
983  const char *basepath,
984  UT_String &prog_name,
985  UT_StringArray &files,
986  UT_StringArray &files_names,
987  UT_StringArray &inputs,
988  UT_StringArray &outputs,
989  UT_StringArray &defines,
990  int &codeversion,
991  fpreal &shading_rate,
992  bool &use_attrib_map,
993  bool &explicit_attrib_map,
994  int max_version,
995  RE_ShaderLanguage &shader_lang,
996  UT_String *msg);
997 
998  static RE_Texture * getDefaultTBO(RE_Render *r);
999 
1001  {
1002  re_NeededLibFuncs(const char *file, const char *func, RE_ShaderType s)
1003  : myFile(file), myFunc(func), myStage(s), myLoaded(false) {}
1007  bool myLoaded;
1008  };
1009  static void scanSourceForFuncs(const char *line,
1010  RE_ShaderType shtype,
1012  re_LibFunc *library,
1013  int glmajor,
1014  int glminor);
1015  static bool getCodeBlock(const char *block_name,
1016  UT_StringHolder &block_source);
1017 
1018 
1028  int myMaxTexW, myMaxTexH;
1032 
1039 
1041 
1045 
1055 
1064 
1065  int myBuiltInMap[ RE_UNIFORM_BUILT_IN__count ];
1068 
1071 
1074 
1078 
1082 
1088 
1089  UT_IntArray myDrawBoundTextures; // scope: single draw.
1092 
1093 public:
1094  // These are called by RE_Render. Use r->pushShader() or r->bindShader()
1095  // or r->popShader() instead
1096  // This should set the shader as current.
1097  virtual void applyShader(RE_Render *r, bool update_re = true) = 0;
1098  // This should do a cleanup and unset any current shader.
1099  virtual void removeShader(RE_Render *r, bool update_re = true) = 0;
1100  // This should cleanup after the shader, but leave it set as current.
1101  virtual void cleanup(RE_Render *r) = 0;
1102 
1103  // public only for RE_OGLRender.
1104  static void unregisterProgram(int program_object);
1105  static void removeAllTextureRefs(RE_OGLTexture *);
1106 };
1107 
1108 
1110 {
1111 public:
1113  const char *name = 0);
1114  virtual ~RE_ShaderStage();
1115 
1116  /// Returns the amount of main memory (NOT graphics memory!)
1117  /// owned by this RE_ShaderStage.
1118  virtual int64 getMemoryUsage(bool inclusive) const
1119  {
1120  int64 mem = inclusive ? sizeof(*this) : 0;
1121  mem += myName.getMemoryUsage(false);
1122  mem += myFilename.getMemoryUsage(false);
1123  return mem;
1124  }
1125 
1126  const char * getName() const { return myName; }
1127  RE_ShaderType getShaderType() const { return myType; }
1128 
1129  const UT_String &getFilename() const { return myFilename; }
1130  void setFilename(const char * fname)
1131  { myFilename.harden(fname); }
1132 
1133  // This maintains an internal cache of shader source code; once the shader
1134  // is loaded, the cached source is used.
1135  bool loadShader(RE_Render *r,
1136  const char *filename,
1137  UT_String *messages = nullptr,
1138  bool dump_source_on_error = true,
1139  const char *defines = nullptr,
1140  int default_code_version
1142 
1143  virtual bool setSource(RE_Render *r,
1144  const char *source_code,
1145  int version,
1146  UT_String *messages = nullptr,
1147  bool dump_source_on_error = true) = 0;
1148  virtual bool getSource(RE_Render *r, UT_String &source);
1149 
1150  void setID(int id) { myID = id; }
1151  int getID() const { return myID; }
1152 
1153  // Returns the version of the shader (100,110,120,130,140,150,330,400,410).
1154  int getCodeVersion() const { return myCodeVersion; }
1155 protected:
1156  void setCodeVersion(int v) { myCodeVersion=v; }
1157 private:
1158 
1159  UT_String myName;
1160  UT_String myFilename;
1161  RE_ShaderType myType;
1162  int myID;
1163  int myCodeVersion;
1164 };
1165 
1166 class RE_UniformData // private to RE - no RE_API
1167 {
1168 public:
1171  arraysize(0), topsize(0), topstride(0),
1172  uniformblock(-1), dataoffset(0),
1173  location(-1), tex_unit(-1), image_unit(-1),
1174  value_cached(false)
1175  {}
1176 
1178  : name(data.name),
1179  type(data.type),
1180  arraysize(data.arraysize),
1181  topsize(data.topsize),
1182  topstride(data.topstride),
1183  uniformblock(data.uniformblock),
1184  dataoffset(data.dataoffset),
1185  location(data.location),
1186  tex_unit(data.tex_unit),
1187  image_unit(data.image_unit),
1188  value_cached(false)
1189  {}
1190 
1191  bool typeMatch(const RE_UniformData *data) const
1192  {
1193  return (type == data->type &&
1194  arraysize == data->arraysize &&
1195  dataoffset == data->dataoffset);
1196  }
1197 
1198  /// Returns the amount of main memory (NOT graphics memory!)
1199  /// owned by this RE_UniformData.
1200  int64 getMemoryUsage(bool inclusive) const
1201  {
1202  int64 mem = inclusive ? sizeof(*this) : 0;
1203  mem += name.getMemoryUsage(false);
1204  return mem;
1205  }
1206 
1207  enum { cached_data_size = sizeof(UT_Matrix4D) };
1208 
1211  int arraysize; // array size of member element, ie, int[4]
1212  int topsize; // array size of buffer variable
1213  int topstride; // array stride of buffer variable
1217  int tex_unit; // for normal textures
1218  int image_unit; // for image-load-store images
1221 };
1222 
1223 /// --- Inlines
1224 
1225 inline bool
1227 {
1228  return bindVariableInt(r, name, &v);
1229 }
1230 
1231 inline bool
1233 {
1234  return bindVariable1(r, name, &v);
1235 }
1236 
1237 inline bool
1239 {
1240  return bindVariable1(r, name, &v);
1241 }
1242 
1243 inline bool
1245 {
1246  return bindVariable2(r, name, v.data());
1247 }
1248 
1249 inline bool
1251 {
1252  fpreal32 col[3];
1253 
1254  c.getRGB(col, col+1, col+2);
1255  return bindVariable3(r, name, col);
1256 }
1257 
1258 inline bool
1260  fpreal alpha)
1261 {
1262  fpreal32 col[4];
1263 
1264  c.getRGB(col, col+1, col+2);
1265  col[3] = alpha;
1266  return bindVariable4(r, name, col);
1267 }
1268 
1269 
1270 inline bool
1272 {
1273  return bindVariable3(r, name, v.data());
1274 }
1275 
1276 inline bool
1278 {
1279  return bindVariable4(r, name, v.data());
1280 }
1281 
1282 inline bool
1284 {
1285  return bindVariable2(r, name, v.data());
1286 }
1287 
1288 inline bool
1290 {
1291  return bindVariable3(r, name, v.data());
1292 }
1293 
1294 inline bool
1296 {
1297  return bindVariable4(r, name, v.data());
1298 }
1299 
1300 
1301 inline bool
1303 {
1304  return bindVariableInt2(r, name, v.data());
1305 }
1306 
1307 inline bool
1309 {
1310  return bindVariableInt3(r, name, v.data());
1311 }
1312 
1313 inline bool
1315 {
1316  return bindVariableInt4(r, name, v.data());
1317 }
1318 
1319 inline bool
1321  const UT_StringHolder &name,
1322  const UT_Vector2FArray &vals)
1323 {
1324  return bindVariable2(r, name, vals(0).data(), vals.entries());
1325 }
1326 
1327 
1328 inline bool
1330  const UT_Vector4FArray &vals)
1331 {
1332  return bindVariable4(r, name, vals(0).data(), vals.entries());
1333 }
1334 
1335 inline bool
1337  const UT_Vector2DArray &vals)
1338 {
1339  return bindVariable2(r, name, vals(0).data(), vals.entries());
1340 }
1341 
1342 inline bool
1344  const UT_Vector4DArray &vals)
1345 {
1346  return bindVariable4(r, name, vals(0).data(), vals.entries());
1347 }
1348 
1349 inline bool
1351 {
1352  return bindMatrix2(r, name, m.data());
1353 }
1354 
1355 inline bool
1357 {
1358  return bindMatrix3(r, name, m.data());
1359 }
1360 
1361 inline bool
1363 {
1364  return bindMatrix4(r, name, m.data());
1365 }
1366 
1367 inline bool
1369 {
1370  return bindMatrix2(r, name, m.data());
1371 }
1372 
1373 inline bool
1375 {
1376  return bindMatrix3(r, name, m.data());
1377 }
1378 
1379 inline bool
1381 {
1382  return bindMatrix4(r, name, m.data());
1383 }
1384 
1385 inline RE_GenericAttribID
1387 {
1388  return myAttribGenIDs(i);
1389 }
1390 
1391 #endif
GLdouble s
Definition: glew.h:1390
const char * getBuiltinAttrib(int i) const
Return the name of GL-builtin vertex shader input at index 'i'.
Definition: RE_Shader.h:800
int getCodeVersion() const
Definition: RE_Shader.h:1154
SYS_FORCE_INLINE constexpr const T * data() const noexcept
const UT_String & getFilename() const
Definition: RE_Shader.h:1129
#define SYSmax(a, b)
Definition: SYS_Math.h:1447
virtual RE_UniformBlockHandle copyUniformBlock(const char *name, bool data_too) const
Copies the structure of the named uniform block, and possibly the data.
Definition: RE_Shader.h:673
virtual bool updateUniformBlock(const RE_UniformBlockHandle &b) const
Definition: RE_Shader.h:679
RE_UniformData(const RE_UniformData &data)
Definition: RE_Shader.h:1177
UT_Matrix4T< fpreal64 > UT_Matrix4D
GLenum GLenum GLenum input
Definition: glew.h:13879
bool hasShaderAttribs() const
Return if the vertex shader has input attributes.
Definition: RE_Shader.h:746
RE_GPUType getShaderAttribType(int i) const
Return the type of vertex shader input at index 'i'.
Definition: RE_Shader.h:766
int myProgramObject
Definition: RE_Shader.h:1021
virtual int getSource()
GLuint id
Definition: glew.h:1679
int getProgram() const
Raw GL id of the shader.
Definition: RE_Shader.h:121
GT_API const UT_StringHolder filename
UT_IntArray myAttribSizes
Definition: RE_Shader.h:1053
GLsizeiptr size
Definition: glew.h:1681
GLenum src
Definition: glew.h:2410
UT_String myProgramName
Definition: RE_Shader.h:1019
char cached_data[cached_data_size]
Definition: RE_Shader.h:1220
GLuint const GLchar * name
Definition: glew.h:1814
UT_ValArray< RE_ShaderStage * > myTessEvalShaders
Definition: RE_Shader.h:1035
void setID(int id)
Definition: RE_Shader.h:1150
GLclampf GLclampf GLclampf alpha
Definition: glew.h:1520
virtual int getMaxUniformBlockSizeB(RE_Render *)
Returns the OpenGL maximum size of a uniform block, in bytes.
Definition: RE_Shader.h:618
virtual int64 getMemoryUsage(bool inclusive) const
Definition: RE_Shader.h:1118
virtual RE_UniformBlockHandle createStorageBlock(const char *name, int array_size)
Definition: RE_Shader.h:703
#define RE_API
Definition: RE_API.h:10
bool isSampleShading() const
Definition: RE_Shader.h:336
UT_String myExtraDefines
Definition: RE_Shader.h:1020
UT_StringList myBufferAttribNames
Definition: RE_Shader.h:1048
void getRGB(float *r, float *g, float *b) const
Definition: UT_Color.h:101
virtual void printUniforms(RE_Render *r, std::ostream *os=nullptr) const
Prints all the active uniforms plus their values to stream os (or cerr)
Definition: RE_Shader.h:602
GLuint index
Definition: glew.h:1814
bool setupForMaterial(const RE_Material *mat) const
Cached RE_Material information.
Definition: RE_Shader.h:934
UT_Array< RE_GenericAttribID > myBufferAttribGenIDs
Definition: RE_Shader.h:1049
void setFilename(const char *fname)
Definition: RE_Shader.h:1130
GLuint const GLfloat * val
Definition: glew.h:2794
UT_IntArray myImageUniforms
Definition: RE_Shader.h:1081
UT_IntrusivePtr< RE_UniformBlock > RE_UniformBlockHandle
Definition: RE_Shader.h:56
int myLightMask
Definition: RE_Shader.h:1086
virtual int getUniformBlockIndex(const char *name) const
Returns the block binding point of the named block.
Definition: RE_Shader.h:632
const UT_IntArray & getShaderAttribSizes() const
Return an int array of all vertex shader input sizes.
Definition: RE_Shader.h:782
RE_TextureCompress myTexFormat
Definition: RE_Shader.h:1026
A collection of vertex arrays defining a geometry object. This class acts as a wrapper around multipl...
Definition: RE_Geometry.h:53
int getMaxTextureWidth() const
Limit the resolution of the textures to a max width and height.
Definition: RE_Shader.h:881
GLsizei GLsizei GLchar * source
Definition: glew.h:1832
GLint limit
Definition: glew.h:12988
virtual bool detachStorageBinding(const char *bind_name)
Unbind the block or buffer attached to binding point 'bind_name'.
Definition: RE_Shader.h:740
virtual void removeOverrideBlock(RE_UniformBlock *b)
Remove a specific override block from the shader.
Definition: RE_Shader.h:692
virtual RE_UniformBlockHandle getStorageBlock(const char *name)
Return the block definition for storage block 'name'.
Definition: RE_Shader.h:708
fpreal myShadingRate
Definition: RE_Shader.h:1079
void setCodeVersion(int v)
Definition: RE_Shader.h:1156
int myLightsSerial
Definition: RE_Shader.h:1084
bool bindColor(RE_Render *r, const UT_StringHolder &name, const UT_Color &c)
Set the value of a uniform. Set uniform value(s) for a specific type. False will be returned if the s...
Definition: RE_Shader.h:1250
const GLdouble * m
Definition: glew.h:9124
const GLdouble * v
Definition: glew.h:1391
UT_StringList myAttribNames
Definition: RE_Shader.h:1047
GLhandleARB obj
Definition: glew.h:6236
RE_UniformType
Definition: RE_Uniform.h:137
bool bindMatrix(RE_Render *r, const UT_StringHolder &name, const UT_Matrix2F &m)
Set the value of a uniform. Set uniform value(s) for a specific type. False will be returned if the s...
Definition: RE_Shader.h:1350
GLuint shader
Definition: glew.h:1813
GLenum GLint GLuint mask
Definition: glew.h:1845
virtual RE_UniformBlockHandle getStorageBlockBinding(const char *name)
Return the storage block bound to 'name' (bound by attachStorageBlock)
Definition: RE_Shader.h:712
RE_TextureDimension
bool typeMatch(const RE_UniformData *data) const
Definition: RE_Shader.h:1191
virtual RE_UniformBlockHandle getOverrideBlock(int) const
Return the currently bound override block for an index, if any is bound.
Definition: RE_Shader.h:697
const T * data(void) const
Return the raw matrix data.
Definition: UT_Matrix2.h:397
const char * getName() const
Readable name of the shader.
Definition: RE_Shader.h:115
UT_ValArray< re_VertexName * > myTransformFeedbackVertices
Definition: RE_Shader.h:1077
void disableShadingRate()
Definition: RE_Shader.h:341
GLenum GLsizei GLenum GLenum const void * image
Definition: glew.h:4943
bool bindInt(RE_Render *r, const UT_StringHolder &name, int v)
Set the value of a uniform. Set uniform value(s) for a specific type. False will be returned if the s...
Definition: RE_Shader.h:1226
void setAnisotropySamples(int samples)
Set anisotropic filtering for textures bound via bindTextureMap()
Definition: RE_Shader.h:899
int getMaxTextureHeight() const
Limit the resolution of the textures to a max width and height.
Definition: RE_Shader.h:882
UT_ValArray< RE_UniformData * > myUniforms
Definition: RE_Shader.h:1056
int getCodeVersion() const
Returns the version of the shader (110,120,130,140,150,330,400,410).
Definition: RE_Shader.h:127
RE_GenericAttribID getShaderAttribGenID(int i) const
Definition: RE_Shader.h:1386
const T * data(void) const
Return the raw matrix data.
Definition: UT_Matrix4.h:1077
UT_Array< RE_GenericAttribID > myAttribGenIDs
Definition: RE_Shader.h:1051
UT_Array< re_NeededLibFuncs > * myNeededLibraryFuncs
Definition: RE_Shader.h:1072
virtual bool initUniformBlock(RE_Render *r, RE_UniformBlock *block, int index=-1) const
Initialize a uniform block based on this shader's block layout Initializes a uniform block with the p...
Definition: RE_Shader.h:667
bool hasAttribute(const char *attrib_name) const
Simpler version of getAttribute() - just returns if the attribute exists.
Definition: RE_Shader.h:832
GLsizei GLenum GLenum * types
Definition: glew.h:3954
re_NeededLibFuncs(const char *file, const char *func, RE_ShaderType s)
Definition: RE_Shader.h:1002
RE_VertexMap * myVertexMap
Definition: RE_Shader.h:1069
int getNumImages() const
Definition: RE_Shader.h:412
RE_ShaderType
Definition: RE_Types.h:228
virtual RE_UniformBlockHandle getUniformBlock(int) const
Return an attached uniform block by index.
Definition: RE_Shader.h:625
int myCodeVersion
Definition: RE_Shader.h:1073
RE_GenericAttribID
Definition: RE_Types.h:323
UT_StringList myBuiltInAttribNames
Definition: RE_Shader.h:1046
long long int64
Definition: SYS_Types.h:111
RE_GPUType
Definition: RE_Types.h:44
RE_Geometry * myTransformFeedbackGeometry
Definition: RE_Shader.h:1075
virtual int getNumUniformBlocks() const
Returns the number of blocks and specific uniform blocks.
Definition: RE_Shader.h:622
RE_BufferType
Definition: RE_Types.h:264
int getNumTextures() const
Return the number of samplers this shader references.
Definition: RE_Shader.h:408
UT_IntArray myAttrModeLocs
Definition: RE_Shader.h:1062
unsigned long long uint64
Definition: SYS_Types.h:112
UT_IntArray myNumTexturesPerStage
Definition: RE_Shader.h:1040
UT_IntArray myDrawBoundTextures
Definition: RE_Shader.h:1089
virtual bool detachAllUniformBlocks()
Detaches all blocks referenced by this shader.
Definition: RE_Shader.h:657
int getShaderAttribSize(int i) const
Return the vector size of vertex shader input at index 'i'.
Definition: RE_Shader.h:770
virtual RE_UniformBlockHandle detachUniformBlock(const char *)
Detach a named RE_UniformBlock from this shader Detach a single uniform block by name or pointer...
Definition: RE_Shader.h:651
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
virtual int setSource(int source)
virtual bool detachStorageBuffer(RE_VertexArray *raw_buffer)
Definition: RE_Shader.h:736
bool mipMapTextures() const
Enable or disable mipmapping for textures bound via bindTextureMap()
Definition: RE_Shader.h:893
bool myLinkedFlag
Definition: RE_Shader.h:1022
UT_ValArray< RE_ShaderStage * > myGeometryShaders
Definition: RE_Shader.h:1036
double fpreal64
Definition: SYS_Types.h:196
bool bindDouble(RE_Render *r, const UT_StringHolder &name, double v)
Set the value of a uniform. Set uniform value(s) for a specific type. False will be returned if the s...
Definition: RE_Shader.h:1238
UT_ValArray< const RE_Uniform * > myBuiltInLastUniform
Definition: RE_Shader.h:1059
UT_IntArray myAttribLocations
Definition: RE_Shader.h:1050
int64 getMemoryUsage(bool inclusive) const
Definition: RE_Shader.h:1200
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1890
UT_Vector3T< T > SYSclamp(const UT_Vector3T< T > &v, const UT_Vector3T< T > &min, const UT_Vector3T< T > &max)
Definition: UT_Vector3.h:820
int getAttributeModeLoc(RE_GenericAttribID t) const
Return the default GL location of generic vertex attribute 't'.
Definition: RE_Shader.h:789
UT_ValArray< RE_ShaderStage * > myTessControlShaders
Definition: RE_Shader.h:1034
bool bindVectors(RE_Render *r, const UT_StringHolder &name, const UT_Vector2FArray &vals)
Set the value of a uniform. Set uniform value(s) for a specific type. False will be returned if the s...
Definition: RE_Shader.h:1320
UT_SymbolMap< int > myUniformBlockTable
Definition: RE_Shader.h:1061
const char * getShaderAttribName(int i) const
Return the name of vertex shader input at index 'i'.
Definition: RE_Shader.h:754
const GLfloat * c
Definition: glew.h:16296
int getNumUniforms() const
Returns the number of active uniforms in this shader.
Definition: RE_Shader.h:441
bool bindFloat(RE_Render *r, const UT_StringHolder &name, float v)
Set the value of a uniform. Set uniform value(s) for a specific type. False will be returned if the s...
Definition: RE_Shader.h:1232
bool isLinked() const
True if the shader has been successfully linked.
Definition: RE_Shader.h:124
UT_IntArray myAttrSamplerLocs
Definition: RE_Shader.h:1063
virtual int overrideUniformBlock(RE_UniformBlock *block, int block_index=-1)
Definition: RE_Shader.h:686
void setForMaterial(const RE_Material *mat)
Definition: RE_Shader.h:936
int myTextureAnisotropy
Definition: RE_Shader.h:1031
RE_UniformBuiltIn
Definition: RE_Uniform.h:28
virtual bool detachUniformBlock(RE_UniformBlock *)
Detach a specific RE_UniformBlock from this shader.
Definition: RE_Shader.h:654
bool myMipMapTexture
Definition: RE_Shader.h:1030
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:8011
virtual bool attachUniformBlock(RE_UniformBlock *)
Attach an RE_UniformBlock to this shader Attach a single uniform block to the shader. The shader owns the block while attached, and will delete it if the shader is deleted, or may delete it if linkShaders() is called and it is no longer in use. If you want to cache a block, detach it when you're done with the shader. If a block with the same name is already attached to this shader, it will be deleted.
Definition: RE_Shader.h:642
int64 myRenderSerial
Definition: RE_Shader.h:1067
int getAnisotropySamples() const
Set anisotropic filtering for textures bound via bindTextureMap()
Definition: RE_Shader.h:901
virtual bool attachStorageBlock(const RE_UniformBlockHandle &block)
Definition: RE_Shader.h:721
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
const UT_IntArray & getBuiltInUniformList() const
Returns a list of RE_UniformBuiltIn IDs this shader uses.
Definition: RE_Shader.h:588
RE_ShaderType getShaderType() const
Definition: RE_Shader.h:1127
int myMaxTexW
Definition: RE_Shader.h:1028
UT_ValArray< RE_ShaderStage * > myVertexShaders
Definition: RE_Shader.h:1033
int getNumBufferAttribs() const
Return the number of texture-buffer object attributes.
Definition: RE_Shader.h:804
int getUniqueID() const
Definition: RE_Shader.h:71
int myUniqueID
Definition: RE_Shader.h:1087
GT_API const UT_StringHolder version
void setTextureScale(float sc)
Scale by a constant scale factor for textures bound via bindTextureMap()
Definition: RE_Shader.h:887
int getLightMask() const
Cached RE_LightList information (uniform blocks)
Definition: RE_Shader.h:930
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:453
void setTextureFormat(RE_TextureCompress format)
Override texture compression for texture bound via bindTextureMap()
Definition: RE_Shader.h:872
fpreal getShadingRate() const
Definition: RE_Shader.h:334
GLdouble GLdouble GLdouble b
Definition: glew.h:9122
virtual RE_VertexArray * getStorageBufferBinding(const char *name)
Return the raw buffer bound to 'name' (bound by attachStorageBlock)
Definition: RE_Shader.h:716
RE_TextureCompress getTextureFormat() const
Override texture compression for texture bound via bindTextureMap()
Definition: RE_Shader.h:871
int getID() const
Definition: RE_Shader.h:1151
UT_Array< RE_GPUType > myAttribTypes
Definition: RE_Shader.h:1052
const UT_StringList & getShaderAttribNames() const
Return a string array of all vertex shader input names.
Definition: RE_Shader.h:774
double fpreal
Definition: SYS_Types.h:276
GLenum func
Definition: glcorearb.h:782
RE_BufferAccess
Definition: RE_Types.h:293
void setShadingRate(fpreal rate)
Definition: RE_Shader.h:332
bool myRegisterProgram
Definition: RE_Shader.h:1025
UT_IntArray myBufferTextures
Definition: RE_Shader.h:1070
virtual void removeOverrideBlock(int)
Remove an override block from the shader, by index.
Definition: RE_Shader.h:690
int getAttributeSamplerLoc(RE_GenericAttribID t) const
Return the texture sampler unit of a texture buffer object for 't'.
Definition: RE_Shader.h:793
float myTextureScale
Definition: RE_Shader.h:1029
bool myLimitTextureSize
Definition: RE_Shader.h:1027
virtual int getMaxUniformBlocks(RE_Render *)
Returns the OpenGL maximum number of supported blocks.
Definition: RE_Shader.h:615
const T * data(void) const
Return the raw matrix data.
Definition: UT_Matrix3.h:1124
virtual RE_UniformBlockHandle getUniformBlock(const char *name) const
Return an attached uniform block by name.
Definition: RE_Shader.h:628
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1254
const char * getBufferAttrib(int i) const
Return the name of texture-buffer object attribute at index 'i'.
Definition: RE_Shader.h:807
GLenum GLuint texture
Definition: glew.h:2361
virtual void removeOverrideBlocks()
Remove all override blocks from the shader.
Definition: RE_Shader.h:694
UT_StringHolder name
Definition: RE_Shader.h:1209
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
GLint GLboolean GLint layer
Definition: glew.h:3601
UT_ValArray< RE_ShaderStage * > myComputeShaders
Definition: RE_Shader.h:1038
const char * getDefines() const
Any define commands used to alter this shader.
Definition: RE_Shader.h:118
UT_SymbolMap< int > myUniformTable
Definition: RE_Shader.h:1060
int myShaderSerial
Definition: RE_Shader.h:1083
bool myTransformFeedbackGeometryOwned
Definition: RE_Shader.h:1076
RE_UniformType type
Definition: RE_Shader.h:1210
UT_IntArray myBuiltInVersion
Definition: RE_Shader.h:1058
const UT_IntArray & getShaderAttribLocations() const
Return an int array of all vertex shader input locations.
Definition: RE_Shader.h:778
void mipMapTextures(bool yes)
Enable or disable mipmapping for textures bound via bindTextureMap()
Definition: RE_Shader.h:894
float getTextureScale() const
Scale by a constant scale factor for textures bound via bindTextureMap()
Definition: RE_Shader.h:888
bool limitTextureSize() const
Limit the resolution of the textures to a max width and height.
Definition: RE_Shader.h:879
virtual bool bindAllUniformBlocks(RE_Render *)
Definition: RE_Shader.h:661
int64 myBuiltInUniformSerial
Definition: RE_Shader.h:1066
RE_PrimType myGeoShaderOutputPrims
Definition: RE_Shader.h:1044
GLenum array
Definition: glew.h:9066
GLint location
Definition: glew.h:1835
bool myUseExplicitAttribMap
Definition: RE_Shader.h:1024
UT_StringList myFeedbackNames
Definition: RE_Shader.h:1054
bool bindVector(RE_Render *r, const UT_StringHolder &name, const UT_Vector2F &v)
Set the value of a uniform. Set uniform value(s) for a specific type. False will be returned if the s...
Definition: RE_Shader.h:1244
const char * getName() const
Definition: RE_Shader.h:1126
void getGeometryParms(int &maxverts, RE_PrimType &input_type, RE_PrimType &output_type) const
Fetch current geometry shader values. Return the geometry shader parameters specified by setGeometryP...
Definition: RE_Shader.h:249
UT_ValArray< RE_ShaderStage * > myFragmentShaders
Definition: RE_Shader.h:1037
RE_PrimType myGeoShaderInputPrims
Definition: RE_Shader.h:1043
virtual bool detachStorageBlock(const RE_UniformBlockHandle &block)
Definition: RE_Shader.h:726
int getNumShaderAttribs() const
Return the number of vertex shader input attributes.
Definition: RE_Shader.h:750
const RE_Material * myLastMaterial
Definition: RE_Shader.h:1091
GLsizei samples
Definition: glew.h:2998
RE_TextureCompress
bool myUseDefaultAttribMap
Definition: RE_Shader.h:1023
int getLinkSerial() const
Bumped every time a link is performed (uniforms invalidated, etc).
Definition: RE_Shader.h:209
RE_ShaderLanguage
Definition: RE_Types.h:242
virtual bool attachStorageBuffer(RE_VertexArray *raw_buffer)
Definition: RE_Shader.h:732
int getShaderAttribLocation(int i) const
Return the GL attribute location of vertex shader input at index 'i'.
Definition: RE_Shader.h:762
RE_PrimType
Definition: RE_Types.h:193
int getNumBuiltinAttribs() const
Return the number of GL-builtin vertex shader inputs (gl_Vertex, gl_*)
Definition: RE_Shader.h:797
float fpreal32
Definition: SYS_Types.h:195
GLboolean enable
Definition: glew.h:2745
GLdouble GLdouble t
Definition: glew.h:1398
virtual void removeTextureRefs(RE_OGLTexture *)
Definition: RE_Shader.h:962
UT_IntArray myTextureUniforms
Definition: RE_Shader.h:1080
UT_WorkBuffer myWorkBuffer
Definition: RE_Shader.h:1090
int myGeoShaderMaxVertices
Definition: RE_Shader.h:1042
GLintptr offset
Definition: glew.h:1682
UT_IntArray myBuiltInUniforms
Definition: RE_Shader.h:1057
RE_GenericAttribID getBufferAttribID(int i) const
Definition: RE_Shader.h:809
int myLightsId
Definition: RE_Shader.h:1085