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