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 <DEP/DEP_MicroNode.h>
31 
32 #ifdef USE_VULKAN
33 #include <RV/RV_ShaderProgram.h>
34 #include <RV/RV_TextureCache.h>
36 #endif
37 
38 class UT_Options;
39 class RV_Render;
40 class RV_ShaderProgram;
41 class GR_MaterialParms;
42 class GR_DisplayOption;
43 
45 {
46 public:
47  // Alpha blend/cutout modes, meant to mirror the enum in RE_Material.h
48  // also defined in glsl/block/VK/glH_Material.blk
49  enum AlphaMode
50  {
51  ALPHA_BLEND = 0, // normal alpha blending C*A + Cbg*(1-A)
52  ALPHA_CUTOUT, // (A >= cutoff) ? C : 0
54  };
56  {
62  MATX_SURFACE
63  };
64 
65  static GR_MaterialPtr create(MaterialType type,
66  const UT_StringHolder &name);
67  MaterialType type() const { return myType; }
68 
69  // Some materials may be defined by nodes; this is the ID of that node.
70  int getNodeID() const { return myNodeID; }
71  void setNodeID(int id) { myNodeID = id; }
72  UT_StringHolder getNodePath() const;
73 
74  void setTimeDependent(bool timedep, fpreal t = 0.0)
75  {
76  myTimeDep = timedep;
77  myTime = timedep ? t : 0.0;
78  }
79  bool isTimeDependent() const { return myTimeDep; }
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  // Returns true if this material has a texture map (diffuse, bump, etc).
112  bool isTextured() const { return myIsTextured; }
113  void setTextured(bool has_tex) { myIsTextured = has_tex; }
114 
115  bool hasUDIM() const { return myHasUDIM; }
116  void setHasUDIM(bool has_udim) { myHasUDIM = has_udim; }
117 
118  // Requires UVs outside of texturing
119  bool needsUVs() const { return myNeedsUVs; }
120  void setNeedsUVs(bool uvs) { myNeedsUVs = uvs; }
121 
122  // Requires tangents for tangent-space generation
123  bool needsTangents() const { return myNeedsTangents; }
124  void setNeedsTangents(bool tan) { myNeedsTangents = tan; }
125 
126  bool isTransparent() const { return myIsTransparent; }
127  void setTransparent(bool has_tex) { myIsTransparent = has_tex; }
128 
129  bool texturesLoaded() const { return myTexturesLoaded; }
130  void setTexturesLoaded(bool loaded) { myTexturesLoaded = loaded; }
131 
133  const GR_DisplayOption *opts)
134  {
135  bool created = false;
136  return (initMaterialSetForRender(r, shader, created) &&
137  initBlocks(r, opts) &&
138  bindSets(r, shader));
139  }
140 
141  virtual void update(const GR_MaterialParms &options) = 0;
142 
143  virtual bool initMaterialSetForRender(RV_Render *r,
145  bool &created)=0;
146  virtual bool initBlocks(RV_Render *r, const GR_DisplayOption *opts) = 0;
147  bool bindSets(RV_Render *r, RV_ShaderProgram *shader);
148 
149  DEP_MicroNode &dependencies() { return myDepNode; }
150 
151  virtual RV_ShaderProgram *getSurfaceShader() const { return nullptr; }
152  virtual RV_ShaderProgram *getCurveShader() const { return nullptr; }
153 
154  bool opDependenciesDirty() const { return myOpDepsDirty; }
155  void clearOpDependenciesDirty() { myOpDepsDirty = false; }
156  const UT_Set<int> opDependencies() const { return myOpNodes; }
157 
158  void setOverrides(const UT_Options *overrides);
159  const UT_Options *getOverrides() const { return myOverrides.get(); }
160 
161 protected:
163  MaterialType type);
164  ~GR_Material() override;
166 
167  void clearOpIDs() { myOpNodes.clear(); myOpDepsDirty = true; }
168  void addOpID(int id)
169  {
170  if(id != -999)
171  {
172  if(!myOpNodes.contains(id))
173  {
174  myOpNodes.emplace(id);
175  myOpDepsDirty = true;
176  }
177  }
178  }
179 
180 #ifdef USE_VULKAN
182  UT_UniquePtr<RV_ShaderVariableSet> myTexMaterialSet;
183 
184  void initTextureParms(RV_TextureParms &parms,
185  const GR_DisplayOption *opts,
186  RV_ImageDim tex_type);
187 
188 #endif
189 
192 
193 private:
194 
195  UT_Set<int> myOpNodes;
196  DEP_MicroNode myDepNode;
197  MaterialType myType;
198  int myNodeID = -999;
199  exint myTexCacheVersion = 0;
200  unsigned myTimeDep : 1,
201  myIsTextured : 1,
202  myNeedsUVs : 1,
203  myHasUDIM : 1,
204  myIsTransparent : 1,
205  myNeedsTangents : 1,
206  myTexturesLoaded : 1,
207  myOpDepsDirty : 1;
208  fpreal myTime = 0.0;
209  UT_UniquePtr<UT_Options> myOverrides;
210 };
211 
212 static inline void intrusive_ptr_add_ref(GR_Material *m) { m->incref(); }
213 static inline void intrusive_ptr_release(GR_Material *m) { m->decref(); }
214 
215 
216 class GR_API GR_MaterialAtlas : public UT_Map<int,GR_MaterialPtr>
217 {
218 public:
219  // Default material for unassigned prims.
221  { return myDefault; }
222 
223  // Factory default material if materials are disabled.
225  { return myFactory; }
226 
227  // True if any of the materials in the atlas are dirty.
228  bool isDirty(fpreal time) const;
229 
230  // Fetch a material by its ID.
231  GR_MaterialPtr get(int material_id) const
232  {
233  if(material_id == -1)
234  return myDefault ? myDefault : myFactory;
235  const_iterator entry = find(material_id);
236  if(entry != end())
237  return entry->second;
238  return nullptr;
239  }
240  // Fetch a material by ID, and if it doesn't exist, check the draw parms
241  // default material, then fallback to the default or factory if there is
242  // no draw parms default.
243  GR_MaterialPtr get(int material_id, const GR_DrawParms &dp) const
244  {
245  if(material_id == -1 && dp.vk_default_mat_id != -1)
246  material_id = dp.vk_default_mat_id;
247 
248  if(material_id == -1)
249  return myDefault ? myDefault : myFactory;
250 
251  const_iterator entry = find(material_id);
252  if(entry != end())
253  return entry->second;
254  return nullptr;
255  }
256 
257  // Add a new material to the atlas
258  void add(const GR_MaterialPtr &material)
259  { (*this)[ material->id() ] = material; }
260 
261  // Set the default material, which will be used if no other material
262  // assignments are present.
263  void setDefaultMaterial(const GR_MaterialPtr &material,bool add_list=true)
264  { myDefault = material; if(add_list && material) add(material); }
265 
266  // Sets the factory default material, which is used when all materials
267  // are turned off (as the default material may be the object material).
268  void setFactoryMaterial(const GR_MaterialPtr &material,bool add_list=true)
269  { myFactory = material; if(add_list && material) add(material); }
270 
271  bool hasMaterial(int material_id) const
272  {
273  const_iterator entry = find(material_id);
274  return (entry != end());
275  }
276 
277 private:
278  GR_MaterialPtr myDefault;
279  GR_MaterialPtr myFactory;
280 };
281 
282 /// This is a fast, non-string access and cut-down version of UT_Options.
284 {
285 public:
287 
288  void setParmI(exint ptype, int value);
289  void setParm(exint ptype, fpreal value);
290  void setParm(exint ptype, const UT_Vector2 &value);
291  void setParm(exint ptype, const UT_Vector3 &value);
292  void setParm(exint ptype, const UT_Vector4 &value);
293  void setParm(exint ptype, const UT_Matrix4 &value);
294  void setParm(exint ptype, const UT_StringHolder &value);
295 
296  int getParmI(exint ptype, int defval = 0) const;
297  fpreal getParmF(exint ptype, fpreal defval = 0.0) const;
298  UT_Vector2 getParmV2(exint ptype, UT_Vector2 defval = {0,0}) const;
299  UT_Vector3 getParmV3(exint ptype, UT_Vector3 defval = {0,0,0}) const;
300  UT_Vector4 getParmV4(exint ptype, UT_Vector4 defval = {0,0,0,0}) const;
301  UT_Matrix4 getParmM4(exint ptype,
303  UT_StringHolder getParmS(exint ptype, UT_StringHolder = UT_StringHolder()) const;
304 
305 private:
307 };
308 
309 // Cache for auto-generated shaders (MatX)
311 {
312 public:
315 
316  static void initCache();
317  static void destroyCache();
318 
319  // return the ID assoicated with this name (0 if it doesn't exist).
320  static exint getShaderID(const UT_StringHolder &name);
321 
322  // Return the shader program assoicated with this id (nullptr if not cached)
323  static RV_ShaderProgram *getShader(exint id);
324 
325  // Cache the shader and return the cache tag ID. The cache takes ownership
326  // of the shader.
327  static exint cacheShader(const UT_StringHolder &name,
329 
330  // Remove the shader from the cache with the given name
331  static bool removeShader(const UT_StringHolder &name);
332 
333 private:
334  UT_StringMap<exint> myNameMap;
335 #ifdef USE_VULKAN
337 #endif
338  UT_RWLock myShaderLock;
339  exint myCurrentID = 1;
340 };
341 
342 #endif
void setFactoryMaterial(const GR_MaterialPtr &material, bool add_list=true)
Definition: GR_Material.h:268
void dirty(bool dirty=true)
Definition: GR_Material.h:99
virtual RV_ShaderProgram * getSurfaceShader() const
Definition: GR_Material.h:151
Unsorted map container.
Definition: UT_Map.h:107
const GR_MaterialPtr & getFactoryMaterial() const
Definition: GR_Material.h:224
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:79
bool opDependenciesDirty() const
Definition: GR_Material.h:154
GT_API const UT_StringHolder time
bool myBlockDirtyFlag
Definition: GR_Material.h:191
int getNodeID() const
Definition: GR_Material.h:70
const UT_Options * getOverrides() const
Definition: GR_Material.h:159
void decref()
Definition: GR_SceneItem.h:33
int64 exint
Definition: SYS_Types.h:125
void setTimeDependent(bool timedep, fpreal t=0.0)
Definition: GR_Material.h:74
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:129
fpreal getCurrentTime() const
Definition: GR_Material.h:80
void setTransparent(bool has_tex)
Definition: GR_Material.h:127
bool isTransparent() const
Definition: GR_Material.h:126
bool hasUDIM() const
Definition: GR_Material.h:115
bool needsTangents() const
Definition: GR_Material.h:123
static const UT_Matrix4T< float > & getIdentityMatrix()
MaterialType type() const
Definition: GR_Material.h:67
GLuint GLuint end
Definition: glcorearb.h:475
#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:156
void add(const GR_MaterialPtr &material)
Definition: GR_Material.h:258
#define GR_API
Definition: GR_API.h:10
void setNeedsTangents(bool tan)
Definition: GR_Material.h:124
static exint cacheRefresh()
bool needsUVs() const
Definition: GR_Material.h:119
void addOpID(int id)
Definition: GR_Material.h:168
bool myDirtyFlag
Definition: GR_Material.h:190
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:71
bool bind(RV_Render *r, RV_ShaderProgram *shader, const GR_DisplayOption *opts)
Definition: GR_Material.h:132
void clearOpDependenciesDirty()
Definition: GR_Material.h:155
GLdouble t
Definition: glad.h:2397
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:149
void setTextured(bool has_tex)
Definition: GR_Material.h:113
void setNeedsUVs(bool uvs)
Definition: GR_Material.h:120
void setHasUDIM(bool has_udim)
Definition: GR_Material.h:116
fpreal64 fpreal
Definition: SYS_Types.h:277
void setTexturesLoaded(bool loaded)
Definition: GR_Material.h:130
virtual RV_ShaderProgram * getCurveShader() const
Definition: GR_Material.h:152
void setDefaultMaterial(const GR_MaterialPtr &material, bool add_list=true)
Definition: GR_Material.h:263
void clearOpIDs()
Definition: GR_Material.h:167
bool hasMaterial(int material_id) const
Definition: GR_Material.h:271
Definition: core.h:1131
bool isTextured() const
Definition: GR_Material.h:112
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:283
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:220
Reader/Writer mutex class.
Definition: UT_RWLock.h:48
RV_ImageDim
Definition: RV_Type.h:111
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