HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GR_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: GR_Material.h (GR Library, C++)
7  *
8  * COMMENTS:
9  */
10 #ifndef GR_Material_h
11 #define GR_Material_h
12 
13 #include "GR_API.h"
14 #include "GR_Defines.h"
15 #include "GR_DrawParms.h"
16 #include "GR_SceneItem.h"
17 
18 #include <UT/UT_IntrusivePtr.h>
19 #include <UT/UT_Map.h>
20 #include <UT/UT_NonCopyable.h>
21 #include <UT/UT_OptionEntry.h>
22 #include <UT/UT_RWLock.h>
23 #include <UT/UT_StringMap.h>
24 #include <UT/UT_StringHolder.h>
25 #include <UT/UT_UniquePtr.h>
26 #include <UT/UT_VectorTypes.h>
27 #include <UT/UT_Vector2.h>
28 #include <UT/UT_Vector3.h>
29 #include <UT/UT_Matrix4.h>
30 #include <UT/UT_Debug.h>
31 #include <DEP/DEP_MicroNode.h>
32 
33 #ifdef USE_VULKAN
34 #include <RV/RV_ShaderProgram.h>
35 #include <RV/RV_TextureCache.h>
37 #endif
38 
39 class UT_Options;
40 class RV_Render;
41 class RV_ShaderProgram;
42 class GR_MaterialParms;
43 class GR_DisplayOption;
44 
46 {
47 public:
48  // Alpha blend/cutout modes, meant to mirror the enum in RE_Material.h
49  // also defined in glsl/block/VK/glH_Material.blk
50  enum AlphaMode
51  {
52  ALPHA_BLEND = 0, // normal alpha blending C*A + Cbg*(1-A)
53  ALPHA_CUTOUT, // (A >= cutoff) ? C : 0
55  };
57  {
64  MATCAP_SURFACE
65  };
66 
67  static GR_MaterialPtr create(MaterialType type,
68  const UT_StringHolder &name);
69  MaterialType type() const { return myType; }
70 
71  // Some materials may be defined by nodes; this is the ID of that node.
72  int getNodeID() const { return myNodeID; }
73  void setNodeID(int id) { myNodeID = id; }
74  UT_StringHolder getNodePath() const;
75 
76  void setTimeDependent(bool timedep, fpreal t = 0.0);
77  void setOPTimeDependent(bool timedep, fpreal t = 0.0);
78  bool isTimeDependent() const { return myTimeDep; }
79  bool isOPTimeDependent() const { return myOPTimeDep; }
80  fpreal getCurrentTime() const { return myTime; }
81 
83  {
84  if(isTextured())
85  {
86  exint cache_version = 0;
87 #ifdef USE_VULKAN
88  cache_version = RV_TextureCache::cacheRefresh();
89 #endif
90  if(myTexCacheVersion != cache_version)
91  return true;
92  }
93  if(myDirtyFlag || myDepNode.isDirty())
94  return true;
95  if(myTimeDep && !SYSisEqual(time, myTime))
96  return true;
97  return false;
98  }
99  void dirty(bool dirty = true)
100  {
101  myDirtyFlag = dirty;
102  if(!dirty)
103  {
104  myDepNode.update(myTime);
105 #ifdef USE_VULKAN
106  myTexCacheVersion = RV_TextureCache::cacheRefresh();
107 #endif
108  }
109  }
110 
111  virtual void dirtyTextures() {}
112 
113  // Returns true if this material has a texture map (diffuse, bump, etc).
114  bool isTextured() const { return myIsTextured; }
115  void setTextured(bool has_tex) { myIsTextured = has_tex; }
116 
117  bool hasUDIM() const { return myHasUDIM; }
118  void setHasUDIM(bool has_udim) { myHasUDIM = has_udim; }
119 
120  bool hasDisplacement() const { return myHasDisplacement; }
121  void setHasDisplacement(bool has_disp) {myHasDisplacement=has_disp;}
122 
123  // Requires UVs outside of texturing
124  bool needsUVs() const { return myNeedsUVs; }
125  void setNeedsUVs(bool uvs) { myNeedsUVs = uvs; }
126 
127  // Requires tangents for tangent-space generation
128  bool needsTangents() const { return myNeedsTangents; }
129  void setNeedsTangents(bool tan) { myNeedsTangents = tan; }
130 
131  bool isTransparent() const { return myIsTransparent; }
132  void setTransparent(bool has_tex) { myIsTransparent = has_tex; }
133 
134  bool texturesLoaded() const { return myTexturesLoaded; }
135  void setTexturesLoaded(bool loaded) { myTexturesLoaded = loaded; }
136 
138  const GR_DisplayOption *opts, bool for_curve = false)
139  {
140  return bindSets(r, shader, for_curve);
141  }
142 
144  const GR_DisplayOption *opts, bool for_curve = false)
145  {
146  bool created = false;
147  return (initMaterialSetForRender(r, shader, created) &&
148  initBlocks(r, opts) &&
149  bindSets(r, shader, for_curve));
150  }
151 
152  virtual void update(const GR_MaterialParms &options) = 0;
153 
154  virtual bool initMaterialSetForRender(RV_Render *r,
156  bool &created)=0;
157  virtual bool initBlocks(RV_Render *r, const GR_DisplayOption *opts) = 0;
158  bool bindSets(RV_Render *r, RV_ShaderProgram *shader, bool for_curve);
159 
160  DEP_MicroNode &dependencies() { return myDepNode; }
161 
162  virtual RV_ShaderProgram *getSurfaceShader() const { return nullptr; }
163  virtual RV_ShaderProgram *getCurveShader() const { return nullptr; }
164 
165  bool opDependenciesDirty() const { return myOpDepsDirty; }
166  void clearOpDependenciesDirty() { myOpDepsDirty = false; }
167  const UT_Set<int> opDependencies() const { return myOpNodes; }
168 
169  // Attribute overrides (mapping an attribute to a shader input)
170  void setOverrides(const UT_Options *overrides);
171  const UT_Options *getOverrides() const { return myOverrides.get(); }
172  // Attribute fallback (value to use if attribute does not exist)
173  void setFallbacks(const UT_Options *attrib_fallbacks);
174  const UT_Options *getFallbacks() const { return myFallbacks.get(); }
175 
177  {
182  IN_VEC4
183  };
184  void setExtraInputs(const UT_StringMap<InputType> &extra_inputs)
185  { myExtraInputs = extra_inputs; }
186  const UT_StringMap<InputType> &extraInputs() const { return myExtraInputs; }
187 
188  static exint getUniqueMaterialID();
189 
190 protected:
192  MaterialType type);
193  ~GR_Material() override;
195 
196  void clearOpIDs() { myOpNodes.clear(); myOpDepsDirty = true; }
197  void addOpID(int id);
198 
199  bool timeChangeCheck() const;
200 
201 #ifdef USE_VULKAN
203  UT_UniquePtr<RV_ShaderVariableSet> myTexMaterialSet;
204  UT_UniquePtr<RV_ShaderVariableSet> myTexCurveMaterialSet;
205 
206  void initTextureParms(RV_TextureParms &parms,
207  const GR_DisplayOption *opts,
208  RV_ImageDim tex_type);
209 
210 #endif
211 
214 
215 private:
216 
217  UT_Set<int> myOpNodes;
218  DEP_MicroNode myDepNode;
219  MaterialType myType;
220  int myNodeID = -999;
221  exint myTexCacheVersion = 0;
222  unsigned myTimeDep : 1,
223  myOPTimeDep : 1,
224  myIsTextured : 1,
225  myNeedsUVs : 1,
226  myHasUDIM : 1,
227  myHasDisplacement : 1,
228  myIsTransparent : 1,
229  myNeedsTangents : 1,
230  myTexturesLoaded : 1,
231  myOpDepsDirty : 1;
232  fpreal myTime = 0.0;
233  UT_UniquePtr<UT_Options> myOverrides;
234  UT_UniquePtr<UT_Options> myFallbacks;
235  UT_StringMap<InputType> myExtraInputs;
236 };
237 
238 static inline void intrusive_ptr_add_ref(GR_Material *m) { m->incref(); }
239 static inline void intrusive_ptr_release(GR_Material *m) { m->decref(); }
240 
241 
242 class GR_API GR_MaterialAtlas : public UT_Map<int,GR_MaterialPtr>
243 {
244 public:
245  // Default material for unassigned prims.
247  { return myDefault; }
248 
249  // Factory default material if materials are disabled.
251  { return myFactory; }
252 
253  // True if any of the materials in the atlas are dirty.
254  bool isDirty(fpreal time) const;
255 
256  // Fetch a material by its ID.
257  GR_MaterialPtr get(int material_id) const
258  {
259  if(myAltAtlas && material_id < 0)
260  return myAltAtlas->get(-material_id);
261  if(material_id == 0)
262  return myDefault ? myDefault : myFactory;
263  const_iterator entry = find(material_id);
264  if(entry != end())
265  return entry->second;
266  return nullptr;
267  }
268  // Fetch a material by ID, and if it doesn't exist, check the draw parms
269  // default material, then fallback to the default or factory if there is
270  // no draw parms default.
271  GR_MaterialPtr get(int material_id, const GR_DrawParms &dp) const
272  {
273  if(myAltAtlas && material_id < 0)
274  return myAltAtlas->get(-material_id);
275 
276  if(material_id == 0 && dp.vk_default_mat_id != 0)
277  material_id = dp.vk_default_mat_id;
278 
279  if(material_id == 0)
280  return myDefault ? myDefault : myFactory;
281 
282  const_iterator entry = find(material_id);
283  if(entry != end())
284  return entry->second;
285  return nullptr;
286  }
287 
288  // Add a new material to the atlas
289  void add(const GR_MaterialPtr &material)
290  { (*this)[ material->id() ] = material; }
291 
292  // Set the default material, which will be used if no other material
293  // assignments are present.
294  void setDefaultMaterial(const GR_MaterialPtr &material,bool add_list=true)
295  { myDefault = material; if(add_list && material) add(material); }
296 
297  // Sets the factory default material, which is used when all materials
298  // are turned off (as the default material may be the object material).
299  void setFactoryMaterial(const GR_MaterialPtr &material,bool add_list=true)
300  { myFactory = material; if(add_list && material) add(material); }
301 
302  // Provide another material atlas to use for material lookups. This is used
303  // for LOP materials when rendering SOPs.
305  { myAltAtlas = atlas; }
306  GR_MaterialAtlas *alternateMaterials() { return myAltAtlas; }
307  const GR_MaterialAtlas *alternateMaterials() const { return myAltAtlas; }
308 
309  bool hasMaterial(int material_id) const
310  {
311  const_iterator entry = find(material_id);
312  return (entry != end());
313  }
314 
315 private:
316  GR_MaterialPtr myDefault;
317  GR_MaterialPtr myFactory;
318 
319  GR_MaterialAtlas *myAltAtlas = nullptr;
320 };
321 
322 /// This is a fast, non-string access and cut-down version of UT_Options.
324 {
325 public:
327 
328  void setParmI(exint ptype, int value);
329  void setParm(exint ptype, fpreal value);
330  void setParm(exint ptype, const UT_Vector2 &value);
331  void setParm(exint ptype, const UT_Vector3 &value);
332  void setParm(exint ptype, const UT_Vector4 &value);
333  void setParm(exint ptype, const UT_Matrix4 &value);
334  void setParm(exint ptype, const UT_StringHolder &value);
335 
336  bool hasParm(exint ptype) const;
337 
338  int getParmI(exint ptype, int defval = 0) const;
339  fpreal getParmF(exint ptype, fpreal defval = 0.0) const;
340  UT_Vector2 getParmV2(exint ptype, UT_Vector2 defval = {0,0}) const;
341  UT_Vector3 getParmV3(exint ptype, UT_Vector3 defval = {0,0,0}) const;
342  UT_Vector4 getParmV4(exint ptype, UT_Vector4 defval = {0,0,0,0}) const;
343  UT_Matrix4 getParmM4(exint ptype,
345  UT_StringHolder getParmS(exint ptype, UT_StringHolder = UT_StringHolder()) const;
346 
347 private:
349 };
350 
351 // Cache for auto-generated shaders (MatX)
353 {
354 public:
357 
358  static void initCache();
359  static void destroyCache();
360 
361  // return the ID assoicated with this name (0 if it doesn't exist).
362  static exint getShaderID(const UT_StringHolder &name);
363 
364  // Return the shader program assoicated with this id (nullptr if not cached)
365  static RV_ShaderProgram *getShader(exint id);
366 
367  // Create a cache entry to store a shader with cacheShader().
368  static exint createEntryID(const UT_StringHolder &name);
369 
370  // Cache the shader by ID, handling over ownership to the cache.
371  static void cacheShader(exint entry,
373 
374  // Remove the shader from the cache with the given name
375  static bool removeShader(const UT_StringHolder &name);
376 
377 private:
378  UT_StringMap<exint> myNameMap;
379 #ifdef USE_VULKAN
381 #endif
382  UT_RWLock myShaderLock;
383  exint myCurrentID = 1;
384 };
385 
386 #endif
void setFactoryMaterial(const GR_MaterialPtr &material, bool add_list=true)
Definition: GR_Material.h:299
void dirty(bool dirty=true)
Definition: GR_Material.h:99
virtual RV_ShaderProgram * getSurfaceShader() const
Definition: GR_Material.h:162
virtual void dirtyTextures()
Definition: GR_Material.h:111
Unsorted map container.
Definition: UT_Map.h:107
const GR_MaterialPtr & getFactoryMaterial() const
Definition: GR_Material.h:250
Base class for various things that can appear in a scene outside of geometry.
Definition: GR_SceneItem.h:19
bool isTimeDependent() const
Definition: GR_Material.h:78
bool opDependenciesDirty() const
Definition: GR_Material.h:165
GT_API const UT_StringHolder time
bool myBlockDirtyFlag
Definition: GR_Material.h:213
int getNodeID() const
Definition: GR_Material.h:72
const UT_Options * getOverrides() const
Definition: GR_Material.h:171
void decref()
Definition: GR_SceneItem.h:33
int64 exint
Definition: SYS_Types.h:125
bool isOPTimeDependent() const
Definition: GR_Material.h:79
UT_NON_COPYABLE(GR_SceneItem)
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
bool texturesLoaded() const
Definition: GR_Material.h:134
const GR_MaterialAtlas * alternateMaterials() const
Definition: GR_Material.h:307
fpreal getCurrentTime() const
Definition: GR_Material.h:80
void setTransparent(bool has_tex)
Definition: GR_Material.h:132
bool isTransparent() const
Definition: GR_Material.h:131
bool hasUDIM() const
Definition: GR_Material.h:117
bool needsTangents() const
Definition: GR_Material.h:128
const UT_StringMap< InputType > & extraInputs() const
Definition: GR_Material.h:186
const UT_Options * getFallbacks() const
Definition: GR_Material.h:174
static const UT_Matrix4T< float > & getIdentityMatrix()
MaterialType type() const
Definition: GR_Material.h:69
GLuint GLuint end
Definition: glcorearb.h:475
void setHasDisplacement(bool has_disp)
Definition: GR_Material.h:121
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
const UT_Set< int > opDependencies() const
Definition: GR_Material.h:167
void add(const GR_MaterialPtr &material)
Definition: GR_Material.h:289
#define GR_API
Definition: GR_API.h:10
void setNeedsTangents(bool tan)
Definition: GR_Material.h:129
static exint cacheRefresh()
bool needsUVs() const
Definition: GR_Material.h:124
bool myDirtyFlag
Definition: GR_Material.h:212
bool isDirty(fpreal time)
Definition: GR_Material.h:82
GLuint const GLchar * name
Definition: glcorearb.h:786
void setNodeID(int id)
Definition: GR_Material.h:73
void clearOpDependenciesDirty()
Definition: GR_Material.h:166
GLdouble t
Definition: glad.h:2397
bool bindWithoutUpdate(RV_Render *r, RV_ShaderProgram *shader, const GR_DisplayOption *opts, bool for_curve=false)
Definition: GR_Material.h:137
SYS_API double tan(double x)
Definition: SYS_FPUMath.h:75
GLuint shader
Definition: glcorearb.h:785
A map of string to various well defined value types.
Definition: UT_Options.h:84
DEP_MicroNode & dependencies()
Definition: GR_Material.h:160
void setTextured(bool has_tex)
Definition: GR_Material.h:115
bool bind(RV_Render *r, RV_ShaderProgram *shader, const GR_DisplayOption *opts, bool for_curve=false)
Definition: GR_Material.h:143
void setNeedsUVs(bool uvs)
Definition: GR_Material.h:125
void setHasUDIM(bool has_udim)
Definition: GR_Material.h:118
fpreal64 fpreal
Definition: SYS_Types.h:277
void setTexturesLoaded(bool loaded)
Definition: GR_Material.h:135
virtual RV_ShaderProgram * getCurveShader() const
Definition: GR_Material.h:163
void setDefaultMaterial(const GR_MaterialPtr &material, bool add_list=true)
Definition: GR_Material.h:294
void clearOpIDs()
Definition: GR_Material.h:196
bool hasMaterial(int material_id) const
Definition: GR_Material.h:309
bool hasDisplacement() const
Definition: GR_Material.h:120
Definition: core.h:1131
GR_MaterialAtlas * alternateMaterials()
Definition: GR_Material.h:306
bool isTextured() const
Definition: GR_Material.h:114
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLboolean r
Definition: glcorearb.h:1222
This is a fast, non-string access and cut-down version of UT_Options.
Definition: GR_Material.h:323
void setAlternateMaterials(GR_MaterialAtlas *atlas)
Definition: GR_Material.h:304
bool SYSisEqual(const UT_Vector2T< T > &a, const UT_Vector2T< T > &b, S tol=SYS_FTOLERANCE)
Componentwise equality.
Definition: UT_Vector2.h:674
const GR_MaterialPtr & getDefaultMaterial() const
Definition: GR_Material.h:246
Reader/Writer mutex class.
Definition: UT_RWLock.h:48
RV_ImageDim
Definition: RV_Type.h:117
type
Definition: core.h:1059
Base::const_iterator const_iterator
Definition: UT_Map.h:118
void incref()
Definition: GR_SceneItem.h:31
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2089
exint id() const
Definition: GR_SceneItem.h:24
void setExtraInputs(const UT_StringMap< InputType > &extra_inputs)
Definition: GR_Material.h:184