HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RE_TextureMap.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: RE_TextureMap.h ( RE Library, C++)
7  *
8  * COMMENTS:
9  * Base class for the RE texture map interface to RE_Textures. This class
10  * is basically a handle to a texture map. It allows you to load textures
11  * from images transparently, and cache generated textures.
12  */
13 
14 #ifndef __RE_TextureMap_h__
15 #define __RE_TextureMap_h__
16 
17 #include "RE_API.h"
18 #include "RE_Texture.h"
19 
20 #include <IMG/IMG_FileTypes.h>
21 #include <UT/UT_Functor.h>
22 #include <UT/UT_String.h>
23 #include <UT/UT_StringHolder.h>
24 
25 #include <time.h>
26 
27 class RE_TextureCache;
28 class re_AutoTextureSource;
29 class TIL_Raster;
30 class RE_Render;
31 class IMG_File;
32 class IMG_FileParms;
33 
35 {
36 public:
37  RE_TextureHolder() : myTexture(nullptr) {}
39  : myTexture(tex)
40  {
41  if(myTexture)
42  myTexture->setInUse(true);
43  }
45  : myTexture(src.myTexture)
46  {
47  if(myTexture)
48  myTexture->setInUse(true);
49  }
51  {
52  if(myTexture)
53  myTexture->setInUse(false);
54  }
55 
56  bool isValid() const { return myTexture != nullptr; }
57  RE_Texture * operator*() const { return myTexture; }
58  RE_Texture * operator->() const { UT_ASSERT(myTexture); return myTexture; }
59 
60  void operator=(RE_Texture *tex)
61  {
62  if(myTexture)
63  myTexture->setInUse(false);
64  myTexture = tex;
65  if(myTexture)
66  myTexture->setInUse(true);
67  }
68  void operator=(const RE_TextureHolder &tex)
69  {
70  *this = tex.myTexture;
71  }
72 
73  void clear()
74  {
75  if(myTexture)
76  {
77  myTexture->setInUse(false);
78  myTexture=nullptr;
79  }
80  }
81 
82  bool operator==(const RE_Texture *tex) const
83  { return myTexture == tex; }
84  bool operator!=(const RE_Texture *tex) const
85  { return myTexture != tex; }
86  bool operator==(const RE_TextureHolder &tex) const
87  { return myTexture == tex.myTexture; }
88  bool operator!=(const RE_TextureHolder &tex) const
89  { return myTexture != tex.myTexture; }
90 
91  int usageCount() const
92  { return myTexture ? myTexture->getUsage() : 0; }
93 
94 private:
95  RE_Texture *myTexture;
96 };
97 
99 {
100 public:
101  virtual ~RE_TextureMap();
102 
103  /// Returns the amount of main memory (NOT graphics memory!)
104  /// owned by this RE_TextureMap.
105  virtual int64 getMemoryUsage(bool inclusive) const;
106 
107  virtual const char *className() const;
108 
109  // The texture dimension and type of this map (1D, 2D, Cube, 3D, etc)
110  virtual RE_TextureDimension getMapType() = 0;
111 
112  // Create an exact replica of this texture map.
113  virtual RE_TextureMap *clone() const = 0;
114 
115  // Create a new texture map of the given type.
116  static RE_TextureMap *newTextureMap(RE_TextureDimension type);
117 
118  // These may return false if the dimension of the texture does not match
119  // what this map is, or if a file or node reference cannot be resolved.
120 
121  // Find an image from a disk file or node.
122  bool setSource(const char *mapname,
123  const char *relativeTo);
124 
125  // Store a generated texture in the cache. You cannot set the translation
126  // options with this method, it just uses the map class as a container.
127  // The filtering can be set, however. You are responsible for clearing the
128  // texture if it is deleted. This allows the texture to be counted as used
129  // when computing the #maps and total texture size for a render.
130  bool setSource(const char *mapname,
131  const char *relativeTo,
133  bool (*releaseCB)(RE_Texture *, void *),
134  void *releaseObject);
135  // Avoid cache collisions with textures that might be sized or formatted
136  // differently (like BG images vs material textures).
137  void setNamespace(const char *ns) { myNamespace = ns; }
138 
139  // This is an optional callback which will modify the map before being used
140  // to create the texture. Any of the image parameters can be changed except
141  // for the basic type of the texture (ie 2D, cube). The operation must be
142  // done inplace. 'pixeldata' will be nullptr when querying for type/size
143  // changes and non-nullptr when about to generate the texture. In the
144  // non-nullptr case, none of the image parameters should be altered (they
145  // will be the values returned from the previous pixeldata=nullptr pass.
147  {
148  public:
150  : pixels(nullptr), type(RE_GPU_FLOAT32),
151  vector_size(1), w(0), h(0), d(0),
152  option(RE_TEXTURE_FORMAT_EXTRA_NONE), texture(nullptr)
153  {}
156  int w,h,d;
158  void *pixels;
160  };
161  void setSourceModifyCallback(const char *opname,
162  bool (*modifyCallback)(RE_TextureMap *map,
163  TexCBPixelData &pixdata,
164  void *user_data),
165  void *user_data);
166 
167  // Clears the texture stored in the map, if the first setSource() method
168  // was called.
169  void clearStoredTexture();
170 
171  // Clears the texture and removes it from the cache. Useful if you're about
172  // to delete the texture.
173  void deleteTexRef(RE_Texture *tex);
174 
175  /// Access to the texture itself. This method may actually build it.
176  RE_TextureHolder getTexture(RE_Render *r, bool deferred_load = false);
177 
178  /// Access to the texture itself, this method will only check the cache.
179  RE_TextureHolder getCachedTexture(RE_Render *r);
180 
181  /// Return a texture that is not cached in the GL cache. You own it.
182  RE_Texture *getUncachedTexture(RE_Render *r);
183 
184  // Returns true if this map has a tagged texture, setSource(.., RE_Texture)
185  bool hasTaggedTexture() const { return myTaggedTexture != nullptr; }
186 
187  // When the version changes, the texture
188  void setVersion(RE_CacheVersion v) { myVersion = v; }
189  void bumpVersion();
190  RE_CacheVersion getVersion() const { return myVersion; }
191 
192  // clear any cached data.
193  virtual void invalidateCached();
194 
195  // Basic information about the map
196  const char *getMapName() const { return myMapName; }
197  const char *getRelativePath() const { return myRelative; }
198 
199  // Set the source to nullptr without changing any of the other parameters.
200  void reset();
201 
202  // Set the source to nullptr and remove it from the cache.
203  void clear(const char *clear_name = nullptr);
204 
205  // returns true if the map/relative names point to a valid source. This
206  // allows you to use the methods below to query the data.
207  bool isSourceValid();
208 
209  // Mark the texture as properly resolved (file loaded, op cooked) or not
210  // (file failed to load)
211  void setValidSourceResolve(bool valid);
212  bool hasValidSourceResolve() const { return myValidSourceResolve; }
213 
214  /// Return trues if a load request has been queued for this texture, but it
215  /// isn't yet loaded.
216  bool isLoadPending() const { return myLoadPending; }
217 
218  // Returns the resolution of the image
219  void getResolution(int &w, int &h, int &d);
220 
221  // The data format of the pixel (8b, 32 FP)
222  RE_GPUType getFormatType();
223 
224  // The number of components in a pixels (1-4)
225  int getFormatSize();
226 
227  // Returns the original aspect ratio of the image (before scaling or sizing)
228  float getAspectRatio();
229  // For 3D textures, X:Z ratio.
230  float getAspectRatioXZ();
231 
232 
233  // Accessors to the filter options for the texture. A single texture map
234  // be referenced by several maps with different filter options.
235  RE_TextureFilter &filter() { return myFilter; }
236  const RE_TextureFilter &filter() const { return myFilter; }
237 
238  // Set the texture colorspace. PXL_CS_CUSTOM_GAMMA is unsupported.
239  void setColorSpace(PXL_ColorSpace space);
240  PXL_ColorSpace getColorSpace() const;
241 
242  // Image to Texture translation. These parameters are used to convert an
243  // image (1D, 2D or 3D) to a texture.
244 
245  // explicitly scale to the given resolution. If scale is specified as well,
246  // it further scales this size down. Setting to 0,0,0 will disable this.
247  void sizeTo(int w, int h, int d=1);
248 
249  // Scale the map up or down by a factor. By default, a uniform scale is
250  // used (h/d = -1.0f)
251  void scaleTo(fpreal w, fpreal h = -1.0f, fpreal d = -1.0f);
252 
253  // Cap the size at these maximums, scaling down uniformly to fit. This is
254  // applied after the sizeTo() and scaleTo() methods. Setting to 0,0,0 will
255  // disable the max size.
256  void maxSize(int w, int h, int d);
257 
258  // Cap the resulting texture so that it doesn't use more than this limit.
259  // A limit of 0 will disable the texture memory limit.
260  void maxMemSize(int mem_limit_mb);
261 
262  // If enabled, this will round the texture to the next power of two if a
263  // dimension is not a power of two.
264  void makePow2(bool pow2);
265 
266  // By default, the map uses the same format as the image.
267  void setCompression(RE_TextureCompress comp);
268 
269  // By default, the texture is not mipmapped.
270  void setMipMap(bool mip);
271 
272  // No options by default. Can currently set a 1chan texture to be
273  // interpreted as A, and 2chan texture RG instead of Lum-A.
275  { myFormatOption = opt; }
276 
277  // Texture Component to sample from when using a single channel from
278  // packed maps. -1 indicates that no mapping is done.
279  void setTextureComponent(int texcomp)
280  { myTextureComponent = SYSclamp(texcomp, -1, 3); }
281 
282  // Sample texture map as { R, R, R, A }. If the texture component is given
283  // above, use that component (eg. texcomp=2 would sample B,B,B,A).
284  void fillRGBFromSingle(bool fill_rgb_mono)
285  { myTextureRGBMono = fill_rgb_mono; }
286 
287  // If true, set A to 1. Otherwise, use A from the map.
288  void setOpaqueAlpha(bool opaque_alpha)
289  { myOpaqueAlpha = opaque_alpha; }
290  // returns the opaque alpha setting, not whether the texture itself
291  // actually has fully opaque alpha.
292  bool getOpaqueAlpha() const { return myOpaqueAlpha; }
293 
294  // When loading the map, look for details about the alpha matte. Currently
295  // only implemented for 2D maps.
296  void checkAlphaDetails(bool check) { myAlphaDetails = check; }
297  IMG_FileAlphaInfo getAlphaDetails() const { return myAlphaInfo; }
298 
299  void allowPreload(bool preload) { myAllowPreloadFlag = preload; }
300  bool allowsPreload() const { return myAllowPreloadFlag; }
301 
302  // If this map references an OP, this returns the unique OP ID.
303  virtual int getSourceOpID() const { return -999; }
304 
305  static void backgroundTextureLoadThreads(int n);
306 
307  // Called when the texture this map references was deleted.
308  static void textureReferenceDeleted(void *, RE_Texture *t);
309 
310  // Texture cache interface
311  static void setTextureCacheSize(int64 size_in_mb);
312  static int64 getTextureCacheSize();
313 
314  static int64 getTextureCacheUsage();
315  static int64 get2DTextureUsage();
316  static int64 get3DTextureUsage();
317  static int64 getMipMapUsage();
318  static int64 get3DTextureFP32Size();
319  static int64 get3DTextureFP16Size();
320  static int64 get2DTextureFP32Size();
321  static int64 get2DTextureFP16Size();
322  static int64 getOrig3DTextureFP32Size();
323  static int64 getOrig3DTextureFP16Size();
324  static int64 getOrig2DTextureFP32Size();
325  static int64 getOrig2DTextureFP16Size();
326 
327  static void pruneTextureCache();
328  static void clearTextureCache(bool full_clear);
329  static void debugDumpCache();
330 
331  static int getTextureCacheSerial();
332 
333  // These methods can be used to query texture map usage over a long period
334  // of time (such as a single redraw). The query methods must be called
335  // *before* recording is disabled.
336  static void recordTextureMapUsage(bool enable);
337  static bool recordingTextureUsage();
338  static void textureUsed(RE_Texture *tex);
339  static void textureDeleted(RE_Texture *tex);
340 
341  static int getNumUsedTextureMaps();
342  static int64 getUsedTextureMemory();
343 
344 protected:
345  // You can't create an RE_TextureMap directly; you have to use one of the
346  // derived classes to define a type.
347  RE_TextureMap();
348 
349  virtual void buildSourceName(UT_String &cachedname,
350  const char *map, const char *rel);
351  virtual void buildTextureName(UT_String &cachedname,
352  const char *map, const char *rel);
353  re_AutoTextureSource getTextureSource(const char *map, const char *relto,
354  void **tex_data,
355  bool deferred_load=false,
356  bool preload_only=false);
357 
358  // Adjusts a resolution based on the maxSize, sizeTo and scaleTo settings.
359  // 1D and 2D maps can set the extra dimensions to nullptr.
360  void getDesiredResolution(RE_GPUType type, int vsize,
361  int *w, int *h, int *d);
362 
363  // Override to actually create the texture from the data returned in
364  // mapptr from getNodeSource() and getFileSource().
365  virtual bool buildTexture(RE_Render *r, RE_Texture *tex,
366  void *data) = 0;
367 
368  // Override if you're interested in when a texture is retrieved from the
369  // cache instead of built.
370  virtual void cachedTextureFound(RE_Texture *tex) {}
371 
372  // Note that mapptr may be nullptr if the actual data of the image is not
373  // required yet.
374  virtual bool getNodeSource(const char *map, const char *rel,
375  void **mapptr,
376  RE_TextureDimension &textype,
377  RE_GPUType &datatype,
378  int &vectorsize,
379  int &w, int &h, int &d);
380  virtual bool getFileSource(const char *map,
381  void **mapptr,
382  RE_TextureDimension &textype,
383  RE_GPUType &datatype,
384  int &vectorsize,
385  int &w, int &h, int &d,
386  bool preload_only);
387 
388  void setPendingLoad(bool pend) { myLoadPending = pend; }
389 
390  // override to return false if the derived class will call the callback to
391  // modify the pixel data.
392  void * callModifyCallback(RE_Texture *tex, void *data);
393 
394  // Can be called after the source is set.
395  bool isNodeSource();
396 
397  RE_TextureCompress getCompression() const { return myCompression; }
398  bool getMipMap() const { return myMipMap; }
399 
400  // Returns a empty texture object with the desired attributes.
401  RE_Texture *getTextureStub(bool preload);
402 
403  void copy(const RE_TextureMap *src);
404 
406  { return myFormatOption; }
407 
408  void setupGammaCorrect(IMG_FileParms &parms,
409  const char *filename,
410  RE_GPUType datatype);
411 
412  RE_TextureHolder fetchTexture(RE_Render *r,
413  bool build_if_missing,
414  bool preload_only);
415 
416  // Translation options
418  fpreal myScale[3];
419  int myRes[3];
420  int myMaxRes[3];
422  int myMipMap;
427 
428 private:
429  void init(RE_Texture *t);
430 
431  static bool cachedMapReleased(void *tex, void *map);
432  bool clearTexture(RE_Texture *tex);
433 
434  bool updateFileModTime(const char *filename, time_t &mod);
435 
436  UT_String myRelative;
437  UT_String myOpName;
438  UT_StringHolder myNamespace;
439  RE_TextureFilter myFilter;
440  bool myMapOwned;
441  RE_CacheVersion myVersion;
442  PXL_ColorSpace myColorSpace;
443  RE_TextureFormatExtra myFormatOption;
444  int myTextureComponent;
445  bool myTextureRGBMono;
446  bool myOpaqueAlpha;
447  bool myAllowPreloadFlag;
448  bool myPrevSourceResult;
449 
450  UT_String myModifyOp;
451  bool (*myModifyCallback)(RE_TextureMap*, TexCBPixelData&,
452  void*);
453  void *myModifyCallbackData;
454 
455  // Only for the setSource(src, rel, tex, version) call.
456  RE_TextureHolder myTaggedTexture;
457  RE_TextureCache *myCache;
458  RE_TextureHolder myCachedTexture;
459  bool myLoadPending;
460  bool myVolatileFlag;
461  bool myValidSourceResolve;
462 
463 
464  bool (*myReleaseCB)(RE_Texture *, void *);
465  void *myReleaseObject;
466 };
467 
468 
469 inline const char *
471 {
472  return "RE_TextureMap";
473 }
474 
475 #endif
RE_TextureFormatExtra getTextureFormatOption() const
bool operator==(const RE_TextureHolder &tex) const
Definition: RE_TextureMap.h:86
GT_API const UT_StringHolder filename
void allowPreload(bool preload)
const RE_TextureFilter & filter() const
#define RE_API
Definition: RE_API.h:10
virtual const char * className() const
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
const GLdouble * v
Definition: glcorearb.h:837
UT_String myMapName
void checkAlphaDetails(bool check)
void operator=(RE_Texture *tex)
Definition: RE_TextureMap.h:60
bool getOpaqueAlpha() const
void setNamespace(const char *ns)
GLint GLint GLsizei GLint GLenum GLenum const void * pixels
Definition: glcorearb.h:108
RE_TextureDimension
void setTextureFormatOption(RE_TextureFormatExtra opt)
RE_TextureCompress getCompression() const
const char * getRelativePath() const
RE_TextureFilter & filter()
RE_TextureFormatExtra option
int usageCount() const
Definition: RE_TextureMap.h:91
bool isLoadPending() const
bool operator!=(const RE_TextureHolder &tex) const
Definition: RE_TextureMap.h:88
RE_GPUType
Definition: RE_Types.h:44
GLdouble n
Definition: glcorearb.h:2008
GLfloat f
Definition: glcorearb.h:1926
void setInUse(bool inc)
RE_Texture * operator->() const
Definition: RE_TextureMap.h:58
GLboolean reset
Definition: glad.h:5138
void setTextureComponent(int texcomp)
virtual void cachedTextureFound(RE_Texture *tex)
RE_TextureCompress myCompression
RE_TextureFormatExtra
UT_Vector3T< T > SYSclamp(const UT_Vector3T< T > &v, const UT_Vector3T< T > &min, const UT_Vector3T< T > &max)
Definition: UT_Vector3.h:1057
RE_CacheVersion getVersion() const
long long int64
Definition: SYS_Types.h:116
void operator=(const RE_TextureHolder &tex)
Definition: RE_TextureMap.h:68
bool getMipMap() const
IMG_FileAlphaInfo myAlphaInfo
bool operator!=(const RE_Texture *tex) const
Definition: RE_TextureMap.h:84
RE_Texture * operator*() const
Definition: RE_TextureMap.h:57
void fillRGBFromSingle(bool fill_rgb_mono)
bool isValid() const
Definition: RE_TextureMap.h:56
GLdouble t
Definition: glad.h:2397
RE_TextureHolder(const RE_TextureHolder &src)
Definition: RE_TextureMap.h:44
GT_API const UT_StringHolder version
RE_TextureHolder(RE_Texture *tex)
Definition: RE_TextureMap.h:38
IMG_FileAlphaInfo
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
void setOpaqueAlpha(bool opaque_alpha)
bool hasValidSourceResolve() const
fpreal64 fpreal
Definition: SYS_Types.h:277
virtual int getSourceOpID() const
IMG_FileAlphaInfo getAlphaDetails() const
File options for manipulating image data on load or save. This class allows you to modify the incomin...
Definition: IMG_FileParms.h:38
PXL_ColorSpace
Definition: PXL_Common.h:72
bool operator==(const RE_Texture *tex) const
Definition: RE_TextureMap.h:82
void setVersion(RE_CacheVersion v)
GLuint texture
Definition: glcorearb.h:415
bool allowsPreload() const
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
Simple class for a mutli-integer cache tag.
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
void setPendingLoad(bool pend)
GLboolean r
Definition: glcorearb.h:1222
int setSource(int source) override
const char * getMapName() const
RE_TextureCompress
type
Definition: core.h:1059
Definition: format.h:895
bool hasTaggedTexture() const
GLenum src
Definition: glcorearb.h:1793