HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RE_Material.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: Render Library (C++)
7  *
8  * COMMENTS: Material definition for lit geometry
9  *
10  */
11 
12 #ifndef __RE_Material_h__
13 #define __RE_Material_h__
14 
15 #include "RE_API.h"
16 #include <DEP/DEP_MicroNode.h>
17 #include <SYS/SYS_AtomicInt.h>
18 #include <UT/UT_Color.h>
19 #include <UT/UT_IntArray.h>
20 #include <UT/UT_NonCopyable.h>
21 #include <UT/UT_Options.h>
22 #include <UT/UT_Rect.h>
23 #include <UT/UT_String.h>
24 #include <UT/UT_UniquePtr.h>
25 #include <UT/UT_Vector3.h>
26 #include <UT/UT_ValArray.h>
27 #include <UT/UT_XformOrder.h>
28 #include "RE_Types.h"
29 #include "RE_TextureTypes.h"
30 #include "RE_TextureMap.h"
31 #include "RE_UniformBlock.h"
32 
33 class RE_OGLRender;
34 class RE_OGLTexture;
35 class RE_Render;
36 class RE_Shader;
37 class RE_Texture2DMap;
38 class RE_TextureMap;
39 class RE_TextureCubeMap;
40 
42 {
43 public:
44  explicit RE_Material(const char *materialName);
45 
46  const UT_StringHolder &
47  getMaterialName() const { return myMaterialName; }
48  int getUniqueId() const { return myUniqueId; }
49 
50  int64 getVersion() const { return myVersion; }
51  void setVersion(int64 v) { myVersion = v; }
52 
53  void dirty(bool dirty = true) { myDirtyFlag = dirty; }
54  bool isDirty(fpreal time) const
55  {
56  if(myDirtyFlag || myDepNode.isDirty())
57  return true;
58  if(myTimeDep && !SYSisEqual(time, myTime))
59  return true;
60  return false;
61  }
62 
63  void setTimeDependent(bool timedep, fpreal t = 0.0)
64  {
65  myTimeDep = timedep;
66  myTime = timedep ? t : 0.0;
67  }
68  bool isTimeDependent() const { return myTimeDep; }
69  fpreal getCurrentTime() const { return myTime; }
70 
71  void incref() { myRefCount.add(1); }
72  void decref()
73  {
74  if (!myRefCount.add(-1))
75  delete this;
76  }
77 
78  void setNodePath(UT_StringHolder path, int node_id);
79  const UT_StringHolder &
80  getNodePath() const { return myNodePath; }
81  int getNodeId() const { return myNodeID; }
82 
83  // Parameter overrides
84  void setOverrides(const UT_Options *overrides);
85  const UT_Options &getOverrides() const { return myOverrides; }
86  bool hasOverrides() const { return myOverrides.getNumOptions()>0; }
87 
88  // Vertex array overrides
90  { myVertexOverrides = ovr; }
92  { return myVertexOverrides; }
93 
95  {
99  TERRAIN_SURFACE
100  };
101  void setMaterialType(RE_MaterialType type);
102  RE_MaterialType getMaterialType() const { return myMaterialType; }
103 
104  void markDefaultShader(bool def) { myDefaultShader = def; }
105  bool isDefaultShader() const { return myDefaultShader; }
106 
107  void assignShader(RE_Shader *sh,
109  bool owned = true);
110  void assignShader(UT_UniquePtr<RE_Shader> &&sh,
112 
113  // methods to deal with textures
114  void clearTexture(RE_Render *r);
115  void clearDiffuseMap();
116 
117  void reserveDiffuseLayers(int nlayers);
118 
120  {
135 
141 
143 
144  NUM_TEXMAP_TYPES
145  };
146 
147  // General diffuse texture, 1 allowed per layer.
148  RE_TextureHolder getDiffuseMap(RE_Render *r,
149  int layer,
150  bool preload=false,
151  bool mat_changed = true);
152 
153  // Extra textures, only 1 allowed for the first layer.
154  RE_TextureHolder getSpecularMap(RE_Render *r, bool preload = false,
155  bool mat_change = true);
156  RE_TextureHolder getBumpMap(RE_Render *r, bool preload = false,
157  bool mat_change = true);
158  RE_TextureHolder getNormalMap(RE_Render *r, bool preload = false,
159  bool mat_change = true);
160  RE_TextureHolder getOpacityMap(RE_Render *r, bool preload = false,
161  bool mat_change = true);
162  RE_TextureHolder getDisplaceMap(RE_Render *r, bool preload = false,
163  bool mat_change = true);
164  RE_TextureHolder getEmissionMap(RE_Render *r, bool preload = false,
165  bool mat_change = true);
166  RE_TextureHolder getRoughnessMap(RE_Render *r, bool preload = false,
167  bool mat_change = true);
168  RE_TextureHolder getOcclusionMap(RE_Render *r, bool preload = false,
169  bool mat_change = true);
170  RE_TextureHolder getMetallicMap(RE_Render *r, bool preload = false,
171  bool mat_change = true);
172  RE_TextureHolder getCoatIntensityMap(RE_Render *r, bool preload = false,
173  bool mat_change = true);
174  RE_TextureHolder getCoatRoughnessMap(RE_Render *r, bool preload = false,
175  bool mat_change = true);
176  RE_TextureHolder getReflectionAmountMap(RE_Render *r, bool preload = false,
177  bool mat_change = true);
178 
179  RE_TextureHolder getHairDiffuseMap(RE_Render *r, bool preload = false);
180  RE_TextureHolder getHairSpecularMap(RE_Render *r, bool preload = false);
181  RE_TextureHolder getHairDiffuseRamp(RE_Render *r, bool preload = false);
182  RE_TextureHolder getHairSpecularRamp(RE_Render *r, bool preload = false);
183  RE_TextureHolder getHairAlphaRamp(RE_Render *r, bool preload = false);
184 
185  // Environment map to affect specular reflection. One allowed for layer 0.
186  RE_TextureHolder getEnvMap(RE_Render *r, bool preload = false);
187 
188  // Returns the explicit texture unit binding for the given texture uniform
189  // or RE_MATTEX_NONE if there is no explicit binding.
191  getMaterialTextureBinding(const UT_StringRef &texname);
192 
193  // Determine if the texture file is a regular texture (2D map) or a UDIM
194  // (2D Array map)
195  static bool fetchRegOrUDIM(const UT_StringRef &tex_name,
196  const UT_StringRef &node_path,
197  RE_TextureMap *&map,
198  bool allow_preload = false,
199  const char *uvtile_marker = nullptr);
200 
201  bool areTexturesPending() { return myTextureLoadPending; }
202 
203  void clearTextureLayer(int layer);
204 
205  // In order for these to return the correct value, the corresponding
206  // get..() method must be called first.
207  bool hasSpecularMap() const { return mySpecMap.myTex.isValid(); }
208  bool hasBumpMap() const { return myBumpMap.myTex.isValid(); }
209  bool hasNormalMap() const { return myNormalMap.myTex.isValid(); }
210  bool hasOpacityMap() const{ return myOpacityMap.myTex.isValid(); }
211  bool hasDisplaceMap() const{return myDisplaceMap.myTex.isValid();}
212  bool hasEnvMap() const { return myEnvMap.myTex.isValid(); }
213  bool hasEmissionMap() const
214  { return myEmissionMap.myTex.isValid(); }
215  bool hasRoughnessMap() const
216  { return myRoughMap.myTex.isValid(); }
217  bool hasOcclusionMap() const
218  { return myOcclusionMap.myTex.isValid(); }
219  bool hasMetallicMap() const
220  { return myMetallicMap.myTex.isValid(); }
221  bool hasCoatRoughnessMap() const
222  { return myCoatRoughMap.myTex.isValid(); }
223  bool hasCoatIntensityMap() const
224  { return myCoatIntMap.myTex.isValid(); }
226  { return myReflectAmountMap.myTex.isValid(); }
227 
228  bool hasHairDiffuseMap() const
229  { return myHairDiffMap.myTex.isValid(); }
230  bool hasHairSpecularMap() const
231  { return myHairSpecMap.myTex.isValid(); }
232  bool hasHairDiffuseRamp() const
233  { return myHairDiffRamp.myTex.isValid(); }
234  bool hasHairSpecularRamp() const
235  { return myHairSpecRamp.myTex.isValid(); }
236  bool hasHairAlphaRamp() const
237  { return myHairAlphaRamp.myTex.isValid(); }
238 
239  // Query the material if it is opaque or transparent. 'check_diffuse_alpha'
240  // will check the diffuse texture's alpha channel.
241  bool isTransparent(bool check_diffuse_alpha) const;
242 
243  // Returns true if this material has a texture map (diffuse, bump, etc).
244  bool isTextured() const { return myTexturedFlag; }
245  void setTextured(bool has_tex) { myTexturedFlag = has_tex; }
246  //
247  void needTextureUpdate() { myNeedsTexUpdate = true; }
248  bool needsTextureUpdate() const { return myNeedsTexUpdate; }
249 
250  // Call after all texure parms and textures are set up.
251  void finishTextureSetup(RE_Render *r, bool preload);
252  bool checkForCacheUpdate(int cache_serial,fpreal t); //inline below
253 
254  bool isMatCapTextured() const { return myEmissionMatCap; }
255 
256  // Returns true if textures are used, and the uvsets used by all layers of
257  // the material.
258  const UT_Map<UT_StringHolder, int> &getUVSets();
259 
260  // Return the texture coord set used by 'layer'
261  const UT_StringHolder &getLayerUVSet(int layer) const;
262 
263  void setTextureFormat(RE_TextureCompress format);
264  RE_TextureCompress getTextureFormat() const { return myTexFormat; }
265 
266  void limitTextureSize(bool limit, int w, int h);
267  bool limitTextureSize() const { return myLimitTextureSize; }
268  void getMaxTextureSize(int &w,int &h)
269  { myMaxTextureW = w;
270  myMaxTextureH = h; }
271 
272  void setTextureScale(float sc);
273  float getTextureScale() const { return myTextureScale; }
274 
275  void setTextureMemLimitMB(int mem);
276  int getTextureMemLimitMB() const { return myTextureMemLimitMB; }
277 
278  void mipMapTextures(bool yes);
279  bool mipMapTextures() const { return myMipMapTextures; }
280 
281  void setAnisotropySamples(int samp);
282  int getAnisotropySamples() const { return myTextureAnisotropy; }
283 
284  void allowShaders(bool yes) { myAllowShaders = yes; }
285  bool allowShaders() const { return myAllowShaders; }
286 
287  // methods to deal with lighting
288  void doLighting(bool yes );
289  bool isLit() const { return myDoLighting; }
290 
292  const char *name = nullptr,
295  { return myShader[sht]; }
298  { return myOwnShader[sht]; }
299 
300  /// caches the source for the shader in the material.
301  void setShaderSource(const char *vert_src,
302  const char *geom_src,
303  const char *frag_src,
304  RE_ShaderTarget ptype
306 
307  /// checks this materials' shader against the source, and returns true if
308  /// it matches.
309  bool hasShaderSource(const char *vert_src,
310  const char *geom_src,
311  const char *frag_src,
312  RE_ShaderTarget ptype
314 
315  void setShaderProgram(const char *program_file);
316  bool hasShaderProgram(const char *program_file) const;
319  { return myShaderProgramFile.isstring() ||
320  myVertexSource[ptype].isstring() ||
321  myGeometrySource[ptype].isstring() ||
322  myFragmentSource[ptype].isstring(); }
323  bool customShaderDirty() const { return myCustomShaderDirty; }
324 
325  void removeShader();
326 
327  // methods to access and change the material information
328  float amb( unsigned idx ) const { return myAmb[idx]; }
329  UT_Vector3F amb() const { return myAmb; }
330  float diff( unsigned idx ) const { return myDiff[idx]; }
331  UT_Vector3F diff() const { return myDiff; }
332  float spec( unsigned idx ) const { return mySpec[idx]; }
333  UT_Vector3F spec() const { return mySpec; }
334  float emit( unsigned idx ) const { return myEmit[idx]; }
335  UT_Vector3F emit() const { return myEmit; }
336  UT_Vector3F metallicEdgeTint() const { return myMetallicEdge; }
337  float metallic() const { return myMetallic; }
338  float alpha() const { return myAlpha; }
339  float roughness() const { return myRoughness; }
340  float reflection() const { return myReflection; }
341  float coatRoughness() const { return myCoatRough; }
342  float coatIntensity() const { return myCoatIntensity; }
343  float diffuseRoughness() const { return myDiffRoughness; }
344 
345  /// Sets the perpendicular and parallel alpha factors. If the parallel
346  /// alpha is less than zero, they are assumed to be the same.
347  void setAlpha( float alpha_perp, float alpha_para = -1.0 );
348 
349  // Alpha blend/cutout modes.
351  {
352  ALPHA_BLEND = 0, // normal alpha blending C*A + Cbg*(1-A)
353  CUTOUT_ALPHA, // (A >= cutoff) ? C : 0
355  };
356  void setAlphaMode(AlphaMode mode);
358  { return myAlphaMode; }
359 
360  void setAlphaCutoff(fpreal alpha_val);
362  {
363  return (myAlphaMode != IGNORE_ALPHA) ? myAlphaCutoff : 0.0;
364  }
365 
366 
367  /// If true, the shader generates its own alpha independent of the material
368  /// parameters. This indicates that the material requires a transparency
369  /// pass, since the renderer can otherwise not determine this from the
370  /// shader code.
372  { myShaderAlphaFlag = enabled; }
373  bool hasShaderAlpha() const { return myShaderAlphaFlag; }
374 
375  // give roughness: this method will calculate shininess needed by OGL
376  void setRoughness( float r );
377 
378  // give shininess: this method will enforce sh within [0.001, 1e10]
379  void setShininess( float sh );
380 
381  void setDiffuseRoughness(fpreal r);
382 
383  void setAmb( float r, float g, float b );
384  void setDiff( float r, float g, float b );
385  void setSpec( float r, float g, float b );
386  void setEmit( float r, float g, float b );
387  void setMetallic(float metal);
388  void setMetallicEdge(const UT_Vector3F &metal);
389 
390  void setAmbient( UT_Color col );
391  void setDiffuse( UT_Color col );
392  void setSpecular( UT_Color col );
393  void setEmission( UT_Color col );
394 
395  void setAmbient( const UT_Vector3F &col );
396  void setDiffuse( const UT_Vector3F &col );
397  void setSpecular( const UT_Vector3F &col );
398  void setEmission( const UT_Vector3F &col );
399 
400  void setSpecularTint(fpreal t);
401  fpreal getSpecularTint() const { return mySpecTint; }
402 
403  enum { PHONG=0, BLINN=1, GGX=2, SPEC_ANGLE=3 };
404  void setSpecularModel(int spec_model);
405  int getSpecularModel() const { return mySpecModel; }
406  void setCoatSpecularModel(int spec_model);
407  int getCoatSpecuarModel() const { return myCoatModel; }
408  void setCoatIntensity(fpreal i);
409  fpreal getCoatIntensity() const { return myCoatIntensity; }
410  void setCoatRoughness(fpreal r);
411  fpreal getCoatRoughness() const { return myCoatRough; }
412 
413  void setIOR(float ior);
414  fpreal getIOR() const { return myIOR; }
415 
416  void setReflection(float r, bool explicit_r);
417  fpreal getReflection() const;
418 
419  void usePackedColor(bool enable);
420  void useGeometryColor(bool enable);
421  bool usePackedColor() const { return myUsePackedColor; }
422  bool useGeometryColor() const { return myUseGeoColor; }
423 
424  // returns true if reflection was explicity set, not derived from IoR.
425  bool hasExplicitReflection() const { return myExplicitReflection; }
426 
427  bool needsTangentSpace() const
428  {
429  return (myNormalMap.myMap &&
430  myNormalMapType == NMLMAP_TANGENT);
431  }
432 
433  // Parameters for the bump, spec, opac, env, disp and normal maps.
434  enum re_NormalMapType { NMLMAP_TANGENT=0, NMLMAP_WORLD=1, NMLMAP_OBJECT=2 };
435 
436  void setOpacityMapParms(bool invert, int component);
437  void setNormalParms(re_NormalMapType type, bool bias,
439  bool flip_x, bool flip_y, bool flip_z);
440  void setEnvParms(float scale, const UT_Vector3 &rot,
441  const UT_XformOrder &rord);
442 
443  enum re_Displace {
447  DISPLACE_VECTOR_OBJECT };
448  void setDisplaceParms(float scale, float offset, float quality,
449  re_Displace vector_space, bool y_up);
450  void setDisplaceLOD(float lod) { myDisplaceLOD = lod; }
451  float getDisplaceLOD() const { return myDisplaceLOD; }
452  void setRoughParms(bool invert_map, int component);
453  void setOcclusionParms(int component);
454  void setMetallicParms(int component);
455  void setCoatRoughnessParms(int component);
456  void setCoatIntensityParms(int component);
457  void setReflectionAmountParms(int component, bool affect_ior);
458  void setEmissionParms(bool use_matcap);
459 
460  void setUVTransform(RE_TexMapType maptype,
461  const UT_Matrix3F &xform,
462  int layer = 0);
463  void clearUVTransform(RE_TexMapType maptype,
464  int layer = 0);
465 
466  // Hair parameters
467  void enableHairParameters(bool e) { myHairMaterialEnabled = e; }
468 
469  void setHairDiffuse(float intensity,
470  UT_Vector3F root_col,
471  UT_Vector3F tip_col,
472  bool use_map,
473  bool use_ramp);
474 
475  void setHairSpecular(float intensity,
476  float angle,
477  float shift,
478  UT_Vector3F root_col,
479  UT_Vector3F tip_col,
480  bool use_map,
481  bool use_ramp);
482  void setHairAlpha(float alpha,
483  bool use_alpha_ramp);
484 
485  // randomize hue, sat, and intensity of diffuse (and specular intensity)
486  void setHairRandomize(bool random_hue, UT_Vector2F hue_range,
487  bool random_sat, UT_Vector2F sat_range,
488  bool random_int, UT_Vector2F int_range,
489  bool random_spec_int,
490  UT_Vector2F spec_int_range);
491 
492  // Game shader
493 
494  // Use a transformed UV set.
495  void setUVTransform(int layer,
496  UT_Vector2F t,
497  fpreal r,
498  UT_Vector2F s);
499  void getUVTransform(int layer,
500  UT_Vector2F &t,
501  fpreal &r,
502  UT_Vector2F &s ) const;
503  void setUVLayerTint(int layer, bool use_tint, UT_Vector4F tint,
504  fpreal rough);
505 
506  // Use triplanar projection.
507  void setUVTriplanar(int layer,
508  fpreal scale = 1.0,
509  fpreal sharp = 1.0,
512  bool isUVTriplanar(int layer) const;
513  fpreal getUVTriplanarSharp(int layer) const;
514  fpreal getUVTriplanarScale(int layer) const;
515  UT_Vector3F getUVTriplanarBlend(int layer) const;
516  UT_Vector3F getUVTriplanarOffset(int layer) const;
517  bool getUVLayerUseTint(int layer) const;
518  UT_Vector4F getUVLayerTint(int layer) const;
519  fpreal getUVLayerRough(int layer) const;
520 
521 
522  // For environment lights, which temporarily apply their map to any
523  // materials that don't already have an env map.
524  static void setEnvMapOverride(RE_TextureHolder env_map,
525  const UT_Matrix3F &rot,
526  const UT_Vector3F &col);
527  static void clearEnvMapOverride();
528  static RE_TextureHolder getEnvMapOverride();
529 
530  void setReflectionMap(RE_Texture *map) { myReflectMap = map; }
531  void clearReflectionMap() { myReflectMap = nullptr; }
532 
533 #ifdef UT_DEBUG
534  // debug output
535  void output();
536 #endif
537 
538  RE_MaterialPtr cloneWithoutTexture() const;
539 
540  bool loadTextureMaps(RE_Render *r, bool preload_only = false);
541 
542  // Maps can only be preloaded with loadTextureMaps(), not on the fly during
543  // the render.
544  void preloadMapsOnly(bool preload) { myPreloadMaps = preload; }
545 
546  // Adds an explicit map to 'maptype'. Will be overriden by setTextureMap()
547  // if that has/is called on the material.
548  // nullptr is acceptable (blocks the RE_TextureCB map).
549  void assignTextureMap(RE_Texture *tex,
550  RE_TexMapType maptype,
551  int index = 0);
552  // Remove the explicit map from 'maptype'.
553  void removeTextureMap(RE_TexMapType maptype,
554  int index = 0);
555 
556  void setTextureMap(RE_TextureMap *map,
557  bool give_ownership,
558  RE_TexMapType maptype,
559  const UT_StringRef &uv_set,
560  int index = 0);
561  void clearTextureMap(RE_TexMapType maptype,
562  int index = 0);
563  RE_TextureMap *getTextureMap(RE_TexMapType maptype,
564  int index = 0);
565 
566 
568  { return myMaterialType==FILM_SURFACE ?myNumDiffuseLayers :1; }
569 
570  bool updateShaderForMaterial(
571  RE_Render *r,
572  int layer,
573  bool textured,
575  RE_Shader *shader = nullptr,
576  bool allow_partial = false);
577  void restoreShader(RE_Render *r,
579 
580  /// Returns the amount of main memory (NOT graphics memory!)
581  /// owned by this RE_Material.
582  int64 getMemoryUsage(bool inclusive) const;
583 
584 
585  void writeMaterialParmsToBlockGL4(RE_Render *r,
586  RE_UniformBlock *block,
587  int material_idx,
588  int layer,
589  bool lit,
590  bool textured);
591  // For bindless texturing.
592  void makeTexturesResident(RE_Render *r);
593  void removeTexturesFromResident(RE_Render *r);
594 
595  DEP_MicroNode &dependencies() { return myDepNode; }
596 
597  void updateMaterialBlock(int layer);
598 
600  { return myMaterialBlock; }
602  { return myHairMaterialBlock; }
604  { return myGameMaterialBlock; }
605 private:
606 
607  void loadShaderAttribNames();
608 
609  class re_MaterialTextureMap
610  {
611  public:
612  re_MaterialTextureMap()
613  : myMap(nullptr), myMapIndex(-1),
614  myArrayMapIndex(-1), myTexCoords(0), myComponent(0),
615  myExplicitMap(false), myOwnMap(false) {}
616  ~re_MaterialTextureMap()
617  {
618  if(myOwnMap)
619  delete myMap;
620  }
621  RE_TextureMap *myMap;
622  RE_TextureHolder myTex;
623  RE_CacheVersion myTexVersion;
624  UT_StringHolder myUVSet;
625  UT_DimRect myUDIMArea;
626  int myMapIndex;
627  int myArrayMapIndex;
628  int myTexCoords;
629  int myComponent;
630  bool myExplicitMap;
631  bool myOwnMap;
632  };
633  re_MaterialTextureMap *fetchMapInfo(RE_TexMapType t,
634  int index);
635 
636  RE_TextureHolder fetchTexture(RE_Render *r, RE_TextureMap *map,
637  bool preload_only = false,
638  bool mipmap = true);
639  RE_TextureHolder fetchMap(RE_Render *r,
640  RE_TexMapType map_target,
641  re_MaterialTextureMap &map,
642  bool preload,
643  bool promote_1chan_to_rgb = false);
644 
645  bool bindTextureMap(RE_Render *r, re_MaterialTextureMap &map,
646  const char *udim_stride_name,
647  RE_TexMapType type,
648  const char *component_name = nullptr);
649  void bindUVTransform(RE_TexMapType type,
650  int layer = 0);
651  void bindComponentAndUDIM(re_MaterialTextureMap &map,
652  const char *udim_area_name,
653  const char *component_name = nullptr);
654  void bindTextureMaps(RE_Render *r, int layer);
655 private:
656  ~RE_Material(); // No subclassing
657 
658  void updateHairMaterialForShader(RE_Render *r,
659  bool textured,
660  RE_ShaderTarget sht,
661  RE_Shader *sh);
662  void updateGameMaterialForShader(RE_Render *r,
663  bool textured,
664  RE_ShaderTarget sht,
665  RE_Shader *sh);
666 
667  // -- Bindless texturing support
668  void makeResident(RE_Render *r, RE_TextureHolder texmap);
669  void removeResident(RE_Render *r, RE_TextureHolder texmap);
670  bool bindTextureHandle(RE_Render *r,
671  re_MaterialTextureMap &map,
672  RE_UniformBlock *material_block,
673  int material_index,
674  const char *texture_name);
675  // --
676 
677  UT_Vector4F getComponentVector(int comp,
678  const re_MaterialTextureMap &map) const;
679 
680  DEP_MicroNode myDepNode;
681  UT_Vector3F myAmb;
682  UT_Vector3F myDiff;
683  UT_Vector3F mySpec;
684  UT_Vector3F myEmit;
685  UT_Vector3F myMetallicEdge;
686  float myMetallic;
687  float myAlpha;
688  float myRoughness;
689  float myDiffRoughness;
690  float mySpecTint;
691  RE_MaterialType myMaterialType;
692  AlphaMode myAlphaMode;
693  float myAlphaCutoff;
694 
695  // I own map if if I created my RE_OGLTex
697 
698  RE_Texture *myReflectMap; // always temporary during a render
699 
700  re_MaterialTextureMap mySpecMap;
701  re_MaterialTextureMap myOpacityMap;
702  re_MaterialTextureMap myBumpMap;
703  re_MaterialTextureMap myRoughMap;
704  re_MaterialTextureMap myEmissionMap;
705  re_MaterialTextureMap myNormalMap;
706  re_MaterialTextureMap myDisplaceMap;
707  re_MaterialTextureMap myOcclusionMap;
708  re_MaterialTextureMap myMetallicMap;
709  re_MaterialTextureMap myCoatRoughMap;
710  re_MaterialTextureMap myCoatIntMap;
711  re_MaterialTextureMap myReflectAmountMap;
712  re_MaterialTextureMap myGameBlendMap;
713  re_MaterialTextureMap myEnvMap;
714  re_MaterialTextureMap myHairDiffMap;
715  re_MaterialTextureMap myHairSpecMap;
716  re_MaterialTextureMap myHairDiffRamp;
717  re_MaterialTextureMap myHairSpecRamp;
718  re_MaterialTextureMap myHairAlphaRamp;
719 
720 
721  UT_IntArray mySamplerBindings;
722 
723  // The above maps are only valid pointers if myRenderCount is equal to
724  // RE_OGLRender::getRenderCount.
725  int64 myRenderCount;
726 
727 
728  UT_StringHolder myMaterialName;
729  UT_StringHolder myNodePath;
730  int myNodeID;
731  UT_Options myOverrides;
732  UT_StringMap<UT_StringHolder> myVertexOverrides;
733  unsigned myDoLighting:1,
734  myMipMapTextures:1,
735  myLimitTextureSize:1,
736  myDefaultShader:1,
737  myAllowShaders:1,
738  myTrivialTextureAlpha:1,
739  myInvertRoughMap:1,
740  myPreloadMaps:1,
741  myTextureLoadPending:1,
742  myCustomShaderDirty:1,
743  myNeedsTexUpdate:1,
744  myNeedsTexCheck:1,
745  myTextureSetup:1;
746  int myTextureAnisotropy;
747  int myNumDiffuseLayers;
748 
749  RE_TextureCompress myTexFormat;
750  int myMaxTextureW, myMaxTextureH;
751  float myTextureScale;
752  int myTextureMemLimitMB;
753  float myAlphaPara;
754  bool myShaderAlphaFlag;
755  bool myUseGeoColor;
756  bool myUsePackedColor;
757  bool myDirtyFlag;
758 
759  // Specular parms
760  fpreal myIOR;
761  fpreal32 myReflection;
762  int mySpecModel;
763  bool myReflectAsIOR;
764  bool myExplicitReflection;
765 
766  // Coat parms
767  fpreal myCoatRough;
768  fpreal myCoatIntensity;
769  int myCoatModel;
770 
771  // normal map parms
772  bool myNormalBias;
773  bool myNormalFlipX;
774  bool myNormalFlipY;
775  bool myNormalFlipZ;
776  UT_Vector2F myNormalScale;
777  re_NormalMapType myNormalMapType;
778 
779  // environment map parms
780  UT_Vector3 myEnvRotation;
781  float myEnvScale;
782  UT_XformOrder myEnvRotOrder;
783 
784  // displacement map parms
785  float myDisplaceScale;
786  float myDisplaceOffset;
787  float myDisplaceQuality;
788  re_Displace myDisplaceSpace;
789  bool myDisplaceYUp;
790  float myDisplaceLOD;
791 
792  bool myOpacityMapInvert;
793  int myRoughComp;
794  int myOcclusionComp;
795  int myMetallicComp;
796  bool myEmissionMatCap;
797 
798  RE_Shader *myShader[RE_SHADER_NUM_TARGETS];
799  bool myOwnShader[RE_SHADER_NUM_TARGETS];
800  UT_String myVertexSource[RE_SHADER_NUM_TARGETS];
801  UT_String myGeometrySource[RE_SHADER_NUM_TARGETS];
802  UT_String myFragmentSource[RE_SHADER_NUM_TARGETS];
803  UT_String myShaderProgramFile;
804 
805  RE_UniformBlockHandle myMaterialBlock;
806  int myMaterialOverride;
807  bool myMatBlockDirty;
808  bool myMatBlockHasEnvMapTexture;
809  bool myMatBlockHasReflectMapTexture;
810  bool myMatBlockHasDiffuseTexture;
811  bool myTexturedFlag;
812  int myTextureCacheSerial;
813 
814  int myEnvMapOverrideSerial;
815 
816  // Hair parameters
817  class re_HairCompParms
818  {
819  public:
820  re_HairCompParms() : intensity(0.5), shift(0.0), angle(1.0),
821  root_col(1,1,1), tip_col(1,1,1),
822  use_ramp(false), use_map(false) {}
823  float intensity;
824  float angle;
825  float shift;
826  UT_Vector3F root_col;
827  UT_Vector3F tip_col;
828  bool use_ramp;
829  bool use_map;
830  };
831  struct re_HairRandomParms
832  {
833  re_HairRandomParms() : randomize(false) {}
834  bool randomize;
836  };
837 
838  bool myHairMaterialEnabled;
839  re_HairCompParms myHairDiffuse;
840  re_HairCompParms myHairSpecular;
841  float myHairAlpha;
842  bool myHairUseAlphaRamp;
843  re_HairRandomParms myHairRandomDiffHue;
844  re_HairRandomParms myHairRandomDiffSat;
845  re_HairRandomParms myHairRandomDiffInt;
846  re_HairRandomParms myHairRandomSpecInt;
847  RE_UniformBlockHandle myHairMaterialBlock;
848  int myHairMatBlockIndex;
849  int myHairRandomTextureIndex;
850  int myHairDiffBaseMapIndex;
851  UT_DimRect myHairDiffBaseMapUDIMArea;
852  UT_DimRect myHairSpecBaseMapUDIMArea;
853 
854  RE_TextureHolder myRandomTexture;
855 
856  // cached texture units for 'myLastMatShader'.
857 
858  int myMatBlockDiffuseIndex;
859  int myMatBlockDiffuseArrayIndex;
860 
861  int myMatBlockEnvMapIndex;
862  int myMatBlockReflectMapIndex;
863  int myMatBlockIndex;
864  int myHairMaterialOverride;
865 
866  // hair material block.
867  void *myLastHairShader;
868  int myLastHairShaderSerial;
869  int myHairDiffIndex;
870  int myHairDiffArrayIndex;
871  int myHairSpecIndex;
872  int myHairSpecArrayIndex;
873  int myHairDiffRampIndex;
874  int myHairSpecRampIndex;
875  int myHairAlphaRampIndex;
876  int myMatBlockHairIndex;
877 
878  // game shader material block
879  RE_UniformBlockHandle myGameMaterialBlock;
880  int myGameMaterialOverride;
881 
882  struct re_GameLayer
883  {
884  re_GameLayer() : triplanar(false), scale(1,1), trans(0,0), rot(0),
885  sharp(1), tpscale(1), transform(1.0),
886  tpblend(1,1,1), tpoff(0,0,0), use_tint(false),
887  tint(1.0, 1.0, 1.0, 1.0), rough(1.0) {}
888  bool triplanar;
891  float rot;
892  float sharp;
893  float tpscale;
894  UT_Vector3F tpblend;
895  UT_Vector3F tpoff;
897  bool use_tint;
898  UT_Vector4F tint;
899  float rough;
900  };
901  UT_Array<re_GameLayer> myGameLayers;
902  const RE_Shader *myLastGameShader;
903  int myLastGameShaderSerial;
904  int myGameBlendIndex;
905  int myGameMatBlockIndex;
906 
907  int myPrevSeamlessCubeMap;
908 
909  bool myTimeDep;
910  fpreal myTime;
911 
912  SYS_AtomicInt32 myRefCount;
913  int64 myUniqueId;
914  int64 myVersion;
915  int64 myTexVersion;
916  UT_StringMap<int> myUVSets;
917 
918  class re_UVTransform
919  {
920  public:
921  re_UVTransform() : transform(1.0), identity(true) {}
922  re_UVTransform(const UT_Matrix3F &t, bool i)
923  : transform(t), identity(i) {}
925  bool identity;
926  };
927  UT_Array<re_UVTransform> myTexUVTransform;
928  UT_Array<re_UVTransform> myDiffuseUVTransform;
929 };
930 
931 static inline void intrusive_ptr_add_ref(RE_Material *m) { m->incref(); }
932 static inline void intrusive_ptr_release(RE_Material *m) { m->decref(); }
933 
934 inline void
936 {
937  setAmb(col.x(), col.y(), col.z());
938 }
939 inline void
941 {
942  setDiff(col.x(), col.y(), col.z());
943 }
944 inline void
946 {
947  setSpec(col.x(), col.y(), col.z());
948 }
949 inline void
951 {
952  setEmit(col.x(), col.y(), col.z());
953 }
954 
955 inline bool
957 {
958  if(cache_serial != myTextureCacheSerial)
959  {
960  myTextureCacheSerial = cache_serial;
961  myNeedsTexUpdate = true;
962  return true;
963  }
964  if(myTimeDep && !SYSisEqual(t, myTime))
965  {
966  myNeedsTexUpdate = true;
967  return true;
968  }
969  return false;
970 }
971 
972 // This contains a list of materials for drawing a given geometry, and is
973 // normally used as a transient list of materials during a redraw.
974 // This class does not own the materials as they are also held in
975 // a higher-level cache as well.
976 class RE_API RE_MaterialAtlas : public UT_Map<int,RE_MaterialPtr>
977 {
978 public:
979  // Default material for unassigned prims.
981  { return myDefault; }
982 
983  // Factory default material if materials are disabled.
985  { return myFactory; }
986 
987  bool isDirty(fpreal time) const;
988 
989  RE_MaterialPtr get(int material_id) const
990  {
991  if(material_id == -1)
992  return myDefault ? myDefault : myFactory;
993  const_iterator entry = find(material_id);
994  if(entry != end())
995  return entry->second;
996 
997  return nullptr;
998  }
999 
1000 
1001  // Add a new material to the atlas
1002  void add(const RE_MaterialPtr &material)
1003  { (*this)[ material->getUniqueId() ] = material; }
1004 
1005  // Set the default material, which will be used if no other material
1006  // assignments are present.
1007  void setDefaultMaterial(const RE_MaterialPtr &material,bool add_list=true)
1008  { myDefault = material; if(add_list && material) add(material); }
1009 
1010  // Sets the factory default material, which is used when all materials
1011  // are turned off (as the default material may be the object material).
1012  void setFactoryMaterial(const RE_MaterialPtr &material,bool add_list=true)
1013  { myFactory = material; if(add_list && material) add(material); }
1014 
1015 private:
1016  RE_MaterialPtr myDefault;
1017  RE_MaterialPtr myFactory;
1018 };
1019 
1020 #endif
void setDiffuse(UT_Color col)
bool mipMapTextures() const
Definition: RE_Material.h:279
void setTextured(bool has_tex)
Definition: RE_Material.h:245
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
UT_Vector3F spec() const
Definition: RE_Material.h:333
GLboolean enable
Definition: glew.h:2750
bool hasOverrides() const
Definition: RE_Material.h:86
bool hasHairDiffuseMap() const
Definition: RE_Material.h:228
float alpha() const
Definition: RE_Material.h:338
void setVersion(int64 v)
Definition: RE_Material.h:51
float getDisplaceLOD() const
Definition: RE_Material.h:451
bool hasOpacityMap() const
Definition: RE_Material.h:210
GLenum GLenum GLenum GLenum GLenum scale
Definition: glew.h:14163
int64 getVersion() const
Definition: RE_Material.h:50
float reflection() const
Definition: RE_Material.h:340
Unsorted map container.
Definition: UT_Map.h:83
const UT_StringHolder & getNodePath() const
Definition: RE_Material.h:80
void dirty(bool dirty=true)
Definition: RE_Material.h:53
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1298
void incref()
Definition: RE_Material.h:71
#define RE_API
Definition: RE_API.h:10
bool isTimeDependent() const
Definition: RE_Material.h:68
GLboolean invert
Definition: glcorearb.h:548
bool hasBumpMap() const
Definition: RE_Material.h:208
GT_API const UT_StringHolder time
UT_Vector3F diff() const
Definition: RE_Material.h:331
Transformation order of scales, rotates, and translates.
Definition: UT_XformOrder.h:23
float roughness() const
Definition: RE_Material.h:339
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
const RE_UniformBlockHandle & hairBlock() const
Definition: RE_Material.h:601
GLboolean GLboolean g
Definition: glcorearb.h:1221
GA_API const UT_StringHolder rot
bool isMatCapTextured() const
Definition: RE_Material.h:254
void setEmit(float r, float g, float b)
float metallic() const
Definition: RE_Material.h:337
int getSpecularModel() const
Definition: RE_Material.h:405
bool areTexturesPending()
Definition: RE_Material.h:201
void setShaderAlpha(bool enabled)
Definition: RE_Material.h:371
bool hasShaderAlpha() const
Definition: RE_Material.h:373
const RE_MaterialPtr & getDefaultMaterial() const
Definition: RE_Material.h:980
void decref()
Definition: RE_Material.h:72
void setSpecular(UT_Color col)
bool hasCoatIntensityMap() const
Definition: RE_Material.h:223
const UT_Options & getOverrides() const
Definition: RE_Material.h:85
void setSpec(float r, float g, float b)
GLenum GLenum GLsizei const GLuint GLboolean enabled
Definition: glcorearb.h:2538
bool hasRoughnessMap() const
Definition: RE_Material.h:215
GLuint const GLchar * name
Definition: glcorearb.h:785
MX_RENDER_API ShaderPtr createShader(const string &shaderName, GenContext &context, ElementPtr elem)
Create a shader for a given element.
fpreal getCurrentTime() const
Definition: RE_Material.h:69
int getTextureMemLimitMB() const
Definition: RE_Material.h:276
const UT_StringMap< UT_StringHolder > & vertexOverrides() const
Definition: RE_Material.h:91
void setAmb(float r, float g, float b)
float fpreal32
Definition: SYS_Types.h:200
GLdouble GLdouble t
Definition: glew.h:1403
int getAnisotropySamples() const
Definition: RE_Material.h:282
bool isShaderOwned(RE_ShaderTarget sht=RE_SHADER_TARGET_TRIANGLE) const
Definition: RE_Material.h:296
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:33
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
bool needsTangentSpace() const
Definition: RE_Material.h:427
GLuint GLenum GLenum transform
Definition: glew.h:15055
vint4 blend(const vint4 &a, const vint4 &b, const vbool4 &mask)
Definition: simd.h:4761
RE_MaterialType getMaterialType() const
Definition: RE_Material.h:102
bool hasHairSpecularRamp() const
Definition: RE_Material.h:234
float amb(unsigned idx) const
Definition: RE_Material.h:328
DEP_MicroNode & dependencies()
Definition: RE_Material.h:595
void setFactoryMaterial(const RE_MaterialPtr &material, bool add_list=true)
Definition: RE_Material.h:1012
GLfloat bias
Definition: glew.h:10316
const RE_UniformBlockHandle & materialBlock() const
Definition: RE_Material.h:599
SYS_FORCE_INLINE T & y()
Definition: UT_Vector3.h:508
void intrusive_ptr_release(T *x)
Definition: refcnt.h:217
bool hasDisplaceMap() const
Definition: RE_Material.h:211
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
GLint limit
Definition: glew.h:13230
int getCoatSpecuarModel() const
Definition: RE_Material.h:407
const RE_UniformBlockHandle & gameMatBlock() const
Definition: RE_Material.h:603
GA_API const UT_StringHolder trans
bool hasHairDiffuseRamp() const
Definition: RE_Material.h:232
FMT_CONSTEXPR bool find(Ptr first, Ptr last, T value, Ptr &out)
Definition: format.h:2929
bool hasReflectionAmountMap() const
Definition: RE_Material.h:225
const GLdouble * v
Definition: glcorearb.h:836
bool hasNormalMap() const
Definition: RE_Material.h:209
void setReflectionMap(RE_Texture *map)
Definition: RE_Material.h:530
bool usePackedColor() const
Definition: RE_Material.h:421
GLuint GLuint end
Definition: glcorearb.h:474
bool limitTextureSize() const
Definition: RE_Material.h:267
SYS_FORCE_INLINE T & z()
Definition: UT_Vector3.h:510
fpreal getIOR() const
Definition: RE_Material.h:414
void setTimeDependent(bool timedep, fpreal t=0.0)
Definition: RE_Material.h:63
bool customShaderDirty() const
Definition: RE_Material.h:323
bool hasMetallicMap() const
Definition: RE_Material.h:219
long long int64
Definition: SYS_Types.h:116
void needTextureUpdate()
Definition: RE_Material.h:247
void setEmission(UT_Color col)
bool isLit() const
Definition: RE_Material.h:289
fpreal getCoatIntensity() const
Definition: RE_Material.h:409
bool hasOcclusionMap() const
Definition: RE_Material.h:217
void markDefaultShader(bool def)
Definition: RE_Material.h:104
int getUniqueId() const
Definition: RE_Material.h:48
bool hasSpecularMap() const
Definition: RE_Material.h:207
bool allowShaders() const
Definition: RE_Material.h:285
void enableHairParameters(bool e)
Definition: RE_Material.h:467
GLenum mode
Definition: glcorearb.h:98
float diff(unsigned idx) const
Definition: RE_Material.h:330
void setDefaultMaterial(const RE_MaterialPtr &material, bool add_list=true)
Definition: RE_Material.h:1007
UT_Vector3T< fpreal32 > UT_Vector3F
RE_Shader * getShader(RE_ShaderTarget sht=RE_SHADER_TARGET_TRIANGLE) const
Definition: RE_Material.h:294
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:107
float emit(unsigned idx) const
Definition: RE_Material.h:334
float coatRoughness() const
Definition: RE_Material.h:341
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2001
GLuint shader
Definition: glcorearb.h:784
A map of string to various well defined value types.
Definition: UT_Options.h:84
AlphaMode getAlphaMode() const
Definition: RE_Material.h:357
float diffuseRoughness() const
Definition: RE_Material.h:343
bool hasCoatRoughnessMap() const
Definition: RE_Material.h:221
float spec(unsigned idx) const
Definition: RE_Material.h:332
float coatIntensity() const
Definition: RE_Material.h:342
void intrusive_ptr_add_ref(T *x)
Definition: refcnt.h:208
void setDiff(float r, float g, float b)
void clearReflectionMap()
Definition: RE_Material.h:531
bool hasEnvMap() const
Definition: RE_Material.h:212
const RE_MaterialPtr & getFactoryMaterial() const
Definition: RE_Material.h:984
bool useGeometryColor() const
Definition: RE_Material.h:422
bool checkForCacheUpdate(int cache_serial, fpreal t)
Definition: RE_Material.h:956
void setVertexOverrides(const UT_StringMap< UT_StringHolder > &ovr)
Definition: RE_Material.h:89
UT_Vector3F metallicEdgeTint() const
Definition: RE_Material.h:336
fpreal64 fpreal
Definition: SYS_Types.h:277
bool hasHairAlphaRamp() const
Definition: RE_Material.h:236
void setDisplaceLOD(float lod)
Definition: RE_Material.h:450
bool hasEmissionMap() const
Definition: RE_Material.h:213
GLuint index
Definition: glcorearb.h:785
void preloadMapsOnly(bool preload)
Definition: RE_Material.h:544
RE_TextureCompress getTextureFormat() const
Definition: RE_Material.h:264
void setAmbient(UT_Color col)
UT_Vector3F amb() const
Definition: RE_Material.h:329
const GLdouble * m
Definition: glew.h:9166
const UT_StringHolder & getMaterialName() const
Definition: RE_Material.h:47
GLfloat f
Definition: glcorearb.h:1925
SYS_FORCE_INLINE T & x()
Definition: UT_Vector3.h:506
GLenum GLint * range
Definition: glcorearb.h:1924
fpreal getAlphaCutoff() const
Definition: RE_Material.h:361
int getNodeId() const
Definition: RE_Material.h:81
GLdouble angle
Definition: glew.h:9177
Simple class for a mutli-integer cache tag.
UT_Vector3F emit() const
Definition: RE_Material.h:335
void add(const RE_MaterialPtr &material)
Definition: RE_Material.h:1002
GLintptr offset
Definition: glcorearb.h:664
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
void getMaxTextureSize(int &w, int &h)
Definition: RE_Material.h:268
RE_ShaderTarget
Definition: RE_Types.h:250
bool hasExplicitReflection() const
Definition: RE_Material.h:425
RE_TextureCompress
GLboolean r
Definition: glcorearb.h:1221
GLfloat GLfloat GLfloat alpha
Definition: glcorearb.h:111
bool isDirty(fpreal time) const
Definition: RE_Material.h:54
fpreal getCoatRoughness() const
Definition: RE_Material.h:411
Base::const_iterator const_iterator
Definition: UT_Map.h:94
GLdouble s
Definition: glew.h:1395
bool isTextured() const
Definition: RE_Material.h:244
int getNumDiffuseLayers() const
Definition: RE_Material.h:567
GLint lod
Definition: glcorearb.h:2764
bool hasHairSpecularMap() const
Definition: RE_Material.h:230
float getTextureScale() const
Definition: RE_Material.h:273
RE_MaterialTextureBinding
bool needsTextureUpdate() const
Definition: RE_Material.h:248
bool isDefaultShader() const
Definition: RE_Material.h:105
fpreal getSpecularTint() const
Definition: RE_Material.h:401
void allowShaders(bool yes)
Definition: RE_Material.h:284
bool hasCustomShader(RE_ShaderTarget ptype=RE_SHADER_TARGET_TRIANGLE) const
Definition: RE_Material.h:317